Debian Codegraph @

lib/sigprocmask.c

raw
1 /* POSIX compatible signal blocking.
2 Copyright (C) 2006-2026 Free Software Foundation, Inc.
3 Written by Bruno Haible <[email protected]>, 2006.
4
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
9
10 This file 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 #include <config.h>
19
20 /* Specification. */
21 #include <signal.h>
22
23 #include <errno.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26
27 #include "glthread/lock.h"
28
29 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
30 # include "msvc-inval.h"
31 #endif
32
33 /* We assume that a platform without POSIX signal blocking functions
34 also does not have the POSIX sigaction() function, only the
35 signal() function. We also assume signal() has SysV semantics,
36 where any handler is uninstalled prior to being invoked. This is
37 true for native Windows platforms. */
38
39 /* We use raw signal(), but also provide a wrapper rpl_signal() so
40 that applications can query or change a blocked signal. */
41 #undef signal
42
43 /* Provide invalid signal numbers as fallbacks if the uncatchable
44 signals are not defined. */
45 #ifndef SIGKILL
46 # define SIGKILL (-1)
47 #endif
48 #ifndef SIGSTOP
49 # define SIGSTOP (-1)
50 #endif
51
52 /* On native Windows, as of 2008, the signal SIGABRT_COMPAT is an alias
53 for the signal SIGABRT. Only one signal handler is stored for both
54 SIGABRT and SIGABRT_COMPAT. SIGABRT_COMPAT is not a signal of its own. */
55 #if defined _WIN32 && ! defined __CYGWIN__
56 # undef SIGABRT_COMPAT
57 # define SIGABRT_COMPAT 6
58 #endif
59 #ifdef SIGABRT_COMPAT
60 # define SIGABRT_COMPAT_MASK (1U << SIGABRT_COMPAT)
61 #else
62 # define SIGABRT_COMPAT_MASK 0
63 #endif
64
65 typedef void (*handler_t) (int);
66
67 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
68 static handler_t
69 signal_nothrow (int sig, handler_t handler)
70 {
71 handler_t result;
72
73 TRY_MSVC_INVAL
74 {
75 result = signal (sig, handler);
76 }
77 CATCH_MSVC_INVAL
78 {
79 result = SIG_ERR;
80 errno = EINVAL;
81 }
82 DONE_MSVC_INVAL;
83
84 return result;
85 }
86 # define signal signal_nothrow
87 #endif
88
89 /* This lock protects the variables defined in this file from concurrent
90 modification in multiple threads. */
91 gl_lock_define_initialized(static, sig_lock)
92
93 /* Handling of gnulib defined signals. */
94
95 #if GNULIB_defined_SIGPIPE
96 static handler_t SIGPIPE_handler = SIG_DFL;
97 #endif
98
99 #if GNULIB_defined_SIGPIPE
100 static handler_t
101 ext_signal (int sig, handler_t handler)
102 {
103 switch (sig)
104 {
105 case SIGPIPE:
106 {
107 handler_t old_handler = SIGPIPE_handler;
108 SIGPIPE_handler = handler;
109 return old_handler;
110 }
111 default: /* System defined signal */
112 return signal (sig, handler);
113 }
114 }
115 # undef signal
116 # define signal ext_signal
117 #endif
118
119 int
120 sigismember (const sigset_t *set, int sig)
121 {
122 if (sig >= 0 && sig < NSIG)
123 {
124 #ifdef SIGABRT_COMPAT
125 if (sig == SIGABRT_COMPAT)
126 sig = SIGABRT;
127 #endif
128
129 return (*set >> sig) & 1;
130 }
131 else
132 return 0;
133 }
134
135 int
136 sigemptyset (sigset_t *set)
137 {
138 *set = 0;
139 return 0;
140 }
141
142 int
143 sigaddset (sigset_t *set, int sig)
144 {
145 if (sig >= 0 && sig < NSIG)
146 {
147 #ifdef SIGABRT_COMPAT
148 if (sig == SIGABRT_COMPAT)
149 sig = SIGABRT;
150 #endif
151
152 *set |= 1U << sig;
153 return 0;
154 }
155 else
156 {
157 errno = EINVAL;
158 return -1;
159 }
160 }
161
162 int
163 sigdelset (sigset_t *set, int sig)
164 {
165 if (sig >= 0 && sig < NSIG)
166 {
167 #ifdef SIGABRT_COMPAT
168 if (sig == SIGABRT_COMPAT)
169 sig = SIGABRT;
170 #endif
171
172 *set &= ~(1U << sig);
173 return 0;
174 }
175 else
176 {
177 errno = EINVAL;
178 return -1;
179 }
180 }
181
182
183 int
184 sigfillset (sigset_t *set)
185 {
186 *set = ((2U << (NSIG - 1)) - 1) & ~ SIGABRT_COMPAT_MASK;
187 return 0;
188 }
189
190 /* Set of currently blocked signals. */
191 static sigset_t blocked_set /* = 0 */;
192
193 /* Set of currently blocked and pending signals. */
194 static volatile sig_atomic_t pending_array[NSIG] /* = { 0 } */;
195
196 /* Signal handler that is installed for blocked signals. */
197 static void
198 blocked_handler (int sig)
199 {
200 /* Reinstall the handler, in case the signal occurs multiple times
201 while blocked. There is an inherent race where an asynchronous
202 signal in between when the kernel uninstalled the handler and
203 when we reinstall it will trigger the default handler; oh
204 well. */
205 signal (sig, blocked_handler);
206 if (sig >= 0 && sig < NSIG)
207 pending_array[sig] = 1;
208 }
209
210 int
211 sigpending (sigset_t *set)
212 {
213 sigset_t pending = 0;
214 for (int sig = 0; sig < NSIG; sig++)
215 if (pending_array[sig])
216 pending |= 1U << sig;
217
218 *set = pending;
219 return 0;
220 }
221
222 /* The previous signal handlers.
223 Only the array elements corresponding to blocked signals are relevant. */
224 static volatile handler_t old_handlers[NSIG];
225
226 int
227 sigprocmask (int operation, const sigset_t *set, sigset_t *old_set)
228 {
229 gl_lock_lock (sig_lock);
230
231 if (old_set != NULL)
232 *old_set = blocked_set;
233
234 if (set != NULL)
235 {
236 sigset_t new_blocked_set;
237 switch (operation)
238 {
239 case SIG_BLOCK:
240 new_blocked_set = blocked_set | *set;
241 break;
242 case SIG_SETMASK:
243 new_blocked_set = *set;
244 break;
245 case SIG_UNBLOCK:
246 new_blocked_set = blocked_set & ~*set;
247 break;
248 default:
249 gl_lock_unlock (sig_lock);
250 errno = EINVAL;
251 return -1;
252 }
253
254 sigset_t to_unblock = blocked_set & ~new_blocked_set;
255 sigset_t to_block = new_blocked_set & ~blocked_set;
256
257 if (to_block != 0)
258 for (int sig = 0; sig < NSIG; sig++)
259 if ((to_block >> sig) & 1)
260 {
261 pending_array[sig] = 0;
262 if ((old_handlers[sig] = signal (sig, blocked_handler)) != SIG_ERR)
263 blocked_set |= 1U << sig;
264 }
265
266 if (to_unblock != 0)
267 {
268 sig_atomic_t received[NSIG];
269
270 for (int sig = 0; sig < NSIG; sig++)
271 if ((to_unblock >> sig) & 1)
272 {
273 if (signal (sig, old_handlers[sig]) != blocked_handler)
274 /* The application changed a signal handler while the signal
275 was blocked, bypassing our rpl_signal replacement.
276 We don't support this. */
277 abort ();
278 received[sig] = pending_array[sig];
279 blocked_set &= ~(1U << sig);
280 pending_array[sig] = 0;
281 }
282 else
283 received[sig] = 0;
284
285 for (int sig = 0; sig < NSIG; sig++)
286 if (received[sig])
287 raise (sig);
288 }
289 }
290
291 gl_lock_unlock (sig_lock);
292 return 0;
293 }
294
295 /* Install the handler FUNC for signal SIG, and return the previous
296 handler. */
297 handler_t
298 rpl_signal (int sig, handler_t handler)
299 {
300 /* We must provide a wrapper, so that a user can query what handler
301 they installed even if that signal is currently blocked. */
302 if (sig >= 0 && sig < NSIG && sig != SIGKILL && sig != SIGSTOP
303 && handler != SIG_ERR)
304 {
305 #ifdef SIGABRT_COMPAT
306 if (sig == SIGABRT_COMPAT)
307 sig = SIGABRT;
308 #endif
309
310 if (blocked_set & (1U << sig))
311 {
312 /* POSIX states that sigprocmask and signal are both
313 async-signal-safe. This is not true of our
314 implementation - there is a slight data race where an
315 asynchronous interrupt on signal A can occur after we
316 install blocked_handler but before we have updated
317 old_handlers for signal B, such that handler A can see
318 stale information if it calls signal(B). Oh well -
319 signal handlers really shouldn't try to manipulate the
320 installed handlers of unrelated signals. */
321 handler_t result = old_handlers[sig];
322 old_handlers[sig] = handler;
323 return result;
324 }
325 else
326 return signal (sig, handler);
327 }
328 else
329 {
330 errno = EINVAL;
331 return SIG_ERR;
332 }
333 }
334
335 #if GNULIB_defined_SIGPIPE
336 /* Raise the signal SIGPIPE. */
337 int
338 _gl_raise_SIGPIPE (void)
339 {
340 gl_lock_lock (sig_lock);
341 if (blocked_set & (1U << SIGPIPE))
342 {
343 pending_array[SIGPIPE] = 1;
344 gl_lock_unlock (sig_lock);
345 }
346 else
347 {
348 handler_t handler = SIGPIPE_handler;
349 gl_lock_unlock (sig_lock);
350 if (handler == SIG_DFL)
351 exit (128 + SIGPIPE);
352 else if (handler != SIG_IGN)
353 (*handler) (SIGPIPE);
354 }
355 return 0;
356 }
357 #endif