1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include <linux/auto_dev-ioctl.h>
7 #include <linux/auto_fs4.h>
13 #include "alloc-util.h"
15 #include "automount.h"
16 #include "bus-error.h"
18 #include "dbus-automount.h"
19 #include "dbus-unit.h"
21 #include "format-util.h"
22 #include "fstab-util.h"
25 #include "mkdir-label.h"
26 #include "mount-util.h"
28 #include "mountpoint-util.h"
29 #include "parse-util.h"
30 #include "path-util.h"
31 #include "process-util.h"
32 #include "serialize.h"
34 #include "stdio-util.h"
35 #include "string-table.h"
36 #include "string-util.h"
37 #include "unit-name.h"
40 static const UnitActiveState state_translation_table
[_AUTOMOUNT_STATE_MAX
] = {
41 [AUTOMOUNT_DEAD
] = UNIT_INACTIVE
,
42 [AUTOMOUNT_WAITING
] = UNIT_ACTIVE
,
43 [AUTOMOUNT_RUNNING
] = UNIT_ACTIVE
,
44 [AUTOMOUNT_FAILED
] = UNIT_FAILED
52 static struct expire_data
* expire_data_free(struct expire_data
*data
) {
56 safe_close(data
->dev_autofs_fd
);
57 safe_close(data
->ioctl_fd
);
61 DEFINE_TRIVIAL_CLEANUP_FUNC(struct expire_data
*, expire_data_free
);
63 static int open_dev_autofs(Manager
*m
);
64 static int automount_dispatch_io(sd_event_source
*s
, int fd
, uint32_t events
, void *userdata
);
65 static int automount_start_expire(Automount
*a
);
66 static void automount_stop_expire(Automount
*a
);
67 static int automount_send_ready(Automount
*a
, Set
*tokens
, int status
);
69 static void automount_init(Unit
*u
) {
70 Automount
*a
= AUTOMOUNT(u
);
74 assert(u
->load_state
== UNIT_STUB
);
77 a
->directory_mode
= 0755;
78 UNIT(a
)->ignore_on_isolate
= true;
81 static void unmount_autofs(Automount
*a
) {
89 a
->pipe_event_source
= sd_event_source_disable_unref(a
->pipe_event_source
);
90 a
->pipe_fd
= safe_close(a
->pipe_fd
);
92 /* If we reload/reexecute things we keep the mount point around */
93 if (!IN_SET(UNIT(a
)->manager
->objective
, MANAGER_RELOAD
, MANAGER_REEXECUTE
)) {
95 automount_send_ready(a
, a
->tokens
, -EHOSTDOWN
);
96 automount_send_ready(a
, a
->expire_tokens
, -EHOSTDOWN
);
99 r
= repeat_unmount(a
->where
, MNT_DETACH
|UMOUNT_NOFOLLOW
);
101 log_error_errno(r
, "Failed to unmount: %m");
106 static void automount_done(Unit
*u
) {
107 Automount
*a
= AUTOMOUNT(u
);
113 a
->where
= mfree(a
->where
);
114 a
->extra_options
= mfree(a
->extra_options
);
116 a
->tokens
= set_free(a
->tokens
);
117 a
->expire_tokens
= set_free(a
->expire_tokens
);
119 a
->expire_event_source
= sd_event_source_disable_unref(a
->expire_event_source
);
122 static int automount_add_trigger_dependencies(Automount
*a
) {
128 r
= unit_load_related_unit(UNIT(a
), ".mount", &x
);
132 return unit_add_two_dependencies(UNIT(a
), UNIT_BEFORE
, UNIT_TRIGGERS
, x
, true, UNIT_DEPENDENCY_IMPLICIT
);
135 static int automount_add_mount_dependencies(Automount
*a
) {
136 _cleanup_free_
char *parent
= NULL
;
141 r
= path_extract_directory(a
->where
, &parent
);
145 return unit_require_mounts_for(UNIT(a
), parent
, UNIT_DEPENDENCY_IMPLICIT
);
148 static int automount_add_default_dependencies(Automount
*a
) {
153 if (!UNIT(a
)->default_dependencies
)
156 if (!MANAGER_IS_SYSTEM(UNIT(a
)->manager
))
159 r
= unit_add_dependency_by_name(UNIT(a
), UNIT_BEFORE
, SPECIAL_LOCAL_FS_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
163 r
= unit_add_dependency_by_name(UNIT(a
), UNIT_AFTER
, SPECIAL_LOCAL_FS_PRE_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
167 r
= unit_add_two_dependencies_by_name(UNIT(a
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
174 static int automount_verify(Automount
*a
) {
175 static const char *const reserved_options
[] = {
184 _cleanup_free_
char *e
= NULL
;
188 assert(UNIT(a
)->load_state
== UNIT_LOADED
);
190 if (path_equal(a
->where
, "/"))
191 return log_unit_error_errno(UNIT(a
), SYNTHETIC_ERRNO(ENOEXEC
), "Cannot have an automount unit for the root directory. Refusing.");
193 r
= unit_name_from_path(a
->where
, ".automount", &e
);
195 return log_unit_error_errno(UNIT(a
), r
, "Failed to generate unit name from path: %m");
197 if (!unit_has_name(UNIT(a
), e
))
198 return log_unit_error_errno(UNIT(a
), SYNTHETIC_ERRNO(ENOEXEC
), "Where= setting doesn't match unit name. Refusing.");
200 for (size_t i
= 0; i
< ELEMENTSOF(reserved_options
); i
++)
201 if (fstab_test_option(a
->extra_options
, reserved_options
[i
]))
202 return log_unit_error_errno(
204 SYNTHETIC_ERRNO(ENOEXEC
),
205 "ExtraOptions= setting may not contain reserved option %s.",
206 reserved_options
[i
]);
211 static int automount_set_where(Automount
*a
) {
219 r
= unit_name_to_path(UNIT(a
)->id
, &a
->where
);
223 path_simplify(a
->where
);
227 static int automount_add_extras(Automount
*a
) {
230 r
= automount_set_where(a
);
234 r
= automount_add_trigger_dependencies(a
);
238 r
= automount_add_mount_dependencies(a
);
242 return automount_add_default_dependencies(a
);
245 static int automount_load(Unit
*u
) {
246 Automount
*a
= AUTOMOUNT(u
);
250 assert(u
->load_state
== UNIT_STUB
);
252 /* Load a .automount file */
253 r
= unit_load_fragment_and_dropin(u
, true);
257 if (u
->load_state
!= UNIT_LOADED
)
260 r
= automount_add_extras(a
);
264 return automount_verify(a
);
267 static void automount_set_state(Automount
*a
, AutomountState state
) {
268 AutomountState old_state
;
271 if (a
->state
!= state
)
272 bus_unit_send_pending_change_signal(UNIT(a
), false);
274 old_state
= a
->state
;
277 if (state
!= AUTOMOUNT_RUNNING
)
278 automount_stop_expire(a
);
280 if (!IN_SET(state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
))
283 if (state
!= old_state
)
284 log_unit_debug(UNIT(a
), "Changed %s -> %s", automount_state_to_string(old_state
), automount_state_to_string(state
));
286 unit_notify(UNIT(a
), state_translation_table
[old_state
], state_translation_table
[state
], /* reload_success = */ true);
289 static int automount_coldplug(Unit
*u
) {
290 Automount
*a
= AUTOMOUNT(u
);
294 assert(a
->state
== AUTOMOUNT_DEAD
);
296 if (a
->deserialized_state
== a
->state
)
299 if (IN_SET(a
->deserialized_state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
)) {
301 r
= automount_set_where(a
);
305 r
= open_dev_autofs(u
->manager
);
309 assert(a
->pipe_fd
>= 0);
311 r
= sd_event_add_io(u
->manager
->event
, &a
->pipe_event_source
, a
->pipe_fd
, EPOLLIN
, automount_dispatch_io
, u
);
315 (void) sd_event_source_set_description(a
->pipe_event_source
, "automount-io");
316 if (a
->deserialized_state
== AUTOMOUNT_RUNNING
) {
317 r
= automount_start_expire(a
);
319 log_unit_warning_errno(UNIT(a
), r
, "Failed to start expiration timer, ignoring: %m");
322 automount_set_state(a
, a
->deserialized_state
);
328 static void automount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
329 Automount
*a
= AUTOMOUNT(u
);
334 "%sAutomount State: %s\n"
337 "%sExtraOptions: %s\n"
338 "%sDirectoryMode: %04o\n"
339 "%sTimeoutIdleUSec: %s\n",
340 prefix
, automount_state_to_string(a
->state
),
341 prefix
, automount_result_to_string(a
->result
),
343 prefix
, a
->extra_options
,
344 prefix
, a
->directory_mode
,
345 prefix
, FORMAT_TIMESPAN(a
->timeout_idle_usec
, USEC_PER_SEC
));
348 static void automount_enter_dead(Automount
*a
, AutomountResult f
) {
351 if (a
->result
== AUTOMOUNT_SUCCESS
)
354 unit_log_result(UNIT(a
), a
->result
== AUTOMOUNT_SUCCESS
, automount_result_to_string(a
->result
));
355 automount_set_state(a
, a
->result
!= AUTOMOUNT_SUCCESS
? AUTOMOUNT_FAILED
: AUTOMOUNT_DEAD
);
358 static int open_dev_autofs(Manager
*m
) {
359 struct autofs_dev_ioctl param
;
363 if (m
->dev_autofs_fd
>= 0)
364 return m
->dev_autofs_fd
;
366 (void) label_fix("/dev/autofs", 0);
368 m
->dev_autofs_fd
= open("/dev/autofs", O_CLOEXEC
|O_RDONLY
);
369 if (m
->dev_autofs_fd
< 0)
370 return log_error_errno(errno
, "Failed to open /dev/autofs: %m");
372 init_autofs_dev_ioctl(¶m
);
373 if (ioctl(m
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_VERSION
, ¶m
) < 0) {
374 m
->dev_autofs_fd
= safe_close(m
->dev_autofs_fd
);
378 log_debug("Autofs kernel version %u.%u", param
.ver_major
, param
.ver_minor
);
380 return m
->dev_autofs_fd
;
383 static int open_ioctl_fd(int dev_autofs_fd
, const char *where
, dev_t devid
) {
384 struct autofs_dev_ioctl
*param
;
387 assert(dev_autofs_fd
>= 0);
390 l
= sizeof(struct autofs_dev_ioctl
) + strlen(where
) + 1;
391 param
= alloca_safe(l
);
393 init_autofs_dev_ioctl(param
);
395 param
->ioctlfd
= -EBADF
;
396 param
->openmount
.devid
= devid
;
397 strcpy(param
->path
, where
);
399 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_OPENMOUNT
, param
) < 0)
402 if (param
->ioctlfd
< 0)
405 (void) fd_cloexec(param
->ioctlfd
, true);
406 return param
->ioctlfd
;
409 static int autofs_protocol(int dev_autofs_fd
, int ioctl_fd
) {
410 uint32_t major
, minor
;
411 struct autofs_dev_ioctl param
;
413 assert(dev_autofs_fd
>= 0);
414 assert(ioctl_fd
>= 0);
416 init_autofs_dev_ioctl(¶m
);
417 param
.ioctlfd
= ioctl_fd
;
419 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOVER
, ¶m
) < 0)
422 major
= param
.protover
.version
;
424 init_autofs_dev_ioctl(¶m
);
425 param
.ioctlfd
= ioctl_fd
;
427 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOSUBVER
, ¶m
) < 0)
430 minor
= param
.protosubver
.sub_version
;
432 log_debug("Autofs protocol version %u.%u", major
, minor
);
436 static int autofs_set_timeout(int dev_autofs_fd
, int ioctl_fd
, usec_t usec
) {
437 struct autofs_dev_ioctl param
;
439 assert(dev_autofs_fd
>= 0);
440 assert(ioctl_fd
>= 0);
442 init_autofs_dev_ioctl(¶m
);
443 param
.ioctlfd
= ioctl_fd
;
445 if (usec
== USEC_INFINITY
)
446 param
.timeout
.timeout
= 0;
448 /* Convert to seconds, rounding up. */
449 param
.timeout
.timeout
= DIV_ROUND_UP(usec
, USEC_PER_SEC
);
451 return RET_NERRNO(ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_TIMEOUT
, ¶m
));
454 static int autofs_send_ready(int dev_autofs_fd
, int ioctl_fd
, uint32_t token
, int status
) {
455 struct autofs_dev_ioctl param
;
457 assert(dev_autofs_fd
>= 0);
458 assert(ioctl_fd
>= 0);
460 init_autofs_dev_ioctl(¶m
);
461 param
.ioctlfd
= ioctl_fd
;
464 param
.fail
.token
= token
;
465 param
.fail
.status
= status
;
467 param
.ready
.token
= token
;
469 return RET_NERRNO(ioctl(dev_autofs_fd
, status
? AUTOFS_DEV_IOCTL_FAIL
: AUTOFS_DEV_IOCTL_READY
, ¶m
));
472 static int automount_send_ready(Automount
*a
, Set
*tokens
, int status
) {
473 _cleanup_close_
int ioctl_fd
= -EBADF
;
480 if (set_isempty(tokens
))
483 ioctl_fd
= open_ioctl_fd(UNIT(a
)->manager
->dev_autofs_fd
, a
->where
, a
->dev_id
);
488 log_unit_debug_errno(UNIT(a
), status
, "Sending failure: %m");
490 log_unit_debug(UNIT(a
), "Sending success.");
494 /* Autofs thankfully does not hand out 0 as a token */
495 while ((token
= PTR_TO_UINT(set_steal_first(tokens
)))) {
500 * if you pass a positive status code here, kernels
501 * prior to 4.12 will freeze! Yay! */
503 k
= autofs_send_ready(UNIT(a
)->manager
->dev_autofs_fd
,
514 static void automount_trigger_notify(Unit
*u
, Unit
*other
) {
515 Automount
*a
= AUTOMOUNT(u
);
521 /* Filter out invocations with bogus state */
522 assert(UNIT_IS_LOAD_COMPLETE(other
->load_state
));
523 assert(other
->type
== UNIT_MOUNT
);
525 /* Don't propagate state changes from the mount if we are already down */
526 if (!IN_SET(a
->state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
))
529 /* Propagate start limit hit state */
530 if (other
->start_limit_hit
) {
531 automount_enter_dead(a
, AUTOMOUNT_FAILURE_MOUNT_START_LIMIT_HIT
);
535 /* Don't propagate anything if there's still a job queued */
539 /* The mount is successfully established */
540 if (IN_SET(MOUNT(other
)->state
, MOUNT_MOUNTED
, MOUNT_REMOUNTING
)) {
541 (void) automount_send_ready(a
, a
->tokens
, 0);
543 r
= automount_start_expire(a
);
545 log_unit_warning_errno(UNIT(a
), r
, "Failed to start expiration timer, ignoring: %m");
547 automount_set_state(a
, AUTOMOUNT_RUNNING
);
550 if (IN_SET(MOUNT(other
)->state
,
551 MOUNT_MOUNTING
, MOUNT_MOUNTING_DONE
,
552 MOUNT_MOUNTED
, MOUNT_REMOUNTING
,
553 MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
,
554 MOUNT_UNMOUNTING_SIGTERM
, MOUNT_UNMOUNTING_SIGKILL
,
556 (void) automount_send_ready(a
, a
->expire_tokens
, -ENODEV
);
558 if (MOUNT(other
)->state
== MOUNT_DEAD
)
559 (void) automount_send_ready(a
, a
->expire_tokens
, 0);
561 /* The mount is in some unhappy state now, let's unfreeze any waiting clients */
562 if (IN_SET(MOUNT(other
)->state
,
563 MOUNT_DEAD
, MOUNT_UNMOUNTING
,
564 MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
,
565 MOUNT_UNMOUNTING_SIGTERM
, MOUNT_UNMOUNTING_SIGKILL
,
568 (void) automount_send_ready(a
, a
->tokens
, -ENODEV
);
570 automount_set_state(a
, AUTOMOUNT_WAITING
);
574 static void automount_enter_waiting(Automount
*a
) {
575 _cleanup_close_
int ioctl_fd
= -EBADF
;
576 int pipe_fd
[2] = PIPE_EBADF
;
577 char name
[STRLEN("systemd-") + DECIMAL_STR_MAX(pid_t
) + 1];
578 _cleanup_free_
char *options
= NULL
;
579 bool mounted
= false;
580 int r
, dev_autofs_fd
;
584 assert(a
->pipe_fd
< 0);
587 set_clear(a
->tokens
);
589 r
= unit_fail_if_noncanonical(UNIT(a
), a
->where
);
593 (void) mkdir_p_label(a
->where
, a
->directory_mode
);
595 unit_warn_if_dir_nonempty(UNIT(a
), a
->where
);
597 dev_autofs_fd
= open_dev_autofs(UNIT(a
)->manager
);
598 if (dev_autofs_fd
< 0) {
603 if (pipe2(pipe_fd
, O_CLOEXEC
) < 0) {
607 r
= fd_nonblock(pipe_fd
[0], true);
613 "fd=%i,pgrp="PID_FMT
",minproto=5,maxproto=5,direct%s%s",
616 isempty(a
->extra_options
) ? "" : ",",
617 strempty(a
->extra_options
)) < 0) {
622 xsprintf(name
, "systemd-"PID_FMT
, getpid_cached());
623 r
= mount_nofollow(name
, a
->where
, "autofs", 0, options
);
629 pipe_fd
[1] = safe_close(pipe_fd
[1]);
631 if (stat(a
->where
, &st
) < 0) {
636 ioctl_fd
= open_ioctl_fd(dev_autofs_fd
, a
->where
, st
.st_dev
);
642 r
= autofs_protocol(dev_autofs_fd
, ioctl_fd
);
646 r
= autofs_set_timeout(dev_autofs_fd
, ioctl_fd
, a
->timeout_idle_usec
);
650 r
= sd_event_add_io(UNIT(a
)->manager
->event
, &a
->pipe_event_source
, pipe_fd
[0], EPOLLIN
, automount_dispatch_io
, a
);
654 (void) sd_event_source_set_description(a
->pipe_event_source
, "automount-io");
656 a
->pipe_fd
= pipe_fd
[0];
657 a
->dev_id
= st
.st_dev
;
659 automount_set_state(a
, AUTOMOUNT_WAITING
);
664 log_unit_error_errno(UNIT(a
), r
, "Failed to initialize automounter: %m");
666 safe_close_pair(pipe_fd
);
669 r
= repeat_unmount(a
->where
, MNT_DETACH
|UMOUNT_NOFOLLOW
);
671 log_error_errno(r
, "Failed to unmount, ignoring: %m");
674 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
677 static void *expire_thread(void *p
) {
678 struct autofs_dev_ioctl param
;
679 _cleanup_(expire_data_freep
) struct expire_data
*data
= p
;
682 assert(data
->dev_autofs_fd
>= 0);
683 assert(data
->ioctl_fd
>= 0);
685 init_autofs_dev_ioctl(¶m
);
686 param
.ioctlfd
= data
->ioctl_fd
;
689 r
= ioctl(data
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_EXPIRE
, ¶m
);
693 log_warning_errno(errno
, "Failed to expire automount, ignoring: %m");
698 static int automount_dispatch_expire(sd_event_source
*source
, usec_t usec
, void *userdata
) {
699 Automount
*a
= AUTOMOUNT(userdata
);
700 _cleanup_(expire_data_freep
) struct expire_data
*data
= NULL
;
704 assert(source
== a
->expire_event_source
);
706 data
= new0(struct expire_data
, 1);
710 data
->ioctl_fd
= -EBADF
;
712 data
->dev_autofs_fd
= fcntl(UNIT(a
)->manager
->dev_autofs_fd
, F_DUPFD_CLOEXEC
, 3);
713 if (data
->dev_autofs_fd
< 0)
714 return log_unit_error_errno(UNIT(a
), errno
, "Failed to duplicate autofs fd: %m");
716 data
->ioctl_fd
= open_ioctl_fd(UNIT(a
)->manager
->dev_autofs_fd
, a
->where
, a
->dev_id
);
717 if (data
->ioctl_fd
< 0)
718 return log_unit_error_errno(UNIT(a
), data
->ioctl_fd
, "Couldn't open autofs ioctl fd: %m");
720 r
= asynchronous_job(expire_thread
, data
);
722 return log_unit_error_errno(UNIT(a
), r
, "Failed to start expire job: %m");
726 return automount_start_expire(a
);
729 static int automount_start_expire(Automount
*a
) {
735 if (a
->timeout_idle_usec
== 0)
738 timeout
= MAX(a
->timeout_idle_usec
/3, USEC_PER_SEC
);
740 if (a
->expire_event_source
) {
741 r
= sd_event_source_set_time_relative(a
->expire_event_source
, timeout
);
745 return sd_event_source_set_enabled(a
->expire_event_source
, SD_EVENT_ONESHOT
);
748 r
= sd_event_add_time_relative(
749 UNIT(a
)->manager
->event
,
750 &a
->expire_event_source
,
751 CLOCK_MONOTONIC
, timeout
, 0,
752 automount_dispatch_expire
, a
);
756 (void) sd_event_source_set_description(a
->expire_event_source
, "automount-expire");
761 static void automount_stop_expire(Automount
*a
) {
764 if (!a
->expire_event_source
)
767 (void) sd_event_source_set_enabled(a
->expire_event_source
, SD_EVENT_OFF
);
770 static void automount_enter_running(Automount
*a
) {
771 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
778 /* If the user masked our unit in the meantime, fail */
779 if (UNIT(a
)->load_state
!= UNIT_LOADED
) {
780 log_unit_error(UNIT(a
), "Suppressing automount event since unit is no longer loaded.");
784 /* We don't take mount requests anymore if we are supposed to
785 * shut down anyway */
786 if (unit_stop_pending(UNIT(a
))) {
787 log_unit_debug(UNIT(a
), "Suppressing automount request since unit stop is scheduled.");
788 automount_send_ready(a
, a
->tokens
, -EHOSTDOWN
);
789 automount_send_ready(a
, a
->expire_tokens
, -EHOSTDOWN
);
793 (void) mkdir_p_label(a
->where
, a
->directory_mode
);
795 /* Before we do anything, let's see if somebody is playing games with us? */
796 if (lstat(a
->where
, &st
) < 0) {
797 log_unit_warning_errno(UNIT(a
), errno
, "Failed to stat automount point: %m");
801 /* The mount unit may have been explicitly started before we got the
802 * autofs request. Ack it to unblock anything waiting on the mount point. */
803 if (!S_ISDIR(st
.st_mode
) || st
.st_dev
!= a
->dev_id
) {
804 log_unit_info(UNIT(a
), "Automount point already active?");
805 automount_send_ready(a
, a
->tokens
, 0);
809 trigger
= UNIT_TRIGGER(UNIT(a
));
811 log_unit_error(UNIT(a
), "Unit to trigger vanished.");
815 r
= manager_add_job(UNIT(a
)->manager
, JOB_START
, trigger
, JOB_REPLACE
, NULL
, &error
, NULL
);
817 log_unit_warning(UNIT(a
), "Failed to queue mount startup job: %s", bus_error_message(&error
, r
));
821 automount_set_state(a
, AUTOMOUNT_RUNNING
);
825 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
828 static int automount_start(Unit
*u
) {
829 Automount
*a
= AUTOMOUNT(u
);
833 assert(IN_SET(a
->state
, AUTOMOUNT_DEAD
, AUTOMOUNT_FAILED
));
835 if (path_is_mount_point(a
->where
, NULL
, 0) > 0)
836 return log_unit_error_errno(u
, SYNTHETIC_ERRNO(EEXIST
), "Path %s is already a mount point, refusing start.", a
->where
);
838 r
= unit_test_trigger_loaded(u
);
842 r
= unit_acquire_invocation_id(u
);
846 a
->result
= AUTOMOUNT_SUCCESS
;
847 automount_enter_waiting(a
);
851 static int automount_stop(Unit
*u
) {
852 Automount
*a
= AUTOMOUNT(u
);
855 assert(IN_SET(a
->state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
));
857 automount_enter_dead(a
, AUTOMOUNT_SUCCESS
);
861 static int automount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
862 Automount
*a
= AUTOMOUNT(u
);
870 (void) serialize_item(f
, "state", automount_state_to_string(a
->state
));
871 (void) serialize_item(f
, "result", automount_result_to_string(a
->result
));
872 (void) serialize_item_format(f
, "dev-id", "%lu", (unsigned long) a
->dev_id
);
874 SET_FOREACH(p
, a
->tokens
)
875 (void) serialize_item_format(f
, "token", "%u", PTR_TO_UINT(p
));
876 SET_FOREACH(p
, a
->expire_tokens
)
877 (void) serialize_item_format(f
, "expire-token", "%u", PTR_TO_UINT(p
));
879 r
= serialize_fd(f
, fds
, "pipe-fd", a
->pipe_fd
);
886 static int automount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
887 Automount
*a
= AUTOMOUNT(u
);
893 if (streq(key
, "state")) {
894 AutomountState state
;
896 state
= automount_state_from_string(value
);
898 log_unit_debug(u
, "Failed to parse state value: %s", value
);
900 a
->deserialized_state
= state
;
901 } else if (streq(key
, "result")) {
904 f
= automount_result_from_string(value
);
906 log_unit_debug(u
, "Failed to parse result value: %s", value
);
907 else if (f
!= AUTOMOUNT_SUCCESS
)
910 } else if (streq(key
, "dev-id")) {
913 if (safe_atolu(value
, &d
) < 0)
914 log_unit_debug(u
, "Failed to parse dev-id value: %s", value
);
916 a
->dev_id
= (dev_t
) d
;
918 } else if (streq(key
, "token")) {
921 if (safe_atou(value
, &token
) < 0)
922 log_unit_debug(u
, "Failed to parse token value: %s", value
);
924 r
= set_ensure_put(&a
->tokens
, NULL
, UINT_TO_PTR(token
));
926 log_unit_error_errno(u
, r
, "Failed to add token to set: %m");
928 } else if (streq(key
, "expire-token")) {
931 if (safe_atou(value
, &token
) < 0)
932 log_unit_debug(u
, "Failed to parse token value: %s", value
);
934 r
= set_ensure_put(&a
->expire_tokens
, NULL
, UINT_TO_PTR(token
));
936 log_unit_error_errno(u
, r
, "Failed to add expire token to set: %m");
938 } else if (streq(key
, "pipe-fd")) {
941 if ((fd
= parse_fd(value
)) < 0 || !fdset_contains(fds
, fd
))
942 log_unit_debug(u
, "Failed to parse pipe-fd value: %s", value
);
944 safe_close(a
->pipe_fd
);
945 a
->pipe_fd
= fdset_remove(fds
, fd
);
948 log_unit_debug(u
, "Unknown serialization key: %s", key
);
953 static UnitActiveState
automount_active_state(Unit
*u
) {
956 return state_translation_table
[AUTOMOUNT(u
)->state
];
959 static const char *automount_sub_state_to_string(Unit
*u
) {
962 return automount_state_to_string(AUTOMOUNT(u
)->state
);
965 static bool automount_may_gc(Unit
*u
) {
974 return UNIT_VTABLE(t
)->may_gc(t
);
977 static int automount_dispatch_io(sd_event_source
*s
, int fd
, uint32_t events
, void *userdata
) {
978 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
979 union autofs_v5_packet_union packet
;
980 Automount
*a
= AUTOMOUNT(userdata
);
985 assert(fd
== a
->pipe_fd
);
987 if (events
& (EPOLLHUP
|EPOLLERR
)) {
988 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?");
989 automount_enter_dead(a
, AUTOMOUNT_FAILURE_UNMOUNTED
);
993 if (events
!= EPOLLIN
) {
994 log_unit_error(UNIT(a
), "Got invalid poll event %"PRIu32
" on pipe (fd=%d)", events
, fd
);
998 r
= loop_read_exact(a
->pipe_fd
, &packet
, sizeof(packet
), true);
1000 log_unit_error_errno(UNIT(a
), r
, "Invalid read from pipe: %m");
1004 switch (packet
.hdr
.type
) {
1006 case autofs_ptype_missing_direct
:
1008 if (packet
.v5_packet
.pid
> 0) {
1009 _cleanup_free_
char *p
= NULL
;
1011 (void) get_process_comm(packet
.v5_packet
.pid
, &p
);
1012 log_unit_info(UNIT(a
), "Got automount request for %s, triggered by %"PRIu32
" (%s)", a
->where
, packet
.v5_packet
.pid
, strna(p
));
1014 log_unit_debug(UNIT(a
), "Got direct mount request on %s", a
->where
);
1016 r
= set_ensure_put(&a
->tokens
, NULL
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
));
1018 log_unit_error_errno(UNIT(a
), r
, "Failed to remember token: %m");
1022 automount_enter_running(a
);
1025 case autofs_ptype_expire_direct
:
1026 log_unit_debug(UNIT(a
), "Got direct umount request on %s", a
->where
);
1028 automount_stop_expire(a
);
1030 r
= set_ensure_put(&a
->expire_tokens
, NULL
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
));
1032 log_unit_error_errno(UNIT(a
), r
, "Failed to remember token: %m");
1036 trigger
= UNIT_TRIGGER(UNIT(a
));
1038 log_unit_error(UNIT(a
), "Unit to trigger vanished.");
1042 r
= manager_add_job(UNIT(a
)->manager
, JOB_STOP
, trigger
, JOB_REPLACE
, NULL
, &error
, NULL
);
1044 log_unit_warning(UNIT(a
), "Failed to queue unmount job: %s", bus_error_message(&error
, r
));
1050 log_unit_error(UNIT(a
), "Received unknown automount request %i", packet
.hdr
.type
);
1057 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
1061 static void automount_shutdown(Manager
*m
) {
1064 m
->dev_autofs_fd
= safe_close(m
->dev_autofs_fd
);
1067 static void automount_reset_failed(Unit
*u
) {
1068 Automount
*a
= AUTOMOUNT(u
);
1072 if (a
->state
== AUTOMOUNT_FAILED
)
1073 automount_set_state(a
, AUTOMOUNT_DEAD
);
1075 a
->result
= AUTOMOUNT_SUCCESS
;
1078 static bool automount_supported(void) {
1079 static int supported
= -1;
1082 supported
= access("/dev/autofs", F_OK
) >= 0;
1087 static int automount_can_start(Unit
*u
) {
1088 Automount
*a
= AUTOMOUNT(u
);
1093 r
= unit_test_start_limit(u
);
1095 automount_enter_dead(a
, AUTOMOUNT_FAILURE_START_LIMIT_HIT
);
1102 static const char* const automount_result_table
[_AUTOMOUNT_RESULT_MAX
] = {
1103 [AUTOMOUNT_SUCCESS
] = "success",
1104 [AUTOMOUNT_FAILURE_RESOURCES
] = "resources",
1105 [AUTOMOUNT_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
1106 [AUTOMOUNT_FAILURE_MOUNT_START_LIMIT_HIT
] = "mount-start-limit-hit",
1107 [AUTOMOUNT_FAILURE_UNMOUNTED
] = "unmounted",
1110 DEFINE_STRING_TABLE_LOOKUP(automount_result
, AutomountResult
);
1112 const UnitVTable automount_vtable
= {
1113 .object_size
= sizeof(Automount
),
1119 .private_section
= "Automount",
1121 .can_transient
= true,
1123 .can_trigger
= true,
1124 .exclude_from_switch_root_serialization
= true,
1126 .init
= automount_init
,
1127 .load
= automount_load
,
1128 .done
= automount_done
,
1130 .coldplug
= automount_coldplug
,
1132 .dump
= automount_dump
,
1134 .start
= automount_start
,
1135 .stop
= automount_stop
,
1137 .serialize
= automount_serialize
,
1138 .deserialize_item
= automount_deserialize_item
,
1140 .active_state
= automount_active_state
,
1141 .sub_state_to_string
= automount_sub_state_to_string
,
1143 .may_gc
= automount_may_gc
,
1145 .trigger_notify
= automount_trigger_notify
,
1147 .reset_failed
= automount_reset_failed
,
1149 .bus_set_property
= bus_automount_set_property
,
1151 .shutdown
= automount_shutdown
,
1152 .supported
= automount_supported
,
1154 .status_message_formats
= {
1155 .finished_start_job
= {
1156 [JOB_DONE
] = "Set up automount %s.",
1157 [JOB_FAILED
] = "Failed to set up automount %s.",
1159 .finished_stop_job
= {
1160 [JOB_DONE
] = "Unset automount %s.",
1161 [JOB_FAILED
] = "Failed to unset automount %s.",
1165 .can_start
= automount_can_start
,