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/>.
25 #include <linux/auto_dev-ioctl.h>
26 #include <linux/auto_fs4.h>
27 #include <sys/epoll.h>
28 #include <sys/mount.h>
33 #include "bus-error.h"
35 #include "dbus-automount.h"
36 #include "formats-util.h"
40 #include "path-util.h"
41 #include "process-util.h"
43 #include "string-util.h"
44 #include "unit-name.h"
46 #include "automount.h"
48 static const UnitActiveState state_translation_table
[_AUTOMOUNT_STATE_MAX
] = {
49 [AUTOMOUNT_DEAD
] = UNIT_INACTIVE
,
50 [AUTOMOUNT_WAITING
] = UNIT_ACTIVE
,
51 [AUTOMOUNT_RUNNING
] = UNIT_ACTIVE
,
52 [AUTOMOUNT_FAILED
] = UNIT_FAILED
60 static inline void expire_data_free(struct expire_data
*data
) {
64 safe_close(data
->dev_autofs_fd
);
65 safe_close(data
->ioctl_fd
);
69 DEFINE_TRIVIAL_CLEANUP_FUNC(struct expire_data
*, expire_data_free
);
71 static int open_dev_autofs(Manager
*m
);
72 static int automount_dispatch_io(sd_event_source
*s
, int fd
, uint32_t events
, void *userdata
);
74 static void automount_init(Unit
*u
) {
75 Automount
*a
= AUTOMOUNT(u
);
78 assert(u
->load_state
== UNIT_STUB
);
81 a
->directory_mode
= 0755;
82 UNIT(a
)->ignore_on_isolate
= true;
85 static void repeat_unmount(const char *path
) {
89 /* If there are multiple mounts on a mount point, this
92 if (umount2(path
, MNT_DETACH
) >= 0)
96 log_error_errno(errno
, "Failed to unmount: %m");
102 static int automount_send_ready(Automount
*a
, Set
*tokens
, int status
);
104 static void unmount_autofs(Automount
*a
) {
110 automount_send_ready(a
, a
->tokens
, -EHOSTDOWN
);
111 automount_send_ready(a
, a
->expire_tokens
, -EHOSTDOWN
);
113 a
->pipe_event_source
= sd_event_source_unref(a
->pipe_event_source
);
114 a
->pipe_fd
= safe_close(a
->pipe_fd
);
116 /* If we reload/reexecute things we keep the mount point
119 (UNIT(a
)->manager
->exit_code
!= MANAGER_RELOAD
&&
120 UNIT(a
)->manager
->exit_code
!= MANAGER_REEXECUTE
))
121 repeat_unmount(a
->where
);
124 static void automount_done(Unit
*u
) {
125 Automount
*a
= AUTOMOUNT(u
);
131 a
->where
= mfree(a
->where
);
133 a
->tokens
= set_free(a
->tokens
);
134 a
->expire_tokens
= set_free(a
->expire_tokens
);
136 a
->expire_event_source
= sd_event_source_unref(a
->expire_event_source
);
139 static int automount_add_mount_links(Automount
*a
) {
140 _cleanup_free_
char *parent
= NULL
;
145 r
= path_get_parent(a
->where
, &parent
);
149 return unit_require_mounts_for(UNIT(a
), parent
);
152 static int automount_add_default_dependencies(Automount
*a
) {
157 if (UNIT(a
)->manager
->running_as
!= MANAGER_SYSTEM
)
160 r
= unit_add_two_dependencies_by_name(UNIT(a
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, NULL
, true);
167 static int automount_verify(Automount
*a
) {
168 _cleanup_free_
char *e
= NULL
;
173 if (UNIT(a
)->load_state
!= UNIT_LOADED
)
176 if (path_equal(a
->where
, "/")) {
177 log_unit_error(UNIT(a
), "Cannot have an automount unit for the root directory. Refusing.");
181 r
= unit_name_from_path(a
->where
, ".automount", &e
);
183 return log_unit_error(UNIT(a
), "Failed to generate unit name from path: %m");
185 if (!unit_has_name(UNIT(a
), e
)) {
186 log_unit_error(UNIT(a
), "Where= setting doesn't match unit name. Refusing.");
193 static int automount_load(Unit
*u
) {
194 Automount
*a
= AUTOMOUNT(u
);
198 assert(u
->load_state
== UNIT_STUB
);
200 /* Load a .automount file */
201 r
= unit_load_fragment_and_dropin_optional(u
);
205 if (u
->load_state
== UNIT_LOADED
) {
209 r
= unit_name_to_path(u
->id
, &a
->where
);
214 path_kill_slashes(a
->where
);
216 r
= unit_load_related_unit(u
, ".mount", &x
);
220 r
= unit_add_two_dependencies(u
, UNIT_BEFORE
, UNIT_TRIGGERS
, x
, true);
224 r
= automount_add_mount_links(a
);
228 if (UNIT(a
)->default_dependencies
) {
229 r
= automount_add_default_dependencies(a
);
235 return automount_verify(a
);
238 static void automount_set_state(Automount
*a
, AutomountState state
) {
239 AutomountState old_state
;
242 old_state
= a
->state
;
245 if (state
!= AUTOMOUNT_WAITING
&&
246 state
!= AUTOMOUNT_RUNNING
)
249 if (state
!= old_state
)
250 log_unit_debug(UNIT(a
), "Changed %s -> %s", automount_state_to_string(old_state
), automount_state_to_string(state
));
252 unit_notify(UNIT(a
), state_translation_table
[old_state
], state_translation_table
[state
], true);
255 static int automount_coldplug(Unit
*u
) {
256 Automount
*a
= AUTOMOUNT(u
);
260 assert(a
->state
== AUTOMOUNT_DEAD
);
262 if (a
->deserialized_state
!= a
->state
) {
264 r
= open_dev_autofs(u
->manager
);
268 if (a
->deserialized_state
== AUTOMOUNT_WAITING
||
269 a
->deserialized_state
== AUTOMOUNT_RUNNING
) {
270 assert(a
->pipe_fd
>= 0);
272 r
= sd_event_add_io(u
->manager
->event
, &a
->pipe_event_source
, a
->pipe_fd
, EPOLLIN
, automount_dispatch_io
, u
);
276 (void) sd_event_source_set_description(a
->pipe_event_source
, "automount-io");
279 automount_set_state(a
, a
->deserialized_state
);
285 static void automount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
286 char time_string
[FORMAT_TIMESPAN_MAX
];
287 Automount
*a
= AUTOMOUNT(u
);
292 "%sAutomount State: %s\n"
295 "%sDirectoryMode: %04o\n"
296 "%sTimeoutIdleUSec: %s\n",
297 prefix
, automount_state_to_string(a
->state
),
298 prefix
, automount_result_to_string(a
->result
),
300 prefix
, a
->directory_mode
,
301 prefix
, format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, a
->timeout_idle_usec
, USEC_PER_SEC
));
304 static void automount_enter_dead(Automount
*a
, AutomountResult f
) {
307 if (f
!= AUTOMOUNT_SUCCESS
)
310 automount_set_state(a
, a
->result
!= AUTOMOUNT_SUCCESS
? AUTOMOUNT_FAILED
: AUTOMOUNT_DEAD
);
313 static int open_dev_autofs(Manager
*m
) {
314 struct autofs_dev_ioctl param
;
318 if (m
->dev_autofs_fd
>= 0)
319 return m
->dev_autofs_fd
;
321 label_fix("/dev/autofs", false, false);
323 m
->dev_autofs_fd
= open("/dev/autofs", O_CLOEXEC
|O_RDONLY
);
324 if (m
->dev_autofs_fd
< 0)
325 return log_error_errno(errno
, "Failed to open /dev/autofs: %m");
327 init_autofs_dev_ioctl(¶m
);
328 if (ioctl(m
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_VERSION
, ¶m
) < 0) {
329 m
->dev_autofs_fd
= safe_close(m
->dev_autofs_fd
);
333 log_debug("Autofs kernel version %i.%i", param
.ver_major
, param
.ver_minor
);
335 return m
->dev_autofs_fd
;
338 static int open_ioctl_fd(int dev_autofs_fd
, const char *where
, dev_t devid
) {
339 struct autofs_dev_ioctl
*param
;
342 assert(dev_autofs_fd
>= 0);
345 l
= sizeof(struct autofs_dev_ioctl
) + strlen(where
) + 1;
348 init_autofs_dev_ioctl(param
);
351 param
->openmount
.devid
= devid
;
352 strcpy(param
->path
, where
);
354 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_OPENMOUNT
, param
) < 0)
357 if (param
->ioctlfd
< 0)
360 (void) fd_cloexec(param
->ioctlfd
, true);
361 return param
->ioctlfd
;
364 static int autofs_protocol(int dev_autofs_fd
, int ioctl_fd
) {
365 uint32_t major
, minor
;
366 struct autofs_dev_ioctl param
;
368 assert(dev_autofs_fd
>= 0);
369 assert(ioctl_fd
>= 0);
371 init_autofs_dev_ioctl(¶m
);
372 param
.ioctlfd
= ioctl_fd
;
374 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOVER
, ¶m
) < 0)
377 major
= param
.protover
.version
;
379 init_autofs_dev_ioctl(¶m
);
380 param
.ioctlfd
= ioctl_fd
;
382 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOSUBVER
, ¶m
) < 0)
385 minor
= param
.protosubver
.sub_version
;
387 log_debug("Autofs protocol version %i.%i", major
, minor
);
391 static int autofs_set_timeout(int dev_autofs_fd
, int ioctl_fd
, usec_t usec
) {
392 struct autofs_dev_ioctl param
;
394 assert(dev_autofs_fd
>= 0);
395 assert(ioctl_fd
>= 0);
397 init_autofs_dev_ioctl(¶m
);
398 param
.ioctlfd
= ioctl_fd
;
400 /* Convert to seconds, rounding up. */
401 param
.timeout
.timeout
= (usec
+ USEC_PER_SEC
- 1) / USEC_PER_SEC
;
403 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_TIMEOUT
, ¶m
) < 0)
409 static int autofs_send_ready(int dev_autofs_fd
, int ioctl_fd
, uint32_t token
, int status
) {
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
;
419 param
.fail
.token
= token
;
420 param
.fail
.status
= status
;
422 param
.ready
.token
= token
;
424 if (ioctl(dev_autofs_fd
, status
? AUTOFS_DEV_IOCTL_FAIL
: AUTOFS_DEV_IOCTL_READY
, ¶m
) < 0)
430 static int automount_send_ready(Automount
*a
, Set
*tokens
, int status
) {
431 _cleanup_close_
int ioctl_fd
= -1;
438 if (set_isempty(tokens
))
441 ioctl_fd
= open_ioctl_fd(UNIT(a
)->manager
->dev_autofs_fd
, a
->where
, a
->dev_id
);
446 log_unit_debug_errno(UNIT(a
), status
, "Sending failure: %m");
448 log_unit_debug(UNIT(a
), "Sending success.");
452 /* Autofs thankfully does not hand out 0 as a token */
453 while ((token
= PTR_TO_UINT(set_steal_first(tokens
)))) {
456 /* Autofs fun fact II:
458 * if you pass a positive status code here, the kernel will
461 k
= autofs_send_ready(UNIT(a
)->manager
->dev_autofs_fd
,
472 static int automount_start_expire(Automount
*a
);
474 int automount_update_mount(Automount
*a
, MountState old_state
, MountState state
) {
481 case MOUNT_REMOUNTING
:
482 automount_send_ready(a
, a
->tokens
, 0);
483 r
= automount_start_expire(a
);
485 log_unit_warning_errno(UNIT(a
), r
, "Failed to start expiration timer, ignoring: %m");
488 case MOUNT_UNMOUNTING
:
489 case MOUNT_MOUNTING_SIGTERM
:
490 case MOUNT_MOUNTING_SIGKILL
:
491 case MOUNT_REMOUNTING_SIGTERM
:
492 case MOUNT_REMOUNTING_SIGKILL
:
493 case MOUNT_UNMOUNTING_SIGTERM
:
494 case MOUNT_UNMOUNTING_SIGKILL
:
496 if (old_state
!= state
)
497 automount_send_ready(a
, a
->tokens
, -ENODEV
);
498 (void) sd_event_source_set_enabled(a
->expire_event_source
, SD_EVENT_OFF
);
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);
541 set_clear(a
->tokens
);
543 r
= unit_fail_if_symlink(UNIT(a
), a
->where
);
547 (void) mkdir_p_label(a
->where
, 0555);
549 unit_warn_if_dir_nonempty(UNIT(a
), a
->where
);
551 dev_autofs_fd
= open_dev_autofs(UNIT(a
)->manager
);
552 if (dev_autofs_fd
< 0) {
557 if (pipe2(p
, O_NONBLOCK
|O_CLOEXEC
) < 0) {
562 xsprintf(options
, "fd=%i,pgrp="PID_FMT
",minproto=5,maxproto=5,direct", p
[1], getpgrp());
563 xsprintf(name
, "systemd-"PID_FMT
, getpid());
564 if (mount(name
, a
->where
, "autofs", 0, options
) < 0) {
571 p
[1] = safe_close(p
[1]);
573 if (stat(a
->where
, &st
) < 0) {
578 ioctl_fd
= open_ioctl_fd(dev_autofs_fd
, a
->where
, st
.st_dev
);
584 r
= autofs_protocol(dev_autofs_fd
, ioctl_fd
);
588 r
= autofs_set_timeout(dev_autofs_fd
, ioctl_fd
, a
->timeout_idle_usec
);
594 * Unless we close the ioctl fd here, for some weird reason
595 * the direct mount will not receive events from the
598 r
= sd_event_add_io(UNIT(a
)->manager
->event
, &a
->pipe_event_source
, p
[0], EPOLLIN
, automount_dispatch_io
, a
);
602 (void) sd_event_source_set_description(a
->pipe_event_source
, "automount-io");
605 a
->dev_id
= st
.st_dev
;
607 automount_set_state(a
, AUTOMOUNT_WAITING
);
615 repeat_unmount(a
->where
);
617 log_unit_error_errno(UNIT(a
), r
, "Failed to initialize automounter: %m");
618 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
621 static void *expire_thread(void *p
) {
622 struct autofs_dev_ioctl param
;
623 _cleanup_(expire_data_freep
) struct expire_data
*data
= (struct expire_data
*)p
;
626 assert(data
->dev_autofs_fd
>= 0);
627 assert(data
->ioctl_fd
>= 0);
629 init_autofs_dev_ioctl(¶m
);
630 param
.ioctlfd
= data
->ioctl_fd
;
633 r
= ioctl(data
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_EXPIRE
, ¶m
);
637 log_warning_errno(errno
, "Failed to expire automount, ignoring: %m");
642 static int automount_dispatch_expire(sd_event_source
*source
, usec_t usec
, void *userdata
) {
643 Automount
*a
= AUTOMOUNT(userdata
);
644 _cleanup_(expire_data_freep
) struct expire_data
*data
= NULL
;
648 assert(source
== a
->expire_event_source
);
650 data
= new0(struct expire_data
, 1);
656 data
->dev_autofs_fd
= fcntl(UNIT(a
)->manager
->dev_autofs_fd
, F_DUPFD_CLOEXEC
, 3);
657 if (data
->dev_autofs_fd
< 0)
658 return log_unit_error_errno(UNIT(a
), errno
, "Failed to duplicate autofs fd: %m");
660 data
->ioctl_fd
= open_ioctl_fd(UNIT(a
)->manager
->dev_autofs_fd
, a
->where
, a
->dev_id
);
661 if (data
->ioctl_fd
< 0)
662 return log_unit_error_errno(UNIT(a
), data
->ioctl_fd
, "Couldn't open autofs ioctl fd: %m");
664 r
= asynchronous_job(expire_thread
, data
);
666 return log_unit_error_errno(UNIT(a
), r
, "Failed to start expire job: %m");
670 return automount_start_expire(a
);
673 static int automount_start_expire(Automount
*a
) {
679 if (a
->timeout_idle_usec
== 0)
682 timeout
= now(CLOCK_MONOTONIC
) + MAX(a
->timeout_idle_usec
/3, USEC_PER_SEC
);
684 if (a
->expire_event_source
) {
685 r
= sd_event_source_set_time(a
->expire_event_source
, timeout
);
689 return sd_event_source_set_enabled(a
->expire_event_source
, SD_EVENT_ONESHOT
);
692 r
= sd_event_add_time(
693 UNIT(a
)->manager
->event
,
694 &a
->expire_event_source
,
695 CLOCK_MONOTONIC
, timeout
, 0,
696 automount_dispatch_expire
, a
);
700 (void) sd_event_source_set_description(a
->expire_event_source
, "automount-expire");
705 static void automount_enter_runnning(Automount
*a
) {
706 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
712 /* We don't take mount requests anymore if we are supposed to
713 * shut down anyway */
714 if (unit_stop_pending(UNIT(a
))) {
715 log_unit_debug(UNIT(a
), "Suppressing automount request since unit stop is scheduled.");
716 automount_send_ready(a
, a
->tokens
, -EHOSTDOWN
);
717 automount_send_ready(a
, a
->expire_tokens
, -EHOSTDOWN
);
721 mkdir_p_label(a
->where
, a
->directory_mode
);
723 /* Before we do anything, let's see if somebody is playing games with us? */
724 if (lstat(a
->where
, &st
) < 0) {
725 log_unit_warning_errno(UNIT(a
), errno
, "Failed to stat automount point: %m");
729 if (!S_ISDIR(st
.st_mode
) || st
.st_dev
!= a
->dev_id
)
730 log_unit_info(UNIT(a
), "Automount point already active?");
732 r
= manager_add_job(UNIT(a
)->manager
, JOB_START
, UNIT_TRIGGER(UNIT(a
)),
733 JOB_REPLACE
, true, &error
, NULL
);
735 log_unit_warning(UNIT(a
), "Failed to queue mount startup job: %s", bus_error_message(&error
, r
));
740 automount_set_state(a
, AUTOMOUNT_RUNNING
);
744 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
747 static int automount_start(Unit
*u
) {
748 Automount
*a
= AUTOMOUNT(u
);
751 assert(a
->state
== AUTOMOUNT_DEAD
|| a
->state
== AUTOMOUNT_FAILED
);
753 if (path_is_mount_point(a
->where
, 0) > 0) {
754 log_unit_error(u
, "Path %s is already a mount point, refusing start.", a
->where
);
758 if (UNIT_TRIGGER(u
)->load_state
!= UNIT_LOADED
)
761 a
->result
= AUTOMOUNT_SUCCESS
;
762 automount_enter_waiting(a
);
766 static int automount_stop(Unit
*u
) {
767 Automount
*a
= AUTOMOUNT(u
);
770 assert(a
->state
== AUTOMOUNT_WAITING
|| a
->state
== AUTOMOUNT_RUNNING
);
772 automount_enter_dead(a
, AUTOMOUNT_SUCCESS
);
776 static int automount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
777 Automount
*a
= AUTOMOUNT(u
);
786 unit_serialize_item(u
, f
, "state", automount_state_to_string(a
->state
));
787 unit_serialize_item(u
, f
, "result", automount_result_to_string(a
->result
));
788 unit_serialize_item_format(u
, f
, "dev-id", "%u", (unsigned) a
->dev_id
);
790 SET_FOREACH(p
, a
->tokens
, i
)
791 unit_serialize_item_format(u
, f
, "token", "%u", PTR_TO_UINT(p
));
792 SET_FOREACH(p
, a
->expire_tokens
, i
)
793 unit_serialize_item_format(u
, f
, "expire-token", "%u", PTR_TO_UINT(p
));
795 r
= unit_serialize_item_fd(u
, f
, fds
, "pipe-fd", a
->pipe_fd
);
802 static int automount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
803 Automount
*a
= AUTOMOUNT(u
);
809 if (streq(key
, "state")) {
810 AutomountState state
;
812 state
= automount_state_from_string(value
);
814 log_unit_debug(u
, "Failed to parse state value: %s", value
);
816 a
->deserialized_state
= state
;
817 } else if (streq(key
, "result")) {
820 f
= automount_result_from_string(value
);
822 log_unit_debug(u
, "Failed to parse result value: %s", value
);
823 else if (f
!= AUTOMOUNT_SUCCESS
)
826 } else if (streq(key
, "dev-id")) {
829 if (safe_atou(value
, &d
) < 0)
830 log_unit_debug(u
, "Failed to parse dev-id value: %s", value
);
832 a
->dev_id
= (unsigned) d
;
833 } else if (streq(key
, "token")) {
836 if (safe_atou(value
, &token
) < 0)
837 log_unit_debug(u
, "Failed to parse token value: %s", value
);
839 r
= set_ensure_allocated(&a
->tokens
, NULL
);
845 r
= set_put(a
->tokens
, UINT_TO_PTR(token
));
847 log_unit_error_errno(u
, r
, "Failed to add token to set: %m");
849 } else if (streq(key
, "expire-token")) {
852 if (safe_atou(value
, &token
) < 0)
853 log_unit_debug(u
, "Failed to parse token value: %s", value
);
855 r
= set_ensure_allocated(&a
->expire_tokens
, NULL
);
861 r
= set_put(a
->expire_tokens
, UINT_TO_PTR(token
));
863 log_unit_error_errno(u
, r
, "Failed to add expire token to set: %m");
865 } else if (streq(key
, "pipe-fd")) {
868 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
869 log_unit_debug(u
, "Failed to parse pipe-fd value: %s", value
);
871 safe_close(a
->pipe_fd
);
872 a
->pipe_fd
= fdset_remove(fds
, fd
);
875 log_unit_debug(u
, "Unknown serialization key: %s", key
);
880 static UnitActiveState
automount_active_state(Unit
*u
) {
883 return state_translation_table
[AUTOMOUNT(u
)->state
];
886 static const char *automount_sub_state_to_string(Unit
*u
) {
889 return automount_state_to_string(AUTOMOUNT(u
)->state
);
892 static bool automount_check_gc(Unit
*u
) {
895 if (!UNIT_TRIGGER(u
))
898 return UNIT_VTABLE(UNIT_TRIGGER(u
))->check_gc(UNIT_TRIGGER(u
));
901 static int automount_dispatch_io(sd_event_source
*s
, int fd
, uint32_t events
, void *userdata
) {
902 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
903 union autofs_v5_packet_union packet
;
904 Automount
*a
= AUTOMOUNT(userdata
);
909 assert(fd
== a
->pipe_fd
);
911 if (events
!= EPOLLIN
) {
912 log_unit_error(UNIT(a
), "Got invalid poll event %"PRIu32
" on pipe (fd=%d)", events
, fd
);
916 r
= loop_read_exact(a
->pipe_fd
, &packet
, sizeof(packet
), true);
918 log_unit_error_errno(UNIT(a
), r
, "Invalid read from pipe: %m");
922 switch (packet
.hdr
.type
) {
924 case autofs_ptype_missing_direct
:
926 if (packet
.v5_packet
.pid
> 0) {
927 _cleanup_free_
char *p
= NULL
;
929 get_process_comm(packet
.v5_packet
.pid
, &p
);
930 log_unit_info(UNIT(a
), "Got automount request for %s, triggered by %"PRIu32
" (%s)", a
->where
, packet
.v5_packet
.pid
, strna(p
));
932 log_unit_debug(UNIT(a
), "Got direct mount request on %s", a
->where
);
934 r
= set_ensure_allocated(&a
->tokens
, NULL
);
936 log_unit_error(UNIT(a
), "Failed to allocate token set.");
940 r
= set_put(a
->tokens
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
));
942 log_unit_error_errno(UNIT(a
), r
, "Failed to remember token: %m");
946 automount_enter_runnning(a
);
949 case autofs_ptype_expire_direct
:
950 log_unit_debug(UNIT(a
), "Got direct umount request on %s", a
->where
);
952 (void) sd_event_source_set_enabled(a
->expire_event_source
, SD_EVENT_OFF
);
954 r
= set_ensure_allocated(&a
->expire_tokens
, NULL
);
956 log_unit_error(UNIT(a
), "Failed to allocate token set.");
960 r
= set_put(a
->expire_tokens
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
));
962 log_unit_error_errno(UNIT(a
), r
, "Failed to remember token: %m");
966 /* Before we do anything, let's see if somebody is playing games with us? */
967 if (lstat(a
->where
, &st
) < 0) {
968 log_unit_warning_errno(UNIT(a
), errno
, "Failed to stat automount point: %m");
972 if (!S_ISDIR(st
.st_mode
) || st
.st_dev
== a
->dev_id
) {
973 log_unit_info(UNIT(a
), "Automount point already unmounted?");
974 automount_send_ready(a
, a
->expire_tokens
, 0);
978 r
= manager_add_job(UNIT(a
)->manager
, JOB_STOP
, UNIT_TRIGGER(UNIT(a
)), JOB_REPLACE
, true, &error
, NULL
);
980 log_unit_warning(UNIT(a
), "Failed to queue umount startup job: %s", bus_error_message(&error
, r
));
986 log_unit_error(UNIT(a
), "Received unknown automount request %i", packet
.hdr
.type
);
993 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
997 static void automount_shutdown(Manager
*m
) {
1000 m
->dev_autofs_fd
= safe_close(m
->dev_autofs_fd
);
1003 static void automount_reset_failed(Unit
*u
) {
1004 Automount
*a
= AUTOMOUNT(u
);
1008 if (a
->state
== AUTOMOUNT_FAILED
)
1009 automount_set_state(a
, AUTOMOUNT_DEAD
);
1011 a
->result
= AUTOMOUNT_SUCCESS
;
1014 static bool automount_supported(void) {
1015 static int supported
= -1;
1018 supported
= access("/dev/autofs", F_OK
) >= 0;
1023 static const char* const automount_result_table
[_AUTOMOUNT_RESULT_MAX
] = {
1024 [AUTOMOUNT_SUCCESS
] = "success",
1025 [AUTOMOUNT_FAILURE_RESOURCES
] = "resources"
1028 DEFINE_STRING_TABLE_LOOKUP(automount_result
, AutomountResult
);
1030 const UnitVTable automount_vtable
= {
1031 .object_size
= sizeof(Automount
),
1039 .no_instances
= true,
1041 .init
= automount_init
,
1042 .load
= automount_load
,
1043 .done
= automount_done
,
1045 .coldplug
= automount_coldplug
,
1047 .dump
= automount_dump
,
1049 .start
= automount_start
,
1050 .stop
= automount_stop
,
1052 .serialize
= automount_serialize
,
1053 .deserialize_item
= automount_deserialize_item
,
1055 .active_state
= automount_active_state
,
1056 .sub_state_to_string
= automount_sub_state_to_string
,
1058 .check_gc
= automount_check_gc
,
1060 .reset_failed
= automount_reset_failed
,
1062 .bus_vtable
= bus_automount_vtable
,
1064 .shutdown
= automount_shutdown
,
1065 .supported
= automount_supported
,
1067 .status_message_formats
= {
1068 .finished_start_job
= {
1069 [JOB_DONE
] = "Set up automount %s.",
1070 [JOB_FAILED
] = "Failed to set up automount %s.",
1072 .finished_stop_job
= {
1073 [JOB_DONE
] = "Unset automount %s.",
1074 [JOB_FAILED
] = "Failed to unset automount %s.",