94cb02e4bbe9b3f44b35c9d6aaab4c8c144ac2e9
[project/fstools.git] / blockd.c
1 #define _GNU_SOURCE
2 #include <sys/stat.h>
3 #include <sys/mount.h>
4 #include <sys/wait.h>
5
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <fcntl.h>
10
11 #include <errno.h>
12
13 #include <linux/limits.h>
14 #include <linux/auto_fs4.h>
15
16 #include <libubox/uloop.h>
17 #include <libubox/vlist.h>
18 #include <libubox/ulog.h>
19 #include <libubox/avl-cmp.h>
20 #include <libubus.h>
21
22 #include "libfstools/libfstools.h"
23
24 #define AUTOFS_MOUNT_PATH "/tmp/run/blockd/"
25 #define AUTOFS_TIMEOUT 30
26 #define AUTOFS_EXPIRE_TIMER (5 * 1000)
27
28 struct hotplug_context {
29 struct uloop_process process;
30 void *priv;
31 };
32
33 struct device {
34 struct vlist_node node;
35 struct blob_attr *msg;
36 char *name;
37 char *target;
38 int autofs;
39 int anon;
40 };
41
42 static struct uloop_fd fd_autofs_read;
43 static int fd_autofs_write = 0;
44 static struct ubus_auto_conn conn;
45 struct blob_buf bb = { 0 };
46
47 enum {
48 MOUNT_UUID,
49 MOUNT_LABEL,
50 MOUNT_ENABLE,
51 MOUNT_TARGET,
52 MOUNT_DEVICE,
53 MOUNT_OPTIONS,
54 MOUNT_AUTOFS,
55 MOUNT_ANON,
56 MOUNT_REMOVE,
57 __MOUNT_MAX
58 };
59
60 static const struct blobmsg_policy mount_policy[__MOUNT_MAX] = {
61 [MOUNT_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
62 [MOUNT_LABEL] = { .name = "label", .type = BLOBMSG_TYPE_STRING },
63 [MOUNT_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
64 [MOUNT_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
65 [MOUNT_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_STRING },
66 [MOUNT_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
67 [MOUNT_AUTOFS] = { .name = "autofs", .type = BLOBMSG_TYPE_INT32 },
68 [MOUNT_ANON] = { .name = "anon", .type = BLOBMSG_TYPE_INT32 },
69 [MOUNT_REMOVE] = { .name = "remove", .type = BLOBMSG_TYPE_INT32 },
70 };
71
72 enum {
73 INFO_DEVICE,
74 __INFO_MAX
75 };
76
77 static const struct blobmsg_policy info_policy[__INFO_MAX] = {
78 [INFO_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
79 };
80
81 static char*
82 _find_mount_point(char *device)
83 {
84 char *dev, *mp;
85
86 if (asprintf(&dev, "/dev/%s", device) == -1)
87 exit(ENOMEM);
88
89 mp = find_mount_point(dev, 0);
90 free(dev);
91
92 return mp;
93 }
94
95 static int
96 block(char *cmd, char *action, char *device, int sync, struct uloop_process *process)
97 {
98 pid_t pid = fork();
99 int ret = sync;
100 int status;
101 char *argv[5] = { 0 };
102 int a = 0;
103
104 switch (pid) {
105 case -1:
106 ULOG_ERR("failed to fork block process\n");
107 break;
108
109 case 0:
110 uloop_end();
111
112 argv[a++] = "/sbin/block";
113 argv[a++] = cmd;
114 argv[a++] = action;
115 argv[a++] = device;
116 execvp(argv[0], argv);
117 ULOG_ERR("failed to spawn %s %s %s\n", *argv, action, device);
118 exit(EXIT_FAILURE);
119
120 default:
121 if (!sync && process) {
122 process->pid = pid;
123 uloop_process_add(process);
124 } else if (sync) {
125 waitpid(pid, &status, 0);
126 ret = WEXITSTATUS(status);
127 if (ret)
128 ULOG_ERR("failed to run block. %s/%s\n", action, device);
129 }
130 break;
131 }
132
133 return ret;
134 }
135
136 static int send_block_notification(struct ubus_context *ctx, const char *action,
137 const char *devname);
138 static int hotplug_call_mount(struct ubus_context *ctx, const char *action,
139 const char *devname, uloop_process_handler cb, void *priv)
140 {
141 char * const argv[] = { "hotplug-call", "mount", NULL };
142 struct hotplug_context *c = NULL;
143 pid_t pid;
144 int err;
145
146 if (cb) {
147 c = calloc(1, sizeof(*c));
148 if (!c)
149 return -ENOMEM;
150 }
151
152 pid = fork();
153 switch (pid) {
154 case -1:
155 err = -errno;
156 ULOG_ERR("fork() failed\n");
157 return err;
158 case 0:
159 uloop_end();
160
161 setenv("ACTION", action, 1);
162 setenv("DEVICE", devname, 1);
163
164 execv("/sbin/hotplug-call", argv);
165 exit(-1);
166 break;
167 default:
168 if (c) {
169 c->process.pid = pid;
170 c->process.cb = cb;
171 c->priv = priv;
172 uloop_process_add(&c->process);
173 }
174 break;
175 }
176
177 send_block_notification(ctx, action, devname);
178
179 return 0;
180 }
181
182 static void device_mount_remove_hotplug_cb(struct uloop_process *p, int stat)
183 {
184 struct hotplug_context *hctx = container_of(p, struct hotplug_context, process);
185 struct device *device = hctx->priv;
186 char *mp;
187
188 if (device->target)
189 unlink(device->target);
190
191 mp = _find_mount_point(device->name);
192 if (mp) {
193 block("autofs", "remove", device->name, 0, NULL);
194 free(mp);
195 }
196
197 free(device);
198 free(hctx);
199 }
200
201 static void device_mount_remove(struct ubus_context *ctx, struct device *device)
202 {
203 hotplug_call_mount(ctx, "remove", device->name,
204 device_mount_remove_hotplug_cb, device);
205 }
206
207 static void device_mount_add(struct ubus_context *ctx, struct device *device)
208 {
209 struct stat st;
210 char *path, *tmp;
211
212 if (asprintf(&path, "/tmp/run/blockd/%s", device->name) == -1)
213 exit(ENOMEM);
214
215 if (!lstat(device->target, &st)) {
216 if (S_ISLNK(st.st_mode))
217 unlink(device->target);
218 else if (S_ISDIR(st.st_mode))
219 rmdir(device->target);
220 }
221
222 tmp = strrchr(device->target, '/');
223 if (tmp && tmp != device->target && tmp != &device->target[strlen(path)-1]) {
224 *tmp = '\0';
225 mkdir_p(device->target, 0755);
226 *tmp = '/';
227 }
228
229 if (symlink(path, device->target))
230 ULOG_ERR("failed to symlink %s->%s (%d) - %m\n", device->target, path, errno);
231 else
232 hotplug_call_mount(ctx, "add", device->name, NULL, NULL);
233
234 free(path);
235 }
236
237 static int
238 device_move(struct device *device_o, struct device *device_n)
239 {
240 char *path;
241
242 if (device_o->autofs != device_n->autofs)
243 return -1;
244
245 if (device_o->anon || device_n->anon)
246 return -1;
247
248 if (device_o->autofs) {
249 unlink(device_o->target);
250 if (asprintf(&path, "/tmp/run/blockd/%s", device_n->name) == -1)
251 exit(ENOMEM);
252
253 if (symlink(path, device_n->target))
254 ULOG_ERR("failed to symlink %s->%s (%d) - %m\n", device_n->target, path, errno);
255
256 free(path);
257 } else {
258 mkdir(device_n->target, 0755);
259 if (mount(device_o->target, device_n->target, NULL, MS_MOVE, NULL))
260 rmdir(device_n->target);
261 else
262 rmdir(device_o->target);
263 }
264
265 return 0;
266 }
267
268 static void vlist_nop_update(struct vlist_tree *tree,
269 struct vlist_node *node_new,
270 struct vlist_node *node_old)
271 {
272 }
273
274 VLIST_TREE(devices, avl_strcmp, vlist_nop_update, false, false);
275
276 static int
277 block_hotplug(struct ubus_context *ctx, struct ubus_object *obj,
278 struct ubus_request_data *req, const char *method,
279 struct blob_attr *msg)
280 {
281 struct blob_attr *data[__MOUNT_MAX];
282 struct device *device;
283 struct blob_attr *_msg;
284 char *devname, *_name;
285 char *target = NULL, *__target;
286 char *_target = NULL;
287
288 blobmsg_parse(mount_policy, __MOUNT_MAX, data, blob_data(msg), blob_len(msg));
289
290 if (!data[MOUNT_DEVICE])
291 return UBUS_STATUS_INVALID_ARGUMENT;
292
293 devname = blobmsg_get_string(data[MOUNT_DEVICE]);
294
295 if (data[MOUNT_TARGET]) {
296 target = blobmsg_get_string(data[MOUNT_TARGET]);
297 } else {
298 if (asprintf(&_target, "/mnt/%s",
299 blobmsg_get_string(data[MOUNT_DEVICE])) == -1)
300 exit(ENOMEM);
301
302 target = _target;
303 }
304
305 if (data[MOUNT_REMOVE])
306 device = vlist_find(&devices, devname, device, node);
307 else
308 device = calloc_a(sizeof(*device), &_msg, blob_raw_len(msg),
309 &_name, strlen(devname) + 1, &__target, strlen(target) + 1);
310
311 if (!device) {
312 if (_target)
313 free(_target);
314
315 return UBUS_STATUS_UNKNOWN_ERROR;
316 }
317
318 if (data[MOUNT_REMOVE]) {
319 vlist_delete(&devices, &device->node);
320
321 if (device->autofs)
322 device_mount_remove(ctx, device);
323 else
324 free(device);
325
326 if (_target)
327 free(_target);
328 } else {
329 struct device *old = vlist_find(&devices, devname, device, node);
330
331 device->autofs = data[MOUNT_AUTOFS] ? blobmsg_get_u32(data[MOUNT_AUTOFS]) : 0;
332 device->anon = data[MOUNT_ANON] ? blobmsg_get_u32(data[MOUNT_ANON]) : 0;
333 device->msg = _msg;
334 memcpy(_msg, msg, blob_raw_len(msg));
335 device->name = _name;
336 strcpy(_name, devname);
337 device->target = __target;
338 strcpy(__target, target);
339 if (_target)
340 free(_target);
341
342 vlist_add(&devices, &device->node, device->name);
343
344 if (old && !device_move(old, device)) {
345 if (device->autofs) {
346 device_mount_remove(ctx, old);
347 device_mount_add(ctx, device);
348 } else {
349 block("mount", NULL, NULL, 0, NULL);
350 }
351 } else if (device->autofs) {
352 device_mount_add(ctx, device);
353 }
354 }
355
356 return 0;
357 }
358
359 static int blockd_mount(struct ubus_context *ctx, struct ubus_object *obj,
360 struct ubus_request_data *req, const char *method,
361 struct blob_attr *msg)
362 {
363 struct blob_attr *data[__MOUNT_MAX];
364 struct device *device;
365 char *devname;
366
367 blobmsg_parse(mount_policy, __MOUNT_MAX, data, blob_data(msg), blob_len(msg));
368
369 if (!data[MOUNT_DEVICE])
370 return UBUS_STATUS_INVALID_ARGUMENT;
371
372 devname = blobmsg_get_string(data[MOUNT_DEVICE]);
373
374 device = vlist_find(&devices, devname, device, node);
375 if (!device)
376 return UBUS_STATUS_UNKNOWN_ERROR;
377
378 hotplug_call_mount(ctx, "add", device->name, NULL, NULL);
379
380 return 0;
381 }
382
383 struct blockd_umount_context {
384 struct ubus_context *ctx;
385 struct ubus_request_data req;
386 };
387
388 static void blockd_umount_hotplug_cb(struct uloop_process *p, int stat)
389 {
390 struct hotplug_context *hctx = container_of(p, struct hotplug_context, process);
391 struct blockd_umount_context *c = hctx->priv;
392
393 ubus_complete_deferred_request(c->ctx, &c->req, 0);
394
395 free(c);
396 free(hctx);
397 }
398
399 static int blockd_umount(struct ubus_context *ctx, struct ubus_object *obj,
400 struct ubus_request_data *req, const char *method,
401 struct blob_attr *msg)
402 {
403 struct blob_attr *data[__MOUNT_MAX];
404 struct blockd_umount_context *c;
405 char *devname;
406 int err;
407
408 blobmsg_parse(mount_policy, __MOUNT_MAX, data, blob_data(msg), blob_len(msg));
409
410 if (!data[MOUNT_DEVICE])
411 return UBUS_STATUS_INVALID_ARGUMENT;
412
413 devname = blobmsg_get_string(data[MOUNT_DEVICE]);
414
415 c = calloc(1, sizeof(*c));
416 if (!c)
417 return UBUS_STATUS_UNKNOWN_ERROR;
418
419 c->ctx = ctx;
420 ubus_defer_request(ctx, req, &c->req);
421
422 err = hotplug_call_mount(ctx, "remove", devname, blockd_umount_hotplug_cb, c);
423 if (err) {
424 free(c);
425 return UBUS_STATUS_UNKNOWN_ERROR;
426 }
427
428 return 0;
429 }
430
431 static void block_info_dump(struct blob_buf *b, struct device *device)
432 {
433 struct blob_attr *v;
434 char *mp;
435 int rem;
436
437 blob_for_each_attr(v, device->msg, rem)
438 blobmsg_add_blob(b, v);
439
440 mp = _find_mount_point(device->name);
441 if (mp) {
442 blobmsg_add_string(b, "mount", mp);
443 free(mp);
444 } else if (device->autofs && device->target) {
445 blobmsg_add_string(b, "mount", device->target);
446 }
447 }
448
449 static int
450 block_info(struct ubus_context *ctx, struct ubus_object *obj,
451 struct ubus_request_data *req, const char *method,
452 struct blob_attr *msg)
453 {
454 struct blob_attr *data[__INFO_MAX];
455 struct device *device = NULL;
456
457 blobmsg_parse(info_policy, __INFO_MAX, data, blob_data(msg), blob_len(msg));
458
459 if (data[INFO_DEVICE]) {
460 device = vlist_find(&devices, blobmsg_get_string(data[INFO_DEVICE]), device, node);
461 if (!device)
462 return UBUS_STATUS_INVALID_ARGUMENT;
463 }
464
465 blob_buf_init(&bb, 0);
466 if (device) {
467 block_info_dump(&bb, device);
468 } else {
469 void *a;
470
471 a = blobmsg_open_array(&bb, "devices");
472 vlist_for_each_element(&devices, device, node) {
473 void *t;
474
475 t = blobmsg_open_table(&bb, "");
476 block_info_dump(&bb, device);
477 blobmsg_close_table(&bb, t);
478 }
479 blobmsg_close_array(&bb, a);
480 }
481 ubus_send_reply(ctx, req, bb.head);
482
483 return 0;
484 }
485
486 static const struct ubus_method block_methods[] = {
487 UBUS_METHOD("hotplug", block_hotplug, mount_policy),
488 UBUS_METHOD("mount", blockd_mount, mount_policy),
489 UBUS_METHOD("umount", blockd_umount, mount_policy),
490 UBUS_METHOD("info", block_info, info_policy),
491 };
492
493 static struct ubus_object_type block_object_type =
494 UBUS_OBJECT_TYPE("block", block_methods);
495
496 static struct ubus_object block_object = {
497 .name = "block",
498 .type = &block_object_type,
499 .methods = block_methods,
500 .n_methods = ARRAY_SIZE(block_methods),
501 };
502
503 /* send ubus event for successful mounts, useful for procd triggers */
504 static int send_block_notification(struct ubus_context *ctx, const char *action,
505 const char *devname)
506 {
507 struct blob_buf buf = { 0 };
508 char evname[16] = "mount.";
509 int err;
510
511 if (!ctx)
512 return -ENXIO;
513
514 strncat(evname, action, sizeof(evname) - 1);
515
516 blob_buf_init(&buf, 0);
517
518 if (devname)
519 blobmsg_add_string(&buf, "devname", devname);
520
521 err = ubus_notify(ctx, &block_object, evname, buf.head, -1);
522
523 return err;
524 }
525
526 static void
527 ubus_connect_handler(struct ubus_context *ctx)
528 {
529 int ret;
530
531 ret = ubus_add_object(ctx, &block_object);
532 if (ret)
533 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
534 }
535
536 static int autofs_umount(void)
537 {
538 umount2(AUTOFS_MOUNT_PATH, MNT_DETACH);
539 return 0;
540 }
541
542 static void autofs_read_handler(struct uloop_fd *u, unsigned int events)
543 {
544 union autofs_v5_packet_union pktu;
545 const struct autofs_v5_packet *pkt;
546 int cmd = AUTOFS_IOC_READY;
547 struct stat st;
548
549 while (read(u->fd, &pktu, sizeof(pktu)) == -1) {
550 if (errno != EINTR)
551 return;
552 continue;
553 }
554
555 if (pktu.hdr.type != autofs_ptype_missing_indirect) {
556 ULOG_ERR("unknown packet type %d\n", pktu.hdr.type);
557 return;
558 }
559
560 pkt = &pktu.missing_indirect;
561 ULOG_ERR("kernel is requesting a mount -> %s\n", pkt->name);
562 if (lstat(pkt->name, &st) == -1)
563 if (block("autofs", "add", (char *)pkt->name, 1, NULL))
564 cmd = AUTOFS_IOC_FAIL;
565
566 if (ioctl(fd_autofs_write, cmd, pkt->wait_queue_token) < 0)
567 ULOG_ERR("failed to report back to kernel\n");
568 }
569
570 static void autofs_expire(struct uloop_timeout *t)
571 {
572 struct autofs_packet_expire pkt;
573
574 while (ioctl(fd_autofs_write, AUTOFS_IOC_EXPIRE, &pkt) == 0)
575 block("autofs", "remove", pkt.name, 1, NULL);
576
577 uloop_timeout_set(t, AUTOFS_EXPIRE_TIMER);
578 }
579
580 struct uloop_timeout autofs_expire_timer = {
581 .cb = autofs_expire,
582 };
583
584 static int autofs_mount(void)
585 {
586 unsigned long autofs_timeout = AUTOFS_TIMEOUT;
587 int kproto_version;
588 int pipefd[2];
589 char source[64];
590 char opts[64];
591
592 if (pipe(pipefd) < 0) {
593 ULOG_ERR("failed to get kernel pipe\n");
594 return -1;
595 }
596
597 snprintf(source, sizeof(source), "mountd(pid%u)", getpid());
598 snprintf(opts, sizeof(opts), "fd=%d,pgrp=%u,minproto=5,maxproto=5", pipefd[1], (unsigned) getpgrp());
599 mkdir(AUTOFS_MOUNT_PATH, 0555);
600 if (mount(source, AUTOFS_MOUNT_PATH, "autofs", 0, opts)) {
601 ULOG_ERR("unable to mount autofs on %s\n", AUTOFS_MOUNT_PATH);
602 close(pipefd[0]);
603 close(pipefd[1]);
604 return -1;
605 }
606 close(pipefd[1]);
607 fd_autofs_read.fd = pipefd[0];
608 fd_autofs_read.cb = autofs_read_handler;
609 uloop_fd_add(&fd_autofs_read, ULOOP_READ);
610
611 fd_autofs_write = open(AUTOFS_MOUNT_PATH, O_RDONLY);
612 if(fd_autofs_write < 0) {
613 autofs_umount();
614 ULOG_ERR("failed to open direcory\n");
615 return -1;
616 }
617
618 ioctl(fd_autofs_write, AUTOFS_IOC_PROTOVER, &kproto_version);
619 if (kproto_version != 5) {
620 ULOG_ERR("only kernel protocol version 5 is tested. You have %d.\n",
621 kproto_version);
622 exit(EXIT_FAILURE);
623 }
624 if (ioctl(fd_autofs_write, AUTOFS_IOC_SETTIMEOUT, &autofs_timeout))
625 ULOG_ERR("failed to set autofs timeout\n");
626
627 uloop_timeout_set(&autofs_expire_timer, AUTOFS_EXPIRE_TIMER);
628
629 fcntl(fd_autofs_write, F_SETFD, fcntl(fd_autofs_write, F_GETFD) | FD_CLOEXEC);
630 fcntl(fd_autofs_read.fd, F_SETFD, fcntl(fd_autofs_read.fd, F_GETFD) | FD_CLOEXEC);
631
632 return 0;
633 }
634
635 static void blockd_startup_cb(struct uloop_process *p, int stat)
636 {
637 send_block_notification(&conn.ctx, "ready", NULL);
638 }
639
640 static struct uloop_process startup_process = {
641 .cb = blockd_startup_cb,
642 };
643
644 static void blockd_startup(struct uloop_timeout *t)
645 {
646 block("autofs", "start", NULL, 0, &startup_process);
647 }
648
649 struct uloop_timeout startup = {
650 .cb = blockd_startup,
651 };
652
653 int main(int argc, char **argv)
654 {
655 /* make sure blockd is in it's own POSIX process group */
656 setpgrp();
657
658 ulog_open(ULOG_SYSLOG | ULOG_STDIO, LOG_DAEMON, "blockd");
659 uloop_init();
660
661 autofs_mount();
662
663 conn.cb = ubus_connect_handler;
664 ubus_auto_connect(&conn);
665
666 uloop_timeout_set(&startup, 1000);
667
668 uloop_run();
669 uloop_done();
670
671 autofs_umount();
672
673 vlist_flush_all(&devices);
674
675 return 0;
676 }