Lomiri
UnixSignalHandler.h
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  */
18 
19 #pragma once
20 
21 #include <QObject>
22 #include <QSocketNotifier>
23 
24 #include <functional>
25 
26 class UnixSignalHandler: public QObject {
27 Q_OBJECT
28 
29 public:
30  UnixSignalHandler(const std::function<void()>& f, QObject *parent = 0);
31  ~UnixSignalHandler() = default;
32 
33  static int setupUnixSignalHandlers();
34 
35 protected Q_SLOTS:
36  void handleSigHup();
37  void handleSigInt();
38  void handleSigTerm();
39 
40 protected:
41  static void hupSignalHandler(int unused);
42  static void intSignalHandler(int unused);
43  static void termSignalHandler(int unused);
44 
45  static int sighupFd[2];
46  static int sigintFd[2];
47  static int sigtermFd[2];
48 
49  std::function<void()> m_func;
50 
51  QSocketNotifier *m_socketNotifierHup;
52  QSocketNotifier *m_socketNotifierInt;
53  QSocketNotifier *m_socketNotifierTerm;
54 };