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>
28 #include "sig-monitor.h"
31 #define SIG_FOR_TIMER SIGVTALRM
34 static _Thread_local sigjmp_buf *error_handler;
37 static _Thread_local int thread_timer_set;
38 static _Thread_local timer_t thread_timerid;
41 * Creates a timer for the current thread
43 * Returns 0 in case of success
45 static inline int timeout_create()
53 sevp.sigev_notify = SIGEV_THREAD_ID;
54 sevp.sigev_signo = SIG_FOR_TIMER;
55 sevp.sigev_value.sival_ptr = NULL;
56 #if defined(sigev_notify_thread_id)
57 sevp.sigev_notify_thread_id = (pid_t)syscall(SYS_gettid);
59 sevp._sigev_un._tid = (pid_t)syscall(SYS_gettid);
61 rc = timer_create(CLOCK_THREAD_CPUTIME_ID, &sevp, &thread_timerid);
62 thread_timer_set = !rc;
68 * Arms the alarm in timeout seconds for the current thread
70 static inline int timeout_arm(int timeout)
73 struct itimerspec its;
75 rc = timeout_create();
77 its.it_interval.tv_sec = 0;
78 its.it_interval.tv_nsec = 0;
79 its.it_value.tv_sec = timeout;
80 its.it_value.tv_nsec = 0;
81 rc = timer_settime(thread_timerid, 0, &its, NULL);
88 * Disarms the current alarm
90 static inline void timeout_disarm()
97 * Destroy any alarm resource for the current thread
99 static inline void timeout_delete()
101 if (thread_timer_set) {
102 timer_delete(thread_timerid);
103 thread_timer_set = 0;
108 /* Handles signals that terminate the process */
109 static void on_signal_terminate (int signum)
111 ERROR("Terminating signal %d received: %s", signum, strsignal(signum));
115 /* Handles monitored signals that can be continued */
116 static void on_signal_error(int signum)
120 ERROR("ALERT! signal %d received: %s", signum, strsignal(signum));
122 // unlock signal to allow a new signal to come
123 if (error_handler != NULL) {
124 sigemptyset(&sigset);
125 sigaddset(&sigset, signum);
126 sigprocmask(SIG_UNBLOCK, &sigset, 0);
127 longjmp(*error_handler, signum);
129 if (signum == SIG_FOR_TIMER)
131 ERROR("Unmonitored signal %d received: %s", signum, strsignal(signum));
135 /* install the handlers */
136 static int install(void (*handler)(int), int *signals)
139 while(*signals > 0) {
140 if (signal(*signals, handler) == SIG_ERR) {
141 ERROR("failed to install signal handler for signal %s", strsignal(*signals));
149 int sig_monitor_init()
151 static int sigerr[] = { SIG_FOR_TIMER, SIGSEGV, SIGFPE, 0 };
152 static int sigterm[] = { SIGINT, SIGABRT, 0 };
154 return (install(on_signal_error, sigerr) & install(on_signal_terminate, sigterm)) - 1;
157 int sig_monitor_init_timeouts()
159 return timeout_create();
162 void sig_monitor_clean_timeouts()
167 void sig_monitor(int timeout, void (*function)(int sig, void*), void *arg)
169 volatile int signum, signum2;
170 sigjmp_buf jmpbuf, *older;
172 older = error_handler;
173 signum = setjmp(jmpbuf);
175 error_handler = &jmpbuf;
177 timeout_arm(timeout);
180 signum2 = setjmp(jmpbuf);
182 function(signum, arg);
184 error_handler = older;