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[0] = handler->script_name;
72 argv[1] = handler->proto.name;
81 case PROTO_CMD_TEARDOWN:
82 ret = run_script(argv);
92 proto_shell_free(struct interface_proto_state *proto)
94 struct proto_shell_state *state;
96 state = container_of(proto, struct proto_shell_state, proto);
101 struct interface_proto_state *
102 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
103 struct blob_attr *attr)
105 struct proto_shell_state *state;
107 state = calloc(1, sizeof(*state));
108 state->config = malloc(blob_pad_len(attr));
112 memcpy(state->config, attr, blob_pad_len(attr));
113 state->proto.free = proto_shell_free;
114 state->proto.cb = proto_shell_handler;
115 state->handler = container_of(h, struct proto_shell_handler, proto);
117 return &state->proto;
125 check_type(json_object *obj, json_type type)
130 if (json_object_get_type(obj) != type)
136 static inline json_object *
137 get_field(json_object *obj, const char *name, json_type type)
139 return check_type(json_object_object_get(obj, name), type);
143 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
145 struct blobmsg_policy *attrs;
146 char *str_buf, *str_cur;
150 attrs = calloc(1, sizeof(*attrs));
154 config->n_params = json_object_array_length(obj);
155 config->params = attrs;
156 for (i = 0; i < config->n_params; i++) {
157 json_object *cur, *name, *type;
159 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
163 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
167 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
171 attrs[i].name = json_object_get_string(name);
172 attrs[i].type = json_object_get_int(type);
173 if (attrs[i].type > BLOBMSG_TYPE_LAST)
176 str_len += strlen(attrs[i].name + 1);
179 str_buf = malloc(str_len);
184 for (i = 0; i < config->n_params; i++) {
185 const char *name = attrs[i].name;
187 attrs[i].name = str_cur;
188 str_cur += sprintf(str_cur, "%s", name) + 1;
195 config->n_params = 0;
200 proto_shell_add_handler(const char *script, json_object *obj)
202 struct proto_shell_handler *handler;
203 struct proto_handler *proto;
204 json_object *config, *tmp;
208 if (!check_type(obj, json_type_object))
211 tmp = get_field(obj, "name", json_type_string);
215 name = json_object_get_string(tmp);
217 handler = calloc(1, sizeof(*handler) +
223 strcpy(handler->script_name, script);
225 str = handler->script_name + strlen(handler->script_name) + 1;
228 proto = &handler->proto;
230 proto->config_params = &handler->config;
231 proto->attach = proto_shell_attach;
233 tmp = get_field(obj, "no-device", json_type_boolean);
234 if (tmp && json_object_get_boolean(tmp))
235 handler->proto.flags |= PROTO_FLAG_NODEV;
237 config = get_field(obj, "config", json_type_array);
239 handler->config_buf = proto_shell_parse_config(&handler->config, config);
241 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
242 add_proto_handler(proto);
245 static void proto_shell_add_script(const char *name)
247 struct json_tokener *tok = NULL;
249 static char buf[512];
250 char *start, *end, *cmd;
254 #define DUMP_SUFFIX " '' dump"
256 cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
257 sprintf(cmd, "%s" DUMP_SUFFIX, name);
264 buflen = fread(buf, 1, sizeof(buf) - 1, f);
271 end = memchr(start, '\n', len);
276 tok = json_tokener_new();
278 obj = json_tokener_parse_ex(tok, start, len);
279 if (!is_error(obj)) {
280 proto_shell_add_handler(name, obj);
281 json_object_put(obj);
282 json_tokener_free(tok);
288 len = buflen - (start - buf);
291 } while (!feof(f) && !ferror(f));
294 json_tokener_free(tok);
299 void __init proto_shell_init(void)
305 main_fd = open(".", O_RDONLY | O_DIRECTORY);
309 if (chdir(main_path)) {
310 perror("chdir(main path)");
314 if (chdir("./proto"))
317 proto_fd = open(".", O_RDONLY | O_DIRECTORY);
321 glob("./*.sh", 0, NULL, &g);
322 for (i = 0; i < g.gl_pathc; i++)
323 proto_shell_add_script(g.gl_pathv[i]);