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