2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "fatal-signal.h"
29 #define THIS_MODULE VLM_fatal_signal
32 /* Signals to catch. */
33 static const int fatal_signals[] = { SIGTERM, SIGINT, SIGHUP, SIGALRM };
35 /* Signals to catch as a sigset_t. */
36 static sigset_t fatal_signal_set;
38 /* Hooks to call upon catching a signal */
40 void (*func)(void *aux);
45 static struct hook hooks[MAX_HOOKS];
46 static size_t n_hooks;
48 /* Number of nesting signal blockers. */
49 static int block_level = 0;
51 /* Signal mask saved by outermost signal blocker. */
52 static sigset_t saved_signal_mask;
54 /* Disabled by fatal_signal_fork()? */
57 static void call_sigprocmask(int how, sigset_t* new_set, sigset_t* old_set);
58 static void atexit_handler(void);
59 static void call_hooks(int sig_nr);
61 /* Registers 'hook' to be called when a process termination signal is raised.
62 * If 'run_at_exit' is true, 'hook' is also called during normal process
63 * termination, e.g. when exit() is called or when main() returns.
65 * 'func' will be invoked from an asynchronous signal handler, so it must be
66 * written appropriately. For example, it must not call most C library
67 * functions, including malloc() or free(). */
69 fatal_signal_add_hook(void (*func)(void *aux), void *aux, bool run_at_exit)
72 assert(n_hooks < MAX_HOOKS);
73 hooks[n_hooks].func = func;
74 hooks[n_hooks].aux = aux;
75 hooks[n_hooks].run_at_exit = run_at_exit;
77 fatal_signal_unblock();
80 /* Blocks program termination signals until fatal_signal_unblock() is called.
81 * May be called multiple times with nesting; if so, fatal_signal_unblock()
82 * must be called the same number of times to unblock signals.
84 * This is needed while adjusting a data structure that will be accessed by a
85 * fatal signal hook, so that the hook is not invoked while the data structure
86 * is in an inconsistent state. */
88 fatal_signal_block(void)
90 static bool inited = false;
95 sigemptyset(&fatal_signal_set);
96 for (i = 0; i < ARRAY_SIZE(fatal_signals); i++) {
97 int sig_nr = fatal_signals[i];
98 struct sigaction old_sa;
100 sigaddset(&fatal_signal_set, sig_nr);
101 if (sigaction(sig_nr, NULL, &old_sa)) {
102 ovs_fatal(errno, "sigaction");
104 if (old_sa.sa_handler == SIG_DFL
105 && signal(sig_nr, fatal_signal_handler) == SIG_ERR) {
106 ovs_fatal(errno, "signal");
109 atexit(atexit_handler);
112 if (++block_level == 1) {
113 call_sigprocmask(SIG_BLOCK, &fatal_signal_set, &saved_signal_mask);
117 /* Unblocks program termination signals blocked by fatal_signal_block() is
118 * called. If multiple calls to fatal_signal_block() are nested,
119 * fatal_signal_unblock() must be called the same number of times to unblock
122 fatal_signal_unblock(void)
124 assert(block_level > 0);
125 if (--block_level == 0) {
126 call_sigprocmask(SIG_SETMASK, &saved_signal_mask, NULL);
130 /* Handles fatal signal number 'sig_nr'.
132 * Ordinarily this is the actual signal handler. When other code needs to
133 * handle one of our signals, however, it can register for that signal and, if
134 * and when necessary, call this function to do fatal signal processing for it
135 * and terminate the process. Currently only timeval.c does this, for SIGALRM.
136 * (It is not important whether the other code sets up its signal handler
137 * before or after this file, because this file will only set up a signal
138 * handler in the case where the signal has its default handling.) */
140 fatal_signal_handler(int sig_nr)
144 /* Re-raise the signal with the default handling so that the program
145 * termination status reflects that we were killed by this signal */
146 signal(sig_nr, SIG_DFL);
159 call_hooks(int sig_nr)
161 static volatile sig_atomic_t recurse = 0;
167 for (i = 0; i < n_hooks; i++) {
168 struct hook *h = &hooks[i];
169 if (sig_nr || h->run_at_exit) {
176 static struct shash files = SHASH_INITIALIZER(&files);
178 static void unlink_files(void *aux);
179 static void do_unlink_files(void);
181 /* Registers 'file' to be unlinked when the program terminates via exit() or a
184 fatal_signal_add_file_to_unlink(const char *file)
186 static bool added_hook = false;
189 fatal_signal_add_hook(unlink_files, NULL, true);
192 fatal_signal_block();
193 if (!shash_find(&files, file)) {
194 shash_add(&files, file, NULL);
196 fatal_signal_unblock();
199 /* Unregisters 'file' from being unlinked when the program terminates via
200 * exit() or a fatal signal. */
202 fatal_signal_remove_file_to_unlink(const char *file)
204 struct shash_node *node;
206 fatal_signal_block();
207 node = shash_find(&files, file);
209 shash_delete(&files, node);
211 fatal_signal_unblock();
214 /* Like fatal_signal_remove_file_to_unlink(), but also unlinks 'file'.
215 * Returns 0 if successful, otherwise a positive errno value. */
217 fatal_signal_unlink_file_now(const char *file)
219 int error = unlink(file) ? errno : 0;
221 VLOG_WARN("could not unlink \"%s\" (%s)", file, strerror(error));
224 fatal_signal_remove_file_to_unlink(file);
230 unlink_files(void *aux OVS_UNUSED)
235 /* This is a fatal_signal_add_hook() callback (via unlink_files()). It will be
236 * invoked from an asynchronous signal handler, so it cannot call most C
237 * library functions (unlink() is an explicit exception, see
238 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html).
239 * That includes free(), so it doesn't try to free the 'files' data
242 do_unlink_files(void)
244 struct shash_node *node;
246 SHASH_FOR_EACH (node, &files) {
251 /* Disables the fatal signal hook mechanism. Following a fork, one of the
252 * resulting processes can call this function to allow it to terminate without
253 * triggering fatal signal processing or removing files. Fatal signal
254 * processing is still enabled in the other process. */
256 fatal_signal_fork(void)
262 for (i = 0; i < ARRAY_SIZE(fatal_signals); i++) {
263 int sig_nr = fatal_signals[i];
264 if (signal(sig_nr, SIG_DFL) == SIG_IGN) {
265 signal(sig_nr, SIG_IGN);
271 call_sigprocmask(int how, sigset_t* new_set, sigset_t* old_set)
273 int error = sigprocmask(how, new_set, old_set);
275 fprintf(stderr, "sigprocmask: %s\n", strerror(errno));