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");
135 blobmsg_parse_array(mkdev_policy, 3, tb, blobmsg_data(data), blobmsg_data_len(data));
136 if (tb[0] && tb[1] && minor && major && subsystem) {
138 char *d = strdup(blobmsg_get_string(tb[0]));
144 if (!strcmp(subsystem, "block"))
146 mknod(blobmsg_get_string(tb[0]),
147 m | strtoul(blobmsg_data(tb[1]), NULL, 8),
148 makedev(atoi(major), atoi(minor)));
150 struct group *g = getgrnam(blobmsg_get_string(tb[2]));
153 ret = chown(blobmsg_get_string(tb[0]), 0, g->gr_gid);
156 ERROR("cannot set group %s for %s\n",
157 blobmsg_get_string(tb[2]),
158 blobmsg_get_string(tb[0]));
164 static void handle_rm(struct blob_attr *msg, struct blob_attr *data)
166 static struct blobmsg_policy rm_policy = {
167 .type = BLOBMSG_TYPE_STRING,
169 struct blob_attr *tb;
171 blobmsg_parse_array(&rm_policy, 1, &tb, blobmsg_data(data), blobmsg_data_len(data));
173 unlink(blobmsg_data(tb));
176 static void handle_exec(struct blob_attr *msg, struct blob_attr *data)
179 struct blob_attr *cur;
183 blobmsg_for_each_attr(cur, msg, rem)
184 setenv(blobmsg_name(cur), blobmsg_data(cur), 1);
186 blobmsg_for_each_attr(cur, data, rem) {
187 argv[i] = blobmsg_data(cur);
194 fd = open("/dev/null", O_RDWR);
196 dup2(fd, STDIN_FILENO);
197 dup2(fd, STDOUT_FILENO);
198 dup2(fd, STDERR_FILENO);
199 if (fd > STDERR_FILENO)
206 execvp(argv[0], &argv[0]);
211 static void handle_button_start(struct blob_attr *msg, struct blob_attr *data)
213 char *button = hotplug_msg_find_var(msg, "BUTTON");
216 button_timeout_remove(button);
219 static void handle_firmware(struct blob_attr *msg, struct blob_attr *data)
221 char *dir = blobmsg_get_string(blobmsg_data(data));
222 char *file = hotplug_msg_find_var(msg, "FIRMWARE");
223 char *dev = hotplug_msg_find_var(msg, "DEVPATH");
224 struct stat s = { 0 };
225 char *path, loadpath[256], syspath[256];
226 int fw, src, load, len;
227 static char buf[4096];
229 DEBUG(2, "Firmware request for %s/%s\n", dir, file);
231 if (!file || !dir || !dev) {
232 ERROR("Request for unknown firmware %s/%s\n", dir, file);
236 path = alloca(strlen(dir) + strlen(file) + 2);
237 sprintf(path, "%s/%s", dir, file);
239 if (stat(path, &s)) {
240 ERROR("Could not find firmware %s\n", path);
246 src = open(path, O_RDONLY);
248 ERROR("Failed to open %s\n", path);
254 snprintf(loadpath, sizeof(loadpath), "/sys/%s/loading", dev);
255 load = open(loadpath, O_WRONLY);
257 ERROR("Failed to open %s\n", loadpath);
260 if (write(load, "1", 1) == -1) {
261 ERROR("Failed to write to %s\n", loadpath);
266 snprintf(syspath, sizeof(syspath), "/sys/%s/data", dev);
267 fw = open(syspath, O_WRONLY);
269 ERROR("Failed to open %s\n", syspath);
275 len = read(src, buf, sizeof(buf));
279 if (write(fw, buf, len) == -1) {
280 ERROR("failed to write firmware file %s/%s to %s\n", dir, file, dev);
289 load = open(loadpath, O_WRONLY);
290 if (write(load, "0", 1) == -1)
291 ERROR("failed to write to %s\n", loadpath);
294 DEBUG(2, "Done loading %s\n", path);
307 static struct cmd_handler {
310 void (*handler)(struct blob_attr *msg, struct blob_attr *data);
311 void (*start)(struct blob_attr *msg, struct blob_attr *data);
312 void (*complete)(struct blob_attr *msg, struct blob_attr *data, int ret);
317 .handler = handle_makedev,
322 .handler = handle_rm,
326 .handler = handle_exec,
330 .handler = handle_exec,
331 .start = handle_button_start,
332 .complete = handle_button_complete,
335 .name = "load-firmware",
336 .handler = handle_firmware,
340 static void queue_next(void)
344 if (queue_proc.pending || list_empty(&cmd_queue))
347 c = list_first_entry(&cmd_queue, struct cmd_queue, list);
349 queue_proc.pid = fork();
350 if (!queue_proc.pid) {
352 c->handler(c->msg, c->data);
356 c->start(c->msg, c->data);
362 if (queue_proc.pid <= 0) {
367 uloop_process_add(&queue_proc);
369 DEBUG(4, "Launched hotplug exec instance, pid=%d\n", (int) queue_proc.pid);
372 static void queue_proc_cb(struct uloop_process *c, int ret)
374 DEBUG(4, "Finished hotplug exec instance, pid=%d\n", (int) c->pid);
377 current->complete(current->msg, current->data, ret);
384 static void queue_add(struct cmd_handler *h, struct blob_attr *msg, struct blob_attr *data)
386 struct cmd_queue *c = NULL;
387 struct blob_attr *_msg, *_data;
389 c = calloc_a(sizeof(struct cmd_queue),
390 &_msg, blob_pad_len(msg),
391 &_data, blob_pad_len(data),
400 memcpy(c->msg, msg, blob_pad_len(msg));
401 memcpy(c->data, data, blob_pad_len(data));
402 c->handler = h->handler;
403 c->complete = h->complete;
405 list_add_tail(&c->list, &cmd_queue);
409 static void handle_button_timeout(struct uloop_timeout *t)
411 struct button_timeout *b;
414 b = container_of(t, struct button_timeout, timeout);
415 blob_buf_init(&button_buf, 0);
416 blobmsg_add_string(&button_buf, "BUTTON", b->name);
417 blobmsg_add_string(&button_buf, "ACTION", "timeout");
418 snprintf(seen, sizeof(seen), "%d", b->seen);
419 blobmsg_add_string(&button_buf, "SEEN", seen);
420 queue_add(&handlers[HANDLER_EXEC], button_buf.head, b->data);
424 static void handle_button_complete(struct blob_attr *msg, struct blob_attr *data, int ret)
426 char *name = hotplug_msg_find_var(msg, "BUTTON");
427 struct button_timeout *b;
428 int timeout = ret >> 8;
433 b = malloc(sizeof(*b));
437 memset(b, 0, sizeof(*b));
439 b->data = malloc(blob_pad_len(data));
440 b->name = strdup(name);
443 memcpy(b->data, data, blob_pad_len(data));
444 b->timeout.cb = handle_button_timeout;
446 uloop_timeout_set(&b->timeout, timeout * 1000);
447 list_add(&b->list, &button_timer);
450 static const char* rule_handle_var(struct json_script_ctx *ctx, const char *name, struct blob_attr *vars)
452 const char *str, *sep;
454 if (!strcmp(name, "DEVICENAME") || !strcmp(name, "DEVNAME")) {
455 str = json_script_find_var(ctx, vars, "DEVPATH");
459 sep = strrchr(str, '/');
469 static struct json_script_file *
470 rule_handle_file(struct json_script_ctx *ctx, const char *name)
474 obj = json_object_from_file((char*)name);
478 blob_buf_init(&script, 0);
479 blobmsg_add_json_element(&script, "", obj);
481 return json_script_file_from_blobmsg(name, blob_data(script.head), blob_len(script.head));
484 static void rule_handle_command(struct json_script_ctx *ctx, const char *name,
485 struct blob_attr *data, struct blob_attr *vars)
487 struct blob_attr *cur;
491 DEBUG(4, "Command: %s", name);
492 blobmsg_for_each_attr(cur, data, rem)
493 DEBUG(4, " %s", (char *) blobmsg_data(cur));
496 DEBUG(4, "Message:");
497 blobmsg_for_each_attr(cur, vars, rem)
498 DEBUG(4, " %s=%s", 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 memset(&nls,0,sizeof(struct sockaddr_nl));
590 nls.nl_family = AF_NETLINK;
591 nls.nl_pid = getpid();
594 if ((hotplug_fd.fd = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT)) == -1) {
595 ERROR("Failed to open hotplug socket: %s\n", strerror(errno));
598 if (bind(hotplug_fd.fd, (void *)&nls, sizeof(struct sockaddr_nl))) {
599 ERROR("Failed to bind hotplug socket: %s\n", strerror(errno));
603 if (setsockopt(hotplug_fd.fd, SOL_SOCKET, SO_RCVBUFFORCE, &nlbufsize, sizeof(nlbufsize)))
604 ERROR("Failed to resize receive buffer: %s\n", strerror(errno));
606 json_script_init(&jctx);
607 queue_proc.cb = queue_proc_cb;
608 uloop_fd_add(&hotplug_fd, ULOOP_READ);
611 int hotplug_run(char *rules)
620 void hotplug_shutdown(void)
622 uloop_fd_delete(&hotplug_fd);
623 close(hotplug_fd.fd);