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
)
40 if (fchdir(prev_fd
)) {}
44 int netifd_open_subdir(const char *name
)
49 prev_dir
= netifd_dir_push(-1);
50 if (chdir(main_path
)) {
51 perror("chdir(main path)");
55 ret
= open(name
, O_RDONLY
| O_DIRECTORY
);
57 system_fd_set_cloexec(ret
);
60 netifd_dir_pop(prev_dir
);
65 netifd_init_script_handler(const char *script
, json_object
*obj
, script_dump_cb cb
)
70 if (!json_check_type(obj
, json_type_object
))
73 tmp
= json_get_field(obj
, "name", json_type_string
);
77 name
= json_object_get_string(tmp
);
78 cb(script
, name
, obj
);
82 netifd_parse_script_handler(const char *name
, script_dump_cb cb
)
84 struct json_tokener
*tok
= NULL
;
91 #define DUMP_SUFFIX " '' dump"
93 cmd
= alloca(strlen(name
) + 1 + sizeof(DUMP_SUFFIX
));
94 sprintf(cmd
, "%s" DUMP_SUFFIX
, name
);
101 start
= fgets(buf
, sizeof(buf
), f
);
108 tok
= json_tokener_new();
110 obj
= json_tokener_parse_ex(tok
, start
, len
);
112 netifd_init_script_handler(name
, obj
, cb
);
113 json_object_put(obj
);
114 json_tokener_free(tok
);
116 } else if (start
[len
- 1] == '\n') {
117 json_tokener_free(tok
);
120 } while (!feof(f
) && !ferror(f
));
123 json_tokener_free(tok
);
128 void netifd_init_script_handlers(int dir_fd
, script_dump_cb cb
)
133 prev_fd
= netifd_dir_push(dir_fd
);
134 if (glob("./*.sh", 0, NULL
, &g
)) {
135 netifd_dir_pop(prev_fd
);
139 for (i
= 0; i
< g
.gl_pathc
; i
++)
140 netifd_parse_script_handler(g
.gl_pathv
[i
], cb
);
141 netifd_dir_pop(prev_fd
);
147 netifd_handler_parse_config(struct uci_blob_param_list
*config
, json_object
*obj
)
149 struct blobmsg_policy
*attrs
;
150 char *str_buf
, *str_cur
;
151 char const **validate
;
155 config
->n_params
= json_object_array_length(obj
);
156 attrs
= calloc(1, sizeof(*attrs
) * config
->n_params
);
160 validate
= calloc(1, sizeof(char*) * config
->n_params
);
164 config
->params
= attrs
;
165 config
->validate
= validate
;
166 for (i
= 0; i
< config
->n_params
; i
++) {
167 json_object
*cur
, *name
, *type
;
169 cur
= json_check_type(json_object_array_get_idx(obj
, i
), json_type_array
);
173 name
= json_check_type(json_object_array_get_idx(cur
, 0), json_type_string
);
177 type
= json_check_type(json_object_array_get_idx(cur
, 1), json_type_int
);
181 attrs
[i
].name
= json_object_get_string(name
);
182 attrs
[i
].type
= json_object_get_int(type
);
183 if (attrs
[i
].type
> BLOBMSG_TYPE_LAST
)
186 str_len
+= strlen(attrs
[i
].name
) + 1;
189 str_buf
= malloc(str_len
);
194 for (i
= 0; i
< config
->n_params
; i
++) {
195 const char *name
= attrs
[i
].name
;
198 attrs
[i
].name
= str_cur
;
199 str_cur
+= sprintf(str_cur
, "%s", name
) + 1;
200 delim
= strchr(attrs
[i
].name
, ':');
203 validate
[i
] = ++delim
;
215 config
->n_params
= 0;