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 static int run_script(const char **argv)
36 if ((pid = fork()) < 0)
41 execvp(argv[0], (char **) argv);
45 if (waitpid(pid, &ret, 0) == -1)
55 proto_shell_handler(struct interface_proto_state *proto,
56 enum interface_proto_cmd cmd, bool force)
58 struct proto_shell_state *state;
59 struct proto_shell_handler *handler;
64 state = container_of(proto, struct proto_shell_state, proto);
65 handler = state->handler;
67 config = blobmsg_format_json(state->config, true);
71 argv[i++] = handler->script_name;
72 argv[i++] = handler->proto.name;
73 argv[i++] = cmd == PROTO_CMD_SETUP ? "setup" : "teardown";
74 argv[i++] = proto->iface->name;
76 if (proto->iface->main_dev.dev)
77 argv[i++] = proto->iface->main_dev.dev->ifname;
80 ret = run_script(argv);
87 proto_shell_free(struct interface_proto_state *proto)
89 struct proto_shell_state *state;
91 state = container_of(proto, struct proto_shell_state, proto);
96 struct interface_proto_state *
97 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
98 struct blob_attr *attr)
100 struct proto_shell_state *state;
102 state = calloc(1, sizeof(*state));
103 state->config = malloc(blob_pad_len(attr));
107 memcpy(state->config, attr, blob_pad_len(attr));
108 state->proto.free = proto_shell_free;
109 state->proto.cb = proto_shell_handler;
110 state->handler = container_of(h, struct proto_shell_handler, proto);
112 return &state->proto;
120 check_type(json_object *obj, json_type type)
125 if (json_object_get_type(obj) != type)
131 static inline json_object *
132 get_field(json_object *obj, const char *name, json_type type)
134 return check_type(json_object_object_get(obj, name), type);
138 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
140 struct blobmsg_policy *attrs;
141 char *str_buf, *str_cur;
145 attrs = calloc(1, sizeof(*attrs));
149 config->n_params = json_object_array_length(obj);
150 config->params = attrs;
151 for (i = 0; i < config->n_params; i++) {
152 json_object *cur, *name, *type;
154 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
158 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
162 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
166 attrs[i].name = json_object_get_string(name);
167 attrs[i].type = json_object_get_int(type);
168 if (attrs[i].type > BLOBMSG_TYPE_LAST)
171 str_len += strlen(attrs[i].name + 1);
174 str_buf = malloc(str_len);
179 for (i = 0; i < config->n_params; i++) {
180 const char *name = attrs[i].name;
182 attrs[i].name = str_cur;
183 str_cur += sprintf(str_cur, "%s", name) + 1;
190 config->n_params = 0;
195 proto_shell_add_handler(const char *script, json_object *obj)
197 struct proto_shell_handler *handler;
198 struct proto_handler *proto;
199 json_object *config, *tmp;
203 if (!check_type(obj, json_type_object))
206 tmp = get_field(obj, "name", json_type_string);
210 name = json_object_get_string(tmp);
212 handler = calloc(1, sizeof(*handler) +
218 strcpy(handler->script_name, script);
220 str = handler->script_name + strlen(handler->script_name) + 1;
223 proto = &handler->proto;
225 proto->config_params = &handler->config;
226 proto->attach = proto_shell_attach;
228 tmp = get_field(obj, "no-device", json_type_boolean);
229 if (tmp && json_object_get_boolean(tmp))
230 handler->proto.flags |= PROTO_FLAG_NODEV;
232 config = get_field(obj, "config", json_type_array);
234 handler->config_buf = proto_shell_parse_config(&handler->config, config);
236 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
237 add_proto_handler(proto);
240 static void proto_shell_add_script(const char *name)
242 struct json_tokener *tok = NULL;
244 static char buf[512];
245 char *start, *end, *cmd;
249 #define DUMP_SUFFIX " '' dump"
251 cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
252 sprintf(cmd, "%s" DUMP_SUFFIX, name);
259 buflen = fread(buf, 1, sizeof(buf) - 1, f);
266 end = memchr(start, '\n', len);
271 tok = json_tokener_new();
273 obj = json_tokener_parse_ex(tok, start, len);
274 if (!is_error(obj)) {
275 proto_shell_add_handler(name, obj);
276 json_object_put(obj);
277 json_tokener_free(tok);
283 len = buflen - (start - buf);
286 } while (!feof(f) && !ferror(f));
289 json_tokener_free(tok);
294 void __init proto_shell_init(void)
300 main_fd = open(".", O_RDONLY | O_DIRECTORY);
304 if (chdir(main_path)) {
305 perror("chdir(main path)");
309 if (chdir("./proto"))
312 proto_fd = open(".", O_RDONLY | O_DIRECTORY);
316 glob("./*.sh", 0, NULL, &g);
317 for (i = 0; i < g.gl_pathc; i++)
318 proto_shell_add_script(g.gl_pathv[i]);