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