1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
#include <config.h>
#include <signal.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#undef signal
#ifndef SIGKILL
# define SIGKILL (-1)
#endif
#ifndef SIGSTOP
# define SIGSTOP (-1)
#endif
#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
# undef SIGABRT_COMPAT
# define SIGABRT_COMPAT 6
#endif
#ifdef SIGABRT_COMPAT
# define SIGABRT_COMPAT_MASK (1U << SIGABRT_COMPAT)
#else
# define SIGABRT_COMPAT_MASK 0
#endif
typedef void (*handler_t) (int);
#if GNULIB_defined_SIGPIPE
static handler_t SIGPIPE_handler = SIG_DFL;
#endif
#if GNULIB_defined_SIGPIPE
static handler_t
ext_signal (int sig, handler_t handler)
{
switch (sig)
{
case SIGPIPE:
{
handler_t old_handler = SIGPIPE_handler;
SIGPIPE_handler = handler;
return old_handler;
}
default:
return signal (sig, handler);
}
}
# define signal ext_signal
#endif
int
sigismember (const sigset_t *set, int sig)
{
if (sig >= 0 && sig < NSIG)
{
#ifdef SIGABRT_COMPAT
if (sig == SIGABRT_COMPAT)
sig = SIGABRT;
#endif
return (*set >> sig) & 1;
}
else
return 0;
}
int
sigemptyset (sigset_t *set)
{
*set = 0;
return 0;
}
int
sigaddset (sigset_t *set, int sig)
{
if (sig >= 0 && sig < NSIG)
{
#ifdef SIGABRT_COMPAT
if (sig == SIGABRT_COMPAT)
sig = SIGABRT;
#endif
*set |= 1U << sig;
return 0;
}
else
{
errno = EINVAL;
return -1;
}
}
int
sigdelset (sigset_t *set, int sig)
{
if (sig >= 0 && sig < NSIG)
{
#ifdef SIGABRT_COMPAT
if (sig == SIGABRT_COMPAT)
sig = SIGABRT;
#endif
*set &= ~(1U << sig);
return 0;
}
else
{
errno = EINVAL;
return -1;
}
}
int
sigfillset (sigset_t *set)
{
*set = ((2U << (NSIG - 1)) - 1) & ~ SIGABRT_COMPAT_MASK;
return 0;
}
static volatile sigset_t blocked_set ;
static volatile sig_atomic_t pending_array[NSIG] ;
static void
blocked_handler (int sig)
{
signal (sig, blocked_handler);
if (sig >= 0 && sig < NSIG)
pending_array[sig] = 1;
}
int
sigpending (sigset_t *set)
{
sigset_t pending = 0;
int sig;
for (sig = 0; sig < NSIG; sig++)
if (pending_array[sig])
pending |= 1U << sig;
*set = pending;
return 0;
}
static volatile handler_t old_handlers[NSIG];
int
sigprocmask (int operation, const sigset_t *set, sigset_t *old_set)
{
if (old_set != NULL)
*old_set = blocked_set;
if (set != NULL)
{
sigset_t new_blocked_set;
sigset_t to_unblock;
sigset_t to_block;
switch (operation)
{
case SIG_BLOCK:
new_blocked_set = blocked_set | *set;
break;
case SIG_SETMASK:
new_blocked_set = *set;
break;
case SIG_UNBLOCK:
new_blocked_set = blocked_set & ~*set;
break;
default:
errno = EINVAL;
return -1;
}
to_unblock = blocked_set & ~new_blocked_set;
to_block = new_blocked_set & ~blocked_set;
if (to_block != 0)
{
int sig;
for (sig = 0; sig < NSIG; sig++)
if ((to_block >> sig) & 1)
{
pending_array[sig] = 0;
if ((old_handlers[sig] = signal (sig, blocked_handler)) != SIG_ERR)
blocked_set |= 1U << sig;
}
}
if (to_unblock != 0)
{
sig_atomic_t received[NSIG];
int sig;
for (sig = 0; sig < NSIG; sig++)
if ((to_unblock >> sig) & 1)
{
if (signal (sig, old_handlers[sig]) != blocked_handler)
abort ();
received[sig] = pending_array[sig];
blocked_set &= ~(1U << sig);
pending_array[sig] = 0;
}
else
received[sig] = 0;
for (sig = 0; sig < NSIG; sig++)
if (received[sig])
raise (sig);
}
}
return 0;
}
handler_t
rpl_signal (int sig, handler_t handler)
{
if (sig >= 0 && sig < NSIG && sig != SIGKILL && sig != SIGSTOP
&& handler != SIG_ERR)
{
#ifdef SIGABRT_COMPAT
if (sig == SIGABRT_COMPAT)
sig = SIGABRT;
#endif
if (blocked_set & (1U << sig))
{
handler_t result = old_handlers[sig];
old_handlers[sig] = handler;
return result;
}
else
return signal (sig, handler);
}
else
{
errno = EINVAL;
return SIG_ERR;
}
}
#if GNULIB_defined_SIGPIPE
int
rpl_raise (int sig)
# undef raise
{
switch (sig)
{
case SIGPIPE:
if (blocked_set & (1U << sig))
pending_array[sig] = 1;
else
{
handler_t handler = SIGPIPE_handler;
if (handler == SIG_DFL)
exit (128 + SIGPIPE);
else if (handler != SIG_IGN)
(*handler) (sig);
}
return 0;
default:
return raise (sig);
}
}
#endif
|