2 // C++ Implementation: sighandler
7 // Author: Stefan Bühler <stbuehler@web.de>, (C) 2007
9 // Copyright: See COPYING file that comes with this distribution
13 #include "sighandler.h"
26 #include <QSocketNotifier>
27 #include <QCoreApplication>
32 static void sig2pipe(int sig
) {
33 write(pipefd
[1], (char*) &sig
, sizeof(sig
));
36 SigHandler::SigHandler(bool quitOnSignal
)
37 : quitOnSignal(quitOnSignal
) {
38 if (pipefd
[1] != 0) return;
39 if (pipe(pipefd
) != 0) return;
40 fcntl(pipefd
[0], F_SETFD
, FD_CLOEXEC
);
41 fcntl(pipefd
[1], F_SETFD
, FD_CLOEXEC
);
43 signal(SIGTERM
, sig2pipe
);
44 signal(SIGINT
, sig2pipe
);
46 QSocketNotifier
*r
= new QSocketNotifier(pipefd
[0], QSocketNotifier::Read
, this);
47 connect(r
, SIGNAL(activated(int)), this, SLOT(pipe_rcv()));
50 SigHandler::~SigHandler() {
53 pipefd
[0] = pipefd
[1] = 0;
55 void SigHandler::pipe_rcv() {
57 if (read(pipefd
[0], &signum
, sizeof(signum
))) {
58 std::cout
<< "Received signal: " << signum
<< std::endl
;
59 emit
gotSignal(signum
);
65 QCoreApplication::quit();