move ip address parsing code to proto.c
[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 #include <signal.h>
8
9 #include <libubox/blobmsg_json.h>
10
11 #include "netifd.h"
12 #include "interface.h"
13 #include "interface-ip.h"
14 #include "proto.h"
15
16 static LIST_HEAD(handlers);
17 static int proto_fd;
18
19 struct proto_shell_handler {
20         struct list_head list;
21         struct proto_handler proto;
22         struct config_param_list config;
23         char *config_buf;
24         char script_name[];
25 };
26
27 struct proto_shell_state {
28         struct interface_proto_state proto;
29         struct proto_shell_handler *handler;
30         struct blob_attr *config;
31
32         struct device_user l3_dev;
33
34         struct uloop_timeout setup_timeout;
35         struct uloop_process setup_task;
36         struct uloop_process teardown_task;
37         bool teardown_pending;
38 };
39
40 static int
41 run_script(const char **argv, struct uloop_process *proc)
42 {
43         int pid;
44
45         if ((pid = fork()) < 0)
46                 return -1;
47
48         if (!pid) {
49                 fchdir(proto_fd);
50                 execvp(argv[0], (char **) argv);
51                 exit(127);
52         }
53
54         if (pid < 0)
55                 return -1;
56
57         proc->pid = pid;
58         uloop_process_add(proc);
59
60         return 0;
61 }
62
63 static int
64 proto_shell_handler(struct interface_proto_state *proto,
65                     enum interface_proto_cmd cmd, bool force)
66 {
67         struct proto_shell_state *state;
68         struct proto_shell_handler *handler;
69         struct uloop_process *proc;
70         const char *argv[6];
71         const char *action;
72         char *config;
73         int ret, i = 0;
74
75         state = container_of(proto, struct proto_shell_state, proto);
76         handler = state->handler;
77
78         if (cmd == PROTO_CMD_SETUP) {
79                 action = "setup";
80                 proc = &state->setup_task;
81         } else {
82                 action = "teardown";
83                 proc = &state->teardown_task;
84                 if (state->setup_task.pending) {
85                         uloop_timeout_set(&state->setup_timeout, 1000);
86                         kill(state->setup_task.pid, SIGINT);
87                         state->teardown_pending = true;
88                         return 0;
89                 }
90         }
91
92         config = blobmsg_format_json(state->config, true);
93         if (!config)
94                 return -1;
95
96         argv[i++] = handler->script_name;
97         argv[i++] = handler->proto.name;
98         argv[i++] = action;
99         argv[i++] = proto->iface->name;
100         argv[i++] = config;
101         if (proto->iface->main_dev.dev)
102                 argv[i++] = proto->iface->main_dev.dev->ifname;
103         argv[i] = NULL;
104
105         ret = run_script(argv, proc);
106         free(config);
107
108         return ret;
109 }
110
111 static void
112 proto_shell_setup_timeout_cb(struct uloop_timeout *timeout)
113 {
114         struct proto_shell_state *state;
115
116         state = container_of(timeout, struct proto_shell_state, setup_timeout);
117         kill(state->setup_task.pid, SIGKILL);
118 }
119
120 static void
121 proto_shell_setup_cb(struct uloop_process *p, int ret)
122 {
123         struct proto_shell_state *state;
124
125         state = container_of(p, struct proto_shell_state, setup_task);
126         uloop_timeout_cancel(&state->setup_timeout);
127         if (state->teardown_pending) {
128                 state->teardown_pending = false;
129                 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
130         }
131 }
132
133 static void
134 proto_shell_teardown_cb(struct uloop_process *p, int ret)
135 {
136         struct proto_shell_state *state;
137
138         state = container_of(p, struct proto_shell_state, teardown_task);
139         state->proto.proto_event(&state->proto, IFPEV_DOWN);
140         if (state->l3_dev.dev)
141                 device_remove_user(&state->l3_dev);
142 }
143
144 static void
145 proto_shell_free(struct interface_proto_state *proto)
146 {
147         struct proto_shell_state *state;
148
149         state = container_of(proto, struct proto_shell_state, proto);
150         free(state->config);
151         free(state);
152 }
153
154 enum {
155         NOTIFY_LINK_UP,
156         NOTIFY_IFNAME,
157         __NOTIFY_LAST
158 };
159
160 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
161         [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
162         [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
163 };
164
165 static int
166 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
167 {
168         struct proto_shell_state *state;
169         struct blob_attr *tb[__NOTIFY_LAST];
170         bool up;
171
172         state = container_of(proto, struct proto_shell_state, proto);
173
174         blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
175         if (!tb[NOTIFY_LINK_UP])
176                 return UBUS_STATUS_INVALID_ARGUMENT;
177
178         up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
179         if (up) {
180                 if (!tb[NOTIFY_IFNAME])
181                         return UBUS_STATUS_INVALID_ARGUMENT;
182
183                 if (!state->l3_dev.dev) {
184                         device_add_user(&state->l3_dev,
185                                 device_get(blobmsg_data(tb[NOTIFY_IFNAME]), true));
186                         device_claim(&state->l3_dev);
187                         state->proto.iface->l3_dev = &state->l3_dev;
188                 }
189                 state->proto.proto_event(&state->proto, IFPEV_UP);
190         } else {
191                 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
192         }
193
194         return 0;
195 }
196
197 struct interface_proto_state *
198 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
199                    struct blob_attr *attr)
200 {
201         struct proto_shell_state *state;
202
203         state = calloc(1, sizeof(*state));
204         state->config = malloc(blob_pad_len(attr));
205         if (!state->config)
206                 goto error;
207
208         memcpy(state->config, attr, blob_pad_len(attr));
209         state->proto.free = proto_shell_free;
210         state->proto.notify = proto_shell_notify;
211         state->proto.cb = proto_shell_handler;
212         state->setup_timeout.cb = proto_shell_setup_timeout_cb;
213         state->setup_task.cb = proto_shell_setup_cb;
214         state->teardown_task.cb = proto_shell_teardown_cb;
215         state->handler = container_of(h, struct proto_shell_handler, proto);
216
217         return &state->proto;
218
219 error:
220         free(state);
221         return NULL;
222 }
223
224 static json_object *
225 check_type(json_object *obj, json_type type)
226 {
227         if (!obj)
228                 return NULL;
229
230         if (json_object_get_type(obj) != type)
231                 return NULL;
232
233         return obj;
234 }
235
236 static inline json_object *
237 get_field(json_object *obj, const char *name, json_type type)
238 {
239         return check_type(json_object_object_get(obj, name), type);
240 }
241
242 static char *
243 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
244 {
245         struct blobmsg_policy *attrs;
246         char *str_buf, *str_cur;
247         int str_len = 0;
248         int i;
249
250         attrs = calloc(1, sizeof(*attrs));
251         if (!attrs)
252                 return NULL;
253
254         config->n_params = json_object_array_length(obj);
255         config->params = attrs;
256         for (i = 0; i < config->n_params; i++) {
257                 json_object *cur, *name, *type;
258
259                 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
260                 if (!cur)
261                         goto error;
262
263                 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
264                 if (!name)
265                         goto error;
266
267                 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
268                 if (!type)
269                         goto error;
270
271                 attrs[i].name = json_object_get_string(name);
272                 attrs[i].type = json_object_get_int(type);
273                 if (attrs[i].type > BLOBMSG_TYPE_LAST)
274                         goto error;
275
276                 str_len += strlen(attrs[i].name + 1);
277         }
278
279         str_buf = malloc(str_len);
280         if (!str_buf)
281                 goto error;
282
283         str_cur = str_buf;
284         for (i = 0; i < config->n_params; i++) {
285                 const char *name = attrs[i].name;
286
287                 attrs[i].name = str_cur;
288                 str_cur += sprintf(str_cur, "%s", name) + 1;
289         }
290
291         return str_buf;
292
293 error:
294         free(attrs);
295         config->n_params = 0;
296         return NULL;
297 }
298
299 static void
300 proto_shell_add_handler(const char *script, json_object *obj)
301 {
302         struct proto_shell_handler *handler;
303         struct proto_handler *proto;
304         json_object *config, *tmp;
305         const char *name;
306         char *str;
307
308         if (!check_type(obj, json_type_object))
309                 return;
310
311         tmp = get_field(obj, "name", json_type_string);
312         if (!tmp)
313                 return;
314
315         name = json_object_get_string(tmp);
316
317         handler = calloc(1, sizeof(*handler) +
318                          strlen(script) + 1 +
319                          strlen(name) + 1);
320         if (!handler)
321                 return;
322
323         strcpy(handler->script_name, script);
324
325         str = handler->script_name + strlen(handler->script_name) + 1;
326         strcpy(str, name);
327
328         proto = &handler->proto;
329         proto->name = str;
330         proto->config_params = &handler->config;
331         proto->attach = proto_shell_attach;
332
333         tmp = get_field(obj, "no-device", json_type_boolean);
334         if (tmp && json_object_get_boolean(tmp))
335                 handler->proto.flags |= PROTO_FLAG_NODEV;
336
337         config = get_field(obj, "config", json_type_array);
338         if (config)
339                 handler->config_buf = proto_shell_parse_config(&handler->config, config);
340
341         DPRINTF("Add handler for script %s: %s\n", script, proto->name);
342         add_proto_handler(proto);
343 }
344
345 static void proto_shell_add_script(const char *name)
346 {
347         struct json_tokener *tok = NULL;
348         json_object *obj;
349         static char buf[512];
350         char *start, *end, *cmd;
351         FILE *f;
352         int buflen, len;
353
354 #define DUMP_SUFFIX     " '' dump"
355
356         cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
357         sprintf(cmd, "%s" DUMP_SUFFIX, name);
358
359         f = popen(cmd, "r");
360         if (!f)
361                 return;
362
363         do {
364                 buflen = fread(buf, 1, sizeof(buf) - 1, f);
365                 if (buflen <= 0)
366                         continue;
367
368                 start = buf;
369                 len = buflen;
370                 do {
371                         end = memchr(start, '\n', len);
372                         if (end)
373                                 len = end - start;
374
375                         if (!tok)
376                                 tok = json_tokener_new();
377
378                         obj = json_tokener_parse_ex(tok, start, len);
379                         if (!is_error(obj)) {
380                                 proto_shell_add_handler(name, obj);
381                                 json_object_put(obj);
382                                 json_tokener_free(tok);
383                                 tok = NULL;
384                         }
385
386                         if (end) {
387                                 start = end + 1;
388                                 len = buflen - (start - buf);
389                         }
390                 } while (len > 0);
391         } while (!feof(f) && !ferror(f));
392
393         if (tok)
394                 json_tokener_free(tok);
395
396         pclose(f);
397 }
398
399 void __init proto_shell_init(void)
400 {
401         glob_t g;
402         int main_fd;
403         int i;
404
405         main_fd = open(".", O_RDONLY | O_DIRECTORY);
406         if (main_fd < 0)
407                 return;
408
409         if (chdir(main_path)) {
410                 perror("chdir(main path)");
411                 goto close_cur;
412         }
413
414         if (chdir("./proto"))
415                 goto close_cur;
416
417         proto_fd = open(".", O_RDONLY | O_DIRECTORY);
418         if (proto_fd < 0)
419                 goto close_cur;
420
421         glob("./*.sh", 0, NULL, &g);
422         for (i = 0; i < g.gl_pathc; i++)
423                 proto_shell_add_script(g.gl_pathv[i]);
424
425 close_cur:
426         fchdir(main_fd);
427         close(main_fd);
428 }