-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfault.c
182 lines (144 loc) · 2.91 KB
/
fault.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "fault.h"
#include "config.h"
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libks/list.h"
#include "log.h"
#ifdef DIAGNOSTIC
LIST(fault_list, fault);
struct fault {
char fu_name[32];
int fu_errno;
unsigned int fu_hits;
LIST_ENTRY(fault_list, fault);
};
static void fault_init(void);
static int fault_match(const char *);
static const char *parse_attr(struct fault *, const char *, const char *);
static struct fault_list faults;
static int cold = 1;
int
fault(const char *name)
{
fault_init();
if (!fault_match(name))
return 0;
warnx("fault: %s", name);
return 1;
}
void
fault_shutdown(void)
{
struct fault *fu;
int error = 0;
fault_init();
while ((fu = LIST_FIRST(&faults)) != NULL) {
LIST_REMOVE(&faults, fu);
if (fu->fu_hits == 0) {
warnx("%s: %s: fault never hit", __func__, fu->fu_name);
error = 1;
}
free(fu);
}
if (error)
exit(66);
}
static void
fault_init(void)
{
const char *str;
if (!cold)
return;
LIST_INIT(&faults);
str = getenv("FAULT");
if (str == NULL || str[0] == '\0')
return;
log_debug("%s: faults=\"%s\"\n", __func__, str);
for (;;) {
struct fault *fu;
const char *end, *p;
p = end = strchr(str, ':');
if (end == NULL)
end = str + strlen(str);
fu = calloc(1, sizeof(*fu));
if (fu == NULL)
err(1, NULL);
for (;;) {
str = parse_attr(fu, str, end);
if (str == NULL)
break;
}
LIST_INSERT_TAIL(&faults, fu);
if (p == NULL)
break;
str = end + 1;
}
cold = 0;
}
static int
fault_match(const char *name)
{
struct fault *fu;
LIST_FOREACH(fu, &faults) {
if (strcmp(fu->fu_name, name) != 0)
continue;
if (fu->fu_hits > 0)
continue;
fu->fu_hits++;
errno = fu->fu_errno;
return 1;
}
return 0;
}
static const char *
parse_attr(struct fault *fu, const char *str, const char *end)
{
const char *key, *p, *val;
size_t keysiz, valsiz;
p = strchr(str, '=');
if (p == NULL)
errx(1, "%s: %s: invalid fault", __func__, str);
key = str;
keysiz = (size_t)(p - str);
str = p + 1;
p = strchr(str, ',');
if (p == NULL)
p = end;
val = str;
valsiz = (size_t)(p - str);
str = p + 1;
if (strncmp(key, "errno", keysiz) == 0) {
#define ERRNO(e) do { \
if (strncmp(val, #e, valsiz) == 0) { \
fu->fu_errno = e; \
goto out; \
} \
} while (0)
ERRNO(EINVAL);
ERRNO(ENAMETOOLONG);
ERRNO(ENOENT);
ERRNO(ENOSPC);
ERRNO(EXDEV);
#undef ERRNO
errx(1, "%s: %.*s: unknown errno value",
__func__, (int)valsiz, val);
} else if (strncmp(key, "name", keysiz) == 0) {
size_t siz;
int n;
siz = sizeof(fu->fu_name);
n = snprintf(fu->fu_name, siz, "%.*s", (int)valsiz, val);
if (n < 0 || (size_t)n >= siz)
errc(1, ENAMETOOLONG, "%s", __func__);
} else {
errx(1, "%s: %.*s: unknown fault attribute",
__func__, (int)keysiz, key);
}
out:
if (p == end)
return NULL;
return str;
}
#endif /* DIAGNOSTIC */