3 Copyright 2013 Adam Reichold
5 This file is part of qpdfview.
7 qpdfview is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 2 of the License, or
10 (at your option) any later version.
12 qpdfview is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with qpdfview. If not, see <http://www.gnu.org/licenses/>.
22 #include "signalhandler.h"
25 #include <sys/socket.h>
28 #include <QSocketNotifier>
30 int SignalHandler::s_sockets[2];
32 bool SignalHandler::prepareSignals()
34 if(socketpair(AF_UNIX, SOCK_STREAM, 0, s_sockets) != 0)
39 struct sigaction sigAction;
41 sigAction.sa_handler = SignalHandler::handleSignals;
42 sigemptyset(&sigAction.sa_mask);
43 sigAction.sa_flags = SA_RESTART;
45 if(sigaction(SIGINT, &sigAction, 0) != 0)
53 if(sigaction(SIGTERM, &sigAction, 0) != 0)
64 SignalHandler::SignalHandler(QObject* parent) : QObject(parent),
67 m_socketNotifier = new QSocketNotifier(s_sockets[1], QSocketNotifier::Read, this);
68 connect(m_socketNotifier, SIGNAL(activated(int)), SLOT(on_socketNotifier_activated()));
71 void SignalHandler::on_socketNotifier_activated()
73 m_socketNotifier->setEnabled(false);
76 read(s_sockets[1], &sigNumber, sizeof(int));
81 emit sigIntReceived();
84 emit sigTermReceived();
88 m_socketNotifier->setEnabled(true);
91 void SignalHandler::handleSignals(int sigNumber)
93 write(s_sockets[0], &sigNumber, sizeof(int));