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>
36 #define HOTPLUG_WAIT 500
39 struct list_head list;
41 struct blob_attr *msg;
42 struct blob_attr *data;
43 void (*handler)(struct blob_attr *msg, struct blob_attr *data);
46 static LIST_HEAD(cmd_queue);
47 static struct uloop_process queue_proc;
48 static struct uloop_timeout last_event;
49 static struct blob_buf b;
50 static char *rule_file;
51 static struct blob_buf script;
53 static char *hotplug_msg_find_var(struct blob_attr *msg, const char *name)
55 struct blob_attr *cur;
58 blobmsg_for_each_attr(cur, msg, rem) {
59 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
62 if (strcmp(blobmsg_name(cur), name) != 0)
65 return blobmsg_data(cur);
71 static void mkdir_p(char *dir)
73 char *l = strrchr(dir, '/');
83 static void handle_makedev(struct blob_attr *msg, struct blob_attr *data)
85 unsigned int oldumask = umask(0);
86 static struct blobmsg_policy mkdev_policy[2] = {
87 { .type = BLOBMSG_TYPE_STRING },
88 { .type = BLOBMSG_TYPE_STRING },
90 struct blob_attr *tb[2];
91 char *minor = hotplug_msg_find_var(msg, "MINOR");
92 char *major = hotplug_msg_find_var(msg, "MAJOR");
93 char *subsystem = hotplug_msg_find_var(msg, "SUBSYSTEM");
95 blobmsg_parse_array(mkdev_policy, 2, tb, blobmsg_data(data), blobmsg_data_len(data));
96 if (tb[0] && tb[1] && minor && major && subsystem) {
98 char *d = strdup(blobmsg_get_string(tb[0]));
104 if (!strcmp(subsystem, "block"))
106 mknod(blobmsg_get_string(tb[0]),
107 m | strtoul(blobmsg_data(tb[1]), NULL, 8),
108 makedev(atoi(major), atoi(minor)));
113 static void handle_rm(struct blob_attr *msg, struct blob_attr *data)
115 static struct blobmsg_policy rm_policy = {
116 .type = BLOBMSG_TYPE_STRING,
118 struct blob_attr *tb;
120 blobmsg_parse_array(&rm_policy, 1, &tb, blobmsg_data(data), blobmsg_data_len(data));
122 unlink(blobmsg_data(tb));
125 static void handle_exec(struct blob_attr *msg, struct blob_attr *data)
128 struct blob_attr *cur;
132 blobmsg_for_each_attr(cur, msg, rem)
133 setenv(blobmsg_name(cur), blobmsg_data(cur), 1);
135 blobmsg_for_each_attr(cur, data, rem) {
136 argv[i] = blobmsg_data(cur);
143 fd = open("/dev/null", O_RDWR);
145 dup2(fd, STDIN_FILENO);
146 dup2(fd, STDOUT_FILENO);
147 dup2(fd, STDERR_FILENO);
148 if (fd > STDERR_FILENO)
155 execvp(argv[0], &argv[0]);
160 static void handle_firmware(struct blob_attr *msg, struct blob_attr *data)
162 char *dir = blobmsg_get_string(blobmsg_data(data));
163 char *file = hotplug_msg_find_var(msg, "FIRMWARE");
164 char *dev = hotplug_msg_find_var(msg, "DEVPATH");
165 struct stat s = { 0 };
166 char *path, loadpath[256], syspath[256];
167 int fw, src, load, len;
168 static char buf[4096];
170 DEBUG(2, "Firmware request for %s/%s\n", dir, file);
172 if (!file || !dir || !dev) {
173 ERROR("Request for unknown firmware %s/%s\n", dir, file);
177 path = alloca(strlen(dir) + strlen(file) + 2);
178 sprintf(path, "%s/%s", dir, file);
180 if (stat(path, &s)) {
181 ERROR("Could not find firmware %s\n", path);
187 src = open(path, O_RDONLY);
189 ERROR("Failed to open %s\n", path);
195 snprintf(loadpath, sizeof(loadpath), "/sys/%s/loading", dev);
196 load = open(loadpath, O_WRONLY);
198 ERROR("Failed to open %s\n", loadpath);
201 if (write(load, "1", 1) == -1) {
202 ERROR("Failed to write to %s\n", loadpath);
207 snprintf(syspath, sizeof(syspath), "/sys/%s/data", dev);
208 fw = open(syspath, O_WRONLY);
210 ERROR("Failed to open %s\n", syspath);
216 len = read(src, buf, sizeof(buf));
220 if (write(fw, buf, len) == -1) {
221 ERROR("failed to write firmware file %s/%s to %s\n", dir, file, dev);
230 load = open(loadpath, O_WRONLY);
231 if (write(load, "0", 1) == -1)
232 ERROR("failed to write to %s\n", loadpath);
235 DEBUG(2, "Done loading %s\n", path);
240 static struct cmd_handler {
243 void (*handler)(struct blob_attr *msg, struct blob_attr *data);
248 .handler = handle_makedev,
252 .handler = handle_rm,
255 .handler = handle_exec,
257 .name = "load-firmware",
258 .handler = handle_firmware,
262 static void queue_next(void)
266 if (queue_proc.pending || list_empty(&cmd_queue))
269 c = list_first_entry(&cmd_queue, struct cmd_queue, list);
271 queue_proc.pid = fork();
272 if (!queue_proc.pid) {
274 c->handler(c->msg, c->data);
281 if (queue_proc.pid <= 0) {
286 uloop_process_add(&queue_proc);
288 DEBUG(4, "Launched hotplug exec instance, pid=%d\n", (int) queue_proc.pid);
291 static void queue_proc_cb(struct uloop_process *c, int ret)
293 DEBUG(4, "Finished hotplug exec instance, pid=%d\n", (int) c->pid);
298 static void queue_add(struct cmd_handler *h, struct blob_attr *msg, struct blob_attr *data)
300 struct cmd_queue *c = NULL;
301 struct blob_attr *_msg, *_data;
303 c = calloc_a(sizeof(struct cmd_queue),
304 &_msg, blob_pad_len(msg),
305 &_data, blob_pad_len(data),
314 memcpy(c->msg, msg, blob_pad_len(msg));
315 memcpy(c->data, data, blob_pad_len(data));
316 c->handler = h->handler;
317 list_add_tail(&c->list, &cmd_queue);
321 static const char* rule_handle_var(struct json_script_ctx *ctx, const char *name, struct blob_attr *vars)
323 const char *str, *sep;
325 if (!strcmp(name, "DEVICENAME") || !strcmp(name, "DEVNAME")) {
326 str = json_script_find_var(ctx, vars, "DEVPATH");
330 sep = strrchr(str, '/');
340 static struct json_script_file *
341 rule_handle_file(struct json_script_ctx *ctx, const char *name)
345 obj = json_object_from_file((char*)name);
349 blob_buf_init(&script, 0);
350 blobmsg_add_json_element(&script, "", obj);
352 return json_script_file_from_blobmsg(name, blob_data(script.head), blob_len(script.head));
355 static void rule_handle_command(struct json_script_ctx *ctx, const char *name,
356 struct blob_attr *data, struct blob_attr *vars)
358 struct blob_attr *cur;
362 DEBUG(4, "Command: %s", name);
363 blobmsg_for_each_attr(cur, data, rem)
364 DEBUG(4, " %s", (char *) blobmsg_data(cur));
367 DEBUG(4, "Message:");
368 blobmsg_for_each_attr(cur, vars, rem)
369 DEBUG(4, " %s=%s", blobmsg_name(cur), (char *) blobmsg_data(cur));
373 for (i = 0; i < ARRAY_SIZE(handlers); i++)
374 if (!strcmp(handlers[i].name, name)) {
375 if (handlers[i].atomic)
376 handlers[i].handler(vars, data);
378 queue_add(&handlers[i], vars, data);
383 uloop_timeout_set(&last_event, HOTPLUG_WAIT);
386 static void rule_handle_error(struct json_script_ctx *ctx, const char *msg,
387 struct blob_attr *context)
391 s = blobmsg_format_json(context, false);
392 ERROR("ERROR: %s in block: %s\n", msg, s);
396 static struct json_script_ctx jctx = {
397 .handle_var = rule_handle_var,
398 .handle_error = rule_handle_error,
399 .handle_command = rule_handle_command,
400 .handle_file = rule_handle_file,
403 static void hotplug_handler_debug(struct blob_attr *data)
410 str = blobmsg_format_json(data, true);
411 DEBUG(3, "%s\n", str);
415 static void hotplug_handler(struct uloop_fd *u, unsigned int ev)
418 static char buf[4096];
419 int len = recv(u->fd, buf, sizeof(buf), MSG_DONTWAIT);
424 blob_buf_init(&b, 0);
425 index = blobmsg_open_table(&b, NULL);
427 int l = strlen(buf + i) + 1;
428 char *e = strstr(&buf[i], "=");
432 blobmsg_add_string(&b, &buf[i], &e[1]);
436 blobmsg_close_table(&b, index);
437 hotplug_handler_debug(b.head);
438 json_script_run(&jctx, rule_file, blob_data(b.head));
441 static struct uloop_fd hotplug_fd = {
442 .cb = hotplug_handler,
445 void hotplug_last_event(uloop_timeout_handler handler)
447 last_event.cb = handler;
449 uloop_timeout_set(&last_event, HOTPLUG_WAIT);
451 uloop_timeout_cancel(&last_event);
454 void hotplug(char *rules)
456 struct sockaddr_nl nls;
457 int nlbufsize = 512 * 1024;
459 rule_file = strdup(rules);
460 memset(&nls,0,sizeof(struct sockaddr_nl));
461 nls.nl_family = AF_NETLINK;
462 nls.nl_pid = getpid();
465 if ((hotplug_fd.fd = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT)) == -1) {
466 ERROR("Failed to open hotplug socket: %s\n", strerror(errno));
469 if (bind(hotplug_fd.fd, (void *)&nls, sizeof(struct sockaddr_nl))) {
470 ERROR("Failed to bind hotplug socket: %s\n", strerror(errno));
474 if (setsockopt(hotplug_fd.fd, SOL_SOCKET, SO_RCVBUFFORCE, &nlbufsize, sizeof(nlbufsize)))
475 ERROR("Failed to resize receive buffer: %s\n", strerror(errno));
477 json_script_init(&jctx);
478 queue_proc.cb = queue_proc_cb;
479 uloop_fd_add(&hotplug_fd, ULOOP_READ);
482 int hotplug_run(char *rules)
491 void hotplug_shutdown(void)
493 uloop_fd_delete(&hotplug_fd);
494 close(hotplug_fd.fd);