on 8.11+16.04.20160212-0ubuntu1 help sign in
unity8/tools/unix-signal-handler.cpp
141 lines 3.7 kB history raw
1/*
2 * Copyright (C) 2015 Canonical, Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3, as published
6 * by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Author: Marcus Tomlinson <marcus.tomlinson@canonical.com>
17 */
19#include "unix-signal-handler.h"
21#include <QDebug>
23#include <csignal>
24#include <sys/socket.h>
25#include <unistd.h>
27int UnixSignalHandler::sighupFd[2];
28int UnixSignalHandler::sigintFd[2];
29int UnixSignalHandler::sigtermFd[2];
31UnixSignalHandler::UnixSignalHandler(const std::function<void()>& f, QObject *parent)
32 : QObject(parent),
33 m_func(f)
35 if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sighupFd))
36 {
37 qFatal("Couldn't create HUP socketpair");
38 }
39 if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sigintFd))
40 {
41 qFatal("Couldn't create INT socketpair");
42 }
43 if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sigtermFd))
44 {
45 qFatal("Couldn't create TERM socketpair");
46 }
48 m_socketNotifierHup = new QSocketNotifier(sighupFd[1], QSocketNotifier::Read, this);
49 connect(m_socketNotifierHup, &QSocketNotifier::activated, this, &UnixSignalHandler::handleSigHup);
50 m_socketNotifierInt = new QSocketNotifier(sigintFd[1], QSocketNotifier::Read, this);
51 connect(m_socketNotifierInt, &QSocketNotifier::activated, this, &UnixSignalHandler::handleSigInt);
52 m_socketNotifierTerm = new QSocketNotifier(sigtermFd[1], QSocketNotifier::Read, this);
53 connect(m_socketNotifierTerm, &QSocketNotifier::activated, this, &UnixSignalHandler::handleSigTerm);
56void UnixSignalHandler::hupSignalHandler(int)
58 char a = 1;
59 ::write(sighupFd[0], &a, sizeof(a));
62void UnixSignalHandler::intSignalHandler(int)
64 char a = 1;
65 ::write(sigintFd[0], &a, sizeof(a));
68void UnixSignalHandler::termSignalHandler(int)
70 char a = 1;
71 ::write(sigtermFd[0], &a, sizeof(a));
74int UnixSignalHandler::setupUnixSignalHandlers()
76 struct sigaction sighup, sigint, sigterm;
78 sighup.sa_handler = UnixSignalHandler::hupSignalHandler;
79 sigemptyset(&sighup.sa_mask);
80 sighup.sa_flags = 0;
81 sighup.sa_flags |= SA_RESTART;
83 if (sigaction(SIGHUP, &sighup, 0) > 0)
84 {
85 return 1;
86 }
88 sigint.sa_handler = UnixSignalHandler::intSignalHandler;
89 sigemptyset(&sigint.sa_mask);
90 sigint.sa_flags = 0;
91 sigint.sa_flags |= SA_RESTART;
93 if (sigaction(SIGINT, &sigint, 0) > 0)
94 {
95 return 2;
96 }
98 sigterm.sa_handler = UnixSignalHandler::termSignalHandler;
99 sigemptyset(&sigterm.sa_mask);
100 sigterm.sa_flags |= SA_RESTART;
102 if (sigaction(SIGTERM, &sigterm, 0) > 0)
103 {
104 return 3;
105 }
107 return 0;
110void UnixSignalHandler::handleSigHup()
112 m_socketNotifierHup->setEnabled(false);
113 char tmp;
114 ::read(sighupFd[1], &tmp, sizeof(tmp));
116 m_func();
118 m_socketNotifierHup->setEnabled(true);
121void UnixSignalHandler::handleSigInt()
123 m_socketNotifierInt->setEnabled(false);
124 char tmp;
125 ::read(sigintFd[1], &tmp, sizeof(tmp));
127 m_func();
129 m_socketNotifierInt->setEnabled(true);
132void UnixSignalHandler::handleSigTerm()
134 m_socketNotifierTerm->setEnabled(false);
135 char tmp;
136 ::read(sigtermFd[1], &tmp, sizeof(tmp));
138 m_func();
140 m_socketNotifierTerm->setEnabled(true);