Add backtrace on errors
[src/app-framework-binder.git] / src / sig-monitor.c
1 /*
2  * Copyright (C) 2017 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <stdlib.h>
21 #include <signal.h>
22 #include <string.h>
23 #include <setjmp.h>
24 #include <time.h>
25 #include <unistd.h>
26 #include <sys/syscall.h>
27 #include <execinfo.h>
28
29 #include "sig-monitor.h"
30 #include "verbose.h"
31
32 #define SIG_FOR_TIMER   SIGVTALRM
33
34 /* local handler */
35 static _Thread_local sigjmp_buf *error_handler;
36
37 /* local timers */
38 static _Thread_local int thread_timer_set;
39 static _Thread_local timer_t thread_timerid;
40
41 /*
42  * Dumps the current stack
43  */
44 static void dumpstack(int crop)
45 {
46         int idx, count;
47         void *addresses[1000];
48         char **locations;
49
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);
54         else {
55                 for (idx = crop; idx < count; idx++)
56                         ERROR("[BACKTRACE %d/%d] %s", idx - crop + 1, count - crop, locations[idx]);
57                 free(locations);
58         }
59 }
60
61 /*
62  * Creates a timer for the current thread
63  *
64  * Returns 0 in case of success
65  */
66 static inline int timeout_create()
67 {
68         int rc;
69         struct sigevent sevp;
70
71         if (thread_timer_set)
72                 rc = 0;
73         else {
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);
79 #else
80                 sevp._sigev_un._tid = (pid_t)syscall(SYS_gettid);
81 #endif
82                 rc = timer_create(CLOCK_THREAD_CPUTIME_ID, &sevp, &thread_timerid);
83                 thread_timer_set = !rc;
84         }
85         return 0;
86 }
87
88 /*
89  * Arms the alarm in timeout seconds for the current thread
90  */
91 static inline int timeout_arm(int timeout)
92 {
93         int rc;
94         struct itimerspec its;
95
96         rc = timeout_create();
97         if (rc == 0) {
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);
103         }
104
105         return rc;
106 }
107
108 /*
109  * Disarms the current alarm
110  */
111 static inline void timeout_disarm()
112 {
113         if (thread_timer_set)
114                 timeout_arm(0);
115 }
116
117 /*
118  * Destroy any alarm resource for the current thread
119  */
120 static inline void timeout_delete()
121 {
122         if (thread_timer_set) {
123                 timer_delete(thread_timerid);
124                 thread_timer_set = 0;
125         }
126 }
127
128
129 /* Handles signals that terminate the process */
130 static void on_signal_terminate (int signum)
131 {
132         ERROR("Terminating signal %d received: %s", signum, strsignal(signum));
133         exit(1);
134 }
135
136 /* Handles monitored signals that can be continued */
137 static void on_signal_error(int signum)
138 {
139         sigset_t sigset;
140
141         ERROR("ALERT! signal %d received: %s", signum, strsignal(signum));
142         if (error_handler == NULL && signum == SIG_FOR_TIMER)
143                 return;
144
145         dumpstack(3);
146
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);
153         }
154         ERROR("Unmonitored signal %d received: %s", signum, strsignal(signum));
155         exit(2);
156 }
157
158 /* install the handlers */
159 static int install(void (*handler)(int), int *signals)
160 {
161         int result = 1;
162         while(*signals > 0) {
163                 if (signal(*signals, handler) == SIG_ERR) {
164                         ERROR("failed to install signal handler for signal %s", strsignal(*signals));
165                         result = 0;
166                 }
167                 signals++;
168         }
169         return result;
170 }
171
172 int sig_monitor_init()
173 {
174         static int sigerr[] = { SIG_FOR_TIMER, SIGSEGV, SIGFPE, 0 };
175         static int sigterm[] = { SIGINT, SIGABRT, 0 };
176
177         return (install(on_signal_error, sigerr) & install(on_signal_terminate, sigterm)) - 1;
178 }
179
180 int sig_monitor_init_timeouts()
181 {
182         return timeout_create();
183 }
184
185 void sig_monitor_clean_timeouts()
186 {
187         timeout_delete();
188 }
189
190 void sig_monitor(int timeout, void (*function)(int sig, void*), void *arg)
191 {
192         volatile int signum, signum2;
193         sigjmp_buf jmpbuf, *older;
194
195         older = error_handler;
196         signum = setjmp(jmpbuf);
197         if (signum == 0) {
198                 error_handler = &jmpbuf;
199                 if (timeout)
200                         timeout_arm(timeout);
201                 function(0, arg);
202         } else {
203                 signum2 = setjmp(jmpbuf);
204                 if (signum2 == 0)
205                         function(signum, arg);
206         }
207         error_handler = older;
208         if (timeout)
209                 timeout_disarm();
210 }
211
212