1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include <linux/auto_dev-ioctl.h>
11 #include "alloc-util.h"
12 #include "automount.h"
13 #include "bus-error.h"
14 #include "dbus-automount.h"
15 #include "dbus-unit.h"
16 #include "errno-util.h"
18 #include "format-util.h"
19 #include "fstab-util.h"
21 #include "label-util.h"
23 #include "mkdir-label.h"
25 #include "mount-util.h"
26 #include "mountpoint-util.h"
27 #include "parse-util.h"
28 #include "path-util.h"
29 #include "process-util.h"
30 #include "serialize.h"
33 #include "stdio-util.h"
34 #include "string-table.h"
35 #include "string-util.h"
37 #include "unit-name.h"
39 static const UnitActiveState state_translation_table
[_AUTOMOUNT_STATE_MAX
] = {
40 [AUTOMOUNT_DEAD
] = UNIT_INACTIVE
,
41 [AUTOMOUNT_WAITING
] = UNIT_ACTIVE
,
42 [AUTOMOUNT_RUNNING
] = UNIT_ACTIVE
,
43 [AUTOMOUNT_FAILED
] = UNIT_FAILED
,
46 static int open_dev_autofs(Manager
*m
);
47 static int automount_dispatch_io(sd_event_source
*s
, int fd
, uint32_t events
, void *userdata
);
48 static int automount_start_expire(Automount
*a
);
49 static void automount_stop_expire(Automount
*a
);
50 static int automount_send_ready(Automount
*a
, Set
*tokens
, int status
);
52 static void automount_init(Unit
*u
) {
53 Automount
*a
= ASSERT_PTR(AUTOMOUNT(u
));
55 assert(u
->load_state
== UNIT_STUB
);
58 a
->directory_mode
= 0755;
59 UNIT(a
)->ignore_on_isolate
= true;
62 static void unmount_autofs(Automount
*a
) {
70 a
->pipe_event_source
= sd_event_source_disable_unref(a
->pipe_event_source
);
71 a
->pipe_fd
= safe_close(a
->pipe_fd
);
73 /* If we reload/reexecute things we keep the mount point around */
74 if (!IN_SET(UNIT(a
)->manager
->objective
, MANAGER_RELOAD
, MANAGER_REEXECUTE
)) {
76 automount_send_ready(a
, a
->tokens
, -EHOSTDOWN
);
77 automount_send_ready(a
, a
->expire_tokens
, -EHOSTDOWN
);
80 r
= repeat_unmount(a
->where
, MNT_DETACH
|UMOUNT_NOFOLLOW
);
82 log_unit_error_errno(UNIT(a
), r
, "Failed to unmount: %m");
87 static void automount_done(Unit
*u
) {
88 Automount
*a
= ASSERT_PTR(AUTOMOUNT(u
));
92 a
->where
= mfree(a
->where
);
93 a
->extra_options
= mfree(a
->extra_options
);
95 a
->tokens
= set_free(a
->tokens
);
96 a
->expire_tokens
= set_free(a
->expire_tokens
);
98 a
->expire_event_source
= sd_event_source_disable_unref(a
->expire_event_source
);
101 static int automount_add_trigger_dependencies(Automount
*a
) {
107 r
= unit_load_related_unit(UNIT(a
), ".mount", &x
);
111 return unit_add_two_dependencies(UNIT(a
), UNIT_BEFORE
, UNIT_TRIGGERS
, x
, true, UNIT_DEPENDENCY_IMPLICIT
);
114 static int automount_add_mount_dependencies(Automount
*a
) {
115 _cleanup_free_
char *parent
= NULL
;
120 r
= path_extract_directory(a
->where
, &parent
);
124 return unit_add_mounts_for(UNIT(a
), parent
, UNIT_DEPENDENCY_IMPLICIT
, UNIT_MOUNT_REQUIRES
);
127 static int automount_add_default_dependencies(Automount
*a
) {
132 if (!UNIT(a
)->default_dependencies
)
135 if (!MANAGER_IS_SYSTEM(UNIT(a
)->manager
))
138 r
= unit_add_dependency_by_name(UNIT(a
), UNIT_BEFORE
, SPECIAL_LOCAL_FS_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
142 r
= unit_add_dependency_by_name(UNIT(a
), UNIT_AFTER
, SPECIAL_LOCAL_FS_PRE_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
146 r
= unit_add_two_dependencies_by_name(UNIT(a
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
153 static int automount_verify(Automount
*a
) {
154 static const char *const reserved_options
[] = {
163 _cleanup_free_
char *e
= NULL
;
167 assert(UNIT(a
)->load_state
== UNIT_LOADED
);
169 if (path_equal(a
->where
, "/"))
170 return log_unit_error_errno(UNIT(a
), SYNTHETIC_ERRNO(ENOEXEC
), "Cannot have an automount unit for the root directory. Refusing.");
172 r
= unit_name_from_path(a
->where
, ".automount", &e
);
174 return log_unit_error_errno(UNIT(a
), r
, "Failed to generate unit name from path: %m");
176 if (!unit_has_name(UNIT(a
), e
))
177 return log_unit_error_errno(UNIT(a
), SYNTHETIC_ERRNO(ENOEXEC
), "Where= setting doesn't match unit name. Refusing.");
179 FOREACH_ELEMENT(reserved_option
, reserved_options
)
180 if (fstab_test_option(a
->extra_options
, *reserved_option
))
181 return log_unit_error_errno(
183 SYNTHETIC_ERRNO(ENOEXEC
),
184 "ExtraOptions= setting may not contain reserved option %s.",
190 static int automount_set_where(Automount
*a
) {
198 r
= unit_name_to_path(UNIT(a
)->id
, &a
->where
);
202 path_simplify(a
->where
);
206 static int automount_add_extras(Automount
*a
) {
209 r
= automount_set_where(a
);
213 r
= automount_add_trigger_dependencies(a
);
217 r
= automount_add_mount_dependencies(a
);
221 return automount_add_default_dependencies(a
);
224 static int automount_load(Unit
*u
) {
225 Automount
*a
= ASSERT_PTR(AUTOMOUNT(u
));
228 assert(u
->load_state
== UNIT_STUB
);
230 /* Load a .automount file */
231 r
= unit_load_fragment_and_dropin(u
, true);
235 if (u
->load_state
!= UNIT_LOADED
)
238 r
= automount_add_extras(a
);
242 return automount_verify(a
);
245 static void automount_set_state(Automount
*a
, AutomountState state
) {
246 AutomountState old_state
;
250 if (a
->state
!= state
)
251 bus_unit_send_pending_change_signal(UNIT(a
), false);
253 old_state
= a
->state
;
256 if (state
!= AUTOMOUNT_RUNNING
)
257 automount_stop_expire(a
);
259 if (!IN_SET(state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
))
262 if (state
!= old_state
)
263 log_unit_debug(UNIT(a
), "Changed %s -> %s", automount_state_to_string(old_state
), automount_state_to_string(state
));
265 unit_notify(UNIT(a
), state_translation_table
[old_state
], state_translation_table
[state
], /* reload_success = */ true);
268 static int automount_coldplug(Unit
*u
) {
269 Automount
*a
= ASSERT_PTR(AUTOMOUNT(u
));
272 assert(a
->state
== AUTOMOUNT_DEAD
);
274 if (a
->deserialized_state
== a
->state
)
277 if (IN_SET(a
->deserialized_state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
)) {
279 r
= automount_set_where(a
);
283 r
= open_dev_autofs(u
->manager
);
287 assert(a
->pipe_fd
>= 0);
289 r
= sd_event_add_io(u
->manager
->event
, &a
->pipe_event_source
, a
->pipe_fd
, EPOLLIN
, automount_dispatch_io
, u
);
293 (void) sd_event_source_set_description(a
->pipe_event_source
, "automount-io");
294 if (a
->deserialized_state
== AUTOMOUNT_RUNNING
) {
295 r
= automount_start_expire(a
);
297 log_unit_warning_errno(UNIT(a
), r
, "Failed to start expiration timer, ignoring: %m");
300 automount_set_state(a
, a
->deserialized_state
);
306 static void automount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
307 Automount
*a
= ASSERT_PTR(AUTOMOUNT(u
));
310 "%sAutomount State: %s\n"
313 "%sExtraOptions: %s\n"
314 "%sDirectoryMode: %04o\n"
315 "%sTimeoutIdleUSec: %s\n",
316 prefix
, automount_state_to_string(a
->state
),
317 prefix
, automount_result_to_string(a
->result
),
319 prefix
, a
->extra_options
,
320 prefix
, a
->directory_mode
,
321 prefix
, FORMAT_TIMESPAN(a
->timeout_idle_usec
, USEC_PER_SEC
));
324 static void automount_enter_dead(Automount
*a
, AutomountResult f
) {
327 if (a
->result
== AUTOMOUNT_SUCCESS
)
330 unit_log_result(UNIT(a
), a
->result
== AUTOMOUNT_SUCCESS
, automount_result_to_string(a
->result
));
331 automount_set_state(a
, a
->result
!= AUTOMOUNT_SUCCESS
? AUTOMOUNT_FAILED
: AUTOMOUNT_DEAD
);
334 static int open_dev_autofs(Manager
*m
) {
335 struct autofs_dev_ioctl param
;
340 if (m
->dev_autofs_fd
>= 0)
341 return m
->dev_autofs_fd
;
343 (void) label_fix("/dev/autofs", 0);
345 m
->dev_autofs_fd
= open("/dev/autofs", O_CLOEXEC
|O_RDONLY
);
346 if (m
->dev_autofs_fd
< 0)
347 return log_error_errno(errno
, "Failed to open %s: %m", "/dev/autofs");
349 init_autofs_dev_ioctl(¶m
);
350 r
= RET_NERRNO(ioctl(m
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_VERSION
, ¶m
));
352 m
->dev_autofs_fd
= safe_close(m
->dev_autofs_fd
);
353 return log_error_errno(r
, "Failed to issue AUTOFS_DEV_IOCTL_VERSION ioctl: %m");
356 log_debug("Autofs kernel version %u.%u", param
.ver_major
, param
.ver_minor
);
358 return m
->dev_autofs_fd
;
361 static int open_ioctl_fd(int dev_autofs_fd
, const char *where
, dev_t devid
) {
362 struct autofs_dev_ioctl
*param
;
365 assert(dev_autofs_fd
>= 0);
368 l
= sizeof(struct autofs_dev_ioctl
) + strlen(where
) + 1;
369 param
= alloca_safe(l
);
371 init_autofs_dev_ioctl(param
);
373 param
->ioctlfd
= -EBADF
;
374 param
->openmount
.devid
= devid
;
375 strcpy(param
->path
, where
);
377 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_OPENMOUNT
, param
) < 0)
380 if (param
->ioctlfd
< 0)
383 (void) fd_cloexec(param
->ioctlfd
, true);
384 return param
->ioctlfd
;
387 static int autofs_protocol(int dev_autofs_fd
, int ioctl_fd
) {
388 uint32_t major
, minor
;
389 struct autofs_dev_ioctl param
;
391 assert(dev_autofs_fd
>= 0);
392 assert(ioctl_fd
>= 0);
394 init_autofs_dev_ioctl(¶m
);
395 param
.ioctlfd
= ioctl_fd
;
397 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOVER
, ¶m
) < 0)
400 major
= param
.protover
.version
;
402 init_autofs_dev_ioctl(¶m
);
403 param
.ioctlfd
= ioctl_fd
;
405 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOSUBVER
, ¶m
) < 0)
408 minor
= param
.protosubver
.sub_version
;
410 log_debug("Autofs protocol version %u.%u", major
, minor
);
414 static int autofs_set_timeout(int dev_autofs_fd
, int ioctl_fd
, usec_t usec
) {
415 struct autofs_dev_ioctl param
;
417 assert(dev_autofs_fd
>= 0);
418 assert(ioctl_fd
>= 0);
420 init_autofs_dev_ioctl(¶m
);
421 param
.ioctlfd
= ioctl_fd
;
423 if (usec
== USEC_INFINITY
)
424 param
.timeout
.timeout
= 0;
426 /* Convert to seconds, rounding up. */
427 param
.timeout
.timeout
= DIV_ROUND_UP(usec
, USEC_PER_SEC
);
429 return RET_NERRNO(ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_TIMEOUT
, ¶m
));
432 static int autofs_send_ready(int dev_autofs_fd
, int ioctl_fd
, uint32_t token
, int status
) {
433 struct autofs_dev_ioctl param
;
435 assert(dev_autofs_fd
>= 0);
436 assert(ioctl_fd
>= 0);
438 init_autofs_dev_ioctl(¶m
);
439 param
.ioctlfd
= ioctl_fd
;
442 param
.fail
.token
= token
;
443 param
.fail
.status
= status
;
445 param
.ready
.token
= token
;
447 return RET_NERRNO(ioctl(dev_autofs_fd
, status
? AUTOFS_DEV_IOCTL_FAIL
: AUTOFS_DEV_IOCTL_READY
, ¶m
));
450 static int automount_send_ready(Automount
*a
, Set
*tokens
, int status
) {
451 _cleanup_close_
int ioctl_fd
= -EBADF
;
458 if (set_isempty(tokens
))
461 ioctl_fd
= open_ioctl_fd(UNIT(a
)->manager
->dev_autofs_fd
, a
->where
, a
->dev_id
);
466 log_unit_debug_errno(UNIT(a
), status
, "Sending failure: %m");
468 log_unit_debug(UNIT(a
), "Sending success.");
472 /* Autofs thankfully does not hand out 0 as a token */
473 while ((token
= PTR_TO_UINT(set_steal_first(tokens
))))
476 * if you pass a positive status code here, kernels prior to 4.12 will freeze! Yay! */
477 RET_GATHER(r
, autofs_send_ready(UNIT(a
)->manager
->dev_autofs_fd
,
485 static void automount_trigger_notify(Unit
*u
, Unit
*other
) {
486 Automount
*a
= ASSERT_PTR(AUTOMOUNT(u
));
491 /* Filter out invocations with bogus state */
492 assert(UNIT_IS_LOAD_COMPLETE(other
->load_state
));
493 assert(other
->type
== UNIT_MOUNT
);
495 /* Don't propagate state changes from the mount if we are already down */
496 if (!IN_SET(a
->state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
))
499 /* Propagate start limit hit state */
500 if (other
->start_limit_hit
) {
501 automount_enter_dead(a
, AUTOMOUNT_FAILURE_MOUNT_START_LIMIT_HIT
);
505 /* Don't propagate anything if there's still a job queued */
509 /* The mount is successfully established */
510 if (IN_SET(MOUNT(other
)->state
, MOUNT_MOUNTED
, MOUNT_REMOUNTING
)) {
511 (void) automount_send_ready(a
, a
->tokens
, 0);
513 r
= automount_start_expire(a
);
515 log_unit_warning_errno(UNIT(a
), r
, "Failed to start expiration timer, ignoring: %m");
517 automount_set_state(a
, AUTOMOUNT_RUNNING
);
520 if (IN_SET(MOUNT(other
)->state
,
521 MOUNT_MOUNTING
, MOUNT_MOUNTING_DONE
,
522 MOUNT_MOUNTED
, MOUNT_REMOUNTING
,
523 MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
,
524 MOUNT_UNMOUNTING_SIGTERM
, MOUNT_UNMOUNTING_SIGKILL
,
526 (void) automount_send_ready(a
, a
->expire_tokens
, -ENODEV
);
528 if (MOUNT(other
)->state
== MOUNT_DEAD
)
529 (void) automount_send_ready(a
, a
->expire_tokens
, 0);
531 /* The mount is in some unhappy state now, let's unfreeze any waiting clients */
532 if (IN_SET(MOUNT(other
)->state
,
533 MOUNT_DEAD
, MOUNT_UNMOUNTING
,
534 MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
,
535 MOUNT_UNMOUNTING_SIGTERM
, MOUNT_UNMOUNTING_SIGKILL
,
538 (void) automount_send_ready(a
, a
->tokens
, -ENODEV
);
540 automount_set_state(a
, AUTOMOUNT_WAITING
);
544 static void automount_enter_waiting(Automount
*a
) {
545 _cleanup_close_pair_
int pipe_fd
[2] = EBADF_PAIR
;
546 _cleanup_close_
int ioctl_fd
= -EBADF
;
547 char name
[STRLEN("systemd-") + DECIMAL_STR_MAX(pid_t
) + 1];
548 _cleanup_free_
char *options
= NULL
;
549 bool mounted
= false;
550 int r
, dev_autofs_fd
;
554 assert(a
->pipe_fd
< 0);
557 set_clear(a
->tokens
);
559 r
= unit_fail_if_noncanonical_mount_path(UNIT(a
), a
->where
);
563 (void) mkdir_p_label(a
->where
, a
->directory_mode
);
565 unit_warn_if_dir_nonempty(UNIT(a
), a
->where
);
567 dev_autofs_fd
= open_dev_autofs(UNIT(a
)->manager
);
568 if (dev_autofs_fd
< 0)
571 if (pipe2(pipe_fd
, O_CLOEXEC
) < 0) {
572 log_unit_warning_errno(UNIT(a
), errno
, "Failed to allocate autofs pipe: %m");
575 r
= fd_nonblock(pipe_fd
[0], true);
577 log_unit_warning_errno(UNIT(a
), r
, "Failed to make read side of pipe non-blocking: %m");
583 "fd=%i,pgrp="PID_FMT
",minproto=5,maxproto=5,direct%s%s",
586 isempty(a
->extra_options
) ? "" : ",",
587 strempty(a
->extra_options
)) < 0) {
592 xsprintf(name
, "systemd-"PID_FMT
, getpid_cached());
593 r
= mount_nofollow_verbose(LOG_WARNING
, name
, a
->where
, "autofs", 0, options
);
599 pipe_fd
[1] = safe_close(pipe_fd
[1]);
601 if (stat(a
->where
, &st
) < 0) {
602 log_unit_warning_errno(UNIT(a
), errno
, "Failed to stat new automount point '%s': %m", a
->where
);
606 ioctl_fd
= open_ioctl_fd(dev_autofs_fd
, a
->where
, st
.st_dev
);
608 log_unit_warning_errno(UNIT(a
), ioctl_fd
, "Failed to open automount ioctl fd for '%s': %m", a
->where
);
612 r
= autofs_protocol(dev_autofs_fd
, ioctl_fd
);
614 log_unit_warning_errno(UNIT(a
), r
, "Failed to validate autofs protocol for '%s': %m", a
->where
);
618 r
= autofs_set_timeout(dev_autofs_fd
, ioctl_fd
, a
->timeout_idle_usec
);
620 log_unit_warning_errno(UNIT(a
), r
, "Failed to set autofs timeout for '%s': %m", a
->where
);
624 r
= sd_event_add_io(UNIT(a
)->manager
->event
, &a
->pipe_event_source
, pipe_fd
[0], EPOLLIN
, automount_dispatch_io
, a
);
626 log_unit_warning_errno(UNIT(a
), r
, "Failed to allocate IO event source for autofs mount '%s': %m", a
->where
);
630 (void) sd_event_source_set_description(a
->pipe_event_source
, "automount-io");
632 a
->pipe_fd
= TAKE_FD(pipe_fd
[0]);
633 a
->dev_id
= st
.st_dev
;
635 automount_set_state(a
, AUTOMOUNT_WAITING
);
640 r
= repeat_unmount(a
->where
, MNT_DETACH
|UMOUNT_NOFOLLOW
);
642 log_unit_warning_errno(UNIT(a
), r
, "Failed to unmount, ignoring: %m");
645 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
648 static int asynchronous_expire(int dev_autofs_fd
, int ioctl_fd
) {
651 assert(dev_autofs_fd
>= 0);
652 assert(ioctl_fd
>= 0);
654 /* Issue AUTOFS_DEV_IOCTL_EXPIRE in subprocess, asynchronously. Note that we don't keep track of the
655 * child's PID, we are PID1/autoreaper after all, hence when it dies we'll automatically clean it up
658 r
= safe_fork_full("(sd-expire)",
659 /* stdio_fds= */ NULL
,
660 (int[]) { dev_autofs_fd
, ioctl_fd
},
661 /* n_except_fds= */ 2,
662 FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_REOPEN_LOG
,
663 /* ret_pid= */ NULL
);
669 struct autofs_dev_ioctl param
;
670 init_autofs_dev_ioctl(¶m
);
671 param
.ioctlfd
= ioctl_fd
;
673 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_EXPIRE
, ¶m
) < 0)
678 log_warning_errno(errno
, "Failed to expire automount, ignoring: %m");
683 static int automount_dispatch_expire(sd_event_source
*source
, usec_t usec
, void *userdata
) {
684 Automount
*a
= ASSERT_PTR(AUTOMOUNT(userdata
));
685 _cleanup_close_
int ioctl_fd
= -EBADF
;
688 assert(source
== a
->expire_event_source
);
690 ioctl_fd
= open_ioctl_fd(UNIT(a
)->manager
->dev_autofs_fd
, a
->where
, a
->dev_id
);
692 return log_unit_error_errno(UNIT(a
), ioctl_fd
, "Couldn't open autofs ioctl fd: %m");
694 r
= asynchronous_expire(UNIT(a
)->manager
->dev_autofs_fd
, ioctl_fd
);
696 return log_unit_error_errno(UNIT(a
), r
, "Failed to start expire job: %m");
698 return automount_start_expire(a
);
701 static int automount_start_expire(Automount
*a
) {
707 if (a
->timeout_idle_usec
== 0)
710 timeout
= MAX(a
->timeout_idle_usec
/3, USEC_PER_SEC
);
712 if (a
->expire_event_source
) {
713 r
= sd_event_source_set_time_relative(a
->expire_event_source
, timeout
);
717 return sd_event_source_set_enabled(a
->expire_event_source
, SD_EVENT_ONESHOT
);
720 r
= sd_event_add_time_relative(
721 UNIT(a
)->manager
->event
,
722 &a
->expire_event_source
,
723 CLOCK_MONOTONIC
, timeout
, 0,
724 automount_dispatch_expire
, a
);
728 (void) sd_event_source_set_description(a
->expire_event_source
, "automount-expire");
733 static void automount_stop_expire(Automount
*a
) {
736 (void) sd_event_source_set_enabled(a
->expire_event_source
, SD_EVENT_OFF
);
739 static void automount_enter_running(Automount
*a
) {
740 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
747 /* If the user masked our unit in the meantime, fail */
748 if (UNIT(a
)->load_state
!= UNIT_LOADED
) {
749 log_unit_error(UNIT(a
), "Suppressing automount event since unit is no longer loaded.");
753 /* We don't take mount requests anymore if we are supposed to
754 * shut down anyway */
755 if (unit_stop_pending(UNIT(a
))) {
756 log_unit_debug(UNIT(a
), "Suppressing automount request since unit stop is scheduled.");
757 automount_send_ready(a
, a
->tokens
, -EHOSTDOWN
);
758 automount_send_ready(a
, a
->expire_tokens
, -EHOSTDOWN
);
762 (void) mkdir_p_label(a
->where
, a
->directory_mode
);
764 /* Before we do anything, let's see if somebody is playing games with us? */
765 if (lstat(a
->where
, &st
) < 0) {
766 log_unit_warning_errno(UNIT(a
), errno
, "Failed to stat automount point: %m");
770 /* The mount unit may have been explicitly started before we got the
771 * autofs request. Ack it to unblock anything waiting on the mount point. */
772 if (!S_ISDIR(st
.st_mode
) || st
.st_dev
!= a
->dev_id
) {
773 log_unit_info(UNIT(a
), "Automount point already active?");
774 automount_send_ready(a
, a
->tokens
, 0);
778 trigger
= UNIT_TRIGGER(UNIT(a
));
780 log_unit_error(UNIT(a
), "Unit to trigger vanished.");
784 r
= manager_add_job(UNIT(a
)->manager
, JOB_START
, trigger
, JOB_REPLACE
, &error
, /* ret = */ NULL
);
786 log_unit_warning(UNIT(a
), "Failed to queue mount startup job: %s", bus_error_message(&error
, r
));
790 automount_set_state(a
, AUTOMOUNT_RUNNING
);
794 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
797 static int automount_start(Unit
*u
) {
798 Automount
*a
= ASSERT_PTR(AUTOMOUNT(u
));
801 assert(IN_SET(a
->state
, AUTOMOUNT_DEAD
, AUTOMOUNT_FAILED
));
803 if (path_is_mount_point(a
->where
) > 0)
804 return log_unit_error_errno(u
, SYNTHETIC_ERRNO(EEXIST
), "Path %s is already a mount point, refusing start.", a
->where
);
806 r
= unit_test_trigger_loaded(u
);
810 r
= unit_acquire_invocation_id(u
);
814 a
->result
= AUTOMOUNT_SUCCESS
;
815 automount_enter_waiting(a
);
819 static int automount_stop(Unit
*u
) {
820 Automount
*a
= ASSERT_PTR(AUTOMOUNT(u
));
822 assert(IN_SET(a
->state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
));
824 automount_enter_dead(a
, AUTOMOUNT_SUCCESS
);
828 static int automount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
829 Automount
*a
= ASSERT_PTR(AUTOMOUNT(u
));
836 (void) serialize_item(f
, "state", automount_state_to_string(a
->state
));
837 (void) serialize_item(f
, "result", automount_result_to_string(a
->result
));
838 (void) serialize_item_format(f
, "dev-id", "%lu", (unsigned long) a
->dev_id
);
840 SET_FOREACH(p
, a
->tokens
)
841 (void) serialize_item_format(f
, "token", "%u", PTR_TO_UINT(p
));
842 SET_FOREACH(p
, a
->expire_tokens
)
843 (void) serialize_item_format(f
, "expire-token", "%u", PTR_TO_UINT(p
));
845 r
= serialize_fd(f
, fds
, "pipe-fd", a
->pipe_fd
);
852 static int automount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
853 Automount
*a
= ASSERT_PTR(AUTOMOUNT(u
));
858 if (streq(key
, "state")) {
859 AutomountState state
;
861 state
= automount_state_from_string(value
);
863 log_unit_debug(u
, "Failed to parse state value: %s", value
);
865 a
->deserialized_state
= state
;
866 } else if (streq(key
, "result")) {
869 f
= automount_result_from_string(value
);
871 log_unit_debug(u
, "Failed to parse result value: %s", value
);
872 else if (f
!= AUTOMOUNT_SUCCESS
)
875 } else if (streq(key
, "dev-id")) {
878 if (safe_atolu(value
, &d
) < 0)
879 log_unit_debug(u
, "Failed to parse dev-id value: %s", value
);
881 a
->dev_id
= (dev_t
) d
;
883 } else if (streq(key
, "token")) {
886 if (safe_atou(value
, &token
) < 0)
887 log_unit_debug(u
, "Failed to parse token value: %s", value
);
889 r
= set_ensure_put(&a
->tokens
, NULL
, UINT_TO_PTR(token
));
891 log_unit_error_errno(u
, r
, "Failed to add token to set: %m");
893 } else if (streq(key
, "expire-token")) {
896 if (safe_atou(value
, &token
) < 0)
897 log_unit_debug(u
, "Failed to parse token value: %s", value
);
899 r
= set_ensure_put(&a
->expire_tokens
, NULL
, UINT_TO_PTR(token
));
901 log_unit_error_errno(u
, r
, "Failed to add expire token to set: %m");
903 } else if (streq(key
, "pipe-fd")) {
904 safe_close(a
->pipe_fd
);
905 a
->pipe_fd
= deserialize_fd(fds
, value
);
907 log_unit_debug(u
, "Unknown serialization key: %s", key
);
912 static UnitActiveState
automount_active_state(Unit
*u
) {
915 return state_translation_table
[AUTOMOUNT(u
)->state
];
918 static const char *automount_sub_state_to_string(Unit
*u
) {
921 return automount_state_to_string(AUTOMOUNT(u
)->state
);
924 static bool automount_may_gc(Unit
*u
) {
933 return UNIT_VTABLE(t
)->may_gc(t
);
936 static int automount_dispatch_io(sd_event_source
*s
, int fd
, uint32_t events
, void *userdata
) {
937 Automount
*a
= ASSERT_PTR(AUTOMOUNT(userdata
));
938 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
939 union autofs_v5_packet_union packet
;
943 assert(fd
== a
->pipe_fd
);
945 if (events
& (EPOLLHUP
|EPOLLERR
)) {
946 log_unit_error(UNIT(a
), "Got hangup/error on autofs pipe from kernel. Likely our automount point has been unmounted by someone or something else?");
947 automount_enter_dead(a
, AUTOMOUNT_FAILURE_UNMOUNTED
);
951 if (events
!= EPOLLIN
) {
952 log_unit_error(UNIT(a
), "Got invalid poll event %"PRIu32
" on pipe (fd=%d)", events
, fd
);
956 r
= loop_read_exact(a
->pipe_fd
, &packet
, sizeof(packet
), true);
958 log_unit_error_errno(UNIT(a
), r
, "Invalid read from pipe: %m");
962 switch (packet
.hdr
.type
) {
964 case autofs_ptype_missing_direct
:
966 if (packet
.v5_packet
.pid
> 0) {
967 _cleanup_free_
char *p
= NULL
;
969 (void) pid_get_comm(packet
.v5_packet
.pid
, &p
);
970 log_unit_info(UNIT(a
), "Got automount request for %s, triggered by %"PRIu32
" (%s)", a
->where
, packet
.v5_packet
.pid
, strna(p
));
972 log_unit_debug(UNIT(a
), "Got direct mount request on %s", a
->where
);
974 r
= set_ensure_put(&a
->tokens
, NULL
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
));
976 log_unit_error_errno(UNIT(a
), r
, "Failed to remember token: %m");
980 automount_enter_running(a
);
983 case autofs_ptype_expire_direct
:
984 log_unit_debug(UNIT(a
), "Got direct umount request on %s", a
->where
);
986 automount_stop_expire(a
);
988 r
= set_ensure_put(&a
->expire_tokens
, NULL
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
));
990 log_unit_error_errno(UNIT(a
), r
, "Failed to remember token: %m");
994 trigger
= UNIT_TRIGGER(UNIT(a
));
996 log_unit_error(UNIT(a
), "Unit to trigger vanished.");
1000 r
= manager_add_job(UNIT(a
)->manager
, JOB_STOP
, trigger
, JOB_REPLACE
, &error
, /* ret = */ NULL
);
1002 log_unit_warning(UNIT(a
), "Failed to queue unmount job: %s", bus_error_message(&error
, r
));
1008 log_unit_error(UNIT(a
), "Received unknown automount request %i", packet
.hdr
.type
);
1014 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
1018 static void automount_shutdown(Manager
*m
) {
1021 m
->dev_autofs_fd
= safe_close(m
->dev_autofs_fd
);
1024 static void automount_reset_failed(Unit
*u
) {
1025 Automount
*a
= ASSERT_PTR(AUTOMOUNT(u
));
1027 if (a
->state
== AUTOMOUNT_FAILED
)
1028 automount_set_state(a
, AUTOMOUNT_DEAD
);
1030 a
->result
= AUTOMOUNT_SUCCESS
;
1033 static bool automount_supported(void) {
1034 static int supported
= -1;
1037 supported
= access("/dev/autofs", F_OK
) >= 0;
1042 static int automount_can_start(Unit
*u
) {
1043 Automount
*a
= ASSERT_PTR(AUTOMOUNT(u
));
1046 r
= unit_test_start_limit(u
);
1048 automount_enter_dead(a
, AUTOMOUNT_FAILURE_START_LIMIT_HIT
);
1055 static const char* const automount_result_table
[_AUTOMOUNT_RESULT_MAX
] = {
1056 [AUTOMOUNT_SUCCESS
] = "success",
1057 [AUTOMOUNT_FAILURE_RESOURCES
] = "resources",
1058 [AUTOMOUNT_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
1059 [AUTOMOUNT_FAILURE_MOUNT_START_LIMIT_HIT
] = "mount-start-limit-hit",
1060 [AUTOMOUNT_FAILURE_UNMOUNTED
] = "unmounted",
1063 DEFINE_STRING_TABLE_LOOKUP(automount_result
, AutomountResult
);
1065 const UnitVTable automount_vtable
= {
1066 .object_size
= sizeof(Automount
),
1072 .private_section
= "Automount",
1074 .can_transient
= true,
1076 .can_trigger
= true,
1077 .exclude_from_switch_root_serialization
= true,
1079 .init
= automount_init
,
1080 .load
= automount_load
,
1081 .done
= automount_done
,
1083 .coldplug
= automount_coldplug
,
1085 .dump
= automount_dump
,
1087 .start
= automount_start
,
1088 .stop
= automount_stop
,
1090 .serialize
= automount_serialize
,
1091 .deserialize_item
= automount_deserialize_item
,
1093 .active_state
= automount_active_state
,
1094 .sub_state_to_string
= automount_sub_state_to_string
,
1096 .may_gc
= automount_may_gc
,
1098 .trigger_notify
= automount_trigger_notify
,
1100 .reset_failed
= automount_reset_failed
,
1102 .bus_set_property
= bus_automount_set_property
,
1104 .shutdown
= automount_shutdown
,
1105 .supported
= automount_supported
,
1107 .status_message_formats
= {
1108 .finished_start_job
= {
1109 [JOB_DONE
] = "Set up automount %s.",
1110 [JOB_FAILED
] = "Failed to set up automount %s.",
1112 .finished_stop_job
= {
1113 [JOB_DONE
] = "Unset automount %s.",
1114 [JOB_FAILED
] = "Failed to unset automount %s.",
1118 .can_start
= automount_can_start
,