hotplug: fix firmware loading
[project/procd.git] / plug / hotplug.c
1 /*
2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4 *
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
8 *
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.
13 */
14
15 #include <sys/stat.h>
16 #include <sys/socket.h>
17 #include <sys/types.h>
18
19 #include <linux/types.h>
20 #include <linux/netlink.h>
21
22 #include <libubox/blobmsg_json.h>
23 #include <libubox/json_script.h>
24 #include <libubox/uloop.h>
25
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <libgen.h>
30
31 #include "../procd.h"
32
33 #include "hotplug.h"
34
35 #define HOTPLUG_WAIT 500
36
37 struct cmd_queue {
38 struct list_head list;
39
40 struct blob_attr *msg;
41 struct blob_attr *data;
42 void (*handler)(struct blob_attr *msg, struct blob_attr *data);
43 };
44
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;
51
52 static char *hotplug_msg_find_var(struct blob_attr *msg, const char *name)
53 {
54 struct blob_attr *cur;
55 int rem;
56
57 blobmsg_for_each_attr(cur, msg, rem) {
58 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
59 continue;
60
61 if (strcmp(blobmsg_name(cur), name) != 0)
62 continue;
63
64 return blobmsg_data(cur);
65 }
66
67 return NULL;
68 }
69
70 static void mkdir_p(char *dir)
71 {
72 char *l = strrchr(dir, '/');
73
74 if (l) {
75 *l = '\0';
76 mkdir_p(dir);
77 *l = '/';
78 mkdir(dir, 0755);
79 }
80 }
81
82 static void handle_makedev(struct blob_attr *msg, struct blob_attr *data)
83 {
84 unsigned int oldumask = umask(0);
85 static struct blobmsg_policy mkdev_policy[2] = {
86 { .type = BLOBMSG_TYPE_STRING },
87 { .type = BLOBMSG_TYPE_STRING },
88 };
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");
93
94 blobmsg_parse_array(mkdev_policy, 2, tb, blobmsg_data(data), blobmsg_data_len(data));
95 if (tb[0] && tb[1] && minor && major && subsystem) {
96 mode_t m = S_IFCHR;
97 char *d = strdup(blobmsg_get_string(tb[0]));
98
99 d = dirname(d);
100 mkdir_p(d);
101 free(d);
102
103 if (!strcmp(subsystem, "block"))
104 m = S_IFBLK;
105 mknod(blobmsg_get_string(tb[0]),
106 m | strtoul(blobmsg_data(tb[1]), NULL, 8),
107 makedev(atoi(major), atoi(minor)));
108 }
109 umask(oldumask);
110 }
111
112 static void handle_rm(struct blob_attr *msg, struct blob_attr *data)
113 {
114 static struct blobmsg_policy rm_policy = {
115 .type = BLOBMSG_TYPE_STRING,
116 };
117 struct blob_attr *tb;
118
119 blobmsg_parse_array(&rm_policy, 1, &tb, blobmsg_data(data), blobmsg_data_len(data));
120 if (tb)
121 unlink(blobmsg_data(tb));
122 }
123
124 static void handle_exec(struct blob_attr *msg, struct blob_attr *data)
125 {
126 char *argv[8];
127 struct blob_attr *cur;
128 int rem, fd;
129 int i = 0;
130
131 blobmsg_for_each_attr(cur, msg, rem)
132 setenv(blobmsg_name(cur), blobmsg_data(cur), 1);
133
134 blobmsg_for_each_attr(cur, data, rem) {
135 argv[i] = blobmsg_data(cur);
136 i++;
137 if (i == 7)
138 break;
139 }
140
141 if (debug < 3) {
142 fd = open("/dev/null", O_RDWR);
143 if (fd > -1) {
144 dup2(fd, STDIN_FILENO);
145 dup2(fd, STDOUT_FILENO);
146 dup2(fd, STDERR_FILENO);
147 if (fd > STDERR_FILENO)
148 close(fd);
149 }
150 }
151
152 if (i > 0) {
153 argv[i] = NULL;
154 execvp(argv[0], &argv[0]);
155 }
156 exit(-1);
157 }
158
159 static void handle_firmware(struct blob_attr *msg, struct blob_attr *data)
160 {
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");
164 struct stat s = { 0 };
165 char *path, loadpath[256], syspath[256];
166 int fw, src, load, len;
167 static char buf[4096];
168
169 DEBUG(2, "Firmware request for %s/%s\n", dir, file);
170
171 if (!file || !dir || !dev) {
172 ERROR("Request for unknown firmware %s/%s\n", dir, file);
173 exit(-1);
174 }
175
176 path = alloca(strlen(dir) + strlen(file) + 2);
177 sprintf(path, "%s/%s", dir, file);
178
179 if (stat(path, &s)) {
180 ERROR("Could not find firmware %s\n", path);
181 src = -1;
182 s.st_size = 0;
183 goto send_to_kernel;
184 }
185
186 src = open(path, O_RDONLY);
187 if (src < 0) {
188 ERROR("Failed to open %s\n", path);
189 s.st_size = 0;
190 goto send_to_kernel;
191 }
192
193 send_to_kernel:
194 snprintf(loadpath, sizeof(loadpath), "/sys/%s/loading", dev);
195 load = open(loadpath, O_WRONLY);
196 if (!load) {
197 ERROR("Failed to open %s\n", loadpath);
198 exit(-1);
199 }
200 write(load, "1", 1);
201 close(load);
202
203 snprintf(syspath, sizeof(syspath), "/sys/%s/data", dev);
204 fw = open(syspath, O_WRONLY);
205 if (fw < 0) {
206 ERROR("Failed to open %s\n", syspath);
207 exit(-1);
208 }
209
210 len = s.st_size;
211 while (len) {
212 len = read(src, buf, sizeof(buf));
213 if (len <= 0)
214 break;
215
216 write(fw, buf, len);
217 }
218
219 if (src >= 0)
220 close(src);
221 close(fw);
222
223 load = open(loadpath, O_WRONLY);
224 write(load, "0", 1);
225 close(load);
226
227 DEBUG(2, "Done loading %s\n", path);
228
229 exit(-1);
230 }
231
232 static struct cmd_handler {
233 char *name;
234 int atomic;
235 void (*handler)(struct blob_attr *msg, struct blob_attr *data);
236 } handlers[] = {
237 {
238 .name = "makedev",
239 .atomic = 1,
240 .handler = handle_makedev,
241 }, {
242 .name = "rm",
243 .atomic = 1,
244 .handler = handle_rm,
245 }, {
246 .name = "exec",
247 .handler = handle_exec,
248 }, {
249 .name = "load-firmware",
250 .handler = handle_firmware,
251 },
252 };
253
254 static void queue_next(void)
255 {
256 struct cmd_queue *c;
257
258 if (queue_proc.pending || list_empty(&cmd_queue))
259 return;
260
261 c = list_first_entry(&cmd_queue, struct cmd_queue, list);
262
263 queue_proc.pid = fork();
264 if (!queue_proc.pid) {
265 uloop_done();
266 c->handler(c->msg, c->data);
267 exit(0);
268 }
269
270 list_del(&c->list);
271 free(c);
272
273 if (queue_proc.pid <= 0) {
274 queue_next();
275 return;
276 }
277
278 uloop_process_add(&queue_proc);
279
280 DEBUG(4, "Launched hotplug exec instance, pid=%d\n", (int) queue_proc.pid);
281 }
282
283 static void queue_proc_cb(struct uloop_process *c, int ret)
284 {
285 DEBUG(4, "Finished hotplug exec instance, pid=%d\n", (int) c->pid);
286
287 queue_next();
288 }
289
290 static void queue_add(struct cmd_handler *h, struct blob_attr *msg, struct blob_attr *data)
291 {
292 struct cmd_queue *c = NULL;
293 struct blob_attr *_msg, *_data;
294
295 c = calloc_a(sizeof(struct cmd_queue),
296 &_msg, blob_pad_len(msg),
297 &_data, blob_pad_len(data),
298 NULL);
299
300 c->msg = _msg;
301 c->data = _data;
302
303 if (!c)
304 return;
305
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);
310 queue_next();
311 }
312
313 static const char* rule_handle_var(struct json_script_ctx *ctx, const char *name, struct blob_attr *vars)
314 {
315 const char *str, *sep;
316
317 if (!strcmp(name, "DEVICENAME") || !strcmp(name, "DEVNAME")) {
318 str = json_script_find_var(ctx, vars, "DEVPATH");
319 if (!str)
320 return NULL;
321
322 sep = strrchr(str, '/');
323 if (sep)
324 return sep + 1;
325
326 return str;
327 }
328
329 return NULL;
330 }
331
332 static struct json_script_file *
333 rule_handle_file(struct json_script_ctx *ctx, const char *name)
334 {
335 json_object *obj;
336
337 obj = json_object_from_file((char*)name);
338 if (is_error(obj))
339 return NULL;
340
341 blob_buf_init(&script, 0);
342 blobmsg_add_json_element(&script, "", obj);
343
344 return json_script_file_from_blobmsg(name, blob_data(script.head), blob_len(script.head));
345 }
346
347 static void rule_handle_command(struct json_script_ctx *ctx, const char *name,
348 struct blob_attr *data, struct blob_attr *vars)
349 {
350 struct blob_attr *cur;
351 int rem, i;
352
353 if (debug > 3) {
354 DEBUG(4, "Command: %s", name);
355 blobmsg_for_each_attr(cur, data, rem)
356 DEBUG(4, " %s", (char *) blobmsg_data(cur));
357 DEBUG(4, "\n");
358
359 DEBUG(4, "Message:");
360 blobmsg_for_each_attr(cur, vars, rem)
361 DEBUG(4, " %s=%s", blobmsg_name(cur), (char *) blobmsg_data(cur));
362 DEBUG(4, "\n");
363 }
364
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);
369 else
370 queue_add(&handlers[i], vars, data);
371 break;
372 }
373
374 if (last_event.cb)
375 uloop_timeout_set(&last_event, HOTPLUG_WAIT);
376 }
377
378 static void rule_handle_error(struct json_script_ctx *ctx, const char *msg,
379 struct blob_attr *context)
380 {
381 char *s;
382
383 s = blobmsg_format_json(context, false);
384 ERROR("ERROR: %s in block: %s\n", msg, s);
385 free(s);
386 }
387
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,
393 };
394
395 static void hotplug_handler_debug(struct blob_attr *data)
396 {
397 char *str;
398
399 if (debug < 3)
400 return;
401
402 str = blobmsg_format_json(data, true);
403 DEBUG(3, "%s\n", str);
404 free(str);
405 }
406
407 static void hotplug_handler(struct uloop_fd *u, unsigned int ev)
408 {
409 int i = 0;
410 static char buf[4096];
411 int len = recv(u->fd, buf, sizeof(buf), MSG_DONTWAIT);
412 void *index;
413 if (len < 1)
414 return;
415
416 blob_buf_init(&b, 0);
417 index = blobmsg_open_table(&b, NULL);
418 while (i < len) {
419 int l = strlen(buf + i) + 1;
420 char *e = strstr(&buf[i], "=");
421
422 if (e) {
423 *e = '\0';
424 blobmsg_add_string(&b, &buf[i], &e[1]);
425 }
426 i += l;
427 }
428 blobmsg_close_table(&b, index);
429 hotplug_handler_debug(b.head);
430 json_script_run(&jctx, rule_file, blob_data(b.head));
431 }
432
433 static struct uloop_fd hotplug_fd = {
434 .cb = hotplug_handler,
435 };
436
437 void hotplug_last_event(uloop_timeout_handler handler)
438 {
439 last_event.cb = handler;
440 if (handler)
441 uloop_timeout_set(&last_event, HOTPLUG_WAIT);
442 else
443 uloop_timeout_cancel(&last_event);
444 }
445
446 void hotplug(char *rules)
447 {
448 struct sockaddr_nl nls;
449 int nlbufsize = 512 * 1024;
450
451 rule_file = strdup(rules);
452 memset(&nls,0,sizeof(struct sockaddr_nl));
453 nls.nl_family = AF_NETLINK;
454 nls.nl_pid = getpid();
455 nls.nl_groups = -1;
456
457 if ((hotplug_fd.fd = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT)) == -1) {
458 ERROR("Failed to open hotplug socket: %s\n", strerror(errno));
459 exit(1);
460 }
461 if (bind(hotplug_fd.fd, (void *)&nls, sizeof(struct sockaddr_nl))) {
462 ERROR("Failed to bind hotplug socket: %s\n", strerror(errno));
463 exit(1);
464 }
465
466 if (setsockopt(hotplug_fd.fd, SOL_SOCKET, SO_RCVBUFFORCE, &nlbufsize, sizeof(nlbufsize))) {
467 ERROR("Failed to resize receive buffer: %s\n", strerror(errno));
468 exit(1);
469 }
470
471 json_script_init(&jctx);
472 queue_proc.cb = queue_proc_cb;
473 uloop_fd_add(&hotplug_fd, ULOOP_READ);
474 }
475
476 int hotplug_run(char *rules)
477 {
478 uloop_init();
479 hotplug(rules);
480 uloop_run();
481
482 return 0;
483 }
484
485 void hotplug_shutdown(void)
486 {
487 uloop_fd_delete(&hotplug_fd);
488 close(hotplug_fd.fd);
489 }