prefer (X ? 1 : -1) when converting from boolean (1,0) to int (1,-1)
[gnulib.git] / lib / signal.in.h
1 /* A GNU-like <signal.h>.
2
3    Copyright (C) 2006-2010 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 typedef int rpl_sig_atomic_t;
54 # undef sig_atomic_t
55 # define sig_atomic_t rpl_sig_atomic_t
56 #endif
57
58 /* A set or mask of signals.  */
59 #if !@HAVE_SIGSET_T@
60 typedef unsigned int sigset_t;
61 #endif
62
63
64 #if @GNULIB_SIGNAL_H_SIGPIPE@
65 # ifndef SIGPIPE
66 /* Define SIGPIPE to a value that does not overlap with other signals.  */
67 #  define SIGPIPE 13
68 #  define GNULIB_defined_SIGPIPE 1
69 /* To actually use SIGPIPE, you also need the gnulib modules 'sigprocmask',
70    'write', 'stdio'.  */
71 # endif
72 #endif
73
74
75 #if @GNULIB_SIGPROCMASK@
76 # if !@HAVE_POSIX_SIGNALBLOCKING@
77
78 /* Maximum signal number + 1.  */
79 #  ifndef NSIG
80 #   define NSIG 32
81 #  endif
82
83 /* This code supports only 32 signals.  */
84 typedef int verify_NSIG_constraint[NSIG <= 32 ? 1 : -1];
85
86 # endif
87
88 /* Test whether a given signal is contained in a signal set.  */
89 # if @HAVE_POSIX_SIGNALBLOCKING@
90 /* This function is defined as a macro on MacOS X.  */
91 #  if defined __cplusplus && defined GNULIB_NAMESPACE
92 #   undef sigismember
93 #  endif
94 # else
95 _GL_FUNCDECL_SYS (sigismember, int, (const sigset_t *set, int sig)
96                                     _GL_ARG_NONNULL ((1)));
97 # endif
98 _GL_CXXALIAS_SYS (sigismember, int, (const sigset_t *set, int sig));
99 _GL_CXXALIASWARN (sigismember);
100
101 /* Initialize a signal set to the empty set.  */
102 # if @HAVE_POSIX_SIGNALBLOCKING@
103 /* This function is defined as a macro on MacOS X.  */
104 #  if defined __cplusplus && defined GNULIB_NAMESPACE
105 #   undef sigemptyset
106 #  endif
107 # else
108 _GL_FUNCDECL_SYS (sigemptyset, int, (sigset_t *set) _GL_ARG_NONNULL ((1)));
109 # endif
110 _GL_CXXALIAS_SYS (sigemptyset, int, (sigset_t *set));
111 _GL_CXXALIASWARN (sigemptyset);
112
113 /* Add a signal to a signal set.  */
114 # if @HAVE_POSIX_SIGNALBLOCKING@
115 /* This function is defined as a macro on MacOS X.  */
116 #  if defined __cplusplus && defined GNULIB_NAMESPACE
117 #   undef sigaddset
118 #  endif
119 # else
120 _GL_FUNCDECL_SYS (sigaddset, int, (sigset_t *set, int sig)
121                                   _GL_ARG_NONNULL ((1)));
122 # endif
123 _GL_CXXALIAS_SYS (sigaddset, int, (sigset_t *set, int sig));
124 _GL_CXXALIASWARN (sigaddset);
125
126 /* Remove a signal from a signal set.  */
127 # if @HAVE_POSIX_SIGNALBLOCKING@
128 /* This function is defined as a macro on MacOS X.  */
129 #  if defined __cplusplus && defined GNULIB_NAMESPACE
130 #   undef sigdelset
131 #  endif
132 # else
133 _GL_FUNCDECL_SYS (sigdelset, int, (sigset_t *set, int sig)
134                                   _GL_ARG_NONNULL ((1)));
135 # endif
136 _GL_CXXALIAS_SYS (sigdelset, int, (sigset_t *set, int sig));
137 _GL_CXXALIASWARN (sigdelset);
138
139 /* Fill a signal set with all possible signals.  */
140 # if @HAVE_POSIX_SIGNALBLOCKING@
141 /* This function is defined as a macro on MacOS X.  */
142 #  if defined __cplusplus && defined GNULIB_NAMESPACE
143 #   undef sigfillset
144 #  endif
145 # else
146 _GL_FUNCDECL_SYS (sigfillset, int, (sigset_t *set) _GL_ARG_NONNULL ((1)));
147 # endif
148 _GL_CXXALIAS_SYS (sigfillset, int, (sigset_t *set));
149 _GL_CXXALIASWARN (sigfillset);
150
151 /* Return the set of those blocked signals that are pending.  */
152 # if !@HAVE_POSIX_SIGNALBLOCKING@
153 _GL_FUNCDECL_SYS (sigpending, int, (sigset_t *set) _GL_ARG_NONNULL ((1)));
154 # endif
155 _GL_CXXALIAS_SYS (sigpending, int, (sigset_t *set));
156 _GL_CXXALIASWARN (sigpending);
157
158 /* If OLD_SET is not NULL, put the current set of blocked signals in *OLD_SET.
159    Then, if SET is not NULL, affect the current set of blocked signals by
160    combining it with *SET as indicated in OPERATION.
161    In this implementation, you are not allowed to change a signal handler
162    while the signal is blocked.  */
163 # if !@HAVE_POSIX_SIGNALBLOCKING@
164 #  define SIG_BLOCK   0  /* blocked_set = blocked_set | *set; */
165 #  define SIG_SETMASK 1  /* blocked_set = *set; */
166 #  define SIG_UNBLOCK 2  /* blocked_set = blocked_set & ~*set; */
167 _GL_FUNCDECL_SYS (sigprocmask, int,
168                   (int operation, const sigset_t *set, sigset_t *old_set));
169 # endif
170 _GL_CXXALIAS_SYS (sigprocmask, int,
171                   (int operation, const sigset_t *set, sigset_t *old_set));
172 _GL_CXXALIASWARN (sigprocmask);
173
174 /* Install the handler FUNC for signal SIG, and return the previous
175    handler.  */
176 # ifdef __cplusplus
177 extern "C" {
178 # endif
179 typedef void (*_gl_function_taking_int_returning_void_t) (int);
180 # ifdef __cplusplus
181 }
182 # endif
183 # if !@HAVE_POSIX_SIGNALBLOCKING@
184 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
185 #   define signal rpl_signal
186 #  endif
187 _GL_FUNCDECL_RPL (signal, _gl_function_taking_int_returning_void_t,
188                   (int sig, _gl_function_taking_int_returning_void_t func));
189 _GL_CXXALIAS_RPL (signal, _gl_function_taking_int_returning_void_t,
190                   (int sig, _gl_function_taking_int_returning_void_t func));
191 # else
192 _GL_CXXALIAS_SYS (signal, _gl_function_taking_int_returning_void_t,
193                   (int sig, _gl_function_taking_int_returning_void_t func));
194 # endif
195 _GL_CXXALIASWARN (signal);
196
197 /* Raise signal SIG.  */
198 # if !@HAVE_POSIX_SIGNALBLOCKING@ && GNULIB_defined_SIGPIPE
199 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
200 #   undef raise
201 #   define raise rpl_raise
202 #  endif
203 _GL_FUNCDECL_RPL (raise, int, (int sig));
204 _GL_CXXALIAS_RPL (raise, int, (int sig));
205 # else
206 _GL_CXXALIAS_SYS (raise, int, (int sig));
207 # endif
208 _GL_CXXALIASWARN (raise);
209
210 #elif defined GNULIB_POSIXCHECK
211 # undef sigaddset
212 # if HAVE_RAW_DECL_SIGADDSET
213 _GL_WARN_ON_USE (sigaddset, "sigaddset is unportable - "
214                  "use the gnulib module sigprocmask for portability");
215 # endif
216 # undef sigdelset
217 # if HAVE_RAW_DECL_SIGDELSET
218 _GL_WARN_ON_USE (sigdelset, "sigdelset is unportable - "
219                  "use the gnulib module sigprocmask for portability");
220 # endif
221 # undef sigemptyset
222 # if HAVE_RAW_DECL_SIGEMPTYSET
223 _GL_WARN_ON_USE (sigemptyset, "sigemptyset is unportable - "
224                  "use the gnulib module sigprocmask for portability");
225 # endif
226 # undef sigfillset
227 # if HAVE_RAW_DECL_SIGFILLSET
228 _GL_WARN_ON_USE (sigfillset, "sigfillset is unportable - "
229                  "use the gnulib module sigprocmask for portability");
230 # endif
231 # undef sigismember
232 # if HAVE_RAW_DECL_SIGISMEMBER
233 _GL_WARN_ON_USE (sigismember, "sigismember is unportable - "
234                  "use the gnulib module sigprocmask for portability");
235 # endif
236 # undef sigpending
237 # if HAVE_RAW_DECL_SIGPENDING
238 _GL_WARN_ON_USE (sigpending, "sigpending is unportable - "
239                  "use the gnulib module sigprocmask for portability");
240 # endif
241 # undef sigprocmask
242 # if HAVE_RAW_DECL_SIGPROCMASK
243 _GL_WARN_ON_USE (sigprocmask, "sigprocmask is unportable - "
244                  "use the gnulib module sigprocmask for portability");
245 # endif
246 #endif /* @GNULIB_SIGPROCMASK@ */
247
248
249 #if @GNULIB_SIGACTION@
250 # if !@HAVE_SIGACTION@
251
252 #  if !@HAVE_SIGINFO_T@
253 /* Present to allow compilation, but unsupported by gnulib.  */
254 union sigval
255 {
256   int sival_int;
257   void *sival_ptr;
258 };
259
260 /* Present to allow compilation, but unsupported by gnulib.  */
261 struct siginfo_t
262 {
263   int si_signo;
264   int si_code;
265   int si_errno;
266   pid_t si_pid;
267   uid_t si_uid;
268   void *si_addr;
269   int si_status;
270   long si_band;
271   union sigval si_value;
272 };
273 typedef struct siginfo_t siginfo_t;
274 #  endif /* !@HAVE_SIGINFO_T@ */
275
276 /* We assume that platforms which lack the sigaction() function also lack
277    the 'struct sigaction' type, and vice versa.  */
278
279 struct sigaction
280 {
281   union
282   {
283     void (*_sa_handler) (int);
284     /* Present to allow compilation, but unsupported by gnulib.  POSIX
285        says that implementations may, but not must, make sa_sigaction
286        overlap with sa_handler, but we know of no implementation where
287        they do not overlap.  */
288     void (*_sa_sigaction) (int, siginfo_t *, void *);
289   } _sa_func;
290   sigset_t sa_mask;
291   /* Not all POSIX flags are supported.  */
292   int sa_flags;
293 };
294 #  define sa_handler _sa_func._sa_handler
295 #  define sa_sigaction _sa_func._sa_sigaction
296 /* Unsupported flags are not present.  */
297 #  define SA_RESETHAND 1
298 #  define SA_NODEFER 2
299 #  define SA_RESTART 4
300
301 _GL_FUNCDECL_SYS (sigaction, int, (int, const struct sigaction *restrict,
302                                    struct sigaction *restrict));
303
304 # elif !@HAVE_STRUCT_SIGACTION_SA_SIGACTION@
305
306 #  define sa_sigaction sa_handler
307
308 # endif /* !@HAVE_SIGACTION@, !@HAVE_STRUCT_SIGACTION_SA_SIGACTION@ */
309
310 _GL_CXXALIAS_SYS (sigaction, int, (int, const struct sigaction *restrict,
311                                    struct sigaction *restrict));
312 _GL_CXXALIASWARN (sigaction);
313
314 #elif defined GNULIB_POSIXCHECK
315 # undef sigaction
316 # if HAVE_RAW_DECL_SIGACTION
317 _GL_WARN_ON_USE (sigaction, "sigaction is unportable - "
318                  "use the gnulib module sigaction for portability");
319 # endif
320 #endif
321
322 /* Some systems don't have SA_NODEFER.  */
323 #ifndef SA_NODEFER
324 # define SA_NODEFER 0
325 #endif
326
327
328 #endif /* _GL_SIGNAL_H */
329 #endif /* _GL_SIGNAL_H */
330 #endif