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