-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.c
285 lines (245 loc) · 7.48 KB
/
main.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
/* cdns - Cure DNS
* Copyright (C) 2016 Zhuofei Wang <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/socket.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <argp.h>
#include "event2/event.h"
#include "util.h"
#include "log.h"
#include "cfg.h"
#define DEFAULT_CONFIG_FILE "/etc/cdns.json"
static const char * program_name = "cdns";
static const char program_doc[] =
"cdns - version: 1.1\n"
"Copyright (C) 2016-2018 Zhuofei Wang <[email protected]>\n"
"\n"
"cdns cures your DNS.";
struct arg_data
{
char * config_file; /* path to config file */
};
/* Define options */
static struct argp_option options[] =
{
{"config", 'c', "path", 0, "Path to config file (Default: " DEFAULT_CONFIG_FILE ")"},
{0}
};
static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
struct arg_data *data = (struct arg_data *)state->input;
switch (key)
{
case 'c':
data->config_file = arg;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
/*
The ARGP structure itself.
*/
static struct argp arg_def = {options, parse_opt, "", program_doc};
static struct arg_data prog_arg = {.config_file = DEFAULT_CONFIG_FILE,
};
/*----------------------------------------------------------------------
-----------------------------------------------------------------------*/
static void start_server();
static int init_signal_handlers(struct event_base * base);
static bool write_pidfile(const char * pidfile);
struct {
bool daemon;
char * log;
bool log_debug;
bool log_info;
char * pidfile;
} g_app_cfg = { true, "syslog:daemon", false, true, NULL};
config_item global_cfg_items[] = {{.key = "daemon",
.type = cdt_bool,
.value = &g_app_cfg.daemon,
},
{.key = "log",
.type = cdt_string,
.value = &g_app_cfg.log,
},
{.key = "log_debug",
.type = cdt_bool,
.value = &g_app_cfg.log_debug,
},
{.key = "log_info",
.type = cdt_bool,
.value = &g_app_cfg.log_info,
},
{.key = "pidfile",
.type = cdt_string,
.value = &g_app_cfg.pidfile,
},
{.key = NULL,}
};
extern config_item cdns_cfg_items[];
config_item app_cfg_items[] = {{.key = "global",
.type = cdt_object,
.subitems = &global_cfg_items[0],
},
{.key = "cdns",
.type = cdt_object,
.subitems = &cdns_cfg_items[0],
},
{.key = NULL,}
};
static void init_global_cfg()
{
/* init with default config */
}
bool cdns_validate_cfg();
int cdns_init_server(struct event_base * base);
void cdns_debug_dump();
int main (int argc, char * argv[])
{
/* Handle arguments */
argp_parse (&arg_def, argc, argv, 0, 0, &prog_arg);
/* init globals */
init_global_cfg();
/* Load configurations */
if (!cfg_loadf(prog_arg.config_file, &app_cfg_items[0]))
return -1;
/* Verify configurations */
if (!cdns_validate_cfg())
return -1;
/* Setup logging */
if (log_preopen("cdns", g_app_cfg.log, g_app_cfg.log_debug, g_app_cfg.log_info)) {
fprintf(stderr, "Failed to set up logging.\n");
return -1;
}
log_open();
if (g_app_cfg.daemon) {
if (daemon(1, 0)) {
log_errno(LOG_ERR, "daemon");
return -1;
}
}
if (g_app_cfg.pidfile)
write_pidfile(g_app_cfg.pidfile);
/* Start serving */
start_server();
/* Clean up before exit */
return 0;
}
/*--------------- CUT --------------------*/
static void terminate(int sig, short what, void *_arg)
{
struct event_base * base = _arg;
if (event_base_loopbreak(base) != 0)
log_error(LOG_WARNING, "event_loopbreak");
}
static void sigusr1_handler(int sig, short what, void *_arg)
{
cdns_debug_dump();
}
static int init_signal_handlers(struct event_base * base)
{
struct event * terminators[2];
struct event * dumper = NULL;
int exit_signals[2] = {SIGTERM, SIGINT};
struct sigaction sa;
int i;
/* Setup signals that terminate program */
memset(terminators, 0, sizeof(terminators));
assert(SIZEOF_ARRAY(exit_signals) == SIZEOF_ARRAY(terminators));
for (i = 0; i < SIZEOF_ARRAY(exit_signals); i++) {
terminators[i] = evsignal_new(base, exit_signals[i], terminate, base);
if (!terminators[i]) {
log_errno(LOG_ERR, "evsignal_new");
goto fail;
}
if (evsignal_add(terminators[i], NULL) != 0) {
log_errno(LOG_ERR, "evsignal_add");
goto fail;
}
}
/* Do not panic due to broken pipe. */
memset(&sa, 0, sizeof(struct sigaction));
sa.sa_handler = SIG_IGN;
sa.sa_flags = SA_RESTART;
if (sigaction(SIGPIPE, &sa, NULL) == -1)
goto fail;
dumper = evsignal_new(base, SIGUSR1, sigusr1_handler, NULL);
if (!dumper) {
log_errno(LOG_ERR, "evsignal_new");
goto fail;
}
if (evsignal_add(dumper, NULL) != 0) {
log_errno(LOG_ERR, "evsignal_add");
goto fail;
}
return 0;
fail:
if (dumper) {
if (evsignal_del(dumper) != 0)
log_errno(LOG_WARNING, "evsignal_del");
event_free(dumper);
}
for (i = 0; i < SIZEOF_ARRAY(exit_signals); i++) {
if (terminators[i]) {
if (evsignal_del(terminators[i]) != 0)
log_errno(LOG_WARNING, "evsignal_del");
event_free(terminators[i]);
}
}
return -1;
}
static void start_server()
{
struct event_base * base = event_base_new();
if (!base) {
log_error(LOG_ERR, "Unable to initialize libevent base");
return;
}
// Handling signals with events ensures no race condition
if (init_signal_handlers(base))
return;
if (cdns_init_server(base))
return;
log_error(LOG_INFO, "%s started", program_name);
event_base_dispatch(base);
event_base_free(base);
}
static bool write_pidfile(const char * pidfile)
{
FILE * f;
if (pidfile) {
f = fopen(pidfile, "w");
if (!f) {
log_error(LOG_WARNING, "Unable to open pidfile for write: %s", pidfile);
return false;
}
fprintf(f, "%d\n", getpid());
fclose(f);
return true;
}
return false;
}