add a protocol event for link loss
[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 = 0;
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         device_remove_user(&state->l3_dev);
141 }
142
143 static void
144 proto_shell_free(struct interface_proto_state *proto)
145 {
146         struct proto_shell_state *state;
147
148         state = container_of(proto, struct proto_shell_state, proto);
149         free(state->config);
150         free(state);
151 }
152
153 enum {
154         NOTIFY_LINK_UP,
155         NOTIFY_IFNAME,
156         __NOTIFY_LAST
157 };
158
159 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
160         [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
161         [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
162 };
163
164 static int
165 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
166 {
167         struct proto_shell_state *state;
168         struct blob_attr *tb[__NOTIFY_LAST];
169         bool up;
170
171         state = container_of(proto, struct proto_shell_state, proto);
172
173         blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
174         if (!tb[NOTIFY_LINK_UP])
175                 return UBUS_STATUS_INVALID_ARGUMENT;
176
177         up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
178         if (up) {
179                 if (!tb[NOTIFY_IFNAME])
180                         return UBUS_STATUS_INVALID_ARGUMENT;
181
182                 device_add_user(&state->l3_dev,
183                         device_get(blobmsg_data(tb[NOTIFY_IFNAME]), true));
184                 device_claim(&state->l3_dev);
185                 state->proto.iface->l3_dev = &state->l3_dev;
186                 state->proto.proto_event(&state->proto, IFPEV_UP);
187         } else {
188                 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
189         }
190
191         return 0;
192 }
193
194 struct interface_proto_state *
195 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
196                    struct blob_attr *attr)
197 {
198         struct proto_shell_state *state;
199
200         state = calloc(1, sizeof(*state));
201         state->config = malloc(blob_pad_len(attr));
202         if (!state->config)
203                 goto error;
204
205         memcpy(state->config, attr, blob_pad_len(attr));
206         state->proto.free = proto_shell_free;
207         state->proto.notify = proto_shell_notify;
208         state->proto.cb = proto_shell_handler;
209         state->setup_timeout.cb = proto_shell_setup_timeout_cb;
210         state->setup_task.cb = proto_shell_setup_cb;
211         state->teardown_task.cb = proto_shell_teardown_cb;
212         state->handler = container_of(h, struct proto_shell_handler, proto);
213
214         return &state->proto;
215
216 error:
217         free(state);
218         return NULL;
219 }
220
221 static json_object *
222 check_type(json_object *obj, json_type type)
223 {
224         if (!obj)
225                 return NULL;
226
227         if (json_object_get_type(obj) != type)
228                 return NULL;
229
230         return obj;
231 }
232
233 static inline json_object *
234 get_field(json_object *obj, const char *name, json_type type)
235 {
236         return check_type(json_object_object_get(obj, name), type);
237 }
238
239 static char *
240 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
241 {
242         struct blobmsg_policy *attrs;
243         char *str_buf, *str_cur;
244         int str_len = 0;
245         int i;
246
247         attrs = calloc(1, sizeof(*attrs));
248         if (!attrs)
249                 return NULL;
250
251         config->n_params = json_object_array_length(obj);
252         config->params = attrs;
253         for (i = 0; i < config->n_params; i++) {
254                 json_object *cur, *name, *type;
255
256                 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
257                 if (!cur)
258                         goto error;
259
260                 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
261                 if (!name)
262                         goto error;
263
264                 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
265                 if (!type)
266                         goto error;
267
268                 attrs[i].name = json_object_get_string(name);
269                 attrs[i].type = json_object_get_int(type);
270                 if (attrs[i].type > BLOBMSG_TYPE_LAST)
271                         goto error;
272
273                 str_len += strlen(attrs[i].name + 1);
274         }
275
276         str_buf = malloc(str_len);
277         if (!str_buf)
278                 goto error;
279
280         str_cur = str_buf;
281         for (i = 0; i < config->n_params; i++) {
282                 const char *name = attrs[i].name;
283
284                 attrs[i].name = str_cur;
285                 str_cur += sprintf(str_cur, "%s", name) + 1;
286         }
287
288         return str_buf;
289
290 error:
291         free(attrs);
292         config->n_params = 0;
293         return NULL;
294 }
295
296 static void
297 proto_shell_add_handler(const char *script, json_object *obj)
298 {
299         struct proto_shell_handler *handler;
300         struct proto_handler *proto;
301         json_object *config, *tmp;
302         const char *name;
303         char *str;
304
305         if (!check_type(obj, json_type_object))
306                 return;
307
308         tmp = get_field(obj, "name", json_type_string);
309         if (!tmp)
310                 return;
311
312         name = json_object_get_string(tmp);
313
314         handler = calloc(1, sizeof(*handler) +
315                          strlen(script) + 1 +
316                          strlen(name) + 1);
317         if (!handler)
318                 return;
319
320         strcpy(handler->script_name, script);
321
322         str = handler->script_name + strlen(handler->script_name) + 1;
323         strcpy(str, name);
324
325         proto = &handler->proto;
326         proto->name = str;
327         proto->config_params = &handler->config;
328         proto->attach = proto_shell_attach;
329
330         tmp = get_field(obj, "no-device", json_type_boolean);
331         if (tmp && json_object_get_boolean(tmp))
332                 handler->proto.flags |= PROTO_FLAG_NODEV;
333
334         config = get_field(obj, "config", json_type_array);
335         if (config)
336                 handler->config_buf = proto_shell_parse_config(&handler->config, config);
337
338         DPRINTF("Add handler for script %s: %s\n", script, proto->name);
339         add_proto_handler(proto);
340 }
341
342 static void proto_shell_add_script(const char *name)
343 {
344         struct json_tokener *tok = NULL;
345         json_object *obj;
346         static char buf[512];
347         char *start, *end, *cmd;
348         FILE *f;
349         int buflen, len;
350
351 #define DUMP_SUFFIX     " '' dump"
352
353         cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
354         sprintf(cmd, "%s" DUMP_SUFFIX, name);
355
356         f = popen(cmd, "r");
357         if (!f)
358                 return;
359
360         do {
361                 buflen = fread(buf, 1, sizeof(buf) - 1, f);
362                 if (buflen <= 0)
363                         continue;
364
365                 start = buf;
366                 len = buflen;
367                 do {
368                         end = memchr(start, '\n', len);
369                         if (end)
370                                 len = end - start;
371
372                         if (!tok)
373                                 tok = json_tokener_new();
374
375                         obj = json_tokener_parse_ex(tok, start, len);
376                         if (!is_error(obj)) {
377                                 proto_shell_add_handler(name, obj);
378                                 json_object_put(obj);
379                                 json_tokener_free(tok);
380                                 tok = NULL;
381                         }
382
383                         if (end) {
384                                 start = end + 1;
385                                 len = buflen - (start - buf);
386                         }
387                 } while (len > 0);
388         } while (!feof(f) && !ferror(f));
389
390         if (tok)
391                 json_tokener_free(tok);
392
393         pclose(f);
394 }
395
396 void __init proto_shell_init(void)
397 {
398         glob_t g;
399         int main_fd;
400         int i;
401
402         main_fd = open(".", O_RDONLY | O_DIRECTORY);
403         if (main_fd < 0)
404                 return;
405
406         if (chdir(main_path)) {
407                 perror("chdir(main path)");
408                 goto close_cur;
409         }
410
411         if (chdir("./proto"))
412                 goto close_cur;
413
414         proto_fd = open(".", O_RDONLY | O_DIRECTORY);
415         if (proto_fd < 0)
416                 goto close_cur;
417
418         glob("./*.sh", 0, NULL, &g);
419         for (i = 0; i < g.gl_pathc; i++)
420                 proto_shell_add_script(g.gl_pathv[i]);
421
422 close_cur:
423         fchdir(main_fd);
424         close(main_fd);
425 }