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"
27 #include "poll-loop.h"
29 #include "socket-util.h"
32 #define THIS_MODULE VLM_fatal_signal
35 /* Signals to catch. */
36 static const int fatal_signals[] = { SIGTERM, SIGINT, SIGHUP, SIGALRM };
38 /* Signals to catch as a sigset_t. */
39 static sigset_t fatal_signal_set;
41 /* Hooks to call upon catching a signal */
43 void (*func)(void *aux);
48 static struct hook hooks[MAX_HOOKS];
49 static size_t n_hooks;
51 static int signal_fds[2];
52 static volatile sig_atomic_t stored_sig_nr = SIG_ATOMIC_MAX;
54 /* Disabled by fatal_signal_fork()? */
57 static void fatal_signal_init(void);
58 static void atexit_handler(void);
59 static void call_hooks(int sig_nr);
62 fatal_signal_init(void)
64 static bool inited = false;
71 if (pipe(signal_fds)) {
72 ovs_fatal(errno, "could not create pipe");
74 set_nonblocking(signal_fds[0]);
75 set_nonblocking(signal_fds[1]);
77 sigemptyset(&fatal_signal_set);
78 for (i = 0; i < ARRAY_SIZE(fatal_signals); i++) {
79 int sig_nr = fatal_signals[i];
80 struct sigaction old_sa;
82 sigaddset(&fatal_signal_set, sig_nr);
83 if (sigaction(sig_nr, NULL, &old_sa)) {
84 ovs_fatal(errno, "sigaction");
86 if (old_sa.sa_handler == SIG_DFL
87 && signal(sig_nr, fatal_signal_handler) == SIG_ERR) {
88 ovs_fatal(errno, "signal");
91 atexit(atexit_handler);
95 /* Registers 'hook' to be called when a process termination signal is raised.
96 * If 'run_at_exit' is true, 'hook' is also called during normal process
97 * termination, e.g. when exit() is called or when main() returns.
99 * The hook is not called immediately from the signal handler but rather the
100 * next time the poll loop iterates, so it is freed from the usual restrictions
101 * on signal handler functions. */
103 fatal_signal_add_hook(void (*func)(void *aux), void *aux, bool run_at_exit)
106 assert(n_hooks < MAX_HOOKS);
107 hooks[n_hooks].func = func;
108 hooks[n_hooks].aux = aux;
109 hooks[n_hooks].run_at_exit = run_at_exit;
113 /* Handles fatal signal number 'sig_nr'.
115 * Ordinarily this is the actual signal handler. When other code needs to
116 * handle one of our signals, however, it can register for that signal and, if
117 * and when necessary, call this function to do fatal signal processing for it
118 * and terminate the process. Currently only timeval.c does this, for SIGALRM.
119 * (It is not important whether the other code sets up its signal handler
120 * before or after this file, because this file will only set up a signal
121 * handler in the case where the signal has its default handling.) */
123 fatal_signal_handler(int sig_nr)
125 ignore(write(signal_fds[1], "", 1));
126 stored_sig_nr = sig_nr;
130 fatal_signal_run(void)
132 int sig_nr = stored_sig_nr;
134 if (sig_nr != SIG_ATOMIC_MAX) {
137 /* Re-raise the signal with the default handling so that the program
138 * termination status reflects that we were killed by this signal */
139 signal(sig_nr, SIG_DFL);
145 fatal_signal_wait(void)
147 poll_fd_wait(signal_fds[0], POLLIN);
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 if (!shash_find(&files, file)) {
193 shash_add(&files, file, NULL);
197 /* Unregisters 'file' from being unlinked when the program terminates via
198 * exit() or a fatal signal. */
200 fatal_signal_remove_file_to_unlink(const char *file)
202 struct shash_node *node;
204 node = shash_find(&files, file);
206 shash_delete(&files, node);
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);
259 /* Raise any signals that we have already received with the default
261 if (stored_sig_nr != SIG_ATOMIC_MAX) {
262 raise(stored_sig_nr);