fe1f52b2df22cb827d44642d7ac79979997bf634
[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 <config.h>
34 #include "fatal-signal.h"
35 #include <assert.h>
36 #include <errno.h>
37 #include <signal.h>
38 #include <stdbool.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include "util.h"
44
45 /* Signals to catch. */
46 static const int fatal_signals[] = { SIGTERM, SIGINT, SIGHUP, SIGALRM };
47
48 /* Signals to catch as a sigset_t. */
49 static sigset_t fatal_signal_set;
50
51 /* Hooks to call upon catching a signal */
52 struct hook {
53     void (*func)(void *aux);
54     void *aux;
55 };
56 #define MAX_HOOKS 32
57 static struct hook hooks[MAX_HOOKS];
58 static size_t n_hooks;
59
60 /* Number of nesting signal blockers. */
61 static int block_level = 0;
62
63 /* Signal mask saved by outermost signal blocker. */
64 static sigset_t saved_signal_mask;
65
66 static void call_sigprocmask(int how, sigset_t* new_set, sigset_t* old_set);
67 static void signal_handler(int sig_nr);
68
69 /* Registers 'hook' to be called when a process termination signal is
70  * raised. */
71 void
72 fatal_signal_add_hook(void (*func)(void *aux), void *aux)
73 {
74     fatal_signal_block();
75     assert(n_hooks < MAX_HOOKS);
76     hooks[n_hooks].func = func;
77     hooks[n_hooks].aux = aux;
78     n_hooks++;
79     fatal_signal_unblock();
80 }
81
82 /* Blocks program termination signals until fatal_signal_unblock() is called.
83  * May be called multiple times with nesting; if so, fatal_signal_unblock()
84  * must be called the same number of times to unblock signals.
85  *
86  * This is needed while adjusting a data structure that will be accessed by a
87  * fatal signal hook, so that the hook is not invoked while the data structure
88  * is in an inconsistent state. */
89 void
90 fatal_signal_block()
91 {
92     static bool inited = false;
93     if (!inited) {
94         size_t i;
95
96         inited = true;
97         sigemptyset(&fatal_signal_set);
98         for (i = 0; i < ARRAY_SIZE(fatal_signals); i++) {
99             int sig_nr = fatal_signals[i];
100             sigaddset(&fatal_signal_set, sig_nr);
101             if (signal(sig_nr, signal_handler) == SIG_IGN) {
102                 signal(sig_nr, SIG_IGN);
103             }
104         }
105     }
106
107     if (++block_level == 1) {
108         call_sigprocmask(SIG_BLOCK, &fatal_signal_set, &saved_signal_mask);
109     }
110 }
111
112 /* Unblocks program termination signals blocked by fatal_signal_block() is
113  * called.  If multiple calls to fatal_signal_block() are nested,
114  * fatal_signal_unblock() must be called the same number of times to unblock
115  * signals. */
116 void
117 fatal_signal_unblock()
118 {
119     assert(block_level > 0);
120     if (--block_level == 0) {
121         call_sigprocmask(SIG_SETMASK, &saved_signal_mask, NULL);
122     }
123 }
124 \f
125 static char **files;
126 static size_t n_files, max_files;
127 static bool disabled;
128
129 static void unlink_files(void *aux);
130 static void do_unlink_files(void);
131
132 /* Registers 'file' to be unlinked when the program terminates via exit() or a
133  * fatal signal. */
134 void
135 fatal_signal_add_file_to_unlink(const char *file)
136 {
137     static bool added_hook = false;
138     if (!added_hook) {
139         added_hook = true;
140         fatal_signal_add_hook(unlink_files, NULL);
141         atexit(do_unlink_files);
142     }
143
144     fatal_signal_block();
145     if (n_files >= max_files) {
146         max_files = max_files * 2 + 1;
147         files = xrealloc(files, sizeof *files * max_files);
148     }
149     files[n_files++] = xstrdup(file);
150     fatal_signal_unblock();
151 }
152
153 /* Unregisters 'file' from being unlinked when the program terminates via
154  * exit() or a fatal signal. */
155 void
156 fatal_signal_remove_file_to_unlink(const char *file)
157 {
158     size_t i;
159
160     fatal_signal_block();
161     for (i = 0; i < n_files; i++) {
162         if (!strcmp(files[i], file)) {
163             free(files[i]);
164             files[i] = files[--n_files];
165             break;
166         }
167     }
168     fatal_signal_unblock();
169 }
170
171 static void
172 unlink_files(void *aux UNUSED)
173 {
174     do_unlink_files(); 
175 }
176
177 static void
178 do_unlink_files(void)
179 {
180     if (!disabled) {
181         size_t i;
182
183         for (i = 0; i < n_files; i++) {
184             unlink(files[i]);
185         }
186     }
187 }
188 \f
189 /* Disables the fatal signal hook mechanism.  Following a fork, one of the
190  * resulting processes can call this function to allow it to terminate without
191  * triggering fatal signal processing or removing files.  Fatal signal
192  * processing is still enabled in the other process. */
193 void
194 fatal_signal_fork(void)
195 {
196     size_t i;
197
198     disabled = true;
199
200     for (i = 0; i < ARRAY_SIZE(fatal_signals); i++) {
201         int sig_nr = fatal_signals[i];
202         if (signal(sig_nr, SIG_DFL) == SIG_IGN) {
203             signal(sig_nr, SIG_IGN);
204         }
205     }
206 }
207 \f
208 static void
209 call_sigprocmask(int how, sigset_t* new_set, sigset_t* old_set)
210 {
211     int error = sigprocmask(how, new_set, old_set);
212     if (error) {
213         fprintf(stderr, "sigprocmask: %s\n", strerror(errno));
214     }
215 }
216
217 static void
218 signal_handler(int sig_nr)
219 {
220     volatile sig_atomic_t recurse = 0;
221     if (!recurse) {
222         size_t i;
223
224         recurse = 1;
225
226         /* Call all the hooks. */
227         for (i = 0; i < n_hooks; i++) {
228             hooks[i].func(hooks[i].aux);
229         }
230     }
231
232     /* Re-raise the signal with the default handling so that the program
233      * termination status reflects that we were killed by this signal */
234     signal(sig_nr, SIG_DFL);
235     raise(sig_nr);
236 }