summaryrefslogtreecommitdiffstats
path: root/lib/sigevent.c
blob: 3e69f280da2ec4266c58649b9db45521c4164fea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// SPDX-License-Identifier: GPL-2.0-or-later
/* Quagga signal handling functions.
 * Copyright (C) 2004 Paul Jakma,
 */

#include <zebra.h>

#include <signal.h>
#include <sigevent.h>
#include <log.h>
#include <memory.h>
#include <lib_errors.h>

#ifdef HAVE_UCONTEXT_H
#ifdef GNU_LINUX
/* get REG_EIP from ucontext.h */
#ifndef __USE_GNU
#define __USE_GNU
#endif /* __USE_GNU */
#endif /* GNU_LINUX */
#include <ucontext.h>
#endif /* HAVE_UCONTEXT_H */


/* master signals descriptor struct */
static struct frr_sigevent_master_t {
	struct event *t;

	struct frr_signal_t *signals;
	int sigc;

	volatile sig_atomic_t caught;
} sigmaster;

/* Generic signal handler
 * Schedules signal event thread
 */
static void frr_signal_handler(int signo)
{
	int i;
	struct frr_signal_t *sig;

	for (i = 0; i < sigmaster.sigc; i++) {
		sig = &(sigmaster.signals[i]);

		if (sig->signal == signo)
			sig->caught = 1;
	}

	sigmaster.caught = 1;
}

/*
 * Check whether any signals have been received and are pending. This is done
 * with the application's key signals blocked. The complete set of signals
 * is returned in 'setp', so the caller can restore them when appropriate.
 * If there are pending signals, returns 'true', 'false' otherwise.
 */
bool frr_sigevent_check(sigset_t *setp)
{
	sigset_t blocked;
	int i;
	bool ret;

	sigemptyset(setp);
	sigemptyset(&blocked);

	/* Set up mask of application's signals */
	for (i = 0; i < sigmaster.sigc; i++)
		sigaddset(&blocked, sigmaster.signals[i].signal);

	pthread_sigmask(SIG_BLOCK, &blocked, setp);

	/* Now that the application's signals are blocked, test. */
	ret = (sigmaster.caught != 0);

	return ret;
}

/* check if signals have been caught and run appropriate handlers */
int frr_sigevent_process(void)
{
	struct frr_signal_t *sig;
	int i;
#ifdef SIGEVENT_BLOCK_SIGNALS
	/* shouldn't need to block signals, but potentially may be needed */
	sigset_t newmask, oldmask;

	/*
	 * Block most signals, but be careful not to defer SIGTRAP because
	 * doing so breaks gdb, at least on NetBSD 2.0.  Avoid asking to
	 * block SIGKILL, just because we shouldn't be able to do so.
	 */
	sigfillset(&newmask);
	sigdelset(&newmask, SIGTRAP);
	sigdelset(&newmask, SIGKILL);

	if ((sigprocmask(SIG_BLOCK, &newmask, &oldmask)) < 0) {
		flog_err_sys(EC_LIB_SYSTEM_CALL,
			     "frr_signal_timer: couldnt block signals!");
		return -1;
	}
#endif /* SIGEVENT_BLOCK_SIGNALS */

	if (sigmaster.caught > 0) {
		sigmaster.caught = 0;
		/* must not read or set sigmaster.caught after here,
		 * race condition with per-sig caught flags if one does
		 */

		for (i = 0; i < sigmaster.sigc; i++) {
			sig = &(sigmaster.signals[i]);

			if (sig->caught > 0) {
				sig->caught = 0;
				if (sig->handler)
					sig->handler();
			}
		}
	}

#ifdef SIGEVENT_BLOCK_SIGNALS
	if (sigprocmask(SIG_UNBLOCK, &oldmask, NULL) < 0)
		return -1;
#endif /* SIGEVENT_BLOCK_SIGNALS */

	return 0;
}

