2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
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.
16 #include <sys/socket.h>
17 #include <sys/types.h>
18 #include <sys/sysmacros.h>
20 #include <linux/types.h>
21 #include <linux/netlink.h>
23 #include <libubox/blobmsg_json.h>
24 #include <libubox/json_script.h>
25 #include <libubox/uloop.h>
26 #include <json-c/json.h>
39 #define HOTPLUG_WAIT 500
43 struct list_head list;
45 struct blob_attr *msg;
46 struct blob_attr *data;
49 void (*handler)(struct blob_attr *msg, struct blob_attr *data);
50 void (*start)(struct blob_attr *msg, struct blob_attr *data);
51 void (*complete)(struct blob_attr *msg, struct blob_attr *data, int ret);
54 struct button_timeout {
55 struct list_head list;
56 struct uloop_timeout timeout;
59 struct blob_attr *data;
62 static LIST_HEAD(cmd_queue);
63 static LIST_HEAD(button_timer);
64 static struct uloop_process queue_proc;
65 static struct uloop_timeout last_event;
66 static struct blob_buf b, button_buf;
67 static char *rule_file;
68 static struct blob_buf script;
69 static struct cmd_queue *current;
71 static void queue_add(struct cmd_handler *h, struct blob_attr *msg, struct blob_attr *data);
72 static void handle_button_complete(struct blob_attr *msg, struct blob_attr *data, int ret);
74 static void button_free(struct button_timeout *b)
76 uloop_timeout_cancel(&b->timeout);
83 static void button_timeout_remove(char *button)
85 struct button_timeout *b, *c;
87 if (!list_empty(&button_timer)) list_for_each_entry_safe(b, c, &button_timer, list) {
88 if (!strcmp(b->name, button))
93 static char *hotplug_msg_find_var(struct blob_attr *msg, const char *name)
95 struct blob_attr *cur;
98 blobmsg_for_each_attr(cur, msg, rem) {
99 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
102 if (strcmp(blobmsg_name(cur), name) != 0)
105 return blobmsg_data(cur);
111 static void mkdir_p(char *dir)
113 char *l = strrchr(dir, '/');
123 static void chgrp_error(const char *group, const char *target, const char *failed)
125 ERROR("cannot set group %s for %s (%s: %d)\n",
126 group, target, failed, errno);
129 static void chgrp_target(struct blob_attr *bgroup, struct blob_attr *btarget)
132 struct group *g = NULL;
133 const char *group = blobmsg_get_string(bgroup);
134 const char *target = blobmsg_get_string(btarget);
140 return chgrp_error(group, target, "getgrnam");
142 ret = chown(target, 0, g->gr_gid);
144 return chgrp_error(group, target, "chown");
147 static void handle_makedev(struct blob_attr *msg, struct blob_attr *data)
149 unsigned int oldumask = umask(0);
150 static struct blobmsg_policy mkdev_policy[3] = {
151 { .type = BLOBMSG_TYPE_STRING },
152 { .type = BLOBMSG_TYPE_STRING },
153 { .type = BLOBMSG_TYPE_STRING },
155 struct blob_attr *tb[3];
156 char *minor = hotplug_msg_find_var(msg, "MINOR");
157 char *major = hotplug_msg_find_var(msg, "MAJOR");
158 char *subsystem = hotplug_msg_find_var(msg, "SUBSYSTEM");
160 blobmsg_parse_array(mkdev_policy, 3, tb, blobmsg_data(data), blobmsg_data_len(data));
161 if (tb[0] && tb[1] && minor && major && subsystem) {
163 char *d = strdup(blobmsg_get_string(tb[0]));
169 if (!strcmp(subsystem, "block"))
171 mknod(blobmsg_get_string(tb[0]),
172 m | strtoul(blobmsg_data(tb[1]), NULL, 8),
173 makedev(atoi(major), atoi(minor)));
175 chgrp_target(tb[2], tb[0]);
180 static void handle_rm(struct blob_attr *msg, struct blob_attr *data)
182 static struct blobmsg_policy rm_policy = {
183 .type = BLOBMSG_TYPE_STRING,
185 struct blob_attr *tb;
187 blobmsg_parse_array(&rm_policy, 1, &tb, blobmsg_data(data), blobmsg_data_len(data));
189 unlink(blobmsg_data(tb));
192 static void handle_exec(struct blob_attr *msg, struct blob_attr *data)
195 struct blob_attr *cur;
199 blobmsg_for_each_attr(cur, msg, rem)
200 setenv(blobmsg_name(cur), blobmsg_data(cur), 1);
202 blobmsg_for_each_attr(cur, data, rem) {
203 argv[i] = blobmsg_data(cur);
210 fd = open("/dev/null", O_RDWR);
212 dup2(fd, STDIN_FILENO);
213 dup2(fd, STDOUT_FILENO);
214 dup2(fd, STDERR_FILENO);
215 if (fd > STDERR_FILENO)
222 execvp(argv[0], &argv[0]);
227 static void handle_button_start(struct blob_attr *msg, struct blob_attr *data)
229 char *button = hotplug_msg_find_var(msg, "BUTTON");
232 button_timeout_remove(button);
235 static void handle_firmware(struct blob_attr *msg, struct blob_attr *data)
237 char *dir = blobmsg_get_string(blobmsg_data(data));
238 char *file = hotplug_msg_find_var(msg, "FIRMWARE");
239 char *dev = hotplug_msg_find_var(msg, "DEVPATH");
240 struct stat s = { 0 };
241 char *path, loadpath[256], syspath[256];
242 int fw, src, load, len;
243 static char buf[4096];
245 DEBUG(2, "Firmware request for %s/%s\n", dir, file);
247 if (!file || !dir || !dev) {
248 ERROR("Request for unknown firmware %s/%s\n", dir, file);
252 path = alloca(strlen(dir) + strlen(file) + 2);
253 sprintf(path, "%s/%s", dir, file);
255 if (stat(path, &s)) {
256 ERROR("Could not find firmware %s: %m\n", path);
262 src = open(path, O_RDONLY);
264 ERROR("Failed to open %s: %m\n", path);
270 snprintf(loadpath, sizeof(loadpath), "/sys/%s/loading", dev);
271 load = open(loadpath, O_WRONLY);
273 ERROR("Failed to open %s: %m\n", loadpath);
276 if (write(load, "1", 1) == -1) {
277 ERROR("Failed to write to %s: %m\n", loadpath);
282 snprintf(syspath, sizeof(syspath), "/sys/%s/data", dev);
283 fw = open(syspath, O_WRONLY);
285 ERROR("Failed to open %s: %m\n", syspath);
291 len = read(src, buf, sizeof(buf));
295 if (write(fw, buf, len) == -1) {
296 ERROR("failed to write firmware file %s/%s to %s: %m\n", dir, file, dev);
305 load = open(loadpath, O_WRONLY);
306 if (write(load, "0", 1) == -1)
307 ERROR("failed to write to %s: %m\n", loadpath);
310 DEBUG(2, "Done loading %s\n", path);
315 static void handle_start_console(struct blob_attr *msg, struct blob_attr *data)
317 char *dev = blobmsg_get_string(blobmsg_data(data));
319 DEBUG(2, "Start console request for %s\n", dev);
321 procd_inittab_run("respawn");
322 procd_inittab_run("askfirst");
324 DEBUG(2, "Done starting console for %s\n", dev);
335 HANDLER_START_CONSOLE,
338 static struct cmd_handler {
341 void (*handler)(struct blob_attr *msg, struct blob_attr *data);
342 void (*start)(struct blob_attr *msg, struct blob_attr *data);
343 void (*complete)(struct blob_attr *msg, struct blob_attr *data, int ret);
348 .handler = handle_makedev,
353 .handler = handle_rm,
357 .handler = handle_exec,
361 .handler = handle_exec,
362 .start = handle_button_start,
363 .complete = handle_button_complete,
366 .name = "load-firmware",
367 .handler = handle_firmware,
369 [HANDLER_START_CONSOLE] = {
370 .name = "start-console",
371 .handler = handle_start_console,
375 static void queue_next(void)
379 if (queue_proc.pending || list_empty(&cmd_queue))
382 c = list_first_entry(&cmd_queue, struct cmd_queue, list);
384 queue_proc.pid = fork();
385 if (!queue_proc.pid) {
387 c->handler(c->msg, c->data);
391 c->start(c->msg, c->data);
397 if (queue_proc.pid <= 0) {
402 uloop_process_add(&queue_proc);
404 DEBUG(4, "Launched hotplug exec instance, pid=%d\n", (int) queue_proc.pid);
407 static void queue_proc_cb(struct uloop_process *c, int ret)
409 DEBUG(4, "Finished hotplug exec instance, pid=%d\n", (int) c->pid);
412 current->complete(current->msg, current->data, ret);
419 static void queue_add(struct cmd_handler *h, struct blob_attr *msg, struct blob_attr *data)
421 struct cmd_queue *c = NULL;
422 struct blob_attr *_msg, *_data;
424 c = calloc_a(sizeof(struct cmd_queue),
425 &_msg, blob_pad_len(msg),
426 &_data, blob_pad_len(data),
435 memcpy(c->msg, msg, blob_pad_len(msg));
436 memcpy(c->data, data, blob_pad_len(data));
437 c->handler = h->handler;
438 c->complete = h->complete;
440 list_add_tail(&c->list, &cmd_queue);
444 static void handle_button_timeout(struct uloop_timeout *t)
446 struct button_timeout *b;
449 b = container_of(t, struct button_timeout, timeout);
450 blob_buf_init(&button_buf, 0);
451 blobmsg_add_string(&button_buf, "BUTTON", b->name);
452 blobmsg_add_string(&button_buf, "ACTION", "timeout");
453 snprintf(seen, sizeof(seen), "%d", b->seen);
454 blobmsg_add_string(&button_buf, "SEEN", seen);
455 queue_add(&handlers[HANDLER_EXEC], button_buf.head, b->data);
459 static void handle_button_complete(struct blob_attr *msg, struct blob_attr *data, int ret)
461 char *name = hotplug_msg_find_var(msg, "BUTTON");
462 struct button_timeout *b;
463 int timeout = ret >> 8;
471 b = calloc(1, sizeof(*b));
475 b->data = malloc(blob_pad_len(data));
476 b->name = strdup(name);
479 memcpy(b->data, data, blob_pad_len(data));
480 b->timeout.cb = handle_button_timeout;
482 uloop_timeout_set(&b->timeout, timeout * 1000);
483 list_add(&b->list, &button_timer);
486 static const char* rule_handle_var(struct json_script_ctx *ctx, const char *name, struct blob_attr *vars)
488 const char *str, *sep;
490 if (!strcmp(name, "DEVICENAME") || !strcmp(name, "DEVNAME")) {
491 str = json_script_find_var(ctx, vars, "DEVPATH");
495 sep = strrchr(str, '/');
505 static struct json_script_file *
506 rule_handle_file(struct json_script_ctx *ctx, const char *name)
510 obj = json_object_from_file((char*)name);
514 blob_buf_init(&script, 0);
515 blobmsg_add_json_element(&script, "", obj);
517 return json_script_file_from_blobmsg(name, blob_data(script.head), blob_len(script.head));
520 static void rule_handle_command(struct json_script_ctx *ctx, const char *name,
521 struct blob_attr *data, struct blob_attr *vars)
523 struct blob_attr *cur;
527 DEBUG(4, "Command: %s\n", name);
528 blobmsg_for_each_attr(cur, data, rem)
529 DEBUG(4, " %s\n", (char *) blobmsg_data(cur));
531 DEBUG(4, "Message:\n");
532 blobmsg_for_each_attr(cur, vars, rem)
533 DEBUG(4, " %s=%s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
536 for (i = 0; i < ARRAY_SIZE(handlers); i++)
537 if (!strcmp(handlers[i].name, name)) {
538 if (handlers[i].atomic)
539 handlers[i].handler(vars, data);
541 queue_add(&handlers[i], vars, data);
546 uloop_timeout_set(&last_event, HOTPLUG_WAIT);
549 static void rule_handle_error(struct json_script_ctx *ctx, const char *msg,
550 struct blob_attr *context)
554 s = blobmsg_format_json(context, false);
555 ERROR("ERROR: %s in block: %s\n", msg, s);
559 static struct json_script_ctx jctx = {
560 .handle_var = rule_handle_var,
561 .handle_error = rule_handle_error,
562 .handle_command = rule_handle_command,
563 .handle_file = rule_handle_file,
566 static void hotplug_handler_debug(struct blob_attr *data)
573 str = blobmsg_format_json(data, true);
574 DEBUG(3, "%s\n", str);
578 static void hotplug_handler(struct uloop_fd *u, unsigned int ev)
581 static char buf[4096];
582 int len = recv(u->fd, buf, sizeof(buf) - 1, MSG_DONTWAIT);
589 blob_buf_init(&b, 0);
590 index = blobmsg_open_table(&b, NULL);
592 int l = strlen(buf + i) + 1;
593 char *e = strstr(&buf[i], "=");
597 blobmsg_add_string(&b, &buf[i], &e[1]);
601 blobmsg_close_table(&b, index);
602 hotplug_handler_debug(b.head);
603 json_script_run(&jctx, rule_file, blob_data(b.head));
606 static struct uloop_fd hotplug_fd = {
607 .cb = hotplug_handler,
610 void hotplug_last_event(uloop_timeout_handler handler)
612 last_event.cb = handler;
614 uloop_timeout_set(&last_event, HOTPLUG_WAIT);
616 uloop_timeout_cancel(&last_event);
619 void hotplug(char *rules)
621 struct sockaddr_nl nls = {};
622 int nlbufsize = 512 * 1024;
624 rule_file = strdup(rules);
625 nls.nl_family = AF_NETLINK;
626 nls.nl_pid = getpid();
629 if ((hotplug_fd.fd = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT)) == -1) {
630 ERROR("Failed to open hotplug socket: %m\n");
633 if (bind(hotplug_fd.fd, (void *)&nls, sizeof(struct sockaddr_nl))) {
634 ERROR("Failed to bind hotplug socket: %m\n");
638 if (setsockopt(hotplug_fd.fd, SOL_SOCKET, SO_RCVBUFFORCE, &nlbufsize, sizeof(nlbufsize)))
639 ERROR("Failed to resize receive buffer: %m\n");
641 json_script_init(&jctx);
642 queue_proc.cb = queue_proc_cb;
643 uloop_fd_add(&hotplug_fd, ULOOP_READ);
646 int hotplug_run(char *rules)
655 void hotplug_shutdown(void)
657 uloop_fd_delete(&hotplug_fd);
658 close(hotplug_fd.fd);