[1f4adec] | 1 | /* |
---|
| 2 | |
---|
| 3 | Copyright 2013, 2015 Adam Reichold |
---|
| 4 | |
---|
| 5 | This file is part of qpdfview. |
---|
| 6 | |
---|
| 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. |
---|
| 11 | |
---|
| 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. |
---|
| 16 | |
---|
| 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/>. |
---|
| 19 | |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #include "signalhandler.h" |
---|
| 23 | |
---|
| 24 | #include <signal.h> |
---|
| 25 | #include <sys/socket.h> |
---|
| 26 | #include <unistd.h> |
---|
| 27 | |
---|
| 28 | #include <QSocketNotifier> |
---|
| 29 | |
---|
| 30 | namespace qpdfview |
---|
| 31 | { |
---|
| 32 | |
---|
| 33 | int SignalHandler::s_sockets[2]; |
---|
| 34 | |
---|
| 35 | bool SignalHandler::prepareSignals() |
---|
| 36 | { |
---|
| 37 | if(socketpair(AF_UNIX, SOCK_STREAM, 0, s_sockets) != 0) |
---|
| 38 | { |
---|
| 39 | return false; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | struct sigaction sigAction; |
---|
| 43 | |
---|
| 44 | sigAction.sa_handler = SignalHandler::handleSignals; |
---|
| 45 | sigemptyset(&sigAction.sa_mask); |
---|
| 46 | sigAction.sa_flags = SA_RESTART; |
---|
| 47 | |
---|
| 48 | if(sigaction(SIGINT, &sigAction, 0) != 0) |
---|
| 49 | { |
---|
| 50 | close(s_sockets[0]); |
---|
| 51 | close(s_sockets[1]); |
---|
| 52 | |
---|
| 53 | return false; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | if(sigaction(SIGTERM, &sigAction, 0) != 0) |
---|
| 57 | { |
---|
| 58 | close(s_sockets[0]); |
---|
| 59 | close(s_sockets[1]); |
---|
| 60 | |
---|
| 61 | return false; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | return true; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | SignalHandler::SignalHandler(QObject* parent) : QObject(parent), |
---|
| 68 | m_socketNotifier(0) |
---|
| 69 | { |
---|
| 70 | m_socketNotifier = new QSocketNotifier(s_sockets[1], QSocketNotifier::Read, this); |
---|
| 71 | connect(m_socketNotifier, SIGNAL(activated(int)), SLOT(on_socketNotifier_activated())); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | void SignalHandler::on_socketNotifier_activated() |
---|
| 75 | { |
---|
| 76 | m_socketNotifier->setEnabled(false); |
---|
| 77 | |
---|
| 78 | int sigNumber = 0; |
---|
| 79 | Q_UNUSED(read(s_sockets[1], &sigNumber, sizeof(int))); |
---|
| 80 | |
---|
| 81 | switch(sigNumber) |
---|
| 82 | { |
---|
| 83 | case SIGINT: |
---|
| 84 | emit sigIntReceived(); |
---|
| 85 | break; |
---|
| 86 | case SIGTERM: |
---|
| 87 | emit sigTermReceived(); |
---|
| 88 | break; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | m_socketNotifier->setEnabled(true); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | void SignalHandler::handleSignals(int sigNumber) |
---|
| 95 | { |
---|
| 96 | Q_UNUSED(write(s_sockets[0], &sigNumber, sizeof(int))); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | } // qpdfview |
---|