TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / external / process-cpp-minimal / include / core / posix / signal.h
1 /*
2  * Copyright © 2013 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Thomas Voß <thomas.voss@canonical.com>
17  */
18
19 #ifndef CORE_POSIX_SIGNAL_H_
20 #define CORE_POSIX_SIGNAL_H_
21
22 #include <core/posix/visibility.h>
23
24 #include <core/signal.h>
25
26 #include <signal.h>
27
28 #include <initializer_list>
29 #include <memory>
30
31 namespace core
32 {
33 namespace posix
34 {
35 /**
36  * @brief The Signal enum collects the most common POSIX signals.
37  */
38 enum class Signal
39 {
40     unknown = 0,
41     sig_hup = SIGHUP,
42     sig_int = SIGINT,
43     sig_quit = SIGQUIT,
44     sig_ill = SIGILL,
45     sig_abrt = SIGABRT,
46     sig_fpe = SIGFPE,
47     sig_kill = SIGKILL,
48     sig_segv = SIGSEGV,
49     sig_pipe = SIGPIPE,
50     sig_alrm = SIGALRM,
51     sig_term = SIGTERM,
52     sig_usr1 = SIGUSR1,
53     sig_usr2 = SIGUSR2,
54     sig_chld = SIGCHLD,
55     sig_cont = SIGCONT,
56     sig_stop = SIGSTOP,
57     sig_tstp = SIGTSTP,
58     sig_ttin = SIGTTIN,
59     sig_ttou = SIGTTOU
60 };
61
62 /**
63  * @brief The SignalTrap class encapsulates functionality to trap and handle signals.
64  */
65 class CORE_POSIX_DLL_PUBLIC SignalTrap
66 {
67 public:
68     SignalTrap(const SignalTrap&) = delete;
69     virtual ~SignalTrap() = default;
70
71     SignalTrap& operator=(const SignalTrap&) = delete;
72     bool operator==(const SignalTrap&) const = delete;
73
74     /**
75      * @brief Returns true if the given signal is trapped by this instance.
76      */
77     virtual bool has(Signal signal) = 0;
78
79     /**
80      * @brief Starts observation of incoming signals, relaying them via
81      * signal_raised(). The call blocks until stop is called.
82      */
83     virtual void run() = 0;
84
85     /**
86      * @brief Stops execution of the signal trap.
87      */
88     virtual void stop() = 0;
89
90     /**
91      * @brief Emitted whenever a trapped signal is raised by the operating system.
92      */
93     virtual core::Signal<Signal>& signal_raised() = 0;
94
95 protected:
96     SignalTrap() = default;
97 };
98
99 /**
100   * @brief Traps the specified signals for the entire process.
101   */
102 CORE_POSIX_DLL_PUBLIC
103 std::shared_ptr<SignalTrap> trap_signals_for_process(
104         std::initializer_list<core::posix::Signal> blocked_signals);
105
106 /**
107   * @brief Traps the specified signals for the current thread, and inherits
108   * the respective signal mask to all child-threads.
109   */
110 CORE_POSIX_DLL_PUBLIC
111 std::shared_ptr<SignalTrap> trap_signals_for_all_subsequent_threads(
112         std::initializer_list<core::posix::Signal> blocked_signals);
113
114 }
115 }
116
117 #endif