debug: allow break after wait
[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
34 #include "verbose.h"
35
36 static char key_env_break[] = "AFB_DEBUG_BREAK";
37 static char key_env_wait[] = "AFB_DEBUG_WAIT";
38 static char separs[] = ", \t\n";
39
40 /*
41  * Checks whether the 'key' is in the 'list'
42  * Return 1 if it is in or 0 otherwise
43  */
44 static int has_key(const char *key, const char *list)
45 {
46         if (list && key) {
47                 list += strspn(list, separs);
48                 while (*list) {
49                         size_t s = strcspn(list, separs);
50                         if (!strncasecmp(list, key, s) && !key[s])
51                                 return 1;
52                         list += s;
53                         list += strspn(list, separs);
54                 }
55         }
56         return 0;
57 }
58
59 static void indicate(const char *key)
60 {
61 #if !defined(NO_AFB_DEBUG_FILE_INDICATION)
62         char filename[200];
63         int fd;
64
65         snprintf(filename, sizeof filename, "/tmp/afb-debug-%ld", (long)getpid());
66         if (key) {
67                 fd = creat(filename, 0644);
68                 write(fd, key, strlen(key));
69                 close(fd);
70         } else {
71                 unlink(filename);
72         }
73 #endif
74 }
75
76 static void handler(int signum)
77 {
78 }
79
80 void afb_debug(const char *key)
81 {
82         struct sigaction sa, psa;
83         sigset_t ss, oss;
84
85         if (has_key(key, secure_getenv(key_env_wait))) {
86                 NOTICE("DEBUG WAIT before %s", key);
87                 sigfillset(&ss);
88                 sigdelset(&ss, SIGINT);
89                 sigprocmask(SIG_SETMASK, &ss, &oss);
90                 sigemptyset(&ss);
91                 sigaddset(&ss, SIGINT);
92                 memset(&sa, 0, sizeof sa);
93                 sa.sa_handler = handler;
94                 sigaction(SIGINT, &sa, &psa);
95                 indicate(key);
96                 sigwaitinfo(&ss, NULL);
97                 sigaction(SIGINT, &psa, NULL);
98                 indicate(NULL);
99                 sigprocmask(SIG_SETMASK, &oss, NULL);
100                 NOTICE("DEBUG WAIT after %s", key);
101         }
102         if (has_key(key, secure_getenv(key_env_break))) {
103                 NOTICE("DEBUG BREAK before %s", key);
104                 memset(&sa, 0, sizeof sa);
105                 sa.sa_handler = handler;
106                 sigaction(SIGINT, &sa, &psa);
107                 raise(SIGINT);
108                 sigaction(SIGINT, &psa, NULL);
109                 NOTICE("DEBUG BREAK after %s", key);
110         }
111 }
112
113 #endif
114