]> git.proxmox.com Git - mirror_frr.git/blob - lib/sigevent.c
lib: assert if someone adds to nexthop list to nhg
[mirror_frr.git] / lib / sigevent.c
1 /* Quagga signal handling functions.
2 * Copyright (C) 2004 Paul Jakma,
3 *
4 * This file is part of Quagga.
5 *
6 * Quagga is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * Quagga is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22 #include <sigevent.h>
23 #include <log.h>
24 #include <memory.h>
25 #include <lib_errors.h>
26
27 #ifdef HAVE_UCONTEXT_H
28 #ifdef GNU_LINUX
29 /* get REG_EIP from ucontext.h */
30 #ifndef __USE_GNU
31 #define __USE_GNU
32 #endif /* __USE_GNU */
33 #endif /* GNU_LINUX */
34 #include <ucontext.h>
35 #endif /* HAVE_UCONTEXT_H */
36
37
38 /* master signals descriptor struct */
39 static struct quagga_sigevent_master_t {
40 struct thread *t;
41
42 struct quagga_signal_t *signals;
43 int sigc;
44
45 volatile sig_atomic_t caught;
46 } sigmaster;
47
48 /* Generic signal handler
49 * Schedules signal event thread
50 */
51 static void quagga_signal_handler(int signo)
52 {
53 int i;
54 struct quagga_signal_t *sig;
55
56 for (i = 0; i < sigmaster.sigc; i++) {
57 sig = &(sigmaster.signals[i]);
58
59 if (sig->signal == signo)
60 sig->caught = 1;
61 }
62
63 sigmaster.caught = 1;
64 }
65
66 /* check if signals have been caught and run appropriate handlers */
67 int quagga_sigevent_process(void)
68 {
69 struct quagga_signal_t *sig;
70 int i;
71 #ifdef SIGEVENT_BLOCK_SIGNALS
72 /* shouldnt need to block signals, but potentially may be needed */
73 sigset_t newmask, oldmask;
74
75 /*
76 * Block most signals, but be careful not to defer SIGTRAP because
77 * doing so breaks gdb, at least on NetBSD 2.0. Avoid asking to
78 * block SIGKILL, just because we shouldn't be able to do so.
79 */
80 sigfillset(&newmask);
81 sigdelset(&newmask, SIGTRAP);
82 sigdelset(&newmask, SIGKILL);
83
84 if ((sigprocmask(SIG_BLOCK, &newmask, &oldmask)) < 0) {
85 flog_err_sys(EC_LIB_SYSTEM_CALL,
86 "quagga_signal_timer: couldnt block signals!");
87 return -1;
88 }
89 #endif /* SIGEVENT_BLOCK_SIGNALS */
90
91 if (sigmaster.caught > 0) {
92 sigmaster.caught = 0;
93 /* must not read or set sigmaster.caught after here,
94 * race condition with per-sig caught flags if one does
95 */
96
97 for (i = 0; i < sigmaster.sigc; i++) {
98 sig = &(sigmaster.signals[i]);
99
100 if (sig->caught > 0) {
101 sig->caught = 0;
102 if (sig->handler)
103 sig->handler();
104 }
105 }
106 }
107
108 #ifdef SIGEVENT_BLOCK_SIGNALS
109 if (sigprocmask(SIG_UNBLOCK, &oldmask, NULL) < 0)
110 ;
111 return -1;
112 #endif /* SIGEVENT_BLOCK_SIGNALS */
113
114 return 0;
115 }
116
117 #ifdef SIGEVENT_SCHEDULE_THREAD
118 /* timer thread to check signals. Shouldnt be needed */
119 int quagga_signal_timer(struct thread *t)
120 {
121 struct quagga_sigevent_master_t *sigm;
122
123 sigm = THREAD_ARG(t);
124 sigm->t = NULL;
125 thread_add_timer(sigm->t->master, quagga_signal_timer, &sigmaster,
126 QUAGGA_SIGNAL_TIMER_INTERVAL, &sigm->t);
127 return quagga_sigevent_process();
128 }
129 #endif /* SIGEVENT_SCHEDULE_THREAD */
130
131 /* Initialization of signal handles. */
132 /* Signal wrapper. */
133 static int signal_set(int signo)
134 {
135 int ret;
136 struct sigaction sig;
137 struct sigaction osig;
138
139 sig.sa_handler = &quagga_signal_handler;
140 sigfillset(&sig.sa_mask);
141 sig.sa_flags = 0;
142 if (signo == SIGALRM) {
143 #ifdef SA_INTERRUPT
144 sig.sa_flags |= SA_INTERRUPT; /* SunOS */
145 #endif
146 } else {
147 #ifdef SA_RESTART
148 sig.sa_flags |= SA_RESTART;
149 #endif /* SA_RESTART */
150 }
151
152 ret = sigaction(signo, &sig, &osig);
153 if (ret < 0)
154 return ret;
155 else
156 return 0;
157 }
158
159 /* XXX This function should be enhanced to support more platforms
160 (it currently works only on Linux/x86). */
161 static void *program_counter(void *context)
162 {
163 #ifdef HAVE_UCONTEXT_H
164 #ifdef GNU_LINUX
165 /* these are from GNU libc, rather than Linux, strictly speaking */
166 #if defined(REG_EIP)
167 # define REG_INDEX REG_EIP
168 #elif defined(REG_RIP)
169 # define REG_INDEX REG_RIP
170 #elif defined(__powerpc__)
171 # define REG_INDEX 32
172 #endif
173 #elif defined(SUNOS_5) /* !GNU_LINUX */
174 # define REG_INDEX REG_PC
175 #endif /* GNU_LINUX */
176
177 #ifdef REG_INDEX
178 #ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_GREGS
179 # define REGS gregs[REG_INDEX]
180 #elif defined(HAVE_UCONTEXT_T_UC_MCONTEXT_UC_REGS)
181 # define REGS uc_regs->gregs[REG_INDEX]
182 #endif /* HAVE_UCONTEXT_T_UC_MCONTEXT_GREGS */
183 #endif /* REG_INDEX */
184
185 #ifdef REGS
186 if (context)
187 return (void *)(((ucontext_t *)context)->uc_mcontext.REGS);
188 #elif defined(HAVE_UCONTEXT_T_UC_MCONTEXT_REGS__NIP)
189 /* older Linux / struct pt_regs ? */
190 if (context)
191 return (void *)(((ucontext_t *)context)->uc_mcontext.regs->nip);
192 #endif /* REGS */
193
194 #endif /* HAVE_UCONTEXT_H */
195 return NULL;
196 }
197
198 static void __attribute__((noreturn))
199 exit_handler(int signo, siginfo_t *siginfo, void *context)
200 {
201 void *pc = program_counter(context);
202
203 zlog_signal(signo, "exiting...", siginfo, pc);
204 _exit(128 + signo);
205 }
206
207 static void __attribute__((noreturn))
208 core_handler(int signo, siginfo_t *siginfo, void *context)
209 {
210 void *pc = program_counter(context);
211
212 /* make sure we don't hang in here. default for SIGALRM is terminate.
213 * - if we're in backtrace for more than a second, abort. */
214 struct sigaction sa_default = {.sa_handler = SIG_DFL};
215 sigaction(SIGALRM, &sa_default, NULL);
216
217 sigset_t sigset;
218 sigemptyset(&sigset);
219 sigaddset(&sigset, SIGALRM);
220 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
221
222 alarm(1);
223
224 zlog_signal(signo, "aborting...", siginfo, pc);
225
226 /* dump memory stats on core */
227 log_memstats(stderr, "core_handler");
228 abort();
229 }
230
231 static void trap_default_signals(void)
232 {
233 static const int core_signals[] = {
234 SIGQUIT, SIGILL,
235 #ifdef SIGEMT
236 SIGEMT,
237 #endif
238 SIGFPE, SIGBUS, SIGSEGV,
239 #ifdef SIGSYS
240 SIGSYS,
241 #endif
242 #ifdef SIGXCPU
243 SIGXCPU,
244 #endif
245 #ifdef SIGXFSZ
246 SIGXFSZ,
247 #endif
248 };
249 static const int exit_signals[] = {
250 SIGHUP, SIGINT, SIGALRM, SIGTERM, SIGUSR1, SIGUSR2,
251 #ifdef SIGPOLL
252 SIGPOLL,
253 #endif
254 #ifdef SIGVTALRM
255 SIGVTALRM,
256 #endif
257 #ifdef SIGSTKFLT
258 SIGSTKFLT,
259 #endif
260 };
261 static const int ignore_signals[] = {
262 SIGPIPE,
263 };
264 static const struct {
265 const int *sigs;
266 unsigned int nsigs;
267 void (*handler)(int signo, siginfo_t *info, void *context);
268 } sigmap[] = {
269 {core_signals, array_size(core_signals), core_handler},
270 {exit_signals, array_size(exit_signals), exit_handler},
271 {ignore_signals, array_size(ignore_signals), NULL},
272 };
273 unsigned int i;
274
275 for (i = 0; i < array_size(sigmap); i++) {
276 unsigned int j;
277
278 for (j = 0; j < sigmap[i].nsigs; j++) {
279 struct sigaction oact;
280 if ((sigaction(sigmap[i].sigs[j], NULL, &oact) == 0)
281 && (oact.sa_handler == SIG_DFL)) {
282 struct sigaction act;
283 sigfillset(&act.sa_mask);
284 if (sigmap[i].handler == NULL) {
285 act.sa_handler = SIG_IGN;
286 act.sa_flags = 0;
287 } else {
288 /* Request extra arguments to signal
289 * handler. */
290 act.sa_sigaction = sigmap[i].handler;
291 act.sa_flags = SA_SIGINFO;
292 #ifdef SA_RESETHAND
293 /* don't try to print backtraces
294 * recursively */
295 if (sigmap[i].handler == core_handler)
296 act.sa_flags |= SA_RESETHAND;
297 #endif
298 }
299 if (sigaction(sigmap[i].sigs[j], &act, NULL)
300 < 0)
301 flog_err(
302 EC_LIB_SYSTEM_CALL,
303 "Unable to set signal handler for signal %d: %s",
304 sigmap[i].sigs[j],
305 safe_strerror(errno));
306 }
307 }
308 }
309 }
310
311 void signal_init(struct thread_master *m, int sigc,
312 struct quagga_signal_t signals[])
313 {
314
315 int i = 0;
316 struct quagga_signal_t *sig;
317
318 /* First establish some default handlers that can be overridden by
319 the application. */
320 trap_default_signals();
321
322 while (i < sigc) {
323 sig = &signals[i];
324 if (signal_set(sig->signal) < 0)
325 exit(-1);
326 i++;
327 }
328
329 sigmaster.sigc = sigc;
330 sigmaster.signals = signals;
331
332 #ifdef SIGEVENT_SCHEDULE_THREAD
333 sigmaster.t = NULL;
334 thread_add_timer(m, quagga_signal_timer, &sigmaster,
335 QUAGGA_SIGNAL_TIMER_INTERVAL, &sigmaster.t);
336 #endif /* SIGEVENT_SCHEDULE_THREAD */
337 }