Adds 2017 to copyrights
[src/app-framework-binder.git] / src / afb-sig-handler.c
1 /*
2  * Copyright (C) 2015, 2016, 2017 "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 <setjmp.h>
25
26 #include <afb/afb-req-itf.h>
27
28 #include "afb-sig-handler.h"
29 #include "afb-thread.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 int afb_sig_req(struct afb_req req, void (*callback)(struct afb_req req))
79 {
80         volatile int signum;
81         sigjmp_buf jmpbuf, *older;
82
83         older = error_handler;
84         signum = setjmp(jmpbuf);
85         if (signum != 0)
86                 afb_req_fail_f(req, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
87         else {
88                 error_handler = &jmpbuf;
89                 callback(req);
90         }
91         error_handler = older;
92         return signum;
93 }
94
95 int afb_sig_req_timeout(struct afb_req req, void (*callback)(struct afb_req req), int timeout)
96 {
97         int rc;
98
99         if (timeout)
100                 afb_thread_timer_arm(timeout);
101         rc = afb_sig_req(req, callback);
102         afb_thread_timer_disarm();
103         return rc;
104 }
105
106 void afb_sig_monitor(void (*function)(int sig, void*), void *closure, int timeout)
107 {
108         volatile int signum;
109         sigjmp_buf jmpbuf, *older;
110
111         older = error_handler;
112         signum = setjmp(jmpbuf);
113         if (signum != 0) {
114                 function(signum, closure);
115         }
116         else {
117                 error_handler = &jmpbuf;
118                 if (timeout)
119                         afb_thread_timer_arm(timeout);
120                 function(0, closure);
121         }
122         afb_thread_timer_disarm();
123         error_handler = older;
124 }
125