1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include <sys/mount.h>
27 #include <sys/epoll.h>
29 #include <linux/auto_fs4.h>
30 #include <linux/auto_dev-ioctl.h>
33 #include "automount.h"
35 #include "unit-name.h"
39 #include "path-util.h"
40 #include "dbus-automount.h"
42 #include "bus-error.h"
43 #include "formats-util.h"
44 #include "process-util.h"
47 static const UnitActiveState state_translation_table
[_AUTOMOUNT_STATE_MAX
] = {
48 [AUTOMOUNT_DEAD
] = UNIT_INACTIVE
,
49 [AUTOMOUNT_WAITING
] = UNIT_ACTIVE
,
50 [AUTOMOUNT_RUNNING
] = UNIT_ACTIVE
,
51 [AUTOMOUNT_FAILED
] = UNIT_FAILED
59 static inline void expire_data_free(struct expire_data
*data
) {
63 safe_close(data
->dev_autofs_fd
);
64 safe_close(data
->ioctl_fd
);
68 DEFINE_TRIVIAL_CLEANUP_FUNC(struct expire_data
*, expire_data_free
);
70 static int open_dev_autofs(Manager
*m
);
71 static int automount_dispatch_io(sd_event_source
*s
, int fd
, uint32_t events
, void *userdata
);
73 static void automount_init(Unit
*u
) {
74 Automount
*a
= AUTOMOUNT(u
);
77 assert(u
->load_state
== UNIT_STUB
);
80 a
->directory_mode
= 0755;
81 UNIT(a
)->ignore_on_isolate
= true;
84 static void repeat_unmount(const char *path
) {
88 /* If there are multiple mounts on a mount point, this
91 if (umount2(path
, MNT_DETACH
) >= 0)
95 log_error_errno(errno
, "Failed to unmount: %m");
101 static int automount_send_ready(Automount
*a
, Set
*tokens
, int status
);
103 static void unmount_autofs(Automount
*a
) {
109 automount_send_ready(a
, a
->tokens
, -EHOSTDOWN
);
110 automount_send_ready(a
, a
->expire_tokens
, -EHOSTDOWN
);
112 a
->pipe_event_source
= sd_event_source_unref(a
->pipe_event_source
);
113 a
->pipe_fd
= safe_close(a
->pipe_fd
);
115 /* If we reload/reexecute things we keep the mount point
118 (UNIT(a
)->manager
->exit_code
!= MANAGER_RELOAD
&&
119 UNIT(a
)->manager
->exit_code
!= MANAGER_REEXECUTE
))
120 repeat_unmount(a
->where
);
123 static void automount_done(Unit
*u
) {
124 Automount
*a
= AUTOMOUNT(u
);
135 set_free(a
->expire_tokens
);
136 a
->expire_tokens
= NULL
;
138 a
->expire_event_source
= sd_event_source_unref(a
->expire_event_source
);
141 static int automount_add_mount_links(Automount
*a
) {
142 _cleanup_free_
char *parent
= NULL
;
147 r
= path_get_parent(a
->where
, &parent
);
151 return unit_require_mounts_for(UNIT(a
), parent
);
154 static int automount_add_default_dependencies(Automount
*a
) {
159 if (UNIT(a
)->manager
->running_as
!= SYSTEMD_SYSTEM
)
162 r
= unit_add_two_dependencies_by_name(UNIT(a
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, NULL
, true);
169 static int automount_verify(Automount
*a
) {
171 _cleanup_free_
char *e
= NULL
;
174 if (UNIT(a
)->load_state
!= UNIT_LOADED
)
177 if (path_equal(a
->where
, "/")) {
178 log_unit_error(UNIT(a
)->id
, "Cannot have an automount unit for the root directory. Refusing.");
182 e
= unit_name_from_path(a
->where
, ".automount");
186 b
= unit_has_name(UNIT(a
), e
);
189 log_unit_error(UNIT(a
)->id
, "%s's Where setting doesn't match unit name. Refusing.", UNIT(a
)->id
);
196 static int automount_load(Unit
*u
) {
197 Automount
*a
= AUTOMOUNT(u
);
201 assert(u
->load_state
== UNIT_STUB
);
203 /* Load a .automount file */
204 r
= unit_load_fragment_and_dropin_optional(u
);
208 if (u
->load_state
== UNIT_LOADED
) {
212 a
->where
= unit_name_to_path(u
->id
);
217 path_kill_slashes(a
->where
);
219 r
= unit_load_related_unit(u
, ".mount", &x
);
223 r
= unit_add_two_dependencies(u
, UNIT_BEFORE
, UNIT_TRIGGERS
, x
, true);
227 r
= automount_add_mount_links(a
);
231 if (UNIT(a
)->default_dependencies
) {
232 r
= automount_add_default_dependencies(a
);
238 return automount_verify(a
);
241 static void automount_set_state(Automount
*a
, AutomountState state
) {
242 AutomountState old_state
;
245 old_state
= a
->state
;
248 if (state
!= AUTOMOUNT_WAITING
&&
249 state
!= AUTOMOUNT_RUNNING
)
252 if (state
!= old_state
)
253 log_unit_debug(UNIT(a
)->id
,
254 "%s changed %s -> %s",
256 automount_state_to_string(old_state
),
257 automount_state_to_string(state
));
259 unit_notify(UNIT(a
), state_translation_table
[old_state
], state_translation_table
[state
], true);
262 static int automount_coldplug(Unit
*u
, Hashmap
*deferred_work
) {
263 Automount
*a
= AUTOMOUNT(u
);
267 assert(a
->state
== AUTOMOUNT_DEAD
);
269 if (a
->deserialized_state
!= a
->state
) {
271 r
= open_dev_autofs(u
->manager
);
275 if (a
->deserialized_state
== AUTOMOUNT_WAITING
||
276 a
->deserialized_state
== AUTOMOUNT_RUNNING
) {
278 assert(a
->pipe_fd
>= 0);
280 r
= sd_event_add_io(u
->manager
->event
, &a
->pipe_event_source
, a
->pipe_fd
, EPOLLIN
, automount_dispatch_io
, u
);
285 automount_set_state(a
, a
->deserialized_state
);
291 static void automount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
292 char time_string
[FORMAT_TIMESPAN_MAX
];
293 Automount
*a
= AUTOMOUNT(u
);
298 "%sAutomount State: %s\n"
301 "%sDirectoryMode: %04o\n"
302 "%sTimeoutIdleUSec: %s\n",
303 prefix
, automount_state_to_string(a
->state
),
304 prefix
, automount_result_to_string(a
->result
),
306 prefix
, a
->directory_mode
,
307 prefix
, format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, a
->timeout_idle_usec
, USEC_PER_SEC
));
310 static void automount_enter_dead(Automount
*a
, AutomountResult f
) {
313 if (f
!= AUTOMOUNT_SUCCESS
)
316 automount_set_state(a
, a
->result
!= AUTOMOUNT_SUCCESS
? AUTOMOUNT_FAILED
: AUTOMOUNT_DEAD
);
319 static int open_dev_autofs(Manager
*m
) {
320 struct autofs_dev_ioctl param
;
324 if (m
->dev_autofs_fd
>= 0)
325 return m
->dev_autofs_fd
;
327 label_fix("/dev/autofs", false, false);
329 m
->dev_autofs_fd
= open("/dev/autofs", O_CLOEXEC
|O_RDONLY
);
330 if (m
->dev_autofs_fd
< 0)
331 return log_error_errno(errno
, "Failed to open /dev/autofs: %m");
333 init_autofs_dev_ioctl(¶m
);
334 if (ioctl(m
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_VERSION
, ¶m
) < 0) {
335 m
->dev_autofs_fd
= safe_close(m
->dev_autofs_fd
);
339 log_debug("Autofs kernel version %i.%i", param
.ver_major
, param
.ver_minor
);
341 return m
->dev_autofs_fd
;
344 static int open_ioctl_fd(int dev_autofs_fd
, const char *where
, dev_t devid
) {
345 struct autofs_dev_ioctl
*param
;
348 assert(dev_autofs_fd
>= 0);
351 l
= sizeof(struct autofs_dev_ioctl
) + strlen(where
) + 1;
354 init_autofs_dev_ioctl(param
);
357 param
->openmount
.devid
= devid
;
358 strcpy(param
->path
, where
);
360 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_OPENMOUNT
, param
) < 0)
363 if (param
->ioctlfd
< 0)
366 fd_cloexec(param
->ioctlfd
, true);
367 return param
->ioctlfd
;
370 static int autofs_protocol(int dev_autofs_fd
, int ioctl_fd
) {
371 uint32_t major
, minor
;
372 struct autofs_dev_ioctl param
;
374 assert(dev_autofs_fd
>= 0);
375 assert(ioctl_fd
>= 0);
377 init_autofs_dev_ioctl(¶m
);
378 param
.ioctlfd
= ioctl_fd
;
380 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOVER
, ¶m
) < 0)
383 major
= param
.protover
.version
;
385 init_autofs_dev_ioctl(¶m
);
386 param
.ioctlfd
= ioctl_fd
;
388 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOSUBVER
, ¶m
) < 0)
391 minor
= param
.protosubver
.sub_version
;
393 log_debug("Autofs protocol version %i.%i", major
, minor
);
397 static int autofs_set_timeout(int dev_autofs_fd
, int ioctl_fd
, usec_t usec
) {
398 struct autofs_dev_ioctl param
;
400 assert(dev_autofs_fd
>= 0);
401 assert(ioctl_fd
>= 0);
403 init_autofs_dev_ioctl(¶m
);
404 param
.ioctlfd
= ioctl_fd
;
406 /* Convert to seconds, rounding up. */
407 param
.timeout
.timeout
= (usec
+ USEC_PER_SEC
- 1) / USEC_PER_SEC
;
409 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_TIMEOUT
, ¶m
) < 0)
415 static int autofs_send_ready(int dev_autofs_fd
, int ioctl_fd
, uint32_t token
, int status
) {
416 struct autofs_dev_ioctl param
;
418 assert(dev_autofs_fd
>= 0);
419 assert(ioctl_fd
>= 0);
421 init_autofs_dev_ioctl(¶m
);
422 param
.ioctlfd
= ioctl_fd
;
425 param
.fail
.token
= token
;
426 param
.fail
.status
= status
;
428 param
.ready
.token
= token
;
430 if (ioctl(dev_autofs_fd
, status
? AUTOFS_DEV_IOCTL_FAIL
: AUTOFS_DEV_IOCTL_READY
, ¶m
) < 0)
436 static int automount_send_ready(Automount
*a
, Set
*tokens
, int status
) {
437 _cleanup_close_
int ioctl_fd
= -1;
444 if (set_isempty(tokens
))
447 ioctl_fd
= open_ioctl_fd(UNIT(a
)->manager
->dev_autofs_fd
, a
->where
, a
->dev_id
);
452 log_unit_debug_errno(UNIT(a
)->id
, status
, "Sending failure: %m");
454 log_unit_debug(UNIT(a
)->id
, "Sending success.");
458 /* Autofs thankfully does not hand out 0 as a token */
459 while ((token
= PTR_TO_UINT(set_steal_first(tokens
)))) {
462 /* Autofs fun fact II:
464 * if you pass a positive status code here, the kernel will
467 k
= autofs_send_ready(UNIT(a
)->manager
->dev_autofs_fd
,
478 int automount_update_mount(Automount
*a
, MountState old_state
, MountState state
) {
479 _cleanup_close_
int ioctl_fd
= -1;
485 case MOUNT_REMOUNTING
:
486 automount_send_ready(a
, a
->tokens
, 0);
489 case MOUNT_UNMOUNTING
:
490 case MOUNT_MOUNTING_SIGTERM
:
491 case MOUNT_MOUNTING_SIGKILL
:
492 case MOUNT_REMOUNTING_SIGTERM
:
493 case MOUNT_REMOUNTING_SIGKILL
:
494 case MOUNT_UNMOUNTING_SIGTERM
:
495 case MOUNT_UNMOUNTING_SIGKILL
:
497 if (old_state
!= state
)
498 automount_send_ready(a
, a
->tokens
, -ENODEV
);
506 automount_send_ready(a
, a
->expire_tokens
, 0);
509 case MOUNT_MOUNTING_DONE
:
510 case MOUNT_MOUNTING_SIGTERM
:
511 case MOUNT_MOUNTING_SIGKILL
:
512 case MOUNT_REMOUNTING_SIGTERM
:
513 case MOUNT_REMOUNTING_SIGKILL
:
514 case MOUNT_UNMOUNTING_SIGTERM
:
515 case MOUNT_UNMOUNTING_SIGKILL
:
517 if (old_state
!= state
)
518 automount_send_ready(a
, a
->expire_tokens
, -ENODEV
);
527 static void automount_enter_waiting(Automount
*a
) {
528 _cleanup_close_
int ioctl_fd
= -1;
529 int p
[2] = { -1, -1 };
530 char name
[sizeof("systemd-")-1 + DECIMAL_STR_MAX(pid_t
) + 1];
531 char options
[sizeof("fd=,pgrp=,minproto=5,maxproto=5,direct")-1
532 + DECIMAL_STR_MAX(int) + DECIMAL_STR_MAX(gid_t
) + 1];
533 bool mounted
= false;
534 int r
, dev_autofs_fd
;
538 assert(a
->pipe_fd
< 0);
542 set_clear(a
->tokens
);
544 dev_autofs_fd
= open_dev_autofs(UNIT(a
)->manager
);
545 if (dev_autofs_fd
< 0) {
550 /* We knowingly ignore the results of this call */
551 mkdir_p_label(a
->where
, 0555);
553 warn_if_dir_nonempty(a
->meta
.id
, a
->where
);
555 if (pipe2(p
, O_NONBLOCK
|O_CLOEXEC
) < 0) {
560 xsprintf(options
, "fd=%i,pgrp="PID_FMT
",minproto=5,maxproto=5,direct", p
[1], getpgrp());
561 xsprintf(name
, "systemd-"PID_FMT
, getpid());
562 if (mount(name
, a
->where
, "autofs", 0, options
) < 0) {
569 p
[1] = safe_close(p
[1]);
571 if (stat(a
->where
, &st
) < 0) {
576 ioctl_fd
= open_ioctl_fd(dev_autofs_fd
, a
->where
, st
.st_dev
);
582 r
= autofs_protocol(dev_autofs_fd
, ioctl_fd
);
586 r
= autofs_set_timeout(dev_autofs_fd
, ioctl_fd
, a
->timeout_idle_usec
);
592 * Unless we close the ioctl fd here, for some weird reason
593 * the direct mount will not receive events from the
596 r
= sd_event_add_io(UNIT(a
)->manager
->event
, &a
->pipe_event_source
, p
[0], EPOLLIN
, automount_dispatch_io
, a
);
601 a
->dev_id
= st
.st_dev
;
603 automount_set_state(a
, AUTOMOUNT_WAITING
);
611 repeat_unmount(a
->where
);
613 log_unit_error(UNIT(a
)->id
,
614 "Failed to initialize automounter: %s", strerror(-r
));
615 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
618 static void *expire_thread(void *p
) {
619 struct autofs_dev_ioctl param
;
620 _cleanup_(expire_data_freep
) struct expire_data
*data
= (struct expire_data
*)p
;
623 assert(data
->dev_autofs_fd
>= 0);
624 assert(data
->ioctl_fd
>= 0);
626 init_autofs_dev_ioctl(¶m
);
627 param
.ioctlfd
= data
->ioctl_fd
;
630 r
= ioctl(data
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_EXPIRE
, ¶m
);
634 log_warning_errno(errno
, "Failed to expire automount, ignoring: %m");
639 static int automount_start_expire(Automount
*a
);
641 static int automount_dispatch_expire(sd_event_source
*source
, usec_t usec
, void *userdata
) {
642 Automount
*a
= AUTOMOUNT(userdata
);
643 _cleanup_(expire_data_freep
) struct expire_data
*data
= NULL
;
647 assert(source
== a
->expire_event_source
);
649 data
= new0(struct expire_data
, 1);
655 data
->dev_autofs_fd
= fcntl(UNIT(a
)->manager
->dev_autofs_fd
, F_DUPFD_CLOEXEC
, 3);
656 if (data
->dev_autofs_fd
< 0)
657 return log_unit_error_errno(UNIT(a
)->id
, errno
, "Failed to duplicate autofs fd: %m");
659 data
->ioctl_fd
= open_ioctl_fd(UNIT(a
)->manager
->dev_autofs_fd
, a
->where
, a
->dev_id
);
660 if (data
->ioctl_fd
< 0)
661 return log_unit_error_errno(UNIT(a
)->id
, data
->ioctl_fd
, "Couldn't open autofs ioctl fd: %m");
663 r
= asynchronous_job(expire_thread
, data
);
665 return log_unit_error_errno(UNIT(a
)->id
, r
, "Failed to start expire job: %m");
669 return automount_start_expire(a
);
672 static int automount_start_expire(Automount
*a
) {
678 timeout
= now(CLOCK_MONOTONIC
) + MAX(a
->timeout_idle_usec
/10, USEC_PER_SEC
);
680 if (a
->expire_event_source
) {
681 r
= sd_event_source_set_time(a
->expire_event_source
, timeout
);
685 return sd_event_source_set_enabled(a
->expire_event_source
, SD_EVENT_ONESHOT
);
688 return sd_event_add_time(
689 UNIT(a
)->manager
->event
,
690 &a
->expire_event_source
,
691 CLOCK_MONOTONIC
, timeout
, 0,
692 automount_dispatch_expire
, a
);
695 static void automount_enter_runnning(Automount
*a
) {
696 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
702 /* We don't take mount requests anymore if we are supposed to
703 * shut down anyway */
704 if (unit_stop_pending(UNIT(a
))) {
705 log_unit_debug(UNIT(a
)->id
,
706 "Suppressing automount request on %s since unit stop is scheduled.", UNIT(a
)->id
);
707 automount_send_ready(a
, a
->tokens
, -EHOSTDOWN
);
708 automount_send_ready(a
, a
->expire_tokens
, -EHOSTDOWN
);
712 mkdir_p_label(a
->where
, a
->directory_mode
);
714 /* Before we do anything, let's see if somebody is playing games with us? */
715 if (lstat(a
->where
, &st
) < 0) {
716 log_unit_warning(UNIT(a
)->id
,
717 "%s failed to stat automount point: %m", UNIT(a
)->id
);
721 if (!S_ISDIR(st
.st_mode
) || st
.st_dev
!= a
->dev_id
)
722 log_unit_info(UNIT(a
)->id
,
723 "%s's automount point already active?", UNIT(a
)->id
);
725 r
= manager_add_job(UNIT(a
)->manager
, JOB_START
, UNIT_TRIGGER(UNIT(a
)),
726 JOB_REPLACE
, true, &error
, NULL
);
728 log_unit_warning(UNIT(a
)->id
,
729 "%s failed to queue mount startup job: %s",
730 UNIT(a
)->id
, bus_error_message(&error
, r
));
735 r
= automount_start_expire(a
);
737 log_unit_warning_errno(UNIT(a
)->id
, r
, "Failed to start expiration timer, ignoring: %m");
739 automount_set_state(a
, AUTOMOUNT_RUNNING
);
743 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
746 static int automount_start(Unit
*u
) {
747 Automount
*a
= AUTOMOUNT(u
);
750 assert(a
->state
== AUTOMOUNT_DEAD
|| a
->state
== AUTOMOUNT_FAILED
);
752 if (path_is_mount_point(a
->where
, false) > 0) {
753 log_unit_error(u
->id
, "Path %s is already a mount point, refusing start for %s", a
->where
, u
->id
);
757 if (UNIT_TRIGGER(u
)->load_state
!= UNIT_LOADED
)
760 a
->result
= AUTOMOUNT_SUCCESS
;
761 automount_enter_waiting(a
);
765 static int automount_stop(Unit
*u
) {
766 Automount
*a
= AUTOMOUNT(u
);
769 assert(a
->state
== AUTOMOUNT_WAITING
|| a
->state
== AUTOMOUNT_RUNNING
);
771 automount_enter_dead(a
, AUTOMOUNT_SUCCESS
);
775 static int automount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
776 Automount
*a
= AUTOMOUNT(u
);
784 unit_serialize_item(u
, f
, "state", automount_state_to_string(a
->state
));
785 unit_serialize_item(u
, f
, "result", automount_result_to_string(a
->result
));
786 unit_serialize_item_format(u
, f
, "dev-id", "%u", (unsigned) a
->dev_id
);
788 SET_FOREACH(p
, a
->tokens
, i
)
789 unit_serialize_item_format(u
, f
, "token", "%u", PTR_TO_UINT(p
));
790 SET_FOREACH(p
, a
->expire_tokens
, i
)
791 unit_serialize_item_format(u
, f
, "expire-token", "%u", PTR_TO_UINT(p
));
793 if (a
->pipe_fd
>= 0) {
796 copy
= fdset_put_dup(fds
, a
->pipe_fd
);
800 unit_serialize_item_format(u
, f
, "pipe-fd", "%i", copy
);
806 static int automount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
807 Automount
*a
= AUTOMOUNT(u
);
813 if (streq(key
, "state")) {
814 AutomountState state
;
816 state
= automount_state_from_string(value
);
818 log_unit_debug(u
->id
, "Failed to parse state value %s", value
);
820 a
->deserialized_state
= state
;
821 } else if (streq(key
, "result")) {
824 f
= automount_result_from_string(value
);
826 log_unit_debug(u
->id
, "Failed to parse result value %s", value
);
827 else if (f
!= AUTOMOUNT_SUCCESS
)
830 } else if (streq(key
, "dev-id")) {
833 if (safe_atou(value
, &d
) < 0)
834 log_unit_debug(u
->id
, "Failed to parse dev-id value %s", value
);
836 a
->dev_id
= (unsigned) d
;
837 } else if (streq(key
, "token")) {
840 if (safe_atou(value
, &token
) < 0)
841 log_unit_debug(u
->id
, "Failed to parse token value %s", value
);
844 if (!(a
->tokens
= set_new(NULL
)))
847 r
= set_put(a
->tokens
, UINT_TO_PTR(token
));
851 } else if (streq(key
, "expire-token")) {
854 if (safe_atou(value
, &token
) < 0)
855 log_unit_debug(u
->id
, "Failed to parse token value %s", value
);
857 r
= set_ensure_allocated(&a
->expire_tokens
, NULL
);
863 r
= set_put(a
->expire_tokens
, UINT_TO_PTR(token
));
865 log_unit_error_errno(u
->id
, r
, "Failed to add expire token to set: %m");
867 } else if (streq(key
, "pipe-fd")) {
870 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
871 log_unit_debug(u
->id
, "Failed to parse pipe-fd value %s", value
);
873 safe_close(a
->pipe_fd
);
874 a
->pipe_fd
= fdset_remove(fds
, fd
);
877 log_unit_debug(u
->id
, "Unknown serialization key '%s'", key
);
882 static UnitActiveState
automount_active_state(Unit
*u
) {
885 return state_translation_table
[AUTOMOUNT(u
)->state
];
888 static const char *automount_sub_state_to_string(Unit
*u
) {
891 return automount_state_to_string(AUTOMOUNT(u
)->state
);
894 static bool automount_check_gc(Unit
*u
) {
897 if (!UNIT_TRIGGER(u
))
900 return UNIT_VTABLE(UNIT_TRIGGER(u
))->check_gc(UNIT_TRIGGER(u
));
903 static int automount_dispatch_io(sd_event_source
*s
, int fd
, uint32_t events
, void *userdata
) {
904 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
905 union autofs_v5_packet_union packet
;
906 Automount
*a
= AUTOMOUNT(userdata
);
910 assert(fd
== a
->pipe_fd
);
912 if (events
!= EPOLLIN
) {
913 log_unit_error(UNIT(a
)->id
, "%s: got invalid poll event %"PRIu32
" on pipe (fd=%d)",
914 UNIT(a
)->id
, events
, fd
);
918 r
= loop_read_exact(a
->pipe_fd
, &packet
, sizeof(packet
), true);
920 log_unit_error_errno(UNIT(a
)->id
, r
, "Invalid read from pipe: %m");
924 switch (packet
.hdr
.type
) {
926 case autofs_ptype_missing_direct
:
928 if (packet
.v5_packet
.pid
> 0) {
929 _cleanup_free_
char *p
= NULL
;
931 get_process_comm(packet
.v5_packet
.pid
, &p
);
932 log_unit_info(UNIT(a
)->id
,
933 "Got automount request for %s, triggered by %"PRIu32
" (%s)",
934 a
->where
, packet
.v5_packet
.pid
, strna(p
));
936 log_unit_debug(UNIT(a
)->id
, "Got direct mount request on %s", a
->where
);
938 r
= set_ensure_allocated(&a
->tokens
, NULL
);
940 log_unit_error(UNIT(a
)->id
, "Failed to allocate token set.");
944 r
= set_put(a
->tokens
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
));
946 log_unit_error_errno(UNIT(a
)->id
, r
, "Failed to remember token: %m");
950 automount_enter_runnning(a
);
953 case autofs_ptype_expire_direct
:
954 log_unit_debug(UNIT(a
)->id
, "Got direct umount request on %s", a
->where
);
956 (void) sd_event_source_set_enabled(a
->expire_event_source
, SD_EVENT_OFF
);
958 r
= set_ensure_allocated(&a
->expire_tokens
, NULL
);
960 log_unit_error(UNIT(a
)->id
, "Failed to allocate token set.");
964 r
= set_put(a
->expire_tokens
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
));
966 log_unit_error_errno(UNIT(a
)->id
, r
, "Failed to remember token: %m");
969 r
= manager_add_job(UNIT(a
)->manager
, JOB_STOP
, UNIT_TRIGGER(UNIT(a
)), JOB_REPLACE
, true, &error
, NULL
);
971 log_unit_warning(UNIT(a
)->id
,
972 "%s failed to queue umount startup job: %s",
973 UNIT(a
)->id
, bus_error_message(&error
, r
));
979 log_unit_error(UNIT(a
)->id
, "Received unknown automount request %i", packet
.hdr
.type
);
986 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
990 static void automount_shutdown(Manager
*m
) {
993 m
->dev_autofs_fd
= safe_close(m
->dev_autofs_fd
);
996 static void automount_reset_failed(Unit
*u
) {
997 Automount
*a
= AUTOMOUNT(u
);
1001 if (a
->state
== AUTOMOUNT_FAILED
)
1002 automount_set_state(a
, AUTOMOUNT_DEAD
);
1004 a
->result
= AUTOMOUNT_SUCCESS
;
1007 static bool automount_supported(Manager
*m
) {
1008 static int supported
= -1;
1013 supported
= access("/dev/autofs", F_OK
) >= 0;
1018 static const char* const automount_state_table
[_AUTOMOUNT_STATE_MAX
] = {
1019 [AUTOMOUNT_DEAD
] = "dead",
1020 [AUTOMOUNT_WAITING
] = "waiting",
1021 [AUTOMOUNT_RUNNING
] = "running",
1022 [AUTOMOUNT_FAILED
] = "failed"
1025 DEFINE_STRING_TABLE_LOOKUP(automount_state
, AutomountState
);
1027 static const char* const automount_result_table
[_AUTOMOUNT_RESULT_MAX
] = {
1028 [AUTOMOUNT_SUCCESS
] = "success",
1029 [AUTOMOUNT_FAILURE_RESOURCES
] = "resources"
1032 DEFINE_STRING_TABLE_LOOKUP(automount_result
, AutomountResult
);
1034 const UnitVTable automount_vtable
= {
1035 .object_size
= sizeof(Automount
),
1043 .no_instances
= true,
1045 .init
= automount_init
,
1046 .load
= automount_load
,
1047 .done
= automount_done
,
1049 .coldplug
= automount_coldplug
,
1051 .dump
= automount_dump
,
1053 .start
= automount_start
,
1054 .stop
= automount_stop
,
1056 .serialize
= automount_serialize
,
1057 .deserialize_item
= automount_deserialize_item
,
1059 .active_state
= automount_active_state
,
1060 .sub_state_to_string
= automount_sub_state_to_string
,
1062 .check_gc
= automount_check_gc
,
1064 .reset_failed
= automount_reset_failed
,
1066 .bus_interface
= "org.freedesktop.systemd1.Automount",
1067 .bus_vtable
= bus_automount_vtable
,
1069 .shutdown
= automount_shutdown
,
1070 .supported
= automount_supported
,
1072 .status_message_formats
= {
1073 .finished_start_job
= {
1074 [JOB_DONE
] = "Set up automount %s.",
1075 [JOB_FAILED
] = "Failed to set up automount %s.",
1076 [JOB_DEPENDENCY
] = "Dependency failed for %s.",
1078 .finished_stop_job
= {
1079 [JOB_DONE
] = "Unset automount %s.",
1080 [JOB_FAILED
] = "Failed to unset automount %s.",