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