-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconfig.c
109 lines (95 loc) · 2.6 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
#include "config.h"
#include "module.h"
#include "logging.h"
#include "avl/avl.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <libconfig.h>
static const char *shortopts = "f:dP:v";
static struct option options[] = {
{ NULL }
};
struct config config = {
.conffile = "/etc/skeeter.conf",
.pidfile = "/var/run/skeeter.pid",
.loglevel = LOG_ERR,
};
int
parse_options(int argc, char **argv, struct config *config)
{
int opt, option_index;
while ((opt = getopt_long(argc, argv, shortopts,
options, &option_index)) != -1)
{
switch (opt)
{
case 0:
/* a separate long option */
break;
case 'd':
/* do not detach - we are in debug mode */
config->debug++;
break;
case 'f':
/* we have a config */
config->conffile = optarg;
break;
case 'P':
/* pidfile specified */
config->pidfile = optarg;
break;
case 'v':
/* print version and end */
printf("Skeeter\n");
exit(0);
break;
default:
/* garbage in, bail */
skeeter_log(LOG_CRIT, "Unknown option -%c", opt);
return 1;
}
}
if (optind < argc) {
/* there is something unexpected on the command line, bail too */
skeeter_log(LOG_CRIT, "Unexpected argument '%s'", argv[optind]);
return 1;
}
return 0;
}
int
process_config_file(struct config *config)
{
int rc = 0;
config_t cfg;
config_setting_t *root;
struct module **p;
config_init(&cfg);
if (config_read_file(&cfg, config->conffile) == CONFIG_FALSE) {
/* failure */
skeeter_log(LOG_ERR, "Failure reading configuration file '%s' at line %d: %s",
config_error_file(&cfg), config_error_line(&cfg),
config_error_text(&cfg));
return 1;
}
root = config_root_setting(&cfg);
for (p = modules; *p; p++) {
struct module *module = *p;
config_setting_t *module_conf;
module_conf = config_setting_get_member(root, module->name);
if (!module_conf)
continue;
rc = register_module(module);
if (rc)
break;
if (module->conf) {
rc = module->conf(module, module_conf);
if (rc)
break;
}
}
config_destroy(&cfg);
return rc;
}