13 #include <linux/limits.h>
14 #include <linux/auto_fs4.h>
16 #include <libubox/uloop.h>
17 #include <libubox/vlist.h>
18 #include <libubox/ulog.h>
19 #include <libubox/avl-cmp.h>
22 #include "libfstools/libfstools.h"
24 #define AUTOFS_MOUNT_PATH "/tmp/run/blockd/"
25 #define AUTOFS_TIMEOUT 30
26 #define AUTOFS_EXPIRE_TIMER (5 * 1000)
28 struct hotplug_context
{
29 struct uloop_process process
;
34 struct vlist_node node
;
35 struct blob_attr
*msg
;
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 };
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
},
77 static const struct blobmsg_policy info_policy
[__INFO_MAX
] = {
78 [INFO_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
82 _find_mount_point(char *device
)
86 if (asprintf(&dev
, "/dev/%s", device
) == -1)
89 mp
= find_mount_point(dev
, 0);
96 block(char *cmd
, char *action
, char *device
, int sync
, struct uloop_process
*process
)
101 char *argv
[5] = { 0 };
106 ULOG_ERR("failed to fork block process\n");
112 argv
[a
++] = "/sbin/block";
116 execvp(argv
[0], argv
);
117 ULOG_ERR("failed to spawn %s %s %s\n", *argv
, action
, device
);
121 if (!sync
&& process
) {
123 uloop_process_add(process
);
125 waitpid(pid
, &status
, 0);
126 ret
= WEXITSTATUS(status
);
128 ULOG_ERR("failed to run block. %s/%s\n", action
, device
);
136 static int send_block_notification(struct ubus_context
*ctx
, const char *action
,
137 const char *devname
, const char *target
);
138 static int hotplug_call_mount(struct ubus_context
*ctx
, const char *action
,
139 const char *devname
, uloop_process_handler cb
, void *priv
)
141 char * const argv
[] = { "hotplug-call", "mount", NULL
};
142 struct hotplug_context
*c
= NULL
;
147 c
= calloc(1, sizeof(*c
));
156 ULOG_ERR("fork() failed\n");
161 setenv("ACTION", action
, 1);
162 setenv("DEVICE", devname
, 1);
164 execv("/sbin/hotplug-call", argv
);
169 c
->process
.pid
= pid
;
172 uloop_process_add(&c
->process
);
180 static void device_mount_remove_hotplug_cb(struct uloop_process
*p
, int stat
)
182 struct hotplug_context
*hctx
= container_of(p
, struct hotplug_context
, process
);
183 struct device
*device
= hctx
->priv
;
187 unlink(device
->target
);
189 mp
= _find_mount_point(device
->name
);
191 block("autofs", "remove", device
->name
, 0, NULL
);
199 static void device_mount_remove(struct ubus_context
*ctx
, struct device
*device
)
201 static const char *action
= "remove";
203 hotplug_call_mount(ctx
, action
, device
->name
,
204 device_mount_remove_hotplug_cb
, device
);
206 send_block_notification(ctx
, action
, device
->name
, device
->target
);
209 static void device_mount_add(struct ubus_context
*ctx
, struct device
*device
)
214 if (asprintf(&path
, "/tmp/run/blockd/%s", device
->name
) == -1)
217 if (!lstat(device
->target
, &st
)) {
218 if (S_ISLNK(st
.st_mode
))
219 unlink(device
->target
);
220 else if (S_ISDIR(st
.st_mode
))
221 rmdir(device
->target
);
224 tmp
= strrchr(device
->target
, '/');
225 if (tmp
&& tmp
!= device
->target
&& tmp
!= &device
->target
[strlen(path
)-1]) {
227 mkdir_p(device
->target
, 0755);
231 if (symlink(path
, device
->target
)) {
232 ULOG_ERR("failed to symlink %s->%s (%d) - %m\n", device
->target
, path
, errno
);
234 static const char *action
= "add";
235 hotplug_call_mount(ctx
, action
, device
->name
, NULL
, NULL
);
236 send_block_notification(ctx
, action
, device
->name
, device
->target
);
242 device_move(struct device
*device_o
, struct device
*device_n
)
246 if (device_o
->autofs
!= device_n
->autofs
)
249 if (device_o
->anon
|| device_n
->anon
)
252 if (device_o
->autofs
) {
253 unlink(device_o
->target
);
254 if (asprintf(&path
, "/tmp/run/blockd/%s", device_n
->name
) == -1)
257 if (symlink(path
, device_n
->target
))
258 ULOG_ERR("failed to symlink %s->%s (%d) - %m\n", device_n
->target
, path
, errno
);
262 mkdir(device_n
->target
, 0755);
263 if (mount(device_o
->target
, device_n
->target
, NULL
, MS_MOVE
, NULL
))
264 rmdir(device_n
->target
);
266 rmdir(device_o
->target
);
272 static void vlist_nop_update(struct vlist_tree
*tree
,
273 struct vlist_node
*node_new
,
274 struct vlist_node
*node_old
)
278 VLIST_TREE(devices
, avl_strcmp
, vlist_nop_update
, false, false);
281 block_hotplug(struct ubus_context
*ctx
, struct ubus_object
*obj
,
282 struct ubus_request_data
*req
, const char *method
,
283 struct blob_attr
*msg
)
285 struct blob_attr
*data
[__MOUNT_MAX
];
286 struct device
*device
;
287 struct blob_attr
*_msg
;
288 char *devname
, *_name
;
289 char *target
= NULL
, *__target
;
290 char *_target
= NULL
;
292 blobmsg_parse(mount_policy
, __MOUNT_MAX
, data
, blob_data(msg
), blob_len(msg
));
294 if (!data
[MOUNT_DEVICE
])
295 return UBUS_STATUS_INVALID_ARGUMENT
;
297 devname
= blobmsg_get_string(data
[MOUNT_DEVICE
]);
299 if (data
[MOUNT_TARGET
]) {
300 target
= blobmsg_get_string(data
[MOUNT_TARGET
]);
302 if (asprintf(&_target
, "/mnt/%s",
303 blobmsg_get_string(data
[MOUNT_DEVICE
])) == -1)
309 if (data
[MOUNT_REMOVE
])
310 device
= vlist_find(&devices
, devname
, device
, node
);
312 device
= calloc_a(sizeof(*device
), &_msg
, blob_raw_len(msg
),
313 &_name
, strlen(devname
) + 1, &__target
, strlen(target
) + 1);
319 return UBUS_STATUS_UNKNOWN_ERROR
;
322 if (data
[MOUNT_REMOVE
]) {
323 vlist_delete(&devices
, &device
->node
);
326 device_mount_remove(ctx
, device
);
333 struct device
*old
= vlist_find(&devices
, devname
, device
, node
);
335 device
->autofs
= data
[MOUNT_AUTOFS
] ? blobmsg_get_u32(data
[MOUNT_AUTOFS
]) : 0;
336 device
->anon
= data
[MOUNT_ANON
] ? blobmsg_get_u32(data
[MOUNT_ANON
]) : 0;
338 memcpy(_msg
, msg
, blob_raw_len(msg
));
339 device
->name
= _name
;
340 strcpy(_name
, devname
);
341 device
->target
= __target
;
342 strcpy(__target
, target
);
346 vlist_add(&devices
, &device
->node
, device
->name
);
348 if (old
&& !device_move(old
, device
)) {
349 if (device
->autofs
) {
350 device_mount_remove(ctx
, old
);
351 device_mount_add(ctx
, device
);
353 block("mount", NULL
, NULL
, 0, NULL
);
355 } else if (device
->autofs
) {
356 device_mount_add(ctx
, device
);
363 static int blockd_mount(struct ubus_context
*ctx
, struct ubus_object
*obj
,
364 struct ubus_request_data
*req
, const char *method
,
365 struct blob_attr
*msg
)
367 static const char *action
= "add";
368 struct blob_attr
*data
[__MOUNT_MAX
];
369 struct device
*device
;
372 blobmsg_parse(mount_policy
, __MOUNT_MAX
, data
, blob_data(msg
), blob_len(msg
));
374 if (!data
[MOUNT_DEVICE
])
375 return UBUS_STATUS_INVALID_ARGUMENT
;
377 devname
= blobmsg_get_string(data
[MOUNT_DEVICE
]);
379 device
= vlist_find(&devices
, devname
, device
, node
);
381 return UBUS_STATUS_UNKNOWN_ERROR
;
383 hotplug_call_mount(ctx
, action
, device
->name
, NULL
, NULL
);
384 send_block_notification(ctx
, action
, device
->name
, device
->target
);
389 struct blockd_umount_context
{
390 struct ubus_context
*ctx
;
391 struct ubus_request_data req
;
394 static void blockd_umount_hotplug_cb(struct uloop_process
*p
, int stat
)
396 struct hotplug_context
*hctx
= container_of(p
, struct hotplug_context
, process
);
397 struct blockd_umount_context
*c
= hctx
->priv
;
399 ubus_complete_deferred_request(c
->ctx
, &c
->req
, 0);
405 static int blockd_umount(struct ubus_context
*ctx
, struct ubus_object
*obj
,
406 struct ubus_request_data
*req
, const char *method
,
407 struct blob_attr
*msg
)
409 struct blob_attr
*data
[__MOUNT_MAX
];
410 struct blockd_umount_context
*c
;
411 static const char *action
= "remove";
413 static char oldtarget
[PATH_MAX
];
414 struct device
*device
;
417 blobmsg_parse(mount_policy
, __MOUNT_MAX
, data
, blob_data(msg
), blob_len(msg
));
419 if (!data
[MOUNT_DEVICE
])
420 return UBUS_STATUS_INVALID_ARGUMENT
;
422 devname
= blobmsg_get_string(data
[MOUNT_DEVICE
]);
423 device
= vlist_find(&devices
, devname
, device
, node
);
425 strncpy(oldtarget
, device
->target
, sizeof(oldtarget
)-1);
426 oldtarget
[PATH_MAX
- 1] = '\0';
429 c
= calloc(1, sizeof(*c
));
431 return UBUS_STATUS_UNKNOWN_ERROR
;
434 ubus_defer_request(ctx
, req
, &c
->req
);
436 err
= hotplug_call_mount(ctx
, action
, devname
, blockd_umount_hotplug_cb
, c
);
439 return UBUS_STATUS_UNKNOWN_ERROR
;
442 send_block_notification(ctx
, action
, devname
, oldtarget
);
447 static void block_info_dump(struct blob_buf
*b
, struct device
*device
)
453 blob_for_each_attr(v
, device
->msg
, rem
)
454 blobmsg_add_blob(b
, v
);
456 mp
= _find_mount_point(device
->name
);
458 blobmsg_add_string(b
, "mount", mp
);
460 } else if (device
->autofs
&& device
->target
) {
461 blobmsg_add_string(b
, "mount", device
->target
);
466 block_info(struct ubus_context
*ctx
, struct ubus_object
*obj
,
467 struct ubus_request_data
*req
, const char *method
,
468 struct blob_attr
*msg
)
470 struct blob_attr
*data
[__INFO_MAX
];
471 struct device
*device
= NULL
;
473 blobmsg_parse(info_policy
, __INFO_MAX
, data
, blob_data(msg
), blob_len(msg
));
475 if (data
[INFO_DEVICE
]) {
476 device
= vlist_find(&devices
, blobmsg_get_string(data
[INFO_DEVICE
]), device
, node
);
478 return UBUS_STATUS_INVALID_ARGUMENT
;
481 blob_buf_init(&bb
, 0);
483 block_info_dump(&bb
, device
);
487 a
= blobmsg_open_array(&bb
, "devices");
488 vlist_for_each_element(&devices
, device
, node
) {
491 t
= blobmsg_open_table(&bb
, "");
492 block_info_dump(&bb
, device
);
493 blobmsg_close_table(&bb
, t
);
495 blobmsg_close_array(&bb
, a
);
497 ubus_send_reply(ctx
, req
, bb
.head
);
502 static const struct ubus_method block_methods
[] = {
503 UBUS_METHOD("hotplug", block_hotplug
, mount_policy
),
504 UBUS_METHOD("mount", blockd_mount
, mount_policy
),
505 UBUS_METHOD("umount", blockd_umount
, mount_policy
),
506 UBUS_METHOD("info", block_info
, info_policy
),
509 static struct ubus_object_type block_object_type
=
510 UBUS_OBJECT_TYPE("block", block_methods
);
512 static struct ubus_object block_object
= {
514 .type
= &block_object_type
,
515 .methods
= block_methods
,
516 .n_methods
= ARRAY_SIZE(block_methods
),
519 /* send ubus event for successful mounts, useful for procd triggers */
520 static int send_block_notification(struct ubus_context
*ctx
, const char *action
,
521 const char *devname
, const char *target
)
523 struct blob_buf buf
= { 0 };
524 char evname
[16] = "mount.";
530 strncat(evname
, action
, sizeof(evname
) - 1);
532 blob_buf_init(&buf
, 0);
535 blobmsg_add_string(&buf
, "device", devname
);
538 blobmsg_add_string(&buf
, "target", target
);
540 err
= ubus_notify(ctx
, &block_object
, evname
, buf
.head
, -1);
546 ubus_connect_handler(struct ubus_context
*ctx
)
550 ret
= ubus_add_object(ctx
, &block_object
);
552 fprintf(stderr
, "Failed to add object: %s\n", ubus_strerror(ret
));
555 static int autofs_umount(void)
557 umount2(AUTOFS_MOUNT_PATH
, MNT_DETACH
);
561 static void autofs_read_handler(struct uloop_fd
*u
, unsigned int events
)
563 union autofs_v5_packet_union pktu
;
564 const struct autofs_v5_packet
*pkt
;
565 int cmd
= AUTOFS_IOC_READY
;
568 while (read(u
->fd
, &pktu
, sizeof(pktu
)) == -1) {
574 if (pktu
.hdr
.type
!= autofs_ptype_missing_indirect
) {
575 ULOG_ERR("unknown packet type %d\n", pktu
.hdr
.type
);
579 pkt
= &pktu
.missing_indirect
;
580 ULOG_ERR("kernel is requesting a mount -> %s\n", pkt
->name
);
581 if (lstat(pkt
->name
, &st
) == -1)
582 if (block("autofs", "add", (char *)pkt
->name
, 1, NULL
))
583 cmd
= AUTOFS_IOC_FAIL
;
585 if (ioctl(fd_autofs_write
, cmd
, pkt
->wait_queue_token
) < 0)
586 ULOG_ERR("failed to report back to kernel\n");
589 static void autofs_expire(struct uloop_timeout
*t
)
591 struct autofs_packet_expire pkt
;
593 while (ioctl(fd_autofs_write
, AUTOFS_IOC_EXPIRE
, &pkt
) == 0)
594 block("autofs", "remove", pkt
.name
, 1, NULL
);
596 uloop_timeout_set(t
, AUTOFS_EXPIRE_TIMER
);
599 struct uloop_timeout autofs_expire_timer
= {
603 static int autofs_mount(void)
605 unsigned long autofs_timeout
= AUTOFS_TIMEOUT
;
611 if (pipe(pipefd
) < 0) {
612 ULOG_ERR("failed to get kernel pipe\n");
616 snprintf(source
, sizeof(source
), "mountd(pid%u)", getpid());
617 snprintf(opts
, sizeof(opts
), "fd=%d,pgrp=%u,minproto=5,maxproto=5", pipefd
[1], (unsigned) getpgrp());
618 mkdir(AUTOFS_MOUNT_PATH
, 0555);
619 if (mount(source
, AUTOFS_MOUNT_PATH
, "autofs", 0, opts
)) {
620 ULOG_ERR("unable to mount autofs on %s\n", AUTOFS_MOUNT_PATH
);
626 fd_autofs_read
.fd
= pipefd
[0];
627 fd_autofs_read
.cb
= autofs_read_handler
;
628 uloop_fd_add(&fd_autofs_read
, ULOOP_READ
);
630 fd_autofs_write
= open(AUTOFS_MOUNT_PATH
, O_RDONLY
);
631 if(fd_autofs_write
< 0) {
633 ULOG_ERR("failed to open direcory\n");
637 ioctl(fd_autofs_write
, AUTOFS_IOC_PROTOVER
, &kproto_version
);
638 if (kproto_version
!= 5) {
639 ULOG_ERR("only kernel protocol version 5 is tested. You have %d.\n",
643 if (ioctl(fd_autofs_write
, AUTOFS_IOC_SETTIMEOUT
, &autofs_timeout
))
644 ULOG_ERR("failed to set autofs timeout\n");
646 uloop_timeout_set(&autofs_expire_timer
, AUTOFS_EXPIRE_TIMER
);
648 fcntl(fd_autofs_write
, F_SETFD
, fcntl(fd_autofs_write
, F_GETFD
) | FD_CLOEXEC
);
649 fcntl(fd_autofs_read
.fd
, F_SETFD
, fcntl(fd_autofs_read
.fd
, F_GETFD
) | FD_CLOEXEC
);
654 static void blockd_startup_cb(struct uloop_process
*p
, int stat
)
656 send_block_notification(&conn
.ctx
, "ready", NULL
, NULL
);
659 static struct uloop_process startup_process
= {
660 .cb
= blockd_startup_cb
,
663 static void blockd_startup(struct uloop_timeout
*t
)
665 block("autofs", "start", NULL
, 0, &startup_process
);
668 struct uloop_timeout startup
= {
669 .cb
= blockd_startup
,
672 int main(int argc
, char **argv
)
674 /* make sure blockd is in it's own POSIX process group */
677 ulog_open(ULOG_SYSLOG
| ULOG_STDIO
, LOG_DAEMON
, "blockd");
682 conn
.cb
= ubus_connect_handler
;
683 ubus_auto_connect(&conn
);
685 uloop_timeout_set(&startup
, 1000);
692 vlist_flush_all(&devices
);