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  */
quagga_signal_handler(int signo)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 /*
67  * Check whether any signals have been received and are pending. This is done
68  * with the application's key signals blocked. The complete set of signals
69  * is returned in 'setp', so the caller can restore them when appropriate.
70  * If there are pending signals, returns 'true', 'false' otherwise.
71  */
frr_sigevent_check(sigset_t * setp)72 bool frr_sigevent_check(sigset_t *setp)
73 {
74 	sigset_t blocked;
75 	int i;
76 	bool ret;
77 
78 	sigemptyset(setp);
79 	sigemptyset(&blocked);
80 
81 	/* Set up mask of application's signals */
82 	for (i = 0; i < sigmaster.sigc; i++)
83 		sigaddset(&blocked, sigmaster.signals[i].signal);
84 
85 	pthread_sigmask(SIG_BLOCK, &blocked, setp);
86 
87 	/* Now that the application's signals are blocked, test. */
88 	ret = (sigmaster.caught != 0);
89 
90 	return ret;
91 }
92 
93 /* check if signals have been caught and run appropriate handlers */
quagga_sigevent_process(void)94 int quagga_sigevent_process(void)
95 {
96 	struct quagga_signal_t *sig;
97 	int i;
98 #ifdef SIGEVENT_BLOCK_SIGNALS
99 	/* shouldnt need to block signals, but potentially may be needed */
100 	sigset_t newmask, oldmask;
101 
102 	/*
103 	 * Block most signals, but be careful not to defer SIGTRAP because
104 	 * doing so breaks gdb, at least on NetBSD 2.0.  Avoid asking to
105 	 * block SIGKILL, just because we shouldn't be able to do so.
106 	 */
107 	sigfillset(&newmask);
108 	sigdelset(&newmask, SIGTRAP);
109 	sigdelset(&newmask, SIGKILL);
110 
111 	if ((sigprocmask(SIG_BLOCK, &newmask, &oldmask)) < 0) {
112 		flog_err_sys(EC_LIB_SYSTEM_CALL,
113 			     "quagga_signal_timer: couldnt block signals!");
114 		return -1;
115 	}
116 #endif /* SIGEVENT_BLOCK_SIGNALS */
117 
118 	if (sigmaster.caught > 0) {
119 		sigmaster.caught = 0;
120 		/* must not read or set sigmaster.caught after here,
121 		 * race condition with per-sig caught flags if one does
122 		 */
123 
124 		for (i = 0; i < sigmaster.sigc; i++) {
125 			sig = &(sigmaster.signals[i]);
126 
127 			if (sig->caught > 0) {
128 				sig->caught = 0;
129 				if (sig->handler)
130 					sig->handler();
131 			}
132 		}
133 	}
134 
135 #ifdef SIGEVENT_BLOCK_SIGNALS
136 	if (sigprocmask(SIG_UNBLOCK, &oldmask, NULL) < 0)
137 		;
138 	return -1;
139 #endif /* SIGEVENT_BLOCK_SIGNALS */
140 
141 	return 0;
142 }
143 
144 #ifdef SIGEVENT_SCHEDULE_THREAD
145 /* timer thread to check signals. Shouldnt be needed */
quagga_signal_timer(struct thread * t)146 int quagga_signal_timer(struct thread *t)
147 {
148 	struct quagga_sigevent_master_t *sigm;
149 
150 	sigm = THREAD_ARG(t);
151 	sigm->t = NULL;
152 	thread_add_timer(sigm->t->master, quagga_signal_timer, &sigmaster,
153 			 QUAGGA_SIGNAL_TIMER_INTERVAL, &sigm->t);
154 	return quagga_sigevent_process();
155 }
156 #endif /* SIGEVENT_SCHEDULE_THREAD */
157 
158 /* Initialization of signal handles. */
159 /* Signal wrapper. */
signal_set(int signo)160 static int signal_set(int signo)
161 {
162 	int ret;
163 	struct sigaction sig;
164 	struct sigaction osig;
165 
166 	sig.sa_handler = &quagga_signal_handler;
167 	sigfillset(&sig.sa_mask);
168 	sig.sa_flags = 0;
169 	if (signo == SIGALRM) {
170 #ifdef SA_INTERRUPT
171 		sig.sa_flags |= SA_INTERRUPT; /* SunOS */
172 #endif
173 	} else {
174 #ifdef SA_RESTART
175 		sig.sa_flags |= SA_RESTART;
176 #endif /* SA_RESTART */
177 	}
178 
179 	ret = sigaction(signo, &sig, &osig);
180 	if (ret < 0)
181 		return ret;
182 	else
183 		return 0;
184 }
185 
186 /* XXX This function should be enhanced to support more platforms
187        (it currently works only on Linux/x86). */
program_counter(void * context)188 static void *program_counter(void *context)
189 {
190 #ifdef HAVE_UCONTEXT_H
191 #ifdef GNU_LINUX
192 /* these are from GNU libc, rather than Linux, strictly speaking */
193 #if defined(REG_EIP)
194 #  define REG_INDEX REG_EIP
195 #elif defined(REG_RIP)
196 #  define REG_INDEX REG_RIP
197 #elif defined(__powerpc__)
198 #  define REG_INDEX 32
199 #endif
200 #elif defined(SUNOS_5) /* !GNU_LINUX */
201 # define REG_INDEX REG_PC
202 #endif		       /* GNU_LINUX */
203 
204 #ifdef REG_INDEX
205 #ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_GREGS
206 #  define REGS gregs[REG_INDEX]
207 #elif defined(HAVE_UCONTEXT_T_UC_MCONTEXT_UC_REGS)
208 #  define REGS uc_regs->gregs[REG_INDEX]
209 #endif /* HAVE_UCONTEXT_T_UC_MCONTEXT_GREGS */
210 #endif /* REG_INDEX */
211 
212 #ifdef REGS
213 	if (context)
214 		return (void *)(((ucontext_t *)context)->uc_mcontext.REGS);
215 #elif defined(HAVE_UCONTEXT_T_UC_MCONTEXT_REGS__NIP)
216 	/* older Linux / struct pt_regs ? */
217 	if (context)
218 		return (void *)(((ucontext_t *)context)->uc_mcontext.regs->nip);
219 #endif /* REGS */
220 
221 #endif /* HAVE_UCONTEXT_H */
222 	return NULL;
223 }
224 
225 static void __attribute__((noreturn))
exit_handler(int signo,siginfo_t * siginfo,void * context)226 exit_handler(int signo, siginfo_t *siginfo, void *context)
227 {
228 	void *pc = program_counter(context);
229 
230 	zlog_signal(signo, "exiting...", siginfo, pc);
231 	_exit(128 + signo);
232 }
233 
234 static void __attribute__((noreturn))
core_handler(int signo,siginfo_t * siginfo,void * context)235 core_handler(int signo, siginfo_t *siginfo, void *context)
236 {
237 	void *pc = program_counter(context);
238 
239 	/* make sure we don't hang in here.  default for SIGALRM is terminate.
240 	 * - if we're in backtrace for more than a second, abort. */
241 	struct sigaction sa_default = {.sa_handler = SIG_DFL};
242 	sigaction(SIGALRM, &sa_default, NULL);
243 
244 	sigset_t sigset;
245 	sigemptyset(&sigset);
246 	sigaddset(&sigset, SIGALRM);
247 	sigprocmask(SIG_UNBLOCK, &sigset, NULL);
248 
249 	alarm(1);
250 
251 	zlog_signal(signo, "aborting...", siginfo, pc);
252 
253 	/* dump memory stats on core */
254 	log_memstats(stderr, "core_handler");
255 
256 	zlog_tls_buffer_fini();
257 	abort();
258 }
259 
trap_default_signals(void)260 static void trap_default_signals(void)
261 {
262 	static const int core_signals[] = {
263 		SIGQUIT, SIGILL,
264 #ifdef SIGEMT
265 		SIGEMT,
266 #endif
267 		SIGFPE,  SIGBUS, SIGSEGV,
268 #ifdef SIGSYS
269 		SIGSYS,
270 #endif
271 #ifdef SIGXCPU
272 		SIGXCPU,
273 #endif
274 #ifdef SIGXFSZ
275 		SIGXFSZ,
276 #endif
277 	};
278 	static const int exit_signals[] = {
279 		SIGHUP,    SIGINT, SIGALRM, SIGTERM, SIGUSR1, SIGUSR2,
280 #ifdef SIGPOLL
281 		SIGPOLL,
282 #endif
283 #ifdef SIGVTALRM
284 		SIGVTALRM,
285 #endif
286 #ifdef SIGSTKFLT
287 		SIGSTKFLT,
288 #endif
289 	};
290 	static const int ignore_signals[] = {
291 		SIGPIPE,
292 	};
293 	static const struct {
294 		const int *sigs;
295 		unsigned int nsigs;
296 		void (*handler)(int signo, siginfo_t *info, void *context);
297 	} sigmap[] = {
298 		{core_signals, array_size(core_signals), core_handler},
299 		{exit_signals, array_size(exit_signals), exit_handler},
300 		{ignore_signals, array_size(ignore_signals), NULL},
301 	};
302 	unsigned int i;
303 
304 	for (i = 0; i < array_size(sigmap); i++) {
305 		unsigned int j;
306 
307 		for (j = 0; j < sigmap[i].nsigs; j++) {
308 			struct sigaction oact;
309 			if ((sigaction(sigmap[i].sigs[j], NULL, &oact) == 0)
310 			    && (oact.sa_handler == SIG_DFL)) {
311 				struct sigaction act;
312 				sigfillset(&act.sa_mask);
313 				if (sigmap[i].handler == NULL) {
314 					act.sa_handler = SIG_IGN;
315 					act.sa_flags = 0;
316 				} else {
317 					/* Request extra arguments to signal
318 					 * handler. */
319 					act.sa_sigaction = sigmap[i].handler;
320 					act.sa_flags = SA_SIGINFO;
321 #ifdef SA_RESETHAND
322 					/* don't try to print backtraces
323 					 * recursively */
324 					if (sigmap[i].handler == core_handler)
325 						act.sa_flags |= SA_RESETHAND;
326 #endif
327 				}
328 				if (sigaction(sigmap[i].sigs[j], &act, NULL)
329 				    < 0)
330 					flog_err(
331 						EC_LIB_SYSTEM_CALL,
332 						"Unable to set signal handler for signal %d: %s",
333 						sigmap[i].sigs[j],
334 						safe_strerror(errno));
335 			}
336 		}
337 	}
338 }
339 
signal_init(struct thread_master * m,int sigc,struct quagga_signal_t signals[])340 void signal_init(struct thread_master *m, int sigc,
341 		 struct quagga_signal_t signals[])
342 {
343 
344 	int i = 0;
345 	struct quagga_signal_t *sig;
346 
347 	/* First establish some default handlers that can be overridden by
348 	   the application. */
349 	trap_default_signals();
350 
351 	while (i < sigc) {
352 		sig = &signals[i];
353 		if (signal_set(sig->signal) < 0)
354 			exit(-1);
355 		i++;
356 	}
357 
358 	sigmaster.sigc = sigc;
359 	sigmaster.signals = signals;
360 
361 #ifdef SIGEVENT_SCHEDULE_THREAD
362 	sigmaster.t = NULL;
363 	thread_add_timer(m, quagga_signal_timer, &sigmaster,
364 			 QUAGGA_SIGNAL_TIMER_INTERVAL, &sigmaster.t);
365 #endif /* SIGEVENT_SCHEDULE_THREAD */
366 }
367