aboutsummaryrefslogtreecommitdiffstats
path: root/src/sig-monitor.c
blob: b209dbf6ab2ef7d43857f4856e8de2871c0f8ecd (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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
 * Copyright (C) 2015-2020 "IoT.bzh"
 * Author José Bollo <jose.bollo@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define _GNU_SOURCE

/*******************************************************************************
*  sig-monitor is under the control of several compilation flags
*******************************************************************************/

/* controls whether to dump stack or not */
#if !defined(WITH_SIG_MONITOR_DUMPSTACK)
#  define WITH_SIG_MONITOR_DUMPSTACK 1
#endif

/* control whether to monitor signals */
#if !defined(WITH_SIG_MONITOR_SIGNALS)
#  define WITH_SIG_MONITOR_SIGNALS 1
#endif

/* controls whether to monitor calls */
#if !defined(WITH_SIG_MONITOR_FOR_CALL)
#  define WITH_SIG_MONITOR_FOR_CALL 1
#endif

/* control whether to monitor timers */
#if !defined(WITH_SIG_MONITOR_TIMERS)
#  define WITH_SIG_MONITOR_TIMERS 1
#endif

#if !WITH_SIG_MONITOR_SIGNALS
#  undef WITH_SIG_MONITOR_FOR_CALL
#  define WITH_SIG_MONITOR_FOR_CALL 0
#endif

#if !WITH_SIG_MONITOR_FOR_CALL
#  undef WITH_SIG_MONITOR_TIMERS
#  define WITH_SIG_MONITOR_TIMERS 0
#endif

/******************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "sig-monitor.h"

#include "verbose.h"

/******************************************************************************/
#if !WITH_SIG_MONITOR_DUMPSTACK

static inline void dumpstack(int crop, int signum) {}

#else

#include <execinfo.h>

/*
 * Dumps the current stack
 */
static void dumpstack(int crop, int signum)
{
	int idx, count, rc;
	void *addresses[100];
	char **locations;
	char buffer[8000];
	size_t pos, length;

	count = backtrace(addresses, sizeof addresses / sizeof *addresses);
	if (count <= crop)
		crop = 0;
	count -= crop;
	locations = backtrace_symbols(&addresses[crop], count);
	if (locations == NULL)
		ERROR("can't get the backtrace (returned %d addresses)", count);
	else {
		length = sizeof buffer - 1;
		pos = 0;
		idx = 0;
		while (pos < length && idx < count) {
			rc = snprintf(&buffer[pos], length - pos, " [%d/%d] %s\n", idx + 1, count, locations[idx]);
			pos += rc >= 0 ? rc : 0;
			idx++;
		}
		buffer[length] = 0;
		if (signum)
			ERROR("BACKTRACE due to signal %s/%d:\n%s", strsignal(signum), signum, buffer);
		else
			ERROR("BACKTRACE:\n%s", buffer);
		free(locations);
	}
}

#endif
/******************************************************************************/
#if !WITH_SIG_MONITOR_TIMERS

static inline int timeout_create() { return 0; }
static inline int timeout_arm(int timeout) { return 0; }
static inline void timeout_disarm() {}
static inline void timeout_delete() {}

#define SIG_FOR_TIMER   0

#else

#include <time.h>
#include <sys/syscall.h>
#include <signal.h>

#define SIG_FOR_TIMER   SIGVTALRM

/* local per thread timers */
static _Thread_local int thread_timer_set;
static _Thread_local timer_t thread_timerid;

/*
 * Creates a timer for the current thread
 *
 * Returns 0 in case of success
 */
static inline int timeout_create()
{
	int rc;
	struct sigevent sevp;

	if (thread_timer_set)
		rc = 0;
	else {
		sevp.sigev_notify = SIGEV_THREAD_ID;
		sevp.sigev_signo = SIG_FOR_TIMER;
		sevp.sigev_value.sival_ptr = NULL;
#if defined(sigev_notify_thread_id)
		sevp.sigev_notify_thread_id = (pid_t)syscall(SYS_gettid);
#else
		sevp._sigev_un._tid = (pid_t)syscall(SYS_gettid);
#endif
		rc = timer_create(CLOCK_THREAD_CPUTIME_ID, &sevp, &thread_timerid);
		thread_timer_set = !rc;
	}
	return rc;
}

/*
 * Arms the alarm in timeout seconds for the current thread
 */
static inline int timeout_arm(int timeout)
{
	int rc;
	struct itimerspec its;

	rc = timeout_create();
	if (rc == 0) {
		its.it_interval.tv_sec = 0;
		its.it_interval.tv_nsec = 0;
		its.it_value.tv_sec = timeout;
		its.it_value.tv_nsec = 0;
		rc = timer_settime(thread_timerid, 0, &its, NULL);
	}

	return rc;
}

/*
 * Disarms the current alarm
 */
static inline void timeout_disarm()
{
	if (thread_timer_set)
		timeout_arm(0);
}

/*
 * Destroy any alarm resource for the current thread
 */
static inline void timeout_delete()
{
	if (thread_timer_set) {
		timer_delete(thread_timerid);
		thread_timer_set = 0;
	}
}
#endif
/******************************************************************************/
#if !WITH_SIG_MONITOR_FOR_CALL

static inline void monitor_raise(int signum) {}

#else

#include <setjmp.h>

/* local handler */
static _Thread_local sigjmp_buf *error_handler;

static void monitor(int timeout, void (*function)(int sig, void*), void *arg)
{
	volatile int signum, signum2;
	sigjmp_buf jmpbuf, *older;

	older = error_handler;
	signum = setjmp(jmpbuf);
	if (signum == 0) {
		error_handler = &jmpbuf;
		if (timeout) {
			timeout_create();
			timeout_arm(timeout);
		}
		function(0, arg);
	} else {
		signum2 = setjmp(jmpbuf);
		if (signum2 == 0)
			function(signum, arg);
	}
	if (timeout)
		timeout_disarm();
	error_handler = older;
}

static inline void monitor_raise(int signum)
{
	if (error_handler != NULL)
		longjmp(*error_handler, signum);
}
#endif
/******************************************************************************/
#if !WITH_SIG_MONITOR_SIGNALS

static inline int enable_signal_handling() { return 0; }

#else

#include <signal.h>

/* internal signal lists */
static int sigerr[] = { SIGSEGV, SIGFPE, SIGILL, SIGBUS, SIG_FOR_TIMER, 0 };
static int sigterm[] = { SIGINT, SIGABRT, SIGTERM, 0 };

static int exiting = 0;
static int enabled = 0;

/* install the handlers */
static int set_signals_handler(void (*handler)(int), int *signals)
{
	int result = 1;
	struct sigaction sa;

	sa.sa_handler = handler;
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = SA_NODEFER;
	while(*signals > 0) {
		if (sigaction(*signals, &sa, NULL) < 0) {
			ERROR("failed to install signal handler for signal %s: %m", strsignal(*signals));
			result = 0;
		}
		signals++;
	}
	return result;
}

/*
 * rescue exit
 */
static void on_rescue_exit(int signum)
{
	ERROR("Rescue exit for signal %d: %s", signum, strsignal(signum));
	_exit(exiting);
}

/*
 * Do a direct safe exit
 */
static void direct_safe_exit(int code)
{
	set_signals_handler(on_rescue_exit, sigerr);
	set_signals_handler(on_rescue_exit, sigterm);
	exiting = code;
	exit(code);
}

/*
 * Do a safe exit
 */
#if WITH_SIG_MONITOR_NO_DEFERRED_EXIT
#  define safe_exit(x) direct_safe_exit(x)
#else
#include "jobs.h"
static void exit_job(int signum, void* arg)
{
	exiting = (int)(intptr_t)arg;
	if (signum)
		on_rescue_exit(signum);
	exit(exiting);
}

static void safe_exit(int code)
{
	if (jobs_queue(safe_exit, 0, exit_job, (void*)(intptr_t)code))
		direct_safe_exit(code);
}
#endif

#if !WITH_SIG_MONITOR_DUMPSTACK

static inline void safe_dumpstack(int crop, int signum) {}
#define in_safe_dumpstack (0)

#else

static _Thread_local int in_safe_dumpstack;

static void safe_dumpstack_cb(int signum, void *closure)
{
	int *args = closure;
	if (signum)
		ERROR("Can't provide backtrace: raised signal %s", strsignal(signum));
	else
		dumpstack(args[0], args[1]);
}

static void safe_dumpstack(int crop, int signum)
{
	int args[2] = { crop + 3, signum };

	in_safe_dumpstack = 1;
	sig_monitor(0, safe_dumpstack_cb, args);
	in_safe_dumpstack = 0;
}
#endif

/* Handles signals that terminate the process */
static void on_signal_terminate (int signum)
{
	if (!in_safe_dumpstack) {
		ERROR("Terminating signal %d received: %s", signum, strsignal(signum));
		if (signum == SIGABRT)
			safe_dumpstack(3, signum);
	}
	safe_exit(1);
}

/* Handles monitored signals that can be continued */
static void on_signal_error(int signum)
{
	if (!in_safe_dumpstack) {
		ERROR("ALERT! signal %d received: %s", signum, strsignal(signum));

		safe_dumpstack(3, signum);
	}
	monitor_raise(signum);

	if (signum != SIG_FOR_TIMER) {
		ERROR("Unmonitored signal %d received: %s", signum, strsignal(signum));
		safe_exit(2);
	}
}

/*
static void disable_signal_handling()
{
	set_signals_handler(SIG_DFL, sigerr);
	set_signals_handler(SIG_DFL, sigterm);
	enabled = 0;
}
*/

static int enable_signal_handling()
{
	if (!set_signals_handler(on_signal_error, sigerr)
	     || !set_signals_handler(on_signal_terminate, sigterm)) {
		return -1;
	}
	enabled = 1;
	return 0;
}
#endif
/******************************************************************************/

int sig_monitor_init(int enable)
{
	return enable ? enable_signal_handling() : 0;
}

int sig_monitor_init_timeouts()
{
	return timeout_create();
}

void sig_monitor_clean_timeouts()
{
	timeout_delete();
}

void sig_monitor(int timeout, void (*function)(int sig, void*), void *arg)
{
#if WITH_SIG_MONITOR_SIGNALS && WITH_SIG_MONITOR_FOR_CALL
	if (enabled)
		monitor(timeout, function, arg);
	else
#endif
		function(0, arg);
}

void sig_monitor_dumpstack()
{
	return dumpstack(1, 0);
}