5fc5bb5e23c9967d54909fc81108efe53705866a
[src/app-framework-binder.git] / src / afb-sig-handler.c
1 /* 
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  * Author José Bollo <jose.bollo@iot.bzh>
5  *
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
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 #define _GNU_SOURCE
20
21 #include <stdlib.h>
22 #include <signal.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <time.h>
26 #include <sys/syscall.h>
27 #include <setjmp.h>
28
29 #include "afb-sig-handler.h"
30 #include "verbose.h"
31
32 static _Thread_local sigjmp_buf *error_handler;
33
34 static void on_signal_terminate (int signum)
35 {
36         ERROR("Terminating signal received %s", strsignal(signum));
37         exit(1);
38 }
39
40 static void on_signal_error(int signum)
41 {
42         sigset_t sigset;
43
44         // unlock signal to allow a new signal to come
45         if (error_handler != NULL) {
46                 sigemptyset(&sigset);
47                 sigaddset(&sigset, signum);
48                 sigprocmask(SIG_UNBLOCK, &sigset, 0);
49                 longjmp(*error_handler, signum);
50         }
51         if (signum == SIGALRM)
52                 return;
53         ERROR("Unmonitored signal received %s", strsignal(signum));
54         exit(2);
55 }
56
57 static int install(void (*handler)(int), int *signals)
58 {
59         int result = 1;
60         while(*signals > 0) {
61                 if (signal(*signals, handler) == SIG_ERR) {
62                         ERROR("failed to install signal handler for signal %s", strsignal(*signals));
63                         result = 0;
64                 }
65                 signals++;
66         }
67         return result;
68 }
69
70 int afb_sig_handler_init()
71 {
72         static int sigerr[] = { SIGALRM, SIGSEGV, SIGFPE, 0 };
73         static int sigterm[] = { SIGINT, SIGABRT, 0 };
74
75         return (install(on_signal_error, sigerr) & install(on_signal_terminate, sigterm)) - 1;
76 }
77
78 void afb_sig_monitor(void (*function)(int sig, void*), void *closure, int timeout)
79 {
80         volatile int signum, timerset;
81         timer_t timerid;
82         sigjmp_buf jmpbuf, *older;
83         struct sigevent sevp;
84         struct itimerspec its;
85
86         timerset = 0;
87         older = error_handler;
88         signum = setjmp(jmpbuf);
89         if (signum != 0) {
90                 function(signum, closure);
91         }
92         else {
93                 error_handler = &jmpbuf;
94                 if (timeout > 0) {
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);
101 #else
102                         sevp._sigev_un._tid = (pid_t)syscall(SYS_gettid);
103 #endif
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);
110                 }
111
112                 function(0, closure);
113         }
114         if (timerset)
115                 timer_delete(timerid);
116         error_handler = older;
117 }
118