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