1 /* Emergency actions in case of a fatal signal.
2 Copyright (C) 2003-2004 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,
53 SIGRTMIN...SIGRTMAX - because they are reserved for application use.
55 SIGXCPU, SIGXFSZ - because they are quite similar to SIGTERM. */
57 static int fatal_signals
[] =
59 /* ISO C 99 signals. */
66 /* POSIX:2001 signals. */
83 #define num_fatal_signals (SIZEOF (fatal_signals) - 1)
85 /* Eliminate signals whose signal handler is SIG_IGN. */
88 init_fatal_signals (void)
90 static bool fatal_signals_initialized
= false;
91 if (!fatal_signals_initialized
)
96 for (i
= 0; i
< num_fatal_signals
; i
++)
98 struct sigaction action
;
100 if (sigaction (fatal_signals
[i
], NULL
, &action
) >= 0
101 && action
.sa_handler
== SIG_IGN
)
102 fatal_signals
[i
] = -1;
106 fatal_signals_initialized
= true;
111 /* ========================================================================= */
114 typedef void (*action_t
) (void);
116 /* Type of an entry in the actions array.
117 The 'action' field is accessed from within the fatal_signal_handler(),
118 therefore we mark it as 'volatile'. */
121 volatile action_t action
;
125 /* The registered cleanup actions. */
126 static actions_entry_t static_actions
[32];
127 static actions_entry_t
* volatile actions
= static_actions
;
128 static sig_atomic_t volatile actions_count
= 0;
129 static size_t actions_allocated
= SIZEOF (static_actions
);
132 /* Uninstall the handlers. */
134 uninstall_handlers ()
138 for (i
= 0; i
< num_fatal_signals
; i
++)
139 if (fatal_signals
[i
] >= 0)
140 signal (fatal_signals
[i
], SIG_DFL
);
144 /* The signal handler. It gets called asynchronously. */
146 fatal_signal_handler (int sig
)
150 /* Get the last registered cleanup action, in a reentrant way. */
152 size_t n
= actions_count
;
157 action
= actions
[n
].action
;
158 /* Execute the action. */
162 /* Now execute the signal's default action.
163 If signal() blocks the signal being delivered for the duration of the
164 signal handler's execution, the re-raised signal is delivered when this
165 handler returns; otherwise it is delivered already during raise(). */
166 uninstall_handlers ();
170 kill (getpid (), sig
);
175 /* Install the handlers. */
181 for (i
= 0; i
< num_fatal_signals
; i
++)
182 if (fatal_signals
[i
] >= 0)
183 signal (fatal_signals
[i
], &fatal_signal_handler
);
187 /* Register a cleanup function to be executed when a catchable fatal signal
190 at_fatal_signal (action_t action
)
192 static bool cleanup_initialized
= false;
193 if (!cleanup_initialized
)
195 init_fatal_signals ();
197 cleanup_initialized
= true;
200 if (actions_count
== actions_allocated
)
202 /* Extend the actions array. Note that we cannot use xrealloc(),
203 because then the cleanup() function could access an already
204 deallocated array. */
205 actions_entry_t
*old_actions
= actions
;
206 size_t new_actions_allocated
= 2 * actions_allocated
;
207 actions_entry_t
*new_actions
=
208 xmalloc (new_actions_allocated
* sizeof (actions_entry_t
));
210 memcpy (new_actions
, old_actions
,
211 actions_allocated
* sizeof (actions_entry_t
));
212 actions
= new_actions
;
213 actions_allocated
= new_actions_allocated
;
214 /* Now we can free the old actions array. */
215 if (old_actions
!= static_actions
)
218 /* The two uses of 'volatile' in the types above (and ISO C 99 section
219 5.1.2.3.(5)) ensure that we increment the actions_count only after
220 the new action has been written to the memory location
221 actions[actions_count]. */
222 actions
[actions_count
].action
= action
;
227 /* ========================================================================= */
230 #if HAVE_POSIX_SIGNALBLOCKING
232 static sigset_t fatal_signal_set
;
235 init_fatal_signal_set ()
237 static bool fatal_signal_set_initialized
= false;
238 if (!fatal_signal_set_initialized
)
242 init_fatal_signals ();
244 sigemptyset (&fatal_signal_set
);
245 for (i
= 0; i
< num_fatal_signals
; i
++)
246 if (fatal_signals
[i
] >= 0)
247 sigaddset (&fatal_signal_set
, fatal_signals
[i
]);
249 fatal_signal_set_initialized
= true;
253 /* Temporarily delay the catchable fatal signals. */
255 block_fatal_signals ()
257 init_fatal_signal_set ();
258 sigprocmask (SIG_BLOCK
, &fatal_signal_set
, NULL
);
261 /* Stop delaying the catchable fatal signals. */
263 unblock_fatal_signals ()
265 init_fatal_signal_set ();
266 sigprocmask (SIG_UNBLOCK
, &fatal_signal_set
, NULL
);
271 /* Don't bother caring about the old systems which don't have POSIX signal
275 block_fatal_signals ()
280 unblock_fatal_signals ()