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