Fedora 30 packaging fix issu
[src/app-framework-binder.git] / src / sig-monitor.c
1 /*
2  * Copyright (C) 2017, 2018 "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 <stdio.h>
22 #include <signal.h>
23 #include <string.h>
24 #include <setjmp.h>
25 #include <time.h>
26 #include <unistd.h>
27 #include <sys/syscall.h>
28 #include <execinfo.h>
29
30 #include "sig-monitor.h"
31 #include "verbose.h"
32
33 #define SIG_FOR_TIMER   SIGVTALRM
34
35 /* local handler */
36 static _Thread_local sigjmp_buf *error_handler;
37 static _Thread_local int in_safe_dumpstack;
38
39 /* local timers */
40 static _Thread_local int thread_timer_set;
41 static _Thread_local timer_t thread_timerid;
42
43 /* internal signal lists */
44 static int sigerr[] = { SIG_FOR_TIMER, SIGSEGV, SIGFPE, SIGILL, SIGBUS, 0 };
45 static int sigterm[] = { SIGINT, SIGABRT, SIGTERM, 0 };
46 static int exiting = 0;
47 static int enabled = 0;
48
49 /*
50  * Dumps the current stack
51  */
52 static void dumpstack(int crop, int signum)
53 {
54         int idx, count, rc;
55         void *addresses[100];
56         char **locations;
57         char buffer[8000];
58         size_t pos, length;
59
60         count = backtrace(addresses, sizeof addresses / sizeof *addresses);
61         if (count <= crop)
62                 crop = 0;
63         count -= crop;
64         locations = backtrace_symbols(&addresses[crop], count);
65         if (locations == NULL)
66                 ERROR("can't get the backtrace (returned %d addresses)", count);
67         else {
68                 length = sizeof buffer - 1;
69                 pos = 0;
70                 idx = 0;
71                 while (pos < length && idx < count) {
72                         rc = snprintf(&buffer[pos], length - pos, " [%d/%d] %s\n", idx + 1, count, locations[idx]);
73                         pos += rc >= 0 ? rc : 0;
74                         idx++;
75                 }
76                 buffer[length] = 0;
77                 if (signum)
78                         ERROR("BACKTRACE due to signal %s/%d:\n%s", strsignal(signum), signum, buffer);
79                 else
80                         ERROR("BACKTRACE:\n%s", buffer);
81                 free(locations);
82         }
83 }
84
85 static void safe_dumpstack_cb(int signum, void *closure)
86 {
87         int *args = closure;
88         if (signum)
89                 ERROR("Can't provide backtrace: raised signal %s", strsignal(signum));
90         else
91                 dumpstack(args[0], args[1]);
92 }
93
94 static void safe_dumpstack(int crop, int signum)
95 {
96         int args[2] = { crop + 3, signum };
97
98         in_safe_dumpstack = 1;
99         sig_monitor(0, safe_dumpstack_cb, args);
100         in_safe_dumpstack = 0;
101 }
102
103 /*
104  * Creates a timer for the current thread
105  *
106  * Returns 0 in case of success
107  */
108 static inline int timeout_create()
109 {
110         int rc;
111         struct sigevent sevp;
112
113         if (thread_timer_set)
114                 rc = 0;
115         else {
116                 sevp.sigev_notify = SIGEV_THREAD_ID;
117                 sevp.sigev_signo = SIG_FOR_TIMER;
118                 sevp.sigev_value.sival_ptr = NULL;
119 #if defined(sigev_notify_thread_id)
120                 sevp.sigev_notify_thread_id = (pid_t)syscall(SYS_gettid);
121 #else
122                 sevp._sigev_un._tid = (pid_t)syscall(SYS_gettid);
123 #endif
124                 rc = timer_create(CLOCK_THREAD_CPUTIME_ID, &sevp, &thread_timerid);
125                 thread_timer_set = !rc;
126         }
127         return 0;
128 }
129
130 /*
131  * Arms the alarm in timeout seconds for the current thread
132  */
133 static inline int timeout_arm(int timeout)
134 {
135         int rc;
136         struct itimerspec its;
137
138         rc = timeout_create();
139         if (rc == 0) {
140                 its.it_interval.tv_sec = 0;
141                 its.it_interval.tv_nsec = 0;
142                 its.it_value.tv_sec = timeout;
143                 its.it_value.tv_nsec = 0;
144                 rc = timer_settime(thread_timerid, 0, &its, NULL);
145         }
146
147         return rc;
148 }
149
150 /*
151  * Disarms the current alarm
152  */
153 static inline void timeout_disarm()
154 {
155         if (thread_timer_set)
156                 timeout_arm(0);
157 }
158
159 /*
160  * Destroy any alarm resource for the current thread
161  */
162 static inline void timeout_delete()
163 {
164         if (thread_timer_set) {
165                 timer_delete(thread_timerid);
166                 thread_timer_set = 0;
167         }
168 }
169
170 /* install the handlers */
171 static int install(void (*handler)(int), int *signals)
172 {
173         int result = 1;
174         struct sigaction sa;
175
176         sa.sa_handler = handler;
177         sigemptyset(&sa.sa_mask);
178         sa.sa_flags = SA_NODEFER;
179         while(*signals > 0) {
180                 if (sigaction(*signals, &sa, NULL) < 0) {
181                         ERROR("failed to install signal handler for signal %s: %m", strsignal(*signals));
182                         result = 0;
183                 }
184                 signals++;
185         }
186         return result;
187 }
188
189 /*
190  * rescue exit
191  */
192 static void on_rescue_exit(int signum)
193 {
194         ERROR("Rescue exit for signal %d: %s", signum, strsignal(signum));
195         _exit(exiting);
196 }
197
198 /*
199  * Do a safe exit
200  */
201 static void safe_exit(int code)
202 {
203         install(on_rescue_exit, sigerr);
204         install(on_rescue_exit, sigterm);
205         exiting = code;
206         exit(code);
207 }
208
209 /* Handles signals that terminate the process */
210 static void on_signal_terminate (int signum)
211 {
212         if (!in_safe_dumpstack) {
213                 ERROR("Terminating signal %d received: %s", signum, strsignal(signum));
214                 if (signum == SIGABRT)
215                         safe_dumpstack(3, signum);
216         }
217         safe_exit(1);
218 }
219
220 /* Handles monitored signals that can be continued */
221 static void on_signal_error(int signum)
222 {
223         if (in_safe_dumpstack)
224                 longjmp(*error_handler, signum);
225
226         ERROR("ALERT! signal %d received: %s", signum, strsignal(signum));
227         if (error_handler == NULL && signum == SIG_FOR_TIMER)
228                 return;
229
230         safe_dumpstack(3, signum);
231
232         // unlock signal to allow a new signal to come
233         if (error_handler != NULL)
234                 longjmp(*error_handler, signum);
235
236         ERROR("Unmonitored signal %d received: %s", signum, strsignal(signum));
237         safe_exit(2);
238 }
239
240 void sig_monitor_disable()
241 {
242         enabled = 0;
243         install(SIG_DFL, sigerr);
244         install(SIG_DFL, sigterm);
245 }
246
247 int sig_monitor_enable()
248 {
249         enabled = install(on_signal_error, sigerr) && install(on_signal_terminate, sigterm);
250         if (enabled)
251                 return 0;
252         sig_monitor_disable();
253         return -1;
254 }
255
256 int sig_monitor_init(int enable)
257 {
258         return enable ? sig_monitor_enable() : (sig_monitor_disable(), 0);
259 }
260
261 int sig_monitor_init_timeouts()
262 {
263         return timeout_create();
264 }
265
266 void sig_monitor_clean_timeouts()
267 {
268         timeout_delete();
269 }
270
271 static void monitor(int timeout, void (*function)(int sig, void*), void *arg)
272 {
273         volatile int signum, signum2;
274         sigjmp_buf jmpbuf, *older;
275
276         older = error_handler;
277         signum = setjmp(jmpbuf);
278         if (signum == 0) {
279                 error_handler = &jmpbuf;
280                 if (timeout) {
281                         timeout_create();
282                         timeout_arm(timeout);
283                 }
284                 function(0, arg);
285         } else {
286                 signum2 = setjmp(jmpbuf);
287                 if (signum2 == 0)
288                         function(signum, arg);
289         }
290         if (timeout)
291                 timeout_disarm();
292         error_handler = older;
293 }
294
295 void sig_monitor(int timeout, void (*function)(int sig, void*), void *arg)
296 {
297         if (enabled)
298                 monitor(timeout, function, arg);
299         else
300                 function(0, arg);
301 }
302
303 void sig_monitor_dumpstack()
304 {
305         return dumpstack(1, 0);
306 }