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;
44 * Dumps the current stack
46 static void dumpstack(int crop, int signum)
54 count = backtrace(addresses, sizeof addresses / sizeof *addresses);
58 locations = backtrace_symbols(&addresses[crop], count);
59 if (locations == NULL)
60 ERROR("can't get the backtrace (returned %d addresses)", count);
62 length = sizeof buffer - 1;
65 while (pos < length && idx < count) {
66 rc = snprintf(&buffer[pos], length - pos, " [%d/%d] %s\n", idx + 1, count, locations[idx]);
67 pos += rc >= 0 ? rc : 0;
72 ERROR("BACKTRACE due to signal %s/%d:\n%s", strsignal(signum), signum, buffer);
74 ERROR("BACKTRACE:\n%s", buffer);
79 static void safe_dumpstack_cb(int signum, void *closure)
83 ERROR("Can't provide backtrace: raised signal %s", strsignal(signum));
85 dumpstack(args[0], args[1]);
88 static void safe_dumpstack(int crop, int signum)
90 int args[2] = { crop + 3, signum };
92 in_safe_dumpstack = 1;
93 sig_monitor(0, safe_dumpstack_cb, args);
94 in_safe_dumpstack = 0;
98 * Creates a timer for the current thread
100 * Returns 0 in case of success
102 static inline int timeout_create()
105 struct sigevent sevp;
107 if (thread_timer_set)
110 sevp.sigev_notify = SIGEV_THREAD_ID;
111 sevp.sigev_signo = SIG_FOR_TIMER;
112 sevp.sigev_value.sival_ptr = NULL;
113 #if defined(sigev_notify_thread_id)
114 sevp.sigev_notify_thread_id = (pid_t)syscall(SYS_gettid);
116 sevp._sigev_un._tid = (pid_t)syscall(SYS_gettid);
118 rc = timer_create(CLOCK_THREAD_CPUTIME_ID, &sevp, &thread_timerid);
119 thread_timer_set = !rc;
125 * Arms the alarm in timeout seconds for the current thread
127 static inline int timeout_arm(int timeout)
130 struct itimerspec its;
132 rc = timeout_create();
134 its.it_interval.tv_sec = 0;
135 its.it_interval.tv_nsec = 0;
136 its.it_value.tv_sec = timeout;
137 its.it_value.tv_nsec = 0;
138 rc = timer_settime(thread_timerid, 0, &its, NULL);
145 * Disarms the current alarm
147 static inline void timeout_disarm()
149 if (thread_timer_set)
154 * Destroy any alarm resource for the current thread
156 static inline void timeout_delete()
158 if (thread_timer_set) {
159 timer_delete(thread_timerid);
160 thread_timer_set = 0;
165 /* Handles signals that terminate the process */
166 static void on_signal_terminate (int signum)
168 if (!in_safe_dumpstack) {
169 ERROR("Terminating signal %d received: %s", signum, strsignal(signum));
170 if (signum == SIGABRT)
171 safe_dumpstack(3, signum);
176 /* Handles monitored signals that can be continued */
177 static void on_signal_error(int signum)
179 if (in_safe_dumpstack)
180 longjmp(*error_handler, signum);
182 ERROR("ALERT! signal %d received: %s", signum, strsignal(signum));
183 if (error_handler == NULL && signum == SIG_FOR_TIMER)
186 safe_dumpstack(3, signum);
188 // unlock signal to allow a new signal to come
189 if (error_handler != NULL)
190 longjmp(*error_handler, signum);
192 ERROR("Unmonitored signal %d received: %s", signum, strsignal(signum));
196 /* install the handlers */
197 static int install(void (*handler)(int), int *signals)
202 sa.sa_handler = handler;
203 sigemptyset(&sa.sa_mask);
204 sa.sa_flags = SA_NODEFER;
205 while(*signals > 0) {
206 if (sigaction(*signals, &sa, NULL) < 0) {
207 ERROR("failed to install signal handler for signal %s: %m", strsignal(*signals));
215 int sig_monitor_init()
217 static int sigerr[] = { SIG_FOR_TIMER, SIGSEGV, SIGFPE, SIGILL, SIGBUS, 0 };
218 static int sigterm[] = { SIGINT, SIGABRT, SIGTERM, 0 };
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;