2 * Copyright (c) 2008, 2009 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 fatal_signal_add_hook(void (*func)(void *aux), void *aux, bool run_at_exit)
68 assert(n_hooks < MAX_HOOKS);
69 hooks[n_hooks].func = func;
70 hooks[n_hooks].aux = aux;
71 hooks[n_hooks].run_at_exit = run_at_exit;
73 fatal_signal_unblock();
76 /* Blocks program termination signals until fatal_signal_unblock() is called.
77 * May be called multiple times with nesting; if so, fatal_signal_unblock()
78 * must be called the same number of times to unblock signals.
80 * This is needed while adjusting a data structure that will be accessed by a
81 * fatal signal hook, so that the hook is not invoked while the data structure
82 * is in an inconsistent state. */
84 fatal_signal_block(void)
86 static bool inited = false;
91 sigemptyset(&fatal_signal_set);
92 for (i = 0; i < ARRAY_SIZE(fatal_signals); i++) {
93 int sig_nr = fatal_signals[i];
94 struct sigaction old_sa;
96 sigaddset(&fatal_signal_set, sig_nr);
97 if (sigaction(sig_nr, NULL, &old_sa)) {
98 ovs_fatal(errno, "sigaction");
100 if (old_sa.sa_handler == SIG_DFL
101 && signal(sig_nr, fatal_signal_handler) == SIG_ERR) {
102 ovs_fatal(errno, "signal");
105 atexit(atexit_handler);
108 if (++block_level == 1) {
109 call_sigprocmask(SIG_BLOCK, &fatal_signal_set, &saved_signal_mask);
113 /* Unblocks program termination signals blocked by fatal_signal_block() is
114 * called. If multiple calls to fatal_signal_block() are nested,
115 * fatal_signal_unblock() must be called the same number of times to unblock
118 fatal_signal_unblock(void)
120 assert(block_level > 0);
121 if (--block_level == 0) {
122 call_sigprocmask(SIG_SETMASK, &saved_signal_mask, NULL);
126 /* Handles fatal signal number 'sig_nr'.
128 * Ordinarily this is the actual signal handler. When other code needs to
129 * handle one of our signals, however, it can register for that signal and, if
130 * and when necessary, call this function to do fatal signal processing for it
131 * and terminate the process. Currently only timeval.c does this, for SIGALRM.
132 * (It is not important whether the other code sets up its signal handler
133 * before or after this file, because this file will only set up a signal
134 * handler in the case where the signal has its default handling.) */
136 fatal_signal_handler(int sig_nr)
140 /* Re-raise the signal with the default handling so that the program
141 * termination status reflects that we were killed by this signal */
142 signal(sig_nr, SIG_DFL);
155 call_hooks(int sig_nr)
157 static volatile sig_atomic_t recurse = 0;
163 for (i = 0; i < n_hooks; i++) {
164 struct hook *h = &hooks[i];
165 if (sig_nr || h->run_at_exit) {
172 static struct shash files = SHASH_INITIALIZER(&files);
174 static void unlink_files(void *aux);
175 static void do_unlink_files(void);
177 /* Registers 'file' to be unlinked when the program terminates via exit() or a
180 fatal_signal_add_file_to_unlink(const char *file)
182 static bool added_hook = false;
185 fatal_signal_add_hook(unlink_files, NULL, true);
188 fatal_signal_block();
189 if (!shash_find(&files, file)) {
190 shash_add(&files, file, NULL);
192 fatal_signal_unblock();
195 /* Unregisters 'file' from being unlinked when the program terminates via
196 * exit() or a fatal signal. */
198 fatal_signal_remove_file_to_unlink(const char *file)
200 struct shash_node *node;
202 fatal_signal_block();
203 node = shash_find(&files, file);
205 shash_delete(&files, node);
207 fatal_signal_unblock();
210 /* Like fatal_signal_remove_file_to_unlink(), but also unlinks 'file'.
211 * Returns 0 if successful, otherwise a positive errno value. */
213 fatal_signal_unlink_file_now(const char *file)
215 int error = unlink(file) ? errno : 0;
217 VLOG_WARN("could not unlink \"%s\" (%s)", file, strerror(error));
220 fatal_signal_remove_file_to_unlink(file);
226 unlink_files(void *aux UNUSED)
232 do_unlink_files(void)
234 struct shash_node *node;
236 SHASH_FOR_EACH (node, &files) {
241 /* Disables the fatal signal hook mechanism. Following a fork, one of the
242 * resulting processes can call this function to allow it to terminate without
243 * triggering fatal signal processing or removing files. Fatal signal
244 * processing is still enabled in the other process. */
246 fatal_signal_fork(void)
252 for (i = 0; i < ARRAY_SIZE(fatal_signals); i++) {
253 int sig_nr = fatal_signals[i];
254 if (signal(sig_nr, SIG_DFL) == SIG_IGN) {
255 signal(sig_nr, SIG_IGN);
261 call_sigprocmask(int how, sigset_t* new_set, sigset_t* old_set)
263 int error = sigprocmask(how, new_set, old_set);
265 fprintf(stderr, "sigprocmask: %s\n", strerror(errno));