fatal-signal: Clean up code by using shash.
[cascardo/ovs.git] / lib / fatal-signal.c
1 /*
2  * Copyright (c) 2008, 2009 Nicira Networks.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 #include <config.h>
17 #include "fatal-signal.h"
18 #include <assert.h>
19 #include <errno.h>
20 #include <signal.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include "shash.h"
27 #include "util.h"
28
29 /* Signals to catch. */
30 static const int fatal_signals[] = { SIGTERM, SIGINT, SIGHUP, SIGALRM };
31
32 /* Signals to catch as a sigset_t. */
33 static sigset_t fatal_signal_set;
34
35 /* Hooks to call upon catching a signal */
36 struct hook {
37     void (*func)(void *aux);
38     void *aux;
39     bool run_at_exit;
40 };
41 #define MAX_HOOKS 32
42 static struct hook hooks[MAX_HOOKS];
43 static size_t n_hooks;
44
45 /* Number of nesting signal blockers. */
46 static int block_level = 0;
47
48 /* Signal mask saved by outermost signal blocker. */
49 static sigset_t saved_signal_mask;
50
51 /* Disabled by fatal_signal_fork()? */
52 static bool disabled;
53
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);
57
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. */
61 void
62 fatal_signal_add_hook(void (*func)(void *aux), void *aux, bool run_at_exit)
63 {
64     fatal_signal_block();
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;
69     n_hooks++;
70     fatal_signal_unblock();
71 }
72
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.
76  *
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. */
80 void
81 fatal_signal_block(void)
82 {
83     static bool inited = false;
84     if (!inited) {
85         size_t i;
86
87         inited = true;
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;
92
93             sigaddset(&fatal_signal_set, sig_nr);
94             if (sigaction(sig_nr, NULL, &old_sa)) {
95                 ovs_fatal(errno, "sigaction");
96             }
97             if (old_sa.sa_handler == SIG_DFL
98                 && signal(sig_nr, fatal_signal_handler) == SIG_ERR) {
99                 ovs_fatal(errno, "signal");
100             }
101         }
102         atexit(atexit_handler);
103     }
104
105     if (++block_level == 1) {
106         call_sigprocmask(SIG_BLOCK, &fatal_signal_set, &saved_signal_mask);
107     }
108 }
109
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
113  * signals. */
114 void
115 fatal_signal_unblock(void)
116 {
117     assert(block_level > 0);
118     if (--block_level == 0) {
119         call_sigprocmask(SIG_SETMASK, &saved_signal_mask, NULL);
120     }
121 }
122
123 /* Handles fatal signal number 'sig_nr'.
124  *
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.)  */
132 void
133 fatal_signal_handler(int sig_nr)
134 {
135     call_hooks(sig_nr);
136
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);
140     raise(sig_nr);
141 }
142
143 static void
144 atexit_handler(void)
145 {
146     if (!disabled) {
147         call_hooks(0);
148     }
149 }
150
151 static void
152 call_hooks(int sig_nr)
153 {
154     static volatile sig_atomic_t recurse = 0;
155     if (!recurse) {
156         size_t i;
157
158         recurse = 1;
159
160         for (i = 0; i < n_hooks; i++) {
161             struct hook *h = &hooks[i];
162             if (sig_nr || h->run_at_exit) {
163                 h->func(h->aux);
164             }
165         }
166     }
167 }
168 \f
169 static struct shash files = SHASH_INITIALIZER(&files);
170
171 static void unlink_files(void *aux);
172 static void do_unlink_files(void);
173
174 /* Registers 'file' to be unlinked when the program terminates via exit() or a
175  * fatal signal. */
176 void
177 fatal_signal_add_file_to_unlink(const char *file)
178 {
179     static bool added_hook = false;
180     if (!added_hook) {
181         added_hook = true;
182         fatal_signal_add_hook(unlink_files, NULL, true);
183     }
184
185     fatal_signal_block();
186     if (!shash_find(&files, file)) {
187         shash_add(&files, file, NULL);
188     }
189     fatal_signal_unblock();
190 }
191
192 /* Unregisters 'file' from being unlinked when the program terminates via
193  * exit() or a fatal signal. */
194 void
195 fatal_signal_remove_file_to_unlink(const char *file)
196 {
197     struct shash_node *node;
198
199     fatal_signal_block();
200     node = shash_find(&files, file);
201     if (node) {
202         shash_delete(&files, node);
203     }
204     fatal_signal_unblock();
205 }
206
207 static void
208 unlink_files(void *aux UNUSED)
209 {
210     do_unlink_files(); 
211 }
212
213 static void
214 do_unlink_files(void)
215 {
216     struct shash_node *node;
217
218     SHASH_FOR_EACH (node, &files) {
219         unlink(node->name);
220     }
221 }
222 \f
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. */
227 void
228 fatal_signal_fork(void)
229 {
230     size_t i;
231
232     disabled = true;
233
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);
238         }
239     }
240 }
241 \f
242 static void
243 call_sigprocmask(int how, sigset_t* new_set, sigset_t* old_set)
244 {
245     int error = sigprocmask(how, new_set, old_set);
246     if (error) {
247         fprintf(stderr, "sigprocmask: %s\n", strerror(errno));
248     }
249 }