1 /* Emergency actions in case of a fatal signal.
2 Copyright (C) 2003-2004, 2006-2008 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 3 of the License, or
8 (at your option) any later version.
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, see <http://www.gnu.org/licenses/>. */
22 #include "fatal-signal.h"
31 #define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
34 /* ========================================================================= */
37 /* The list of fatal signals.
38 These are those signals whose default action is to terminate the process
39 without a core dump, except
40 SIGKILL - because it cannot be caught,
41 SIGALRM SIGUSR1 SIGUSR2 SIGPOLL SIGIO SIGLOST - because applications
42 often use them for their own purpose,
43 SIGPROF SIGVTALRM - because they are used for profiling,
44 SIGSTKFLT - because it is more similar to SIGFPE, SIGSEGV, SIGBUS,
45 SIGSYS - because it is more similar to SIGABRT, SIGSEGV,
46 SIGPWR - because it of too special use,
47 SIGRTMIN...SIGRTMAX - because they are reserved for application use.
49 SIGXCPU, SIGXFSZ - because they are quite similar to SIGTERM. */
51 static int fatal_signals[] =
53 /* ISO C 99 signals. */
60 /* POSIX:2001 signals. */
81 #define num_fatal_signals (SIZEOF (fatal_signals) - 1)
83 /* Eliminate signals whose signal handler is SIG_IGN. */
86 init_fatal_signals (void)
88 static bool fatal_signals_initialized = false;
89 if (!fatal_signals_initialized)
94 for (i = 0; i < num_fatal_signals; i++)
96 struct sigaction action;
98 if (sigaction (fatal_signals[i], NULL, &action) >= 0
99 && action.sa_handler == SIG_IGN)
100 fatal_signals[i] = -1;
104 fatal_signals_initialized = true;
109 /* ========================================================================= */
112 typedef void (*action_t) (void);
114 /* Type of an entry in the actions array.
115 The 'action' field is accessed from within the fatal_signal_handler(),
116 therefore we mark it as 'volatile'. */
119 volatile action_t action;
123 /* The registered cleanup actions. */
124 static actions_entry_t static_actions[32];
125 static actions_entry_t * volatile actions = static_actions;
126 static sig_atomic_t volatile actions_count = 0;
127 static size_t actions_allocated = SIZEOF (static_actions);
130 /* Uninstall the handlers. */
132 uninstall_handlers ()
136 for (i = 0; i < num_fatal_signals; i++)
137 if (fatal_signals[i] >= 0)
138 signal (fatal_signals[i], SIG_DFL);
142 /* The signal handler. It gets called asynchronously. */
144 fatal_signal_handler (int sig)
148 /* Get the last registered cleanup action, in a reentrant way. */
150 size_t n = actions_count;
155 action = actions[n].action;
156 /* Execute the action. */
160 /* Now execute the signal's default action.
161 If signal() blocks the signal being delivered for the duration of the
162 signal handler's execution, the re-raised signal is delivered when this
163 handler returns; otherwise it is delivered already during raise(). */
164 uninstall_handlers ();
169 /* Install the handlers. */
175 for (i = 0; i < num_fatal_signals; i++)
176 if (fatal_signals[i] >= 0)
177 signal (fatal_signals[i], &fatal_signal_handler);
181 /* Register a cleanup function to be executed when a catchable fatal signal
184 at_fatal_signal (action_t action)
186 static bool cleanup_initialized = false;
187 if (!cleanup_initialized)
189 init_fatal_signals ();
191 cleanup_initialized = true;
194 if (actions_count == actions_allocated)
196 /* Extend the actions array. Note that we cannot use xrealloc(),
197 because then the cleanup() function could access an already
198 deallocated array. */
199 actions_entry_t *old_actions = actions;
200 size_t old_actions_allocated = actions_allocated;
201 size_t new_actions_allocated = 2 * actions_allocated;
202 actions_entry_t *new_actions =
203 XNMALLOC (new_actions_allocated, actions_entry_t);
206 /* Don't use memcpy() here, because memcpy takes non-volatile arguments
207 and is therefore not guaranteed to complete all memory stores before
208 the next statement. */
209 for (k = 0; k < old_actions_allocated; k++)
210 new_actions[k] = old_actions[k];
211 actions = new_actions;
212 actions_allocated = new_actions_allocated;
213 /* Now we can free the old actions array. */
214 if (old_actions != static_actions)
217 /* The two uses of 'volatile' in the types above (and ISO C 99 section
218 5.1.2.3.(5)) ensure that we increment the actions_count only after
219 the new action has been written to the memory location
220 actions[actions_count]. */
221 actions[actions_count].action = action;
226 /* ========================================================================= */
229 static sigset_t fatal_signal_set;
232 init_fatal_signal_set ()
234 static bool fatal_signal_set_initialized = false;
235 if (!fatal_signal_set_initialized)
239 init_fatal_signals ();
241 sigemptyset (&fatal_signal_set);
242 for (i = 0; i < num_fatal_signals; i++)
243 if (fatal_signals[i] >= 0)
244 sigaddset (&fatal_signal_set, fatal_signals[i]);
246 fatal_signal_set_initialized = true;
250 /* Temporarily delay the catchable fatal signals. */
252 block_fatal_signals ()
254 init_fatal_signal_set ();
255 sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
258 /* Stop delaying the catchable fatal signals. */
260 unblock_fatal_signals ()
262 init_fatal_signal_set ();
263 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);