1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "sd-messages.h"
10 #include "alloc-util.h"
11 #include "dbus-mount.h"
12 #include "dbus-unit.h"
14 #include "exit-status.h"
15 #include "format-util.h"
16 #include "fstab-util.h"
17 #include "libmount-util.h"
21 #include "mount-setup.h"
23 #include "mountpoint-util.h"
24 #include "parse-util.h"
25 #include "path-util.h"
26 #include "process-util.h"
27 #include "serialize.h"
29 #include "string-table.h"
30 #include "string-util.h"
32 #include "unit-name.h"
35 #define RETRY_UMOUNT_MAX 32
37 static const UnitActiveState state_translation_table
[_MOUNT_STATE_MAX
] = {
38 [MOUNT_DEAD
] = UNIT_INACTIVE
,
39 [MOUNT_MOUNTING
] = UNIT_ACTIVATING
,
40 [MOUNT_MOUNTING_DONE
] = UNIT_ACTIVATING
,
41 [MOUNT_MOUNTED
] = UNIT_ACTIVE
,
42 [MOUNT_REMOUNTING
] = UNIT_RELOADING
,
43 [MOUNT_UNMOUNTING
] = UNIT_DEACTIVATING
,
44 [MOUNT_REMOUNTING_SIGTERM
] = UNIT_RELOADING
,
45 [MOUNT_REMOUNTING_SIGKILL
] = UNIT_RELOADING
,
46 [MOUNT_UNMOUNTING_SIGTERM
] = UNIT_DEACTIVATING
,
47 [MOUNT_UNMOUNTING_SIGKILL
] = UNIT_DEACTIVATING
,
48 [MOUNT_FAILED
] = UNIT_FAILED
,
49 [MOUNT_CLEANING
] = UNIT_MAINTENANCE
,
52 static int mount_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
53 static int mount_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
);
54 static int mount_process_proc_self_mountinfo(Manager
*m
);
56 static bool MOUNT_STATE_WITH_PROCESS(MountState state
) {
61 MOUNT_REMOUNTING_SIGTERM
,
62 MOUNT_REMOUNTING_SIGKILL
,
64 MOUNT_UNMOUNTING_SIGTERM
,
65 MOUNT_UNMOUNTING_SIGKILL
,
69 static bool mount_is_network(const MountParameters
*p
) {
72 if (fstab_test_option(p
->options
, "_netdev\0"))
75 if (p
->fstype
&& fstype_is_network(p
->fstype
))
81 static bool mount_is_loop(const MountParameters
*p
) {
84 if (fstab_test_option(p
->options
, "loop\0"))
90 static bool mount_is_bind(const MountParameters
*p
) {
93 if (fstab_test_option(p
->options
, "bind\0" "rbind\0"))
96 if (p
->fstype
&& STR_IN_SET(p
->fstype
, "bind", "rbind"))
102 static bool mount_is_bound_to_device(const Mount
*m
) {
103 const MountParameters
*p
;
105 if (m
->from_fragment
)
108 p
= &m
->parameters_proc_self_mountinfo
;
109 return fstab_test_option(p
->options
, "x-systemd.device-bound\0");
112 static bool mount_needs_quota(const MountParameters
*p
) {
115 /* Quotas are not enabled on network filesystems, but we want them, for example, on storage connected via
116 * iscsi. We hence don't use mount_is_network() here, as that would also return true for _netdev devices. */
117 if (p
->fstype
&& fstype_is_network(p
->fstype
))
120 if (mount_is_bind(p
))
123 return fstab_test_option(p
->options
,
124 "usrquota\0" "grpquota\0" "quota\0" "usrjquota\0" "grpjquota\0");
127 static void mount_init(Unit
*u
) {
131 assert(u
->load_state
== UNIT_STUB
);
133 m
->timeout_usec
= u
->manager
->default_timeout_start_usec
;
135 m
->exec_context
.std_output
= u
->manager
->default_std_output
;
136 m
->exec_context
.std_error
= u
->manager
->default_std_error
;
138 m
->directory_mode
= 0755;
140 /* We need to make sure that /usr/bin/mount is always called
141 * in the same process group as us, so that the autofs kernel
142 * side doesn't send us another mount request while we are
143 * already trying to comply its last one. */
144 m
->exec_context
.same_pgrp
= true;
146 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
148 u
->ignore_on_isolate
= true;
151 static int mount_arm_timer(Mount
*m
, usec_t usec
) {
156 if (m
->timer_event_source
) {
157 r
= sd_event_source_set_time(m
->timer_event_source
, usec
);
161 return sd_event_source_set_enabled(m
->timer_event_source
, SD_EVENT_ONESHOT
);
164 if (usec
== USEC_INFINITY
)
167 r
= sd_event_add_time(
168 UNIT(m
)->manager
->event
,
169 &m
->timer_event_source
,
172 mount_dispatch_timer
, m
);
176 (void) sd_event_source_set_description(m
->timer_event_source
, "mount-timer");
181 static void mount_unwatch_control_pid(Mount
*m
) {
184 if (m
->control_pid
<= 0)
187 unit_unwatch_pid(UNIT(m
), m
->control_pid
);
191 static void mount_parameters_done(MountParameters
*p
) {
194 p
->what
= mfree(p
->what
);
195 p
->options
= mfree(p
->options
);
196 p
->fstype
= mfree(p
->fstype
);
199 static void mount_done(Unit
*u
) {
204 m
->where
= mfree(m
->where
);
206 mount_parameters_done(&m
->parameters_proc_self_mountinfo
);
207 mount_parameters_done(&m
->parameters_fragment
);
209 m
->exec_runtime
= exec_runtime_unref(m
->exec_runtime
, false);
210 exec_command_done_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
211 m
->control_command
= NULL
;
213 dynamic_creds_unref(&m
->dynamic_creds
);
215 mount_unwatch_control_pid(m
);
217 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
220 _pure_
static MountParameters
* get_mount_parameters_fragment(Mount
*m
) {
223 if (m
->from_fragment
)
224 return &m
->parameters_fragment
;
229 _pure_
static MountParameters
* get_mount_parameters(Mount
*m
) {
232 if (m
->from_proc_self_mountinfo
)
233 return &m
->parameters_proc_self_mountinfo
;
235 return get_mount_parameters_fragment(m
);
238 static int update_parameters_proc_self_mountinfo(
242 const char *fstype
) {
247 p
= &m
->parameters_proc_self_mountinfo
;
249 r
= free_and_strdup(&p
->what
, what
);
253 q
= free_and_strdup(&p
->options
, options
);
257 w
= free_and_strdup(&p
->fstype
, fstype
);
261 return r
> 0 || q
> 0 || w
> 0;
264 static int mount_add_mount_dependencies(Mount
*m
) {
273 if (!path_equal(m
->where
, "/")) {
274 _cleanup_free_
char *parent
= NULL
;
276 /* Adds in links to other mount points that might lie further up in the hierarchy */
278 parent
= dirname_malloc(m
->where
);
282 r
= unit_require_mounts_for(UNIT(m
), parent
, UNIT_DEPENDENCY_IMPLICIT
);
287 /* Adds in dependencies to other mount points that might be needed for the source path (if this is a bind mount
288 * or a loop mount) to be available. */
289 pm
= get_mount_parameters_fragment(m
);
290 if (pm
&& pm
->what
&&
291 path_is_absolute(pm
->what
) &&
292 (mount_is_bind(pm
) || mount_is_loop(pm
) || !mount_is_network(pm
))) {
294 r
= unit_require_mounts_for(UNIT(m
), pm
->what
, UNIT_DEPENDENCY_FILE
);
299 /* Adds in dependencies to other units that use this path or paths further down in the hierarchy */
300 s
= manager_get_units_requiring_mounts_for(UNIT(m
)->manager
, m
->where
);
301 SET_FOREACH(other
, s
, i
) {
303 if (other
->load_state
!= UNIT_LOADED
)
306 if (other
== UNIT(m
))
309 r
= unit_add_dependency(other
, UNIT_AFTER
, UNIT(m
), true, UNIT_DEPENDENCY_PATH
);
313 if (UNIT(m
)->fragment_path
) {
314 /* If we have fragment configuration, then make this dependency required */
315 r
= unit_add_dependency(other
, UNIT_REQUIRES
, UNIT(m
), true, UNIT_DEPENDENCY_PATH
);
324 static int mount_add_device_dependencies(Mount
*m
) {
325 UnitDependencyMask mask
;
332 p
= get_mount_parameters(m
);
339 if (mount_is_bind(p
))
342 if (!is_device_path(p
->what
))
345 /* /dev/root is a really weird thing, it's not a real device,
346 * but just a path the kernel exports for the root file system
347 * specified on the kernel command line. Ignore it here. */
348 if (path_equal(p
->what
, "/dev/root"))
351 if (path_equal(m
->where
, "/"))
354 /* Mount units from /proc/self/mountinfo are not bound to devices
355 * by default since they're subject to races when devices are
356 * unplugged. But the user can still force this dep with an
357 * appropriate option (or udev property) so the mount units are
358 * automatically stopped when the device disappears suddenly. */
359 dep
= mount_is_bound_to_device(m
) ? UNIT_BINDS_TO
: UNIT_REQUIRES
;
361 /* We always use 'what' from /proc/self/mountinfo if mounted */
362 mask
= m
->from_proc_self_mountinfo
? UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
: UNIT_DEPENDENCY_FILE
;
364 r
= unit_add_node_dependency(UNIT(m
), p
->what
, false, dep
, mask
);
371 static int mount_add_quota_dependencies(Mount
*m
) {
372 UnitDependencyMask mask
;
378 if (!MANAGER_IS_SYSTEM(UNIT(m
)->manager
))
381 p
= get_mount_parameters_fragment(m
);
385 if (!mount_needs_quota(p
))
388 mask
= m
->from_fragment
? UNIT_DEPENDENCY_FILE
: UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
;
390 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTACHECK_SERVICE
, true, mask
);
394 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTAON_SERVICE
, true, mask
);
401 static bool mount_is_extrinsic(Mount
*m
) {
405 /* Returns true for all units that are "magic" and should be excluded from the usual start-up and shutdown
406 * dependencies. We call them "extrinsic" here, as they are generally mounted outside of the systemd dependency
407 * logic. We shouldn't attempt to manage them ourselves but it's fine if the user operates on them with us. */
409 if (!MANAGER_IS_SYSTEM(UNIT(m
)->manager
)) /* We only automatically manage mounts if we are in system mode */
412 if (PATH_IN_SET(m
->where
, /* Don't bother with the OS data itself */
417 if (PATH_STARTSWITH_SET(m
->where
,
418 "/run/initramfs", /* This should stay around from before we boot until after we shutdown */
419 "/proc", /* All of this is API VFS */
420 "/sys", /* … dito … */
421 "/dev")) /* … dito … */
424 /* If this is an initrd mount, and we are not in the initrd, then leave this around forever, too. */
425 p
= get_mount_parameters(m
);
426 if (p
&& fstab_test_option(p
->options
, "x-initrd.mount\0") && !in_initrd())
432 static int mount_add_default_dependencies(Mount
*m
) {
433 const char *after
, *before
;
434 UnitDependencyMask mask
;
441 if (!UNIT(m
)->default_dependencies
)
444 /* We do not add any default dependencies to /, /usr or /run/initramfs/, since they are guaranteed to stay
445 * mounted the whole time, since our system is on it. Also, don't bother with anything mounted below virtual
446 * file systems, it's also going to be virtual, and hence not worth the effort. */
447 if (mount_is_extrinsic(m
))
450 p
= get_mount_parameters(m
);
454 mask
= m
->from_fragment
? UNIT_DEPENDENCY_FILE
: UNIT_DEPENDENCY_MOUNTINFO_DEFAULT
;
455 nofail
= m
->from_fragment
? fstab_test_yes_no_option(m
->parameters_fragment
.options
, "nofail\0" "fail\0") : false;
457 if (mount_is_network(p
)) {
458 /* We order ourselves after network.target. This is
459 * primarily useful at shutdown: services that take
460 * down the network should order themselves before
461 * network.target, so that they are shut down only
462 * after this mount unit is stopped. */
464 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, SPECIAL_NETWORK_TARGET
, true, mask
);
468 /* We pull in network-online.target, and order
469 * ourselves after it. This is useful at start-up to
470 * actively pull in tools that want to be started
471 * before we start mounting network file systems, and
472 * whose purpose it is to delay this until the network
475 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_WANTS
, UNIT_AFTER
, SPECIAL_NETWORK_ONLINE_TARGET
, true, mask
);
479 after
= SPECIAL_REMOTE_FS_PRE_TARGET
;
480 before
= SPECIAL_REMOTE_FS_TARGET
;
482 after
= SPECIAL_LOCAL_FS_PRE_TARGET
;
483 before
= SPECIAL_LOCAL_FS_TARGET
;
487 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_BEFORE
, before
, true, mask
);
492 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, after
, true, mask
);
496 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, true, mask
);
500 /* If this is a tmpfs mount then we have to unmount it before we try to deactivate swaps */
501 if (streq_ptr(p
->fstype
, "tmpfs")) {
502 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, SPECIAL_SWAP_TARGET
, true, mask
);
510 static int mount_verify(Mount
*m
) {
511 _cleanup_free_
char *e
= NULL
;
516 assert(UNIT(m
)->load_state
== UNIT_LOADED
);
518 if (!m
->from_fragment
&& !m
->from_proc_self_mountinfo
&& !UNIT(m
)->perpetual
)
521 r
= unit_name_from_path(m
->where
, ".mount", &e
);
523 return log_unit_error_errno(UNIT(m
), r
, "Failed to generate unit name from mount path: %m");
525 if (!unit_has_name(UNIT(m
), e
)) {
526 log_unit_error(UNIT(m
), "Where= setting doesn't match unit name. Refusing.");
530 if (mount_point_is_api(m
->where
) || mount_point_ignore(m
->where
)) {
531 log_unit_error(UNIT(m
), "Cannot create mount unit for API file system %s. Refusing.", m
->where
);
535 p
= get_mount_parameters_fragment(m
);
537 log_unit_error(UNIT(m
), "What= setting is missing. Refusing.");
541 if (m
->exec_context
.pam_name
&& m
->kill_context
.kill_mode
!= KILL_CONTROL_GROUP
) {
542 log_unit_error(UNIT(m
), "Unit has PAM enabled. Kill mode must be set to control-group'. Refusing.");
549 static int mount_add_extras(Mount
*m
) {
555 /* Note: this call might be called after we already have been loaded once (and even when it has already been
556 * activated), in case data from /proc/self/mountinfo has changed. This means all code here needs to be ready
557 * to run with an already set up unit. */
559 if (u
->fragment_path
)
560 m
->from_fragment
= true;
563 r
= unit_name_to_path(u
->id
, &m
->where
);
568 path_simplify(m
->where
, false);
570 if (!u
->description
) {
571 r
= unit_set_description(u
, m
->where
);
576 r
= mount_add_device_dependencies(m
);
580 r
= mount_add_mount_dependencies(m
);
584 r
= mount_add_quota_dependencies(m
);
588 r
= unit_patch_contexts(u
);
592 r
= unit_add_exec_dependencies(u
, &m
->exec_context
);
596 r
= unit_set_default_slice(u
);
600 r
= mount_add_default_dependencies(m
);
607 static void mount_load_root_mount(Unit
*u
) {
610 if (!unit_has_name(u
, SPECIAL_ROOT_MOUNT
))
614 u
->default_dependencies
= false;
616 /* The stdio/kmsg bridge socket is on /, in order to avoid a dep loop, don't use kmsg logging for -.mount */
617 MOUNT(u
)->exec_context
.std_output
= EXEC_OUTPUT_NULL
;
618 MOUNT(u
)->exec_context
.std_input
= EXEC_INPUT_NULL
;
621 u
->description
= strdup("Root Mount");
624 static int mount_load(Unit
*u
) {
629 assert(u
->load_state
== UNIT_STUB
);
631 mount_load_root_mount(u
);
633 bool fragment_optional
= m
->from_proc_self_mountinfo
|| u
->perpetual
;
634 r
= unit_load_fragment_and_dropin(u
, !fragment_optional
);
636 /* Add in some extras. Note we do this in all cases (even if we failed to load the unit) when announced by the
637 * kernel, because we need some things to be set up no matter what when the kernel establishes a mount and thus
638 * we need to update the state in our unit to track it. After all, consider that we don't allow changing the
639 * 'slice' field for a unit once it is active. */
640 if (u
->load_state
== UNIT_LOADED
|| m
->from_proc_self_mountinfo
|| u
->perpetual
)
641 q
= mount_add_extras(m
);
647 if (u
->load_state
!= UNIT_LOADED
)
650 return mount_verify(m
);
653 static void mount_set_state(Mount
*m
, MountState state
) {
654 MountState old_state
;
657 if (m
->state
!= state
)
658 bus_unit_send_pending_change_signal(UNIT(m
), false);
660 old_state
= m
->state
;
663 if (!MOUNT_STATE_WITH_PROCESS(state
)) {
664 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
665 mount_unwatch_control_pid(m
);
666 m
->control_command
= NULL
;
667 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
670 if (state
!= old_state
)
671 log_unit_debug(UNIT(m
), "Changed %s -> %s", mount_state_to_string(old_state
), mount_state_to_string(state
));
673 unit_notify(UNIT(m
), state_translation_table
[old_state
], state_translation_table
[state
],
674 m
->reload_result
== MOUNT_SUCCESS
? 0 : UNIT_NOTIFY_RELOAD_FAILURE
);
677 static int mount_coldplug(Unit
*u
) {
679 MountState new_state
= MOUNT_DEAD
;
683 assert(m
->state
== MOUNT_DEAD
);
685 if (m
->deserialized_state
!= m
->state
)
686 new_state
= m
->deserialized_state
;
687 else if (m
->from_proc_self_mountinfo
)
688 new_state
= MOUNT_MOUNTED
;
690 if (new_state
== m
->state
)
693 if (m
->control_pid
> 0 &&
694 pid_is_unwaited(m
->control_pid
) &&
695 MOUNT_STATE_WITH_PROCESS(new_state
)) {
697 r
= unit_watch_pid(UNIT(m
), m
->control_pid
, false);
701 r
= mount_arm_timer(m
, usec_add(u
->state_change_timestamp
.monotonic
, m
->timeout_usec
));
706 if (!IN_SET(new_state
, MOUNT_DEAD
, MOUNT_FAILED
)) {
707 (void) unit_setup_dynamic_creds(u
);
708 (void) unit_setup_exec_runtime(u
);
711 mount_set_state(m
, new_state
);
715 static void mount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
716 char buf
[FORMAT_TIMESPAN_MAX
];
723 p
= get_mount_parameters(m
);
726 "%sMount State: %s\n"
728 "%sClean Result: %s\n"
731 "%sFile System Type: %s\n"
733 "%sFrom /proc/self/mountinfo: %s\n"
734 "%sFrom fragment: %s\n"
736 "%sDirectoryMode: %04o\n"
737 "%sSloppyOptions: %s\n"
738 "%sLazyUnmount: %s\n"
739 "%sForceUnmount: %s\n"
740 "%sTimeoutSec: %s\n",
741 prefix
, mount_state_to_string(m
->state
),
742 prefix
, mount_result_to_string(m
->result
),
743 prefix
, mount_result_to_string(m
->clean_result
),
745 prefix
, p
? strna(p
->what
) : "n/a",
746 prefix
, p
? strna(p
->fstype
) : "n/a",
747 prefix
, p
? strna(p
->options
) : "n/a",
748 prefix
, yes_no(m
->from_proc_self_mountinfo
),
749 prefix
, yes_no(m
->from_fragment
),
750 prefix
, yes_no(mount_is_extrinsic(m
)),
751 prefix
, m
->directory_mode
,
752 prefix
, yes_no(m
->sloppy_options
),
753 prefix
, yes_no(m
->lazy_unmount
),
754 prefix
, yes_no(m
->force_unmount
),
755 prefix
, format_timespan(buf
, sizeof(buf
), m
->timeout_usec
, USEC_PER_SEC
));
757 if (m
->control_pid
> 0)
759 "%sControl PID: "PID_FMT
"\n",
760 prefix
, m
->control_pid
);
762 exec_context_dump(&m
->exec_context
, f
, prefix
);
763 kill_context_dump(&m
->kill_context
, f
, prefix
);
764 cgroup_context_dump(UNIT(m
), f
, prefix
);
767 static int mount_spawn(Mount
*m
, ExecCommand
*c
, pid_t
*_pid
) {
769 _cleanup_(exec_params_clear
) ExecParameters exec_params
= {
770 .flags
= EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
,
783 r
= unit_prepare_exec(UNIT(m
));
787 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->timeout_usec
));
791 r
= unit_set_exec_params(UNIT(m
), &exec_params
);
795 r
= exec_spawn(UNIT(m
),
805 r
= unit_watch_pid(UNIT(m
), pid
, true);
814 static void mount_enter_dead(Mount
*m
, MountResult f
) {
817 if (m
->result
== MOUNT_SUCCESS
)
820 unit_log_result(UNIT(m
), m
->result
== MOUNT_SUCCESS
, mount_result_to_string(m
->result
));
821 mount_set_state(m
, m
->result
!= MOUNT_SUCCESS
? MOUNT_FAILED
: MOUNT_DEAD
);
823 m
->exec_runtime
= exec_runtime_unref(m
->exec_runtime
, true);
825 unit_destroy_runtime_directory(UNIT(m
), &m
->exec_context
);
827 unit_unref_uid_gid(UNIT(m
), true);
829 dynamic_creds_destroy(&m
->dynamic_creds
);
831 /* Any dependencies based on /proc/self/mountinfo are now stale */
832 unit_remove_dependencies(UNIT(m
), UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
);
835 static void mount_enter_mounted(Mount
*m
, MountResult f
) {
838 if (m
->result
== MOUNT_SUCCESS
)
841 mount_set_state(m
, MOUNT_MOUNTED
);
844 static void mount_enter_dead_or_mounted(Mount
*m
, MountResult f
) {
847 /* Enter DEAD or MOUNTED state, depending on what the kernel currently says about the mount point. We use this
848 * whenever we executed an operation, so that our internal state reflects what the kernel says again, after all
849 * ultimately we just mirror the kernel's internal state on this. */
851 if (m
->from_proc_self_mountinfo
)
852 mount_enter_mounted(m
, f
);
854 mount_enter_dead(m
, f
);
857 static int state_to_kill_operation(MountState state
) {
860 case MOUNT_REMOUNTING_SIGTERM
:
863 case MOUNT_UNMOUNTING_SIGTERM
:
864 return KILL_TERMINATE
;
866 case MOUNT_REMOUNTING_SIGKILL
:
867 case MOUNT_UNMOUNTING_SIGKILL
:
871 return _KILL_OPERATION_INVALID
;
875 static void mount_enter_signal(Mount
*m
, MountState state
, MountResult f
) {
880 if (m
->result
== MOUNT_SUCCESS
)
883 r
= unit_kill_context(
886 state_to_kill_operation(state
),
894 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->timeout_usec
));
898 mount_set_state(m
, state
);
899 } else if (state
== MOUNT_REMOUNTING_SIGTERM
&& m
->kill_context
.send_sigkill
)
900 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
901 else if (IN_SET(state
, MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
))
902 mount_enter_mounted(m
, MOUNT_SUCCESS
);
903 else if (state
== MOUNT_UNMOUNTING_SIGTERM
&& m
->kill_context
.send_sigkill
)
904 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
906 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
911 log_unit_warning_errno(UNIT(m
), r
, "Failed to kill processes: %m");
912 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
915 static void mount_enter_unmounting(Mount
*m
) {
920 /* Start counting our attempts */
921 if (!IN_SET(m
->state
,
923 MOUNT_UNMOUNTING_SIGTERM
,
924 MOUNT_UNMOUNTING_SIGKILL
))
925 m
->n_retry_umount
= 0;
927 m
->control_command_id
= MOUNT_EXEC_UNMOUNT
;
928 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_UNMOUNT
;
930 r
= exec_command_set(m
->control_command
, UMOUNT_PATH
, m
->where
, "-c", NULL
);
931 if (r
>= 0 && m
->lazy_unmount
)
932 r
= exec_command_append(m
->control_command
, "-l", NULL
);
933 if (r
>= 0 && m
->force_unmount
)
934 r
= exec_command_append(m
->control_command
, "-f", NULL
);
938 mount_unwatch_control_pid(m
);
940 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
944 mount_set_state(m
, MOUNT_UNMOUNTING
);
949 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'umount' task: %m");
950 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
953 static void mount_enter_mounting(Mount
*m
) {
959 r
= unit_fail_if_noncanonical(UNIT(m
), m
->where
);
963 (void) mkdir_p_label(m
->where
, m
->directory_mode
);
965 unit_warn_if_dir_nonempty(UNIT(m
), m
->where
);
966 unit_warn_leftover_processes(UNIT(m
));
968 m
->control_command_id
= MOUNT_EXEC_MOUNT
;
969 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_MOUNT
;
971 /* Create the source directory for bind-mounts if needed */
972 p
= get_mount_parameters_fragment(m
);
973 if (p
&& mount_is_bind(p
))
974 (void) mkdir_p_label(p
->what
, m
->directory_mode
);
977 _cleanup_free_
char *opts
= NULL
;
979 r
= fstab_filter_options(p
->options
, "nofail\0" "noauto\0" "auto\0", NULL
, NULL
, &opts
);
983 r
= exec_command_set(m
->control_command
, MOUNT_PATH
, p
->what
, m
->where
, NULL
);
984 if (r
>= 0 && m
->sloppy_options
)
985 r
= exec_command_append(m
->control_command
, "-s", NULL
);
986 if (r
>= 0 && p
->fstype
)
987 r
= exec_command_append(m
->control_command
, "-t", p
->fstype
, NULL
);
988 if (r
>= 0 && !isempty(opts
))
989 r
= exec_command_append(m
->control_command
, "-o", opts
, NULL
);
995 mount_unwatch_control_pid(m
);
997 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
1001 mount_set_state(m
, MOUNT_MOUNTING
);
1006 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'mount' task: %m");
1007 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
1010 static void mount_set_reload_result(Mount
*m
, MountResult result
) {
1013 /* Only store the first error we encounter */
1014 if (m
->reload_result
!= MOUNT_SUCCESS
)
1017 m
->reload_result
= result
;
1020 static void mount_enter_remounting(Mount
*m
) {
1026 /* Reset reload result when we are about to start a new remount operation */
1027 m
->reload_result
= MOUNT_SUCCESS
;
1029 m
->control_command_id
= MOUNT_EXEC_REMOUNT
;
1030 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_REMOUNT
;
1032 p
= get_mount_parameters_fragment(m
);
1037 o
= strjoina("remount,", p
->options
);
1041 r
= exec_command_set(m
->control_command
, MOUNT_PATH
,
1044 if (r
>= 0 && m
->sloppy_options
)
1045 r
= exec_command_append(m
->control_command
, "-s", NULL
);
1046 if (r
>= 0 && p
->fstype
)
1047 r
= exec_command_append(m
->control_command
, "-t", p
->fstype
, NULL
);
1053 mount_unwatch_control_pid(m
);
1055 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
1059 mount_set_state(m
, MOUNT_REMOUNTING
);
1064 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'remount' task: %m");
1065 mount_set_reload_result(m
, MOUNT_FAILURE_RESOURCES
);
1066 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1069 static void mount_cycle_clear(Mount
*m
) {
1072 /* Clear all state we shall forget for this new cycle */
1074 m
->result
= MOUNT_SUCCESS
;
1075 m
->reload_result
= MOUNT_SUCCESS
;
1076 exec_command_reset_status_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
1077 UNIT(m
)->reset_accounting
= true;
1080 static int mount_start(Unit
*u
) {
1081 Mount
*m
= MOUNT(u
);
1086 /* We cannot fulfill this request right now, try again later
1088 if (IN_SET(m
->state
,
1090 MOUNT_UNMOUNTING_SIGTERM
,
1091 MOUNT_UNMOUNTING_SIGKILL
,
1095 /* Already on it! */
1096 if (m
->state
== MOUNT_MOUNTING
)
1099 assert(IN_SET(m
->state
, MOUNT_DEAD
, MOUNT_FAILED
));
1101 r
= unit_test_start_limit(u
);
1103 mount_enter_dead(m
, MOUNT_FAILURE_START_LIMIT_HIT
);
1107 r
= unit_acquire_invocation_id(u
);
1111 mount_cycle_clear(m
);
1112 mount_enter_mounting(m
);
1117 static int mount_stop(Unit
*u
) {
1118 Mount
*m
= MOUNT(u
);
1124 case MOUNT_UNMOUNTING
:
1125 case MOUNT_UNMOUNTING_SIGKILL
:
1126 case MOUNT_UNMOUNTING_SIGTERM
:
1130 case MOUNT_MOUNTING
:
1131 case MOUNT_MOUNTING_DONE
:
1132 case MOUNT_REMOUNTING
:
1133 /* If we are still waiting for /bin/mount, we go directly into kill mode. */
1134 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_SUCCESS
);
1137 case MOUNT_REMOUNTING_SIGTERM
:
1138 /* If we are already waiting for a hung remount, convert this to the matching unmounting state */
1139 mount_set_state(m
, MOUNT_UNMOUNTING_SIGTERM
);
1142 case MOUNT_REMOUNTING_SIGKILL
:
1144 mount_set_state(m
, MOUNT_UNMOUNTING_SIGKILL
);
1148 mount_enter_unmounting(m
);
1151 case MOUNT_CLEANING
:
1152 /* If we are currently cleaning, then abort it, brutally. */
1153 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
1157 assert_not_reached("Unexpected state.");
1161 static int mount_reload(Unit
*u
) {
1162 Mount
*m
= MOUNT(u
);
1165 assert(m
->state
== MOUNT_MOUNTED
);
1167 mount_enter_remounting(m
);
1172 static int mount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1173 Mount
*m
= MOUNT(u
);
1179 (void) serialize_item(f
, "state", mount_state_to_string(m
->state
));
1180 (void) serialize_item(f
, "result", mount_result_to_string(m
->result
));
1181 (void) serialize_item(f
, "reload-result", mount_result_to_string(m
->reload_result
));
1182 (void) serialize_item_format(f
, "n-retry-umount", "%u", m
->n_retry_umount
);
1184 if (m
->control_pid
> 0)
1185 (void) serialize_item_format(f
, "control-pid", PID_FMT
, m
->control_pid
);
1187 if (m
->control_command_id
>= 0)
1188 (void) serialize_item(f
, "control-command", mount_exec_command_to_string(m
->control_command_id
));
1193 static int mount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1194 Mount
*m
= MOUNT(u
);
1202 if (streq(key
, "state")) {
1205 if ((state
= mount_state_from_string(value
)) < 0)
1206 log_unit_debug(u
, "Failed to parse state value: %s", value
);
1208 m
->deserialized_state
= state
;
1210 } else if (streq(key
, "result")) {
1213 f
= mount_result_from_string(value
);
1215 log_unit_debug(u
, "Failed to parse result value: %s", value
);
1216 else if (f
!= MOUNT_SUCCESS
)
1219 } else if (streq(key
, "reload-result")) {
1222 f
= mount_result_from_string(value
);
1224 log_unit_debug(u
, "Failed to parse reload result value: %s", value
);
1225 else if (f
!= MOUNT_SUCCESS
)
1226 m
->reload_result
= f
;
1228 } else if (streq(key
, "n-retry-umount")) {
1230 r
= safe_atou(value
, &m
->n_retry_umount
);
1232 log_unit_debug(u
, "Failed to parse n-retry-umount value: %s", value
);
1234 } else if (streq(key
, "control-pid")) {
1236 if (parse_pid(value
, &m
->control_pid
) < 0)
1237 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
1239 } else if (streq(key
, "control-command")) {
1240 MountExecCommand id
;
1242 id
= mount_exec_command_from_string(value
);
1244 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
1246 m
->control_command_id
= id
;
1247 m
->control_command
= m
->exec_command
+ id
;
1250 log_unit_debug(u
, "Unknown serialization key: %s", key
);
1255 _pure_
static UnitActiveState
mount_active_state(Unit
*u
) {
1258 return state_translation_table
[MOUNT(u
)->state
];
1261 _pure_
static const char *mount_sub_state_to_string(Unit
*u
) {
1264 return mount_state_to_string(MOUNT(u
)->state
);
1267 _pure_
static bool mount_may_gc(Unit
*u
) {
1268 Mount
*m
= MOUNT(u
);
1272 if (m
->from_proc_self_mountinfo
)
1278 static void mount_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1279 Mount
*m
= MOUNT(u
);
1285 if (pid
!= m
->control_pid
)
1288 /* So here's the thing, we really want to know before /usr/bin/mount or /usr/bin/umount exit whether
1289 * they established/remove a mount. This is important when mounting, but even more so when unmounting
1290 * since we need to deal with nested mounts and otherwise cannot safely determine whether to repeat
1291 * the unmounts. In theory, the kernel fires /proc/self/mountinfo changes off before returning from
1292 * the mount() or umount() syscalls, and thus we should see the changes to the proc file before we
1293 * process the waitid() for the /usr/bin/(u)mount processes. However, this is unfortunately racy: we
1294 * have to waitid() for processes using P_ALL (since we need to reap unexpected children that got
1295 * reparented to PID 1), but when using P_ALL we might end up reaping processes that terminated just
1296 * instants ago, i.e. already after our last event loop iteration (i.e. after the last point we might
1297 * have noticed /proc/self/mountinfo events via epoll). This means event loop priorities for
1298 * processing SIGCHLD vs. /proc/self/mountinfo IO events are not as relevant as we want. To fix that
1299 * race, let's explicitly scan /proc/self/mountinfo before we start processing /usr/bin/(u)mount
1300 * dying. It's ugly, but it makes our ordering systematic again, and makes sure we always see
1301 * /proc/self/mountinfo changes before our mount/umount exits. */
1302 (void) mount_process_proc_self_mountinfo(u
->manager
);
1306 if (is_clean_exit(code
, status
, EXIT_CLEAN_COMMAND
, NULL
))
1308 else if (code
== CLD_EXITED
)
1309 f
= MOUNT_FAILURE_EXIT_CODE
;
1310 else if (code
== CLD_KILLED
)
1311 f
= MOUNT_FAILURE_SIGNAL
;
1312 else if (code
== CLD_DUMPED
)
1313 f
= MOUNT_FAILURE_CORE_DUMP
;
1315 assert_not_reached("Unknown code");
1317 if (IN_SET(m
->state
, MOUNT_REMOUNTING
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_REMOUNTING_SIGTERM
))
1318 mount_set_reload_result(m
, f
);
1319 else if (m
->result
== MOUNT_SUCCESS
)
1322 if (m
->control_command
) {
1323 exec_status_exit(&m
->control_command
->exec_status
, &m
->exec_context
, pid
, code
, status
);
1325 m
->control_command
= NULL
;
1326 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
1329 unit_log_process_exit(
1332 mount_exec_command_to_string(m
->control_command_id
),
1336 /* Note that due to the io event priority logic, we can be sure the new mountinfo is loaded
1337 * before we process the SIGCHLD for the mount command. */
1341 case MOUNT_MOUNTING
:
1342 /* Our mount point has not appeared in mountinfo. Something went wrong. */
1344 if (f
== MOUNT_SUCCESS
) {
1345 /* Either /bin/mount has an unexpected definition of success,
1346 * or someone raced us and we lost. */
1347 log_unit_warning(UNIT(m
), "Mount process finished, but there is no mount.");
1348 f
= MOUNT_FAILURE_PROTOCOL
;
1350 mount_enter_dead(m
, f
);
1353 case MOUNT_MOUNTING_DONE
:
1354 mount_enter_mounted(m
, f
);
1357 case MOUNT_REMOUNTING
:
1358 case MOUNT_REMOUNTING_SIGTERM
:
1359 case MOUNT_REMOUNTING_SIGKILL
:
1360 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1363 case MOUNT_UNMOUNTING
:
1365 if (f
== MOUNT_SUCCESS
&& m
->from_proc_self_mountinfo
) {
1367 /* Still a mount point? If so, let's try again. Most likely there were multiple mount points
1368 * stacked on top of each other. We might exceed the timeout specified by the user overall,
1369 * but we will stop as soon as any one umount times out. */
1371 if (m
->n_retry_umount
< RETRY_UMOUNT_MAX
) {
1372 log_unit_debug(u
, "Mount still present, trying again.");
1373 m
->n_retry_umount
++;
1374 mount_enter_unmounting(m
);
1376 log_unit_warning(u
, "Mount still present after %u attempts to unmount, giving up.", m
->n_retry_umount
);
1377 mount_enter_mounted(m
, f
);
1380 mount_enter_dead_or_mounted(m
, f
);
1384 case MOUNT_UNMOUNTING_SIGKILL
:
1385 case MOUNT_UNMOUNTING_SIGTERM
:
1386 mount_enter_dead_or_mounted(m
, f
);
1389 case MOUNT_CLEANING
:
1390 if (m
->clean_result
== MOUNT_SUCCESS
)
1391 m
->clean_result
= f
;
1393 mount_enter_dead(m
, MOUNT_SUCCESS
);
1397 assert_not_reached("Uh, control process died at wrong time.");
1400 /* Notify clients about changed exit status */
1401 unit_add_to_dbus_queue(u
);
1404 static int mount_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
1405 Mount
*m
= MOUNT(userdata
);
1408 assert(m
->timer_event_source
== source
);
1412 case MOUNT_MOUNTING
:
1413 case MOUNT_MOUNTING_DONE
:
1414 log_unit_warning(UNIT(m
), "Mounting timed out. Terminating.");
1415 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1418 case MOUNT_REMOUNTING
:
1419 log_unit_warning(UNIT(m
), "Remounting timed out. Terminating remount process.");
1420 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1421 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGTERM
, MOUNT_SUCCESS
);
1424 case MOUNT_REMOUNTING_SIGTERM
:
1425 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1427 if (m
->kill_context
.send_sigkill
) {
1428 log_unit_warning(UNIT(m
), "Remounting timed out. Killing.");
1429 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
1431 log_unit_warning(UNIT(m
), "Remounting timed out. Skipping SIGKILL. Ignoring.");
1432 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1436 case MOUNT_REMOUNTING_SIGKILL
:
1437 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1439 log_unit_warning(UNIT(m
), "Mount process still around after SIGKILL. Ignoring.");
1440 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1443 case MOUNT_UNMOUNTING
:
1444 log_unit_warning(UNIT(m
), "Unmounting timed out. Terminating.");
1445 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1448 case MOUNT_UNMOUNTING_SIGTERM
:
1449 if (m
->kill_context
.send_sigkill
) {
1450 log_unit_warning(UNIT(m
), "Mount process timed out. Killing.");
1451 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_FAILURE_TIMEOUT
);
1453 log_unit_warning(UNIT(m
), "Mount process timed out. Skipping SIGKILL. Ignoring.");
1454 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1458 case MOUNT_UNMOUNTING_SIGKILL
:
1459 log_unit_warning(UNIT(m
), "Mount process still around after SIGKILL. Ignoring.");
1460 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1463 case MOUNT_CLEANING
:
1464 log_unit_warning(UNIT(m
), "Cleaning timed out. killing.");
1466 if (m
->clean_result
== MOUNT_SUCCESS
)
1467 m
->clean_result
= MOUNT_FAILURE_TIMEOUT
;
1469 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, 0);
1473 assert_not_reached("Timeout at wrong time.");
1479 static int mount_setup_new_unit(
1484 const char *options
,
1486 MountProcFlags
*ret_flags
,
1489 _cleanup_(unit_freep
) Unit
*u
= NULL
;
1497 r
= unit_new_for_name(m
, sizeof(Mount
), name
, &u
);
1501 r
= free_and_strdup(&u
->source_path
, "/proc/self/mountinfo");
1505 r
= free_and_strdup(&MOUNT(u
)->where
, where
);
1509 r
= update_parameters_proc_self_mountinfo(MOUNT(u
), what
, options
, fstype
);
1513 /* This unit was generated because /proc/self/mountinfo reported it. Remember this, so that by the time we load
1514 * the unit file for it (and thus add in extra deps right after) we know what source to attributes the deps
1516 MOUNT(u
)->from_proc_self_mountinfo
= true;
1518 /* We have only allocated the stub now, let's enqueue this unit for loading now, so that everything else is
1520 unit_add_to_load_queue(u
);
1522 *ret_flags
= MOUNT_PROC_IS_MOUNTED
| MOUNT_PROC_JUST_MOUNTED
| MOUNT_PROC_JUST_CHANGED
;
1527 static int mount_setup_existing_unit(
1531 const char *options
,
1533 MountProcFlags
*ret_flags
) {
1535 MountProcFlags flags
= MOUNT_PROC_IS_MOUNTED
;
1541 if (!MOUNT(u
)->where
) {
1542 MOUNT(u
)->where
= strdup(where
);
1543 if (!MOUNT(u
)->where
)
1547 r
= update_parameters_proc_self_mountinfo(MOUNT(u
), what
, options
, fstype
);
1551 flags
|= MOUNT_PROC_JUST_CHANGED
;
1553 if (!MOUNT(u
)->from_proc_self_mountinfo
|| FLAGS_SET(MOUNT(u
)->proc_flags
, MOUNT_PROC_JUST_MOUNTED
))
1554 flags
|= MOUNT_PROC_JUST_MOUNTED
;
1556 MOUNT(u
)->from_proc_self_mountinfo
= true;
1558 if (IN_SET(u
->load_state
, UNIT_NOT_FOUND
, UNIT_BAD_SETTING
, UNIT_ERROR
)) {
1559 /* The unit was previously not found or otherwise not loaded. Now that the unit shows up in
1560 * /proc/self/mountinfo we should reconsider it this, hence set it to UNIT_LOADED. */
1561 u
->load_state
= UNIT_LOADED
;
1564 flags
|= MOUNT_PROC_JUST_CHANGED
;
1567 if (FLAGS_SET(flags
, MOUNT_PROC_JUST_CHANGED
)) {
1568 /* If things changed, then make sure that all deps are regenerated. Let's
1569 * first remove all automatic deps, and then add in the new ones. */
1571 unit_remove_dependencies(u
, UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
);
1573 r
= mount_add_extras(MOUNT(u
));
1582 static int mount_setup_unit(
1586 const char *options
,
1590 _cleanup_free_
char *e
= NULL
;
1591 MountProcFlags flags
;
1601 /* Ignore API mount points. They should never be referenced in
1602 * dependencies ever. */
1603 if (mount_point_is_api(where
) || mount_point_ignore(where
))
1606 if (streq(fstype
, "autofs"))
1609 /* probably some kind of swap, ignore */
1610 if (!is_path(where
))
1613 r
= unit_name_from_path(where
, ".mount", &e
);
1615 return log_error_errno(r
, "Failed to generate unit name from path '%s': %m", where
);
1617 u
= manager_get_unit(m
, e
);
1619 r
= mount_setup_existing_unit(u
, what
, where
, options
, fstype
, &flags
);
1621 /* First time we see this mount point meaning that it's not been initiated by a mount unit but rather
1622 * by the sysadmin having called mount(8) directly. */
1623 r
= mount_setup_new_unit(m
, e
, what
, where
, options
, fstype
, &flags
, &u
);
1625 return log_warning_errno(r
, "Failed to set up mount unit: %m");
1627 /* If the mount changed properties or state, let's notify our clients */
1628 if (flags
& (MOUNT_PROC_JUST_CHANGED
|MOUNT_PROC_JUST_MOUNTED
))
1629 unit_add_to_dbus_queue(u
);
1632 MOUNT(u
)->proc_flags
= flags
;
1637 static int mount_load_proc_self_mountinfo(Manager
*m
, bool set_flags
) {
1638 _cleanup_(mnt_free_tablep
) struct libmnt_table
*table
= NULL
;
1639 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*iter
= NULL
;
1644 r
= libmount_parse(NULL
, NULL
, &table
, &iter
);
1646 return log_error_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
1649 struct libmnt_fs
*fs
;
1650 const char *device
, *path
, *options
, *fstype
;
1652 r
= mnt_table_next_fs(table
, iter
, &fs
);
1656 return log_error_errno(r
, "Failed to get next entry from /proc/self/mountinfo: %m");
1658 device
= mnt_fs_get_source(fs
);
1659 path
= mnt_fs_get_target(fs
);
1660 options
= mnt_fs_get_options(fs
);
1661 fstype
= mnt_fs_get_fstype(fs
);
1663 if (!device
|| !path
)
1666 device_found_node(m
, device
, DEVICE_FOUND_MOUNT
, DEVICE_FOUND_MOUNT
);
1668 (void) mount_setup_unit(m
, device
, path
, options
, fstype
, set_flags
);
1674 static void mount_shutdown(Manager
*m
) {
1677 m
->mount_event_source
= sd_event_source_unref(m
->mount_event_source
);
1679 mnt_unref_monitor(m
->mount_monitor
);
1680 m
->mount_monitor
= NULL
;
1683 static int mount_get_timeout(Unit
*u
, usec_t
*timeout
) {
1684 Mount
*m
= MOUNT(u
);
1688 if (!m
->timer_event_source
)
1691 r
= sd_event_source_get_time(m
->timer_event_source
, &t
);
1694 if (t
== USEC_INFINITY
)
1701 static void mount_enumerate_perpetual(Manager
*m
) {
1707 /* Whatever happens, we know for sure that the root directory is around, and cannot go away. Let's
1708 * unconditionally synthesize it here and mark it as perpetual. */
1710 u
= manager_get_unit(m
, SPECIAL_ROOT_MOUNT
);
1712 r
= unit_new_for_name(m
, sizeof(Mount
), SPECIAL_ROOT_MOUNT
, &u
);
1714 log_error_errno(r
, "Failed to allocate the special " SPECIAL_ROOT_MOUNT
" unit: %m");
1719 u
->perpetual
= true;
1720 MOUNT(u
)->deserialized_state
= MOUNT_MOUNTED
;
1722 unit_add_to_load_queue(u
);
1723 unit_add_to_dbus_queue(u
);
1726 static bool mount_is_mounted(Mount
*m
) {
1729 return UNIT(m
)->perpetual
|| FLAGS_SET(m
->proc_flags
, MOUNT_PROC_IS_MOUNTED
);
1732 static void mount_enumerate(Manager
*m
) {
1739 if (!m
->mount_monitor
) {
1742 m
->mount_monitor
= mnt_new_monitor();
1743 if (!m
->mount_monitor
) {
1748 r
= mnt_monitor_enable_kernel(m
->mount_monitor
, 1);
1750 log_error_errno(r
, "Failed to enable watching of kernel mount events: %m");
1754 r
= mnt_monitor_enable_userspace(m
->mount_monitor
, 1, NULL
);
1756 log_error_errno(r
, "Failed to enable watching of userspace mount events: %m");
1760 /* mnt_unref_monitor() will close the fd */
1761 fd
= r
= mnt_monitor_get_fd(m
->mount_monitor
);
1763 log_error_errno(r
, "Failed to acquire watch file descriptor: %m");
1767 r
= sd_event_add_io(m
->event
, &m
->mount_event_source
, fd
, EPOLLIN
, mount_dispatch_io
, m
);
1769 log_error_errno(r
, "Failed to watch mount file descriptor: %m");
1773 r
= sd_event_source_set_priority(m
->mount_event_source
, SD_EVENT_PRIORITY_NORMAL
-10);
1775 log_error_errno(r
, "Failed to adjust mount watch priority: %m");
1779 (void) sd_event_source_set_description(m
->mount_event_source
, "mount-monitor-dispatch");
1782 r
= mount_load_proc_self_mountinfo(m
, false);
1792 static int drain_libmount(Manager
*m
) {
1793 bool rescan
= false;
1798 /* Drain all events and verify that the event is valid.
1800 * Note that libmount also monitors /run/mount mkdir if the directory does not exist yet. The mkdir
1801 * may generate event which is irrelevant for us.
1803 * error: r < 0; valid: r == 0, false positive: r == 1 */
1805 r
= mnt_monitor_next_change(m
->mount_monitor
, NULL
, NULL
);
1807 return log_error_errno(r
, "Failed to drain libmount events: %m");
1815 static int mount_process_proc_self_mountinfo(Manager
*m
) {
1816 _cleanup_set_free_free_ Set
*around
= NULL
, *gone
= NULL
;
1824 r
= drain_libmount(m
);
1828 r
= mount_load_proc_self_mountinfo(m
, true);
1830 /* Reset flags, just in case, for later calls */
1831 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
])
1832 MOUNT(u
)->proc_flags
= 0;
1837 manager_dispatch_load_queue(m
);
1839 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
]) {
1840 Mount
*mount
= MOUNT(u
);
1842 if (!mount_is_mounted(mount
)) {
1844 /* A mount point is not around right now. It
1845 * might be gone, or might never have
1848 if (mount
->from_proc_self_mountinfo
&&
1849 mount
->parameters_proc_self_mountinfo
.what
) {
1851 /* Remember that this device might just have disappeared */
1852 if (set_ensure_allocated(&gone
, &path_hash_ops
) < 0 ||
1853 set_put_strdup(gone
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
1854 log_oom(); /* we don't care too much about OOM here... */
1857 mount
->from_proc_self_mountinfo
= false;
1858 assert_se(update_parameters_proc_self_mountinfo(mount
, NULL
, NULL
, NULL
) >= 0);
1860 switch (mount
->state
) {
1863 /* This has just been unmounted by somebody else, follow the state change. */
1864 mount_enter_dead(mount
, MOUNT_SUCCESS
);
1871 } else if (mount
->proc_flags
& (MOUNT_PROC_JUST_MOUNTED
|MOUNT_PROC_JUST_CHANGED
)) {
1873 /* A mount point was added or changed */
1875 switch (mount
->state
) {
1880 /* This has just been mounted by somebody else, follow the state change, but let's
1881 * generate a new invocation ID for this implicitly and automatically. */
1882 (void) unit_acquire_invocation_id(u
);
1883 mount_cycle_clear(mount
);
1884 mount_enter_mounted(mount
, MOUNT_SUCCESS
);
1887 case MOUNT_MOUNTING
:
1888 mount_set_state(mount
, MOUNT_MOUNTING_DONE
);
1892 /* Nothing really changed, but let's
1893 * issue an notification call
1894 * nonetheless, in case somebody is
1895 * waiting for this. (e.g. file system
1896 * ro/rw remounts.) */
1897 mount_set_state(mount
, mount
->state
);
1902 if (mount_is_mounted(mount
) &&
1903 mount
->from_proc_self_mountinfo
&&
1904 mount
->parameters_proc_self_mountinfo
.what
) {
1905 /* Track devices currently used */
1907 if (set_ensure_allocated(&around
, &path_hash_ops
) < 0 ||
1908 set_put_strdup(around
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
1912 /* Reset the flags for later calls */
1913 mount
->proc_flags
= 0;
1916 SET_FOREACH(what
, gone
, i
) {
1917 if (set_contains(around
, what
))
1920 /* Let the device units know that the device is no longer mounted */
1921 device_found_node(m
, what
, 0, DEVICE_FOUND_MOUNT
);
1927 static int mount_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
1928 Manager
*m
= userdata
;
1931 assert(revents
& EPOLLIN
);
1933 return mount_process_proc_self_mountinfo(m
);
1936 static void mount_reset_failed(Unit
*u
) {
1937 Mount
*m
= MOUNT(u
);
1941 if (m
->state
== MOUNT_FAILED
)
1942 mount_set_state(m
, MOUNT_DEAD
);
1944 m
->result
= MOUNT_SUCCESS
;
1945 m
->reload_result
= MOUNT_SUCCESS
;
1946 m
->clean_result
= MOUNT_SUCCESS
;
1949 static int mount_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
1950 Mount
*m
= MOUNT(u
);
1954 return unit_kill_common(u
, who
, signo
, -1, m
->control_pid
, error
);
1957 static int mount_control_pid(Unit
*u
) {
1958 Mount
*m
= MOUNT(u
);
1962 return m
->control_pid
;
1965 static int mount_clean(Unit
*u
, ExecCleanMask mask
) {
1966 _cleanup_strv_free_
char **l
= NULL
;
1967 Mount
*m
= MOUNT(u
);
1973 if (m
->state
!= MOUNT_DEAD
)
1976 r
= exec_context_get_clean_directories(&m
->exec_context
, u
->manager
->prefix
, mask
, &l
);
1980 if (strv_isempty(l
))
1983 mount_unwatch_control_pid(m
);
1984 m
->clean_result
= MOUNT_SUCCESS
;
1985 m
->control_command
= NULL
;
1986 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
1988 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->exec_context
.timeout_clean_usec
));
1992 r
= unit_fork_and_watch_rm_rf(u
, l
, &m
->control_pid
);
1996 mount_set_state(m
, MOUNT_CLEANING
);
2001 log_unit_warning_errno(u
, r
, "Failed to initiate cleaning: %m");
2002 m
->clean_result
= MOUNT_FAILURE_RESOURCES
;
2003 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
2007 static int mount_can_clean(Unit
*u
, ExecCleanMask
*ret
) {
2008 Mount
*m
= MOUNT(u
);
2012 return exec_context_get_clean_mask(&m
->exec_context
, ret
);
2015 static const char* const mount_exec_command_table
[_MOUNT_EXEC_COMMAND_MAX
] = {
2016 [MOUNT_EXEC_MOUNT
] = "ExecMount",
2017 [MOUNT_EXEC_UNMOUNT
] = "ExecUnmount",
2018 [MOUNT_EXEC_REMOUNT
] = "ExecRemount",
2021 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command
, MountExecCommand
);
2023 static const char* const mount_result_table
[_MOUNT_RESULT_MAX
] = {
2024 [MOUNT_SUCCESS
] = "success",
2025 [MOUNT_FAILURE_RESOURCES
] = "resources",
2026 [MOUNT_FAILURE_TIMEOUT
] = "timeout",
2027 [MOUNT_FAILURE_EXIT_CODE
] = "exit-code",
2028 [MOUNT_FAILURE_SIGNAL
] = "signal",
2029 [MOUNT_FAILURE_CORE_DUMP
] = "core-dump",
2030 [MOUNT_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
2031 [MOUNT_FAILURE_PROTOCOL
] = "protocol",
2034 DEFINE_STRING_TABLE_LOOKUP(mount_result
, MountResult
);
2036 const UnitVTable mount_vtable
= {
2037 .object_size
= sizeof(Mount
),
2038 .exec_context_offset
= offsetof(Mount
, exec_context
),
2039 .cgroup_context_offset
= offsetof(Mount
, cgroup_context
),
2040 .kill_context_offset
= offsetof(Mount
, kill_context
),
2041 .exec_runtime_offset
= offsetof(Mount
, exec_runtime
),
2042 .dynamic_creds_offset
= offsetof(Mount
, dynamic_creds
),
2048 .private_section
= "Mount",
2054 .coldplug
= mount_coldplug
,
2058 .start
= mount_start
,
2060 .reload
= mount_reload
,
2063 .clean
= mount_clean
,
2064 .can_clean
= mount_can_clean
,
2066 .serialize
= mount_serialize
,
2067 .deserialize_item
= mount_deserialize_item
,
2069 .active_state
= mount_active_state
,
2070 .sub_state_to_string
= mount_sub_state_to_string
,
2072 .will_restart
= unit_will_restart_default
,
2074 .may_gc
= mount_may_gc
,
2076 .sigchld_event
= mount_sigchld_event
,
2078 .reset_failed
= mount_reset_failed
,
2080 .control_pid
= mount_control_pid
,
2082 .bus_vtable
= bus_mount_vtable
,
2083 .bus_set_property
= bus_mount_set_property
,
2084 .bus_commit_properties
= bus_mount_commit_properties
,
2086 .get_timeout
= mount_get_timeout
,
2088 .can_transient
= true,
2090 .enumerate_perpetual
= mount_enumerate_perpetual
,
2091 .enumerate
= mount_enumerate
,
2092 .shutdown
= mount_shutdown
,
2094 .status_message_formats
= {
2095 .starting_stopping
= {
2096 [0] = "Mounting %s...",
2097 [1] = "Unmounting %s...",
2099 .finished_start_job
= {
2100 [JOB_DONE
] = "Mounted %s.",
2101 [JOB_FAILED
] = "Failed to mount %s.",
2102 [JOB_TIMEOUT
] = "Timed out mounting %s.",
2104 .finished_stop_job
= {
2105 [JOB_DONE
] = "Unmounted %s.",
2106 [JOB_FAILED
] = "Failed unmounting %s.",
2107 [JOB_TIMEOUT
] = "Timed out unmounting %s.",