1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "sd-messages.h"
12 #include "alloc-util.h"
13 #include "dbus-mount.h"
14 #include "dbus-unit.h"
17 #include "exit-status.h"
18 #include "format-util.h"
19 #include "fstab-util.h"
23 #include "mount-setup.h"
25 #include "mountpoint-util.h"
26 #include "parse-util.h"
27 #include "path-util.h"
28 #include "process-util.h"
29 #include "serialize.h"
31 #include "string-table.h"
32 #include "string-util.h"
34 #include "unit-name.h"
37 #define RETRY_UMOUNT_MAX 32
39 DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_table
*, mnt_free_table
);
40 DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_iter
*, mnt_free_iter
);
42 static const UnitActiveState state_translation_table
[_MOUNT_STATE_MAX
] = {
43 [MOUNT_DEAD
] = UNIT_INACTIVE
,
44 [MOUNT_MOUNTING
] = UNIT_ACTIVATING
,
45 [MOUNT_MOUNTING_DONE
] = UNIT_ACTIVATING
,
46 [MOUNT_MOUNTED
] = UNIT_ACTIVE
,
47 [MOUNT_REMOUNTING
] = UNIT_RELOADING
,
48 [MOUNT_UNMOUNTING
] = UNIT_DEACTIVATING
,
49 [MOUNT_REMOUNTING_SIGTERM
] = UNIT_RELOADING
,
50 [MOUNT_REMOUNTING_SIGKILL
] = UNIT_RELOADING
,
51 [MOUNT_UNMOUNTING_SIGTERM
] = UNIT_DEACTIVATING
,
52 [MOUNT_UNMOUNTING_SIGKILL
] = UNIT_DEACTIVATING
,
53 [MOUNT_FAILED
] = UNIT_FAILED
56 static int mount_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
57 static int mount_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
);
58 static int mount_dispatch_proc_self_mountinfo_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
60 static bool MOUNT_STATE_WITH_PROCESS(MountState state
) {
65 MOUNT_REMOUNTING_SIGTERM
,
66 MOUNT_REMOUNTING_SIGKILL
,
68 MOUNT_UNMOUNTING_SIGTERM
,
69 MOUNT_UNMOUNTING_SIGKILL
);
72 static bool mount_is_network(const MountParameters
*p
) {
75 if (fstab_test_option(p
->options
, "_netdev\0"))
78 if (p
->fstype
&& fstype_is_network(p
->fstype
))
84 static bool mount_is_loop(const MountParameters
*p
) {
87 if (fstab_test_option(p
->options
, "loop\0"))
93 static bool mount_is_bind(const MountParameters
*p
) {
96 if (fstab_test_option(p
->options
, "bind\0" "rbind\0"))
99 if (p
->fstype
&& STR_IN_SET(p
->fstype
, "bind", "rbind"))
105 static bool mount_is_auto(const MountParameters
*p
) {
108 return !fstab_test_option(p
->options
, "noauto\0");
111 static bool mount_is_automount(const MountParameters
*p
) {
114 return fstab_test_option(p
->options
,
115 "comment=systemd.automount\0"
116 "x-systemd.automount\0");
119 static bool mount_is_bound_to_device(const Mount
*m
) {
120 const MountParameters
*p
;
122 if (m
->from_fragment
)
125 p
= &m
->parameters_proc_self_mountinfo
;
126 return fstab_test_option(p
->options
, "x-systemd.device-bound\0");
129 static bool mount_needs_quota(const MountParameters
*p
) {
132 /* Quotas are not enabled on network filesystems, but we want them, for example, on storage connected via
133 * iscsi. We hence don't use mount_is_network() here, as that would also return true for _netdev devices. */
134 if (p
->fstype
&& fstype_is_network(p
->fstype
))
137 if (mount_is_bind(p
))
140 return fstab_test_option(p
->options
,
141 "usrquota\0" "grpquota\0" "quota\0" "usrjquota\0" "grpjquota\0");
144 static void mount_init(Unit
*u
) {
148 assert(u
->load_state
== UNIT_STUB
);
150 m
->timeout_usec
= u
->manager
->default_timeout_start_usec
;
152 m
->exec_context
.std_output
= u
->manager
->default_std_output
;
153 m
->exec_context
.std_error
= u
->manager
->default_std_error
;
155 m
->directory_mode
= 0755;
157 /* We need to make sure that /usr/bin/mount is always called
158 * in the same process group as us, so that the autofs kernel
159 * side doesn't send us another mount request while we are
160 * already trying to comply its last one. */
161 m
->exec_context
.same_pgrp
= true;
163 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
165 u
->ignore_on_isolate
= true;
168 static int mount_arm_timer(Mount
*m
, usec_t usec
) {
173 if (m
->timer_event_source
) {
174 r
= sd_event_source_set_time(m
->timer_event_source
, usec
);
178 return sd_event_source_set_enabled(m
->timer_event_source
, SD_EVENT_ONESHOT
);
181 if (usec
== USEC_INFINITY
)
184 r
= sd_event_add_time(
185 UNIT(m
)->manager
->event
,
186 &m
->timer_event_source
,
189 mount_dispatch_timer
, m
);
193 (void) sd_event_source_set_description(m
->timer_event_source
, "mount-timer");
198 static void mount_unwatch_control_pid(Mount
*m
) {
201 if (m
->control_pid
<= 0)
204 unit_unwatch_pid(UNIT(m
), m
->control_pid
);
208 static void mount_parameters_done(MountParameters
*p
) {
211 p
->what
= mfree(p
->what
);
212 p
->options
= mfree(p
->options
);
213 p
->fstype
= mfree(p
->fstype
);
216 static void mount_done(Unit
*u
) {
221 if (!IN_SET(m
->state
, MOUNT_DEAD
, MOUNT_MOUNTED
, MOUNT_FAILED
)) {
222 /* This was pending, so need to udpate the count */
223 assert(u
->manager
->mount_pending_count
> 0);
224 u
->manager
->mount_pending_count
--;
227 m
->where
= mfree(m
->where
);
229 mount_parameters_done(&m
->parameters_proc_self_mountinfo
);
230 mount_parameters_done(&m
->parameters_fragment
);
232 m
->exec_runtime
= exec_runtime_unref(m
->exec_runtime
, false);
233 exec_command_done_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
234 m
->control_command
= NULL
;
236 dynamic_creds_unref(&m
->dynamic_creds
);
238 mount_unwatch_control_pid(m
);
240 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
243 _pure_
static MountParameters
* get_mount_parameters_fragment(Mount
*m
) {
246 if (m
->from_fragment
)
247 return &m
->parameters_fragment
;
252 _pure_
static MountParameters
* get_mount_parameters(Mount
*m
) {
255 if (m
->from_proc_self_mountinfo
)
256 return &m
->parameters_proc_self_mountinfo
;
258 return get_mount_parameters_fragment(m
);
261 static int mount_add_mount_dependencies(Mount
*m
) {
270 if (!path_equal(m
->where
, "/")) {
271 _cleanup_free_
char *parent
= NULL
;
273 /* Adds in links to other mount points that might lie further up in the hierarchy */
275 parent
= dirname_malloc(m
->where
);
279 r
= unit_require_mounts_for(UNIT(m
), parent
, UNIT_DEPENDENCY_IMPLICIT
);
284 /* Adds in dependencies to other mount points that might be needed for the source path (if this is a bind mount
285 * or a loop mount) to be available. */
286 pm
= get_mount_parameters_fragment(m
);
287 if (pm
&& pm
->what
&&
288 path_is_absolute(pm
->what
) &&
289 (mount_is_bind(pm
) || mount_is_loop(pm
) || !mount_is_network(pm
))) {
291 r
= unit_require_mounts_for(UNIT(m
), pm
->what
, UNIT_DEPENDENCY_FILE
);
296 /* Adds in dependencies to other units that use this path or paths further down in the hierarchy */
297 s
= manager_get_units_requiring_mounts_for(UNIT(m
)->manager
, m
->where
);
298 SET_FOREACH(other
, s
, i
) {
300 if (other
->load_state
!= UNIT_LOADED
)
303 if (other
== UNIT(m
))
306 r
= unit_add_dependency(other
, UNIT_AFTER
, UNIT(m
), true, UNIT_DEPENDENCY_PATH
);
310 if (UNIT(m
)->fragment_path
) {
311 /* If we have fragment configuration, then make this dependency required */
312 r
= unit_add_dependency(other
, UNIT_REQUIRES
, UNIT(m
), true, UNIT_DEPENDENCY_PATH
);
321 static int mount_add_device_dependencies(Mount
*m
) {
322 bool device_wants_mount
;
323 UnitDependencyMask mask
;
330 p
= get_mount_parameters(m
);
337 if (mount_is_bind(p
))
340 if (!is_device_path(p
->what
))
343 /* /dev/root is a really weird thing, it's not a real device,
344 * but just a path the kernel exports for the root file system
345 * specified on the kernel command line. Ignore it here. */
346 if (path_equal(p
->what
, "/dev/root"))
349 if (path_equal(m
->where
, "/"))
353 mount_is_auto(p
) && !mount_is_automount(p
) && MANAGER_IS_SYSTEM(UNIT(m
)->manager
);
355 /* Mount units from /proc/self/mountinfo are not bound to devices
356 * by default since they're subject to races when devices are
357 * unplugged. But the user can still force this dep with an
358 * appropriate option (or udev property) so the mount units are
359 * automatically stopped when the device disappears suddenly. */
360 dep
= mount_is_bound_to_device(m
) ? UNIT_BINDS_TO
: UNIT_REQUIRES
;
362 mask
= m
->from_fragment
? UNIT_DEPENDENCY_FILE
: UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
;
364 r
= unit_add_node_dependency(UNIT(m
), p
->what
, device_wants_mount
, dep
, mask
);
371 static int mount_add_quota_dependencies(Mount
*m
) {
372 UnitDependencyMask mask
;
378 if (!MANAGER_IS_SYSTEM(UNIT(m
)->manager
))
381 p
= get_mount_parameters_fragment(m
);
385 if (!mount_needs_quota(p
))
388 mask
= m
->from_fragment
? UNIT_DEPENDENCY_FILE
: UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
;
390 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTACHECK_SERVICE
, true, mask
);
394 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTAON_SERVICE
, true, mask
);
401 static bool mount_is_extrinsic(Mount
*m
) {
405 /* Returns true for all units that are "magic" and should be excluded from the usual start-up and shutdown
406 * dependencies. We call them "extrinsic" here, as they are generally mounted outside of the systemd dependency
407 * logic. We shouldn't attempt to manage them ourselves but it's fine if the user operates on them with us. */
409 if (!MANAGER_IS_SYSTEM(UNIT(m
)->manager
)) /* We only automatically manage mounts if we are in system mode */
412 if (PATH_IN_SET(m
->where
, /* Don't bother with the OS data itself */
417 if (PATH_STARTSWITH_SET(m
->where
,
418 "/run/initramfs", /* This should stay around from before we boot until after we shutdown */
419 "/proc", /* All of this is API VFS */
420 "/sys", /* … dito … */
421 "/dev")) /* … dito … */
424 /* If this is an initrd mount, and we are not in the initrd, then leave this around forever, too. */
425 p
= get_mount_parameters(m
);
426 if (p
&& fstab_test_option(p
->options
, "x-initrd.mount\0") && !in_initrd())
432 static int mount_add_default_dependencies(Mount
*m
) {
433 const char *after
, *before
;
434 UnitDependencyMask mask
;
440 if (!UNIT(m
)->default_dependencies
)
443 /* We do not add any default dependencies to /, /usr or /run/initramfs/, since they are guaranteed to stay
444 * mounted the whole time, since our system is on it. Also, don't bother with anything mounted below virtual
445 * file systems, it's also going to be virtual, and hence not worth the effort. */
446 if (mount_is_extrinsic(m
))
449 p
= get_mount_parameters(m
);
453 mask
= m
->from_fragment
? UNIT_DEPENDENCY_FILE
: UNIT_DEPENDENCY_MOUNTINFO_DEFAULT
;
455 if (mount_is_network(p
)) {
456 /* We order ourselves after network.target. This is
457 * primarily useful at shutdown: services that take
458 * down the network should order themselves before
459 * network.target, so that they are shut down only
460 * after this mount unit is stopped. */
462 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, SPECIAL_NETWORK_TARGET
, true, mask
);
466 /* We pull in network-online.target, and order
467 * ourselves after it. This is useful at start-up to
468 * actively pull in tools that want to be started
469 * before we start mounting network file systems, and
470 * whose purpose it is to delay this until the network
473 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_WANTS
, UNIT_AFTER
, SPECIAL_NETWORK_ONLINE_TARGET
, true, mask
);
477 after
= SPECIAL_REMOTE_FS_PRE_TARGET
;
478 before
= SPECIAL_REMOTE_FS_TARGET
;
480 after
= SPECIAL_LOCAL_FS_PRE_TARGET
;
481 before
= SPECIAL_LOCAL_FS_TARGET
;
484 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_BEFORE
, before
, true, mask
);
488 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, after
, true, mask
);
492 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, true, mask
);
496 /* If this is a tmpfs mount then we have to unmount it before we try to deactivate swaps */
497 if (streq_ptr(p
->fstype
, "tmpfs")) {
498 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, SPECIAL_SWAP_TARGET
, true, mask
);
506 static int mount_verify(Mount
*m
) {
507 _cleanup_free_
char *e
= NULL
;
513 if (UNIT(m
)->load_state
!= UNIT_LOADED
)
516 if (!m
->from_fragment
&& !m
->from_proc_self_mountinfo
&& !UNIT(m
)->perpetual
)
519 r
= unit_name_from_path(m
->where
, ".mount", &e
);
521 return log_unit_error_errno(UNIT(m
), r
, "Failed to generate unit name from mount path: %m");
523 if (!unit_has_name(UNIT(m
), e
)) {
524 log_unit_error(UNIT(m
), "Where= setting doesn't match unit name. Refusing.");
528 if (mount_point_is_api(m
->where
) || mount_point_ignore(m
->where
)) {
529 log_unit_error(UNIT(m
), "Cannot create mount unit for API file system %s. Refusing.", m
->where
);
533 p
= get_mount_parameters_fragment(m
);
535 log_unit_error(UNIT(m
), "What= setting is missing. Refusing.");
539 if (m
->exec_context
.pam_name
&& m
->kill_context
.kill_mode
!= KILL_CONTROL_GROUP
) {
540 log_unit_error(UNIT(m
), "Unit has PAM enabled. Kill mode must be set to control-group'. Refusing.");
547 static int mount_add_extras(Mount
*m
) {
553 /* Note: this call might be called after we already have been loaded once (and even when it has already been
554 * activated), in case data from /proc/self/mountinfo has changed. This means all code here needs to be ready
555 * to run with an already set up unit. */
557 if (u
->fragment_path
)
558 m
->from_fragment
= true;
561 r
= unit_name_to_path(u
->id
, &m
->where
);
566 path_simplify(m
->where
, false);
568 if (!u
->description
) {
569 r
= unit_set_description(u
, m
->where
);
574 r
= mount_add_device_dependencies(m
);
578 r
= mount_add_mount_dependencies(m
);
582 r
= mount_add_quota_dependencies(m
);
586 r
= unit_patch_contexts(u
);
590 r
= unit_add_exec_dependencies(u
, &m
->exec_context
);
594 r
= unit_set_default_slice(u
);
598 r
= mount_add_default_dependencies(m
);
605 static int mount_load_root_mount(Unit
*u
) {
608 if (!unit_has_name(u
, SPECIAL_ROOT_MOUNT
))
612 u
->default_dependencies
= false;
614 /* The stdio/kmsg bridge socket is on /, in order to avoid a dep loop, don't use kmsg logging for -.mount */
615 MOUNT(u
)->exec_context
.std_output
= EXEC_OUTPUT_NULL
;
616 MOUNT(u
)->exec_context
.std_input
= EXEC_INPUT_NULL
;
619 u
->description
= strdup("Root Mount");
624 static int mount_load(Unit
*u
) {
629 assert(u
->load_state
== UNIT_STUB
);
631 r
= mount_load_root_mount(u
);
633 if (m
->from_proc_self_mountinfo
|| u
->perpetual
)
634 q
= unit_load_fragment_and_dropin_optional(u
);
636 q
= unit_load_fragment_and_dropin(u
);
638 /* Add in some extras. Note we do this in all cases (even if we failed to load the unit) when announced by the
639 * kernel, because we need some things to be set up no matter what when the kernel establishes a mount and thus
640 * we need to update the state in our unit to track it. After all, consider that we don't allow changing the
641 * 'slice' field for a unit once it is active. */
642 if (u
->load_state
== UNIT_LOADED
|| m
->from_proc_self_mountinfo
|| u
->perpetual
)
643 w
= mount_add_extras(m
);
654 return mount_verify(m
);
657 static void mount_set_state(Mount
*m
, MountState state
) {
658 MountState old_state
;
659 int was_pending
, is_pending
;
662 if (m
->state
!= state
)
663 bus_unit_send_pending_change_signal(UNIT(m
), false);
665 old_state
= m
->state
;
668 was_pending
= !IN_SET(old_state
, MOUNT_DEAD
, MOUNT_MOUNTED
, MOUNT_FAILED
);
669 is_pending
= !IN_SET(state
, MOUNT_DEAD
, MOUNT_MOUNTED
, MOUNT_FAILED
);
671 if (was_pending
&& !is_pending
) {
672 assert(UNIT(m
)->manager
->mount_pending_count
> 0);
673 UNIT(m
)->manager
->mount_pending_count
--;
676 if (is_pending
&& !was_pending
)
677 UNIT(m
)->manager
->mount_pending_count
++;
679 if (!MOUNT_STATE_WITH_PROCESS(state
)) {
680 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
681 mount_unwatch_control_pid(m
);
682 m
->control_command
= NULL
;
683 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
686 if (state
!= old_state
)
687 log_unit_debug(UNIT(m
), "Changed %s -> %s", mount_state_to_string(old_state
), mount_state_to_string(state
));
689 unit_notify(UNIT(m
), state_translation_table
[old_state
], state_translation_table
[state
],
690 m
->reload_result
== MOUNT_SUCCESS
? 0 : UNIT_NOTIFY_RELOAD_FAILURE
);
693 static int mount_coldplug(Unit
*u
) {
695 MountState new_state
= MOUNT_DEAD
;
699 assert(m
->state
== MOUNT_DEAD
);
701 if (m
->deserialized_state
!= m
->state
)
702 new_state
= m
->deserialized_state
;
703 else if (m
->from_proc_self_mountinfo
)
704 new_state
= MOUNT_MOUNTED
;
706 if (new_state
== m
->state
)
709 if (m
->control_pid
> 0 &&
710 pid_is_unwaited(m
->control_pid
) &&
711 MOUNT_STATE_WITH_PROCESS(new_state
)) {
713 r
= unit_watch_pid(UNIT(m
), m
->control_pid
);
717 r
= mount_arm_timer(m
, usec_add(u
->state_change_timestamp
.monotonic
, m
->timeout_usec
));
722 if (!IN_SET(new_state
, MOUNT_DEAD
, MOUNT_FAILED
)) {
723 (void) unit_setup_dynamic_creds(u
);
724 (void) unit_setup_exec_runtime(u
);
727 mount_set_state(m
, new_state
);
731 static void mount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
732 char buf
[FORMAT_TIMESPAN_MAX
];
739 p
= get_mount_parameters(m
);
742 "%sMount State: %s\n"
746 "%sFile System Type: %s\n"
748 "%sFrom /proc/self/mountinfo: %s\n"
749 "%sFrom fragment: %s\n"
751 "%sDirectoryMode: %04o\n"
752 "%sSloppyOptions: %s\n"
753 "%sLazyUnmount: %s\n"
754 "%sForceUnmount: %s\n"
756 prefix
, mount_state_to_string(m
->state
),
757 prefix
, mount_result_to_string(m
->result
),
759 prefix
, p
? strna(p
->what
) : "n/a",
760 prefix
, p
? strna(p
->fstype
) : "n/a",
761 prefix
, p
? strna(p
->options
) : "n/a",
762 prefix
, yes_no(m
->from_proc_self_mountinfo
),
763 prefix
, yes_no(m
->from_fragment
),
764 prefix
, yes_no(mount_is_extrinsic(m
)),
765 prefix
, m
->directory_mode
,
766 prefix
, yes_no(m
->sloppy_options
),
767 prefix
, yes_no(m
->lazy_unmount
),
768 prefix
, yes_no(m
->force_unmount
),
769 prefix
, format_timespan(buf
, sizeof(buf
), m
->timeout_usec
, USEC_PER_SEC
));
771 if (m
->control_pid
> 0)
773 "%sControl PID: "PID_FMT
"\n",
774 prefix
, m
->control_pid
);
776 exec_context_dump(&m
->exec_context
, f
, prefix
);
777 kill_context_dump(&m
->kill_context
, f
, prefix
);
778 cgroup_context_dump(&m
->cgroup_context
, f
, prefix
);
781 static int mount_spawn(Mount
*m
, ExecCommand
*c
, pid_t
*_pid
) {
783 _cleanup_(exec_params_clear
) ExecParameters exec_params
= {
784 .flags
= EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
,
797 r
= unit_prepare_exec(UNIT(m
));
801 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->timeout_usec
));
805 r
= unit_set_exec_params(UNIT(m
), &exec_params
);
809 r
= exec_spawn(UNIT(m
),
819 r
= unit_watch_pid(UNIT(m
), pid
);
821 /* FIXME: we need to do something here */
829 static void mount_enter_dead(Mount
*m
, MountResult f
) {
832 if (m
->result
== MOUNT_SUCCESS
)
835 unit_log_result(UNIT(m
), m
->result
== MOUNT_SUCCESS
, mount_result_to_string(m
->result
));
836 mount_set_state(m
, m
->result
!= MOUNT_SUCCESS
? MOUNT_FAILED
: MOUNT_DEAD
);
838 m
->exec_runtime
= exec_runtime_unref(m
->exec_runtime
, true);
840 exec_context_destroy_runtime_directory(&m
->exec_context
, UNIT(m
)->manager
->prefix
[EXEC_DIRECTORY_RUNTIME
]);
842 unit_unref_uid_gid(UNIT(m
), true);
844 dynamic_creds_destroy(&m
->dynamic_creds
);
847 static void mount_enter_mounted(Mount
*m
, MountResult f
) {
850 if (m
->result
== MOUNT_SUCCESS
)
853 mount_set_state(m
, MOUNT_MOUNTED
);
856 static void mount_enter_dead_or_mounted(Mount
*m
, MountResult f
) {
859 /* Enter DEAD or MOUNTED state, depending on what the kernel currently says about the mount point. We use this
860 * whenever we executed an operation, so that our internal state reflects what the kernel says again, after all
861 * ultimately we just mirror the kernel's internal state on this. */
863 if (m
->from_proc_self_mountinfo
)
864 mount_enter_mounted(m
, f
);
866 mount_enter_dead(m
, f
);
869 static int state_to_kill_operation(MountState state
) {
872 case MOUNT_REMOUNTING_SIGTERM
:
873 case MOUNT_UNMOUNTING_SIGTERM
:
874 return KILL_TERMINATE
;
876 case MOUNT_REMOUNTING_SIGKILL
:
877 case MOUNT_UNMOUNTING_SIGKILL
:
881 return _KILL_OPERATION_INVALID
;
885 static void mount_enter_signal(Mount
*m
, MountState state
, MountResult f
) {
890 if (m
->result
== MOUNT_SUCCESS
)
893 r
= unit_kill_context(
896 state_to_kill_operation(state
),
904 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->timeout_usec
));
908 mount_set_state(m
, state
);
909 } else if (state
== MOUNT_REMOUNTING_SIGTERM
&& m
->kill_context
.send_sigkill
)
910 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
911 else if (IN_SET(state
, MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
))
912 mount_enter_mounted(m
, MOUNT_SUCCESS
);
913 else if (state
== MOUNT_UNMOUNTING_SIGTERM
&& m
->kill_context
.send_sigkill
)
914 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
916 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
921 log_unit_warning_errno(UNIT(m
), r
, "Failed to kill processes: %m");
922 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
925 static void mount_enter_unmounting(Mount
*m
) {
930 /* Start counting our attempts */
931 if (!IN_SET(m
->state
,
933 MOUNT_UNMOUNTING_SIGTERM
,
934 MOUNT_UNMOUNTING_SIGKILL
))
935 m
->n_retry_umount
= 0;
937 m
->control_command_id
= MOUNT_EXEC_UNMOUNT
;
938 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_UNMOUNT
;
940 r
= exec_command_set(m
->control_command
, UMOUNT_PATH
, m
->where
, "-c", NULL
);
941 if (r
>= 0 && m
->lazy_unmount
)
942 r
= exec_command_append(m
->control_command
, "-l", NULL
);
943 if (r
>= 0 && m
->force_unmount
)
944 r
= exec_command_append(m
->control_command
, "-f", NULL
);
948 mount_unwatch_control_pid(m
);
950 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
954 mount_set_state(m
, MOUNT_UNMOUNTING
);
959 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'umount' task: %m");
960 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
963 static void mount_enter_mounting(Mount
*m
) {
969 r
= unit_fail_if_noncanonical(UNIT(m
), m
->where
);
973 (void) mkdir_p_label(m
->where
, m
->directory_mode
);
975 unit_warn_if_dir_nonempty(UNIT(m
), m
->where
);
976 unit_warn_leftover_processes(UNIT(m
));
978 m
->control_command_id
= MOUNT_EXEC_MOUNT
;
979 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_MOUNT
;
981 /* Create the source directory for bind-mounts if needed */
982 p
= get_mount_parameters_fragment(m
);
983 if (p
&& mount_is_bind(p
))
984 (void) mkdir_p_label(p
->what
, m
->directory_mode
);
987 _cleanup_free_
char *opts
= NULL
;
989 r
= fstab_filter_options(p
->options
, "nofail\0" "noauto\0" "auto\0", NULL
, NULL
, &opts
);
993 r
= exec_command_set(m
->control_command
, MOUNT_PATH
, p
->what
, m
->where
, NULL
);
994 if (r
>= 0 && m
->sloppy_options
)
995 r
= exec_command_append(m
->control_command
, "-s", NULL
);
996 if (r
>= 0 && p
->fstype
)
997 r
= exec_command_append(m
->control_command
, "-t", p
->fstype
, NULL
);
998 if (r
>= 0 && !isempty(opts
))
999 r
= exec_command_append(m
->control_command
, "-o", opts
, NULL
);
1005 mount_unwatch_control_pid(m
);
1007 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
1011 mount_set_state(m
, MOUNT_MOUNTING
);
1016 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'mount' task: %m");
1017 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
1020 static void mount_set_reload_result(Mount
*m
, MountResult result
) {
1023 /* Only store the first error we encounter */
1024 if (m
->reload_result
!= MOUNT_SUCCESS
)
1027 m
->reload_result
= result
;
1030 static void mount_enter_remounting(Mount
*m
) {
1036 /* Reset reload result when we are about to start a new remount operation */
1037 m
->reload_result
= MOUNT_SUCCESS
;
1039 m
->control_command_id
= MOUNT_EXEC_REMOUNT
;
1040 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_REMOUNT
;
1042 p
= get_mount_parameters_fragment(m
);
1047 o
= strjoina("remount,", p
->options
);
1051 r
= exec_command_set(m
->control_command
, MOUNT_PATH
,
1054 if (r
>= 0 && m
->sloppy_options
)
1055 r
= exec_command_append(m
->control_command
, "-s", NULL
);
1056 if (r
>= 0 && p
->fstype
)
1057 r
= exec_command_append(m
->control_command
, "-t", p
->fstype
, NULL
);
1063 mount_unwatch_control_pid(m
);
1065 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
1069 mount_set_state(m
, MOUNT_REMOUNTING
);
1074 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'remount' task: %m");
1075 mount_set_reload_result(m
, MOUNT_FAILURE_RESOURCES
);
1076 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1079 static void mount_cycle_clear(Mount
*m
) {
1082 /* Clear all state we shall forget for this new cycle */
1084 m
->result
= MOUNT_SUCCESS
;
1085 m
->reload_result
= MOUNT_SUCCESS
;
1086 exec_command_reset_status_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
1087 UNIT(m
)->reset_accounting
= true;
1090 static int mount_start(Unit
*u
) {
1091 Mount
*m
= MOUNT(u
);
1096 /* We cannot fulfill this request right now, try again later
1098 if (IN_SET(m
->state
,
1100 MOUNT_UNMOUNTING_SIGTERM
,
1101 MOUNT_UNMOUNTING_SIGKILL
))
1104 /* Already on it! */
1105 if (m
->state
== MOUNT_MOUNTING
)
1108 assert(IN_SET(m
->state
, MOUNT_DEAD
, MOUNT_FAILED
));
1110 r
= unit_start_limit_test(u
);
1112 mount_enter_dead(m
, MOUNT_FAILURE_START_LIMIT_HIT
);
1116 r
= unit_acquire_invocation_id(u
);
1120 mount_cycle_clear(m
);
1121 mount_enter_mounting(m
);
1126 static int mount_stop(Unit
*u
) {
1127 Mount
*m
= MOUNT(u
);
1133 case MOUNT_UNMOUNTING
:
1134 case MOUNT_UNMOUNTING_SIGKILL
:
1135 case MOUNT_UNMOUNTING_SIGTERM
:
1139 case MOUNT_MOUNTING
:
1140 case MOUNT_MOUNTING_DONE
:
1141 case MOUNT_REMOUNTING
:
1142 /* If we are still waiting for /bin/mount, we go directly into kill mode. */
1143 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_SUCCESS
);
1146 case MOUNT_REMOUNTING_SIGTERM
:
1147 /* If we are already waiting for a hung remount, convert this to the matching unmounting state */
1148 mount_set_state(m
, MOUNT_UNMOUNTING_SIGTERM
);
1151 case MOUNT_REMOUNTING_SIGKILL
:
1153 mount_set_state(m
, MOUNT_UNMOUNTING_SIGKILL
);
1157 mount_enter_unmounting(m
);
1161 assert_not_reached("Unexpected state.");
1165 static int mount_reload(Unit
*u
) {
1166 Mount
*m
= MOUNT(u
);
1169 assert(m
->state
== MOUNT_MOUNTED
);
1171 mount_enter_remounting(m
);
1176 static int mount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1177 Mount
*m
= MOUNT(u
);
1183 (void) serialize_item(f
, "state", mount_state_to_string(m
->state
));
1184 (void) serialize_item(f
, "result", mount_result_to_string(m
->result
));
1185 (void) serialize_item(f
, "reload-result", mount_result_to_string(m
->reload_result
));
1186 (void) serialize_item_format(f
, "n-retry-umount", "%u", m
->n_retry_umount
);
1188 if (m
->control_pid
> 0)
1189 (void) serialize_item_format(f
, "control-pid", PID_FMT
, m
->control_pid
);
1191 if (m
->control_command_id
>= 0)
1192 (void) serialize_item(f
, "control-command", mount_exec_command_to_string(m
->control_command_id
));
1197 static int mount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1198 Mount
*m
= MOUNT(u
);
1206 if (streq(key
, "state")) {
1209 if ((state
= mount_state_from_string(value
)) < 0)
1210 log_unit_debug(u
, "Failed to parse state value: %s", value
);
1212 m
->deserialized_state
= state
;
1214 } else if (streq(key
, "result")) {
1217 f
= mount_result_from_string(value
);
1219 log_unit_debug(u
, "Failed to parse result value: %s", value
);
1220 else if (f
!= MOUNT_SUCCESS
)
1223 } else if (streq(key
, "reload-result")) {
1226 f
= mount_result_from_string(value
);
1228 log_unit_debug(u
, "Failed to parse reload result value: %s", value
);
1229 else if (f
!= MOUNT_SUCCESS
)
1230 m
->reload_result
= f
;
1232 } else if (streq(key
, "n-retry-umount")) {
1234 r
= safe_atou(value
, &m
->n_retry_umount
);
1236 log_unit_debug(u
, "Failed to parse n-retry-umount value: %s", value
);
1238 } else if (streq(key
, "control-pid")) {
1240 if (parse_pid(value
, &m
->control_pid
) < 0)
1241 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
1243 } else if (streq(key
, "control-command")) {
1244 MountExecCommand id
;
1246 id
= mount_exec_command_from_string(value
);
1248 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
1250 m
->control_command_id
= id
;
1251 m
->control_command
= m
->exec_command
+ id
;
1254 log_unit_debug(u
, "Unknown serialization key: %s", key
);
1259 _pure_
static UnitActiveState
mount_active_state(Unit
*u
) {
1262 return state_translation_table
[MOUNT(u
)->state
];
1265 _pure_
static const char *mount_sub_state_to_string(Unit
*u
) {
1268 return mount_state_to_string(MOUNT(u
)->state
);
1271 _pure_
static bool mount_may_gc(Unit
*u
) {
1272 Mount
*m
= MOUNT(u
);
1276 if (m
->from_proc_self_mountinfo
)
1282 static void mount_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1283 Mount
*m
= MOUNT(u
);
1289 if (pid
!= m
->control_pid
)
1294 if (is_clean_exit(code
, status
, EXIT_CLEAN_COMMAND
, NULL
))
1296 else if (code
== CLD_EXITED
)
1297 f
= MOUNT_FAILURE_EXIT_CODE
;
1298 else if (code
== CLD_KILLED
)
1299 f
= MOUNT_FAILURE_SIGNAL
;
1300 else if (code
== CLD_DUMPED
)
1301 f
= MOUNT_FAILURE_CORE_DUMP
;
1303 assert_not_reached("Unknown code");
1305 if (IN_SET(m
->state
, MOUNT_REMOUNTING
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_REMOUNTING_SIGTERM
))
1306 mount_set_reload_result(m
, f
);
1307 else if (m
->result
== MOUNT_SUCCESS
)
1310 if (m
->control_command
) {
1311 exec_status_exit(&m
->control_command
->exec_status
, &m
->exec_context
, pid
, code
, status
);
1313 m
->control_command
= NULL
;
1314 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
1317 unit_log_process_exit(
1318 u
, f
== MOUNT_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
1320 mount_exec_command_to_string(m
->control_command_id
),
1323 /* Note that due to the io event priority logic, we can be sure the new mountinfo is loaded
1324 * before we process the SIGCHLD for the mount command. */
1328 case MOUNT_MOUNTING
:
1329 /* Our mount point has not appeared in mountinfo. Something went wrong. */
1331 if (f
== MOUNT_SUCCESS
) {
1332 /* Either /bin/mount has an unexpected definition of success,
1333 * or someone raced us and we lost. */
1334 log_unit_warning(UNIT(m
), "Mount process finished, but there is no mount.");
1335 f
= MOUNT_FAILURE_PROTOCOL
;
1337 mount_enter_dead(m
, f
);
1340 case MOUNT_MOUNTING_DONE
:
1341 mount_enter_mounted(m
, f
);
1344 case MOUNT_REMOUNTING
:
1345 case MOUNT_REMOUNTING_SIGTERM
:
1346 case MOUNT_REMOUNTING_SIGKILL
:
1347 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1350 case MOUNT_UNMOUNTING
:
1352 if (f
== MOUNT_SUCCESS
&& m
->from_proc_self_mountinfo
) {
1354 /* Still a mount point? If so, let's try again. Most likely there were multiple mount points
1355 * stacked on top of each other. We might exceed the timeout specified by the user overall,
1356 * but we will stop as soon as any one umount times out. */
1358 if (m
->n_retry_umount
< RETRY_UMOUNT_MAX
) {
1359 log_unit_debug(u
, "Mount still present, trying again.");
1360 m
->n_retry_umount
++;
1361 mount_enter_unmounting(m
);
1363 log_unit_warning(u
, "Mount still present after %u attempts to unmount, giving up.", m
->n_retry_umount
);
1364 mount_enter_mounted(m
, f
);
1367 mount_enter_dead_or_mounted(m
, f
);
1371 case MOUNT_UNMOUNTING_SIGKILL
:
1372 case MOUNT_UNMOUNTING_SIGTERM
:
1373 mount_enter_dead_or_mounted(m
, f
);
1377 assert_not_reached("Uh, control process died at wrong time.");
1380 /* Notify clients about changed exit status */
1381 unit_add_to_dbus_queue(u
);
1384 static int mount_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
1385 Mount
*m
= MOUNT(userdata
);
1388 assert(m
->timer_event_source
== source
);
1392 case MOUNT_MOUNTING
:
1393 case MOUNT_MOUNTING_DONE
:
1394 log_unit_warning(UNIT(m
), "Mounting timed out. Terminating.");
1395 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1398 case MOUNT_REMOUNTING
:
1399 log_unit_warning(UNIT(m
), "Remounting timed out. Terminating remount process.");
1400 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1401 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGTERM
, MOUNT_SUCCESS
);
1404 case MOUNT_REMOUNTING_SIGTERM
:
1405 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1407 if (m
->kill_context
.send_sigkill
) {
1408 log_unit_warning(UNIT(m
), "Remounting timed out. Killing.");
1409 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
1411 log_unit_warning(UNIT(m
), "Remounting timed out. Skipping SIGKILL. Ignoring.");
1412 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1416 case MOUNT_REMOUNTING_SIGKILL
:
1417 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1419 log_unit_warning(UNIT(m
), "Mount process still around after SIGKILL. Ignoring.");
1420 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1423 case MOUNT_UNMOUNTING
:
1424 log_unit_warning(UNIT(m
), "Unmounting timed out. Terminating.");
1425 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1428 case MOUNT_UNMOUNTING_SIGTERM
:
1429 if (m
->kill_context
.send_sigkill
) {
1430 log_unit_warning(UNIT(m
), "Mount process timed out. Killing.");
1431 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_FAILURE_TIMEOUT
);
1433 log_unit_warning(UNIT(m
), "Mount process timed out. Skipping SIGKILL. Ignoring.");
1434 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1438 case MOUNT_UNMOUNTING_SIGKILL
:
1439 log_unit_warning(UNIT(m
), "Mount process still around after SIGKILL. Ignoring.");
1440 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1444 assert_not_reached("Timeout at wrong time.");
1450 static int update_parameters_proc_self_mount_info(
1453 const char *options
,
1454 const char *fstype
) {
1459 p
= &m
->parameters_proc_self_mountinfo
;
1461 r
= free_and_strdup(&p
->what
, what
);
1465 q
= free_and_strdup(&p
->options
, options
);
1469 w
= free_and_strdup(&p
->fstype
, fstype
);
1473 return r
> 0 || q
> 0 || w
> 0;
1476 static int mount_setup_new_unit(
1481 const char *options
,
1483 MountProcFlags
*ret_flags
,
1486 _cleanup_(unit_freep
) Unit
*u
= NULL
;
1494 r
= unit_new_for_name(m
, sizeof(Mount
), name
, &u
);
1498 r
= free_and_strdup(&u
->source_path
, "/proc/self/mountinfo");
1502 r
= free_and_strdup(&MOUNT(u
)->where
, where
);
1506 r
= update_parameters_proc_self_mount_info(MOUNT(u
), what
, options
, fstype
);
1510 /* This unit was generated because /proc/self/mountinfo reported it. Remember this, so that by the time we load
1511 * the unit file for it (and thus add in extra deps right after) we know what source to attributes the deps
1513 MOUNT(u
)->from_proc_self_mountinfo
= true;
1515 /* We have only allocated the stub now, let's enqueue this unit for loading now, so that everything else is
1517 unit_add_to_load_queue(u
);
1519 *ret_flags
= MOUNT_PROC_IS_MOUNTED
| MOUNT_PROC_JUST_MOUNTED
| MOUNT_PROC_JUST_CHANGED
;
1524 static int mount_setup_existing_unit(
1528 const char *options
,
1530 MountProcFlags
*ret_flags
) {
1532 MountProcFlags flags
= MOUNT_PROC_IS_MOUNTED
;
1538 if (!MOUNT(u
)->where
) {
1539 MOUNT(u
)->where
= strdup(where
);
1540 if (!MOUNT(u
)->where
)
1544 r
= update_parameters_proc_self_mount_info(MOUNT(u
), what
, options
, fstype
);
1548 flags
|= MOUNT_PROC_JUST_CHANGED
;
1550 if (!MOUNT(u
)->from_proc_self_mountinfo
) {
1551 flags
|= MOUNT_PROC_JUST_MOUNTED
;
1552 MOUNT(u
)->from_proc_self_mountinfo
= true;
1555 if (IN_SET(u
->load_state
, UNIT_NOT_FOUND
, UNIT_BAD_SETTING
, UNIT_ERROR
)) {
1556 /* The unit was previously not found or otherwise not loaded. Now that the unit shows up in
1557 * /proc/self/mountinfo we should reconsider it this, hence set it to UNIT_LOADED. */
1558 u
->load_state
= UNIT_LOADED
;
1561 flags
|= MOUNT_PROC_JUST_CHANGED
;
1564 if (FLAGS_SET(flags
, MOUNT_PROC_JUST_CHANGED
)) {
1565 /* If things changed, then make sure that all deps are regenerated. Let's
1566 * first remove all automatic deps, and then add in the new ones. */
1568 unit_remove_dependencies(u
, UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
);
1570 r
= mount_add_extras(MOUNT(u
));
1579 static int mount_setup_unit(
1583 const char *options
,
1587 _cleanup_free_
char *e
= NULL
;
1588 MountProcFlags flags
;
1598 /* Ignore API mount points. They should never be referenced in
1599 * dependencies ever. */
1600 if (mount_point_is_api(where
) || mount_point_ignore(where
))
1603 if (streq(fstype
, "autofs"))
1606 /* probably some kind of swap, ignore */
1607 if (!is_path(where
))
1610 r
= unit_name_from_path(where
, ".mount", &e
);
1612 return log_error_errno(r
, "Failed to generate unit name from path '%s': %m", where
);
1614 u
= manager_get_unit(m
, e
);
1616 r
= mount_setup_existing_unit(u
, what
, where
, options
, fstype
, &flags
);
1618 /* First time we see this mount point meaning that it's not been initiated by a mount unit but rather
1619 * by the sysadmin having called mount(8) directly. */
1620 r
= mount_setup_new_unit(m
, e
, what
, where
, options
, fstype
, &flags
, &u
);
1622 return log_warning_errno(r
, "Failed to set up mount unit: %m");
1624 /* If the mount changed properties or state, let's notify our clients */
1625 if (flags
& (MOUNT_PROC_JUST_CHANGED
|MOUNT_PROC_JUST_MOUNTED
))
1626 unit_add_to_dbus_queue(u
);
1629 MOUNT(u
)->proc_flags
= flags
;
1634 static int mount_load_proc_self_mountinfo(Manager
*m
, bool set_flags
) {
1635 _cleanup_(mnt_free_tablep
) struct libmnt_table
*t
= NULL
;
1636 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*i
= NULL
;
1641 t
= mnt_new_table();
1642 i
= mnt_new_iter(MNT_ITER_FORWARD
);
1646 r
= mnt_table_parse_mtab(t
, NULL
);
1648 return log_error_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
1651 struct libmnt_fs
*fs
;
1652 const char *device
, *path
, *options
, *fstype
;
1653 _cleanup_free_
char *d
= NULL
, *p
= NULL
;
1656 k
= mnt_table_next_fs(t
, i
, &fs
);
1660 return log_error_errno(k
, "Failed to get next entry from /proc/self/mountinfo: %m");
1662 device
= mnt_fs_get_source(fs
);
1663 path
= mnt_fs_get_target(fs
);
1664 options
= mnt_fs_get_options(fs
);
1665 fstype
= mnt_fs_get_fstype(fs
);
1667 if (!device
|| !path
)
1670 if (cunescape(device
, UNESCAPE_RELAX
, &d
) < 0)
1673 if (cunescape(path
, UNESCAPE_RELAX
, &p
) < 0)
1676 device_found_node(m
, d
, DEVICE_FOUND_MOUNT
, DEVICE_FOUND_MOUNT
);
1678 (void) mount_setup_unit(m
, d
, p
, options
, fstype
, set_flags
);
1684 static void mount_shutdown(Manager
*m
) {
1687 m
->mount_timeout_source
= sd_event_source_unref(m
->mount_timeout_source
);
1688 m
->mount_event_source
= sd_event_source_unref(m
->mount_event_source
);
1690 mnt_unref_monitor(m
->mount_monitor
);
1691 m
->mount_monitor
= NULL
;
1694 static int mount_get_timeout(Unit
*u
, usec_t
*timeout
) {
1695 Mount
*m
= MOUNT(u
);
1699 if (!m
->timer_event_source
)
1702 r
= sd_event_source_get_time(m
->timer_event_source
, &t
);
1705 if (t
== USEC_INFINITY
)
1712 static void mount_enumerate_perpetual(Manager
*m
) {
1718 /* Whatever happens, we know for sure that the root directory is around, and cannot go away. Let's
1719 * unconditionally synthesize it here and mark it as perpetual. */
1721 u
= manager_get_unit(m
, SPECIAL_ROOT_MOUNT
);
1723 r
= unit_new_for_name(m
, sizeof(Mount
), SPECIAL_ROOT_MOUNT
, &u
);
1725 log_error_errno(r
, "Failed to allocate the special " SPECIAL_ROOT_MOUNT
" unit: %m");
1730 u
->perpetual
= true;
1731 MOUNT(u
)->deserialized_state
= MOUNT_MOUNTED
;
1733 unit_add_to_load_queue(u
);
1734 unit_add_to_dbus_queue(u
);
1737 static bool mount_is_mounted(Mount
*m
) {
1740 return UNIT(m
)->perpetual
|| FLAGS_SET(m
->proc_flags
, MOUNT_PROC_IS_MOUNTED
);
1743 static void mount_enumerate(Manager
*m
) {
1750 if (!m
->mount_monitor
) {
1753 m
->mount_monitor
= mnt_new_monitor();
1754 if (!m
->mount_monitor
) {
1759 r
= mnt_monitor_enable_kernel(m
->mount_monitor
, 1);
1761 log_error_errno(r
, "Failed to enable watching of kernel mount events: %m");
1765 r
= mnt_monitor_enable_userspace(m
->mount_monitor
, 1, NULL
);
1767 log_error_errno(r
, "Failed to enable watching of userspace mount events: %m");
1771 /* mnt_unref_monitor() will close the fd */
1772 fd
= r
= mnt_monitor_get_fd(m
->mount_monitor
);
1774 log_error_errno(r
, "Failed to acquire watch file descriptor: %m");
1778 r
= sd_event_add_io(m
->event
, &m
->mount_event_source
, fd
, EPOLLIN
, mount_dispatch_io
, m
);
1780 log_error_errno(r
, "Failed to watch mount file descriptor: %m");
1784 r
= sd_event_source_set_priority(m
->mount_event_source
, SD_EVENT_PRIORITY_NORMAL
-10);
1786 log_error_errno(r
, "Failed to adjust mount watch priority: %m");
1790 (void) sd_event_source_set_description(m
->mount_event_source
, "mount-monitor-dispatch");
1793 r
= mount_load_proc_self_mountinfo(m
, false);
1803 static void mount_process_proc_self_mountinfo(Manager
*m
);
1805 static int mount_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
1806 Manager
*m
= userdata
;
1808 usec_t next_read
= usec_add(m
->mount_last_read_usec
,
1809 m
->mount_last_duration_usec
* 10);
1811 /* If there are pending mounts initiated by systemd, then
1812 * we need to process changes promptly, otherwise we
1813 * rate limit re-reading the file.
1815 if (m
->mount_pending_count
== 0 &&
1816 now(CLOCK_MONOTONIC
) < next_read
) {
1817 /* The (current) API for getting mount events from the Linux kernel
1818 * involves getting a "something changed" notification, and then having
1819 * to re-read the entire /proc/self/mountinfo file. When there are lots
1820 * of mountpoints, this file is large and parsing it can take noticeable
1821 * time. As most of the file won't have changed, this can be seen as wasted time.
1822 * If there is a "mount storm" such as 1000 mount points being created
1823 * in quick succession, this will result in 1000 successive notification.
1824 * If we respond to every notification, we will do quadratically more work
1825 * than if we respond just once after all the notifications have arrived.
1826 * In this (pathological) case, a delay in scheduling would actually
1827 * improve throughput as we would combine notifications and parse
1828 * the file less often. We cannot expect the scheduler to notice
1829 * this pathology without help.
1830 * So when the rate of notifications means we are spending more than
1831 * 10% of real time handling them, we set a timer and stop listening
1832 * to notifications for a while.
1833 * If/when Linux provides an API which provides only details of what
1834 * has changed, this rate-limiting can be removed.
1837 r
= sd_event_source_set_enabled(source
, SD_EVENT_OFF
);
1839 log_warning_errno(r
, "Failed to disable monitoring of /proc/self/mounting, ignoring: %m");
1840 if (!m
->mount_timeout_source
) {
1841 r
= sd_event_add_time(m
->event
, &m
->mount_timeout_source
,
1845 mount_dispatch_proc_self_mountinfo_timer
,
1848 log_warning_errno(r
, "Failed to set timeout to reread /proc/self/mounting, ignoring: %m");
1854 assert(revents
& EPOLLIN
);
1856 if (fd
== mnt_monitor_get_fd(m
->mount_monitor
)) {
1857 bool rescan
= false;
1859 /* Drain all events and verify that the event is valid.
1861 * Note that libmount also monitors /run/mount mkdir if the
1862 * directory does not exist yet. The mkdir may generate event
1863 * which is irrelevant for us.
1865 * error: r < 0; valid: r == 0, false positive: rc == 1 */
1867 r
= mnt_monitor_next_change(m
->mount_monitor
, NULL
, NULL
);
1871 return log_error_errno(r
, "Failed to drain libmount events: %m");
1874 log_debug("libmount event [rescan: %s]", yes_no(rescan
));
1879 mount_process_proc_self_mountinfo(m
);
1883 static int mount_dispatch_proc_self_mountinfo_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
1884 Manager
*m
= userdata
;
1887 r
= sd_event_source_set_enabled(m
->mount_event_source
, SD_EVENT_ON
);
1889 log_warning_errno(r
, "Failed to reenable /proc/self/mountinfo monitor, ignoring: %m");
1890 m
->mount_timeout_source
= sd_event_source_unref(source
);
1891 mount_process_proc_self_mountinfo(m
);
1895 static void mount_process_proc_self_mountinfo(Manager
*m
) {
1896 _cleanup_set_free_free_ Set
*around
= NULL
, *gone
= NULL
;
1902 m
->mount_last_read_usec
= now(CLOCK_MONOTONIC
);
1903 /* If an error occurs, assume 10ms */
1904 m
->mount_last_duration_usec
= 10 * USEC_PER_MSEC
;
1906 r
= mount_load_proc_self_mountinfo(m
, true);
1908 /* Reset flags, just in case, for later calls */
1909 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
])
1910 MOUNT(u
)->proc_flags
= 0;
1915 manager_dispatch_load_queue(m
);
1917 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
]) {
1918 Mount
*mount
= MOUNT(u
);
1920 if (!mount_is_mounted(mount
)) {
1922 /* A mount point is not around right now. It
1923 * might be gone, or might never have
1926 if (mount
->from_proc_self_mountinfo
&&
1927 mount
->parameters_proc_self_mountinfo
.what
) {
1929 /* Remember that this device might just have disappeared */
1930 if (set_ensure_allocated(&gone
, &path_hash_ops
) < 0 ||
1931 set_put_strdup(gone
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
1932 log_oom(); /* we don't care too much about OOM here... */
1935 mount
->from_proc_self_mountinfo
= false;
1937 switch (mount
->state
) {
1940 /* This has just been unmounted by somebody else, follow the state change. */
1941 mount_enter_dead(mount
, MOUNT_SUCCESS
);
1948 } else if (mount
->proc_flags
& (MOUNT_PROC_JUST_MOUNTED
|MOUNT_PROC_JUST_CHANGED
)) {
1950 /* A mount point was added or changed */
1952 switch (mount
->state
) {
1957 /* This has just been mounted by somebody else, follow the state change, but let's
1958 * generate a new invocation ID for this implicitly and automatically. */
1959 (void) unit_acquire_invocation_id(u
);
1960 mount_cycle_clear(mount
);
1961 mount_enter_mounted(mount
, MOUNT_SUCCESS
);
1964 case MOUNT_MOUNTING
:
1965 mount_set_state(mount
, MOUNT_MOUNTING_DONE
);
1969 /* Nothing really changed, but let's
1970 * issue an notification call
1971 * nonetheless, in case somebody is
1972 * waiting for this. (e.g. file system
1973 * ro/rw remounts.) */
1974 mount_set_state(mount
, mount
->state
);
1979 if (mount_is_mounted(mount
) &&
1980 mount
->from_proc_self_mountinfo
&&
1981 mount
->parameters_proc_self_mountinfo
.what
) {
1982 /* Track devices currently used */
1984 if (set_ensure_allocated(&around
, &path_hash_ops
) < 0 ||
1985 set_put_strdup(around
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
1989 /* Reset the flags for later calls */
1990 mount
->proc_flags
= 0;
1993 SET_FOREACH(what
, gone
, i
) {
1994 if (set_contains(around
, what
))
1997 /* Let the device units know that the device is no longer mounted */
1998 device_found_node(m
, what
, 0, DEVICE_FOUND_MOUNT
);
2000 m
->mount_last_duration_usec
= usec_sub_unsigned(now(CLOCK_MONOTONIC
),
2001 m
->mount_last_read_usec
);
2004 static void mount_reset_failed(Unit
*u
) {
2005 Mount
*m
= MOUNT(u
);
2009 if (m
->state
== MOUNT_FAILED
)
2010 mount_set_state(m
, MOUNT_DEAD
);
2012 m
->result
= MOUNT_SUCCESS
;
2013 m
->reload_result
= MOUNT_SUCCESS
;
2016 static int mount_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
2017 Mount
*m
= MOUNT(u
);
2021 return unit_kill_common(u
, who
, signo
, -1, MOUNT(u
)->control_pid
, error
);
2024 static int mount_control_pid(Unit
*u
) {
2025 Mount
*m
= MOUNT(u
);
2029 return m
->control_pid
;
2032 static const char* const mount_exec_command_table
[_MOUNT_EXEC_COMMAND_MAX
] = {
2033 [MOUNT_EXEC_MOUNT
] = "ExecMount",
2034 [MOUNT_EXEC_UNMOUNT
] = "ExecUnmount",
2035 [MOUNT_EXEC_REMOUNT
] = "ExecRemount",
2038 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command
, MountExecCommand
);
2040 static const char* const mount_result_table
[_MOUNT_RESULT_MAX
] = {
2041 [MOUNT_SUCCESS
] = "success",
2042 [MOUNT_FAILURE_RESOURCES
] = "resources",
2043 [MOUNT_FAILURE_TIMEOUT
] = "timeout",
2044 [MOUNT_FAILURE_EXIT_CODE
] = "exit-code",
2045 [MOUNT_FAILURE_SIGNAL
] = "signal",
2046 [MOUNT_FAILURE_CORE_DUMP
] = "core-dump",
2047 [MOUNT_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
2048 [MOUNT_FAILURE_PROTOCOL
] = "protocol",
2051 DEFINE_STRING_TABLE_LOOKUP(mount_result
, MountResult
);
2053 const UnitVTable mount_vtable
= {
2054 .object_size
= sizeof(Mount
),
2055 .exec_context_offset
= offsetof(Mount
, exec_context
),
2056 .cgroup_context_offset
= offsetof(Mount
, cgroup_context
),
2057 .kill_context_offset
= offsetof(Mount
, kill_context
),
2058 .exec_runtime_offset
= offsetof(Mount
, exec_runtime
),
2059 .dynamic_creds_offset
= offsetof(Mount
, dynamic_creds
),
2065 .private_section
= "Mount",
2071 .coldplug
= mount_coldplug
,
2075 .start
= mount_start
,
2077 .reload
= mount_reload
,
2081 .serialize
= mount_serialize
,
2082 .deserialize_item
= mount_deserialize_item
,
2084 .active_state
= mount_active_state
,
2085 .sub_state_to_string
= mount_sub_state_to_string
,
2087 .may_gc
= mount_may_gc
,
2089 .sigchld_event
= mount_sigchld_event
,
2091 .reset_failed
= mount_reset_failed
,
2093 .control_pid
= mount_control_pid
,
2095 .bus_vtable
= bus_mount_vtable
,
2096 .bus_set_property
= bus_mount_set_property
,
2097 .bus_commit_properties
= bus_mount_commit_properties
,
2099 .get_timeout
= mount_get_timeout
,
2101 .can_transient
= true,
2103 .enumerate_perpetual
= mount_enumerate_perpetual
,
2104 .enumerate
= mount_enumerate
,
2105 .shutdown
= mount_shutdown
,
2107 .status_message_formats
= {
2108 .starting_stopping
= {
2109 [0] = "Mounting %s...",
2110 [1] = "Unmounting %s...",
2112 .finished_start_job
= {
2113 [JOB_DONE
] = "Mounted %s.",
2114 [JOB_FAILED
] = "Failed to mount %s.",
2115 [JOB_TIMEOUT
] = "Timed out mounting %s.",
2117 .finished_stop_job
= {
2118 [JOB_DONE
] = "Unmounted %s.",
2119 [JOB_FAILED
] = "Failed unmounting %s.",
2120 [JOB_TIMEOUT
] = "Timed out unmounting %s.",