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 };
46 static int exiting = 0;
49 * Dumps the current stack
51 static void dumpstack(int crop, int signum)
59 count = backtrace(addresses, sizeof addresses / sizeof *addresses);
63 locations = backtrace_symbols(&addresses[crop], count);
64 if (locations == NULL)
65 ERROR("can't get the backtrace (returned %d addresses)", count);
67 length = sizeof buffer - 1;
70 while (pos < length && idx < count) {
71 rc = snprintf(&buffer[pos], length - pos, " [%d/%d] %s\n", idx + 1, count, locations[idx]);
72 pos += rc >= 0 ? rc : 0;
77 ERROR("BACKTRACE due to signal %s/%d:\n%s", strsignal(signum), signum, buffer);
79 ERROR("BACKTRACE:\n%s", buffer);
84 static void safe_dumpstack_cb(int signum, void *closure)
88 ERROR("Can't provide backtrace: raised signal %s", strsignal(signum));
90 dumpstack(args[0], args[1]);
93 static void safe_dumpstack(int crop, int signum)
95 int args[2] = { crop + 3, signum };
97 in_safe_dumpstack = 1;
98 sig_monitor(0, safe_dumpstack_cb, args);
99 in_safe_dumpstack = 0;
103 * Creates a timer for the current thread
105 * Returns 0 in case of success
107 static inline int timeout_create()
110 struct sigevent sevp;
112 if (thread_timer_set)
115 sevp.sigev_notify = SIGEV_THREAD_ID;
116 sevp.sigev_signo = SIG_FOR_TIMER;
117 sevp.sigev_value.sival_ptr = NULL;
118 #if defined(sigev_notify_thread_id)
119 sevp.sigev_notify_thread_id = (pid_t)syscall(SYS_gettid);
121 sevp._sigev_un._tid = (pid_t)syscall(SYS_gettid);
123 rc = timer_create(CLOCK_THREAD_CPUTIME_ID, &sevp, &thread_timerid);
124 thread_timer_set = !rc;
130 * Arms the alarm in timeout seconds for the current thread
132 static inline int timeout_arm(int timeout)
135 struct itimerspec its;
137 rc = timeout_create();
139 its.it_interval.tv_sec = 0;
140 its.it_interval.tv_nsec = 0;
141 its.it_value.tv_sec = timeout;
142 its.it_value.tv_nsec = 0;
143 rc = timer_settime(thread_timerid, 0, &its, NULL);
150 * Disarms the current alarm
152 static inline void timeout_disarm()
154 if (thread_timer_set)
159 * Destroy any alarm resource for the current thread
161 static inline void timeout_delete()
163 if (thread_timer_set) {
164 timer_delete(thread_timerid);
165 thread_timer_set = 0;
169 /* install the handlers */
170 static int install(void (*handler)(int), int *signals)
175 sa.sa_handler = handler;
176 sigemptyset(&sa.sa_mask);
177 sa.sa_flags = SA_NODEFER;
178 while(*signals > 0) {
179 if (sigaction(*signals, &sa, NULL) < 0) {
180 ERROR("failed to install signal handler for signal %s: %m", strsignal(*signals));
191 static void on_rescue_exit(int signum)
193 ERROR("Rescue exit for signal %d: %s", signum, strsignal(signum));
200 static void safe_exit(int code)
202 install(on_rescue_exit, sigerr);
203 install(on_rescue_exit, sigterm);
208 /* Handles signals that terminate the process */
209 static void on_signal_terminate (int signum)
211 if (!in_safe_dumpstack) {
212 ERROR("Terminating signal %d received: %s", signum, strsignal(signum));
213 if (signum == SIGABRT)
214 safe_dumpstack(3, signum);
219 /* Handles monitored signals that can be continued */
220 static void on_signal_error(int signum)
222 if (in_safe_dumpstack)
223 longjmp(*error_handler, signum);
225 ERROR("ALERT! signal %d received: %s", signum, strsignal(signum));
226 if (error_handler == NULL && signum == SIG_FOR_TIMER)
229 safe_dumpstack(3, signum);
231 // unlock signal to allow a new signal to come
232 if (error_handler != NULL)
233 longjmp(*error_handler, signum);
235 ERROR("Unmonitored signal %d received: %s", signum, strsignal(signum));
239 int sig_monitor_init()
241 return (install(on_signal_error, sigerr) & install(on_signal_terminate, sigterm)) - 1;
244 int sig_monitor_init_timeouts()
246 return timeout_create();
249 void sig_monitor_clean_timeouts()
254 void sig_monitor(int timeout, void (*function)(int sig, void*), void *arg)
256 volatile int signum, signum2;
257 sigjmp_buf jmpbuf, *older;
259 older = error_handler;
260 signum = setjmp(jmpbuf);
262 error_handler = &jmpbuf;
264 timeout_arm(timeout);
267 signum2 = setjmp(jmpbuf);
269 function(signum, arg);
271 error_handler = older;