2 * Copyright (C) 2015, 2016 "IoT.bzh"
3 * Author "Fulup Ar Foll"
4 * Author José Bollo <jose.bollo@iot.bzh>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
26 #include <sys/syscall.h>
29 #include "afb-sig-handler.h"
32 static _Thread_local sigjmp_buf *error_handler;
34 static void on_signal_terminate (int signum)
36 ERROR("Terminating signal received %s", strsignal(signum));
40 static void on_signal_error(int signum)
44 // unlock signal to allow a new signal to come
45 if (error_handler != NULL) {
47 sigaddset(&sigset, signum);
48 sigprocmask(SIG_UNBLOCK, &sigset, 0);
49 longjmp(*error_handler, signum);
51 if (signum == SIGALRM)
53 ERROR("Unmonitored signal received %s", strsignal(signum));
57 static int install(void (*handler)(int), int *signals)
61 if (signal(*signals, handler) == SIG_ERR) {
62 ERROR("failed to install signal handler for signal %s", strsignal(*signals));
70 int afb_sig_handler_init()
72 static int sigerr[] = { SIGALRM, SIGSEGV, SIGFPE, 0 };
73 static int sigterm[] = { SIGINT, SIGABRT, 0 };
75 return (install(on_signal_error, sigerr) & install(on_signal_terminate, sigterm)) - 1;
78 void afb_sig_monitor(void (*function)(int sig, void*), void *closure, int timeout)
80 volatile int signum, timerset;
82 sigjmp_buf jmpbuf, *older;
84 struct itimerspec its;
87 older = error_handler;
88 signum = setjmp(jmpbuf);
90 function(signum, closure);
93 error_handler = &jmpbuf;
95 timerset = 1; /* TODO: check statuses */
96 sevp.sigev_notify = SIGEV_THREAD_ID;
97 sevp.sigev_signo = SIGALRM;
98 sevp.sigev_value.sival_ptr = NULL;
99 #if defined(sigev_notify_thread_id)
100 sevp.sigev_notify_thread_id = (pid_t)syscall(SYS_gettid);
102 sevp._sigev_un._tid = (pid_t)syscall(SYS_gettid);
104 timer_create(CLOCK_THREAD_CPUTIME_ID, &sevp, &timerid);
105 its.it_interval.tv_sec = 0;
106 its.it_interval.tv_nsec = 0;
107 its.it_value.tv_sec = timeout;
108 its.it_value.tv_nsec = 0;
109 timer_settime(timerid, 0, &its, NULL);
112 function(0, closure);
115 timer_delete(timerid);
116 error_handler = older;