1 /* SPDX-License-Identifier: LGPL-2.1+ */
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"
25 #include "mount-util.h"
27 #include "mountpoint-util.h"
28 #include "parse-util.h"
29 #include "path-util.h"
30 #include "process-util.h"
31 #include "serialize.h"
33 #include "stdio-util.h"
34 #include "string-table.h"
35 #include "string-util.h"
36 #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
51 static void expire_data_free(struct expire_data
*data
) {
55 safe_close(data
->dev_autofs_fd
);
56 safe_close(data
->ioctl_fd
);
60 DEFINE_TRIVIAL_CLEANUP_FUNC(struct expire_data
*, expire_data_free
);
62 static int open_dev_autofs(Manager
*m
);
63 static int automount_dispatch_io(sd_event_source
*s
, int fd
, uint32_t events
, void *userdata
);
64 static int automount_start_expire(Automount
*a
);
65 static void automount_stop_expire(Automount
*a
);
66 static int automount_send_ready(Automount
*a
, Set
*tokens
, int status
);
68 static void automount_init(Unit
*u
) {
69 Automount
*a
= AUTOMOUNT(u
);
72 assert(u
->load_state
== UNIT_STUB
);
75 a
->directory_mode
= 0755;
76 UNIT(a
)->ignore_on_isolate
= true;
79 static void unmount_autofs(Automount
*a
) {
87 a
->pipe_event_source
= sd_event_source_unref(a
->pipe_event_source
);
88 a
->pipe_fd
= safe_close(a
->pipe_fd
);
90 /* If we reload/reexecute things we keep the mount point around */
91 if (!IN_SET(UNIT(a
)->manager
->objective
, MANAGER_RELOAD
, MANAGER_REEXECUTE
)) {
93 automount_send_ready(a
, a
->tokens
, -EHOSTDOWN
);
94 automount_send_ready(a
, a
->expire_tokens
, -EHOSTDOWN
);
97 r
= repeat_unmount(a
->where
, MNT_DETACH
);
99 log_error_errno(r
, "Failed to unmount: %m");
104 static void automount_done(Unit
*u
) {
105 Automount
*a
= AUTOMOUNT(u
);
111 a
->where
= mfree(a
->where
);
113 a
->tokens
= set_free(a
->tokens
);
114 a
->expire_tokens
= set_free(a
->expire_tokens
);
116 a
->expire_event_source
= sd_event_source_unref(a
->expire_event_source
);
119 static int automount_add_trigger_dependencies(Automount
*a
) {
125 r
= unit_load_related_unit(UNIT(a
), ".mount", &x
);
129 return unit_add_two_dependencies(UNIT(a
), UNIT_BEFORE
, UNIT_TRIGGERS
, x
, true, UNIT_DEPENDENCY_IMPLICIT
);
132 static int automount_add_mount_dependencies(Automount
*a
) {
133 _cleanup_free_
char *parent
= NULL
;
137 parent
= dirname_malloc(a
->where
);
141 return unit_require_mounts_for(UNIT(a
), parent
, UNIT_DEPENDENCY_IMPLICIT
);
144 static int automount_add_default_dependencies(Automount
*a
) {
149 if (!UNIT(a
)->default_dependencies
)
152 if (!MANAGER_IS_SYSTEM(UNIT(a
)->manager
))
155 r
= unit_add_two_dependencies_by_name(UNIT(a
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
162 static int automount_verify(Automount
*a
) {
163 _cleanup_free_
char *e
= NULL
;
168 if (UNIT(a
)->load_state
!= UNIT_LOADED
)
171 if (path_equal(a
->where
, "/")) {
172 log_unit_error(UNIT(a
), "Cannot have an automount unit for the root directory. Refusing.");
176 r
= unit_name_from_path(a
->where
, ".automount", &e
);
178 return log_unit_error_errno(UNIT(a
), r
, "Failed to generate unit name from path: %m");
180 if (!unit_has_name(UNIT(a
), e
)) {
181 log_unit_error(UNIT(a
), "Where= setting doesn't match unit name. Refusing.");
188 static int automount_set_where(Automount
*a
) {
196 r
= unit_name_to_path(UNIT(a
)->id
, &a
->where
);
200 path_simplify(a
->where
, false);
204 static int automount_load(Unit
*u
) {
205 Automount
*a
= AUTOMOUNT(u
);
209 assert(u
->load_state
== UNIT_STUB
);
211 /* Load a .automount file */
212 r
= unit_load_fragment_and_dropin(u
);
216 if (u
->load_state
== UNIT_LOADED
) {
217 r
= automount_set_where(a
);
221 r
= automount_add_trigger_dependencies(a
);
225 r
= automount_add_mount_dependencies(a
);
229 r
= automount_add_default_dependencies(a
);
234 return automount_verify(a
);
237 static void automount_set_state(Automount
*a
, AutomountState state
) {
238 AutomountState old_state
;
241 if (a
->state
!= state
)
242 bus_unit_send_pending_change_signal(UNIT(a
), false);
244 old_state
= a
->state
;
247 if (state
!= AUTOMOUNT_RUNNING
)
248 automount_stop_expire(a
);
250 if (!IN_SET(state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
))
253 if (state
!= old_state
)
254 log_unit_debug(UNIT(a
), "Changed %s -> %s", automount_state_to_string(old_state
), automount_state_to_string(state
));
256 unit_notify(UNIT(a
), state_translation_table
[old_state
], state_translation_table
[state
], 0);
259 static int automount_coldplug(Unit
*u
) {
260 Automount
*a
= AUTOMOUNT(u
);
264 assert(a
->state
== AUTOMOUNT_DEAD
);
266 if (a
->deserialized_state
== a
->state
)
269 if (IN_SET(a
->deserialized_state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
)) {
271 r
= automount_set_where(a
);
275 r
= open_dev_autofs(u
->manager
);
279 assert(a
->pipe_fd
>= 0);
281 r
= sd_event_add_io(u
->manager
->event
, &a
->pipe_event_source
, a
->pipe_fd
, EPOLLIN
, automount_dispatch_io
, u
);
285 (void) sd_event_source_set_description(a
->pipe_event_source
, "automount-io");
286 if (a
->deserialized_state
== AUTOMOUNT_RUNNING
) {
287 r
= automount_start_expire(a
);
289 log_unit_warning_errno(UNIT(a
), r
, "Failed to start expiration timer, ignoring: %m");
292 automount_set_state(a
, a
->deserialized_state
);
298 static void automount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
299 char time_string
[FORMAT_TIMESPAN_MAX
];
300 Automount
*a
= AUTOMOUNT(u
);
305 "%sAutomount State: %s\n"
308 "%sDirectoryMode: %04o\n"
309 "%sTimeoutIdleUSec: %s\n",
310 prefix
, automount_state_to_string(a
->state
),
311 prefix
, automount_result_to_string(a
->result
),
313 prefix
, a
->directory_mode
,
314 prefix
, format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, a
->timeout_idle_usec
, USEC_PER_SEC
));
317 static void automount_enter_dead(Automount
*a
, AutomountResult f
) {
320 if (a
->result
== AUTOMOUNT_SUCCESS
)
323 unit_log_result(UNIT(a
), a
->result
== AUTOMOUNT_SUCCESS
, automount_result_to_string(a
->result
));
324 automount_set_state(a
, a
->result
!= AUTOMOUNT_SUCCESS
? AUTOMOUNT_FAILED
: AUTOMOUNT_DEAD
);
327 static int open_dev_autofs(Manager
*m
) {
328 struct autofs_dev_ioctl param
;
332 if (m
->dev_autofs_fd
>= 0)
333 return m
->dev_autofs_fd
;
335 (void) label_fix("/dev/autofs", 0);
337 m
->dev_autofs_fd
= open("/dev/autofs", O_CLOEXEC
|O_RDONLY
);
338 if (m
->dev_autofs_fd
< 0)
339 return log_error_errno(errno
, "Failed to open /dev/autofs: %m");
341 init_autofs_dev_ioctl(¶m
);
342 if (ioctl(m
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_VERSION
, ¶m
) < 0) {
343 m
->dev_autofs_fd
= safe_close(m
->dev_autofs_fd
);
347 log_debug("Autofs kernel version %i.%i", param
.ver_major
, param
.ver_minor
);
349 return m
->dev_autofs_fd
;
352 static int open_ioctl_fd(int dev_autofs_fd
, const char *where
, dev_t devid
) {
353 struct autofs_dev_ioctl
*param
;
356 assert(dev_autofs_fd
>= 0);
359 l
= sizeof(struct autofs_dev_ioctl
) + strlen(where
) + 1;
362 init_autofs_dev_ioctl(param
);
365 param
->openmount
.devid
= devid
;
366 strcpy(param
->path
, where
);
368 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_OPENMOUNT
, param
) < 0)
371 if (param
->ioctlfd
< 0)
374 (void) fd_cloexec(param
->ioctlfd
, true);
375 return param
->ioctlfd
;
378 static int autofs_protocol(int dev_autofs_fd
, int ioctl_fd
) {
379 uint32_t major
, minor
;
380 struct autofs_dev_ioctl param
;
382 assert(dev_autofs_fd
>= 0);
383 assert(ioctl_fd
>= 0);
385 init_autofs_dev_ioctl(¶m
);
386 param
.ioctlfd
= ioctl_fd
;
388 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOVER
, ¶m
) < 0)
391 major
= param
.protover
.version
;
393 init_autofs_dev_ioctl(¶m
);
394 param
.ioctlfd
= ioctl_fd
;
396 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOSUBVER
, ¶m
) < 0)
399 minor
= param
.protosubver
.sub_version
;
401 log_debug("Autofs protocol version %i.%i", major
, minor
);
405 static int autofs_set_timeout(int dev_autofs_fd
, int ioctl_fd
, usec_t usec
) {
406 struct autofs_dev_ioctl param
;
408 assert(dev_autofs_fd
>= 0);
409 assert(ioctl_fd
>= 0);
411 init_autofs_dev_ioctl(¶m
);
412 param
.ioctlfd
= ioctl_fd
;
414 if (usec
== USEC_INFINITY
)
415 param
.timeout
.timeout
= 0;
417 /* Convert to seconds, rounding up. */
418 param
.timeout
.timeout
= DIV_ROUND_UP(usec
, USEC_PER_SEC
);
420 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_TIMEOUT
, ¶m
) < 0)
426 static int autofs_send_ready(int dev_autofs_fd
, int ioctl_fd
, uint32_t token
, int status
) {
427 struct autofs_dev_ioctl param
;
429 assert(dev_autofs_fd
>= 0);
430 assert(ioctl_fd
>= 0);
432 init_autofs_dev_ioctl(¶m
);
433 param
.ioctlfd
= ioctl_fd
;
436 param
.fail
.token
= token
;
437 param
.fail
.status
= status
;
439 param
.ready
.token
= token
;
441 if (ioctl(dev_autofs_fd
, status
? AUTOFS_DEV_IOCTL_FAIL
: AUTOFS_DEV_IOCTL_READY
, ¶m
) < 0)
447 static int automount_send_ready(Automount
*a
, Set
*tokens
, int status
) {
448 _cleanup_close_
int ioctl_fd
= -1;
455 if (set_isempty(tokens
))
458 ioctl_fd
= open_ioctl_fd(UNIT(a
)->manager
->dev_autofs_fd
, a
->where
, a
->dev_id
);
463 log_unit_debug_errno(UNIT(a
), status
, "Sending failure: %m");
465 log_unit_debug(UNIT(a
), "Sending success.");
469 /* Autofs thankfully does not hand out 0 as a token */
470 while ((token
= PTR_TO_UINT(set_steal_first(tokens
)))) {
475 * if you pass a positive status code here, kernels
476 * prior to 4.12 will freeze! Yay! */
478 k
= autofs_send_ready(UNIT(a
)->manager
->dev_autofs_fd
,
489 static void automount_trigger_notify(Unit
*u
, Unit
*other
) {
490 Automount
*a
= AUTOMOUNT(u
);
496 /* Filter out invocations with bogus state */
497 if (other
->load_state
!= UNIT_LOADED
|| other
->type
!= UNIT_MOUNT
)
500 /* Don't propagate state changes from the mount if we are already down */
501 if (!IN_SET(a
->state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
))
504 /* Propagate start limit hit state */
505 if (other
->start_limit_hit
) {
506 automount_enter_dead(a
, AUTOMOUNT_FAILURE_MOUNT_START_LIMIT_HIT
);
510 /* Don't propagate anything if there's still a job queued */
514 /* The mount is successfully established */
515 if (IN_SET(MOUNT(other
)->state
, MOUNT_MOUNTED
, MOUNT_REMOUNTING
)) {
516 (void) automount_send_ready(a
, a
->tokens
, 0);
518 r
= automount_start_expire(a
);
520 log_unit_warning_errno(UNIT(a
), r
, "Failed to start expiration timer, ignoring: %m");
522 automount_set_state(a
, AUTOMOUNT_RUNNING
);
525 if (IN_SET(MOUNT(other
)->state
,
526 MOUNT_MOUNTING
, MOUNT_MOUNTING_DONE
,
527 MOUNT_MOUNTED
, MOUNT_REMOUNTING
,
528 MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
,
529 MOUNT_UNMOUNTING_SIGTERM
, MOUNT_UNMOUNTING_SIGKILL
,
532 (void) automount_send_ready(a
, a
->expire_tokens
, -ENODEV
);
535 if (MOUNT(other
)->state
== MOUNT_DEAD
)
536 (void) automount_send_ready(a
, a
->expire_tokens
, 0);
538 /* The mount is in some unhappy state now, let's unfreeze any waiting clients */
539 if (IN_SET(MOUNT(other
)->state
,
540 MOUNT_DEAD
, MOUNT_UNMOUNTING
,
541 MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
,
542 MOUNT_UNMOUNTING_SIGTERM
, MOUNT_UNMOUNTING_SIGKILL
,
545 (void) automount_send_ready(a
, a
->tokens
, -ENODEV
);
547 automount_set_state(a
, AUTOMOUNT_WAITING
);
551 static void automount_enter_waiting(Automount
*a
) {
552 _cleanup_close_
int ioctl_fd
= -1;
553 int p
[2] = { -1, -1 };
554 char name
[STRLEN("systemd-") + DECIMAL_STR_MAX(pid_t
) + 1];
555 char options
[STRLEN("fd=,pgrp=,minproto=5,maxproto=5,direct")
556 + DECIMAL_STR_MAX(int) + DECIMAL_STR_MAX(gid_t
) + 1];
557 bool mounted
= false;
558 int r
, dev_autofs_fd
;
562 assert(a
->pipe_fd
< 0);
565 set_clear(a
->tokens
);
567 r
= unit_fail_if_noncanonical(UNIT(a
), a
->where
);
571 (void) mkdir_p_label(a
->where
, 0555);
573 unit_warn_if_dir_nonempty(UNIT(a
), a
->where
);
575 dev_autofs_fd
= open_dev_autofs(UNIT(a
)->manager
);
576 if (dev_autofs_fd
< 0) {
581 if (pipe2(p
, O_CLOEXEC
) < 0) {
585 r
= fd_nonblock(p
[0], true);
589 xsprintf(options
, "fd=%i,pgrp="PID_FMT
",minproto=5,maxproto=5,direct", p
[1], getpgrp());
590 xsprintf(name
, "systemd-"PID_FMT
, getpid_cached());
591 if (mount(name
, a
->where
, "autofs", 0, options
) < 0) {
598 p
[1] = safe_close(p
[1]);
600 if (stat(a
->where
, &st
) < 0) {
605 ioctl_fd
= open_ioctl_fd(dev_autofs_fd
, a
->where
, st
.st_dev
);
611 r
= autofs_protocol(dev_autofs_fd
, ioctl_fd
);
615 r
= autofs_set_timeout(dev_autofs_fd
, ioctl_fd
, a
->timeout_idle_usec
);
619 r
= sd_event_add_io(UNIT(a
)->manager
->event
, &a
->pipe_event_source
, p
[0], EPOLLIN
, automount_dispatch_io
, a
);
623 (void) sd_event_source_set_description(a
->pipe_event_source
, "automount-io");
626 a
->dev_id
= st
.st_dev
;
628 automount_set_state(a
, AUTOMOUNT_WAITING
);
633 log_unit_error_errno(UNIT(a
), r
, "Failed to initialize automounter: %m");
638 r
= repeat_unmount(a
->where
, MNT_DETACH
);
640 log_error_errno(r
, "Failed to unmount, ignoring: %m");
643 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
646 static void *expire_thread(void *p
) {
647 struct autofs_dev_ioctl param
;
648 _cleanup_(expire_data_freep
) struct expire_data
*data
= (struct expire_data
*)p
;
651 assert(data
->dev_autofs_fd
>= 0);
652 assert(data
->ioctl_fd
>= 0);
654 init_autofs_dev_ioctl(¶m
);
655 param
.ioctlfd
= data
->ioctl_fd
;
658 r
= ioctl(data
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_EXPIRE
, ¶m
);
662 log_warning_errno(errno
, "Failed to expire automount, ignoring: %m");
667 static int automount_dispatch_expire(sd_event_source
*source
, usec_t usec
, void *userdata
) {
668 Automount
*a
= AUTOMOUNT(userdata
);
669 _cleanup_(expire_data_freep
) struct expire_data
*data
= NULL
;
673 assert(source
== a
->expire_event_source
);
675 data
= new0(struct expire_data
, 1);
681 data
->dev_autofs_fd
= fcntl(UNIT(a
)->manager
->dev_autofs_fd
, F_DUPFD_CLOEXEC
, 3);
682 if (data
->dev_autofs_fd
< 0)
683 return log_unit_error_errno(UNIT(a
), errno
, "Failed to duplicate autofs fd: %m");
685 data
->ioctl_fd
= open_ioctl_fd(UNIT(a
)->manager
->dev_autofs_fd
, a
->where
, a
->dev_id
);
686 if (data
->ioctl_fd
< 0)
687 return log_unit_error_errno(UNIT(a
), data
->ioctl_fd
, "Couldn't open autofs ioctl fd: %m");
689 r
= asynchronous_job(expire_thread
, data
);
691 return log_unit_error_errno(UNIT(a
), r
, "Failed to start expire job: %m");
695 return automount_start_expire(a
);
698 static int automount_start_expire(Automount
*a
) {
704 if (a
->timeout_idle_usec
== 0)
707 timeout
= now(CLOCK_MONOTONIC
) + MAX(a
->timeout_idle_usec
/3, USEC_PER_SEC
);
709 if (a
->expire_event_source
) {
710 r
= sd_event_source_set_time(a
->expire_event_source
, timeout
);
714 return sd_event_source_set_enabled(a
->expire_event_source
, SD_EVENT_ONESHOT
);
717 r
= sd_event_add_time(
718 UNIT(a
)->manager
->event
,
719 &a
->expire_event_source
,
720 CLOCK_MONOTONIC
, timeout
, 0,
721 automount_dispatch_expire
, a
);
725 (void) sd_event_source_set_description(a
->expire_event_source
, "automount-expire");
730 static void automount_stop_expire(Automount
*a
) {
733 if (!a
->expire_event_source
)
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 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
, 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
= AUTOMOUNT(u
);
803 assert(IN_SET(a
->state
, AUTOMOUNT_DEAD
, AUTOMOUNT_FAILED
));
805 if (path_is_mount_point(a
->where
, NULL
, 0) > 0) {
806 log_unit_error(u
, "Path %s is already a mount point, refusing start.", a
->where
);
810 trigger
= UNIT_TRIGGER(u
);
811 if (!trigger
|| trigger
->load_state
!= UNIT_LOADED
) {
812 log_unit_error(u
, "Refusing to start, unit to trigger not loaded.");
816 r
= unit_start_limit_test(u
);
818 automount_enter_dead(a
, AUTOMOUNT_FAILURE_START_LIMIT_HIT
);
822 r
= unit_acquire_invocation_id(u
);
826 a
->result
= AUTOMOUNT_SUCCESS
;
827 automount_enter_waiting(a
);
831 static int automount_stop(Unit
*u
) {
832 Automount
*a
= AUTOMOUNT(u
);
835 assert(IN_SET(a
->state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
));
837 automount_enter_dead(a
, AUTOMOUNT_SUCCESS
);
841 static int automount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
842 Automount
*a
= AUTOMOUNT(u
);
851 (void) serialize_item(f
, "state", automount_state_to_string(a
->state
));
852 (void) serialize_item(f
, "result", automount_result_to_string(a
->result
));
853 (void) serialize_item_format(f
, "dev-id", "%lu", (unsigned long) a
->dev_id
);
855 SET_FOREACH(p
, a
->tokens
, i
)
856 (void) serialize_item_format(f
, "token", "%u", PTR_TO_UINT(p
));
857 SET_FOREACH(p
, a
->expire_tokens
, i
)
858 (void) serialize_item_format(f
, "expire-token", "%u", PTR_TO_UINT(p
));
860 r
= serialize_fd(f
, fds
, "pipe-fd", a
->pipe_fd
);
867 static int automount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
868 Automount
*a
= AUTOMOUNT(u
);
874 if (streq(key
, "state")) {
875 AutomountState state
;
877 state
= automount_state_from_string(value
);
879 log_unit_debug(u
, "Failed to parse state value: %s", value
);
881 a
->deserialized_state
= state
;
882 } else if (streq(key
, "result")) {
885 f
= automount_result_from_string(value
);
887 log_unit_debug(u
, "Failed to parse result value: %s", value
);
888 else if (f
!= AUTOMOUNT_SUCCESS
)
891 } else if (streq(key
, "dev-id")) {
894 if (safe_atolu(value
, &d
) < 0)
895 log_unit_debug(u
, "Failed to parse dev-id value: %s", value
);
897 a
->dev_id
= (dev_t
) d
;
899 } else if (streq(key
, "token")) {
902 if (safe_atou(value
, &token
) < 0)
903 log_unit_debug(u
, "Failed to parse token value: %s", value
);
905 r
= set_ensure_allocated(&a
->tokens
, NULL
);
911 r
= set_put(a
->tokens
, UINT_TO_PTR(token
));
913 log_unit_error_errno(u
, r
, "Failed to add token to set: %m");
915 } else if (streq(key
, "expire-token")) {
918 if (safe_atou(value
, &token
) < 0)
919 log_unit_debug(u
, "Failed to parse token value: %s", value
);
921 r
= set_ensure_allocated(&a
->expire_tokens
, NULL
);
927 r
= set_put(a
->expire_tokens
, UINT_TO_PTR(token
));
929 log_unit_error_errno(u
, r
, "Failed to add expire token to set: %m");
931 } else if (streq(key
, "pipe-fd")) {
934 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
935 log_unit_debug(u
, "Failed to parse pipe-fd value: %s", value
);
937 safe_close(a
->pipe_fd
);
938 a
->pipe_fd
= fdset_remove(fds
, fd
);
941 log_unit_debug(u
, "Unknown serialization key: %s", key
);
946 static UnitActiveState
automount_active_state(Unit
*u
) {
949 return state_translation_table
[AUTOMOUNT(u
)->state
];
952 static const char *automount_sub_state_to_string(Unit
*u
) {
955 return automount_state_to_string(AUTOMOUNT(u
)->state
);
958 static bool automount_may_gc(Unit
*u
) {
967 return UNIT_VTABLE(t
)->may_gc(t
);
970 static int automount_dispatch_io(sd_event_source
*s
, int fd
, uint32_t events
, void *userdata
) {
971 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
972 union autofs_v5_packet_union packet
;
973 Automount
*a
= AUTOMOUNT(userdata
);
978 assert(fd
== a
->pipe_fd
);
980 if (events
!= EPOLLIN
) {
981 log_unit_error(UNIT(a
), "Got invalid poll event %"PRIu32
" on pipe (fd=%d)", events
, fd
);
985 r
= loop_read_exact(a
->pipe_fd
, &packet
, sizeof(packet
), true);
987 log_unit_error_errno(UNIT(a
), r
, "Invalid read from pipe: %m");
991 switch (packet
.hdr
.type
) {
993 case autofs_ptype_missing_direct
:
995 if (packet
.v5_packet
.pid
> 0) {
996 _cleanup_free_
char *p
= NULL
;
998 get_process_comm(packet
.v5_packet
.pid
, &p
);
999 log_unit_info(UNIT(a
), "Got automount request for %s, triggered by %"PRIu32
" (%s)", a
->where
, packet
.v5_packet
.pid
, strna(p
));
1001 log_unit_debug(UNIT(a
), "Got direct mount request on %s", a
->where
);
1003 r
= set_ensure_allocated(&a
->tokens
, NULL
);
1005 log_unit_error(UNIT(a
), "Failed to allocate token set.");
1009 r
= set_put(a
->tokens
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
));
1011 log_unit_error_errno(UNIT(a
), r
, "Failed to remember token: %m");
1015 automount_enter_running(a
);
1018 case autofs_ptype_expire_direct
:
1019 log_unit_debug(UNIT(a
), "Got direct umount request on %s", a
->where
);
1021 automount_stop_expire(a
);
1023 r
= set_ensure_allocated(&a
->expire_tokens
, NULL
);
1025 log_unit_error(UNIT(a
), "Failed to allocate token set.");
1029 r
= set_put(a
->expire_tokens
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
));
1031 log_unit_error_errno(UNIT(a
), r
, "Failed to remember token: %m");
1035 trigger
= UNIT_TRIGGER(UNIT(a
));
1037 log_unit_error(UNIT(a
), "Unit to trigger vanished.");
1041 r
= manager_add_job(UNIT(a
)->manager
, JOB_STOP
, trigger
, JOB_REPLACE
, &error
, NULL
);
1043 log_unit_warning(UNIT(a
), "Failed to queue umount startup job: %s", bus_error_message(&error
, r
));
1049 log_unit_error(UNIT(a
), "Received unknown automount request %i", packet
.hdr
.type
);
1056 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
1060 static void automount_shutdown(Manager
*m
) {
1063 m
->dev_autofs_fd
= safe_close(m
->dev_autofs_fd
);
1066 static void automount_reset_failed(Unit
*u
) {
1067 Automount
*a
= AUTOMOUNT(u
);
1071 if (a
->state
== AUTOMOUNT_FAILED
)
1072 automount_set_state(a
, AUTOMOUNT_DEAD
);
1074 a
->result
= AUTOMOUNT_SUCCESS
;
1077 static bool automount_supported(void) {
1078 static int supported
= -1;
1081 supported
= access("/dev/autofs", F_OK
) >= 0;
1086 static const char* const automount_result_table
[_AUTOMOUNT_RESULT_MAX
] = {
1087 [AUTOMOUNT_SUCCESS
] = "success",
1088 [AUTOMOUNT_FAILURE_RESOURCES
] = "resources",
1089 [AUTOMOUNT_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
1090 [AUTOMOUNT_FAILURE_MOUNT_START_LIMIT_HIT
] = "mount-start-limit-hit",
1093 DEFINE_STRING_TABLE_LOOKUP(automount_result
, AutomountResult
);
1095 const UnitVTable automount_vtable
= {
1096 .object_size
= sizeof(Automount
),
1103 .init
= automount_init
,
1104 .load
= automount_load
,
1105 .done
= automount_done
,
1107 .coldplug
= automount_coldplug
,
1109 .dump
= automount_dump
,
1111 .start
= automount_start
,
1112 .stop
= automount_stop
,
1114 .serialize
= automount_serialize
,
1115 .deserialize_item
= automount_deserialize_item
,
1117 .active_state
= automount_active_state
,
1118 .sub_state_to_string
= automount_sub_state_to_string
,
1120 .may_gc
= automount_may_gc
,
1122 .trigger_notify
= automount_trigger_notify
,
1124 .reset_failed
= automount_reset_failed
,
1126 .bus_vtable
= bus_automount_vtable
,
1127 .bus_set_property
= bus_automount_set_property
,
1129 .can_transient
= true,
1131 .shutdown
= automount_shutdown
,
1132 .supported
= automount_supported
,
1134 .status_message_formats
= {
1135 .finished_start_job
= {
1136 [JOB_DONE
] = "Set up automount %s.",
1137 [JOB_FAILED
] = "Failed to set up automount %s.",
1139 .finished_stop_job
= {
1140 [JOB_DONE
] = "Unset automount %s.",
1141 [JOB_FAILED
] = "Failed to unset automount %s.",