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 "libmount-util.h"
20 #include "mkdir-label.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 void mount_enter_dead(Mount
*m
, MountResult f
);
55 static void mount_enter_mounted(Mount
*m
, MountResult f
);
56 static void mount_cycle_clear(Mount
*m
);
57 static int mount_process_proc_self_mountinfo(Manager
*m
);
59 static bool MOUNT_STATE_WITH_PROCESS(MountState state
) {
64 MOUNT_REMOUNTING_SIGTERM
,
65 MOUNT_REMOUNTING_SIGKILL
,
67 MOUNT_UNMOUNTING_SIGTERM
,
68 MOUNT_UNMOUNTING_SIGKILL
,
72 static MountParameters
* get_mount_parameters_fragment(Mount
*m
) {
76 return &m
->parameters_fragment
;
81 static MountParameters
* get_mount_parameters(Mount
*m
) {
84 if (m
->from_proc_self_mountinfo
)
85 return &m
->parameters_proc_self_mountinfo
;
87 return get_mount_parameters_fragment(m
);
90 static bool mount_is_network(const MountParameters
*p
) {
93 if (fstab_test_option(p
->options
, "_netdev\0"))
96 if (p
->fstype
&& fstype_is_network(p
->fstype
))
102 static bool mount_is_nofail(const Mount
*m
) {
105 if (!m
->from_fragment
)
108 return fstab_test_yes_no_option(m
->parameters_fragment
.options
, "nofail\0" "fail\0");
111 static bool mount_is_loop(const MountParameters
*p
) {
114 if (fstab_test_option(p
->options
, "loop\0"))
120 static bool mount_is_bind(const MountParameters
*p
) {
123 if (fstab_test_option(p
->options
, "bind\0" "rbind\0"))
126 if (p
->fstype
&& STR_IN_SET(p
->fstype
, "bind", "rbind"))
132 static bool mount_is_bound_to_device(Mount
*m
) {
133 const MountParameters
*p
;
137 /* Determines whether to place a Requires= or BindsTo= dependency on the backing device unit. We do
138 * this by checking for the x-systemd.device-bound mount option. Iff it is set we use BindsTo=,
139 * otherwise Requires=. But note that we might combine the latter with StopPropagatedFrom=, see
142 p
= get_mount_parameters(m
);
146 return fstab_test_option(p
->options
, "x-systemd.device-bound\0");
149 static bool mount_propagate_stop(Mount
*m
) {
152 if (mount_is_bound_to_device(m
)) /* If we are using BindsTo= the stop propagation is implicit, no need to bother */
155 return m
->from_fragment
; /* let's propagate stop whenever this is an explicitly configured unit,
156 * otherwise let's not bother. */
159 static bool mount_needs_quota(const MountParameters
*p
) {
162 /* Quotas are not enabled on network filesystems, but we want them, for example, on storage connected via
163 * iscsi. We hence don't use mount_is_network() here, as that would also return true for _netdev devices. */
164 if (p
->fstype
&& fstype_is_network(p
->fstype
))
167 if (mount_is_bind(p
))
170 return fstab_test_option(p
->options
,
171 "usrquota\0" "grpquota\0" "quota\0" "usrjquota\0" "grpjquota\0");
174 static void mount_init(Unit
*u
) {
179 assert(u
->load_state
== UNIT_STUB
);
181 m
->timeout_usec
= u
->manager
->default_timeout_start_usec
;
183 m
->exec_context
.std_output
= u
->manager
->default_std_output
;
184 m
->exec_context
.std_error
= u
->manager
->default_std_error
;
186 m
->directory_mode
= 0755;
188 /* We need to make sure that /usr/bin/mount is always called
189 * in the same process group as us, so that the autofs kernel
190 * side doesn't send us another mount request while we are
191 * already trying to comply its last one. */
192 m
->exec_context
.same_pgrp
= true;
194 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
196 u
->ignore_on_isolate
= true;
199 static int mount_arm_timer(Mount
*m
, usec_t usec
) {
204 if (m
->timer_event_source
) {
205 r
= sd_event_source_set_time(m
->timer_event_source
, usec
);
209 return sd_event_source_set_enabled(m
->timer_event_source
, SD_EVENT_ONESHOT
);
212 if (usec
== USEC_INFINITY
)
215 r
= sd_event_add_time(
216 UNIT(m
)->manager
->event
,
217 &m
->timer_event_source
,
220 mount_dispatch_timer
, m
);
224 (void) sd_event_source_set_description(m
->timer_event_source
, "mount-timer");
229 static void mount_unwatch_control_pid(Mount
*m
) {
232 if (m
->control_pid
<= 0)
235 unit_unwatch_pid(UNIT(m
), TAKE_PID(m
->control_pid
));
238 static void mount_parameters_done(MountParameters
*p
) {
241 p
->what
= mfree(p
->what
);
242 p
->options
= mfree(p
->options
);
243 p
->fstype
= mfree(p
->fstype
);
246 static void mount_done(Unit
*u
) {
251 m
->where
= mfree(m
->where
);
253 mount_parameters_done(&m
->parameters_proc_self_mountinfo
);
254 mount_parameters_done(&m
->parameters_fragment
);
256 m
->exec_runtime
= exec_runtime_unref(m
->exec_runtime
, false);
257 exec_command_done_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
258 m
->control_command
= NULL
;
260 dynamic_creds_unref(&m
->dynamic_creds
);
262 mount_unwatch_control_pid(m
);
264 m
->timer_event_source
= sd_event_source_disable_unref(m
->timer_event_source
);
267 static int update_parameters_proc_self_mountinfo(
271 const char *fstype
) {
276 p
= &m
->parameters_proc_self_mountinfo
;
278 r
= free_and_strdup(&p
->what
, what
);
282 q
= free_and_strdup(&p
->options
, options
);
286 w
= free_and_strdup(&p
->fstype
, fstype
);
290 return r
> 0 || q
> 0 || w
> 0;
293 static int mount_add_mount_dependencies(Mount
*m
) {
301 if (!path_equal(m
->where
, "/")) {
302 _cleanup_free_
char *parent
= NULL
;
304 /* Adds in links to other mount points that might lie further up in the hierarchy */
306 r
= path_extract_directory(m
->where
, &parent
);
310 r
= unit_require_mounts_for(UNIT(m
), parent
, UNIT_DEPENDENCY_IMPLICIT
);
315 /* Adds in dependencies to other mount points that might be needed for the source path (if this is a bind mount
316 * or a loop mount) to be available. */
317 pm
= get_mount_parameters_fragment(m
);
318 if (pm
&& pm
->what
&&
319 path_is_absolute(pm
->what
) &&
320 (mount_is_bind(pm
) || mount_is_loop(pm
) || !mount_is_network(pm
))) {
322 r
= unit_require_mounts_for(UNIT(m
), pm
->what
, UNIT_DEPENDENCY_FILE
);
327 /* Adds in dependencies to other units that use this path or paths further down in the hierarchy */
328 s
= manager_get_units_requiring_mounts_for(UNIT(m
)->manager
, m
->where
);
329 SET_FOREACH(other
, s
) {
331 if (other
->load_state
!= UNIT_LOADED
)
334 if (other
== UNIT(m
))
337 r
= unit_add_dependency(other
, UNIT_AFTER
, UNIT(m
), true, UNIT_DEPENDENCY_PATH
);
341 if (UNIT(m
)->fragment_path
) {
342 /* If we have fragment configuration, then make this dependency required */
343 r
= unit_add_dependency(other
, UNIT_REQUIRES
, UNIT(m
), true, UNIT_DEPENDENCY_PATH
);
352 static int mount_add_device_dependencies(Mount
*m
) {
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 r
= unit_add_node_dependency(UNIT(m
), p
->what
, dep
, UNIT_DEPENDENCY_MOUNTINFO_OR_FILE
);
405 log_unit_trace(UNIT(m
), "Added %s dependency on %s", unit_dependency_to_string(dep
), p
->what
);
407 if (mount_propagate_stop(m
)) {
408 r
= unit_add_node_dependency(UNIT(m
), p
->what
, UNIT_STOP_PROPAGATED_FROM
, UNIT_DEPENDENCY_MOUNTINFO_OR_FILE
);
412 log_unit_trace(UNIT(m
), "Added %s dependency on %s",
413 unit_dependency_to_string(UNIT_STOP_PROPAGATED_FROM
), p
->what
);
416 r
= unit_add_blockdev_dependency(UNIT(m
), p
->what
, UNIT_DEPENDENCY_MOUNTINFO_OR_FILE
);
418 log_unit_trace(UNIT(m
), "Added %s dependency on %s", unit_dependency_to_string(UNIT_AFTER
), p
->what
);
423 static int mount_add_quota_dependencies(Mount
*m
) {
424 UnitDependencyMask mask
;
430 if (!MANAGER_IS_SYSTEM(UNIT(m
)->manager
))
433 p
= get_mount_parameters_fragment(m
);
437 if (!mount_needs_quota(p
))
440 mask
= m
->from_fragment
? UNIT_DEPENDENCY_FILE
: UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
;
442 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTACHECK_SERVICE
, true, mask
);
446 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTAON_SERVICE
, true, mask
);
453 static bool mount_is_extrinsic(Unit
*u
) {
458 /* Returns true for all units that are "magic" and should be excluded from the usual
459 * start-up and shutdown dependencies. We call them "extrinsic" here, as they are generally
460 * mounted outside of the systemd dependency logic. We shouldn't attempt to manage them
461 * ourselves but it's fine if the user operates on them with us. */
463 /* We only automatically manage mounts if we are in system mode */
464 if (MANAGER_IS_USER(u
->manager
))
467 p
= get_mount_parameters(m
);
468 if (p
&& fstab_is_extrinsic(m
->where
, p
->options
))
474 static int mount_add_default_ordering_dependencies(
476 MountParameters
*p
) {
478 const char *after
, *before
, *e
;
483 e
= path_startswith(m
->where
, "/sysroot");
484 if (e
&& in_initrd()) {
485 /* All mounts under /sysroot need to happen later, at initrd-fs.target time. IOW,
486 * it's not technically part of the basic initrd filesystem itself, and so
487 * shouldn't inherit the default Before=local-fs.target dependency. */
490 before
= isempty(e
) ? SPECIAL_INITRD_ROOT_FS_TARGET
: SPECIAL_INITRD_FS_TARGET
;
492 } else if (mount_is_network(p
)) {
493 after
= SPECIAL_REMOTE_FS_PRE_TARGET
;
494 before
= SPECIAL_REMOTE_FS_TARGET
;
497 after
= SPECIAL_LOCAL_FS_PRE_TARGET
;
498 before
= SPECIAL_LOCAL_FS_TARGET
;
501 if (!mount_is_nofail(m
)) {
502 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_BEFORE
, before
, true,
503 UNIT_DEPENDENCY_MOUNTINFO_OR_FILE
);
509 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, after
, true,
510 UNIT_DEPENDENCY_MOUNTINFO_OR_FILE
);
515 return unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_CONFLICTS
,
516 SPECIAL_UMOUNT_TARGET
, true,
517 UNIT_DEPENDENCY_MOUNTINFO_OR_FILE
);
520 static int mount_add_default_dependencies(Mount
*m
) {
526 if (!UNIT(m
)->default_dependencies
)
529 /* We do not add any default dependencies to /, /usr or /run/initramfs/, since they are
530 * guaranteed to stay mounted the whole time, since our system is on it. Also, don't
531 * bother with anything mounted below virtual file systems, it's also going to be virtual,
532 * and hence not worth the effort. */
533 if (mount_is_extrinsic(UNIT(m
)))
536 p
= get_mount_parameters(m
);
540 r
= mount_add_default_ordering_dependencies(m
, p
);
544 if (mount_is_network(p
)) {
545 /* We order ourselves after network.target. This is primarily useful at shutdown:
546 * services that take down the network should order themselves before
547 * network.target, so that they are shut down only after this mount unit is
550 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, SPECIAL_NETWORK_TARGET
, true,
551 UNIT_DEPENDENCY_MOUNTINFO_OR_FILE
);
555 /* We pull in network-online.target, and order ourselves after it. This is useful
556 * at start-up to actively pull in tools that want to be started before we start
557 * mounting network file systems, and whose purpose it is to delay this until the
558 * network is "up". */
560 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_WANTS
, UNIT_AFTER
,
561 SPECIAL_NETWORK_ONLINE_TARGET
, true,
562 UNIT_DEPENDENCY_MOUNTINFO_OR_FILE
);
567 /* If this is a tmpfs mount then we have to unmount it before we try to deactivate swaps */
568 if (streq_ptr(p
->fstype
, "tmpfs")) {
569 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, SPECIAL_SWAP_TARGET
, true,
570 UNIT_DEPENDENCY_MOUNTINFO_OR_FILE
);
578 static int mount_verify(Mount
*m
) {
579 _cleanup_free_
char *e
= NULL
;
584 assert(UNIT(m
)->load_state
== UNIT_LOADED
);
586 if (!m
->from_fragment
&& !m
->from_proc_self_mountinfo
&& !UNIT(m
)->perpetual
)
589 r
= unit_name_from_path(m
->where
, ".mount", &e
);
591 return log_unit_error_errno(UNIT(m
), r
, "Failed to generate unit name from mount path: %m");
593 if (!unit_has_name(UNIT(m
), e
))
594 return log_unit_error_errno(UNIT(m
), SYNTHETIC_ERRNO(ENOEXEC
), "Where= setting doesn't match unit name. Refusing.");
596 if (mount_point_is_api(m
->where
) || mount_point_ignore(m
->where
))
597 return log_unit_error_errno(UNIT(m
), SYNTHETIC_ERRNO(ENOEXEC
), "Cannot create mount unit for API file system %s. Refusing.", m
->where
);
599 p
= get_mount_parameters_fragment(m
);
600 if (p
&& !p
->what
&& !UNIT(m
)->perpetual
)
601 return log_unit_error_errno(UNIT(m
), SYNTHETIC_ERRNO(ENOEXEC
),
602 "What= setting is missing. Refusing.");
604 if (m
->exec_context
.pam_name
&& m
->kill_context
.kill_mode
!= KILL_CONTROL_GROUP
)
605 return log_unit_error_errno(UNIT(m
), SYNTHETIC_ERRNO(ENOEXEC
), "Unit has PAM enabled. Kill mode must be set to control-group'. Refusing.");
610 static int mount_add_non_exec_dependencies(Mount
*m
) {
615 /* Any dependencies based on /proc/self/mountinfo may be now stale. */
616 unit_remove_dependencies(UNIT(m
),
617 UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
|
618 UNIT_DEPENDENCY_MOUNTINFO_DEFAULT
|
619 UNIT_DEPENDENCY_MOUNTINFO_OR_FILE
);
624 /* Adds in all dependencies directly responsible for ordering the mount, as opposed to dependencies
625 * resulting from the ExecContext and such. */
627 r
= mount_add_device_dependencies(m
);
631 r
= mount_add_mount_dependencies(m
);
635 r
= mount_add_quota_dependencies(m
);
639 r
= mount_add_default_dependencies(m
);
646 static int mount_add_extras(Mount
*m
) {
652 /* Note: this call might be called after we already have been loaded once (and even when it has already been
653 * activated), in case data from /proc/self/mountinfo has changed. This means all code here needs to be ready
654 * to run with an already set up unit. */
656 if (u
->fragment_path
)
657 m
->from_fragment
= true;
660 r
= unit_name_to_path(u
->id
, &m
->where
);
661 if (r
== -ENAMETOOLONG
)
662 log_unit_error_errno(u
, r
, "Failed to derive mount point path from unit name, because unit name is hashed. "
663 "Set \"Where=\" in the unit file explicitly.");
668 path_simplify(m
->where
);
670 if (!u
->description
) {
671 r
= unit_set_description(u
, m
->where
);
676 r
= unit_patch_contexts(u
);
680 r
= unit_add_exec_dependencies(u
, &m
->exec_context
);
684 r
= unit_set_default_slice(u
);
688 r
= mount_add_non_exec_dependencies(m
);
695 static void mount_load_root_mount(Unit
*u
) {
698 if (!unit_has_name(u
, SPECIAL_ROOT_MOUNT
))
702 u
->default_dependencies
= false;
704 /* The stdio/kmsg bridge socket is on /, in order to avoid a dep loop, don't use kmsg logging for -.mount */
705 MOUNT(u
)->exec_context
.std_output
= EXEC_OUTPUT_NULL
;
706 MOUNT(u
)->exec_context
.std_input
= EXEC_INPUT_NULL
;
709 u
->description
= strdup("Root Mount");
712 static int mount_load(Unit
*u
) {
718 assert(u
->load_state
== UNIT_STUB
);
720 mount_load_root_mount(u
);
722 bool fragment_optional
= m
->from_proc_self_mountinfo
|| u
->perpetual
;
723 r
= unit_load_fragment_and_dropin(u
, !fragment_optional
);
725 /* Add in some extras. Note we do this in all cases (even if we failed to load the unit) when announced by the
726 * kernel, because we need some things to be set up no matter what when the kernel establishes a mount and thus
727 * we need to update the state in our unit to track it. After all, consider that we don't allow changing the
728 * 'slice' field for a unit once it is active. */
729 if (u
->load_state
== UNIT_LOADED
|| m
->from_proc_self_mountinfo
|| u
->perpetual
)
730 q
= mount_add_extras(m
);
736 if (u
->load_state
!= UNIT_LOADED
)
739 return mount_verify(m
);
742 static void mount_set_state(Mount
*m
, MountState state
) {
743 MountState old_state
;
746 if (m
->state
!= state
)
747 bus_unit_send_pending_change_signal(UNIT(m
), false);
749 old_state
= m
->state
;
752 if (!MOUNT_STATE_WITH_PROCESS(state
)) {
753 m
->timer_event_source
= sd_event_source_disable_unref(m
->timer_event_source
);
754 mount_unwatch_control_pid(m
);
755 m
->control_command
= NULL
;
756 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
759 if (state
!= old_state
)
760 log_unit_debug(UNIT(m
), "Changed %s -> %s", mount_state_to_string(old_state
), mount_state_to_string(state
));
762 unit_notify(UNIT(m
), state_translation_table
[old_state
], state_translation_table
[state
],
763 m
->reload_result
== MOUNT_SUCCESS
? 0 : UNIT_NOTIFY_RELOAD_FAILURE
);
766 static int mount_coldplug(Unit
*u
) {
771 assert(m
->state
== MOUNT_DEAD
);
773 if (m
->deserialized_state
== m
->state
)
776 if (m
->control_pid
> 0 &&
777 pid_is_unwaited(m
->control_pid
) &&
778 MOUNT_STATE_WITH_PROCESS(m
->deserialized_state
)) {
780 r
= unit_watch_pid(UNIT(m
), m
->control_pid
, false);
784 r
= mount_arm_timer(m
, usec_add(u
->state_change_timestamp
.monotonic
, m
->timeout_usec
));
789 if (!IN_SET(m
->deserialized_state
, MOUNT_DEAD
, MOUNT_FAILED
)) {
790 (void) unit_setup_dynamic_creds(u
);
791 (void) unit_setup_exec_runtime(u
);
794 mount_set_state(m
, m
->deserialized_state
);
798 static void mount_catchup(Unit
*u
) {
799 Mount
*m
= MOUNT(ASSERT_PTR(u
));
803 /* Adjust the deserialized state. See comments in mount_process_proc_self_mountinfo(). */
804 if (m
->from_proc_self_mountinfo
)
808 assert(m
->control_pid
== 0);
809 (void) unit_acquire_invocation_id(u
);
810 mount_cycle_clear(m
);
811 mount_enter_mounted(m
, MOUNT_SUCCESS
);
814 assert(m
->control_pid
> 0);
815 mount_set_state(m
, MOUNT_MOUNTING_DONE
);
822 case MOUNT_MOUNTING_DONE
:
823 assert(m
->control_pid
> 0);
824 mount_set_state(m
, MOUNT_MOUNTING
);
827 assert(m
->control_pid
== 0);
828 mount_enter_dead(m
, MOUNT_SUCCESS
);
835 static void mount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
842 p
= get_mount_parameters(m
);
845 "%sMount State: %s\n"
847 "%sClean Result: %s\n"
850 "%sFile System Type: %s\n"
852 "%sFrom /proc/self/mountinfo: %s\n"
853 "%sFrom fragment: %s\n"
855 "%sDirectoryMode: %04o\n"
856 "%sSloppyOptions: %s\n"
857 "%sLazyUnmount: %s\n"
858 "%sForceUnmount: %s\n"
859 "%sReadWriteOnly: %s\n"
860 "%sTimeoutSec: %s\n",
861 prefix
, mount_state_to_string(m
->state
),
862 prefix
, mount_result_to_string(m
->result
),
863 prefix
, mount_result_to_string(m
->clean_result
),
865 prefix
, p
? strna(p
->what
) : "n/a",
866 prefix
, p
? strna(p
->fstype
) : "n/a",
867 prefix
, p
? strna(p
->options
) : "n/a",
868 prefix
, yes_no(m
->from_proc_self_mountinfo
),
869 prefix
, yes_no(m
->from_fragment
),
870 prefix
, yes_no(mount_is_extrinsic(u
)),
871 prefix
, m
->directory_mode
,
872 prefix
, yes_no(m
->sloppy_options
),
873 prefix
, yes_no(m
->lazy_unmount
),
874 prefix
, yes_no(m
->force_unmount
),
875 prefix
, yes_no(m
->read_write_only
),
876 prefix
, FORMAT_TIMESPAN(m
->timeout_usec
, USEC_PER_SEC
));
878 if (m
->control_pid
> 0)
880 "%sControl PID: "PID_FMT
"\n",
881 prefix
, m
->control_pid
);
883 exec_context_dump(&m
->exec_context
, f
, prefix
);
884 kill_context_dump(&m
->kill_context
, f
, prefix
);
885 cgroup_context_dump(UNIT(m
), f
, prefix
);
888 static int mount_spawn(Mount
*m
, ExecCommand
*c
, pid_t
*_pid
) {
890 _cleanup_(exec_params_clear
) ExecParameters exec_params
= {
891 .flags
= EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
,
904 r
= unit_prepare_exec(UNIT(m
));
908 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->timeout_usec
));
912 r
= unit_set_exec_params(UNIT(m
), &exec_params
);
916 r
= exec_spawn(UNIT(m
),
926 r
= unit_watch_pid(UNIT(m
), pid
, true);
935 static void mount_enter_dead(Mount
*m
, MountResult f
) {
938 if (m
->result
== MOUNT_SUCCESS
)
941 unit_log_result(UNIT(m
), m
->result
== MOUNT_SUCCESS
, mount_result_to_string(m
->result
));
942 unit_warn_leftover_processes(UNIT(m
), unit_log_leftover_process_stop
);
944 mount_set_state(m
, m
->result
!= MOUNT_SUCCESS
? MOUNT_FAILED
: MOUNT_DEAD
);
946 m
->exec_runtime
= exec_runtime_unref(m
->exec_runtime
, true);
948 unit_destroy_runtime_data(UNIT(m
), &m
->exec_context
);
950 unit_unref_uid_gid(UNIT(m
), true);
952 dynamic_creds_destroy(&m
->dynamic_creds
);
954 /* Any dependencies based on /proc/self/mountinfo are now stale. Let's re-generate dependencies from
956 (void) mount_add_non_exec_dependencies(m
);
959 static void mount_enter_mounted(Mount
*m
, MountResult f
) {
962 if (m
->result
== MOUNT_SUCCESS
)
965 mount_set_state(m
, MOUNT_MOUNTED
);
968 static void mount_enter_dead_or_mounted(Mount
*m
, MountResult f
) {
971 /* Enter DEAD or MOUNTED state, depending on what the kernel currently says about the mount point. We use this
972 * whenever we executed an operation, so that our internal state reflects what the kernel says again, after all
973 * ultimately we just mirror the kernel's internal state on this. */
975 if (m
->from_proc_self_mountinfo
)
976 mount_enter_mounted(m
, f
);
978 mount_enter_dead(m
, f
);
981 static int state_to_kill_operation(MountState state
) {
984 case MOUNT_REMOUNTING_SIGTERM
:
987 case MOUNT_UNMOUNTING_SIGTERM
:
988 return KILL_TERMINATE
;
990 case MOUNT_REMOUNTING_SIGKILL
:
991 case MOUNT_UNMOUNTING_SIGKILL
:
995 return _KILL_OPERATION_INVALID
;
999 static void mount_enter_signal(Mount
*m
, MountState state
, MountResult f
) {
1004 if (m
->result
== MOUNT_SUCCESS
)
1007 r
= unit_kill_context(
1010 state_to_kill_operation(state
),
1018 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->timeout_usec
));
1022 mount_set_state(m
, state
);
1023 } else if (state
== MOUNT_REMOUNTING_SIGTERM
&& m
->kill_context
.send_sigkill
)
1024 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
1025 else if (IN_SET(state
, MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
))
1026 mount_enter_mounted(m
, MOUNT_SUCCESS
);
1027 else if (state
== MOUNT_UNMOUNTING_SIGTERM
&& m
->kill_context
.send_sigkill
)
1028 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
1030 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1035 log_unit_warning_errno(UNIT(m
), r
, "Failed to kill processes: %m");
1036 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
1039 static void mount_enter_unmounting(Mount
*m
) {
1044 /* Start counting our attempts */
1045 if (!IN_SET(m
->state
,
1047 MOUNT_UNMOUNTING_SIGTERM
,
1048 MOUNT_UNMOUNTING_SIGKILL
))
1049 m
->n_retry_umount
= 0;
1051 m
->control_command_id
= MOUNT_EXEC_UNMOUNT
;
1052 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_UNMOUNT
;
1054 r
= exec_command_set(m
->control_command
, UMOUNT_PATH
, m
->where
, "-c", NULL
);
1055 if (r
>= 0 && m
->lazy_unmount
)
1056 r
= exec_command_append(m
->control_command
, "-l", NULL
);
1057 if (r
>= 0 && m
->force_unmount
)
1058 r
= exec_command_append(m
->control_command
, "-f", NULL
);
1062 mount_unwatch_control_pid(m
);
1064 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
1068 mount_set_state(m
, MOUNT_UNMOUNTING
);
1073 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'umount' task: %m");
1074 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
1077 static void mount_enter_mounting(Mount
*m
) {
1083 r
= unit_fail_if_noncanonical(UNIT(m
), m
->where
);
1087 (void) mkdir_p_label(m
->where
, m
->directory_mode
);
1089 unit_warn_if_dir_nonempty(UNIT(m
), m
->where
);
1090 unit_warn_leftover_processes(UNIT(m
), unit_log_leftover_process_start
);
1092 m
->control_command_id
= MOUNT_EXEC_MOUNT
;
1093 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_MOUNT
;
1095 /* Create the source directory for bind-mounts if needed */
1096 p
= get_mount_parameters_fragment(m
);
1097 if (p
&& mount_is_bind(p
)) {
1098 r
= mkdir_p_label(p
->what
, m
->directory_mode
);
1099 /* mkdir_p_label() can return -EEXIST if the target path exists and is not a directory - which is
1100 * totally OK, in case the user wants us to overmount a non-directory inode. Also -EROFS can be
1101 * returned on read-only filesystem. Moreover, -EACCES (and also maybe -EPERM?) may be returned
1102 * when the path is on NFS. See issue #24120. All such errors will be logged in the debug level. */
1103 if (r
< 0 && r
!= -EEXIST
)
1104 log_unit_full_errno(UNIT(m
),
1105 (r
== -EROFS
|| ERRNO_IS_PRIVILEGE(r
)) ? LOG_DEBUG
: LOG_WARNING
,
1106 r
, "Failed to make bind mount source '%s', ignoring: %m", p
->what
);
1110 _cleanup_free_
char *opts
= NULL
;
1112 r
= fstab_filter_options(p
->options
, "nofail\0" "noauto\0" "auto\0", NULL
, NULL
, NULL
, &opts
);
1116 r
= exec_command_set(m
->control_command
, MOUNT_PATH
, p
->what
, m
->where
, NULL
);
1117 if (r
>= 0 && m
->sloppy_options
)
1118 r
= exec_command_append(m
->control_command
, "-s", NULL
);
1119 if (r
>= 0 && m
->read_write_only
)
1120 r
= exec_command_append(m
->control_command
, "-w", NULL
);
1121 if (r
>= 0 && p
->fstype
)
1122 r
= exec_command_append(m
->control_command
, "-t", p
->fstype
, NULL
);
1123 if (r
>= 0 && !isempty(opts
))
1124 r
= exec_command_append(m
->control_command
, "-o", opts
, NULL
);
1130 mount_unwatch_control_pid(m
);
1132 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
1136 mount_set_state(m
, MOUNT_MOUNTING
);
1141 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'mount' task: %m");
1142 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
1145 static void mount_set_reload_result(Mount
*m
, MountResult result
) {
1148 /* Only store the first error we encounter */
1149 if (m
->reload_result
!= MOUNT_SUCCESS
)
1152 m
->reload_result
= result
;
1155 static void mount_enter_remounting(Mount
*m
) {
1161 /* Reset reload result when we are about to start a new remount operation */
1162 m
->reload_result
= MOUNT_SUCCESS
;
1164 m
->control_command_id
= MOUNT_EXEC_REMOUNT
;
1165 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_REMOUNT
;
1167 p
= get_mount_parameters_fragment(m
);
1172 o
= strjoina("remount,", p
->options
);
1176 r
= exec_command_set(m
->control_command
, MOUNT_PATH
,
1179 if (r
>= 0 && m
->sloppy_options
)
1180 r
= exec_command_append(m
->control_command
, "-s", NULL
);
1181 if (r
>= 0 && m
->read_write_only
)
1182 r
= exec_command_append(m
->control_command
, "-w", NULL
);
1183 if (r
>= 0 && p
->fstype
)
1184 r
= exec_command_append(m
->control_command
, "-t", p
->fstype
, NULL
);
1190 mount_unwatch_control_pid(m
);
1192 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
1196 mount_set_state(m
, MOUNT_REMOUNTING
);
1201 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'remount' task: %m");
1202 mount_set_reload_result(m
, MOUNT_FAILURE_RESOURCES
);
1203 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1206 static void mount_cycle_clear(Mount
*m
) {
1209 /* Clear all state we shall forget for this new cycle */
1211 m
->result
= MOUNT_SUCCESS
;
1212 m
->reload_result
= MOUNT_SUCCESS
;
1213 exec_command_reset_status_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
1214 UNIT(m
)->reset_accounting
= true;
1217 static int mount_start(Unit
*u
) {
1218 Mount
*m
= MOUNT(u
);
1223 /* We cannot fulfill this request right now, try again later
1225 if (IN_SET(m
->state
,
1227 MOUNT_UNMOUNTING_SIGTERM
,
1228 MOUNT_UNMOUNTING_SIGKILL
,
1232 /* Already on it! */
1233 if (IN_SET(m
->state
, MOUNT_MOUNTING
, MOUNT_MOUNTING_DONE
))
1236 assert(IN_SET(m
->state
, MOUNT_DEAD
, MOUNT_FAILED
));
1238 r
= unit_acquire_invocation_id(u
);
1242 mount_cycle_clear(m
);
1243 mount_enter_mounting(m
);
1248 static int mount_stop(Unit
*u
) {
1249 Mount
*m
= MOUNT(u
);
1255 case MOUNT_UNMOUNTING
:
1256 case MOUNT_UNMOUNTING_SIGKILL
:
1257 case MOUNT_UNMOUNTING_SIGTERM
:
1261 case MOUNT_MOUNTING
:
1262 case MOUNT_MOUNTING_DONE
:
1263 case MOUNT_REMOUNTING
:
1264 /* If we are still waiting for /bin/mount, we go directly into kill mode. */
1265 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_SUCCESS
);
1268 case MOUNT_REMOUNTING_SIGTERM
:
1269 /* If we are already waiting for a hung remount, convert this to the matching unmounting state */
1270 mount_set_state(m
, MOUNT_UNMOUNTING_SIGTERM
);
1273 case MOUNT_REMOUNTING_SIGKILL
:
1275 mount_set_state(m
, MOUNT_UNMOUNTING_SIGKILL
);
1279 mount_enter_unmounting(m
);
1282 case MOUNT_CLEANING
:
1283 /* If we are currently cleaning, then abort it, brutally. */
1284 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
1288 assert_not_reached();
1292 static int mount_reload(Unit
*u
) {
1293 Mount
*m
= MOUNT(u
);
1296 assert(m
->state
== MOUNT_MOUNTED
);
1298 mount_enter_remounting(m
);
1303 static int mount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1304 Mount
*m
= MOUNT(u
);
1310 (void) serialize_item(f
, "state", mount_state_to_string(m
->state
));
1311 (void) serialize_item(f
, "result", mount_result_to_string(m
->result
));
1312 (void) serialize_item(f
, "reload-result", mount_result_to_string(m
->reload_result
));
1313 (void) serialize_item_format(f
, "n-retry-umount", "%u", m
->n_retry_umount
);
1315 if (m
->control_pid
> 0)
1316 (void) serialize_item_format(f
, "control-pid", PID_FMT
, m
->control_pid
);
1318 if (m
->control_command_id
>= 0)
1319 (void) serialize_item(f
, "control-command", mount_exec_command_to_string(m
->control_command_id
));
1324 static int mount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1325 Mount
*m
= MOUNT(u
);
1334 if (streq(key
, "state")) {
1337 state
= mount_state_from_string(value
);
1339 log_unit_debug_errno(u
, state
, "Failed to parse state value: %s", value
);
1341 m
->deserialized_state
= state
;
1343 } else if (streq(key
, "result")) {
1346 f
= mount_result_from_string(value
);
1348 log_unit_debug_errno(u
, f
, "Failed to parse result value: %s", value
);
1349 else if (f
!= MOUNT_SUCCESS
)
1352 } else if (streq(key
, "reload-result")) {
1355 f
= mount_result_from_string(value
);
1357 log_unit_debug_errno(u
, f
, "Failed to parse reload result value: %s", value
);
1358 else if (f
!= MOUNT_SUCCESS
)
1359 m
->reload_result
= f
;
1361 } else if (streq(key
, "n-retry-umount")) {
1363 r
= safe_atou(value
, &m
->n_retry_umount
);
1365 log_unit_debug_errno(u
, r
, "Failed to parse n-retry-umount value: %s", value
);
1367 } else if (streq(key
, "control-pid")) {
1369 r
= parse_pid(value
, &m
->control_pid
);
1371 log_unit_debug_errno(u
, r
, "Failed to parse control-pid value: %s", value
);
1373 } else if (streq(key
, "control-command")) {
1374 MountExecCommand id
;
1376 id
= mount_exec_command_from_string(value
);
1378 log_unit_debug_errno(u
, id
, "Failed to parse exec-command value: %s", value
);
1380 m
->control_command_id
= id
;
1381 m
->control_command
= m
->exec_command
+ id
;
1384 log_unit_debug(u
, "Unknown serialization key: %s", key
);
1389 _pure_
static UnitActiveState
mount_active_state(Unit
*u
) {
1392 return state_translation_table
[MOUNT(u
)->state
];
1395 _pure_
static const char *mount_sub_state_to_string(Unit
*u
) {
1398 return mount_state_to_string(MOUNT(u
)->state
);
1401 _pure_
static bool mount_may_gc(Unit
*u
) {
1402 Mount
*m
= MOUNT(u
);
1406 if (m
->from_proc_self_mountinfo
)
1412 static void mount_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1413 Mount
*m
= MOUNT(u
);
1419 if (pid
!= m
->control_pid
)
1422 /* So here's the thing, we really want to know before /usr/bin/mount or /usr/bin/umount exit whether
1423 * they established/remove a mount. This is important when mounting, but even more so when unmounting
1424 * since we need to deal with nested mounts and otherwise cannot safely determine whether to repeat
1425 * the unmounts. In theory, the kernel fires /proc/self/mountinfo changes off before returning from
1426 * the mount() or umount() syscalls, and thus we should see the changes to the proc file before we
1427 * process the waitid() for the /usr/bin/(u)mount processes. However, this is unfortunately racy: we
1428 * have to waitid() for processes using P_ALL (since we need to reap unexpected children that got
1429 * reparented to PID 1), but when using P_ALL we might end up reaping processes that terminated just
1430 * instants ago, i.e. already after our last event loop iteration (i.e. after the last point we might
1431 * have noticed /proc/self/mountinfo events via epoll). This means event loop priorities for
1432 * processing SIGCHLD vs. /proc/self/mountinfo IO events are not as relevant as we want. To fix that
1433 * race, let's explicitly scan /proc/self/mountinfo before we start processing /usr/bin/(u)mount
1434 * dying. It's ugly, but it makes our ordering systematic again, and makes sure we always see
1435 * /proc/self/mountinfo changes before our mount/umount exits. */
1436 (void) mount_process_proc_self_mountinfo(u
->manager
);
1440 if (is_clean_exit(code
, status
, EXIT_CLEAN_COMMAND
, NULL
))
1442 else if (code
== CLD_EXITED
)
1443 f
= MOUNT_FAILURE_EXIT_CODE
;
1444 else if (code
== CLD_KILLED
)
1445 f
= MOUNT_FAILURE_SIGNAL
;
1446 else if (code
== CLD_DUMPED
)
1447 f
= MOUNT_FAILURE_CORE_DUMP
;
1449 assert_not_reached();
1451 if (IN_SET(m
->state
, MOUNT_REMOUNTING
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_REMOUNTING_SIGTERM
))
1452 mount_set_reload_result(m
, f
);
1453 else if (m
->result
== MOUNT_SUCCESS
)
1456 if (m
->control_command
) {
1457 exec_status_exit(&m
->control_command
->exec_status
, &m
->exec_context
, pid
, code
, status
);
1459 m
->control_command
= NULL
;
1460 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
1463 unit_log_process_exit(
1466 mount_exec_command_to_string(m
->control_command_id
),
1470 /* Note that due to the io event priority logic, we can be sure the new mountinfo is loaded
1471 * before we process the SIGCHLD for the mount command. */
1475 case MOUNT_MOUNTING
:
1476 /* Our mount point has not appeared in mountinfo. Something went wrong. */
1478 if (f
== MOUNT_SUCCESS
) {
1479 /* Either /bin/mount has an unexpected definition of success,
1480 * or someone raced us and we lost. */
1481 log_unit_warning(UNIT(m
), "Mount process finished, but there is no mount.");
1482 f
= MOUNT_FAILURE_PROTOCOL
;
1484 mount_enter_dead(m
, f
);
1487 case MOUNT_MOUNTING_DONE
:
1488 mount_enter_mounted(m
, f
);
1491 case MOUNT_REMOUNTING
:
1492 case MOUNT_REMOUNTING_SIGTERM
:
1493 case MOUNT_REMOUNTING_SIGKILL
:
1494 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1497 case MOUNT_UNMOUNTING
:
1499 if (f
== MOUNT_SUCCESS
&& m
->from_proc_self_mountinfo
) {
1501 /* Still a mount point? If so, let's try again. Most likely there were multiple mount points
1502 * stacked on top of each other. We might exceed the timeout specified by the user overall,
1503 * but we will stop as soon as any one umount times out. */
1505 if (m
->n_retry_umount
< RETRY_UMOUNT_MAX
) {
1506 log_unit_debug(u
, "Mount still present, trying again.");
1507 m
->n_retry_umount
++;
1508 mount_enter_unmounting(m
);
1510 log_unit_warning(u
, "Mount still present after %u attempts to unmount, giving up.", m
->n_retry_umount
);
1511 mount_enter_mounted(m
, f
);
1514 mount_enter_dead_or_mounted(m
, f
);
1518 case MOUNT_UNMOUNTING_SIGKILL
:
1519 case MOUNT_UNMOUNTING_SIGTERM
:
1520 mount_enter_dead_or_mounted(m
, f
);
1523 case MOUNT_CLEANING
:
1524 if (m
->clean_result
== MOUNT_SUCCESS
)
1525 m
->clean_result
= f
;
1527 mount_enter_dead(m
, MOUNT_SUCCESS
);
1531 assert_not_reached();
1534 /* Notify clients about changed exit status */
1535 unit_add_to_dbus_queue(u
);
1538 static int mount_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
1539 Mount
*m
= MOUNT(userdata
);
1542 assert(m
->timer_event_source
== source
);
1546 case MOUNT_MOUNTING
:
1547 case MOUNT_MOUNTING_DONE
:
1548 log_unit_warning(UNIT(m
), "Mounting timed out. Terminating.");
1549 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1552 case MOUNT_REMOUNTING
:
1553 log_unit_warning(UNIT(m
), "Remounting timed out. Terminating remount process.");
1554 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1555 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGTERM
, MOUNT_SUCCESS
);
1558 case MOUNT_REMOUNTING_SIGTERM
:
1559 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1561 if (m
->kill_context
.send_sigkill
) {
1562 log_unit_warning(UNIT(m
), "Remounting timed out. Killing.");
1563 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
1565 log_unit_warning(UNIT(m
), "Remounting timed out. Skipping SIGKILL. Ignoring.");
1566 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1570 case MOUNT_REMOUNTING_SIGKILL
:
1571 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1573 log_unit_warning(UNIT(m
), "Mount process still around after SIGKILL. Ignoring.");
1574 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1577 case MOUNT_UNMOUNTING
:
1578 log_unit_warning(UNIT(m
), "Unmounting timed out. Terminating.");
1579 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1582 case MOUNT_UNMOUNTING_SIGTERM
:
1583 if (m
->kill_context
.send_sigkill
) {
1584 log_unit_warning(UNIT(m
), "Mount process timed out. Killing.");
1585 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_FAILURE_TIMEOUT
);
1587 log_unit_warning(UNIT(m
), "Mount process timed out. Skipping SIGKILL. Ignoring.");
1588 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1592 case MOUNT_UNMOUNTING_SIGKILL
:
1593 log_unit_warning(UNIT(m
), "Mount process still around after SIGKILL. Ignoring.");
1594 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1597 case MOUNT_CLEANING
:
1598 log_unit_warning(UNIT(m
), "Cleaning timed out. killing.");
1600 if (m
->clean_result
== MOUNT_SUCCESS
)
1601 m
->clean_result
= MOUNT_FAILURE_TIMEOUT
;
1603 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, 0);
1607 assert_not_reached();
1613 static int mount_setup_new_unit(
1618 const char *options
,
1620 MountProcFlags
*ret_flags
,
1623 _cleanup_(unit_freep
) Unit
*u
= NULL
;
1631 r
= unit_new_for_name(m
, sizeof(Mount
), name
, &u
);
1635 r
= free_and_strdup(&u
->source_path
, "/proc/self/mountinfo");
1639 r
= free_and_strdup(&MOUNT(u
)->where
, where
);
1643 r
= update_parameters_proc_self_mountinfo(MOUNT(u
), what
, options
, fstype
);
1647 /* This unit was generated because /proc/self/mountinfo reported it. Remember this, so that by the
1648 * time we load the unit file for it (and thus add in extra deps right after) we know what source to
1649 * attributes the deps to. */
1650 MOUNT(u
)->from_proc_self_mountinfo
= true;
1652 r
= mount_add_non_exec_dependencies(MOUNT(u
));
1656 /* We have only allocated the stub now, let's enqueue this unit for loading now, so that everything
1657 * else is loaded in now. */
1658 unit_add_to_load_queue(u
);
1660 *ret_flags
= MOUNT_PROC_IS_MOUNTED
| MOUNT_PROC_JUST_MOUNTED
| MOUNT_PROC_JUST_CHANGED
;
1665 static int mount_setup_existing_unit(
1669 const char *options
,
1671 MountProcFlags
*ret_flags
) {
1678 if (!MOUNT(u
)->where
) {
1679 MOUNT(u
)->where
= strdup(where
);
1680 if (!MOUNT(u
)->where
)
1684 /* In case we have multiple mounts established on the same mount point, let's merge flags set already
1685 * for the current unit. Note that the flags field is reset on each iteration of reading
1686 * /proc/self/mountinfo, hence we know for sure anything already set here is from the current
1687 * iteration and thus worthy of taking into account. */
1688 MountProcFlags flags
=
1689 MOUNT(u
)->proc_flags
| MOUNT_PROC_IS_MOUNTED
;
1691 r
= update_parameters_proc_self_mountinfo(MOUNT(u
), what
, options
, fstype
);
1695 flags
|= MOUNT_PROC_JUST_CHANGED
;
1697 /* There are two conditions when we consider a mount point just mounted: when we haven't seen it in
1698 * /proc/self/mountinfo before or when MOUNT_MOUNTING is our current state. Why bother with the
1699 * latter? Shouldn't that be covered by the former? No, during reload it is not because we might then
1700 * encounter a new /proc/self/mountinfo in combination with an old mount unit state (since it stems
1701 * from the serialized state), and need to catch up. Since we know that the MOUNT_MOUNTING state is
1702 * reached when we wait for the mount to appear we hence can assume that if we are in it, we are
1703 * actually seeing it established for the first time. */
1704 if (!MOUNT(u
)->from_proc_self_mountinfo
|| MOUNT(u
)->state
== MOUNT_MOUNTING
)
1705 flags
|= MOUNT_PROC_JUST_MOUNTED
;
1707 MOUNT(u
)->from_proc_self_mountinfo
= true;
1709 if (IN_SET(u
->load_state
, UNIT_NOT_FOUND
, UNIT_BAD_SETTING
, UNIT_ERROR
)) {
1710 /* The unit was previously not found or otherwise not loaded. Now that the unit shows up in
1711 * /proc/self/mountinfo we should reconsider it this, hence set it to UNIT_LOADED. */
1712 u
->load_state
= UNIT_LOADED
;
1715 flags
|= MOUNT_PROC_JUST_CHANGED
;
1718 if (FLAGS_SET(flags
, MOUNT_PROC_JUST_CHANGED
)) {
1719 /* If things changed, then make sure that all deps are regenerated. Let's
1720 * first remove all automatic deps, and then add in the new ones. */
1721 r
= mount_add_non_exec_dependencies(MOUNT(u
));
1730 static int mount_setup_unit(
1734 const char *options
,
1738 _cleanup_free_
char *e
= NULL
;
1739 MountProcFlags flags
;
1749 /* Ignore API mount points. They should never be referenced in
1750 * dependencies ever. */
1751 if (mount_point_is_api(where
) || mount_point_ignore(where
))
1754 if (streq(fstype
, "autofs"))
1757 /* probably some kind of swap, ignore */
1758 if (!is_path(where
))
1761 r
= unit_name_from_path(where
, ".mount", &e
);
1763 return log_struct_errno(
1765 "MESSAGE_ID=" SD_MESSAGE_MOUNT_POINT_PATH_NOT_SUITABLE_STR
,
1766 "MOUNT_POINT=%s", where
,
1767 LOG_MESSAGE("Failed to generate valid unit name from mount point path '%s', ignoring mount point: %m",
1770 u
= manager_get_unit(m
, e
);
1772 r
= mount_setup_existing_unit(u
, what
, where
, options
, fstype
, &flags
);
1774 /* First time we see this mount point meaning that it's not been initiated by a mount unit
1775 * but rather by the sysadmin having called mount(8) directly. */
1776 r
= mount_setup_new_unit(m
, e
, what
, where
, options
, fstype
, &flags
, &u
);
1778 return log_warning_errno(r
, "Failed to set up mount unit for '%s': %m", where
);
1780 /* If the mount changed properties or state, let's notify our clients */
1781 if (flags
& (MOUNT_PROC_JUST_CHANGED
|MOUNT_PROC_JUST_MOUNTED
))
1782 unit_add_to_dbus_queue(u
);
1785 MOUNT(u
)->proc_flags
= flags
;
1790 static int mount_load_proc_self_mountinfo(Manager
*m
, bool set_flags
) {
1791 _cleanup_(mnt_free_tablep
) struct libmnt_table
*table
= NULL
;
1792 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*iter
= NULL
;
1797 r
= libmount_parse(NULL
, NULL
, &table
, &iter
);
1799 return log_error_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
1802 struct libmnt_fs
*fs
;
1803 const char *device
, *path
, *options
, *fstype
;
1805 r
= mnt_table_next_fs(table
, iter
, &fs
);
1809 return log_error_errno(r
, "Failed to get next entry from /proc/self/mountinfo: %m");
1811 device
= mnt_fs_get_source(fs
);
1812 path
= mnt_fs_get_target(fs
);
1813 options
= mnt_fs_get_options(fs
);
1814 fstype
= mnt_fs_get_fstype(fs
);
1816 if (!device
|| !path
)
1819 device_found_node(m
, device
, DEVICE_FOUND_MOUNT
, DEVICE_FOUND_MOUNT
);
1821 (void) mount_setup_unit(m
, device
, path
, options
, fstype
, set_flags
);
1827 static void mount_shutdown(Manager
*m
) {
1830 m
->mount_event_source
= sd_event_source_disable_unref(m
->mount_event_source
);
1832 mnt_unref_monitor(m
->mount_monitor
);
1833 m
->mount_monitor
= NULL
;
1836 static int mount_get_timeout(Unit
*u
, usec_t
*timeout
) {
1837 Mount
*m
= MOUNT(u
);
1844 if (!m
->timer_event_source
)
1847 r
= sd_event_source_get_time(m
->timer_event_source
, &t
);
1850 if (t
== USEC_INFINITY
)
1857 static void mount_enumerate_perpetual(Manager
*m
) {
1863 /* Whatever happens, we know for sure that the root directory is around, and cannot go away. Let's
1864 * unconditionally synthesize it here and mark it as perpetual. */
1866 u
= manager_get_unit(m
, SPECIAL_ROOT_MOUNT
);
1868 r
= unit_new_for_name(m
, sizeof(Mount
), SPECIAL_ROOT_MOUNT
, &u
);
1870 log_error_errno(r
, "Failed to allocate the special " SPECIAL_ROOT_MOUNT
" unit: %m");
1875 u
->perpetual
= true;
1876 MOUNT(u
)->deserialized_state
= MOUNT_MOUNTED
;
1878 unit_add_to_load_queue(u
);
1879 unit_add_to_dbus_queue(u
);
1882 static bool mount_is_mounted(Mount
*m
) {
1885 return UNIT(m
)->perpetual
|| FLAGS_SET(m
->proc_flags
, MOUNT_PROC_IS_MOUNTED
);
1888 static int mount_on_ratelimit_expire(sd_event_source
*s
, void *userdata
) {
1889 Manager
*m
= ASSERT_PTR(userdata
);
1892 /* Let's enqueue all start jobs that were previously skipped because of active ratelimit. */
1893 HASHMAP_FOREACH(j
, m
->jobs
) {
1894 if (j
->unit
->type
!= UNIT_MOUNT
)
1897 job_add_to_run_queue(j
);
1900 /* By entering ratelimited state we made all mount start jobs not runnable, now rate limit is over so
1901 * let's make sure we dispatch them in the next iteration. */
1902 manager_trigger_run_queue(m
);
1907 static void mount_enumerate(Manager
*m
) {
1914 if (!m
->mount_monitor
) {
1917 m
->mount_monitor
= mnt_new_monitor();
1918 if (!m
->mount_monitor
) {
1923 r
= mnt_monitor_enable_kernel(m
->mount_monitor
, 1);
1925 log_error_errno(r
, "Failed to enable watching of kernel mount events: %m");
1929 r
= mnt_monitor_enable_userspace(m
->mount_monitor
, 1, NULL
);
1931 log_error_errno(r
, "Failed to enable watching of userspace mount events: %m");
1935 /* mnt_unref_monitor() will close the fd */
1936 fd
= r
= mnt_monitor_get_fd(m
->mount_monitor
);
1938 log_error_errno(r
, "Failed to acquire watch file descriptor: %m");
1942 r
= sd_event_add_io(m
->event
, &m
->mount_event_source
, fd
, EPOLLIN
, mount_dispatch_io
, m
);
1944 log_error_errno(r
, "Failed to watch mount file descriptor: %m");
1948 r
= sd_event_source_set_priority(m
->mount_event_source
, SD_EVENT_PRIORITY_NORMAL
-10);
1950 log_error_errno(r
, "Failed to adjust mount watch priority: %m");
1954 r
= sd_event_source_set_ratelimit(m
->mount_event_source
, 1 * USEC_PER_SEC
, 5);
1956 log_error_errno(r
, "Failed to enable rate limit for mount events: %m");
1960 r
= sd_event_source_set_ratelimit_expire_callback(m
->mount_event_source
, mount_on_ratelimit_expire
);
1962 log_error_errno(r
, "Failed to enable rate limit for mount events: %m");
1966 (void) sd_event_source_set_description(m
->mount_event_source
, "mount-monitor-dispatch");
1969 r
= mount_load_proc_self_mountinfo(m
, false);
1979 static int drain_libmount(Manager
*m
) {
1980 bool rescan
= false;
1985 /* Drain all events and verify that the event is valid.
1987 * Note that libmount also monitors /run/mount mkdir if the directory does not exist yet. The mkdir
1988 * may generate event which is irrelevant for us.
1990 * error: r < 0; valid: r == 0, false positive: r == 1 */
1992 r
= mnt_monitor_next_change(m
->mount_monitor
, NULL
, NULL
);
1994 return log_error_errno(r
, "Failed to drain libmount events: %m");
2002 static int mount_process_proc_self_mountinfo(Manager
*m
) {
2003 _cleanup_set_free_ Set
*around
= NULL
, *gone
= NULL
;
2009 r
= drain_libmount(m
);
2013 r
= mount_load_proc_self_mountinfo(m
, true);
2015 /* Reset flags, just in case, for later calls */
2016 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
])
2017 MOUNT(u
)->proc_flags
= 0;
2022 manager_dispatch_load_queue(m
);
2024 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
]) {
2025 Mount
*mount
= MOUNT(u
);
2027 if (!mount_is_mounted(mount
)) {
2029 /* A mount point is not around right now. It
2030 * might be gone, or might never have
2033 if (mount
->from_proc_self_mountinfo
&&
2034 mount
->parameters_proc_self_mountinfo
.what
)
2035 /* Remember that this device might just have disappeared */
2036 if (set_put_strdup_full(&gone
, &path_hash_ops_free
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
2037 log_oom(); /* we don't care too much about OOM here... */
2039 mount
->from_proc_self_mountinfo
= false;
2040 assert_se(update_parameters_proc_self_mountinfo(mount
, NULL
, NULL
, NULL
) >= 0);
2042 switch (mount
->state
) {
2045 /* This has just been unmounted by somebody else, follow the state change. */
2046 mount_enter_dead(mount
, MOUNT_SUCCESS
);
2049 case MOUNT_MOUNTING_DONE
:
2050 /* The mount command may add the corresponding proc mountinfo entry and
2051 * then remove it because of an internal error. E.g., fuse.sshfs seems
2052 * to do that when the connection fails. See #17617. To handle such the
2053 * case, let's once set the state back to mounting. Then, the unit can
2054 * correctly enter the failed state later in mount_sigchld(). */
2055 mount_set_state(mount
, MOUNT_MOUNTING
);
2062 } else if (mount
->proc_flags
& (MOUNT_PROC_JUST_MOUNTED
|MOUNT_PROC_JUST_CHANGED
)) {
2064 /* A mount point was added or changed */
2066 switch (mount
->state
) {
2071 /* This has just been mounted by somebody else, follow the state change, but let's
2072 * generate a new invocation ID for this implicitly and automatically. */
2073 (void) unit_acquire_invocation_id(u
);
2074 mount_cycle_clear(mount
);
2075 mount_enter_mounted(mount
, MOUNT_SUCCESS
);
2078 case MOUNT_MOUNTING
:
2079 mount_set_state(mount
, MOUNT_MOUNTING_DONE
);
2083 /* Nothing really changed, but let's
2084 * issue an notification call
2085 * nonetheless, in case somebody is
2086 * waiting for this. (e.g. file system
2087 * ro/rw remounts.) */
2088 mount_set_state(mount
, mount
->state
);
2093 if (mount_is_mounted(mount
) &&
2094 mount
->from_proc_self_mountinfo
&&
2095 mount
->parameters_proc_self_mountinfo
.what
)
2096 /* Track devices currently used */
2097 if (set_put_strdup_full(&around
, &path_hash_ops_free
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
2100 /* Reset the flags for later calls */
2101 mount
->proc_flags
= 0;
2104 SET_FOREACH(what
, gone
) {
2105 if (set_contains(around
, what
))
2108 /* Let the device units know that the device is no longer mounted */
2109 device_found_node(m
, what
, DEVICE_NOT_FOUND
, DEVICE_FOUND_MOUNT
);
2115 static int mount_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
2116 Manager
*m
= ASSERT_PTR(userdata
);
2118 assert(revents
& EPOLLIN
);
2120 return mount_process_proc_self_mountinfo(m
);
2123 static void mount_reset_failed(Unit
*u
) {
2124 Mount
*m
= MOUNT(u
);
2128 if (m
->state
== MOUNT_FAILED
)
2129 mount_set_state(m
, MOUNT_DEAD
);
2131 m
->result
= MOUNT_SUCCESS
;
2132 m
->reload_result
= MOUNT_SUCCESS
;
2133 m
->clean_result
= MOUNT_SUCCESS
;
2136 static int mount_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
2137 Mount
*m
= MOUNT(u
);
2141 return unit_kill_common(u
, who
, signo
, -1, m
->control_pid
, error
);
2144 static int mount_control_pid(Unit
*u
) {
2145 Mount
*m
= MOUNT(u
);
2149 return m
->control_pid
;
2152 static int mount_clean(Unit
*u
, ExecCleanMask mask
) {
2153 _cleanup_strv_free_
char **l
= NULL
;
2154 Mount
*m
= MOUNT(u
);
2160 if (m
->state
!= MOUNT_DEAD
)
2163 r
= exec_context_get_clean_directories(&m
->exec_context
, u
->manager
->prefix
, mask
, &l
);
2167 if (strv_isempty(l
))
2170 mount_unwatch_control_pid(m
);
2171 m
->clean_result
= MOUNT_SUCCESS
;
2172 m
->control_command
= NULL
;
2173 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
2175 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->exec_context
.timeout_clean_usec
));
2179 r
= unit_fork_and_watch_rm_rf(u
, l
, &m
->control_pid
);
2183 mount_set_state(m
, MOUNT_CLEANING
);
2188 log_unit_warning_errno(u
, r
, "Failed to initiate cleaning: %m");
2189 m
->clean_result
= MOUNT_FAILURE_RESOURCES
;
2190 m
->timer_event_source
= sd_event_source_disable_unref(m
->timer_event_source
);
2194 static int mount_can_clean(Unit
*u
, ExecCleanMask
*ret
) {
2195 Mount
*m
= MOUNT(u
);
2199 return exec_context_get_clean_mask(&m
->exec_context
, ret
);
2202 static int mount_can_start(Unit
*u
) {
2203 Mount
*m
= MOUNT(u
);
2208 r
= unit_test_start_limit(u
);
2210 mount_enter_dead(m
, MOUNT_FAILURE_START_LIMIT_HIT
);
2217 static const char* const mount_exec_command_table
[_MOUNT_EXEC_COMMAND_MAX
] = {
2218 [MOUNT_EXEC_MOUNT
] = "ExecMount",
2219 [MOUNT_EXEC_UNMOUNT
] = "ExecUnmount",
2220 [MOUNT_EXEC_REMOUNT
] = "ExecRemount",
2223 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command
, MountExecCommand
);
2225 static const char* const mount_result_table
[_MOUNT_RESULT_MAX
] = {
2226 [MOUNT_SUCCESS
] = "success",
2227 [MOUNT_FAILURE_RESOURCES
] = "resources",
2228 [MOUNT_FAILURE_TIMEOUT
] = "timeout",
2229 [MOUNT_FAILURE_EXIT_CODE
] = "exit-code",
2230 [MOUNT_FAILURE_SIGNAL
] = "signal",
2231 [MOUNT_FAILURE_CORE_DUMP
] = "core-dump",
2232 [MOUNT_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
2233 [MOUNT_FAILURE_PROTOCOL
] = "protocol",
2236 DEFINE_STRING_TABLE_LOOKUP(mount_result
, MountResult
);
2238 const UnitVTable mount_vtable
= {
2239 .object_size
= sizeof(Mount
),
2240 .exec_context_offset
= offsetof(Mount
, exec_context
),
2241 .cgroup_context_offset
= offsetof(Mount
, cgroup_context
),
2242 .kill_context_offset
= offsetof(Mount
, kill_context
),
2243 .exec_runtime_offset
= offsetof(Mount
, exec_runtime
),
2244 .dynamic_creds_offset
= offsetof(Mount
, dynamic_creds
),
2250 .private_section
= "Mount",
2252 .can_transient
= true,
2254 .exclude_from_switch_root_serialization
= true,
2260 .coldplug
= mount_coldplug
,
2261 .catchup
= mount_catchup
,
2265 .start
= mount_start
,
2267 .reload
= mount_reload
,
2270 .clean
= mount_clean
,
2271 .can_clean
= mount_can_clean
,
2273 .serialize
= mount_serialize
,
2274 .deserialize_item
= mount_deserialize_item
,
2276 .active_state
= mount_active_state
,
2277 .sub_state_to_string
= mount_sub_state_to_string
,
2279 .will_restart
= unit_will_restart_default
,
2281 .may_gc
= mount_may_gc
,
2282 .is_extrinsic
= mount_is_extrinsic
,
2284 .sigchld_event
= mount_sigchld_event
,
2286 .reset_failed
= mount_reset_failed
,
2288 .control_pid
= mount_control_pid
,
2290 .bus_set_property
= bus_mount_set_property
,
2291 .bus_commit_properties
= bus_mount_commit_properties
,
2293 .get_timeout
= mount_get_timeout
,
2295 .enumerate_perpetual
= mount_enumerate_perpetual
,
2296 .enumerate
= mount_enumerate
,
2297 .shutdown
= mount_shutdown
,
2299 .status_message_formats
= {
2300 .starting_stopping
= {
2301 [0] = "Mounting %s...",
2302 [1] = "Unmounting %s...",
2304 .finished_start_job
= {
2305 [JOB_DONE
] = "Mounted %s.",
2306 [JOB_FAILED
] = "Failed to mount %s.",
2307 [JOB_TIMEOUT
] = "Timed out mounting %s.",
2309 .finished_stop_job
= {
2310 [JOB_DONE
] = "Unmounted %s.",
2311 [JOB_FAILED
] = "Failed unmounting %s.",
2312 [JOB_TIMEOUT
] = "Timed out unmounting %s.",
2316 .can_start
= mount_can_start
,