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_break))) {
86 NOTICE("DEBUG BREAK before %s", key);
87 memset(&sa, 0, sizeof sa);
88 sa.sa_handler = handler;
89 sigaction(SIGINT, &sa, &psa);
91 sigaction(SIGINT, &psa, NULL);
92 NOTICE("DEBUG BREAK after %s", key);
93 } else if (has_key(key, secure_getenv(key_env_wait))) {
94 NOTICE("DEBUG WAIT before %s", key);
96 sigdelset(&ss, SIGINT);
97 sigprocmask(SIG_SETMASK, &ss, &oss);
99 sigaddset(&ss, SIGINT);
100 memset(&sa, 0, sizeof sa);
101 sa.sa_handler = handler;
102 sigaction(SIGINT, &sa, &psa);
104 sigwaitinfo(&ss, NULL);
105 sigaction(SIGINT, &psa, NULL);
107 sigprocmask(SIG_SETMASK, &oss, NULL);
108 NOTICE("DEBUG WAIT after %s", key);