ef6233f5e0fd83e2a707d9018a24fad5a10f5ab1
[project/netifd.git] / proto-shell.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <glob.h>
5 #include <unistd.h>
6 #include <fcntl.h>
7
8 #include <libubox/blobmsg_json.h>
9
10 #include "netifd.h"
11 #include "interface.h"
12 #include "interface-ip.h"
13 #include "proto.h"
14
15 static LIST_HEAD(handlers);
16 static int proto_fd;
17
18 struct proto_shell_handler {
19         struct list_head list;
20         struct proto_handler proto;
21         struct config_param_list config;
22         char *config_buf;
23         char script_name[];
24 };
25
26 struct proto_shell_state {
27         struct interface_proto_state proto;
28         struct proto_shell_handler *handler;
29         struct blob_attr *config;
30 };
31
32 static int run_script(const char **argv)
33 {
34         int pid, ret;
35
36         if ((pid = fork()) < 0)
37                 return -1;
38
39         if (!pid) {
40                 fchdir(proto_fd);
41                 execvp(argv[0], (char **) argv);
42                 exit(127);
43         }
44
45         if (waitpid(pid, &ret, 0) == -1)
46                 ret = -1;
47
48         if (ret > 0)
49                 return -ret;
50
51         return 0;
52 }
53
54 static int
55 proto_shell_handler(struct interface_proto_state *proto,
56                     enum interface_proto_cmd cmd, bool force)
57 {
58         struct proto_shell_state *state;
59         struct proto_shell_handler *handler;
60         const char *argv[6];
61         char *config;
62         int ret;
63
64         state = container_of(proto, struct proto_shell_state, proto);
65         handler = state->handler;
66
67         config = blobmsg_format_json(state->config, true);
68         if (!config)
69                 return -1;
70
71         argv[0] = handler->script_name;
72         argv[1] = handler->proto.name;
73         argv[2] = "setup";
74         argv[3] = config;
75         argv[4] = NULL;
76         if (proto->iface->main_dev.dev) {
77                 argv[4] = proto->iface->main_dev.dev->ifname;
78                 argv[5] = NULL;
79         }
80
81         switch(cmd) {
82         case PROTO_CMD_TEARDOWN:
83                 argv[2] = "teardown";
84                 /* fall through */
85         case PROTO_CMD_SETUP:
86                 ret = run_script(argv);
87                 break;
88         }
89
90         free(config);
91
92         return ret;
93 }
94
95 static void
96 proto_shell_free(struct interface_proto_state *proto)
97 {
98         struct proto_shell_state *state;
99
100         state = container_of(proto, struct proto_shell_state, proto);
101         free(state->config);
102         free(state);
103 }
104
105 struct interface_proto_state *
106 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
107                    struct blob_attr *attr)
108 {
109         struct proto_shell_state *state;
110
111         state = calloc(1, sizeof(*state));
112         state->config = malloc(blob_pad_len(attr));
113         if (!state->config)
114                 goto error;
115
116         memcpy(state->config, attr, blob_pad_len(attr));
117         state->proto.free = proto_shell_free;
118         state->proto.cb = proto_shell_handler;
119         state->handler = container_of(h, struct proto_shell_handler, proto);
120
121         return &state->proto;
122
123 error:
124         free(state);
125         return NULL;
126 }
127
128 static json_object *
129 check_type(json_object *obj, json_type type)
130 {
131         if (!obj)
132                 return NULL;
133
134         if (json_object_get_type(obj) != type)
135                 return NULL;
136
137         return obj;
138 }
139
140 static inline json_object *
141 get_field(json_object *obj, const char *name, json_type type)
142 {
143         return check_type(json_object_object_get(obj, name), type);
144 }
145
146 static char *
147 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
148 {
149         struct blobmsg_policy *attrs;
150         char *str_buf, *str_cur;
151         int str_len = 0;
152         int i;
153
154         attrs = calloc(1, sizeof(*attrs));
155         if (!attrs)
156                 return NULL;
157
158         config->n_params = json_object_array_length(obj);
159         config->params = attrs;
160         for (i = 0; i < config->n_params; i++) {
161                 json_object *cur, *name, *type;
162
163                 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
164                 if (!cur)
165                         goto error;
166
167                 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
168                 if (!name)
169                         goto error;
170
171                 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
172                 if (!type)
173                         goto error;
174
175                 attrs[i].name = json_object_get_string(name);
176                 attrs[i].type = json_object_get_int(type);
177                 if (attrs[i].type > BLOBMSG_TYPE_LAST)
178                         goto error;
179
180                 str_len += strlen(attrs[i].name + 1);
181         }
182
183         str_buf = malloc(str_len);
184         if (!str_buf)
185                 goto error;
186
187         str_cur = str_buf;
188         for (i = 0; i < config->n_params; i++) {
189                 const char *name = attrs[i].name;
190
191                 attrs[i].name = str_cur;
192                 str_cur += sprintf(str_cur, "%s", name) + 1;
193         }
194
195         return str_buf;
196
197 error:
198         free(attrs);
199         config->n_params = 0;
200         return NULL;
201 }
202
203 static void
204 proto_shell_add_handler(const char *script, json_object *obj)
205 {
206         struct proto_shell_handler *handler;
207         struct proto_handler *proto;
208         json_object *config, *tmp;
209         const char *name;
210         char *str;
211
212         if (!check_type(obj, json_type_object))
213                 return;
214
215         tmp = get_field(obj, "name", json_type_string);
216         if (!tmp)
217                 return;
218
219         name = json_object_get_string(tmp);
220
221         handler = calloc(1, sizeof(*handler) +
222                          strlen(script) + 1 +
223                          strlen(name) + 1);
224         if (!handler)
225                 return;
226
227         strcpy(handler->script_name, script);
228
229         str = handler->script_name + strlen(handler->script_name) + 1;
230         strcpy(str, name);
231
232         proto = &handler->proto;
233         proto->name = str;
234         proto->config_params = &handler->config;
235         proto->attach = proto_shell_attach;
236
237         tmp = get_field(obj, "no-device", json_type_boolean);
238         if (tmp && json_object_get_boolean(tmp))
239                 handler->proto.flags |= PROTO_FLAG_NODEV;
240
241         config = get_field(obj, "config", json_type_array);
242         if (config)
243                 handler->config_buf = proto_shell_parse_config(&handler->config, config);
244
245         DPRINTF("Add handler for script %s: %s\n", script, proto->name);
246         add_proto_handler(proto);
247 }
248
249 static void proto_shell_add_script(const char *name)
250 {
251         struct json_tokener *tok = NULL;
252         json_object *obj;
253         static char buf[512];
254         char *start, *end, *cmd;
255         FILE *f;
256         int buflen, len;
257
258 #define DUMP_SUFFIX     " '' dump"
259
260         cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
261         sprintf(cmd, "%s" DUMP_SUFFIX, name);
262
263         f = popen(cmd, "r");
264         if (!f)
265                 return;
266
267         do {
268                 buflen = fread(buf, 1, sizeof(buf) - 1, f);
269                 if (buflen <= 0)
270                         continue;
271
272                 start = buf;
273                 len = buflen;
274                 do {
275                         end = memchr(start, '\n', len);
276                         if (end)
277                                 len = end - start;
278
279                         if (!tok)
280                                 tok = json_tokener_new();
281
282                         obj = json_tokener_parse_ex(tok, start, len);
283                         if (!is_error(obj)) {
284                                 proto_shell_add_handler(name, obj);
285                                 json_object_put(obj);
286                                 json_tokener_free(tok);
287                                 tok = NULL;
288                         }
289
290                         if (end) {
291                                 start = end + 1;
292                                 len = buflen - (start - buf);
293                         }
294                 } while (len > 0);
295         } while (!feof(f) && !ferror(f));
296
297         if (tok)
298                 json_tokener_free(tok);
299
300         pclose(f);
301 }
302
303 void __init proto_shell_init(void)
304 {
305         glob_t g;
306         int main_fd;
307         int i;
308
309         main_fd = open(".", O_RDONLY | O_DIRECTORY);
310         if (main_fd < 0)
311                 return;
312
313         if (chdir(main_path)) {
314                 perror("chdir(main path)");
315                 goto close_cur;
316         }
317
318         if (chdir("./proto"))
319                 goto close_cur;
320
321         proto_fd = open(".", O_RDONLY | O_DIRECTORY);
322         if (proto_fd < 0)
323                 goto close_cur;
324
325         glob("./*.sh", 0, NULL, &g);
326         for (i = 0; i < g.gl_pathc; i++)
327                 proto_shell_add_script(g.gl_pathv[i]);
328
329 close_cur:
330         fchdir(main_fd);
331         close(main_fd);
332 }