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)
29 struct vlist_node node
;
30 struct blob_attr
*msg
;
37 static struct uloop_fd fd_autofs_read
;
38 static int fd_autofs_write
= 0;
39 static struct ubus_auto_conn conn
;
40 struct blob_buf bb
= { 0 };
55 static const struct blobmsg_policy mount_policy
[__MOUNT_MAX
] = {
56 [MOUNT_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
57 [MOUNT_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
58 [MOUNT_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
59 [MOUNT_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
60 [MOUNT_OPTIONS
] = { .name
= "options", .type
= BLOBMSG_TYPE_STRING
},
61 [MOUNT_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
62 [MOUNT_AUTOFS
] = { .name
= "autofs", .type
= BLOBMSG_TYPE_INT32
},
63 [MOUNT_ANON
] = { .name
= "anon", .type
= BLOBMSG_TYPE_INT32
},
64 [MOUNT_REMOVE
] = { .name
= "remove", .type
= BLOBMSG_TYPE_INT32
},
68 _find_mount_point(char *device
)
72 snprintf(dev
, sizeof(dev
), "/dev/%s", device
);
74 return find_mount_point(dev
, 0);
78 block(char *cmd
, char *action
, char *device
)
83 char *argv
[5] = { 0 };
88 ULOG_ERR("failed to fork block process\n");
92 argv
[a
++] = "/sbin/block";
96 execvp(argv
[0], argv
);
97 ULOG_ERR("failed to spawn %s %s %s\n", *argv
, action
, device
);
101 waitpid(pid
, &status
, 0);
102 ret
= WEXITSTATUS(status
);
104 ULOG_ERR("failed to run block. %s/%s\n", action
, device
);
112 device_free(struct device
*device
)
119 block("autofs", "unavailable", device
->name
);
122 unlink(device
->target
);
124 mp
= _find_mount_point(device
->name
);
126 block("autofs", "remove", device
->name
);
132 device_add(struct device
*device
)
140 snprintf(path
, sizeof(path
), "/tmp/run/blockd/%s", device
->name
);
141 if (!lstat(device
->target
, &st
)) {
142 if (S_ISLNK(st
.st_mode
))
143 unlink(device
->target
);
144 else if (S_ISDIR(st
.st_mode
))
145 rmdir(device
->target
);
147 if (symlink(path
, device
->target
))
148 ULOG_ERR("failed to symlink %s->%s (%d) - %m\n", device
->target
, path
, errno
);
150 block("autofs", "available", device
->name
);
154 device_move(struct device
*device_o
, struct device
*device_n
)
158 if (device_o
->autofs
!= device_n
->autofs
)
161 if (device_o
->anon
|| device_n
->anon
)
164 if (device_o
->autofs
) {
165 unlink(device_o
->target
);
166 snprintf(path
, sizeof(path
), "/tmp/run/blockd/%s", device_n
->name
);
167 if (symlink(path
, device_n
->target
))
168 ULOG_ERR("failed to symlink %s->%s (%d) - %m\n", device_n
->target
, path
, errno
);
170 mkdir(device_n
->target
, 0755);
171 if (mount(device_o
->target
, device_n
->target
, NULL
, MS_MOVE
, NULL
))
172 rmdir(device_n
->target
);
174 rmdir(device_o
->target
);
181 devices_update_cb(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
182 struct vlist_node
*node_old
)
184 struct device
*device_o
= NULL
, *device_n
= NULL
;
187 device_o
= container_of(node_old
, struct device
, node
);
190 device_n
= container_of(node_new
, struct device
, node
);
192 if (device_o
&& device_n
) {
193 if (device_move(device_o
, device_n
)) {
194 device_free(device_o
);
195 device_add(device_n
);
196 if (!device_n
->autofs
)
197 block("mount", NULL
, NULL
);
199 } else if (device_n
) {
200 device_add(device_n
);
202 device_free(device_o
);
209 VLIST_TREE(devices
, avl_strcmp
, devices_update_cb
, false, false);
212 block_hotplug(struct ubus_context
*ctx
, struct ubus_object
*obj
,
213 struct ubus_request_data
*req
, const char *method
,
214 struct blob_attr
*msg
)
216 struct blob_attr
*data
[__MOUNT_MAX
];
217 struct device
*device
;
218 struct blob_attr
*_msg
;
219 char *devname
, *_name
;
220 char *target
= NULL
, *__target
;
223 blobmsg_parse(mount_policy
, __MOUNT_MAX
, data
, blob_data(msg
), blob_len(msg
));
225 if (!data
[MOUNT_DEVICE
])
226 return UBUS_STATUS_INVALID_ARGUMENT
;
228 devname
= blobmsg_get_string(data
[MOUNT_DEVICE
]);
230 if (data
[MOUNT_TARGET
]) {
231 target
= blobmsg_get_string(data
[MOUNT_TARGET
]);
233 snprintf(_target
, sizeof(_target
), "/mnt/%s",
234 blobmsg_get_string(data
[MOUNT_DEVICE
]));
238 if (data
[MOUNT_REMOVE
])
239 device
= vlist_find(&devices
, devname
, device
, node
);
241 device
= calloc_a(sizeof(*device
), &_msg
, blob_raw_len(msg
),
242 &_name
, strlen(devname
) + 1, &__target
, strlen(target
) + 1);
245 return UBUS_STATUS_UNKNOWN_ERROR
;
247 if (data
[MOUNT_REMOVE
]) {
248 vlist_delete(&devices
, &device
->node
);
250 if (data
[MOUNT_AUTOFS
])
251 device
->autofs
= blobmsg_get_u32(data
[MOUNT_AUTOFS
]);
254 if (data
[MOUNT_ANON
])
255 device
->anon
= blobmsg_get_u32(data
[MOUNT_ANON
]);
259 memcpy(_msg
, msg
, blob_raw_len(msg
));
260 device
->name
= _name
;
261 strcpy(_name
, devname
);
262 device
->target
= __target
;
263 strcpy(__target
, target
);
264 vlist_add(&devices
, &device
->node
, device
->name
);
271 block_info(struct ubus_context
*ctx
, struct ubus_object
*obj
,
272 struct ubus_request_data
*req
, const char *method
,
273 struct blob_attr
*msg
)
275 struct device
*device
;
278 blob_buf_init(&bb
, 0);
279 a
= blobmsg_open_array(&bb
, "devices");
280 vlist_for_each_element(&devices
, device
, node
) {
281 void *t
= blobmsg_open_table(&bb
, "");
286 blob_for_each_attr(v
, device
->msg
, rem
)
287 blobmsg_add_blob(&bb
, v
);
289 mp
= _find_mount_point(device
->name
);
291 blobmsg_add_string(&bb
, "mount", mp
);
293 } else if (device
->autofs
&& device
->target
) {
294 blobmsg_add_string(&bb
, "mount", device
->target
);
296 blobmsg_close_table(&bb
, t
);
298 blobmsg_close_array(&bb
, a
);
299 ubus_send_reply(ctx
, req
, bb
.head
);
304 static const struct ubus_method block_methods
[] = {
305 UBUS_METHOD("hotplug", block_hotplug
, mount_policy
),
306 UBUS_METHOD_NOARG("info", block_info
),
309 static struct ubus_object_type block_object_type
=
310 UBUS_OBJECT_TYPE("block", block_methods
);
312 static struct ubus_object block_object
= {
314 .type
= &block_object_type
,
315 .methods
= block_methods
,
316 .n_methods
= ARRAY_SIZE(block_methods
),
320 ubus_connect_handler(struct ubus_context
*ctx
)
324 ret
= ubus_add_object(ctx
, &block_object
);
326 fprintf(stderr
, "Failed to add object: %s\n", ubus_strerror(ret
));
329 static int autofs_umount(void)
331 umount2(AUTOFS_MOUNT_PATH
, MNT_DETACH
);
335 static void autofs_read_handler(struct uloop_fd
*u
, unsigned int events
)
337 union autofs_v5_packet_union pktu
;
338 const struct autofs_v5_packet
*pkt
;
339 int cmd
= AUTOFS_IOC_READY
;
342 while (read(u
->fd
, &pktu
, sizeof(pktu
)) == -1) {
348 if (pktu
.hdr
.type
!= autofs_ptype_missing_indirect
) {
349 ULOG_ERR("unknown packet type %d\n", pktu
.hdr
.type
);
353 pkt
= &pktu
.missing_indirect
;
354 ULOG_ERR("kernel is requesting a mount -> %s\n", pkt
->name
);
355 if (lstat(pkt
->name
, &st
) == -1)
356 if (block("autofs", "add", (char *)pkt
->name
))
357 cmd
= AUTOFS_IOC_FAIL
;
359 if (ioctl(fd_autofs_write
, cmd
, pkt
->wait_queue_token
) < 0)
360 ULOG_ERR("failed to report back to kernel\n");
363 static void autofs_expire(struct uloop_timeout
*t
)
365 struct autofs_packet_expire pkt
;
367 while (ioctl(fd_autofs_write
, AUTOFS_IOC_EXPIRE
, &pkt
) == 0)
368 block("autofs", "remove", pkt
.name
);
370 uloop_timeout_set(t
, AUTOFS_EXPIRE_TIMER
);
373 struct uloop_timeout autofs_expire_timer
= {
377 static int autofs_mount(void)
379 int autofs_timeout
= AUTOFS_TIMEOUT
;
385 if (pipe(pipefd
) < 0) {
386 ULOG_ERR("failed to get kernel pipe\n");
390 snprintf(source
, sizeof(source
), "mountd(pid%u)", getpid());
391 snprintf(opts
, sizeof(opts
), "fd=%d,pgrp=%u,minproto=5,maxproto=5", pipefd
[1], (unsigned) getpgrp());
392 mkdir(AUTOFS_MOUNT_PATH
, 0555);
393 if (mount(source
, AUTOFS_MOUNT_PATH
, "autofs", 0, opts
)) {
394 ULOG_ERR("unable to mount autofs on %s\n", AUTOFS_MOUNT_PATH
);
400 fd_autofs_read
.fd
= pipefd
[0];
401 fd_autofs_read
.cb
= autofs_read_handler
;
402 uloop_fd_add(&fd_autofs_read
, ULOOP_READ
);
404 fd_autofs_write
= open(AUTOFS_MOUNT_PATH
, O_RDONLY
);
405 if(fd_autofs_write
< 0) {
407 ULOG_ERR("failed to open direcory\n");
411 ioctl(fd_autofs_write
, AUTOFS_IOC_PROTOVER
, &kproto_version
);
412 if (kproto_version
!= 5) {
413 ULOG_ERR("only kernel protocol version 5 is tested. You have %d.\n",
417 if (ioctl(fd_autofs_write
, AUTOFS_IOC_SETTIMEOUT
, &autofs_timeout
))
418 ULOG_ERR("failed to set autofs timeout\n");
420 uloop_timeout_set(&autofs_expire_timer
, AUTOFS_EXPIRE_TIMER
);
422 fcntl(fd_autofs_write
, F_SETFD
, fcntl(fd_autofs_write
, F_GETFD
) | FD_CLOEXEC
);
423 fcntl(fd_autofs_read
.fd
, F_SETFD
, fcntl(fd_autofs_read
.fd
, F_GETFD
) | FD_CLOEXEC
);
428 static void blockd_startup(struct uloop_timeout
*t
)
430 block("autofs", "start", NULL
);
433 struct uloop_timeout startup
= {
434 .cb
= blockd_startup
,
437 int main(int argc
, char **argv
)
439 ulog_open(ULOG_SYSLOG
| ULOG_STDIO
, LOG_DAEMON
, "blockd");
444 conn
.cb
= ubus_connect_handler
;
445 ubus_auto_connect(&conn
);
447 uloop_timeout_set(&startup
, 1000);
454 vlist_flush_all(&devices
);