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));
136 /* Handles monitored signals that can be continued */
137 static void on_signal_error(int signum)
141 ERROR("ALERT! signal %d received: %s", signum, strsignal(signum));
142 if (error_handler == NULL && signum == SIG_FOR_TIMER)
147 // unlock signal to allow a new signal to come
148 if (error_handler != NULL) {
149 sigemptyset(&sigset);
150 sigaddset(&sigset, signum);
151 sigprocmask(SIG_UNBLOCK, &sigset, 0);
152 longjmp(*error_handler, signum);
154 ERROR("Unmonitored signal %d received: %s", signum, strsignal(signum));
158 /* install the handlers */
159 static int install(void (*handler)(int), int *signals)
162 while(*signals > 0) {
163 if (signal(*signals, handler) == SIG_ERR) {
164 ERROR("failed to install signal handler for signal %s", strsignal(*signals));
172 int sig_monitor_init()
174 static int sigerr[] = { SIG_FOR_TIMER, SIGSEGV, SIGFPE, SIGILL, SIGBUS, 0 };
175 static int sigterm[] = { SIGINT, SIGABRT, SIGTERM, 0 };
177 return (install(on_signal_error, sigerr) & install(on_signal_terminate, sigterm)) - 1;
180 int sig_monitor_init_timeouts()
182 return timeout_create();
185 void sig_monitor_clean_timeouts()
190 void sig_monitor(int timeout, void (*function)(int sig, void*), void *arg)
192 volatile int signum, signum2;
193 sigjmp_buf jmpbuf, *older;
195 older = error_handler;
196 signum = setjmp(jmpbuf);
198 error_handler = &jmpbuf;
200 timeout_arm(timeout);
203 signum2 = setjmp(jmpbuf);
205 function(signum, arg);
207 error_handler = older;