Mercurial > gnulib
annotate lib/fatal-signal.c @ 10221:8c0d36d59e97
Add a comment.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Thu, 19 Jun 2008 03:34:29 +0200 |
parents | 9f848a3cc15a |
children | 54813304edd2 |
rev | line source |
---|---|
4770 | 1 /* Emergency actions in case of a fatal signal. |
10210 | 2 Copyright (C) 2003-2004, 2006-2008 Free Software Foundation, Inc. |
4770 | 3 Written by Bruno Haible <bruno@clisp.org>, 2003. |
4 | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8783
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
4770 | 6 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8783
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8783
diff
changeset
|
8 (at your option) any later version. |
4770 | 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 | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8783
diff
changeset
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
4770 | 17 |
18 | |
7304
1c4ed7637c24
Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents:
7014
diff
changeset
|
19 #include <config.h> |
4770 | 20 |
21 /* Specification. */ | |
22 #include "fatal-signal.h" | |
23 | |
24 #include <stdbool.h> | |
25 #include <stdlib.h> | |
26 #include <signal.h> | |
6751
1b0092424a44
Include <unistd.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
27 #include <unistd.h> |
4770 | 28 |
29 #include "xalloc.h" | |
30 | |
31 #define SIZEOF(a) (sizeof(a) / sizeof(a[0])) | |
32 | |
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, | |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
47 SIGRTMIN...SIGRTMAX - because they are reserved for application use. |
4770 | 48 plus |
49 SIGXCPU, SIGXFSZ - because they are quite similar to SIGTERM. */ | |
50 | |
5536
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
51 static int fatal_signals[] = |
4770 | 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 | |
7404
71b958155bb9
Handle the Woe32 SIGBREAK too.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
74 /* Woe32 signals. */ |
71b958155bb9
Handle the Woe32 SIGBREAK too.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
75 #ifdef SIGBREAK |
71b958155bb9
Handle the Woe32 SIGBREAK too.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
76 SIGBREAK, |
71b958155bb9
Handle the Woe32 SIGBREAK too.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
77 #endif |
4770 | 78 0 |
79 }; | |
80 | |
81 #define num_fatal_signals (SIZEOF (fatal_signals) - 1) | |
82 | |
5536
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
83 /* 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
|
84 |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
85 static void |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
86 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
|
87 { |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
88 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
|
89 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
|
90 { |
5537
0fc3beabfb42
Portability fix: Don't assume sigaction(). (mingw doesn't have it.)
Bruno Haible <bruno@clisp.org>
parents:
5536
diff
changeset
|
91 #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
|
92 size_t i; |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
93 |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
94 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
|
95 { |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
96 struct sigaction action; |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
97 |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
98 if (sigaction (fatal_signals[i], NULL, &action) >= 0 |
10221 | 99 /* POSIX says that SIG_IGN can only occur when action.sa_flags |
100 does not contain SA_SIGINFO. But in Linux 2.4, for example, | |
101 SA_SIGINFO can actually be set and is ignored when sa_handler | |
102 is SIG_IGN. So don't bother testing for SA_SIGINFO. */ | |
5536
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
103 && 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
|
104 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
|
105 } |
5537
0fc3beabfb42
Portability fix: Don't assume sigaction(). (mingw doesn't have it.)
Bruno Haible <bruno@clisp.org>
parents:
5536
diff
changeset
|
106 #endif |
5536
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
107 |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
108 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
|
109 } |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
110 } |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
111 |
4770 | 112 |
113 /* ========================================================================= */ | |
114 | |
115 | |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
116 typedef void (*action_t) (void); |
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 /* Type of an entry in the actions array. |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
119 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
|
120 therefore we mark it as 'volatile'. */ |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
121 typedef struct |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
122 { |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
123 volatile action_t action; |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
124 } |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
125 actions_entry_t; |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
126 |
4770 | 127 /* The registered cleanup actions. */ |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
128 static actions_entry_t static_actions[32]; |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
129 static actions_entry_t * volatile actions = static_actions; |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
130 static sig_atomic_t volatile actions_count = 0; |
4770 | 131 static size_t actions_allocated = SIZEOF (static_actions); |
132 | |
133 | |
134 /* Uninstall the handlers. */ | |
135 static inline void | |
136 uninstall_handlers () | |
137 { | |
138 size_t i; | |
139 | |
140 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
|
141 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
|
142 signal (fatal_signals[i], SIG_DFL); |
4770 | 143 } |
144 | |
145 | |
146 /* The signal handler. It gets called asynchronously. */ | |
147 static void | |
148 fatal_signal_handler (int sig) | |
149 { | |
150 for (;;) | |
151 { | |
152 /* Get the last registered cleanup action, in a reentrant way. */ | |
153 action_t action; | |
154 size_t n = actions_count; | |
155 if (n == 0) | |
156 break; | |
157 n--; | |
158 actions_count = n; | |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
159 action = actions[n].action; |
4770 | 160 /* Execute the action. */ |
161 action (); | |
162 } | |
163 | |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
164 /* Now execute the signal's default action. |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
165 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
|
166 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
|
167 handler returns; otherwise it is delivered already during raise(). */ |
4770 | 168 uninstall_handlers (); |
169 raise (sig); | |
170 } | |
171 | |
172 | |
173 /* Install the handlers. */ | |
174 static inline void | |
175 install_handlers () | |
176 { | |
177 size_t i; | |
178 | |
179 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
|
180 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
|
181 signal (fatal_signals[i], &fatal_signal_handler); |
4770 | 182 } |
183 | |
184 | |
185 /* Register a cleanup function to be executed when a catchable fatal signal | |
186 occurs. */ | |
187 void | |
188 at_fatal_signal (action_t action) | |
189 { | |
190 static bool cleanup_initialized = false; | |
191 if (!cleanup_initialized) | |
192 { | |
5536
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
193 init_fatal_signals (); |
4770 | 194 install_handlers (); |
195 cleanup_initialized = true; | |
196 } | |
197 | |
198 if (actions_count == actions_allocated) | |
199 { | |
200 /* Extend the actions array. Note that we cannot use xrealloc(), | |
201 because then the cleanup() function could access an already | |
202 deallocated array. */ | |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
203 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
|
204 size_t old_actions_allocated = actions_allocated; |
4770 | 205 size_t new_actions_allocated = 2 * actions_allocated; |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
206 actions_entry_t *new_actions = |
7603
23f14c284219
Simplify xmalloc expressions. Add overflow check in xmalloc arguments.
Bruno Haible <bruno@clisp.org>
parents:
7586
diff
changeset
|
207 XNMALLOC (new_actions_allocated, 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
|
208 size_t k; |
4770 | 209 |
7014
43bb3848f1c7
Merge from gettext 0.15: Be more careful to use 'volatile'.
Bruno Haible <bruno@clisp.org>
parents:
6751
diff
changeset
|
210 /* 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
|
211 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
|
212 the next statement. */ |
43bb3848f1c7
Merge from gettext 0.15: Be more careful to use 'volatile'.
Bruno Haible <bruno@clisp.org>
parents:
6751
diff
changeset
|
213 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
|
214 new_actions[k] = old_actions[k]; |
4770 | 215 actions = new_actions; |
216 actions_allocated = new_actions_allocated; | |
217 /* Now we can free the old actions array. */ | |
218 if (old_actions != static_actions) | |
219 free (old_actions); | |
220 } | |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
221 /* 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
|
222 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
|
223 the new action has been written to the memory location |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
224 actions[actions_count]. */ |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
225 actions[actions_count].action = action; |
4770 | 226 actions_count++; |
227 } | |
228 | |
229 | |
230 /* ========================================================================= */ | |
231 | |
232 | |
233 static sigset_t fatal_signal_set; | |
234 | |
235 static void | |
236 init_fatal_signal_set () | |
237 { | |
238 static bool fatal_signal_set_initialized = false; | |
239 if (!fatal_signal_set_initialized) | |
240 { | |
241 size_t i; | |
242 | |
5536
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
243 init_fatal_signals (); |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
244 |
4770 | 245 sigemptyset (&fatal_signal_set); |
246 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
|
247 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
|
248 sigaddset (&fatal_signal_set, fatal_signals[i]); |
4770 | 249 |
250 fatal_signal_set_initialized = true; | |
251 } | |
252 } | |
253 | |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
254 /* Temporarily delay the catchable fatal signals. */ |
4770 | 255 void |
256 block_fatal_signals () | |
257 { | |
258 init_fatal_signal_set (); | |
259 sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL); | |
260 } | |
261 | |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
262 /* Stop delaying the catchable fatal signals. */ |
4770 | 263 void |
264 unblock_fatal_signals () | |
265 { | |
266 init_fatal_signal_set (); | |
267 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL); | |
268 } |