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.
26 #include <sys/syscall.h>
29 #include "sig-monitor.h"
32 #define SIG_FOR_TIMER SIGVTALRM
35 static _Thread_local sigjmp_buf *error_handler;
38 static _Thread_local int thread_timer_set;
39 static _Thread_local timer_t thread_timerid;
42 * Dumps the current stack
44 static void dumpstack(int crop)
47 void *addresses[1000];
50 count = backtrace(addresses, sizeof addresses / sizeof *addresses);
51 locations = backtrace_symbols(addresses, count);
52 if (locations == NULL)
53 ERROR("can't get the backtrace (returned %d addresses)", count);
55 for (idx = crop; idx < count; idx++)
56 ERROR("[BACKTRACE %d/%d] %s", idx - crop + 1, count - crop, locations[idx]);
62 * Creates a timer for the current thread
64 * Returns 0 in case of success
66 static inline int timeout_create()
74 sevp.sigev_notify = SIGEV_THREAD_ID;
75 sevp.sigev_signo = SIG_FOR_TIMER;
76 sevp.sigev_value.sival_ptr = NULL;
77 #if defined(sigev_notify_thread_id)
78 sevp.sigev_notify_thread_id = (pid_t)syscall(SYS_gettid);
80 sevp._sigev_un._tid = (pid_t)syscall(SYS_gettid);
82 rc = timer_create(CLOCK_THREAD_CPUTIME_ID, &sevp, &thread_timerid);
83 thread_timer_set = !rc;
89 * Arms the alarm in timeout seconds for the current thread
91 static inline int timeout_arm(int timeout)
94 struct itimerspec its;
96 rc = timeout_create();
98 its.it_interval.tv_sec = 0;
99 its.it_interval.tv_nsec = 0;
100 its.it_value.tv_sec = timeout;
101 its.it_value.tv_nsec = 0;
102 rc = timer_settime(thread_timerid, 0, &its, NULL);
109 * Disarms the current alarm
111 static inline void timeout_disarm()
113 if (thread_timer_set)
118 * Destroy any alarm resource for the current thread
120 static inline void timeout_delete()
122 if (thread_timer_set) {
123 timer_delete(thread_timerid);
124 thread_timer_set = 0;
129 /* Handles signals that terminate the process */
130 static void on_signal_terminate (int signum)
132 ERROR("Terminating signal %d received: %s", signum, strsignal(signum));
133 if (signum == SIGABRT)
138 /* Handles monitored signals that can be continued */
139 static void on_signal_error(int signum)
143 ERROR("ALERT! signal %d received: %s", signum, strsignal(signum));
144 if (error_handler == NULL && signum == SIG_FOR_TIMER)
149 // unlock signal to allow a new signal to come
150 if (error_handler != NULL) {
151 sigemptyset(&sigset);
152 sigaddset(&sigset, signum);
153 sigprocmask(SIG_UNBLOCK, &sigset, 0);
154 longjmp(*error_handler, signum);
156 ERROR("Unmonitored signal %d received: %s", signum, strsignal(signum));
160 /* install the handlers */
161 static int install(void (*handler)(int), int *signals)
164 while(*signals > 0) {
165 if (signal(*signals, handler) == SIG_ERR) {
166 ERROR("failed to install signal handler for signal %s", strsignal(*signals));
174 int sig_monitor_init()
176 static int sigerr[] = { SIG_FOR_TIMER, SIGSEGV, SIGFPE, SIGILL, SIGBUS, 0 };
177 static int sigterm[] = { SIGINT, SIGABRT, SIGTERM, 0 };
179 return (install(on_signal_error, sigerr) & install(on_signal_terminate, sigterm)) - 1;
182 int sig_monitor_init_timeouts()
184 return timeout_create();
187 void sig_monitor_clean_timeouts()
192 void sig_monitor(int timeout, void (*function)(int sig, void*), void *arg)
194 volatile int signum, signum2;
195 sigjmp_buf jmpbuf, *older;
197 older = error_handler;
198 signum = setjmp(jmpbuf);
200 error_handler = &jmpbuf;
202 timeout_arm(timeout);
205 signum2 = setjmp(jmpbuf);
207 function(signum, arg);
209 error_handler = older;