43b27a39a0d0e3b1311eb54402b4fb2e43f8744d
[sliver-openvswitch.git] / lib / fatal-signal.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  * 
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33 #include "fatal-signal.h"
34 #include <assert.h>
35 #include <errno.h>
36 #include <signal.h>
37 #include <stdbool.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include "util.h"
43
44 /* Signals to catch. */
45 static const int fatal_signals[] = { SIGTERM, SIGINT, SIGHUP };
46
47 /* Signals to catch as a sigset_t. */
48 static sigset_t fatal_signal_set;
49
50 /* Hooks to call upon catching a signal */
51 struct hook {
52     void (*func)(void *aux);
53     void *aux;
54 };
55 #define MAX_HOOKS 32
56 static struct hook hooks[MAX_HOOKS];
57 static size_t n_hooks;
58
59 /* Number of nesting signal blockers. */
60 static int block_level = 0;
61
62 /* Signal mask saved by outermost signal blocker. */
63 static sigset_t saved_signal_mask;
64
65 static void call_sigprocmask(int how, sigset_t* new_set, sigset_t* old_set);
66 static void signal_handler(int sig_nr);
67
68 /* Registers 'hook' to be called when a process termination signal is
69  * raised. */
70 void
71 fatal_signal_add_hook(void (*func)(void *aux), void *aux)
72 {
73     fatal_signal_block();
74     assert(n_hooks < MAX_HOOKS);
75     hooks[n_hooks].func = func;
76     hooks[n_hooks].aux = aux;
77     n_hooks++;
78     fatal_signal_unblock();
79 }
80
81 /* Blocks program termination signals until fatal_signal_unblock() is called.
82  * May be called multiple times with nesting; if so, fatal_signal_unblock()
83  * must be called the same number of times to unblock signals.
84  *
85  * This is needed while adjusting a data structure that will be accessed by a
86  * fatal signal hook, so that the hook is not invoked while the data structure
87  * is in an inconsistent state. */
88 void
89 fatal_signal_block()
90 {
91     static bool inited = false;
92     if (!inited) {
93         size_t i;
94
95         inited = true;
96         sigemptyset(&fatal_signal_set);
97         for (i = 0; i < ARRAY_SIZE(fatal_signals); i++) {
98             int sig_nr = fatal_signals[i];
99             sigaddset(&fatal_signal_set, sig_nr);
100             if (signal(sig_nr, signal_handler) == SIG_IGN) {
101                 signal(sig_nr, SIG_IGN);
102             }
103         }
104     }
105
106     if (++block_level == 1) {
107         call_sigprocmask(SIG_BLOCK, &fatal_signal_set, &saved_signal_mask);
108     }
109 }
110
111 /* Unblocks program termination signals blocked by fatal_signal_block() is
112  * called.  If multiple calls to fatal_signal_block() are nested,
113  * fatal_signal_unblock() must be called the same number of times to unblock
114  * signals. */
115 void
116 fatal_signal_unblock()
117 {
118     assert(block_level > 0);
119     if (--block_level == 0) {
120         call_sigprocmask(SIG_SETMASK, &saved_signal_mask, NULL);
121     }
122 }
123 \f
124 static char **files;
125 static size_t n_files, max_files;
126 static bool disabled;
127
128 static void unlink_files(void *aux);
129 static void do_unlink_files(void);
130
131 /* Registers 'file' to be unlinked when the program terminates via exit() or a
132  * fatal signal. */
133 void
134 fatal_signal_add_file_to_unlink(const char *file)
135 {
136     static bool added_hook = false;
137     if (!added_hook) {
138         added_hook = true;
139         fatal_signal_add_hook(unlink_files, NULL);
140         atexit(do_unlink_files);
141     }
142
143     fatal_signal_block();
144     if (n_files >= max_files) {
145         max_files = max_files * 2 + 1;
146         files = xrealloc(files, sizeof *files * max_files);
147     }
148     files[n_files++] = xstrdup(file);
149     fatal_signal_unblock();
150 }
151
152 /* Unregisters 'file' from being unlinked when the program terminates via
153  * exit() or a fatal signal. */
154 void
155 fatal_signal_remove_file_to_unlink(const char *file)
156 {
157     size_t i;
158
159     fatal_signal_block();
160     for (i = 0; i < n_files; i++) {
161         if (!strcmp(files[i], file)) {
162             free(files[i]);
163             files[i] = files[--n_files];
164             break;
165         }
166     }
167     fatal_signal_unblock();
168 }
169
170 static void
171 unlink_files(void *aux UNUSED)
172 {
173     do_unlink_files(); 
174 }
175
176 static void
177 do_unlink_files(void)
178 {
179     if (!disabled) {
180         size_t i;
181
182         for (i = 0; i < n_files; i++) {
183             unlink(files[i]);
184         }
185     }
186 }
187 \f
188 /* Disables the fatal signal hook mechanism.  Following a fork, one of the
189  * resulting processes can call this function to allow it to terminate without
190  * triggering fatal signal processing or removing files.  Fatal signal
191  * processing is still enabled in the other process. */
192 void
193 fatal_signal_fork(void)
194 {
195     size_t i;
196
197     disabled = true;
198
199     for (i = 0; i < ARRAY_SIZE(fatal_signals); i++) {
200         int sig_nr = fatal_signals[i];
201         if (signal(sig_nr, SIG_DFL) == SIG_IGN) {
202             signal(sig_nr, SIG_IGN);
203         }
204     }
205 }
206 \f
207 static void
208 call_sigprocmask(int how, sigset_t* new_set, sigset_t* old_set)
209 {
210     int error = sigprocmask(how, new_set, old_set);
211     if (error) {
212         fprintf(stderr, "sigprocmask: %s\n", strerror(errno));
213     }
214 }
215
216 static void
217 signal_handler(int sig_nr)
218 {
219     volatile sig_atomic_t recurse = 0;
220     if (!recurse) {
221         size_t i;
222
223         recurse = 1;
224
225         /* Call all the hooks. */
226         for (i = 0; i < n_hooks; i++) {
227             hooks[i].func(hooks[i].aux);
228         }
229     }
230
231     /* Re-raise the signal with the default handling so that the program
232      * termination status reflects that we were killed by this signal */
233     signal(sig_nr, SIG_DFL);
234     raise(sig_nr);
235 }