-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.c
357 lines (299 loc) · 8.21 KB
/
config.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
355
356
#define _ISOC99_SOURCE /* for strtoull() */
#define _GNU_SOURCE /* for strdupa() */
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <librdkafka/rdkafka.h>
#include "kafkatee.h"
#include "input.h"
#include "output.h"
#include "ezd.h"
/**
* kafkatee global configuration
*/
struct conf conf;
/* Left trim string '*s' of white spaces and return the new position.
* Does not modify the string. */
static char *ltrim (const char *s) {
while (isspace(*s))
s++;
return (char *)s;
}
/**
* Parses the value as true or false.
*/
static int conf_tof (const char *val) {
char *end;
int i;
i = strtoul(val, &end, 0);
if (end > val) /* treat as integer value */
return !!i;
if (!strcasecmp(val, "yes") ||
!strcasecmp(val, "true") ||
!strcasecmp(val, "on") ||
!*val /* empty value is true */)
return 1;
else
return 0;
}
static encoding_t encoding_parse (const char *val) {
if (!strcasecmp(val, "string"))
return ENC_STRING;
else if (!strcasecmp(val, "json"))
return ENC_JSON;
else
return ENC_ERROR;
}
static char *str_unescape (char *str) {
char *s = str;
while (*s) {
int esc = -1;
if (!strncmp(s, "\\n", 2))
esc = '\n';
else if (!strncmp(s, "\\r", 2))
esc = '\r';
else if (!strncmp(s, "\\t", 2))
esc = '\t';
else if (!strncmp(s, "\\0", 2))
esc = '\0';
if (esc != -1) {
*s = esc;
memmove(s+1, s+2, strlen(s+2)+1);
}
s++;
}
return str;
}
/**
* Parses a "k=v,k2=v2,.." string in 'str' (which is modified)
* and returns 1 on match, else 0.
* Key name and value are returned in 'namep' and 'valp'.
* If no value if specified (and no '='), then value is set to "".
* '*strp' is forwarded to the next token on return for a sub-sequent
* call to kv_parse() (if return is 1).
*/
static int kv_parse (char **strp, const char **namep, const char **valp) {
char *s;
char *str = *strp;
if (!str)
return 0;
while (isspace((int)*str))
str++;
if (!*str)
return 0;
if (!(s = strchr(str, '='))) {
*namep = str;
*valp = " ";
*strp = NULL; /* EOF */
return 1;
}
*s = '\0';
*namep = str;
*valp = s+1;
if ((s = strchr(*valp, ','))) {
*s = '\0';
*strp = s+1;
} else
*strp = NULL;
return 1;
}
/**
* Parse input key-values.
*/
static int input_kvs_parse (char *str, encoding_t *encp, int *flagsp,
char *errstr, size_t errstr_size) {
const char *kv_n, *kv_v;
while (kv_parse(&str, &kv_n, &kv_v)) {
if (!strcmp(kv_n, "encoding")) {
if ((*encp = encoding_parse(kv_v)) == ENC_ERROR) {
snprintf(errstr, errstr_size,
"Invalid encoding %s", kv_v);
return -1;
}
} else if (!strcmp(kv_n, "stop.eof")) {
if (conf_tof(kv_v))
*flagsp |= INPUT_F_STOP_EOF;
else
*flagsp &= ~INPUT_F_STOP_EOF;
} else if (!strcmp(kv_n, "stop.error")) {
if (conf_tof(kv_v))
*flagsp |= INPUT_F_STOP_ERROR;
else
*flagsp &= ~INPUT_F_STOP_ERROR;
} else if (!strcmp(kv_n, "exit.on.exit")) {
if (conf_tof(kv_v))
*flagsp |= INPUT_F_EXIT_ON_EXIT;
else
*flagsp &= ~INPUT_F_EXIT_ON_EXIT;
} else {
snprintf(errstr, errstr_size,
"Unknown option: %s", kv_n);
return -1;
}
}
return 0;
}
/**
* Set a single configuration property 'name' using value 'val'.
* Returns 0 on success, and -1 on error in which case 'errstr' will
* contain an error string.
*/
int conf_set (const char *name, const char *val,
char *errstr, size_t errstr_size,
void *opaque) {
rd_kafka_conf_res_t res;
/* Kafka configuration */
if (!strncmp(name, "kafka.", strlen("kafka."))) {
name += strlen("kafka.");
/* Kafka topic configuration. */
if (!strncmp(name, "topic.", strlen("topic.")))
res = rd_kafka_topic_conf_set(conf.rkt_conf,
name+strlen("topic."),
val,
errstr, errstr_size);
else /* Kafka global configuration */
res = rd_kafka_conf_set(conf.rk_conf, name,
val, errstr, errstr_size);
if (res == RD_KAFKA_CONF_OK)
return 0;
else if (res != RD_KAFKA_CONF_UNKNOWN)
return -1;
/* Unknown configs: fall thru */
name -= strlen("kafka.");
}
/* Non "key=value" config properties */
if (!val) {
struct iovec arg[8+1];
if (ezd_regmatch("^input +(\\[([^\\]+)\\] +)?pipe +(.+)$",
name, arg, 3) == 3) {
encoding_t enc = ENC_STRING;
int flags = INPUT_F_DEFAULTS;
/* Optional: [k=v,k2=v2,..] key-value pairs */
if (arg[1].iov_base) {
if (input_kvs_parse(ezd_strndupa_iov(&arg[1]),
&enc, &flags,
errstr, errstr_size) == -1)
return -1;
}
if (!input_add(INPUT_PIPE, enc, flags,
ltrim(ezd_strndupa_iov(&arg[2])), 0, 0,
errstr, errstr_size))
return -1;
} else if (ezd_regmatch("^input +(\\[([^\\]+)\\] +)?kafka +"
"topic +([^ ]+) +"
"partition +([0-9]+)(-([0-9]+))?"
"( +from "
"+(beginning|end|stored|[0-9]+))"
"$",
name, arg, 8) >= 4) {
int part_lo, part_hi;
char *topic;
int64_t offset = RD_KAFKA_OFFSET_STORED;
encoding_t enc = ENC_STRING;
int flags = INPUT_F_DEFAULTS;
if (arg[1].iov_base) {
if (input_kvs_parse(ezd_strndupa_iov(&arg[1]),
&enc, &flags,
errstr, errstr_size) == -1)
return -1;
}
if (arg[7].iov_base) {
if (!strncmp(arg[7].iov_base, "beginning", 9))
offset = RD_KAFKA_OFFSET_BEGINNING;
else if (!strncmp(arg[7].iov_base, "end", 3))
offset = RD_KAFKA_OFFSET_END;
else if (!strncmp(arg[7].iov_base, "stored", 6))
offset = RD_KAFKA_OFFSET_STORED;
else
offset = strtoull(ezd_strndupa_iov(
&arg[7]),
NULL, 10);
}
topic = ezd_strndupa_iov(&arg[2]);
part_lo = atoi(ezd_strndupa_iov(&arg[3]));
if (arg[5].iov_base)
part_hi = atoi(ezd_strndupa_iov(&arg[5]));
else
part_hi = part_lo;
for ( ; part_lo <= part_hi ; part_lo++)
if (!input_add(INPUT_KAFKA, enc, flags,
topic, part_lo,
offset, errstr, errstr_size))
return -1;
} else if (ezd_regmatch("^output +pipe +([0-9]+) +(.+)$",
name, arg, 2) == 2) {
output_add(OUTPUT_PIPE, atoi(ezd_strndupa_iov(&arg[0])),
ezd_strndupa_iov(&arg[1]));
} else if (ezd_regmatch("^output +file +([0-9]+) +(.+)$",
name, arg, 2) == 2) {
output_add(OUTPUT_FILE, atoi(ezd_strndupa_iov(&arg[0])),
ezd_strndupa_iov(&arg[1]));
} else {
snprintf(errstr, errstr_size,
"Unknown configuration directive \"%s\"",
name);
return -1;
}
return 0;
}
/* kafkatee configuration options */
if (!strcmp(name, "output.format")) {
if (conf.fconf.format)
free(conf.fconf.format);
conf.fconf.format = strdup(val);
} else if (!strcmp(name, "output.encoding")) {
if ((conf.fconf.encoding =
encoding_parse(val)) == ENC_ERROR) {
snprintf(errstr, errstr_size,
"Unknown %s value \"%s\"", name, val);
return -1;
}
} else if (!strcmp(name, "output.delimiter")) {
if (conf.output_delimiter)
free(conf.output_delimiter);
conf.output_delimiter = str_unescape(strdup(val));
conf.output_delimiter_len = strlen(conf.output_delimiter);
} else if (!strcmp(name, "output.queue.size")) {
conf.output_queue_size = atoi(val);
} else if (!strcmp(name, "log.level"))
conf.log_level = atoi(val);
else if (!strcmp(name, "log.statistics.file")) {
free(conf.stats_file);
conf.stats_file = strdup(val);
} else if (!strcmp(name, "log.statistics.interval"))
conf.stats_interval = atoi(val);
else if (!strcmp(name, "log.rate.max"))
conf.log_rate = atoi(val);
else if (!strcmp(name, "log.rate.period"))
conf.log_rate_period = atoi(val);
else if (!strcmp(name, "daemonize"))
conf.daemonize = conf_tof(val);
else if (!strcmp(name, "pid.file.path")) {
free(conf.pid_file_path);
conf.pid_file_path = strdup(val);
} else if (!strcmp(name, "command.init")) {
if (conf.cmd_init)
free(conf.cmd_init);
conf.cmd_init = strdup(val);
} else if (!strcmp(name, "command.term")) {
if (conf.cmd_term)
free(conf.cmd_term);
conf.cmd_term = strdup(val);
} else if (!strncmp(name, "env.", strlen("env."))) {
if (*val)
setenv(name+strlen("env."), val, 1);
else
unsetenv(name+strlen("env."));
} else {
snprintf(errstr, errstr_size,
"Unknown configuration property \"%s\"\n", name);
return -1;
}
return 0;
}