#ifdef SIGEVENT_SCHEDULE_THREAD
/* timer thread to check signals. shouldn't be needed */
void frr_signal_timer(struct event *t)
{
	struct frr_sigevent_master_t *sigm;

	sigm = EVENT_ARG(t);
	sigm->t = NULL;
	event_add_timer(sigm->t->master, frr_signal_timer, &sigmaster,
			FRR_SIGNAL_TIMER_INTERVAL, &sigm->t);
	frr_sigevent_process();
}
#endif /* SIGEVENT_SCHEDULE_THREAD */

/* Initialization of signal handles. */
/* Signal wrapper. */
static int signal_set(int signo)
{
	int ret;
	struct sigaction sig;
	struct sigaction osig;

	sig.sa_handler = &frr_signal_handler;
	sigfillset(&sig.sa_mask);
	sig.sa_flags = 0;
	if (signo == SIGALRM) {
#ifdef SA_INTERRUPT
		sig.sa_flags |= SA_INTERRUPT; /* SunOS */
#endif
	} else {
#ifdef SA_RESTART
		sig.sa_flags |= SA_RESTART;
#endif /* SA_RESTART */
	}

	ret = sigaction(signo, &sig, &osig);
	if (ret < 0)
		return ret;
	else
		return 0;
}

/* XXX This function should be enhanced to support more platforms
       (it currently works only on Linux/x86). */
static void *program_counter(void *context)
{
#ifdef HAVE_UCONTEXT_H
#ifdef GNU_LINUX
/* these are from GNU libc, rather than Linux, strictly speaking */
#if defined(REG_EIP)
#  define REG_INDEX REG_EIP
#elif defined(REG_RIP)
#  define REG_INDEX REG_RIP
#elif defined(__powerpc__)
#  define REG_INDEX 32
#endif
#endif		       /* GNU_LINUX */

#ifdef REG_INDEX
#ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_GREGS
#  define REGS gregs[REG_INDEX]
#elif defined(HAVE_UCONTEXT_T_UC_MCONTEXT_UC_REGS)
#  define REGS uc_regs->gregs[REG_INDEX]
#endif /* HAVE_UCONTEXT_T_UC_MCONTEXT_GREGS */
#endif /* REG_INDEX */

#ifdef REGS
	if (context)
		return (void *)(((ucontext_t *)context)->uc_mcontext.REGS);
#elif defined(HAVE_UCONTEXT_T_UC_MCONTEXT_REGS__NIP)
	/* older Linux / struct pt_regs ? */
	if (context)
		return (void *)(((ucontext_t *)context)->uc_mcontext.regs->nip);
#endif /* REGS */

#endif /* HAVE_UCONTEXT_H */
	return NULL;
}

static void __attribute__((noreturn))
exit_handler(int signo, siginfo_t *siginfo, void *context)
{
	void *pc = program_counter(context);

	zlog_signal(signo, "exiting...", siginfo, pc);
	_exit(128 + signo);
}

static void __attribute__((noreturn))
core_handler(int signo, siginfo_t *siginfo, void *context)
{
	void *pc = program_counter(context);

	/* make sure we don't hang in here.  default for SIGALRM is terminate.
	 * - if we're in backtrace for more than a second, abort. */
	struct sigaction sa_default = {.sa_handler = SIG_DFL};

	sigaction(SIGALRM, &sa_default, NULL);
	sigaction(signo, &sa_default, NULL);

	sigset_t sigset;

	sigemptyset(&sigset);
	sigaddset(&sigset, SIGALRM);
	sigprocmask(SIG_UNBLOCK, &sigset, NULL);

	alarm(1);

	zlog_signal(signo, "aborting...", siginfo, pc);

	/* dump memory stats on core */
	log_memstats(stderr, "core_handler");

	/*
	 * This is a buffer flush because FRR is going down
	 * hard.  This is especially important if the crash
	 * was caused by a memory operation and if we call
	 * zlog_tls_buffer_fini() then it has memory
	 * operations as well.  This will cause the
	 * core dump to not happen.  BAD MOJO
	 * So this is intentional, let's try to flush
	 * what we can and let the crash happen.
	 */
	zlog_tls_buffer_flush();

	/* give the kernel a chance to generate a coredump */
	sigaddset(&sigset, signo);
	sigprocmask(SIG_UNBLOCK, &sigset, NULL);
	raise(signo);

	/* only chance to end up here is if the default action for signo is
	 * something other than kill or coredump the process
	 */
	_exit(128 + signo);
}

