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