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>
19 #include <linux/types.h>
20 #include <linux/netlink.h>
22 #include <libubox/blobmsg_json.h>
23 #include <libubox/json_script.h>
24 #include <libubox/uloop.h>
25 #include <json-c/json.h>
37 #define HOTPLUG_WAIT 500
41 struct list_head list;
43 struct blob_attr *msg;
44 struct blob_attr *data;
47 void (*handler)(struct blob_attr *msg, struct blob_attr *data);
48 void (*start)(struct blob_attr *msg, struct blob_attr *data);
49 void (*complete)(struct blob_attr *msg, struct blob_attr *data, int ret);
52 struct button_timeout {
53 struct list_head list;
54 struct uloop_timeout timeout;
57 struct blob_attr *data;
60 static LIST_HEAD(cmd_queue);
61 static LIST_HEAD(button_timer);
62 static struct uloop_process queue_proc;
63 static struct uloop_timeout last_event;
64 static struct blob_buf b, button_buf;
65 static char *rule_file;
66 static struct blob_buf script;
67 static struct cmd_queue *current;
69 static void queue_add(struct cmd_handler *h, struct blob_attr *msg, struct blob_attr *data);
70 static void handle_button_complete(struct blob_attr *msg, struct blob_attr *data, int ret);
72 static void button_free(struct button_timeout *b)
74 uloop_timeout_cancel(&b->timeout);
81 static void button_timeout_remove(char *button)
83 struct button_timeout *b, *c;
85 if (!list_empty(&button_timer)) list_for_each_entry_safe(b, c, &button_timer, list) {
86 if (!strcmp(b->name, button))
91 static char *hotplug_msg_find_var(struct blob_attr *msg, const char *name)
93 struct blob_attr *cur;
96 blobmsg_for_each_attr(cur, msg, rem) {
97 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
100 if (strcmp(blobmsg_name(cur), name) != 0)
103 return blobmsg_data(cur);
109 static void mkdir_p(char *dir)
111 char *l = strrchr(dir, '/');
121 static void handle_makedev(struct blob_attr *msg, struct blob_attr *data)
123 unsigned int oldumask = umask(0);
124 static struct blobmsg_policy mkdev_policy[3] = {
125 { .type = BLOBMSG_TYPE_STRING },
126 { .type = BLOBMSG_TYPE_STRING },
127 { .type = BLOBMSG_TYPE_STRING },
129 struct blob_attr *tb[3];
130 char *minor = hotplug_msg_find_var(msg, "MINOR");
131 char *major = hotplug_msg_find_var(msg, "MAJOR");
132 char *subsystem = hotplug_msg_find_var(msg, "SUBSYSTEM");
134 blobmsg_parse_array(mkdev_policy, 3, tb, blobmsg_data(data), blobmsg_data_len(data));
135 if (tb[0] && tb[1] && minor && major && subsystem) {
137 char *d = strdup(blobmsg_get_string(tb[0]));
143 if (!strcmp(subsystem, "block"))
145 mknod(blobmsg_get_string(tb[0]),
146 m | strtoul(blobmsg_data(tb[1]), NULL, 8),
147 makedev(atoi(major), atoi(minor)));
149 struct group *g = getgrnam(blobmsg_get_string(tb[2]));
152 chown(blobmsg_get_string(tb[0]), 0, g->gr_gid);
154 ERROR("cannot set group %s for %s\n",
155 blobmsg_get_string(tb[2]),
156 blobmsg_get_string(tb[0]));
162 static void handle_rm(struct blob_attr *msg, struct blob_attr *data)
164 static struct blobmsg_policy rm_policy = {
165 .type = BLOBMSG_TYPE_STRING,
167 struct blob_attr *tb;
169 blobmsg_parse_array(&rm_policy, 1, &tb, blobmsg_data(data), blobmsg_data_len(data));
171 unlink(blobmsg_data(tb));
174 static void handle_exec(struct blob_attr *msg, struct blob_attr *data)
177 struct blob_attr *cur;
181 blobmsg_for_each_attr(cur, msg, rem)
182 setenv(blobmsg_name(cur), blobmsg_data(cur), 1);
184 blobmsg_for_each_attr(cur, data, rem) {
185 argv[i] = blobmsg_data(cur);
192 fd = open("/dev/null", O_RDWR);
194 dup2(fd, STDIN_FILENO);
195 dup2(fd, STDOUT_FILENO);
196 dup2(fd, STDERR_FILENO);
197 if (fd > STDERR_FILENO)
204 execvp(argv[0], &argv[0]);
209 static void handle_button_start(struct blob_attr *msg, struct blob_attr *data)
211 char *button = hotplug_msg_find_var(msg, "BUTTON");
214 button_timeout_remove(button);
217 static void handle_firmware(struct blob_attr *msg, struct blob_attr *data)
219 char *dir = blobmsg_get_string(blobmsg_data(data));
220 char *file = hotplug_msg_find_var(msg, "FIRMWARE");
221 char *dev = hotplug_msg_find_var(msg, "DEVPATH");
222 struct stat s = { 0 };
223 char *path, loadpath[256], syspath[256];
224 int fw, src, load, len;
225 static char buf[4096];
227 DEBUG(2, "Firmware request for %s/%s\n", dir, file);
229 if (!file || !dir || !dev) {
230 ERROR("Request for unknown firmware %s/%s\n", dir, file);
234 path = alloca(strlen(dir) + strlen(file) + 2);
235 sprintf(path, "%s/%s", dir, file);
237 if (stat(path, &s)) {
238 ERROR("Could not find firmware %s\n", path);
244 src = open(path, O_RDONLY);
246 ERROR("Failed to open %s\n", path);
252 snprintf(loadpath, sizeof(loadpath), "/sys/%s/loading", dev);
253 load = open(loadpath, O_WRONLY);
255 ERROR("Failed to open %s\n", loadpath);
258 if (write(load, "1", 1) == -1) {
259 ERROR("Failed to write to %s\n", loadpath);
264 snprintf(syspath, sizeof(syspath), "/sys/%s/data", dev);
265 fw = open(syspath, O_WRONLY);
267 ERROR("Failed to open %s\n", syspath);
273 len = read(src, buf, sizeof(buf));
277 if (write(fw, buf, len) == -1) {
278 ERROR("failed to write firmware file %s/%s to %s\n", dir, file, dev);
287 load = open(loadpath, O_WRONLY);
288 if (write(load, "0", 1) == -1)
289 ERROR("failed to write to %s\n", loadpath);
292 DEBUG(2, "Done loading %s\n", path);
305 static struct cmd_handler {
308 void (*handler)(struct blob_attr *msg, struct blob_attr *data);
309 void (*start)(struct blob_attr *msg, struct blob_attr *data);
310 void (*complete)(struct blob_attr *msg, struct blob_attr *data, int ret);
315 .handler = handle_makedev,
320 .handler = handle_rm,
324 .handler = handle_exec,
328 .handler = handle_exec,
329 .start = handle_button_start,
330 .complete = handle_button_complete,
333 .name = "load-firmware",
334 .handler = handle_firmware,
338 static void queue_next(void)
342 if (queue_proc.pending || list_empty(&cmd_queue))
345 c = list_first_entry(&cmd_queue, struct cmd_queue, list);
347 queue_proc.pid = fork();
348 if (!queue_proc.pid) {
350 c->handler(c->msg, c->data);
354 c->start(c->msg, c->data);
360 if (queue_proc.pid <= 0) {
365 uloop_process_add(&queue_proc);
367 DEBUG(4, "Launched hotplug exec instance, pid=%d\n", (int) queue_proc.pid);
370 static void queue_proc_cb(struct uloop_process *c, int ret)
372 DEBUG(4, "Finished hotplug exec instance, pid=%d\n", (int) c->pid);
375 current->complete(current->msg, current->data, ret);
382 static void queue_add(struct cmd_handler *h, struct blob_attr *msg, struct blob_attr *data)
384 struct cmd_queue *c = NULL;
385 struct blob_attr *_msg, *_data;
387 c = calloc_a(sizeof(struct cmd_queue),
388 &_msg, blob_pad_len(msg),
389 &_data, blob_pad_len(data),
398 memcpy(c->msg, msg, blob_pad_len(msg));
399 memcpy(c->data, data, blob_pad_len(data));
400 c->handler = h->handler;
401 c->complete = h->complete;
403 list_add_tail(&c->list, &cmd_queue);
407 static void handle_button_timeout(struct uloop_timeout *t)
409 struct button_timeout *b;
412 b = container_of(t, struct button_timeout, timeout);
413 blob_buf_init(&button_buf, 0);
414 blobmsg_add_string(&button_buf, "BUTTON", b->name);
415 blobmsg_add_string(&button_buf, "ACTION", "timeout");
416 snprintf(seen, sizeof(seen), "%d", b->seen);
417 blobmsg_add_string(&button_buf, "SEEN", seen);
418 queue_add(&handlers[HANDLER_EXEC], button_buf.head, b->data);
422 static void handle_button_complete(struct blob_attr *msg, struct blob_attr *data, int ret)
424 char *name = hotplug_msg_find_var(msg, "BUTTON");
425 struct button_timeout *b;
426 int timeout = ret >> 8;
431 b = malloc(sizeof(*b));
435 memset(b, 0, sizeof(*b));
437 b->data = malloc(blob_pad_len(data));
438 b->name = strdup(name);
441 memcpy(b->data, data, blob_pad_len(data));
442 b->timeout.cb = handle_button_timeout;
444 uloop_timeout_set(&b->timeout, timeout * 1000);
445 list_add(&b->list, &button_timer);
448 static const char* rule_handle_var(struct json_script_ctx *ctx, const char *name, struct blob_attr *vars)
450 const char *str, *sep;
452 if (!strcmp(name, "DEVICENAME") || !strcmp(name, "DEVNAME")) {
453 str = json_script_find_var(ctx, vars, "DEVPATH");
457 sep = strrchr(str, '/');
467 static struct json_script_file *
468 rule_handle_file(struct json_script_ctx *ctx, const char *name)
472 obj = json_object_from_file((char*)name);
476 blob_buf_init(&script, 0);
477 blobmsg_add_json_element(&script, "", obj);
479 return json_script_file_from_blobmsg(name, blob_data(script.head), blob_len(script.head));
482 static void rule_handle_command(struct json_script_ctx *ctx, const char *name,
483 struct blob_attr *data, struct blob_attr *vars)
485 struct blob_attr *cur;
489 DEBUG(4, "Command: %s", name);
490 blobmsg_for_each_attr(cur, data, rem)
491 DEBUG(4, " %s", (char *) blobmsg_data(cur));
494 DEBUG(4, "Message:");
495 blobmsg_for_each_attr(cur, vars, rem)
496 DEBUG(4, " %s=%s", blobmsg_name(cur), (char *) blobmsg_data(cur));
500 for (i = 0; i < ARRAY_SIZE(handlers); i++)
501 if (!strcmp(handlers[i].name, name)) {
502 if (handlers[i].atomic)
503 handlers[i].handler(vars, data);
505 queue_add(&handlers[i], vars, data);
510 uloop_timeout_set(&last_event, HOTPLUG_WAIT);
513 static void rule_handle_error(struct json_script_ctx *ctx, const char *msg,
514 struct blob_attr *context)
518 s = blobmsg_format_json(context, false);
519 ERROR("ERROR: %s in block: %s\n", msg, s);
523 static struct json_script_ctx jctx = {
524 .handle_var = rule_handle_var,
525 .handle_error = rule_handle_error,
526 .handle_command = rule_handle_command,
527 .handle_file = rule_handle_file,
530 static void hotplug_handler_debug(struct blob_attr *data)
537 str = blobmsg_format_json(data, true);
538 DEBUG(3, "%s\n", str);
542 static void hotplug_handler(struct uloop_fd *u, unsigned int ev)
545 static char buf[4096];
546 int len = recv(u->fd, buf, sizeof(buf), MSG_DONTWAIT);
551 blob_buf_init(&b, 0);
552 index = blobmsg_open_table(&b, NULL);
554 int l = strlen(buf + i) + 1;
555 char *e = strstr(&buf[i], "=");
559 blobmsg_add_string(&b, &buf[i], &e[1]);
563 blobmsg_close_table(&b, index);
564 hotplug_handler_debug(b.head);
565 json_script_run(&jctx, rule_file, blob_data(b.head));
568 static struct uloop_fd hotplug_fd = {
569 .cb = hotplug_handler,
572 void hotplug_last_event(uloop_timeout_handler handler)
574 last_event.cb = handler;
576 uloop_timeout_set(&last_event, HOTPLUG_WAIT);
578 uloop_timeout_cancel(&last_event);
581 void hotplug(char *rules)
583 struct sockaddr_nl nls;
584 int nlbufsize = 512 * 1024;
586 rule_file = strdup(rules);
587 memset(&nls,0,sizeof(struct sockaddr_nl));
588 nls.nl_family = AF_NETLINK;
589 nls.nl_pid = getpid();
592 if ((hotplug_fd.fd = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT)) == -1) {
593 ERROR("Failed to open hotplug socket: %s\n", strerror(errno));
596 if (bind(hotplug_fd.fd, (void *)&nls, sizeof(struct sockaddr_nl))) {
597 ERROR("Failed to bind hotplug socket: %s\n", strerror(errno));
601 if (setsockopt(hotplug_fd.fd, SOL_SOCKET, SO_RCVBUFFORCE, &nlbufsize, sizeof(nlbufsize)))
602 ERROR("Failed to resize receive buffer: %s\n", strerror(errno));
604 json_script_init(&jctx);
605 queue_proc.cb = queue_proc_cb;
606 uloop_fd_add(&hotplug_fd, ULOOP_READ);
609 int hotplug_run(char *rules)
618 void hotplug_shutdown(void)
620 uloop_fd_delete(&hotplug_fd);
621 close(hotplug_fd.fd);