mktime.c: normalize tp->tm_isdst value to -1/0/1.
[pspp] / lib / fatal-signal.c
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.
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 3 of the License, or
8    (at your option) 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, see <http://www.gnu.org/licenses/>.  */
17
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include "fatal-signal.h"
23
24 #include <stdbool.h>
25 #include <stdlib.h>
26 #include <signal.h>
27 #include <unistd.h>
28
29 #include "sig-handler.h"
30 #include "xalloc.h"
31
32 #define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
33
34 /* ========================================================================= */
35
36
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.
48    plus
49      SIGXCPU, SIGXFSZ - because they are quite similar to SIGTERM.  */
50
51 static int fatal_signals[] =
52   {
53     /* ISO C 99 signals.  */
54 #ifdef SIGINT
55     SIGINT,
56 #endif
57 #ifdef SIGTERM
58     SIGTERM,
59 #endif
60     /* POSIX:2001 signals.  */
61 #ifdef SIGHUP
62     SIGHUP,
63 #endif
64 #ifdef SIGPIPE
65     SIGPIPE,
66 #endif
67     /* BSD signals.  */
68 #ifdef SIGXCPU
69     SIGXCPU,
70 #endif
71 #ifdef SIGXFSZ
72     SIGXFSZ,
73 #endif
74     /* Woe32 signals.  */
75 #ifdef SIGBREAK
76     SIGBREAK,
77 #endif
78     0
79   };
80
81 #define num_fatal_signals (SIZEOF (fatal_signals) - 1)
82
83 /* Eliminate signals whose signal handler is SIG_IGN.  */
84
85 static void
86 init_fatal_signals (void)
87 {
88   static bool fatal_signals_initialized = false;
89   if (!fatal_signals_initialized)
90     {
91       size_t i;
92
93       for (i = 0; i < num_fatal_signals; i++)
94         {
95           struct sigaction action;
96
97           if (sigaction (fatal_signals[i], NULL, &action) >= 0
98               && get_handler (&action) == SIG_IGN)
99             fatal_signals[i] = -1;
100         }
101
102       fatal_signals_initialized = true;
103     }
104 }
105
106
107 /* ========================================================================= */
108
109
110 typedef void (*action_t) (void);
111
112 /* Type of an entry in the actions array.
113    The 'action' field is accessed from within the fatal_signal_handler(),
114    therefore we mark it as 'volatile'.  */
115 typedef struct
116 {
117   volatile action_t action;
118 }
119 actions_entry_t;
120
121 /* The registered cleanup actions.  */
122 static actions_entry_t static_actions[32];
123 static actions_entry_t * volatile actions = static_actions;
124 static sig_atomic_t volatile actions_count = 0;
125 static size_t actions_allocated = SIZEOF (static_actions);
126
127
128 /* Uninstall the handlers.  */
129 static inline void
130 uninstall_handlers ()
131 {
132   size_t i;
133   struct sigaction action;
134
135   action.sa_handler = SIG_DFL;
136   action.sa_flags = 0;
137   sigemptyset (&action.sa_mask);
138   for (i = 0; i < num_fatal_signals; i++)
139     if (fatal_signals[i] >= 0)
140       sigaction (fatal_signals[i], &action, NULL);
141 }
142
143
144 /* The signal handler.  It gets called asynchronously.  */
145 static void
146 fatal_signal_handler (int sig)
147 {
148   for (;;)
149     {
150       /* Get the last registered cleanup action, in a reentrant way.  */
151       action_t action;
152       size_t n = actions_count;
153       if (n == 0)
154         break;
155       n--;
156       actions_count = n;
157       action = actions[n].action;
158       /* Execute the action.  */
159       action ();
160     }
161
162   /* Now execute the signal's default action.
163      If the signal being delivered was blocked, the re-raised signal would be
164      delivered when this handler returns.  But the way we install this handler,
165      no signal is blocked, and the re-raised signal is delivered already
166      during raise().  */
167   uninstall_handlers ();
168   raise (sig);
169 }
170
171
172 /* Install the handlers.  */
173 static inline void
174 install_handlers ()
175 {
176   size_t i;
177   struct sigaction action;
178
179   action.sa_handler = &fatal_signal_handler;
180   /* If we get a fatal signal while executing fatal_signal_handler, enter
181      fatal_signal_handler recursively, since it is reentrant.  Hence no
182      SA_RESETHAND.  */
183   action.sa_flags = SA_NODEFER;
184   sigemptyset (&action.sa_mask);
185   for (i = 0; i < num_fatal_signals; i++)
186     if (fatal_signals[i] >= 0)
187       sigaction (fatal_signals[i], &action, NULL);
188 }
189
190
191 /* Register a cleanup function to be executed when a catchable fatal signal
192    occurs.  */
193 void
194 at_fatal_signal (action_t action)
195 {
196   static bool cleanup_initialized = false;
197   if (!cleanup_initialized)
198     {
199       init_fatal_signals ();
200       install_handlers ();
201       cleanup_initialized = true;
202     }
203
204   if (actions_count == actions_allocated)
205     {
206       /* Extend the actions array.  Note that we cannot use xrealloc(),
207          because then the cleanup() function could access an already
208          deallocated array.  */
209       actions_entry_t *old_actions = actions;
210       size_t old_actions_allocated = actions_allocated;
211       size_t new_actions_allocated = 2 * actions_allocated;
212       actions_entry_t *new_actions =
213         XNMALLOC (new_actions_allocated, actions_entry_t);
214       size_t k;
215
216       /* Don't use memcpy() here, because memcpy takes non-volatile arguments
217          and is therefore not guaranteed to complete all memory stores before
218          the next statement.  */
219       for (k = 0; k < old_actions_allocated; k++)
220         new_actions[k] = old_actions[k];
221       actions = new_actions;
222       actions_allocated = new_actions_allocated;
223       /* Now we can free the old actions array.  */
224       if (old_actions != static_actions)
225         free (old_actions);
226     }
227   /* The two uses of 'volatile' in the types above (and ISO C 99 section
228      5.1.2.3.(5)) ensure that we increment the actions_count only after
229      the new action has been written to the memory location
230      actions[actions_count].  */
231   actions[actions_count].action = action;
232   actions_count++;
233 }
234
235
236 /* ========================================================================= */
237
238
239 static sigset_t fatal_signal_set;
240
241 static void
242 init_fatal_signal_set ()
243 {
244   static bool fatal_signal_set_initialized = false;
245   if (!fatal_signal_set_initialized)
246     {
247       size_t i;
248
249       init_fatal_signals ();
250
251       sigemptyset (&fatal_signal_set);
252       for (i = 0; i < num_fatal_signals; i++)
253         if (fatal_signals[i] >= 0)
254           sigaddset (&fatal_signal_set, fatal_signals[i]);
255
256       fatal_signal_set_initialized = true;
257     }
258 }
259
260 /* Temporarily delay the catchable fatal signals.  */
261 void
262 block_fatal_signals ()
263 {
264   init_fatal_signal_set ();
265   sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
266 }
267
268 /* Stop delaying the catchable fatal signals.  */
269 void
270 unblock_fatal_signals ()
271 {
272   init_fatal_signal_set ();
273   sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
274 }