Mercurial > gnulib
annotate lib/fatal-signal.c @ 7304:1c4ed7637c24
Include <config.h> unconditionally.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Thu, 14 Sep 2006 14:18:36 +0000 |
parents | 43bb3848f1c7 |
children | 71b958155bb9 |
rev | line source |
---|---|
4770 | 1 /* Emergency actions in case of a fatal signal. |
6751
1b0092424a44
Include <unistd.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
2 Copyright (C) 2003-2004, 2006 Free Software Foundation, Inc. |
4770 | 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, | |
5848
a48fb0e98c8c
*** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents:
5537
diff
changeset
|
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
4770 | 18 |
19 | |
7304
1c4ed7637c24
Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents:
7014
diff
changeset
|
20 #include <config.h> |
4770 | 21 |
22 /* Specification. */ | |
23 #include "fatal-signal.h" | |
24 | |
25 #include <stdbool.h> | |
26 #include <stdlib.h> | |
27 #include <signal.h> | |
6751
1b0092424a44
Include <unistd.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
28 #include <unistd.h> |
4770 | 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, | |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
48 SIGRTMIN...SIGRTMAX - because they are reserved for application use. |
4770 | 49 plus |
50 SIGXCPU, SIGXFSZ - because they are quite similar to SIGTERM. */ | |
51 | |
5536
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
52 static int fatal_signals[] = |
4770 | 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 0 | |
76 }; | |
77 | |
78 #define num_fatal_signals (SIZEOF (fatal_signals) - 1) | |
79 | |
5536
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
80 /* Eliminate signals whose signal handler is SIG_IGN. */ |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
81 |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
82 static void |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
83 init_fatal_signals (void) |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
84 { |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
85 static bool fatal_signals_initialized = false; |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
86 if (!fatal_signals_initialized) |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
87 { |
5537
0fc3beabfb42
Portability fix: Don't assume sigaction(). (mingw doesn't have it.)
Bruno Haible <bruno@clisp.org>
parents:
5536
diff
changeset
|
88 #if HAVE_SIGACTION |
5536
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
89 size_t i; |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
90 |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
91 for (i = 0; i < num_fatal_signals; i++) |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
92 { |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
93 struct sigaction action; |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
94 |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
95 if (sigaction (fatal_signals[i], NULL, &action) >= 0 |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
96 && action.sa_handler == SIG_IGN) |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
97 fatal_signals[i] = -1; |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
98 } |
5537
0fc3beabfb42
Portability fix: Don't assume sigaction(). (mingw doesn't have it.)
Bruno Haible <bruno@clisp.org>
parents:
5536
diff
changeset
|
99 #endif |
5536
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
100 |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
101 fatal_signals_initialized = true; |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
102 } |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
103 } |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
104 |
4770 | 105 |
106 /* ========================================================================= */ | |
107 | |
108 | |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
109 typedef void (*action_t) (void); |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
110 |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
111 /* Type of an entry in the actions array. |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
112 The 'action' field is accessed from within the fatal_signal_handler(), |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
113 therefore we mark it as 'volatile'. */ |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
114 typedef struct |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
115 { |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
116 volatile action_t action; |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
117 } |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
118 actions_entry_t; |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
119 |
4770 | 120 /* The registered cleanup actions. */ |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
121 static actions_entry_t static_actions[32]; |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
122 static actions_entry_t * volatile actions = static_actions; |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
123 static sig_atomic_t volatile actions_count = 0; |
4770 | 124 static size_t actions_allocated = SIZEOF (static_actions); |
125 | |
126 | |
127 /* Uninstall the handlers. */ | |
128 static inline void | |
129 uninstall_handlers () | |
130 { | |
131 size_t i; | |
132 | |
133 for (i = 0; i < num_fatal_signals; i++) | |
5536
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
134 if (fatal_signals[i] >= 0) |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
135 signal (fatal_signals[i], SIG_DFL); |
4770 | 136 } |
137 | |
138 | |
139 /* The signal handler. It gets called asynchronously. */ | |
140 static void | |
141 fatal_signal_handler (int sig) | |
142 { | |
143 for (;;) | |
144 { | |
145 /* Get the last registered cleanup action, in a reentrant way. */ | |
146 action_t action; | |
147 size_t n = actions_count; | |
148 if (n == 0) | |
149 break; | |
150 n--; | |
151 actions_count = n; | |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
152 action = actions[n].action; |
4770 | 153 /* Execute the action. */ |
154 action (); | |
155 } | |
156 | |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
157 /* Now execute the signal's default action. |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
158 If signal() blocks the signal being delivered for the duration of the |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
159 signal handler's execution, the re-raised signal is delivered when this |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
160 handler returns; otherwise it is delivered already during raise(). */ |
4770 | 161 uninstall_handlers (); |
162 #if HAVE_RAISE | |
163 raise (sig); | |
164 #else | |
165 kill (getpid (), sig); | |
166 #endif | |
167 } | |
168 | |
169 | |
170 /* Install the handlers. */ | |
171 static inline void | |
172 install_handlers () | |
173 { | |
174 size_t i; | |
175 | |
176 for (i = 0; i < num_fatal_signals; i++) | |
5536
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
177 if (fatal_signals[i] >= 0) |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
178 signal (fatal_signals[i], &fatal_signal_handler); |
4770 | 179 } |
180 | |
181 | |
182 /* Register a cleanup function to be executed when a catchable fatal signal | |
183 occurs. */ | |
184 void | |
185 at_fatal_signal (action_t action) | |
186 { | |
187 static bool cleanup_initialized = false; | |
188 if (!cleanup_initialized) | |
189 { | |
5536
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
190 init_fatal_signals (); |
4770 | 191 install_handlers (); |
192 cleanup_initialized = true; | |
193 } | |
194 | |
195 if (actions_count == actions_allocated) | |
196 { | |
197 /* Extend the actions array. Note that we cannot use xrealloc(), | |
198 because then the cleanup() function could access an already | |
199 deallocated array. */ | |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
200 actions_entry_t *old_actions = actions; |
7014
43bb3848f1c7
Merge from gettext 0.15: Be more careful to use 'volatile'.
Bruno Haible <bruno@clisp.org>
parents:
6751
diff
changeset
|
201 size_t old_actions_allocated = actions_allocated; |
4770 | 202 size_t new_actions_allocated = 2 * actions_allocated; |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
203 actions_entry_t *new_actions = |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
204 xmalloc (new_actions_allocated * sizeof (actions_entry_t)); |
7014
43bb3848f1c7
Merge from gettext 0.15: Be more careful to use 'volatile'.
Bruno Haible <bruno@clisp.org>
parents:
6751
diff
changeset
|
205 size_t k; |
4770 | 206 |
7014
43bb3848f1c7
Merge from gettext 0.15: Be more careful to use 'volatile'.
Bruno Haible <bruno@clisp.org>
parents:
6751
diff
changeset
|
207 /* Don't use memcpy() here, because memcpy takes non-volatile arguments |
43bb3848f1c7
Merge from gettext 0.15: Be more careful to use 'volatile'.
Bruno Haible <bruno@clisp.org>
parents:
6751
diff
changeset
|
208 and is therefore not guaranteed to complete all memory stores before |
43bb3848f1c7
Merge from gettext 0.15: Be more careful to use 'volatile'.
Bruno Haible <bruno@clisp.org>
parents:
6751
diff
changeset
|
209 the next statement. */ |
43bb3848f1c7
Merge from gettext 0.15: Be more careful to use 'volatile'.
Bruno Haible <bruno@clisp.org>
parents:
6751
diff
changeset
|
210 for (k = 0; k < old_actions_allocated; k++) |
43bb3848f1c7
Merge from gettext 0.15: Be more careful to use 'volatile'.
Bruno Haible <bruno@clisp.org>
parents:
6751
diff
changeset
|
211 new_actions[k] = old_actions[k]; |
4770 | 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) | |
216 free (old_actions); | |
217 } | |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
218 /* The two uses of 'volatile' in the types above (and ISO C 99 section |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
219 5.1.2.3.(5)) ensure that we increment the actions_count only after |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
220 the new action has been written to the memory location |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
221 actions[actions_count]. */ |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
222 actions[actions_count].action = action; |
4770 | 223 actions_count++; |
224 } | |
225 | |
226 | |
227 /* ========================================================================= */ | |
228 | |
229 | |
230 #if HAVE_POSIX_SIGNALBLOCKING | |
231 | |
232 static sigset_t fatal_signal_set; | |
233 | |
234 static void | |
235 init_fatal_signal_set () | |
236 { | |
237 static bool fatal_signal_set_initialized = false; | |
238 if (!fatal_signal_set_initialized) | |
239 { | |
240 size_t i; | |
241 | |
5536
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
242 init_fatal_signals (); |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
243 |
4770 | 244 sigemptyset (&fatal_signal_set); |
245 for (i = 0; i < num_fatal_signals; i++) | |
5536
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
246 if (fatal_signals[i] >= 0) |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
247 sigaddset (&fatal_signal_set, fatal_signals[i]); |
4770 | 248 |
249 fatal_signal_set_initialized = true; | |
250 } | |
251 } | |
252 | |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
253 /* Temporarily delay the catchable fatal signals. */ |
4770 | 254 void |
255 block_fatal_signals () | |
256 { | |
257 init_fatal_signal_set (); | |
258 sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL); | |
259 } | |
260 | |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
261 /* Stop delaying the catchable fatal signals. */ |
4770 | 262 void |
263 unblock_fatal_signals () | |
264 { | |
265 init_fatal_signal_set (); | |
266 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL); | |
267 } | |
268 | |
269 #else | |
270 | |
271 /* Don't bother caring about the old systems which don't have POSIX signal | |
272 blocking. */ | |
273 | |
274 void | |
275 block_fatal_signals () | |
276 { | |
277 } | |
278 | |
279 void | |
280 unblock_fatal_signals () | |
281 { | |
282 } | |
283 | |
284 #endif |