chiark / gitweb /
login: move seat udev rules to login subdir
[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->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         UNIT(a)->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             (UNIT(a)->manager->exit_code != MANAGER_RELOAD &&
98              UNIT(a)->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         unit_ref_unset(&a->mount);
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 (UNIT(a)->load_state != UNIT_LOADED ||
124             UNIT(m)->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         Unit *other;
141         int r;
142
143         assert(a);
144
145         LIST_FOREACH(units_by_type, other, UNIT(a)->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 (UNIT(a)->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 (UNIT(a)->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.", UNIT(a)->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->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->load_state == UNIT_LOADED) {
208                 Unit *x;
209
210                 if (!a->where)
211                         if (!(a->where = unit_name_to_path(u->id)))
212                                 return -ENOMEM;
213
214                 path_kill_slashes(a->where);
215
216                 if ((r = automount_add_mount_links(a)) < 0)
217                         return r;
218
219                 r = unit_load_related_unit(u, ".mount", &x);
220                 if (r < 0)
221                         return r;
222
223                 unit_ref_set(&a->mount, x);
224
225                 r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(a->mount), true);
226                 if (r < 0)
227                         return r;
228
229                 if (UNIT(a)->default_dependencies)
230                         if ((r = automount_add_default_dependencies(a)) < 0)
231                                 return r;
232         }
233
234         return automount_verify(a);
235 }
236
237 static void automount_set_state(Automount *a, AutomountState state) {
238         AutomountState old_state;
239         assert(a);
240
241         old_state = a->state;
242         a->state = state;
243
244         if (state != AUTOMOUNT_WAITING &&
245             state != AUTOMOUNT_RUNNING)
246                 unmount_autofs(a);
247
248         if (state != old_state)
249                 log_debug("%s changed %s -> %s",
250                           UNIT(a)->id,
251                           automount_state_to_string(old_state),
252                           automount_state_to_string(state));
253
254         unit_notify(UNIT(a), state_translation_table[old_state], state_translation_table[state], true);
255 }
256
257 static int automount_coldplug(Unit *u) {
258         Automount *a = AUTOMOUNT(u);
259         int r;
260
261         assert(a);
262         assert(a->state == AUTOMOUNT_DEAD);
263
264         if (a->deserialized_state != a->state) {
265
266                 if ((r = open_dev_autofs(u->manager)) < 0)
267                         return r;
268
269                 if (a->deserialized_state == AUTOMOUNT_WAITING ||
270                     a->deserialized_state == AUTOMOUNT_RUNNING) {
271
272                         assert(a->pipe_fd >= 0);
273
274                         if ((r = unit_watch_fd(UNIT(a), a->pipe_fd, EPOLLIN, &a->pipe_watch)) < 0)
275                                 return r;
276                 }
277
278                 automount_set_state(a, a->deserialized_state);
279         }
280
281         return 0;
282 }
283
284 static void automount_dump(Unit *u, FILE *f, const char *prefix) {
285         Automount *a = AUTOMOUNT(u);
286
287         assert(a);
288
289         fprintf(f,
290                 "%sAutomount State: %s\n"
291                 "%sWhere: %s\n"
292                 "%sDirectoryMode: %04o\n",
293                 prefix, automount_state_to_string(a->state),
294                 prefix, a->where,
295                 prefix, a->directory_mode);
296 }
297
298 static void automount_enter_dead(Automount *a, bool success) {
299         assert(a);
300
301         if (!success)
302                 a->failure = true;
303
304         automount_set_state(a, a->failure ? AUTOMOUNT_FAILED : AUTOMOUNT_DEAD);
305 }
306
307 static int open_dev_autofs(Manager *m) {
308         struct autofs_dev_ioctl param;
309
310         assert(m);
311
312         if (m->dev_autofs_fd >= 0)
313                 return m->dev_autofs_fd;
314
315         label_fix("/dev/autofs", false);
316
317         if ((m->dev_autofs_fd = open("/dev/autofs", O_CLOEXEC|O_RDONLY)) < 0) {
318                 log_error("Failed to open /dev/autofs: %s", strerror(errno));
319                 return -errno;
320         }
321
322         init_autofs_dev_ioctl(&param);
323         if (ioctl(m->dev_autofs_fd, AUTOFS_DEV_IOCTL_VERSION, &param) < 0) {
324                 close_nointr_nofail(m->dev_autofs_fd);
325                 m->dev_autofs_fd = -1;
326                 return -errno;
327         }
328
329         log_debug("Autofs kernel version %i.%i", param.ver_major, param.ver_minor);
330
331         return m->dev_autofs_fd;
332 }
333
334 static int open_ioctl_fd(int dev_autofs_fd, const char *where, dev_t devid) {
335         struct autofs_dev_ioctl *param;
336         size_t l;
337         int r;
338
339         assert(dev_autofs_fd >= 0);
340         assert(where);
341
342         l = sizeof(struct autofs_dev_ioctl) + strlen(where) + 1;
343
344         if (!(param = malloc(l)))
345                 return -ENOMEM;
346
347         init_autofs_dev_ioctl(param);
348         param->size = l;
349         param->ioctlfd = -1;
350         param->openmount.devid = devid;
351         strcpy(param->path, where);
352
353         if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_OPENMOUNT, param) < 0) {
354                 r = -errno;
355                 goto finish;
356         }
357
358         if (param->ioctlfd < 0) {
359                 r = -EIO;
360                 goto finish;
361         }
362
363         fd_cloexec(param->ioctlfd, true);
364         r = param->ioctlfd;
365
366 finish:
367         free(param);
368         return r;
369 }
370
371 static int autofs_protocol(int dev_autofs_fd, int ioctl_fd) {
372         uint32_t major, minor;
373         struct autofs_dev_ioctl param;
374
375         assert(dev_autofs_fd >= 0);
376         assert(ioctl_fd >= 0);
377
378         init_autofs_dev_ioctl(&param);
379         param.ioctlfd = ioctl_fd;
380
381         if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOVER, &param) < 0)
382                 return -errno;
383
384         major = param.protover.version;
385
386         init_autofs_dev_ioctl(&param);
387         param.ioctlfd = ioctl_fd;
388
389         if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOSUBVER, &param) < 0)
390                 return -errno;
391
392         minor = param.protosubver.sub_version;
393
394         log_debug("Autofs protocol version %i.%i", major, minor);
395         return 0;
396 }
397
398 static int autofs_set_timeout(int dev_autofs_fd, int ioctl_fd, time_t sec) {
399         struct autofs_dev_ioctl param;
400
401         assert(dev_autofs_fd >= 0);
402         assert(ioctl_fd >= 0);
403
404         init_autofs_dev_ioctl(&param);
405         param.ioctlfd = ioctl_fd;
406         param.timeout.timeout = sec;
407
408         if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_TIMEOUT, &param) < 0)
409                 return -errno;
410
411         return 0;
412 }
413
414 static int autofs_send_ready(int dev_autofs_fd, int ioctl_fd, uint32_t token, int status) {
415         struct autofs_dev_ioctl param;
416
417         assert(dev_autofs_fd >= 0);
418         assert(ioctl_fd >= 0);
419
420         init_autofs_dev_ioctl(&param);
421         param.ioctlfd = ioctl_fd;
422
423         if (status) {
424                 param.fail.token = token;
425                 param.fail.status = status;
426         } else
427                 param.ready.token = token;
428
429         if (ioctl(dev_autofs_fd, status ? AUTOFS_DEV_IOCTL_FAIL : AUTOFS_DEV_IOCTL_READY, &param) < 0)
430                 return -errno;
431
432         return 0;
433 }
434
435 int automount_send_ready(Automount *a, int status) {
436         int ioctl_fd, r;
437         unsigned token;
438
439         assert(a);
440         assert(status <= 0);
441
442         if (set_isempty(a->tokens))
443                 return 0;
444
445         if ((ioctl_fd = open_ioctl_fd(UNIT(a)->manager->dev_autofs_fd, a->where, a->dev_id)) < 0) {
446                 r = ioctl_fd;
447                 goto fail;
448         }
449
450         if (status)
451                 log_debug("Sending failure: %s", strerror(-status));
452         else
453                 log_debug("Sending success.");
454
455         r = 0;
456
457         /* Autofs thankfully does not hand out 0 as a token */
458         while ((token = PTR_TO_UINT(set_steal_first(a->tokens)))) {
459                 int k;
460
461                 /* Autofs fun fact II:
462                  *
463                  * if you pass a positive status code here, the kernel will
464                  * freeze! Yay! */
465
466                 if ((k = autofs_send_ready(UNIT(a)->manager->dev_autofs_fd,
467                                            ioctl_fd,
468                                            token,
469                                            status)) < 0)
470                         r = k;
471         }
472
473 fail:
474         if (ioctl_fd >= 0)
475                 close_nointr_nofail(ioctl_fd);
476
477         return r;
478 }
479
480 static void automount_enter_waiting(Automount *a) {
481         int p[2] = { -1, -1 };
482         char name[32], options[128];
483         bool mounted = false;
484         int r, ioctl_fd = -1, dev_autofs_fd;
485         struct stat st;
486
487         assert(a);
488         assert(a->pipe_fd < 0);
489         assert(a->where);
490
491         if (a->tokens)
492                 set_clear(a->tokens);
493
494         if ((dev_autofs_fd = open_dev_autofs(UNIT(a)->manager)) < 0) {
495                 r = dev_autofs_fd;
496                 goto fail;
497         }
498
499         /* We knowingly ignore the results of this call */
500         mkdir_p(a->where, 0555);
501
502         if (pipe2(p, O_NONBLOCK|O_CLOEXEC) < 0) {
503                 r = -errno;
504                 goto fail;
505         }
506
507         snprintf(options, sizeof(options), "fd=%i,pgrp=%u,minproto=5,maxproto=5,direct", p[1], (unsigned) getpgrp());
508         char_array_0(options);
509
510         snprintf(name, sizeof(name), "systemd-%u", (unsigned) getpid());
511         char_array_0(name);
512
513         if (mount(name, a->where, "autofs", 0, options) < 0) {
514                 r = -errno;
515                 goto fail;
516         }
517
518         mounted = true;
519
520         close_nointr_nofail(p[1]);
521         p[1] = -1;
522
523         if (stat(a->where, &st) < 0) {
524                 r = -errno;
525                 goto fail;
526         }
527
528         if ((ioctl_fd = open_ioctl_fd(dev_autofs_fd, a->where, st.st_dev)) < 0) {
529                 r = ioctl_fd;
530                 goto fail;
531         }
532
533         if ((r = autofs_protocol(dev_autofs_fd, ioctl_fd)) < 0)
534                 goto fail;
535
536         if ((r = autofs_set_timeout(dev_autofs_fd, ioctl_fd, 300)) < 0)
537                 goto fail;
538
539         /* Autofs fun fact:
540          *
541          * Unless we close the ioctl fd here, for some weird reason
542          * the direct mount will not receive events from the
543          * kernel. */
544
545         close_nointr_nofail(ioctl_fd);
546         ioctl_fd = -1;
547
548         if ((r = unit_watch_fd(UNIT(a), p[0], EPOLLIN, &a->pipe_watch)) < 0)
549                 goto fail;
550
551         a->pipe_fd = p[0];
552         a->dev_id = st.st_dev;
553
554         automount_set_state(a, AUTOMOUNT_WAITING);
555
556         return;
557
558 fail:
559         assert_se(close_pipe(p) == 0);
560
561         if (ioctl_fd >= 0)
562                 close_nointr_nofail(ioctl_fd);
563
564         if (mounted)
565                 repeat_unmout(a->where);
566
567         log_error("Failed to initialize automounter: %s", strerror(-r));
568         automount_enter_dead(a, false);
569 }
570
571 static void automount_enter_runnning(Automount *a) {
572         int r;
573         struct stat st;
574         DBusError error;
575
576         assert(a);
577         assert(UNIT_DEREF(a->mount));
578
579         dbus_error_init(&error);
580
581         /* We don't take mount requests anymore if we are supposed to
582          * shut down anyway */
583         if (unit_pending_inactive(UNIT(a))) {
584                 log_debug("Suppressing automount request on %s since unit stop is scheduled.", UNIT(a)->id);
585                 automount_send_ready(a, -EHOSTDOWN);
586                 return;
587         }
588
589         mkdir_p(a->where, a->directory_mode);
590
591         /* Before we do anything, let's see if somebody is playing games with us? */
592         if (lstat(a->where, &st) < 0) {
593                 log_warning("%s failed to stat automount point: %m", UNIT(a)->id);
594                 goto fail;
595         }
596
597         if (!S_ISDIR(st.st_mode) || st.st_dev != a->dev_id)
598                 log_info("%s's automount point already active?", UNIT(a)->id);
599         else if ((r = manager_add_job(UNIT(a)->manager, JOB_START, UNIT_DEREF(a->mount), JOB_REPLACE, true, &error, NULL)) < 0) {
600                 log_warning("%s failed to queue mount startup job: %s", UNIT(a)->id, bus_error(&error, r));
601                 goto fail;
602         }
603
604         automount_set_state(a, AUTOMOUNT_RUNNING);
605         return;
606
607 fail:
608         automount_enter_dead(a, false);
609         dbus_error_free(&error);
610 }
611
612 static int automount_start(Unit *u) {
613         Automount *a = AUTOMOUNT(u);
614
615         assert(a);
616
617         assert(a->state == AUTOMOUNT_DEAD || a->state == AUTOMOUNT_FAILED);
618
619         if (path_is_mount_point(a->where, false)) {
620                 log_error("Path %s is already a mount point, refusing start for %s", a->where, u->id);
621                 return -EEXIST;
622         }
623
624         if (UNIT_DEREF(a->mount)->load_state != UNIT_LOADED)
625                 return -ENOENT;
626
627         a->failure = false;
628         automount_enter_waiting(a);
629         return 0;
630 }
631
632 static int automount_stop(Unit *u) {
633         Automount *a = AUTOMOUNT(u);
634
635         assert(a);
636
637         assert(a->state == AUTOMOUNT_WAITING || a->state == AUTOMOUNT_RUNNING);
638
639         automount_enter_dead(a, true);
640         return 0;
641 }
642
643 static int automount_serialize(Unit *u, FILE *f, FDSet *fds) {
644         Automount *a = AUTOMOUNT(u);
645         void *p;
646         Iterator i;
647
648         assert(a);
649         assert(f);
650         assert(fds);
651
652         unit_serialize_item(u, f, "state", automount_state_to_string(a->state));
653         unit_serialize_item(u, f, "failure", yes_no(a->failure));
654         unit_serialize_item_format(u, f, "dev-id", "%u", (unsigned) a->dev_id);
655
656         SET_FOREACH(p, a->tokens, i)
657                 unit_serialize_item_format(u, f, "token", "%u", PTR_TO_UINT(p));
658
659         if (a->pipe_fd >= 0) {
660                 int copy;
661
662                 if ((copy = fdset_put_dup(fds, a->pipe_fd)) < 0)
663                         return copy;
664
665                 unit_serialize_item_format(u, f, "pipe-fd", "%i", copy);
666         }
667
668         return 0;
669 }
670
671 static int automount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
672         Automount *a = AUTOMOUNT(u);
673         int r;
674
675         assert(a);
676         assert(fds);
677
678         if (streq(key, "state")) {
679                 AutomountState state;
680
681                 if ((state = automount_state_from_string(value)) < 0)
682                         log_debug("Failed to parse state value %s", value);
683                 else
684                         a->deserialized_state = state;
685         } else if (streq(key, "failure")) {
686                 int b;
687
688                 if ((b = parse_boolean(value)) < 0)
689                         log_debug("Failed to parse failure value %s", value);
690                 else
691                         a->failure = b || a->failure;
692         } else if (streq(key, "dev-id")) {
693                 unsigned d;
694
695                 if (safe_atou(value, &d) < 0)
696                         log_debug("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("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                         if ((r = set_put(a->tokens, UINT_TO_PTR(token))) < 0)
710                                 return r;
711                 }
712         } else if (streq(key, "pipe-fd")) {
713                 int fd;
714
715                 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
716                         log_debug("Failed to parse pipe-fd value %s", value);
717                 else {
718                         if (a->pipe_fd >= 0)
719                                 close_nointr_nofail(a->pipe_fd);
720
721                         a->pipe_fd = fdset_remove(fds, fd);
722                 }
723         } else
724                 log_debug("Unknown serialization key '%s'", key);
725
726         return 0;
727 }
728
729 static UnitActiveState automount_active_state(Unit *u) {
730         assert(u);
731
732         return state_translation_table[AUTOMOUNT(u)->state];
733 }
734
735 static const char *automount_sub_state_to_string(Unit *u) {
736         assert(u);
737
738         return automount_state_to_string(AUTOMOUNT(u)->state);
739 }
740
741 static bool automount_check_gc(Unit *u) {
742         Automount *a = AUTOMOUNT(u);
743
744         assert(a);
745
746         if (!UNIT_DEREF(a->mount))
747                 return false;
748
749         return UNIT_VTABLE(UNIT_DEREF(a->mount))->check_gc(UNIT_DEREF(a->mount));
750 }
751
752 static void automount_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
753         Automount *a = AUTOMOUNT(u);
754         union autofs_v5_packet_union packet;
755         ssize_t l;
756         int r;
757
758         assert(a);
759         assert(fd == a->pipe_fd);
760
761         if (events != EPOLLIN) {
762                 log_error("Got invalid poll event on pipe.");
763                 goto fail;
764         }
765
766         if ((l = loop_read(a->pipe_fd, &packet, sizeof(packet), true)) != sizeof(packet)) {
767                 log_error("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                         char *p = NULL;
777
778                         get_process_comm(packet.v5_packet.pid, &p);
779                         log_debug("Got direct mount request for %s, triggered by %lu (%s)", packet.v5_packet.name, (unsigned long) packet.v5_packet.pid, strna(p));
780                         free(p);
781
782                 } else
783                         log_debug("Got direct mount request for %s", packet.v5_packet.name);
784
785                 if (!a->tokens)
786                         if (!(a->tokens = set_new(trivial_hash_func, trivial_compare_func))) {
787                                 log_error("Failed to allocate token set.");
788                                 goto fail;
789                         }
790
791                 if ((r = set_put(a->tokens, UINT_TO_PTR(packet.v5_packet.wait_queue_token))) < 0) {
792                         log_error("Failed to remember token: %s", strerror(-r));
793                         goto fail;
794                 }
795
796                 automount_enter_runnning(a);
797                 break;
798
799         default:
800                 log_error("Received unknown automount request %i", packet.hdr.type);
801                 break;
802         }
803
804         return;
805
806 fail:
807         automount_enter_dead(a, false);
808 }
809
810 static void automount_shutdown(Manager *m) {
811         assert(m);
812
813         if (m->dev_autofs_fd >= 0)
814                 close_nointr_nofail(m->dev_autofs_fd);
815 }
816
817 static void automount_reset_failed(Unit *u) {
818         Automount *a = AUTOMOUNT(u);
819
820         assert(a);
821
822         if (a->state == AUTOMOUNT_FAILED)
823                 automount_set_state(a, AUTOMOUNT_DEAD);
824
825         a->failure = false;
826 }
827
828 static const char* const automount_state_table[_AUTOMOUNT_STATE_MAX] = {
829         [AUTOMOUNT_DEAD] = "dead",
830         [AUTOMOUNT_WAITING] = "waiting",
831         [AUTOMOUNT_RUNNING] = "running",
832         [AUTOMOUNT_FAILED] = "failed"
833 };
834
835 DEFINE_STRING_TABLE_LOOKUP(automount_state, AutomountState);
836
837 const UnitVTable automount_vtable = {
838         .suffix = ".automount",
839         .object_size = sizeof(Automount),
840         .sections =
841                 "Unit\0"
842                 "Automount\0"
843                 "Install\0",
844
845         .no_alias = true,
846         .no_instances = true,
847
848         .init = automount_init,
849         .load = automount_load,
850         .done = automount_done,
851
852         .coldplug = automount_coldplug,
853
854         .dump = automount_dump,
855
856         .start = automount_start,
857         .stop = automount_stop,
858
859         .serialize = automount_serialize,
860         .deserialize_item = automount_deserialize_item,
861
862         .active_state = automount_active_state,
863         .sub_state_to_string = automount_sub_state_to_string,
864
865         .check_gc = automount_check_gc,
866
867         .fd_event = automount_fd_event,
868
869         .reset_failed = automount_reset_failed,
870
871         .bus_interface = "org.freedesktop.systemd1.Automount",
872         .bus_message_handler = bus_automount_message_handler,
873
874         .shutdown = automount_shutdown
875 };