2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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"
26 #include "ovs-thread.h"
27 #include "poll-loop.h"
31 #include "socket-util.h"
35 #include "type-props.h"
37 #ifndef SIG_ATOMIC_MAX
38 #define SIG_ATOMIC_MAX TYPE_MAXIMUM(sig_atomic_t)
41 VLOG_DEFINE_THIS_MODULE(fatal_signal
);
43 /* Signals to catch. */
45 static const int fatal_signals
[] = { SIGTERM
, SIGINT
, SIGHUP
, SIGALRM
};
47 static const int fatal_signals
[] = { SIGTERM
};
50 /* Hooks to call upon catching a signal */
52 void (*hook_cb
)(void *aux
);
53 void (*cancel_cb
)(void *aux
);
58 static struct hook hooks
[MAX_HOOKS
];
59 static size_t n_hooks
;
61 static int signal_fds
[2];
63 static volatile sig_atomic_t stored_sig_nr
= SIG_ATOMIC_MAX
;
65 static struct ovs_mutex mutex
;
67 static void call_hooks(int sig_nr
);
69 static BOOL WINAPI
ConsoleHandlerRoutine(DWORD dwCtrlType
);
72 /* Initializes the fatal signal handling module. Calling this function is
73 * optional, because calling any other function in the module will also
74 * initialize it. However, in a multithreaded program, the module must be
75 * initialized while the process is still single-threaded. */
77 fatal_signal_init(void)
79 static bool inited
= false;
84 assert_single_threaded();
87 ovs_mutex_init_recursive(&mutex
);
89 xpipe_nonblocking(signal_fds
);
91 wevent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
93 char *msg_buf
= ovs_lasterror_to_string();
94 VLOG_FATAL("Failed to create a event (%s).", msg_buf
);
97 /* Register a function to handle Ctrl+C. */
98 SetConsoleCtrlHandler(ConsoleHandlerRoutine
, true);
101 for (i
= 0; i
< ARRAY_SIZE(fatal_signals
); i
++) {
102 int sig_nr
= fatal_signals
[i
];
104 struct sigaction old_sa
;
106 xsigaction(sig_nr
, NULL
, &old_sa
);
107 if (old_sa
.sa_handler
== SIG_DFL
108 && signal(sig_nr
, fatal_signal_handler
) == SIG_ERR
) {
109 VLOG_FATAL("signal failed (%s)", ovs_strerror(errno
));
112 if (signal(sig_nr
, fatal_signal_handler
) == SIG_ERR
) {
113 VLOG_FATAL("signal failed (%s)", ovs_strerror(errno
));
117 atexit(fatal_signal_atexit_handler
);
121 /* Registers 'hook_cb' to be called from inside poll_block() following a fatal
122 * signal. 'hook_cb' does not need to be async-signal-safe. In a
123 * multithreaded program 'hook_cb' might be called from any thread, with
124 * threads other than the one running 'hook_cb' in unknown states.
126 * If 'run_at_exit' is true, 'hook_cb' is also called during normal process
127 * termination, e.g. when exit() is called or when main() returns.
129 * If the current process forks, fatal_signal_fork() may be called to clear the
130 * parent process's fatal signal hooks, so that 'hook_cb' is only called when
131 * the child terminates, not when the parent does. When fatal_signal_fork() is
132 * called, it calls the 'cancel_cb' function if it is nonnull, passing 'aux',
133 * to notify that the hook has been canceled. This allows the hook to free
136 fatal_signal_add_hook(void (*hook_cb
)(void *aux
), void (*cancel_cb
)(void *aux
),
137 void *aux
, bool run_at_exit
)
141 ovs_mutex_lock(&mutex
);
142 ovs_assert(n_hooks
< MAX_HOOKS
);
143 hooks
[n_hooks
].hook_cb
= hook_cb
;
144 hooks
[n_hooks
].cancel_cb
= cancel_cb
;
145 hooks
[n_hooks
].aux
= aux
;
146 hooks
[n_hooks
].run_at_exit
= run_at_exit
;
148 ovs_mutex_unlock(&mutex
);
151 /* Handles fatal signal number 'sig_nr'.
153 * Ordinarily this is the actual signal handler. When other code needs to
154 * handle one of our signals, however, it can register for that signal and, if
155 * and when necessary, call this function to do fatal signal processing for it
156 * and terminate the process. Currently only timeval.c does this, for SIGALRM.
157 * (It is not important whether the other code sets up its signal handler
158 * before or after this file, because this file will only set up a signal
159 * handler in the case where the signal has its default handling.) */
161 fatal_signal_handler(int sig_nr
)
164 ignore(write(signal_fds
[1], "", 1));
168 stored_sig_nr
= sig_nr
;
171 /* Check whether a fatal signal has occurred and, if so, call the fatal signal
174 * This function is called automatically by poll_block(), but specialized
175 * programs that may not always call poll_block() on a regular basis should
176 * also call it periodically. (Therefore, any function with "block" in its
177 * name should call fatal_signal_run() each time it is called, either directly
178 * or through poll_block(), because such functions can only used by specialized
179 * programs that can afford to block outside their main loop around
183 fatal_signal_run(void)
189 sig_nr
= stored_sig_nr
;
190 if (sig_nr
!= SIG_ATOMIC_MAX
) {
191 char namebuf
[SIGNAL_NAME_BUFSIZE
];
193 ovs_mutex_lock(&mutex
);
196 VLOG_WARN("terminating with signal %d (%s)",
197 (int)sig_nr
, signal_name(sig_nr
, namebuf
, sizeof namebuf
));
199 VLOG_WARN("terminating with signal %d", (int)sig_nr
);
204 /* Re-raise the signal with the default handling so that the program
205 * termination status reflects that we were killed by this signal */
206 signal(sig_nr
, SIG_DFL
);
209 ovs_mutex_unlock(&mutex
);
215 fatal_signal_wait(void)
218 poll_fd_wait_event(signal_fds
[0], wevent
, POLLIN
);
222 fatal_ignore_sigpipe(void)
225 signal(SIGPIPE
, SIG_IGN
);
230 fatal_signal_atexit_handler(void)
236 call_hooks(int sig_nr
)
238 static volatile sig_atomic_t recurse
= 0;
244 for (i
= 0; i
< n_hooks
; i
++) {
245 struct hook
*h
= &hooks
[i
];
246 if (sig_nr
|| h
->run_at_exit
) {
254 BOOL WINAPI
ConsoleHandlerRoutine(DWORD dwCtrlType
)
256 stored_sig_nr
= SIGINT
;
262 /* Files to delete on exit. */
263 static struct sset files
= SSET_INITIALIZER(&files
);
265 /* Has a hook function been registered with fatal_signal_add_hook() (and not
266 * cleared by fatal_signal_fork())? */
267 static bool added_hook
;
269 static void unlink_files(void *aux
);
270 static void cancel_files(void *aux
);
271 static void do_unlink_files(void);
273 /* Registers 'file' to be unlinked when the program terminates via exit() or a
276 fatal_signal_add_file_to_unlink(const char *file
)
280 ovs_mutex_lock(&mutex
);
283 fatal_signal_add_hook(unlink_files
, cancel_files
, NULL
, true);
286 sset_add(&files
, file
);
287 ovs_mutex_unlock(&mutex
);
290 /* Unregisters 'file' from being unlinked when the program terminates via
291 * exit() or a fatal signal. */
293 fatal_signal_remove_file_to_unlink(const char *file
)
297 ovs_mutex_lock(&mutex
);
298 sset_find_and_delete(&files
, file
);
299 ovs_mutex_unlock(&mutex
);
302 /* Like fatal_signal_remove_file_to_unlink(), but also unlinks 'file'.
303 * Returns 0 if successful, otherwise a positive errno value. */
305 fatal_signal_unlink_file_now(const char *file
)
311 ovs_mutex_lock(&mutex
);
313 error
= unlink(file
) ? errno
: 0;
315 VLOG_WARN("could not unlink \"%s\" (%s)", file
, ovs_strerror(error
));
318 fatal_signal_remove_file_to_unlink(file
);
320 ovs_mutex_unlock(&mutex
);
326 unlink_files(void *aux OVS_UNUSED
)
332 cancel_files(void *aux OVS_UNUSED
)
339 do_unlink_files(void)
343 SSET_FOR_EACH (file
, &files
) {
348 /* Clears all of the fatal signal hooks without executing them. If any of the
349 * hooks passed a 'cancel_cb' function to fatal_signal_add_hook(), then those
350 * functions will be called, allowing them to free resources, etc.
352 * Following a fork, one of the resulting processes can call this function to
353 * allow it to terminate without calling the hooks registered before calling
354 * this function. New hooks registered after calling this function will take
355 * effect normally. */
357 fatal_signal_fork(void)
361 assert_single_threaded();
363 for (i
= 0; i
< n_hooks
; i
++) {
364 struct hook
*h
= &hooks
[i
];
366 h
->cancel_cb(h
->aux
);
371 /* Raise any signals that we have already received with the default
373 if (stored_sig_nr
!= SIG_ATOMIC_MAX
) {
374 raise(stored_sig_nr
);
379 /* Blocks all fatal signals and returns previous signal mask into
382 fatal_signal_block(sigset_t
*prev_mask
)
387 sigemptyset(&block_mask
);
388 for (i
= 0; i
< ARRAY_SIZE(fatal_signals
); i
++) {
389 int sig_nr
= fatal_signals
[i
];
390 sigaddset(&block_mask
, sig_nr
);
392 xpthread_sigmask(SIG_BLOCK
, &block_mask
, prev_mask
);