1 /*
2 * This file is part of signon
3 *
4 * Copyright (C) 2009-2010 Nokia Corporation.
5 * Copyright (C) 2012-2016 Canonical Ltd.
6 *
7 * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * version 2.1 as published by the Free Software Foundation.
12 *
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 */
23
24 extern "C" {
25 #include <signal.h>
26 #include <stdlib.h>
27 #include <stddef.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <sys/poll.h>
32 #include <syslog.h>
33 }
34
35 #include "debug.h"
36 #include "remotepluginprocess.h"
37
38 #include <QDebug>
39
40 using namespace RemotePluginProcessNS;
41
42 RemotePluginProcess *process = NULL;
43
messageHandler(QtMsgType type,const QMessageLogContext & context,const QString & msg)44 void messageHandler(QtMsgType type, const QMessageLogContext &context,
45 const QString & msg)
46 {
47 Q_UNUSED(context);
48
49 if (debugLevel < 2) {
50 if (type == QtDebugMsg) return;
51 if (debugLevel < 1 && type == QtWarningMsg) return;
52 }
53
54 int priority;
55 switch (type) {
56 case QtWarningMsg: priority = LOG_WARNING; break;
57 case QtCriticalMsg: priority = LOG_CRIT; break;
58 case QtFatalMsg: priority = LOG_EMERG; break;
59 case QtDebugMsg:
60 /* fall through */
61 default: priority = LOG_INFO; break;
62 }
63
64 syslog(priority, "%s", msg.toUtf8().constData());
65 }
66
main(int argc,char * argv[])67 int main(int argc, char *argv[])
68 {
69 openlog(NULL, LOG_CONS | LOG_PID, LOG_DAEMON);
70 qInstallMessageHandler(messageHandler);
71 debugInit();
72
73 TRACE() << "handler:" << (void *)messageHandler;
74
75 #ifndef NO_SIGNON_USER
76 if (!::getuid()) {
77 BLAME() << argv[0] << " cannot be started with root priviledges!!!";
78 exit(2);
79 }
80 #endif
81
82 QCoreApplication app(argc, argv);
83
84 if (argc < 2) {
85 TRACE() << "Type of plugin is not specified";
86 exit(1);
87 }
88
89 QString type = app.arguments().at(1); TRACE() << type;
90
91 fcntl(fileno(stdin), F_SETFL, fcntl(fileno(stdin), F_GETFL, 0) | O_NONBLOCK);
92
93 process = RemotePluginProcess::createRemotePluginProcess(type, &app);
94
95 if (!process)
96 return 1;
97
98 fprintf(stdout, "process started");
99 fflush(stdout);
100
101 QObject::connect(process, SIGNAL(processStopped()), &app, SLOT(quit()));
102 int ret = app.exec();
103 closelog();
104 return ret;
105 }
106