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
);
79 static void automount_init(Unit
*u
) {
80 Automount
*a
= AUTOMOUNT(u
);
83 assert(u
->load_state
== UNIT_STUB
);
86 a
->directory_mode
= 0755;
87 UNIT(a
)->ignore_on_isolate
= true;
90 static int automount_send_ready(Automount
*a
, Set
*tokens
, int status
);
92 static void unmount_autofs(Automount
*a
) {
100 automount_send_ready(a
, a
->tokens
, -EHOSTDOWN
);
101 automount_send_ready(a
, a
->expire_tokens
, -EHOSTDOWN
);
103 a
->pipe_event_source
= sd_event_source_unref(a
->pipe_event_source
);
104 a
->pipe_fd
= safe_close(a
->pipe_fd
);
106 /* If we reload/reexecute things we keep the mount point
109 (UNIT(a
)->manager
->exit_code
!= MANAGER_RELOAD
&&
110 UNIT(a
)->manager
->exit_code
!= MANAGER_REEXECUTE
)) {
111 r
= repeat_unmount(a
->where
, MNT_DETACH
);
113 log_error_errno(r
, "Failed to unmount: %m");
117 static void automount_done(Unit
*u
) {
118 Automount
*a
= AUTOMOUNT(u
);
124 a
->where
= mfree(a
->where
);
126 a
->tokens
= set_free(a
->tokens
);
127 a
->expire_tokens
= set_free(a
->expire_tokens
);
129 a
->expire_event_source
= sd_event_source_unref(a
->expire_event_source
);
132 static int automount_add_mount_links(Automount
*a
) {
133 _cleanup_free_
char *parent
= NULL
;
137 parent
= dirname_malloc(a
->where
);
141 return unit_require_mounts_for(UNIT(a
), parent
);
144 static int automount_add_default_dependencies(Automount
*a
) {
149 if (!UNIT(a
)->default_dependencies
)
152 if (!MANAGER_IS_SYSTEM(UNIT(a
)->manager
))
155 r
= unit_add_two_dependencies_by_name(UNIT(a
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, NULL
, true);
162 static int automount_verify(Automount
*a
) {
163 _cleanup_free_
char *e
= NULL
;
168 if (UNIT(a
)->load_state
!= UNIT_LOADED
)
171 if (path_equal(a
->where
, "/")) {
172 log_unit_error(UNIT(a
), "Cannot have an automount unit for the root directory. Refusing.");
176 r
= unit_name_from_path(a
->where
, ".automount", &e
);
178 return log_unit_error(UNIT(a
), "Failed to generate unit name from path: %m");
180 if (!unit_has_name(UNIT(a
), e
)) {
181 log_unit_error(UNIT(a
), "Where= setting doesn't match unit name. Refusing.");
188 static int automount_load(Unit
*u
) {
189 Automount
*a
= AUTOMOUNT(u
);
193 assert(u
->load_state
== UNIT_STUB
);
195 /* Load a .automount file */
196 r
= unit_load_fragment_and_dropin_optional(u
);
200 if (u
->load_state
== UNIT_LOADED
) {
204 r
= unit_name_to_path(u
->id
, &a
->where
);
209 path_kill_slashes(a
->where
);
211 r
= unit_load_related_unit(u
, ".mount", &x
);
215 r
= unit_add_two_dependencies(u
, UNIT_BEFORE
, UNIT_TRIGGERS
, x
, true);
219 r
= automount_add_mount_links(a
);
223 r
= automount_add_default_dependencies(a
);
228 return automount_verify(a
);
231 static void automount_set_state(Automount
*a
, AutomountState state
) {
232 AutomountState old_state
;
235 old_state
= a
->state
;
238 if (state
!= AUTOMOUNT_WAITING
&&
239 state
!= AUTOMOUNT_RUNNING
)
242 if (state
!= old_state
)
243 log_unit_debug(UNIT(a
), "Changed %s -> %s", automount_state_to_string(old_state
), automount_state_to_string(state
));
245 unit_notify(UNIT(a
), state_translation_table
[old_state
], state_translation_table
[state
], true);
248 static int automount_coldplug(Unit
*u
) {
249 Automount
*a
= AUTOMOUNT(u
);
253 assert(a
->state
== AUTOMOUNT_DEAD
);
255 if (a
->deserialized_state
!= a
->state
) {
257 r
= open_dev_autofs(u
->manager
);
261 if (a
->deserialized_state
== AUTOMOUNT_WAITING
||
262 a
->deserialized_state
== AUTOMOUNT_RUNNING
) {
263 assert(a
->pipe_fd
>= 0);
265 r
= sd_event_add_io(u
->manager
->event
, &a
->pipe_event_source
, a
->pipe_fd
, EPOLLIN
, automount_dispatch_io
, u
);
269 (void) sd_event_source_set_description(a
->pipe_event_source
, "automount-io");
272 automount_set_state(a
, a
->deserialized_state
);
278 static void automount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
279 char time_string
[FORMAT_TIMESPAN_MAX
];
280 Automount
*a
= AUTOMOUNT(u
);
285 "%sAutomount State: %s\n"
288 "%sDirectoryMode: %04o\n"
289 "%sTimeoutIdleUSec: %s\n",
290 prefix
, automount_state_to_string(a
->state
),
291 prefix
, automount_result_to_string(a
->result
),
293 prefix
, a
->directory_mode
,
294 prefix
, format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, a
->timeout_idle_usec
, USEC_PER_SEC
));
297 static void automount_enter_dead(Automount
*a
, AutomountResult f
) {
300 if (f
!= AUTOMOUNT_SUCCESS
)
303 automount_set_state(a
, a
->result
!= AUTOMOUNT_SUCCESS
? AUTOMOUNT_FAILED
: AUTOMOUNT_DEAD
);
306 static int open_dev_autofs(Manager
*m
) {
307 struct autofs_dev_ioctl param
;
311 if (m
->dev_autofs_fd
>= 0)
312 return m
->dev_autofs_fd
;
314 label_fix("/dev/autofs", false, false);
316 m
->dev_autofs_fd
= open("/dev/autofs", O_CLOEXEC
|O_RDONLY
);
317 if (m
->dev_autofs_fd
< 0)
318 return log_error_errno(errno
, "Failed to open /dev/autofs: %m");
320 init_autofs_dev_ioctl(¶m
);
321 if (ioctl(m
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_VERSION
, ¶m
) < 0) {
322 m
->dev_autofs_fd
= safe_close(m
->dev_autofs_fd
);
326 log_debug("Autofs kernel version %i.%i", param
.ver_major
, param
.ver_minor
);
328 return m
->dev_autofs_fd
;
331 static int open_ioctl_fd(int dev_autofs_fd
, const char *where
, dev_t devid
) {
332 struct autofs_dev_ioctl
*param
;
335 assert(dev_autofs_fd
>= 0);
338 l
= sizeof(struct autofs_dev_ioctl
) + strlen(where
) + 1;
341 init_autofs_dev_ioctl(param
);
344 param
->openmount
.devid
= devid
;
345 strcpy(param
->path
, where
);
347 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_OPENMOUNT
, param
) < 0)
350 if (param
->ioctlfd
< 0)
353 (void) fd_cloexec(param
->ioctlfd
, true);
354 return param
->ioctlfd
;
357 static int autofs_protocol(int dev_autofs_fd
, int ioctl_fd
) {
358 uint32_t major
, minor
;
359 struct autofs_dev_ioctl param
;
361 assert(dev_autofs_fd
>= 0);
362 assert(ioctl_fd
>= 0);
364 init_autofs_dev_ioctl(¶m
);
365 param
.ioctlfd
= ioctl_fd
;
367 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOVER
, ¶m
) < 0)
370 major
= param
.protover
.version
;
372 init_autofs_dev_ioctl(¶m
);
373 param
.ioctlfd
= ioctl_fd
;
375 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOSUBVER
, ¶m
) < 0)
378 minor
= param
.protosubver
.sub_version
;
380 log_debug("Autofs protocol version %i.%i", major
, minor
);
384 static int autofs_set_timeout(int dev_autofs_fd
, int ioctl_fd
, usec_t usec
) {
385 struct autofs_dev_ioctl param
;
387 assert(dev_autofs_fd
>= 0);
388 assert(ioctl_fd
>= 0);
390 init_autofs_dev_ioctl(¶m
);
391 param
.ioctlfd
= ioctl_fd
;
393 /* Convert to seconds, rounding up. */
394 param
.timeout
.timeout
= (usec
+ USEC_PER_SEC
- 1) / USEC_PER_SEC
;
396 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_TIMEOUT
, ¶m
) < 0)
402 static int autofs_send_ready(int dev_autofs_fd
, int ioctl_fd
, uint32_t token
, int status
) {
403 struct autofs_dev_ioctl param
;
405 assert(dev_autofs_fd
>= 0);
406 assert(ioctl_fd
>= 0);
408 init_autofs_dev_ioctl(¶m
);
409 param
.ioctlfd
= ioctl_fd
;
412 param
.fail
.token
= token
;
413 param
.fail
.status
= status
;
415 param
.ready
.token
= token
;
417 if (ioctl(dev_autofs_fd
, status
? AUTOFS_DEV_IOCTL_FAIL
: AUTOFS_DEV_IOCTL_READY
, ¶m
) < 0)
423 static int automount_send_ready(Automount
*a
, Set
*tokens
, int status
) {
424 _cleanup_close_
int ioctl_fd
= -1;
431 if (set_isempty(tokens
))
434 ioctl_fd
= open_ioctl_fd(UNIT(a
)->manager
->dev_autofs_fd
, a
->where
, a
->dev_id
);
439 log_unit_debug_errno(UNIT(a
), status
, "Sending failure: %m");
441 log_unit_debug(UNIT(a
), "Sending success.");
445 /* Autofs thankfully does not hand out 0 as a token */
446 while ((token
= PTR_TO_UINT(set_steal_first(tokens
)))) {
449 /* Autofs fun fact II:
451 * if you pass a positive status code here, the kernel will
454 k
= autofs_send_ready(UNIT(a
)->manager
->dev_autofs_fd
,
465 static int automount_start_expire(Automount
*a
);
467 int automount_update_mount(Automount
*a
, MountState old_state
, MountState state
) {
474 case MOUNT_REMOUNTING
:
475 automount_send_ready(a
, a
->tokens
, 0);
476 r
= automount_start_expire(a
);
478 log_unit_warning_errno(UNIT(a
), r
, "Failed to start expiration timer, ignoring: %m");
481 case MOUNT_UNMOUNTING
:
482 case MOUNT_MOUNTING_SIGTERM
:
483 case MOUNT_MOUNTING_SIGKILL
:
484 case MOUNT_REMOUNTING_SIGTERM
:
485 case MOUNT_REMOUNTING_SIGKILL
:
486 case MOUNT_UNMOUNTING_SIGTERM
:
487 case MOUNT_UNMOUNTING_SIGKILL
:
489 if (old_state
!= state
)
490 automount_send_ready(a
, a
->tokens
, -ENODEV
);
491 (void) sd_event_source_set_enabled(a
->expire_event_source
, SD_EVENT_OFF
);
499 automount_send_ready(a
, a
->expire_tokens
, 0);
502 case MOUNT_MOUNTING_DONE
:
503 case MOUNT_MOUNTING_SIGTERM
:
504 case MOUNT_MOUNTING_SIGKILL
:
505 case MOUNT_REMOUNTING_SIGTERM
:
506 case MOUNT_REMOUNTING_SIGKILL
:
507 case MOUNT_UNMOUNTING_SIGTERM
:
508 case MOUNT_UNMOUNTING_SIGKILL
:
510 if (old_state
!= state
)
511 automount_send_ready(a
, a
->expire_tokens
, -ENODEV
);
520 static void automount_enter_waiting(Automount
*a
) {
521 _cleanup_close_
int ioctl_fd
= -1;
522 int p
[2] = { -1, -1 };
523 char name
[sizeof("systemd-")-1 + DECIMAL_STR_MAX(pid_t
) + 1];
524 char options
[sizeof("fd=,pgrp=,minproto=5,maxproto=5,direct")-1
525 + DECIMAL_STR_MAX(int) + DECIMAL_STR_MAX(gid_t
) + 1];
526 bool mounted
= false;
527 int r
, dev_autofs_fd
;
531 assert(a
->pipe_fd
< 0);
534 set_clear(a
->tokens
);
536 r
= unit_fail_if_symlink(UNIT(a
), a
->where
);
540 (void) mkdir_p_label(a
->where
, 0555);
542 unit_warn_if_dir_nonempty(UNIT(a
), a
->where
);
544 dev_autofs_fd
= open_dev_autofs(UNIT(a
)->manager
);
545 if (dev_autofs_fd
< 0) {
550 if (pipe2(p
, O_NONBLOCK
|O_CLOEXEC
) < 0) {
555 xsprintf(options
, "fd=%i,pgrp="PID_FMT
",minproto=5,maxproto=5,direct", p
[1], getpgrp());
556 xsprintf(name
, "systemd-"PID_FMT
, getpid());
557 if (mount(name
, a
->where
, "autofs", 0, options
) < 0) {
564 p
[1] = safe_close(p
[1]);
566 if (stat(a
->where
, &st
) < 0) {
571 ioctl_fd
= open_ioctl_fd(dev_autofs_fd
, a
->where
, st
.st_dev
);
577 r
= autofs_protocol(dev_autofs_fd
, ioctl_fd
);
581 r
= autofs_set_timeout(dev_autofs_fd
, ioctl_fd
, a
->timeout_idle_usec
);
587 * Unless we close the ioctl fd here, for some weird reason
588 * the direct mount will not receive events from the
591 r
= sd_event_add_io(UNIT(a
)->manager
->event
, &a
->pipe_event_source
, p
[0], EPOLLIN
, automount_dispatch_io
, a
);
595 (void) sd_event_source_set_description(a
->pipe_event_source
, "automount-io");
598 a
->dev_id
= st
.st_dev
;
600 automount_set_state(a
, AUTOMOUNT_WAITING
);
605 log_unit_error_errno(UNIT(a
), r
, "Failed to initialize automounter: %m");
610 r
= repeat_unmount(a
->where
, MNT_DETACH
);
612 log_error_errno(r
, "Failed to unmount, ignoring: %m");
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_dispatch_expire(sd_event_source
*source
, usec_t usec
, void *userdata
) {
640 Automount
*a
= AUTOMOUNT(userdata
);
641 _cleanup_(expire_data_freep
) struct expire_data
*data
= NULL
;
645 assert(source
== a
->expire_event_source
);
647 data
= new0(struct expire_data
, 1);
653 data
->dev_autofs_fd
= fcntl(UNIT(a
)->manager
->dev_autofs_fd
, F_DUPFD_CLOEXEC
, 3);
654 if (data
->dev_autofs_fd
< 0)
655 return log_unit_error_errno(UNIT(a
), errno
, "Failed to duplicate autofs fd: %m");
657 data
->ioctl_fd
= open_ioctl_fd(UNIT(a
)->manager
->dev_autofs_fd
, a
->where
, a
->dev_id
);
658 if (data
->ioctl_fd
< 0)
659 return log_unit_error_errno(UNIT(a
), data
->ioctl_fd
, "Couldn't open autofs ioctl fd: %m");
661 r
= asynchronous_job(expire_thread
, data
);
663 return log_unit_error_errno(UNIT(a
), r
, "Failed to start expire job: %m");
667 return automount_start_expire(a
);
670 static int automount_start_expire(Automount
*a
) {
676 if (a
->timeout_idle_usec
== 0)
679 timeout
= now(CLOCK_MONOTONIC
) + MAX(a
->timeout_idle_usec
/3, USEC_PER_SEC
);
681 if (a
->expire_event_source
) {
682 r
= sd_event_source_set_time(a
->expire_event_source
, timeout
);
686 return sd_event_source_set_enabled(a
->expire_event_source
, SD_EVENT_ONESHOT
);
689 r
= sd_event_add_time(
690 UNIT(a
)->manager
->event
,
691 &a
->expire_event_source
,
692 CLOCK_MONOTONIC
, timeout
, 0,
693 automount_dispatch_expire
, a
);
697 (void) sd_event_source_set_description(a
->expire_event_source
, "automount-expire");
702 static void automount_enter_runnning(Automount
*a
) {
703 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
709 /* We don't take mount requests anymore if we are supposed to
710 * shut down anyway */
711 if (unit_stop_pending(UNIT(a
))) {
712 log_unit_debug(UNIT(a
), "Suppressing automount request since unit stop is scheduled.");
713 automount_send_ready(a
, a
->tokens
, -EHOSTDOWN
);
714 automount_send_ready(a
, a
->expire_tokens
, -EHOSTDOWN
);
718 mkdir_p_label(a
->where
, a
->directory_mode
);
720 /* Before we do anything, let's see if somebody is playing games with us? */
721 if (lstat(a
->where
, &st
) < 0) {
722 log_unit_warning_errno(UNIT(a
), errno
, "Failed to stat automount point: %m");
726 if (!S_ISDIR(st
.st_mode
) || st
.st_dev
!= a
->dev_id
)
727 log_unit_info(UNIT(a
), "Automount point already active?");
731 trigger
= UNIT_TRIGGER(UNIT(a
));
733 log_unit_error(UNIT(a
), "Unit to trigger vanished.");
737 r
= manager_add_job(UNIT(a
)->manager
, JOB_START
, trigger
, JOB_REPLACE
, &error
, NULL
);
739 log_unit_warning(UNIT(a
), "Failed to queue mount startup job: %s", bus_error_message(&error
, r
));
744 automount_set_state(a
, AUTOMOUNT_RUNNING
);
748 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
751 static int automount_start(Unit
*u
) {
752 Automount
*a
= AUTOMOUNT(u
);
756 assert(a
->state
== AUTOMOUNT_DEAD
|| a
->state
== AUTOMOUNT_FAILED
);
758 if (path_is_mount_point(a
->where
, 0) > 0) {
759 log_unit_error(u
, "Path %s is already a mount point, refusing start.", a
->where
);
763 trigger
= UNIT_TRIGGER(u
);
764 if (!trigger
|| trigger
->load_state
!= UNIT_LOADED
) {
765 log_unit_error(u
, "Refusing to start, unit to trigger not loaded.");
769 a
->result
= AUTOMOUNT_SUCCESS
;
770 automount_enter_waiting(a
);
774 static int automount_stop(Unit
*u
) {
775 Automount
*a
= AUTOMOUNT(u
);
778 assert(a
->state
== AUTOMOUNT_WAITING
|| a
->state
== AUTOMOUNT_RUNNING
);
780 automount_enter_dead(a
, AUTOMOUNT_SUCCESS
);
784 static int automount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
785 Automount
*a
= AUTOMOUNT(u
);
794 unit_serialize_item(u
, f
, "state", automount_state_to_string(a
->state
));
795 unit_serialize_item(u
, f
, "result", automount_result_to_string(a
->result
));
796 unit_serialize_item_format(u
, f
, "dev-id", "%u", (unsigned) a
->dev_id
);
798 SET_FOREACH(p
, a
->tokens
, i
)
799 unit_serialize_item_format(u
, f
, "token", "%u", PTR_TO_UINT(p
));
800 SET_FOREACH(p
, a
->expire_tokens
, i
)
801 unit_serialize_item_format(u
, f
, "expire-token", "%u", PTR_TO_UINT(p
));
803 r
= unit_serialize_item_fd(u
, f
, fds
, "pipe-fd", a
->pipe_fd
);
810 static int automount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
811 Automount
*a
= AUTOMOUNT(u
);
817 if (streq(key
, "state")) {
818 AutomountState state
;
820 state
= automount_state_from_string(value
);
822 log_unit_debug(u
, "Failed to parse state value: %s", value
);
824 a
->deserialized_state
= state
;
825 } else if (streq(key
, "result")) {
828 f
= automount_result_from_string(value
);
830 log_unit_debug(u
, "Failed to parse result value: %s", value
);
831 else if (f
!= AUTOMOUNT_SUCCESS
)
834 } else if (streq(key
, "dev-id")) {
837 if (safe_atou(value
, &d
) < 0)
838 log_unit_debug(u
, "Failed to parse dev-id value: %s", value
);
840 a
->dev_id
= (unsigned) d
;
841 } else if (streq(key
, "token")) {
844 if (safe_atou(value
, &token
) < 0)
845 log_unit_debug(u
, "Failed to parse token value: %s", value
);
847 r
= set_ensure_allocated(&a
->tokens
, NULL
);
853 r
= set_put(a
->tokens
, UINT_TO_PTR(token
));
855 log_unit_error_errno(u
, r
, "Failed to add token to set: %m");
857 } else if (streq(key
, "expire-token")) {
860 if (safe_atou(value
, &token
) < 0)
861 log_unit_debug(u
, "Failed to parse token value: %s", value
);
863 r
= set_ensure_allocated(&a
->expire_tokens
, NULL
);
869 r
= set_put(a
->expire_tokens
, UINT_TO_PTR(token
));
871 log_unit_error_errno(u
, r
, "Failed to add expire token to set: %m");
873 } else if (streq(key
, "pipe-fd")) {
876 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
877 log_unit_debug(u
, "Failed to parse pipe-fd value: %s", value
);
879 safe_close(a
->pipe_fd
);
880 a
->pipe_fd
= fdset_remove(fds
, fd
);
883 log_unit_debug(u
, "Unknown serialization key: %s", key
);
888 static UnitActiveState
automount_active_state(Unit
*u
) {
891 return state_translation_table
[AUTOMOUNT(u
)->state
];
894 static const char *automount_sub_state_to_string(Unit
*u
) {
897 return automount_state_to_string(AUTOMOUNT(u
)->state
);
900 static bool automount_check_gc(Unit
*u
) {
903 if (!UNIT_TRIGGER(u
))
906 return UNIT_VTABLE(UNIT_TRIGGER(u
))->check_gc(UNIT_TRIGGER(u
));
909 static int automount_dispatch_io(sd_event_source
*s
, int fd
, uint32_t events
, void *userdata
) {
910 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
911 union autofs_v5_packet_union packet
;
912 Automount
*a
= AUTOMOUNT(userdata
);
918 assert(fd
== a
->pipe_fd
);
920 if (events
!= EPOLLIN
) {
921 log_unit_error(UNIT(a
), "Got invalid poll event %"PRIu32
" on pipe (fd=%d)", events
, fd
);
925 r
= loop_read_exact(a
->pipe_fd
, &packet
, sizeof(packet
), true);
927 log_unit_error_errno(UNIT(a
), r
, "Invalid read from pipe: %m");
931 switch (packet
.hdr
.type
) {
933 case autofs_ptype_missing_direct
:
935 if (packet
.v5_packet
.pid
> 0) {
936 _cleanup_free_
char *p
= NULL
;
938 get_process_comm(packet
.v5_packet
.pid
, &p
);
939 log_unit_info(UNIT(a
), "Got automount request for %s, triggered by %"PRIu32
" (%s)", a
->where
, packet
.v5_packet
.pid
, strna(p
));
941 log_unit_debug(UNIT(a
), "Got direct mount request on %s", a
->where
);
943 r
= set_ensure_allocated(&a
->tokens
, NULL
);
945 log_unit_error(UNIT(a
), "Failed to allocate token set.");
949 r
= set_put(a
->tokens
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
));
951 log_unit_error_errno(UNIT(a
), r
, "Failed to remember token: %m");
955 automount_enter_runnning(a
);
958 case autofs_ptype_expire_direct
:
959 log_unit_debug(UNIT(a
), "Got direct umount request on %s", a
->where
);
961 (void) sd_event_source_set_enabled(a
->expire_event_source
, SD_EVENT_OFF
);
963 r
= set_ensure_allocated(&a
->expire_tokens
, NULL
);
965 log_unit_error(UNIT(a
), "Failed to allocate token set.");
969 r
= set_put(a
->expire_tokens
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
));
971 log_unit_error_errno(UNIT(a
), r
, "Failed to remember token: %m");
975 /* Before we do anything, let's see if somebody is playing games with us? */
976 if (lstat(a
->where
, &st
) < 0) {
977 log_unit_warning_errno(UNIT(a
), errno
, "Failed to stat automount point: %m");
981 if (!S_ISDIR(st
.st_mode
) || st
.st_dev
== a
->dev_id
) {
982 log_unit_info(UNIT(a
), "Automount point already unmounted?");
983 automount_send_ready(a
, a
->expire_tokens
, 0);
987 trigger
= UNIT_TRIGGER(UNIT(a
));
989 log_unit_error(UNIT(a
), "Unit to trigger vanished.");
993 r
= manager_add_job(UNIT(a
)->manager
, JOB_STOP
, trigger
, JOB_REPLACE
, &error
, NULL
);
995 log_unit_warning(UNIT(a
), "Failed to queue umount startup job: %s", bus_error_message(&error
, r
));
1001 log_unit_error(UNIT(a
), "Received unknown automount request %i", packet
.hdr
.type
);
1008 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
1012 static void automount_shutdown(Manager
*m
) {
1015 m
->dev_autofs_fd
= safe_close(m
->dev_autofs_fd
);
1018 static void automount_reset_failed(Unit
*u
) {
1019 Automount
*a
= AUTOMOUNT(u
);
1023 if (a
->state
== AUTOMOUNT_FAILED
)
1024 automount_set_state(a
, AUTOMOUNT_DEAD
);
1026 a
->result
= AUTOMOUNT_SUCCESS
;
1029 static bool automount_supported(void) {
1030 static int supported
= -1;
1033 supported
= access("/dev/autofs", F_OK
) >= 0;
1038 static const char* const automount_result_table
[_AUTOMOUNT_RESULT_MAX
] = {
1039 [AUTOMOUNT_SUCCESS
] = "success",
1040 [AUTOMOUNT_FAILURE_RESOURCES
] = "resources"
1043 DEFINE_STRING_TABLE_LOOKUP(automount_result
, AutomountResult
);
1045 const UnitVTable automount_vtable
= {
1046 .object_size
= sizeof(Automount
),
1053 .init
= automount_init
,
1054 .load
= automount_load
,
1055 .done
= automount_done
,
1057 .coldplug
= automount_coldplug
,
1059 .dump
= automount_dump
,
1061 .start
= automount_start
,
1062 .stop
= automount_stop
,
1064 .serialize
= automount_serialize
,
1065 .deserialize_item
= automount_deserialize_item
,
1067 .active_state
= automount_active_state
,
1068 .sub_state_to_string
= automount_sub_state_to_string
,
1070 .check_gc
= automount_check_gc
,
1072 .reset_failed
= automount_reset_failed
,
1074 .bus_vtable
= bus_automount_vtable
,
1076 .shutdown
= automount_shutdown
,
1077 .supported
= automount_supported
,
1079 .status_message_formats
= {
1080 .finished_start_job
= {
1081 [JOB_DONE
] = "Set up automount %s.",
1082 [JOB_FAILED
] = "Failed to set up automount %s.",
1084 .finished_stop_job
= {
1085 [JOB_DONE
] = "Unset automount %s.",
1086 [JOB_FAILED
] = "Failed to unset automount %s.",