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>
35 #define HOTPLUG_WAIT 500
38 struct list_head list;
40 struct blob_attr *msg;
41 struct blob_attr *data;
42 void (*handler)(struct blob_attr *msg, struct blob_attr *data);
45 static LIST_HEAD(cmd_queue);
46 static struct uloop_process queue_proc;
47 static struct uloop_timeout last_event;
48 static struct blob_buf b;
49 static char *rule_file;
50 static struct blob_buf script;
52 static char *hotplug_msg_find_var(struct blob_attr *msg, const char *name)
54 struct blob_attr *cur;
57 blobmsg_for_each_attr(cur, msg, rem) {
58 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
61 if (strcmp(blobmsg_name(cur), name) != 0)
64 return blobmsg_data(cur);
70 static void mkdir_p(char *dir)
72 char *l = strrchr(dir, '/');
82 static void handle_makedev(struct blob_attr *msg, struct blob_attr *data)
84 unsigned int oldumask = umask(0);
85 static struct blobmsg_policy mkdev_policy[2] = {
86 { .type = BLOBMSG_TYPE_STRING },
87 { .type = BLOBMSG_TYPE_STRING },
89 struct blob_attr *tb[2];
90 char *minor = hotplug_msg_find_var(msg, "MINOR");
91 char *major = hotplug_msg_find_var(msg, "MAJOR");
92 char *subsystem = hotplug_msg_find_var(msg, "SUBSYSTEM");
94 blobmsg_parse_array(mkdev_policy, 2, tb, blobmsg_data(data), blobmsg_data_len(data));
95 if (tb[0] && tb[1] && minor && major && subsystem) {
97 char *d = strdup(blobmsg_get_string(tb[0]));
103 if (!strcmp(subsystem, "block"))
105 mknod(blobmsg_get_string(tb[0]),
106 m | strtoul(blobmsg_data(tb[1]), NULL, 8),
107 makedev(atoi(major), atoi(minor)));
112 static void handle_rm(struct blob_attr *msg, struct blob_attr *data)
114 static struct blobmsg_policy rm_policy = {
115 .type = BLOBMSG_TYPE_STRING,
117 struct blob_attr *tb;
119 blobmsg_parse_array(&rm_policy, 1, &tb, blobmsg_data(data), blobmsg_data_len(data));
121 unlink(blobmsg_data(tb));
124 static void handle_exec(struct blob_attr *msg, struct blob_attr *data)
127 struct blob_attr *cur;
131 blobmsg_for_each_attr(cur, msg, rem)
132 setenv(blobmsg_name(cur), blobmsg_data(cur), 1);
134 blobmsg_for_each_attr(cur, data, rem) {
135 argv[i] = blobmsg_data(cur);
142 fd = open("/dev/null", O_RDWR);
144 dup2(fd, STDIN_FILENO);
145 dup2(fd, STDOUT_FILENO);
146 dup2(fd, STDERR_FILENO);
147 if (fd > STDERR_FILENO)
154 execvp(argv[0], &argv[0]);
159 static void handle_firmware(struct blob_attr *msg, struct blob_attr *data)
161 char *dir = blobmsg_get_string(blobmsg_data(data));
162 char *file = hotplug_msg_find_var(msg, "FIRMWARE");
163 char *dev = hotplug_msg_find_var(msg, "DEVPATH");
166 char *path, loadpath[256], syspath[256];
167 int fw, load, sys, len;
169 DEBUG(2, "Firmware request for %s/%s\n", dir, file);
171 if (!file || !dir || !dev) {
172 ERROR("Request for unknown firmware %s/%s\n", dir, file);
176 path = malloc(strlen(dir) + strlen(file) + 2);
178 ERROR("Failed to allocate memory\n");
181 sprintf(path, "%s/%s", dir, file);
183 if (stat(path, &s)) {
184 ERROR("Could not find firmware %s\n", path);
188 fw_data = malloc(s.st_size);
190 ERROR("Failed to allocate firmware data memory\n");
194 fw = open(path, O_RDONLY);
196 ERROR("Failed to open %s\n", path);
199 if (read(fw, fw_data, s.st_size) != s.st_size) {
200 ERROR("Failed to read firmware data\n");
205 snprintf(loadpath, sizeof(loadpath), "/sys/%s/loading", dev);
206 load = open(loadpath, O_WRONLY);
208 ERROR("Failed to open %s\n", loadpath);
214 snprintf(syspath, sizeof(syspath), "/sys/%s/data", dev);
215 sys = open(syspath, O_WRONLY);
217 ERROR("Failed to open %s\n", syspath);
223 write(fw, fw_data, 4096);
227 write(fw, fw_data, len);
230 load = open(loadpath, O_WRONLY);
234 DEBUG(2, "Done loading %s\n", path);
239 static struct cmd_handler {
242 void (*handler)(struct blob_attr *msg, struct blob_attr *data);
247 .handler = handle_makedev,
251 .handler = handle_rm,
254 .handler = handle_exec,
256 .name = "load-firmware",
257 .handler = handle_firmware,
261 static void queue_next(void)
265 if (queue_proc.pending || list_empty(&cmd_queue))
268 c = list_first_entry(&cmd_queue, struct cmd_queue, list);
270 queue_proc.pid = fork();
271 if (!queue_proc.pid) {
273 c->handler(c->msg, c->data);
280 if (queue_proc.pid <= 0) {
285 uloop_process_add(&queue_proc);
287 DEBUG(4, "Launched hotplug exec instance, pid=%d\n", (int) queue_proc.pid);
290 static void queue_proc_cb(struct uloop_process *c, int ret)
292 DEBUG(4, "Finished hotplug exec instance, pid=%d\n", (int) c->pid);
297 static void queue_add(struct cmd_handler *h, struct blob_attr *msg, struct blob_attr *data)
299 struct cmd_queue *c = NULL;
300 struct blob_attr *_msg, *_data;
302 c = calloc_a(sizeof(struct cmd_queue),
303 &_msg, blob_pad_len(msg),
304 &_data, blob_pad_len(data),
313 memcpy(c->msg, msg, blob_pad_len(msg));
314 memcpy(c->data, data, blob_pad_len(data));
315 c->handler = h->handler;
316 list_add_tail(&c->list, &cmd_queue);
320 static const char* rule_handle_var(struct json_script_ctx *ctx, const char *name, struct blob_attr *vars)
322 const char *str, *sep;
324 if (!strcmp(name, "DEVICENAME") || !strcmp(name, "DEVNAME")) {
325 str = json_script_find_var(ctx, vars, "DEVPATH");
329 sep = strrchr(str, '/');
339 static struct json_script_file *
340 rule_handle_file(struct json_script_ctx *ctx, const char *name)
344 obj = json_object_from_file((char*)name);
348 blob_buf_init(&script, 0);
349 blobmsg_add_json_element(&script, "", obj);
351 return json_script_file_from_blobmsg(name, blob_data(script.head), blob_len(script.head));
354 static void rule_handle_command(struct json_script_ctx *ctx, const char *name,
355 struct blob_attr *data, struct blob_attr *vars)
357 struct blob_attr *cur;
361 DEBUG(4, "Command: %s", name);
362 blobmsg_for_each_attr(cur, data, rem)
363 DEBUG(4, " %s", (char *) blobmsg_data(cur));
366 DEBUG(4, "Message:");
367 blobmsg_for_each_attr(cur, vars, rem)
368 DEBUG(4, " %s=%s", blobmsg_name(cur), (char *) blobmsg_data(cur));
372 for (i = 0; i < ARRAY_SIZE(handlers); i++)
373 if (!strcmp(handlers[i].name, name)) {
374 if (handlers[i].atomic)
375 handlers[i].handler(vars, data);
377 queue_add(&handlers[i], vars, data);
382 uloop_timeout_set(&last_event, HOTPLUG_WAIT);
385 static void rule_handle_error(struct json_script_ctx *ctx, const char *msg,
386 struct blob_attr *context)
390 s = blobmsg_format_json(context, false);
391 ERROR("ERROR: %s in block: %s\n", msg, s);
395 static struct json_script_ctx jctx = {
396 .handle_var = rule_handle_var,
397 .handle_error = rule_handle_error,
398 .handle_command = rule_handle_command,
399 .handle_file = rule_handle_file,
402 static void hotplug_handler_debug(struct blob_attr *data)
409 str = blobmsg_format_json(data, true);
410 DEBUG(3, "%s\n", str);
414 static void hotplug_handler(struct uloop_fd *u, unsigned int ev)
417 static char buf[4096];
418 int len = recv(u->fd, buf, sizeof(buf), MSG_DONTWAIT);
423 blob_buf_init(&b, 0);
424 index = blobmsg_open_table(&b, NULL);
426 int l = strlen(buf + i) + 1;
427 char *e = strstr(&buf[i], "=");
431 blobmsg_add_string(&b, &buf[i], &e[1]);
435 blobmsg_close_table(&b, index);
436 hotplug_handler_debug(b.head);
437 json_script_run(&jctx, rule_file, blob_data(b.head));
440 static struct uloop_fd hotplug_fd = {
441 .cb = hotplug_handler,
444 void hotplug_last_event(uloop_timeout_handler handler)
446 last_event.cb = handler;
448 uloop_timeout_set(&last_event, HOTPLUG_WAIT);
450 uloop_timeout_cancel(&last_event);
453 void hotplug(char *rules)
455 struct sockaddr_nl nls;
456 int nlbufsize = 512 * 1024;
458 rule_file = strdup(rules);
459 memset(&nls,0,sizeof(struct sockaddr_nl));
460 nls.nl_family = AF_NETLINK;
461 nls.nl_pid = getpid();
464 if ((hotplug_fd.fd = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT)) == -1) {
465 ERROR("Failed to open hotplug socket: %s\n", strerror(errno));
468 if (bind(hotplug_fd.fd, (void *)&nls, sizeof(struct sockaddr_nl))) {
469 ERROR("Failed to bind hotplug socket: %s\n", strerror(errno));
473 if (setsockopt(hotplug_fd.fd, SOL_SOCKET, SO_RCVBUFFORCE, &nlbufsize, sizeof(nlbufsize))) {
474 ERROR("Failed to resize receive buffer: %s\n", strerror(errno));
478 json_script_init(&jctx);
479 queue_proc.cb = queue_proc_cb;
480 uloop_fd_add(&hotplug_fd, ULOOP_READ);
483 int hotplug_run(char *rules)
492 void hotplug_shutdown(void)
494 uloop_fd_delete(&hotplug_fd);
495 close(hotplug_fd.fd);