sig-monitor: dump the stack on SIGABRT
[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         if (signum == SIGABRT)
134                 dumpstack(3);
135         exit(1);
136 }
137
138 /* Handles monitored signals that can be continued */
139 static void on_signal_error(int signum)
140 {
141         sigset_t sigset;
142
143         ERROR("ALERT! signal %d received: %s", signum, strsignal(signum));
144         if (error_handler == NULL && signum == SIG_FOR_TIMER)
145                 return;
146
147         dumpstack(3);
148
149         // unlock signal to allow a new signal to come
150         if (error_handler != NULL) {
151                 sigemptyset(&sigset);
152                 sigaddset(&sigset, signum);
153                 sigprocmask(SIG_UNBLOCK, &sigset, 0);
154                 longjmp(*error_handler, signum);
155         }
156         ERROR("Unmonitored signal %d received: %s", signum, strsignal(signum));
157         exit(2);
158 }
159
160 /* install the handlers */
161 static int install(void (*handler)(int), int *signals)
162 {
163         int result = 1;
164         while(*signals > 0) {
165                 if (signal(*signals, handler) == SIG_ERR) {
166                         ERROR("failed to install signal handler for signal %s", strsignal(*signals));
167                         result = 0;
168                 }
169                 signals++;
170         }
171         return result;
172 }
173
174 int sig_monitor_init()
175 {
176         static int sigerr[] = { SIG_FOR_TIMER, SIGSEGV, SIGFPE, SIGILL, SIGBUS, 0 };
177         static int sigterm[] = { SIGINT, SIGABRT, SIGTERM, 0 };
178
179         return (install(on_signal_error, sigerr) & install(on_signal_terminate, sigterm)) - 1;
180 }
181
182 int sig_monitor_init_timeouts()
183 {
184         return timeout_create();
185 }
186
187 void sig_monitor_clean_timeouts()
188 {
189         timeout_delete();
190 }
191
192 void sig_monitor(int timeout, void (*function)(int sig, void*), void *arg)
193 {
194         volatile int signum, signum2;
195         sigjmp_buf jmpbuf, *older;
196
197         older = error_handler;
198         signum = setjmp(jmpbuf);
199         if (signum == 0) {
200                 error_handler = &jmpbuf;
201                 if (timeout)
202                         timeout_arm(timeout);
203                 function(0, arg);
204         } else {
205                 signum2 = setjmp(jmpbuf);
206                 if (signum2 == 0)
207                         function(signum, arg);
208         }
209         error_handler = older;
210         if (timeout)
211                 timeout_disarm();
212 }
213
214