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/avl-cmp.h>
23 #include <libubox/blobmsg_json.h>
24 #include <libubox/json_script.h>
25 #include <libubox/uloop.h>
26 #include <json-c/json.h>
37 #define HOTPLUG_WAIT 500
40 struct list_head list;
42 struct blob_attr *msg;
43 struct blob_attr *data;
44 void (*handler)(struct blob_attr *msg, struct blob_attr *data);
51 struct timespec start;
52 struct uloop_timeout timeout;
53 struct uloop_process process;
55 struct blob_attr *msg;
56 struct blob_attr *data;
59 static LIST_HEAD(cmd_queue);
60 static AVL_TREE(cmd_intervals, avl_strcmp, false, NULL);
61 static struct uloop_process queue_proc;
62 static struct uloop_timeout last_event;
63 static struct blob_buf b;
64 static char *rule_file;
65 static struct blob_buf script;
67 static char *hotplug_msg_find_var(struct blob_attr *msg, const char *name)
69 struct blob_attr *cur;
72 blobmsg_for_each_attr(cur, msg, rem) {
73 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
76 if (strcmp(blobmsg_name(cur), name) != 0)
79 return blobmsg_data(cur);
85 static void mkdir_p(char *dir)
87 char *l = strrchr(dir, '/');
97 static void handle_makedev(struct blob_attr *msg, struct blob_attr *data)
99 unsigned int oldumask = umask(0);
100 static struct blobmsg_policy mkdev_policy[2] = {
101 { .type = BLOBMSG_TYPE_STRING },
102 { .type = BLOBMSG_TYPE_STRING },
104 struct blob_attr *tb[2];
105 char *minor = hotplug_msg_find_var(msg, "MINOR");
106 char *major = hotplug_msg_find_var(msg, "MAJOR");
107 char *subsystem = hotplug_msg_find_var(msg, "SUBSYSTEM");
109 blobmsg_parse_array(mkdev_policy, 2, tb, blobmsg_data(data), blobmsg_data_len(data));
110 if (tb[0] && tb[1] && minor && major && subsystem) {
112 char *d = strdup(blobmsg_get_string(tb[0]));
118 if (!strcmp(subsystem, "block"))
120 mknod(blobmsg_get_string(tb[0]),
121 m | strtoul(blobmsg_data(tb[1]), NULL, 8),
122 makedev(atoi(major), atoi(minor)));
127 static void handle_rm(struct blob_attr *msg, struct blob_attr *data)
129 static struct blobmsg_policy rm_policy = {
130 .type = BLOBMSG_TYPE_STRING,
132 struct blob_attr *tb;
134 blobmsg_parse_array(&rm_policy, 1, &tb, blobmsg_data(data), blobmsg_data_len(data));
136 unlink(blobmsg_data(tb));
139 static void handle_exec(struct blob_attr *msg, struct blob_attr *data)
142 struct blob_attr *cur;
146 blobmsg_for_each_attr(cur, msg, rem)
147 setenv(blobmsg_name(cur), blobmsg_data(cur), 1);
149 blobmsg_for_each_attr(cur, data, rem) {
150 argv[i] = blobmsg_data(cur);
157 fd = open("/dev/null", O_RDWR);
159 dup2(fd, STDIN_FILENO);
160 dup2(fd, STDOUT_FILENO);
161 dup2(fd, STDERR_FILENO);
162 if (fd > STDERR_FILENO)
169 execvp(argv[0], &argv[0]);
174 static void handle_set_interval_timeout(struct uloop_timeout *timeout)
176 struct cmd_interval *interval = container_of(timeout, struct cmd_interval, timeout);
177 struct blob_attr *cur;
183 blobmsg_for_each_attr(cur, interval->data, rem) {
188 msecs = strtol(blobmsg_get_string(cur), NULL, 0);
191 argv[i - 2] = blobmsg_data(cur);
198 if (interval->process.pending) {
199 uloop_timeout_set(&interval->timeout, msecs);
203 interval->process.pid = fork();
204 if (interval->process.pid < 0) {
206 } else if (interval->process.pid == 0) {
213 clock_gettime(CLOCK_MONOTONIC, &now);
214 snprintf(elapsed, sizeof(elapsed), "%ld", now.tv_sec - interval->start.tv_sec);
216 blobmsg_for_each_attr(cur, interval->msg, rem)
217 setenv(blobmsg_name(cur), blobmsg_data(cur), 1);
218 setenv("ACTION", "interval", 1);
219 setenv("ELAPSED", elapsed, 1);
223 fd = open("/dev/null", O_RDWR);
225 dup2(fd, STDIN_FILENO);
226 dup2(fd, STDOUT_FILENO);
227 dup2(fd, STDERR_FILENO);
228 if (fd > STDERR_FILENO)
234 execvp(argv[0], &argv[0]);
237 uloop_process_add(&interval->process);
238 uloop_timeout_set(&interval->timeout, msecs);
242 static void handle_set_interval_process_cb(struct uloop_process *process, int ret)
244 struct cmd_interval *interval = container_of(process, struct cmd_interval, process);
246 if (interval->cancelled)
250 static void handle_set_interval(struct blob_attr *msg, struct blob_attr *data)
252 static struct blobmsg_policy set_interval_policy[2] = {
253 { .type = BLOBMSG_TYPE_STRING },
254 { .type = BLOBMSG_TYPE_STRING },
256 struct blob_attr *tb[2];
257 struct cmd_interval *interval;
258 struct blob_attr *_msg, *_data;
263 blobmsg_parse_array(set_interval_policy, 2, tb, blobmsg_data(data), blobmsg_data_len(data));
264 if (!tb[0] || !tb[1])
266 name = blobmsg_get_string(tb[0]);
267 msecs = strtol(blobmsg_get_string(tb[1]), NULL, 0);
269 interval = calloc_a(sizeof(struct cmd_interval),
270 &_key, strlen(name) + 1,
271 &_msg, blob_pad_len(msg),
272 &_data, blob_pad_len(data),
278 interval->avl.key = _key;
279 interval->msg = _msg;
280 interval->data = _data;
281 clock_gettime(CLOCK_MONOTONIC, &interval->start);
282 interval->timeout.cb = handle_set_interval_timeout;
283 interval->process.cb = handle_set_interval_process_cb;
285 memcpy(interval->msg, msg, blob_pad_len(msg));
286 memcpy(interval->data, data, blob_pad_len(data));
288 avl_insert(&cmd_intervals, &interval->avl);
290 uloop_timeout_set(&interval->timeout, msecs);
293 static void handle_clear_interval(struct blob_attr *msg, struct blob_attr *data)
295 static struct blobmsg_policy clear_interval_policy = {
296 .type = BLOBMSG_TYPE_STRING,
298 struct blob_attr *tb;
299 struct cmd_interval *interval;
302 blobmsg_parse_array(&clear_interval_policy, 1, &tb, blobmsg_data(data), blobmsg_data_len(data));
305 name = blobmsg_get_string(tb);
307 interval = avl_find_element(&cmd_intervals, name, interval, avl);
309 uloop_timeout_cancel(&interval->timeout);
310 avl_delete(&cmd_intervals, &interval->avl);
311 if (interval->process.pending)
312 interval->cancelled = true;
318 static void handle_firmware(struct blob_attr *msg, struct blob_attr *data)
320 char *dir = blobmsg_get_string(blobmsg_data(data));
321 char *file = hotplug_msg_find_var(msg, "FIRMWARE");
322 char *dev = hotplug_msg_find_var(msg, "DEVPATH");
323 struct stat s = { 0 };
324 char *path, loadpath[256], syspath[256];
325 int fw, src, load, len;
326 static char buf[4096];
328 DEBUG(2, "Firmware request for %s/%s\n", dir, file);
330 if (!file || !dir || !dev) {
331 ERROR("Request for unknown firmware %s/%s\n", dir, file);
335 path = alloca(strlen(dir) + strlen(file) + 2);
336 sprintf(path, "%s/%s", dir, file);
338 if (stat(path, &s)) {
339 ERROR("Could not find firmware %s\n", path);
345 src = open(path, O_RDONLY);
347 ERROR("Failed to open %s\n", path);
353 snprintf(loadpath, sizeof(loadpath), "/sys/%s/loading", dev);
354 load = open(loadpath, O_WRONLY);
356 ERROR("Failed to open %s\n", loadpath);
359 if (write(load, "1", 1) == -1) {
360 ERROR("Failed to write to %s\n", loadpath);
365 snprintf(syspath, sizeof(syspath), "/sys/%s/data", dev);
366 fw = open(syspath, O_WRONLY);
368 ERROR("Failed to open %s\n", syspath);
374 len = read(src, buf, sizeof(buf));
378 if (write(fw, buf, len) == -1) {
379 ERROR("failed to write firmware file %s/%s to %s\n", dir, file, dev);
388 load = open(loadpath, O_WRONLY);
389 if (write(load, "0", 1) == -1)
390 ERROR("failed to write to %s\n", loadpath);
393 DEBUG(2, "Done loading %s\n", path);
398 static struct cmd_handler {
401 void (*handler)(struct blob_attr *msg, struct blob_attr *data);
406 .handler = handle_makedev,
410 .handler = handle_rm,
413 .handler = handle_exec,
415 .name = "set-interval",
417 .handler = handle_set_interval,
419 .name = "clear-interval",
421 .handler = handle_clear_interval,
423 .name = "load-firmware",
424 .handler = handle_firmware,
428 static void queue_next(void)
432 if (queue_proc.pending || list_empty(&cmd_queue))
435 c = list_first_entry(&cmd_queue, struct cmd_queue, list);
437 queue_proc.pid = fork();
438 if (!queue_proc.pid) {
440 c->handler(c->msg, c->data);
447 if (queue_proc.pid <= 0) {
452 uloop_process_add(&queue_proc);
454 DEBUG(4, "Launched hotplug exec instance, pid=%d\n", (int) queue_proc.pid);
457 static void queue_proc_cb(struct uloop_process *c, int ret)
459 DEBUG(4, "Finished hotplug exec instance, pid=%d\n", (int) c->pid);
464 static void queue_add(struct cmd_handler *h, struct blob_attr *msg, struct blob_attr *data)
466 struct cmd_queue *c = NULL;
467 struct blob_attr *_msg, *_data;
469 c = calloc_a(sizeof(struct cmd_queue),
470 &_msg, blob_pad_len(msg),
471 &_data, blob_pad_len(data),
480 memcpy(c->msg, msg, blob_pad_len(msg));
481 memcpy(c->data, data, blob_pad_len(data));
482 c->handler = h->handler;
483 list_add_tail(&c->list, &cmd_queue);
487 static const char* rule_handle_var(struct json_script_ctx *ctx, const char *name, struct blob_attr *vars)
489 const char *str, *sep;
491 if (!strcmp(name, "DEVICENAME") || !strcmp(name, "DEVNAME")) {
492 str = json_script_find_var(ctx, vars, "DEVPATH");
496 sep = strrchr(str, '/');
506 static struct json_script_file *
507 rule_handle_file(struct json_script_ctx *ctx, const char *name)
511 obj = json_object_from_file((char*)name);
515 blob_buf_init(&script, 0);
516 blobmsg_add_json_element(&script, "", obj);
518 return json_script_file_from_blobmsg(name, blob_data(script.head), blob_len(script.head));
521 static void rule_handle_command(struct json_script_ctx *ctx, const char *name,
522 struct blob_attr *data, struct blob_attr *vars)
524 struct blob_attr *cur;
528 DEBUG(4, "Command: %s", name);
529 blobmsg_for_each_attr(cur, data, rem)
530 DEBUG(4, " %s", (char *) blobmsg_data(cur));
533 DEBUG(4, "Message:");
534 blobmsg_for_each_attr(cur, vars, rem)
535 DEBUG(4, " %s=%s", blobmsg_name(cur), (char *) blobmsg_data(cur));
539 for (i = 0; i < ARRAY_SIZE(handlers); i++)
540 if (!strcmp(handlers[i].name, name)) {
541 if (handlers[i].atomic)
542 handlers[i].handler(vars, data);
544 queue_add(&handlers[i], vars, data);
549 uloop_timeout_set(&last_event, HOTPLUG_WAIT);
552 static void rule_handle_error(struct json_script_ctx *ctx, const char *msg,
553 struct blob_attr *context)
557 s = blobmsg_format_json(context, false);
558 ERROR("ERROR: %s in block: %s\n", msg, s);
562 static struct json_script_ctx jctx = {
563 .handle_var = rule_handle_var,
564 .handle_error = rule_handle_error,
565 .handle_command = rule_handle_command,
566 .handle_file = rule_handle_file,
569 static void hotplug_handler_debug(struct blob_attr *data)
576 str = blobmsg_format_json(data, true);
577 DEBUG(3, "%s\n", str);
581 static void hotplug_handler(struct uloop_fd *u, unsigned int ev)
584 static char buf[4096];
585 int len = recv(u->fd, buf, sizeof(buf), MSG_DONTWAIT);
590 blob_buf_init(&b, 0);
591 index = blobmsg_open_table(&b, NULL);
593 int l = strlen(buf + i) + 1;
594 char *e = strstr(&buf[i], "=");
598 blobmsg_add_string(&b, &buf[i], &e[1]);
602 blobmsg_close_table(&b, index);
603 hotplug_handler_debug(b.head);
604 json_script_run(&jctx, rule_file, blob_data(b.head));
607 static struct uloop_fd hotplug_fd = {
608 .cb = hotplug_handler,
611 void hotplug_last_event(uloop_timeout_handler handler)
613 last_event.cb = handler;
615 uloop_timeout_set(&last_event, HOTPLUG_WAIT);
617 uloop_timeout_cancel(&last_event);
620 void hotplug(char *rules)
622 struct sockaddr_nl nls;
623 int nlbufsize = 512 * 1024;
625 rule_file = strdup(rules);
626 memset(&nls,0,sizeof(struct sockaddr_nl));
627 nls.nl_family = AF_NETLINK;
628 nls.nl_pid = getpid();
631 if ((hotplug_fd.fd = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT)) == -1) {
632 ERROR("Failed to open hotplug socket: %s\n", strerror(errno));
635 if (bind(hotplug_fd.fd, (void *)&nls, sizeof(struct sockaddr_nl))) {
636 ERROR("Failed to bind hotplug socket: %s\n", strerror(errno));
640 if (setsockopt(hotplug_fd.fd, SOL_SOCKET, SO_RCVBUFFORCE, &nlbufsize, sizeof(nlbufsize)))
641 ERROR("Failed to resize receive buffer: %s\n", strerror(errno));
643 json_script_init(&jctx);
644 queue_proc.cb = queue_proc_cb;
645 uloop_fd_add(&hotplug_fd, ULOOP_READ);
648 int hotplug_run(char *rules)
657 void hotplug_shutdown(void)
659 uloop_fd_delete(&hotplug_fd);
660 close(hotplug_fd.fd);