1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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 "initrd-util.h"
18 #include "libmount-util.h"
21 #include "mkdir-label.h"
22 #include "mount-setup.h"
24 #include "mountpoint-util.h"
25 #include "parse-util.h"
26 #include "path-util.h"
27 #include "process-util.h"
28 #include "serialize.h"
30 #include "string-table.h"
31 #include "string-util.h"
33 #include "unit-name.h"
36 #define RETRY_UMOUNT_MAX 32
38 static const UnitActiveState state_translation_table
[_MOUNT_STATE_MAX
] = {
39 [MOUNT_DEAD
] = UNIT_INACTIVE
,
40 [MOUNT_MOUNTING
] = UNIT_ACTIVATING
,
41 [MOUNT_MOUNTING_DONE
] = UNIT_ACTIVATING
,
42 [MOUNT_MOUNTED
] = UNIT_ACTIVE
,
43 [MOUNT_REMOUNTING
] = UNIT_RELOADING
,
44 [MOUNT_UNMOUNTING
] = UNIT_DEACTIVATING
,
45 [MOUNT_REMOUNTING_SIGTERM
] = UNIT_RELOADING
,
46 [MOUNT_REMOUNTING_SIGKILL
] = UNIT_RELOADING
,
47 [MOUNT_UNMOUNTING_SIGTERM
] = UNIT_DEACTIVATING
,
48 [MOUNT_UNMOUNTING_SIGKILL
] = UNIT_DEACTIVATING
,
49 [MOUNT_FAILED
] = UNIT_FAILED
,
50 [MOUNT_CLEANING
] = UNIT_MAINTENANCE
,
53 static int mount_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
54 static int mount_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
);
55 static void mount_enter_dead(Mount
*m
, MountResult f
);
56 static void mount_enter_mounted(Mount
*m
, MountResult f
);
57 static void mount_cycle_clear(Mount
*m
);
58 static int mount_process_proc_self_mountinfo(Manager
*m
);
60 static bool MOUNT_STATE_WITH_PROCESS(MountState state
) {
65 MOUNT_REMOUNTING_SIGTERM
,
66 MOUNT_REMOUNTING_SIGKILL
,
68 MOUNT_UNMOUNTING_SIGTERM
,
69 MOUNT_UNMOUNTING_SIGKILL
,
73 static MountParameters
* get_mount_parameters_fragment(Mount
*m
) {
77 return &m
->parameters_fragment
;
82 static MountParameters
* get_mount_parameters(Mount
*m
) {
85 if (m
->from_proc_self_mountinfo
)
86 return &m
->parameters_proc_self_mountinfo
;
88 return get_mount_parameters_fragment(m
);
91 static bool mount_is_network(const MountParameters
*p
) {
94 if (fstab_test_option(p
->options
, "_netdev\0"))
97 if (p
->fstype
&& fstype_is_network(p
->fstype
))
103 static bool mount_is_nofail(const Mount
*m
) {
106 if (!m
->from_fragment
)
109 return fstab_test_yes_no_option(m
->parameters_fragment
.options
, "nofail\0" "fail\0");
112 static bool mount_is_loop(const MountParameters
*p
) {
115 if (fstab_test_option(p
->options
, "loop\0"))
121 static bool mount_is_bind(const MountParameters
*p
) {
124 if (fstab_test_option(p
->options
, "bind\0" "rbind\0"))
127 if (p
->fstype
&& STR_IN_SET(p
->fstype
, "bind", "rbind"))
133 static bool mount_is_bound_to_device(Mount
*m
) {
134 const MountParameters
*p
;
138 /* Determines whether to place a Requires= or BindsTo= dependency on the backing device unit. We do
139 * this by checking for the x-systemd.device-bound mount option. Iff it is set we use BindsTo=,
140 * otherwise Requires=. But note that we might combine the latter with StopPropagatedFrom=, see
143 p
= get_mount_parameters(m
);
147 return fstab_test_option(p
->options
, "x-systemd.device-bound\0");
150 static bool mount_propagate_stop(Mount
*m
) {
153 if (mount_is_bound_to_device(m
)) /* If we are using BindsTo= the stop propagation is implicit, no need to bother */
156 return m
->from_fragment
; /* let's propagate stop whenever this is an explicitly configured unit,
157 * otherwise let's not bother. */
160 static bool mount_needs_quota(const MountParameters
*p
) {
163 if (p
->fstype
&& !fstype_needs_quota(p
->fstype
))
166 if (mount_is_bind(p
))
169 return fstab_test_option(p
->options
,
170 "usrquota\0" "grpquota\0" "quota\0" "usrjquota\0" "grpjquota\0");
173 static void mount_init(Unit
*u
) {
178 assert(u
->load_state
== UNIT_STUB
);
180 m
->timeout_usec
= u
->manager
->default_timeout_start_usec
;
182 m
->exec_context
.std_output
= u
->manager
->default_std_output
;
183 m
->exec_context
.std_error
= u
->manager
->default_std_error
;
185 m
->directory_mode
= 0755;
187 /* We need to make sure that /usr/bin/mount is always called
188 * in the same process group as us, so that the autofs kernel
189 * side doesn't send us another mount request while we are
190 * already trying to comply its last one. */
191 m
->exec_context
.same_pgrp
= true;
193 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
195 u
->ignore_on_isolate
= true;
198 static int mount_arm_timer(Mount
*m
, usec_t usec
) {
203 if (m
->timer_event_source
) {
204 r
= sd_event_source_set_time(m
->timer_event_source
, usec
);
208 return sd_event_source_set_enabled(m
->timer_event_source
, SD_EVENT_ONESHOT
);
211 if (usec
== USEC_INFINITY
)
214 r
= sd_event_add_time(
215 UNIT(m
)->manager
->event
,
216 &m
->timer_event_source
,
219 mount_dispatch_timer
, m
);
223 (void) sd_event_source_set_description(m
->timer_event_source
, "mount-timer");
228 static void mount_unwatch_control_pid(Mount
*m
) {
231 if (m
->control_pid
<= 0)
234 unit_unwatch_pid(UNIT(m
), TAKE_PID(m
->control_pid
));
237 static void mount_parameters_done(MountParameters
*p
) {
240 p
->what
= mfree(p
->what
);
241 p
->options
= mfree(p
->options
);
242 p
->fstype
= mfree(p
->fstype
);
245 static void mount_done(Unit
*u
) {
250 m
->where
= mfree(m
->where
);
252 mount_parameters_done(&m
->parameters_proc_self_mountinfo
);
253 mount_parameters_done(&m
->parameters_fragment
);
255 m
->exec_runtime
= exec_runtime_unref(m
->exec_runtime
, false);
256 exec_command_done_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
257 m
->control_command
= NULL
;
259 dynamic_creds_unref(&m
->dynamic_creds
);
261 mount_unwatch_control_pid(m
);
263 m
->timer_event_source
= sd_event_source_disable_unref(m
->timer_event_source
);
266 static int update_parameters_proc_self_mountinfo(
270 const char *fstype
) {
275 p
= &m
->parameters_proc_self_mountinfo
;
277 r
= free_and_strdup(&p
->what
, what
);
281 q
= free_and_strdup(&p
->options
, options
);
285 w
= free_and_strdup(&p
->fstype
, fstype
);
289 return r
> 0 || q
> 0 || w
> 0;
292 static int mount_add_mount_dependencies(Mount
*m
) {
300 if (!path_equal(m
->where
, "/")) {
301 _cleanup_free_
char *parent
= NULL
;
303 /* Adds in links to other mount points that might lie further up in the hierarchy */
305 r
= path_extract_directory(m
->where
, &parent
);
309 r
= unit_require_mounts_for(UNIT(m
), parent
, UNIT_DEPENDENCY_IMPLICIT
);
314 /* Adds in dependencies to other mount points that might be needed for the source path (if this is a bind mount
315 * or a loop mount) to be available. */
316 pm
= get_mount_parameters_fragment(m
);
317 if (pm
&& pm
->what
&&
318 path_is_absolute(pm
->what
) &&
319 (mount_is_bind(pm
) || mount_is_loop(pm
) || !mount_is_network(pm
))) {
321 r
= unit_require_mounts_for(UNIT(m
), pm
->what
, UNIT_DEPENDENCY_FILE
);
326 /* Adds in dependencies to other units that use this path or paths further down in the hierarchy */
327 s
= manager_get_units_requiring_mounts_for(UNIT(m
)->manager
, m
->where
);
328 SET_FOREACH(other
, s
) {
330 if (other
->load_state
!= UNIT_LOADED
)
333 if (other
== UNIT(m
))
336 r
= unit_add_dependency(other
, UNIT_AFTER
, UNIT(m
), true, UNIT_DEPENDENCY_PATH
);
340 if (UNIT(m
)->fragment_path
) {
341 /* If we have fragment configuration, then make this dependency required */
342 r
= unit_add_dependency(other
, UNIT_REQUIRES
, UNIT(m
), true, UNIT_DEPENDENCY_PATH
);
351 static int mount_add_device_dependencies(Mount
*m
) {
352 UnitDependencyMask mask
;
359 log_unit_trace(UNIT(m
), "Processing implicit device dependencies");
361 p
= get_mount_parameters(m
);
363 log_unit_trace(UNIT(m
), "Missing mount parameters, skipping implicit device dependencies");
368 log_unit_trace(UNIT(m
), "Missing mount source, skipping implicit device dependencies");
372 if (mount_is_bind(p
)) {
373 log_unit_trace(UNIT(m
), "Mount unit is a bind mount, skipping implicit device dependencies");
377 if (!is_device_path(p
->what
)) {
378 log_unit_trace(UNIT(m
), "Mount source is not a device path, skipping implicit device dependencies");
382 /* /dev/root is a really weird thing, it's not a real device, but just a path the kernel exports for
383 * the root file system specified on the kernel command line. Ignore it here. */
384 if (PATH_IN_SET(p
->what
, "/dev/root", "/dev/nfs")) {
385 log_unit_trace(UNIT(m
), "Mount source is in /dev/root or /dev/nfs, skipping implicit device dependencies");
389 if (path_equal(m
->where
, "/")) {
390 log_unit_trace(UNIT(m
), "Mount destination is '/', skipping implicit device dependencies");
394 /* Mount units from /proc/self/mountinfo are not bound to devices by default since they're subject to
395 * races when mounts are established by other tools with different backing devices than what we
396 * maintain. The user can still force this to be a BindsTo= dependency with an appropriate option (or
397 * udev property) so the mount units are automatically stopped when the device disappears
399 dep
= mount_is_bound_to_device(m
) ? UNIT_BINDS_TO
: UNIT_REQUIRES
;
401 /* We always use 'what' from /proc/self/mountinfo if mounted */
402 mask
= m
->from_proc_self_mountinfo
? UNIT_DEPENDENCY_MOUNTINFO
: UNIT_DEPENDENCY_MOUNT_FILE
;
404 r
= unit_add_node_dependency(UNIT(m
), p
->what
, dep
, mask
);
408 log_unit_trace(UNIT(m
), "Added %s dependency on %s", unit_dependency_to_string(dep
), p
->what
);
410 if (mount_propagate_stop(m
)) {
411 r
= unit_add_node_dependency(UNIT(m
), p
->what
, UNIT_STOP_PROPAGATED_FROM
, mask
);
415 log_unit_trace(UNIT(m
), "Added %s dependency on %s",
416 unit_dependency_to_string(UNIT_STOP_PROPAGATED_FROM
), p
->what
);
419 r
= unit_add_blockdev_dependency(UNIT(m
), p
->what
, mask
);
421 log_unit_trace(UNIT(m
), "Added %s dependency on %s", unit_dependency_to_string(UNIT_AFTER
), p
->what
);
426 static int mount_add_quota_dependencies(Mount
*m
) {
432 if (!MANAGER_IS_SYSTEM(UNIT(m
)->manager
))
435 p
= get_mount_parameters_fragment(m
);
439 if (!mount_needs_quota(p
))
442 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTACHECK_SERVICE
,
443 /* add_reference= */ true, UNIT_DEPENDENCY_FILE
);
447 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTAON_SERVICE
,
448 /* add_reference= */true, UNIT_DEPENDENCY_FILE
);
455 static bool mount_is_extrinsic(Unit
*u
) {
460 /* Returns true for all units that are "magic" and should be excluded from the usual
461 * start-up and shutdown dependencies. We call them "extrinsic" here, as they are generally
462 * mounted outside of the systemd dependency logic. We shouldn't attempt to manage them
463 * ourselves but it's fine if the user operates on them with us. */
465 /* We only automatically manage mounts if we are in system mode */
466 if (MANAGER_IS_USER(u
->manager
))
469 p
= get_mount_parameters(m
);
470 if (p
&& fstab_is_extrinsic(m
->where
, p
->options
))
476 static int mount_add_default_ordering_dependencies(Mount
*m
, MountParameters
*p
, UnitDependencyMask mask
) {
477 const char *after
, *before
, *e
;
482 e
= path_startswith(m
->where
, "/sysroot");
483 if (e
&& in_initrd()) {
484 /* All mounts under /sysroot need to happen later, at initrd-fs.target time. IOW,
485 * it's not technically part of the basic initrd filesystem itself, and so
486 * shouldn't inherit the default Before=local-fs.target dependency. */
489 before
= isempty(e
) ? SPECIAL_INITRD_ROOT_FS_TARGET
: SPECIAL_INITRD_FS_TARGET
;
491 } else if (mount_is_network(p
)) {
492 after
= SPECIAL_REMOTE_FS_PRE_TARGET
;
493 before
= SPECIAL_REMOTE_FS_TARGET
;
496 after
= SPECIAL_LOCAL_FS_PRE_TARGET
;
497 before
= SPECIAL_LOCAL_FS_TARGET
;
500 if (!mount_is_nofail(m
)) {
501 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_BEFORE
, before
, /* add_reference= */ true, mask
);
507 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, after
, /* add_reference= */ true, mask
);
512 return unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
,
513 /* add_reference= */ true, mask
);
516 static int mount_add_default_dependencies(Mount
*m
) {
517 UnitDependencyMask mask
;
523 if (!UNIT(m
)->default_dependencies
)
526 /* We do not add any default dependencies to /, /usr or /run/initramfs/, since they are
527 * guaranteed to stay mounted the whole time, since our system is on it. Also, don't
528 * bother with anything mounted below virtual file systems, it's also going to be virtual,
529 * and hence not worth the effort. */
530 if (mount_is_extrinsic(UNIT(m
)))
533 p
= get_mount_parameters(m
);
537 mask
= m
->from_proc_self_mountinfo
? UNIT_DEPENDENCY_MOUNTINFO
: UNIT_DEPENDENCY_MOUNT_FILE
;
539 r
= mount_add_default_ordering_dependencies(m
, p
, mask
);
543 if (mount_is_network(p
)) {
544 /* We order ourselves after network.target. This is primarily useful at shutdown:
545 * services that take down the network should order themselves before
546 * network.target, so that they are shut down only after this mount unit is
549 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, SPECIAL_NETWORK_TARGET
,
550 /* add_reference= */ true, mask
);
554 /* We pull in network-online.target, and order ourselves after it. This is useful
555 * at start-up to actively pull in tools that want to be started before we start
556 * mounting network file systems, and whose purpose it is to delay this until the
557 * network is "up". */
559 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_WANTS
, UNIT_AFTER
, SPECIAL_NETWORK_ONLINE_TARGET
,
560 /* add_reference= */ true, mask
);
565 /* If this is a tmpfs mount then we have to unmount it before we try to deactivate swaps */
566 if (streq_ptr(p
->fstype
, "tmpfs")) {
567 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, SPECIAL_SWAP_TARGET
,
568 /* add_reference= */ true, mask
);
576 static int mount_verify(Mount
*m
) {
577 _cleanup_free_
char *e
= NULL
;
582 assert(UNIT(m
)->load_state
== UNIT_LOADED
);
584 if (!m
->from_fragment
&& !m
->from_proc_self_mountinfo
&& !UNIT(m
)->perpetual
)
587 r
= unit_name_from_path(m
->where
, ".mount", &e
);
589 return log_unit_error_errno(UNIT(m
), r
, "Failed to generate unit name from mount path: %m");
591 if (!unit_has_name(UNIT(m
), e
))
592 return log_unit_error_errno(UNIT(m
), SYNTHETIC_ERRNO(ENOEXEC
), "Where= setting doesn't match unit name. Refusing.");
594 if (mount_point_is_api(m
->where
) || mount_point_ignore(m
->where
))
595 return log_unit_error_errno(UNIT(m
), SYNTHETIC_ERRNO(ENOEXEC
), "Cannot create mount unit for API file system %s. Refusing.", m
->where
);
597 p
= get_mount_parameters_fragment(m
);
598 if (p
&& !p
->what
&& !UNIT(m
)->perpetual
)
599 return log_unit_error_errno(UNIT(m
), SYNTHETIC_ERRNO(ENOEXEC
),
600 "What= setting is missing. Refusing.");
602 if (m
->exec_context
.pam_name
&& m
->kill_context
.kill_mode
!= KILL_CONTROL_GROUP
)
603 return log_unit_error_errno(UNIT(m
), SYNTHETIC_ERRNO(ENOEXEC
), "Unit has PAM enabled. Kill mode must be set to control-group'. Refusing.");
608 static int mount_add_non_exec_dependencies(Mount
*m
) {
613 /* We may be called due to this mount appearing in /proc/self/mountinfo, hence we clear all existing
614 * dependencies that were initialized from the unit file but whose final value really depends on the
615 * content of /proc/self/mountinfo. Some (such as m->where) might have become stale now. */
616 unit_remove_dependencies(UNIT(m
), UNIT_DEPENDENCY_MOUNTINFO
| UNIT_DEPENDENCY_MOUNT_FILE
);
621 /* Adds in all dependencies directly responsible for ordering the mount, as opposed to dependencies
622 * resulting from the ExecContext and such. */
624 r
= mount_add_device_dependencies(m
);
628 r
= mount_add_mount_dependencies(m
);
632 r
= mount_add_quota_dependencies(m
);
636 r
= mount_add_default_dependencies(m
);
643 static int mount_add_extras(Mount
*m
) {
649 /* Note: this call might be called after we already have been loaded once (and even when it has already been
650 * activated), in case data from /proc/self/mountinfo has changed. This means all code here needs to be ready
651 * to run with an already set up unit. */
653 if (u
->fragment_path
)
654 m
->from_fragment
= true;
657 r
= unit_name_to_path(u
->id
, &m
->where
);
658 if (r
== -ENAMETOOLONG
)
659 log_unit_error_errno(u
, r
, "Failed to derive mount point path from unit name, because unit name is hashed. "
660 "Set \"Where=\" in the unit file explicitly.");
665 path_simplify(m
->where
);
667 if (!u
->description
) {
668 r
= unit_set_description(u
, m
->where
);
673 r
= unit_patch_contexts(u
);
677 r
= unit_add_exec_dependencies(u
, &m
->exec_context
);
681 r
= unit_set_default_slice(u
);
685 r
= mount_add_non_exec_dependencies(m
);
692 static void mount_load_root_mount(Unit
*u
) {
695 if (!unit_has_name(u
, SPECIAL_ROOT_MOUNT
))
699 u
->default_dependencies
= false;
701 /* The stdio/kmsg bridge socket is on /, in order to avoid a dep loop, don't use kmsg logging for -.mount */
702 MOUNT(u
)->exec_context
.std_output
= EXEC_OUTPUT_NULL
;
703 MOUNT(u
)->exec_context
.std_input
= EXEC_INPUT_NULL
;
706 u
->description
= strdup("Root Mount");
709 static int mount_load(Unit
*u
) {
715 assert(u
->load_state
== UNIT_STUB
);
717 mount_load_root_mount(u
);
719 bool fragment_optional
= m
->from_proc_self_mountinfo
|| u
->perpetual
;
720 r
= unit_load_fragment_and_dropin(u
, !fragment_optional
);
722 /* Add in some extras. Note we do this in all cases (even if we failed to load the unit) when announced by the
723 * kernel, because we need some things to be set up no matter what when the kernel establishes a mount and thus
724 * we need to update the state in our unit to track it. After all, consider that we don't allow changing the
725 * 'slice' field for a unit once it is active. */
726 if (u
->load_state
== UNIT_LOADED
|| m
->from_proc_self_mountinfo
|| u
->perpetual
)
727 q
= mount_add_extras(m
);
733 if (u
->load_state
!= UNIT_LOADED
)
736 return mount_verify(m
);
739 static void mount_set_state(Mount
*m
, MountState state
) {
740 MountState old_state
;
743 if (m
->state
!= state
)
744 bus_unit_send_pending_change_signal(UNIT(m
), false);
746 old_state
= m
->state
;
749 if (!MOUNT_STATE_WITH_PROCESS(state
)) {
750 m
->timer_event_source
= sd_event_source_disable_unref(m
->timer_event_source
);
751 mount_unwatch_control_pid(m
);
752 m
->control_command
= NULL
;
753 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
756 if (state
!= old_state
)
757 log_unit_debug(UNIT(m
), "Changed %s -> %s", mount_state_to_string(old_state
), mount_state_to_string(state
));
759 unit_notify(UNIT(m
), state_translation_table
[old_state
], state_translation_table
[state
],
760 m
->reload_result
== MOUNT_SUCCESS
? 0 : UNIT_NOTIFY_RELOAD_FAILURE
);
763 static int mount_coldplug(Unit
*u
) {
768 assert(m
->state
== MOUNT_DEAD
);
770 if (m
->deserialized_state
== m
->state
)
773 if (m
->control_pid
> 0 &&
774 pid_is_unwaited(m
->control_pid
) &&
775 MOUNT_STATE_WITH_PROCESS(m
->deserialized_state
)) {
777 r
= unit_watch_pid(UNIT(m
), m
->control_pid
, false);
781 r
= mount_arm_timer(m
, usec_add(u
->state_change_timestamp
.monotonic
, m
->timeout_usec
));
786 if (!IN_SET(m
->deserialized_state
, MOUNT_DEAD
, MOUNT_FAILED
)) {
787 (void) unit_setup_dynamic_creds(u
);
788 (void) unit_setup_exec_runtime(u
);
791 mount_set_state(m
, m
->deserialized_state
);
795 static void mount_catchup(Unit
*u
) {
796 Mount
*m
= MOUNT(ASSERT_PTR(u
));
800 /* Adjust the deserialized state. See comments in mount_process_proc_self_mountinfo(). */
801 if (m
->from_proc_self_mountinfo
)
805 assert(m
->control_pid
== 0);
806 (void) unit_acquire_invocation_id(u
);
807 mount_cycle_clear(m
);
808 mount_enter_mounted(m
, MOUNT_SUCCESS
);
811 assert(m
->control_pid
> 0);
812 mount_set_state(m
, MOUNT_MOUNTING_DONE
);
819 case MOUNT_MOUNTING_DONE
:
820 assert(m
->control_pid
> 0);
821 mount_set_state(m
, MOUNT_MOUNTING
);
824 assert(m
->control_pid
== 0);
825 mount_enter_dead(m
, MOUNT_SUCCESS
);
832 static void mount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
839 p
= get_mount_parameters(m
);
842 "%sMount State: %s\n"
844 "%sClean Result: %s\n"
847 "%sFile System Type: %s\n"
849 "%sFrom /proc/self/mountinfo: %s\n"
850 "%sFrom fragment: %s\n"
852 "%sDirectoryMode: %04o\n"
853 "%sSloppyOptions: %s\n"
854 "%sLazyUnmount: %s\n"
855 "%sForceUnmount: %s\n"
856 "%sReadWriteOnly: %s\n"
857 "%sTimeoutSec: %s\n",
858 prefix
, mount_state_to_string(m
->state
),
859 prefix
, mount_result_to_string(m
->result
),
860 prefix
, mount_result_to_string(m
->clean_result
),
862 prefix
, p
? strna(p
->what
) : "n/a",
863 prefix
, p
? strna(p
->fstype
) : "n/a",
864 prefix
, p
? strna(p
->options
) : "n/a",
865 prefix
, yes_no(m
->from_proc_self_mountinfo
),
866 prefix
, yes_no(m
->from_fragment
),
867 prefix
, yes_no(mount_is_extrinsic(u
)),
868 prefix
, m
->directory_mode
,
869 prefix
, yes_no(m
->sloppy_options
),
870 prefix
, yes_no(m
->lazy_unmount
),
871 prefix
, yes_no(m
->force_unmount
),
872 prefix
, yes_no(m
->read_write_only
),
873 prefix
, FORMAT_TIMESPAN(m
->timeout_usec
, USEC_PER_SEC
));
875 if (m
->control_pid
> 0)
877 "%sControl PID: "PID_FMT
"\n",
878 prefix
, m
->control_pid
);
880 exec_context_dump(&m
->exec_context
, f
, prefix
);
881 kill_context_dump(&m
->kill_context
, f
, prefix
);
882 cgroup_context_dump(UNIT(m
), f
, prefix
);
885 static int mount_spawn(Mount
*m
, ExecCommand
*c
, pid_t
*_pid
) {
887 _cleanup_(exec_params_clear
) ExecParameters exec_params
= {
888 .flags
= EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
,
901 r
= unit_prepare_exec(UNIT(m
));
905 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->timeout_usec
));
909 r
= unit_set_exec_params(UNIT(m
), &exec_params
);
913 r
= exec_spawn(UNIT(m
),
923 r
= unit_watch_pid(UNIT(m
), pid
, true);
932 static void mount_enter_dead(Mount
*m
, MountResult f
) {
935 if (m
->result
== MOUNT_SUCCESS
)
938 unit_log_result(UNIT(m
), m
->result
== MOUNT_SUCCESS
, mount_result_to_string(m
->result
));
939 unit_warn_leftover_processes(UNIT(m
), unit_log_leftover_process_stop
);
941 mount_set_state(m
, m
->result
!= MOUNT_SUCCESS
? MOUNT_FAILED
: MOUNT_DEAD
);
943 m
->exec_runtime
= exec_runtime_unref(m
->exec_runtime
, true);
945 unit_destroy_runtime_data(UNIT(m
), &m
->exec_context
);
947 unit_unref_uid_gid(UNIT(m
), true);
949 dynamic_creds_destroy(&m
->dynamic_creds
);
951 /* Any dependencies based on /proc/self/mountinfo are now stale. Let's re-generate dependencies from
953 (void) mount_add_non_exec_dependencies(m
);
956 static void mount_enter_mounted(Mount
*m
, MountResult f
) {
959 if (m
->result
== MOUNT_SUCCESS
)
962 mount_set_state(m
, MOUNT_MOUNTED
);
965 static void mount_enter_dead_or_mounted(Mount
*m
, MountResult f
) {
968 /* Enter DEAD or MOUNTED state, depending on what the kernel currently says about the mount point. We use this
969 * whenever we executed an operation, so that our internal state reflects what the kernel says again, after all
970 * ultimately we just mirror the kernel's internal state on this. */
972 if (m
->from_proc_self_mountinfo
)
973 mount_enter_mounted(m
, f
);
975 mount_enter_dead(m
, f
);
978 static int state_to_kill_operation(MountState state
) {
981 case MOUNT_REMOUNTING_SIGTERM
:
984 case MOUNT_UNMOUNTING_SIGTERM
:
985 return KILL_TERMINATE
;
987 case MOUNT_REMOUNTING_SIGKILL
:
988 case MOUNT_UNMOUNTING_SIGKILL
:
992 return _KILL_OPERATION_INVALID
;
996 static void mount_enter_signal(Mount
*m
, MountState state
, MountResult f
) {
1001 if (m
->result
== MOUNT_SUCCESS
)
1004 r
= unit_kill_context(
1007 state_to_kill_operation(state
),
1015 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->timeout_usec
));
1019 mount_set_state(m
, state
);
1020 } else if (state
== MOUNT_REMOUNTING_SIGTERM
&& m
->kill_context
.send_sigkill
)
1021 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
1022 else if (IN_SET(state
, MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
))
1023 mount_enter_mounted(m
, MOUNT_SUCCESS
);
1024 else if (state
== MOUNT_UNMOUNTING_SIGTERM
&& m
->kill_context
.send_sigkill
)
1025 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
1027 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1032 log_unit_warning_errno(UNIT(m
), r
, "Failed to kill processes: %m");
1033 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
1036 static void mount_enter_unmounting(Mount
*m
) {
1041 /* Start counting our attempts */
1042 if (!IN_SET(m
->state
,
1044 MOUNT_UNMOUNTING_SIGTERM
,
1045 MOUNT_UNMOUNTING_SIGKILL
))
1046 m
->n_retry_umount
= 0;
1048 m
->control_command_id
= MOUNT_EXEC_UNMOUNT
;
1049 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_UNMOUNT
;
1051 r
= exec_command_set(m
->control_command
, UMOUNT_PATH
, m
->where
, "-c", NULL
);
1052 if (r
>= 0 && m
->lazy_unmount
)
1053 r
= exec_command_append(m
->control_command
, "-l", NULL
);
1054 if (r
>= 0 && m
->force_unmount
)
1055 r
= exec_command_append(m
->control_command
, "-f", NULL
);
1059 mount_unwatch_control_pid(m
);
1061 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
1065 mount_set_state(m
, MOUNT_UNMOUNTING
);
1070 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'umount' task: %m");
1071 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
1074 static void mount_enter_mounting(Mount
*m
) {
1080 r
= unit_fail_if_noncanonical(UNIT(m
), m
->where
);
1084 (void) mkdir_p_label(m
->where
, m
->directory_mode
);
1086 unit_warn_if_dir_nonempty(UNIT(m
), m
->where
);
1087 unit_warn_leftover_processes(UNIT(m
), unit_log_leftover_process_start
);
1089 m
->control_command_id
= MOUNT_EXEC_MOUNT
;
1090 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_MOUNT
;
1092 /* Create the source directory for bind-mounts if needed */
1093 p
= get_mount_parameters_fragment(m
);
1094 if (p
&& mount_is_bind(p
)) {
1095 r
= mkdir_p_label(p
->what
, m
->directory_mode
);
1096 /* mkdir_p_label() can return -EEXIST if the target path exists and is not a directory - which is
1097 * totally OK, in case the user wants us to overmount a non-directory inode. Also -EROFS can be
1098 * returned on read-only filesystem. Moreover, -EACCES (and also maybe -EPERM?) may be returned
1099 * when the path is on NFS. See issue #24120. All such errors will be logged in the debug level. */
1100 if (r
< 0 && r
!= -EEXIST
)
1101 log_unit_full_errno(UNIT(m
),
1102 (r
== -EROFS
|| ERRNO_IS_PRIVILEGE(r
)) ? LOG_DEBUG
: LOG_WARNING
,
1103 r
, "Failed to make bind mount source '%s', ignoring: %m", p
->what
);
1107 _cleanup_free_
char *opts
= NULL
;
1109 r
= fstab_filter_options(p
->options
, "nofail\0" "noauto\0" "auto\0", NULL
, NULL
, NULL
, &opts
);
1113 r
= exec_command_set(m
->control_command
, MOUNT_PATH
, p
->what
, m
->where
, NULL
);
1114 if (r
>= 0 && m
->sloppy_options
)
1115 r
= exec_command_append(m
->control_command
, "-s", NULL
);
1116 if (r
>= 0 && m
->read_write_only
)
1117 r
= exec_command_append(m
->control_command
, "-w", NULL
);
1118 if (r
>= 0 && p
->fstype
)
1119 r
= exec_command_append(m
->control_command
, "-t", p
->fstype
, NULL
);
1120 if (r
>= 0 && !isempty(opts
))
1121 r
= exec_command_append(m
->control_command
, "-o", opts
, NULL
);
1127 mount_unwatch_control_pid(m
);
1129 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
1133 mount_set_state(m
, MOUNT_MOUNTING
);
1138 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'mount' task: %m");
1139 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
1142 static void mount_set_reload_result(Mount
*m
, MountResult result
) {
1145 /* Only store the first error we encounter */
1146 if (m
->reload_result
!= MOUNT_SUCCESS
)
1149 m
->reload_result
= result
;
1152 static void mount_enter_remounting(Mount
*m
) {
1158 /* Reset reload result when we are about to start a new remount operation */
1159 m
->reload_result
= MOUNT_SUCCESS
;
1161 m
->control_command_id
= MOUNT_EXEC_REMOUNT
;
1162 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_REMOUNT
;
1164 p
= get_mount_parameters_fragment(m
);
1169 o
= strjoina("remount,", p
->options
);
1173 r
= exec_command_set(m
->control_command
, MOUNT_PATH
,
1176 if (r
>= 0 && m
->sloppy_options
)
1177 r
= exec_command_append(m
->control_command
, "-s", NULL
);
1178 if (r
>= 0 && m
->read_write_only
)
1179 r
= exec_command_append(m
->control_command
, "-w", NULL
);
1180 if (r
>= 0 && p
->fstype
)
1181 r
= exec_command_append(m
->control_command
, "-t", p
->fstype
, NULL
);
1187 mount_unwatch_control_pid(m
);
1189 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
1193 mount_set_state(m
, MOUNT_REMOUNTING
);
1198 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'remount' task: %m");
1199 mount_set_reload_result(m
, MOUNT_FAILURE_RESOURCES
);
1200 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1203 static void mount_cycle_clear(Mount
*m
) {
1206 /* Clear all state we shall forget for this new cycle */
1208 m
->result
= MOUNT_SUCCESS
;
1209 m
->reload_result
= MOUNT_SUCCESS
;
1210 exec_command_reset_status_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
1211 UNIT(m
)->reset_accounting
= true;
1214 static int mount_start(Unit
*u
) {
1215 Mount
*m
= MOUNT(u
);
1220 /* We cannot fulfill this request right now, try again later
1222 if (IN_SET(m
->state
,
1224 MOUNT_UNMOUNTING_SIGTERM
,
1225 MOUNT_UNMOUNTING_SIGKILL
,
1229 /* Already on it! */
1230 if (IN_SET(m
->state
, MOUNT_MOUNTING
, MOUNT_MOUNTING_DONE
))
1233 assert(IN_SET(m
->state
, MOUNT_DEAD
, MOUNT_FAILED
));
1235 r
= unit_acquire_invocation_id(u
);
1239 mount_cycle_clear(m
);
1240 mount_enter_mounting(m
);
1245 static int mount_stop(Unit
*u
) {
1246 Mount
*m
= MOUNT(u
);
1252 case MOUNT_UNMOUNTING
:
1253 case MOUNT_UNMOUNTING_SIGKILL
:
1254 case MOUNT_UNMOUNTING_SIGTERM
:
1258 case MOUNT_MOUNTING
:
1259 case MOUNT_MOUNTING_DONE
:
1260 case MOUNT_REMOUNTING
:
1261 /* If we are still waiting for /bin/mount, we go directly into kill mode. */
1262 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_SUCCESS
);
1265 case MOUNT_REMOUNTING_SIGTERM
:
1266 /* If we are already waiting for a hung remount, convert this to the matching unmounting state */
1267 mount_set_state(m
, MOUNT_UNMOUNTING_SIGTERM
);
1270 case MOUNT_REMOUNTING_SIGKILL
:
1272 mount_set_state(m
, MOUNT_UNMOUNTING_SIGKILL
);
1276 mount_enter_unmounting(m
);
1279 case MOUNT_CLEANING
:
1280 /* If we are currently cleaning, then abort it, brutally. */
1281 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
1285 assert_not_reached();
1289 static int mount_reload(Unit
*u
) {
1290 Mount
*m
= MOUNT(u
);
1293 assert(m
->state
== MOUNT_MOUNTED
);
1295 mount_enter_remounting(m
);
1300 static int mount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1301 Mount
*m
= MOUNT(u
);
1307 (void) serialize_item(f
, "state", mount_state_to_string(m
->state
));
1308 (void) serialize_item(f
, "result", mount_result_to_string(m
->result
));
1309 (void) serialize_item(f
, "reload-result", mount_result_to_string(m
->reload_result
));
1310 (void) serialize_item_format(f
, "n-retry-umount", "%u", m
->n_retry_umount
);
1312 if (m
->control_pid
> 0)
1313 (void) serialize_item_format(f
, "control-pid", PID_FMT
, m
->control_pid
);
1315 if (m
->control_command_id
>= 0)
1316 (void) serialize_item(f
, "control-command", mount_exec_command_to_string(m
->control_command_id
));
1321 static int mount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1322 Mount
*m
= MOUNT(u
);
1331 if (streq(key
, "state")) {
1334 state
= mount_state_from_string(value
);
1336 log_unit_debug_errno(u
, state
, "Failed to parse state value: %s", value
);
1338 m
->deserialized_state
= state
;
1340 } else if (streq(key
, "result")) {
1343 f
= mount_result_from_string(value
);
1345 log_unit_debug_errno(u
, f
, "Failed to parse result value: %s", value
);
1346 else if (f
!= MOUNT_SUCCESS
)
1349 } else if (streq(key
, "reload-result")) {
1352 f
= mount_result_from_string(value
);
1354 log_unit_debug_errno(u
, f
, "Failed to parse reload result value: %s", value
);
1355 else if (f
!= MOUNT_SUCCESS
)
1356 m
->reload_result
= f
;
1358 } else if (streq(key
, "n-retry-umount")) {
1360 r
= safe_atou(value
, &m
->n_retry_umount
);
1362 log_unit_debug_errno(u
, r
, "Failed to parse n-retry-umount value: %s", value
);
1364 } else if (streq(key
, "control-pid")) {
1366 r
= parse_pid(value
, &m
->control_pid
);
1368 log_unit_debug_errno(u
, r
, "Failed to parse control-pid value: %s", value
);
1370 } else if (streq(key
, "control-command")) {
1371 MountExecCommand id
;
1373 id
= mount_exec_command_from_string(value
);
1375 log_unit_debug_errno(u
, id
, "Failed to parse exec-command value: %s", value
);
1377 m
->control_command_id
= id
;
1378 m
->control_command
= m
->exec_command
+ id
;
1381 log_unit_debug(u
, "Unknown serialization key: %s", key
);
1386 _pure_
static UnitActiveState
mount_active_state(Unit
*u
) {
1389 return state_translation_table
[MOUNT(u
)->state
];
1392 _pure_
static const char *mount_sub_state_to_string(Unit
*u
) {
1395 return mount_state_to_string(MOUNT(u
)->state
);
1398 _pure_
static bool mount_may_gc(Unit
*u
) {
1399 Mount
*m
= MOUNT(u
);
1403 if (m
->from_proc_self_mountinfo
)
1409 static void mount_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1410 Mount
*m
= MOUNT(u
);
1416 if (pid
!= m
->control_pid
)
1419 /* So here's the thing, we really want to know before /usr/bin/mount or /usr/bin/umount exit whether
1420 * they established/remove a mount. This is important when mounting, but even more so when unmounting
1421 * since we need to deal with nested mounts and otherwise cannot safely determine whether to repeat
1422 * the unmounts. In theory, the kernel fires /proc/self/mountinfo changes off before returning from
1423 * the mount() or umount() syscalls, and thus we should see the changes to the proc file before we
1424 * process the waitid() for the /usr/bin/(u)mount processes. However, this is unfortunately racy: we
1425 * have to waitid() for processes using P_ALL (since we need to reap unexpected children that got
1426 * reparented to PID 1), but when using P_ALL we might end up reaping processes that terminated just
1427 * instants ago, i.e. already after our last event loop iteration (i.e. after the last point we might
1428 * have noticed /proc/self/mountinfo events via epoll). This means event loop priorities for
1429 * processing SIGCHLD vs. /proc/self/mountinfo IO events are not as relevant as we want. To fix that
1430 * race, let's explicitly scan /proc/self/mountinfo before we start processing /usr/bin/(u)mount
1431 * dying. It's ugly, but it makes our ordering systematic again, and makes sure we always see
1432 * /proc/self/mountinfo changes before our mount/umount exits. */
1433 (void) mount_process_proc_self_mountinfo(u
->manager
);
1437 if (is_clean_exit(code
, status
, EXIT_CLEAN_COMMAND
, NULL
))
1439 else if (code
== CLD_EXITED
)
1440 f
= MOUNT_FAILURE_EXIT_CODE
;
1441 else if (code
== CLD_KILLED
)
1442 f
= MOUNT_FAILURE_SIGNAL
;
1443 else if (code
== CLD_DUMPED
)
1444 f
= MOUNT_FAILURE_CORE_DUMP
;
1446 assert_not_reached();
1448 if (IN_SET(m
->state
, MOUNT_REMOUNTING
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_REMOUNTING_SIGTERM
))
1449 mount_set_reload_result(m
, f
);
1450 else if (m
->result
== MOUNT_SUCCESS
)
1453 if (m
->control_command
) {
1454 exec_status_exit(&m
->control_command
->exec_status
, &m
->exec_context
, pid
, code
, status
);
1456 m
->control_command
= NULL
;
1457 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
1460 unit_log_process_exit(
1463 mount_exec_command_to_string(m
->control_command_id
),
1467 /* Note that due to the io event priority logic, we can be sure the new mountinfo is loaded
1468 * before we process the SIGCHLD for the mount command. */
1472 case MOUNT_MOUNTING
:
1473 /* Our mount point has not appeared in mountinfo. Something went wrong. */
1475 if (f
== MOUNT_SUCCESS
) {
1476 /* Either /bin/mount has an unexpected definition of success,
1477 * or someone raced us and we lost. */
1478 log_unit_warning(UNIT(m
), "Mount process finished, but there is no mount.");
1479 f
= MOUNT_FAILURE_PROTOCOL
;
1481 mount_enter_dead(m
, f
);
1484 case MOUNT_MOUNTING_DONE
:
1485 mount_enter_mounted(m
, f
);
1488 case MOUNT_REMOUNTING
:
1489 case MOUNT_REMOUNTING_SIGTERM
:
1490 case MOUNT_REMOUNTING_SIGKILL
:
1491 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1494 case MOUNT_UNMOUNTING
:
1496 if (f
== MOUNT_SUCCESS
&& m
->from_proc_self_mountinfo
) {
1498 /* Still a mount point? If so, let's try again. Most likely there were multiple mount points
1499 * stacked on top of each other. We might exceed the timeout specified by the user overall,
1500 * but we will stop as soon as any one umount times out. */
1502 if (m
->n_retry_umount
< RETRY_UMOUNT_MAX
) {
1503 log_unit_debug(u
, "Mount still present, trying again.");
1504 m
->n_retry_umount
++;
1505 mount_enter_unmounting(m
);
1507 log_unit_warning(u
, "Mount still present after %u attempts to unmount, giving up.", m
->n_retry_umount
);
1508 mount_enter_mounted(m
, f
);
1511 mount_enter_dead_or_mounted(m
, f
);
1515 case MOUNT_UNMOUNTING_SIGKILL
:
1516 case MOUNT_UNMOUNTING_SIGTERM
:
1517 mount_enter_dead_or_mounted(m
, f
);
1520 case MOUNT_CLEANING
:
1521 if (m
->clean_result
== MOUNT_SUCCESS
)
1522 m
->clean_result
= f
;
1524 mount_enter_dead(m
, MOUNT_SUCCESS
);
1528 assert_not_reached();
1531 /* Notify clients about changed exit status */
1532 unit_add_to_dbus_queue(u
);
1535 static int mount_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
1536 Mount
*m
= MOUNT(userdata
);
1539 assert(m
->timer_event_source
== source
);
1543 case MOUNT_MOUNTING
:
1544 case MOUNT_MOUNTING_DONE
:
1545 log_unit_warning(UNIT(m
), "Mounting timed out. Terminating.");
1546 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1549 case MOUNT_REMOUNTING
:
1550 log_unit_warning(UNIT(m
), "Remounting timed out. Terminating remount process.");
1551 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1552 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGTERM
, MOUNT_SUCCESS
);
1555 case MOUNT_REMOUNTING_SIGTERM
:
1556 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1558 if (m
->kill_context
.send_sigkill
) {
1559 log_unit_warning(UNIT(m
), "Remounting timed out. Killing.");
1560 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
1562 log_unit_warning(UNIT(m
), "Remounting timed out. Skipping SIGKILL. Ignoring.");
1563 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1567 case MOUNT_REMOUNTING_SIGKILL
:
1568 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1570 log_unit_warning(UNIT(m
), "Mount process still around after SIGKILL. Ignoring.");
1571 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1574 case MOUNT_UNMOUNTING
:
1575 log_unit_warning(UNIT(m
), "Unmounting timed out. Terminating.");
1576 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1579 case MOUNT_UNMOUNTING_SIGTERM
:
1580 if (m
->kill_context
.send_sigkill
) {
1581 log_unit_warning(UNIT(m
), "Mount process timed out. Killing.");
1582 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_FAILURE_TIMEOUT
);
1584 log_unit_warning(UNIT(m
), "Mount process timed out. Skipping SIGKILL. Ignoring.");
1585 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1589 case MOUNT_UNMOUNTING_SIGKILL
:
1590 log_unit_warning(UNIT(m
), "Mount process still around after SIGKILL. Ignoring.");
1591 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1594 case MOUNT_CLEANING
:
1595 log_unit_warning(UNIT(m
), "Cleaning timed out. killing.");
1597 if (m
->clean_result
== MOUNT_SUCCESS
)
1598 m
->clean_result
= MOUNT_FAILURE_TIMEOUT
;
1600 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, 0);
1604 assert_not_reached();
1610 static int mount_setup_new_unit(
1615 const char *options
,
1617 MountProcFlags
*ret_flags
,
1620 _cleanup_(unit_freep
) Unit
*u
= NULL
;
1628 r
= unit_new_for_name(m
, sizeof(Mount
), name
, &u
);
1632 r
= free_and_strdup(&u
->source_path
, "/proc/self/mountinfo");
1636 r
= free_and_strdup(&MOUNT(u
)->where
, where
);
1640 r
= update_parameters_proc_self_mountinfo(MOUNT(u
), what
, options
, fstype
);
1644 /* This unit was generated because /proc/self/mountinfo reported it. Remember this, so that by the
1645 * time we load the unit file for it (and thus add in extra deps right after) we know what source to
1646 * attributes the deps to. */
1647 MOUNT(u
)->from_proc_self_mountinfo
= true;
1649 r
= mount_add_non_exec_dependencies(MOUNT(u
));
1653 /* We have only allocated the stub now, let's enqueue this unit for loading now, so that everything
1654 * else is loaded in now. */
1655 unit_add_to_load_queue(u
);
1657 *ret_flags
= MOUNT_PROC_IS_MOUNTED
| MOUNT_PROC_JUST_MOUNTED
| MOUNT_PROC_JUST_CHANGED
;
1662 static int mount_setup_existing_unit(
1666 const char *options
,
1668 MountProcFlags
*ret_flags
) {
1675 if (!MOUNT(u
)->where
) {
1676 MOUNT(u
)->where
= strdup(where
);
1677 if (!MOUNT(u
)->where
)
1681 /* In case we have multiple mounts established on the same mount point, let's merge flags set already
1682 * for the current unit. Note that the flags field is reset on each iteration of reading
1683 * /proc/self/mountinfo, hence we know for sure anything already set here is from the current
1684 * iteration and thus worthy of taking into account. */
1685 MountProcFlags flags
=
1686 MOUNT(u
)->proc_flags
| MOUNT_PROC_IS_MOUNTED
;
1688 r
= update_parameters_proc_self_mountinfo(MOUNT(u
), what
, options
, fstype
);
1692 flags
|= MOUNT_PROC_JUST_CHANGED
;
1694 /* There are two conditions when we consider a mount point just mounted: when we haven't seen it in
1695 * /proc/self/mountinfo before or when MOUNT_MOUNTING is our current state. Why bother with the
1696 * latter? Shouldn't that be covered by the former? No, during reload it is not because we might then
1697 * encounter a new /proc/self/mountinfo in combination with an old mount unit state (since it stems
1698 * from the serialized state), and need to catch up. Since we know that the MOUNT_MOUNTING state is
1699 * reached when we wait for the mount to appear we hence can assume that if we are in it, we are
1700 * actually seeing it established for the first time. */
1701 if (!MOUNT(u
)->from_proc_self_mountinfo
|| MOUNT(u
)->state
== MOUNT_MOUNTING
)
1702 flags
|= MOUNT_PROC_JUST_MOUNTED
;
1704 MOUNT(u
)->from_proc_self_mountinfo
= true;
1706 if (IN_SET(u
->load_state
, UNIT_NOT_FOUND
, UNIT_BAD_SETTING
, UNIT_ERROR
)) {
1707 /* The unit was previously not found or otherwise not loaded. Now that the unit shows up in
1708 * /proc/self/mountinfo we should reconsider it this, hence set it to UNIT_LOADED. */
1709 u
->load_state
= UNIT_LOADED
;
1712 flags
|= MOUNT_PROC_JUST_CHANGED
;
1715 if (FLAGS_SET(flags
, MOUNT_PROC_JUST_CHANGED
)) {
1716 /* If things changed, then make sure that all deps are regenerated. Let's
1717 * first remove all automatic deps, and then add in the new ones. */
1718 r
= mount_add_non_exec_dependencies(MOUNT(u
));
1727 static int mount_setup_unit(
1731 const char *options
,
1735 _cleanup_free_
char *e
= NULL
;
1736 MountProcFlags flags
;
1746 /* Ignore API mount points. They should never be referenced in
1747 * dependencies ever. */
1748 if (mount_point_is_api(where
) || mount_point_ignore(where
))
1751 if (streq(fstype
, "autofs"))
1754 /* probably some kind of swap, ignore */
1755 if (!is_path(where
))
1758 r
= unit_name_from_path(where
, ".mount", &e
);
1760 return log_struct_errno(
1762 "MESSAGE_ID=" SD_MESSAGE_MOUNT_POINT_PATH_NOT_SUITABLE_STR
,
1763 "MOUNT_POINT=%s", where
,
1764 LOG_MESSAGE("Failed to generate valid unit name from mount point path '%s', ignoring mount point: %m",
1767 u
= manager_get_unit(m
, e
);
1769 r
= mount_setup_existing_unit(u
, what
, where
, options
, fstype
, &flags
);
1771 /* First time we see this mount point meaning that it's not been initiated by a mount unit
1772 * but rather by the sysadmin having called mount(8) directly. */
1773 r
= mount_setup_new_unit(m
, e
, what
, where
, options
, fstype
, &flags
, &u
);
1775 return log_warning_errno(r
, "Failed to set up mount unit for '%s': %m", where
);
1777 /* If the mount changed properties or state, let's notify our clients */
1778 if (flags
& (MOUNT_PROC_JUST_CHANGED
|MOUNT_PROC_JUST_MOUNTED
))
1779 unit_add_to_dbus_queue(u
);
1782 MOUNT(u
)->proc_flags
= flags
;
1787 static int mount_load_proc_self_mountinfo(Manager
*m
, bool set_flags
) {
1788 _cleanup_(mnt_free_tablep
) struct libmnt_table
*table
= NULL
;
1789 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*iter
= NULL
;
1794 r
= libmount_parse(NULL
, NULL
, &table
, &iter
);
1796 return log_error_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
1799 struct libmnt_fs
*fs
;
1800 const char *device
, *path
, *options
, *fstype
;
1802 r
= mnt_table_next_fs(table
, iter
, &fs
);
1806 return log_error_errno(r
, "Failed to get next entry from /proc/self/mountinfo: %m");
1808 device
= mnt_fs_get_source(fs
);
1809 path
= mnt_fs_get_target(fs
);
1810 options
= mnt_fs_get_options(fs
);
1811 fstype
= mnt_fs_get_fstype(fs
);
1813 if (!device
|| !path
)
1816 device_found_node(m
, device
, DEVICE_FOUND_MOUNT
, DEVICE_FOUND_MOUNT
);
1818 (void) mount_setup_unit(m
, device
, path
, options
, fstype
, set_flags
);
1824 static void mount_shutdown(Manager
*m
) {
1827 m
->mount_event_source
= sd_event_source_disable_unref(m
->mount_event_source
);
1829 mnt_unref_monitor(m
->mount_monitor
);
1830 m
->mount_monitor
= NULL
;
1833 static int mount_get_timeout(Unit
*u
, usec_t
*timeout
) {
1834 Mount
*m
= MOUNT(u
);
1841 if (!m
->timer_event_source
)
1844 r
= sd_event_source_get_time(m
->timer_event_source
, &t
);
1847 if (t
== USEC_INFINITY
)
1854 static void mount_enumerate_perpetual(Manager
*m
) {
1860 /* Whatever happens, we know for sure that the root directory is around, and cannot go away. Let's
1861 * unconditionally synthesize it here and mark it as perpetual. */
1863 u
= manager_get_unit(m
, SPECIAL_ROOT_MOUNT
);
1865 r
= unit_new_for_name(m
, sizeof(Mount
), SPECIAL_ROOT_MOUNT
, &u
);
1867 log_error_errno(r
, "Failed to allocate the special " SPECIAL_ROOT_MOUNT
" unit: %m");
1872 u
->perpetual
= true;
1873 MOUNT(u
)->deserialized_state
= MOUNT_MOUNTED
;
1875 unit_add_to_load_queue(u
);
1876 unit_add_to_dbus_queue(u
);
1879 static bool mount_is_mounted(Mount
*m
) {
1882 return UNIT(m
)->perpetual
|| FLAGS_SET(m
->proc_flags
, MOUNT_PROC_IS_MOUNTED
);
1885 static int mount_on_ratelimit_expire(sd_event_source
*s
, void *userdata
) {
1886 Manager
*m
= ASSERT_PTR(userdata
);
1889 /* Let's enqueue all start jobs that were previously skipped because of active ratelimit. */
1890 HASHMAP_FOREACH(j
, m
->jobs
) {
1891 if (j
->unit
->type
!= UNIT_MOUNT
)
1894 job_add_to_run_queue(j
);
1897 /* By entering ratelimited state we made all mount start jobs not runnable, now rate limit is over so
1898 * let's make sure we dispatch them in the next iteration. */
1899 manager_trigger_run_queue(m
);
1904 static void mount_enumerate(Manager
*m
) {
1911 if (!m
->mount_monitor
) {
1914 m
->mount_monitor
= mnt_new_monitor();
1915 if (!m
->mount_monitor
) {
1920 r
= mnt_monitor_enable_kernel(m
->mount_monitor
, 1);
1922 log_error_errno(r
, "Failed to enable watching of kernel mount events: %m");
1926 r
= mnt_monitor_enable_userspace(m
->mount_monitor
, 1, NULL
);
1928 log_error_errno(r
, "Failed to enable watching of userspace mount events: %m");
1932 /* mnt_unref_monitor() will close the fd */
1933 fd
= r
= mnt_monitor_get_fd(m
->mount_monitor
);
1935 log_error_errno(r
, "Failed to acquire watch file descriptor: %m");
1939 r
= sd_event_add_io(m
->event
, &m
->mount_event_source
, fd
, EPOLLIN
, mount_dispatch_io
, m
);
1941 log_error_errno(r
, "Failed to watch mount file descriptor: %m");
1945 r
= sd_event_source_set_priority(m
->mount_event_source
, SD_EVENT_PRIORITY_NORMAL
-10);
1947 log_error_errno(r
, "Failed to adjust mount watch priority: %m");
1951 r
= sd_event_source_set_ratelimit(m
->mount_event_source
, 1 * USEC_PER_SEC
, 5);
1953 log_error_errno(r
, "Failed to enable rate limit for mount events: %m");
1957 r
= sd_event_source_set_ratelimit_expire_callback(m
->mount_event_source
, mount_on_ratelimit_expire
);
1959 log_error_errno(r
, "Failed to enable rate limit for mount events: %m");
1963 (void) sd_event_source_set_description(m
->mount_event_source
, "mount-monitor-dispatch");
1966 r
= mount_load_proc_self_mountinfo(m
, false);
1976 static int drain_libmount(Manager
*m
) {
1977 bool rescan
= false;
1982 /* Drain all events and verify that the event is valid.
1984 * Note that libmount also monitors /run/mount mkdir if the directory does not exist yet. The mkdir
1985 * may generate event which is irrelevant for us.
1987 * error: r < 0; valid: r == 0, false positive: r == 1 */
1989 r
= mnt_monitor_next_change(m
->mount_monitor
, NULL
, NULL
);
1991 return log_error_errno(r
, "Failed to drain libmount events: %m");
1999 static int mount_process_proc_self_mountinfo(Manager
*m
) {
2000 _cleanup_set_free_ Set
*around
= NULL
, *gone
= NULL
;
2006 r
= drain_libmount(m
);
2010 r
= mount_load_proc_self_mountinfo(m
, true);
2012 /* Reset flags, just in case, for later calls */
2013 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
])
2014 MOUNT(u
)->proc_flags
= 0;
2019 manager_dispatch_load_queue(m
);
2021 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
]) {
2022 Mount
*mount
= MOUNT(u
);
2024 if (!mount_is_mounted(mount
)) {
2026 /* A mount point is not around right now. It
2027 * might be gone, or might never have
2030 if (mount
->from_proc_self_mountinfo
&&
2031 mount
->parameters_proc_self_mountinfo
.what
)
2032 /* Remember that this device might just have disappeared */
2033 if (set_put_strdup_full(&gone
, &path_hash_ops_free
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
2034 log_oom(); /* we don't care too much about OOM here... */
2036 mount
->from_proc_self_mountinfo
= false;
2037 assert_se(update_parameters_proc_self_mountinfo(mount
, NULL
, NULL
, NULL
) >= 0);
2039 switch (mount
->state
) {
2042 /* This has just been unmounted by somebody else, follow the state change. */
2043 mount_enter_dead(mount
, MOUNT_SUCCESS
);
2046 case MOUNT_MOUNTING_DONE
:
2047 /* The mount command may add the corresponding proc mountinfo entry and
2048 * then remove it because of an internal error. E.g., fuse.sshfs seems
2049 * to do that when the connection fails. See #17617. To handle such the
2050 * case, let's once set the state back to mounting. Then, the unit can
2051 * correctly enter the failed state later in mount_sigchld(). */
2052 mount_set_state(mount
, MOUNT_MOUNTING
);
2059 } else if (mount
->proc_flags
& (MOUNT_PROC_JUST_MOUNTED
|MOUNT_PROC_JUST_CHANGED
)) {
2061 /* A mount point was added or changed */
2063 switch (mount
->state
) {
2068 /* This has just been mounted by somebody else, follow the state change, but let's
2069 * generate a new invocation ID for this implicitly and automatically. */
2070 (void) unit_acquire_invocation_id(u
);
2071 mount_cycle_clear(mount
);
2072 mount_enter_mounted(mount
, MOUNT_SUCCESS
);
2075 case MOUNT_MOUNTING
:
2076 mount_set_state(mount
, MOUNT_MOUNTING_DONE
);
2080 /* Nothing really changed, but let's
2081 * issue an notification call
2082 * nonetheless, in case somebody is
2083 * waiting for this. (e.g. file system
2084 * ro/rw remounts.) */
2085 mount_set_state(mount
, mount
->state
);
2090 if (mount_is_mounted(mount
) &&
2091 mount
->from_proc_self_mountinfo
&&
2092 mount
->parameters_proc_self_mountinfo
.what
)
2093 /* Track devices currently used */
2094 if (set_put_strdup_full(&around
, &path_hash_ops_free
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
2097 /* Reset the flags for later calls */
2098 mount
->proc_flags
= 0;
2101 SET_FOREACH(what
, gone
) {
2102 if (set_contains(around
, what
))
2105 /* Let the device units know that the device is no longer mounted */
2106 device_found_node(m
, what
, DEVICE_NOT_FOUND
, DEVICE_FOUND_MOUNT
);
2112 static int mount_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
2113 Manager
*m
= ASSERT_PTR(userdata
);
2115 assert(revents
& EPOLLIN
);
2117 return mount_process_proc_self_mountinfo(m
);
2120 static void mount_reset_failed(Unit
*u
) {
2121 Mount
*m
= MOUNT(u
);
2125 if (m
->state
== MOUNT_FAILED
)
2126 mount_set_state(m
, MOUNT_DEAD
);
2128 m
->result
= MOUNT_SUCCESS
;
2129 m
->reload_result
= MOUNT_SUCCESS
;
2130 m
->clean_result
= MOUNT_SUCCESS
;
2133 static int mount_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
2134 Mount
*m
= MOUNT(u
);
2138 return unit_kill_common(u
, who
, signo
, -1, m
->control_pid
, error
);
2141 static int mount_control_pid(Unit
*u
) {
2142 Mount
*m
= MOUNT(u
);
2146 return m
->control_pid
;
2149 static int mount_clean(Unit
*u
, ExecCleanMask mask
) {
2150 _cleanup_strv_free_
char **l
= NULL
;
2151 Mount
*m
= MOUNT(u
);
2157 if (m
->state
!= MOUNT_DEAD
)
2160 r
= exec_context_get_clean_directories(&m
->exec_context
, u
->manager
->prefix
, mask
, &l
);
2164 if (strv_isempty(l
))
2167 mount_unwatch_control_pid(m
);
2168 m
->clean_result
= MOUNT_SUCCESS
;
2169 m
->control_command
= NULL
;
2170 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
2172 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->exec_context
.timeout_clean_usec
));
2176 r
= unit_fork_and_watch_rm_rf(u
, l
, &m
->control_pid
);
2180 mount_set_state(m
, MOUNT_CLEANING
);
2185 log_unit_warning_errno(u
, r
, "Failed to initiate cleaning: %m");
2186 m
->clean_result
= MOUNT_FAILURE_RESOURCES
;
2187 m
->timer_event_source
= sd_event_source_disable_unref(m
->timer_event_source
);
2191 static int mount_can_clean(Unit
*u
, ExecCleanMask
*ret
) {
2192 Mount
*m
= MOUNT(u
);
2196 return exec_context_get_clean_mask(&m
->exec_context
, ret
);
2199 static int mount_can_start(Unit
*u
) {
2200 Mount
*m
= MOUNT(u
);
2205 r
= unit_test_start_limit(u
);
2207 mount_enter_dead(m
, MOUNT_FAILURE_START_LIMIT_HIT
);
2214 static const char* const mount_exec_command_table
[_MOUNT_EXEC_COMMAND_MAX
] = {
2215 [MOUNT_EXEC_MOUNT
] = "ExecMount",
2216 [MOUNT_EXEC_UNMOUNT
] = "ExecUnmount",
2217 [MOUNT_EXEC_REMOUNT
] = "ExecRemount",
2220 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command
, MountExecCommand
);
2222 static const char* const mount_result_table
[_MOUNT_RESULT_MAX
] = {
2223 [MOUNT_SUCCESS
] = "success",
2224 [MOUNT_FAILURE_RESOURCES
] = "resources",
2225 [MOUNT_FAILURE_TIMEOUT
] = "timeout",
2226 [MOUNT_FAILURE_EXIT_CODE
] = "exit-code",
2227 [MOUNT_FAILURE_SIGNAL
] = "signal",
2228 [MOUNT_FAILURE_CORE_DUMP
] = "core-dump",
2229 [MOUNT_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
2230 [MOUNT_FAILURE_PROTOCOL
] = "protocol",
2233 DEFINE_STRING_TABLE_LOOKUP(mount_result
, MountResult
);
2235 const UnitVTable mount_vtable
= {
2236 .object_size
= sizeof(Mount
),
2237 .exec_context_offset
= offsetof(Mount
, exec_context
),
2238 .cgroup_context_offset
= offsetof(Mount
, cgroup_context
),
2239 .kill_context_offset
= offsetof(Mount
, kill_context
),
2240 .exec_runtime_offset
= offsetof(Mount
, exec_runtime
),
2241 .dynamic_creds_offset
= offsetof(Mount
, dynamic_creds
),
2247 .private_section
= "Mount",
2249 .can_transient
= true,
2251 .exclude_from_switch_root_serialization
= true,
2257 .coldplug
= mount_coldplug
,
2258 .catchup
= mount_catchup
,
2262 .start
= mount_start
,
2264 .reload
= mount_reload
,
2267 .clean
= mount_clean
,
2268 .can_clean
= mount_can_clean
,
2270 .serialize
= mount_serialize
,
2271 .deserialize_item
= mount_deserialize_item
,
2273 .active_state
= mount_active_state
,
2274 .sub_state_to_string
= mount_sub_state_to_string
,
2276 .will_restart
= unit_will_restart_default
,
2278 .may_gc
= mount_may_gc
,
2279 .is_extrinsic
= mount_is_extrinsic
,
2281 .sigchld_event
= mount_sigchld_event
,
2283 .reset_failed
= mount_reset_failed
,
2285 .control_pid
= mount_control_pid
,
2287 .bus_set_property
= bus_mount_set_property
,
2288 .bus_commit_properties
= bus_mount_commit_properties
,
2290 .get_timeout
= mount_get_timeout
,
2292 .enumerate_perpetual
= mount_enumerate_perpetual
,
2293 .enumerate
= mount_enumerate
,
2294 .shutdown
= mount_shutdown
,
2296 .status_message_formats
= {
2297 .starting_stopping
= {
2298 [0] = "Mounting %s...",
2299 [1] = "Unmounting %s...",
2301 .finished_start_job
= {
2302 [JOB_DONE
] = "Mounted %s.",
2303 [JOB_FAILED
] = "Failed to mount %s.",
2304 [JOB_TIMEOUT
] = "Timed out mounting %s.",
2306 .finished_stop_job
= {
2307 [JOB_DONE
] = "Unmounted %s.",
2308 [JOB_FAILED
] = "Failed unmounting %s.",
2309 [JOB_TIMEOUT
] = "Timed out unmounting %s.",
2313 .can_start
= mount_can_start
,