2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include <linux/auto_dev-ioctl.h>
24 #include <linux/auto_fs4.h>
25 #include <sys/epoll.h>
26 #include <sys/mount.h>
30 #include "alloc-util.h"
32 #include "automount.h"
33 #include "bus-error.h"
35 #include "dbus-automount.h"
37 #include "format-util.h"
41 #include "mount-util.h"
43 #include "parse-util.h"
44 #include "path-util.h"
45 #include "process-util.h"
47 #include "stdio-util.h"
48 #include "string-table.h"
49 #include "string-util.h"
50 #include "unit-name.h"
53 static const UnitActiveState state_translation_table
[_AUTOMOUNT_STATE_MAX
] = {
54 [AUTOMOUNT_DEAD
] = UNIT_INACTIVE
,
55 [AUTOMOUNT_WAITING
] = UNIT_ACTIVE
,
56 [AUTOMOUNT_RUNNING
] = UNIT_ACTIVE
,
57 [AUTOMOUNT_FAILED
] = UNIT_FAILED
65 static inline void expire_data_free(struct expire_data
*data
) {
69 safe_close(data
->dev_autofs_fd
);
70 safe_close(data
->ioctl_fd
);
74 DEFINE_TRIVIAL_CLEANUP_FUNC(struct expire_data
*, expire_data_free
);
76 static int open_dev_autofs(Manager
*m
);
77 static int automount_dispatch_io(sd_event_source
*s
, int fd
, uint32_t events
, void *userdata
);
78 static int automount_start_expire(Automount
*a
);
79 static void automount_stop_expire(Automount
*a
);
80 static int automount_send_ready(Automount
*a
, Set
*tokens
, int status
);
82 static void automount_init(Unit
*u
) {
83 Automount
*a
= AUTOMOUNT(u
);
86 assert(u
->load_state
== UNIT_STUB
);
89 a
->directory_mode
= 0755;
90 UNIT(a
)->ignore_on_isolate
= true;
93 static void unmount_autofs(Automount
*a
) {
101 a
->pipe_event_source
= sd_event_source_unref(a
->pipe_event_source
);
102 a
->pipe_fd
= safe_close(a
->pipe_fd
);
104 /* If we reload/reexecute things we keep the mount point around */
105 if (!IN_SET(UNIT(a
)->manager
->exit_code
, MANAGER_RELOAD
, MANAGER_REEXECUTE
)) {
107 automount_send_ready(a
, a
->tokens
, -EHOSTDOWN
);
108 automount_send_ready(a
, a
->expire_tokens
, -EHOSTDOWN
);
111 r
= repeat_unmount(a
->where
, MNT_DETACH
);
113 log_error_errno(r
, "Failed to unmount: %m");
118 static void automount_done(Unit
*u
) {
119 Automount
*a
= AUTOMOUNT(u
);
125 a
->where
= mfree(a
->where
);
127 a
->tokens
= set_free(a
->tokens
);
128 a
->expire_tokens
= set_free(a
->expire_tokens
);
130 a
->expire_event_source
= sd_event_source_unref(a
->expire_event_source
);
133 static int automount_add_mount_links(Automount
*a
) {
134 _cleanup_free_
char *parent
= NULL
;
138 parent
= dirname_malloc(a
->where
);
142 return unit_require_mounts_for(UNIT(a
), parent
);
145 static int automount_add_default_dependencies(Automount
*a
) {
150 if (!UNIT(a
)->default_dependencies
)
153 if (!MANAGER_IS_SYSTEM(UNIT(a
)->manager
))
156 r
= unit_add_two_dependencies_by_name(UNIT(a
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, NULL
, true);
163 static int automount_verify(Automount
*a
) {
164 _cleanup_free_
char *e
= NULL
;
169 if (UNIT(a
)->load_state
!= UNIT_LOADED
)
172 if (path_equal(a
->where
, "/")) {
173 log_unit_error(UNIT(a
), "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(UNIT(a
), "Failed to generate unit name from path: %m");
181 if (!unit_has_name(UNIT(a
), e
)) {
182 log_unit_error(UNIT(a
), "Where= setting doesn't match unit name. Refusing.");
189 static int automount_set_where(Automount
*a
) {
197 r
= unit_name_to_path(UNIT(a
)->id
, &a
->where
);
201 path_kill_slashes(a
->where
);
205 static int automount_load(Unit
*u
) {
206 Automount
*a
= AUTOMOUNT(u
);
210 assert(u
->load_state
== UNIT_STUB
);
212 /* Load a .automount file */
213 r
= unit_load_fragment_and_dropin_optional(u
);
217 if (u
->load_state
== UNIT_LOADED
) {
220 r
= automount_set_where(a
);
224 r
= unit_load_related_unit(u
, ".mount", &x
);
228 r
= unit_add_two_dependencies(u
, UNIT_BEFORE
, UNIT_TRIGGERS
, x
, true);
232 r
= automount_add_mount_links(a
);
236 r
= automount_add_default_dependencies(a
);
241 return automount_verify(a
);
244 static void automount_set_state(Automount
*a
, AutomountState state
) {
245 AutomountState old_state
;
248 old_state
= a
->state
;
251 if (state
!= AUTOMOUNT_RUNNING
)
252 automount_stop_expire(a
);
254 if (state
!= AUTOMOUNT_WAITING
&&
255 state
!= AUTOMOUNT_RUNNING
)
258 if (state
!= old_state
)
259 log_unit_debug(UNIT(a
), "Changed %s -> %s", automount_state_to_string(old_state
), automount_state_to_string(state
));
261 unit_notify(UNIT(a
), state_translation_table
[old_state
], state_translation_table
[state
], true);
264 static int automount_coldplug(Unit
*u
) {
265 Automount
*a
= AUTOMOUNT(u
);
269 assert(a
->state
== AUTOMOUNT_DEAD
);
271 if (a
->deserialized_state
== a
->state
)
274 if (IN_SET(a
->deserialized_state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
)) {
276 r
= automount_set_where(a
);
280 r
= open_dev_autofs(u
->manager
);
284 assert(a
->pipe_fd
>= 0);
286 r
= sd_event_add_io(u
->manager
->event
, &a
->pipe_event_source
, a
->pipe_fd
, EPOLLIN
, automount_dispatch_io
, u
);
290 (void) sd_event_source_set_description(a
->pipe_event_source
, "automount-io");
291 if (a
->deserialized_state
== AUTOMOUNT_RUNNING
) {
292 r
= automount_start_expire(a
);
294 log_unit_warning_errno(UNIT(a
), r
, "Failed to start expiration timer, ignoring: %m");
297 automount_set_state(a
, a
->deserialized_state
);
303 static void automount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
304 char time_string
[FORMAT_TIMESPAN_MAX
];
305 Automount
*a
= AUTOMOUNT(u
);
310 "%sAutomount State: %s\n"
313 "%sDirectoryMode: %04o\n"
314 "%sTimeoutIdleUSec: %s\n",
315 prefix
, automount_state_to_string(a
->state
),
316 prefix
, automount_result_to_string(a
->result
),
318 prefix
, a
->directory_mode
,
319 prefix
, format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, a
->timeout_idle_usec
, USEC_PER_SEC
));
322 static void automount_enter_dead(Automount
*a
, AutomountResult f
) {
325 if (a
->result
== AUTOMOUNT_SUCCESS
)
328 automount_set_state(a
, a
->result
!= AUTOMOUNT_SUCCESS
? AUTOMOUNT_FAILED
: AUTOMOUNT_DEAD
);
331 static int open_dev_autofs(Manager
*m
) {
332 struct autofs_dev_ioctl param
;
336 if (m
->dev_autofs_fd
>= 0)
337 return m
->dev_autofs_fd
;
339 label_fix("/dev/autofs", false, false);
341 m
->dev_autofs_fd
= open("/dev/autofs", O_CLOEXEC
|O_RDONLY
);
342 if (m
->dev_autofs_fd
< 0)
343 return log_error_errno(errno
, "Failed to open /dev/autofs: %m");
345 init_autofs_dev_ioctl(¶m
);
346 if (ioctl(m
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_VERSION
, ¶m
) < 0) {
347 m
->dev_autofs_fd
= safe_close(m
->dev_autofs_fd
);
351 log_debug("Autofs kernel version %i.%i", param
.ver_major
, param
.ver_minor
);
353 return m
->dev_autofs_fd
;
356 static int open_ioctl_fd(int dev_autofs_fd
, const char *where
, dev_t devid
) {
357 struct autofs_dev_ioctl
*param
;
360 assert(dev_autofs_fd
>= 0);
363 l
= sizeof(struct autofs_dev_ioctl
) + strlen(where
) + 1;
366 init_autofs_dev_ioctl(param
);
369 param
->openmount
.devid
= devid
;
370 strcpy(param
->path
, where
);
372 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_OPENMOUNT
, param
) < 0)
375 if (param
->ioctlfd
< 0)
378 (void) fd_cloexec(param
->ioctlfd
, true);
379 return param
->ioctlfd
;
382 static int autofs_protocol(int dev_autofs_fd
, int ioctl_fd
) {
383 uint32_t major
, minor
;
384 struct autofs_dev_ioctl param
;
386 assert(dev_autofs_fd
>= 0);
387 assert(ioctl_fd
>= 0);
389 init_autofs_dev_ioctl(¶m
);
390 param
.ioctlfd
= ioctl_fd
;
392 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOVER
, ¶m
) < 0)
395 major
= param
.protover
.version
;
397 init_autofs_dev_ioctl(¶m
);
398 param
.ioctlfd
= ioctl_fd
;
400 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOSUBVER
, ¶m
) < 0)
403 minor
= param
.protosubver
.sub_version
;
405 log_debug("Autofs protocol version %i.%i", major
, minor
);
409 static int autofs_set_timeout(int dev_autofs_fd
, int ioctl_fd
, usec_t usec
) {
410 struct autofs_dev_ioctl param
;
412 assert(dev_autofs_fd
>= 0);
413 assert(ioctl_fd
>= 0);
415 init_autofs_dev_ioctl(¶m
);
416 param
.ioctlfd
= ioctl_fd
;
418 if (usec
== USEC_INFINITY
)
419 param
.timeout
.timeout
= 0;
421 /* Convert to seconds, rounding up. */
422 param
.timeout
.timeout
= (usec
+ USEC_PER_SEC
- 1) / USEC_PER_SEC
;
424 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_TIMEOUT
, ¶m
) < 0)
430 static int autofs_send_ready(int dev_autofs_fd
, int ioctl_fd
, uint32_t token
, int status
) {
431 struct autofs_dev_ioctl param
;
433 assert(dev_autofs_fd
>= 0);
434 assert(ioctl_fd
>= 0);
436 init_autofs_dev_ioctl(¶m
);
437 param
.ioctlfd
= ioctl_fd
;
440 param
.fail
.token
= token
;
441 param
.fail
.status
= status
;
443 param
.ready
.token
= token
;
445 if (ioctl(dev_autofs_fd
, status
? AUTOFS_DEV_IOCTL_FAIL
: AUTOFS_DEV_IOCTL_READY
, ¶m
) < 0)
451 static int automount_send_ready(Automount
*a
, Set
*tokens
, int status
) {
452 _cleanup_close_
int ioctl_fd
= -1;
459 if (set_isempty(tokens
))
462 ioctl_fd
= open_ioctl_fd(UNIT(a
)->manager
->dev_autofs_fd
, a
->where
, a
->dev_id
);
467 log_unit_debug_errno(UNIT(a
), status
, "Sending failure: %m");
469 log_unit_debug(UNIT(a
), "Sending success.");
473 /* Autofs thankfully does not hand out 0 as a token */
474 while ((token
= PTR_TO_UINT(set_steal_first(tokens
)))) {
479 * if you pass a positive status code here, kernels
480 * prior to 4.12 will freeze! Yay! */
482 k
= autofs_send_ready(UNIT(a
)->manager
->dev_autofs_fd
,
493 static void automount_trigger_notify(Unit
*u
, Unit
*other
) {
494 Automount
*a
= AUTOMOUNT(u
);
500 /* Filter out invocations with bogus state */
501 if (other
->load_state
!= UNIT_LOADED
|| other
->type
!= UNIT_MOUNT
)
504 /* Don't propagate state changes from the mount if we are already down */
505 if (!IN_SET(a
->state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
))
508 /* Propagate start limit hit state */
509 if (other
->start_limit_hit
) {
510 automount_enter_dead(a
, AUTOMOUNT_FAILURE_MOUNT_START_LIMIT_HIT
);
514 /* Don't propagate anything if there's still a job queued */
518 /* The mount is successfully established */
519 if (IN_SET(MOUNT(other
)->state
, MOUNT_MOUNTED
, MOUNT_REMOUNTING
)) {
520 (void) automount_send_ready(a
, a
->tokens
, 0);
522 r
= automount_start_expire(a
);
524 log_unit_warning_errno(UNIT(a
), r
, "Failed to start expiration timer, ignoring: %m");
526 automount_set_state(a
, AUTOMOUNT_RUNNING
);
529 if (IN_SET(MOUNT(other
)->state
,
530 MOUNT_MOUNTING
, MOUNT_MOUNTING_DONE
,
531 MOUNT_MOUNTED
, MOUNT_REMOUNTING
,
532 MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
,
533 MOUNT_UNMOUNTING_SIGTERM
, MOUNT_UNMOUNTING_SIGKILL
,
536 (void) automount_send_ready(a
, a
->expire_tokens
, -ENODEV
);
539 if (MOUNT(other
)->state
== MOUNT_DEAD
)
540 (void) automount_send_ready(a
, a
->expire_tokens
, 0);
542 /* The mount is in some unhappy state now, let's unfreeze any waiting clients */
543 if (IN_SET(MOUNT(other
)->state
,
544 MOUNT_DEAD
, MOUNT_UNMOUNTING
,
545 MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
,
546 MOUNT_UNMOUNTING_SIGTERM
, MOUNT_UNMOUNTING_SIGKILL
,
549 (void) automount_send_ready(a
, a
->tokens
, -ENODEV
);
551 automount_set_state(a
, AUTOMOUNT_WAITING
);
555 static void automount_enter_waiting(Automount
*a
) {
556 _cleanup_close_
int ioctl_fd
= -1;
557 int p
[2] = { -1, -1 };
558 char name
[sizeof("systemd-")-1 + DECIMAL_STR_MAX(pid_t
) + 1];
559 char options
[sizeof("fd=,pgrp=,minproto=5,maxproto=5,direct")-1
560 + DECIMAL_STR_MAX(int) + DECIMAL_STR_MAX(gid_t
) + 1];
561 bool mounted
= false;
562 int r
, dev_autofs_fd
;
566 assert(a
->pipe_fd
< 0);
569 set_clear(a
->tokens
);
571 r
= unit_fail_if_symlink(UNIT(a
), a
->where
);
575 (void) mkdir_p_label(a
->where
, 0555);
577 unit_warn_if_dir_nonempty(UNIT(a
), a
->where
);
579 dev_autofs_fd
= open_dev_autofs(UNIT(a
)->manager
);
580 if (dev_autofs_fd
< 0) {
585 if (pipe2(p
, O_NONBLOCK
|O_CLOEXEC
) < 0) {
590 xsprintf(options
, "fd=%i,pgrp="PID_FMT
",minproto=5,maxproto=5,direct", p
[1], getpgrp());
591 xsprintf(name
, "systemd-"PID_FMT
, getpid_cached());
592 if (mount(name
, a
->where
, "autofs", 0, options
) < 0) {
599 p
[1] = safe_close(p
[1]);
601 if (stat(a
->where
, &st
) < 0) {
606 ioctl_fd
= open_ioctl_fd(dev_autofs_fd
, a
->where
, st
.st_dev
);
612 r
= autofs_protocol(dev_autofs_fd
, ioctl_fd
);
616 r
= autofs_set_timeout(dev_autofs_fd
, ioctl_fd
, a
->timeout_idle_usec
);
620 r
= sd_event_add_io(UNIT(a
)->manager
->event
, &a
->pipe_event_source
, p
[0], EPOLLIN
, automount_dispatch_io
, a
);
624 (void) sd_event_source_set_description(a
->pipe_event_source
, "automount-io");
627 a
->dev_id
= st
.st_dev
;
629 automount_set_state(a
, AUTOMOUNT_WAITING
);
634 log_unit_error_errno(UNIT(a
), r
, "Failed to initialize automounter: %m");
639 r
= repeat_unmount(a
->where
, MNT_DETACH
);
641 log_error_errno(r
, "Failed to unmount, ignoring: %m");
644 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
647 static void *expire_thread(void *p
) {
648 struct autofs_dev_ioctl param
;
649 _cleanup_(expire_data_freep
) struct expire_data
*data
= (struct expire_data
*)p
;
652 assert(data
->dev_autofs_fd
>= 0);
653 assert(data
->ioctl_fd
>= 0);
655 init_autofs_dev_ioctl(¶m
);
656 param
.ioctlfd
= data
->ioctl_fd
;
659 r
= ioctl(data
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_EXPIRE
, ¶m
);
663 log_warning_errno(errno
, "Failed to expire automount, ignoring: %m");
668 static int automount_dispatch_expire(sd_event_source
*source
, usec_t usec
, void *userdata
) {
669 Automount
*a
= AUTOMOUNT(userdata
);
670 _cleanup_(expire_data_freep
) struct expire_data
*data
= NULL
;
674 assert(source
== a
->expire_event_source
);
676 data
= new0(struct expire_data
, 1);
682 data
->dev_autofs_fd
= fcntl(UNIT(a
)->manager
->dev_autofs_fd
, F_DUPFD_CLOEXEC
, 3);
683 if (data
->dev_autofs_fd
< 0)
684 return log_unit_error_errno(UNIT(a
), errno
, "Failed to duplicate autofs fd: %m");
686 data
->ioctl_fd
= open_ioctl_fd(UNIT(a
)->manager
->dev_autofs_fd
, a
->where
, a
->dev_id
);
687 if (data
->ioctl_fd
< 0)
688 return log_unit_error_errno(UNIT(a
), data
->ioctl_fd
, "Couldn't open autofs ioctl fd: %m");
690 r
= asynchronous_job(expire_thread
, data
);
692 return log_unit_error_errno(UNIT(a
), r
, "Failed to start expire job: %m");
696 return automount_start_expire(a
);
699 static int automount_start_expire(Automount
*a
) {
705 if (a
->timeout_idle_usec
== 0)
708 timeout
= now(CLOCK_MONOTONIC
) + MAX(a
->timeout_idle_usec
/3, USEC_PER_SEC
);
710 if (a
->expire_event_source
) {
711 r
= sd_event_source_set_time(a
->expire_event_source
, timeout
);
715 return sd_event_source_set_enabled(a
->expire_event_source
, SD_EVENT_ONESHOT
);
718 r
= sd_event_add_time(
719 UNIT(a
)->manager
->event
,
720 &a
->expire_event_source
,
721 CLOCK_MONOTONIC
, timeout
, 0,
722 automount_dispatch_expire
, a
);
726 (void) sd_event_source_set_description(a
->expire_event_source
, "automount-expire");
731 static void automount_stop_expire(Automount
*a
) {
734 if (!a
->expire_event_source
)
737 (void) sd_event_source_set_enabled(a
->expire_event_source
, SD_EVENT_OFF
);
740 static void automount_enter_running(Automount
*a
) {
741 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
748 /* If the user masked our unit in the meantime, fail */
749 if (UNIT(a
)->load_state
!= UNIT_LOADED
) {
750 log_unit_error(UNIT(a
), "Suppressing automount event since unit is no longer loaded.");
754 /* We don't take mount requests anymore if we are supposed to
755 * shut down anyway */
756 if (unit_stop_pending(UNIT(a
))) {
757 log_unit_debug(UNIT(a
), "Suppressing automount request since unit stop is scheduled.");
758 automount_send_ready(a
, a
->tokens
, -EHOSTDOWN
);
759 automount_send_ready(a
, a
->expire_tokens
, -EHOSTDOWN
);
763 mkdir_p_label(a
->where
, a
->directory_mode
);
765 /* Before we do anything, let's see if somebody is playing games with us? */
766 if (lstat(a
->where
, &st
) < 0) {
767 log_unit_warning_errno(UNIT(a
), errno
, "Failed to stat automount point: %m");
771 /* The mount unit may have been explicitly started before we got the
772 * autofs request. Ack it to unblock anything waiting on the mount point. */
773 if (!S_ISDIR(st
.st_mode
) || st
.st_dev
!= a
->dev_id
) {
774 log_unit_info(UNIT(a
), "Automount point already active?");
775 automount_send_ready(a
, a
->tokens
, 0);
779 trigger
= UNIT_TRIGGER(UNIT(a
));
781 log_unit_error(UNIT(a
), "Unit to trigger vanished.");
785 r
= manager_add_job(UNIT(a
)->manager
, JOB_START
, trigger
, JOB_REPLACE
, &error
, NULL
);
787 log_unit_warning(UNIT(a
), "Failed to queue mount startup job: %s", bus_error_message(&error
, r
));
791 automount_set_state(a
, AUTOMOUNT_RUNNING
);
795 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
798 static int automount_start(Unit
*u
) {
799 Automount
*a
= AUTOMOUNT(u
);
804 assert(a
->state
== AUTOMOUNT_DEAD
|| a
->state
== AUTOMOUNT_FAILED
);
806 if (path_is_mount_point(a
->where
, NULL
, 0) > 0) {
807 log_unit_error(u
, "Path %s is already a mount point, refusing start.", a
->where
);
811 trigger
= UNIT_TRIGGER(u
);
812 if (!trigger
|| trigger
->load_state
!= UNIT_LOADED
) {
813 log_unit_error(u
, "Refusing to start, unit to trigger not loaded.");
817 r
= unit_start_limit_test(u
);
819 automount_enter_dead(a
, AUTOMOUNT_FAILURE_START_LIMIT_HIT
);
823 r
= unit_acquire_invocation_id(u
);
827 a
->result
= AUTOMOUNT_SUCCESS
;
828 automount_enter_waiting(a
);
832 static int automount_stop(Unit
*u
) {
833 Automount
*a
= AUTOMOUNT(u
);
836 assert(a
->state
== AUTOMOUNT_WAITING
|| a
->state
== AUTOMOUNT_RUNNING
);
838 automount_enter_dead(a
, AUTOMOUNT_SUCCESS
);
842 static int automount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
843 Automount
*a
= AUTOMOUNT(u
);
852 unit_serialize_item(u
, f
, "state", automount_state_to_string(a
->state
));
853 unit_serialize_item(u
, f
, "result", automount_result_to_string(a
->result
));
854 unit_serialize_item_format(u
, f
, "dev-id", "%u", (unsigned) a
->dev_id
);
856 SET_FOREACH(p
, a
->tokens
, i
)
857 unit_serialize_item_format(u
, f
, "token", "%u", PTR_TO_UINT(p
));
858 SET_FOREACH(p
, a
->expire_tokens
, i
)
859 unit_serialize_item_format(u
, f
, "expire-token", "%u", PTR_TO_UINT(p
));
861 r
= unit_serialize_item_fd(u
, f
, fds
, "pipe-fd", a
->pipe_fd
);
868 static int automount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
869 Automount
*a
= AUTOMOUNT(u
);
875 if (streq(key
, "state")) {
876 AutomountState state
;
878 state
= automount_state_from_string(value
);
880 log_unit_debug(u
, "Failed to parse state value: %s", value
);
882 a
->deserialized_state
= state
;
883 } else if (streq(key
, "result")) {
886 f
= automount_result_from_string(value
);
888 log_unit_debug(u
, "Failed to parse result value: %s", value
);
889 else if (f
!= AUTOMOUNT_SUCCESS
)
892 } else if (streq(key
, "dev-id")) {
895 if (safe_atou(value
, &d
) < 0)
896 log_unit_debug(u
, "Failed to parse dev-id value: %s", value
);
898 a
->dev_id
= (unsigned) 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_check_gc(Unit
*u
) {
961 if (!UNIT_TRIGGER(u
))
964 return UNIT_VTABLE(UNIT_TRIGGER(u
))->check_gc(UNIT_TRIGGER(u
));
967 static int automount_dispatch_io(sd_event_source
*s
, int fd
, uint32_t events
, void *userdata
) {
968 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
969 union autofs_v5_packet_union packet
;
970 Automount
*a
= AUTOMOUNT(userdata
);
975 assert(fd
== a
->pipe_fd
);
977 if (events
!= EPOLLIN
) {
978 log_unit_error(UNIT(a
), "Got invalid poll event %"PRIu32
" on pipe (fd=%d)", events
, fd
);
982 r
= loop_read_exact(a
->pipe_fd
, &packet
, sizeof(packet
), true);
984 log_unit_error_errno(UNIT(a
), r
, "Invalid read from pipe: %m");
988 switch (packet
.hdr
.type
) {
990 case autofs_ptype_missing_direct
:
992 if (packet
.v5_packet
.pid
> 0) {
993 _cleanup_free_
char *p
= NULL
;
995 get_process_comm(packet
.v5_packet
.pid
, &p
);
996 log_unit_info(UNIT(a
), "Got automount request for %s, triggered by %"PRIu32
" (%s)", a
->where
, packet
.v5_packet
.pid
, strna(p
));
998 log_unit_debug(UNIT(a
), "Got direct mount request on %s", a
->where
);
1000 r
= set_ensure_allocated(&a
->tokens
, NULL
);
1002 log_unit_error(UNIT(a
), "Failed to allocate token set.");
1006 r
= set_put(a
->tokens
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
));
1008 log_unit_error_errno(UNIT(a
), r
, "Failed to remember token: %m");
1012 automount_enter_running(a
);
1015 case autofs_ptype_expire_direct
:
1016 log_unit_debug(UNIT(a
), "Got direct umount request on %s", a
->where
);
1018 automount_stop_expire(a
);
1020 r
= set_ensure_allocated(&a
->expire_tokens
, NULL
);
1022 log_unit_error(UNIT(a
), "Failed to allocate token set.");
1026 r
= set_put(a
->expire_tokens
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
));
1028 log_unit_error_errno(UNIT(a
), r
, "Failed to remember token: %m");
1032 trigger
= UNIT_TRIGGER(UNIT(a
));
1034 log_unit_error(UNIT(a
), "Unit to trigger vanished.");
1038 r
= manager_add_job(UNIT(a
)->manager
, JOB_STOP
, trigger
, JOB_REPLACE
, &error
, NULL
);
1040 log_unit_warning(UNIT(a
), "Failed to queue umount startup job: %s", bus_error_message(&error
, r
));
1046 log_unit_error(UNIT(a
), "Received unknown automount request %i", packet
.hdr
.type
);
1053 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
1057 static void automount_shutdown(Manager
*m
) {
1060 m
->dev_autofs_fd
= safe_close(m
->dev_autofs_fd
);
1063 static void automount_reset_failed(Unit
*u
) {
1064 Automount
*a
= AUTOMOUNT(u
);
1068 if (a
->state
== AUTOMOUNT_FAILED
)
1069 automount_set_state(a
, AUTOMOUNT_DEAD
);
1071 a
->result
= AUTOMOUNT_SUCCESS
;
1074 static bool automount_supported(void) {
1075 static int supported
= -1;
1078 supported
= access("/dev/autofs", F_OK
) >= 0;
1083 static const char* const automount_result_table
[_AUTOMOUNT_RESULT_MAX
] = {
1084 [AUTOMOUNT_SUCCESS
] = "success",
1085 [AUTOMOUNT_FAILURE_RESOURCES
] = "resources",
1086 [AUTOMOUNT_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
1087 [AUTOMOUNT_FAILURE_MOUNT_START_LIMIT_HIT
] = "mount-start-limit-hit",
1090 DEFINE_STRING_TABLE_LOOKUP(automount_result
, AutomountResult
);
1092 const UnitVTable automount_vtable
= {
1093 .object_size
= sizeof(Automount
),
1100 .init
= automount_init
,
1101 .load
= automount_load
,
1102 .done
= automount_done
,
1104 .coldplug
= automount_coldplug
,
1106 .dump
= automount_dump
,
1108 .start
= automount_start
,
1109 .stop
= automount_stop
,
1111 .serialize
= automount_serialize
,
1112 .deserialize_item
= automount_deserialize_item
,
1114 .active_state
= automount_active_state
,
1115 .sub_state_to_string
= automount_sub_state_to_string
,
1117 .check_gc
= automount_check_gc
,
1119 .trigger_notify
= automount_trigger_notify
,
1121 .reset_failed
= automount_reset_failed
,
1123 .bus_vtable
= bus_automount_vtable
,
1124 .bus_set_property
= bus_automount_set_property
,
1126 .can_transient
= true,
1128 .shutdown
= automount_shutdown
,
1129 .supported
= automount_supported
,
1131 .status_message_formats
= {
1132 .finished_start_job
= {
1133 [JOB_DONE
] = "Set up automount %s.",
1134 [JOB_FAILED
] = "Failed to set up automount %s.",
1136 .finished_stop_job
= {
1137 [JOB_DONE
] = "Unset automount %s.",
1138 [JOB_FAILED
] = "Failed to unset automount %s.",