]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
10a89ef0 | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc. |
064af421 | 3 | * |
a14bc59f BP |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
064af421 | 7 | * |
a14bc59f BP |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
064af421 BP |
15 | */ |
16 | #include <config.h> | |
17 | #include "fatal-signal.h" | |
064af421 BP |
18 | #include <errno.h> |
19 | #include <signal.h> | |
20 | #include <stdbool.h> | |
21 | #include <stdio.h> | |
d8b30702 | 22 | #include <stdint.h> |
064af421 BP |
23 | #include <stdlib.h> |
24 | #include <string.h> | |
25 | #include <unistd.h> | |
b847adc6 | 26 | #include "ovs-thread.h" |
d8b30702 | 27 | #include "poll-loop.h" |
411baaac | 28 | #include "shash.h" |
b3c01ed3 | 29 | #include "sset.h" |
279c9e03 | 30 | #include "signals.h" |
d8b30702 | 31 | #include "socket-util.h" |
064af421 | 32 | #include "util.h" |
6a0061cb BP |
33 | #include "vlog.h" |
34 | ||
0c2c9057 SH |
35 | #include "type-props.h" |
36 | ||
37 | #ifndef SIG_ATOMIC_MAX | |
38 | #define SIG_ATOMIC_MAX TYPE_MAXIMUM(sig_atomic_t) | |
39 | #endif | |
40 | ||
d98e6007 | 41 | VLOG_DEFINE_THIS_MODULE(fatal_signal); |
5136ce49 | 42 | |
064af421 | 43 | /* Signals to catch. */ |
84a6cbae | 44 | #ifndef _WIN32 |
064af421 | 45 | static const int fatal_signals[] = { SIGTERM, SIGINT, SIGHUP, SIGALRM }; |
84a6cbae GS |
46 | #else |
47 | static const int fatal_signals[] = { SIGTERM }; | |
48 | #endif | |
064af421 | 49 | |
064af421 BP |
50 | /* Hooks to call upon catching a signal */ |
51 | struct hook { | |
e3830e90 BP |
52 | void (*hook_cb)(void *aux); |
53 | void (*cancel_cb)(void *aux); | |
064af421 BP |
54 | void *aux; |
55 | bool run_at_exit; | |
56 | }; | |
57 | #define MAX_HOOKS 32 | |
58 | static struct hook hooks[MAX_HOOKS]; | |
59 | static size_t n_hooks; | |
60 | ||
d8b30702 | 61 | static int signal_fds[2]; |
45cd2b93 | 62 | static HANDLE wevent; |
d8b30702 | 63 | static volatile sig_atomic_t stored_sig_nr = SIG_ATOMIC_MAX; |
064af421 | 64 | |
97be1538 | 65 | static struct ovs_mutex mutex; |
b847adc6 | 66 | |
064af421 BP |
67 | static void atexit_handler(void); |
68 | static void call_hooks(int sig_nr); | |
0c100540 GS |
69 | #ifdef _WIN32 |
70 | static BOOL WINAPI ConsoleHandlerRoutine(DWORD dwCtrlType); | |
71 | #endif | |
064af421 | 72 | |
b847adc6 BP |
73 | /* Initializes the fatal signal handling module. Calling this function is |
74 | * optional, because calling any other function in the module will also | |
75 | * initialize it. However, in a multithreaded program, the module must be | |
76 | * initialized while the process is still single-threaded. */ | |
77 | void | |
d8b30702 | 78 | fatal_signal_init(void) |
064af421 BP |
79 | { |
80 | static bool inited = false; | |
d8b30702 | 81 | |
064af421 BP |
82 | if (!inited) { |
83 | size_t i; | |
84 | ||
b847adc6 | 85 | assert_single_threaded(); |
064af421 | 86 | inited = true; |
d8b30702 | 87 | |
834d6caf | 88 | ovs_mutex_init_recursive(&mutex); |
84a6cbae | 89 | #ifndef _WIN32 |
c0d95206 | 90 | xpipe_nonblocking(signal_fds); |
84a6cbae GS |
91 | #else |
92 | wevent = CreateEvent(NULL, TRUE, FALSE, NULL); | |
93 | if (!wevent) { | |
94 | char *msg_buf = ovs_lasterror_to_string(); | |
95 | VLOG_FATAL("Failed to create a event (%s).", msg_buf); | |
96 | } | |
0c100540 GS |
97 | |
98 | /* Register a function to handle Ctrl+C. */ | |
99 | SetConsoleCtrlHandler(ConsoleHandlerRoutine, true); | |
84a6cbae | 100 | #endif |
d8b30702 | 101 | |
064af421 BP |
102 | for (i = 0; i < ARRAY_SIZE(fatal_signals); i++) { |
103 | int sig_nr = fatal_signals[i]; | |
84a6cbae | 104 | #ifndef _WIN32 |
064af421 BP |
105 | struct sigaction old_sa; |
106 | ||
279c9e03 | 107 | xsigaction(sig_nr, NULL, &old_sa); |
064af421 BP |
108 | if (old_sa.sa_handler == SIG_DFL |
109 | && signal(sig_nr, fatal_signal_handler) == SIG_ERR) { | |
10a89ef0 | 110 | VLOG_FATAL("signal failed (%s)", ovs_strerror(errno)); |
064af421 | 111 | } |
84a6cbae GS |
112 | #else |
113 | if (signal(sig_nr, fatal_signal_handler) == SIG_ERR) { | |
114 | VLOG_FATAL("signal failed (%s)", ovs_strerror(errno)); | |
115 | } | |
116 | #endif | |
064af421 BP |
117 | } |
118 | atexit(atexit_handler); | |
119 | } | |
064af421 BP |
120 | } |
121 | ||
b847adc6 BP |
122 | /* Registers 'hook_cb' to be called from inside poll_block() following a fatal |
123 | * signal. 'hook_cb' does not need to be async-signal-safe. In a | |
124 | * multithreaded program 'hook_cb' might be called from any thread, with | |
125 | * threads other than the one running 'hook_cb' in unknown states. | |
d8b30702 | 126 | * |
b847adc6 BP |
127 | * If 'run_at_exit' is true, 'hook_cb' is also called during normal process |
128 | * termination, e.g. when exit() is called or when main() returns. | |
e3830e90 BP |
129 | * |
130 | * If the current process forks, fatal_signal_fork() may be called to clear the | |
131 | * parent process's fatal signal hooks, so that 'hook_cb' is only called when | |
132 | * the child terminates, not when the parent does. When fatal_signal_fork() is | |
133 | * called, it calls the 'cancel_cb' function if it is nonnull, passing 'aux', | |
134 | * to notify that the hook has been canceled. This allows the hook to free | |
135 | * memory, etc. */ | |
064af421 | 136 | void |
e3830e90 BP |
137 | fatal_signal_add_hook(void (*hook_cb)(void *aux), void (*cancel_cb)(void *aux), |
138 | void *aux, bool run_at_exit) | |
064af421 | 139 | { |
d8b30702 | 140 | fatal_signal_init(); |
e3830e90 | 141 | |
97be1538 | 142 | ovs_mutex_lock(&mutex); |
cb22974d | 143 | ovs_assert(n_hooks < MAX_HOOKS); |
e3830e90 BP |
144 | hooks[n_hooks].hook_cb = hook_cb; |
145 | hooks[n_hooks].cancel_cb = cancel_cb; | |
d8b30702 JG |
146 | hooks[n_hooks].aux = aux; |
147 | hooks[n_hooks].run_at_exit = run_at_exit; | |
148 | n_hooks++; | |
97be1538 | 149 | ovs_mutex_unlock(&mutex); |
064af421 BP |
150 | } |
151 | ||
152 | /* Handles fatal signal number 'sig_nr'. | |
153 | * | |
154 | * Ordinarily this is the actual signal handler. When other code needs to | |
155 | * handle one of our signals, however, it can register for that signal and, if | |
156 | * and when necessary, call this function to do fatal signal processing for it | |
157 | * and terminate the process. Currently only timeval.c does this, for SIGALRM. | |
158 | * (It is not important whether the other code sets up its signal handler | |
159 | * before or after this file, because this file will only set up a signal | |
160 | * handler in the case where the signal has its default handling.) */ | |
161 | void | |
162 | fatal_signal_handler(int sig_nr) | |
163 | { | |
84a6cbae | 164 | #ifndef _WIN32 |
d8b30702 | 165 | ignore(write(signal_fds[1], "", 1)); |
84a6cbae GS |
166 | #else |
167 | SetEvent(wevent); | |
168 | #endif | |
d8b30702 JG |
169 | stored_sig_nr = sig_nr; |
170 | } | |
171 | ||
b302749b BP |
172 | /* Check whether a fatal signal has occurred and, if so, call the fatal signal |
173 | * hooks and exit. | |
174 | * | |
175 | * This function is called automatically by poll_block(), but specialized | |
176 | * programs that may not always call poll_block() on a regular basis should | |
177 | * also call it periodically. (Therefore, any function with "block" in its | |
178 | * name should call fatal_signal_run() each time it is called, either directly | |
179 | * or through poll_block(), because such functions can only used by specialized | |
180 | * programs that can afford to block outside their main loop around | |
181 | * poll_block().) | |
182 | */ | |
d8b30702 JG |
183 | void |
184 | fatal_signal_run(void) | |
185 | { | |
bf82917b | 186 | sig_atomic_t sig_nr; |
064af421 | 187 | |
c874f17f BP |
188 | fatal_signal_init(); |
189 | ||
190 | sig_nr = stored_sig_nr; | |
d8b30702 | 191 | if (sig_nr != SIG_ATOMIC_MAX) { |
eee8089c BP |
192 | char namebuf[SIGNAL_NAME_BUFSIZE]; |
193 | ||
97be1538 | 194 | ovs_mutex_lock(&mutex); |
b847adc6 | 195 | |
84a6cbae | 196 | #ifndef _WIN32 |
b67b2b0a | 197 | VLOG_WARN("terminating with signal %d (%s)", |
eee8089c | 198 | (int)sig_nr, signal_name(sig_nr, namebuf, sizeof namebuf)); |
84a6cbae GS |
199 | #else |
200 | VLOG_WARN("terminating with signal %d", (int)sig_nr); | |
201 | #endif | |
d8b30702 JG |
202 | call_hooks(sig_nr); |
203 | ||
204 | /* Re-raise the signal with the default handling so that the program | |
205 | * termination status reflects that we were killed by this signal */ | |
206 | signal(sig_nr, SIG_DFL); | |
207 | raise(sig_nr); | |
b847adc6 | 208 | |
97be1538 | 209 | ovs_mutex_unlock(&mutex); |
428b2edd | 210 | OVS_NOT_REACHED(); |
d8b30702 JG |
211 | } |
212 | } | |
213 | ||
214 | void | |
215 | fatal_signal_wait(void) | |
216 | { | |
c874f17f | 217 | fatal_signal_init(); |
84a6cbae | 218 | poll_fd_wait_event(signal_fds[0], wevent, POLLIN); |
064af421 BP |
219 | } |
220 | ||
8a777cf6 GS |
221 | void |
222 | fatal_ignore_sigpipe(void) | |
223 | { | |
224 | #ifndef _WIN32 | |
225 | signal(SIGPIPE, SIG_IGN); | |
226 | #endif | |
227 | } | |
228 | ||
064af421 BP |
229 | static void |
230 | atexit_handler(void) | |
231 | { | |
e3830e90 | 232 | call_hooks(0); |
064af421 BP |
233 | } |
234 | ||
235 | static void | |
236 | call_hooks(int sig_nr) | |
237 | { | |
238 | static volatile sig_atomic_t recurse = 0; | |
239 | if (!recurse) { | |
240 | size_t i; | |
241 | ||
242 | recurse = 1; | |
243 | ||
244 | for (i = 0; i < n_hooks; i++) { | |
245 | struct hook *h = &hooks[i]; | |
246 | if (sig_nr || h->run_at_exit) { | |
e3830e90 | 247 | h->hook_cb(h->aux); |
064af421 BP |
248 | } |
249 | } | |
250 | } | |
251 | } | |
0c100540 GS |
252 | |
253 | #ifdef _WIN32 | |
254 | BOOL WINAPI ConsoleHandlerRoutine(DWORD dwCtrlType) | |
255 | { | |
256 | stored_sig_nr = SIGINT; | |
257 | SetEvent(wevent); | |
258 | return true; | |
259 | } | |
260 | #endif | |
064af421 | 261 | \f |
b3c01ed3 BP |
262 | /* Files to delete on exit. */ |
263 | static struct sset files = SSET_INITIALIZER(&files); | |
064af421 | 264 | |
e3830e90 BP |
265 | /* Has a hook function been registered with fatal_signal_add_hook() (and not |
266 | * cleared by fatal_signal_fork())? */ | |
267 | static bool added_hook; | |
268 | ||
064af421 | 269 | static void unlink_files(void *aux); |
e3830e90 | 270 | static void cancel_files(void *aux); |
064af421 BP |
271 | static void do_unlink_files(void); |
272 | ||
273 | /* Registers 'file' to be unlinked when the program terminates via exit() or a | |
274 | * fatal signal. */ | |
275 | void | |
276 | fatal_signal_add_file_to_unlink(const char *file) | |
277 | { | |
b847adc6 BP |
278 | fatal_signal_init(); |
279 | ||
97be1538 | 280 | ovs_mutex_lock(&mutex); |
064af421 BP |
281 | if (!added_hook) { |
282 | added_hook = true; | |
e3830e90 | 283 | fatal_signal_add_hook(unlink_files, cancel_files, NULL, true); |
064af421 BP |
284 | } |
285 | ||
b3c01ed3 | 286 | sset_add(&files, file); |
97be1538 | 287 | ovs_mutex_unlock(&mutex); |
064af421 BP |
288 | } |
289 | ||
290 | /* Unregisters 'file' from being unlinked when the program terminates via | |
291 | * exit() or a fatal signal. */ | |
292 | void | |
293 | fatal_signal_remove_file_to_unlink(const char *file) | |
294 | { | |
b847adc6 BP |
295 | fatal_signal_init(); |
296 | ||
97be1538 | 297 | ovs_mutex_lock(&mutex); |
b3c01ed3 | 298 | sset_find_and_delete(&files, file); |
97be1538 | 299 | ovs_mutex_unlock(&mutex); |
064af421 BP |
300 | } |
301 | ||
6a0061cb BP |
302 | /* Like fatal_signal_remove_file_to_unlink(), but also unlinks 'file'. |
303 | * Returns 0 if successful, otherwise a positive errno value. */ | |
304 | int | |
305 | fatal_signal_unlink_file_now(const char *file) | |
306 | { | |
b847adc6 BP |
307 | int error; |
308 | ||
309 | fatal_signal_init(); | |
310 | ||
97be1538 | 311 | ovs_mutex_lock(&mutex); |
b847adc6 BP |
312 | |
313 | error = unlink(file) ? errno : 0; | |
6a0061cb | 314 | if (error) { |
10a89ef0 | 315 | VLOG_WARN("could not unlink \"%s\" (%s)", file, ovs_strerror(error)); |
6a0061cb BP |
316 | } |
317 | ||
318 | fatal_signal_remove_file_to_unlink(file); | |
319 | ||
97be1538 | 320 | ovs_mutex_unlock(&mutex); |
b847adc6 | 321 | |
6a0061cb BP |
322 | return error; |
323 | } | |
324 | ||
064af421 | 325 | static void |
67a4917b | 326 | unlink_files(void *aux OVS_UNUSED) |
064af421 | 327 | { |
d295e8e9 | 328 | do_unlink_files(); |
064af421 BP |
329 | } |
330 | ||
e3830e90 | 331 | static void |
c69ee87c | 332 | cancel_files(void *aux OVS_UNUSED) |
e3830e90 | 333 | { |
b3c01ed3 | 334 | sset_clear(&files); |
e3830e90 BP |
335 | added_hook = false; |
336 | } | |
337 | ||
064af421 BP |
338 | static void |
339 | do_unlink_files(void) | |
340 | { | |
b3c01ed3 | 341 | const char *file; |
064af421 | 342 | |
b3c01ed3 BP |
343 | SSET_FOR_EACH (file, &files) { |
344 | unlink(file); | |
064af421 BP |
345 | } |
346 | } | |
347 | \f | |
e3830e90 BP |
348 | /* Clears all of the fatal signal hooks without executing them. If any of the |
349 | * hooks passed a 'cancel_cb' function to fatal_signal_add_hook(), then those | |
350 | * functions will be called, allowing them to free resources, etc. | |
351 | * | |
352 | * Following a fork, one of the resulting processes can call this function to | |
353 | * allow it to terminate without calling the hooks registered before calling | |
354 | * this function. New hooks registered after calling this function will take | |
355 | * effect normally. */ | |
064af421 BP |
356 | void |
357 | fatal_signal_fork(void) | |
358 | { | |
359 | size_t i; | |
360 | ||
b847adc6 BP |
361 | assert_single_threaded(); |
362 | ||
e3830e90 BP |
363 | for (i = 0; i < n_hooks; i++) { |
364 | struct hook *h = &hooks[i]; | |
365 | if (h->cancel_cb) { | |
366 | h->cancel_cb(h->aux); | |
064af421 BP |
367 | } |
368 | } | |
e3830e90 | 369 | n_hooks = 0; |
d8b30702 JG |
370 | |
371 | /* Raise any signals that we have already received with the default | |
372 | * handler. */ | |
373 | if (stored_sig_nr != SIG_ATOMIC_MAX) { | |
374 | raise(stored_sig_nr); | |
064af421 BP |
375 | } |
376 | } | |
1481a755 AA |
377 | |
378 | #ifndef _WIN32 | |
379 | /* Blocks all fatal signals and returns previous signal mask into | |
380 | * 'prev_mask'. */ | |
381 | void | |
382 | fatal_signal_block(sigset_t *prev_mask) | |
383 | { | |
384 | int i; | |
385 | sigset_t block_mask; | |
386 | ||
387 | sigemptyset(&block_mask); | |
388 | for (i = 0; i < ARRAY_SIZE(fatal_signals); i++) { | |
389 | int sig_nr = fatal_signals[i]; | |
390 | sigaddset(&block_mask, sig_nr); | |
391 | } | |
392 | xpthread_sigmask(SIG_BLOCK, &block_mask, prev_mask); | |
393 | } | |
394 | #endif |