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 /* Signals to catch. */
30 static const int fatal_signals
[] = { SIGTERM
, SIGINT
, SIGHUP
, SIGALRM
};
32 /* Signals to catch as a sigset_t. */
33 static sigset_t fatal_signal_set
;
35 /* Hooks to call upon catching a signal */
37 void (*func
)(void *aux
);
42 static struct hook hooks
[MAX_HOOKS
];
43 static size_t n_hooks
;
45 /* Number of nesting signal blockers. */
46 static int block_level
= 0;
48 /* Signal mask saved by outermost signal blocker. */
49 static sigset_t saved_signal_mask
;
51 /* Disabled by fatal_signal_fork()? */
54 static void call_sigprocmask(int how
, sigset_t
* new_set
, sigset_t
* old_set
);
55 static void atexit_handler(void);
56 static void call_hooks(int sig_nr
);
58 /* Registers 'hook' to be called when a process termination signal is raised.
59 * If 'run_at_exit' is true, 'hook' is also called during normal process
60 * termination, e.g. when exit() is called or when main() returns. */
62 fatal_signal_add_hook(void (*func
)(void *aux
), void *aux
, bool run_at_exit
)
65 assert(n_hooks
< MAX_HOOKS
);
66 hooks
[n_hooks
].func
= func
;
67 hooks
[n_hooks
].aux
= aux
;
68 hooks
[n_hooks
].run_at_exit
= run_at_exit
;
70 fatal_signal_unblock();
73 /* Blocks program termination signals until fatal_signal_unblock() is called.
74 * May be called multiple times with nesting; if so, fatal_signal_unblock()
75 * must be called the same number of times to unblock signals.
77 * This is needed while adjusting a data structure that will be accessed by a
78 * fatal signal hook, so that the hook is not invoked while the data structure
79 * is in an inconsistent state. */
81 fatal_signal_block(void)
83 static bool inited
= false;
88 sigemptyset(&fatal_signal_set
);
89 for (i
= 0; i
< ARRAY_SIZE(fatal_signals
); i
++) {
90 int sig_nr
= fatal_signals
[i
];
91 struct sigaction old_sa
;
93 sigaddset(&fatal_signal_set
, sig_nr
);
94 if (sigaction(sig_nr
, NULL
, &old_sa
)) {
95 ovs_fatal(errno
, "sigaction");
97 if (old_sa
.sa_handler
== SIG_DFL
98 && signal(sig_nr
, fatal_signal_handler
) == SIG_ERR
) {
99 ovs_fatal(errno
, "signal");
102 atexit(atexit_handler
);
105 if (++block_level
== 1) {
106 call_sigprocmask(SIG_BLOCK
, &fatal_signal_set
, &saved_signal_mask
);
110 /* Unblocks program termination signals blocked by fatal_signal_block() is
111 * called. If multiple calls to fatal_signal_block() are nested,
112 * fatal_signal_unblock() must be called the same number of times to unblock
115 fatal_signal_unblock(void)
117 assert(block_level
> 0);
118 if (--block_level
== 0) {
119 call_sigprocmask(SIG_SETMASK
, &saved_signal_mask
, NULL
);
123 /* Handles fatal signal number 'sig_nr'.
125 * Ordinarily this is the actual signal handler. When other code needs to
126 * handle one of our signals, however, it can register for that signal and, if
127 * and when necessary, call this function to do fatal signal processing for it
128 * and terminate the process. Currently only timeval.c does this, for SIGALRM.
129 * (It is not important whether the other code sets up its signal handler
130 * before or after this file, because this file will only set up a signal
131 * handler in the case where the signal has its default handling.) */
133 fatal_signal_handler(int sig_nr
)
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
);
152 call_hooks(int sig_nr
)
154 static volatile sig_atomic_t recurse
= 0;
160 for (i
= 0; i
< n_hooks
; i
++) {
161 struct hook
*h
= &hooks
[i
];
162 if (sig_nr
|| h
->run_at_exit
) {
169 static struct shash files
= SHASH_INITIALIZER(&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 (!shash_find(&files
, file
)) {
187 shash_add(&files
, file
, NULL
);
189 fatal_signal_unblock();
192 /* Unregisters 'file' from being unlinked when the program terminates via
193 * exit() or a fatal signal. */
195 fatal_signal_remove_file_to_unlink(const char *file
)
197 struct shash_node
*node
;
199 fatal_signal_block();
200 node
= shash_find(&files
, file
);
202 shash_delete(&files
, node
);
204 fatal_signal_unblock();
208 unlink_files(void *aux UNUSED
)
214 do_unlink_files(void)
216 struct shash_node
*node
;
218 SHASH_FOR_EACH (node
, &files
) {
223 /* Disables the fatal signal hook mechanism. Following a fork, one of the
224 * resulting processes can call this function to allow it to terminate without
225 * triggering fatal signal processing or removing files. Fatal signal
226 * processing is still enabled in the other process. */
228 fatal_signal_fork(void)
234 for (i
= 0; i
< ARRAY_SIZE(fatal_signals
); i
++) {
235 int sig_nr
= fatal_signals
[i
];
236 if (signal(sig_nr
, SIG_DFL
) == SIG_IGN
) {
237 signal(sig_nr
, SIG_IGN
);
243 call_sigprocmask(int how
, sigset_t
* new_set
, sigset_t
* old_set
)
245 int error
= sigprocmask(how
, new_set
, old_set
);
247 fprintf(stderr
, "sigprocmask: %s\n", strerror(errno
));