1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
25 #include <linux/auto_dev-ioctl.h>
26 #include <linux/auto_fs4.h>
27 #include <sys/epoll.h>
28 #include <sys/mount.h>
33 #include "automount.h"
34 #include "bus-error.h"
36 #include "dbus-automount.h"
38 #include "formats-util.h"
43 #include "parse-util.h"
44 #include "path-util.h"
45 #include "process-util.h"
47 #include "string-util.h"
48 #include "unit-name.h"
51 static const UnitActiveState state_translation_table
[_AUTOMOUNT_STATE_MAX
] = {
52 [AUTOMOUNT_DEAD
] = UNIT_INACTIVE
,
53 [AUTOMOUNT_WAITING
] = UNIT_ACTIVE
,
54 [AUTOMOUNT_RUNNING
] = UNIT_ACTIVE
,
55 [AUTOMOUNT_FAILED
] = UNIT_FAILED
63 static inline void expire_data_free(struct expire_data
*data
) {
67 safe_close(data
->dev_autofs_fd
);
68 safe_close(data
->ioctl_fd
);
72 DEFINE_TRIVIAL_CLEANUP_FUNC(struct expire_data
*, expire_data_free
);
74 static int open_dev_autofs(Manager
*m
);
75 static int automount_dispatch_io(sd_event_source
*s
, int fd
, uint32_t events
, void *userdata
);
77 static void automount_init(Unit
*u
) {
78 Automount
*a
= AUTOMOUNT(u
);
81 assert(u
->load_state
== UNIT_STUB
);
84 a
->directory_mode
= 0755;
85 UNIT(a
)->ignore_on_isolate
= true;
88 static void repeat_unmount(const char *path
) {
92 /* If there are multiple mounts on a mount point, this
95 if (umount2(path
, MNT_DETACH
) >= 0)
99 log_error_errno(errno
, "Failed to unmount: %m");
105 static int automount_send_ready(Automount
*a
, Set
*tokens
, int status
);
107 static void unmount_autofs(Automount
*a
) {
113 automount_send_ready(a
, a
->tokens
, -EHOSTDOWN
);
114 automount_send_ready(a
, a
->expire_tokens
, -EHOSTDOWN
);
116 a
->pipe_event_source
= sd_event_source_unref(a
->pipe_event_source
);
117 a
->pipe_fd
= safe_close(a
->pipe_fd
);
119 /* If we reload/reexecute things we keep the mount point
122 (UNIT(a
)->manager
->exit_code
!= MANAGER_RELOAD
&&
123 UNIT(a
)->manager
->exit_code
!= MANAGER_REEXECUTE
))
124 repeat_unmount(a
->where
);
127 static void automount_done(Unit
*u
) {
128 Automount
*a
= AUTOMOUNT(u
);
134 a
->where
= mfree(a
->where
);
136 a
->tokens
= set_free(a
->tokens
);
137 a
->expire_tokens
= set_free(a
->expire_tokens
);
139 a
->expire_event_source
= sd_event_source_unref(a
->expire_event_source
);
142 static int automount_add_mount_links(Automount
*a
) {
143 _cleanup_free_
char *parent
= NULL
;
148 r
= path_get_parent(a
->where
, &parent
);
152 return unit_require_mounts_for(UNIT(a
), parent
);
155 static int automount_add_default_dependencies(Automount
*a
) {
160 if (UNIT(a
)->manager
->running_as
!= MANAGER_SYSTEM
)
163 r
= unit_add_two_dependencies_by_name(UNIT(a
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, NULL
, true);
170 static int automount_verify(Automount
*a
) {
171 _cleanup_free_
char *e
= NULL
;
176 if (UNIT(a
)->load_state
!= UNIT_LOADED
)
179 if (path_equal(a
->where
, "/")) {
180 log_unit_error(UNIT(a
), "Cannot have an automount unit for the root directory. Refusing.");
184 r
= unit_name_from_path(a
->where
, ".automount", &e
);
186 return log_unit_error(UNIT(a
), "Failed to generate unit name from path: %m");
188 if (!unit_has_name(UNIT(a
), e
)) {
189 log_unit_error(UNIT(a
), "Where= setting doesn't match unit name. Refusing.");
196 static int automount_load(Unit
*u
) {
197 Automount
*a
= AUTOMOUNT(u
);
201 assert(u
->load_state
== UNIT_STUB
);
203 /* Load a .automount file */
204 r
= unit_load_fragment_and_dropin_optional(u
);
208 if (u
->load_state
== UNIT_LOADED
) {
212 r
= unit_name_to_path(u
->id
, &a
->where
);
217 path_kill_slashes(a
->where
);
219 r
= unit_load_related_unit(u
, ".mount", &x
);
223 r
= unit_add_two_dependencies(u
, UNIT_BEFORE
, UNIT_TRIGGERS
, x
, true);
227 r
= automount_add_mount_links(a
);
231 if (UNIT(a
)->default_dependencies
) {
232 r
= automount_add_default_dependencies(a
);
238 return automount_verify(a
);
241 static void automount_set_state(Automount
*a
, AutomountState state
) {
242 AutomountState old_state
;
245 old_state
= a
->state
;
248 if (state
!= AUTOMOUNT_WAITING
&&
249 state
!= AUTOMOUNT_RUNNING
)
252 if (state
!= old_state
)
253 log_unit_debug(UNIT(a
), "Changed %s -> %s", automount_state_to_string(old_state
), automount_state_to_string(state
));
255 unit_notify(UNIT(a
), state_translation_table
[old_state
], state_translation_table
[state
], true);
258 static int automount_coldplug(Unit
*u
) {
259 Automount
*a
= AUTOMOUNT(u
);
263 assert(a
->state
== AUTOMOUNT_DEAD
);
265 if (a
->deserialized_state
!= a
->state
) {
267 r
= open_dev_autofs(u
->manager
);
271 if (a
->deserialized_state
== AUTOMOUNT_WAITING
||
272 a
->deserialized_state
== AUTOMOUNT_RUNNING
) {
273 assert(a
->pipe_fd
>= 0);
275 r
= sd_event_add_io(u
->manager
->event
, &a
->pipe_event_source
, a
->pipe_fd
, EPOLLIN
, automount_dispatch_io
, u
);
279 (void) sd_event_source_set_description(a
->pipe_event_source
, "automount-io");
282 automount_set_state(a
, a
->deserialized_state
);
288 static void automount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
289 char time_string
[FORMAT_TIMESPAN_MAX
];
290 Automount
*a
= AUTOMOUNT(u
);
295 "%sAutomount State: %s\n"
298 "%sDirectoryMode: %04o\n"
299 "%sTimeoutIdleUSec: %s\n",
300 prefix
, automount_state_to_string(a
->state
),
301 prefix
, automount_result_to_string(a
->result
),
303 prefix
, a
->directory_mode
,
304 prefix
, format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, a
->timeout_idle_usec
, USEC_PER_SEC
));
307 static void automount_enter_dead(Automount
*a
, AutomountResult f
) {
310 if (f
!= AUTOMOUNT_SUCCESS
)
313 automount_set_state(a
, a
->result
!= AUTOMOUNT_SUCCESS
? AUTOMOUNT_FAILED
: AUTOMOUNT_DEAD
);
316 static int open_dev_autofs(Manager
*m
) {
317 struct autofs_dev_ioctl param
;
321 if (m
->dev_autofs_fd
>= 0)
322 return m
->dev_autofs_fd
;
324 label_fix("/dev/autofs", false, false);
326 m
->dev_autofs_fd
= open("/dev/autofs", O_CLOEXEC
|O_RDONLY
);
327 if (m
->dev_autofs_fd
< 0)
328 return log_error_errno(errno
, "Failed to open /dev/autofs: %m");
330 init_autofs_dev_ioctl(¶m
);
331 if (ioctl(m
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_VERSION
, ¶m
) < 0) {
332 m
->dev_autofs_fd
= safe_close(m
->dev_autofs_fd
);
336 log_debug("Autofs kernel version %i.%i", param
.ver_major
, param
.ver_minor
);
338 return m
->dev_autofs_fd
;
341 static int open_ioctl_fd(int dev_autofs_fd
, const char *where
, dev_t devid
) {
342 struct autofs_dev_ioctl
*param
;
345 assert(dev_autofs_fd
>= 0);
348 l
= sizeof(struct autofs_dev_ioctl
) + strlen(where
) + 1;
351 init_autofs_dev_ioctl(param
);
354 param
->openmount
.devid
= devid
;
355 strcpy(param
->path
, where
);
357 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_OPENMOUNT
, param
) < 0)
360 if (param
->ioctlfd
< 0)
363 (void) fd_cloexec(param
->ioctlfd
, true);
364 return param
->ioctlfd
;
367 static int autofs_protocol(int dev_autofs_fd
, int ioctl_fd
) {
368 uint32_t major
, minor
;
369 struct autofs_dev_ioctl param
;
371 assert(dev_autofs_fd
>= 0);
372 assert(ioctl_fd
>= 0);
374 init_autofs_dev_ioctl(¶m
);
375 param
.ioctlfd
= ioctl_fd
;
377 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOVER
, ¶m
) < 0)
380 major
= param
.protover
.version
;
382 init_autofs_dev_ioctl(¶m
);
383 param
.ioctlfd
= ioctl_fd
;
385 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOSUBVER
, ¶m
) < 0)
388 minor
= param
.protosubver
.sub_version
;
390 log_debug("Autofs protocol version %i.%i", major
, minor
);
394 static int autofs_set_timeout(int dev_autofs_fd
, int ioctl_fd
, usec_t usec
) {
395 struct autofs_dev_ioctl param
;
397 assert(dev_autofs_fd
>= 0);
398 assert(ioctl_fd
>= 0);
400 init_autofs_dev_ioctl(¶m
);
401 param
.ioctlfd
= ioctl_fd
;
403 /* Convert to seconds, rounding up. */
404 param
.timeout
.timeout
= (usec
+ USEC_PER_SEC
- 1) / USEC_PER_SEC
;
406 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_TIMEOUT
, ¶m
) < 0)
412 static int autofs_send_ready(int dev_autofs_fd
, int ioctl_fd
, uint32_t token
, int status
) {
413 struct autofs_dev_ioctl param
;
415 assert(dev_autofs_fd
>= 0);
416 assert(ioctl_fd
>= 0);
418 init_autofs_dev_ioctl(¶m
);
419 param
.ioctlfd
= ioctl_fd
;
422 param
.fail
.token
= token
;
423 param
.fail
.status
= status
;
425 param
.ready
.token
= token
;
427 if (ioctl(dev_autofs_fd
, status
? AUTOFS_DEV_IOCTL_FAIL
: AUTOFS_DEV_IOCTL_READY
, ¶m
) < 0)
433 static int automount_send_ready(Automount
*a
, Set
*tokens
, int status
) {
434 _cleanup_close_
int ioctl_fd
= -1;
441 if (set_isempty(tokens
))
444 ioctl_fd
= open_ioctl_fd(UNIT(a
)->manager
->dev_autofs_fd
, a
->where
, a
->dev_id
);
449 log_unit_debug_errno(UNIT(a
), status
, "Sending failure: %m");
451 log_unit_debug(UNIT(a
), "Sending success.");
455 /* Autofs thankfully does not hand out 0 as a token */
456 while ((token
= PTR_TO_UINT(set_steal_first(tokens
)))) {
459 /* Autofs fun fact II:
461 * if you pass a positive status code here, the kernel will
464 k
= autofs_send_ready(UNIT(a
)->manager
->dev_autofs_fd
,
475 static int automount_start_expire(Automount
*a
);
477 int automount_update_mount(Automount
*a
, MountState old_state
, MountState state
) {
484 case MOUNT_REMOUNTING
:
485 automount_send_ready(a
, a
->tokens
, 0);
486 r
= automount_start_expire(a
);
488 log_unit_warning_errno(UNIT(a
), r
, "Failed to start expiration timer, ignoring: %m");
491 case MOUNT_UNMOUNTING
:
492 case MOUNT_MOUNTING_SIGTERM
:
493 case MOUNT_MOUNTING_SIGKILL
:
494 case MOUNT_REMOUNTING_SIGTERM
:
495 case MOUNT_REMOUNTING_SIGKILL
:
496 case MOUNT_UNMOUNTING_SIGTERM
:
497 case MOUNT_UNMOUNTING_SIGKILL
:
499 if (old_state
!= state
)
500 automount_send_ready(a
, a
->tokens
, -ENODEV
);
501 (void) sd_event_source_set_enabled(a
->expire_event_source
, SD_EVENT_OFF
);
509 automount_send_ready(a
, a
->expire_tokens
, 0);
512 case MOUNT_MOUNTING_DONE
:
513 case MOUNT_MOUNTING_SIGTERM
:
514 case MOUNT_MOUNTING_SIGKILL
:
515 case MOUNT_REMOUNTING_SIGTERM
:
516 case MOUNT_REMOUNTING_SIGKILL
:
517 case MOUNT_UNMOUNTING_SIGTERM
:
518 case MOUNT_UNMOUNTING_SIGKILL
:
520 if (old_state
!= state
)
521 automount_send_ready(a
, a
->expire_tokens
, -ENODEV
);
530 static void automount_enter_waiting(Automount
*a
) {
531 _cleanup_close_
int ioctl_fd
= -1;
532 int p
[2] = { -1, -1 };
533 char name
[sizeof("systemd-")-1 + DECIMAL_STR_MAX(pid_t
) + 1];
534 char options
[sizeof("fd=,pgrp=,minproto=5,maxproto=5,direct")-1
535 + DECIMAL_STR_MAX(int) + DECIMAL_STR_MAX(gid_t
) + 1];
536 bool mounted
= false;
537 int r
, dev_autofs_fd
;
541 assert(a
->pipe_fd
< 0);
544 set_clear(a
->tokens
);
546 r
= unit_fail_if_symlink(UNIT(a
), a
->where
);
550 (void) mkdir_p_label(a
->where
, 0555);
552 unit_warn_if_dir_nonempty(UNIT(a
), a
->where
);
554 dev_autofs_fd
= open_dev_autofs(UNIT(a
)->manager
);
555 if (dev_autofs_fd
< 0) {
560 if (pipe2(p
, O_NONBLOCK
|O_CLOEXEC
) < 0) {
565 xsprintf(options
, "fd=%i,pgrp="PID_FMT
",minproto=5,maxproto=5,direct", p
[1], getpgrp());
566 xsprintf(name
, "systemd-"PID_FMT
, getpid());
567 if (mount(name
, a
->where
, "autofs", 0, options
) < 0) {
574 p
[1] = safe_close(p
[1]);
576 if (stat(a
->where
, &st
) < 0) {
581 ioctl_fd
= open_ioctl_fd(dev_autofs_fd
, a
->where
, st
.st_dev
);
587 r
= autofs_protocol(dev_autofs_fd
, ioctl_fd
);
591 r
= autofs_set_timeout(dev_autofs_fd
, ioctl_fd
, a
->timeout_idle_usec
);
597 * Unless we close the ioctl fd here, for some weird reason
598 * the direct mount will not receive events from the
601 r
= sd_event_add_io(UNIT(a
)->manager
->event
, &a
->pipe_event_source
, p
[0], EPOLLIN
, automount_dispatch_io
, a
);
605 (void) sd_event_source_set_description(a
->pipe_event_source
, "automount-io");
608 a
->dev_id
= st
.st_dev
;
610 automount_set_state(a
, AUTOMOUNT_WAITING
);
618 repeat_unmount(a
->where
);
620 log_unit_error_errno(UNIT(a
), r
, "Failed to initialize automounter: %m");
621 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
624 static void *expire_thread(void *p
) {
625 struct autofs_dev_ioctl param
;
626 _cleanup_(expire_data_freep
) struct expire_data
*data
= (struct expire_data
*)p
;
629 assert(data
->dev_autofs_fd
>= 0);
630 assert(data
->ioctl_fd
>= 0);
632 init_autofs_dev_ioctl(¶m
);
633 param
.ioctlfd
= data
->ioctl_fd
;
636 r
= ioctl(data
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_EXPIRE
, ¶m
);
640 log_warning_errno(errno
, "Failed to expire automount, ignoring: %m");
645 static int automount_dispatch_expire(sd_event_source
*source
, usec_t usec
, void *userdata
) {
646 Automount
*a
= AUTOMOUNT(userdata
);
647 _cleanup_(expire_data_freep
) struct expire_data
*data
= NULL
;
651 assert(source
== a
->expire_event_source
);
653 data
= new0(struct expire_data
, 1);
659 data
->dev_autofs_fd
= fcntl(UNIT(a
)->manager
->dev_autofs_fd
, F_DUPFD_CLOEXEC
, 3);
660 if (data
->dev_autofs_fd
< 0)
661 return log_unit_error_errno(UNIT(a
), errno
, "Failed to duplicate autofs fd: %m");
663 data
->ioctl_fd
= open_ioctl_fd(UNIT(a
)->manager
->dev_autofs_fd
, a
->where
, a
->dev_id
);
664 if (data
->ioctl_fd
< 0)
665 return log_unit_error_errno(UNIT(a
), data
->ioctl_fd
, "Couldn't open autofs ioctl fd: %m");
667 r
= asynchronous_job(expire_thread
, data
);
669 return log_unit_error_errno(UNIT(a
), r
, "Failed to start expire job: %m");
673 return automount_start_expire(a
);
676 static int automount_start_expire(Automount
*a
) {
682 if (a
->timeout_idle_usec
== 0)
685 timeout
= now(CLOCK_MONOTONIC
) + MAX(a
->timeout_idle_usec
/3, USEC_PER_SEC
);
687 if (a
->expire_event_source
) {
688 r
= sd_event_source_set_time(a
->expire_event_source
, timeout
);
692 return sd_event_source_set_enabled(a
->expire_event_source
, SD_EVENT_ONESHOT
);
695 r
= sd_event_add_time(
696 UNIT(a
)->manager
->event
,
697 &a
->expire_event_source
,
698 CLOCK_MONOTONIC
, timeout
, 0,
699 automount_dispatch_expire
, a
);
703 (void) sd_event_source_set_description(a
->expire_event_source
, "automount-expire");
708 static void automount_enter_runnning(Automount
*a
) {
709 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
715 /* We don't take mount requests anymore if we are supposed to
716 * shut down anyway */
717 if (unit_stop_pending(UNIT(a
))) {
718 log_unit_debug(UNIT(a
), "Suppressing automount request since unit stop is scheduled.");
719 automount_send_ready(a
, a
->tokens
, -EHOSTDOWN
);
720 automount_send_ready(a
, a
->expire_tokens
, -EHOSTDOWN
);
724 mkdir_p_label(a
->where
, a
->directory_mode
);
726 /* Before we do anything, let's see if somebody is playing games with us? */
727 if (lstat(a
->where
, &st
) < 0) {
728 log_unit_warning_errno(UNIT(a
), errno
, "Failed to stat automount point: %m");
732 if (!S_ISDIR(st
.st_mode
) || st
.st_dev
!= a
->dev_id
)
733 log_unit_info(UNIT(a
), "Automount point already active?");
735 r
= manager_add_job(UNIT(a
)->manager
, JOB_START
, UNIT_TRIGGER(UNIT(a
)),
736 JOB_REPLACE
, true, &error
, NULL
);
738 log_unit_warning(UNIT(a
), "Failed to queue mount startup job: %s", bus_error_message(&error
, r
));
743 automount_set_state(a
, AUTOMOUNT_RUNNING
);
747 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
750 static int automount_start(Unit
*u
) {
751 Automount
*a
= AUTOMOUNT(u
);
754 assert(a
->state
== AUTOMOUNT_DEAD
|| a
->state
== AUTOMOUNT_FAILED
);
756 if (path_is_mount_point(a
->where
, 0) > 0) {
757 log_unit_error(u
, "Path %s is already a mount point, refusing start.", a
->where
);
761 if (UNIT_TRIGGER(u
)->load_state
!= UNIT_LOADED
)
764 a
->result
= AUTOMOUNT_SUCCESS
;
765 automount_enter_waiting(a
);
769 static int automount_stop(Unit
*u
) {
770 Automount
*a
= AUTOMOUNT(u
);
773 assert(a
->state
== AUTOMOUNT_WAITING
|| a
->state
== AUTOMOUNT_RUNNING
);
775 automount_enter_dead(a
, AUTOMOUNT_SUCCESS
);
779 static int automount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
780 Automount
*a
= AUTOMOUNT(u
);
789 unit_serialize_item(u
, f
, "state", automount_state_to_string(a
->state
));
790 unit_serialize_item(u
, f
, "result", automount_result_to_string(a
->result
));
791 unit_serialize_item_format(u
, f
, "dev-id", "%u", (unsigned) a
->dev_id
);
793 SET_FOREACH(p
, a
->tokens
, i
)
794 unit_serialize_item_format(u
, f
, "token", "%u", PTR_TO_UINT(p
));
795 SET_FOREACH(p
, a
->expire_tokens
, i
)
796 unit_serialize_item_format(u
, f
, "expire-token", "%u", PTR_TO_UINT(p
));
798 r
= unit_serialize_item_fd(u
, f
, fds
, "pipe-fd", a
->pipe_fd
);
805 static int automount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
806 Automount
*a
= AUTOMOUNT(u
);
812 if (streq(key
, "state")) {
813 AutomountState state
;
815 state
= automount_state_from_string(value
);
817 log_unit_debug(u
, "Failed to parse state value: %s", value
);
819 a
->deserialized_state
= state
;
820 } else if (streq(key
, "result")) {
823 f
= automount_result_from_string(value
);
825 log_unit_debug(u
, "Failed to parse result value: %s", value
);
826 else if (f
!= AUTOMOUNT_SUCCESS
)
829 } else if (streq(key
, "dev-id")) {
832 if (safe_atou(value
, &d
) < 0)
833 log_unit_debug(u
, "Failed to parse dev-id value: %s", value
);
835 a
->dev_id
= (unsigned) d
;
836 } else if (streq(key
, "token")) {
839 if (safe_atou(value
, &token
) < 0)
840 log_unit_debug(u
, "Failed to parse token value: %s", value
);
842 r
= set_ensure_allocated(&a
->tokens
, NULL
);
848 r
= set_put(a
->tokens
, UINT_TO_PTR(token
));
850 log_unit_error_errno(u
, r
, "Failed to add token to set: %m");
852 } else if (streq(key
, "expire-token")) {
855 if (safe_atou(value
, &token
) < 0)
856 log_unit_debug(u
, "Failed to parse token value: %s", value
);
858 r
= set_ensure_allocated(&a
->expire_tokens
, NULL
);
864 r
= set_put(a
->expire_tokens
, UINT_TO_PTR(token
));
866 log_unit_error_errno(u
, r
, "Failed to add expire token to set: %m");
868 } else if (streq(key
, "pipe-fd")) {
871 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
872 log_unit_debug(u
, "Failed to parse pipe-fd value: %s", value
);
874 safe_close(a
->pipe_fd
);
875 a
->pipe_fd
= fdset_remove(fds
, fd
);
878 log_unit_debug(u
, "Unknown serialization key: %s", key
);
883 static UnitActiveState
automount_active_state(Unit
*u
) {
886 return state_translation_table
[AUTOMOUNT(u
)->state
];
889 static const char *automount_sub_state_to_string(Unit
*u
) {
892 return automount_state_to_string(AUTOMOUNT(u
)->state
);
895 static bool automount_check_gc(Unit
*u
) {
898 if (!UNIT_TRIGGER(u
))
901 return UNIT_VTABLE(UNIT_TRIGGER(u
))->check_gc(UNIT_TRIGGER(u
));
904 static int automount_dispatch_io(sd_event_source
*s
, int fd
, uint32_t events
, void *userdata
) {
905 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
906 union autofs_v5_packet_union packet
;
907 Automount
*a
= AUTOMOUNT(userdata
);
912 assert(fd
== a
->pipe_fd
);
914 if (events
!= EPOLLIN
) {
915 log_unit_error(UNIT(a
), "Got invalid poll event %"PRIu32
" on pipe (fd=%d)", events
, fd
);
919 r
= loop_read_exact(a
->pipe_fd
, &packet
, sizeof(packet
), true);
921 log_unit_error_errno(UNIT(a
), r
, "Invalid read from pipe: %m");
925 switch (packet
.hdr
.type
) {
927 case autofs_ptype_missing_direct
:
929 if (packet
.v5_packet
.pid
> 0) {
930 _cleanup_free_
char *p
= NULL
;
932 get_process_comm(packet
.v5_packet
.pid
, &p
);
933 log_unit_info(UNIT(a
), "Got automount request for %s, triggered by %"PRIu32
" (%s)", a
->where
, packet
.v5_packet
.pid
, strna(p
));
935 log_unit_debug(UNIT(a
), "Got direct mount request on %s", a
->where
);
937 r
= set_ensure_allocated(&a
->tokens
, NULL
);
939 log_unit_error(UNIT(a
), "Failed to allocate token set.");
943 r
= set_put(a
->tokens
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
));
945 log_unit_error_errno(UNIT(a
), r
, "Failed to remember token: %m");
949 automount_enter_runnning(a
);
952 case autofs_ptype_expire_direct
:
953 log_unit_debug(UNIT(a
), "Got direct umount request on %s", a
->where
);
955 (void) sd_event_source_set_enabled(a
->expire_event_source
, SD_EVENT_OFF
);
957 r
= set_ensure_allocated(&a
->expire_tokens
, NULL
);
959 log_unit_error(UNIT(a
), "Failed to allocate token set.");
963 r
= set_put(a
->expire_tokens
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
));
965 log_unit_error_errno(UNIT(a
), r
, "Failed to remember token: %m");
969 /* Before we do anything, let's see if somebody is playing games with us? */
970 if (lstat(a
->where
, &st
) < 0) {
971 log_unit_warning_errno(UNIT(a
), errno
, "Failed to stat automount point: %m");
975 if (!S_ISDIR(st
.st_mode
) || st
.st_dev
== a
->dev_id
) {
976 log_unit_info(UNIT(a
), "Automount point already unmounted?");
977 automount_send_ready(a
, a
->expire_tokens
, 0);
981 r
= manager_add_job(UNIT(a
)->manager
, JOB_STOP
, UNIT_TRIGGER(UNIT(a
)), JOB_REPLACE
, true, &error
, NULL
);
983 log_unit_warning(UNIT(a
), "Failed to queue umount startup job: %s", bus_error_message(&error
, r
));
989 log_unit_error(UNIT(a
), "Received unknown automount request %i", packet
.hdr
.type
);
996 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
1000 static void automount_shutdown(Manager
*m
) {
1003 m
->dev_autofs_fd
= safe_close(m
->dev_autofs_fd
);
1006 static void automount_reset_failed(Unit
*u
) {
1007 Automount
*a
= AUTOMOUNT(u
);
1011 if (a
->state
== AUTOMOUNT_FAILED
)
1012 automount_set_state(a
, AUTOMOUNT_DEAD
);
1014 a
->result
= AUTOMOUNT_SUCCESS
;
1017 static bool automount_supported(void) {
1018 static int supported
= -1;
1021 supported
= access("/dev/autofs", F_OK
) >= 0;
1026 static const char* const automount_result_table
[_AUTOMOUNT_RESULT_MAX
] = {
1027 [AUTOMOUNT_SUCCESS
] = "success",
1028 [AUTOMOUNT_FAILURE_RESOURCES
] = "resources"
1031 DEFINE_STRING_TABLE_LOOKUP(automount_result
, AutomountResult
);
1033 const UnitVTable automount_vtable
= {
1034 .object_size
= sizeof(Automount
),
1042 .no_instances
= true,
1044 .init
= automount_init
,
1045 .load
= automount_load
,
1046 .done
= automount_done
,
1048 .coldplug
= automount_coldplug
,
1050 .dump
= automount_dump
,
1052 .start
= automount_start
,
1053 .stop
= automount_stop
,
1055 .serialize
= automount_serialize
,
1056 .deserialize_item
= automount_deserialize_item
,
1058 .active_state
= automount_active_state
,
1059 .sub_state_to_string
= automount_sub_state_to_string
,
1061 .check_gc
= automount_check_gc
,
1063 .reset_failed
= automount_reset_failed
,
1065 .bus_vtable
= bus_automount_vtable
,
1067 .shutdown
= automount_shutdown
,
1068 .supported
= automount_supported
,
1070 .status_message_formats
= {
1071 .finished_start_job
= {
1072 [JOB_DONE
] = "Set up automount %s.",
1073 [JOB_FAILED
] = "Failed to set up automount %s.",
1075 .finished_stop_job
= {
1076 [JOB_DONE
] = "Unset automount %s.",
1077 [JOB_FAILED
] = "Failed to unset automount %s.",