1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include <sys/socket.h>
8 #include "sd-messages.h"
10 #include "alloc-util.h"
12 #include "dbus-mount.h"
13 #include "dbus-unit.h"
15 #include "errno-util.h"
16 #include "exec-credential.h"
17 #include "exit-status.h"
19 #include "format-util.h"
20 #include "fstab-util.h"
21 #include "glyph-util.h"
22 #include "initrd-util.h"
23 #include "libmount-util.h"
26 #include "mkdir-label.h"
27 #include "mount-util.h"
29 #include "mount-setup.h"
30 #include "mountpoint-util.h"
31 #include "parse-util.h"
32 #include "path-util.h"
33 #include "process-util.h"
34 #include "serialize.h"
37 #include "stat-util.h"
38 #include "string-table.h"
39 #include "string-util.h"
42 #include "unit-name.h"
45 #define RETRY_UMOUNT_MAX 32
47 static const UnitActiveState state_translation_table
[_MOUNT_STATE_MAX
] = {
48 [MOUNT_DEAD
] = UNIT_INACTIVE
,
49 [MOUNT_MOUNTING
] = UNIT_ACTIVATING
,
50 [MOUNT_MOUNTING_DONE
] = UNIT_ACTIVATING
,
51 [MOUNT_MOUNTED
] = UNIT_ACTIVE
,
52 [MOUNT_REMOUNTING
] = UNIT_RELOADING
,
53 [MOUNT_REMOUNTING_SIGTERM
] = UNIT_RELOADING
,
54 [MOUNT_REMOUNTING_SIGKILL
] = UNIT_RELOADING
,
55 [MOUNT_UNMOUNTING
] = UNIT_DEACTIVATING
,
56 [MOUNT_UNMOUNTING_SIGTERM
] = UNIT_DEACTIVATING
,
57 [MOUNT_UNMOUNTING_SIGKILL
] = UNIT_DEACTIVATING
,
58 [MOUNT_FAILED
] = UNIT_FAILED
,
59 [MOUNT_CLEANING
] = UNIT_MAINTENANCE
,
62 static int mount_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
63 static int mount_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
);
64 static void mount_enter_dead(Mount
*m
, MountResult f
, bool flush_result
);
65 static void mount_enter_mounted(Mount
*m
, MountResult f
);
66 static void mount_cycle_clear(Mount
*m
);
67 static int mount_process_proc_self_mountinfo(Manager
*m
);
69 static bool MOUNT_STATE_WITH_PROCESS(MountState state
) {
74 MOUNT_REMOUNTING_SIGTERM
,
75 MOUNT_REMOUNTING_SIGKILL
,
77 MOUNT_UNMOUNTING_SIGTERM
,
78 MOUNT_UNMOUNTING_SIGKILL
,
82 static MountParameters
* get_mount_parameters_fragment(Mount
*m
) {
86 return &m
->parameters_fragment
;
91 static MountParameters
* get_mount_parameters(Mount
*m
) {
94 if (m
->from_proc_self_mountinfo
)
95 return &m
->parameters_proc_self_mountinfo
;
97 return get_mount_parameters_fragment(m
);
100 static bool mount_is_network(const MountParameters
*p
) {
103 if (fstab_test_option(p
->options
, "_netdev\0"))
106 if (p
->fstype
&& fstype_is_network(p
->fstype
))
112 static bool mount_is_nofail(const Mount
*m
) {
115 if (!m
->from_fragment
)
118 return fstab_test_yes_no_option(m
->parameters_fragment
.options
, "nofail\0" "fail\0");
121 static bool mount_is_loop(const MountParameters
*p
) {
124 if (fstab_test_option(p
->options
, "loop\0"))
130 static bool mount_is_bind(const MountParameters
*p
) {
132 return fstab_is_bind(p
->options
, p
->fstype
);
135 static int mount_is_bound_to_device(Mount
*m
) {
136 _cleanup_free_
char *value
= NULL
;
137 const MountParameters
*p
;
142 /* Determines whether to place a Requires= or BindsTo= dependency on the backing device unit. We do
143 * this by checking for the x-systemd.device-bound= mount option. If it is enabled we use BindsTo=,
144 * otherwise Requires=. But note that we might combine the latter with StopPropagatedFrom=, see
147 p
= get_mount_parameters(m
);
151 r
= fstab_filter_options(p
->options
, "x-systemd.device-bound\0", NULL
, &value
, NULL
, NULL
);
155 return -EIDRM
; /* If unspecified at all, return recognizable error */
160 return parse_boolean(value
);
163 static bool mount_propagate_stop(Mount
*m
) {
168 r
= mount_is_bound_to_device(m
);
170 /* If x-systemd.device-bound=no is explicitly requested by user, don't try to set StopPropagatedFrom=.
171 * Also don't bother if true, since with BindsTo= the stop propagation is implicit. */
174 log_debug_errno(r
, "Failed to get x-systemd.device-bound= option, ignoring: %m");
176 return m
->from_fragment
; /* let's propagate stop whenever this is an explicitly configured unit,
177 * otherwise let's not bother. */
180 static void mount_init(Unit
*u
) {
181 Mount
*m
= ASSERT_PTR(MOUNT(u
));
183 assert(u
->load_state
== UNIT_STUB
);
185 m
->timeout_usec
= u
->manager
->defaults
.timeout_start_usec
;
187 m
->exec_context
.std_output
= u
->manager
->defaults
.std_output
;
188 m
->exec_context
.std_error
= u
->manager
->defaults
.std_error
;
190 m
->directory_mode
= 0755;
192 /* We need to make sure that /usr/bin/mount is always called
193 * in the same process group as us, so that the autofs kernel
194 * side doesn't send us another mount request while we are
195 * already trying to comply its last one. */
196 m
->exec_context
.same_pgrp
= true;
198 m
->control_pid
= PIDREF_NULL
;
199 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
201 u
->ignore_on_isolate
= true;
204 static int mount_arm_timer(Mount
*m
, bool relative
, usec_t usec
) {
207 return unit_arm_timer(UNIT(m
), &m
->timer_event_source
, relative
, usec
, mount_dispatch_timer
);
210 static void mount_unwatch_control_pid(Mount
*m
) {
212 unit_unwatch_pidref_done(UNIT(m
), &m
->control_pid
);
215 static void mount_parameters_done(MountParameters
*p
) {
218 p
->what
= mfree(p
->what
);
219 p
->options
= mfree(p
->options
);
220 p
->fstype
= mfree(p
->fstype
);
223 static void mount_done(Unit
*u
) {
224 Mount
*m
= ASSERT_PTR(MOUNT(u
));
226 m
->where
= mfree(m
->where
);
228 mount_parameters_done(&m
->parameters_proc_self_mountinfo
);
229 mount_parameters_done(&m
->parameters_fragment
);
231 m
->exec_runtime
= exec_runtime_free(m
->exec_runtime
);
233 exec_command_done_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
234 m
->control_command
= NULL
;
236 mount_unwatch_control_pid(m
);
238 m
->timer_event_source
= sd_event_source_disable_unref(m
->timer_event_source
);
241 static int update_parameters_proc_self_mountinfo(
245 const char *fstype
) {
252 p
= &m
->parameters_proc_self_mountinfo
;
254 r
= free_and_strdup(&p
->what
, what
);
258 q
= free_and_strdup(&p
->options
, options
);
262 w
= free_and_strdup(&p
->fstype
, fstype
);
266 return r
> 0 || q
> 0 || w
> 0;
269 static int mount_add_mount_dependencies(Mount
*m
) {
275 if (!path_equal(m
->where
, "/")) {
276 _cleanup_free_
char *parent
= NULL
;
278 /* Adds in links to other mount points that might lie further up in the hierarchy */
280 r
= path_extract_directory(m
->where
, &parent
);
284 r
= unit_add_mounts_for(UNIT(m
), parent
, UNIT_DEPENDENCY_IMPLICIT
, UNIT_MOUNT_REQUIRES
);
289 /* Adds in dependencies to other mount points that might be needed for the source path (if this is a bind mount
290 * or a loop mount) to be available. */
291 pm
= get_mount_parameters_fragment(m
);
292 if (pm
&& pm
->what
&&
293 path_is_absolute(pm
->what
) &&
294 (mount_is_bind(pm
) || mount_is_loop(pm
) || !mount_is_network(pm
))) {
296 r
= unit_add_mounts_for(UNIT(m
), pm
->what
, UNIT_DEPENDENCY_FILE
, UNIT_MOUNT_REQUIRES
);
301 /* Adds in dependencies to other units that use this path or paths further down in the hierarchy */
302 for (UnitMountDependencyType t
= 0; t
< _UNIT_MOUNT_DEPENDENCY_TYPE_MAX
; ++t
) {
304 Set
*s
= manager_get_units_needing_mounts_for(UNIT(m
)->manager
, m
->where
, t
);
306 SET_FOREACH(other
, s
) {
307 if (other
->load_state
!= UNIT_LOADED
)
310 if (other
== UNIT(m
))
313 r
= unit_add_dependency(
317 /* add_reference= */ true,
318 UNIT_DEPENDENCY_PATH
);
322 if (UNIT(m
)->fragment_path
) {
323 /* If we have fragment configuration, then make this dependency required/wanted */
324 r
= unit_add_dependency(
326 unit_mount_dependency_type_to_dependency_type(t
),
328 /* add_reference= */ true,
329 UNIT_DEPENDENCY_PATH
);
339 static int mount_add_device_dependencies(Mount
*m
) {
340 UnitDependencyMask mask
;
347 log_unit_trace(UNIT(m
), "Processing implicit device dependencies");
349 p
= get_mount_parameters(m
);
351 log_unit_trace(UNIT(m
), "Missing mount parameters, skipping implicit device dependencies");
356 log_unit_trace(UNIT(m
), "Missing mount source, skipping implicit device dependencies");
360 if (mount_is_bind(p
)) {
361 log_unit_trace(UNIT(m
), "Mount unit is a bind mount, skipping implicit device dependencies");
365 if (!is_device_path(p
->what
)) {
366 log_unit_trace(UNIT(m
), "Mount source is not a device path, skipping implicit device dependencies");
370 /* /dev/root is a really weird thing, it's not a real device, but just a path the kernel exports for
371 * the root file system specified on the kernel command line. Ignore it here. */
372 if (PATH_IN_SET(p
->what
, "/dev/root", "/dev/nfs")) {
373 log_unit_trace(UNIT(m
), "Mount source is in /dev/root or /dev/nfs, skipping implicit device dependencies");
377 if (path_equal(m
->where
, "/")) {
378 log_unit_trace(UNIT(m
), "Mount destination is '/', skipping implicit device dependencies");
382 /* Mount units from /proc/self/mountinfo are not bound to devices by default since they're subject to
383 * races when mounts are established by other tools with different backing devices than what we
384 * maintain. The user can still force this to be a BindsTo= dependency with an appropriate option (or
385 * udev property) so the mount units are automatically stopped when the device disappears
387 dep
= mount_is_bound_to_device(m
) > 0 ? UNIT_BINDS_TO
: UNIT_REQUIRES
;
389 /* We always use 'what' from /proc/self/mountinfo if mounted */
390 mask
= m
->from_proc_self_mountinfo
? UNIT_DEPENDENCY_MOUNTINFO
: UNIT_DEPENDENCY_MOUNT_FILE
;
392 r
= unit_add_node_dependency(UNIT(m
), p
->what
, dep
, mask
);
396 log_unit_trace(UNIT(m
), "Added %s dependency on %s", unit_dependency_to_string(dep
), p
->what
);
398 if (mount_propagate_stop(m
)) {
399 r
= unit_add_node_dependency(UNIT(m
), p
->what
, UNIT_STOP_PROPAGATED_FROM
, mask
);
403 log_unit_trace(UNIT(m
), "Added %s dependency on %s",
404 unit_dependency_to_string(UNIT_STOP_PROPAGATED_FROM
), p
->what
);
407 r
= unit_add_blockdev_dependency(UNIT(m
), p
->what
, mask
);
409 log_unit_trace(UNIT(m
), "Added %s dependency on %s", unit_dependency_to_string(UNIT_AFTER
), p
->what
);
414 static bool mount_is_extrinsic(Unit
*u
) {
415 Mount
*m
= ASSERT_PTR(MOUNT(u
));
418 /* Returns true for all units that are "magic" and should be excluded from the usual
419 * start-up and shutdown dependencies. We call them "extrinsic" here, as they are generally
420 * mounted outside of the systemd dependency logic. We shouldn't attempt to manage them
421 * ourselves but it's fine if the user operates on them with us. */
423 /* We only automatically manage mounts if we are in system mode */
424 if (MANAGER_IS_USER(u
->manager
))
427 p
= get_mount_parameters(m
);
428 if (p
&& fstab_is_extrinsic(m
->where
, p
->options
))
434 static int mount_add_default_ordering_dependencies(Mount
*m
, MountParameters
*p
, UnitDependencyMask mask
) {
435 const char *after
, *before
, *e
;
440 e
= path_startswith(m
->where
, "/sysroot");
441 if (e
&& in_initrd()) {
442 /* All mounts under /sysroot need to happen later, at initrd-fs.target time. IOW,
443 * it's not technically part of the basic initrd filesystem itself, and so
444 * shouldn't inherit the default Before=local-fs.target dependency. However,
445 * these mounts still need to start after local-fs-pre.target, as a sync point
446 * for things like systemd-hibernate-resume.service that should start before
449 after
= SPECIAL_LOCAL_FS_PRE_TARGET
;
450 before
= isempty(e
) ? SPECIAL_INITRD_ROOT_FS_TARGET
: SPECIAL_INITRD_FS_TARGET
;
452 } else if (in_initrd() && path_startswith(m
->where
, "/sysusr/usr")) {
453 after
= SPECIAL_LOCAL_FS_PRE_TARGET
;
454 before
= SPECIAL_INITRD_USR_FS_TARGET
;
456 } else if (mount_is_network(p
)) {
457 after
= SPECIAL_REMOTE_FS_PRE_TARGET
;
458 before
= SPECIAL_REMOTE_FS_TARGET
;
461 after
= SPECIAL_LOCAL_FS_PRE_TARGET
;
462 before
= SPECIAL_LOCAL_FS_TARGET
;
465 if (before
&& !mount_is_nofail(m
)) {
466 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_BEFORE
, before
, /* add_reference= */ true, mask
);
472 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, after
, /* add_reference= */ true, mask
);
477 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
,
478 /* add_reference= */ true, mask
);
482 /* If this is a tmpfs mount then we have to unmount it before we try to deactivate swaps */
483 if (streq_ptr(p
->fstype
, "tmpfs")) {
484 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, SPECIAL_SWAP_TARGET
,
485 /* add_reference= */ true, mask
);
493 static int mount_add_default_network_dependencies(Mount
*m
, MountParameters
*p
, UnitDependencyMask mask
) {
498 if (!mount_is_network(p
))
501 /* We order ourselves after network.target. This is primarily useful at shutdown: services that take
502 * down the network should order themselves before network.target, so that they are shut down only
503 * after this mount unit is stopped. */
505 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, SPECIAL_NETWORK_TARGET
,
506 /* add_reference= */ true, mask
);
510 /* We pull in network-online.target, and order ourselves after it. This is useful at start-up to
511 * actively pull in tools that want to be started before we start mounting network file systems, and
512 * whose purpose it is to delay this until the network is "up". */
514 return unit_add_two_dependencies_by_name(UNIT(m
), UNIT_WANTS
, UNIT_AFTER
, SPECIAL_NETWORK_ONLINE_TARGET
,
515 /* add_reference= */ true, mask
);
518 static int mount_add_default_dependencies(Mount
*m
) {
519 UnitDependencyMask mask
;
525 if (!UNIT(m
)->default_dependencies
)
528 /* We do not add any default dependencies to /, /usr or /run/initramfs/, since they are
529 * guaranteed to stay mounted the whole time, since our system is on it. Also, don't
530 * bother with anything mounted below virtual file systems, it's also going to be virtual,
531 * and hence not worth the effort. */
532 if (mount_is_extrinsic(UNIT(m
)))
535 p
= get_mount_parameters(m
);
539 mask
= m
->from_proc_self_mountinfo
? UNIT_DEPENDENCY_MOUNTINFO
: UNIT_DEPENDENCY_MOUNT_FILE
;
541 r
= mount_add_default_ordering_dependencies(m
, p
, mask
);
545 r
= mount_add_default_network_dependencies(m
, p
, mask
);
552 static int mount_verify(Mount
*m
) {
553 _cleanup_free_
char *e
= NULL
;
558 assert(UNIT(m
)->load_state
== UNIT_LOADED
);
560 if (!m
->from_fragment
&& !m
->from_proc_self_mountinfo
&& !UNIT(m
)->perpetual
)
563 r
= unit_name_from_path(m
->where
, ".mount", &e
);
565 return log_unit_error_errno(UNIT(m
), r
, "Failed to generate unit name from mount path: %m");
567 if (!unit_has_name(UNIT(m
), e
))
568 return log_unit_error_errno(UNIT(m
), SYNTHETIC_ERRNO(ENOEXEC
), "Where= setting doesn't match unit name. Refusing.");
570 if (mount_point_is_api(m
->where
) || mount_point_ignore(m
->where
))
571 return log_unit_error_errno(UNIT(m
), SYNTHETIC_ERRNO(ENOEXEC
),
572 "Cannot create mount unit for API file system '%s'. Refusing.", m
->where
);
574 if (mount_point_is_credentials(UNIT(m
)->manager
->prefix
[EXEC_DIRECTORY_RUNTIME
], m
->where
))
575 return log_unit_error_errno(UNIT(m
), SYNTHETIC_ERRNO(ENOEXEC
),
576 "Cannot create mount unit for credential mount '%s'. Refusing.", m
->where
);
578 p
= get_mount_parameters_fragment(m
);
579 if (p
&& !p
->what
&& !UNIT(m
)->perpetual
)
580 return log_unit_error_errno(UNIT(m
), SYNTHETIC_ERRNO(ENOEXEC
), "What= setting is missing. Refusing.");
585 static int mount_add_non_exec_dependencies(Mount
*m
) {
590 /* We may be called due to this mount appearing in /proc/self/mountinfo, hence we clear all existing
591 * dependencies that were initialized from the unit file but whose final value really depends on the
592 * content of /proc/self/mountinfo. Some (such as m->where) might have become stale now. */
593 unit_remove_dependencies(UNIT(m
), UNIT_DEPENDENCY_MOUNTINFO
| UNIT_DEPENDENCY_MOUNT_FILE
);
598 /* Adds in all dependencies directly responsible for ordering the mount, as opposed to dependencies
599 * resulting from the ExecContext and such. */
601 r
= mount_add_device_dependencies(m
);
605 r
= mount_add_mount_dependencies(m
);
609 r
= mount_add_default_dependencies(m
);
616 static int mount_add_extras(Mount
*m
) {
617 Unit
*u
= UNIT(ASSERT_PTR(m
));
620 /* Note: this call might be called after we already have been loaded once (and even when it has already been
621 * activated), in case data from /proc/self/mountinfo has changed. This means all code here needs to be ready
622 * to run with an already set up unit. */
624 if (u
->fragment_path
)
625 m
->from_fragment
= true;
628 r
= unit_name_to_path(u
->id
, &m
->where
);
629 if (r
== -ENAMETOOLONG
)
630 log_unit_error_errno(u
, r
, "Failed to derive mount point path from unit name, because unit name is hashed. "
631 "Set \"Where=\" in the unit file explicitly.");
636 path_simplify(m
->where
);
638 if (!u
->description
) {
639 _cleanup_free_
char *w
= mount_get_where_escaped(m
);
643 r
= unit_set_description(u
, w
);
648 r
= unit_patch_contexts(u
);
652 r
= unit_add_exec_dependencies(u
, &m
->exec_context
);
656 r
= unit_set_default_slice(u
);
660 r
= mount_add_non_exec_dependencies(m
);
667 static void mount_load_root_mount(Unit
*u
) {
668 Mount
*m
= ASSERT_PTR(MOUNT(u
));
670 if (!unit_has_name(u
, SPECIAL_ROOT_MOUNT
))
674 u
->default_dependencies
= false;
676 /* The stdio/kmsg bridge socket is on /, in order to avoid a dep loop, don't use kmsg logging for -.mount */
677 m
->exec_context
.std_output
= EXEC_OUTPUT_NULL
;
678 m
->exec_context
.std_input
= EXEC_INPUT_NULL
;
681 u
->description
= strdup("Root Mount");
684 static int mount_load(Unit
*u
) {
685 Mount
*m
= ASSERT_PTR(MOUNT(u
));
688 assert(u
->load_state
== UNIT_STUB
);
690 mount_load_root_mount(u
);
692 bool from_kernel
= m
->from_proc_self_mountinfo
|| u
->perpetual
;
694 r
= unit_load_fragment_and_dropin(u
, /* fragment_required = */ !from_kernel
);
696 /* Add in some extras. Note we do this in all cases (even if we failed to load the unit) when announced by the
697 * kernel, because we need some things to be set up no matter what when the kernel establishes a mount and thus
698 * we need to update the state in our unit to track it. After all, consider that we don't allow changing the
699 * 'slice' field for a unit once it is active. */
700 if (u
->load_state
== UNIT_LOADED
|| from_kernel
)
701 RET_GATHER(r
, mount_add_extras(m
));
706 if (u
->load_state
!= UNIT_LOADED
)
709 return mount_verify(m
);
712 static void mount_set_state(Mount
*m
, MountState state
) {
713 MountState old_state
;
717 if (m
->state
!= state
)
718 bus_unit_send_pending_change_signal(UNIT(m
), false);
720 old_state
= m
->state
;
723 if (!MOUNT_STATE_WITH_PROCESS(state
)) {
724 m
->timer_event_source
= sd_event_source_disable_unref(m
->timer_event_source
);
725 mount_unwatch_control_pid(m
);
726 m
->control_command
= NULL
;
727 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
730 if (state
!= old_state
)
731 log_unit_debug(UNIT(m
), "Changed %s -> %s", mount_state_to_string(old_state
), mount_state_to_string(state
));
733 unit_notify(UNIT(m
), state_translation_table
[old_state
], state_translation_table
[state
], m
->reload_result
== MOUNT_SUCCESS
);
736 static int mount_coldplug(Unit
*u
) {
737 Mount
*m
= ASSERT_PTR(MOUNT(u
));
740 assert(m
->state
== MOUNT_DEAD
);
742 if (m
->deserialized_state
== m
->state
)
745 if (pidref_is_set(&m
->control_pid
) &&
746 pidref_is_unwaited(&m
->control_pid
) > 0 &&
747 MOUNT_STATE_WITH_PROCESS(m
->deserialized_state
)) {
749 r
= unit_watch_pidref(UNIT(m
), &m
->control_pid
, /* exclusive= */ false);
753 r
= mount_arm_timer(m
, /* relative= */ false, usec_add(u
->state_change_timestamp
.monotonic
, m
->timeout_usec
));
758 if (!IN_SET(m
->deserialized_state
, MOUNT_DEAD
, MOUNT_FAILED
))
759 (void) unit_setup_exec_runtime(u
);
761 mount_set_state(m
, m
->deserialized_state
);
765 static void mount_catchup(Unit
*u
) {
766 Mount
*m
= ASSERT_PTR(MOUNT(u
));
768 /* Adjust the deserialized state. See comments in mount_process_proc_self_mountinfo(). */
769 if (m
->from_proc_self_mountinfo
)
773 assert(!pidref_is_set(&m
->control_pid
));
774 (void) unit_acquire_invocation_id(u
);
775 mount_cycle_clear(m
);
776 mount_enter_mounted(m
, MOUNT_SUCCESS
);
779 assert(pidref_is_set(&m
->control_pid
));
780 mount_set_state(m
, MOUNT_MOUNTING_DONE
);
787 case MOUNT_MOUNTING_DONE
:
788 assert(pidref_is_set(&m
->control_pid
));
789 mount_set_state(m
, MOUNT_MOUNTING
);
792 assert(!pidref_is_set(&m
->control_pid
));
793 mount_enter_dead(m
, MOUNT_SUCCESS
, /* flush_result = */ false);
800 static void mount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
801 Mount
*m
= ASSERT_PTR(MOUNT(u
));
807 prefix
= strempty(prefix
);
808 prefix2
= strjoina(prefix
, "\t");
810 p
= get_mount_parameters(m
);
813 "%sMount State: %s\n"
815 "%sClean Result: %s\n"
818 "%sFile System Type: %s\n"
820 "%sFrom /proc/self/mountinfo: %s\n"
821 "%sFrom fragment: %s\n"
823 "%sDirectoryMode: %04o\n"
824 "%sSloppyOptions: %s\n"
825 "%sLazyUnmount: %s\n"
826 "%sForceUnmount: %s\n"
827 "%sReadWriteOnly: %s\n"
828 "%sTimeoutSec: %s\n",
829 prefix
, mount_state_to_string(m
->state
),
830 prefix
, mount_result_to_string(m
->result
),
831 prefix
, mount_result_to_string(m
->clean_result
),
833 prefix
, p
? strna(p
->what
) : "n/a",
834 prefix
, p
? strna(p
->fstype
) : "n/a",
835 prefix
, p
? strna(p
->options
) : "n/a",
836 prefix
, yes_no(m
->from_proc_self_mountinfo
),
837 prefix
, yes_no(m
->from_fragment
),
838 prefix
, yes_no(mount_is_extrinsic(u
)),
839 prefix
, m
->directory_mode
,
840 prefix
, yes_no(m
->sloppy_options
),
841 prefix
, yes_no(m
->lazy_unmount
),
842 prefix
, yes_no(m
->force_unmount
),
843 prefix
, yes_no(m
->read_write_only
),
844 prefix
, FORMAT_TIMESPAN(m
->timeout_usec
, USEC_PER_SEC
));
846 if (pidref_is_set(&m
->control_pid
))
848 "%sControl PID: "PID_FMT
"\n",
849 prefix
, m
->control_pid
.pid
);
851 exec_context_dump(&m
->exec_context
, f
, prefix
);
852 kill_context_dump(&m
->kill_context
, f
, prefix
);
853 cgroup_context_dump(UNIT(m
), f
, prefix
);
855 for (MountExecCommand c
= 0; c
< _MOUNT_EXEC_COMMAND_MAX
; c
++) {
856 if (!m
->exec_command
[c
].argv
)
859 fprintf(f
, "%s%s %s:\n",
860 prefix
, glyph(GLYPH_ARROW_RIGHT
), mount_exec_command_to_string(c
));
862 exec_command_dump(m
->exec_command
+ c
, f
, prefix2
);
866 static ExecFlags
mount_exec_flags(MountState state
) {
867 ExecFlags flags
= EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
;
869 assert(IN_SET(state
, MOUNT_MOUNTING
, MOUNT_REMOUNTING
, MOUNT_UNMOUNTING
));
871 if (IN_SET(state
, MOUNT_MOUNTING
, MOUNT_REMOUNTING
))
872 flags
|= EXEC_SETUP_CREDENTIALS
;
877 static int mount_spawn(Mount
*m
, ExecCommand
*c
, ExecFlags flags
, PidRef
*ret_pid
) {
878 _cleanup_(exec_params_shallow_clear
) ExecParameters exec_params
= EXEC_PARAMETERS_INIT(flags
);
879 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
886 r
= unit_prepare_exec(UNIT(m
));
890 r
= mount_arm_timer(m
, /* relative= */ true, m
->timeout_usec
);
894 r
= unit_set_exec_params(UNIT(m
), &exec_params
);
898 /* Assume the label inherited from systemd as the fallback */
899 exec_params
.fallback_smack_process_label
= NULL
;
901 r
= exec_spawn(UNIT(m
),
911 r
= unit_watch_pidref(UNIT(m
), &pidref
, /* exclusive= */ true);
915 *ret_pid
= TAKE_PIDREF(pidref
);
919 static void mount_enter_dead(Mount
*m
, MountResult f
, bool flush_result
) {
922 if (m
->result
== MOUNT_SUCCESS
|| flush_result
)
925 unit_log_result(UNIT(m
), m
->result
== MOUNT_SUCCESS
, mount_result_to_string(m
->result
));
926 unit_warn_leftover_processes(UNIT(m
), /* start = */ false);
928 mount_set_state(m
, m
->result
!= MOUNT_SUCCESS
? MOUNT_FAILED
: MOUNT_DEAD
);
930 m
->exec_runtime
= exec_runtime_destroy(m
->exec_runtime
);
932 unit_destroy_runtime_data(UNIT(m
), &m
->exec_context
, /* destroy_runtime_dir = */ true);
934 unit_unref_uid_gid(UNIT(m
), true);
936 /* Any dependencies based on /proc/self/mountinfo are now stale. Let's re-generate dependencies from
938 (void) mount_add_non_exec_dependencies(m
);
941 static void mount_enter_mounted(Mount
*m
, MountResult f
) {
944 if (m
->result
== MOUNT_SUCCESS
)
947 /* Refer to service_set_state() handling of SERVICE_EXITED state for rationale. In particular
948 * for mount units let's not leave cred mounts around, otherwise the actual number of mounts would be
951 * Note that this effectively means fresh creds are used during remount, but that's mostly what
952 * users would expect anyways. */
953 unit_destroy_runtime_data(UNIT(m
), &m
->exec_context
, /* destroy_runtime_dir = */ false);
955 mount_set_state(m
, MOUNT_MOUNTED
);
958 static void mount_enter_dead_or_mounted(Mount
*m
, MountResult f
, bool flush_result
) {
961 /* Enter DEAD or MOUNTED state, depending on what the kernel currently says about the mount point.
962 * We use this whenever we executed an operation, so that our internal state reflects what
963 * the kernel says again, after all ultimately we just mirror the kernel's internal state on this.
965 * Note that flush_result only applies to mount_enter_dead(), since that's when the result gets
966 * turned into unit end state. */
968 if (m
->from_proc_self_mountinfo
)
969 mount_enter_mounted(m
, f
);
971 mount_enter_dead(m
, f
, flush_result
);
974 static int state_to_kill_operation(MountState state
) {
977 case MOUNT_REMOUNTING_SIGTERM
:
980 case MOUNT_UNMOUNTING_SIGTERM
:
981 return KILL_TERMINATE
;
983 case MOUNT_REMOUNTING_SIGKILL
:
984 case MOUNT_UNMOUNTING_SIGKILL
:
988 return _KILL_OPERATION_INVALID
;
992 static void mount_enter_signal(Mount
*m
, MountState state
, MountResult f
) {
997 if (m
->result
== MOUNT_SUCCESS
)
1000 r
= unit_kill_context(UNIT(m
), state_to_kill_operation(state
));
1002 log_unit_warning_errno(UNIT(m
), r
, "Failed to kill processes: %m");
1007 r
= mount_arm_timer(m
, /* relative= */ true, m
->timeout_usec
);
1009 log_unit_warning_errno(UNIT(m
), r
, "Failed to install timer: %m");
1013 mount_set_state(m
, state
);
1014 } else if (state
== MOUNT_REMOUNTING_SIGTERM
&& m
->kill_context
.send_sigkill
)
1015 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
1016 else if (IN_SET(state
, MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
))
1017 mount_enter_mounted(m
, MOUNT_SUCCESS
);
1018 else if (state
== MOUNT_UNMOUNTING_SIGTERM
&& m
->kill_context
.send_sigkill
)
1019 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
1021 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
, /* flush_result = */ false);
1026 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
, /* flush_result = */ false);
1029 static int mount_set_umount_command(Mount
*m
, ExecCommand
*c
) {
1035 r
= exec_command_set(c
, UMOUNT_PATH
, m
->where
, "-c", NULL
);
1039 if (m
->lazy_unmount
) {
1040 r
= exec_command_append(c
, "-l", NULL
);
1045 if (m
->force_unmount
) {
1046 r
= exec_command_append(c
, "-f", NULL
);
1054 static void mount_enter_unmounting(Mount
*m
) {
1059 /* Start counting our attempts */
1060 if (!IN_SET(m
->state
,
1062 MOUNT_UNMOUNTING_SIGTERM
,
1063 MOUNT_UNMOUNTING_SIGKILL
))
1064 m
->n_retry_umount
= 0;
1066 mount_unwatch_control_pid(m
);
1068 m
->control_command_id
= MOUNT_EXEC_UNMOUNT
;
1069 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_UNMOUNT
;
1071 r
= mount_set_umount_command(m
, m
->control_command
);
1073 log_unit_warning_errno(UNIT(m
), r
, "Failed to prepare umount command line: %m");
1077 r
= mount_spawn(m
, m
->control_command
, mount_exec_flags(MOUNT_UNMOUNTING
), &m
->control_pid
);
1079 log_unit_warning_errno(UNIT(m
), r
, "Failed to spawn 'umount' task: %m");
1083 mount_set_state(m
, MOUNT_UNMOUNTING
);
1087 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
, /* flush_result = */ false);
1090 static int mount_apply_graceful_options(Mount
*m
, const MountParameters
*p
, char **opts
) {
1091 _cleanup_strv_free_
char **graceful
= NULL
;
1092 _cleanup_free_
char *filtered
= NULL
;
1099 r
= fstab_filter_options(*opts
, "x-systemd.graceful-option\0", NULL
, NULL
, &graceful
, &filtered
);
1104 log_unit_warning(UNIT(m
), "x-systemd.graceful-option= used but file system type not known, suppressing all graceful options.");
1108 STRV_FOREACH(o
, graceful
) {
1109 _cleanup_free_
char *k
= NULL
, *v
= NULL
;
1111 r
= split_pair(*o
, "=", &k
, &v
);
1112 if (r
< 0 && r
!= -EINVAL
) /* EINVAL → not a key/value pair */
1115 r
= mount_option_supported(p
->fstype
, k
?: *o
, v
);
1117 log_unit_warning_errno(UNIT(m
), r
,
1118 "x-systemd.graceful-option= used but not supported by file system %s, suppressing all.",
1123 log_unit_warning_errno(UNIT(m
), r
,
1124 "x-systemd.graceful-option=%s specified, but cannot determine availability, suppressing: %m", *o
);
1126 log_unit_info(UNIT(m
), "x-systemd.graceful-option=%s specified, but option is not available, suppressing.", *o
);
1128 log_unit_debug(UNIT(m
), "x-systemd.graceful-option=%s specified and supported, appending to mount option string.", *o
);
1130 if (!strextend_with_separator(&filtered
, ",", *o
))
1135 return free_and_replace(*opts
, filtered
);
1138 static int mount_set_mount_command(Mount
*m
, ExecCommand
*c
, const MountParameters
*p
, bool remount
) {
1145 r
= exec_command_set(c
, MOUNT_PATH
, p
->what
, m
->where
, NULL
);
1149 if (m
->sloppy_options
) {
1150 r
= exec_command_append(c
, "-s", NULL
);
1155 if (m
->read_write_only
) {
1156 r
= exec_command_append(c
, "-w", NULL
);
1162 r
= exec_command_append(c
, "-t", p
->fstype
, NULL
);
1167 _cleanup_free_
char *opts
= NULL
;
1168 r
= fstab_filter_options(p
->options
, "nofail\0" "fail\0" "noauto\0" "auto\0", NULL
, NULL
, NULL
, &opts
);
1172 r
= mount_apply_graceful_options(m
, p
, &opts
);
1177 if (!strprepend_with_separator(&opts
, ",", "remount"))
1180 if (!isempty(opts
)) {
1181 r
= exec_command_append(c
, "-o", opts
, NULL
);
1189 static void mount_enter_mounting(Mount
*m
) {
1190 _cleanup_close_
int fd
= -EBADF
;
1191 _cleanup_free_
char *fn
= NULL
;
1196 /* Validate that the path we are overmounting does not contain any symlinks, because if it does, we
1197 * couldn't support that reasonably: the mounts in /proc/self/mountinfo would not be recognizable to
1199 fd
= chase_and_open_parent(m
->where
, /* root= */ NULL
, CHASE_PROHIBIT_SYMLINKS
|CHASE_MKDIR_0755
, &fn
);
1200 if (fd
== -EREMCHG
) {
1201 r
= unit_log_noncanonical_mount_path(UNIT(m
), m
->where
);
1205 log_unit_error_errno(UNIT(m
), fd
, "Failed to resolve parent of mount point '%s': %m", m
->where
);
1209 MountParameters
*p
= get_mount_parameters_fragment(m
);
1211 r
= log_unit_warning_errno(UNIT(m
), SYNTHETIC_ERRNO(ENOENT
), "No mount parameters to operate on.");
1215 bool source_is_dir
= true;
1216 if (mount_is_bind(p
)) {
1217 r
= is_dir(p
->what
, /* follow = */ true);
1218 if (r
< 0 && r
!= -ENOENT
)
1219 log_unit_info_errno(UNIT(m
), r
, "Failed to determine type of bind mount source '%s', ignoring: %m", p
->what
);
1221 source_is_dir
= false;
1224 r
= make_mount_point_inode_from_mode(
1227 source_is_dir
? S_IFDIR
: S_IFREG
,
1229 if (r
< 0 && r
!= -EEXIST
)
1230 log_unit_warning_errno(UNIT(m
), r
, "Failed to create mount point '%s', ignoring: %m", m
->where
);
1232 /* If we are asked to create an OverlayFS, create the upper/work directories if they are missing */
1233 if (streq_ptr(p
->fstype
, "overlay")) {
1234 _cleanup_strv_free_
char **dirs
= NULL
;
1236 r
= fstab_filter_options(
1238 "upperdir\0workdir\0",
1239 /* ret_namefound= */ NULL
,
1240 /* ret_value= */ NULL
,
1242 /* ret_filtered= */ NULL
);
1244 log_unit_warning_errno(
1247 "Failed to determine upper directory for OverlayFS, ignoring: %m");
1249 STRV_FOREACH(d
, dirs
) {
1250 r
= mkdir_p_label(*d
, m
->directory_mode
);
1251 if (r
< 0 && r
!= -EEXIST
)
1252 log_unit_warning_errno(
1255 "Failed to create overlay directory '%s', ignoring: %m",
1261 unit_warn_if_dir_nonempty(UNIT(m
), m
->where
);
1263 /* Create the source directory for bind-mounts if needed */
1264 if (mount_is_bind(p
)) {
1265 r
= mkdir_p_label(p
->what
, m
->directory_mode
);
1266 /* mkdir_p_label() can return -EEXIST if the target path exists and is not a directory - which is
1267 * totally OK, in case the user wants us to overmount a non-directory inode. Also -EROFS can be
1268 * returned on read-only filesystem. Moreover, -EACCES (and also maybe -EPERM?) may be returned
1269 * when the path is on NFS. See issue #24120. All such errors will be logged in the debug level. */
1270 if (r
< 0 && r
!= -EEXIST
)
1271 log_unit_full_errno(UNIT(m
),
1272 (r
== -EROFS
|| ERRNO_IS_PRIVILEGE(r
)) ? LOG_DEBUG
: LOG_WARNING
,
1273 r
, "Failed to make bind mount source '%s', ignoring: %m", p
->what
);
1276 mount_unwatch_control_pid(m
);
1277 unit_warn_leftover_processes(UNIT(m
), /* start = */ true);
1279 m
->control_command_id
= MOUNT_EXEC_MOUNT
;
1280 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_MOUNT
;
1282 r
= mount_set_mount_command(m
, m
->control_command
, p
, /* remount = */ false);
1284 log_unit_error_errno(UNIT(m
), r
, "Failed to prepare mount command line: %m");
1288 r
= mount_spawn(m
, m
->control_command
, mount_exec_flags(MOUNT_MOUNTING
), &m
->control_pid
);
1290 log_unit_warning_errno(UNIT(m
), r
, "Failed to spawn 'mount' task: %m");
1294 mount_set_state(m
, MOUNT_MOUNTING
);
1298 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
, /* flush_result = */ false);
1301 static void mount_set_reload_result(Mount
*m
, MountResult result
) {
1304 /* Only store the first error we encounter */
1305 if (m
->reload_result
!= MOUNT_SUCCESS
)
1308 m
->reload_result
= result
;
1311 static void mount_enter_remounting(Mount
*m
) {
1317 p
= get_mount_parameters_fragment(m
);
1319 r
= log_unit_warning_errno(UNIT(m
), SYNTHETIC_ERRNO(ENOENT
), "No mount parameters to operate on.");
1323 mount_unwatch_control_pid(m
);
1324 m
->reload_result
= MOUNT_SUCCESS
;
1326 m
->control_command_id
= MOUNT_EXEC_REMOUNT
;
1327 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_REMOUNT
;
1329 r
= mount_set_mount_command(m
, m
->control_command
, p
, /* remount = */ true);
1331 log_unit_error_errno(UNIT(m
), r
, "Failed to prepare remount command line: %m");
1335 r
= mount_spawn(m
, m
->control_command
, mount_exec_flags(MOUNT_REMOUNTING
), &m
->control_pid
);
1337 log_unit_warning_errno(UNIT(m
), r
, "Failed to spawn 'remount' task: %m");
1341 mount_set_state(m
, MOUNT_REMOUNTING
);
1345 mount_set_reload_result(m
, MOUNT_FAILURE_RESOURCES
);
1346 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
, /* flush_result = */ false);
1349 static void mount_cycle_clear(Mount
*m
) {
1352 /* Clear all state we shall forget for this new cycle */
1354 m
->result
= MOUNT_SUCCESS
;
1355 m
->reload_result
= MOUNT_SUCCESS
;
1356 exec_command_reset_status_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
1358 if (m
->cgroup_runtime
)
1359 m
->cgroup_runtime
->reset_accounting
= true;
1362 static int mount_start(Unit
*u
) {
1363 Mount
*m
= ASSERT_PTR(MOUNT(u
));
1366 /* We cannot fulfill this request right now, try again later
1368 if (IN_SET(m
->state
,
1370 MOUNT_UNMOUNTING_SIGTERM
,
1371 MOUNT_UNMOUNTING_SIGKILL
,
1375 /* Already on it! */
1376 if (IN_SET(m
->state
, MOUNT_MOUNTING
, MOUNT_MOUNTING_DONE
))
1379 assert(IN_SET(m
->state
, MOUNT_DEAD
, MOUNT_FAILED
));
1381 r
= unit_acquire_invocation_id(u
);
1385 mount_cycle_clear(m
);
1386 mount_enter_mounting(m
);
1391 static int mount_stop(Unit
*u
) {
1392 Mount
*m
= ASSERT_PTR(MOUNT(u
));
1396 case MOUNT_UNMOUNTING
:
1397 case MOUNT_UNMOUNTING_SIGKILL
:
1398 case MOUNT_UNMOUNTING_SIGTERM
:
1402 case MOUNT_MOUNTING
:
1403 case MOUNT_MOUNTING_DONE
:
1404 case MOUNT_REMOUNTING
:
1405 /* If we are still waiting for /bin/mount, we go directly into kill mode. */
1406 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_SUCCESS
);
1409 case MOUNT_REMOUNTING_SIGTERM
:
1410 /* If we are already waiting for a hung remount, convert this to the matching unmounting state */
1411 mount_set_state(m
, MOUNT_UNMOUNTING_SIGTERM
);
1414 case MOUNT_REMOUNTING_SIGKILL
:
1416 mount_set_state(m
, MOUNT_UNMOUNTING_SIGKILL
);
1420 mount_enter_unmounting(m
);
1423 case MOUNT_CLEANING
:
1424 /* If we are currently cleaning, then abort it, brutally. */
1425 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
1429 assert_not_reached();
1433 static int mount_reload(Unit
*u
) {
1434 Mount
*m
= ASSERT_PTR(MOUNT(u
));
1436 assert(m
->state
== MOUNT_MOUNTED
);
1438 mount_enter_remounting(m
);
1443 static bool mount_can_reload(Unit
*u
) {
1444 Mount
*m
= ASSERT_PTR(MOUNT(u
));
1446 return get_mount_parameters_fragment(m
);
1449 static int mount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1450 Mount
*m
= ASSERT_PTR(MOUNT(u
));
1455 (void) serialize_item(f
, "state", mount_state_to_string(m
->state
));
1456 (void) serialize_item(f
, "result", mount_result_to_string(m
->result
));
1457 (void) serialize_item(f
, "reload-result", mount_result_to_string(m
->reload_result
));
1458 (void) serialize_item_format(f
, "n-retry-umount", "%u", m
->n_retry_umount
);
1459 (void) serialize_pidref(f
, fds
, "control-pid", &m
->control_pid
);
1461 if (m
->control_command_id
>= 0)
1462 (void) serialize_item(f
, "control-command", mount_exec_command_to_string(m
->control_command_id
));
1467 static int mount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1468 Mount
*m
= ASSERT_PTR(MOUNT(u
));
1475 if (streq(key
, "state")) {
1478 state
= mount_state_from_string(value
);
1480 log_unit_debug_errno(u
, state
, "Failed to parse state value: %s", value
);
1482 m
->deserialized_state
= state
;
1484 } else if (streq(key
, "result")) {
1487 f
= mount_result_from_string(value
);
1489 log_unit_debug_errno(u
, f
, "Failed to parse result value: %s", value
);
1490 else if (f
!= MOUNT_SUCCESS
)
1493 } else if (streq(key
, "reload-result")) {
1496 f
= mount_result_from_string(value
);
1498 log_unit_debug_errno(u
, f
, "Failed to parse reload result value: %s", value
);
1499 else if (f
!= MOUNT_SUCCESS
)
1500 m
->reload_result
= f
;
1502 } else if (streq(key
, "n-retry-umount")) {
1504 r
= safe_atou(value
, &m
->n_retry_umount
);
1506 log_unit_debug_errno(u
, r
, "Failed to parse n-retry-umount value: %s", value
);
1508 } else if (streq(key
, "control-pid")) {
1510 if (!pidref_is_set(&m
->control_pid
))
1511 (void) deserialize_pidref(fds
, value
, &m
->control_pid
);
1513 } else if (streq(key
, "control-command")) {
1514 MountExecCommand id
;
1516 id
= mount_exec_command_from_string(value
);
1518 log_unit_debug_errno(u
, id
, "Failed to parse exec-command value: %s", value
);
1520 m
->control_command_id
= id
;
1521 m
->control_command
= m
->exec_command
+ id
;
1524 log_unit_debug(u
, "Unknown serialization key: %s", key
);
1529 static UnitActiveState
mount_active_state(Unit
*u
) {
1530 Mount
*m
= ASSERT_PTR(MOUNT(u
));
1532 return state_translation_table
[m
->state
];
1535 static const char *mount_sub_state_to_string(Unit
*u
) {
1536 Mount
*m
= ASSERT_PTR(MOUNT(u
));
1538 return mount_state_to_string(m
->state
);
1541 static bool mount_may_gc(Unit
*u
) {
1542 Mount
*m
= ASSERT_PTR(MOUNT(u
));
1544 if (m
->from_proc_self_mountinfo
)
1550 static void mount_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1551 Mount
*m
= ASSERT_PTR(MOUNT(u
));
1556 if (pid
!= m
->control_pid
.pid
)
1559 /* So here's the thing, we really want to know before /usr/bin/mount or /usr/bin/umount exit whether
1560 * they established/remove a mount. This is important when mounting, but even more so when unmounting
1561 * since we need to deal with nested mounts and otherwise cannot safely determine whether to repeat
1562 * the unmounts. In theory, the kernel fires /proc/self/mountinfo changes off before returning from
1563 * the mount() or umount() syscalls, and thus we should see the changes to the proc file before we
1564 * process the waitid() for the /usr/bin/(u)mount processes. However, this is unfortunately racy: we
1565 * have to waitid() for processes using P_ALL (since we need to reap unexpected children that got
1566 * reparented to PID 1), but when using P_ALL we might end up reaping processes that terminated just
1567 * instants ago, i.e. already after our last event loop iteration (i.e. after the last point we might
1568 * have noticed /proc/self/mountinfo events via epoll). This means event loop priorities for
1569 * processing SIGCHLD vs. /proc/self/mountinfo IO events are not as relevant as we want. To fix that
1570 * race, let's explicitly scan /proc/self/mountinfo before we start processing /usr/bin/(u)mount
1571 * dying. It's ugly, but it makes our ordering systematic again, and makes sure we always see
1572 * /proc/self/mountinfo changes before our mount/umount exits. */
1573 (void) mount_process_proc_self_mountinfo(u
->manager
);
1575 pidref_done(&m
->control_pid
);
1577 if (is_clean_exit(code
, status
, EXIT_CLEAN_COMMAND
, NULL
))
1579 else if (code
== CLD_EXITED
)
1580 f
= MOUNT_FAILURE_EXIT_CODE
;
1581 else if (code
== CLD_KILLED
)
1582 f
= MOUNT_FAILURE_SIGNAL
;
1583 else if (code
== CLD_DUMPED
)
1584 f
= MOUNT_FAILURE_CORE_DUMP
;
1586 assert_not_reached();
1588 if (IN_SET(m
->state
, MOUNT_REMOUNTING
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_REMOUNTING_SIGTERM
))
1589 mount_set_reload_result(m
, f
);
1590 else if (m
->result
== MOUNT_SUCCESS
&& !IN_SET(m
->state
, MOUNT_MOUNTING
, MOUNT_UNMOUNTING
))
1591 /* MOUNT_MOUNTING and MOUNT_UNMOUNTING states need to be patched, see below. */
1594 if (m
->control_command
) {
1595 exec_status_exit(&m
->control_command
->exec_status
, &m
->exec_context
, pid
, code
, status
);
1597 m
->control_command
= NULL
;
1598 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
1601 unit_log_process_exit(
1604 mount_exec_command_to_string(m
->control_command_id
),
1608 /* Note that due to the io event priority logic, we can be sure the new mountinfo is loaded
1609 * before we process the SIGCHLD for the mount command. */
1613 case MOUNT_MOUNTING
:
1614 /* Our mount point has not appeared in mountinfo. Something went wrong. */
1616 if (f
== MOUNT_SUCCESS
) {
1617 /* Either /bin/mount has an unexpected definition of success, or someone raced us
1619 log_unit_warning(UNIT(m
), "Mount process finished, but there is no mount.");
1620 f
= MOUNT_FAILURE_PROTOCOL
;
1622 mount_enter_dead(m
, f
, /* flush_result = */ false);
1625 case MOUNT_MOUNTING_DONE
:
1626 mount_enter_mounted(m
, f
);
1629 case MOUNT_REMOUNTING
:
1630 case MOUNT_REMOUNTING_SIGTERM
:
1631 case MOUNT_REMOUNTING_SIGKILL
:
1632 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
, /* flush_result = */ false);
1635 case MOUNT_UNMOUNTING
:
1636 if (f
== MOUNT_SUCCESS
&& m
->from_proc_self_mountinfo
) {
1637 /* Still a mount point? If so, let's try again. Most likely there were multiple mount points
1638 * stacked on top of each other. We might exceed the timeout specified by the user overall,
1639 * but we will stop as soon as any one umount times out. */
1641 if (m
->n_retry_umount
< RETRY_UMOUNT_MAX
) {
1642 log_unit_debug(u
, "Mount still present, trying again.");
1643 m
->n_retry_umount
++;
1644 mount_enter_unmounting(m
);
1646 log_unit_warning(u
, "Mount still present after %u attempts to unmount, giving up.", m
->n_retry_umount
);
1647 mount_enter_mounted(m
, MOUNT_FAILURE_PROTOCOL
);
1649 } else if (f
== MOUNT_FAILURE_EXIT_CODE
&& !m
->from_proc_self_mountinfo
) {
1650 /* Hmm, umount process spawned by us failed, but the mount disappeared anyway?
1651 * Maybe someone else is trying to unmount at the same time. */
1652 log_unit_notice(u
, "Mount disappeared even though umount process failed, continuing.");
1653 mount_enter_dead(m
, MOUNT_SUCCESS
, /* flush_result = */ true);
1655 /* At this point, either the unmount succeeded or unexpected error occurred. We usually
1656 * remember the first error in 'result', but here let's update that forcibly, since
1657 * there could previous failed attempts yet we only care about the most recent
1658 * attempt. IOW, if we eventually managed to unmount the stuff, don't enter failed
1660 mount_enter_dead_or_mounted(m
, f
, /* flush_result = */ true);
1664 case MOUNT_UNMOUNTING_SIGTERM
:
1665 case MOUNT_UNMOUNTING_SIGKILL
:
1666 mount_enter_dead_or_mounted(m
, f
, /* flush_result = */ false);
1669 case MOUNT_CLEANING
:
1670 if (m
->clean_result
== MOUNT_SUCCESS
)
1671 m
->clean_result
= f
;
1673 mount_enter_dead(m
, MOUNT_SUCCESS
, /* flush_result = */ false);
1677 assert_not_reached();
1680 /* Notify clients about changed exit status */
1681 unit_add_to_dbus_queue(u
);
1684 static int mount_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
1685 Mount
*m
= ASSERT_PTR(MOUNT(userdata
));
1687 assert(m
->timer_event_source
== source
);
1691 case MOUNT_MOUNTING
:
1692 case MOUNT_MOUNTING_DONE
:
1693 log_unit_warning(UNIT(m
), "Mounting timed out. Terminating.");
1694 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1697 case MOUNT_REMOUNTING
:
1698 log_unit_warning(UNIT(m
), "Remounting timed out. Terminating remount process.");
1699 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1700 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGTERM
, MOUNT_SUCCESS
);
1703 case MOUNT_REMOUNTING_SIGTERM
:
1704 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1706 if (m
->kill_context
.send_sigkill
) {
1707 log_unit_warning(UNIT(m
), "Remounting timed out. Killing.");
1708 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
1710 log_unit_warning(UNIT(m
), "Remounting timed out. Skipping SIGKILL. Ignoring.");
1711 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
, /* flush_result = */ false);
1715 case MOUNT_REMOUNTING_SIGKILL
:
1716 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1718 log_unit_warning(UNIT(m
), "Mount process still around after SIGKILL. Ignoring.");
1719 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
, /* flush_result = */ false);
1722 case MOUNT_UNMOUNTING
:
1723 log_unit_warning(UNIT(m
), "Unmounting timed out. Terminating.");
1724 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1727 case MOUNT_UNMOUNTING_SIGTERM
:
1728 if (m
->kill_context
.send_sigkill
) {
1729 log_unit_warning(UNIT(m
), "Mount process timed out. Killing.");
1730 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_FAILURE_TIMEOUT
);
1732 log_unit_warning(UNIT(m
), "Mount process timed out. Skipping SIGKILL. Ignoring.");
1733 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_TIMEOUT
, /* flush_result = */ false);
1737 case MOUNT_UNMOUNTING_SIGKILL
:
1738 log_unit_warning(UNIT(m
), "Mount process still around after SIGKILL. Ignoring.");
1739 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_TIMEOUT
, /* flush_result = */ false);
1742 case MOUNT_CLEANING
:
1743 log_unit_warning(UNIT(m
), "Cleaning timed out. killing.");
1745 if (m
->clean_result
== MOUNT_SUCCESS
)
1746 m
->clean_result
= MOUNT_FAILURE_TIMEOUT
;
1748 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, 0);
1752 assert_not_reached();
1758 static int mount_setup_new_unit(
1763 const char *options
,
1765 MountProcFlags
*ret_flags
,
1768 _cleanup_(unit_freep
) Unit
*u
= NULL
;
1777 r
= unit_new_for_name(m
, sizeof(Mount
), name
, &u
);
1781 mnt
= ASSERT_PTR(MOUNT(u
));
1783 r
= free_and_strdup(&u
->source_path
, "/proc/self/mountinfo");
1787 r
= free_and_strdup(&mnt
->where
, where
);
1791 r
= update_parameters_proc_self_mountinfo(mnt
, what
, options
, fstype
);
1795 /* This unit was generated because /proc/self/mountinfo reported it. Remember this, so that by the
1796 * time we load the unit file for it (and thus add in extra deps right after) we know what source to
1797 * attributes the deps to. */
1798 mnt
->from_proc_self_mountinfo
= true;
1800 /* We have only allocated the stub now, let's enqueue this unit for loading now, so that everything
1801 * else is loaded in now. */
1802 unit_add_to_load_queue(u
);
1804 *ret_flags
= MOUNT_PROC_IS_MOUNTED
| MOUNT_PROC_JUST_MOUNTED
| MOUNT_PROC_JUST_CHANGED
;
1809 static int mount_setup_existing_unit(
1813 const char *options
,
1815 MountProcFlags
*ret_flags
) {
1817 Mount
*m
= ASSERT_PTR(MOUNT(u
));
1825 m
->where
= strdup(where
);
1830 /* In case we have multiple mounts established on the same mount point, let's merge flags set already
1831 * for the current unit. Note that the flags field is reset on each iteration of reading
1832 * /proc/self/mountinfo, hence we know for sure anything already set here is from the current
1833 * iteration and thus worthy of taking into account. */
1834 MountProcFlags flags
= m
->proc_flags
| MOUNT_PROC_IS_MOUNTED
;
1836 r
= update_parameters_proc_self_mountinfo(m
, what
, options
, fstype
);
1840 flags
|= MOUNT_PROC_JUST_CHANGED
;
1842 /* There are two conditions when we consider a mount point just mounted: when we haven't seen it in
1843 * /proc/self/mountinfo before or when MOUNT_MOUNTING is our current state. Why bother with the
1844 * latter? Shouldn't that be covered by the former? No, during reload it is not because we might then
1845 * encounter a new /proc/self/mountinfo in combination with an old mount unit state (since it stems
1846 * from the serialized state), and need to catch up. Since we know that the MOUNT_MOUNTING state is
1847 * reached when we wait for the mount to appear we hence can assume that if we are in it, we are
1848 * actually seeing it established for the first time. */
1849 if (!m
->from_proc_self_mountinfo
|| m
->state
== MOUNT_MOUNTING
)
1850 flags
|= MOUNT_PROC_JUST_MOUNTED
;
1852 m
->from_proc_self_mountinfo
= true;
1854 if (UNIT_IS_LOAD_ERROR(u
->load_state
)) {
1855 /* The unit was previously not found or otherwise not loaded. Now that the unit shows up in
1856 * /proc/self/mountinfo we should reconsider that. Hence, let's reset the load state and load
1857 * error, and add the unit to load queue. */
1858 u
->load_state
= UNIT_STUB
;
1860 unit_add_to_load_queue(u
);
1862 flags
|= MOUNT_PROC_JUST_CHANGED
;
1865 if (FLAGS_SET(flags
, MOUNT_PROC_JUST_CHANGED
) && u
->load_state
== UNIT_LOADED
) {
1866 /* If things changed, and we have successfully loaded the unit, then make sure that all deps
1867 * are regenerated. Let's first remove all automatic deps, and then add in the new ones. */
1868 r
= mount_add_non_exec_dependencies(m
);
1877 static int mount_setup_unit(
1881 const char *options
,
1885 _cleanup_free_
char *e
= NULL
;
1886 MountProcFlags flags
;
1896 /* Ignore API and credential mount points. They should never be referenced in dependencies ever.
1897 * Furthermore, the lifetime of credential mounts is strictly bound to the owning services,
1898 * so mount units make little sense for them. */
1899 if (mount_point_is_api(where
) || mount_point_ignore(where
) ||
1900 mount_point_is_credentials(m
->prefix
[EXEC_DIRECTORY_RUNTIME
], where
))
1903 if (streq(fstype
, "autofs"))
1906 /* probably some kind of swap, ignore */
1907 if (!is_path(where
))
1910 r
= unit_name_from_path(where
, ".mount", &e
);
1912 return log_struct_errno(
1914 LOG_MESSAGE_ID(SD_MESSAGE_MOUNT_POINT_PATH_NOT_SUITABLE_STR
),
1915 LOG_ITEM("MOUNT_POINT=%s", where
),
1916 LOG_MESSAGE("Failed to generate valid unit name from mount point path '%s', ignoring mount point: %m",
1919 u
= manager_get_unit(m
, e
);
1921 r
= mount_setup_existing_unit(u
, what
, where
, options
, fstype
, &flags
);
1923 /* First time we see this mount point meaning that it's not been initiated by a mount unit
1924 * but rather by the sysadmin having called mount(8) directly. */
1925 r
= mount_setup_new_unit(m
, e
, what
, where
, options
, fstype
, &flags
, &u
);
1927 return log_warning_errno(r
, "Failed to set up mount unit for '%s': %m", where
);
1929 /* If the mount changed properties or state, let's notify our clients */
1930 if (flags
& (MOUNT_PROC_JUST_CHANGED
|MOUNT_PROC_JUST_MOUNTED
))
1931 unit_add_to_dbus_queue(u
);
1934 MOUNT(u
)->proc_flags
= flags
;
1939 static int mount_load_proc_self_mountinfo(Manager
*m
, bool set_flags
) {
1940 _cleanup_(mnt_free_tablep
) struct libmnt_table
*table
= NULL
;
1941 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*iter
= NULL
;
1942 _cleanup_set_free_ Set
*devices
= NULL
;
1947 r
= libmount_parse_with_utab(&table
, &iter
);
1949 return log_error_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
1952 struct libmnt_fs
*fs
;
1953 const char *device
, *path
, *options
, *fstype
;
1955 r
= mnt_table_next_fs(table
, iter
, &fs
);
1959 return log_error_errno(r
, "Failed to get next entry from /proc/self/mountinfo: %m");
1961 device
= mnt_fs_get_source(fs
);
1962 path
= mnt_fs_get_target(fs
);
1963 options
= mnt_fs_get_options(fs
);
1964 fstype
= mnt_fs_get_fstype(fs
);
1966 if (!device
|| !path
)
1969 /* Just to achieve device name uniqueness. Note that the suppression of the duplicate
1970 * processing is merely an optimization, hence in case of OOM (unlikely) we'll just process
1972 if (set_put_strdup_full(&devices
, &path_hash_ops_free
, device
) != 0)
1973 device_found_node(m
, device
, DEVICE_FOUND_MOUNT
, DEVICE_FOUND_MOUNT
);
1975 (void) mount_setup_unit(m
, device
, path
, options
, fstype
, set_flags
);
1981 static void mount_shutdown(Manager
*m
) {
1984 m
->mount_event_source
= sd_event_source_disable_unref(m
->mount_event_source
);
1986 mnt_unref_monitor(m
->mount_monitor
);
1987 m
->mount_monitor
= NULL
;
1990 static void mount_handoff_timestamp(
1992 const struct ucred
*ucred
,
1993 const dual_timestamp
*ts
) {
1995 Mount
*m
= ASSERT_PTR(MOUNT(u
));
2000 if (m
->control_pid
.pid
== ucred
->pid
&& m
->control_command
) {
2001 exec_status_handoff(&m
->control_command
->exec_status
, ucred
, ts
);
2002 unit_add_to_dbus_queue(u
);
2006 static int mount_get_timeout(Unit
*u
, usec_t
*timeout
) {
2007 Mount
*m
= ASSERT_PTR(MOUNT(u
));
2011 if (!m
->timer_event_source
)
2014 r
= sd_event_source_get_time(m
->timer_event_source
, &t
);
2017 if (t
== USEC_INFINITY
)
2024 static void mount_enumerate_perpetual(Manager
*m
) {
2030 /* Whatever happens, we know for sure that the root directory is around, and cannot go away. Let's
2031 * unconditionally synthesize it here and mark it as perpetual. */
2033 u
= manager_get_unit(m
, SPECIAL_ROOT_MOUNT
);
2035 r
= unit_new_for_name(m
, sizeof(Mount
), SPECIAL_ROOT_MOUNT
, &u
);
2037 return (void) log_error_errno(r
, "Failed to allocate the special %s unit: %m",
2038 SPECIAL_ROOT_MOUNT
);
2041 u
->perpetual
= true;
2042 MOUNT(u
)->deserialized_state
= MOUNT_MOUNTED
;
2044 unit_add_to_load_queue(u
);
2045 unit_add_to_dbus_queue(u
);
2048 static bool mount_is_mounted(Mount
*m
) {
2051 return UNIT(m
)->perpetual
|| FLAGS_SET(m
->proc_flags
, MOUNT_PROC_IS_MOUNTED
);
2054 static int mount_on_ratelimit_expire(sd_event_source
*s
, void *userdata
) {
2055 Manager
*m
= ASSERT_PTR(userdata
);
2058 /* Let's enqueue all start jobs that were previously skipped because of active ratelimit. */
2059 HASHMAP_FOREACH(j
, m
->jobs
) {
2060 if (j
->unit
->type
!= UNIT_MOUNT
)
2063 job_add_to_run_queue(j
);
2066 /* By entering ratelimited state we made all mount start jobs not runnable, now rate limit is over so
2067 * let's make sure we dispatch them in the next iteration. */
2068 manager_trigger_run_queue(m
);
2073 static void mount_enumerate(Manager
*m
) {
2080 if (!m
->mount_monitor
) {
2081 usec_t mount_rate_limit_interval
= 1 * USEC_PER_SEC
;
2082 unsigned mount_rate_limit_burst
= 5;
2085 m
->mount_monitor
= mnt_new_monitor();
2086 if (!m
->mount_monitor
) {
2091 r
= mnt_monitor_enable_kernel(m
->mount_monitor
, 1);
2093 log_error_errno(r
, "Failed to enable watching of kernel mount events: %m");
2097 r
= mnt_monitor_enable_userspace(m
->mount_monitor
, 1, NULL
);
2099 log_error_errno(r
, "Failed to enable watching of userspace mount events: %m");
2103 /* mnt_unref_monitor() will close the fd */
2104 fd
= r
= mnt_monitor_get_fd(m
->mount_monitor
);
2106 log_error_errno(r
, "Failed to acquire watch file descriptor: %m");
2110 r
= sd_event_add_io(m
->event
, &m
->mount_event_source
, fd
, EPOLLIN
, mount_dispatch_io
, m
);
2112 log_error_errno(r
, "Failed to watch mount file descriptor: %m");
2116 r
= sd_event_source_set_priority(m
->mount_event_source
, EVENT_PRIORITY_MOUNT_TABLE
);
2118 log_error_errno(r
, "Failed to adjust mount watch priority: %m");
2122 /* Let users override the default (5 in 1s), as it stalls the boot sequence on busy systems. */
2123 const char *e
= secure_getenv("SYSTEMD_DEFAULT_MOUNT_RATE_LIMIT_INTERVAL_SEC");
2125 r
= parse_sec(e
, &mount_rate_limit_interval
);
2127 log_debug_errno(r
, "Invalid value in $SYSTEMD_DEFAULT_MOUNT_RATE_LIMIT_INTERVAL_SEC, ignoring: %s", e
);
2130 e
= secure_getenv("SYSTEMD_DEFAULT_MOUNT_RATE_LIMIT_BURST");
2132 r
= safe_atou(e
, &mount_rate_limit_burst
);
2134 log_debug_errno(r
, "Invalid value in $SYSTEMD_DEFAULT_MOUNT_RATE_LIMIT_BURST, ignoring: %s", e
);
2137 r
= sd_event_source_set_ratelimit(m
->mount_event_source
, mount_rate_limit_interval
, mount_rate_limit_burst
);
2139 log_error_errno(r
, "Failed to enable rate limit for mount events: %m");
2143 r
= sd_event_source_set_ratelimit_expire_callback(m
->mount_event_source
, mount_on_ratelimit_expire
);
2145 log_error_errno(r
, "Failed to enable rate limit for mount events: %m");
2149 (void) sd_event_source_set_description(m
->mount_event_source
, "mount-monitor-dispatch");
2152 r
= mount_load_proc_self_mountinfo(m
, false);
2162 static int drain_libmount(Manager
*m
) {
2163 bool rescan
= false;
2168 /* Drain all events and verify that the event is valid.
2170 * Note that libmount also monitors /run/mount mkdir if the directory does not exist yet. The mkdir
2171 * may generate event which is irrelevant for us.
2173 * error: r < 0; valid: r == 0, false positive: r == 1 */
2175 r
= mnt_monitor_next_change(m
->mount_monitor
, NULL
, NULL
);
2177 return log_error_errno(r
, "Failed to drain libmount events: %m");
2185 static int mount_process_proc_self_mountinfo(Manager
*m
) {
2186 _cleanup_set_free_ Set
*around
= NULL
, *gone
= NULL
;
2192 r
= drain_libmount(m
);
2196 r
= mount_load_proc_self_mountinfo(m
, true);
2198 /* Reset flags, just in case, for later calls */
2199 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
])
2200 MOUNT(u
)->proc_flags
= 0;
2205 manager_dispatch_load_queue(m
);
2207 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
]) {
2208 Mount
*mount
= MOUNT(u
);
2210 if (!mount_is_mounted(mount
)) {
2212 /* A mount point is not around right now. It might be gone, or might never have
2215 if (mount
->from_proc_self_mountinfo
&&
2216 mount
->parameters_proc_self_mountinfo
.what
)
2217 /* Remember that this device might just have disappeared */
2218 if (set_put_strdup_full(&gone
, &path_hash_ops_free
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
2219 log_oom(); /* we don't care too much about OOM here... */
2221 mount
->from_proc_self_mountinfo
= false;
2222 assert_se(update_parameters_proc_self_mountinfo(mount
, NULL
, NULL
, NULL
) >= 0);
2224 switch (mount
->state
) {
2227 /* This has just been unmounted by somebody else, follow the state change.
2228 * Also explicitly override the result (see the comment in mount_sigchld_event()),
2229 * but more aggressively here since the state change is extrinsic. */
2230 mount_cycle_clear(mount
);
2231 mount_enter_dead(mount
, MOUNT_SUCCESS
, /* flush_result = */ true);
2234 case MOUNT_MOUNTING_DONE
:
2235 /* The mount command may add the corresponding proc mountinfo entry and
2236 * then remove it because of an internal error. E.g., fuse.sshfs seems
2237 * to do that when the connection fails. See #17617. To handle such the
2238 * case, let's once set the state back to mounting. Then, the unit can
2239 * correctly enter the failed state later in mount_sigchld_event(). */
2240 mount_set_state(mount
, MOUNT_MOUNTING
);
2247 } else if (mount
->proc_flags
& (MOUNT_PROC_JUST_MOUNTED
|MOUNT_PROC_JUST_CHANGED
)) {
2249 /* A mount point was added or changed */
2251 switch (mount
->state
) {
2256 /* This has just been mounted by somebody else, follow the state change, but let's
2257 * generate a new invocation ID for this implicitly and automatically. */
2258 (void) unit_acquire_invocation_id(u
);
2259 mount_cycle_clear(mount
);
2260 mount_enter_mounted(mount
, MOUNT_SUCCESS
);
2263 case MOUNT_MOUNTING
:
2264 mount_set_state(mount
, MOUNT_MOUNTING_DONE
);
2268 /* Nothing really changed, but let's issue an notification call nonetheless,
2269 * in case somebody is waiting for this. (e.g. file system ro/rw
2271 mount_set_state(mount
, mount
->state
);
2275 if (mount_is_mounted(mount
) &&
2276 mount
->from_proc_self_mountinfo
&&
2277 mount
->parameters_proc_self_mountinfo
.what
)
2278 /* Track devices currently used */
2279 if (set_put_strdup_full(&around
, &path_hash_ops_free
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
2282 /* Reset the flags for later calls */
2283 mount
->proc_flags
= 0;
2286 SET_FOREACH(what
, gone
) {
2287 if (set_contains(around
, what
))
2290 /* Let the device units know that the device is no longer mounted */
2291 device_found_node(m
, what
, DEVICE_NOT_FOUND
, DEVICE_FOUND_MOUNT
);
2297 static int mount_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
2298 Manager
*m
= ASSERT_PTR(userdata
);
2300 assert(revents
& EPOLLIN
);
2302 return mount_process_proc_self_mountinfo(m
);
2305 static void mount_reset_failed(Unit
*u
) {
2306 Mount
*m
= MOUNT(u
);
2310 if (m
->state
== MOUNT_FAILED
)
2311 mount_set_state(m
, MOUNT_DEAD
);
2313 m
->result
= MOUNT_SUCCESS
;
2314 m
->reload_result
= MOUNT_SUCCESS
;
2315 m
->clean_result
= MOUNT_SUCCESS
;
2318 static PidRef
* mount_control_pid(Unit
*u
) {
2319 return &ASSERT_PTR(MOUNT(u
))->control_pid
;
2322 static int mount_clean(Unit
*u
, ExecCleanMask mask
) {
2323 _cleanup_strv_free_
char **l
= NULL
;
2324 Mount
*m
= MOUNT(u
);
2330 if (m
->state
!= MOUNT_DEAD
)
2333 r
= exec_context_get_clean_directories(&m
->exec_context
, u
->manager
->prefix
, mask
, &l
);
2337 if (strv_isempty(l
))
2340 mount_unwatch_control_pid(m
);
2341 m
->clean_result
= MOUNT_SUCCESS
;
2342 m
->control_command
= NULL
;
2343 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
2345 r
= mount_arm_timer(m
, /* relative= */ true, m
->exec_context
.timeout_clean_usec
);
2347 log_unit_warning_errno(u
, r
, "Failed to install timer: %m");
2351 r
= unit_fork_and_watch_rm_rf(u
, l
, &m
->control_pid
);
2353 log_unit_warning_errno(u
, r
, "Failed to spawn cleaning task: %m");
2357 mount_set_state(m
, MOUNT_CLEANING
);
2361 m
->clean_result
= MOUNT_FAILURE_RESOURCES
;
2362 m
->timer_event_source
= sd_event_source_disable_unref(m
->timer_event_source
);
2366 static int mount_can_clean(Unit
*u
, ExecCleanMask
*ret
) {
2367 Mount
*m
= ASSERT_PTR(MOUNT(u
));
2369 return exec_context_get_clean_mask(&m
->exec_context
, ret
);
2372 static int mount_can_start(Unit
*u
) {
2373 Mount
*m
= ASSERT_PTR(MOUNT(u
));
2376 r
= unit_test_start_limit(u
);
2378 mount_enter_dead(m
, MOUNT_FAILURE_START_LIMIT_HIT
, /* flush_result = */ false);
2382 if (!get_mount_parameters_fragment(m
))
2388 static int mount_subsystem_ratelimited(Manager
*m
) {
2391 if (!m
->mount_event_source
)
2394 return sd_event_source_is_ratelimited(m
->mount_event_source
);
2397 char* mount_get_where_escaped(const Mount
*m
) {
2403 return utf8_escape_invalid(m
->where
);
2406 char* mount_get_what_escaped(const Mount
*m
) {
2407 _cleanup_free_
char *escaped
= NULL
;
2408 const char *s
= NULL
;
2412 if (m
->from_proc_self_mountinfo
&& m
->parameters_proc_self_mountinfo
.what
)
2413 s
= m
->parameters_proc_self_mountinfo
.what
;
2414 else if (m
->from_fragment
&& m
->parameters_fragment
.what
)
2415 s
= m
->parameters_fragment
.what
;
2418 escaped
= utf8_escape_invalid(s
);
2423 return escaped
? TAKE_PTR(escaped
) : strdup("");
2426 char* mount_get_options_escaped(const Mount
*m
) {
2427 const char *s
= NULL
;
2431 if (m
->from_proc_self_mountinfo
&& m
->parameters_proc_self_mountinfo
.options
)
2432 s
= m
->parameters_proc_self_mountinfo
.options
;
2433 else if (m
->from_fragment
&& m
->parameters_fragment
.options
)
2434 s
= m
->parameters_fragment
.options
;
2438 return utf8_escape_invalid(s
);
2441 const char* mount_get_fstype(const Mount
*m
) {
2444 if (m
->from_proc_self_mountinfo
&& m
->parameters_proc_self_mountinfo
.fstype
)
2445 return m
->parameters_proc_self_mountinfo
.fstype
;
2447 if (m
->from_fragment
&& m
->parameters_fragment
.fstype
)
2448 return m
->parameters_fragment
.fstype
;
2453 static const char* const mount_exec_command_table
[_MOUNT_EXEC_COMMAND_MAX
] = {
2454 [MOUNT_EXEC_MOUNT
] = "ExecMount",
2455 [MOUNT_EXEC_UNMOUNT
] = "ExecUnmount",
2456 [MOUNT_EXEC_REMOUNT
] = "ExecRemount",
2459 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command
, MountExecCommand
);
2461 static const char* const mount_result_table
[_MOUNT_RESULT_MAX
] = {
2462 [MOUNT_SUCCESS
] = "success",
2463 [MOUNT_FAILURE_RESOURCES
] = "resources",
2464 [MOUNT_FAILURE_TIMEOUT
] = "timeout",
2465 [MOUNT_FAILURE_EXIT_CODE
] = "exit-code",
2466 [MOUNT_FAILURE_SIGNAL
] = "signal",
2467 [MOUNT_FAILURE_CORE_DUMP
] = "core-dump",
2468 [MOUNT_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
2469 [MOUNT_FAILURE_PROTOCOL
] = "protocol",
2472 DEFINE_STRING_TABLE_LOOKUP(mount_result
, MountResult
);
2474 const UnitVTable mount_vtable
= {
2475 .object_size
= sizeof(Mount
),
2476 .exec_context_offset
= offsetof(Mount
, exec_context
),
2477 .cgroup_context_offset
= offsetof(Mount
, cgroup_context
),
2478 .kill_context_offset
= offsetof(Mount
, kill_context
),
2479 .exec_runtime_offset
= offsetof(Mount
, exec_runtime
),
2480 .cgroup_runtime_offset
= offsetof(Mount
, cgroup_runtime
),
2486 .private_section
= "Mount",
2488 .can_transient
= true,
2490 .exclude_from_switch_root_serialization
= true,
2496 .coldplug
= mount_coldplug
,
2497 .catchup
= mount_catchup
,
2501 .start
= mount_start
,
2504 .reload
= mount_reload
,
2505 .can_reload
= mount_can_reload
,
2507 .clean
= mount_clean
,
2508 .can_clean
= mount_can_clean
,
2510 .serialize
= mount_serialize
,
2511 .deserialize_item
= mount_deserialize_item
,
2513 .active_state
= mount_active_state
,
2514 .sub_state_to_string
= mount_sub_state_to_string
,
2516 .will_restart
= unit_will_restart_default
,
2518 .may_gc
= mount_may_gc
,
2519 .is_extrinsic
= mount_is_extrinsic
,
2521 .sigchld_event
= mount_sigchld_event
,
2523 .reset_failed
= mount_reset_failed
,
2525 .notify_handoff_timestamp
= mount_handoff_timestamp
,
2527 .control_pid
= mount_control_pid
,
2529 .bus_set_property
= bus_mount_set_property
,
2530 .bus_commit_properties
= bus_mount_commit_properties
,
2532 .get_timeout
= mount_get_timeout
,
2534 .enumerate_perpetual
= mount_enumerate_perpetual
,
2535 .enumerate
= mount_enumerate
,
2536 .shutdown
= mount_shutdown
,
2537 .subsystem_ratelimited
= mount_subsystem_ratelimited
,
2539 .status_message_formats
= {
2540 .starting_stopping
= {
2541 [0] = "Mounting %s...",
2542 [1] = "Unmounting %s...",
2544 .finished_start_job
= {
2545 [JOB_DONE
] = "Mounted %s.",
2546 [JOB_FAILED
] = "Failed to mount %s.",
2547 [JOB_TIMEOUT
] = "Timed out mounting %s.",
2549 .finished_stop_job
= {
2550 [JOB_DONE
] = "Unmounted %s.",
2551 [JOB_FAILED
] = "Failed unmounting %s.",
2552 [JOB_TIMEOUT
] = "Timed out unmounting %s.",
2556 .can_start
= mount_can_start
,
2558 .notify_plymouth
= true,