chiark / gitweb /
manager: fix build on 32bit systems
[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 #include "dbus-common.h"
44
45 static const UnitActiveState state_translation_table[_AUTOMOUNT_STATE_MAX] = {
46         [AUTOMOUNT_DEAD] = UNIT_INACTIVE,
47         [AUTOMOUNT_WAITING] = UNIT_ACTIVE,
48         [AUTOMOUNT_RUNNING] = UNIT_ACTIVE,
49         [AUTOMOUNT_FAILED] = UNIT_FAILED
50 };
51
52 static int open_dev_autofs(Manager *m);
53
54 static void automount_init(Unit *u) {
55         Automount *a = AUTOMOUNT(u);
56
57         assert(u);
58         assert(u->load_state == UNIT_STUB);
59
60         a->pipe_watch.fd = a->pipe_fd = -1;
61         a->pipe_watch.type = WATCH_INVALID;
62
63         a->directory_mode = 0755;
64
65         UNIT(a)->ignore_on_isolate = true;
66 }
67
68 static void repeat_unmout(const char *path) {
69         assert(path);
70
71         for (;;) {
72                 /* If there are multiple mounts on a mount point, this
73                  * removes them all */
74
75                 if (umount2(path, MNT_DETACH) >= 0)
76                         continue;
77
78                 if (errno != EINVAL)
79                         log_error("Failed to unmount: %m");
80
81                 break;
82         }
83 }
84
85 static void unmount_autofs(Automount *a) {
86         assert(a);
87
88         if (a->pipe_fd < 0)
89                 return;
90
91         automount_send_ready(a, -EHOSTDOWN);
92
93         unit_unwatch_fd(UNIT(a), &a->pipe_watch);
94         close_nointr_nofail(a->pipe_fd);
95         a->pipe_fd = -1;
96
97         /* If we reload/reexecute things we keep the mount point
98          * around */
99         if (a->where &&
100             (UNIT(a)->manager->exit_code != MANAGER_RELOAD &&
101              UNIT(a)->manager->exit_code != MANAGER_REEXECUTE))
102                 repeat_unmout(a->where);
103 }
104
105 static void automount_done(Unit *u) {
106         Automount *a = AUTOMOUNT(u);
107
108         assert(a);
109
110         unmount_autofs(a);
111         unit_ref_unset(&a->mount);
112
113         free(a->where);
114         a->where = NULL;
115
116         set_free(a->tokens);
117         a->tokens = NULL;
118 }
119
120 int automount_add_one_mount_link(Automount *a, Mount *m) {
121         int r;
122
123         assert(a);
124         assert(m);
125
126         if (UNIT(a)->load_state != UNIT_LOADED ||
127             UNIT(m)->load_state != UNIT_LOADED)
128                 return 0;
129
130         if (!path_startswith(a->where, m->where))
131                 return 0;
132
133         if (path_equal(a->where, m->where))
134                 return 0;
135
136         r = unit_add_two_dependencies(UNIT(a), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true);
137         if (r < 0)
138                 return r;
139
140         return 0;
141 }
142
143 static int automount_add_mount_links(Automount *a) {
144         Unit *other;
145         int r;
146
147         assert(a);
148
149         LIST_FOREACH(units_by_type, other, UNIT(a)->manager->units_by_type[UNIT_MOUNT]) {
150                 r = automount_add_one_mount_link(a, MOUNT(other));
151                 if (r < 0)
152                         return r;
153         }
154
155         return 0;
156 }
157
158 static int automount_add_default_dependencies(Automount *a) {
159         int r;
160
161         assert(a);
162
163         if (UNIT(a)->manager->running_as != SYSTEMD_SYSTEM)
164                 return 0;
165
166         r = unit_add_two_dependencies_by_name(UNIT(a), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
167         if (r < 0)
168                 return r;
169
170         return 0;
171 }
172
173 static int automount_verify(Automount *a) {
174         bool b;
175         char *e;
176         assert(a);
177
178         if (UNIT(a)->load_state != UNIT_LOADED)
179                 return 0;
180
181         if (path_equal(a->where, "/")) {
182                 log_error("Cannot have an automount unit for the root directory. Refusing.");
183                 return -EINVAL;
184         }
185
186         e = unit_name_from_path(a->where, ".automount");
187         if (!e)
188                 return -ENOMEM;
189
190         b = unit_has_name(UNIT(a), e);
191         free(e);
192
193         if (!b) {
194                 log_error("%s's Where setting doesn't match unit name. Refusing.", UNIT(a)->id);
195                 return -EINVAL;
196         }
197
198         return 0;
199 }
200
201 static int automount_load(Unit *u) {
202         int r;
203         Automount *a = AUTOMOUNT(u);
204
205         assert(u);
206         assert(u->load_state == UNIT_STUB);
207
208         /* Load a .automount file */
209         r = unit_load_fragment_and_dropin_optional(u);
210         if (r < 0)
211                 return r;
212
213         if (u->load_state == UNIT_LOADED) {
214                 Unit *x;
215
216                 if (!a->where) {
217                         a->where = unit_name_to_path(u->id);
218                         if (!a->where)
219                                 return -ENOMEM;
220                 }
221
222                 path_kill_slashes(a->where);
223
224                 r = automount_add_mount_links(a);
225                 if (r < 0)
226                         return r;
227
228                 r = unit_load_related_unit(u, ".mount", &x);
229                 if (r < 0)
230                         return r;
231
232                 unit_ref_set(&a->mount, x);
233
234                 r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(a->mount), true);
235                 if (r < 0)
236                         return r;
237
238                 if (UNIT(a)->default_dependencies) {
239                         r = automount_add_default_dependencies(a);
240                         if (r < 0)
241                                 return r;
242                 }
243         }
244
245         return automount_verify(a);
246 }
247
248 static void automount_set_state(Automount *a, AutomountState state) {
249         AutomountState old_state;
250         assert(a);
251
252         old_state = a->state;
253         a->state = state;
254
255         if (state != AUTOMOUNT_WAITING &&
256             state != AUTOMOUNT_RUNNING)
257                 unmount_autofs(a);
258
259         if (state != old_state)
260                 log_debug("%s changed %s -> %s",
261                           UNIT(a)->id,
262                           automount_state_to_string(old_state),
263                           automount_state_to_string(state));
264
265         unit_notify(UNIT(a), state_translation_table[old_state], state_translation_table[state], true);
266 }
267
268 static int automount_coldplug(Unit *u) {
269         Automount *a = AUTOMOUNT(u);
270         int r;
271
272         assert(a);
273         assert(a->state == AUTOMOUNT_DEAD);
274
275         if (a->deserialized_state != a->state) {
276
277                 r = open_dev_autofs(u->manager);
278                 if (r < 0)
279                         return r;
280
281                 if (a->deserialized_state == AUTOMOUNT_WAITING ||
282                     a->deserialized_state == AUTOMOUNT_RUNNING) {
283
284                         assert(a->pipe_fd >= 0);
285
286                         r = unit_watch_fd(UNIT(a), a->pipe_fd, EPOLLIN, &a->pipe_watch);
287                         if (r < 0)
288                                 return r;
289                 }
290
291                 automount_set_state(a, a->deserialized_state);
292         }
293
294         return 0;
295 }
296
297 static void automount_dump(Unit *u, FILE *f, const char *prefix) {
298         Automount *a = AUTOMOUNT(u);
299
300         assert(a);
301
302         fprintf(f,
303                 "%sAutomount State: %s\n"
304                 "%sResult: %s\n"
305                 "%sWhere: %s\n"
306                 "%sDirectoryMode: %04o\n",
307                 prefix, automount_state_to_string(a->state),
308                 prefix, automount_result_to_string(a->result),
309                 prefix, a->where,
310                 prefix, a->directory_mode);
311 }
312
313 static void automount_enter_dead(Automount *a, AutomountResult f) {
314         assert(a);
315
316         if (f != AUTOMOUNT_SUCCESS)
317                 a->result = f;
318
319         automount_set_state(a, a->result != AUTOMOUNT_SUCCESS ? AUTOMOUNT_FAILED : AUTOMOUNT_DEAD);
320 }
321
322 static int open_dev_autofs(Manager *m) {
323         struct autofs_dev_ioctl param;
324
325         assert(m);
326
327         if (m->dev_autofs_fd >= 0)
328                 return m->dev_autofs_fd;
329
330         label_fix("/dev/autofs", false, false);
331
332         m->dev_autofs_fd = open("/dev/autofs", O_CLOEXEC|O_RDONLY);
333         if (m->dev_autofs_fd < 0) {
334                 log_error("Failed to open /dev/autofs: %s", strerror(errno));
335                 return -errno;
336         }
337
338         init_autofs_dev_ioctl(&param);
339         if (ioctl(m->dev_autofs_fd, AUTOFS_DEV_IOCTL_VERSION, &param) < 0) {
340                 close_nointr_nofail(m->dev_autofs_fd);
341                 m->dev_autofs_fd = -1;
342                 return -errno;
343         }
344
345         log_debug("Autofs kernel version %i.%i", param.ver_major, param.ver_minor);
346
347         return m->dev_autofs_fd;
348 }
349
350 static int open_ioctl_fd(int dev_autofs_fd, const char *where, dev_t devid) {
351         struct autofs_dev_ioctl *param;
352         size_t l;
353
354         assert(dev_autofs_fd >= 0);
355         assert(where);
356
357         l = sizeof(struct autofs_dev_ioctl) + strlen(where) + 1;
358         param = alloca(l);
359
360         init_autofs_dev_ioctl(param);
361         param->size = l;
362         param->ioctlfd = -1;
363         param->openmount.devid = devid;
364         strcpy(param->path, where);
365
366         if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_OPENMOUNT, param) < 0)
367                 return -errno;
368
369         if (param->ioctlfd < 0)
370                 return -EIO;
371
372         fd_cloexec(param->ioctlfd, true);
373         return param->ioctlfd;
374 }
375
376 static int autofs_protocol(int dev_autofs_fd, int ioctl_fd) {
377         uint32_t major, minor;
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
386         if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOVER, &param) < 0)
387                 return -errno;
388
389         major = param.protover.version;
390
391         init_autofs_dev_ioctl(&param);
392         param.ioctlfd = ioctl_fd;
393
394         if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOSUBVER, &param) < 0)
395                 return -errno;
396
397         minor = param.protosubver.sub_version;
398
399         log_debug("Autofs protocol version %i.%i", major, minor);
400         return 0;
401 }
402
403 static int autofs_set_timeout(int dev_autofs_fd, int ioctl_fd, time_t sec) {
404         struct autofs_dev_ioctl param;
405
406         assert(dev_autofs_fd >= 0);
407         assert(ioctl_fd >= 0);
408
409         init_autofs_dev_ioctl(&param);
410         param.ioctlfd = ioctl_fd;
411         param.timeout.timeout = sec;
412
413         if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_TIMEOUT, &param) < 0)
414                 return -errno;
415
416         return 0;
417 }
418
419 static int autofs_send_ready(int dev_autofs_fd, int ioctl_fd, uint32_t token, int status) {
420         struct autofs_dev_ioctl param;
421
422         assert(dev_autofs_fd >= 0);
423         assert(ioctl_fd >= 0);
424
425         init_autofs_dev_ioctl(&param);
426         param.ioctlfd = ioctl_fd;
427
428         if (status) {
429                 param.fail.token = token;
430                 param.fail.status = status;
431         } else
432                 param.ready.token = token;
433
434         if (ioctl(dev_autofs_fd, status ? AUTOFS_DEV_IOCTL_FAIL : AUTOFS_DEV_IOCTL_READY, &param) < 0)
435                 return -errno;
436
437         return 0;
438 }
439
440 int automount_send_ready(Automount *a, int status) {
441         int ioctl_fd, r;
442         unsigned token;
443
444         assert(a);
445         assert(status <= 0);
446
447         if (set_isempty(a->tokens))
448                 return 0;
449
450         ioctl_fd = open_ioctl_fd(UNIT(a)->manager->dev_autofs_fd, a->where, a->dev_id);
451         if (ioctl_fd < 0) {
452                 r = ioctl_fd;
453                 goto fail;
454         }
455
456         if (status)
457                 log_debug("Sending failure: %s", strerror(-status));
458         else
459                 log_debug("Sending success.");
460
461         r = 0;
462
463         /* Autofs thankfully does not hand out 0 as a token */
464         while ((token = PTR_TO_UINT(set_steal_first(a->tokens)))) {
465                 int k;
466
467                 /* Autofs fun fact II:
468                  *
469                  * if you pass a positive status code here, the kernel will
470                  * freeze! Yay! */
471
472                 k = autofs_send_ready(UNIT(a)->manager->dev_autofs_fd,
473                                       ioctl_fd,
474                                       token,
475                                       status);
476                 if (k < 0)
477                         r = k;
478         }
479
480 fail:
481         if (ioctl_fd >= 0)
482                 close_nointr_nofail(ioctl_fd);
483
484         return r;
485 }
486
487 static void automount_enter_waiting(Automount *a) {
488         int p[2] = { -1, -1 };
489         char name[32], options[128];
490         bool mounted = false;
491         int r, ioctl_fd = -1, dev_autofs_fd;
492         struct stat st;
493
494         assert(a);
495         assert(a->pipe_fd < 0);
496         assert(a->where);
497
498         if (a->tokens)
499                 set_clear(a->tokens);
500
501         dev_autofs_fd = open_dev_autofs(UNIT(a)->manager);
502         if (dev_autofs_fd < 0) {
503                 r = dev_autofs_fd;
504                 goto fail;
505         }
506
507         /* We knowingly ignore the results of this call */
508         mkdir_p_label(a->where, 0555);
509
510         if (dir_is_empty(a->where) <= 0)
511                 log_notice("%s: Directory %s to mount over is not empty, mounting anyway. (To see the over-mounted files, please manually mount the underlying file system to a secondary location.)", 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 };