Support signal handling with SIGPIPE on native Windows platforms.
[gnulib.git] / lib / signal.in.h
1 /* A GNU-like <signal.h>.
2
3    Copyright (C) 2006-2008 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 @PRAGMA_SYSTEM_HEADER@
19
20 #if defined __need_sig_atomic_t || defined __need_sigset_t
21 /* Special invocation convention inside glibc header files.  */
22
23 # @INCLUDE_NEXT@ @NEXT_SIGNAL_H@
24
25 #else
26 /* Normal invocation convention.  */
27
28 #ifndef _GL_SIGNAL_H
29
30 /* The include_next requires a split double-inclusion guard.  */
31 #@INCLUDE_NEXT@ @NEXT_SIGNAL_H@
32
33 #ifndef _GL_SIGNAL_H
34 #define _GL_SIGNAL_H
35
36 /* The definition of GL_LINK_WARNING is copied here.  */
37
38 /* Define pid_t, uid_t.
39    Also, mingw defines sigset_t not in <signal.h>, but in <sys/types.h>.  */
40 #include <sys/types.h>
41
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45
46
47 #if @GNULIB_SIGNAL_H_SIGPIPE@
48 # ifndef SIGPIPE
49 /* Define SIGPIPE to a value that does not overlap with other signals.  */
50 #  define SIGPIPE 13
51 #  define GNULIB_defined_SIGPIPE 1
52 /* To actually use SIGPIPE, you also need the gnulib modules 'sigprocmask',
53    'write', 'stdio'.  */
54 # endif
55 #endif
56
57
58 #if !@HAVE_POSIX_SIGNALBLOCKING@
59
60 /* Maximum signal number + 1.  */
61 # ifndef NSIG
62 #  define NSIG 32
63 # endif
64
65 /* This code supports only 32 signals.  */
66 typedef int verify_NSIG_constraint[2 * (NSIG <= 32) - 1];
67
68 /* A set or mask of signals.  */
69 # if !@HAVE_SIGSET_T@
70 typedef unsigned int sigset_t;
71 # endif
72
73 /* Test whether a given signal is contained in a signal set.  */
74 extern int sigismember (const sigset_t *set, int sig);
75
76 /* Initialize a signal set to the empty set.  */
77 extern int sigemptyset (sigset_t *set);
78
79 /* Add a signal to a signal set.  */
80 extern int sigaddset (sigset_t *set, int sig);
81
82 /* Remove a signal from a signal set.  */
83 extern int sigdelset (sigset_t *set, int sig);
84
85 /* Fill a signal set with all possible signals.  */
86 extern int sigfillset (sigset_t *set);
87
88 /* Return the set of those blocked signals that are pending.  */
89 extern int sigpending (sigset_t *set);
90
91 /* If OLD_SET is not NULL, put the current set of blocked signals in *OLD_SET.
92    Then, if SET is not NULL, affect the current set of blocked signals by
93    combining it with *SET as indicated in OPERATION.
94    In this implementation, you are not allowed to change a signal handler
95    while the signal is blocked.  */
96 # define SIG_BLOCK   0  /* blocked_set = blocked_set | *set; */
97 # define SIG_SETMASK 1  /* blocked_set = *set; */
98 # define SIG_UNBLOCK 2  /* blocked_set = blocked_set & ~*set; */
99 extern int sigprocmask (int operation, const sigset_t *set, sigset_t *old_set);
100
101 # define signal rpl_signal
102 /* Install the handler FUNC for signal SIG, and return the previous
103    handler.  */
104 extern void (*signal (int sig, void (*func) (int))) (int);
105
106 # if GNULIB_defined_SIGPIPE
107
108 /* Raise signal SIG.  */
109 #  undef raise
110 #  define raise rpl_raise
111 extern int raise (int sig);
112
113 # endif
114
115 #endif /* !@HAVE_POSIX_SIGNALBLOCKING@ */
116
117
118 #if !@HAVE_SIGACTION@
119
120 # if !@HAVE_SIGINFO_T@
121 /* Present to allow compilation, but unsupported by gnulib.  */
122 union sigval
123 {
124   int sival_int;
125   void *sival_ptr;
126 };
127
128 /* Present to allow compilation, but unsupported by gnulib.  */
129 struct siginfo_t
130 {
131   int si_signo;
132   int si_code;
133   int si_errno;
134   pid_t si_pid;
135   uid_t si_uid;
136   void *si_addr;
137   int si_status;
138   long si_band;
139   union sigval si_value;
140 };
141 typedef struct siginfo_t siginfo_t;
142 # endif /* !@HAVE_SIGINFO_T@ */
143
144 /* We assume that platforms which lack the sigaction() function also lack
145    the 'struct sigaction' type, and vice versa.  */
146
147 struct sigaction
148 {
149   union
150   {
151     void (*_sa_handler) (int);
152     /* Present to allow compilation, but unsupported by gnulib.  POSIX
153        says that implementations may, but not must, make sa_sigaction
154        overlap with sa_handler, but we know of no implementation where
155        they do not overlap.  */
156     void (*_sa_sigaction) (int, siginfo_t *, void *);
157   } _sa_func;
158   sigset_t sa_mask;
159   /* Not all POSIX flags are supported.  */
160   int sa_flags;
161 };
162 # define sa_handler _sa_func._sa_handler
163 # define sa_sigaction _sa_func._sa_sigaction
164 /* Unsupported flags are not present.  */
165 # define SA_RESETHAND 1
166 # define SA_NODEFER 2
167 # define SA_RESTART 4
168
169 extern int sigaction (int, const struct sigaction *restrict,
170                       struct sigaction *restrict);
171
172 #elif !@HAVE_STRUCT_SIGACTION_SA_SIGACTION@
173
174 # define sa_sigaction sa_handler
175
176 #endif /* !@HAVE_SIGACTION@, !@HAVE_STRUCT_SIGACTION_SA_SIGACTION@ */
177
178
179 #ifdef __cplusplus
180 }
181 #endif
182
183 #endif /* _GL_SIGNAL_H */
184 #endif /* _GL_SIGNAL_H */
185 #endif