forked from lastfm/balance.fm
/
butils.c
354 lines (308 loc) · 8.59 KB
/
butils.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* Copyright (c) 2000-2004,2005 by Inlab Software GmbH, Gruenwald, Germany.
* All rights reserved.
*
*/
/*
* $Id: butils.c,v 1.1 2010/01/29 10:40:16 t Exp $
*/
#include "balance.h"
unsigned int hash_fold(const void *ptr, int len)
{
const unsigned char *s = ptr;
unsigned int rc = 0;
for (int i = 0; i < len; i++) {
rc = s[i] + 31 * rc;
}
return rc;
}
ssize_t writen(int fd, const unsigned char *ptr, size_t nbytes)
{
int nleft;
ssize_t nwritten;
nleft = nbytes;
while (nleft > 0) {
nwritten = write(fd, ptr, nleft);
if (nwritten <= 0) {
return nwritten; /* error */
}
nleft -= nwritten;
ptr += nwritten;
}
return nbytes - nleft;
}
static void *safe_malloc(size_t size)
{
void *m = malloc(size);
if (m == NULL) {
err_dump("out of memory");
}
return m;
}
static void *safe_realloc(void *ptr, size_t size)
{
void *m = realloc(ptr, size);
if (size > 0 && m == NULL) {
err_dump("out of memory");
}
return m;
}
static char *get_token(const char **str, const char *delim_list)
{
const char *p = *str;
char *token = safe_malloc(strlen(p) + 1);
char *t = token;
while (*p) {
const char *d = delim_list;
while (*p && *p == '\\') {
if (*++p) {
switch (*p) {
case 'a': *t++ = '\a'; break;
case 'b': *t++ = '\b'; break;
case 't': *t++ = '\t'; break;
case 'n': *t++ = '\n'; break;
case 'v': *t++ = '\v'; break;
case 'f': *t++ = '\f'; break;
case 'r': *t++ = '\r'; break;
default: *t++ = *p; break;
}
p++;
}
}
while (*p && *d && *p != *d) {
d++;
}
if (*p == '\0' || *p == *d) {
break;
}
*t++ = *p++;
}
*str = p;
*t++ = '\0';
return safe_realloc(token, t - token);
}
static struct monitor_action *append_action(struct monitor_action ***pppma, enum monitor_action_type type)
{
struct monitor_action *ma = safe_malloc(sizeof(struct monitor_action));
ma->type = type;
ma->next = **pppma;
**pppma = ma;
*pppma = &ma->next;
return ma;
}
struct monitor_spec *monitor_spec_parse(const char *str, const struct monitor_defaults *defaults)
{
struct monitor_spec *ms = safe_malloc(sizeof(struct monitor_spec));
ms->action_list = NULL;
ms->interval = DEFAULT_MON_INTERVAL;
ms->enable = true;
ms->disable = true;
struct monitor_action **ppma = &ms->action_list;
struct monitor_action *cur_ma = NULL;
while (*str) {
char *k = get_token(&str, ":=");
char *v = NULL;
if (*str == '=') {
str++;
v = get_token(&str, ":");
}
if (strcmp(k, "connect") == 0) {
if (v) {
err_dump("unexpected value for 'connect'");
}
cur_ma = append_action(&ppma, MA_CONNECT);
cur_ma->u.connect.timeout = defaults->connect_timeout;
} else if (strcmp(k, "command") == 0) {
if (v == NULL) {
err_dump("'command' action requires a value");
}
cur_ma = append_action(&ppma, MA_COMMAND);
cur_ma->u.command.cmdline = v;
cur_ma->u.command.num_pass = 1;
cur_ma->u.command.pass = safe_malloc(sizeof(int));
cur_ma->u.command.pass[0] = 0;
} else if (strcmp(k, "interval") == 0) {
if (v == NULL) {
err_dump("'interval' option requires a value");
}
char *end;
ms->interval = strtol(v, &end, 0);
if (*end || ms->interval <= 0) {
err_dump("error parsing interval");
}
free(v);
} else if (strcmp(k, "timeout") == 0) {
if (v == NULL) {
err_dump("'timeout' option requires a value");
}
if (cur_ma == NULL) {
err_dump("'timeout' option given without action");
}
if (cur_ma->type != MA_CONNECT) {
err_dump("'timeout' option unsupported for this action");
}
char *end;
cur_ma->u.connect.timeout = strtod(v, &end);
if (*end) {
err_dump("error parsing timeout");
}
if (cur_ma->u.connect.timeout < 1e-6f) {
err_dump("timeout outside of supported range");
}
free(v);
} else if (strcmp(k, "pass") == 0) {
if (v == NULL) {
err_dump("'pass' option requires a value");
}
if (cur_ma == NULL) {
err_dump("'pass' option given without action");
}
if (cur_ma->type != MA_COMMAND) {
err_dump("'pass' option unsupported for this action");
}
cur_ma->u.command.num_pass = 1;
for (const char *c = v; *c; c++) {
if (*c == ',') {
cur_ma->u.command.num_pass++;
}
}
cur_ma->u.command.pass = safe_realloc(cur_ma->u.command.pass,
cur_ma->u.command.num_pass*sizeof(int));
char *end = v;
for (size_t i = 0; i < cur_ma->u.command.num_pass; i++) {
cur_ma->u.command.pass[i] = strtol(end, &end, 0);
if (*end != (i < cur_ma->u.command.num_pass - 1 ? ',' : '\0')) {
err_dump("error parsing pass value list");
}
if (*end) {
end++;
}
}
free(v);
} else if (strcmp(k, "noenable") == 0) {
if (v) {
err_dump("unexpected value for 'noenable'");
}
ms->enable = false;
} else if (strcmp(k, "nodisable") == 0) {
if (v) {
err_dump("unexpected value for 'nodisable'");
}
ms->disable = false;
} else {
err_dump("invalid monitoring spec");
}
free(k);
if (*str) {
str++;
}
}
return ms;
}
void monitor_spec_free(struct monitor_spec *spec)
{
if (spec)
{
struct monitor_action *ma = spec->action_list;
while (ma) {
switch (ma->type) {
case MA_COMMAND:
free(ma->u.command.cmdline);
free(ma->u.command.pass);
break;
default:
break;
}
struct monitor_action *d = ma;
ma = ma->next;
free(d);
}
free(spec);
}
}
void monitor_spec_dump(FILE *fh, const struct monitor_spec *spec)
{
if (spec)
{
fprintf(fh, "monitoring setup:\n");
fprintf(fh, " check interval: %d seconds\n", spec->interval);
fprintf(fh, " channels will %sbe automatically enabled\n", spec->enable ? "" : "not ");
fprintf(fh, " channels will %sbe automatically disabled\n", spec->disable ? "" : "not ");
const struct monitor_action *ma = spec->action_list;
fprintf(fh, " checks:%s\n", ma ? "" : " none");
while (ma) {
switch (ma->type) {
case MA_CONNECT:
fprintf(fh, " - connect to channel host [timeout: %.2fs]\n", ma->u.connect.timeout);
break;
case MA_COMMAND:
fprintf(fh, " - run command: %s [pass exit code%s: ",
ma->u.command.cmdline, ma->u.command.num_pass > 1 ? "s" : "");
for (size_t i = 0; i < ma->u.command.num_pass; i++) {
fprintf(fh, "%s%d", i == 0 ? "" : ", ", ma->u.command.pass[i]);
}
fprintf(fh, "]\n");
break;
default:
break;
}
ma = ma->next;
}
}
}
static int command_format(char *buffer, int buflen, const char *format, const char *host, int port)
{
int len = 0;
char portbuf[32];
sprintf(portbuf, "%d", port);
#define APPEND_C(c) \
do { \
if (buffer && len < buflen) { \
buffer[len] = (c); \
} \
len++; \
} while (0)
#define APPEND_S(s) \
do { \
const char *scp = (s); \
int sl = strlen(scp); \
if (buffer && len < buflen) { \
int cp = buflen - len; \
if (sl < cp) cp = sl; \
strncpy(buffer + len, (scp), cp); \
} \
len += sl; \
} while (0)
for (const char *f = format; *f; f++) {
if (*f == '%' && f[1]) {
switch (*++f) {
case 'H':
APPEND_S(host);
break;
case 'P':
APPEND_S(portbuf);
break;
case '%':
APPEND_C('%');
break;
default:
APPEND_C('%');
APPEND_C(*f);
break;
}
} else {
APPEND_C(*f);
}
}
APPEND_C('\0');
#undef APPEND_C
#undef APPEND_S
return len;
}
char *monitor_command_format(const char *format, const char *host, int port)
{
int len = command_format(NULL, 0, format, host, port);
char *buf = safe_malloc(len);
command_format(buf, len, format, host, port);
return buf;
}