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 <libubox/utils.h>
27 #include <json-c/json.h>
40 #define HOTPLUG_WAIT 500
44 struct list_head list
;
46 struct blob_attr
*msg
;
47 struct blob_attr
*data
;
50 void (*handler
)(struct blob_attr
*msg
, struct blob_attr
*data
);
51 void (*start
)(struct blob_attr
*msg
, struct blob_attr
*data
);
52 void (*complete
)(struct blob_attr
*msg
, struct blob_attr
*data
, int ret
);
55 struct button_timeout
{
56 struct list_head list
;
57 struct uloop_timeout timeout
;
60 struct blob_attr
*data
;
63 static LIST_HEAD(cmd_queue
);
64 static LIST_HEAD(button_timer
);
65 static struct uloop_process queue_proc
;
66 static struct uloop_timeout last_event
;
67 static struct blob_buf b
, button_buf
;
68 static char *rule_file
;
69 static struct blob_buf script
;
70 static struct cmd_queue
*current
;
72 static void queue_add(struct cmd_handler
*h
, struct blob_attr
*msg
, struct blob_attr
*data
);
73 static void handle_button_complete(struct blob_attr
*msg
, struct blob_attr
*data
, int ret
);
75 static void button_free(struct button_timeout
*b
)
77 uloop_timeout_cancel(&b
->timeout
);
84 static void button_timeout_remove(char *button
)
86 struct button_timeout
*b
, *c
;
88 if (!list_empty(&button_timer
)) list_for_each_entry_safe(b
, c
, &button_timer
, list
) {
89 if (!strcmp(b
->name
, button
))
94 static char *hotplug_msg_find_var(struct blob_attr
*msg
, const char *name
)
96 struct blob_attr
*cur
;
99 blobmsg_for_each_attr(cur
, msg
, rem
) {
100 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
103 if (strcmp(blobmsg_name(cur
), name
) != 0)
106 return blobmsg_data(cur
);
112 static void chgrp_error(const char *group
, const char *target
, const char *failed
)
114 ERROR("cannot set group %s for %s (%s: %d)\n",
115 group
, target
, failed
, errno
);
118 static void chgrp_target(struct blob_attr
*bgroup
, struct blob_attr
*btarget
)
121 struct group
*g
= NULL
;
122 const char *group
= blobmsg_get_string(bgroup
);
123 const char *target
= blobmsg_get_string(btarget
);
129 return chgrp_error(group
, target
, "getgrnam");
131 ret
= chown(target
, 0, g
->gr_gid
);
133 return chgrp_error(group
, target
, "chown");
136 static void handle_makedev(struct blob_attr
*msg
, struct blob_attr
*data
)
138 unsigned int oldumask
= umask(0);
139 static struct blobmsg_policy mkdev_policy
[3] = {
140 { .type
= BLOBMSG_TYPE_STRING
},
141 { .type
= BLOBMSG_TYPE_STRING
},
142 { .type
= BLOBMSG_TYPE_STRING
},
144 struct blob_attr
*tb
[3];
145 char *minor
= hotplug_msg_find_var(msg
, "MINOR");
146 char *major
= hotplug_msg_find_var(msg
, "MAJOR");
147 char *subsystem
= hotplug_msg_find_var(msg
, "SUBSYSTEM");
149 blobmsg_parse_array(mkdev_policy
, 3, tb
, blobmsg_data(data
), blobmsg_data_len(data
));
150 if (tb
[0] && tb
[1] && minor
&& major
&& subsystem
) {
152 char *d
= strdup(blobmsg_get_string(tb
[0]));
158 if (!strcmp(subsystem
, "block"))
160 mknod(blobmsg_get_string(tb
[0]),
161 m
| strtoul(blobmsg_data(tb
[1]), NULL
, 8),
162 makedev(atoi(major
), atoi(minor
)));
164 chgrp_target(tb
[2], tb
[0]);
169 static void handle_rm(struct blob_attr
*msg
, struct blob_attr
*data
)
171 static struct blobmsg_policy rm_policy
= {
172 .type
= BLOBMSG_TYPE_STRING
,
174 struct blob_attr
*tb
;
176 blobmsg_parse_array(&rm_policy
, 1, &tb
, blobmsg_data(data
), blobmsg_data_len(data
));
178 unlink(blobmsg_data(tb
));
181 static void handle_exec(struct blob_attr
*msg
, struct blob_attr
*data
)
184 struct blob_attr
*cur
;
188 blobmsg_for_each_attr(cur
, msg
, rem
)
189 setenv(blobmsg_name(cur
), blobmsg_data(cur
), 1);
191 blobmsg_for_each_attr(cur
, data
, rem
) {
192 argv
[i
] = blobmsg_data(cur
);
199 fd
= open("/dev/null", O_RDWR
);
201 dup2(fd
, STDIN_FILENO
);
202 dup2(fd
, STDOUT_FILENO
);
203 dup2(fd
, STDERR_FILENO
);
204 if (fd
> STDERR_FILENO
)
211 execvp(argv
[0], &argv
[0]);
216 static void handle_button_start(struct blob_attr
*msg
, struct blob_attr
*data
)
218 char *button
= hotplug_msg_find_var(msg
, "BUTTON");
221 button_timeout_remove(button
);
224 static void handle_firmware(struct blob_attr
*msg
, struct blob_attr
*data
)
226 char *dir
= blobmsg_get_string(blobmsg_data(data
));
227 char *file
= hotplug_msg_find_var(msg
, "FIRMWARE");
228 char *dev
= hotplug_msg_find_var(msg
, "DEVPATH");
229 struct stat s
= { 0 };
230 char *path
, loadpath
[256], syspath
[256];
231 int fw
, src
, load
, len
;
232 static char buf
[4096];
234 P_DEBUG(2, "Firmware request for %s/%s\n", dir
, file
);
236 if (!file
|| !dir
|| !dev
) {
237 ERROR("Request for unknown firmware %s/%s\n", dir
, file
);
241 path
= alloca(strlen(dir
) + strlen(file
) + 2);
242 sprintf(path
, "%s/%s", dir
, file
);
244 if (stat(path
, &s
)) {
245 ERROR("Could not find firmware %s: %m\n", path
);
251 src
= open(path
, O_RDONLY
);
253 ERROR("Failed to open %s: %m\n", path
);
259 snprintf(loadpath
, sizeof(loadpath
), "/sys/%s/loading", dev
);
260 load
= open(loadpath
, O_WRONLY
);
262 ERROR("Failed to open %s: %m\n", loadpath
);
265 if (write(load
, "1", 1) == -1) {
266 ERROR("Failed to write to %s: %m\n", loadpath
);
271 snprintf(syspath
, sizeof(syspath
), "/sys/%s/data", dev
);
272 fw
= open(syspath
, O_WRONLY
);
274 ERROR("Failed to open %s: %m\n", syspath
);
280 len
= read(src
, buf
, sizeof(buf
));
284 if (write(fw
, buf
, len
) == -1) {
285 ERROR("failed to write firmware file %s/%s to %s: %m\n", dir
, file
, dev
);
294 load
= open(loadpath
, O_WRONLY
);
295 if (write(load
, "0", 1) == -1)
296 ERROR("failed to write to %s: %m\n", loadpath
);
299 P_DEBUG(2, "Done loading %s\n", path
);
304 static void handle_start_console(struct blob_attr
*msg
, struct blob_attr
*data
)
306 char *dev
= blobmsg_get_string(blobmsg_data(data
));
308 P_DEBUG(2, "Start console request for %s\n", dev
);
310 procd_inittab_run("respawn");
311 procd_inittab_run("askfirst");
313 P_DEBUG(2, "Done starting console for %s\n", dev
);
324 HANDLER_START_CONSOLE
,
327 static struct cmd_handler
{
330 void (*handler
)(struct blob_attr
*msg
, struct blob_attr
*data
);
331 void (*start
)(struct blob_attr
*msg
, struct blob_attr
*data
);
332 void (*complete
)(struct blob_attr
*msg
, struct blob_attr
*data
, int ret
);
337 .handler
= handle_makedev
,
342 .handler
= handle_rm
,
346 .handler
= handle_exec
,
350 .handler
= handle_exec
,
351 .start
= handle_button_start
,
352 .complete
= handle_button_complete
,
355 .name
= "load-firmware",
356 .handler
= handle_firmware
,
358 [HANDLER_START_CONSOLE
] = {
359 .name
= "start-console",
360 .handler
= handle_start_console
,
364 static void queue_next(void)
368 if (queue_proc
.pending
|| list_empty(&cmd_queue
))
371 c
= list_first_entry(&cmd_queue
, struct cmd_queue
, list
);
373 queue_proc
.pid
= fork();
374 if (!queue_proc
.pid
) {
376 c
->handler(c
->msg
, c
->data
);
380 c
->start(c
->msg
, c
->data
);
386 if (queue_proc
.pid
<= 0) {
391 uloop_process_add(&queue_proc
);
393 P_DEBUG(4, "Launched hotplug exec instance, pid=%d\n", (int) queue_proc
.pid
);
396 static void queue_proc_cb(struct uloop_process
*c
, int ret
)
398 P_DEBUG(4, "Finished hotplug exec instance, pid=%d\n", (int) c
->pid
);
401 current
->complete(current
->msg
, current
->data
, ret
);
408 static void queue_add(struct cmd_handler
*h
, struct blob_attr
*msg
, struct blob_attr
*data
)
410 struct cmd_queue
*c
= NULL
;
411 struct blob_attr
*_msg
, *_data
;
413 c
= calloc_a(sizeof(struct cmd_queue
),
414 &_msg
, blob_pad_len(msg
),
415 &_data
, blob_pad_len(data
),
424 memcpy(c
->msg
, msg
, blob_pad_len(msg
));
425 memcpy(c
->data
, data
, blob_pad_len(data
));
426 c
->handler
= h
->handler
;
427 c
->complete
= h
->complete
;
429 list_add_tail(&c
->list
, &cmd_queue
);
433 static void handle_button_timeout(struct uloop_timeout
*t
)
435 struct button_timeout
*b
;
438 b
= container_of(t
, struct button_timeout
, timeout
);
439 blob_buf_init(&button_buf
, 0);
440 blobmsg_add_string(&button_buf
, "BUTTON", b
->name
);
441 blobmsg_add_string(&button_buf
, "ACTION", "timeout");
442 snprintf(seen
, sizeof(seen
), "%d", b
->seen
);
443 blobmsg_add_string(&button_buf
, "SEEN", seen
);
444 queue_add(&handlers
[HANDLER_EXEC
], button_buf
.head
, b
->data
);
448 static void handle_button_complete(struct blob_attr
*msg
, struct blob_attr
*data
, int ret
)
450 char *name
= hotplug_msg_find_var(msg
, "BUTTON");
451 struct button_timeout
*b
;
452 int timeout
= ret
>> 8;
460 b
= calloc(1, sizeof(*b
));
464 b
->data
= malloc(blob_pad_len(data
));
465 b
->name
= strdup(name
);
468 memcpy(b
->data
, data
, blob_pad_len(data
));
469 b
->timeout
.cb
= handle_button_timeout
;
471 uloop_timeout_set(&b
->timeout
, timeout
* 1000);
472 list_add(&b
->list
, &button_timer
);
475 static const char* rule_handle_var(struct json_script_ctx
*ctx
, const char *name
, struct blob_attr
*vars
)
477 const char *str
, *sep
;
479 if (!strcmp(name
, "DEVICENAME") || !strcmp(name
, "DEVNAME")) {
480 str
= json_script_find_var(ctx
, vars
, "DEVPATH");
484 sep
= strrchr(str
, '/');
494 static struct json_script_file
*
495 rule_handle_file(struct json_script_ctx
*ctx
, const char *name
)
499 obj
= json_object_from_file((char*)name
);
503 blob_buf_init(&script
, 0);
504 blobmsg_add_json_element(&script
, "", obj
);
506 return json_script_file_from_blobmsg(name
, blob_data(script
.head
), blob_len(script
.head
));
509 static void rule_handle_command(struct json_script_ctx
*ctx
, const char *name
,
510 struct blob_attr
*data
, struct blob_attr
*vars
)
512 struct blob_attr
*cur
;
516 P_DEBUG(4, "Command: %s\n", name
);
517 blobmsg_for_each_attr(cur
, data
, rem
)
518 P_DEBUG(4, " %s\n", (char *) blobmsg_data(cur
));
520 P_DEBUG(4, "Message:\n");
521 blobmsg_for_each_attr(cur
, vars
, rem
)
522 P_DEBUG(4, " %s=%s\n", blobmsg_name(cur
), (char *) blobmsg_data(cur
));
525 for (i
= 0; i
< ARRAY_SIZE(handlers
); i
++)
526 if (!strcmp(handlers
[i
].name
, name
)) {
527 if (handlers
[i
].atomic
)
528 handlers
[i
].handler(vars
, data
);
530 queue_add(&handlers
[i
], vars
, data
);
535 uloop_timeout_set(&last_event
, HOTPLUG_WAIT
);
538 static void rule_handle_error(struct json_script_ctx
*ctx
, const char *msg
,
539 struct blob_attr
*context
)
543 s
= blobmsg_format_json(context
, false);
544 ERROR("ERROR: %s in block: %s\n", msg
, s
);
548 static struct json_script_ctx jctx
= {
549 .handle_var
= rule_handle_var
,
550 .handle_error
= rule_handle_error
,
551 .handle_command
= rule_handle_command
,
552 .handle_file
= rule_handle_file
,
555 static void hotplug_handler_debug(struct blob_attr
*data
)
562 str
= blobmsg_format_json(data
, true);
563 P_DEBUG(3, "%s\n", str
);
567 static void hotplug_handler(struct uloop_fd
*u
, unsigned int ev
)
570 static char buf
[4096];
571 int len
= recv(u
->fd
, buf
, sizeof(buf
) - 1, MSG_DONTWAIT
);
578 blob_buf_init(&b
, 0);
579 index
= blobmsg_open_table(&b
, NULL
);
581 int l
= strlen(buf
+ i
) + 1;
582 char *e
= strstr(&buf
[i
], "=");
586 blobmsg_add_string(&b
, &buf
[i
], &e
[1]);
590 blobmsg_close_table(&b
, index
);
591 hotplug_handler_debug(b
.head
);
592 json_script_run(&jctx
, rule_file
, blob_data(b
.head
));
595 static struct uloop_fd hotplug_fd
= {
596 .cb
= hotplug_handler
,
599 void hotplug_last_event(uloop_timeout_handler handler
)
601 last_event
.cb
= handler
;
603 uloop_timeout_set(&last_event
, HOTPLUG_WAIT
);
605 uloop_timeout_cancel(&last_event
);
608 void hotplug(char *rules
)
610 struct sockaddr_nl nls
= {};
611 int nlbufsize
= 512 * 1024;
613 rule_file
= strdup(rules
);
614 nls
.nl_family
= AF_NETLINK
;
618 if ((hotplug_fd
.fd
= socket(PF_NETLINK
, SOCK_DGRAM
| SOCK_CLOEXEC
, NETLINK_KOBJECT_UEVENT
)) == -1) {
619 ERROR("Failed to open hotplug socket: %m\n");
622 if (bind(hotplug_fd
.fd
, (void *)&nls
, sizeof(struct sockaddr_nl
))) {
623 ERROR("Failed to bind hotplug socket: %m\n");
627 if (setsockopt(hotplug_fd
.fd
, SOL_SOCKET
, SO_RCVBUFFORCE
, &nlbufsize
, sizeof(nlbufsize
)))
628 ERROR("Failed to resize receive buffer: %m\n");
630 json_script_init(&jctx
);
631 queue_proc
.cb
= queue_proc_cb
;
632 uloop_fd_add(&hotplug_fd
, ULOOP_READ
);
635 int hotplug_run(char *rules
)
644 void hotplug_shutdown(void)
646 uloop_fd_delete(&hotplug_fd
);
647 close(hotplug_fd
.fd
);