]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/automount.c
update fixme
[thirdparty/systemd.git] / src / automount.c
CommitLineData
5cb5a6ff
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
2
a7334b09
LP
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
5cb5a6ff 22#include <errno.h>
8d567588
LP
23#include <limits.h>
24#include <sys/mount.h>
25#include <unistd.h>
26#include <fcntl.h>
27#include <sys/epoll.h>
28#include <sys/stat.h>
29#include <linux/auto_fs4.h>
30#include <linux/auto_dev-ioctl.h>
5cb5a6ff 31
87f0e418 32#include "unit.h"
5cb5a6ff
LP
33#include "automount.h"
34#include "load-fragment.h"
5cb5a6ff 35#include "load-dropin.h"
8d567588 36#include "unit-name.h"
4139c1b2 37#include "dbus-automount.h"
398ef8ba 38#include "bus-errors.h"
4e67ddd6 39#include "special.h"
5cb5a6ff 40
e537352b
LP
41static const UnitActiveState state_translation_table[_AUTOMOUNT_STATE_MAX] = {
42 [AUTOMOUNT_DEAD] = UNIT_INACTIVE,
43 [AUTOMOUNT_WAITING] = UNIT_ACTIVE,
44 [AUTOMOUNT_RUNNING] = UNIT_ACTIVE,
032ff4af 45 [AUTOMOUNT_MAINTENANCE] = UNIT_MAINTENANCE
e537352b 46};
5cb5a6ff 47
a16e1123 48static int open_dev_autofs(Manager *m);
8d567588 49
e537352b
LP
50static void automount_init(Unit *u) {
51 Automount *a = AUTOMOUNT(u);
5cb5a6ff 52
8d567588
LP
53 assert(u);
54 assert(u->meta.load_state == UNIT_STUB);
55
56 a->pipe_watch.fd = a->pipe_fd = -1;
a16e1123 57 a->pipe_watch.type = WATCH_INVALID;
1cf18f27
LP
58
59 a->directory_mode = 0755;
8d567588
LP
60}
61
62static void repeat_unmout(const char *path) {
63 assert(path);
64
65 for (;;) {
1b560190
LP
66 /* If there are multiple mounts on a mount point, this
67 * removes them all */
8d567588
LP
68
69 if (umount2(path, MNT_DETACH) >= 0)
70 continue;
71
72 if (errno != EINVAL)
73 log_error("Failed to unmount: %m");
74
75 break;
76 }
77}
78
79static void unmount_autofs(Automount *a) {
80 assert(a);
81
82 if (a->pipe_fd < 0)
83 return;
84
85 automount_send_ready(a, -EHOSTDOWN);
86
87 unit_unwatch_fd(UNIT(a), &a->pipe_watch);
88 close_nointr_nofail(a->pipe_fd);
89 a->pipe_fd = -1;
90
a16e1123
LP
91 /* If we reload/reexecute things we keep the mount point
92 * around */
93 if (a->where &&
4cd1fbcc
LP
94 (a->meta.manager->exit_code != MANAGER_RELOAD &&
95 a->meta.manager->exit_code != MANAGER_REEXECUTE))
a16e1123 96 repeat_unmout(a->where);
8d567588
LP
97}
98
99static void automount_done(Unit *u) {
100 Automount *a = AUTOMOUNT(u);
101
102 assert(a);
103
104 unmount_autofs(a);
e537352b 105 a->mount = NULL;
8d567588 106
a16e1123
LP
107 free(a->where);
108 a->where = NULL;
109
110 set_free(a->tokens);
111 a->tokens = NULL;
8d567588
LP
112}
113
6e2ef85b
LP
114int automount_add_one_mount_link(Automount *a, Mount *m) {
115 int r;
116
117 assert(a);
118 assert(m);
119
120 if (a->meta.load_state != UNIT_LOADED ||
121 m->meta.load_state != UNIT_LOADED)
122 return 0;
123
124 if (!path_startswith(a->where, m->where))
125 return 0;
126
1b560190
LP
127 if (path_equal(a->where, m->where))
128 return 0;
129
2c966c03 130 if ((r = unit_add_two_dependencies(UNIT(a), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0)
6e2ef85b
LP
131 return r;
132
133 return 0;
134}
135
136static int automount_add_mount_links(Automount *a) {
137 Meta *other;
138 int r;
139
140 assert(a);
141
142 LIST_FOREACH(units_per_type, other, a->meta.manager->units_per_type[UNIT_MOUNT])
143 if ((r = automount_add_one_mount_link(a, (Mount*) other)) < 0)
144 return r;
145
146 return 0;
147}
148
2edd4434
LP
149static int automount_add_default_dependencies(Automount *a) {
150 int r;
151
152 assert(a);
153
154 if (a->meta.manager->running_as == MANAGER_SYSTEM) {
155
156 if ((r = unit_add_dependency_by_name(UNIT(a), UNIT_AFTER, SPECIAL_SYSINIT_TARGET, NULL, true)) < 0)
157 return r;
158
159 if ((r = unit_add_two_dependencies_by_name(UNIT(a), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true)) < 0)
160 return r;
161 }
162
163 return 0;
164}
165
8d567588
LP
166static int automount_verify(Automount *a) {
167 bool b;
168 char *e;
169 assert(a);
170
4cd1fbcc 171 if (a->meta.load_state != UNIT_LOADED)
8d567588
LP
172 return 0;
173
41e45059
LP
174 if (path_equal(a->where, "/")) {
175 log_error("Cannot have an automount unit for the root directory. Refusing.");
176 return -EINVAL;
177 }
178
a16e1123 179 if (!(e = unit_name_from_path(a->where, ".automount")))
8d567588
LP
180 return -ENOMEM;
181
182 b = unit_has_name(UNIT(a), e);
183 free(e);
184
185 if (!b) {
4cd1fbcc 186 log_error("%s's Where setting doesn't match unit name. Refusing.", a->meta.id);
8d567588
LP
187 return -EINVAL;
188 }
189
190 return 0;
e537352b 191}
5cb5a6ff 192
e537352b
LP
193static int automount_load(Unit *u) {
194 int r;
195 Automount *a = AUTOMOUNT(u);
23a177ef 196
e537352b
LP
197 assert(u);
198 assert(u->meta.load_state == UNIT_STUB);
5cb5a6ff 199
e537352b
LP
200 /* Load a .automount file */
201 if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
202 return r;
23a177ef 203
e537352b 204 if (u->meta.load_state == UNIT_LOADED) {
23a177ef 205
a16e1123
LP
206 if (!a->where)
207 if (!(a->where = unit_name_to_path(u->meta.id)))
208 return -ENOMEM;
209
210 path_kill_slashes(a->where);
211
6e2ef85b
LP
212 if ((r = automount_add_mount_links(a)) < 0)
213 return r;
214
e537352b 215 if ((r = unit_load_related_unit(u, ".mount", (Unit**) &a->mount)) < 0)
23a177ef
LP
216 return r;
217
701cc384 218 if ((r = unit_add_dependency(u, UNIT_BEFORE, UNIT(a->mount), true)) < 0)
23a177ef 219 return r;
4e67ddd6 220
2edd4434
LP
221 if (a->meta.default_dependencies)
222 if ((r = automount_add_default_dependencies(a)) < 0)
4e67ddd6 223 return r;
23a177ef
LP
224 }
225
8d567588 226 return automount_verify(a);
5cb5a6ff
LP
227}
228
8d567588
LP
229static void automount_set_state(Automount *a, AutomountState state) {
230 AutomountState old_state;
e537352b 231 assert(a);
034c6ed7 232
8d567588
LP
233 old_state = a->state;
234 a->state = state;
235
236 if (state != AUTOMOUNT_WAITING &&
237 state != AUTOMOUNT_RUNNING)
238 unmount_autofs(a);
239
240 if (state != old_state)
40d50879 241 log_debug("%s changed %s -> %s",
4cd1fbcc 242 a->meta.id,
a16e1123
LP
243 automount_state_to_string(old_state),
244 automount_state_to_string(state));
8d567588
LP
245
246 unit_notify(UNIT(a), state_translation_table[old_state], state_translation_table[state]);
034c6ed7
LP
247}
248
a16e1123
LP
249static int automount_coldplug(Unit *u) {
250 Automount *a = AUTOMOUNT(u);
251 int r;
252
253 assert(a);
254 assert(a->state == AUTOMOUNT_DEAD);
255
256 if (a->deserialized_state != a->state) {
257
258 if ((r = open_dev_autofs(u->meta.manager)) < 0)
259 return r;
260
261 if (a->deserialized_state == AUTOMOUNT_WAITING ||
262 a->deserialized_state == AUTOMOUNT_RUNNING) {
263
264 assert(a->pipe_fd >= 0);
265
266 if ((r = unit_watch_fd(UNIT(a), a->pipe_fd, EPOLLIN, &a->pipe_watch)) < 0)
267 return r;
268 }
269
270 automount_set_state(a, a->deserialized_state);
271 }
272
273 return 0;
274}
275
87f0e418 276static void automount_dump(Unit *u, FILE *f, const char *prefix) {
a16e1123 277 Automount *a = AUTOMOUNT(u);
5cb5a6ff 278
a16e1123 279 assert(a);
5cb5a6ff
LP
280
281 fprintf(f,
a16e1123 282 "%sAutomount State: %s\n"
1cf18f27
LP
283 "%sWhere: %s\n"
284 "%sDirectoryMode: %04o\n",
a16e1123 285 prefix, automount_state_to_string(a->state),
1cf18f27
LP
286 prefix, a->where,
287 prefix, a->directory_mode);
5cb5a6ff
LP
288}
289
8d567588
LP
290static void automount_enter_dead(Automount *a, bool success) {
291 assert(a);
292
293 if (!success)
294 a->failure = true;
295
18c78fb1 296 automount_set_state(a, a->failure ? AUTOMOUNT_MAINTENANCE : AUTOMOUNT_DEAD);
8d567588
LP
297}
298
299static int open_dev_autofs(Manager *m) {
300 struct autofs_dev_ioctl param;
301
302 assert(m);
303
304 if (m->dev_autofs_fd >= 0)
305 return m->dev_autofs_fd;
306
a16e1123 307 if ((m->dev_autofs_fd = open("/dev/autofs", O_CLOEXEC|O_RDONLY)) < 0) {
11c3a4ee
LP
308 log_error("Failed to open /dev/autofs: %s", strerror(errno));
309 return -errno;
8d567588
LP
310 }
311
312 init_autofs_dev_ioctl(&param);
313 if (ioctl(m->dev_autofs_fd, AUTOFS_DEV_IOCTL_VERSION, &param) < 0) {
314 close_nointr_nofail(m->dev_autofs_fd);
315 m->dev_autofs_fd = -1;
316 return -errno;
317 }
318
319 log_debug("Autofs kernel version %i.%i", param.ver_major, param.ver_minor);
320
321 return m->dev_autofs_fd;
322}
323
324static int open_ioctl_fd(int dev_autofs_fd, const char *where, dev_t devid) {
325 struct autofs_dev_ioctl *param;
326 size_t l;
327 int r;
328
329 assert(dev_autofs_fd >= 0);
330 assert(where);
331
332 l = sizeof(struct autofs_dev_ioctl) + strlen(where) + 1;
333
334 if (!(param = malloc(l)))
335 return -ENOMEM;
336
337 init_autofs_dev_ioctl(param);
338 param->size = l;
339 param->ioctlfd = -1;
340 param->openmount.devid = devid;
341 strcpy(param->path, where);
342
343 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_OPENMOUNT, param) < 0) {
344 r = -errno;
345 goto finish;
346 }
347
348 if (param->ioctlfd < 0) {
349 r = -EIO;
350 goto finish;
351 }
352
a16e1123 353 fd_cloexec(param->ioctlfd, true);
8d567588
LP
354 r = param->ioctlfd;
355
356finish:
357 free(param);
358 return r;
359}
360
361static int autofs_protocol(int dev_autofs_fd, int ioctl_fd) {
362 uint32_t major, minor;
363 struct autofs_dev_ioctl param;
364
365 assert(dev_autofs_fd >= 0);
366 assert(ioctl_fd >= 0);
367
368 init_autofs_dev_ioctl(&param);
369 param.ioctlfd = ioctl_fd;
370
371 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOVER, &param) < 0)
372 return -errno;
373
374 major = param.protover.version;
375
376 init_autofs_dev_ioctl(&param);
377 param.ioctlfd = ioctl_fd;
378
379 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOSUBVER, &param) < 0)
380 return -errno;
381
382 minor = param.protosubver.sub_version;
383
384 log_debug("Autofs protocol version %i.%i", major, minor);
385 return 0;
386}
387
388static int autofs_set_timeout(int dev_autofs_fd, int ioctl_fd, time_t sec) {
389 struct autofs_dev_ioctl param;
390
391 assert(dev_autofs_fd >= 0);
392 assert(ioctl_fd >= 0);
393
394 init_autofs_dev_ioctl(&param);
395 param.ioctlfd = ioctl_fd;
396 param.timeout.timeout = sec;
397
398 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_TIMEOUT, &param) < 0)
399 return -errno;
400
401 return 0;
402}
403
404static int autofs_send_ready(int dev_autofs_fd, int ioctl_fd, uint32_t token, int status) {
405 struct autofs_dev_ioctl param;
406
407 assert(dev_autofs_fd >= 0);
408 assert(ioctl_fd >= 0);
409
410 init_autofs_dev_ioctl(&param);
411 param.ioctlfd = ioctl_fd;
412
413 if (status) {
414 param.fail.token = token;
415 param.fail.status = status;
416 } else
417 param.ready.token = token;
418
419 if (ioctl(dev_autofs_fd, status ? AUTOFS_DEV_IOCTL_FAIL : AUTOFS_DEV_IOCTL_READY, &param) < 0)
420 return -errno;
421
422 return 0;
423}
424
425int automount_send_ready(Automount *a, int status) {
426 int ioctl_fd, r;
427 unsigned token;
428
429 assert(a);
430 assert(status <= 0);
431
432 if (set_isempty(a->tokens))
433 return 0;
434
4cd1fbcc 435 if ((ioctl_fd = open_ioctl_fd(a->meta.manager->dev_autofs_fd, a->where, a->dev_id)) < 0) {
8d567588
LP
436 r = ioctl_fd;
437 goto fail;
438 }
439
440 if (status)
441 log_debug("Sending failure: %s", strerror(-status));
442 else
443 log_debug("Sending success.");
444
445 /* Autofs thankfully does not hand out 0 as a token */
446 while ((token = PTR_TO_UINT(set_steal_first(a->tokens)))) {
447 int k;
448
449 /* Autofs fun fact II:
450 *
451 * if you pass a positive status code here, the kernel will
452 * freeze! Yay! */
453
4cd1fbcc 454 if ((k = autofs_send_ready(a->meta.manager->dev_autofs_fd,
8d567588
LP
455 ioctl_fd,
456 token,
457 status)) < 0)
458 r = k;
459 }
460
461 r = 0;
462
463fail:
464 if (ioctl_fd >= 0)
465 close_nointr_nofail(ioctl_fd);
466
467 return r;
468}
469
470static void automount_enter_waiting(Automount *a) {
471 int p[2] = { -1, -1 };
472 char name[32], options[128];
473 bool mounted = false;
474 int r, ioctl_fd = -1, dev_autofs_fd;
475 struct stat st;
476
477 assert(a);
478 assert(a->pipe_fd < 0);
479 assert(a->where);
480
481 if (a->tokens)
482 set_clear(a->tokens);
8d567588 483
4cd1fbcc 484 if ((dev_autofs_fd = open_dev_autofs(a->meta.manager)) < 0) {
8d567588
LP
485 r = dev_autofs_fd;
486 goto fail;
487 }
488
489 /* We knowingly ignore the results of this call */
490 mkdir_p(a->where, 0555);
491
a16e1123 492 if (pipe2(p, O_NONBLOCK|O_CLOEXEC) < 0) {
8d567588
LP
493 r = -errno;
494 goto fail;
495 }
496
497 snprintf(options, sizeof(options), "fd=%i,pgrp=%u,minproto=5,maxproto=5,direct", p[1], (unsigned) getpgrp());
498 char_array_0(options);
499
500 snprintf(name, sizeof(name), "systemd-%u", (unsigned) getpid());
501 char_array_0(name);
502
503 if (mount(name, a->where, "autofs", 0, options) < 0) {
504 r = -errno;
505 goto fail;
506 }
507
508 mounted = true;
509
510 close_nointr_nofail(p[1]);
511 p[1] = -1;
512
513 if (stat(a->where, &st) < 0) {
514 r = -errno;
515 goto fail;
516 }
517
518 if ((ioctl_fd = open_ioctl_fd(dev_autofs_fd, a->where, st.st_dev)) < 0) {
519 r = ioctl_fd;
520 goto fail;
521 }
522
523 if ((r = autofs_protocol(dev_autofs_fd, ioctl_fd)) < 0)
524 goto fail;
525
526 if ((r = autofs_set_timeout(dev_autofs_fd, ioctl_fd, 300)) < 0)
527 goto fail;
528
529 /* Autofs fun fact:
530 *
531 * Unless we close the ioctl fd here, for some weird reason
532 * the direct mount will not receive events from the
533 * kernel. */
534
535 close_nointr_nofail(ioctl_fd);
536 ioctl_fd = -1;
537
538 if ((r = unit_watch_fd(UNIT(a), p[0], EPOLLIN, &a->pipe_watch)) < 0)
539 goto fail;
540
541 a->pipe_fd = p[0];
542 a->dev_id = st.st_dev;
543
544 automount_set_state(a, AUTOMOUNT_WAITING);
545
546 return;
547
548fail:
549 assert_se(close_pipe(p) == 0);
550
551 if (ioctl_fd >= 0)
552 close_nointr_nofail(ioctl_fd);
553
554 if (mounted)
555 repeat_unmout(a->where);
556
557 log_error("Failed to initialize automounter: %s", strerror(-r));
558 automount_enter_dead(a, false);
559}
560
561static void automount_enter_runnning(Automount *a) {
562 int r;
563 struct stat st;
398ef8ba 564 DBusError error;
8d567588
LP
565
566 assert(a);
567 assert(a->mount);
568
398ef8ba
LP
569 dbus_error_init(&error);
570
ba3e67a7
LP
571 /* We don't take mount requests anymore if we are supposed to
572 * shut down anyway */
573 if (a->meta.job && a->meta.job->type == JOB_STOP) {
574 automount_send_ready(a, -EHOSTDOWN);
575 return;
576 }
577
1cf18f27 578 mkdir_p(a->where, a->directory_mode);
8d567588 579
1cf18f27
LP
580 /* Before we do anything, let's see if somebody is playing games with us? */
581 if (lstat(a->where, &st) < 0) {
8d567588
LP
582 log_warning("%s failed stat automount point: %m", a->meta.id);
583 goto fail;
584 }
585
586 if (!S_ISDIR(st.st_mode) || st.st_dev != a->dev_id)
587 log_info("%s's automount point already active?", a->meta.id);
398ef8ba
LP
588 else if ((r = manager_add_job(a->meta.manager, JOB_START, UNIT(a->mount), JOB_REPLACE, true, &error, NULL)) < 0) {
589 log_warning("%s failed to queue mount startup job: %s", a->meta.id, bus_error(&error, r));
8d567588
LP
590 goto fail;
591 }
592
593 automount_set_state(a, AUTOMOUNT_RUNNING);
594 return;
595
596fail:
597 automount_enter_dead(a, false);
398ef8ba 598 dbus_error_free(&error);
8d567588
LP
599}
600
601static int automount_start(Unit *u) {
602 Automount *a = AUTOMOUNT(u);
603
604 assert(a);
605
18c78fb1 606 assert(a->state == AUTOMOUNT_DEAD || a->state == AUTOMOUNT_MAINTENANCE);
01f78473 607
8d567588
LP
608 if (path_is_mount_point(a->where)) {
609 log_error("Path %s is already a mount point, refusing start for %s", a->where, u->meta.id);
610 return -EEXIST;
611 }
612
01f78473
LP
613 if (a->mount->meta.load_state != UNIT_LOADED)
614 return -ENOENT;
8d567588
LP
615
616 a->failure = false;
617 automount_enter_waiting(a);
618 return 0;
619}
620
621static int automount_stop(Unit *u) {
622 Automount *a = AUTOMOUNT(u);
623
624 assert(a);
625
626 assert(a->state == AUTOMOUNT_WAITING || a->state == AUTOMOUNT_RUNNING);
627
628 automount_enter_dead(a, true);
629 return 0;
630}
631
a16e1123
LP
632static int automount_serialize(Unit *u, FILE *f, FDSet *fds) {
633 Automount *a = AUTOMOUNT(u);
634 void *p;
635 Iterator i;
636
637 assert(a);
638 assert(f);
639 assert(fds);
640
641 unit_serialize_item(u, f, "state", automount_state_to_string(a->state));
642 unit_serialize_item(u, f, "failure", yes_no(a->failure));
643 unit_serialize_item_format(u, f, "dev-id", "%u", (unsigned) a->dev_id);
644
645 SET_FOREACH(p, a->tokens, i)
646 unit_serialize_item_format(u, f, "token", "%u", PTR_TO_UINT(p));
647
648 if (a->pipe_fd >= 0) {
649 int copy;
650
651 if ((copy = fdset_put_dup(fds, a->pipe_fd)) < 0)
652 return copy;
653
654 unit_serialize_item_format(u, f, "pipe-fd", "%i", copy);
655 }
656
657 return 0;
658}
659
660static int automount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
661 Automount *a = AUTOMOUNT(u);
662 int r;
663
664 assert(a);
665 assert(fds);
666
667 if (streq(key, "state")) {
668 AutomountState state;
669
670 if ((state = automount_state_from_string(value)) < 0)
671 log_debug("Failed to parse state value %s", value);
672 else
673 a->deserialized_state = state;
674 } else if (streq(key, "failure")) {
675 int b;
676
677 if ((b = parse_boolean(value)) < 0)
678 log_debug("Failed to parse failure value %s", value);
679 else
680 a->failure = b || a->failure;
681 } else if (streq(key, "dev-id")) {
682 unsigned d;
683
684 if (safe_atou(value, &d) < 0)
685 log_debug("Failed to parse dev-id value %s", value);
686 else
687 a->dev_id = (unsigned) d;
688 } else if (streq(key, "token")) {
689 unsigned token;
690
691 if (safe_atou(value, &token) < 0)
692 log_debug("Failed to parse token value %s", value);
693 else {
694 if (!a->tokens)
695 if (!(a->tokens = set_new(trivial_hash_func, trivial_compare_func)))
696 return -ENOMEM;
697
698 if ((r = set_put(a->tokens, UINT_TO_PTR(token))) < 0)
699 return r;
700 }
701 } else if (streq(key, "pipe-fd")) {
702 int fd;
703
704 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
705 log_debug("Failed to parse pipe-fd value %s", value);
706 else {
707 if (a->pipe_fd >= 0)
708 close_nointr_nofail(a->pipe_fd);
709
710 a->pipe_fd = fdset_remove(fds, fd);
711 }
712 } else
713 log_debug("Unknown serialization key '%s'", key);
714
715 return 0;
716}
717
87f0e418 718static UnitActiveState automount_active_state(Unit *u) {
a16e1123 719 assert(u);
87f0e418 720
e537352b 721 return state_translation_table[AUTOMOUNT(u)->state];
5cb5a6ff
LP
722}
723
10a94420
LP
724static const char *automount_sub_state_to_string(Unit *u) {
725 assert(u);
726
a16e1123 727 return automount_state_to_string(AUTOMOUNT(u)->state);
10a94420
LP
728}
729
701cc384
LP
730static bool automount_check_gc(Unit *u) {
731 Automount *a = AUTOMOUNT(u);
732
733 assert(a);
734
735 return UNIT_VTABLE(UNIT(a->mount))->check_gc(UNIT(a->mount));
736}
737
8d567588 738static void automount_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
701cc384 739 Automount *a = AUTOMOUNT(u);
8d567588
LP
740 union autofs_v5_packet_union packet;
741 ssize_t l;
742 int r;
743
8d567588
LP
744 assert(a);
745 assert(fd == a->pipe_fd);
746
747 if (events != EPOLLIN) {
748 log_error("Got invalid poll event on pipe.");
749 goto fail;
750 }
751
eb22ac37 752 if ((l = loop_read(a->pipe_fd, &packet, sizeof(packet), true)) != sizeof(packet)) {
8d567588
LP
753 log_error("Invalid read from pipe: %s", l < 0 ? strerror(-l) : "short read");
754 goto fail;
755 }
756
757 switch (packet.hdr.type) {
758
759 case autofs_ptype_missing_direct:
760 log_debug("Got direct mount request for %s", packet.v5_packet.name);
761
a16e1123
LP
762 if (!a->tokens)
763 if (!(a->tokens = set_new(trivial_hash_func, trivial_compare_func))) {
764 log_error("Failed to allocate token set.");
765 goto fail;
766 }
767
8d567588
LP
768 if ((r = set_put(a->tokens, UINT_TO_PTR(packet.v5_packet.wait_queue_token))) < 0) {
769 log_error("Failed to remember token: %s", strerror(-r));
770 goto fail;
771 }
772
773 automount_enter_runnning(a);
774 break;
775
776 default:
777 log_error("Received unknown automount request %i", packet.hdr.type);
778 break;
779 }
780
781 return;
782
783fail:
784 automount_enter_dead(a, false);
785}
786
787static void automount_shutdown(Manager *m) {
788 assert(m);
789
790 if (m->dev_autofs_fd >= 0)
791 close_nointr_nofail(m->dev_autofs_fd);
792}
793
5632e374
LP
794static void automount_reset_maintenance(Unit *u) {
795 Automount *a = AUTOMOUNT(u);
796
797 assert(a);
798
799 if (a->state == AUTOMOUNT_MAINTENANCE)
800 automount_set_state(a, AUTOMOUNT_DEAD);
801
802 a->failure = false;
803}
804
a16e1123
LP
805static const char* const automount_state_table[_AUTOMOUNT_STATE_MAX] = {
806 [AUTOMOUNT_DEAD] = "dead",
807 [AUTOMOUNT_WAITING] = "waiting",
808 [AUTOMOUNT_RUNNING] = "running",
18c78fb1 809 [AUTOMOUNT_MAINTENANCE] = "maintenance"
a16e1123
LP
810};
811
812DEFINE_STRING_TABLE_LOOKUP(automount_state, AutomountState);
813
87f0e418 814const UnitVTable automount_vtable = {
8d567588 815 .suffix = ".automount",
5cb5a6ff 816
e537352b 817 .no_alias = true,
8d567588 818 .no_instances = true,
e537352b 819
034c6ed7 820 .init = automount_init,
e537352b 821 .load = automount_load,
034c6ed7 822 .done = automount_done,
5cb5a6ff 823
a16e1123
LP
824 .coldplug = automount_coldplug,
825
034c6ed7 826 .dump = automount_dump,
5cb5a6ff 827
8d567588
LP
828 .start = automount_start,
829 .stop = automount_stop,
830
a16e1123
LP
831 .serialize = automount_serialize,
832 .deserialize_item = automount_deserialize_item,
833
10a94420 834 .active_state = automount_active_state,
8d567588
LP
835 .sub_state_to_string = automount_sub_state_to_string,
836
701cc384
LP
837 .check_gc = automount_check_gc,
838
8d567588
LP
839 .fd_event = automount_fd_event,
840
5632e374
LP
841 .reset_maintenance = automount_reset_maintenance,
842
4139c1b2
LP
843 .bus_message_handler = bus_automount_message_handler,
844
8d567588 845 .shutdown = automount_shutdown
5cb5a6ff 846};