1 /* Emergency actions in case of a fatal signal.
2 Copyright (C) 2003 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "fatal-signal.h"
37 #define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
40 /* ========================================================================= */
43 /* The list of fatal signals.
44 These are those signals whose default action is to terminate the process
45 without a core dump, except
46 SIGKILL - because it cannot be caught,
47 SIGALRM SIGUSR1 SIGUSR2 SIGPOLL SIGIO SIGLOST - because applications
48 often use them for their own purpose,
49 SIGPROF SIGVTALRM - because they are used for profiling,
50 SIGSTKFLT - because it is more similar to SIGFPE, SIGSEGV, SIGBUS,
51 SIGSYS - because it is more similar to SIGABRT, SIGSEGV,
52 SIGPWR - because it of too special use,
54 SIGXCPU, SIGXFSZ - because they are quite similar to SIGTERM. */
56 static const int fatal_signals[] =
58 /* ISO C 99 signals. */
65 /* POSIX:2001 signals. */
82 #define num_fatal_signals (SIZEOF (fatal_signals) - 1)
85 /* ========================================================================= */
88 /* The registered cleanup actions. */
89 typedef void (*action_t) (void);
90 static action_t static_actions[32];
91 static action_t * volatile actions = static_actions;
92 static size_t volatile actions_count = 0;
93 static size_t actions_allocated = SIZEOF (static_actions);
96 /* Uninstall the handlers. */
102 for (i = 0; i < num_fatal_signals; i++)
103 signal (fatal_signals[i], SIG_DFL);
107 /* The signal handler. It gets called asynchronously. */
109 fatal_signal_handler (int sig)
113 /* Get the last registered cleanup action, in a reentrant way. */
115 size_t n = actions_count;
121 /* Execute the action. */
125 /* Now execute the signal's default action. */
126 uninstall_handlers ();
130 kill (getpid (), sig);
135 /* Install the handlers. */
141 for (i = 0; i < num_fatal_signals; i++)
142 signal (fatal_signals[i], &fatal_signal_handler);
146 /* Register a cleanup function to be executed when a catchable fatal signal
149 at_fatal_signal (action_t action)
151 static bool cleanup_initialized = false;
152 if (!cleanup_initialized)
155 cleanup_initialized = true;
158 if (actions_count == actions_allocated)
160 /* Extend the actions array. Note that we cannot use xrealloc(),
161 because then the cleanup() function could access an already
162 deallocated array. */
163 action_t *old_actions = actions;
164 size_t new_actions_allocated = 2 * actions_allocated;
165 action_t *new_actions =
166 xmalloc (new_actions_allocated * sizeof (action_t));
168 memcpy (new_actions, actions, actions_allocated * sizeof (action_t));
169 actions = new_actions;
170 actions_allocated = new_actions_allocated;
171 /* Now we can free the old actions array. */
172 if (old_actions != static_actions)
175 actions[actions_count] = action;
180 /* ========================================================================= */
183 #if HAVE_POSIX_SIGNALBLOCKING
185 static sigset_t fatal_signal_set;
188 init_fatal_signal_set ()
190 static bool fatal_signal_set_initialized = false;
191 if (!fatal_signal_set_initialized)
195 sigemptyset (&fatal_signal_set);
196 for (i = 0; i < num_fatal_signals; i++)
197 sigaddset (&fatal_signal_set, fatal_signals[i]);
199 fatal_signal_set_initialized = true;
204 block_fatal_signals ()
206 init_fatal_signal_set ();
207 sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
211 unblock_fatal_signals ()
213 init_fatal_signal_set ();
214 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
219 /* Don't bother caring about the old systems which don't have POSIX signal
223 block_fatal_signals ()
228 unblock_fatal_signals ()