Handle the Woe32 SIGBREAK too.
[gnulib.git] / lib / fatal-signal.c
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19
20 #include <config.h>
21
22 /* Specification.  */
23 #include "fatal-signal.h"
24
25 #include <stdbool.h>
26 #include <stdlib.h>
27 #include <signal.h>
28 #include <unistd.h>
29
30 #include "xalloc.h"
31
32 #define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
33
34
35 /* ========================================================================= */
36
37
38 /* The list of fatal signals.
39    These are those signals whose default action is to terminate the process
40    without a core dump, except
41      SIGKILL - because it cannot be caught,
42      SIGALRM SIGUSR1 SIGUSR2 SIGPOLL SIGIO SIGLOST - because applications
43        often use them for their own purpose,
44      SIGPROF SIGVTALRM - because they are used for profiling,
45      SIGSTKFLT - because it is more similar to SIGFPE, SIGSEGV, SIGBUS,
46      SIGSYS - because it is more similar to SIGABRT, SIGSEGV,
47      SIGPWR - because it of too special use,
48      SIGRTMIN...SIGRTMAX - because they are reserved for application use.
49    plus
50      SIGXCPU, SIGXFSZ - because they are quite similar to SIGTERM.  */
51
52 static int fatal_signals[] =
53   {
54     /* ISO C 99 signals.  */
55 #ifdef SIGINT
56     SIGINT,
57 #endif
58 #ifdef SIGTERM
59     SIGTERM,
60 #endif
61     /* POSIX:2001 signals.  */
62 #ifdef SIGHUP
63     SIGHUP,
64 #endif
65 #ifdef SIGPIPE
66     SIGPIPE,
67 #endif
68     /* BSD signals.  */
69 #ifdef SIGXCPU
70     SIGXCPU,
71 #endif
72 #ifdef SIGXFSZ
73     SIGXFSZ,
74 #endif
75     /* Woe32 signals.  */
76 #ifdef SIGBREAK
77     SIGBREAK,
78 #endif
79     0
80   };
81
82 #define num_fatal_signals (SIZEOF (fatal_signals) - 1)
83
84 /* Eliminate signals whose signal handler is SIG_IGN.  */
85
86 static void
87 init_fatal_signals (void)
88 {
89   static bool fatal_signals_initialized = false;
90   if (!fatal_signals_initialized)
91     {
92 #if HAVE_SIGACTION
93       size_t i;
94
95       for (i = 0; i < num_fatal_signals; i++)
96         {
97           struct sigaction action;
98
99           if (sigaction (fatal_signals[i], NULL, &action) >= 0
100               && action.sa_handler == SIG_IGN)
101             fatal_signals[i] = -1;
102         }
103 #endif
104
105       fatal_signals_initialized = true;
106     }
107 }
108
109
110 /* ========================================================================= */
111
112
113 typedef void (*action_t) (void);
114
115 /* Type of an entry in the actions array.
116    The 'action' field is accessed from within the fatal_signal_handler(),
117    therefore we mark it as 'volatile'.  */
118 typedef struct
119 {
120   volatile action_t action;
121 }
122 actions_entry_t;
123
124 /* The registered cleanup actions.  */
125 static actions_entry_t static_actions[32];
126 static actions_entry_t * volatile actions = static_actions;
127 static sig_atomic_t volatile actions_count = 0;
128 static size_t actions_allocated = SIZEOF (static_actions);
129
130
131 /* Uninstall the handlers.  */
132 static inline void
133 uninstall_handlers ()
134 {
135   size_t i;
136
137   for (i = 0; i < num_fatal_signals; i++)
138     if (fatal_signals[i] >= 0)
139       signal (fatal_signals[i], SIG_DFL);
140 }
141
142
143 /* The signal handler.  It gets called asynchronously.  */
144 static void
145 fatal_signal_handler (int sig)
146 {
147   for (;;)
148     {
149       /* Get the last registered cleanup action, in a reentrant way.  */
150       action_t action;
151       size_t n = actions_count;
152       if (n == 0)
153         break;
154       n--;
155       actions_count = n;
156       action = actions[n].action;
157       /* Execute the action.  */
158       action ();
159     }
160
161   /* Now execute the signal's default action.
162      If signal() blocks the signal being delivered for the duration of the
163      signal handler's execution, the re-raised signal is delivered when this
164      handler returns; otherwise it is delivered already during raise().  */
165   uninstall_handlers ();
166 #if HAVE_RAISE
167   raise (sig);
168 #else
169   kill (getpid (), sig);
170 #endif
171 }
172
173
174 /* Install the handlers.  */
175 static inline void
176 install_handlers ()
177 {
178   size_t i;
179
180   for (i = 0; i < num_fatal_signals; i++)
181     if (fatal_signals[i] >= 0)
182       signal (fatal_signals[i], &fatal_signal_handler);
183 }
184
185
186 /* Register a cleanup function to be executed when a catchable fatal signal
187    occurs.  */
188 void
189 at_fatal_signal (action_t action)
190 {
191   static bool cleanup_initialized = false;
192   if (!cleanup_initialized)
193     {
194       init_fatal_signals ();
195       install_handlers ();
196       cleanup_initialized = true;
197     }
198
199   if (actions_count == actions_allocated)
200     {
201       /* Extend the actions array.  Note that we cannot use xrealloc(),
202          because then the cleanup() function could access an already
203          deallocated array.  */
204       actions_entry_t *old_actions = actions;
205       size_t old_actions_allocated = actions_allocated;
206       size_t new_actions_allocated = 2 * actions_allocated;
207       actions_entry_t *new_actions =
208         xmalloc (new_actions_allocated * sizeof (actions_entry_t));
209       size_t k;
210
211       /* Don't use memcpy() here, because memcpy takes non-volatile arguments
212          and is therefore not guaranteed to complete all memory stores before
213          the next statement.  */
214       for (k = 0; k < old_actions_allocated; k++)
215         new_actions[k] = old_actions[k];
216       actions = new_actions;
217       actions_allocated = new_actions_allocated;
218       /* Now we can free the old actions array.  */
219       if (old_actions != static_actions)
220         free (old_actions);
221     }
222   /* The two uses of 'volatile' in the types above (and ISO C 99 section
223      5.1.2.3.(5)) ensure that we increment the actions_count only after
224      the new action has been written to the memory location
225      actions[actions_count].  */
226   actions[actions_count].action = action;
227   actions_count++;
228 }
229
230
231 /* ========================================================================= */
232
233
234 #if HAVE_POSIX_SIGNALBLOCKING
235
236 static sigset_t fatal_signal_set;
237
238 static void
239 init_fatal_signal_set ()
240 {
241   static bool fatal_signal_set_initialized = false;
242   if (!fatal_signal_set_initialized)
243     {
244       size_t i;
245
246       init_fatal_signals ();
247
248       sigemptyset (&fatal_signal_set);
249       for (i = 0; i < num_fatal_signals; i++)
250         if (fatal_signals[i] >= 0)
251           sigaddset (&fatal_signal_set, fatal_signals[i]);
252
253       fatal_signal_set_initialized = true;
254     }
255 }
256
257 /* Temporarily delay the catchable fatal signals.  */
258 void
259 block_fatal_signals ()
260 {
261   init_fatal_signal_set ();
262   sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
263 }
264
265 /* Stop delaying the catchable fatal signals.  */
266 void
267 unblock_fatal_signals ()
268 {
269   init_fatal_signal_set ();
270   sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
271 }
272
273 #else
274
275 /* Don't bother caring about the old systems which don't have POSIX signal
276    blocking.  */
277
278 void
279 block_fatal_signals ()
280 {
281 }
282
283 void
284 unblock_fatal_signals ()
285 {
286 }
287
288 #endif