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