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>
38 #define HOTPLUG_WAIT 500
42 struct list_head list;
44 struct blob_attr *msg;
45 struct blob_attr *data;
48 void (*handler)(struct blob_attr *msg, struct blob_attr *data);
49 void (*start)(struct blob_attr *msg, struct blob_attr *data);
50 void (*complete)(struct blob_attr *msg, struct blob_attr *data, int ret);
53 struct button_timeout {
54 struct list_head list;
55 struct uloop_timeout timeout;
58 struct blob_attr *data;
61 static LIST_HEAD(cmd_queue);
62 static LIST_HEAD(button_timer);
63 static struct uloop_process queue_proc;
64 static struct uloop_timeout last_event;
65 static struct blob_buf b, button_buf;
66 static char *rule_file;
67 static struct blob_buf script;
68 static struct cmd_queue *current;
70 static void queue_add(struct cmd_handler *h, struct blob_attr *msg, struct blob_attr *data);
71 static void handle_button_complete(struct blob_attr *msg, struct blob_attr *data, int ret);
73 static void button_free(struct button_timeout *b)
75 uloop_timeout_cancel(&b->timeout);
82 static void button_timeout_remove(char *button)
84 struct button_timeout *b, *c;
86 if (!list_empty(&button_timer)) list_for_each_entry_safe(b, c, &button_timer, list) {
87 if (!strcmp(b->name, button))
92 static char *hotplug_msg_find_var(struct blob_attr *msg, const char *name)
94 struct blob_attr *cur;
97 blobmsg_for_each_attr(cur, msg, rem) {
98 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
101 if (strcmp(blobmsg_name(cur), name) != 0)
104 return blobmsg_data(cur);
110 static void mkdir_p(char *dir)
112 char *l = strrchr(dir, '/');
122 static void handle_makedev(struct blob_attr *msg, struct blob_attr *data)
124 unsigned int oldumask = umask(0);
125 static struct blobmsg_policy mkdev_policy[3] = {
126 { .type = BLOBMSG_TYPE_STRING },
127 { .type = BLOBMSG_TYPE_STRING },
128 { .type = BLOBMSG_TYPE_STRING },
130 struct blob_attr *tb[3];
131 char *minor = hotplug_msg_find_var(msg, "MINOR");
132 char *major = hotplug_msg_find_var(msg, "MAJOR");
133 char *subsystem = hotplug_msg_find_var(msg, "SUBSYSTEM");
136 blobmsg_parse_array(mkdev_policy, 3, tb, blobmsg_data(data), blobmsg_data_len(data));
137 if (tb[0] && tb[1] && minor && major && subsystem) {
139 char *d = strdup(blobmsg_get_string(tb[0]));
145 if (!strcmp(subsystem, "block"))
147 mknod(blobmsg_get_string(tb[0]),
148 m | strtoul(blobmsg_data(tb[1]), NULL, 8),
149 makedev(atoi(major), atoi(minor)));
151 struct group *g = getgrnam(blobmsg_get_string(tb[2]));
154 ret = chown(blobmsg_get_string(tb[0]), 0, g->gr_gid);
157 ERROR("cannot set group %s for %s\n",
158 blobmsg_get_string(tb[2]),
159 blobmsg_get_string(tb[0]));
165 static void handle_rm(struct blob_attr *msg, struct blob_attr *data)
167 static struct blobmsg_policy rm_policy = {
168 .type = BLOBMSG_TYPE_STRING,
170 struct blob_attr *tb;
172 blobmsg_parse_array(&rm_policy, 1, &tb, blobmsg_data(data), blobmsg_data_len(data));
174 unlink(blobmsg_data(tb));
177 static void handle_exec(struct blob_attr *msg, struct blob_attr *data)
180 struct blob_attr *cur;
184 blobmsg_for_each_attr(cur, msg, rem)
185 setenv(blobmsg_name(cur), blobmsg_data(cur), 1);
187 blobmsg_for_each_attr(cur, data, rem) {
188 argv[i] = blobmsg_data(cur);
195 fd = open("/dev/null", O_RDWR);
197 dup2(fd, STDIN_FILENO);
198 dup2(fd, STDOUT_FILENO);
199 dup2(fd, STDERR_FILENO);
200 if (fd > STDERR_FILENO)
207 execvp(argv[0], &argv[0]);
212 static void handle_button_start(struct blob_attr *msg, struct blob_attr *data)
214 char *button = hotplug_msg_find_var(msg, "BUTTON");
217 button_timeout_remove(button);
220 static void handle_firmware(struct blob_attr *msg, struct blob_attr *data)
222 char *dir = blobmsg_get_string(blobmsg_data(data));
223 char *file = hotplug_msg_find_var(msg, "FIRMWARE");
224 char *dev = hotplug_msg_find_var(msg, "DEVPATH");
225 struct stat s = { 0 };
226 char *path, loadpath[256], syspath[256];
227 int fw, src, load, len;
228 static char buf[4096];
230 DEBUG(2, "Firmware request for %s/%s\n", dir, file);
232 if (!file || !dir || !dev) {
233 ERROR("Request for unknown firmware %s/%s\n", dir, file);
237 path = alloca(strlen(dir) + strlen(file) + 2);
238 sprintf(path, "%s/%s", dir, file);
240 if (stat(path, &s)) {
241 ERROR("Could not find firmware %s\n", path);
247 src = open(path, O_RDONLY);
249 ERROR("Failed to open %s\n", path);
255 snprintf(loadpath, sizeof(loadpath), "/sys/%s/loading", dev);
256 load = open(loadpath, O_WRONLY);
258 ERROR("Failed to open %s\n", loadpath);
261 if (write(load, "1", 1) == -1) {
262 ERROR("Failed to write to %s\n", loadpath);
267 snprintf(syspath, sizeof(syspath), "/sys/%s/data", dev);
268 fw = open(syspath, O_WRONLY);
270 ERROR("Failed to open %s\n", syspath);
276 len = read(src, buf, sizeof(buf));
280 if (write(fw, buf, len) == -1) {
281 ERROR("failed to write firmware file %s/%s to %s\n", dir, file, dev);
290 load = open(loadpath, O_WRONLY);
291 if (write(load, "0", 1) == -1)
292 ERROR("failed to write to %s\n", loadpath);
295 DEBUG(2, "Done loading %s\n", path);
308 static struct cmd_handler {
311 void (*handler)(struct blob_attr *msg, struct blob_attr *data);
312 void (*start)(struct blob_attr *msg, struct blob_attr *data);
313 void (*complete)(struct blob_attr *msg, struct blob_attr *data, int ret);
318 .handler = handle_makedev,
323 .handler = handle_rm,
327 .handler = handle_exec,
331 .handler = handle_exec,
332 .start = handle_button_start,
333 .complete = handle_button_complete,
336 .name = "load-firmware",
337 .handler = handle_firmware,
341 static void queue_next(void)
345 if (queue_proc.pending || list_empty(&cmd_queue))
348 c = list_first_entry(&cmd_queue, struct cmd_queue, list);
350 queue_proc.pid = fork();
351 if (!queue_proc.pid) {
353 c->handler(c->msg, c->data);
357 c->start(c->msg, c->data);
363 if (queue_proc.pid <= 0) {
368 uloop_process_add(&queue_proc);
370 DEBUG(4, "Launched hotplug exec instance, pid=%d\n", (int) queue_proc.pid);
373 static void queue_proc_cb(struct uloop_process *c, int ret)
375 DEBUG(4, "Finished hotplug exec instance, pid=%d\n", (int) c->pid);
378 current->complete(current->msg, current->data, ret);
385 static void queue_add(struct cmd_handler *h, struct blob_attr *msg, struct blob_attr *data)
387 struct cmd_queue *c = NULL;
388 struct blob_attr *_msg, *_data;
390 c = calloc_a(sizeof(struct cmd_queue),
391 &_msg, blob_pad_len(msg),
392 &_data, blob_pad_len(data),
401 memcpy(c->msg, msg, blob_pad_len(msg));
402 memcpy(c->data, data, blob_pad_len(data));
403 c->handler = h->handler;
404 c->complete = h->complete;
406 list_add_tail(&c->list, &cmd_queue);
410 static void handle_button_timeout(struct uloop_timeout *t)
412 struct button_timeout *b;
415 b = container_of(t, struct button_timeout, timeout);
416 blob_buf_init(&button_buf, 0);
417 blobmsg_add_string(&button_buf, "BUTTON", b->name);
418 blobmsg_add_string(&button_buf, "ACTION", "timeout");
419 snprintf(seen, sizeof(seen), "%d", b->seen);
420 blobmsg_add_string(&button_buf, "SEEN", seen);
421 queue_add(&handlers[HANDLER_EXEC], button_buf.head, b->data);
425 static void handle_button_complete(struct blob_attr *msg, struct blob_attr *data, int ret)
427 char *name = hotplug_msg_find_var(msg, "BUTTON");
428 struct button_timeout *b;
429 int timeout = ret >> 8;
437 b = calloc(1, sizeof(*b));
441 b->data = malloc(blob_pad_len(data));
442 b->name = strdup(name);
445 memcpy(b->data, data, blob_pad_len(data));
446 b->timeout.cb = handle_button_timeout;
448 uloop_timeout_set(&b->timeout, timeout * 1000);
449 list_add(&b->list, &button_timer);
452 static const char* rule_handle_var(struct json_script_ctx *ctx, const char *name, struct blob_attr *vars)
454 const char *str, *sep;
456 if (!strcmp(name, "DEVICENAME") || !strcmp(name, "DEVNAME")) {
457 str = json_script_find_var(ctx, vars, "DEVPATH");
461 sep = strrchr(str, '/');
471 static struct json_script_file *
472 rule_handle_file(struct json_script_ctx *ctx, const char *name)
476 obj = json_object_from_file((char*)name);
480 blob_buf_init(&script, 0);
481 blobmsg_add_json_element(&script, "", obj);
483 return json_script_file_from_blobmsg(name, blob_data(script.head), blob_len(script.head));
486 static void rule_handle_command(struct json_script_ctx *ctx, const char *name,
487 struct blob_attr *data, struct blob_attr *vars)
489 struct blob_attr *cur;
493 DEBUG(4, "Command: %s\n", name);
494 blobmsg_for_each_attr(cur, data, rem)
495 DEBUG(4, " %s\n", (char *) blobmsg_data(cur));
497 DEBUG(4, "Message:\n");
498 blobmsg_for_each_attr(cur, vars, rem)
499 DEBUG(4, " %s=%s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
502 for (i = 0; i < ARRAY_SIZE(handlers); i++)
503 if (!strcmp(handlers[i].name, name)) {
504 if (handlers[i].atomic)
505 handlers[i].handler(vars, data);
507 queue_add(&handlers[i], vars, data);
512 uloop_timeout_set(&last_event, HOTPLUG_WAIT);
515 static void rule_handle_error(struct json_script_ctx *ctx, const char *msg,
516 struct blob_attr *context)
520 s = blobmsg_format_json(context, false);
521 ERROR("ERROR: %s in block: %s\n", msg, s);
525 static struct json_script_ctx jctx = {
526 .handle_var = rule_handle_var,
527 .handle_error = rule_handle_error,
528 .handle_command = rule_handle_command,
529 .handle_file = rule_handle_file,
532 static void hotplug_handler_debug(struct blob_attr *data)
539 str = blobmsg_format_json(data, true);
540 DEBUG(3, "%s\n", str);
544 static void hotplug_handler(struct uloop_fd *u, unsigned int ev)
547 static char buf[4096];
548 int len = recv(u->fd, buf, sizeof(buf), MSG_DONTWAIT);
553 blob_buf_init(&b, 0);
554 index = blobmsg_open_table(&b, NULL);
556 int l = strlen(buf + i) + 1;
557 char *e = strstr(&buf[i], "=");
561 blobmsg_add_string(&b, &buf[i], &e[1]);
565 blobmsg_close_table(&b, index);
566 hotplug_handler_debug(b.head);
567 json_script_run(&jctx, rule_file, blob_data(b.head));
570 static struct uloop_fd hotplug_fd = {
571 .cb = hotplug_handler,
574 void hotplug_last_event(uloop_timeout_handler handler)
576 last_event.cb = handler;
578 uloop_timeout_set(&last_event, HOTPLUG_WAIT);
580 uloop_timeout_cancel(&last_event);
583 void hotplug(char *rules)
585 struct sockaddr_nl nls = {};
586 int nlbufsize = 512 * 1024;
588 rule_file = strdup(rules);
589 nls.nl_family = AF_NETLINK;
590 nls.nl_pid = getpid();
593 if ((hotplug_fd.fd = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT)) == -1) {
594 ERROR("Failed to open hotplug socket: %s\n", strerror(errno));
597 if (bind(hotplug_fd.fd, (void *)&nls, sizeof(struct sockaddr_nl))) {
598 ERROR("Failed to bind hotplug socket: %s\n", strerror(errno));
602 if (setsockopt(hotplug_fd.fd, SOL_SOCKET, SO_RCVBUFFORCE, &nlbufsize, sizeof(nlbufsize)))
603 ERROR("Failed to resize receive buffer: %s\n", strerror(errno));
605 json_script_init(&jctx);
606 queue_proc.cb = queue_proc_cb;
607 uloop_fd_add(&hotplug_fd, ULOOP_READ);
610 int hotplug_run(char *rules)
619 void hotplug_shutdown(void)
621 uloop_fd_delete(&hotplug_fd);
622 close(hotplug_fd.fd);