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
},
73 _find_mount_point(char *device
)
77 snprintf(dev
, sizeof(dev
), "/dev/%s", device
);
79 return find_mount_point(dev
, 0);
83 block(char *cmd
, char *action
, char *device
)
88 char *argv
[5] = { 0 };
93 ULOG_ERR("failed to fork block process\n");
97 argv
[a
++] = "/sbin/block";
101 execvp(argv
[0], argv
);
102 ULOG_ERR("failed to spawn %s %s %s\n", *argv
, action
, device
);
106 waitpid(pid
, &status
, 0);
107 ret
= WEXITSTATUS(status
);
109 ULOG_ERR("failed to run block. %s/%s\n", action
, device
);
116 static int hotplug_call_mount(const char *action
, const char *devname
,
117 uloop_process_handler cb
, void *priv
)
119 char * const argv
[] = { "hotplug-call", "mount", NULL
};
120 struct hotplug_context
*c
= NULL
;
125 c
= calloc(1, sizeof(*c
));
134 ULOG_ERR("fork() failed\n");
139 setenv("ACTION", action
, 1);
140 setenv("DEVICE", devname
, 1);
142 execv("/sbin/hotplug-call", argv
);
147 c
->process
.pid
= pid
;
150 uloop_process_add(&c
->process
);
158 static void device_mount_remove_hotplug_cb(struct uloop_process
*p
, int stat
)
160 struct hotplug_context
*hctx
= container_of(p
, struct hotplug_context
, process
);
161 struct device
*device
= hctx
->priv
;
165 unlink(device
->target
);
167 mp
= _find_mount_point(device
->name
);
169 block("autofs", "remove", device
->name
);
177 static void device_mount_remove(struct device
*device
)
179 hotplug_call_mount("remove", device
->name
,
180 device_mount_remove_hotplug_cb
, device
);
183 static void device_mount_add(struct device
*device
)
188 snprintf(path
, sizeof(path
), "/tmp/run/blockd/%s", device
->name
);
189 if (!lstat(device
->target
, &st
)) {
190 if (S_ISLNK(st
.st_mode
))
191 unlink(device
->target
);
192 else if (S_ISDIR(st
.st_mode
))
193 rmdir(device
->target
);
195 if (symlink(path
, device
->target
))
196 ULOG_ERR("failed to symlink %s->%s (%d) - %m\n", device
->target
, path
, errno
);
198 hotplug_call_mount("add", device
->name
, NULL
, NULL
);
202 device_move(struct device
*device_o
, struct device
*device_n
)
206 if (device_o
->autofs
!= device_n
->autofs
)
209 if (device_o
->anon
|| device_n
->anon
)
212 if (device_o
->autofs
) {
213 unlink(device_o
->target
);
214 snprintf(path
, sizeof(path
), "/tmp/run/blockd/%s", device_n
->name
);
215 if (symlink(path
, device_n
->target
))
216 ULOG_ERR("failed to symlink %s->%s (%d) - %m\n", device_n
->target
, path
, errno
);
218 mkdir(device_n
->target
, 0755);
219 if (mount(device_o
->target
, device_n
->target
, NULL
, MS_MOVE
, NULL
))
220 rmdir(device_n
->target
);
222 rmdir(device_o
->target
);
228 static void vlist_nop_update(struct vlist_tree
*tree
,
229 struct vlist_node
*node_new
,
230 struct vlist_node
*node_old
)
234 VLIST_TREE(devices
, avl_strcmp
, vlist_nop_update
, false, false);
237 block_hotplug(struct ubus_context
*ctx
, struct ubus_object
*obj
,
238 struct ubus_request_data
*req
, const char *method
,
239 struct blob_attr
*msg
)
241 struct blob_attr
*data
[__MOUNT_MAX
];
242 struct device
*device
;
243 struct blob_attr
*_msg
;
244 char *devname
, *_name
;
245 char *target
= NULL
, *__target
;
248 blobmsg_parse(mount_policy
, __MOUNT_MAX
, data
, blob_data(msg
), blob_len(msg
));
250 if (!data
[MOUNT_DEVICE
])
251 return UBUS_STATUS_INVALID_ARGUMENT
;
253 devname
= blobmsg_get_string(data
[MOUNT_DEVICE
]);
255 if (data
[MOUNT_TARGET
]) {
256 target
= blobmsg_get_string(data
[MOUNT_TARGET
]);
258 snprintf(_target
, sizeof(_target
), "/mnt/%s",
259 blobmsg_get_string(data
[MOUNT_DEVICE
]));
263 if (data
[MOUNT_REMOVE
])
264 device
= vlist_find(&devices
, devname
, device
, node
);
266 device
= calloc_a(sizeof(*device
), &_msg
, blob_raw_len(msg
),
267 &_name
, strlen(devname
) + 1, &__target
, strlen(target
) + 1);
270 return UBUS_STATUS_UNKNOWN_ERROR
;
272 if (data
[MOUNT_REMOVE
]) {
273 vlist_delete(&devices
, &device
->node
);
276 device_mount_remove(device
);
280 struct device
*old
= vlist_find(&devices
, devname
, device
, node
);
282 device
->autofs
= data
[MOUNT_AUTOFS
] ? blobmsg_get_u32(data
[MOUNT_AUTOFS
]) : 0;
283 device
->anon
= data
[MOUNT_ANON
] ? blobmsg_get_u32(data
[MOUNT_ANON
]) : 0;
285 memcpy(_msg
, msg
, blob_raw_len(msg
));
286 device
->name
= _name
;
287 strcpy(_name
, devname
);
288 device
->target
= __target
;
289 strcpy(__target
, target
);
291 vlist_add(&devices
, &device
->node
, device
->name
);
293 if (old
&& !device_move(old
, device
)) {
294 if (device
->autofs
) {
295 device_mount_remove(old
);
296 device_mount_add(device
);
298 block("mount", NULL
, NULL
);
300 } else if (device
->autofs
) {
301 device_mount_add(device
);
308 static int blockd_mount(struct ubus_context
*ctx
, struct ubus_object
*obj
,
309 struct ubus_request_data
*req
, const char *method
,
310 struct blob_attr
*msg
)
312 struct blob_attr
*data
[__MOUNT_MAX
];
313 struct device
*device
;
316 blobmsg_parse(mount_policy
, __MOUNT_MAX
, data
, blob_data(msg
), blob_len(msg
));
318 if (!data
[MOUNT_DEVICE
])
319 return UBUS_STATUS_INVALID_ARGUMENT
;
321 devname
= blobmsg_get_string(data
[MOUNT_DEVICE
]);
323 device
= vlist_find(&devices
, devname
, device
, node
);
325 return UBUS_STATUS_UNKNOWN_ERROR
;
327 hotplug_call_mount("add", device
->name
, NULL
, NULL
);
332 struct blockd_umount_context
{
333 struct ubus_context
*ctx
;
334 struct ubus_request_data req
;
337 static void blockd_umount_hotplug_cb(struct uloop_process
*p
, int stat
)
339 struct hotplug_context
*hctx
= container_of(p
, struct hotplug_context
, process
);
340 struct blockd_umount_context
*c
= hctx
->priv
;
342 ubus_complete_deferred_request(c
->ctx
, &c
->req
, 0);
348 static int blockd_umount(struct ubus_context
*ctx
, struct ubus_object
*obj
,
349 struct ubus_request_data
*req
, const char *method
,
350 struct blob_attr
*msg
)
352 struct blob_attr
*data
[__MOUNT_MAX
];
353 struct blockd_umount_context
*c
;
357 blobmsg_parse(mount_policy
, __MOUNT_MAX
, data
, blob_data(msg
), blob_len(msg
));
359 if (!data
[MOUNT_DEVICE
])
360 return UBUS_STATUS_INVALID_ARGUMENT
;
362 devname
= blobmsg_get_string(data
[MOUNT_DEVICE
]);
364 c
= calloc(1, sizeof(*c
));
366 return UBUS_STATUS_UNKNOWN_ERROR
;
369 ubus_defer_request(ctx
, req
, &c
->req
);
371 err
= hotplug_call_mount("remove", devname
, blockd_umount_hotplug_cb
, c
);
374 return UBUS_STATUS_UNKNOWN_ERROR
;
381 block_info(struct ubus_context
*ctx
, struct ubus_object
*obj
,
382 struct ubus_request_data
*req
, const char *method
,
383 struct blob_attr
*msg
)
385 struct device
*device
;
388 blob_buf_init(&bb
, 0);
389 a
= blobmsg_open_array(&bb
, "devices");
390 vlist_for_each_element(&devices
, device
, node
) {
391 void *t
= blobmsg_open_table(&bb
, "");
396 blob_for_each_attr(v
, device
->msg
, rem
)
397 blobmsg_add_blob(&bb
, v
);
399 mp
= _find_mount_point(device
->name
);
401 blobmsg_add_string(&bb
, "mount", mp
);
403 } else if (device
->autofs
&& device
->target
) {
404 blobmsg_add_string(&bb
, "mount", device
->target
);
406 blobmsg_close_table(&bb
, t
);
408 blobmsg_close_array(&bb
, a
);
409 ubus_send_reply(ctx
, req
, bb
.head
);
414 static const struct ubus_method block_methods
[] = {
415 UBUS_METHOD("hotplug", block_hotplug
, mount_policy
),
416 UBUS_METHOD("mount", blockd_mount
, mount_policy
),
417 UBUS_METHOD("umount", blockd_umount
, mount_policy
),
418 UBUS_METHOD_NOARG("info", block_info
),
421 static struct ubus_object_type block_object_type
=
422 UBUS_OBJECT_TYPE("block", block_methods
);
424 static struct ubus_object block_object
= {
426 .type
= &block_object_type
,
427 .methods
= block_methods
,
428 .n_methods
= ARRAY_SIZE(block_methods
),
432 ubus_connect_handler(struct ubus_context
*ctx
)
436 ret
= ubus_add_object(ctx
, &block_object
);
438 fprintf(stderr
, "Failed to add object: %s\n", ubus_strerror(ret
));
441 static int autofs_umount(void)
443 umount2(AUTOFS_MOUNT_PATH
, MNT_DETACH
);
447 static void autofs_read_handler(struct uloop_fd
*u
, unsigned int events
)
449 union autofs_v5_packet_union pktu
;
450 const struct autofs_v5_packet
*pkt
;
451 int cmd
= AUTOFS_IOC_READY
;
454 while (read(u
->fd
, &pktu
, sizeof(pktu
)) == -1) {
460 if (pktu
.hdr
.type
!= autofs_ptype_missing_indirect
) {
461 ULOG_ERR("unknown packet type %d\n", pktu
.hdr
.type
);
465 pkt
= &pktu
.missing_indirect
;
466 ULOG_ERR("kernel is requesting a mount -> %s\n", pkt
->name
);
467 if (lstat(pkt
->name
, &st
) == -1)
468 if (block("autofs", "add", (char *)pkt
->name
))
469 cmd
= AUTOFS_IOC_FAIL
;
471 if (ioctl(fd_autofs_write
, cmd
, pkt
->wait_queue_token
) < 0)
472 ULOG_ERR("failed to report back to kernel\n");
475 static void autofs_expire(struct uloop_timeout
*t
)
477 struct autofs_packet_expire pkt
;
479 while (ioctl(fd_autofs_write
, AUTOFS_IOC_EXPIRE
, &pkt
) == 0)
480 block("autofs", "remove", pkt
.name
);
482 uloop_timeout_set(t
, AUTOFS_EXPIRE_TIMER
);
485 struct uloop_timeout autofs_expire_timer
= {
489 static int autofs_mount(void)
491 int autofs_timeout
= AUTOFS_TIMEOUT
;
497 if (pipe(pipefd
) < 0) {
498 ULOG_ERR("failed to get kernel pipe\n");
502 snprintf(source
, sizeof(source
), "mountd(pid%u)", getpid());
503 snprintf(opts
, sizeof(opts
), "fd=%d,pgrp=%u,minproto=5,maxproto=5", pipefd
[1], (unsigned) getpgrp());
504 mkdir(AUTOFS_MOUNT_PATH
, 0555);
505 if (mount(source
, AUTOFS_MOUNT_PATH
, "autofs", 0, opts
)) {
506 ULOG_ERR("unable to mount autofs on %s\n", AUTOFS_MOUNT_PATH
);
512 fd_autofs_read
.fd
= pipefd
[0];
513 fd_autofs_read
.cb
= autofs_read_handler
;
514 uloop_fd_add(&fd_autofs_read
, ULOOP_READ
);
516 fd_autofs_write
= open(AUTOFS_MOUNT_PATH
, O_RDONLY
);
517 if(fd_autofs_write
< 0) {
519 ULOG_ERR("failed to open direcory\n");
523 ioctl(fd_autofs_write
, AUTOFS_IOC_PROTOVER
, &kproto_version
);
524 if (kproto_version
!= 5) {
525 ULOG_ERR("only kernel protocol version 5 is tested. You have %d.\n",
529 if (ioctl(fd_autofs_write
, AUTOFS_IOC_SETTIMEOUT
, &autofs_timeout
))
530 ULOG_ERR("failed to set autofs timeout\n");
532 uloop_timeout_set(&autofs_expire_timer
, AUTOFS_EXPIRE_TIMER
);
534 fcntl(fd_autofs_write
, F_SETFD
, fcntl(fd_autofs_write
, F_GETFD
) | FD_CLOEXEC
);
535 fcntl(fd_autofs_read
.fd
, F_SETFD
, fcntl(fd_autofs_read
.fd
, F_GETFD
) | FD_CLOEXEC
);
540 static void blockd_startup(struct uloop_timeout
*t
)
542 block("autofs", "start", NULL
);
545 struct uloop_timeout startup
= {
546 .cb
= blockd_startup
,
549 int main(int argc
, char **argv
)
551 ulog_open(ULOG_SYSLOG
| ULOG_STDIO
, LOG_DAEMON
, "blockd");
556 conn
.cb
= ubus_connect_handler
;
557 ubus_auto_connect(&conn
);
559 uloop_timeout_set(&startup
, 1000);
566 vlist_flush_all(&devices
);