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 <json-c/json.h>
39 #define HOTPLUG_WAIT 500
43 struct list_head list
;
45 struct blob_attr
*msg
;
46 struct blob_attr
*data
;
49 void (*handler
)(struct blob_attr
*msg
, struct blob_attr
*data
);
50 void (*start
)(struct blob_attr
*msg
, struct blob_attr
*data
);
51 void (*complete
)(struct blob_attr
*msg
, struct blob_attr
*data
, int ret
);
54 struct button_timeout
{
55 struct list_head list
;
56 struct uloop_timeout timeout
;
59 struct blob_attr
*data
;
62 static LIST_HEAD(cmd_queue
);
63 static LIST_HEAD(button_timer
);
64 static struct uloop_process queue_proc
;
65 static struct uloop_timeout last_event
;
66 static struct blob_buf b
, button_buf
;
67 static char *rule_file
;
68 static struct blob_buf script
;
69 static struct cmd_queue
*current
;
71 static void queue_add(struct cmd_handler
*h
, struct blob_attr
*msg
, struct blob_attr
*data
);
72 static void handle_button_complete(struct blob_attr
*msg
, struct blob_attr
*data
, int ret
);
74 static void button_free(struct button_timeout
*b
)
76 uloop_timeout_cancel(&b
->timeout
);
83 static void button_timeout_remove(char *button
)
85 struct button_timeout
*b
, *c
;
87 if (!list_empty(&button_timer
)) list_for_each_entry_safe(b
, c
, &button_timer
, list
) {
88 if (!strcmp(b
->name
, button
))
93 static char *hotplug_msg_find_var(struct blob_attr
*msg
, const char *name
)
95 struct blob_attr
*cur
;
98 blobmsg_for_each_attr(cur
, msg
, rem
) {
99 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
102 if (strcmp(blobmsg_name(cur
), name
) != 0)
105 return blobmsg_data(cur
);
111 static void mkdir_p(char *dir
)
113 char *l
= strrchr(dir
, '/');
123 static void chgrp_error(const char *group
, const char *target
, const char *failed
)
125 ERROR("cannot set group %s for %s (%s: %d)\n",
126 group
, target
, failed
, errno
);
129 static void chgrp_target(struct blob_attr
*bgroup
, struct blob_attr
*btarget
)
132 struct group
*g
= NULL
;
133 const char *group
= blobmsg_get_string(bgroup
);
134 const char *target
= blobmsg_get_string(btarget
);
140 return chgrp_error(group
, target
, "getgrnam");
142 ret
= chown(target
, 0, g
->gr_gid
);
144 return chgrp_error(group
, target
, "chown");
147 static void handle_makedev(struct blob_attr
*msg
, struct blob_attr
*data
)
149 unsigned int oldumask
= umask(0);
150 static struct blobmsg_policy mkdev_policy
[3] = {
151 { .type
= BLOBMSG_TYPE_STRING
},
152 { .type
= BLOBMSG_TYPE_STRING
},
153 { .type
= BLOBMSG_TYPE_STRING
},
155 struct blob_attr
*tb
[3];
156 char *minor
= hotplug_msg_find_var(msg
, "MINOR");
157 char *major
= hotplug_msg_find_var(msg
, "MAJOR");
158 char *subsystem
= hotplug_msg_find_var(msg
, "SUBSYSTEM");
160 blobmsg_parse_array(mkdev_policy
, 3, tb
, blobmsg_data(data
), blobmsg_data_len(data
));
161 if (tb
[0] && tb
[1] && minor
&& major
&& subsystem
) {
163 char *d
= strdup(blobmsg_get_string(tb
[0]));
169 if (!strcmp(subsystem
, "block"))
171 mknod(blobmsg_get_string(tb
[0]),
172 m
| strtoul(blobmsg_data(tb
[1]), NULL
, 8),
173 makedev(atoi(major
), atoi(minor
)));
175 chgrp_target(tb
[2], tb
[0]);
180 static void handle_rm(struct blob_attr
*msg
, struct blob_attr
*data
)
182 static struct blobmsg_policy rm_policy
= {
183 .type
= BLOBMSG_TYPE_STRING
,
185 struct blob_attr
*tb
;
187 blobmsg_parse_array(&rm_policy
, 1, &tb
, blobmsg_data(data
), blobmsg_data_len(data
));
189 unlink(blobmsg_data(tb
));
192 static void handle_exec(struct blob_attr
*msg
, struct blob_attr
*data
)
195 struct blob_attr
*cur
;
199 blobmsg_for_each_attr(cur
, msg
, rem
)
200 setenv(blobmsg_name(cur
), blobmsg_data(cur
), 1);
202 blobmsg_for_each_attr(cur
, data
, rem
) {
203 argv
[i
] = blobmsg_data(cur
);
210 fd
= open("/dev/null", O_RDWR
);
212 dup2(fd
, STDIN_FILENO
);
213 dup2(fd
, STDOUT_FILENO
);
214 dup2(fd
, STDERR_FILENO
);
215 if (fd
> STDERR_FILENO
)
222 execvp(argv
[0], &argv
[0]);
227 static void handle_button_start(struct blob_attr
*msg
, struct blob_attr
*data
)
229 char *button
= hotplug_msg_find_var(msg
, "BUTTON");
232 button_timeout_remove(button
);
235 static void handle_firmware(struct blob_attr
*msg
, struct blob_attr
*data
)
237 char *dir
= blobmsg_get_string(blobmsg_data(data
));
238 char *file
= hotplug_msg_find_var(msg
, "FIRMWARE");
239 char *dev
= hotplug_msg_find_var(msg
, "DEVPATH");
240 struct stat s
= { 0 };
241 char *path
, loadpath
[256], syspath
[256];
242 int fw
, src
, load
, len
;
243 static char buf
[4096];
245 DEBUG(2, "Firmware request for %s/%s\n", dir
, file
);
247 if (!file
|| !dir
|| !dev
) {
248 ERROR("Request for unknown firmware %s/%s\n", dir
, file
);
252 path
= alloca(strlen(dir
) + strlen(file
) + 2);
253 sprintf(path
, "%s/%s", dir
, file
);
255 if (stat(path
, &s
)) {
256 ERROR("Could not find firmware %s: %m\n", path
);
262 src
= open(path
, O_RDONLY
);
264 ERROR("Failed to open %s: %m\n", path
);
270 snprintf(loadpath
, sizeof(loadpath
), "/sys/%s/loading", dev
);
271 load
= open(loadpath
, O_WRONLY
);
273 ERROR("Failed to open %s: %m\n", loadpath
);
276 if (write(load
, "1", 1) == -1) {
277 ERROR("Failed to write to %s: %m\n", loadpath
);
282 snprintf(syspath
, sizeof(syspath
), "/sys/%s/data", dev
);
283 fw
= open(syspath
, O_WRONLY
);
285 ERROR("Failed to open %s: %m\n", syspath
);
291 len
= read(src
, buf
, sizeof(buf
));
295 if (write(fw
, buf
, len
) == -1) {
296 ERROR("failed to write firmware file %s/%s to %s: %m\n", dir
, file
, dev
);
305 load
= open(loadpath
, O_WRONLY
);
306 if (write(load
, "0", 1) == -1)
307 ERROR("failed to write to %s: %m\n", loadpath
);
310 DEBUG(2, "Done loading %s\n", path
);
323 static struct cmd_handler
{
326 void (*handler
)(struct blob_attr
*msg
, struct blob_attr
*data
);
327 void (*start
)(struct blob_attr
*msg
, struct blob_attr
*data
);
328 void (*complete
)(struct blob_attr
*msg
, struct blob_attr
*data
, int ret
);
333 .handler
= handle_makedev
,
338 .handler
= handle_rm
,
342 .handler
= handle_exec
,
346 .handler
= handle_exec
,
347 .start
= handle_button_start
,
348 .complete
= handle_button_complete
,
351 .name
= "load-firmware",
352 .handler
= handle_firmware
,
356 static void queue_next(void)
360 if (queue_proc
.pending
|| list_empty(&cmd_queue
))
363 c
= list_first_entry(&cmd_queue
, struct cmd_queue
, list
);
365 queue_proc
.pid
= fork();
366 if (!queue_proc
.pid
) {
368 c
->handler(c
->msg
, c
->data
);
372 c
->start(c
->msg
, c
->data
);
378 if (queue_proc
.pid
<= 0) {
383 uloop_process_add(&queue_proc
);
385 DEBUG(4, "Launched hotplug exec instance, pid=%d\n", (int) queue_proc
.pid
);
388 static void queue_proc_cb(struct uloop_process
*c
, int ret
)
390 DEBUG(4, "Finished hotplug exec instance, pid=%d\n", (int) c
->pid
);
393 current
->complete(current
->msg
, current
->data
, ret
);
400 static void queue_add(struct cmd_handler
*h
, struct blob_attr
*msg
, struct blob_attr
*data
)
402 struct cmd_queue
*c
= NULL
;
403 struct blob_attr
*_msg
, *_data
;
405 c
= calloc_a(sizeof(struct cmd_queue
),
406 &_msg
, blob_pad_len(msg
),
407 &_data
, blob_pad_len(data
),
416 memcpy(c
->msg
, msg
, blob_pad_len(msg
));
417 memcpy(c
->data
, data
, blob_pad_len(data
));
418 c
->handler
= h
->handler
;
419 c
->complete
= h
->complete
;
421 list_add_tail(&c
->list
, &cmd_queue
);
425 static void handle_button_timeout(struct uloop_timeout
*t
)
427 struct button_timeout
*b
;
430 b
= container_of(t
, struct button_timeout
, timeout
);
431 blob_buf_init(&button_buf
, 0);
432 blobmsg_add_string(&button_buf
, "BUTTON", b
->name
);
433 blobmsg_add_string(&button_buf
, "ACTION", "timeout");
434 snprintf(seen
, sizeof(seen
), "%d", b
->seen
);
435 blobmsg_add_string(&button_buf
, "SEEN", seen
);
436 queue_add(&handlers
[HANDLER_EXEC
], button_buf
.head
, b
->data
);
440 static void handle_button_complete(struct blob_attr
*msg
, struct blob_attr
*data
, int ret
)
442 char *name
= hotplug_msg_find_var(msg
, "BUTTON");
443 struct button_timeout
*b
;
444 int timeout
= ret
>> 8;
452 b
= calloc(1, sizeof(*b
));
456 b
->data
= malloc(blob_pad_len(data
));
457 b
->name
= strdup(name
);
460 memcpy(b
->data
, data
, blob_pad_len(data
));
461 b
->timeout
.cb
= handle_button_timeout
;
463 uloop_timeout_set(&b
->timeout
, timeout
* 1000);
464 list_add(&b
->list
, &button_timer
);
467 static const char* rule_handle_var(struct json_script_ctx
*ctx
, const char *name
, struct blob_attr
*vars
)
469 const char *str
, *sep
;
471 if (!strcmp(name
, "DEVICENAME") || !strcmp(name
, "DEVNAME")) {
472 str
= json_script_find_var(ctx
, vars
, "DEVPATH");
476 sep
= strrchr(str
, '/');
486 static struct json_script_file
*
487 rule_handle_file(struct json_script_ctx
*ctx
, const char *name
)
491 obj
= json_object_from_file((char*)name
);
495 blob_buf_init(&script
, 0);
496 blobmsg_add_json_element(&script
, "", obj
);
498 return json_script_file_from_blobmsg(name
, blob_data(script
.head
), blob_len(script
.head
));
501 static void rule_handle_command(struct json_script_ctx
*ctx
, const char *name
,
502 struct blob_attr
*data
, struct blob_attr
*vars
)
504 struct blob_attr
*cur
;
508 DEBUG(4, "Command: %s\n", name
);
509 blobmsg_for_each_attr(cur
, data
, rem
)
510 DEBUG(4, " %s\n", (char *) blobmsg_data(cur
));
512 DEBUG(4, "Message:\n");
513 blobmsg_for_each_attr(cur
, vars
, rem
)
514 DEBUG(4, " %s=%s\n", blobmsg_name(cur
), (char *) blobmsg_data(cur
));
517 for (i
= 0; i
< ARRAY_SIZE(handlers
); i
++)
518 if (!strcmp(handlers
[i
].name
, name
)) {
519 if (handlers
[i
].atomic
)
520 handlers
[i
].handler(vars
, data
);
522 queue_add(&handlers
[i
], vars
, data
);
527 uloop_timeout_set(&last_event
, HOTPLUG_WAIT
);
530 static void rule_handle_error(struct json_script_ctx
*ctx
, const char *msg
,
531 struct blob_attr
*context
)
535 s
= blobmsg_format_json(context
, false);
536 ERROR("ERROR: %s in block: %s\n", msg
, s
);
540 static struct json_script_ctx jctx
= {
541 .handle_var
= rule_handle_var
,
542 .handle_error
= rule_handle_error
,
543 .handle_command
= rule_handle_command
,
544 .handle_file
= rule_handle_file
,
547 static void hotplug_handler_debug(struct blob_attr
*data
)
554 str
= blobmsg_format_json(data
, true);
555 DEBUG(3, "%s\n", str
);
559 static void hotplug_handler(struct uloop_fd
*u
, unsigned int ev
)
562 static char buf
[4096];
563 int len
= recv(u
->fd
, buf
, sizeof(buf
) - 1, MSG_DONTWAIT
);
570 blob_buf_init(&b
, 0);
571 index
= blobmsg_open_table(&b
, NULL
);
573 int l
= strlen(buf
+ i
) + 1;
574 char *e
= strstr(&buf
[i
], "=");
578 blobmsg_add_string(&b
, &buf
[i
], &e
[1]);
582 blobmsg_close_table(&b
, index
);
583 hotplug_handler_debug(b
.head
);
584 json_script_run(&jctx
, rule_file
, blob_data(b
.head
));
587 static struct uloop_fd hotplug_fd
= {
588 .cb
= hotplug_handler
,
591 void hotplug_last_event(uloop_timeout_handler handler
)
593 last_event
.cb
= handler
;
595 uloop_timeout_set(&last_event
, HOTPLUG_WAIT
);
597 uloop_timeout_cancel(&last_event
);
600 void hotplug(char *rules
)
602 struct sockaddr_nl nls
= {};
603 int nlbufsize
= 512 * 1024;
605 rule_file
= strdup(rules
);
606 nls
.nl_family
= AF_NETLINK
;
607 nls
.nl_pid
= getpid();
610 if ((hotplug_fd
.fd
= socket(PF_NETLINK
, SOCK_DGRAM
| SOCK_CLOEXEC
, NETLINK_KOBJECT_UEVENT
)) == -1) {
611 ERROR("Failed to open hotplug socket: %m\n");
614 if (bind(hotplug_fd
.fd
, (void *)&nls
, sizeof(struct sockaddr_nl
))) {
615 ERROR("Failed to bind hotplug socket: %m\n");
619 if (setsockopt(hotplug_fd
.fd
, SOL_SOCKET
, SO_RCVBUFFORCE
, &nlbufsize
, sizeof(nlbufsize
)))
620 ERROR("Failed to resize receive buffer: %m\n");
622 json_script_init(&jctx
);
623 queue_proc
.cb
= queue_proc_cb
;
624 uloop_fd_add(&hotplug_fd
, ULOOP_READ
);
627 int hotplug_run(char *rules
)
636 void hotplug_shutdown(void)
638 uloop_fd_delete(&hotplug_fd
);
639 close(hotplug_fd
.fd
);