475727944de604f543daee3649a494e175ccfaba
[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 || defined _GL_ALREADY_INCLUDING_SIGNAL_H || (defined _SIGNAL_H && !defined __SIZEOF_PTHREAD_MUTEX_T)
24 /* Special invocation convention:
25    - Inside glibc header files.
26    - On glibc systems we have a sequence of nested includes
27      <signal.h> -> <ucontext.h> -> <signal.h>.
28      In this situation, the functions are not yet declared, therefore we cannot
29      provide the C++ aliases.
30    - On glibc systems with GCC 4.3 we have a sequence of nested includes
31      <csignal> -> </usr/include/signal.h> -> <sys/ucontext.h> -> <signal.h>.
32      In this situation, some of the functions are not yet declared, therefore
33      we cannot provide the C++ aliases.  */
34
35 # @INCLUDE_NEXT@ @NEXT_SIGNAL_H@
36
37 #else
38 /* Normal invocation convention.  */
39
40 #ifndef _@GUARD_PREFIX@_SIGNAL_H
41
42 #define _GL_ALREADY_INCLUDING_SIGNAL_H
43
44 /* Define pid_t, uid_t.
45    Also, mingw defines sigset_t not in <signal.h>, but in <sys/types.h>.
46    On Solaris 10, <signal.h> includes <sys/types.h>, which eventually includes
47    us; so include <sys/types.h> now, before the second inclusion guard.  */
48 #include <sys/types.h>
49
50 /* The include_next requires a split double-inclusion guard.  */
51 #@INCLUDE_NEXT@ @NEXT_SIGNAL_H@
52
53 #undef _GL_ALREADY_INCLUDING_SIGNAL_H
54
55 #ifndef _@GUARD_PREFIX@_SIGNAL_H
56 #define _@GUARD_PREFIX@_SIGNAL_H
57
58 /* MacOS X 10.3, FreeBSD 6.4, OpenBSD 3.8, OSF/1 4.0, Solaris 2.6 declare
59    pthread_sigmask in <pthread.h>, not in <signal.h>.
60    But avoid namespace pollution on glibc systems.*/
61 #if (@GNULIB_PTHREAD_SIGMASK@ || defined GNULIB_POSIXCHECK) \
62     && ((defined __APPLE__ && defined __MACH__) || defined __FreeBSD__ || defined __OpenBSD__ || defined __osf__ || defined __sun) \
63     && ! defined __GLIBC__
64 # include <pthread.h>
65 #endif
66
67 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here.  */
68
69 /* The definition of _GL_ARG_NONNULL is copied here.  */
70
71 /* The definition of _GL_WARN_ON_USE is copied here.  */
72
73 /* On AIX, sig_atomic_t already includes volatile.  C99 requires that
74    'volatile sig_atomic_t' ignore the extra modifier, but C89 did not.
75    Hence, redefine this to a non-volatile type as needed.  */
76 #if ! @HAVE_TYPE_VOLATILE_SIG_ATOMIC_T@
77 # if !GNULIB_defined_sig_atomic_t
78 typedef int rpl_sig_atomic_t;
79 #  undef sig_atomic_t
80 #  define sig_atomic_t rpl_sig_atomic_t
81 #  define GNULIB_defined_sig_atomic_t 1
82 # endif
83 #endif
84
85 /* A set or mask of signals.  */
86 #if !@HAVE_SIGSET_T@
87 # if !GNULIB_defined_sigset_t
88 typedef unsigned int sigset_t;
89 #  define GNULIB_defined_sigset_t 1
90 # endif
91 #endif
92
93 /* Define sighandler_t, the type of signal handlers.  A GNU extension.  */
94 #if !@HAVE_SIGHANDLER_T@
95 # ifdef __cplusplus
96 extern "C" {
97 # endif
98 # if !GNULIB_defined_sighandler_t
99 typedef void (*sighandler_t) (int);
100 #  define GNULIB_defined_sighandler_t 1
101 # endif
102 # ifdef __cplusplus
103 }
104 # endif
105 #endif
106
107
108 #if @GNULIB_SIGNAL_H_SIGPIPE@
109 # ifndef SIGPIPE
110 /* Define SIGPIPE to a value that does not overlap with other signals.  */
111 #  define SIGPIPE 13
112 #  define GNULIB_defined_SIGPIPE 1
113 /* To actually use SIGPIPE, you also need the gnulib modules 'sigprocmask',
114    'write', 'stdio'.  */
115 # endif
116 #endif
117
118
119 /* Maximum signal number + 1.  */
120 #ifndef NSIG
121 # if defined __TANDEM
122 #  define NSIG 32
123 # endif
124 #endif
125
126
127 #if @GNULIB_PTHREAD_SIGMASK@
128 # if !@HAVE_PTHREAD_SIGMASK@
129 _GL_FUNCDECL_SYS (pthread_sigmask, int,
130                   (int how, const sigset_t *new_mask, sigset_t *old_mask));
131 # endif
132 _GL_CXXALIAS_SYS (pthread_sigmask, int,
133                   (int how, const sigset_t *new_mask, sigset_t *old_mask));
134 _GL_CXXALIASWARN (pthread_sigmask);
135 #elif defined GNULIB_POSIXCHECK
136 # undef pthread_sigmask
137 # if HAVE_RAW_DECL_PTHREAD_SIGMASK
138 _GL_WARN_ON_USE (pthread_sigmask, "pthread_sigmask is not portable - "
139                  "use gnulib module pthread_sigmask for portability");
140 # endif
141 #endif
142
143
144 #if @GNULIB_SIGPROCMASK@
145 # if !@HAVE_POSIX_SIGNALBLOCKING@
146
147 /* Maximum signal number + 1.  */
148 #  ifndef NSIG
149 #   define NSIG 32
150 #  endif
151
152 /* This code supports only 32 signals.  */
153 #  if !GNULIB_defined_verify_NSIG_constraint
154 typedef int verify_NSIG_constraint[NSIG <= 32 ? 1 : -1];
155 #   define GNULIB_defined_verify_NSIG_constraint 1
156 #  endif
157
158 # endif
159
160 /* Test whether a given signal is contained in a signal set.  */
161 # if @HAVE_POSIX_SIGNALBLOCKING@
162 /* This function is defined as a macro on MacOS X.  */
163 #  if defined __cplusplus && defined GNULIB_NAMESPACE
164 #   undef sigismember
165 #  endif
166 # else
167 _GL_FUNCDECL_SYS (sigismember, int, (const sigset_t *set, int sig)
168                                     _GL_ARG_NONNULL ((1)));
169 # endif
170 _GL_CXXALIAS_SYS (sigismember, int, (const sigset_t *set, int sig));
171 _GL_CXXALIASWARN (sigismember);
172
173 /* Initialize a signal set to the empty set.  */
174 # if @HAVE_POSIX_SIGNALBLOCKING@
175 /* This function is defined as a macro on MacOS X.  */
176 #  if defined __cplusplus && defined GNULIB_NAMESPACE
177 #   undef sigemptyset
178 #  endif
179 # else
180 _GL_FUNCDECL_SYS (sigemptyset, int, (sigset_t *set) _GL_ARG_NONNULL ((1)));
181 # endif
182 _GL_CXXALIAS_SYS (sigemptyset, int, (sigset_t *set));
183 _GL_CXXALIASWARN (sigemptyset);
184
185 /* Add a signal to a signal set.  */
186 # if @HAVE_POSIX_SIGNALBLOCKING@
187 /* This function is defined as a macro on MacOS X.  */
188 #  if defined __cplusplus && defined GNULIB_NAMESPACE
189 #   undef sigaddset
190 #  endif
191 # else
192 _GL_FUNCDECL_SYS (sigaddset, int, (sigset_t *set, int sig)
193                                   _GL_ARG_NONNULL ((1)));
194 # endif
195 _GL_CXXALIAS_SYS (sigaddset, int, (sigset_t *set, int sig));
196 _GL_CXXALIASWARN (sigaddset);
197
198 /* Remove a signal from a signal set.  */
199 # if @HAVE_POSIX_SIGNALBLOCKING@
200 /* This function is defined as a macro on MacOS X.  */
201 #  if defined __cplusplus && defined GNULIB_NAMESPACE
202 #   undef sigdelset
203 #  endif
204 # else
205 _GL_FUNCDECL_SYS (sigdelset, int, (sigset_t *set, int sig)
206                                   _GL_ARG_NONNULL ((1)));
207 # endif
208 _GL_CXXALIAS_SYS (sigdelset, int, (sigset_t *set, int sig));
209 _GL_CXXALIASWARN (sigdelset);
210
211 /* Fill a signal set with all possible signals.  */
212 # if @HAVE_POSIX_SIGNALBLOCKING@
213 /* This function is defined as a macro on MacOS X.  */
214 #  if defined __cplusplus && defined GNULIB_NAMESPACE
215 #   undef sigfillset
216 #  endif
217 # else
218 _GL_FUNCDECL_SYS (sigfillset, int, (sigset_t *set) _GL_ARG_NONNULL ((1)));
219 # endif
220 _GL_CXXALIAS_SYS (sigfillset, int, (sigset_t *set));
221 _GL_CXXALIASWARN (sigfillset);
222
223 /* Return the set of those blocked signals that are pending.  */
224 # if !@HAVE_POSIX_SIGNALBLOCKING@
225 _GL_FUNCDECL_SYS (sigpending, int, (sigset_t *set) _GL_ARG_NONNULL ((1)));
226 # endif
227 _GL_CXXALIAS_SYS (sigpending, int, (sigset_t *set));
228 _GL_CXXALIASWARN (sigpending);
229
230 /* If OLD_SET is not NULL, put the current set of blocked signals in *OLD_SET.
231    Then, if SET is not NULL, affect the current set of blocked signals by
232    combining it with *SET as indicated in OPERATION.
233    In this implementation, you are not allowed to change a signal handler
234    while the signal is blocked.  */
235 # if !@HAVE_POSIX_SIGNALBLOCKING@
236 #  define SIG_BLOCK   0  /* blocked_set = blocked_set | *set; */
237 #  define SIG_SETMASK 1  /* blocked_set = *set; */
238 #  define SIG_UNBLOCK 2  /* blocked_set = blocked_set & ~*set; */
239 _GL_FUNCDECL_SYS (sigprocmask, int,
240                   (int operation, const sigset_t *set, sigset_t *old_set));
241 # endif
242 _GL_CXXALIAS_SYS (sigprocmask, int,
243                   (int operation, const sigset_t *set, sigset_t *old_set));
244 _GL_CXXALIASWARN (sigprocmask);
245
246 /* Install the handler FUNC for signal SIG, and return the previous
247    handler.  */
248 # ifdef __cplusplus
249 extern "C" {
250 # endif
251 # if !GNULIB_defined_function_taking_int_returning_void_t
252 typedef void (*_gl_function_taking_int_returning_void_t) (int);
253 #  define GNULIB_defined_function_taking_int_returning_void_t 1
254 # endif
255 # ifdef __cplusplus
256 }
257 # endif
258 # if !@HAVE_POSIX_SIGNALBLOCKING@
259 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
260 #   define signal rpl_signal
261 #  endif
262 _GL_FUNCDECL_RPL (signal, _gl_function_taking_int_returning_void_t,
263                   (int sig, _gl_function_taking_int_returning_void_t func));
264 _GL_CXXALIAS_RPL (signal, _gl_function_taking_int_returning_void_t,
265                   (int sig, _gl_function_taking_int_returning_void_t func));
266 # else
267 _GL_CXXALIAS_SYS (signal, _gl_function_taking_int_returning_void_t,
268                   (int sig, _gl_function_taking_int_returning_void_t func));
269 # endif
270 _GL_CXXALIASWARN (signal);
271
272 /* Raise signal SIG.  */
273 # if !@HAVE_POSIX_SIGNALBLOCKING@ && GNULIB_defined_SIGPIPE
274 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
275 #   undef raise
276 #   define raise rpl_raise
277 #  endif
278 _GL_FUNCDECL_RPL (raise, int, (int sig));
279 _GL_CXXALIAS_RPL (raise, int, (int sig));
280 # else
281 _GL_CXXALIAS_SYS (raise, int, (int sig));
282 # endif
283 _GL_CXXALIASWARN (raise);
284
285 #elif defined GNULIB_POSIXCHECK
286 # undef sigaddset
287 # if HAVE_RAW_DECL_SIGADDSET
288 _GL_WARN_ON_USE (sigaddset, "sigaddset is unportable - "
289                  "use the gnulib module sigprocmask for portability");
290 # endif
291 # undef sigdelset
292 # if HAVE_RAW_DECL_SIGDELSET
293 _GL_WARN_ON_USE (sigdelset, "sigdelset is unportable - "
294                  "use the gnulib module sigprocmask for portability");
295 # endif
296 # undef sigemptyset
297 # if HAVE_RAW_DECL_SIGEMPTYSET
298 _GL_WARN_ON_USE (sigemptyset, "sigemptyset is unportable - "
299                  "use the gnulib module sigprocmask for portability");
300 # endif
301 # undef sigfillset
302 # if HAVE_RAW_DECL_SIGFILLSET
303 _GL_WARN_ON_USE (sigfillset, "sigfillset is unportable - "
304                  "use the gnulib module sigprocmask for portability");
305 # endif
306 # undef sigismember
307 # if HAVE_RAW_DECL_SIGISMEMBER
308 _GL_WARN_ON_USE (sigismember, "sigismember is unportable - "
309                  "use the gnulib module sigprocmask for portability");
310 # endif
311 # undef sigpending
312 # if HAVE_RAW_DECL_SIGPENDING
313 _GL_WARN_ON_USE (sigpending, "sigpending is unportable - "
314                  "use the gnulib module sigprocmask for portability");
315 # endif
316 # undef sigprocmask
317 # if HAVE_RAW_DECL_SIGPROCMASK
318 _GL_WARN_ON_USE (sigprocmask, "sigprocmask is unportable - "
319                  "use the gnulib module sigprocmask for portability");
320 # endif
321 #endif /* @GNULIB_SIGPROCMASK@ */
322
323
324 #if @GNULIB_SIGACTION@
325 # if !@HAVE_SIGACTION@
326
327 #  if !@HAVE_SIGINFO_T@
328
329 #   if !GNULIB_defined_siginfo_types
330
331 /* Present to allow compilation, but unsupported by gnulib.  */
332 union sigval
333 {
334   int sival_int;
335   void *sival_ptr;
336 };
337
338 /* Present to allow compilation, but unsupported by gnulib.  */
339 struct siginfo_t
340 {
341   int si_signo;
342   int si_code;
343   int si_errno;
344   pid_t si_pid;
345   uid_t si_uid;
346   void *si_addr;
347   int si_status;
348   long si_band;
349   union sigval si_value;
350 };
351 typedef struct siginfo_t siginfo_t;
352
353 #    define GNULIB_defined_siginfo_types 1
354 #   endif
355
356 #  endif /* !@HAVE_SIGINFO_T@ */
357
358 /* We assume that platforms which lack the sigaction() function also lack
359    the 'struct sigaction' type, and vice versa.  */
360
361 #  if !GNULIB_defined_struct_sigaction
362
363 struct sigaction
364 {
365   union
366   {
367     void (*_sa_handler) (int);
368     /* Present to allow compilation, but unsupported by gnulib.  POSIX
369        says that implementations may, but not must, make sa_sigaction
370        overlap with sa_handler, but we know of no implementation where
371        they do not overlap.  */
372     void (*_sa_sigaction) (int, siginfo_t *, void *);
373   } _sa_func;
374   sigset_t sa_mask;
375   /* Not all POSIX flags are supported.  */
376   int sa_flags;
377 };
378 #   define sa_handler _sa_func._sa_handler
379 #   define sa_sigaction _sa_func._sa_sigaction
380 /* Unsupported flags are not present.  */
381 #   define SA_RESETHAND 1
382 #   define SA_NODEFER 2
383 #   define SA_RESTART 4
384
385 #   define GNULIB_defined_struct_sigaction 1
386 #  endif
387
388 _GL_FUNCDECL_SYS (sigaction, int, (int, const struct sigaction *restrict,
389                                    struct sigaction *restrict));
390
391 # elif !@HAVE_STRUCT_SIGACTION_SA_SIGACTION@
392
393 #  define sa_sigaction sa_handler
394
395 # endif /* !@HAVE_SIGACTION@, !@HAVE_STRUCT_SIGACTION_SA_SIGACTION@ */
396
397 _GL_CXXALIAS_SYS (sigaction, int, (int, const struct sigaction *restrict,
398                                    struct sigaction *restrict));
399 _GL_CXXALIASWARN (sigaction);
400
401 #elif defined GNULIB_POSIXCHECK
402 # undef sigaction
403 # if HAVE_RAW_DECL_SIGACTION
404 _GL_WARN_ON_USE (sigaction, "sigaction is unportable - "
405                  "use the gnulib module sigaction for portability");
406 # endif
407 #endif
408
409 /* Some systems don't have SA_NODEFER.  */
410 #ifndef SA_NODEFER
411 # define SA_NODEFER 0
412 #endif
413
414
415 #endif /* _@GUARD_PREFIX@_SIGNAL_H */
416 #endif /* _@GUARD_PREFIX@_SIGNAL_H */
417 #endif