2 * Copyright (C) 2017 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
27 #include <sys/syscall.h>
30 #include "sig-monitor.h"
33 #define SIG_FOR_TIMER SIGVTALRM
36 static _Thread_local sigjmp_buf *error_handler;
37 static _Thread_local int in_safe_dumpstack;
40 static _Thread_local int thread_timer_set;
41 static _Thread_local timer_t thread_timerid;
43 /* internal signal lists */
44 static int sigerr[] = { SIG_FOR_TIMER, SIGSEGV, SIGFPE, SIGILL, SIGBUS, 0 };
45 static int sigterm[] = { SIGINT, SIGABRT, SIGTERM, 0 };
47 * Dumps the current stack
49 static void dumpstack(int crop, int signum)
57 count = backtrace(addresses, sizeof addresses / sizeof *addresses);
61 locations = backtrace_symbols(&addresses[crop], count);
62 if (locations == NULL)
63 ERROR("can't get the backtrace (returned %d addresses)", count);
65 length = sizeof buffer - 1;
68 while (pos < length && idx < count) {
69 rc = snprintf(&buffer[pos], length - pos, " [%d/%d] %s\n", idx + 1, count, locations[idx]);
70 pos += rc >= 0 ? rc : 0;
75 ERROR("BACKTRACE due to signal %s/%d:\n%s", strsignal(signum), signum, buffer);
77 ERROR("BACKTRACE:\n%s", buffer);
82 static void safe_dumpstack_cb(int signum, void *closure)
86 ERROR("Can't provide backtrace: raised signal %s", strsignal(signum));
88 dumpstack(args[0], args[1]);
91 static void safe_dumpstack(int crop, int signum)
93 int args[2] = { crop + 3, signum };
95 in_safe_dumpstack = 1;
96 sig_monitor(0, safe_dumpstack_cb, args);
97 in_safe_dumpstack = 0;
101 * Creates a timer for the current thread
103 * Returns 0 in case of success
105 static inline int timeout_create()
108 struct sigevent sevp;
110 if (thread_timer_set)
113 sevp.sigev_notify = SIGEV_THREAD_ID;
114 sevp.sigev_signo = SIG_FOR_TIMER;
115 sevp.sigev_value.sival_ptr = NULL;
116 #if defined(sigev_notify_thread_id)
117 sevp.sigev_notify_thread_id = (pid_t)syscall(SYS_gettid);
119 sevp._sigev_un._tid = (pid_t)syscall(SYS_gettid);
121 rc = timer_create(CLOCK_THREAD_CPUTIME_ID, &sevp, &thread_timerid);
122 thread_timer_set = !rc;
128 * Arms the alarm in timeout seconds for the current thread
130 static inline int timeout_arm(int timeout)
133 struct itimerspec its;
135 rc = timeout_create();
137 its.it_interval.tv_sec = 0;
138 its.it_interval.tv_nsec = 0;
139 its.it_value.tv_sec = timeout;
140 its.it_value.tv_nsec = 0;
141 rc = timer_settime(thread_timerid, 0, &its, NULL);
148 * Disarms the current alarm
150 static inline void timeout_disarm()
152 if (thread_timer_set)
157 * Destroy any alarm resource for the current thread
159 static inline void timeout_delete()
161 if (thread_timer_set) {
162 timer_delete(thread_timerid);
163 thread_timer_set = 0;
167 /* install the handlers */
168 static int install(void (*handler)(int), int *signals)
173 sa.sa_handler = handler;
174 sigemptyset(&sa.sa_mask);
175 sa.sa_flags = SA_NODEFER;
176 while(*signals > 0) {
177 if (sigaction(*signals, &sa, NULL) < 0) {
178 ERROR("failed to install signal handler for signal %s: %m", strsignal(*signals));
187 /* Handles signals that terminate the process */
188 static void on_signal_terminate (int signum)
190 if (!in_safe_dumpstack) {
191 ERROR("Terminating signal %d received: %s", signum, strsignal(signum));
192 if (signum == SIGABRT)
193 safe_dumpstack(3, signum);
198 /* Handles monitored signals that can be continued */
199 static void on_signal_error(int signum)
201 if (in_safe_dumpstack)
202 longjmp(*error_handler, signum);
204 ERROR("ALERT! signal %d received: %s", signum, strsignal(signum));
205 if (error_handler == NULL && signum == SIG_FOR_TIMER)
208 safe_dumpstack(3, signum);
210 // unlock signal to allow a new signal to come
211 if (error_handler != NULL)
212 longjmp(*error_handler, signum);
214 ERROR("Unmonitored signal %d received: %s", signum, strsignal(signum));
218 int sig_monitor_init()
220 return (install(on_signal_error, sigerr) & install(on_signal_terminate, sigterm)) - 1;
223 int sig_monitor_init_timeouts()
225 return timeout_create();
228 void sig_monitor_clean_timeouts()
233 void sig_monitor(int timeout, void (*function)(int sig, void*), void *arg)
235 volatile int signum, signum2;
236 sigjmp_buf jmpbuf, *older;
238 older = error_handler;
239 signum = setjmp(jmpbuf);
241 error_handler = &jmpbuf;
243 timeout_arm(timeout);
246 signum2 = setjmp(jmpbuf);
248 function(signum, arg);
250 error_handler = older;