proto-shell: retry setup if the proto handler script quits without changing the state...
[project/netifd.git] / proto-shell.c
1 /*
2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14 #define _GNU_SOURCE
15
16 #include <string.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <signal.h>
20
21 #include <arpa/inet.h>
22 #include <netinet/in.h>
23
24
25 #include "netifd.h"
26 #include "interface.h"
27 #include "interface-ip.h"
28 #include "proto.h"
29 #include "system.h"
30 #include "handler.h"
31
32 static int proto_fd = -1;
33
34 enum proto_shell_sm {
35 S_IDLE,
36 S_SETUP,
37 S_SETUP_ABORT,
38 S_TEARDOWN,
39 };
40
41 struct proto_shell_handler {
42 struct list_head list;
43 struct proto_handler proto;
44 char *config_buf;
45 char *script_name;
46 bool init_available;
47
48 struct uci_blob_param_list config;
49 };
50
51 struct proto_shell_dependency {
52 struct list_head list;
53
54 char *interface;
55 struct proto_shell_state *proto;
56 struct interface_user dep;
57
58 union if_addr host;
59 bool v6;
60 };
61
62 struct proto_shell_state {
63 struct interface_proto_state proto;
64 struct proto_shell_handler *handler;
65 struct blob_attr *config;
66
67 struct uloop_timeout teardown_timeout;
68
69 struct netifd_process script_task;
70 struct netifd_process proto_task;
71
72 enum proto_shell_sm sm;
73 bool proto_task_killed;
74 bool renew_pending;
75
76 int last_error;
77
78 struct list_head deps;
79 };
80
81 static void
82 proto_shell_check_dependencies(struct proto_shell_state *state)
83 {
84 struct proto_shell_dependency *dep;
85 bool available = true;
86
87 list_for_each_entry(dep, &state->deps, list) {
88 if (dep->dep.iface)
89 continue;
90
91 available = false;
92 break;
93 }
94
95 interface_set_available(state->proto.iface, available);
96 }
97
98 static void
99 proto_shell_if_up_cb(struct interface_user *dep, struct interface *iface,
100 enum interface_event ev);
101 static void
102 proto_shell_if_down_cb(struct interface_user *dep, struct interface *iface,
103 enum interface_event ev);
104
105 static void
106 proto_shell_update_host_dep(struct proto_shell_dependency *dep)
107 {
108 struct interface *iface = NULL;
109
110 if (dep->dep.iface)
111 goto out;
112
113 if (dep->interface[0])
114 iface = vlist_find(&interfaces, dep->interface, iface, node);
115
116 iface = interface_ip_add_target_route(&dep->host, dep->v6, iface);
117 if (!iface)
118 goto out;
119
120 interface_remove_user(&dep->dep);
121 dep->dep.cb = proto_shell_if_down_cb;
122 interface_add_user(&dep->dep, iface);
123
124 out:
125 proto_shell_check_dependencies(dep->proto);
126 }
127
128 static void
129 proto_shell_clear_host_dep(struct proto_shell_state *state)
130 {
131 struct proto_shell_dependency *dep, *tmp;
132
133 list_for_each_entry_safe(dep, tmp, &state->deps, list) {
134 interface_remove_user(&dep->dep);
135 list_del(&dep->list);
136 free(dep);
137 }
138 }
139
140 static int
141 proto_shell_handler(struct interface_proto_state *proto,
142 enum interface_proto_cmd cmd, bool force)
143 {
144 struct proto_shell_state *state;
145 struct proto_shell_handler *handler;
146 struct netifd_process *proc;
147 static char error_buf[32];
148 const char *argv[7];
149 char *envp[2];
150 const char *action;
151 char *config;
152 int ret, i = 0, j = 0;
153
154 state = container_of(proto, struct proto_shell_state, proto);
155 handler = state->handler;
156 proc = &state->script_task;
157
158 if (cmd == PROTO_CMD_SETUP) {
159 switch (state->sm) {
160 case S_IDLE:
161 action = "setup";
162 state->last_error = -1;
163 proto_shell_clear_host_dep(state);
164 state->sm = S_SETUP;
165 break;
166
167 case S_SETUP_ABORT:
168 case S_TEARDOWN:
169 case S_SETUP:
170 return 0;
171
172 default:
173 return -1;
174 }
175 } else if (cmd == PROTO_CMD_RENEW) {
176 if (!(handler->proto.flags & PROTO_FLAG_RENEW_AVAILABLE))
177 return 0;
178
179 if (state->script_task.uloop.pending) {
180 state->renew_pending = true;
181 return 0;
182 }
183
184 state->renew_pending = false;
185 action = "renew";
186 } else {
187 switch (state->sm) {
188 case S_SETUP:
189 if (state->script_task.uloop.pending) {
190 uloop_timeout_set(&state->teardown_timeout, 1000);
191 kill(state->script_task.uloop.pid, SIGTERM);
192 if (state->proto_task.uloop.pending)
193 kill(state->proto_task.uloop.pid, SIGTERM);
194 state->renew_pending = false;
195 state->sm = S_SETUP_ABORT;
196 return 0;
197 }
198 /* fall through if no script task is running */
199 case S_IDLE:
200 action = "teardown";
201 state->renew_pending = false;
202 state->sm = S_TEARDOWN;
203 if (state->last_error >= 0) {
204 snprintf(error_buf, sizeof(error_buf), "ERROR=%d", state->last_error);
205 envp[j++] = error_buf;
206 }
207 uloop_timeout_set(&state->teardown_timeout, 5000);
208 break;
209
210 case S_TEARDOWN:
211 return 0;
212
213 default:
214 return -1;
215 }
216 }
217
218 D(INTERFACE, "run %s for interface '%s'\n", action, proto->iface->name);
219 config = blobmsg_format_json(state->config, true);
220 if (!config)
221 return -1;
222
223 argv[i++] = handler->script_name;
224 argv[i++] = handler->proto.name;
225 argv[i++] = action;
226 argv[i++] = proto->iface->name;
227 argv[i++] = config;
228 if (proto->iface->main_dev.dev)
229 argv[i++] = proto->iface->main_dev.dev->ifname;
230 argv[i] = NULL;
231 envp[j] = NULL;
232
233 ret = netifd_start_process(argv, envp, proc);
234 free(config);
235
236 return ret;
237 }
238
239 static void
240 proto_shell_if_up_cb(struct interface_user *dep, struct interface *iface,
241 enum interface_event ev)
242 {
243 struct proto_shell_dependency *pdep;
244
245 if (ev != IFEV_UP && ev != IFEV_UPDATE)
246 return;
247
248 pdep = container_of(dep, struct proto_shell_dependency, dep);
249 proto_shell_update_host_dep(pdep);
250 }
251
252 static void
253 proto_shell_if_down_cb(struct interface_user *dep, struct interface *iface,
254 enum interface_event ev)
255 {
256 struct proto_shell_dependency *pdep;
257 struct proto_shell_state *state;
258
259 if (ev == IFEV_UP || ev == IFEV_UPDATE)
260 return;
261
262 pdep = container_of(dep, struct proto_shell_dependency, dep);
263 interface_remove_user(dep);
264 dep->cb = proto_shell_if_up_cb;
265 interface_add_user(dep, NULL);
266
267 state = pdep->proto;
268 if (state->sm == S_IDLE) {
269 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
270 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
271 }
272 }
273
274 static void
275 proto_shell_task_finish(struct proto_shell_state *state,
276 struct netifd_process *task)
277 {
278 switch (state->sm) {
279 case S_IDLE:
280 if (task == &state->proto_task)
281 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
282 /* fall through */
283 case S_SETUP:
284 if (task == &state->proto_task)
285 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN,
286 false);
287 if (task == &state->script_task) {
288 if (state->renew_pending)
289 proto_shell_handler(&state->proto,
290 PROTO_CMD_RENEW, false);
291 else if (!state->proto_task.uloop.pending &&
292 state->sm == S_SETUP)
293 proto_shell_handler(&state->proto,
294 PROTO_CMD_TEARDOWN,
295 false);
296 }
297 break;
298
299 case S_SETUP_ABORT:
300 if (state->script_task.uloop.pending ||
301 state->proto_task.uloop.pending)
302 break;
303
304 uloop_timeout_cancel(&state->teardown_timeout);
305 state->sm = S_IDLE;
306 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
307 break;
308
309 case S_TEARDOWN:
310 if (state->script_task.uloop.pending)
311 break;
312
313 if (state->proto_task.uloop.pending) {
314 if (!state->proto_task_killed)
315 kill(state->proto_task.uloop.pid, SIGTERM);
316 break;
317 }
318
319 uloop_timeout_cancel(&state->teardown_timeout);
320 state->sm = S_IDLE;
321 state->proto.proto_event(&state->proto, IFPEV_DOWN);
322 break;
323 }
324 }
325
326 static void
327 proto_shell_teardown_timeout_cb(struct uloop_timeout *timeout)
328 {
329 struct proto_shell_state *state;
330
331 state = container_of(timeout, struct proto_shell_state, teardown_timeout);
332
333 netifd_kill_process(&state->script_task);
334 netifd_kill_process(&state->proto_task);
335 proto_shell_task_finish(state, NULL);
336 }
337
338 static void
339 proto_shell_script_cb(struct netifd_process *p, int ret)
340 {
341 struct proto_shell_state *state;
342
343 state = container_of(p, struct proto_shell_state, script_task);
344 proto_shell_task_finish(state, p);
345 }
346
347 static void
348 proto_shell_task_cb(struct netifd_process *p, int ret)
349 {
350 struct proto_shell_state *state;
351
352 state = container_of(p, struct proto_shell_state, proto_task);
353
354 if (state->sm == S_IDLE || state->sm == S_SETUP)
355 state->last_error = WEXITSTATUS(ret);
356
357 proto_shell_task_finish(state, p);
358 }
359
360 static void
361 proto_shell_free(struct interface_proto_state *proto)
362 {
363 struct proto_shell_state *state;
364
365 state = container_of(proto, struct proto_shell_state, proto);
366 uloop_timeout_cancel(&state->teardown_timeout);
367 proto_shell_clear_host_dep(state);
368 netifd_kill_process(&state->script_task);
369 netifd_kill_process(&state->proto_task);
370 free(state->config);
371 free(state);
372 }
373
374 static void
375 proto_shell_parse_route_list(struct interface *iface, struct blob_attr *attr,
376 bool v6)
377 {
378 struct blob_attr *cur;
379 int rem;
380
381 blobmsg_for_each_attr(cur, attr, rem) {
382 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE) {
383 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur));
384 continue;
385 }
386
387 interface_ip_add_route(iface, cur, v6);
388 }
389 }
390
391 static void
392 proto_shell_parse_data(struct interface *iface, struct blob_attr *attr)
393 {
394 struct blob_attr *cur;
395 int rem;
396
397 blobmsg_for_each_attr(cur, attr, rem)
398 interface_add_data(iface, cur);
399 }
400
401 static struct device *
402 proto_shell_create_tunnel(const char *name, struct blob_attr *attr)
403 {
404 struct device *dev;
405 struct blob_buf b;
406
407 memset(&b, 0, sizeof(b));
408 blob_buf_init(&b, 0);
409 blob_put(&b, 0, blobmsg_data(attr), blobmsg_data_len(attr));
410 dev = device_create(name, &tunnel_device_type, blob_data(b.head));
411 blob_buf_free(&b);
412
413 return dev;
414 }
415
416 enum {
417 NOTIFY_ACTION,
418 NOTIFY_ERROR,
419 NOTIFY_COMMAND,
420 NOTIFY_ENV,
421 NOTIFY_SIGNAL,
422 NOTIFY_AVAILABLE,
423 NOTIFY_LINK_UP,
424 NOTIFY_IFNAME,
425 NOTIFY_ADDR_EXT,
426 NOTIFY_ROUTES,
427 NOTIFY_ROUTES6,
428 NOTIFY_TUNNEL,
429 NOTIFY_DATA,
430 NOTIFY_KEEP,
431 NOTIFY_HOST,
432 NOTIFY_DNS,
433 NOTIFY_DNS_SEARCH,
434 __NOTIFY_LAST
435 };
436
437 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
438 [NOTIFY_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_INT32 },
439 [NOTIFY_ERROR] = { .name = "error", .type = BLOBMSG_TYPE_ARRAY },
440 [NOTIFY_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_ARRAY },
441 [NOTIFY_ENV] = { .name = "env", .type = BLOBMSG_TYPE_ARRAY },
442 [NOTIFY_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
443 [NOTIFY_AVAILABLE] = { .name = "available", .type = BLOBMSG_TYPE_BOOL },
444 [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
445 [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
446 [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
447 [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
448 [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
449 [NOTIFY_TUNNEL] = { .name = "tunnel", .type = BLOBMSG_TYPE_TABLE },
450 [NOTIFY_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
451 [NOTIFY_KEEP] = { .name = "keep", .type = BLOBMSG_TYPE_BOOL },
452 [NOTIFY_HOST] = { .name = "host", .type = BLOBMSG_TYPE_STRING },
453 [NOTIFY_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
454 [NOTIFY_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
455 };
456
457 static int
458 proto_shell_update_link(struct proto_shell_state *state, struct blob_attr *data, struct blob_attr **tb)
459 {
460 struct interface *iface = state->proto.iface;
461 struct blob_attr *cur;
462 struct device *dev;
463 const char *devname;
464 int dev_create = 1;
465 bool addr_ext = false;
466 bool keep = false;
467 bool up;
468
469 if (state->sm == S_TEARDOWN || state->sm == S_SETUP_ABORT)
470 return UBUS_STATUS_PERMISSION_DENIED;
471
472 if (!tb[NOTIFY_LINK_UP])
473 return UBUS_STATUS_INVALID_ARGUMENT;
474
475 up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
476 if (!up) {
477 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
478 return 0;
479 }
480
481 if ((cur = tb[NOTIFY_KEEP]) != NULL)
482 keep = blobmsg_get_bool(cur);
483
484 if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL) {
485 addr_ext = blobmsg_get_bool(cur);
486 if (addr_ext)
487 dev_create = 2;
488 }
489
490 if (iface->state != IFS_UP || !iface->l3_dev.dev)
491 keep = false;
492
493 if (!keep) {
494 dev = iface->main_dev.dev;
495 if (tb[NOTIFY_IFNAME]) {
496 keep = false;
497 devname = blobmsg_data(tb[NOTIFY_IFNAME]);
498 if (tb[NOTIFY_TUNNEL])
499 dev = proto_shell_create_tunnel(devname, tb[NOTIFY_TUNNEL]);
500 else
501 dev = device_get(devname, dev_create);
502 }
503
504 if (!dev)
505 return UBUS_STATUS_INVALID_ARGUMENT;
506
507 interface_set_l3_dev(iface, dev);
508 if (device_claim(&iface->l3_dev) < 0)
509 return UBUS_STATUS_UNKNOWN_ERROR;
510
511 device_set_present(dev, true);
512
513 interface_update_start(iface);
514 }
515
516 proto_apply_ip_settings(iface, data, addr_ext);
517
518 if ((cur = tb[NOTIFY_ROUTES]) != NULL)
519 proto_shell_parse_route_list(state->proto.iface, cur, false);
520
521 if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
522 proto_shell_parse_route_list(state->proto.iface, cur, true);
523
524 if ((cur = tb[NOTIFY_DNS]))
525 interface_add_dns_server_list(&iface->proto_ip, cur);
526
527 if ((cur = tb[NOTIFY_DNS_SEARCH]))
528 interface_add_dns_search_list(&iface->proto_ip, cur);
529
530 if ((cur = tb[NOTIFY_DATA]))
531 proto_shell_parse_data(state->proto.iface, cur);
532
533 interface_update_complete(state->proto.iface);
534
535 if ((state->sm != S_SETUP_ABORT) && (state->sm != S_TEARDOWN)) {
536 if (!keep)
537 state->proto.proto_event(&state->proto, IFPEV_UP);
538 state->sm = S_IDLE;
539 }
540
541 return 0;
542 }
543
544 static bool
545 fill_string_list(struct blob_attr *attr, char **argv, int max)
546 {
547 struct blob_attr *cur;
548 int argc = 0;
549 int rem;
550
551 if (!attr)
552 goto out;
553
554 blobmsg_for_each_attr(cur, attr, rem) {
555 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
556 return false;
557
558 if (!blobmsg_check_attr(cur, NULL))
559 return false;
560
561 argv[argc++] = blobmsg_data(cur);
562 if (argc == max - 1)
563 return false;
564 }
565
566 out:
567 argv[argc] = NULL;
568 return true;
569 }
570
571 static int
572 proto_shell_run_command(struct proto_shell_state *state, struct blob_attr **tb)
573 {
574 static char *argv[64];
575 static char *env[32];
576
577 if (state->sm == S_TEARDOWN || state->sm == S_SETUP_ABORT)
578 return UBUS_STATUS_PERMISSION_DENIED;
579
580 if (!tb[NOTIFY_COMMAND])
581 goto error;
582
583 if (!fill_string_list(tb[NOTIFY_COMMAND], argv, ARRAY_SIZE(argv)))
584 goto error;
585
586 if (!fill_string_list(tb[NOTIFY_ENV], env, ARRAY_SIZE(env)))
587 goto error;
588
589 netifd_start_process((const char **) argv, (char **) env, &state->proto_task);
590
591 return 0;
592
593 error:
594 return UBUS_STATUS_INVALID_ARGUMENT;
595 }
596
597 static int
598 proto_shell_kill_command(struct proto_shell_state *state, struct blob_attr **tb)
599 {
600 unsigned int signal = ~0;
601
602 if (tb[NOTIFY_SIGNAL])
603 signal = blobmsg_get_u32(tb[NOTIFY_SIGNAL]);
604
605 if (signal > 31)
606 signal = SIGTERM;
607
608 if (state->proto_task.uloop.pending) {
609 if (signal == SIGTERM || signal == SIGKILL)
610 state->proto_task_killed = true;
611 kill(state->proto_task.uloop.pid, signal);
612 }
613
614 return 0;
615 }
616
617 static int
618 proto_shell_notify_error(struct proto_shell_state *state, struct blob_attr **tb)
619 {
620 struct blob_attr *cur;
621 char *data[16];
622 int n_data = 0;
623 int rem;
624
625 if (!tb[NOTIFY_ERROR])
626 return UBUS_STATUS_INVALID_ARGUMENT;
627
628 blobmsg_for_each_attr(cur, tb[NOTIFY_ERROR], rem) {
629 if (n_data + 1 == ARRAY_SIZE(data))
630 goto error;
631
632 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
633 goto error;
634
635 if (!blobmsg_check_attr(cur, NULL))
636 goto error;
637
638 data[n_data++] = blobmsg_data(cur);
639 }
640
641 if (!n_data)
642 goto error;
643
644 interface_add_error(state->proto.iface, state->handler->proto.name,
645 data[0], (const char **) &data[1], n_data - 1);
646
647 return 0;
648
649 error:
650 return UBUS_STATUS_INVALID_ARGUMENT;
651 }
652
653 static int
654 proto_shell_block_restart(struct proto_shell_state *state, struct blob_attr **tb)
655 {
656 state->proto.iface->autostart = false;
657 return 0;
658 }
659
660 static int
661 proto_shell_set_available(struct proto_shell_state *state, struct blob_attr **tb)
662 {
663 if (!tb[NOTIFY_AVAILABLE])
664 return UBUS_STATUS_INVALID_ARGUMENT;
665
666 interface_set_available(state->proto.iface, blobmsg_get_bool(tb[NOTIFY_AVAILABLE]));
667 return 0;
668 }
669
670 static int
671 proto_shell_add_host_dependency(struct proto_shell_state *state, struct blob_attr **tb)
672 {
673 struct proto_shell_dependency *dep;
674 struct blob_attr *host = tb[NOTIFY_HOST];
675 struct blob_attr *ifname_a = tb[NOTIFY_IFNAME];
676 const char *ifname_str = ifname_a ? blobmsg_data(ifname_a) : "";
677 char *ifname;
678
679 if (state->sm == S_TEARDOWN || state->sm == S_SETUP_ABORT)
680 return UBUS_STATUS_PERMISSION_DENIED;
681
682 if (!host)
683 return UBUS_STATUS_INVALID_ARGUMENT;
684
685 dep = calloc_a(sizeof(*dep), &ifname, strlen(ifname_str) + 1);
686 if (inet_pton(AF_INET, blobmsg_data(host), &dep->host) < 1) {
687 if (inet_pton(AF_INET6, blobmsg_data(host), &dep->host) < 1) {
688 free(dep);
689 return UBUS_STATUS_INVALID_ARGUMENT;
690 } else {
691 dep->v6 = true;
692 }
693 }
694
695 dep->proto = state;
696 dep->interface = strcpy(ifname, ifname_str);
697
698 dep->dep.cb = proto_shell_if_up_cb;
699 interface_add_user(&dep->dep, NULL);
700 list_add(&dep->list, &state->deps);
701 proto_shell_update_host_dep(dep);
702 if (!dep->dep.iface)
703 return UBUS_STATUS_NOT_FOUND;
704
705 return 0;
706 }
707
708 static int
709 proto_shell_setup_failed(struct proto_shell_state *state)
710 {
711 int ret = 0;
712
713 switch (state->sm) {
714 case S_IDLE:
715 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
716 /* fall through */
717 case S_SETUP:
718 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
719 break;
720 case S_SETUP_ABORT:
721 case S_TEARDOWN:
722 default:
723 ret = UBUS_STATUS_PERMISSION_DENIED;
724 break;
725 }
726 return ret;
727 }
728
729 static int
730 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
731 {
732 struct proto_shell_state *state;
733 struct blob_attr *tb[__NOTIFY_LAST];
734
735 state = container_of(proto, struct proto_shell_state, proto);
736
737 blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
738 if (!tb[NOTIFY_ACTION])
739 return UBUS_STATUS_INVALID_ARGUMENT;
740
741 switch(blobmsg_get_u32(tb[NOTIFY_ACTION])) {
742 case 0:
743 return proto_shell_update_link(state, attr, tb);
744 case 1:
745 return proto_shell_run_command(state, tb);
746 case 2:
747 return proto_shell_kill_command(state, tb);
748 case 3:
749 return proto_shell_notify_error(state, tb);
750 case 4:
751 return proto_shell_block_restart(state, tb);
752 case 5:
753 return proto_shell_set_available(state, tb);
754 case 6:
755 return proto_shell_add_host_dependency(state, tb);
756 case 7:
757 return proto_shell_setup_failed(state);
758 default:
759 return UBUS_STATUS_INVALID_ARGUMENT;
760 }
761 }
762
763 static struct interface_proto_state *
764 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
765 struct blob_attr *attr)
766 {
767 struct proto_shell_state *state;
768
769 state = calloc(1, sizeof(*state));
770 INIT_LIST_HEAD(&state->deps);
771
772 state->config = malloc(blob_pad_len(attr));
773 if (!state->config)
774 goto error;
775
776 memcpy(state->config, attr, blob_pad_len(attr));
777 state->proto.free = proto_shell_free;
778 state->proto.notify = proto_shell_notify;
779 state->proto.cb = proto_shell_handler;
780 state->teardown_timeout.cb = proto_shell_teardown_timeout_cb;
781 state->script_task.cb = proto_shell_script_cb;
782 state->script_task.dir_fd = proto_fd;
783 state->script_task.log_prefix = iface->name;
784 state->proto_task.cb = proto_shell_task_cb;
785 state->proto_task.dir_fd = proto_fd;
786 state->proto_task.log_prefix = iface->name;
787 state->handler = container_of(h, struct proto_shell_handler, proto);
788
789 return &state->proto;
790
791 error:
792 free(state);
793 return NULL;
794 }
795
796 static void
797 proto_shell_add_handler(const char *script, const char *name, json_object *obj)
798 {
799 struct proto_shell_handler *handler;
800 struct proto_handler *proto;
801 json_object *config, *tmp;
802 char *proto_name, *script_name;
803
804 handler = calloc_a(sizeof(*handler),
805 &proto_name, strlen(name) + 1,
806 &script_name, strlen(script) + 1);
807 if (!handler)
808 return;
809
810 handler->script_name = strcpy(script_name, script);
811
812 proto = &handler->proto;
813 proto->name = strcpy(proto_name, name);
814 proto->config_params = &handler->config;
815 proto->attach = proto_shell_attach;
816
817 tmp = json_get_field(obj, "no-device", json_type_boolean);
818 if (tmp && json_object_get_boolean(tmp))
819 handler->proto.flags |= PROTO_FLAG_NODEV;
820
821 tmp = json_get_field(obj, "available", json_type_boolean);
822 if (tmp && json_object_get_boolean(tmp))
823 handler->proto.flags |= PROTO_FLAG_INIT_AVAILABLE;
824
825 tmp = json_get_field(obj, "renew-handler", json_type_boolean);
826 if (tmp && json_object_get_boolean(tmp))
827 handler->proto.flags |= PROTO_FLAG_RENEW_AVAILABLE;
828
829 tmp = json_get_field(obj, "lasterror", json_type_boolean);
830 if (tmp && json_object_get_boolean(tmp))
831 handler->proto.flags |= PROTO_FLAG_LASTERROR;
832
833 config = json_get_field(obj, "config", json_type_array);
834 if (config)
835 handler->config_buf = netifd_handler_parse_config(&handler->config, config);
836
837 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
838 add_proto_handler(proto);
839 }
840
841 void proto_shell_init(void)
842 {
843 proto_fd = netifd_open_subdir("proto");
844 if (proto_fd < 0)
845 return;
846
847 netifd_init_script_handlers(proto_fd, proto_shell_add_handler);
848 }