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