debugging: add calls to 'personality'
[src/app-framework-binder.git] / src / afb-debug.c
1 /*
2  Copyright 2017 IoT.bzh
3
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 #include "afb-debug.h"
20
21 #if defined(AFB_INSERT_DEBUG_FEATURES)
22
23 #define _GNU_SOURCE
24 #include <stdlib.h>
25 #include <string.h>
26 #include <signal.h>
27
28 #include <stdio.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #if !defined(NO_CALL_PERSONALITY)
34 #include <sys/personality.h>
35 #endif
36
37 #include "verbose.h"
38
39 static char key_env_break[] = "AFB_DEBUG_BREAK";
40 static char key_env_wait[] = "AFB_DEBUG_WAIT";
41 static char separs[] = ", \t\n";
42
43 /*
44  * Checks whether the 'key' is in the 'list'
45  * Return 1 if it is in or 0 otherwise
46  */
47 static int has_key(const char *key, const char *list)
48 {
49         if (list && key) {
50                 list += strspn(list, separs);
51                 while (*list) {
52                         size_t s = strcspn(list, separs);
53                         if (!strncasecmp(list, key, s) && !key[s])
54                                 return 1;
55                         list += s;
56                         list += strspn(list, separs);
57                 }
58         }
59         return 0;
60 }
61
62 static void indicate(const char *key)
63 {
64 #if !defined(NO_AFB_DEBUG_FILE_INDICATION)
65         char filename[200];
66         int fd;
67
68         snprintf(filename, sizeof filename, "/tmp/afb-debug-%ld", (long)getpid());
69         if (key) {
70                 fd = creat(filename, 0644);
71                 write(fd, key, strlen(key));
72                 close(fd);
73         } else {
74                 unlink(filename);
75         }
76 #endif
77 }
78
79 static void handler(int signum)
80 {
81 }
82
83 void afb_debug(const char *key)
84 {
85         struct sigaction sa, psa;
86         sigset_t ss, oss;
87
88         if (has_key(key, secure_getenv(key_env_wait))) {
89                 NOTICE("DEBUG WAIT before %s", key);
90                 sigfillset(&ss);
91                 sigdelset(&ss, SIGINT);
92                 sigprocmask(SIG_SETMASK, &ss, &oss);
93                 sigemptyset(&ss);
94                 sigaddset(&ss, SIGINT);
95                 memset(&sa, 0, sizeof sa);
96                 sa.sa_handler = handler;
97                 sigaction(SIGINT, &sa, &psa);
98                 indicate(key);
99                 sigwaitinfo(&ss, NULL);
100                 sigaction(SIGINT, &psa, NULL);
101                 indicate(NULL);
102                 sigprocmask(SIG_SETMASK, &oss, NULL);
103                 NOTICE("DEBUG WAIT after %s", key);
104 #if !defined(NO_CALL_PERSONALITY)
105                 personality((unsigned long)-1L);
106 #endif
107         }
108         if (has_key(key, secure_getenv(key_env_break))) {
109                 NOTICE("DEBUG BREAK before %s", key);
110                 memset(&sa, 0, sizeof sa);
111                 sa.sa_handler = handler;
112                 sigaction(SIGINT, &sa, &psa);
113                 raise(SIGINT);
114                 sigaction(SIGINT, &psa, NULL);
115                 NOTICE("DEBUG BREAK after %s", key);
116         }
117 }
118
119 #endif
120