summaryrefslogtreecommitdiffstats
path: root/src/input/pvsprivinputd.cpp
diff options
context:
space:
mode:
authorSebastien Braun2010-10-06 15:19:44 +0200
committerSebastien Braun2010-10-07 09:30:24 +0200
commitaeeb2b034f50ad2938b1c32507f20070a26c1849 (patch)
tree54dfca1f4dff7202c012adfc26070cfc3c9a222c /src/input/pvsprivinputd.cpp
parentInstall PolKit policy file (diff)
downloadpvs-aeeb2b034f50ad2938b1c32507f20070a26c1849.tar.gz
pvs-aeeb2b034f50ad2938b1c32507f20070a26c1849.tar.xz
pvs-aeeb2b034f50ad2938b1c32507f20070a26c1849.zip
Implement log targets for pvsprivinputd
Also: - add command line options and usage message. - change --no-fork to --daemon to bring it in line with the pvs daemon.
Diffstat (limited to 'src/input/pvsprivinputd.cpp')
-rw-r--r--src/input/pvsprivinputd.cpp116
1 files changed, 110 insertions, 6 deletions
diff --git a/src/input/pvsprivinputd.cpp b/src/input/pvsprivinputd.cpp
index 2f19d90..e6ae155 100644
--- a/src/input/pvsprivinputd.cpp
+++ b/src/input/pvsprivinputd.cpp
@@ -22,8 +22,10 @@
#include <unistd.h>
#include <cerrno>
#include <cstdlib>
+#include <cstdio>
#include <cstring>
#include <QtDebug>
+#include <QFileInfo>
#include <QSettings>
#include <QCoreApplication>
#include <QStringList>
@@ -32,6 +34,7 @@
#include "pvsPrivInputSocket.h"
#include "pvsPrivInputHandler.h"
#include "pvsPrivInputSignalHandler.h"
+#include "pvsSyslog.h"
using namespace std;
@@ -42,6 +45,9 @@ using namespace std;
QByteArray socketPath;
int signalFds[2];
+QTextStream qout(stdout, QIODevice::WriteOnly);
+QTextStream qerr(stderr, QIODevice::WriteOnly);
+
static void unlinkSocket()
{
if(!socketPath.isNull())
@@ -55,13 +61,70 @@ static void onCaughtSignal(int signum)
pvsPrivInputSendMessage(signalFds[0], &signum, sizeof(signum), 0);
}
+static void usage()
+{
+ qout << QObject::tr(
+ "Usage: %1 [--help|-h] [--daemon|-d] [--log=<Logger>|-l<Logger>]\n"
+ "\n"
+ "Options:\n"
+ " --help, -h Show this message\n"
+ " --daemon, -d Run in background\n"
+ " --log=<Logger>,\n"
+ " -l<Logger> Redirect all output to <Logger>\n"
+ " valid values are:\n"
+ " - any file name\n"
+ " - `syslog' to redirect output to the system log\n"
+ " - `null' to discard output (default)\n"
+ " (without quotes)\n"
+ "\n"
+ "Signals:\n"
+ " SIGINT, SIGTERM (or press Ctrl+C when run in foreground)\n"
+ " Quit\n"
+ " SIGHUP Reload configuration and cached user data\n").arg(qApp->arguments().at(0)) << flush;
+}
+
int main(int argc, char** argv)
{
QCoreApplication app(argc, argv);
app.setApplicationName("pvsprivinputd");
app.setOrganizationName("openslx");
- bool no_fork = app.arguments().contains("--no-fork", Qt::CaseInsensitive);
+ bool no_fork = true;
+ QString logTarget = "null";
+
+ foreach(QString arg, app.arguments().mid(1))
+ {
+ if(arg == "--daemon" || arg == "-d")
+ {
+ no_fork = false;
+ }
+ else if(arg.startsWith("-l"))
+ {
+ logTarget = arg.mid(2);
+ }
+ else if(arg.startsWith("--log="))
+ {
+ logTarget = arg.mid(6);
+ }
+ else if(arg == "--help" || arg == "-h")
+ {
+ usage();
+ return EXIT_SUCCESS;
+ }
+ else
+ {
+ qout << "Unexpected argument: " << arg << endl;
+ usage();
+ return EXIT_FAILURE;
+ }
+ }
+
+ if(logTarget != "null" && logTarget != "syslog")
+ {
+ logTarget = QFileInfo(logTarget).absoluteFilePath();
+ qout << "Writing log to " << logTarget << endl;
+ }
+
if(!no_fork)
{
pid_t pid;
@@ -118,6 +181,9 @@ int main(int argc, char** argv)
exit(EXIT_FAILURE);
}
+ // Install our main object
+ PVSPrivInputHandler handler(sock);
+
// set up signal handling:
if(socketpair(AF_UNIX, SOCK_DGRAM, 0, signalFds) < 0)
{
@@ -135,24 +201,62 @@ int main(int argc, char** argv)
signal(SIGTERM, onCaughtSignal);
signal(SIGHUP, onCaughtSignal);
+ PVSLogRedirector* logRedir = 0;
+ QSocketNotifier* logNotif = 0;
// Our setup is complete.
if(!no_fork)
{
// Reopen standard file descriptors:
freopen("/dev/null", "r", stdin);
- freopen("/dev/null", "w", stderr);
- freopen("/dev/null", "w", stdout);
- }
- // Install our main object
- PVSPrivInputHandler handler(sock);
+ if(logTarget == "null")
+ {
+ freopen("/dev/null", "w", stdout);
+ freopen("/dev/null", "w", stderr);
+ }
+ else if(logTarget == "syslog")
+ {
+ logRedir = new PVSSyslogRedirector();
+ }
+ else
+ {
+ logRedir = new PVSLogfileRedirector(logTarget);
+ }
+
+ if(logRedir)
+ {
+ int logFds[2];
+ if(socketpair(AF_UNIX, SOCK_STREAM, 0, logFds) < 0)
+ {
+ qWarning("Could not open a socket pair: %s", strerror(errno));
+ return EXIT_FAILURE;
+ }
+
+ logNotif = new QSocketNotifier(logFds[1], QSocketNotifier::Read);
+ QObject::connect(logNotif, SIGNAL(activated(int)), logRedir, SLOT(inputAvailableOn(int)));
+
+ // redirect stdout and stderr:
+ dup2(logFds[0], 1);
+ dup2(logFds[0], 2);
+ }
+ }
atexit(unlinkSocket);
+ qout << "PVS Privileged Input Daemon initialization complete. Entering main loop." << endl;
+
// and run the main loop.
int ret = app.exec();
+ if(logRedir)
+ {
+ delete logNotif;
+ delete logRedir;
+ }
PVSCheckPrivileges::deleteInstance();
+
+ qout << "PVS Privileged Input Daemon deinitialization complete. Exiting." << endl;
+
return ret;
}