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"
28 /* Signals to catch. */
29 static const int fatal_signals[] = { SIGTERM, SIGINT, SIGHUP, SIGALRM };
31 /* Signals to catch as a sigset_t. */
32 static sigset_t fatal_signal_set;
34 /* Hooks to call upon catching a signal */
36 void (*func)(void *aux);
41 static struct hook hooks[MAX_HOOKS];
42 static size_t n_hooks;
44 /* Number of nesting signal blockers. */
45 static int block_level = 0;
47 /* Signal mask saved by outermost signal blocker. */
48 static sigset_t saved_signal_mask;
50 /* Disabled by fatal_signal_fork()? */
53 static void call_sigprocmask(int how, sigset_t* new_set, sigset_t* old_set);
54 static void atexit_handler(void);
55 static void call_hooks(int sig_nr);
57 /* Registers 'hook' to be called when a process termination signal is raised.
58 * If 'run_at_exit' is true, 'hook' is also called during normal process
59 * termination, e.g. when exit() is called or when main() returns. */
61 fatal_signal_add_hook(void (*func)(void *aux), void *aux, bool run_at_exit)
64 assert(n_hooks < MAX_HOOKS);
65 hooks[n_hooks].func = func;
66 hooks[n_hooks].aux = aux;
67 hooks[n_hooks].run_at_exit = run_at_exit;
69 fatal_signal_unblock();
72 /* Blocks program termination signals until fatal_signal_unblock() is called.
73 * May be called multiple times with nesting; if so, fatal_signal_unblock()
74 * must be called the same number of times to unblock signals.
76 * This is needed while adjusting a data structure that will be accessed by a
77 * fatal signal hook, so that the hook is not invoked while the data structure
78 * is in an inconsistent state. */
80 fatal_signal_block(void)
82 static bool inited = false;
87 sigemptyset(&fatal_signal_set);
88 for (i = 0; i < ARRAY_SIZE(fatal_signals); i++) {
89 int sig_nr = fatal_signals[i];
90 struct sigaction old_sa;
92 sigaddset(&fatal_signal_set, sig_nr);
93 if (sigaction(sig_nr, NULL, &old_sa)) {
94 ovs_fatal(errno, "sigaction");
96 if (old_sa.sa_handler == SIG_DFL
97 && signal(sig_nr, fatal_signal_handler) == SIG_ERR) {
98 ovs_fatal(errno, "signal");
101 atexit(atexit_handler);
104 if (++block_level == 1) {
105 call_sigprocmask(SIG_BLOCK, &fatal_signal_set, &saved_signal_mask);
109 /* Unblocks program termination signals blocked by fatal_signal_block() is
110 * called. If multiple calls to fatal_signal_block() are nested,
111 * fatal_signal_unblock() must be called the same number of times to unblock
114 fatal_signal_unblock(void)
116 assert(block_level > 0);
117 if (--block_level == 0) {
118 call_sigprocmask(SIG_SETMASK, &saved_signal_mask, NULL);
122 /* Handles fatal signal number 'sig_nr'.
124 * Ordinarily this is the actual signal handler. When other code needs to
125 * handle one of our signals, however, it can register for that signal and, if
126 * and when necessary, call this function to do fatal signal processing for it
127 * and terminate the process. Currently only timeval.c does this, for SIGALRM.
128 * (It is not important whether the other code sets up its signal handler
129 * before or after this file, because this file will only set up a signal
130 * handler in the case where the signal has its default handling.) */
132 fatal_signal_handler(int sig_nr)
136 /* Re-raise the signal with the default handling so that the program
137 * termination status reflects that we were killed by this signal */
138 signal(sig_nr, SIG_DFL);
151 call_hooks(int sig_nr)
153 static volatile sig_atomic_t recurse = 0;
159 for (i = 0; i < n_hooks; i++) {
160 struct hook *h = &hooks[i];
161 if (sig_nr || h->run_at_exit) {
169 static size_t n_files, max_files;
171 static void unlink_files(void *aux);
172 static void do_unlink_files(void);
174 /* Registers 'file' to be unlinked when the program terminates via exit() or a
177 fatal_signal_add_file_to_unlink(const char *file)
179 static bool added_hook = false;
182 fatal_signal_add_hook(unlink_files, NULL, true);
185 fatal_signal_block();
186 if (n_files >= max_files) {
187 files = x2nrealloc(files, &max_files, sizeof *files);
189 files[n_files++] = xstrdup(file);
190 fatal_signal_unblock();
193 /* Unregisters 'file' from being unlinked when the program terminates via
194 * exit() or a fatal signal. */
196 fatal_signal_remove_file_to_unlink(const char *file)
200 fatal_signal_block();
201 for (i = 0; i < n_files; i++) {
202 if (!strcmp(files[i], file)) {
204 files[i] = files[--n_files];
208 fatal_signal_unblock();
212 unlink_files(void *aux UNUSED)
218 do_unlink_files(void)
222 for (i = 0; i < n_files; i++) {
227 /* Disables the fatal signal hook mechanism. Following a fork, one of the
228 * resulting processes can call this function to allow it to terminate without
229 * triggering fatal signal processing or removing files. Fatal signal
230 * processing is still enabled in the other process. */
232 fatal_signal_fork(void)
238 for (i = 0; i < ARRAY_SIZE(fatal_signals); i++) {
239 int sig_nr = fatal_signals[i];
240 if (signal(sig_nr, SIG_DFL) == SIG_IGN) {
241 signal(sig_nr, SIG_IGN);
247 call_sigprocmask(int how, sigset_t* new_set, sigset_t* old_set)
249 int error = sigprocmask(how, new_set, old_set);
251 fprintf(stderr, "sigprocmask: %s\n", strerror(errno));