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"
20 #include "format-util.h"
24 #include "mount-util.h"
26 #include "parse-util.h"
27 #include "path-util.h"
28 #include "process-util.h"
30 #include "stdio-util.h"
31 #include "string-table.h"
32 #include "string-util.h"
33 #include "unit-name.h"
36 static const UnitActiveState state_translation_table
[_AUTOMOUNT_STATE_MAX
] = {
37 [AUTOMOUNT_DEAD
] = UNIT_INACTIVE
,
38 [AUTOMOUNT_WAITING
] = UNIT_ACTIVE
,
39 [AUTOMOUNT_RUNNING
] = UNIT_ACTIVE
,
40 [AUTOMOUNT_FAILED
] = UNIT_FAILED
48 static inline void expire_data_free(struct expire_data
*data
) {
52 safe_close(data
->dev_autofs_fd
);
53 safe_close(data
->ioctl_fd
);
57 DEFINE_TRIVIAL_CLEANUP_FUNC(struct expire_data
*, expire_data_free
);
59 static int open_dev_autofs(Manager
*m
);
60 static int automount_dispatch_io(sd_event_source
*s
, int fd
, uint32_t events
, void *userdata
);
61 static int automount_start_expire(Automount
*a
);
62 static void automount_stop_expire(Automount
*a
);
63 static int automount_send_ready(Automount
*a
, Set
*tokens
, int status
);
65 static void automount_init(Unit
*u
) {
66 Automount
*a
= AUTOMOUNT(u
);
69 assert(u
->load_state
== UNIT_STUB
);
72 a
->directory_mode
= 0755;
73 UNIT(a
)->ignore_on_isolate
= true;
76 static void unmount_autofs(Automount
*a
) {
84 a
->pipe_event_source
= sd_event_source_unref(a
->pipe_event_source
);
85 a
->pipe_fd
= safe_close(a
->pipe_fd
);
87 /* If we reload/reexecute things we keep the mount point around */
88 if (!IN_SET(UNIT(a
)->manager
->objective
, MANAGER_RELOAD
, MANAGER_REEXECUTE
)) {
90 automount_send_ready(a
, a
->tokens
, -EHOSTDOWN
);
91 automount_send_ready(a
, a
->expire_tokens
, -EHOSTDOWN
);
94 r
= repeat_unmount(a
->where
, MNT_DETACH
);
96 log_error_errno(r
, "Failed to unmount: %m");
101 static void automount_done(Unit
*u
) {
102 Automount
*a
= AUTOMOUNT(u
);
108 a
->where
= mfree(a
->where
);
110 a
->tokens
= set_free(a
->tokens
);
111 a
->expire_tokens
= set_free(a
->expire_tokens
);
113 a
->expire_event_source
= sd_event_source_unref(a
->expire_event_source
);
116 static int automount_add_trigger_dependencies(Automount
*a
) {
122 r
= unit_load_related_unit(UNIT(a
), ".mount", &x
);
126 return unit_add_two_dependencies(UNIT(a
), UNIT_BEFORE
, UNIT_TRIGGERS
, x
, true, UNIT_DEPENDENCY_IMPLICIT
);
129 static int automount_add_mount_dependencies(Automount
*a
) {
130 _cleanup_free_
char *parent
= NULL
;
134 parent
= dirname_malloc(a
->where
);
138 return unit_require_mounts_for(UNIT(a
), parent
, UNIT_DEPENDENCY_IMPLICIT
);
141 static int automount_add_default_dependencies(Automount
*a
) {
146 if (!UNIT(a
)->default_dependencies
)
149 if (!MANAGER_IS_SYSTEM(UNIT(a
)->manager
))
152 r
= unit_add_two_dependencies_by_name(UNIT(a
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
159 static int automount_verify(Automount
*a
) {
160 _cleanup_free_
char *e
= NULL
;
165 if (UNIT(a
)->load_state
!= UNIT_LOADED
)
168 if (path_equal(a
->where
, "/")) {
169 log_unit_error(UNIT(a
), "Cannot have an automount unit for the root directory. Refusing.");
173 r
= unit_name_from_path(a
->where
, ".automount", &e
);
175 return log_unit_error_errno(UNIT(a
), r
, "Failed to generate unit name from path: %m");
177 if (!unit_has_name(UNIT(a
), e
)) {
178 log_unit_error(UNIT(a
), "Where= setting doesn't match unit name. Refusing.");
185 static int automount_set_where(Automount
*a
) {
193 r
= unit_name_to_path(UNIT(a
)->id
, &a
->where
);
197 path_simplify(a
->where
, false);
201 static int automount_load(Unit
*u
) {
202 Automount
*a
= AUTOMOUNT(u
);
206 assert(u
->load_state
== UNIT_STUB
);
208 /* Load a .automount file */
209 r
= unit_load_fragment_and_dropin(u
);
213 if (u
->load_state
== UNIT_LOADED
) {
214 r
= automount_set_where(a
);
218 r
= automount_add_trigger_dependencies(a
);
222 r
= automount_add_mount_dependencies(a
);
226 r
= automount_add_default_dependencies(a
);
231 return automount_verify(a
);
234 static void automount_set_state(Automount
*a
, AutomountState state
) {
235 AutomountState old_state
;
238 old_state
= a
->state
;
241 if (state
!= AUTOMOUNT_RUNNING
)
242 automount_stop_expire(a
);
244 if (!IN_SET(state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
))
247 if (state
!= old_state
)
248 log_unit_debug(UNIT(a
), "Changed %s -> %s", automount_state_to_string(old_state
), automount_state_to_string(state
));
250 unit_notify(UNIT(a
), state_translation_table
[old_state
], state_translation_table
[state
], 0);
253 static int automount_coldplug(Unit
*u
) {
254 Automount
*a
= AUTOMOUNT(u
);
258 assert(a
->state
== AUTOMOUNT_DEAD
);
260 if (a
->deserialized_state
== a
->state
)
263 if (IN_SET(a
->deserialized_state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
)) {
265 r
= automount_set_where(a
);
269 r
= open_dev_autofs(u
->manager
);
273 assert(a
->pipe_fd
>= 0);
275 r
= sd_event_add_io(u
->manager
->event
, &a
->pipe_event_source
, a
->pipe_fd
, EPOLLIN
, automount_dispatch_io
, u
);
279 (void) sd_event_source_set_description(a
->pipe_event_source
, "automount-io");
280 if (a
->deserialized_state
== AUTOMOUNT_RUNNING
) {
281 r
= automount_start_expire(a
);
283 log_unit_warning_errno(UNIT(a
), r
, "Failed to start expiration timer, ignoring: %m");
286 automount_set_state(a
, a
->deserialized_state
);
292 static void automount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
293 char time_string
[FORMAT_TIMESPAN_MAX
];
294 Automount
*a
= AUTOMOUNT(u
);
299 "%sAutomount State: %s\n"
302 "%sDirectoryMode: %04o\n"
303 "%sTimeoutIdleUSec: %s\n",
304 prefix
, automount_state_to_string(a
->state
),
305 prefix
, automount_result_to_string(a
->result
),
307 prefix
, a
->directory_mode
,
308 prefix
, format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, a
->timeout_idle_usec
, USEC_PER_SEC
));
311 static void automount_enter_dead(Automount
*a
, AutomountResult f
) {
314 if (a
->result
== AUTOMOUNT_SUCCESS
)
317 if (a
->result
!= AUTOMOUNT_SUCCESS
)
318 log_unit_warning(UNIT(a
), "Failed with result '%s'.", automount_result_to_string(a
->result
));
320 automount_set_state(a
, a
->result
!= AUTOMOUNT_SUCCESS
? AUTOMOUNT_FAILED
: AUTOMOUNT_DEAD
);
323 static int open_dev_autofs(Manager
*m
) {
324 struct autofs_dev_ioctl param
;
328 if (m
->dev_autofs_fd
>= 0)
329 return m
->dev_autofs_fd
;
331 (void) label_fix("/dev/autofs", 0);
333 m
->dev_autofs_fd
= open("/dev/autofs", O_CLOEXEC
|O_RDONLY
);
334 if (m
->dev_autofs_fd
< 0)
335 return log_error_errno(errno
, "Failed to open /dev/autofs: %m");
337 init_autofs_dev_ioctl(¶m
);
338 if (ioctl(m
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_VERSION
, ¶m
) < 0) {
339 m
->dev_autofs_fd
= safe_close(m
->dev_autofs_fd
);
343 log_debug("Autofs kernel version %i.%i", param
.ver_major
, param
.ver_minor
);
345 return m
->dev_autofs_fd
;
348 static int open_ioctl_fd(int dev_autofs_fd
, const char *where
, dev_t devid
) {
349 struct autofs_dev_ioctl
*param
;
352 assert(dev_autofs_fd
>= 0);
355 l
= sizeof(struct autofs_dev_ioctl
) + strlen(where
) + 1;
358 init_autofs_dev_ioctl(param
);
361 param
->openmount
.devid
= devid
;
362 strcpy(param
->path
, where
);
364 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_OPENMOUNT
, param
) < 0)
367 if (param
->ioctlfd
< 0)
370 (void) fd_cloexec(param
->ioctlfd
, true);
371 return param
->ioctlfd
;
374 static int autofs_protocol(int dev_autofs_fd
, int ioctl_fd
) {
375 uint32_t major
, minor
;
376 struct autofs_dev_ioctl param
;
378 assert(dev_autofs_fd
>= 0);
379 assert(ioctl_fd
>= 0);
381 init_autofs_dev_ioctl(¶m
);
382 param
.ioctlfd
= ioctl_fd
;
384 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOVER
, ¶m
) < 0)
387 major
= param
.protover
.version
;
389 init_autofs_dev_ioctl(¶m
);
390 param
.ioctlfd
= ioctl_fd
;
392 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOSUBVER
, ¶m
) < 0)
395 minor
= param
.protosubver
.sub_version
;
397 log_debug("Autofs protocol version %i.%i", major
, minor
);
401 static int autofs_set_timeout(int dev_autofs_fd
, int ioctl_fd
, usec_t usec
) {
402 struct autofs_dev_ioctl param
;
404 assert(dev_autofs_fd
>= 0);
405 assert(ioctl_fd
>= 0);
407 init_autofs_dev_ioctl(¶m
);
408 param
.ioctlfd
= ioctl_fd
;
410 if (usec
== USEC_INFINITY
)
411 param
.timeout
.timeout
= 0;
413 /* Convert to seconds, rounding up. */
414 param
.timeout
.timeout
= DIV_ROUND_UP(usec
, USEC_PER_SEC
);
416 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_TIMEOUT
, ¶m
) < 0)
422 static int autofs_send_ready(int dev_autofs_fd
, int ioctl_fd
, uint32_t token
, int status
) {
423 struct autofs_dev_ioctl param
;
425 assert(dev_autofs_fd
>= 0);
426 assert(ioctl_fd
>= 0);
428 init_autofs_dev_ioctl(¶m
);
429 param
.ioctlfd
= ioctl_fd
;
432 param
.fail
.token
= token
;
433 param
.fail
.status
= status
;
435 param
.ready
.token
= token
;
437 if (ioctl(dev_autofs_fd
, status
? AUTOFS_DEV_IOCTL_FAIL
: AUTOFS_DEV_IOCTL_READY
, ¶m
) < 0)
443 static int automount_send_ready(Automount
*a
, Set
*tokens
, int status
) {
444 _cleanup_close_
int ioctl_fd
= -1;
451 if (set_isempty(tokens
))
454 ioctl_fd
= open_ioctl_fd(UNIT(a
)->manager
->dev_autofs_fd
, a
->where
, a
->dev_id
);
459 log_unit_debug_errno(UNIT(a
), status
, "Sending failure: %m");
461 log_unit_debug(UNIT(a
), "Sending success.");
465 /* Autofs thankfully does not hand out 0 as a token */
466 while ((token
= PTR_TO_UINT(set_steal_first(tokens
)))) {
471 * if you pass a positive status code here, kernels
472 * prior to 4.12 will freeze! Yay! */
474 k
= autofs_send_ready(UNIT(a
)->manager
->dev_autofs_fd
,
485 static void automount_trigger_notify(Unit
*u
, Unit
*other
) {
486 Automount
*a
= AUTOMOUNT(u
);
492 /* Filter out invocations with bogus state */
493 if (other
->load_state
!= UNIT_LOADED
|| other
->type
!= UNIT_MOUNT
)
496 /* Don't propagate state changes from the mount if we are already down */
497 if (!IN_SET(a
->state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
))
500 /* Propagate start limit hit state */
501 if (other
->start_limit_hit
) {
502 automount_enter_dead(a
, AUTOMOUNT_FAILURE_MOUNT_START_LIMIT_HIT
);
506 /* Don't propagate anything if there's still a job queued */
510 /* The mount is successfully established */
511 if (IN_SET(MOUNT(other
)->state
, MOUNT_MOUNTED
, MOUNT_REMOUNTING
)) {
512 (void) automount_send_ready(a
, a
->tokens
, 0);
514 r
= automount_start_expire(a
);
516 log_unit_warning_errno(UNIT(a
), r
, "Failed to start expiration timer, ignoring: %m");
518 automount_set_state(a
, AUTOMOUNT_RUNNING
);
521 if (IN_SET(MOUNT(other
)->state
,
522 MOUNT_MOUNTING
, MOUNT_MOUNTING_DONE
,
523 MOUNT_MOUNTED
, MOUNT_REMOUNTING
,
524 MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
,
525 MOUNT_UNMOUNTING_SIGTERM
, MOUNT_UNMOUNTING_SIGKILL
,
528 (void) automount_send_ready(a
, a
->expire_tokens
, -ENODEV
);
531 if (MOUNT(other
)->state
== MOUNT_DEAD
)
532 (void) automount_send_ready(a
, a
->expire_tokens
, 0);
534 /* The mount is in some unhappy state now, let's unfreeze any waiting clients */
535 if (IN_SET(MOUNT(other
)->state
,
536 MOUNT_DEAD
, MOUNT_UNMOUNTING
,
537 MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
,
538 MOUNT_UNMOUNTING_SIGTERM
, MOUNT_UNMOUNTING_SIGKILL
,
541 (void) automount_send_ready(a
, a
->tokens
, -ENODEV
);
543 automount_set_state(a
, AUTOMOUNT_WAITING
);
547 static void automount_enter_waiting(Automount
*a
) {
548 _cleanup_close_
int ioctl_fd
= -1;
549 int p
[2] = { -1, -1 };
550 char name
[STRLEN("systemd-") + DECIMAL_STR_MAX(pid_t
) + 1];
551 char options
[STRLEN("fd=,pgrp=,minproto=5,maxproto=5,direct")
552 + DECIMAL_STR_MAX(int) + DECIMAL_STR_MAX(gid_t
) + 1];
553 bool mounted
= false;
554 int r
, dev_autofs_fd
;
558 assert(a
->pipe_fd
< 0);
561 set_clear(a
->tokens
);
563 r
= unit_fail_if_noncanonical(UNIT(a
), a
->where
);
567 (void) mkdir_p_label(a
->where
, 0555);
569 unit_warn_if_dir_nonempty(UNIT(a
), a
->where
);
571 dev_autofs_fd
= open_dev_autofs(UNIT(a
)->manager
);
572 if (dev_autofs_fd
< 0) {
577 if (pipe2(p
, O_NONBLOCK
|O_CLOEXEC
) < 0) {
582 xsprintf(options
, "fd=%i,pgrp="PID_FMT
",minproto=5,maxproto=5,direct", p
[1], getpgrp());
583 xsprintf(name
, "systemd-"PID_FMT
, getpid_cached());
584 if (mount(name
, a
->where
, "autofs", 0, options
) < 0) {
591 p
[1] = safe_close(p
[1]);
593 if (stat(a
->where
, &st
) < 0) {
598 ioctl_fd
= open_ioctl_fd(dev_autofs_fd
, a
->where
, st
.st_dev
);
604 r
= autofs_protocol(dev_autofs_fd
, ioctl_fd
);
608 r
= autofs_set_timeout(dev_autofs_fd
, ioctl_fd
, a
->timeout_idle_usec
);
612 r
= sd_event_add_io(UNIT(a
)->manager
->event
, &a
->pipe_event_source
, p
[0], EPOLLIN
, automount_dispatch_io
, a
);
616 (void) sd_event_source_set_description(a
->pipe_event_source
, "automount-io");
619 a
->dev_id
= st
.st_dev
;
621 automount_set_state(a
, AUTOMOUNT_WAITING
);
626 log_unit_error_errno(UNIT(a
), r
, "Failed to initialize automounter: %m");
631 r
= repeat_unmount(a
->where
, MNT_DETACH
);
633 log_error_errno(r
, "Failed to unmount, ignoring: %m");
636 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
639 static void *expire_thread(void *p
) {
640 struct autofs_dev_ioctl param
;
641 _cleanup_(expire_data_freep
) struct expire_data
*data
= (struct expire_data
*)p
;
644 assert(data
->dev_autofs_fd
>= 0);
645 assert(data
->ioctl_fd
>= 0);
647 init_autofs_dev_ioctl(¶m
);
648 param
.ioctlfd
= data
->ioctl_fd
;
651 r
= ioctl(data
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_EXPIRE
, ¶m
);
655 log_warning_errno(errno
, "Failed to expire automount, ignoring: %m");
660 static int automount_dispatch_expire(sd_event_source
*source
, usec_t usec
, void *userdata
) {
661 Automount
*a
= AUTOMOUNT(userdata
);
662 _cleanup_(expire_data_freep
) struct expire_data
*data
= NULL
;
666 assert(source
== a
->expire_event_source
);
668 data
= new0(struct expire_data
, 1);
674 data
->dev_autofs_fd
= fcntl(UNIT(a
)->manager
->dev_autofs_fd
, F_DUPFD_CLOEXEC
, 3);
675 if (data
->dev_autofs_fd
< 0)
676 return log_unit_error_errno(UNIT(a
), errno
, "Failed to duplicate autofs fd: %m");
678 data
->ioctl_fd
= open_ioctl_fd(UNIT(a
)->manager
->dev_autofs_fd
, a
->where
, a
->dev_id
);
679 if (data
->ioctl_fd
< 0)
680 return log_unit_error_errno(UNIT(a
), data
->ioctl_fd
, "Couldn't open autofs ioctl fd: %m");
682 r
= asynchronous_job(expire_thread
, data
);
684 return log_unit_error_errno(UNIT(a
), r
, "Failed to start expire job: %m");
688 return automount_start_expire(a
);
691 static int automount_start_expire(Automount
*a
) {
697 if (a
->timeout_idle_usec
== 0)
700 timeout
= now(CLOCK_MONOTONIC
) + MAX(a
->timeout_idle_usec
/3, USEC_PER_SEC
);
702 if (a
->expire_event_source
) {
703 r
= sd_event_source_set_time(a
->expire_event_source
, timeout
);
707 return sd_event_source_set_enabled(a
->expire_event_source
, SD_EVENT_ONESHOT
);
710 r
= sd_event_add_time(
711 UNIT(a
)->manager
->event
,
712 &a
->expire_event_source
,
713 CLOCK_MONOTONIC
, timeout
, 0,
714 automount_dispatch_expire
, a
);
718 (void) sd_event_source_set_description(a
->expire_event_source
, "automount-expire");
723 static void automount_stop_expire(Automount
*a
) {
726 if (!a
->expire_event_source
)
729 (void) sd_event_source_set_enabled(a
->expire_event_source
, SD_EVENT_OFF
);
732 static void automount_enter_running(Automount
*a
) {
733 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
740 /* If the user masked our unit in the meantime, fail */
741 if (UNIT(a
)->load_state
!= UNIT_LOADED
) {
742 log_unit_error(UNIT(a
), "Suppressing automount event since unit is no longer loaded.");
746 /* We don't take mount requests anymore if we are supposed to
747 * shut down anyway */
748 if (unit_stop_pending(UNIT(a
))) {
749 log_unit_debug(UNIT(a
), "Suppressing automount request since unit stop is scheduled.");
750 automount_send_ready(a
, a
->tokens
, -EHOSTDOWN
);
751 automount_send_ready(a
, a
->expire_tokens
, -EHOSTDOWN
);
755 mkdir_p_label(a
->where
, a
->directory_mode
);
757 /* Before we do anything, let's see if somebody is playing games with us? */
758 if (lstat(a
->where
, &st
) < 0) {
759 log_unit_warning_errno(UNIT(a
), errno
, "Failed to stat automount point: %m");
763 /* The mount unit may have been explicitly started before we got the
764 * autofs request. Ack it to unblock anything waiting on the mount point. */
765 if (!S_ISDIR(st
.st_mode
) || st
.st_dev
!= a
->dev_id
) {
766 log_unit_info(UNIT(a
), "Automount point already active?");
767 automount_send_ready(a
, a
->tokens
, 0);
771 trigger
= UNIT_TRIGGER(UNIT(a
));
773 log_unit_error(UNIT(a
), "Unit to trigger vanished.");
777 r
= manager_add_job(UNIT(a
)->manager
, JOB_START
, trigger
, JOB_REPLACE
, &error
, NULL
);
779 log_unit_warning(UNIT(a
), "Failed to queue mount startup job: %s", bus_error_message(&error
, r
));
783 automount_set_state(a
, AUTOMOUNT_RUNNING
);
787 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
790 static int automount_start(Unit
*u
) {
791 Automount
*a
= AUTOMOUNT(u
);
796 assert(IN_SET(a
->state
, AUTOMOUNT_DEAD
, AUTOMOUNT_FAILED
));
798 if (path_is_mount_point(a
->where
, NULL
, 0) > 0) {
799 log_unit_error(u
, "Path %s is already a mount point, refusing start.", a
->where
);
803 trigger
= UNIT_TRIGGER(u
);
804 if (!trigger
|| trigger
->load_state
!= UNIT_LOADED
) {
805 log_unit_error(u
, "Refusing to start, unit to trigger not loaded.");
809 r
= unit_start_limit_test(u
);
811 automount_enter_dead(a
, AUTOMOUNT_FAILURE_START_LIMIT_HIT
);
815 r
= unit_acquire_invocation_id(u
);
819 a
->result
= AUTOMOUNT_SUCCESS
;
820 automount_enter_waiting(a
);
824 static int automount_stop(Unit
*u
) {
825 Automount
*a
= AUTOMOUNT(u
);
828 assert(IN_SET(a
->state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
));
830 automount_enter_dead(a
, AUTOMOUNT_SUCCESS
);
834 static int automount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
835 Automount
*a
= AUTOMOUNT(u
);
844 unit_serialize_item(u
, f
, "state", automount_state_to_string(a
->state
));
845 unit_serialize_item(u
, f
, "result", automount_result_to_string(a
->result
));
846 unit_serialize_item_format(u
, f
, "dev-id", "%u", (unsigned) a
->dev_id
);
848 SET_FOREACH(p
, a
->tokens
, i
)
849 unit_serialize_item_format(u
, f
, "token", "%u", PTR_TO_UINT(p
));
850 SET_FOREACH(p
, a
->expire_tokens
, i
)
851 unit_serialize_item_format(u
, f
, "expire-token", "%u", PTR_TO_UINT(p
));
853 r
= unit_serialize_item_fd(u
, f
, fds
, "pipe-fd", a
->pipe_fd
);
860 static int automount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
861 Automount
*a
= AUTOMOUNT(u
);
867 if (streq(key
, "state")) {
868 AutomountState state
;
870 state
= automount_state_from_string(value
);
872 log_unit_debug(u
, "Failed to parse state value: %s", value
);
874 a
->deserialized_state
= state
;
875 } else if (streq(key
, "result")) {
878 f
= automount_result_from_string(value
);
880 log_unit_debug(u
, "Failed to parse result value: %s", value
);
881 else if (f
!= AUTOMOUNT_SUCCESS
)
884 } else if (streq(key
, "dev-id")) {
887 if (safe_atou(value
, &d
) < 0)
888 log_unit_debug(u
, "Failed to parse dev-id value: %s", value
);
890 a
->dev_id
= (unsigned) d
;
891 } else if (streq(key
, "token")) {
894 if (safe_atou(value
, &token
) < 0)
895 log_unit_debug(u
, "Failed to parse token value: %s", value
);
897 r
= set_ensure_allocated(&a
->tokens
, NULL
);
903 r
= set_put(a
->tokens
, UINT_TO_PTR(token
));
905 log_unit_error_errno(u
, r
, "Failed to add token to set: %m");
907 } else if (streq(key
, "expire-token")) {
910 if (safe_atou(value
, &token
) < 0)
911 log_unit_debug(u
, "Failed to parse token value: %s", value
);
913 r
= set_ensure_allocated(&a
->expire_tokens
, NULL
);
919 r
= set_put(a
->expire_tokens
, UINT_TO_PTR(token
));
921 log_unit_error_errno(u
, r
, "Failed to add expire token to set: %m");
923 } else if (streq(key
, "pipe-fd")) {
926 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
927 log_unit_debug(u
, "Failed to parse pipe-fd value: %s", value
);
929 safe_close(a
->pipe_fd
);
930 a
->pipe_fd
= fdset_remove(fds
, fd
);
933 log_unit_debug(u
, "Unknown serialization key: %s", key
);
938 static UnitActiveState
automount_active_state(Unit
*u
) {
941 return state_translation_table
[AUTOMOUNT(u
)->state
];
944 static const char *automount_sub_state_to_string(Unit
*u
) {
947 return automount_state_to_string(AUTOMOUNT(u
)->state
);
950 static bool automount_may_gc(Unit
*u
) {
959 return UNIT_VTABLE(t
)->may_gc(t
);
962 static int automount_dispatch_io(sd_event_source
*s
, int fd
, uint32_t events
, void *userdata
) {
963 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
964 union autofs_v5_packet_union packet
;
965 Automount
*a
= AUTOMOUNT(userdata
);
970 assert(fd
== a
->pipe_fd
);
972 if (events
!= EPOLLIN
) {
973 log_unit_error(UNIT(a
), "Got invalid poll event %"PRIu32
" on pipe (fd=%d)", events
, fd
);
977 r
= loop_read_exact(a
->pipe_fd
, &packet
, sizeof(packet
), true);
979 log_unit_error_errno(UNIT(a
), r
, "Invalid read from pipe: %m");
983 switch (packet
.hdr
.type
) {
985 case autofs_ptype_missing_direct
:
987 if (packet
.v5_packet
.pid
> 0) {
988 _cleanup_free_
char *p
= NULL
;
990 get_process_comm(packet
.v5_packet
.pid
, &p
);
991 log_unit_info(UNIT(a
), "Got automount request for %s, triggered by %"PRIu32
" (%s)", a
->where
, packet
.v5_packet
.pid
, strna(p
));
993 log_unit_debug(UNIT(a
), "Got direct mount request on %s", a
->where
);
995 r
= set_ensure_allocated(&a
->tokens
, NULL
);
997 log_unit_error(UNIT(a
), "Failed to allocate token set.");
1001 r
= set_put(a
->tokens
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
));
1003 log_unit_error_errno(UNIT(a
), r
, "Failed to remember token: %m");
1007 automount_enter_running(a
);
1010 case autofs_ptype_expire_direct
:
1011 log_unit_debug(UNIT(a
), "Got direct umount request on %s", a
->where
);
1013 automount_stop_expire(a
);
1015 r
= set_ensure_allocated(&a
->expire_tokens
, NULL
);
1017 log_unit_error(UNIT(a
), "Failed to allocate token set.");
1021 r
= set_put(a
->expire_tokens
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
));
1023 log_unit_error_errno(UNIT(a
), r
, "Failed to remember token: %m");
1027 trigger
= UNIT_TRIGGER(UNIT(a
));
1029 log_unit_error(UNIT(a
), "Unit to trigger vanished.");
1033 r
= manager_add_job(UNIT(a
)->manager
, JOB_STOP
, trigger
, JOB_REPLACE
, &error
, NULL
);
1035 log_unit_warning(UNIT(a
), "Failed to queue umount startup job: %s", bus_error_message(&error
, r
));
1041 log_unit_error(UNIT(a
), "Received unknown automount request %i", packet
.hdr
.type
);
1048 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
1052 static void automount_shutdown(Manager
*m
) {
1055 m
->dev_autofs_fd
= safe_close(m
->dev_autofs_fd
);
1058 static void automount_reset_failed(Unit
*u
) {
1059 Automount
*a
= AUTOMOUNT(u
);
1063 if (a
->state
== AUTOMOUNT_FAILED
)
1064 automount_set_state(a
, AUTOMOUNT_DEAD
);
1066 a
->result
= AUTOMOUNT_SUCCESS
;
1069 static bool automount_supported(void) {
1070 static int supported
= -1;
1073 supported
= access("/dev/autofs", F_OK
) >= 0;
1078 static const char* const automount_result_table
[_AUTOMOUNT_RESULT_MAX
] = {
1079 [AUTOMOUNT_SUCCESS
] = "success",
1080 [AUTOMOUNT_FAILURE_RESOURCES
] = "resources",
1081 [AUTOMOUNT_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
1082 [AUTOMOUNT_FAILURE_MOUNT_START_LIMIT_HIT
] = "mount-start-limit-hit",
1085 DEFINE_STRING_TABLE_LOOKUP(automount_result
, AutomountResult
);
1087 const UnitVTable automount_vtable
= {
1088 .object_size
= sizeof(Automount
),
1095 .init
= automount_init
,
1096 .load
= automount_load
,
1097 .done
= automount_done
,
1099 .coldplug
= automount_coldplug
,
1101 .dump
= automount_dump
,
1103 .start
= automount_start
,
1104 .stop
= automount_stop
,
1106 .serialize
= automount_serialize
,
1107 .deserialize_item
= automount_deserialize_item
,
1109 .active_state
= automount_active_state
,
1110 .sub_state_to_string
= automount_sub_state_to_string
,
1112 .may_gc
= automount_may_gc
,
1114 .trigger_notify
= automount_trigger_notify
,
1116 .reset_failed
= automount_reset_failed
,
1118 .bus_vtable
= bus_automount_vtable
,
1119 .bus_set_property
= bus_automount_set_property
,
1121 .can_transient
= true,
1123 .shutdown
= automount_shutdown
,
1124 .supported
= automount_supported
,
1126 .status_message_formats
= {
1127 .finished_start_job
= {
1128 [JOB_DONE
] = "Set up automount %s.",
1129 [JOB_FAILED
] = "Failed to set up automount %s.",
1131 .finished_stop_job
= {
1132 [JOB_DONE
] = "Unset automount %s.",
1133 [JOB_FAILED
] = "Failed to unset automount %s.",