9 #include <libubox/blobmsg_json.h>
12 #include "interface.h"
13 #include "interface-ip.h"
16 static LIST_HEAD(handlers);
19 struct proto_shell_handler {
20 struct list_head list;
21 struct proto_handler proto;
22 struct config_param_list config;
27 struct proto_shell_state {
28 struct interface_proto_state proto;
29 struct proto_shell_handler *handler;
30 struct blob_attr *config;
32 struct uloop_process setup_task;
33 struct uloop_process teardown_task;
34 bool teardown_pending;
38 run_script(const char **argv, struct uloop_process *proc)
42 if ((pid = fork()) < 0)
47 execvp(argv[0], (char **) argv);
55 uloop_process_add(proc);
61 proto_shell_handler(struct interface_proto_state *proto,
62 enum interface_proto_cmd cmd, bool force)
64 struct proto_shell_state *state;
65 struct proto_shell_handler *handler;
66 struct uloop_process *proc;
72 state = container_of(proto, struct proto_shell_state, proto);
73 handler = state->handler;
75 if (cmd == PROTO_CMD_SETUP) {
77 proc = &state->setup_task;
80 proc = &state->teardown_task;
81 if (state->setup_task.pending) {
82 kill(state->setup_task.pid, SIGINT);
83 state->teardown_pending = true;
88 config = blobmsg_format_json(state->config, true);
92 argv[i++] = handler->script_name;
93 argv[i++] = handler->proto.name;
95 argv[i++] = proto->iface->name;
97 if (proto->iface->main_dev.dev)
98 argv[i++] = proto->iface->main_dev.dev->ifname;
101 ret = run_script(argv, proc);
108 proto_shell_setup_cb(struct uloop_process *p, int ret)
110 struct proto_shell_state *state;
112 state = container_of(p, struct proto_shell_state, setup_task);
113 if (state->teardown_pending) {
114 state->teardown_pending = 0;
115 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
120 proto_shell_teardown_cb(struct uloop_process *p, int ret)
122 struct proto_shell_state *state;
124 state = container_of(p, struct proto_shell_state, teardown_task);
125 state->proto.proto_event(&state->proto, IFPEV_DOWN);
129 proto_shell_free(struct interface_proto_state *proto)
131 struct proto_shell_state *state;
133 state = container_of(proto, struct proto_shell_state, proto);
138 struct interface_proto_state *
139 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
140 struct blob_attr *attr)
142 struct proto_shell_state *state;
144 state = calloc(1, sizeof(*state));
145 state->config = malloc(blob_pad_len(attr));
149 memcpy(state->config, attr, blob_pad_len(attr));
150 state->proto.free = proto_shell_free;
151 state->proto.cb = proto_shell_handler;
152 state->setup_task.cb = proto_shell_setup_cb;
153 state->teardown_task.cb = proto_shell_teardown_cb;
154 state->handler = container_of(h, struct proto_shell_handler, proto);
156 return &state->proto;
164 check_type(json_object *obj, json_type type)
169 if (json_object_get_type(obj) != type)
175 static inline json_object *
176 get_field(json_object *obj, const char *name, json_type type)
178 return check_type(json_object_object_get(obj, name), type);
182 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
184 struct blobmsg_policy *attrs;
185 char *str_buf, *str_cur;
189 attrs = calloc(1, sizeof(*attrs));
193 config->n_params = json_object_array_length(obj);
194 config->params = attrs;
195 for (i = 0; i < config->n_params; i++) {
196 json_object *cur, *name, *type;
198 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
202 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
206 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
210 attrs[i].name = json_object_get_string(name);
211 attrs[i].type = json_object_get_int(type);
212 if (attrs[i].type > BLOBMSG_TYPE_LAST)
215 str_len += strlen(attrs[i].name + 1);
218 str_buf = malloc(str_len);
223 for (i = 0; i < config->n_params; i++) {
224 const char *name = attrs[i].name;
226 attrs[i].name = str_cur;
227 str_cur += sprintf(str_cur, "%s", name) + 1;
234 config->n_params = 0;
239 proto_shell_add_handler(const char *script, json_object *obj)
241 struct proto_shell_handler *handler;
242 struct proto_handler *proto;
243 json_object *config, *tmp;
247 if (!check_type(obj, json_type_object))
250 tmp = get_field(obj, "name", json_type_string);
254 name = json_object_get_string(tmp);
256 handler = calloc(1, sizeof(*handler) +
262 strcpy(handler->script_name, script);
264 str = handler->script_name + strlen(handler->script_name) + 1;
267 proto = &handler->proto;
269 proto->config_params = &handler->config;
270 proto->attach = proto_shell_attach;
272 tmp = get_field(obj, "no-device", json_type_boolean);
273 if (tmp && json_object_get_boolean(tmp))
274 handler->proto.flags |= PROTO_FLAG_NODEV;
276 config = get_field(obj, "config", json_type_array);
278 handler->config_buf = proto_shell_parse_config(&handler->config, config);
280 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
281 add_proto_handler(proto);
284 static void proto_shell_add_script(const char *name)
286 struct json_tokener *tok = NULL;
288 static char buf[512];
289 char *start, *end, *cmd;
293 #define DUMP_SUFFIX " '' dump"
295 cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
296 sprintf(cmd, "%s" DUMP_SUFFIX, name);
303 buflen = fread(buf, 1, sizeof(buf) - 1, f);
310 end = memchr(start, '\n', len);
315 tok = json_tokener_new();
317 obj = json_tokener_parse_ex(tok, start, len);
318 if (!is_error(obj)) {
319 proto_shell_add_handler(name, obj);
320 json_object_put(obj);
321 json_tokener_free(tok);
327 len = buflen - (start - buf);
330 } while (!feof(f) && !ferror(f));
333 json_tokener_free(tok);
338 void __init proto_shell_init(void)
344 main_fd = open(".", O_RDONLY | O_DIRECTORY);
348 if (chdir(main_path)) {
349 perror("chdir(main path)");
353 if (chdir("./proto"))
356 proto_fd = open(".", O_RDONLY | O_DIRECTORY);
360 glob("./*.sh", 0, NULL, &g);
361 for (i = 0; i < g.gl_pathc; i++)
362 proto_shell_add_script(g.gl_pathv[i]);