NEWS.stable: log cherry-pick [79aac72]->[cc1a9b7] canonicalize-lgpl: use common idiom
[gnulib.git] / lib / signal.in.h
1 /* A GNU-like <signal.h>.
2
3    Copyright (C) 2006-2011 Free Software Foundation, Inc.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #if __GNUC__ >= 3
19 @PRAGMA_SYSTEM_HEADER@
20 #endif
21 @PRAGMA_COLUMNS@
22
23 #if defined __need_sig_atomic_t || defined __need_sigset_t
24 /* Special invocation convention inside glibc header files.  */
25
26 # @INCLUDE_NEXT@ @NEXT_SIGNAL_H@
27
28 #else
29 /* Normal invocation convention.  */
30
31 #ifndef _GL_SIGNAL_H
32
33 /* The include_next requires a split double-inclusion guard.  */
34 #@INCLUDE_NEXT@ @NEXT_SIGNAL_H@
35
36 #ifndef _GL_SIGNAL_H
37 #define _GL_SIGNAL_H
38
39 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here.  */
40
41 /* The definition of _GL_ARG_NONNULL is copied here.  */
42
43 /* The definition of _GL_WARN_ON_USE is copied here.  */
44
45 /* Define pid_t, uid_t.
46    Also, mingw defines sigset_t not in <signal.h>, but in <sys/types.h>.  */
47 #include <sys/types.h>
48
49 /* On AIX, sig_atomic_t already includes volatile.  C99 requires that
50    'volatile sig_atomic_t' ignore the extra modifier, but C89 did not.
51    Hence, redefine this to a non-volatile type as needed.  */
52 #if ! @HAVE_TYPE_VOLATILE_SIG_ATOMIC_T@
53 # if !GNULIB_defined_sig_atomic_t
54 typedef int rpl_sig_atomic_t;
55 #  undef sig_atomic_t
56 #  define sig_atomic_t rpl_sig_atomic_t
57 #  define GNULIB_defined_sig_atomic_t 1
58 # endif
59 #endif
60
61 /* A set or mask of signals.  */
62 #if !@HAVE_SIGSET_T@
63 # if !GNULIB_defined_sigset_t
64 typedef unsigned int sigset_t;
65 #  define GNULIB_defined_sigset_t 1
66 # endif
67 #endif
68
69 /* Define sighandler_t, the type of signal handlers.  A GNU extension.  */
70 #if !@HAVE_SIGHANDLER_T@
71 # ifdef __cplusplus
72 extern "C" {
73 # endif
74 # if !GNULIB_defined_sighandler_t
75 typedef void (*sighandler_t) (int);
76 #  define GNULIB_defined_sighandler_t 1
77 # endif
78 # ifdef __cplusplus
79 }
80 # endif
81 #endif
82
83
84 #if @GNULIB_SIGNAL_H_SIGPIPE@
85 # ifndef SIGPIPE
86 /* Define SIGPIPE to a value that does not overlap with other signals.  */
87 #  define SIGPIPE 13
88 #  define GNULIB_defined_SIGPIPE 1
89 /* To actually use SIGPIPE, you also need the gnulib modules 'sigprocmask',
90    'write', 'stdio'.  */
91 # endif
92 #endif
93
94
95 /* Maximum signal number + 1.  */
96 #ifndef NSIG
97 # if defined __TANDEM
98 #  define NSIG 32
99 # endif
100 #endif
101
102
103 #if @GNULIB_SIGPROCMASK@
104 # if !@HAVE_POSIX_SIGNALBLOCKING@
105
106 /* Maximum signal number + 1.  */
107 #  ifndef NSIG
108 #   define NSIG 32
109 #  endif
110
111 /* This code supports only 32 signals.  */
112 #  if !GNULIB_defined_verify_NSIG_constraint
113 typedef int verify_NSIG_constraint[NSIG <= 32 ? 1 : -1];
114 #   define GNULIB_defined_verify_NSIG_constraint 1
115 #  endif
116
117 # endif
118
119 /* Test whether a given signal is contained in a signal set.  */
120 # if @HAVE_POSIX_SIGNALBLOCKING@
121 /* This function is defined as a macro on MacOS X.  */
122 #  if defined __cplusplus && defined GNULIB_NAMESPACE
123 #   undef sigismember
124 #  endif
125 # else
126 _GL_FUNCDECL_SYS (sigismember, int, (const sigset_t *set, int sig)
127                                     _GL_ARG_NONNULL ((1)));
128 # endif
129 _GL_CXXALIAS_SYS (sigismember, int, (const sigset_t *set, int sig));
130 _GL_CXXALIASWARN (sigismember);
131
132 /* Initialize a signal set to the empty set.  */
133 # if @HAVE_POSIX_SIGNALBLOCKING@
134 /* This function is defined as a macro on MacOS X.  */
135 #  if defined __cplusplus && defined GNULIB_NAMESPACE
136 #   undef sigemptyset
137 #  endif
138 # else
139 _GL_FUNCDECL_SYS (sigemptyset, int, (sigset_t *set) _GL_ARG_NONNULL ((1)));
140 # endif
141 _GL_CXXALIAS_SYS (sigemptyset, int, (sigset_t *set));
142 _GL_CXXALIASWARN (sigemptyset);
143
144 /* Add a signal to a signal set.  */
145 # if @HAVE_POSIX_SIGNALBLOCKING@
146 /* This function is defined as a macro on MacOS X.  */
147 #  if defined __cplusplus && defined GNULIB_NAMESPACE
148 #   undef sigaddset
149 #  endif
150 # else
151 _GL_FUNCDECL_SYS (sigaddset, int, (sigset_t *set, int sig)
152                                   _GL_ARG_NONNULL ((1)));
153 # endif
154 _GL_CXXALIAS_SYS (sigaddset, int, (sigset_t *set, int sig));
155 _GL_CXXALIASWARN (sigaddset);
156
157 /* Remove a signal from a signal set.  */
158 # if @HAVE_POSIX_SIGNALBLOCKING@
159 /* This function is defined as a macro on MacOS X.  */
160 #  if defined __cplusplus && defined GNULIB_NAMESPACE
161 #   undef sigdelset
162 #  endif
163 # else
164 _GL_FUNCDECL_SYS (sigdelset, int, (sigset_t *set, int sig)
165                                   _GL_ARG_NONNULL ((1)));
166 # endif
167 _GL_CXXALIAS_SYS (sigdelset, int, (sigset_t *set, int sig));
168 _GL_CXXALIASWARN (sigdelset);
169
170 /* Fill a signal set with all possible signals.  */
171 # if @HAVE_POSIX_SIGNALBLOCKING@
172 /* This function is defined as a macro on MacOS X.  */
173 #  if defined __cplusplus && defined GNULIB_NAMESPACE
174 #   undef sigfillset
175 #  endif
176 # else
177 _GL_FUNCDECL_SYS (sigfillset, int, (sigset_t *set) _GL_ARG_NONNULL ((1)));
178 # endif
179 _GL_CXXALIAS_SYS (sigfillset, int, (sigset_t *set));
180 _GL_CXXALIASWARN (sigfillset);
181
182 /* Return the set of those blocked signals that are pending.  */
183 # if !@HAVE_POSIX_SIGNALBLOCKING@
184 _GL_FUNCDECL_SYS (sigpending, int, (sigset_t *set) _GL_ARG_NONNULL ((1)));
185 # endif
186 _GL_CXXALIAS_SYS (sigpending, int, (sigset_t *set));
187 _GL_CXXALIASWARN (sigpending);
188
189 /* If OLD_SET is not NULL, put the current set of blocked signals in *OLD_SET.
190    Then, if SET is not NULL, affect the current set of blocked signals by
191    combining it with *SET as indicated in OPERATION.
192    In this implementation, you are not allowed to change a signal handler
193    while the signal is blocked.  */
194 # if !@HAVE_POSIX_SIGNALBLOCKING@
195 #  define SIG_BLOCK   0  /* blocked_set = blocked_set | *set; */
196 #  define SIG_SETMASK 1  /* blocked_set = *set; */
197 #  define SIG_UNBLOCK 2  /* blocked_set = blocked_set & ~*set; */
198 _GL_FUNCDECL_SYS (sigprocmask, int,
199                   (int operation, const sigset_t *set, sigset_t *old_set));
200 # endif
201 _GL_CXXALIAS_SYS (sigprocmask, int,
202                   (int operation, const sigset_t *set, sigset_t *old_set));
203 _GL_CXXALIASWARN (sigprocmask);
204
205 /* Install the handler FUNC for signal SIG, and return the previous
206    handler.  */
207 # ifdef __cplusplus
208 extern "C" {
209 # endif
210 # if !GNULIB_defined_function_taking_int_returning_void_t
211 typedef void (*_gl_function_taking_int_returning_void_t) (int);
212 #  define GNULIB_defined_function_taking_int_returning_void_t 1
213 # endif
214 # ifdef __cplusplus
215 }
216 # endif
217 # if !@HAVE_POSIX_SIGNALBLOCKING@
218 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
219 #   define signal rpl_signal
220 #  endif
221 _GL_FUNCDECL_RPL (signal, _gl_function_taking_int_returning_void_t,
222                   (int sig, _gl_function_taking_int_returning_void_t func));
223 _GL_CXXALIAS_RPL (signal, _gl_function_taking_int_returning_void_t,
224                   (int sig, _gl_function_taking_int_returning_void_t func));
225 # else
226 _GL_CXXALIAS_SYS (signal, _gl_function_taking_int_returning_void_t,
227                   (int sig, _gl_function_taking_int_returning_void_t func));
228 # endif
229 _GL_CXXALIASWARN (signal);
230
231 /* Raise signal SIG.  */
232 # if !@HAVE_POSIX_SIGNALBLOCKING@ && GNULIB_defined_SIGPIPE
233 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
234 #   undef raise
235 #   define raise rpl_raise
236 #  endif
237 _GL_FUNCDECL_RPL (raise, int, (int sig));
238 _GL_CXXALIAS_RPL (raise, int, (int sig));
239 # else
240 _GL_CXXALIAS_SYS (raise, int, (int sig));
241 # endif
242 _GL_CXXALIASWARN (raise);
243
244 #elif defined GNULIB_POSIXCHECK
245 # undef sigaddset
246 # if HAVE_RAW_DECL_SIGADDSET
247 _GL_WARN_ON_USE (sigaddset, "sigaddset is unportable - "
248                  "use the gnulib module sigprocmask for portability");
249 # endif
250 # undef sigdelset
251 # if HAVE_RAW_DECL_SIGDELSET
252 _GL_WARN_ON_USE (sigdelset, "sigdelset is unportable - "
253                  "use the gnulib module sigprocmask for portability");
254 # endif
255 # undef sigemptyset
256 # if HAVE_RAW_DECL_SIGEMPTYSET
257 _GL_WARN_ON_USE (sigemptyset, "sigemptyset is unportable - "
258                  "use the gnulib module sigprocmask for portability");
259 # endif
260 # undef sigfillset
261 # if HAVE_RAW_DECL_SIGFILLSET
262 _GL_WARN_ON_USE (sigfillset, "sigfillset is unportable - "
263                  "use the gnulib module sigprocmask for portability");
264 # endif
265 # undef sigismember
266 # if HAVE_RAW_DECL_SIGISMEMBER
267 _GL_WARN_ON_USE (sigismember, "sigismember is unportable - "
268                  "use the gnulib module sigprocmask for portability");
269 # endif
270 # undef sigpending
271 # if HAVE_RAW_DECL_SIGPENDING
272 _GL_WARN_ON_USE (sigpending, "sigpending is unportable - "
273                  "use the gnulib module sigprocmask for portability");
274 # endif
275 # undef sigprocmask
276 # if HAVE_RAW_DECL_SIGPROCMASK
277 _GL_WARN_ON_USE (sigprocmask, "sigprocmask is unportable - "
278                  "use the gnulib module sigprocmask for portability");
279 # endif
280 #endif /* @GNULIB_SIGPROCMASK@ */
281
282
283 #if @GNULIB_SIGACTION@
284 # if !@HAVE_SIGACTION@
285
286 #  if !@HAVE_SIGINFO_T@
287
288 #   if !GNULIB_defined_siginfo_types
289
290 /* Present to allow compilation, but unsupported by gnulib.  */
291 union sigval
292 {
293   int sival_int;
294   void *sival_ptr;
295 };
296
297 /* Present to allow compilation, but unsupported by gnulib.  */
298 struct siginfo_t
299 {
300   int si_signo;
301   int si_code;
302   int si_errno;
303   pid_t si_pid;
304   uid_t si_uid;
305   void *si_addr;
306   int si_status;
307   long si_band;
308   union sigval si_value;
309 };
310 typedef struct siginfo_t siginfo_t;
311
312 #    define GNULIB_defined_siginfo_types 1
313 #   endif
314
315 #  endif /* !@HAVE_SIGINFO_T@ */
316
317 /* We assume that platforms which lack the sigaction() function also lack
318    the 'struct sigaction' type, and vice versa.  */
319
320 #  if !GNULIB_defined_struct_sigaction
321
322 struct sigaction
323 {
324   union
325   {
326     void (*_sa_handler) (int);
327     /* Present to allow compilation, but unsupported by gnulib.  POSIX
328        says that implementations may, but not must, make sa_sigaction
329        overlap with sa_handler, but we know of no implementation where
330        they do not overlap.  */
331     void (*_sa_sigaction) (int, siginfo_t *, void *);
332   } _sa_func;
333   sigset_t sa_mask;
334   /* Not all POSIX flags are supported.  */
335   int sa_flags;
336 };
337 #   define sa_handler _sa_func._sa_handler
338 #   define sa_sigaction _sa_func._sa_sigaction
339 /* Unsupported flags are not present.  */
340 #   define SA_RESETHAND 1
341 #   define SA_NODEFER 2
342 #   define SA_RESTART 4
343
344 #   define GNULIB_defined_struct_sigaction 1
345 #  endif
346
347 _GL_FUNCDECL_SYS (sigaction, int, (int, const struct sigaction *restrict,
348                                    struct sigaction *restrict));
349
350 # elif !@HAVE_STRUCT_SIGACTION_SA_SIGACTION@
351
352 #  define sa_sigaction sa_handler
353
354 # endif /* !@HAVE_SIGACTION@, !@HAVE_STRUCT_SIGACTION_SA_SIGACTION@ */
355
356 _GL_CXXALIAS_SYS (sigaction, int, (int, const struct sigaction *restrict,
357                                    struct sigaction *restrict));
358 _GL_CXXALIASWARN (sigaction);
359
360 #elif defined GNULIB_POSIXCHECK
361 # undef sigaction
362 # if HAVE_RAW_DECL_SIGACTION
363 _GL_WARN_ON_USE (sigaction, "sigaction is unportable - "
364                  "use the gnulib module sigaction for portability");
365 # endif
366 #endif
367
368 /* Some systems don't have SA_NODEFER.  */
369 #ifndef SA_NODEFER
370 # define SA_NODEFER 0
371 #endif
372
373
374 #endif /* _GL_SIGNAL_H */
375 #endif /* _GL_SIGNAL_H */
376 #endif