summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2017-09-06 18:13:21 +0200
committerJosé Bollo <jose.bollo@iot.bzh>2017-09-07 09:23:54 +0200
commitbee892ba769f78ede968ad9b6aea006b6aee26e9 (patch)
treee4dbcb5e7b7050fe70f40f967c2ce2850bcae37f
parent4d5ce1b0b3b41b8fb53126a0c67534db7d010a75 (diff)
sig-monitor: handle SIGSEGV in backtrace
There was cases were the backtrace raised a segmentation fault. This commit also improves unblocking of signals in handler. Change-Id: I09493d3ecc4b477660366ea213bf451c9ae7d517 Signed-off-by: José Bollo <jose.bollo@iot.bzh>
-rw-r--r--src/sig-monitor.c48
1 files changed, 37 insertions, 11 deletions
diff --git a/src/sig-monitor.c b/src/sig-monitor.c
index a92fc814..75982507 100644
--- a/src/sig-monitor.c
+++ b/src/sig-monitor.c
@@ -34,6 +34,7 @@
/* local handler */
static _Thread_local sigjmp_buf *error_handler;
+static _Thread_local int in_safe_dumpstack;
/* local timers */
static _Thread_local int thread_timer_set;
@@ -75,6 +76,24 @@ static void dumpstack(int crop, int signum)
}
}
+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, signum };
+
+ in_safe_dumpstack = 1;
+ sig_monitor(0, safe_dumpstack_cb, args);
+ in_safe_dumpstack = 0;
+}
+
/*
* Creates a timer for the current thread
*
@@ -146,9 +165,11 @@ static inline void timeout_delete()
/* Handles signals that terminate the process */
static void on_signal_terminate (int signum)
{
- ERROR("Terminating signal %d received: %s", signum, strsignal(signum));
- if (signum == SIGABRT)
- dumpstack(3, signum);
+ if (!in_safe_dumpstack) {
+ ERROR("Terminating signal %d received: %s", signum, strsignal(signum));
+ if (signum == SIGABRT)
+ safe_dumpstack(3, signum);
+ }
exit(1);
}
@@ -157,19 +178,19 @@ static void on_signal_error(int signum)
{
sigset_t sigset;
+ if (in_safe_dumpstack)
+ longjmp(*error_handler, signum);
+
ERROR("ALERT! signal %d received: %s", signum, strsignal(signum));
if (error_handler == NULL && signum == SIG_FOR_TIMER)
return;
- dumpstack(3, signum);
+ safe_dumpstack(3, signum);
// unlock signal to allow a new signal to come
- if (error_handler != NULL) {
- sigemptyset(&sigset);
- sigaddset(&sigset, signum);
- sigprocmask(SIG_UNBLOCK, &sigset, 0);
+ if (error_handler != NULL)
longjmp(*error_handler, signum);
- }
+
ERROR("Unmonitored signal %d received: %s", signum, strsignal(signum));
exit(2);
}
@@ -178,9 +199,14 @@ static void on_signal_error(int signum)
static int install(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 (signal(*signals, handler) == SIG_ERR) {
- ERROR("failed to install signal handler for signal %s", strsignal(*signals));
+ if (sigaction(*signals, &sa, NULL) < 0) {
+ ERROR("failed to install signal handler for signal %s: %m", strsignal(*signals));
result = 0;
}
signals++;