summaryrefslogtreecommitdiffstats
path: root/src/input/pvsprivinputd.cpp
diff options
context:
space:
mode:
authorSebastien Braun2010-10-06 15:08:03 +0200
committerSebastien Braun2010-10-06 15:08:03 +0200
commit6afd810d1954018791456a7cebca0d275c50ed95 (patch)
tree64d7b3e560dde3049c6c3056387c14f10066c666 /src/input/pvsprivinputd.cpp
parentImplement administratively configured user privileges (diff)
downloadpvs-6afd810d1954018791456a7cebca0d275c50ed95.tar.gz
pvs-6afd810d1954018791456a7cebca0d275c50ed95.tar.xz
pvs-6afd810d1954018791456a7cebca0d275c50ed95.zip
Refactor signal handling in pvsprivinputd
Only use one socketpair and delegate the actual decision of what to do when a specific signal is received to a special object.
Diffstat (limited to 'src/input/pvsprivinputd.cpp')
-rw-r--r--src/input/pvsprivinputd.cpp40
1 files changed, 24 insertions, 16 deletions
diff --git a/src/input/pvsprivinputd.cpp b/src/input/pvsprivinputd.cpp
index 3a5e1a8..361bf9f 100644
--- a/src/input/pvsprivinputd.cpp
+++ b/src/input/pvsprivinputd.cpp
@@ -30,6 +30,7 @@
#include <QSocketNotifier>
#include "pvsPrivInputSocket.h"
#include "pvsPrivInputHandler.h"
+#include "pvsPrivInputSignalHandler.h"
using namespace std;
@@ -38,7 +39,7 @@ using namespace std;
#endif
QByteArray socketPath;
-int sigintFds[2];
+int signalFds[2];
static void unlinkSocket()
{
@@ -48,12 +49,9 @@ static void unlinkSocket()
}
}
-static void onInterrupt(int)
+static void onCaughtSignal(int signum)
{
- char buf[] = { '!' };
- char msg[] = "SIGINT caught. Quitting.\n";
- write(sigintFds[0], buf, 1);
- write(1, msg, sizeof(msg)-1);
+ pvsPrivInputSendMessage(signalFds[0], &signum, sizeof(signum), 0);
}
int main(int argc, char** argv)
@@ -103,28 +101,38 @@ int main(int argc, char** argv)
// the socket (access control is handled differently):
umask(0);
+ // Store the socket path before the signal handler is installed so it does not risk segfaulting
+ // due to bad timing.
+ socketPath = pvsPrivInputGetSocketAddress().toLocal8Bit();
+
// Ignore SIGPIPE. Connection errors are handled internally.
// According to signal(2), the only error is that the signal number
// is invalid. This cannot happen.
signal(SIGPIPE, SIG_IGN);
- // set up signal handling:
- if(socketpair(AF_UNIX, SOCK_STREAM, 0, sigintFds) < 0)
+ // Create our socket:
+ int sock = pvsPrivInputMakeServerSocket();
+ if(sock < 0)
{
- qCritical("Could not set up signal handling. Giving up.");
exit(EXIT_FAILURE);
}
- socketPath = pvsPrivInputGetSocketAddress().toLocal8Bit();
- QSocketNotifier sigintWatcher(sigintFds[1], QSocketNotifier::Read);
- QObject::connect(&sigintWatcher, SIGNAL(activated(int)), &app, SLOT(quit()));
- signal(SIGINT, onInterrupt);
- // Create our socket:
- int sock = pvsPrivInputMakeServerSocket();
- if(sock < 0)
+ // set up signal handling:
+ if(socketpair(AF_UNIX, SOCK_DGRAM, 0, signalFds) < 0)
{
+ qCritical("Could not set up signal handling. Giving up.");
exit(EXIT_FAILURE);
}
+ PVSPrivInputSignalHandler sigHandler;
+ QSocketNotifier sigintWatcher(signalFds[1], QSocketNotifier::Read);
+ sigHandler.allowUnauthenticatedKilling(!pvsPrivInputEnableReceiveCredentials(signalFds[1]));
+ QObject::connect(&sigintWatcher, SIGNAL(activated(int)), &sigHandler, SLOT(signalReceived(int)));
+ QObject::connect(&sigHandler, SIGNAL(terminate()), &app, SLOT(quit()));
+ QObject::connect(&sigHandler, SIGNAL(reloadConfiguration()), PVSCheckPrivileges::instance(), SLOT(updateCachedUserDatabase()));
+ QObject::connect(&sigHandler, SIGNAL(reloadConfiguration()), PVSCheckPrivileges::instance(), SLOT(rereadConfiguration()));
+ signal(SIGINT, onCaughtSignal);
+ signal(SIGTERM, onCaughtSignal);
+ signal(SIGHUP, onCaughtSignal);
// Our setup is complete.
if(!no_fork)