Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/fatal-signal.c @ 6751:1b0092424a44
Include <unistd.h> unconditionally.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Mon, 24 Apr 2006 11:38:06 +0000 (2006-04-24) |
parents | 96c32553b4c6 |
children | 43bb3848f1c7 |
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 | |
20 #ifdef HAVE_CONFIG_H | |
6259
96c32553b4c6
Use a consistent style for including <config.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
21 # include <config.h> |
4770 | 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> | |
6751
1b0092424a44
Include <unistd.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
31 #include <unistd.h> |
4770 | 32 |
33 #include "xalloc.h" | |
34 | |
35 #define SIZEOF(a) (sizeof(a) / sizeof(a[0])) | |
36 | |
37 | |
38 /* ========================================================================= */ | |
39 | |
40 | |
41 /* The list of fatal signals. | |
42 These are those signals whose default action is to terminate the process | |
43 without a core dump, except | |
44 SIGKILL - because it cannot be caught, | |
45 SIGALRM SIGUSR1 SIGUSR2 SIGPOLL SIGIO SIGLOST - because applications | |
46 often use them for their own purpose, | |
47 SIGPROF SIGVTALRM - because they are used for profiling, | |
48 SIGSTKFLT - because it is more similar to SIGFPE, SIGSEGV, SIGBUS, | |
49 SIGSYS - because it is more similar to SIGABRT, SIGSEGV, | |
50 SIGPWR - because it of too special use, | |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
51 SIGRTMIN...SIGRTMAX - because they are reserved for application use. |
4770 | 52 plus |
53 SIGXCPU, SIGXFSZ - because they are quite similar to SIGTERM. */ | |
54 | |
5536
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
55 static int fatal_signals[] = |
4770 | 56 { |
57 /* ISO C 99 signals. */ | |
58 #ifdef SIGINT | |
59 SIGINT, | |
60 #endif | |
61 #ifdef SIGTERM | |
62 SIGTERM, | |
63 #endif | |
64 /* POSIX:2001 signals. */ | |
65 #ifdef SIGHUP | |
66 SIGHUP, | |
67 #endif | |
68 #ifdef SIGPIPE | |
69 SIGPIPE, | |
70 #endif | |
71 /* BSD signals. */ | |
72 #ifdef SIGXCPU | |
73 SIGXCPU, | |
74 #endif | |
75 #ifdef SIGXFSZ | |
76 SIGXFSZ, | |
77 #endif | |
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 |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
99 && 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
|
100 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
|
101 } |
5537
0fc3beabfb42
Portability fix: Don't assume sigaction(). (mingw doesn't have it.)
Bruno Haible <bruno@clisp.org>
parents:
5536
diff
changeset
|
102 #endif |
5536
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 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
|
105 } |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
106 } |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
107 |
4770 | 108 |
109 /* ========================================================================= */ | |
110 | |
111 | |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
112 typedef void (*action_t) (void); |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
113 |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
114 /* Type of an entry in the actions array. |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
115 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
|
116 therefore we mark it as 'volatile'. */ |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
117 typedef struct |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
118 { |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
119 volatile action_t action; |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
120 } |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
121 actions_entry_t; |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
122 |
4770 | 123 /* The registered cleanup actions. */ |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
124 static actions_entry_t static_actions[32]; |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
125 static actions_entry_t * volatile actions = static_actions; |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
126 static sig_atomic_t volatile actions_count = 0; |
4770 | 127 static size_t actions_allocated = SIZEOF (static_actions); |
128 | |
129 | |
130 /* Uninstall the handlers. */ | |
131 static inline void | |
132 uninstall_handlers () | |
133 { | |
134 size_t i; | |
135 | |
136 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
|
137 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
|
138 signal (fatal_signals[i], SIG_DFL); |
4770 | 139 } |
140 | |
141 | |
142 /* The signal handler. It gets called asynchronously. */ | |
143 static void | |
144 fatal_signal_handler (int sig) | |
145 { | |
146 for (;;) | |
147 { | |
148 /* Get the last registered cleanup action, in a reentrant way. */ | |
149 action_t action; | |
150 size_t n = actions_count; | |
151 if (n == 0) | |
152 break; | |
153 n--; | |
154 actions_count = n; | |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
155 action = actions[n].action; |
4770 | 156 /* Execute the action. */ |
157 action (); | |
158 } | |
159 | |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
160 /* Now execute the signal's default action. |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
161 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
|
162 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
|
163 handler returns; otherwise it is delivered already during raise(). */ |
4770 | 164 uninstall_handlers (); |
165 #if HAVE_RAISE | |
166 raise (sig); | |
167 #else | |
168 kill (getpid (), sig); | |
169 #endif | |
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; |
4770 | 204 size_t new_actions_allocated = 2 * actions_allocated; |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
205 actions_entry_t *new_actions = |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
206 xmalloc (new_actions_allocated * sizeof (actions_entry_t)); |
4770 | 207 |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
208 memcpy (new_actions, old_actions, |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
209 actions_allocated * sizeof (actions_entry_t)); |
4770 | 210 actions = new_actions; |
211 actions_allocated = new_actions_allocated; | |
212 /* Now we can free the old actions array. */ | |
213 if (old_actions != static_actions) | |
214 free (old_actions); | |
215 } | |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
216 /* 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
|
217 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
|
218 the new action has been written to the memory location |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
219 actions[actions_count]. */ |
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
220 actions[actions_count].action = action; |
4770 | 221 actions_count++; |
222 } | |
223 | |
224 | |
225 /* ========================================================================= */ | |
226 | |
227 | |
228 #if HAVE_POSIX_SIGNALBLOCKING | |
229 | |
230 static sigset_t fatal_signal_set; | |
231 | |
232 static void | |
233 init_fatal_signal_set () | |
234 { | |
235 static bool fatal_signal_set_initialized = false; | |
236 if (!fatal_signal_set_initialized) | |
237 { | |
238 size_t i; | |
239 | |
5536
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
240 init_fatal_signals (); |
f64f1da7e350
Signals whose handler is set to SIG_IGN are not fatal.
Bruno Haible <bruno@clisp.org>
parents:
4786
diff
changeset
|
241 |
4770 | 242 sigemptyset (&fatal_signal_set); |
243 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
|
244 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
|
245 sigaddset (&fatal_signal_set, fatal_signals[i]); |
4770 | 246 |
247 fatal_signal_set_initialized = true; | |
248 } | |
249 } | |
250 | |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
251 /* Temporarily delay the catchable fatal signals. */ |
4770 | 252 void |
253 block_fatal_signals () | |
254 { | |
255 init_fatal_signal_set (); | |
256 sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL); | |
257 } | |
258 | |
4786
83d8d561903a
Improved 'fatal-signal' module.
Bruno Haible <bruno@clisp.org>
parents:
4770
diff
changeset
|
259 /* Stop delaying the catchable fatal signals. */ |
4770 | 260 void |
261 unblock_fatal_signals () | |
262 { | |
263 init_fatal_signal_set (); | |
264 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL); | |
265 } | |
266 | |
267 #else | |
268 | |
269 /* Don't bother caring about the old systems which don't have POSIX signal | |
270 blocking. */ | |
271 | |
272 void | |
273 block_fatal_signals () | |
274 { | |
275 } | |
276 | |
277 void | |
278 unblock_fatal_signals () | |
279 { | |
280 } | |
281 | |
282 #endif |