openat: don’t close (-1)
[gnulib.git] / lib / fatal-signal.c
blob8e960f302569495f2bc0c78c3d5853716d0a45ea
1 /* Emergency actions in case of a fatal signal.
2 Copyright (C) 2003-2004, 2006-2024 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2003.
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.
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.
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/>. */
19 #include <config.h>
21 /* Specification. */
22 #include "fatal-signal.h"
24 #include <stdlib.h>
25 #include <signal.h>
26 #include <unistd.h>
28 #include "glthread/lock.h"
29 #include "glthread/once.h"
30 #include "thread-optim.h"
31 #include "sig-handler.h"
33 #define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
35 /* ========================================================================= */
38 /* The list of fatal signals.
39 These are those signals whose default action is to terminate the process
40 without a core dump, except
41 SIGKILL - because it cannot be caught,
42 SIGALRM SIGUSR1 SIGUSR2 SIGPOLL SIGIO SIGLOST - because applications
43 often use them for their own purpose,
44 SIGPROF SIGVTALRM - because they are used for profiling,
45 SIGSTKFLT - because it is more similar to SIGFPE, SIGSEGV, SIGBUS,
46 SIGSYS - because it is more similar to SIGABRT, SIGSEGV,
47 SIGPWR - because it of too special use,
48 SIGRTMIN...SIGRTMAX - because they are reserved for application use.
49 plus
50 SIGXCPU, SIGXFSZ - because they are quite similar to SIGTERM. */
52 static int fatal_signals[] =
54 /* ISO C 99 signals. */
55 #ifdef SIGINT
56 SIGINT,
57 #endif
58 #ifdef SIGTERM
59 SIGTERM,
60 #endif
61 /* POSIX:2001 signals. */
62 #ifdef SIGHUP
63 SIGHUP,
64 #endif
65 #ifdef SIGPIPE
66 SIGPIPE,
67 #endif
68 /* BSD signals. */
69 #ifdef SIGXCPU
70 SIGXCPU,
71 #endif
72 #ifdef SIGXFSZ
73 SIGXFSZ,
74 #endif
75 /* Native Windows signals. */
76 #ifdef SIGBREAK
77 SIGBREAK,
78 #endif
82 #define num_fatal_signals (SIZEOF (fatal_signals) - 1)
84 /* Eliminate signals whose signal handler is SIG_IGN. */
86 static void
87 init_fatal_signals (void)
89 /* This function is multithread-safe even without synchronization, because
90 if two threads execute it simultaneously, the fatal_signals[] array will
91 not change any more after the first of the threads has completed this
92 function. */
93 static bool fatal_signals_initialized = false;
94 if (!fatal_signals_initialized)
96 size_t i;
98 for (i = 0; i < num_fatal_signals; i++)
100 struct sigaction action;
102 if (sigaction (fatal_signals[i], NULL, &action) >= 0
103 && get_handler (&action) == SIG_IGN)
104 fatal_signals[i] = -1;
107 fatal_signals_initialized = true;
112 /* ========================================================================= */
115 typedef _GL_ASYNC_SAFE void (*action_t) (int sig);
117 /* Type of an entry in the actions array.
118 The 'action' field is accessed from within the fatal_signal_handler(),
119 therefore we mark it as 'volatile'. */
120 typedef struct
122 volatile action_t action;
124 actions_entry_t;
126 /* The registered cleanup actions. */
127 static actions_entry_t static_actions[32];
128 static actions_entry_t * volatile actions = static_actions;
129 static sig_atomic_t volatile actions_count = 0;
130 static size_t actions_allocated = SIZEOF (static_actions);
133 /* The saved signal handlers.
134 Size 32 would not be sufficient: On HP-UX, SIGXCPU = 33, SIGXFSZ = 34. */
135 static struct sigaction saved_sigactions[64];
138 /* Uninstall the handlers. */
139 static _GL_ASYNC_SAFE void
140 uninstall_handlers (void)
142 size_t i;
144 for (i = 0; i < num_fatal_signals; i++)
145 if (fatal_signals[i] >= 0)
147 int sig = fatal_signals[i];
148 if (saved_sigactions[sig].sa_handler == SIG_IGN)
149 saved_sigactions[sig].sa_handler = SIG_DFL;
150 sigaction (sig, &saved_sigactions[sig], NULL);
155 /* The signal handler. It gets called asynchronously. */
156 static _GL_ASYNC_SAFE void
157 fatal_signal_handler (int sig)
159 for (;;)
161 /* Get the last registered cleanup action, in a reentrant way. */
162 action_t action;
163 size_t n = actions_count;
164 if (n == 0)
165 break;
166 n--;
167 actions_count = n;
168 action = actions[n].action;
169 /* Execute the action. */
170 action (sig);
173 /* Now execute the signal's default action.
174 If the signal being delivered was blocked, the re-raised signal would be
175 delivered when this handler returns. But the way we install this handler,
176 no signal is blocked, and the re-raised signal is delivered already
177 during raise(). */
178 uninstall_handlers ();
179 raise (sig);
183 /* Install the handlers. */
184 static void
185 install_handlers (void)
187 size_t i;
188 struct sigaction action;
190 action.sa_handler = &fatal_signal_handler;
191 /* If we get a fatal signal while executing fatal_signal_handler, enter
192 fatal_signal_handler recursively, since it is reentrant. Hence no
193 SA_RESETHAND. */
194 action.sa_flags = SA_NODEFER;
195 sigemptyset (&action.sa_mask);
196 for (i = 0; i < num_fatal_signals; i++)
197 if (fatal_signals[i] >= 0)
199 int sig = fatal_signals[i];
201 if (!(sig < sizeof (saved_sigactions) / sizeof (saved_sigactions[0])))
202 abort ();
203 sigaction (sig, &action, &saved_sigactions[sig]);
208 /* Lock that makes at_fatal_signal multi-thread safe. */
209 gl_lock_define_initialized (static, at_fatal_signal_lock)
211 /* Register a cleanup function to be executed when a catchable fatal signal
212 occurs. */
214 at_fatal_signal (action_t action)
216 bool mt = gl_multithreaded ();
218 if (mt) gl_lock_lock (at_fatal_signal_lock);
220 static bool cleanup_initialized = false;
221 if (!cleanup_initialized)
223 init_fatal_signals ();
224 install_handlers ();
225 cleanup_initialized = true;
228 int ret = 0;
230 if (actions_count == actions_allocated)
232 /* Extend the actions array. Note that we cannot use xrealloc(),
233 because then the cleanup() function could access an already
234 deallocated array. */
235 actions_entry_t *old_actions = actions;
236 size_t old_actions_allocated = actions_allocated;
237 size_t new_actions_allocated = 2 * actions_allocated;
238 actions_entry_t *new_actions =
239 (actions_entry_t *)
240 malloc (new_actions_allocated * sizeof (actions_entry_t));
241 if (new_actions == NULL)
243 ret = -1;
244 goto done;
247 size_t k;
248 /* Don't use memcpy() here, because memcpy takes non-volatile arguments
249 and is therefore not guaranteed to complete all memory stores before
250 the next statement. */
251 for (k = 0; k < old_actions_allocated; k++)
252 new_actions[k] = old_actions[k];
253 actions = new_actions;
254 actions_allocated = new_actions_allocated;
255 /* Now we can free the old actions array. */
256 /* No, we can't do that. If fatal_signal_handler is running in a
257 different thread and has already fetched the actions pointer (getting
258 old_actions) but not yet accessed its n-th element, that thread may
259 crash when accessing an element of the already freed old_actions
260 array. */
261 #if 0
262 if (old_actions != static_actions)
263 free (old_actions);
264 #endif
266 /* The two uses of 'volatile' in the types above (and ISO C 99 section
267 5.1.2.3.(5)) ensure that we increment the actions_count only after
268 the new action has been written to the memory location
269 actions[actions_count]. */
270 actions[actions_count].action = action;
271 actions_count++;
273 done:
274 if (mt) gl_lock_unlock (at_fatal_signal_lock);
276 return ret;
280 /* ========================================================================= */
283 static sigset_t fatal_signal_set;
285 static void
286 do_init_fatal_signal_set (void)
288 size_t i;
290 init_fatal_signals ();
292 sigemptyset (&fatal_signal_set);
293 for (i = 0; i < num_fatal_signals; i++)
294 if (fatal_signals[i] >= 0)
295 sigaddset (&fatal_signal_set, fatal_signals[i]);
298 /* Ensure that do_init_fatal_signal_set is called once only. */
299 gl_once_define(static, fatal_signal_set_once)
301 static void
302 init_fatal_signal_set (void)
304 gl_once (fatal_signal_set_once, do_init_fatal_signal_set);
307 /* Lock and counter that allow block_fatal_signals/unblock_fatal_signals pairs
308 to occur in different threads and even overlap in time. */
309 gl_lock_define_initialized (static, fatal_signals_block_lock)
310 static unsigned int fatal_signals_block_counter = 0;
312 /* Temporarily delay the catchable fatal signals. */
313 void
314 block_fatal_signals (void)
316 bool mt = gl_multithreaded ();
318 if (mt) gl_lock_lock (fatal_signals_block_lock);
320 if (fatal_signals_block_counter++ == 0)
322 init_fatal_signal_set ();
323 sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
326 if (mt) gl_lock_unlock (fatal_signals_block_lock);
329 /* Stop delaying the catchable fatal signals. */
330 void
331 unblock_fatal_signals (void)
333 bool mt = gl_multithreaded ();
335 if (mt) gl_lock_lock (fatal_signals_block_lock);
337 if (fatal_signals_block_counter == 0)
338 /* There are more calls to unblock_fatal_signals() than to
339 block_fatal_signals(). */
340 abort ();
341 if (--fatal_signals_block_counter == 0)
343 init_fatal_signal_set ();
344 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
347 if (mt) gl_lock_unlock (fatal_signals_block_lock);
351 unsigned int
352 get_fatal_signals (int signals[64])
354 init_fatal_signal_set ();
357 int *p = signals;
358 size_t i;
360 for (i = 0; i < num_fatal_signals; i++)
361 if (fatal_signals[i] >= 0)
362 *p++ = fatal_signals[i];
363 return p - signals;
367 const sigset_t *
368 get_fatal_signal_set (void)
370 init_fatal_signal_set ();
371 return &fatal_signal_set;