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