00ebd2cc8fcbea77546bcb00fdaabe424508fc9d
[libasyncio] / i2ncommon / signalfunc.hpp
1 /*
2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
4
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
7
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
13
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
16
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
19 */
20 /** @file
21  * @brief provides wrapper and tools for signal related stuff.
22  *
23  *
24  * @copyright © Copyright 2007-2008 by Intra2net AG
25  * @contact opensource@intra2net.com
26  *
27  * @bug 
28  * Although most stuff should work under most POSIX like systems;
29  * some funcs might be very linux related.
30  * (But at least we use that lib currently under linux only.)
31  */
32
33 #ifndef _I2N_SIGNALFUNC_HPP_
34 #define _I2N_SIGNALFUNC_HPP_
35
36 #include <vector>
37
38 // with pain in the stomach; the following include was added....:
39 // (since these includes a lot of #define's... what we usually don't want to have in C++ headers...)
40 #include <sys/types.h>
41
42 extern "C"
43 {
44
45 struct siginfo;
46
47 }
48
49
50 namespace I2n
51 {
52 namespace SystemTools
53 {
54
55
56 namespace Detail
57 {
58
59 /**
60  * base class for internal implementation classes.
61  */
62 class SObject
63 {
64     public:
65         virtual ~SObject() {}
66 }; // eo class SObject
67
68 } // eo namespace Detail
69
70 /**
71  * @brief representation of system signal.
72  *
73  * This struct also provides constants for the usual system signals; so it is not necessary to include signal.h
74  * for obtaining the constants.
75  *
76  * Due to an appropriate cast operator instances of the class can be used in all places where
77  * a plain signal (i.e. an int) is expected.
78  */
79 struct Signal
80 {
81     static const int VOID;
82 /// @cond
83
84 #define SIG(s) static const int s
85     SIG(HUP);   SIG(INT);   SIG(QUIT);  SIG(ILL);   SIG(TRAP);
86     SIG(ABRT);  SIG(IOT);   SIG(BUS);   SIG(FPE);   SIG(KILL);
87     SIG(USR1);  SIG(SEGV);  SIG(USR2);  SIG(PIPE);  SIG(ALRM);
88     SIG(TERM);  SIG(STKFLT); SIG(CLD);  SIG(CHLD);  SIG(CONT);  SIG(STOP);
89     SIG(TSTP);  SIG(TTIN);  SIG(TTOU);  SIG(URG);   SIG(XCPU);
90     SIG(XFSZ);  SIG(VTALRM); SIG(PROF); SIG(WINCH); SIG(POLL);
91     SIG(IO);    SIG(PWR);   SIG(SYS);
92 #undef SIG
93
94 /// @endcond
95
96     static int RT(int num=0);
97
98     int Value;
99
100     Signal(int signal) : Value(signal) {}
101
102     operator int () const { return Value; }
103 }; // eo struct Signal
104
105
106 /**
107  * @brief representation of signal codes.
108  *
109  * This struct provides constants for signal codes; so it is not necessary to include signal.h
110  * for obtaining the constants.
111  *
112  * Due to an appropriate cast operator instances of the class can be used in all places where
113  * a plain signal code (i.e. an int) is expected.
114  */
115 struct SignalCode
116 {
117
118
119 /// @cond
120 #define CODE(name) static const int name
121     CODE(USER);    CODE(QUEUE); CODE(TIMER); CODE(MESGQ);
122     CODE(ASYNCIO);
123 #undef SIGIO
124     CODE(SIGIO);
125 /// @endcond
126
127     /**
128      * @brief contains the codes for signal SIGILL
129      */
130     struct ILL
131     {
132         /// @cond
133         CODE(ILLOPC); CODE(ILLOPN); CODE(ILLADR); CODE(ILLTRP);
134         CODE(PRVOPC); CODE(PRVREG); CODE(COPROC); CODE(BADSTK);
135         /// @endcond
136     }; // eo struct ILL;
137
138
139     /**
140      * @brief contains the codes for signal SIGFPE
141      */
142     struct FPE
143     {
144         /// @cond
145         CODE(INTDIV); CODE(INTOVF); CODE(FLTDIV); CODE(FLTOVF);
146         CODE(FLTUND); CODE(FLTRES); CODE(FLTINV); CODE(FLTSUB);
147         /// @endcond
148     }; // eo struct FPE
149
150
151     /**
152      * @brief contains the codes for signal SIGSEGV
153      */
154     struct SEGV
155     {
156         /// @cond
157         CODE(MAPERR); CODE(ACCERR);
158         /// @endcond
159     }; // eo struct SEGV
160
161
162     /**
163      * @brief contains the codes for signal SIGBUS
164      */
165     struct BUS
166     {
167         /// @cond
168         CODE(ADRALN); CODE(ADRERR); CODE(OBJERR);
169         /// @endcond
170     }; // eo struct BUS
171
172
173     /**
174      * @brief contains the codes for signal SIGTRAP
175      */
176     struct TRAP
177     {
178         /// @cond
179         CODE(BRKPT); CODE(TRACE);
180         /// @endcond
181     }; // eo struct TRAP
182
183
184     /**
185      * @brief contains the codes for signal SIGCHLD
186      */
187     struct CHLD
188     {
189         /// @cond
190         CODE(EXITED); CODE(KILLED); CODE(DUMPED); CODE(TRAPPED);
191         CODE(STOPPED); CODE(CONTINUED);
192         /// @endcond
193     }; // eo struct CHLD
194
195
196     /**
197      * @brief contains the codes for signal SIGPOLL
198      */
199     struct POLL
200     {
201         /// @cond
202         CODE(IN); CODE(OUT); CODE(MSG); CODE(ERR); CODE(PRI); CODE(HUP);
203         /// @endcond
204     }; // eo strcut POLL
205
206 #undef CODE
207
208     int TheCode;
209
210     SignalCode(int code) : TheCode(code) {}
211
212     operator int () const { return TheCode; }
213 }; // eo SignalCode
214
215
216
217
218 /**
219  * @brief helper for blocking a (or some) signal(s) during an operation.
220  *
221  * This class blocks the given signals when constructed and resets the original block mask
222  * when destructed.
223  */
224 class ScopedSignalBlocker
225 {
226     public:
227         ScopedSignalBlocker(Signal sig);
228         ScopedSignalBlocker(Signal sig1, Signal sig2);
229         ScopedSignalBlocker(Signal sig1, Signal sig2, Signal sig3);
230         ScopedSignalBlocker(const std::vector<Signal>& sigs);
231         virtual ~ScopedSignalBlocker();
232
233     private:
234         Detail::SObject* Implementation;
235
236 }; // eo class ScopedSignalBlocker
237
238
239
240
241 bool install_signal_handler(
242     Signal sig,
243     void(*handler)(int)
244 );
245
246 bool install_signal_handler(
247     Signal sig,
248     void(*handler)(int,struct siginfo*,void*)
249 );
250
251 bool ignore_signal(Signal sig);
252 bool install_default_signal_handler(Signal sig);
253
254 bool restore_signal_handler(Signal sig);
255
256
257 bool send_signal( pid_t pid, Signal signal);
258
259
260 } // eo namespace SysTools
261 } // eo namespace I2n
262
263 #endif