New module 'fatal-signal'.
[gnulib.git] / lib / fatal-signal.c
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.
4
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)
8    any later version.
9
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.
14
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.  */
18
19
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
23
24 /* Specification.  */
25 #include "fatal-signal.h"
26
27 #include <stdbool.h>
28 #include <stdlib.h>
29 #include <signal.h>
30 #include <string.h>
31 #if HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
34
35 #include "xalloc.h"
36
37 #define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
38
39
40 /* ========================================================================= */
41
42
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    plus
54      SIGXCPU, SIGXFSZ - because they are quite similar to SIGTERM.  */
55
56 static const int fatal_signals[] =
57   {
58     /* ISO C 99 signals.  */
59 #ifdef SIGINT
60     SIGINT,
61 #endif
62 #ifdef SIGTERM
63     SIGTERM,
64 #endif
65     /* POSIX:2001 signals.  */
66 #ifdef SIGHUP
67     SIGHUP,
68 #endif
69 #ifdef SIGPIPE
70     SIGPIPE,
71 #endif
72     /* BSD signals.  */
73 #ifdef SIGXCPU
74     SIGXCPU,
75 #endif
76 #ifdef SIGXFSZ
77     SIGXFSZ,
78 #endif
79     0
80   };
81
82 #define num_fatal_signals (SIZEOF (fatal_signals) - 1)
83
84
85 /* ========================================================================= */
86
87
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);
94
95
96 /* Uninstall the handlers.  */
97 static inline void
98 uninstall_handlers ()
99 {
100   size_t i;
101
102   for (i = 0; i < num_fatal_signals; i++)
103     signal (fatal_signals[i], SIG_DFL);
104 }
105
106
107 /* The signal handler.  It gets called asynchronously.  */
108 static void
109 fatal_signal_handler (int sig)
110 {
111   for (;;)
112     {
113       /* Get the last registered cleanup action, in a reentrant way.  */
114       action_t action;
115       size_t n = actions_count;
116       if (n == 0)
117         break;
118       n--;
119       actions_count = n;
120       action = actions[n];
121       /* Execute the action.  */
122       action ();
123     }
124
125   /* Now execute the signal's default action.  */
126   uninstall_handlers ();
127 #if HAVE_RAISE
128   raise (sig);
129 #else
130   kill (getpid (), sig);
131 #endif
132 }
133
134
135 /* Install the handlers.  */
136 static inline void
137 install_handlers ()
138 {
139   size_t i;
140
141   for (i = 0; i < num_fatal_signals; i++)
142     signal (fatal_signals[i], &fatal_signal_handler);
143 }
144
145
146 /* Register a cleanup function to be executed when a catchable fatal signal
147    occurs.  */
148 void
149 at_fatal_signal (action_t action)
150 {
151   static bool cleanup_initialized = false;
152   if (!cleanup_initialized)
153     {
154       install_handlers ();
155       cleanup_initialized = true;
156     }
157
158   if (actions_count == actions_allocated)
159     {
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));
167
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)
173         free (old_actions);
174     }
175   actions[actions_count] = action;
176   actions_count++;
177 }
178
179
180 /* ========================================================================= */
181
182
183 #if HAVE_POSIX_SIGNALBLOCKING
184
185 static sigset_t fatal_signal_set;
186
187 static void
188 init_fatal_signal_set ()
189 {
190   static bool fatal_signal_set_initialized = false;
191   if (!fatal_signal_set_initialized)
192     {
193       size_t i;
194
195       sigemptyset (&fatal_signal_set);
196       for (i = 0; i < num_fatal_signals; i++)
197         sigaddset (&fatal_signal_set, fatal_signals[i]);
198
199       fatal_signal_set_initialized = true;
200     }
201 }
202
203 void
204 block_fatal_signals ()
205 {
206   init_fatal_signal_set ();
207   sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
208 }
209
210 void
211 unblock_fatal_signals ()
212 {
213   init_fatal_signal_set ();
214   sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
215 }
216
217 #else
218
219 /* Don't bother caring about the old systems which don't have POSIX signal
220    blocking.  */
221
222 void
223 block_fatal_signals ()
224 {
225 }
226
227 void
228 unblock_fatal_signals ()
229 {
230 }
231
232 #endif