1 /* Emergency actions in case of a fatal signal.
2 Copyright (C) 2003-2004, 2006 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2003.
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 2, or (at your option)
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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
25 #include "fatal-signal.h"
34 #define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
37 /* ========================================================================= */
40 /* The list of fatal signals.
41 These are those signals whose default action is to terminate the process
42 without a core dump, except
43 SIGKILL - because it cannot be caught,
44 SIGALRM SIGUSR1 SIGUSR2 SIGPOLL SIGIO SIGLOST - because applications
45 often use them for their own purpose,
46 SIGPROF SIGVTALRM - because they are used for profiling,
47 SIGSTKFLT - because it is more similar to SIGFPE, SIGSEGV, SIGBUS,
48 SIGSYS - because it is more similar to SIGABRT, SIGSEGV,
49 SIGPWR - because it of too special use,
50 SIGRTMIN...SIGRTMAX - because they are reserved for application use.
52 SIGXCPU, SIGXFSZ - because they are quite similar to SIGTERM. */
54 static int fatal_signals[] =
56 /* ISO C 99 signals. */
63 /* POSIX:2001 signals. */
80 #define num_fatal_signals (SIZEOF (fatal_signals) - 1)
82 /* Eliminate signals whose signal handler is SIG_IGN. */
85 init_fatal_signals (void)
87 static bool fatal_signals_initialized = false;
88 if (!fatal_signals_initialized)
93 for (i = 0; i < num_fatal_signals; i++)
95 struct sigaction action;
97 if (sigaction (fatal_signals[i], NULL, &action) >= 0
98 && action.sa_handler == SIG_IGN)
99 fatal_signals[i] = -1;
103 fatal_signals_initialized = true;
108 /* ========================================================================= */
111 typedef void (*action_t) (void);
113 /* Type of an entry in the actions array.
114 The 'action' field is accessed from within the fatal_signal_handler(),
115 therefore we mark it as 'volatile'. */
118 volatile action_t action;
122 /* The registered cleanup actions. */
123 static actions_entry_t static_actions[32];
124 static actions_entry_t * volatile actions = static_actions;
125 static sig_atomic_t volatile actions_count = 0;
126 static size_t actions_allocated = SIZEOF (static_actions);
129 /* Uninstall the handlers. */
131 uninstall_handlers ()
135 for (i = 0; i < num_fatal_signals; i++)
136 if (fatal_signals[i] >= 0)
137 signal (fatal_signals[i], SIG_DFL);
141 /* The signal handler. It gets called asynchronously. */
143 fatal_signal_handler (int sig)
147 /* Get the last registered cleanup action, in a reentrant way. */
149 size_t n = actions_count;
154 action = actions[n].action;
155 /* Execute the action. */
159 /* Now execute the signal's default action.
160 If signal() blocks the signal being delivered for the duration of the
161 signal handler's execution, the re-raised signal is delivered when this
162 handler returns; otherwise it is delivered already during raise(). */
163 uninstall_handlers ();
167 kill (getpid (), sig);
172 /* Install the handlers. */
178 for (i = 0; i < num_fatal_signals; i++)
179 if (fatal_signals[i] >= 0)
180 signal (fatal_signals[i], &fatal_signal_handler);
184 /* Register a cleanup function to be executed when a catchable fatal signal
187 at_fatal_signal (action_t action)
189 static bool cleanup_initialized = false;
190 if (!cleanup_initialized)
192 init_fatal_signals ();
194 cleanup_initialized = true;
197 if (actions_count == actions_allocated)
199 /* Extend the actions array. Note that we cannot use xrealloc(),
200 because then the cleanup() function could access an already
201 deallocated array. */
202 actions_entry_t *old_actions = actions;
203 size_t old_actions_allocated = actions_allocated;
204 size_t new_actions_allocated = 2 * actions_allocated;
205 actions_entry_t *new_actions =
206 xmalloc (new_actions_allocated * sizeof (actions_entry_t));
209 /* Don't use memcpy() here, because memcpy takes non-volatile arguments
210 and is therefore not guaranteed to complete all memory stores before
211 the next statement. */
212 for (k = 0; k < old_actions_allocated; k++)
213 new_actions[k] = old_actions[k];
214 actions = new_actions;
215 actions_allocated = new_actions_allocated;
216 /* Now we can free the old actions array. */
217 if (old_actions != static_actions)
220 /* The two uses of 'volatile' in the types above (and ISO C 99 section
221 5.1.2.3.(5)) ensure that we increment the actions_count only after
222 the new action has been written to the memory location
223 actions[actions_count]. */
224 actions[actions_count].action = action;
229 /* ========================================================================= */
232 #if HAVE_POSIX_SIGNALBLOCKING
234 static sigset_t fatal_signal_set;
237 init_fatal_signal_set ()
239 static bool fatal_signal_set_initialized = false;
240 if (!fatal_signal_set_initialized)
244 init_fatal_signals ();
246 sigemptyset (&fatal_signal_set);
247 for (i = 0; i < num_fatal_signals; i++)
248 if (fatal_signals[i] >= 0)
249 sigaddset (&fatal_signal_set, fatal_signals[i]);
251 fatal_signal_set_initialized = true;
255 /* Temporarily delay the catchable fatal signals. */
257 block_fatal_signals ()
259 init_fatal_signal_set ();
260 sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
263 /* Stop delaying the catchable fatal signals. */
265 unblock_fatal_signals ()
267 init_fatal_signal_set ();
268 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
273 /* Don't bother caring about the old systems which don't have POSIX signal
277 block_fatal_signals ()
282 unblock_fatal_signals ()