d283f24c0af9cdc5a3522d7ca1196abfbe4fb8c2
[project/fstools.git] / blockd.c
1 #include <sys/types.h>
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 static char*
73 _find_mount_point(char *device)
74 {
75 char dev[32] = { 0 };
76
77 snprintf(dev, sizeof(dev), "/dev/%s", device);
78
79 return find_mount_point(dev, 0);
80 }
81
82 static int
83 block(char *cmd, char *action, char *device)
84 {
85 pid_t pid = fork();
86 int ret = -1;
87 int status;
88 char *argv[5] = { 0 };
89 int a = 0;
90
91 switch (pid) {
92 case -1:
93 ULOG_ERR("failed to fork block process\n");
94 break;
95
96 case 0:
97 argv[a++] = "/sbin/block";
98 argv[a++] = cmd;
99 argv[a++] = action;
100 argv[a++] = device;
101 execvp(argv[0], argv);
102 ULOG_ERR("failed to spawn %s %s %s\n", *argv, action, device);
103 exit(EXIT_FAILURE);
104
105 default:
106 waitpid(pid, &status, 0);
107 ret = WEXITSTATUS(status);
108 if (ret)
109 ULOG_ERR("failed to run block. %s/%s\n", action, device);
110 break;
111 }
112
113 return ret;
114 }
115
116 static int hotplug_call_mount(const char *action, const char *devname,
117 uloop_process_handler cb, void *priv)
118 {
119 char * const argv[] = { "hotplug-call", "mount", NULL };
120 struct hotplug_context *c = NULL;
121 pid_t pid;
122 int err;
123
124 if (cb) {
125 c = calloc(1, sizeof(*c));
126 if (!c)
127 return -ENOMEM;
128 }
129
130 pid = fork();
131 switch (pid) {
132 case -1:
133 err = -errno;
134 ULOG_ERR("fork() failed\n");
135 return err;
136 case 0:
137 uloop_end();
138
139 setenv("ACTION", action, 1);
140 setenv("DEVICE", devname, 1);
141
142 execv("/sbin/hotplug-call", argv);
143 exit(-1);
144 break;
145 default:
146 if (c) {
147 c->process.pid = pid;
148 c->process.cb = cb;
149 c->priv = priv;
150 uloop_process_add(&c->process);
151 }
152 break;
153 }
154
155 return 0;
156 }
157
158 static void device_mount_remove_hotplug_cb(struct uloop_process *p, int stat)
159 {
160 struct hotplug_context *hctx = container_of(p, struct hotplug_context, process);
161 struct device *device = hctx->priv;
162 char *mp;
163
164 if (device->target)
165 unlink(device->target);
166
167 mp = _find_mount_point(device->name);
168 if (mp) {
169 block("autofs", "remove", device->name);
170 free(mp);
171 }
172
173 free(device);
174 free(hctx);
175 }
176
177 static void device_mount_remove(struct device *device)
178 {
179 hotplug_call_mount("remove", device->name,
180 device_mount_remove_hotplug_cb, device);
181 }
182
183 static void device_mount_add(struct device *device)
184 {
185 struct stat st;
186 char path[64];
187
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);
194 }
195 if (symlink(path, device->target))
196 ULOG_ERR("failed to symlink %s->%s (%d) - %m\n", device->target, path, errno);
197 else
198 hotplug_call_mount("add", device->name, NULL, NULL);
199 }
200
201 static int
202 device_move(struct device *device_o, struct device *device_n)
203 {
204 char path[64];
205
206 if (device_o->autofs != device_n->autofs)
207 return -1;
208
209 if (device_o->anon || device_n->anon)
210 return -1;
211
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);
217 } else {
218 mkdir(device_n->target, 0755);
219 if (mount(device_o->target, device_n->target, NULL, MS_MOVE, NULL))
220 rmdir(device_n->target);
221 else
222 rmdir(device_o->target);
223 }
224
225 return 0;
226 }
227
228 static void vlist_nop_update(struct vlist_tree *tree,
229 struct vlist_node *node_new,
230 struct vlist_node *node_old)
231 {
232 }
233
234 VLIST_TREE(devices, avl_strcmp, vlist_nop_update, false, false);
235
236 static int
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)
240 {
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;
246 char _target[32];
247
248 blobmsg_parse(mount_policy, __MOUNT_MAX, data, blob_data(msg), blob_len(msg));
249
250 if (!data[MOUNT_DEVICE])
251 return UBUS_STATUS_INVALID_ARGUMENT;
252
253 devname = blobmsg_get_string(data[MOUNT_DEVICE]);
254
255 if (data[MOUNT_TARGET]) {
256 target = blobmsg_get_string(data[MOUNT_TARGET]);
257 } else {
258 snprintf(_target, sizeof(_target), "/mnt/%s",
259 blobmsg_get_string(data[MOUNT_DEVICE]));
260 target = _target;
261 }
262
263 if (data[MOUNT_REMOVE])
264 device = vlist_find(&devices, devname, device, node);
265 else
266 device = calloc_a(sizeof(*device), &_msg, blob_raw_len(msg),
267 &_name, strlen(devname) + 1, &__target, strlen(target) + 1);
268
269 if (!device)
270 return UBUS_STATUS_UNKNOWN_ERROR;
271
272 if (data[MOUNT_REMOVE]) {
273 vlist_delete(&devices, &device->node);
274
275 if (device->autofs)
276 device_mount_remove(device);
277 else
278 free(device);
279 } else {
280 struct device *old = vlist_find(&devices, devname, device, node);
281
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;
284 device->msg = _msg;
285 memcpy(_msg, msg, blob_raw_len(msg));
286 device->name = _name;
287 strcpy(_name, devname);
288 device->target = __target;
289 strcpy(__target, target);
290
291 vlist_add(&devices, &device->node, device->name);
292
293 if (old && !device_move(old, device)) {
294 if (device->autofs) {
295 device_mount_remove(old);
296 device_mount_add(device);
297 } else {
298 block("mount", NULL, NULL);
299 }
300 } else if (device->autofs) {
301 device_mount_add(device);
302 }
303 }
304
305 return 0;
306 }
307
308 static int
309 block_info(struct ubus_context *ctx, struct ubus_object *obj,
310 struct ubus_request_data *req, const char *method,
311 struct blob_attr *msg)
312 {
313 struct device *device;
314 void *a;
315
316 blob_buf_init(&bb, 0);
317 a = blobmsg_open_array(&bb, "devices");
318 vlist_for_each_element(&devices, device, node) {
319 void *t = blobmsg_open_table(&bb, "");
320 struct blob_attr *v;
321 char *mp;
322 int rem;
323
324 blob_for_each_attr(v, device->msg, rem)
325 blobmsg_add_blob(&bb, v);
326
327 mp = _find_mount_point(device->name);
328 if (mp) {
329 blobmsg_add_string(&bb, "mount", mp);
330 free(mp);
331 } else if (device->autofs && device->target) {
332 blobmsg_add_string(&bb, "mount", device->target);
333 }
334 blobmsg_close_table(&bb, t);
335 }
336 blobmsg_close_array(&bb, a);
337 ubus_send_reply(ctx, req, bb.head);
338
339 return 0;
340 }
341
342 static const struct ubus_method block_methods[] = {
343 UBUS_METHOD("hotplug", block_hotplug, mount_policy),
344 UBUS_METHOD_NOARG("info", block_info),
345 };
346
347 static struct ubus_object_type block_object_type =
348 UBUS_OBJECT_TYPE("block", block_methods);
349
350 static struct ubus_object block_object = {
351 .name = "block",
352 .type = &block_object_type,
353 .methods = block_methods,
354 .n_methods = ARRAY_SIZE(block_methods),
355 };
356
357 static void
358 ubus_connect_handler(struct ubus_context *ctx)
359 {
360 int ret;
361
362 ret = ubus_add_object(ctx, &block_object);
363 if (ret)
364 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
365 }
366
367 static int autofs_umount(void)
368 {
369 umount2(AUTOFS_MOUNT_PATH, MNT_DETACH);
370 return 0;
371 }
372
373 static void autofs_read_handler(struct uloop_fd *u, unsigned int events)
374 {
375 union autofs_v5_packet_union pktu;
376 const struct autofs_v5_packet *pkt;
377 int cmd = AUTOFS_IOC_READY;
378 struct stat st;
379
380 while (read(u->fd, &pktu, sizeof(pktu)) == -1) {
381 if (errno != EINTR)
382 return;
383 continue;
384 }
385
386 if (pktu.hdr.type != autofs_ptype_missing_indirect) {
387 ULOG_ERR("unknown packet type %d\n", pktu.hdr.type);
388 return;
389 }
390
391 pkt = &pktu.missing_indirect;
392 ULOG_ERR("kernel is requesting a mount -> %s\n", pkt->name);
393 if (lstat(pkt->name, &st) == -1)
394 if (block("autofs", "add", (char *)pkt->name))
395 cmd = AUTOFS_IOC_FAIL;
396
397 if (ioctl(fd_autofs_write, cmd, pkt->wait_queue_token) < 0)
398 ULOG_ERR("failed to report back to kernel\n");
399 }
400
401 static void autofs_expire(struct uloop_timeout *t)
402 {
403 struct autofs_packet_expire pkt;
404
405 while (ioctl(fd_autofs_write, AUTOFS_IOC_EXPIRE, &pkt) == 0)
406 block("autofs", "remove", pkt.name);
407
408 uloop_timeout_set(t, AUTOFS_EXPIRE_TIMER);
409 }
410
411 struct uloop_timeout autofs_expire_timer = {
412 .cb = autofs_expire,
413 };
414
415 static int autofs_mount(void)
416 {
417 int autofs_timeout = AUTOFS_TIMEOUT;
418 int kproto_version;
419 int pipefd[2];
420 char source[64];
421 char opts[64];
422
423 if (pipe(pipefd) < 0) {
424 ULOG_ERR("failed to get kernel pipe\n");
425 return -1;
426 }
427
428 snprintf(source, sizeof(source), "mountd(pid%u)", getpid());
429 snprintf(opts, sizeof(opts), "fd=%d,pgrp=%u,minproto=5,maxproto=5", pipefd[1], (unsigned) getpgrp());
430 mkdir(AUTOFS_MOUNT_PATH, 0555);
431 if (mount(source, AUTOFS_MOUNT_PATH, "autofs", 0, opts)) {
432 ULOG_ERR("unable to mount autofs on %s\n", AUTOFS_MOUNT_PATH);
433 close(pipefd[0]);
434 close(pipefd[1]);
435 return -1;
436 }
437 close(pipefd[1]);
438 fd_autofs_read.fd = pipefd[0];
439 fd_autofs_read.cb = autofs_read_handler;
440 uloop_fd_add(&fd_autofs_read, ULOOP_READ);
441
442 fd_autofs_write = open(AUTOFS_MOUNT_PATH, O_RDONLY);
443 if(fd_autofs_write < 0) {
444 autofs_umount();
445 ULOG_ERR("failed to open direcory\n");
446 return -1;
447 }
448
449 ioctl(fd_autofs_write, AUTOFS_IOC_PROTOVER, &kproto_version);
450 if (kproto_version != 5) {
451 ULOG_ERR("only kernel protocol version 5 is tested. You have %d.\n",
452 kproto_version);
453 exit(EXIT_FAILURE);
454 }
455 if (ioctl(fd_autofs_write, AUTOFS_IOC_SETTIMEOUT, &autofs_timeout))
456 ULOG_ERR("failed to set autofs timeout\n");
457
458 uloop_timeout_set(&autofs_expire_timer, AUTOFS_EXPIRE_TIMER);
459
460 fcntl(fd_autofs_write, F_SETFD, fcntl(fd_autofs_write, F_GETFD) | FD_CLOEXEC);
461 fcntl(fd_autofs_read.fd, F_SETFD, fcntl(fd_autofs_read.fd, F_GETFD) | FD_CLOEXEC);
462
463 return 0;
464 }
465
466 static void blockd_startup(struct uloop_timeout *t)
467 {
468 block("autofs", "start", NULL);
469 }
470
471 struct uloop_timeout startup = {
472 .cb = blockd_startup,
473 };
474
475 int main(int argc, char **argv)
476 {
477 ulog_open(ULOG_SYSLOG | ULOG_STDIO, LOG_DAEMON, "blockd");
478 uloop_init();
479
480 autofs_mount();
481
482 conn.cb = ubus_connect_handler;
483 ubus_auto_connect(&conn);
484
485 uloop_timeout_set(&startup, 1000);
486
487 uloop_run();
488 uloop_done();
489
490 autofs_umount();
491
492 vlist_flush_all(&devices);
493
494 return 0;
495 }