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
40 struct list_head list
;
42 struct blob_attr
*msg
;
43 struct blob_attr
*data
;
46 void (*handler
)(struct blob_attr
*msg
, struct blob_attr
*data
);
47 void (*start
)(struct blob_attr
*msg
, struct blob_attr
*data
);
48 void (*complete
)(struct blob_attr
*msg
, struct blob_attr
*data
, int ret
);
51 struct button_timeout
{
52 struct list_head list
;
53 struct uloop_timeout timeout
;
56 struct blob_attr
*data
;
59 static LIST_HEAD(cmd_queue
);
60 static LIST_HEAD(button_timer
);
61 static struct uloop_process queue_proc
;
62 static struct uloop_timeout last_event
;
63 static struct blob_buf b
, button_buf
;
64 static char *rule_file
;
65 static struct blob_buf script
;
66 static struct cmd_queue
*current
;
68 static void queue_add(struct cmd_handler
*h
, struct blob_attr
*msg
, struct blob_attr
*data
);
69 static void handle_button_complete(struct blob_attr
*msg
, struct blob_attr
*data
, int ret
);
71 static void button_free(struct button_timeout
*b
)
73 uloop_timeout_cancel(&b
->timeout
);
80 static void button_timeout_remove(char *button
)
82 struct button_timeout
*b
, *c
;
84 if (!list_empty(&button_timer
)) list_for_each_entry_safe(b
, c
, &button_timer
, list
) {
85 if (!strcmp(b
->name
, button
))
90 static char *hotplug_msg_find_var(struct blob_attr
*msg
, const char *name
)
92 struct blob_attr
*cur
;
95 blobmsg_for_each_attr(cur
, msg
, rem
) {
96 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
99 if (strcmp(blobmsg_name(cur
), name
) != 0)
102 return blobmsg_data(cur
);
108 static void mkdir_p(char *dir
)
110 char *l
= strrchr(dir
, '/');
120 static void handle_makedev(struct blob_attr
*msg
, struct blob_attr
*data
)
122 unsigned int oldumask
= umask(0);
123 static struct blobmsg_policy mkdev_policy
[2] = {
124 { .type
= BLOBMSG_TYPE_STRING
},
125 { .type
= BLOBMSG_TYPE_STRING
},
127 struct blob_attr
*tb
[2];
128 char *minor
= hotplug_msg_find_var(msg
, "MINOR");
129 char *major
= hotplug_msg_find_var(msg
, "MAJOR");
130 char *subsystem
= hotplug_msg_find_var(msg
, "SUBSYSTEM");
132 blobmsg_parse_array(mkdev_policy
, 2, tb
, blobmsg_data(data
), blobmsg_data_len(data
));
133 if (tb
[0] && tb
[1] && minor
&& major
&& subsystem
) {
135 char *d
= strdup(blobmsg_get_string(tb
[0]));
141 if (!strcmp(subsystem
, "block"))
143 mknod(blobmsg_get_string(tb
[0]),
144 m
| strtoul(blobmsg_data(tb
[1]), NULL
, 8),
145 makedev(atoi(major
), atoi(minor
)));
150 static void handle_rm(struct blob_attr
*msg
, struct blob_attr
*data
)
152 static struct blobmsg_policy rm_policy
= {
153 .type
= BLOBMSG_TYPE_STRING
,
155 struct blob_attr
*tb
;
157 blobmsg_parse_array(&rm_policy
, 1, &tb
, blobmsg_data(data
), blobmsg_data_len(data
));
159 unlink(blobmsg_data(tb
));
162 static void handle_exec(struct blob_attr
*msg
, struct blob_attr
*data
)
165 struct blob_attr
*cur
;
169 blobmsg_for_each_attr(cur
, msg
, rem
)
170 setenv(blobmsg_name(cur
), blobmsg_data(cur
), 1);
172 blobmsg_for_each_attr(cur
, data
, rem
) {
173 argv
[i
] = blobmsg_data(cur
);
180 fd
= open("/dev/null", O_RDWR
);
182 dup2(fd
, STDIN_FILENO
);
183 dup2(fd
, STDOUT_FILENO
);
184 dup2(fd
, STDERR_FILENO
);
185 if (fd
> STDERR_FILENO
)
192 execvp(argv
[0], &argv
[0]);
197 static void handle_button_start(struct blob_attr
*msg
, struct blob_attr
*data
)
199 char *button
= hotplug_msg_find_var(msg
, "BUTTON");
202 button_timeout_remove(button
);
205 static void handle_firmware(struct blob_attr
*msg
, struct blob_attr
*data
)
207 char *dir
= blobmsg_get_string(blobmsg_data(data
));
208 char *file
= hotplug_msg_find_var(msg
, "FIRMWARE");
209 char *dev
= hotplug_msg_find_var(msg
, "DEVPATH");
210 struct stat s
= { 0 };
211 char *path
, loadpath
[256], syspath
[256];
212 int fw
, src
, load
, len
;
213 static char buf
[4096];
215 DEBUG(2, "Firmware request for %s/%s\n", dir
, file
);
217 if (!file
|| !dir
|| !dev
) {
218 ERROR("Request for unknown firmware %s/%s\n", dir
, file
);
222 path
= alloca(strlen(dir
) + strlen(file
) + 2);
223 sprintf(path
, "%s/%s", dir
, file
);
225 if (stat(path
, &s
)) {
226 ERROR("Could not find firmware %s\n", path
);
232 src
= open(path
, O_RDONLY
);
234 ERROR("Failed to open %s\n", path
);
240 snprintf(loadpath
, sizeof(loadpath
), "/sys/%s/loading", dev
);
241 load
= open(loadpath
, O_WRONLY
);
243 ERROR("Failed to open %s\n", loadpath
);
246 if (write(load
, "1", 1) == -1) {
247 ERROR("Failed to write to %s\n", loadpath
);
252 snprintf(syspath
, sizeof(syspath
), "/sys/%s/data", dev
);
253 fw
= open(syspath
, O_WRONLY
);
255 ERROR("Failed to open %s\n", syspath
);
261 len
= read(src
, buf
, sizeof(buf
));
265 if (write(fw
, buf
, len
) == -1) {
266 ERROR("failed to write firmware file %s/%s to %s\n", dir
, file
, dev
);
275 load
= open(loadpath
, O_WRONLY
);
276 if (write(load
, "0", 1) == -1)
277 ERROR("failed to write to %s\n", loadpath
);
280 DEBUG(2, "Done loading %s\n", path
);
293 static struct cmd_handler
{
296 void (*handler
)(struct blob_attr
*msg
, struct blob_attr
*data
);
297 void (*start
)(struct blob_attr
*msg
, struct blob_attr
*data
);
298 void (*complete
)(struct blob_attr
*msg
, struct blob_attr
*data
, int ret
);
303 .handler
= handle_makedev
,
308 .handler
= handle_rm
,
312 .handler
= handle_exec
,
316 .handler
= handle_exec
,
317 .start
= handle_button_start
,
318 .complete
= handle_button_complete
,
321 .name
= "load-firmware",
322 .handler
= handle_firmware
,
326 static void queue_next(void)
330 if (queue_proc
.pending
|| list_empty(&cmd_queue
))
333 c
= list_first_entry(&cmd_queue
, struct cmd_queue
, list
);
335 queue_proc
.pid
= fork();
336 if (!queue_proc
.pid
) {
338 c
->handler(c
->msg
, c
->data
);
342 c
->start(c
->msg
, c
->data
);
348 if (queue_proc
.pid
<= 0) {
353 uloop_process_add(&queue_proc
);
355 DEBUG(4, "Launched hotplug exec instance, pid=%d\n", (int) queue_proc
.pid
);
358 static void queue_proc_cb(struct uloop_process
*c
, int ret
)
360 DEBUG(4, "Finished hotplug exec instance, pid=%d\n", (int) c
->pid
);
363 current
->complete(current
->msg
, current
->data
, ret
);
370 static void queue_add(struct cmd_handler
*h
, struct blob_attr
*msg
, struct blob_attr
*data
)
372 struct cmd_queue
*c
= NULL
;
373 struct blob_attr
*_msg
, *_data
;
375 c
= calloc_a(sizeof(struct cmd_queue
),
376 &_msg
, blob_pad_len(msg
),
377 &_data
, blob_pad_len(data
),
386 memcpy(c
->msg
, msg
, blob_pad_len(msg
));
387 memcpy(c
->data
, data
, blob_pad_len(data
));
388 c
->handler
= h
->handler
;
389 c
->complete
= h
->complete
;
391 list_add_tail(&c
->list
, &cmd_queue
);
395 static void handle_button_timeout(struct uloop_timeout
*t
)
397 struct button_timeout
*b
;
400 b
= container_of(t
, struct button_timeout
, timeout
);
401 blob_buf_init(&button_buf
, 0);
402 blobmsg_add_string(&button_buf
, "ACTION", "timeout");
403 snprintf(seen
, sizeof(seen
), "%d", b
->seen
);
404 blobmsg_add_string(&button_buf
, "SEEN", seen
);
405 queue_add(&handlers
[HANDLER_EXEC
], button_buf
.head
, b
->data
);
409 static void handle_button_complete(struct blob_attr
*msg
, struct blob_attr
*data
, int ret
)
411 char *name
= hotplug_msg_find_var(msg
, "BUTTON");
412 struct button_timeout
*b
;
413 int timeout
= ret
>> 8;
418 b
= malloc(sizeof(*b
));
422 memset(b
, 0, sizeof(*b
));
424 b
->data
= malloc(blob_pad_len(data
));
425 b
->name
= strdup(name
);
428 memcpy(b
->data
, data
, blob_pad_len(data
));
429 b
->timeout
.cb
= handle_button_timeout
;
431 uloop_timeout_set(&b
->timeout
, timeout
* 1000);
432 list_add(&b
->list
, &button_timer
);
435 static const char* rule_handle_var(struct json_script_ctx
*ctx
, const char *name
, struct blob_attr
*vars
)
437 const char *str
, *sep
;
439 if (!strcmp(name
, "DEVICENAME") || !strcmp(name
, "DEVNAME")) {
440 str
= json_script_find_var(ctx
, vars
, "DEVPATH");
444 sep
= strrchr(str
, '/');
454 static struct json_script_file
*
455 rule_handle_file(struct json_script_ctx
*ctx
, const char *name
)
459 obj
= json_object_from_file((char*)name
);
463 blob_buf_init(&script
, 0);
464 blobmsg_add_json_element(&script
, "", obj
);
466 return json_script_file_from_blobmsg(name
, blob_data(script
.head
), blob_len(script
.head
));
469 static void rule_handle_command(struct json_script_ctx
*ctx
, const char *name
,
470 struct blob_attr
*data
, struct blob_attr
*vars
)
472 struct blob_attr
*cur
;
476 DEBUG(4, "Command: %s", name
);
477 blobmsg_for_each_attr(cur
, data
, rem
)
478 DEBUG(4, " %s", (char *) blobmsg_data(cur
));
481 DEBUG(4, "Message:");
482 blobmsg_for_each_attr(cur
, vars
, rem
)
483 DEBUG(4, " %s=%s", blobmsg_name(cur
), (char *) blobmsg_data(cur
));
487 for (i
= 0; i
< ARRAY_SIZE(handlers
); i
++)
488 if (!strcmp(handlers
[i
].name
, name
)) {
489 if (handlers
[i
].atomic
)
490 handlers
[i
].handler(vars
, data
);
492 queue_add(&handlers
[i
], vars
, data
);
497 uloop_timeout_set(&last_event
, HOTPLUG_WAIT
);
500 static void rule_handle_error(struct json_script_ctx
*ctx
, const char *msg
,
501 struct blob_attr
*context
)
505 s
= blobmsg_format_json(context
, false);
506 ERROR("ERROR: %s in block: %s\n", msg
, s
);
510 static struct json_script_ctx jctx
= {
511 .handle_var
= rule_handle_var
,
512 .handle_error
= rule_handle_error
,
513 .handle_command
= rule_handle_command
,
514 .handle_file
= rule_handle_file
,
517 static void hotplug_handler_debug(struct blob_attr
*data
)
524 str
= blobmsg_format_json(data
, true);
525 DEBUG(3, "%s\n", str
);
529 static void hotplug_handler(struct uloop_fd
*u
, unsigned int ev
)
532 static char buf
[4096];
533 int len
= recv(u
->fd
, buf
, sizeof(buf
), MSG_DONTWAIT
);
538 blob_buf_init(&b
, 0);
539 index
= blobmsg_open_table(&b
, NULL
);
541 int l
= strlen(buf
+ i
) + 1;
542 char *e
= strstr(&buf
[i
], "=");
546 blobmsg_add_string(&b
, &buf
[i
], &e
[1]);
550 blobmsg_close_table(&b
, index
);
551 hotplug_handler_debug(b
.head
);
552 json_script_run(&jctx
, rule_file
, blob_data(b
.head
));
555 static struct uloop_fd hotplug_fd
= {
556 .cb
= hotplug_handler
,
559 void hotplug_last_event(uloop_timeout_handler handler
)
561 last_event
.cb
= handler
;
563 uloop_timeout_set(&last_event
, HOTPLUG_WAIT
);
565 uloop_timeout_cancel(&last_event
);
568 void hotplug(char *rules
)
570 struct sockaddr_nl nls
;
571 int nlbufsize
= 512 * 1024;
573 rule_file
= strdup(rules
);
574 memset(&nls
,0,sizeof(struct sockaddr_nl
));
575 nls
.nl_family
= AF_NETLINK
;
576 nls
.nl_pid
= getpid();
579 if ((hotplug_fd
.fd
= socket(PF_NETLINK
, SOCK_DGRAM
| SOCK_CLOEXEC
, NETLINK_KOBJECT_UEVENT
)) == -1) {
580 ERROR("Failed to open hotplug socket: %s\n", strerror(errno
));
583 if (bind(hotplug_fd
.fd
, (void *)&nls
, sizeof(struct sockaddr_nl
))) {
584 ERROR("Failed to bind hotplug socket: %s\n", strerror(errno
));
588 if (setsockopt(hotplug_fd
.fd
, SOL_SOCKET
, SO_RCVBUFFORCE
, &nlbufsize
, sizeof(nlbufsize
)))
589 ERROR("Failed to resize receive buffer: %s\n", strerror(errno
));
591 json_script_init(&jctx
);
592 queue_proc
.cb
= queue_proc_cb
;
593 uloop_fd_add(&hotplug_fd
, ULOOP_READ
);
596 int hotplug_run(char *rules
)
605 void hotplug_shutdown(void)
607 uloop_fd_delete(&hotplug_fd
);
608 close(hotplug_fd
.fd
);