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