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>
34 #define HOTPLUG_WAIT 500
37 struct list_head list
;
39 struct blob_attr
*msg
;
40 struct blob_attr
*data
;
41 void (*handler
)(struct blob_attr
*msg
, struct blob_attr
*data
);
44 static LIST_HEAD(cmd_queue
);
45 static struct uloop_process queue_proc
;
46 static struct uloop_timeout last_event
;
47 static struct blob_buf b
;
48 static char *rule_file
;
49 static struct blob_buf script
;
51 static char *hotplug_msg_find_var(struct blob_attr
*msg
, const char *name
)
53 struct blob_attr
*cur
;
56 blobmsg_for_each_attr(cur
, msg
, rem
) {
57 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
60 if (strcmp(blobmsg_name(cur
), name
) != 0)
63 return blobmsg_data(cur
);
69 static void mkdir_p(char *dir
)
71 char *l
= strrchr(dir
, '/');
81 static void handle_makedev(struct blob_attr
*msg
, struct blob_attr
*data
)
83 unsigned int oldumask
= umask(0);
84 static struct blobmsg_policy mkdev_policy
[2] = {
85 { .type
= BLOBMSG_TYPE_STRING
},
86 { .type
= BLOBMSG_TYPE_STRING
},
88 struct blob_attr
*tb
[2];
89 char *minor
= hotplug_msg_find_var(msg
, "MINOR");
90 char *major
= hotplug_msg_find_var(msg
, "MAJOR");
91 char *subsystem
= hotplug_msg_find_var(msg
, "SUBSYSTEM");
93 blobmsg_parse_array(mkdev_policy
, 2, tb
, blobmsg_data(data
), blobmsg_data_len(data
));
94 if (tb
[0] && tb
[1] && minor
&& major
&& subsystem
) {
96 char *d
= strdup(blobmsg_get_string(tb
[0]));
102 if (!strcmp(subsystem
, "block"))
104 mknod(blobmsg_get_string(tb
[0]),
105 m
| strtoul(blobmsg_data(tb
[1]), NULL
, 8),
106 makedev(atoi(major
), atoi(minor
)));
111 static void handle_rm(struct blob_attr
*msg
, struct blob_attr
*data
)
113 static struct blobmsg_policy rm_policy
= {
114 .type
= BLOBMSG_TYPE_STRING
,
116 struct blob_attr
*tb
;
118 blobmsg_parse_array(&rm_policy
, 1, &tb
, blobmsg_data(data
), blobmsg_data_len(data
));
120 unlink(blobmsg_data(tb
));
123 static void handle_exec(struct blob_attr
*msg
, struct blob_attr
*data
)
126 struct blob_attr
*cur
;
130 blobmsg_for_each_attr(cur
, msg
, rem
)
131 setenv(blobmsg_name(cur
), blobmsg_data(cur
), 1);
133 blobmsg_for_each_attr(cur
, data
, rem
) {
134 argv
[i
] = blobmsg_data(cur
);
142 close(STDOUT_FILENO
);
143 close(STDERR_FILENO
);
148 execvp(argv
[0], &argv
[0]);
153 static void handle_firmware(struct blob_attr
*msg
, struct blob_attr
*data
)
155 char *dir
= blobmsg_get_string(blobmsg_data(data
));
156 char *file
= hotplug_msg_find_var(msg
, "FIRMWARE");
157 char *dev
= hotplug_msg_find_var(msg
, "DEVPATH");
160 char *path
, loadpath
[256], syspath
[256];
161 int fw
, load
, sys
, len
;
163 DEBUG(1, "Firmware request for %s/%s\n", dir
, file
);
165 if (!file
|| !dir
|| !dev
) {
166 ERROR("Request for unknown firmware %s/%s\n", dir
, file
);
170 path
= malloc(strlen(dir
) + strlen(file
) + 2);
172 ERROR("Failed to allocate memory\n");
175 sprintf(path
, "%s/%s", dir
, file
);
177 if (stat(path
, &s
)) {
178 ERROR("Could not find firmware %s\n", path
);
182 fw_data
= malloc(s
.st_size
);
184 ERROR("Failed to allocate firmware data memory\n");
188 fw
= open(path
, O_RDONLY
);
190 ERROR("Failed to open %s\n", path
);
193 if (read(fw
, fw_data
, s
.st_size
) != s
.st_size
) {
194 ERROR("Failed to read firmware data\n");
199 snprintf(loadpath
, sizeof(loadpath
), "/sys/%s/loading", dev
);
200 load
= open(loadpath
, O_WRONLY
);
202 ERROR("Failed to open %s\n", loadpath
);
208 snprintf(syspath
, sizeof(syspath
), "/sys/%s/data", dev
);
209 sys
= open(syspath
, O_WRONLY
);
211 ERROR("Failed to open %s\n", syspath
);
217 write(fw
, fw_data
, 4096);
221 write(fw
, fw_data
, len
);
224 load
= open(loadpath
, O_WRONLY
);
228 DEBUG(1, "Done loading %s\n", path
);
233 static struct cmd_handler
{
236 void (*handler
)(struct blob_attr
*msg
, struct blob_attr
*data
);
241 .handler
= handle_makedev
,
245 .handler
= handle_rm
,
248 .handler
= handle_exec
,
250 .name
= "load-firmware",
251 .handler
= handle_firmware
,
255 static void queue_next(void)
259 if (queue_proc
.pending
|| list_empty(&cmd_queue
))
262 c
= list_first_entry(&cmd_queue
, struct cmd_queue
, list
);
264 queue_proc
.pid
= fork();
265 if (!queue_proc
.pid
) {
266 c
->handler(c
->msg
, c
->data
);
273 if (queue_proc
.pid
<= 0) {
278 uloop_process_add(&queue_proc
);
280 DEBUG(2, "Launched hotplug exec instance, pid=%d\n", (int) queue_proc
.pid
);
283 static void queue_proc_cb(struct uloop_process
*c
, int ret
)
285 DEBUG(2, "Finished hotplug exec instance, pid=%d\n", (int) c
->pid
);
290 static void queue_add(struct cmd_handler
*h
, struct blob_attr
*msg
, struct blob_attr
*data
)
292 struct cmd_queue
*c
= NULL
;
293 struct blob_attr
*_msg
, *_data
;
295 c
= calloc_a(sizeof(struct cmd_queue
),
296 &_msg
, blob_pad_len(msg
),
297 &_data
, blob_pad_len(data
),
306 memcpy(c
->msg
, msg
, blob_pad_len(msg
));
307 memcpy(c
->data
, data
, blob_pad_len(data
));
308 c
->handler
= h
->handler
;
309 list_add_tail(&c
->list
, &cmd_queue
);
313 static const char* rule_handle_var(struct json_script_ctx
*ctx
, const char *name
, struct blob_attr
*vars
)
315 const char *str
, *sep
;
317 if (!strcmp(name
, "DEVICENAME") || !strcmp(name
, "DEVNAME")) {
318 str
= json_script_find_var(ctx
, vars
, "DEVPATH");
322 sep
= strrchr(str
, '/');
332 static struct json_script_file
*
333 rule_handle_file(struct json_script_ctx
*ctx
, const char *name
)
337 obj
= json_object_from_file((char*)name
);
341 blob_buf_init(&script
, 0);
342 blobmsg_add_json_element(&script
, "", obj
);
344 return json_script_file_from_blobmsg(name
, blob_data(script
.head
), blob_len(script
.head
));
347 static void rule_handle_command(struct json_script_ctx
*ctx
, const char *name
,
348 struct blob_attr
*data
, struct blob_attr
*vars
)
350 struct blob_attr
*cur
;
354 DEBUG(2, "Command: %s", name
);
355 blobmsg_for_each_attr(cur
, data
, rem
)
356 DEBUG(2, " %s", (char *) blobmsg_data(cur
));
359 DEBUG(2, "Message:");
360 blobmsg_for_each_attr(cur
, vars
, rem
)
361 DEBUG(2, " %s=%s", blobmsg_name(cur
), (char *) blobmsg_data(cur
));
365 for (i
= 0; i
< ARRAY_SIZE(handlers
); i
++)
366 if (!strcmp(handlers
[i
].name
, name
)) {
367 if (handlers
[i
].atomic
)
368 handlers
[i
].handler(vars
, data
);
370 queue_add(&handlers
[i
], vars
, data
);
375 uloop_timeout_set(&last_event
, HOTPLUG_WAIT
);
378 static void rule_handle_error(struct json_script_ctx
*ctx
, const char *msg
,
379 struct blob_attr
*context
)
383 s
= blobmsg_format_json(context
, false);
384 ERROR("ERROR: %s in block: %s\n", msg
, s
);
388 static struct json_script_ctx jctx
= {
389 .handle_var
= rule_handle_var
,
390 .handle_error
= rule_handle_error
,
391 .handle_command
= rule_handle_command
,
392 .handle_file
= rule_handle_file
,
395 static void hotplug_handler(struct uloop_fd
*u
, unsigned int ev
)
398 static char buf
[4096];
399 int len
= recv(u
->fd
, buf
, sizeof(buf
), MSG_DONTWAIT
);
404 blob_buf_init(&b
, 0);
405 index
= blobmsg_open_table(&b
, NULL
);
407 int l
= strlen(buf
+ i
) + 1;
408 char *e
= strstr(&buf
[i
], "=");
412 blobmsg_add_string(&b
, &buf
[i
], &e
[1]);
416 blobmsg_close_table(&b
, index
);
417 DEBUG(3, "%s\n", blobmsg_format_json(b
.head
, true));
418 json_script_run(&jctx
, rule_file
, blob_data(b
.head
));
421 static struct uloop_fd hotplug_fd
= {
422 .cb
= hotplug_handler
,
425 void hotplug_last_event(uloop_timeout_handler handler
)
427 last_event
.cb
= handler
;
429 uloop_timeout_set(&last_event
, HOTPLUG_WAIT
);
431 uloop_timeout_cancel(&last_event
);
434 void hotplug(char *rules
)
436 struct sockaddr_nl nls
;
438 rule_file
= strdup(rules
);
439 memset(&nls
,0,sizeof(struct sockaddr_nl
));
440 nls
.nl_family
= AF_NETLINK
;
441 nls
.nl_pid
= getpid();
444 if ((hotplug_fd
.fd
= socket(PF_NETLINK
, SOCK_DGRAM
| SOCK_CLOEXEC
, NETLINK_KOBJECT_UEVENT
)) == -1) {
445 ERROR("Failed to open hotplug socket: %s\n", strerror(errno
));
448 if (bind(hotplug_fd
.fd
, (void *)&nls
, sizeof(struct sockaddr_nl
))) {
449 ERROR("Failed to bind hotplug socket: %s\n", strerror(errno
));
453 json_script_init(&jctx
);
454 queue_proc
.cb
= queue_proc_cb
;
455 uloop_fd_add(&hotplug_fd
, ULOOP_READ
);
458 void hotplug_shutdown(void)
460 uloop_fd_delete(&hotplug_fd
);
461 close(hotplug_fd
.fd
);