1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
9 #include <linux/auto_dev-ioctl.h>
10 #include <linux/auto_fs4.h>
11 #include <sys/epoll.h>
12 #include <sys/mount.h>
16 #include "alloc-util.h"
18 #include "automount.h"
19 #include "bus-error.h"
21 #include "dbus-automount.h"
23 #include "format-util.h"
27 #include "mount-util.h"
29 #include "parse-util.h"
30 #include "path-util.h"
31 #include "process-util.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 inline 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
->exit_code
, 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
, NULL
, 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 old_state
= a
->state
;
244 if (state
!= AUTOMOUNT_RUNNING
)
245 automount_stop_expire(a
);
247 if (!IN_SET(state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
))
250 if (state
!= old_state
)
251 log_unit_debug(UNIT(a
), "Changed %s -> %s", automount_state_to_string(old_state
), automount_state_to_string(state
));
253 unit_notify(UNIT(a
), state_translation_table
[old_state
], state_translation_table
[state
], 0);
256 static int automount_coldplug(Unit
*u
) {
257 Automount
*a
= AUTOMOUNT(u
);
261 assert(a
->state
== AUTOMOUNT_DEAD
);
263 if (a
->deserialized_state
== a
->state
)
266 if (IN_SET(a
->deserialized_state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
)) {
268 r
= automount_set_where(a
);
272 r
= open_dev_autofs(u
->manager
);
276 assert(a
->pipe_fd
>= 0);
278 r
= sd_event_add_io(u
->manager
->event
, &a
->pipe_event_source
, a
->pipe_fd
, EPOLLIN
, automount_dispatch_io
, u
);
282 (void) sd_event_source_set_description(a
->pipe_event_source
, "automount-io");
283 if (a
->deserialized_state
== AUTOMOUNT_RUNNING
) {
284 r
= automount_start_expire(a
);
286 log_unit_warning_errno(UNIT(a
), r
, "Failed to start expiration timer, ignoring: %m");
289 automount_set_state(a
, a
->deserialized_state
);
295 static void automount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
296 char time_string
[FORMAT_TIMESPAN_MAX
];
297 Automount
*a
= AUTOMOUNT(u
);
302 "%sAutomount State: %s\n"
305 "%sDirectoryMode: %04o\n"
306 "%sTimeoutIdleUSec: %s\n",
307 prefix
, automount_state_to_string(a
->state
),
308 prefix
, automount_result_to_string(a
->result
),
310 prefix
, a
->directory_mode
,
311 prefix
, format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, a
->timeout_idle_usec
, USEC_PER_SEC
));
314 static void automount_enter_dead(Automount
*a
, AutomountResult f
) {
317 if (a
->result
== AUTOMOUNT_SUCCESS
)
320 if (a
->result
!= AUTOMOUNT_SUCCESS
)
321 log_unit_warning(UNIT(a
), "Failed with result '%s'.", automount_result_to_string(a
->result
));
323 automount_set_state(a
, a
->result
!= AUTOMOUNT_SUCCESS
? AUTOMOUNT_FAILED
: AUTOMOUNT_DEAD
);
326 static int open_dev_autofs(Manager
*m
) {
327 struct autofs_dev_ioctl param
;
331 if (m
->dev_autofs_fd
>= 0)
332 return m
->dev_autofs_fd
;
334 (void) label_fix("/dev/autofs", 0);
336 m
->dev_autofs_fd
= open("/dev/autofs", O_CLOEXEC
|O_RDONLY
);
337 if (m
->dev_autofs_fd
< 0)
338 return log_error_errno(errno
, "Failed to open /dev/autofs: %m");
340 init_autofs_dev_ioctl(¶m
);
341 if (ioctl(m
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_VERSION
, ¶m
) < 0) {
342 m
->dev_autofs_fd
= safe_close(m
->dev_autofs_fd
);
346 log_debug("Autofs kernel version %i.%i", param
.ver_major
, param
.ver_minor
);
348 return m
->dev_autofs_fd
;
351 static int open_ioctl_fd(int dev_autofs_fd
, const char *where
, dev_t devid
) {
352 struct autofs_dev_ioctl
*param
;
355 assert(dev_autofs_fd
>= 0);
358 l
= sizeof(struct autofs_dev_ioctl
) + strlen(where
) + 1;
361 init_autofs_dev_ioctl(param
);
364 param
->openmount
.devid
= devid
;
365 strcpy(param
->path
, where
);
367 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_OPENMOUNT
, param
) < 0)
370 if (param
->ioctlfd
< 0)
373 (void) fd_cloexec(param
->ioctlfd
, true);
374 return param
->ioctlfd
;
377 static int autofs_protocol(int dev_autofs_fd
, int ioctl_fd
) {
378 uint32_t major
, minor
;
379 struct autofs_dev_ioctl param
;
381 assert(dev_autofs_fd
>= 0);
382 assert(ioctl_fd
>= 0);
384 init_autofs_dev_ioctl(¶m
);
385 param
.ioctlfd
= ioctl_fd
;
387 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOVER
, ¶m
) < 0)
390 major
= param
.protover
.version
;
392 init_autofs_dev_ioctl(¶m
);
393 param
.ioctlfd
= ioctl_fd
;
395 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOSUBVER
, ¶m
) < 0)
398 minor
= param
.protosubver
.sub_version
;
400 log_debug("Autofs protocol version %i.%i", major
, minor
);
404 static int autofs_set_timeout(int dev_autofs_fd
, int ioctl_fd
, usec_t usec
) {
405 struct autofs_dev_ioctl param
;
407 assert(dev_autofs_fd
>= 0);
408 assert(ioctl_fd
>= 0);
410 init_autofs_dev_ioctl(¶m
);
411 param
.ioctlfd
= ioctl_fd
;
413 if (usec
== USEC_INFINITY
)
414 param
.timeout
.timeout
= 0;
416 /* Convert to seconds, rounding up. */
417 param
.timeout
.timeout
= DIV_ROUND_UP(usec
, USEC_PER_SEC
);
419 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_TIMEOUT
, ¶m
) < 0)
425 static int autofs_send_ready(int dev_autofs_fd
, int ioctl_fd
, uint32_t token
, int status
) {
426 struct autofs_dev_ioctl param
;
428 assert(dev_autofs_fd
>= 0);
429 assert(ioctl_fd
>= 0);
431 init_autofs_dev_ioctl(¶m
);
432 param
.ioctlfd
= ioctl_fd
;
435 param
.fail
.token
= token
;
436 param
.fail
.status
= status
;
438 param
.ready
.token
= token
;
440 if (ioctl(dev_autofs_fd
, status
? AUTOFS_DEV_IOCTL_FAIL
: AUTOFS_DEV_IOCTL_READY
, ¶m
) < 0)
446 static int automount_send_ready(Automount
*a
, Set
*tokens
, int status
) {
447 _cleanup_close_
int ioctl_fd
= -1;
454 if (set_isempty(tokens
))
457 ioctl_fd
= open_ioctl_fd(UNIT(a
)->manager
->dev_autofs_fd
, a
->where
, a
->dev_id
);
462 log_unit_debug_errno(UNIT(a
), status
, "Sending failure: %m");
464 log_unit_debug(UNIT(a
), "Sending success.");
468 /* Autofs thankfully does not hand out 0 as a token */
469 while ((token
= PTR_TO_UINT(set_steal_first(tokens
)))) {
474 * if you pass a positive status code here, kernels
475 * prior to 4.12 will freeze! Yay! */
477 k
= autofs_send_ready(UNIT(a
)->manager
->dev_autofs_fd
,
488 static void automount_trigger_notify(Unit
*u
, Unit
*other
) {
489 Automount
*a
= AUTOMOUNT(u
);
495 /* Filter out invocations with bogus state */
496 if (other
->load_state
!= UNIT_LOADED
|| other
->type
!= UNIT_MOUNT
)
499 /* Don't propagate state changes from the mount if we are already down */
500 if (!IN_SET(a
->state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
))
503 /* Propagate start limit hit state */
504 if (other
->start_limit_hit
) {
505 automount_enter_dead(a
, AUTOMOUNT_FAILURE_MOUNT_START_LIMIT_HIT
);
509 /* Don't propagate anything if there's still a job queued */
513 /* The mount is successfully established */
514 if (IN_SET(MOUNT(other
)->state
, MOUNT_MOUNTED
, MOUNT_REMOUNTING
)) {
515 (void) automount_send_ready(a
, a
->tokens
, 0);
517 r
= automount_start_expire(a
);
519 log_unit_warning_errno(UNIT(a
), r
, "Failed to start expiration timer, ignoring: %m");
521 automount_set_state(a
, AUTOMOUNT_RUNNING
);
524 if (IN_SET(MOUNT(other
)->state
,
525 MOUNT_MOUNTING
, MOUNT_MOUNTING_DONE
,
526 MOUNT_MOUNTED
, MOUNT_REMOUNTING
,
527 MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
,
528 MOUNT_UNMOUNTING_SIGTERM
, MOUNT_UNMOUNTING_SIGKILL
,
531 (void) automount_send_ready(a
, a
->expire_tokens
, -ENODEV
);
534 if (MOUNT(other
)->state
== MOUNT_DEAD
)
535 (void) automount_send_ready(a
, a
->expire_tokens
, 0);
537 /* The mount is in some unhappy state now, let's unfreeze any waiting clients */
538 if (IN_SET(MOUNT(other
)->state
,
539 MOUNT_DEAD
, MOUNT_UNMOUNTING
,
540 MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
,
541 MOUNT_UNMOUNTING_SIGTERM
, MOUNT_UNMOUNTING_SIGKILL
,
544 (void) automount_send_ready(a
, a
->tokens
, -ENODEV
);
546 automount_set_state(a
, AUTOMOUNT_WAITING
);
550 static void automount_enter_waiting(Automount
*a
) {
551 _cleanup_close_
int ioctl_fd
= -1;
552 int p
[2] = { -1, -1 };
553 char name
[STRLEN("systemd-") + DECIMAL_STR_MAX(pid_t
) + 1];
554 char options
[STRLEN("fd=,pgrp=,minproto=5,maxproto=5,direct")
555 + DECIMAL_STR_MAX(int) + DECIMAL_STR_MAX(gid_t
) + 1];
556 bool mounted
= false;
557 int r
, dev_autofs_fd
;
561 assert(a
->pipe_fd
< 0);
564 set_clear(a
->tokens
);
566 r
= unit_fail_if_noncanonical(UNIT(a
), a
->where
);
570 (void) mkdir_p_label(a
->where
, 0555);
572 unit_warn_if_dir_nonempty(UNIT(a
), a
->where
);
574 dev_autofs_fd
= open_dev_autofs(UNIT(a
)->manager
);
575 if (dev_autofs_fd
< 0) {
580 if (pipe2(p
, O_NONBLOCK
|O_CLOEXEC
) < 0) {
585 xsprintf(options
, "fd=%i,pgrp="PID_FMT
",minproto=5,maxproto=5,direct", p
[1], getpgrp());
586 xsprintf(name
, "systemd-"PID_FMT
, getpid_cached());
587 if (mount(name
, a
->where
, "autofs", 0, options
) < 0) {
594 p
[1] = safe_close(p
[1]);
596 if (stat(a
->where
, &st
) < 0) {
601 ioctl_fd
= open_ioctl_fd(dev_autofs_fd
, a
->where
, st
.st_dev
);
607 r
= autofs_protocol(dev_autofs_fd
, ioctl_fd
);
611 r
= autofs_set_timeout(dev_autofs_fd
, ioctl_fd
, a
->timeout_idle_usec
);
615 r
= sd_event_add_io(UNIT(a
)->manager
->event
, &a
->pipe_event_source
, p
[0], EPOLLIN
, automount_dispatch_io
, a
);
619 (void) sd_event_source_set_description(a
->pipe_event_source
, "automount-io");
622 a
->dev_id
= st
.st_dev
;
624 automount_set_state(a
, AUTOMOUNT_WAITING
);
629 log_unit_error_errno(UNIT(a
), r
, "Failed to initialize automounter: %m");
634 r
= repeat_unmount(a
->where
, MNT_DETACH
);
636 log_error_errno(r
, "Failed to unmount, ignoring: %m");
639 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
642 static void *expire_thread(void *p
) {
643 struct autofs_dev_ioctl param
;
644 _cleanup_(expire_data_freep
) struct expire_data
*data
= (struct expire_data
*)p
;
647 assert(data
->dev_autofs_fd
>= 0);
648 assert(data
->ioctl_fd
>= 0);
650 init_autofs_dev_ioctl(¶m
);
651 param
.ioctlfd
= data
->ioctl_fd
;
654 r
= ioctl(data
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_EXPIRE
, ¶m
);
658 log_warning_errno(errno
, "Failed to expire automount, ignoring: %m");
663 static int automount_dispatch_expire(sd_event_source
*source
, usec_t usec
, void *userdata
) {
664 Automount
*a
= AUTOMOUNT(userdata
);
665 _cleanup_(expire_data_freep
) struct expire_data
*data
= NULL
;
669 assert(source
== a
->expire_event_source
);
671 data
= new0(struct expire_data
, 1);
677 data
->dev_autofs_fd
= fcntl(UNIT(a
)->manager
->dev_autofs_fd
, F_DUPFD_CLOEXEC
, 3);
678 if (data
->dev_autofs_fd
< 0)
679 return log_unit_error_errno(UNIT(a
), errno
, "Failed to duplicate autofs fd: %m");
681 data
->ioctl_fd
= open_ioctl_fd(UNIT(a
)->manager
->dev_autofs_fd
, a
->where
, a
->dev_id
);
682 if (data
->ioctl_fd
< 0)
683 return log_unit_error_errno(UNIT(a
), data
->ioctl_fd
, "Couldn't open autofs ioctl fd: %m");
685 r
= asynchronous_job(expire_thread
, data
);
687 return log_unit_error_errno(UNIT(a
), r
, "Failed to start expire job: %m");
691 return automount_start_expire(a
);
694 static int automount_start_expire(Automount
*a
) {
700 if (a
->timeout_idle_usec
== 0)
703 timeout
= now(CLOCK_MONOTONIC
) + MAX(a
->timeout_idle_usec
/3, USEC_PER_SEC
);
705 if (a
->expire_event_source
) {
706 r
= sd_event_source_set_time(a
->expire_event_source
, timeout
);
710 return sd_event_source_set_enabled(a
->expire_event_source
, SD_EVENT_ONESHOT
);
713 r
= sd_event_add_time(
714 UNIT(a
)->manager
->event
,
715 &a
->expire_event_source
,
716 CLOCK_MONOTONIC
, timeout
, 0,
717 automount_dispatch_expire
, a
);
721 (void) sd_event_source_set_description(a
->expire_event_source
, "automount-expire");
726 static void automount_stop_expire(Automount
*a
) {
729 if (!a
->expire_event_source
)
732 (void) sd_event_source_set_enabled(a
->expire_event_source
, SD_EVENT_OFF
);
735 static void automount_enter_running(Automount
*a
) {
736 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
743 /* If the user masked our unit in the meantime, fail */
744 if (UNIT(a
)->load_state
!= UNIT_LOADED
) {
745 log_unit_error(UNIT(a
), "Suppressing automount event since unit is no longer loaded.");
749 /* We don't take mount requests anymore if we are supposed to
750 * shut down anyway */
751 if (unit_stop_pending(UNIT(a
))) {
752 log_unit_debug(UNIT(a
), "Suppressing automount request since unit stop is scheduled.");
753 automount_send_ready(a
, a
->tokens
, -EHOSTDOWN
);
754 automount_send_ready(a
, a
->expire_tokens
, -EHOSTDOWN
);
758 mkdir_p_label(a
->where
, a
->directory_mode
);
760 /* Before we do anything, let's see if somebody is playing games with us? */
761 if (lstat(a
->where
, &st
) < 0) {
762 log_unit_warning_errno(UNIT(a
), errno
, "Failed to stat automount point: %m");
766 /* The mount unit may have been explicitly started before we got the
767 * autofs request. Ack it to unblock anything waiting on the mount point. */
768 if (!S_ISDIR(st
.st_mode
) || st
.st_dev
!= a
->dev_id
) {
769 log_unit_info(UNIT(a
), "Automount point already active?");
770 automount_send_ready(a
, a
->tokens
, 0);
774 trigger
= UNIT_TRIGGER(UNIT(a
));
776 log_unit_error(UNIT(a
), "Unit to trigger vanished.");
780 r
= manager_add_job(UNIT(a
)->manager
, JOB_START
, trigger
, JOB_REPLACE
, &error
, NULL
);
782 log_unit_warning(UNIT(a
), "Failed to queue mount startup job: %s", bus_error_message(&error
, r
));
786 automount_set_state(a
, AUTOMOUNT_RUNNING
);
790 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
793 static int automount_start(Unit
*u
) {
794 Automount
*a
= AUTOMOUNT(u
);
799 assert(IN_SET(a
->state
, AUTOMOUNT_DEAD
, AUTOMOUNT_FAILED
));
801 if (path_is_mount_point(a
->where
, NULL
, 0) > 0) {
802 log_unit_error(u
, "Path %s is already a mount point, refusing start.", a
->where
);
806 trigger
= UNIT_TRIGGER(u
);
807 if (!trigger
|| trigger
->load_state
!= UNIT_LOADED
) {
808 log_unit_error(u
, "Refusing to start, unit to trigger not loaded.");
812 r
= unit_start_limit_test(u
);
814 automount_enter_dead(a
, AUTOMOUNT_FAILURE_START_LIMIT_HIT
);
818 r
= unit_acquire_invocation_id(u
);
822 a
->result
= AUTOMOUNT_SUCCESS
;
823 automount_enter_waiting(a
);
827 static int automount_stop(Unit
*u
) {
828 Automount
*a
= AUTOMOUNT(u
);
831 assert(IN_SET(a
->state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
));
833 automount_enter_dead(a
, AUTOMOUNT_SUCCESS
);
837 static int automount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
838 Automount
*a
= AUTOMOUNT(u
);
847 unit_serialize_item(u
, f
, "state", automount_state_to_string(a
->state
));
848 unit_serialize_item(u
, f
, "result", automount_result_to_string(a
->result
));
849 unit_serialize_item_format(u
, f
, "dev-id", "%u", (unsigned) a
->dev_id
);
851 SET_FOREACH(p
, a
->tokens
, i
)
852 unit_serialize_item_format(u
, f
, "token", "%u", PTR_TO_UINT(p
));
853 SET_FOREACH(p
, a
->expire_tokens
, i
)
854 unit_serialize_item_format(u
, f
, "expire-token", "%u", PTR_TO_UINT(p
));
856 r
= unit_serialize_item_fd(u
, f
, fds
, "pipe-fd", a
->pipe_fd
);
863 static int automount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
864 Automount
*a
= AUTOMOUNT(u
);
870 if (streq(key
, "state")) {
871 AutomountState state
;
873 state
= automount_state_from_string(value
);
875 log_unit_debug(u
, "Failed to parse state value: %s", value
);
877 a
->deserialized_state
= state
;
878 } else if (streq(key
, "result")) {
881 f
= automount_result_from_string(value
);
883 log_unit_debug(u
, "Failed to parse result value: %s", value
);
884 else if (f
!= AUTOMOUNT_SUCCESS
)
887 } else if (streq(key
, "dev-id")) {
890 if (safe_atou(value
, &d
) < 0)
891 log_unit_debug(u
, "Failed to parse dev-id value: %s", value
);
893 a
->dev_id
= (unsigned) d
;
894 } else if (streq(key
, "token")) {
897 if (safe_atou(value
, &token
) < 0)
898 log_unit_debug(u
, "Failed to parse token value: %s", value
);
900 r
= set_ensure_allocated(&a
->tokens
, NULL
);
906 r
= set_put(a
->tokens
, UINT_TO_PTR(token
));
908 log_unit_error_errno(u
, r
, "Failed to add token to set: %m");
910 } else if (streq(key
, "expire-token")) {
913 if (safe_atou(value
, &token
) < 0)
914 log_unit_debug(u
, "Failed to parse token value: %s", value
);
916 r
= set_ensure_allocated(&a
->expire_tokens
, NULL
);
922 r
= set_put(a
->expire_tokens
, UINT_TO_PTR(token
));
924 log_unit_error_errno(u
, r
, "Failed to add expire token to set: %m");
926 } else if (streq(key
, "pipe-fd")) {
929 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
930 log_unit_debug(u
, "Failed to parse pipe-fd value: %s", value
);
932 safe_close(a
->pipe_fd
);
933 a
->pipe_fd
= fdset_remove(fds
, fd
);
936 log_unit_debug(u
, "Unknown serialization key: %s", key
);
941 static UnitActiveState
automount_active_state(Unit
*u
) {
944 return state_translation_table
[AUTOMOUNT(u
)->state
];
947 static const char *automount_sub_state_to_string(Unit
*u
) {
950 return automount_state_to_string(AUTOMOUNT(u
)->state
);
953 static bool automount_may_gc(Unit
*u
) {
962 return UNIT_VTABLE(t
)->may_gc(t
);
965 static int automount_dispatch_io(sd_event_source
*s
, int fd
, uint32_t events
, void *userdata
) {
966 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
967 union autofs_v5_packet_union packet
;
968 Automount
*a
= AUTOMOUNT(userdata
);
973 assert(fd
== a
->pipe_fd
);
975 if (events
!= EPOLLIN
) {
976 log_unit_error(UNIT(a
), "Got invalid poll event %"PRIu32
" on pipe (fd=%d)", events
, fd
);
980 r
= loop_read_exact(a
->pipe_fd
, &packet
, sizeof(packet
), true);
982 log_unit_error_errno(UNIT(a
), r
, "Invalid read from pipe: %m");
986 switch (packet
.hdr
.type
) {
988 case autofs_ptype_missing_direct
:
990 if (packet
.v5_packet
.pid
> 0) {
991 _cleanup_free_
char *p
= NULL
;
993 get_process_comm(packet
.v5_packet
.pid
, &p
);
994 log_unit_info(UNIT(a
), "Got automount request for %s, triggered by %"PRIu32
" (%s)", a
->where
, packet
.v5_packet
.pid
, strna(p
));
996 log_unit_debug(UNIT(a
), "Got direct mount request on %s", a
->where
);
998 r
= set_ensure_allocated(&a
->tokens
, NULL
);
1000 log_unit_error(UNIT(a
), "Failed to allocate token set.");
1004 r
= set_put(a
->tokens
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
));
1006 log_unit_error_errno(UNIT(a
), r
, "Failed to remember token: %m");
1010 automount_enter_running(a
);
1013 case autofs_ptype_expire_direct
:
1014 log_unit_debug(UNIT(a
), "Got direct umount request on %s", a
->where
);
1016 automount_stop_expire(a
);
1018 r
= set_ensure_allocated(&a
->expire_tokens
, NULL
);
1020 log_unit_error(UNIT(a
), "Failed to allocate token set.");
1024 r
= set_put(a
->expire_tokens
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
));
1026 log_unit_error_errno(UNIT(a
), r
, "Failed to remember token: %m");
1030 trigger
= UNIT_TRIGGER(UNIT(a
));
1032 log_unit_error(UNIT(a
), "Unit to trigger vanished.");
1036 r
= manager_add_job(UNIT(a
)->manager
, JOB_STOP
, trigger
, JOB_REPLACE
, &error
, NULL
);
1038 log_unit_warning(UNIT(a
), "Failed to queue umount startup job: %s", bus_error_message(&error
, r
));
1044 log_unit_error(UNIT(a
), "Received unknown automount request %i", packet
.hdr
.type
);
1051 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
1055 static void automount_shutdown(Manager
*m
) {
1058 m
->dev_autofs_fd
= safe_close(m
->dev_autofs_fd
);
1061 static void automount_reset_failed(Unit
*u
) {
1062 Automount
*a
= AUTOMOUNT(u
);
1066 if (a
->state
== AUTOMOUNT_FAILED
)
1067 automount_set_state(a
, AUTOMOUNT_DEAD
);
1069 a
->result
= AUTOMOUNT_SUCCESS
;
1072 static bool automount_supported(void) {
1073 static int supported
= -1;
1076 supported
= access("/dev/autofs", F_OK
) >= 0;
1081 static const char* const automount_result_table
[_AUTOMOUNT_RESULT_MAX
] = {
1082 [AUTOMOUNT_SUCCESS
] = "success",
1083 [AUTOMOUNT_FAILURE_RESOURCES
] = "resources",
1084 [AUTOMOUNT_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
1085 [AUTOMOUNT_FAILURE_MOUNT_START_LIMIT_HIT
] = "mount-start-limit-hit",
1088 DEFINE_STRING_TABLE_LOOKUP(automount_result
, AutomountResult
);
1090 const UnitVTable automount_vtable
= {
1091 .object_size
= sizeof(Automount
),
1098 .init
= automount_init
,
1099 .load
= automount_load
,
1100 .done
= automount_done
,
1102 .coldplug
= automount_coldplug
,
1104 .dump
= automount_dump
,
1106 .start
= automount_start
,
1107 .stop
= automount_stop
,
1109 .serialize
= automount_serialize
,
1110 .deserialize_item
= automount_deserialize_item
,
1112 .active_state
= automount_active_state
,
1113 .sub_state_to_string
= automount_sub_state_to_string
,
1115 .may_gc
= automount_may_gc
,
1117 .trigger_notify
= automount_trigger_notify
,
1119 .reset_failed
= automount_reset_failed
,
1121 .bus_vtable
= bus_automount_vtable
,
1122 .bus_set_property
= bus_automount_set_property
,
1124 .can_transient
= true,
1126 .shutdown
= automount_shutdown
,
1127 .supported
= automount_supported
,
1129 .status_message_formats
= {
1130 .finished_start_job
= {
1131 [JOB_DONE
] = "Set up automount %s.",
1132 [JOB_FAILED
] = "Failed to set up automount %s.",
1134 .finished_stop_job
= {
1135 [JOB_DONE
] = "Unset automount %s.",
1136 [JOB_FAILED
] = "Failed to unset automount %s.",