2 * Copyright (c) 2008, 2009, 2010, 2011 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"
30 #include "socket-util.h"
34 VLOG_DEFINE_THIS_MODULE(fatal_signal);
36 /* Signals to catch. */
37 static const int fatal_signals[] = { SIGTERM, SIGINT, SIGHUP, SIGALRM };
39 /* Signals to catch as a sigset_t. */
40 static sigset_t fatal_signal_set;
42 /* Hooks to call upon catching a signal */
44 void (*hook_cb)(void *aux);
45 void (*cancel_cb)(void *aux);
50 static struct hook hooks[MAX_HOOKS];
51 static size_t n_hooks;
53 static int signal_fds[2];
54 static volatile sig_atomic_t stored_sig_nr = SIG_ATOMIC_MAX;
56 static void fatal_signal_init(void);
57 static void atexit_handler(void);
58 static void call_hooks(int sig_nr);
61 fatal_signal_init(void)
63 static bool inited = false;
70 if (pipe(signal_fds)) {
71 ovs_fatal(errno, "could not create pipe");
73 set_nonblocking(signal_fds[0]);
74 set_nonblocking(signal_fds[1]);
76 sigemptyset(&fatal_signal_set);
77 for (i = 0; i < ARRAY_SIZE(fatal_signals); i++) {
78 int sig_nr = fatal_signals[i];
79 struct sigaction old_sa;
81 sigaddset(&fatal_signal_set, sig_nr);
82 if (sigaction(sig_nr, NULL, &old_sa)) {
83 ovs_fatal(errno, "sigaction");
85 if (old_sa.sa_handler == SIG_DFL
86 && signal(sig_nr, fatal_signal_handler) == SIG_ERR) {
87 ovs_fatal(errno, "signal");
90 atexit(atexit_handler);
94 /* Registers 'hook_cb' to be called when a process termination signal is
95 * raised. If 'run_at_exit' is true, 'hook_cb' is also called during normal
96 * process termination, e.g. when exit() is called or when main() returns.
98 * 'hook_cb' is not called immediately from the signal handler but rather the
99 * next time the poll loop iterates, so it is freed from the usual restrictions
100 * on signal handler functions.
102 * If the current process forks, fatal_signal_fork() may be called to clear the
103 * parent process's fatal signal hooks, so that 'hook_cb' is only called when
104 * the child terminates, not when the parent does. When fatal_signal_fork() is
105 * called, it calls the 'cancel_cb' function if it is nonnull, passing 'aux',
106 * to notify that the hook has been canceled. This allows the hook to free
109 fatal_signal_add_hook(void (*hook_cb)(void *aux), void (*cancel_cb)(void *aux),
110 void *aux, bool run_at_exit)
114 assert(n_hooks < MAX_HOOKS);
115 hooks[n_hooks].hook_cb = hook_cb;
116 hooks[n_hooks].cancel_cb = cancel_cb;
117 hooks[n_hooks].aux = aux;
118 hooks[n_hooks].run_at_exit = run_at_exit;
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)
134 ignore(write(signal_fds[1], "", 1));
135 stored_sig_nr = sig_nr;
138 /* Check whether a fatal signal has occurred and, if so, call the fatal signal
141 * This function is called automatically by poll_block(), but specialized
142 * programs that may not always call poll_block() on a regular basis should
143 * also call it periodically. (Therefore, any function with "block" in its
144 * name should call fatal_signal_run() each time it is called, either directly
145 * or through poll_block(), because such functions can only used by specialized
146 * programs that can afford to block outside their main loop around
150 fatal_signal_run(void)
156 sig_nr = stored_sig_nr;
157 if (sig_nr != SIG_ATOMIC_MAX) {
160 /* Re-raise the signal with the default handling so that the program
161 * termination status reflects that we were killed by this signal */
162 signal(sig_nr, SIG_DFL);
168 fatal_signal_wait(void)
171 poll_fd_wait(signal_fds[0], POLLIN);
181 call_hooks(int sig_nr)
183 static volatile sig_atomic_t recurse = 0;
189 for (i = 0; i < n_hooks; i++) {
190 struct hook *h = &hooks[i];
191 if (sig_nr || h->run_at_exit) {
198 /* Files to delete on exit. */
199 static struct sset files = SSET_INITIALIZER(&files);
201 /* Has a hook function been registered with fatal_signal_add_hook() (and not
202 * cleared by fatal_signal_fork())? */
203 static bool added_hook;
205 static void unlink_files(void *aux);
206 static void cancel_files(void *aux);
207 static void do_unlink_files(void);
209 /* Registers 'file' to be unlinked when the program terminates via exit() or a
212 fatal_signal_add_file_to_unlink(const char *file)
216 fatal_signal_add_hook(unlink_files, cancel_files, NULL, true);
219 sset_add(&files, file);
222 /* Unregisters 'file' from being unlinked when the program terminates via
223 * exit() or a fatal signal. */
225 fatal_signal_remove_file_to_unlink(const char *file)
227 sset_find_and_delete(&files, file);
230 /* Like fatal_signal_remove_file_to_unlink(), but also unlinks 'file'.
231 * Returns 0 if successful, otherwise a positive errno value. */
233 fatal_signal_unlink_file_now(const char *file)
235 int error = unlink(file) ? errno : 0;
237 VLOG_WARN("could not unlink \"%s\" (%s)", file, strerror(error));
240 fatal_signal_remove_file_to_unlink(file);
246 unlink_files(void *aux OVS_UNUSED)
252 cancel_files(void *aux OVS_UNUSED)
259 do_unlink_files(void)
263 SSET_FOR_EACH (file, &files) {
268 /* Clears all of the fatal signal hooks without executing them. If any of the
269 * hooks passed a 'cancel_cb' function to fatal_signal_add_hook(), then those
270 * functions will be called, allowing them to free resources, etc.
272 * Following a fork, one of the resulting processes can call this function to
273 * allow it to terminate without calling the hooks registered before calling
274 * this function. New hooks registered after calling this function will take
275 * effect normally. */
277 fatal_signal_fork(void)
281 for (i = 0; i < n_hooks; i++) {
282 struct hook *h = &hooks[i];
284 h->cancel_cb(h->aux);
289 /* Raise any signals that we have already received with the default
291 if (stored_sig_nr != SIG_ATOMIC_MAX) {
292 raise(stored_sig_nr);