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.handler = proto_shell_handler;
115 state->handler = container_of(h, struct proto_shell_handler, proto);
117 return &state->proto;
125 proto_shell_parse_config(struct config_param_list *config, struct json_object *obj)
127 struct blobmsg_policy *attrs;
128 char *str_buf, *str_cur;
132 attrs = calloc(1, sizeof(*attrs));
136 config->n_params = json_object_array_length(obj);
137 config->params = attrs;
138 for (i = 0; i < config->n_params; i++) {
139 struct json_object *cur, *name, *type;
141 cur = json_object_array_get_idx(obj, i);
142 if (!cur || json_object_get_type(cur) != json_type_array)
145 name = json_object_array_get_idx(cur, 0);
146 if (!name || json_object_get_type(name) != json_type_string)
149 type = json_object_array_get_idx(cur, 1);
150 if (!type || json_object_get_type(type) != json_type_int)
153 attrs[i].name = json_object_get_string(name);
154 attrs[i].type = json_object_get_int(type);
155 if (attrs[i].type > BLOBMSG_TYPE_LAST)
158 str_len += strlen(attrs[i].name + 1);
161 str_buf = malloc(str_len);
166 for (i = 0; i < config->n_params; i++) {
167 const char *name = attrs[i].name;
169 attrs[i].name = str_cur;
170 str_cur += sprintf(str_cur, "%s", name) + 1;
177 config->n_params = 0;
182 proto_shell_add_handler(const char *script, struct json_object *obj)
184 struct proto_shell_handler *handler;
185 struct proto_handler *proto;
186 json_object *config, *tmp;
190 if (json_object_get_type(obj) != json_type_object)
193 tmp = json_object_object_get(obj, "name");
194 if (!tmp || json_object_get_type(tmp) != json_type_string)
197 name = json_object_get_string(tmp);
199 handler = calloc(1, sizeof(*handler) +
205 strcpy(handler->script_name, script);
207 str = handler->script_name + strlen(handler->script_name) + 1;
210 proto = &handler->proto;
212 proto->config_params = &handler->config;
213 proto->attach = proto_shell_attach;
215 config = json_object_object_get(obj, "config");
216 if (config && json_object_get_type(config) == json_type_array)
217 handler->config_buf = proto_shell_parse_config(&handler->config, config);
219 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
220 add_proto_handler(proto);
223 static void proto_shell_add_script(const char *name)
225 struct json_tokener *tok = NULL;
226 struct json_object *obj;
227 static char buf[512];
228 char *start, *end, *cmd;
232 #define DUMP_SUFFIX " '' dump"
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]);