]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/automount.c
clang: fix numerous little issues found with clang-analyzer
[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
75d287d3 156 if ((r = unit_add_dependency_by_name(UNIT(a), UNIT_AFTER, SPECIAL_FSCK_TARGET, NULL, true)) < 0)
2edd4434
LP
157 return r;
158
69dd2852 159 if ((r = unit_add_two_dependencies_by_name(UNIT(a), UNIT_BEFORE, UNIT_CONFLICTED_BY, SPECIAL_UMOUNT_TARGET, NULL, true)) < 0)
2edd4434
LP
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
56cf987f
DW
307 label_fix("/dev/autofs");
308
a16e1123 309 if ((m->dev_autofs_fd = open("/dev/autofs", O_CLOEXEC|O_RDONLY)) < 0) {
11c3a4ee
LP
310 log_error("Failed to open /dev/autofs: %s", strerror(errno));
311 return -errno;
8d567588
LP
312 }
313
314 init_autofs_dev_ioctl(&param);
315 if (ioctl(m->dev_autofs_fd, AUTOFS_DEV_IOCTL_VERSION, &param) < 0) {
316 close_nointr_nofail(m->dev_autofs_fd);
317 m->dev_autofs_fd = -1;
318 return -errno;
319 }
320
321 log_debug("Autofs kernel version %i.%i", param.ver_major, param.ver_minor);
322
323 return m->dev_autofs_fd;
324}
325
326static int open_ioctl_fd(int dev_autofs_fd, const char *where, dev_t devid) {
327 struct autofs_dev_ioctl *param;
328 size_t l;
329 int r;
330
331 assert(dev_autofs_fd >= 0);
332 assert(where);
333
334 l = sizeof(struct autofs_dev_ioctl) + strlen(where) + 1;
335
336 if (!(param = malloc(l)))
337 return -ENOMEM;
338
339 init_autofs_dev_ioctl(param);
340 param->size = l;
341 param->ioctlfd = -1;
342 param->openmount.devid = devid;
343 strcpy(param->path, where);
344
345 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_OPENMOUNT, param) < 0) {
346 r = -errno;
347 goto finish;
348 }
349
350 if (param->ioctlfd < 0) {
351 r = -EIO;
352 goto finish;
353 }
354
a16e1123 355 fd_cloexec(param->ioctlfd, true);
8d567588
LP
356 r = param->ioctlfd;
357
358finish:
359 free(param);
360 return r;
361}
362
363static int autofs_protocol(int dev_autofs_fd, int ioctl_fd) {
364 uint32_t major, minor;
365 struct autofs_dev_ioctl param;
366
367 assert(dev_autofs_fd >= 0);
368 assert(ioctl_fd >= 0);
369
370 init_autofs_dev_ioctl(&param);
371 param.ioctlfd = ioctl_fd;
372
373 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOVER, &param) < 0)
374 return -errno;
375
376 major = param.protover.version;
377
378 init_autofs_dev_ioctl(&param);
379 param.ioctlfd = ioctl_fd;
380
381 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOSUBVER, &param) < 0)
382 return -errno;
383
384 minor = param.protosubver.sub_version;
385
386 log_debug("Autofs protocol version %i.%i", major, minor);
387 return 0;
388}
389
390static int autofs_set_timeout(int dev_autofs_fd, int ioctl_fd, time_t sec) {
391 struct autofs_dev_ioctl param;
392
393 assert(dev_autofs_fd >= 0);
394 assert(ioctl_fd >= 0);
395
396 init_autofs_dev_ioctl(&param);
397 param.ioctlfd = ioctl_fd;
398 param.timeout.timeout = sec;
399
400 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_TIMEOUT, &param) < 0)
401 return -errno;
402
403 return 0;
404}
405
406static int autofs_send_ready(int dev_autofs_fd, int ioctl_fd, uint32_t token, int status) {
407 struct autofs_dev_ioctl param;
408
409 assert(dev_autofs_fd >= 0);
410 assert(ioctl_fd >= 0);
411
412 init_autofs_dev_ioctl(&param);
413 param.ioctlfd = ioctl_fd;
414
415 if (status) {
416 param.fail.token = token;
417 param.fail.status = status;
418 } else
419 param.ready.token = token;
420
421 if (ioctl(dev_autofs_fd, status ? AUTOFS_DEV_IOCTL_FAIL : AUTOFS_DEV_IOCTL_READY, &param) < 0)
422 return -errno;
423
424 return 0;
425}
426
427int automount_send_ready(Automount *a, int status) {
428 int ioctl_fd, r;
429 unsigned token;
430
431 assert(a);
432 assert(status <= 0);
433
434 if (set_isempty(a->tokens))
435 return 0;
436
4cd1fbcc 437 if ((ioctl_fd = open_ioctl_fd(a->meta.manager->dev_autofs_fd, a->where, a->dev_id)) < 0) {
8d567588
LP
438 r = ioctl_fd;
439 goto fail;
440 }
441
442 if (status)
443 log_debug("Sending failure: %s", strerror(-status));
444 else
445 log_debug("Sending success.");
446
e364ad06
LP
447 r = 0;
448
8d567588
LP
449 /* Autofs thankfully does not hand out 0 as a token */
450 while ((token = PTR_TO_UINT(set_steal_first(a->tokens)))) {
451 int k;
452
453 /* Autofs fun fact II:
454 *
455 * if you pass a positive status code here, the kernel will
456 * freeze! Yay! */
457
4cd1fbcc 458 if ((k = autofs_send_ready(a->meta.manager->dev_autofs_fd,
8d567588
LP
459 ioctl_fd,
460 token,
461 status)) < 0)
462 r = k;
463 }
464
8d567588
LP
465fail:
466 if (ioctl_fd >= 0)
467 close_nointr_nofail(ioctl_fd);
468
469 return r;
470}
471
472static void automount_enter_waiting(Automount *a) {
473 int p[2] = { -1, -1 };
474 char name[32], options[128];
475 bool mounted = false;
476 int r, ioctl_fd = -1, dev_autofs_fd;
477 struct stat st;
478
479 assert(a);
480 assert(a->pipe_fd < 0);
481 assert(a->where);
482
483 if (a->tokens)
484 set_clear(a->tokens);
8d567588 485
4cd1fbcc 486 if ((dev_autofs_fd = open_dev_autofs(a->meta.manager)) < 0) {
8d567588
LP
487 r = dev_autofs_fd;
488 goto fail;
489 }
490
491 /* We knowingly ignore the results of this call */
492 mkdir_p(a->where, 0555);
493
a16e1123 494 if (pipe2(p, O_NONBLOCK|O_CLOEXEC) < 0) {
8d567588
LP
495 r = -errno;
496 goto fail;
497 }
498
499 snprintf(options, sizeof(options), "fd=%i,pgrp=%u,minproto=5,maxproto=5,direct", p[1], (unsigned) getpgrp());
500 char_array_0(options);
501
502 snprintf(name, sizeof(name), "systemd-%u", (unsigned) getpid());
503 char_array_0(name);
504
505 if (mount(name, a->where, "autofs", 0, options) < 0) {
506 r = -errno;
507 goto fail;
508 }
509
510 mounted = true;
511
512 close_nointr_nofail(p[1]);
513 p[1] = -1;
514
515 if (stat(a->where, &st) < 0) {
516 r = -errno;
517 goto fail;
518 }
519
520 if ((ioctl_fd = open_ioctl_fd(dev_autofs_fd, a->where, st.st_dev)) < 0) {
521 r = ioctl_fd;
522 goto fail;
523 }
524
525 if ((r = autofs_protocol(dev_autofs_fd, ioctl_fd)) < 0)
526 goto fail;
527
528 if ((r = autofs_set_timeout(dev_autofs_fd, ioctl_fd, 300)) < 0)
529 goto fail;
530
531 /* Autofs fun fact:
532 *
533 * Unless we close the ioctl fd here, for some weird reason
534 * the direct mount will not receive events from the
535 * kernel. */
536
537 close_nointr_nofail(ioctl_fd);
538 ioctl_fd = -1;
539
540 if ((r = unit_watch_fd(UNIT(a), p[0], EPOLLIN, &a->pipe_watch)) < 0)
541 goto fail;
542
543 a->pipe_fd = p[0];
544 a->dev_id = st.st_dev;
545
546 automount_set_state(a, AUTOMOUNT_WAITING);
547
548 return;
549
550fail:
551 assert_se(close_pipe(p) == 0);
552
553 if (ioctl_fd >= 0)
554 close_nointr_nofail(ioctl_fd);
555
556 if (mounted)
557 repeat_unmout(a->where);
558
559 log_error("Failed to initialize automounter: %s", strerror(-r));
560 automount_enter_dead(a, false);
561}
562
563static void automount_enter_runnning(Automount *a) {
564 int r;
565 struct stat st;
398ef8ba 566 DBusError error;
8d567588
LP
567
568 assert(a);
569 assert(a->mount);
570
398ef8ba
LP
571 dbus_error_init(&error);
572
ba3e67a7
LP
573 /* We don't take mount requests anymore if we are supposed to
574 * shut down anyway */
575 if (a->meta.job && a->meta.job->type == JOB_STOP) {
576 automount_send_ready(a, -EHOSTDOWN);
577 return;
578 }
579
1cf18f27 580 mkdir_p(a->where, a->directory_mode);
8d567588 581
1cf18f27
LP
582 /* Before we do anything, let's see if somebody is playing games with us? */
583 if (lstat(a->where, &st) < 0) {
8d567588
LP
584 log_warning("%s failed stat automount point: %m", a->meta.id);
585 goto fail;
586 }
587
588 if (!S_ISDIR(st.st_mode) || st.st_dev != a->dev_id)
589 log_info("%s's automount point already active?", a->meta.id);
398ef8ba
LP
590 else if ((r = manager_add_job(a->meta.manager, JOB_START, UNIT(a->mount), JOB_REPLACE, true, &error, NULL)) < 0) {
591 log_warning("%s failed to queue mount startup job: %s", a->meta.id, bus_error(&error, r));
8d567588
LP
592 goto fail;
593 }
594
595 automount_set_state(a, AUTOMOUNT_RUNNING);
596 return;
597
598fail:
599 automount_enter_dead(a, false);
398ef8ba 600 dbus_error_free(&error);
8d567588
LP
601}
602
603static int automount_start(Unit *u) {
604 Automount *a = AUTOMOUNT(u);
605
606 assert(a);
607
18c78fb1 608 assert(a->state == AUTOMOUNT_DEAD || a->state == AUTOMOUNT_MAINTENANCE);
01f78473 609
8d567588
LP
610 if (path_is_mount_point(a->where)) {
611 log_error("Path %s is already a mount point, refusing start for %s", a->where, u->meta.id);
612 return -EEXIST;
613 }
614
01f78473
LP
615 if (a->mount->meta.load_state != UNIT_LOADED)
616 return -ENOENT;
8d567588
LP
617
618 a->failure = false;
619 automount_enter_waiting(a);
620 return 0;
621}
622
623static int automount_stop(Unit *u) {
624 Automount *a = AUTOMOUNT(u);
625
626 assert(a);
627
628 assert(a->state == AUTOMOUNT_WAITING || a->state == AUTOMOUNT_RUNNING);
629
630 automount_enter_dead(a, true);
631 return 0;
632}
633
a16e1123
LP
634static int automount_serialize(Unit *u, FILE *f, FDSet *fds) {
635 Automount *a = AUTOMOUNT(u);
636 void *p;
637 Iterator i;
638
639 assert(a);
640 assert(f);
641 assert(fds);
642
643 unit_serialize_item(u, f, "state", automount_state_to_string(a->state));
644 unit_serialize_item(u, f, "failure", yes_no(a->failure));
645 unit_serialize_item_format(u, f, "dev-id", "%u", (unsigned) a->dev_id);
646
647 SET_FOREACH(p, a->tokens, i)
648 unit_serialize_item_format(u, f, "token", "%u", PTR_TO_UINT(p));
649
650 if (a->pipe_fd >= 0) {
651 int copy;
652
653 if ((copy = fdset_put_dup(fds, a->pipe_fd)) < 0)
654 return copy;
655
656 unit_serialize_item_format(u, f, "pipe-fd", "%i", copy);
657 }
658
659 return 0;
660}
661
662static int automount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
663 Automount *a = AUTOMOUNT(u);
664 int r;
665
666 assert(a);
667 assert(fds);
668
669 if (streq(key, "state")) {
670 AutomountState state;
671
672 if ((state = automount_state_from_string(value)) < 0)
673 log_debug("Failed to parse state value %s", value);
674 else
675 a->deserialized_state = state;
676 } else if (streq(key, "failure")) {
677 int b;
678
679 if ((b = parse_boolean(value)) < 0)
680 log_debug("Failed to parse failure value %s", value);
681 else
682 a->failure = b || a->failure;
683 } else if (streq(key, "dev-id")) {
684 unsigned d;
685
686 if (safe_atou(value, &d) < 0)
687 log_debug("Failed to parse dev-id value %s", value);
688 else
689 a->dev_id = (unsigned) d;
690 } else if (streq(key, "token")) {
691 unsigned token;
692
693 if (safe_atou(value, &token) < 0)
694 log_debug("Failed to parse token value %s", value);
695 else {
696 if (!a->tokens)
697 if (!(a->tokens = set_new(trivial_hash_func, trivial_compare_func)))
698 return -ENOMEM;
699
700 if ((r = set_put(a->tokens, UINT_TO_PTR(token))) < 0)
701 return r;
702 }
703 } else if (streq(key, "pipe-fd")) {
704 int fd;
705
706 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
707 log_debug("Failed to parse pipe-fd value %s", value);
708 else {
709 if (a->pipe_fd >= 0)
710 close_nointr_nofail(a->pipe_fd);
711
712 a->pipe_fd = fdset_remove(fds, fd);
713 }
714 } else
715 log_debug("Unknown serialization key '%s'", key);
716
717 return 0;
718}
719
87f0e418 720static UnitActiveState automount_active_state(Unit *u) {
a16e1123 721 assert(u);
87f0e418 722
e537352b 723 return state_translation_table[AUTOMOUNT(u)->state];
5cb5a6ff
LP
724}
725
10a94420
LP
726static const char *automount_sub_state_to_string(Unit *u) {
727 assert(u);
728
a16e1123 729 return automount_state_to_string(AUTOMOUNT(u)->state);
10a94420
LP
730}
731
701cc384
LP
732static bool automount_check_gc(Unit *u) {
733 Automount *a = AUTOMOUNT(u);
734
735 assert(a);
736
737 return UNIT_VTABLE(UNIT(a->mount))->check_gc(UNIT(a->mount));
738}
739
8d567588 740static void automount_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
701cc384 741 Automount *a = AUTOMOUNT(u);
8d567588
LP
742 union autofs_v5_packet_union packet;
743 ssize_t l;
744 int r;
745
8d567588
LP
746 assert(a);
747 assert(fd == a->pipe_fd);
748
749 if (events != EPOLLIN) {
750 log_error("Got invalid poll event on pipe.");
751 goto fail;
752 }
753
eb22ac37 754 if ((l = loop_read(a->pipe_fd, &packet, sizeof(packet), true)) != sizeof(packet)) {
8d567588
LP
755 log_error("Invalid read from pipe: %s", l < 0 ? strerror(-l) : "short read");
756 goto fail;
757 }
758
759 switch (packet.hdr.type) {
760
761 case autofs_ptype_missing_direct:
762 log_debug("Got direct mount request for %s", packet.v5_packet.name);
763
a16e1123
LP
764 if (!a->tokens)
765 if (!(a->tokens = set_new(trivial_hash_func, trivial_compare_func))) {
766 log_error("Failed to allocate token set.");
767 goto fail;
768 }
769
8d567588
LP
770 if ((r = set_put(a->tokens, UINT_TO_PTR(packet.v5_packet.wait_queue_token))) < 0) {
771 log_error("Failed to remember token: %s", strerror(-r));
772 goto fail;
773 }
774
775 automount_enter_runnning(a);
776 break;
777
778 default:
779 log_error("Received unknown automount request %i", packet.hdr.type);
780 break;
781 }
782
783 return;
784
785fail:
786 automount_enter_dead(a, false);
787}
788
789static void automount_shutdown(Manager *m) {
790 assert(m);
791
792 if (m->dev_autofs_fd >= 0)
793 close_nointr_nofail(m->dev_autofs_fd);
794}
795
5632e374
LP
796static void automount_reset_maintenance(Unit *u) {
797 Automount *a = AUTOMOUNT(u);
798
799 assert(a);
800
801 if (a->state == AUTOMOUNT_MAINTENANCE)
802 automount_set_state(a, AUTOMOUNT_DEAD);
803
804 a->failure = false;
805}
806
a16e1123
LP
807static const char* const automount_state_table[_AUTOMOUNT_STATE_MAX] = {
808 [AUTOMOUNT_DEAD] = "dead",
809 [AUTOMOUNT_WAITING] = "waiting",
810 [AUTOMOUNT_RUNNING] = "running",
18c78fb1 811 [AUTOMOUNT_MAINTENANCE] = "maintenance"
a16e1123
LP
812};
813
814DEFINE_STRING_TABLE_LOOKUP(automount_state, AutomountState);
815
87f0e418 816const UnitVTable automount_vtable = {
8d567588 817 .suffix = ".automount",
5cb5a6ff 818
e537352b 819 .no_alias = true,
8d567588 820 .no_instances = true,
e537352b 821
034c6ed7 822 .init = automount_init,
e537352b 823 .load = automount_load,
034c6ed7 824 .done = automount_done,
5cb5a6ff 825
a16e1123
LP
826 .coldplug = automount_coldplug,
827
034c6ed7 828 .dump = automount_dump,
5cb5a6ff 829
8d567588
LP
830 .start = automount_start,
831 .stop = automount_stop,
832
a16e1123
LP
833 .serialize = automount_serialize,
834 .deserialize_item = automount_deserialize_item,
835
10a94420 836 .active_state = automount_active_state,
8d567588
LP
837 .sub_state_to_string = automount_sub_state_to_string,
838
701cc384
LP
839 .check_gc = automount_check_gc,
840
8d567588
LP
841 .fd_event = automount_fd_event,
842
5632e374
LP
843 .reset_maintenance = automount_reset_maintenance,
844
4139c1b2
LP
845 .bus_message_handler = bus_automount_message_handler,
846
8d567588 847 .shutdown = automount_shutdown
5cb5a6ff 848};