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 "formats-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
107 (UNIT(a
)->manager
->exit_code
!= MANAGER_RELOAD
&&
108 UNIT(a
)->manager
->exit_code
!= MANAGER_REEXECUTE
)) {
109 automount_send_ready(a
, a
->tokens
, -EHOSTDOWN
);
110 automount_send_ready(a
, a
->expire_tokens
, -EHOSTDOWN
);
112 r
= repeat_unmount(a
->where
, MNT_DETACH
);
114 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_load(Unit
*u
) {
190 Automount
*a
= AUTOMOUNT(u
);
194 assert(u
->load_state
== UNIT_STUB
);
196 /* Load a .automount file */
197 r
= unit_load_fragment_and_dropin_optional(u
);
201 if (u
->load_state
== UNIT_LOADED
) {
205 r
= unit_name_to_path(u
->id
, &a
->where
);
210 path_kill_slashes(a
->where
);
212 r
= unit_load_related_unit(u
, ".mount", &x
);
216 r
= unit_add_two_dependencies(u
, UNIT_BEFORE
, UNIT_TRIGGERS
, x
, true);
220 r
= automount_add_mount_links(a
);
224 r
= automount_add_default_dependencies(a
);
229 return automount_verify(a
);
232 static void automount_set_state(Automount
*a
, AutomountState state
) {
233 AutomountState old_state
;
236 old_state
= a
->state
;
239 if (state
!= AUTOMOUNT_RUNNING
)
240 automount_stop_expire(a
);
242 if (state
!= AUTOMOUNT_WAITING
&&
243 state
!= AUTOMOUNT_RUNNING
)
246 if (state
!= old_state
)
247 log_unit_debug(UNIT(a
), "Changed %s -> %s", automount_state_to_string(old_state
), automount_state_to_string(state
));
249 unit_notify(UNIT(a
), state_translation_table
[old_state
], state_translation_table
[state
], true);
252 static int automount_coldplug(Unit
*u
) {
253 Automount
*a
= AUTOMOUNT(u
);
257 assert(a
->state
== AUTOMOUNT_DEAD
);
259 if (a
->deserialized_state
!= a
->state
) {
261 r
= open_dev_autofs(u
->manager
);
265 if (a
->deserialized_state
== AUTOMOUNT_WAITING
||
266 a
->deserialized_state
== AUTOMOUNT_RUNNING
) {
267 assert(a
->pipe_fd
>= 0);
269 r
= sd_event_add_io(u
->manager
->event
, &a
->pipe_event_source
, a
->pipe_fd
, EPOLLIN
, automount_dispatch_io
, u
);
273 (void) sd_event_source_set_description(a
->pipe_event_source
, "automount-io");
276 automount_set_state(a
, a
->deserialized_state
);
282 static void automount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
283 char time_string
[FORMAT_TIMESPAN_MAX
];
284 Automount
*a
= AUTOMOUNT(u
);
289 "%sAutomount State: %s\n"
292 "%sDirectoryMode: %04o\n"
293 "%sTimeoutIdleUSec: %s\n",
294 prefix
, automount_state_to_string(a
->state
),
295 prefix
, automount_result_to_string(a
->result
),
297 prefix
, a
->directory_mode
,
298 prefix
, format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, a
->timeout_idle_usec
, USEC_PER_SEC
));
301 static void automount_enter_dead(Automount
*a
, AutomountResult f
) {
304 if (f
!= AUTOMOUNT_SUCCESS
)
307 automount_set_state(a
, a
->result
!= AUTOMOUNT_SUCCESS
? AUTOMOUNT_FAILED
: AUTOMOUNT_DEAD
);
310 static int open_dev_autofs(Manager
*m
) {
311 struct autofs_dev_ioctl param
;
315 if (m
->dev_autofs_fd
>= 0)
316 return m
->dev_autofs_fd
;
318 label_fix("/dev/autofs", false, false);
320 m
->dev_autofs_fd
= open("/dev/autofs", O_CLOEXEC
|O_RDONLY
);
321 if (m
->dev_autofs_fd
< 0)
322 return log_error_errno(errno
, "Failed to open /dev/autofs: %m");
324 init_autofs_dev_ioctl(¶m
);
325 if (ioctl(m
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_VERSION
, ¶m
) < 0) {
326 m
->dev_autofs_fd
= safe_close(m
->dev_autofs_fd
);
330 log_debug("Autofs kernel version %i.%i", param
.ver_major
, param
.ver_minor
);
332 return m
->dev_autofs_fd
;
335 static int open_ioctl_fd(int dev_autofs_fd
, const char *where
, dev_t devid
) {
336 struct autofs_dev_ioctl
*param
;
339 assert(dev_autofs_fd
>= 0);
342 l
= sizeof(struct autofs_dev_ioctl
) + strlen(where
) + 1;
345 init_autofs_dev_ioctl(param
);
348 param
->openmount
.devid
= devid
;
349 strcpy(param
->path
, where
);
351 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_OPENMOUNT
, param
) < 0)
354 if (param
->ioctlfd
< 0)
357 (void) fd_cloexec(param
->ioctlfd
, true);
358 return param
->ioctlfd
;
361 static int autofs_protocol(int dev_autofs_fd
, int ioctl_fd
) {
362 uint32_t major
, minor
;
363 struct autofs_dev_ioctl param
;
365 assert(dev_autofs_fd
>= 0);
366 assert(ioctl_fd
>= 0);
368 init_autofs_dev_ioctl(¶m
);
369 param
.ioctlfd
= ioctl_fd
;
371 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOVER
, ¶m
) < 0)
374 major
= param
.protover
.version
;
376 init_autofs_dev_ioctl(¶m
);
377 param
.ioctlfd
= ioctl_fd
;
379 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOSUBVER
, ¶m
) < 0)
382 minor
= param
.protosubver
.sub_version
;
384 log_debug("Autofs protocol version %i.%i", major
, minor
);
388 static int autofs_set_timeout(int dev_autofs_fd
, int ioctl_fd
, usec_t usec
) {
389 struct autofs_dev_ioctl param
;
391 assert(dev_autofs_fd
>= 0);
392 assert(ioctl_fd
>= 0);
394 init_autofs_dev_ioctl(¶m
);
395 param
.ioctlfd
= ioctl_fd
;
397 /* Convert to seconds, rounding up. */
398 param
.timeout
.timeout
= (usec
+ USEC_PER_SEC
- 1) / USEC_PER_SEC
;
400 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_TIMEOUT
, ¶m
) < 0)
406 static int autofs_send_ready(int dev_autofs_fd
, int ioctl_fd
, uint32_t token
, int status
) {
407 struct autofs_dev_ioctl param
;
409 assert(dev_autofs_fd
>= 0);
410 assert(ioctl_fd
>= 0);
412 init_autofs_dev_ioctl(¶m
);
413 param
.ioctlfd
= ioctl_fd
;
416 param
.fail
.token
= token
;
417 param
.fail
.status
= status
;
419 param
.ready
.token
= token
;
421 if (ioctl(dev_autofs_fd
, status
? AUTOFS_DEV_IOCTL_FAIL
: AUTOFS_DEV_IOCTL_READY
, ¶m
) < 0)
427 static int automount_send_ready(Automount
*a
, Set
*tokens
, int status
) {
428 _cleanup_close_
int ioctl_fd
= -1;
435 if (set_isempty(tokens
))
438 ioctl_fd
= open_ioctl_fd(UNIT(a
)->manager
->dev_autofs_fd
, a
->where
, a
->dev_id
);
443 log_unit_debug_errno(UNIT(a
), status
, "Sending failure: %m");
445 log_unit_debug(UNIT(a
), "Sending success.");
449 /* Autofs thankfully does not hand out 0 as a token */
450 while ((token
= PTR_TO_UINT(set_steal_first(tokens
)))) {
453 /* Autofs fun fact II:
455 * if you pass a positive status code here, the kernel will
458 k
= autofs_send_ready(UNIT(a
)->manager
->dev_autofs_fd
,
469 static void automount_trigger_notify(Unit
*u
, Unit
*other
) {
470 Automount
*a
= AUTOMOUNT(u
);
476 /* Filter out invocations with bogus state */
477 if (other
->load_state
!= UNIT_LOADED
|| other
->type
!= UNIT_MOUNT
)
480 /* Don't propagate state changes from the mount if we are already down */
481 if (!IN_SET(a
->state
, AUTOMOUNT_WAITING
, AUTOMOUNT_RUNNING
))
484 /* Propagate start limit hit state */
485 if (other
->start_limit_hit
) {
486 automount_enter_dead(a
, AUTOMOUNT_FAILURE_MOUNT_START_LIMIT_HIT
);
490 /* Don't propagate anything if there's still a job queued */
494 /* The mount is successfully established */
495 if (IN_SET(MOUNT(other
)->state
, MOUNT_MOUNTED
, MOUNT_REMOUNTING
)) {
496 (void) automount_send_ready(a
, a
->tokens
, 0);
498 r
= automount_start_expire(a
);
500 log_unit_warning_errno(UNIT(a
), r
, "Failed to start expiration timer, ignoring: %m");
502 automount_set_state(a
, AUTOMOUNT_RUNNING
);
505 if (IN_SET(MOUNT(other
)->state
,
506 MOUNT_MOUNTING
, MOUNT_MOUNTING_DONE
,
507 MOUNT_MOUNTED
, MOUNT_REMOUNTING
,
508 MOUNT_MOUNTING_SIGTERM
, MOUNT_MOUNTING_SIGKILL
,
509 MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
,
510 MOUNT_UNMOUNTING_SIGTERM
, MOUNT_UNMOUNTING_SIGKILL
,
513 (void) automount_send_ready(a
, a
->expire_tokens
, -ENODEV
);
516 if (MOUNT(other
)->state
== MOUNT_DEAD
)
517 (void) automount_send_ready(a
, a
->expire_tokens
, 0);
519 /* The mount is in some unhappy state now, let's unfreeze any waiting clients */
520 if (IN_SET(MOUNT(other
)->state
,
521 MOUNT_DEAD
, MOUNT_UNMOUNTING
,
522 MOUNT_MOUNTING_SIGTERM
, MOUNT_MOUNTING_SIGKILL
,
523 MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
,
524 MOUNT_UNMOUNTING_SIGTERM
, MOUNT_UNMOUNTING_SIGKILL
,
527 (void) automount_send_ready(a
, a
->tokens
, -ENODEV
);
529 automount_set_state(a
, AUTOMOUNT_WAITING
);
533 static void automount_enter_waiting(Automount
*a
) {
534 _cleanup_close_
int ioctl_fd
= -1;
535 int p
[2] = { -1, -1 };
536 char name
[sizeof("systemd-")-1 + DECIMAL_STR_MAX(pid_t
) + 1];
537 char options
[sizeof("fd=,pgrp=,minproto=5,maxproto=5,direct")-1
538 + DECIMAL_STR_MAX(int) + DECIMAL_STR_MAX(gid_t
) + 1];
539 bool mounted
= false;
540 int r
, dev_autofs_fd
;
544 assert(a
->pipe_fd
< 0);
547 set_clear(a
->tokens
);
549 r
= unit_fail_if_symlink(UNIT(a
), a
->where
);
553 (void) mkdir_p_label(a
->where
, 0555);
555 unit_warn_if_dir_nonempty(UNIT(a
), a
->where
);
557 dev_autofs_fd
= open_dev_autofs(UNIT(a
)->manager
);
558 if (dev_autofs_fd
< 0) {
563 if (pipe2(p
, O_NONBLOCK
|O_CLOEXEC
) < 0) {
568 xsprintf(options
, "fd=%i,pgrp="PID_FMT
",minproto=5,maxproto=5,direct", p
[1], getpgrp());
569 xsprintf(name
, "systemd-"PID_FMT
, getpid());
570 if (mount(name
, a
->where
, "autofs", 0, options
) < 0) {
577 p
[1] = safe_close(p
[1]);
579 if (stat(a
->where
, &st
) < 0) {
584 ioctl_fd
= open_ioctl_fd(dev_autofs_fd
, a
->where
, st
.st_dev
);
590 r
= autofs_protocol(dev_autofs_fd
, ioctl_fd
);
594 r
= autofs_set_timeout(dev_autofs_fd
, ioctl_fd
, a
->timeout_idle_usec
);
600 * Unless we close the ioctl fd here, for some weird reason
601 * the direct mount will not receive events from the
604 r
= sd_event_add_io(UNIT(a
)->manager
->event
, &a
->pipe_event_source
, p
[0], EPOLLIN
, automount_dispatch_io
, a
);
608 (void) sd_event_source_set_description(a
->pipe_event_source
, "automount-io");
611 a
->dev_id
= st
.st_dev
;
613 automount_set_state(a
, AUTOMOUNT_WAITING
);
618 log_unit_error_errno(UNIT(a
), r
, "Failed to initialize automounter: %m");
623 r
= repeat_unmount(a
->where
, MNT_DETACH
);
625 log_error_errno(r
, "Failed to unmount, ignoring: %m");
628 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
631 static void *expire_thread(void *p
) {
632 struct autofs_dev_ioctl param
;
633 _cleanup_(expire_data_freep
) struct expire_data
*data
= (struct expire_data
*)p
;
636 assert(data
->dev_autofs_fd
>= 0);
637 assert(data
->ioctl_fd
>= 0);
639 init_autofs_dev_ioctl(¶m
);
640 param
.ioctlfd
= data
->ioctl_fd
;
643 r
= ioctl(data
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_EXPIRE
, ¶m
);
647 log_warning_errno(errno
, "Failed to expire automount, ignoring: %m");
652 static int automount_dispatch_expire(sd_event_source
*source
, usec_t usec
, void *userdata
) {
653 Automount
*a
= AUTOMOUNT(userdata
);
654 _cleanup_(expire_data_freep
) struct expire_data
*data
= NULL
;
658 assert(source
== a
->expire_event_source
);
660 data
= new0(struct expire_data
, 1);
666 data
->dev_autofs_fd
= fcntl(UNIT(a
)->manager
->dev_autofs_fd
, F_DUPFD_CLOEXEC
, 3);
667 if (data
->dev_autofs_fd
< 0)
668 return log_unit_error_errno(UNIT(a
), errno
, "Failed to duplicate autofs fd: %m");
670 data
->ioctl_fd
= open_ioctl_fd(UNIT(a
)->manager
->dev_autofs_fd
, a
->where
, a
->dev_id
);
671 if (data
->ioctl_fd
< 0)
672 return log_unit_error_errno(UNIT(a
), data
->ioctl_fd
, "Couldn't open autofs ioctl fd: %m");
674 r
= asynchronous_job(expire_thread
, data
);
676 return log_unit_error_errno(UNIT(a
), r
, "Failed to start expire job: %m");
680 return automount_start_expire(a
);
683 static int automount_start_expire(Automount
*a
) {
689 if (a
->timeout_idle_usec
== 0)
692 timeout
= now(CLOCK_MONOTONIC
) + MAX(a
->timeout_idle_usec
/3, USEC_PER_SEC
);
694 if (a
->expire_event_source
) {
695 r
= sd_event_source_set_time(a
->expire_event_source
, timeout
);
699 return sd_event_source_set_enabled(a
->expire_event_source
, SD_EVENT_ONESHOT
);
702 r
= sd_event_add_time(
703 UNIT(a
)->manager
->event
,
704 &a
->expire_event_source
,
705 CLOCK_MONOTONIC
, timeout
, 0,
706 automount_dispatch_expire
, a
);
710 (void) sd_event_source_set_description(a
->expire_event_source
, "automount-expire");
715 static void automount_stop_expire(Automount
*a
) {
718 if (!a
->expire_event_source
)
721 (void) sd_event_source_set_enabled(a
->expire_event_source
, SD_EVENT_OFF
);
724 static void automount_enter_runnning(Automount
*a
) {
725 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
731 /* We don't take mount requests anymore if we are supposed to
732 * shut down anyway */
733 if (unit_stop_pending(UNIT(a
))) {
734 log_unit_debug(UNIT(a
), "Suppressing automount request since unit stop is scheduled.");
735 automount_send_ready(a
, a
->tokens
, -EHOSTDOWN
);
736 automount_send_ready(a
, a
->expire_tokens
, -EHOSTDOWN
);
740 mkdir_p_label(a
->where
, a
->directory_mode
);
742 /* Before we do anything, let's see if somebody is playing games with us? */
743 if (lstat(a
->where
, &st
) < 0) {
744 log_unit_warning_errno(UNIT(a
), errno
, "Failed to stat automount point: %m");
748 if (!S_ISDIR(st
.st_mode
) || st
.st_dev
!= a
->dev_id
)
749 log_unit_info(UNIT(a
), "Automount point already active?");
753 trigger
= UNIT_TRIGGER(UNIT(a
));
755 log_unit_error(UNIT(a
), "Unit to trigger vanished.");
759 r
= manager_add_job(UNIT(a
)->manager
, JOB_START
, trigger
, JOB_REPLACE
, &error
, NULL
);
761 log_unit_warning(UNIT(a
), "Failed to queue mount startup job: %s", bus_error_message(&error
, r
));
766 automount_set_state(a
, AUTOMOUNT_RUNNING
);
770 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
773 static int automount_start(Unit
*u
) {
774 Automount
*a
= AUTOMOUNT(u
);
779 assert(a
->state
== AUTOMOUNT_DEAD
|| a
->state
== AUTOMOUNT_FAILED
);
781 if (path_is_mount_point(a
->where
, 0) > 0) {
782 log_unit_error(u
, "Path %s is already a mount point, refusing start.", a
->where
);
786 trigger
= UNIT_TRIGGER(u
);
787 if (!trigger
|| trigger
->load_state
!= UNIT_LOADED
) {
788 log_unit_error(u
, "Refusing to start, unit to trigger not loaded.");
792 r
= unit_start_limit_test(u
);
794 automount_enter_dead(a
, AUTOMOUNT_FAILURE_START_LIMIT_HIT
);
798 a
->result
= AUTOMOUNT_SUCCESS
;
799 automount_enter_waiting(a
);
803 static int automount_stop(Unit
*u
) {
804 Automount
*a
= AUTOMOUNT(u
);
807 assert(a
->state
== AUTOMOUNT_WAITING
|| a
->state
== AUTOMOUNT_RUNNING
);
809 automount_enter_dead(a
, AUTOMOUNT_SUCCESS
);
813 static int automount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
814 Automount
*a
= AUTOMOUNT(u
);
823 unit_serialize_item(u
, f
, "state", automount_state_to_string(a
->state
));
824 unit_serialize_item(u
, f
, "result", automount_result_to_string(a
->result
));
825 unit_serialize_item_format(u
, f
, "dev-id", "%u", (unsigned) a
->dev_id
);
827 SET_FOREACH(p
, a
->tokens
, i
)
828 unit_serialize_item_format(u
, f
, "token", "%u", PTR_TO_UINT(p
));
829 SET_FOREACH(p
, a
->expire_tokens
, i
)
830 unit_serialize_item_format(u
, f
, "expire-token", "%u", PTR_TO_UINT(p
));
832 r
= unit_serialize_item_fd(u
, f
, fds
, "pipe-fd", a
->pipe_fd
);
839 static int automount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
840 Automount
*a
= AUTOMOUNT(u
);
846 if (streq(key
, "state")) {
847 AutomountState state
;
849 state
= automount_state_from_string(value
);
851 log_unit_debug(u
, "Failed to parse state value: %s", value
);
853 a
->deserialized_state
= state
;
854 } else if (streq(key
, "result")) {
857 f
= automount_result_from_string(value
);
859 log_unit_debug(u
, "Failed to parse result value: %s", value
);
860 else if (f
!= AUTOMOUNT_SUCCESS
)
863 } else if (streq(key
, "dev-id")) {
866 if (safe_atou(value
, &d
) < 0)
867 log_unit_debug(u
, "Failed to parse dev-id value: %s", value
);
869 a
->dev_id
= (unsigned) d
;
870 } else if (streq(key
, "token")) {
873 if (safe_atou(value
, &token
) < 0)
874 log_unit_debug(u
, "Failed to parse token value: %s", value
);
876 r
= set_ensure_allocated(&a
->tokens
, NULL
);
882 r
= set_put(a
->tokens
, UINT_TO_PTR(token
));
884 log_unit_error_errno(u
, r
, "Failed to add token to set: %m");
886 } else if (streq(key
, "expire-token")) {
889 if (safe_atou(value
, &token
) < 0)
890 log_unit_debug(u
, "Failed to parse token value: %s", value
);
892 r
= set_ensure_allocated(&a
->expire_tokens
, NULL
);
898 r
= set_put(a
->expire_tokens
, UINT_TO_PTR(token
));
900 log_unit_error_errno(u
, r
, "Failed to add expire token to set: %m");
902 } else if (streq(key
, "pipe-fd")) {
905 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
906 log_unit_debug(u
, "Failed to parse pipe-fd value: %s", value
);
908 safe_close(a
->pipe_fd
);
909 a
->pipe_fd
= fdset_remove(fds
, fd
);
912 log_unit_debug(u
, "Unknown serialization key: %s", key
);
917 static UnitActiveState
automount_active_state(Unit
*u
) {
920 return state_translation_table
[AUTOMOUNT(u
)->state
];
923 static const char *automount_sub_state_to_string(Unit
*u
) {
926 return automount_state_to_string(AUTOMOUNT(u
)->state
);
929 static bool automount_check_gc(Unit
*u
) {
932 if (!UNIT_TRIGGER(u
))
935 return UNIT_VTABLE(UNIT_TRIGGER(u
))->check_gc(UNIT_TRIGGER(u
));
938 static int automount_dispatch_io(sd_event_source
*s
, int fd
, uint32_t events
, void *userdata
) {
939 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
940 union autofs_v5_packet_union packet
;
941 Automount
*a
= AUTOMOUNT(userdata
);
947 assert(fd
== a
->pipe_fd
);
949 if (events
!= EPOLLIN
) {
950 log_unit_error(UNIT(a
), "Got invalid poll event %"PRIu32
" on pipe (fd=%d)", events
, fd
);
954 r
= loop_read_exact(a
->pipe_fd
, &packet
, sizeof(packet
), true);
956 log_unit_error_errno(UNIT(a
), r
, "Invalid read from pipe: %m");
960 switch (packet
.hdr
.type
) {
962 case autofs_ptype_missing_direct
:
964 if (packet
.v5_packet
.pid
> 0) {
965 _cleanup_free_
char *p
= NULL
;
967 get_process_comm(packet
.v5_packet
.pid
, &p
);
968 log_unit_info(UNIT(a
), "Got automount request for %s, triggered by %"PRIu32
" (%s)", a
->where
, packet
.v5_packet
.pid
, strna(p
));
970 log_unit_debug(UNIT(a
), "Got direct mount request on %s", a
->where
);
972 r
= set_ensure_allocated(&a
->tokens
, NULL
);
974 log_unit_error(UNIT(a
), "Failed to allocate token set.");
978 r
= set_put(a
->tokens
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
));
980 log_unit_error_errno(UNIT(a
), r
, "Failed to remember token: %m");
984 automount_enter_runnning(a
);
987 case autofs_ptype_expire_direct
:
988 log_unit_debug(UNIT(a
), "Got direct umount request on %s", a
->where
);
990 automount_stop_expire(a
);
992 r
= set_ensure_allocated(&a
->expire_tokens
, NULL
);
994 log_unit_error(UNIT(a
), "Failed to allocate token set.");
998 r
= set_put(a
->expire_tokens
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
));
1000 log_unit_error_errno(UNIT(a
), r
, "Failed to remember token: %m");
1004 /* Before we do anything, let's see if somebody is playing games with us? */
1005 if (lstat(a
->where
, &st
) < 0) {
1006 log_unit_warning_errno(UNIT(a
), errno
, "Failed to stat automount point: %m");
1010 if (!S_ISDIR(st
.st_mode
) || st
.st_dev
== a
->dev_id
) {
1011 log_unit_info(UNIT(a
), "Automount point already unmounted?");
1012 automount_send_ready(a
, a
->expire_tokens
, 0);
1016 trigger
= UNIT_TRIGGER(UNIT(a
));
1018 log_unit_error(UNIT(a
), "Unit to trigger vanished.");
1022 r
= manager_add_job(UNIT(a
)->manager
, JOB_STOP
, trigger
, JOB_REPLACE
, &error
, NULL
);
1024 log_unit_warning(UNIT(a
), "Failed to queue umount startup job: %s", bus_error_message(&error
, r
));
1030 log_unit_error(UNIT(a
), "Received unknown automount request %i", packet
.hdr
.type
);
1037 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
1041 static void automount_shutdown(Manager
*m
) {
1044 m
->dev_autofs_fd
= safe_close(m
->dev_autofs_fd
);
1047 static void automount_reset_failed(Unit
*u
) {
1048 Automount
*a
= AUTOMOUNT(u
);
1052 if (a
->state
== AUTOMOUNT_FAILED
)
1053 automount_set_state(a
, AUTOMOUNT_DEAD
);
1055 a
->result
= AUTOMOUNT_SUCCESS
;
1058 static bool automount_supported(void) {
1059 static int supported
= -1;
1062 supported
= access("/dev/autofs", F_OK
) >= 0;
1067 static const char* const automount_result_table
[_AUTOMOUNT_RESULT_MAX
] = {
1068 [AUTOMOUNT_SUCCESS
] = "success",
1069 [AUTOMOUNT_FAILURE_RESOURCES
] = "resources",
1070 [AUTOMOUNT_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
1071 [AUTOMOUNT_FAILURE_MOUNT_START_LIMIT_HIT
] = "mount-start-limit-hit",
1074 DEFINE_STRING_TABLE_LOOKUP(automount_result
, AutomountResult
);
1076 const UnitVTable automount_vtable
= {
1077 .object_size
= sizeof(Automount
),
1084 .init
= automount_init
,
1085 .load
= automount_load
,
1086 .done
= automount_done
,
1088 .coldplug
= automount_coldplug
,
1090 .dump
= automount_dump
,
1092 .start
= automount_start
,
1093 .stop
= automount_stop
,
1095 .serialize
= automount_serialize
,
1096 .deserialize_item
= automount_deserialize_item
,
1098 .active_state
= automount_active_state
,
1099 .sub_state_to_string
= automount_sub_state_to_string
,
1101 .check_gc
= automount_check_gc
,
1103 .trigger_notify
= automount_trigger_notify
,
1105 .reset_failed
= automount_reset_failed
,
1107 .bus_vtable
= bus_automount_vtable
,
1108 .bus_set_property
= bus_automount_set_property
,
1110 .can_transient
= true,
1112 .shutdown
= automount_shutdown
,
1113 .supported
= automount_supported
,
1115 .status_message_formats
= {
1116 .finished_start_job
= {
1117 [JOB_DONE
] = "Set up automount %s.",
1118 [JOB_FAILED
] = "Failed to set up automount %s.",
1120 .finished_stop_job
= {
1121 [JOB_DONE
] = "Unset automount %s.",
1122 [JOB_FAILED
] = "Failed to unset automount %s.",