4 author: José Bollo <jose.bollo@iot.bzh>
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
10 http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "afb-debug.h"
21 #if defined(AFB_INSERT_DEBUG_FEATURES)
29 #include <sys/types.h>
36 static char key_env_break[] = "AFB_DEBUG_BREAK";
37 static char key_env_wait[] = "AFB_DEBUG_WAIT";
38 static char separs[] = ", \t\n";
41 * Checks whether the 'key' is in the 'list'
42 * Return 1 if it is in or 0 otherwise
44 static int has_key(const char *key, const char *list)
47 list += strspn(list, separs);
49 size_t s = strcspn(list, separs);
50 if (!strncasecmp(list, key, s) && !key[s])
53 list += strspn(list, separs);
59 static void indicate(const char *key)
61 #if !defined(NO_AFB_DEBUG_FILE_INDICATION)
65 snprintf(filename, sizeof filename, "/tmp/afb-debug-%ld", (long)getpid());
67 fd = creat(filename, 0644);
68 write(fd, key, strlen(key));
76 static void handler(int signum)
80 void afb_debug(const char *key)
82 struct sigaction sa, psa;
85 if (has_key(key, secure_getenv(key_env_wait))) {
86 NOTICE("DEBUG WAIT before %s", key);
88 sigdelset(&ss, SIGINT);
89 sigprocmask(SIG_SETMASK, &ss, &oss);
91 sigaddset(&ss, SIGINT);
92 memset(&sa, 0, sizeof sa);
93 sa.sa_handler = handler;
94 sigaction(SIGINT, &sa, &psa);
96 sigwaitinfo(&ss, NULL);
97 sigaction(SIGINT, &psa, NULL);
99 sigprocmask(SIG_SETMASK, &oss, NULL);
100 NOTICE("DEBUG WAIT after %s", key);
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);
108 sigaction(SIGINT, &psa, NULL);
109 NOTICE("DEBUG BREAK after %s", key);