static void trap_default_signals(void)
{
	static const int core_signals[] = {
		SIGQUIT, SIGILL, SIGABRT,
#ifdef SIGEMT
		SIGEMT,
#endif
		SIGFPE,  SIGBUS, SIGSEGV,
#ifdef SIGSYS
		SIGSYS,
#endif
#ifdef SIGXCPU
		SIGXCPU,
#endif
#ifdef SIGXFSZ
		SIGXFSZ,
#endif
	};
	static const int exit_signals[] = {
		SIGHUP,    SIGINT, SIGALRM, SIGTERM, SIGUSR1, SIGUSR2,
#ifdef SIGPOLL
		SIGPOLL,
#endif
#ifdef SIGVTALRM
		SIGVTALRM,
#endif
#ifdef SIGSTKFLT
		SIGSTKFLT,
#endif
	};
	static const int ignore_signals[] = {
		SIGPIPE,
	};
	static const struct {
		const int *sigs;
		unsigned int nsigs;
		void (*handler)(int signo, siginfo_t *info, void *context);
	} sigmap[] = {
		{core_signals, array_size(core_signals), core_handler},
		{exit_signals, array_size(exit_signals), exit_handler},
		{ignore_signals, array_size(ignore_signals), NULL},
	};
	unsigned int i;

	for (i = 0; i < array_size(sigmap); i++) {
		unsigned int j;

		for (j = 0; j < sigmap[i].nsigs; j++) {
			struct sigaction oact;
			if ((sigaction(sigmap[i].sigs[j], NULL, &oact) == 0)
			    && (oact.sa_handler == SIG_DFL)) {
				struct sigaction act;
				sigfillset(&act.sa_mask);
				if (sigmap[i].handler == NULL) {
					act.sa_handler = SIG_IGN;
					act.sa_flags = 0;
				} else {
					/* Request extra arguments to signal
					 * handler. */
					act.sa_sigaction = sigmap[i].handler;
					act.sa_flags = SA_SIGINFO;
#ifdef SA_RESETHAND
					/* don't try to print backtraces
					 * recursively */
					if (sigmap[i].handler == core_handler)
						act.sa_flags |= SA_RESETHAND;
#endif
				}
				if (sigaction(sigmap[i].sigs[j], &act, NULL)
				    < 0)
					flog_err(
						EC_LIB_SYSTEM_CALL,
						"Unable to set signal handler for signal %d: %s",
						sigmap[i].sigs[j],
						safe_strerror(errno));
			}
		}
	}
}

void signal_init(struct event_loop *m, int sigc, struct frr_signal_t signals[])
{

	int i = 0;
	struct frr_signal_t *sig;

	/* First establish some default handlers that can be overridden by
	   the application. */
	trap_default_signals();

	while (i < sigc) {
		sig = &signals[i];
		if (signal_set(sig->signal) < 0)
			exit(-1);
		i++;
	}

	sigmaster.sigc = sigc;
	sigmaster.signals = signals;

#ifdef SIGEVENT_SCHEDULE_THREAD
	sigmaster.t = NULL;
	event_add_timer(m, frr_signal_timer, &sigmaster,
			FRR_SIGNAL_TIMER_INTERVAL, &sigmaster.t);
#endif /* SIGEVENT_SCHEDULE_THREAD */
}