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