2 * Copyright (C) 2015-2020 "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.
20 /*******************************************************************************
21 * sig-monitor is under the control of several compilation flags
22 *******************************************************************************/
24 /* controls whether to dump stack or not */
25 #if !defined(WITH_SIG_MONITOR_DUMPSTACK)
26 # define WITH_SIG_MONITOR_DUMPSTACK 1
29 /* control whether to monitor signals */
30 #if !defined(WITH_SIG_MONITOR_SIGNALS)
31 # define WITH_SIG_MONITOR_SIGNALS 1
34 /* controls whether to monitor calls */
35 #if !defined(WITH_SIG_MONITOR_FOR_CALL)
36 # define WITH_SIG_MONITOR_FOR_CALL 1
39 /* control whether to monitor timers */
40 #if !defined(WITH_SIG_MONITOR_TIMERS)
41 # define WITH_SIG_MONITOR_TIMERS 1
44 #if !WITH_SIG_MONITOR_SIGNALS
45 # undef WITH_SIG_MONITOR_FOR_CALL
46 # define WITH_SIG_MONITOR_FOR_CALL 0
49 #if !WITH_SIG_MONITOR_FOR_CALL
50 # undef WITH_SIG_MONITOR_TIMERS
51 # define WITH_SIG_MONITOR_TIMERS 0
54 /******************************************************************************/
61 #include "sig-monitor.h"
65 /******************************************************************************/
66 #if !WITH_SIG_MONITOR_DUMPSTACK
68 static inline void dumpstack(int crop, int signum) {}
75 * Dumps the current stack
77 static void dumpstack(int crop, int signum)
85 count = backtrace(addresses, sizeof addresses / sizeof *addresses);
89 locations = backtrace_symbols(&addresses[crop], count);
90 if (locations == NULL)
91 ERROR("can't get the backtrace (returned %d addresses)", count);
93 length = sizeof buffer - 1;
96 while (pos < length && idx < count) {
97 rc = snprintf(&buffer[pos], length - pos, " [%d/%d] %s\n", idx + 1, count, locations[idx]);
98 pos += rc >= 0 ? rc : 0;
103 ERROR("BACKTRACE due to signal %s/%d:\n%s", strsignal(signum), signum, buffer);
105 ERROR("BACKTRACE:\n%s", buffer);
111 /******************************************************************************/
112 #if !WITH_SIG_MONITOR_TIMERS
114 static inline int timeout_create() { return 0; }
115 static inline int timeout_arm(int timeout) { return 0; }
116 static inline void timeout_disarm() {}
117 static inline void timeout_delete() {}
119 #define SIG_FOR_TIMER 0
124 #include <sys/syscall.h>
127 #define SIG_FOR_TIMER SIGVTALRM
129 /* local per thread timers */
130 static _Thread_local int thread_timer_set;
131 static _Thread_local timer_t thread_timerid;
134 * Creates a timer for the current thread
136 * Returns 0 in case of success
138 static inline int timeout_create()
141 struct sigevent sevp;
143 if (thread_timer_set)
146 sevp.sigev_notify = SIGEV_THREAD_ID;
147 sevp.sigev_signo = SIG_FOR_TIMER;
148 sevp.sigev_value.sival_ptr = NULL;
149 #if defined(sigev_notify_thread_id)
150 sevp.sigev_notify_thread_id = (pid_t)syscall(SYS_gettid);
152 sevp._sigev_un._tid = (pid_t)syscall(SYS_gettid);
154 rc = timer_create(CLOCK_THREAD_CPUTIME_ID, &sevp, &thread_timerid);
155 thread_timer_set = !rc;
161 * Arms the alarm in timeout seconds for the current thread
163 static inline int timeout_arm(int timeout)
166 struct itimerspec its;
168 rc = timeout_create();
170 its.it_interval.tv_sec = 0;
171 its.it_interval.tv_nsec = 0;
172 its.it_value.tv_sec = timeout;
173 its.it_value.tv_nsec = 0;
174 rc = timer_settime(thread_timerid, 0, &its, NULL);
181 * Disarms the current alarm
183 static inline void timeout_disarm()
185 if (thread_timer_set)
190 * Destroy any alarm resource for the current thread
192 static inline void timeout_delete()
194 if (thread_timer_set) {
195 timer_delete(thread_timerid);
196 thread_timer_set = 0;
200 /******************************************************************************/
201 #if !WITH_SIG_MONITOR_FOR_CALL
203 static inline void monitor_raise(int signum) {}
210 static _Thread_local sigjmp_buf *error_handler;
212 static void monitor(int timeout, void (*function)(int sig, void*), void *arg)
214 volatile int signum, signum2;
215 sigjmp_buf jmpbuf, *older;
217 older = error_handler;
218 signum = setjmp(jmpbuf);
220 error_handler = &jmpbuf;
223 timeout_arm(timeout);
227 signum2 = setjmp(jmpbuf);
229 function(signum, arg);
233 error_handler = older;
236 static inline void monitor_raise(int signum)
238 if (error_handler != NULL)
239 longjmp(*error_handler, signum);
242 /******************************************************************************/
243 #if !WITH_SIG_MONITOR_SIGNALS
245 static inline int enable_signal_handling() { return 0; }
251 /* internal signal lists */
252 static int sigerr[] = { SIGSEGV, SIGFPE, SIGILL, SIGBUS, SIG_FOR_TIMER, 0 };
253 static int sigterm[] = { SIGINT, SIGABRT, SIGTERM, 0 };
255 static int exiting = 0;
256 static int enabled = 0;
258 /* install the handlers */
259 static int set_signals_handler(void (*handler)(int), int *signals)
264 sa.sa_handler = handler;
265 sigemptyset(&sa.sa_mask);
266 sa.sa_flags = SA_NODEFER;
267 while(*signals > 0) {
268 if (sigaction(*signals, &sa, NULL) < 0) {
269 ERROR("failed to install signal handler for signal %s: %m", strsignal(*signals));
280 static void on_rescue_exit(int signum)
282 ERROR("Rescue exit for signal %d: %s", signum, strsignal(signum));
287 * Do a direct safe exit
289 static void direct_safe_exit(int code)
291 set_signals_handler(on_rescue_exit, sigerr);
292 set_signals_handler(on_rescue_exit, sigterm);
300 #if WITH_SIG_MONITOR_NO_DEFERRED_EXIT
301 # define safe_exit(x) direct_safe_exit(x)
304 static void exit_job(int signum, void* arg)
306 exiting = (int)(intptr_t)arg;
308 on_rescue_exit(signum);
312 static void safe_exit(int code)
314 if (jobs_queue(safe_exit, 0, exit_job, (void*)(intptr_t)code))
315 direct_safe_exit(code);
319 #if !WITH_SIG_MONITOR_DUMPSTACK
321 static inline void safe_dumpstack(int crop, int signum) {}
322 #define in_safe_dumpstack (0)
326 static _Thread_local int in_safe_dumpstack;
328 static void safe_dumpstack_cb(int signum, void *closure)
332 ERROR("Can't provide backtrace: raised signal %s", strsignal(signum));
334 dumpstack(args[0], args[1]);
337 static void safe_dumpstack(int crop, int signum)
339 int args[2] = { crop + 3, signum };
341 in_safe_dumpstack = 1;
342 sig_monitor(0, safe_dumpstack_cb, args);
343 in_safe_dumpstack = 0;
347 /* Handles signals that terminate the process */
348 static void on_signal_terminate (int signum)
350 if (!in_safe_dumpstack) {
351 ERROR("Terminating signal %d received: %s", signum, strsignal(signum));
352 if (signum == SIGABRT)
353 safe_dumpstack(3, signum);
358 /* Handles monitored signals that can be continued */
359 static void on_signal_error(int signum)
361 if (!in_safe_dumpstack) {
362 ERROR("ALERT! signal %d received: %s", signum, strsignal(signum));
364 safe_dumpstack(3, signum);
366 monitor_raise(signum);
368 if (signum != SIG_FOR_TIMER) {
369 ERROR("Unmonitored signal %d received: %s", signum, strsignal(signum));
375 static void disable_signal_handling()
377 set_signals_handler(SIG_DFL, sigerr);
378 set_signals_handler(SIG_DFL, sigterm);
383 static int enable_signal_handling()
385 if (!set_signals_handler(on_signal_error, sigerr)
386 || !set_signals_handler(on_signal_terminate, sigterm)) {
393 /******************************************************************************/
395 int sig_monitor_init(int enable)
397 return enable ? enable_signal_handling() : 0;
400 int sig_monitor_init_timeouts()
402 return timeout_create();
405 void sig_monitor_clean_timeouts()
410 void sig_monitor(int timeout, void (*function)(int sig, void*), void *arg)
412 #if WITH_SIG_MONITOR_SIGNALS && WITH_SIG_MONITOR_FOR_CALL
414 monitor(timeout, function, arg);
420 void sig_monitor_dumpstack()
422 return dumpstack(1, 0);