8 #include <libubox/blobmsg_json.h>
11 #include "interface.h"
12 #include "interface-ip.h"
15 static LIST_HEAD(handlers);
18 struct proto_shell_handler {
19 struct list_head list;
20 struct proto_handler proto;
21 struct config_param_list config;
26 struct proto_shell_state {
27 struct interface_proto_state proto;
28 struct proto_shell_handler *handler;
29 struct blob_attr *config;
32 #define DUMP_SUFFIX " '' dump"
34 static int run_script(const char **argv)
38 if ((pid = fork()) < 0)
43 execvp(argv[0], (char **) argv);
47 if (waitpid(pid, &ret, 0) == -1)
57 proto_shell_handler(struct interface_proto_state *proto,
58 enum interface_proto_cmd cmd, bool force)
60 struct proto_shell_state *state;
61 struct proto_shell_handler *handler;
66 state = container_of(proto, struct proto_shell_state, proto);
67 handler = state->handler;
69 config = blobmsg_format_json(state->config, true);
73 argv[0] = handler->script_name;
74 argv[1] = handler->proto.name;
83 case PROTO_CMD_TEARDOWN:
84 ret = run_script(argv);
94 proto_shell_free(struct interface_proto_state *proto)
96 struct proto_shell_state *state;
98 state = container_of(proto, struct proto_shell_state, proto);
103 struct interface_proto_state *
104 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
105 struct blob_attr *attr)
107 struct proto_shell_state *state;
109 state = calloc(1, sizeof(*state));
110 state->config = malloc(blob_pad_len(attr));
114 memcpy(state->config, attr, blob_pad_len(attr));
115 state->proto.free = proto_shell_free;
116 state->proto.handler = proto_shell_handler;
117 state->handler = container_of(h, struct proto_shell_handler, proto);
119 return &state->proto;
127 proto_shell_parse_config(struct config_param_list *config, struct json_object *obj)
129 struct blobmsg_policy *attrs;
130 char *str_buf, *str_cur;
134 attrs = calloc(1, sizeof(*attrs));
138 config->n_params = json_object_array_length(obj);
139 config->params = attrs;
140 for (i = 0; i < config->n_params; i++) {
141 struct json_object *cur, *name, *type;
143 cur = json_object_array_get_idx(obj, i);
144 if (!cur || json_object_get_type(cur) != json_type_array)
147 name = json_object_array_get_idx(cur, 0);
148 if (!name || json_object_get_type(name) != json_type_string)
151 type = json_object_array_get_idx(cur, 1);
152 if (!type || json_object_get_type(type) != json_type_int)
155 attrs[i].name = json_object_get_string(name);
156 attrs[i].type = json_object_get_int(type);
157 if (attrs[i].type > BLOBMSG_TYPE_LAST)
160 str_len += strlen(attrs[i].name + 1);
163 str_buf = malloc(str_len);
168 for (i = 0; i < config->n_params; i++) {
169 const char *name = attrs[i].name;
171 attrs[i].name = str_cur;
172 str_cur += sprintf(str_cur, "%s", name) + 1;
179 config->n_params = 0;
184 proto_shell_add_handler(const char *script, struct json_object *obj)
186 struct proto_shell_handler *handler;
187 struct proto_handler *proto;
188 json_object *config, *tmp;
192 if (json_object_get_type(obj) != json_type_object)
195 tmp = json_object_object_get(obj, "name");
196 if (!tmp || json_object_get_type(tmp) != json_type_string)
199 name = json_object_get_string(tmp);
201 handler = calloc(1, sizeof(*handler) +
207 strcpy(handler->script_name, script);
209 str = handler->script_name + strlen(handler->script_name) + 1;
212 proto = &handler->proto;
214 proto->config_params = &handler->config;
215 proto->attach = proto_shell_attach;
217 config = json_object_object_get(obj, "config");
218 if (config && json_object_get_type(config) == json_type_array)
219 handler->config_buf = proto_shell_parse_config(&handler->config, config);
221 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
222 add_proto_handler(proto);
225 static void proto_shell_add_script(const char *name)
227 struct json_tokener *tok = NULL;
228 struct json_object *obj;
229 static char buf[512];
230 char *start, *end, *cmd;
234 cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
235 sprintf(cmd, "%s" DUMP_SUFFIX, name);
242 buflen = fread(buf, 1, sizeof(buf) - 1, f);
249 end = memchr(start, '\n', len);
254 tok = json_tokener_new();
256 obj = json_tokener_parse_ex(tok, start, len);
257 if (!is_error(obj)) {
258 proto_shell_add_handler(name, obj);
259 json_object_put(obj);
260 json_tokener_free(tok);
266 len = buflen - (start - buf);
269 } while (!feof(f) && !ferror(f));
272 json_tokener_free(tok);
277 void __init proto_shell_init(void)
283 main_fd = open(".", O_RDONLY | O_DIRECTORY);
287 if (chdir(main_path)) {
288 perror("chdir(main path)");
292 if (chdir("./proto"))
295 proto_fd = open(".", O_RDONLY | O_DIRECTORY);
299 glob("./*.sh", 0, NULL, &g);
300 for (i = 0; i < g.gl_pathc; i++)
301 proto_shell_add_script(g.gl_pathv[i]);