-
Notifications
You must be signed in to change notification settings - Fork 25
/
handler.c
332 lines (267 loc) · 6.71 KB
/
handler.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
/*
* netifd - network interface daemon
* Copyright (C) 2012-2013 Felix Fietkau <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#define _GNU_SOURCE
#include <glob.h>
#include <fcntl.h>
#include <stdio.h>
#include "netifd.h"
#include "system.h"
#include "handler.h"
static int
netifd_dir_push(int fd)
{
int prev_fd = open(".", O_RDONLY | O_DIRECTORY);
system_fd_set_cloexec(prev_fd);
if (fd >= 0)
if (fchdir(fd)) {}
return prev_fd;
}
static void
netifd_dir_pop(int prev_fd)
{
if (prev_fd < 0)
return;
if (fchdir(prev_fd)) {}
close(prev_fd);
}
int netifd_open_subdir(const char *name)
{
int prev_dir;
int ret = -1;
prev_dir = netifd_dir_push(-1);
if (chdir(main_path)) {
perror("chdir(main path)");
goto out;
}
ret = open(name, O_RDONLY | O_DIRECTORY);
if (ret >= 0)
system_fd_set_cloexec(ret);
out:
netifd_dir_pop(prev_dir);
return ret;
}
static void
netifd_init_script_handler(const char *script, json_object *obj, script_dump_cb cb)
{
json_object *tmp;
const char *name;
if (!json_check_type(obj, json_type_object))
return;
tmp = json_get_field(obj, "name", json_type_string);
if (!tmp)
return;
name = json_object_get_string(tmp);
cb(script, name, obj);
}
static void
netifd_init_extdev_handler(const char *config_file, json_object *obj,
create_extdev_handler_cb cb)
{
json_object *tmp, *cfg, *info, *stats;
const char *name, *ubus_name, *br_prefix = NULL;
bool bridge_support = true;
char *err_missing;
if (!json_check_type(obj, json_type_object))
return;
tmp = json_get_field(obj, "name", json_type_string);
if (!tmp) {
err_missing = "name";
goto field_missing;
}
name = json_object_get_string(tmp);
tmp = json_get_field(obj, "ubus_name", json_type_string);
if (!tmp) {
err_missing = "ubus_name";
goto field_missing;
}
ubus_name = json_object_get_string(tmp);
tmp = json_get_field(obj, "bridge", json_type_string);
if (!tmp || !strcmp(json_object_get_string(tmp), "0"))
bridge_support = false;
if (bridge_support) {
tmp = json_get_field(obj, "br-prefix", json_type_string);
if (!tmp)
br_prefix = name;
else
br_prefix = json_object_get_string(tmp);
}
tmp = json_get_field(obj, "config", json_type_array);
if (!tmp) {
err_missing = "config";
goto field_missing;
}
cfg = tmp;
info = json_get_field(obj, "info", json_type_array);
stats = json_get_field(obj, "stats", json_type_array);
cb(config_file, name, ubus_name, bridge_support, br_prefix, cfg, info, stats);
return;
field_missing:
netifd_log_message(L_WARNING, "external device handler description '%s' is"
"missing field '%s'\n", config_file, err_missing);
}
static void
netifd_parse_script_handler(const char *name, script_dump_cb cb)
{
struct json_tokener *tok = NULL;
json_object *obj;
static char buf[512];
char *start, *cmd;
FILE *f;
int len;
#define DUMP_SUFFIX " '' dump"
cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
sprintf(cmd, "%s" DUMP_SUFFIX, name);
f = popen(cmd, "r");
if (!f)
return;
do {
start = fgets(buf, sizeof(buf), f);
if (!start)
continue;
len = strlen(start);
if (!tok)
tok = json_tokener_new();
obj = json_tokener_parse_ex(tok, start, len);
if (obj) {
netifd_init_script_handler(name, obj, cb);
json_object_put(obj);
json_tokener_free(tok);
tok = NULL;
} else if (start[len - 1] == '\n') {
json_tokener_free(tok);
tok = NULL;
}
} while (!feof(f) && !ferror(f));
if (tok)
json_tokener_free(tok);
pclose(f);
}
static void
netifd_parse_extdev_handler(const char *path_to_file, create_extdev_handler_cb cb)
{
struct json_tokener *tok = NULL;
json_object *obj;
FILE *file;
int len;
char buf[512], *start;
file = fopen(path_to_file, "r");
if (!file)
return;
do {
start = fgets(buf, sizeof(buf), file);
if (!start)
continue;
len = strlen(start);
if (!tok)
tok = json_tokener_new();
obj = json_tokener_parse_ex(tok, start, len);
if (obj) {
netifd_init_extdev_handler(path_to_file, obj, cb);
json_object_put(obj);
json_tokener_free(tok);
tok = NULL;
} else if (start[len - 1] == '\n') {
json_tokener_free(tok);
tok = NULL;
}
} while (!feof(file) && !ferror(file));
if (tok)
json_tokener_free(tok);
fclose(file);
}
void netifd_init_script_handlers(int dir_fd, script_dump_cb cb)
{
glob_t g;
int prev_fd;
size_t i;
prev_fd = netifd_dir_push(dir_fd);
if (glob("./*.sh", 0, NULL, &g)) {
netifd_dir_pop(prev_fd);
return;
}
for (i = 0; i < g.gl_pathc; i++)
netifd_parse_script_handler(g.gl_pathv[i], cb);
netifd_dir_pop(prev_fd);
globfree(&g);
}
void
netifd_init_extdev_handlers(int dir_fd, create_extdev_handler_cb cb)
{
glob_t g;
int prev_fd;
prev_fd = netifd_dir_push(dir_fd);
glob("*.json", 0, NULL, &g);
for (size_t i = 0; i < g.gl_pathc; i++)
netifd_parse_extdev_handler(g.gl_pathv[i], cb);
netifd_dir_pop(prev_fd);
}
char *
netifd_handler_parse_config(struct uci_blob_param_list *config, json_object *obj)
{
struct blobmsg_policy *attrs;
char *str_buf, *str_cur;
char const **validate;
int str_len = 0;
int i;
config->n_params = json_object_array_length(obj);
attrs = calloc(1, sizeof(*attrs) * config->n_params);
if (!attrs)
return NULL;
validate = calloc(1, sizeof(char*) * config->n_params);
if (!validate)
goto error;
config->params = attrs;
config->validate = validate;
for (i = 0; i < config->n_params; i++) {
json_object *cur, *name, *type;
cur = json_check_type(json_object_array_get_idx(obj, i), json_type_array);
if (!cur)
goto error;
name = json_check_type(json_object_array_get_idx(cur, 0), json_type_string);
if (!name)
goto error;
type = json_check_type(json_object_array_get_idx(cur, 1), json_type_int);
if (!type)
goto error;
attrs[i].name = json_object_get_string(name);
attrs[i].type = json_object_get_int(type);
if (attrs[i].type > BLOBMSG_TYPE_LAST)
goto error;
str_len += strlen(attrs[i].name) + 1;
}
str_buf = malloc(str_len);
if (!str_buf)
goto error;
str_cur = str_buf;
for (i = 0; i < config->n_params; i++) {
const char *name = attrs[i].name;
char *delim;
attrs[i].name = str_cur;
str_cur += sprintf(str_cur, "%s", name) + 1;
delim = strchr(attrs[i].name, ':');
if (delim) {
*delim = '\0';
validate[i] = ++delim;
} else {
validate[i] = NULL;
}
}
return str_buf;
error:
free(attrs);
if (validate)
free(validate);
config->n_params = 0;
return NULL;
}