source: terepaima/terepaima-0.4.16/sources/signalhandler.cpp

desarrollostretch
Last change on this file was 1f4adec, checked in by aosorio <aosorio@…>, 8 years ago

Agregado proyecto base, esto luego del dh_make -f

  • Property mode set to 100644
File size: 2.2 KB
Line 
1/*
2
3Copyright 2013, 2015 Adam Reichold
4
5This file is part of qpdfview.
6
7qpdfview is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation, either version 2 of the License, or
10(at your option) any later version.
11
12qpdfview is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along 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
30namespace qpdfview
31{
32
33int SignalHandler::s_sockets[2];
34
35bool 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
67SignalHandler::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
74void 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
94void SignalHandler::handleSignals(int sigNumber)
95{
96    Q_UNUSED(write(s_sockets[0], &sigNumber, sizeof(int)));
97}
98
99} // qpdfview
Note: See TracBrowser for help on using the repository browser.