2 * netifd - network interface daemon
3 * Copyright (C) 2012-2013 Felix Fietkau <nbd@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
25 netifd_dir_push(int fd)
27 int prev_fd = open(".", O_RDONLY | O_DIRECTORY);
28 system_fd_set_cloexec(prev_fd);
35 netifd_dir_pop(int prev_fd)
37 if (fchdir(prev_fd)) {}
41 int netifd_open_subdir(const char *name)
46 prev_dir = netifd_dir_push(-1);
47 if (chdir(main_path)) {
48 perror("chdir(main path)");
52 ret = open(name, O_RDONLY | O_DIRECTORY);
54 system_fd_set_cloexec(ret);
57 netifd_dir_pop(prev_dir);
62 netifd_init_script_handler(const char *script, json_object *obj, script_dump_cb cb)
67 if (!json_check_type(obj, json_type_object))
70 tmp = json_get_field(obj, "name", json_type_string);
74 name = json_object_get_string(tmp);
75 cb(script, name, obj);
79 netifd_parse_script_handler(const char *name, script_dump_cb cb)
81 struct json_tokener *tok = NULL;
88 #define DUMP_SUFFIX " '' dump"
90 cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
91 sprintf(cmd, "%s" DUMP_SUFFIX, name);
98 start = fgets(buf, sizeof(buf), f);
105 tok = json_tokener_new();
107 obj = json_tokener_parse_ex(tok, start, len);
108 if (!is_error(obj)) {
109 netifd_init_script_handler(name, obj, cb);
110 json_object_put(obj);
111 json_tokener_free(tok);
113 } else if (start[len - 1] == '\n') {
114 json_tokener_free(tok);
117 } while (!feof(f) && !ferror(f));
120 json_tokener_free(tok);
125 void netifd_init_script_handlers(int dir_fd, script_dump_cb cb)
130 prev_fd = netifd_dir_push(dir_fd);
131 glob("./*.sh", 0, NULL, &g);
132 for (i = 0; i < g.gl_pathc; i++)
133 netifd_parse_script_handler(g.gl_pathv[i], cb);
134 netifd_dir_pop(prev_fd);
138 netifd_handler_parse_config(struct uci_blob_param_list *config, json_object *obj)
140 struct blobmsg_policy *attrs;
141 char *str_buf, *str_cur;
142 char const **validate;
146 config->n_params = json_object_array_length(obj);
147 attrs = calloc(1, sizeof(*attrs) * config->n_params);
151 validate = calloc(1, sizeof(char*) * config->n_params);
155 config->params = attrs;
156 config->validate = validate;
157 for (i = 0; i < config->n_params; i++) {
158 json_object *cur, *name, *type;
160 cur = json_check_type(json_object_array_get_idx(obj, i), json_type_array);
164 name = json_check_type(json_object_array_get_idx(cur, 0), json_type_string);
168 type = json_check_type(json_object_array_get_idx(cur, 1), json_type_int);
172 attrs[i].name = json_object_get_string(name);
173 attrs[i].type = json_object_get_int(type);
174 if (attrs[i].type > BLOBMSG_TYPE_LAST)
177 str_len += strlen(attrs[i].name) + 1;
180 str_buf = malloc(str_len);
185 for (i = 0; i < config->n_params; i++) {
186 const char *name = attrs[i].name;
189 attrs[i].name = str_cur;
190 str_cur += sprintf(str_cur, "%s", name) + 1;
191 delim = strchr(attrs[i].name, ':');
194 validate[i] = ++delim;
206 config->n_params = 0;