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"
24 #include "label-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
47 static int open_dev_autofs(Manager
*m
);
48 static int automount_dispatch_io(sd_event_source
*s
, int fd
, uint32_t events
, void *userdata
);
49 static int automount_start_expire(Automount
*a
);
50 static void automount_stop_expire(Automount
*a
);
51 static int automount_send_ready(Automount
*a
, Set
*tokens
, int status
);
53 static void automount_init(Unit
*u
) {
54 Automount
*a
= AUTOMOUNT(u
);
58 assert(u
->load_state
== UNIT_STUB
);
61 a
->directory_mode
= 0755;
62 UNIT(a
)->ignore_on_isolate
= true;
65 static void unmount_autofs(Automount
*a
) {
73 a
->pipe_event_source
= sd_event_source_disable_unref(a
->pipe_event_source
);
74 a
->pipe_fd
= safe_close(a
->pipe_fd
);
76 /* If we reload/reexecute things we keep the mount point around */
77 if (!IN_SET(UNIT(a
)->manager
->objective
, MANAGER_RELOAD
, MANAGER_REEXECUTE
)) {
79 automount_send_ready(a
, a
->tokens
, -EHOSTDOWN
);
80 automount_send_ready(a
, a
->expire_tokens
, -EHOSTDOWN
);
83 r
= repeat_unmount(a
->where
, MNT_DETACH
|UMOUNT_NOFOLLOW
);
85 log_error_errno(r
, "Failed to unmount: %m");
90 static void automount_done(Unit
*u
) {
91 Automount
*a
= AUTOMOUNT(u
);
97 a
->where
= mfree(a
->where
);
98 a
->extra_options
= mfree(a
->extra_options
);
100 a
->tokens
= set_free(a
->tokens
);
101 a
->expire_tokens
= set_free(a
->expire_tokens
);
103 a
->expire_event_source
= sd_event_source_disable_unref(a
->expire_event_source
);
106 static int automount_add_trigger_dependencies(Automount
*a
) {
112 r
= unit_load_related_unit(UNIT(a
), ".mount", &x
);
116 return unit_add_two_dependencies(UNIT(a
), UNIT_BEFORE
, UNIT_TRIGGERS
, x
, true, UNIT_DEPENDENCY_IMPLICIT
);
119 static int automount_add_mount_dependencies(Automount
*a
) {
120 _cleanup_free_
char *parent
= NULL
;
125 r
= path_extract_directory(a
->where
, &parent
);
129 return unit_require_mounts_for(UNIT(a
), parent
, UNIT_DEPENDENCY_IMPLICIT
);
132 static int automount_add_default_dependencies(Automount
*a
) {
137 if (!UNIT(a
)->default_dependencies
)
140 if (!MANAGER_IS_SYSTEM(UNIT(a
)->manager
))
143 r
= unit_add_dependency_by_name(UNIT(a
), UNIT_BEFORE
, SPECIAL_LOCAL_FS_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
147 r
= unit_add_dependency_by_name(UNIT(a
), UNIT_AFTER
, SPECIAL_LOCAL_FS_PRE_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
151 r
= unit_add_two_dependencies_by_name(UNIT(a
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
158 static int automount_verify(Automount
*a
) {
159 static const char *const reserved_options
[] = {
168 _cleanup_free_
char *e
= NULL
;
172 assert(UNIT(a
)->load_state
== UNIT_LOADED
);
174 if (path_equal(a
->where
, "/"))
175 return log_unit_error_errno(UNIT(a
), SYNTHETIC_ERRNO(ENOEXEC
), "Cannot have an automount unit for the root directory. Refusing.");
177 r
= unit_name_from_path(a
->where
, ".automount", &e
);
179 return log_unit_error_errno(UNIT(a
), r
, "Failed to generate unit name from path: %m");
181 if (!unit_has_name(UNIT(a
), e
))
182 return log_unit_error_errno(UNIT(a
), SYNTHETIC_ERRNO(ENOEXEC
), "Where= setting doesn't match unit name. Refusing.");
184 for (size_t i
= 0; i
< ELEMENTSOF(reserved_options
); i
++)
185 if (fstab_test_option(a
->extra_options
, reserved_options
[i
]))
186 return log_unit_error_errno(
188 SYNTHETIC_ERRNO(ENOEXEC
),
189 "ExtraOptions= setting may not contain reserved option %s.",
190 reserved_options
[i
]);
195 static int automount_set_where(Automount
*a
) {
203 r
= unit_name_to_path(UNIT(a
)->id
, &a
->where
);
207 path_simplify(a
->where
);
211 static int automount_add_extras(Automount
*a
) {
214 r
= automount_set_where(a
);
218 r
= automount_add_trigger_dependencies(a
);
222 r
= automount_add_mount_dependencies(a
);
226 return automount_add_default_dependencies(a
);
229 static int automount_load(Unit
*u
) {
230 Automount
*a
= AUTOMOUNT(u
);
234 assert(u
->load_state
== UNIT_STUB
);
236 /* Load a .automount file */
237 r
= unit_load_fragment_and_dropin(u
, true);
241 if (u
->load_state
!= UNIT_LOADED
)
244 r
= automount_add_extras(a
);
248 return automount_verify(a
);
251 static void automount_set_state(Automount
*a
, AutomountState state
) {
252 AutomountState old_state
;
255 if (a
->state
!= state
)
256 bus_unit_send_pending_change_signal(UNIT(a
), false);
258 old_state
= a
->state
;
261 if (state
!= AUTOMOUNT_RUNNING
)
262 automount_stop_expire(a
);
264 if (!IN_SET(state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
))
267 if (state
!= old_state
)
268 log_unit_debug(UNIT(a
), "Changed %s -> %s", automount_state_to_string(old_state
), automount_state_to_string(state
));
270 unit_notify(UNIT(a
), state_translation_table
[old_state
], state_translation_table
[state
], /* reload_success = */ true);
273 static int automount_coldplug(Unit
*u
) {
274 Automount
*a
= AUTOMOUNT(u
);
278 assert(a
->state
== AUTOMOUNT_DEAD
);
280 if (a
->deserialized_state
== a
->state
)
283 if (IN_SET(a
->deserialized_state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
)) {
285 r
= automount_set_where(a
);
289 r
= open_dev_autofs(u
->manager
);
293 assert(a
->pipe_fd
>= 0);
295 r
= sd_event_add_io(u
->manager
->event
, &a
->pipe_event_source
, a
->pipe_fd
, EPOLLIN
, automount_dispatch_io
, u
);
299 (void) sd_event_source_set_description(a
->pipe_event_source
, "automount-io");
300 if (a
->deserialized_state
== AUTOMOUNT_RUNNING
) {
301 r
= automount_start_expire(a
);
303 log_unit_warning_errno(UNIT(a
), r
, "Failed to start expiration timer, ignoring: %m");
306 automount_set_state(a
, a
->deserialized_state
);
312 static void automount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
313 Automount
*a
= AUTOMOUNT(u
);
318 "%sAutomount State: %s\n"
321 "%sExtraOptions: %s\n"
322 "%sDirectoryMode: %04o\n"
323 "%sTimeoutIdleUSec: %s\n",
324 prefix
, automount_state_to_string(a
->state
),
325 prefix
, automount_result_to_string(a
->result
),
327 prefix
, a
->extra_options
,
328 prefix
, a
->directory_mode
,
329 prefix
, FORMAT_TIMESPAN(a
->timeout_idle_usec
, USEC_PER_SEC
));
332 static void automount_enter_dead(Automount
*a
, AutomountResult f
) {
335 if (a
->result
== AUTOMOUNT_SUCCESS
)
338 unit_log_result(UNIT(a
), a
->result
== AUTOMOUNT_SUCCESS
, automount_result_to_string(a
->result
));
339 automount_set_state(a
, a
->result
!= AUTOMOUNT_SUCCESS
? AUTOMOUNT_FAILED
: AUTOMOUNT_DEAD
);
342 static int open_dev_autofs(Manager
*m
) {
343 struct autofs_dev_ioctl param
;
347 if (m
->dev_autofs_fd
>= 0)
348 return m
->dev_autofs_fd
;
350 (void) label_fix("/dev/autofs", 0);
352 m
->dev_autofs_fd
= open("/dev/autofs", O_CLOEXEC
|O_RDONLY
);
353 if (m
->dev_autofs_fd
< 0)
354 return log_error_errno(errno
, "Failed to open /dev/autofs: %m");
356 init_autofs_dev_ioctl(¶m
);
357 if (ioctl(m
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_VERSION
, ¶m
) < 0) {
358 m
->dev_autofs_fd
= safe_close(m
->dev_autofs_fd
);
362 log_debug("Autofs kernel version %u.%u", param
.ver_major
, param
.ver_minor
);
364 return m
->dev_autofs_fd
;
367 static int open_ioctl_fd(int dev_autofs_fd
, const char *where
, dev_t devid
) {
368 struct autofs_dev_ioctl
*param
;
371 assert(dev_autofs_fd
>= 0);
374 l
= sizeof(struct autofs_dev_ioctl
) + strlen(where
) + 1;
375 param
= alloca_safe(l
);
377 init_autofs_dev_ioctl(param
);
379 param
->ioctlfd
= -EBADF
;
380 param
->openmount
.devid
= devid
;
381 strcpy(param
->path
, where
);
383 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_OPENMOUNT
, param
) < 0)
386 if (param
->ioctlfd
< 0)
389 (void) fd_cloexec(param
->ioctlfd
, true);
390 return param
->ioctlfd
;
393 static int autofs_protocol(int dev_autofs_fd
, int ioctl_fd
) {
394 uint32_t major
, minor
;
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 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOVER
, ¶m
) < 0)
406 major
= param
.protover
.version
;
408 init_autofs_dev_ioctl(¶m
);
409 param
.ioctlfd
= ioctl_fd
;
411 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOSUBVER
, ¶m
) < 0)
414 minor
= param
.protosubver
.sub_version
;
416 log_debug("Autofs protocol version %u.%u", major
, minor
);
420 static int autofs_set_timeout(int dev_autofs_fd
, int ioctl_fd
, usec_t usec
) {
421 struct autofs_dev_ioctl param
;
423 assert(dev_autofs_fd
>= 0);
424 assert(ioctl_fd
>= 0);
426 init_autofs_dev_ioctl(¶m
);
427 param
.ioctlfd
= ioctl_fd
;
429 if (usec
== USEC_INFINITY
)
430 param
.timeout
.timeout
= 0;
432 /* Convert to seconds, rounding up. */
433 param
.timeout
.timeout
= DIV_ROUND_UP(usec
, USEC_PER_SEC
);
435 return RET_NERRNO(ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_TIMEOUT
, ¶m
));
438 static int autofs_send_ready(int dev_autofs_fd
, int ioctl_fd
, uint32_t token
, int status
) {
439 struct autofs_dev_ioctl param
;
441 assert(dev_autofs_fd
>= 0);
442 assert(ioctl_fd
>= 0);
444 init_autofs_dev_ioctl(¶m
);
445 param
.ioctlfd
= ioctl_fd
;
448 param
.fail
.token
= token
;
449 param
.fail
.status
= status
;
451 param
.ready
.token
= token
;
453 return RET_NERRNO(ioctl(dev_autofs_fd
, status
? AUTOFS_DEV_IOCTL_FAIL
: AUTOFS_DEV_IOCTL_READY
, ¶m
));
456 static int automount_send_ready(Automount
*a
, Set
*tokens
, int status
) {
457 _cleanup_close_
int ioctl_fd
= -EBADF
;
464 if (set_isempty(tokens
))
467 ioctl_fd
= open_ioctl_fd(UNIT(a
)->manager
->dev_autofs_fd
, a
->where
, a
->dev_id
);
472 log_unit_debug_errno(UNIT(a
), status
, "Sending failure: %m");
474 log_unit_debug(UNIT(a
), "Sending success.");
478 /* Autofs thankfully does not hand out 0 as a token */
479 while ((token
= PTR_TO_UINT(set_steal_first(tokens
)))) {
484 * if you pass a positive status code here, kernels
485 * prior to 4.12 will freeze! Yay! */
487 k
= autofs_send_ready(UNIT(a
)->manager
->dev_autofs_fd
,
498 static void automount_trigger_notify(Unit
*u
, Unit
*other
) {
499 Automount
*a
= AUTOMOUNT(u
);
505 /* Filter out invocations with bogus state */
506 assert(UNIT_IS_LOAD_COMPLETE(other
->load_state
));
507 assert(other
->type
== UNIT_MOUNT
);
509 /* Don't propagate state changes from the mount if we are already down */
510 if (!IN_SET(a
->state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
))
513 /* Propagate start limit hit state */
514 if (other
->start_limit_hit
) {
515 automount_enter_dead(a
, AUTOMOUNT_FAILURE_MOUNT_START_LIMIT_HIT
);
519 /* Don't propagate anything if there's still a job queued */
523 /* The mount is successfully established */
524 if (IN_SET(MOUNT(other
)->state
, MOUNT_MOUNTED
, MOUNT_REMOUNTING
)) {
525 (void) automount_send_ready(a
, a
->tokens
, 0);
527 r
= automount_start_expire(a
);
529 log_unit_warning_errno(UNIT(a
), r
, "Failed to start expiration timer, ignoring: %m");
531 automount_set_state(a
, AUTOMOUNT_RUNNING
);
534 if (IN_SET(MOUNT(other
)->state
,
535 MOUNT_MOUNTING
, MOUNT_MOUNTING_DONE
,
536 MOUNT_MOUNTED
, MOUNT_REMOUNTING
,
537 MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
,
538 MOUNT_UNMOUNTING_SIGTERM
, MOUNT_UNMOUNTING_SIGKILL
,
540 (void) automount_send_ready(a
, a
->expire_tokens
, -ENODEV
);
542 if (MOUNT(other
)->state
== MOUNT_DEAD
)
543 (void) automount_send_ready(a
, a
->expire_tokens
, 0);
545 /* The mount is in some unhappy state now, let's unfreeze any waiting clients */
546 if (IN_SET(MOUNT(other
)->state
,
547 MOUNT_DEAD
, MOUNT_UNMOUNTING
,
548 MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
,
549 MOUNT_UNMOUNTING_SIGTERM
, MOUNT_UNMOUNTING_SIGKILL
,
552 (void) automount_send_ready(a
, a
->tokens
, -ENODEV
);
554 automount_set_state(a
, AUTOMOUNT_WAITING
);
558 static void automount_enter_waiting(Automount
*a
) {
559 _cleanup_close_
int ioctl_fd
= -EBADF
;
560 int pipe_fd
[2] = PIPE_EBADF
;
561 char name
[STRLEN("systemd-") + DECIMAL_STR_MAX(pid_t
) + 1];
562 _cleanup_free_
char *options
= NULL
;
563 bool mounted
= false;
564 int r
, dev_autofs_fd
;
568 assert(a
->pipe_fd
< 0);
571 set_clear(a
->tokens
);
573 r
= unit_fail_if_noncanonical(UNIT(a
), a
->where
);
577 (void) mkdir_p_label(a
->where
, a
->directory_mode
);
579 unit_warn_if_dir_nonempty(UNIT(a
), a
->where
);
581 dev_autofs_fd
= open_dev_autofs(UNIT(a
)->manager
);
582 if (dev_autofs_fd
< 0) {
587 if (pipe2(pipe_fd
, O_CLOEXEC
) < 0) {
591 r
= fd_nonblock(pipe_fd
[0], true);
597 "fd=%i,pgrp="PID_FMT
",minproto=5,maxproto=5,direct%s%s",
600 isempty(a
->extra_options
) ? "" : ",",
601 strempty(a
->extra_options
)) < 0) {
606 xsprintf(name
, "systemd-"PID_FMT
, getpid_cached());
607 r
= mount_nofollow(name
, a
->where
, "autofs", 0, options
);
613 pipe_fd
[1] = safe_close(pipe_fd
[1]);
615 if (stat(a
->where
, &st
) < 0) {
620 ioctl_fd
= open_ioctl_fd(dev_autofs_fd
, a
->where
, st
.st_dev
);
626 r
= autofs_protocol(dev_autofs_fd
, ioctl_fd
);
630 r
= autofs_set_timeout(dev_autofs_fd
, ioctl_fd
, a
->timeout_idle_usec
);
634 r
= sd_event_add_io(UNIT(a
)->manager
->event
, &a
->pipe_event_source
, pipe_fd
[0], EPOLLIN
, automount_dispatch_io
, a
);
638 (void) sd_event_source_set_description(a
->pipe_event_source
, "automount-io");
640 a
->pipe_fd
= pipe_fd
[0];
641 a
->dev_id
= st
.st_dev
;
643 automount_set_state(a
, AUTOMOUNT_WAITING
);
648 log_unit_error_errno(UNIT(a
), r
, "Failed to initialize automounter: %m");
650 safe_close_pair(pipe_fd
);
653 r
= repeat_unmount(a
->where
, MNT_DETACH
|UMOUNT_NOFOLLOW
);
655 log_error_errno(r
, "Failed to unmount, ignoring: %m");
658 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
661 static int asynchronous_expire(int dev_autofs_fd
, int ioctl_fd
) {
664 assert(dev_autofs_fd
>= 0);
665 assert(ioctl_fd
>= 0);
667 /* Issue AUTOFS_DEV_IOCTL_EXPIRE in subprocess, asynchronously. Note that we don't keep track of the
668 * child's PID, we are PID1/autoreaper after all, hence when it dies we'll automatically clean it up
671 r
= safe_fork_full("(sd-expire)",
672 /* stdio_fds= */ NULL
,
673 (int[]) { dev_autofs_fd
, ioctl_fd
},
674 /* n_except_fds= */ 2,
675 FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_REOPEN_LOG
,
682 struct autofs_dev_ioctl param
;
683 init_autofs_dev_ioctl(¶m
);
684 param
.ioctlfd
= ioctl_fd
;
686 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_EXPIRE
, ¶m
) < 0)
691 log_warning_errno(errno
, "Failed to expire automount, ignoring: %m");
696 static int automount_dispatch_expire(sd_event_source
*source
, usec_t usec
, void *userdata
) {
697 _cleanup_close_
int ioctl_fd
= -EBADF
;
698 Automount
*a
= AUTOMOUNT(userdata
);
702 assert(source
== a
->expire_event_source
);
704 ioctl_fd
= open_ioctl_fd(UNIT(a
)->manager
->dev_autofs_fd
, a
->where
, a
->dev_id
);
706 return log_unit_error_errno(UNIT(a
), ioctl_fd
, "Couldn't open autofs ioctl fd: %m");
708 r
= asynchronous_expire(UNIT(a
)->manager
->dev_autofs_fd
, ioctl_fd
);
710 return log_unit_error_errno(UNIT(a
), r
, "Failed to start expire job: %m");
712 return automount_start_expire(a
);
715 static int automount_start_expire(Automount
*a
) {
721 if (a
->timeout_idle_usec
== 0)
724 timeout
= MAX(a
->timeout_idle_usec
/3, USEC_PER_SEC
);
726 if (a
->expire_event_source
) {
727 r
= sd_event_source_set_time_relative(a
->expire_event_source
, timeout
);
731 return sd_event_source_set_enabled(a
->expire_event_source
, SD_EVENT_ONESHOT
);
734 r
= sd_event_add_time_relative(
735 UNIT(a
)->manager
->event
,
736 &a
->expire_event_source
,
737 CLOCK_MONOTONIC
, timeout
, 0,
738 automount_dispatch_expire
, a
);
742 (void) sd_event_source_set_description(a
->expire_event_source
, "automount-expire");
747 static void automount_stop_expire(Automount
*a
) {
750 if (!a
->expire_event_source
)
753 (void) sd_event_source_set_enabled(a
->expire_event_source
, SD_EVENT_OFF
);
756 static void automount_enter_running(Automount
*a
) {
757 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
764 /* If the user masked our unit in the meantime, fail */
765 if (UNIT(a
)->load_state
!= UNIT_LOADED
) {
766 log_unit_error(UNIT(a
), "Suppressing automount event since unit is no longer loaded.");
770 /* We don't take mount requests anymore if we are supposed to
771 * shut down anyway */
772 if (unit_stop_pending(UNIT(a
))) {
773 log_unit_debug(UNIT(a
), "Suppressing automount request since unit stop is scheduled.");
774 automount_send_ready(a
, a
->tokens
, -EHOSTDOWN
);
775 automount_send_ready(a
, a
->expire_tokens
, -EHOSTDOWN
);
779 (void) mkdir_p_label(a
->where
, a
->directory_mode
);
781 /* Before we do anything, let's see if somebody is playing games with us? */
782 if (lstat(a
->where
, &st
) < 0) {
783 log_unit_warning_errno(UNIT(a
), errno
, "Failed to stat automount point: %m");
787 /* The mount unit may have been explicitly started before we got the
788 * autofs request. Ack it to unblock anything waiting on the mount point. */
789 if (!S_ISDIR(st
.st_mode
) || st
.st_dev
!= a
->dev_id
) {
790 log_unit_info(UNIT(a
), "Automount point already active?");
791 automount_send_ready(a
, a
->tokens
, 0);
795 trigger
= UNIT_TRIGGER(UNIT(a
));
797 log_unit_error(UNIT(a
), "Unit to trigger vanished.");
801 r
= manager_add_job(UNIT(a
)->manager
, JOB_START
, trigger
, JOB_REPLACE
, NULL
, &error
, NULL
);
803 log_unit_warning(UNIT(a
), "Failed to queue mount startup job: %s", bus_error_message(&error
, r
));
807 automount_set_state(a
, AUTOMOUNT_RUNNING
);
811 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
814 static int automount_start(Unit
*u
) {
815 Automount
*a
= AUTOMOUNT(u
);
819 assert(IN_SET(a
->state
, AUTOMOUNT_DEAD
, AUTOMOUNT_FAILED
));
821 if (path_is_mount_point(a
->where
, NULL
, 0) > 0)
822 return log_unit_error_errno(u
, SYNTHETIC_ERRNO(EEXIST
), "Path %s is already a mount point, refusing start.", a
->where
);
824 r
= unit_test_trigger_loaded(u
);
828 r
= unit_acquire_invocation_id(u
);
832 a
->result
= AUTOMOUNT_SUCCESS
;
833 automount_enter_waiting(a
);
837 static int automount_stop(Unit
*u
) {
838 Automount
*a
= AUTOMOUNT(u
);
841 assert(IN_SET(a
->state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
));
843 automount_enter_dead(a
, AUTOMOUNT_SUCCESS
);
847 static int automount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
848 Automount
*a
= AUTOMOUNT(u
);
856 (void) serialize_item(f
, "state", automount_state_to_string(a
->state
));
857 (void) serialize_item(f
, "result", automount_result_to_string(a
->result
));
858 (void) serialize_item_format(f
, "dev-id", "%lu", (unsigned long) a
->dev_id
);
860 SET_FOREACH(p
, a
->tokens
)
861 (void) serialize_item_format(f
, "token", "%u", PTR_TO_UINT(p
));
862 SET_FOREACH(p
, a
->expire_tokens
)
863 (void) serialize_item_format(f
, "expire-token", "%u", PTR_TO_UINT(p
));
865 r
= serialize_fd(f
, fds
, "pipe-fd", a
->pipe_fd
);
872 static int automount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
873 Automount
*a
= AUTOMOUNT(u
);
879 if (streq(key
, "state")) {
880 AutomountState state
;
882 state
= automount_state_from_string(value
);
884 log_unit_debug(u
, "Failed to parse state value: %s", value
);
886 a
->deserialized_state
= state
;
887 } else if (streq(key
, "result")) {
890 f
= automount_result_from_string(value
);
892 log_unit_debug(u
, "Failed to parse result value: %s", value
);
893 else if (f
!= AUTOMOUNT_SUCCESS
)
896 } else if (streq(key
, "dev-id")) {
899 if (safe_atolu(value
, &d
) < 0)
900 log_unit_debug(u
, "Failed to parse dev-id value: %s", value
);
902 a
->dev_id
= (dev_t
) d
;
904 } else if (streq(key
, "token")) {
907 if (safe_atou(value
, &token
) < 0)
908 log_unit_debug(u
, "Failed to parse token value: %s", value
);
910 r
= set_ensure_put(&a
->tokens
, NULL
, UINT_TO_PTR(token
));
912 log_unit_error_errno(u
, r
, "Failed to add token to set: %m");
914 } else if (streq(key
, "expire-token")) {
917 if (safe_atou(value
, &token
) < 0)
918 log_unit_debug(u
, "Failed to parse token value: %s", value
);
920 r
= set_ensure_put(&a
->expire_tokens
, NULL
, UINT_TO_PTR(token
));
922 log_unit_error_errno(u
, r
, "Failed to add expire token to set: %m");
924 } else if (streq(key
, "pipe-fd")) {
927 if ((fd
= parse_fd(value
)) < 0 || !fdset_contains(fds
, fd
))
928 log_unit_debug(u
, "Failed to parse pipe-fd value: %s", value
);
930 safe_close(a
->pipe_fd
);
931 a
->pipe_fd
= fdset_remove(fds
, fd
);
934 log_unit_debug(u
, "Unknown serialization key: %s", key
);
939 static UnitActiveState
automount_active_state(Unit
*u
) {
942 return state_translation_table
[AUTOMOUNT(u
)->state
];
945 static const char *automount_sub_state_to_string(Unit
*u
) {
948 return automount_state_to_string(AUTOMOUNT(u
)->state
);
951 static bool automount_may_gc(Unit
*u
) {
960 return UNIT_VTABLE(t
)->may_gc(t
);
963 static int automount_dispatch_io(sd_event_source
*s
, int fd
, uint32_t events
, void *userdata
) {
964 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
965 union autofs_v5_packet_union packet
;
966 Automount
*a
= AUTOMOUNT(userdata
);
971 assert(fd
== a
->pipe_fd
);
973 if (events
& (EPOLLHUP
|EPOLLERR
)) {
974 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?");
975 automount_enter_dead(a
, AUTOMOUNT_FAILURE_UNMOUNTED
);
979 if (events
!= EPOLLIN
) {
980 log_unit_error(UNIT(a
), "Got invalid poll event %"PRIu32
" on pipe (fd=%d)", events
, fd
);
984 r
= loop_read_exact(a
->pipe_fd
, &packet
, sizeof(packet
), true);
986 log_unit_error_errno(UNIT(a
), r
, "Invalid read from pipe: %m");
990 switch (packet
.hdr
.type
) {
992 case autofs_ptype_missing_direct
:
994 if (packet
.v5_packet
.pid
> 0) {
995 _cleanup_free_
char *p
= NULL
;
997 (void) get_process_comm(packet
.v5_packet
.pid
, &p
);
998 log_unit_info(UNIT(a
), "Got automount request for %s, triggered by %"PRIu32
" (%s)", a
->where
, packet
.v5_packet
.pid
, strna(p
));
1000 log_unit_debug(UNIT(a
), "Got direct mount request on %s", a
->where
);
1002 r
= set_ensure_put(&a
->tokens
, NULL
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
));
1004 log_unit_error_errno(UNIT(a
), r
, "Failed to remember token: %m");
1008 automount_enter_running(a
);
1011 case autofs_ptype_expire_direct
:
1012 log_unit_debug(UNIT(a
), "Got direct umount request on %s", a
->where
);
1014 automount_stop_expire(a
);
1016 r
= set_ensure_put(&a
->expire_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 trigger
= UNIT_TRIGGER(UNIT(a
));
1024 log_unit_error(UNIT(a
), "Unit to trigger vanished.");
1028 r
= manager_add_job(UNIT(a
)->manager
, JOB_STOP
, trigger
, JOB_REPLACE
, NULL
, &error
, NULL
);
1030 log_unit_warning(UNIT(a
), "Failed to queue unmount job: %s", bus_error_message(&error
, r
));
1036 log_unit_error(UNIT(a
), "Received unknown automount request %i", packet
.hdr
.type
);
1043 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
1047 static void automount_shutdown(Manager
*m
) {
1050 m
->dev_autofs_fd
= safe_close(m
->dev_autofs_fd
);
1053 static void automount_reset_failed(Unit
*u
) {
1054 Automount
*a
= AUTOMOUNT(u
);
1058 if (a
->state
== AUTOMOUNT_FAILED
)
1059 automount_set_state(a
, AUTOMOUNT_DEAD
);
1061 a
->result
= AUTOMOUNT_SUCCESS
;
1064 static bool automount_supported(void) {
1065 static int supported
= -1;
1068 supported
= access("/dev/autofs", F_OK
) >= 0;
1073 static int automount_can_start(Unit
*u
) {
1074 Automount
*a
= AUTOMOUNT(u
);
1079 r
= unit_test_start_limit(u
);
1081 automount_enter_dead(a
, AUTOMOUNT_FAILURE_START_LIMIT_HIT
);
1088 static const char* const automount_result_table
[_AUTOMOUNT_RESULT_MAX
] = {
1089 [AUTOMOUNT_SUCCESS
] = "success",
1090 [AUTOMOUNT_FAILURE_RESOURCES
] = "resources",
1091 [AUTOMOUNT_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
1092 [AUTOMOUNT_FAILURE_MOUNT_START_LIMIT_HIT
] = "mount-start-limit-hit",
1093 [AUTOMOUNT_FAILURE_UNMOUNTED
] = "unmounted",
1096 DEFINE_STRING_TABLE_LOOKUP(automount_result
, AutomountResult
);
1098 const UnitVTable automount_vtable
= {
1099 .object_size
= sizeof(Automount
),
1105 .private_section
= "Automount",
1107 .can_transient
= true,
1109 .can_trigger
= true,
1110 .exclude_from_switch_root_serialization
= true,
1112 .init
= automount_init
,
1113 .load
= automount_load
,
1114 .done
= automount_done
,
1116 .coldplug
= automount_coldplug
,
1118 .dump
= automount_dump
,
1120 .start
= automount_start
,
1121 .stop
= automount_stop
,
1123 .serialize
= automount_serialize
,
1124 .deserialize_item
= automount_deserialize_item
,
1126 .active_state
= automount_active_state
,
1127 .sub_state_to_string
= automount_sub_state_to_string
,
1129 .may_gc
= automount_may_gc
,
1131 .trigger_notify
= automount_trigger_notify
,
1133 .reset_failed
= automount_reset_failed
,
1135 .bus_set_property
= bus_automount_set_property
,
1137 .shutdown
= automount_shutdown
,
1138 .supported
= automount_supported
,
1140 .status_message_formats
= {
1141 .finished_start_job
= {
1142 [JOB_DONE
] = "Set up automount %s.",
1143 [JOB_FAILED
] = "Failed to set up automount %s.",
1145 .finished_stop_job
= {
1146 [JOB_DONE
] = "Unset automount %s.",
1147 [JOB_FAILED
] = "Failed to unset automount %s.",
1151 .can_start
= automount_can_start
,