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 m
->where
= mfree(m
->where
);
223 mount_parameters_done(&m
->parameters_proc_self_mountinfo
);
224 mount_parameters_done(&m
->parameters_fragment
);
226 m
->exec_runtime
= exec_runtime_unref(m
->exec_runtime
, false);
227 exec_command_done_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
228 m
->control_command
= NULL
;
230 dynamic_creds_unref(&m
->dynamic_creds
);
232 mount_unwatch_control_pid(m
);
234 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
237 _pure_
static MountParameters
* get_mount_parameters_fragment(Mount
*m
) {
240 if (m
->from_fragment
)
241 return &m
->parameters_fragment
;
246 _pure_
static MountParameters
* get_mount_parameters(Mount
*m
) {
249 if (m
->from_proc_self_mountinfo
)
250 return &m
->parameters_proc_self_mountinfo
;
252 return get_mount_parameters_fragment(m
);
255 static int mount_add_mount_dependencies(Mount
*m
) {
264 if (!path_equal(m
->where
, "/")) {
265 _cleanup_free_
char *parent
= NULL
;
267 /* Adds in links to other mount points that might lie further up in the hierarchy */
269 parent
= dirname_malloc(m
->where
);
273 r
= unit_require_mounts_for(UNIT(m
), parent
, UNIT_DEPENDENCY_IMPLICIT
);
278 /* Adds in dependencies to other mount points that might be needed for the source path (if this is a bind mount
279 * or a loop mount) to be available. */
280 pm
= get_mount_parameters_fragment(m
);
281 if (pm
&& pm
->what
&&
282 path_is_absolute(pm
->what
) &&
283 (mount_is_bind(pm
) || mount_is_loop(pm
) || !mount_is_network(pm
))) {
285 r
= unit_require_mounts_for(UNIT(m
), pm
->what
, UNIT_DEPENDENCY_FILE
);
290 /* Adds in dependencies to other units that use this path or paths further down in the hierarchy */
291 s
= manager_get_units_requiring_mounts_for(UNIT(m
)->manager
, m
->where
);
292 SET_FOREACH(other
, s
, i
) {
294 if (other
->load_state
!= UNIT_LOADED
)
297 if (other
== UNIT(m
))
300 r
= unit_add_dependency(other
, UNIT_AFTER
, UNIT(m
), true, UNIT_DEPENDENCY_PATH
);
304 if (UNIT(m
)->fragment_path
) {
305 /* If we have fragment configuration, then make this dependency required */
306 r
= unit_add_dependency(other
, UNIT_REQUIRES
, UNIT(m
), true, UNIT_DEPENDENCY_PATH
);
315 static int mount_add_device_dependencies(Mount
*m
) {
316 bool device_wants_mount
;
317 UnitDependencyMask mask
;
324 p
= get_mount_parameters(m
);
331 if (mount_is_bind(p
))
334 if (!is_device_path(p
->what
))
337 /* /dev/root is a really weird thing, it's not a real device,
338 * but just a path the kernel exports for the root file system
339 * specified on the kernel command line. Ignore it here. */
340 if (path_equal(p
->what
, "/dev/root"))
343 if (path_equal(m
->where
, "/"))
347 mount_is_auto(p
) && !mount_is_automount(p
) && MANAGER_IS_SYSTEM(UNIT(m
)->manager
);
349 /* Mount units from /proc/self/mountinfo are not bound to devices
350 * by default since they're subject to races when devices are
351 * unplugged. But the user can still force this dep with an
352 * appropriate option (or udev property) so the mount units are
353 * automatically stopped when the device disappears suddenly. */
354 dep
= mount_is_bound_to_device(m
) ? UNIT_BINDS_TO
: UNIT_REQUIRES
;
356 mask
= m
->from_fragment
? UNIT_DEPENDENCY_FILE
: UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
;
358 r
= unit_add_node_dependency(UNIT(m
), p
->what
, device_wants_mount
, dep
, mask
);
365 static int mount_add_quota_dependencies(Mount
*m
) {
366 UnitDependencyMask mask
;
372 if (!MANAGER_IS_SYSTEM(UNIT(m
)->manager
))
375 p
= get_mount_parameters_fragment(m
);
379 if (!mount_needs_quota(p
))
382 mask
= m
->from_fragment
? UNIT_DEPENDENCY_FILE
: UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
;
384 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTACHECK_SERVICE
, true, mask
);
388 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTAON_SERVICE
, true, mask
);
395 static bool mount_is_extrinsic(Mount
*m
) {
399 /* Returns true for all units that are "magic" and should be excluded from the usual start-up and shutdown
400 * dependencies. We call them "extrinsic" here, as they are generally mounted outside of the systemd dependency
401 * logic. We shouldn't attempt to manage them ourselves but it's fine if the user operates on them with us. */
403 if (!MANAGER_IS_SYSTEM(UNIT(m
)->manager
)) /* We only automatically manage mounts if we are in system mode */
406 if (PATH_IN_SET(m
->where
, /* Don't bother with the OS data itself */
411 if (PATH_STARTSWITH_SET(m
->where
,
412 "/run/initramfs", /* This should stay around from before we boot until after we shutdown */
413 "/proc", /* All of this is API VFS */
414 "/sys", /* … dito … */
415 "/dev")) /* … dito … */
418 /* If this is an initrd mount, and we are not in the initrd, then leave this around forever, too. */
419 p
= get_mount_parameters(m
);
420 if (p
&& fstab_test_option(p
->options
, "x-initrd.mount\0") && !in_initrd())
426 static int mount_add_default_dependencies(Mount
*m
) {
427 const char *after
, *before
;
428 UnitDependencyMask mask
;
434 if (!UNIT(m
)->default_dependencies
)
437 /* We do not add any default dependencies to /, /usr or /run/initramfs/, since they are guaranteed to stay
438 * mounted the whole time, since our system is on it. Also, don't bother with anything mounted below virtual
439 * file systems, it's also going to be virtual, and hence not worth the effort. */
440 if (mount_is_extrinsic(m
))
443 p
= get_mount_parameters(m
);
447 mask
= m
->from_fragment
? UNIT_DEPENDENCY_FILE
: UNIT_DEPENDENCY_MOUNTINFO_DEFAULT
;
449 if (mount_is_network(p
)) {
450 /* We order ourselves after network.target. This is
451 * primarily useful at shutdown: services that take
452 * down the network should order themselves before
453 * network.target, so that they are shut down only
454 * after this mount unit is stopped. */
456 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, SPECIAL_NETWORK_TARGET
, true, mask
);
460 /* We pull in network-online.target, and order
461 * ourselves after it. This is useful at start-up to
462 * actively pull in tools that want to be started
463 * before we start mounting network file systems, and
464 * whose purpose it is to delay this until the network
467 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_WANTS
, UNIT_AFTER
, SPECIAL_NETWORK_ONLINE_TARGET
, true, mask
);
471 after
= SPECIAL_REMOTE_FS_PRE_TARGET
;
472 before
= SPECIAL_REMOTE_FS_TARGET
;
474 after
= SPECIAL_LOCAL_FS_PRE_TARGET
;
475 before
= SPECIAL_LOCAL_FS_TARGET
;
478 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_BEFORE
, before
, true, mask
);
482 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, after
, true, mask
);
486 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, true, mask
);
490 /* If this is a tmpfs mount then we have to unmount it before we try to deactivate swaps */
491 if (streq_ptr(p
->fstype
, "tmpfs")) {
492 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, SPECIAL_SWAP_TARGET
, true, mask
);
500 static int mount_verify(Mount
*m
) {
501 _cleanup_free_
char *e
= NULL
;
507 if (UNIT(m
)->load_state
!= UNIT_LOADED
)
510 if (!m
->from_fragment
&& !m
->from_proc_self_mountinfo
&& !UNIT(m
)->perpetual
)
513 r
= unit_name_from_path(m
->where
, ".mount", &e
);
515 return log_unit_error_errno(UNIT(m
), r
, "Failed to generate unit name from mount path: %m");
517 if (!unit_has_name(UNIT(m
), e
)) {
518 log_unit_error(UNIT(m
), "Where= setting doesn't match unit name. Refusing.");
522 if (mount_point_is_api(m
->where
) || mount_point_ignore(m
->where
)) {
523 log_unit_error(UNIT(m
), "Cannot create mount unit for API file system %s. Refusing.", m
->where
);
527 p
= get_mount_parameters_fragment(m
);
529 log_unit_error(UNIT(m
), "What= setting is missing. Refusing.");
533 if (m
->exec_context
.pam_name
&& m
->kill_context
.kill_mode
!= KILL_CONTROL_GROUP
) {
534 log_unit_error(UNIT(m
), "Unit has PAM enabled. Kill mode must be set to control-group'. Refusing.");
541 static int mount_add_extras(Mount
*m
) {
547 /* Note: this call might be called after we already have been loaded once (and even when it has already been
548 * activated), in case data from /proc/self/mountinfo has changed. This means all code here needs to be ready
549 * to run with an already set up unit. */
551 if (u
->fragment_path
)
552 m
->from_fragment
= true;
555 r
= unit_name_to_path(u
->id
, &m
->where
);
560 path_simplify(m
->where
, false);
562 if (!u
->description
) {
563 r
= unit_set_description(u
, m
->where
);
568 r
= mount_add_device_dependencies(m
);
572 r
= mount_add_mount_dependencies(m
);
576 r
= mount_add_quota_dependencies(m
);
580 r
= unit_patch_contexts(u
);
584 r
= unit_add_exec_dependencies(u
, &m
->exec_context
);
588 r
= unit_set_default_slice(u
);
592 r
= mount_add_default_dependencies(m
);
599 static int mount_load_root_mount(Unit
*u
) {
602 if (!unit_has_name(u
, SPECIAL_ROOT_MOUNT
))
606 u
->default_dependencies
= false;
608 /* The stdio/kmsg bridge socket is on /, in order to avoid a dep loop, don't use kmsg logging for -.mount */
609 MOUNT(u
)->exec_context
.std_output
= EXEC_OUTPUT_NULL
;
610 MOUNT(u
)->exec_context
.std_input
= EXEC_INPUT_NULL
;
613 u
->description
= strdup("Root Mount");
618 static int mount_load(Unit
*u
) {
623 assert(u
->load_state
== UNIT_STUB
);
625 r
= mount_load_root_mount(u
);
627 if (m
->from_proc_self_mountinfo
|| u
->perpetual
)
628 q
= unit_load_fragment_and_dropin_optional(u
);
630 q
= unit_load_fragment_and_dropin(u
);
632 /* Add in some extras. Note we do this in all cases (even if we failed to load the unit) when announced by the
633 * kernel, because we need some things to be set up no matter what when the kernel establishes a mount and thus
634 * we need to update the state in our unit to track it. After all, consider that we don't allow changing the
635 * 'slice' field for a unit once it is active. */
636 if (u
->load_state
== UNIT_LOADED
|| m
->from_proc_self_mountinfo
|| u
->perpetual
)
637 w
= mount_add_extras(m
);
648 return mount_verify(m
);
651 static void mount_set_state(Mount
*m
, MountState state
) {
652 MountState old_state
;
655 if (m
->state
!= state
)
656 bus_unit_send_pending_change_signal(UNIT(m
), false);
658 old_state
= m
->state
;
661 if (!MOUNT_STATE_WITH_PROCESS(state
)) {
662 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
663 mount_unwatch_control_pid(m
);
664 m
->control_command
= NULL
;
665 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
668 if (state
!= old_state
)
669 log_unit_debug(UNIT(m
), "Changed %s -> %s", mount_state_to_string(old_state
), mount_state_to_string(state
));
671 unit_notify(UNIT(m
), state_translation_table
[old_state
], state_translation_table
[state
],
672 m
->reload_result
== MOUNT_SUCCESS
? 0 : UNIT_NOTIFY_RELOAD_FAILURE
);
675 static int mount_coldplug(Unit
*u
) {
677 MountState new_state
= MOUNT_DEAD
;
681 assert(m
->state
== MOUNT_DEAD
);
683 if (m
->deserialized_state
!= m
->state
)
684 new_state
= m
->deserialized_state
;
685 else if (m
->from_proc_self_mountinfo
)
686 new_state
= MOUNT_MOUNTED
;
688 if (new_state
== m
->state
)
691 if (m
->control_pid
> 0 &&
692 pid_is_unwaited(m
->control_pid
) &&
693 MOUNT_STATE_WITH_PROCESS(new_state
)) {
695 r
= unit_watch_pid(UNIT(m
), m
->control_pid
);
699 r
= mount_arm_timer(m
, usec_add(u
->state_change_timestamp
.monotonic
, m
->timeout_usec
));
704 if (!IN_SET(new_state
, MOUNT_DEAD
, MOUNT_FAILED
)) {
705 (void) unit_setup_dynamic_creds(u
);
706 (void) unit_setup_exec_runtime(u
);
709 mount_set_state(m
, new_state
);
713 static void mount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
714 char buf
[FORMAT_TIMESPAN_MAX
];
721 p
= get_mount_parameters(m
);
724 "%sMount State: %s\n"
728 "%sFile System Type: %s\n"
730 "%sFrom /proc/self/mountinfo: %s\n"
731 "%sFrom fragment: %s\n"
733 "%sDirectoryMode: %04o\n"
734 "%sSloppyOptions: %s\n"
735 "%sLazyUnmount: %s\n"
736 "%sForceUnmount: %s\n"
738 prefix
, mount_state_to_string(m
->state
),
739 prefix
, mount_result_to_string(m
->result
),
741 prefix
, p
? strna(p
->what
) : "n/a",
742 prefix
, p
? strna(p
->fstype
) : "n/a",
743 prefix
, p
? strna(p
->options
) : "n/a",
744 prefix
, yes_no(m
->from_proc_self_mountinfo
),
745 prefix
, yes_no(m
->from_fragment
),
746 prefix
, yes_no(mount_is_extrinsic(m
)),
747 prefix
, m
->directory_mode
,
748 prefix
, yes_no(m
->sloppy_options
),
749 prefix
, yes_no(m
->lazy_unmount
),
750 prefix
, yes_no(m
->force_unmount
),
751 prefix
, format_timespan(buf
, sizeof(buf
), m
->timeout_usec
, USEC_PER_SEC
));
753 if (m
->control_pid
> 0)
755 "%sControl PID: "PID_FMT
"\n",
756 prefix
, m
->control_pid
);
758 exec_context_dump(&m
->exec_context
, f
, prefix
);
759 kill_context_dump(&m
->kill_context
, f
, prefix
);
760 cgroup_context_dump(&m
->cgroup_context
, f
, prefix
);
763 static int mount_spawn(Mount
*m
, ExecCommand
*c
, pid_t
*_pid
) {
765 _cleanup_(exec_params_clear
) ExecParameters exec_params
= {
766 .flags
= EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
,
779 r
= unit_prepare_exec(UNIT(m
));
783 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->timeout_usec
));
787 r
= unit_set_exec_params(UNIT(m
), &exec_params
);
791 r
= exec_spawn(UNIT(m
),
801 r
= unit_watch_pid(UNIT(m
), pid
);
803 /* FIXME: we need to do something here */
811 static void mount_enter_dead(Mount
*m
, MountResult f
) {
814 if (m
->result
== MOUNT_SUCCESS
)
817 unit_log_result(UNIT(m
), m
->result
== MOUNT_SUCCESS
, mount_result_to_string(m
->result
));
818 mount_set_state(m
, m
->result
!= MOUNT_SUCCESS
? MOUNT_FAILED
: MOUNT_DEAD
);
820 m
->exec_runtime
= exec_runtime_unref(m
->exec_runtime
, true);
822 exec_context_destroy_runtime_directory(&m
->exec_context
, UNIT(m
)->manager
->prefix
[EXEC_DIRECTORY_RUNTIME
]);
824 unit_unref_uid_gid(UNIT(m
), true);
826 dynamic_creds_destroy(&m
->dynamic_creds
);
829 static void mount_enter_mounted(Mount
*m
, MountResult f
) {
832 if (m
->result
== MOUNT_SUCCESS
)
835 mount_set_state(m
, MOUNT_MOUNTED
);
838 static void mount_enter_dead_or_mounted(Mount
*m
, MountResult f
) {
841 /* Enter DEAD or MOUNTED state, depending on what the kernel currently says about the mount point. We use this
842 * whenever we executed an operation, so that our internal state reflects what the kernel says again, after all
843 * ultimately we just mirror the kernel's internal state on this. */
845 if (m
->from_proc_self_mountinfo
)
846 mount_enter_mounted(m
, f
);
848 mount_enter_dead(m
, f
);
851 static int state_to_kill_operation(MountState state
) {
854 case MOUNT_REMOUNTING_SIGTERM
:
855 case MOUNT_UNMOUNTING_SIGTERM
:
856 return KILL_TERMINATE
;
858 case MOUNT_REMOUNTING_SIGKILL
:
859 case MOUNT_UNMOUNTING_SIGKILL
:
863 return _KILL_OPERATION_INVALID
;
867 static void mount_enter_signal(Mount
*m
, MountState state
, MountResult f
) {
872 if (m
->result
== MOUNT_SUCCESS
)
875 r
= unit_kill_context(
878 state_to_kill_operation(state
),
886 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->timeout_usec
));
890 mount_set_state(m
, state
);
891 } else if (state
== MOUNT_REMOUNTING_SIGTERM
&& m
->kill_context
.send_sigkill
)
892 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
893 else if (IN_SET(state
, MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
))
894 mount_enter_mounted(m
, MOUNT_SUCCESS
);
895 else if (state
== MOUNT_UNMOUNTING_SIGTERM
&& m
->kill_context
.send_sigkill
)
896 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
898 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
903 log_unit_warning_errno(UNIT(m
), r
, "Failed to kill processes: %m");
904 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
907 static void mount_enter_unmounting(Mount
*m
) {
912 /* Start counting our attempts */
913 if (!IN_SET(m
->state
,
915 MOUNT_UNMOUNTING_SIGTERM
,
916 MOUNT_UNMOUNTING_SIGKILL
))
917 m
->n_retry_umount
= 0;
919 m
->control_command_id
= MOUNT_EXEC_UNMOUNT
;
920 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_UNMOUNT
;
922 r
= exec_command_set(m
->control_command
, UMOUNT_PATH
, m
->where
, "-c", NULL
);
923 if (r
>= 0 && m
->lazy_unmount
)
924 r
= exec_command_append(m
->control_command
, "-l", NULL
);
925 if (r
>= 0 && m
->force_unmount
)
926 r
= exec_command_append(m
->control_command
, "-f", NULL
);
930 mount_unwatch_control_pid(m
);
932 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
936 mount_set_state(m
, MOUNT_UNMOUNTING
);
941 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'umount' task: %m");
942 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
945 static void mount_enter_mounting(Mount
*m
) {
951 r
= unit_fail_if_noncanonical(UNIT(m
), m
->where
);
955 (void) mkdir_p_label(m
->where
, m
->directory_mode
);
957 unit_warn_if_dir_nonempty(UNIT(m
), m
->where
);
958 unit_warn_leftover_processes(UNIT(m
));
960 m
->control_command_id
= MOUNT_EXEC_MOUNT
;
961 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_MOUNT
;
963 /* Create the source directory for bind-mounts if needed */
964 p
= get_mount_parameters_fragment(m
);
965 if (p
&& mount_is_bind(p
))
966 (void) mkdir_p_label(p
->what
, m
->directory_mode
);
969 _cleanup_free_
char *opts
= NULL
;
971 r
= fstab_filter_options(p
->options
, "nofail\0" "noauto\0" "auto\0", NULL
, NULL
, &opts
);
975 r
= exec_command_set(m
->control_command
, MOUNT_PATH
, p
->what
, m
->where
, NULL
);
976 if (r
>= 0 && m
->sloppy_options
)
977 r
= exec_command_append(m
->control_command
, "-s", NULL
);
978 if (r
>= 0 && p
->fstype
)
979 r
= exec_command_append(m
->control_command
, "-t", p
->fstype
, NULL
);
980 if (r
>= 0 && !isempty(opts
))
981 r
= exec_command_append(m
->control_command
, "-o", opts
, NULL
);
987 mount_unwatch_control_pid(m
);
989 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
993 mount_set_state(m
, MOUNT_MOUNTING
);
998 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'mount' task: %m");
999 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
1002 static void mount_set_reload_result(Mount
*m
, MountResult result
) {
1005 /* Only store the first error we encounter */
1006 if (m
->reload_result
!= MOUNT_SUCCESS
)
1009 m
->reload_result
= result
;
1012 static void mount_enter_remounting(Mount
*m
) {
1018 /* Reset reload result when we are about to start a new remount operation */
1019 m
->reload_result
= MOUNT_SUCCESS
;
1021 m
->control_command_id
= MOUNT_EXEC_REMOUNT
;
1022 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_REMOUNT
;
1024 p
= get_mount_parameters_fragment(m
);
1029 o
= strjoina("remount,", p
->options
);
1033 r
= exec_command_set(m
->control_command
, MOUNT_PATH
,
1036 if (r
>= 0 && m
->sloppy_options
)
1037 r
= exec_command_append(m
->control_command
, "-s", NULL
);
1038 if (r
>= 0 && p
->fstype
)
1039 r
= exec_command_append(m
->control_command
, "-t", p
->fstype
, NULL
);
1045 mount_unwatch_control_pid(m
);
1047 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
1051 mount_set_state(m
, MOUNT_REMOUNTING
);
1056 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'remount' task: %m");
1057 mount_set_reload_result(m
, MOUNT_FAILURE_RESOURCES
);
1058 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1061 static void mount_cycle_clear(Mount
*m
) {
1064 /* Clear all state we shall forget for this new cycle */
1066 m
->result
= MOUNT_SUCCESS
;
1067 m
->reload_result
= MOUNT_SUCCESS
;
1068 exec_command_reset_status_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
1069 UNIT(m
)->reset_accounting
= true;
1072 static int mount_start(Unit
*u
) {
1073 Mount
*m
= MOUNT(u
);
1078 /* We cannot fulfill this request right now, try again later
1080 if (IN_SET(m
->state
,
1082 MOUNT_UNMOUNTING_SIGTERM
,
1083 MOUNT_UNMOUNTING_SIGKILL
))
1086 /* Already on it! */
1087 if (m
->state
== MOUNT_MOUNTING
)
1090 assert(IN_SET(m
->state
, MOUNT_DEAD
, MOUNT_FAILED
));
1092 r
= unit_start_limit_test(u
);
1094 mount_enter_dead(m
, MOUNT_FAILURE_START_LIMIT_HIT
);
1098 r
= unit_acquire_invocation_id(u
);
1102 mount_cycle_clear(m
);
1103 mount_enter_mounting(m
);
1108 static int mount_stop(Unit
*u
) {
1109 Mount
*m
= MOUNT(u
);
1115 case MOUNT_UNMOUNTING
:
1116 case MOUNT_UNMOUNTING_SIGKILL
:
1117 case MOUNT_UNMOUNTING_SIGTERM
:
1121 case MOUNT_MOUNTING
:
1122 case MOUNT_MOUNTING_DONE
:
1123 case MOUNT_REMOUNTING
:
1124 /* If we are still waiting for /bin/mount, we go directly into kill mode. */
1125 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_SUCCESS
);
1128 case MOUNT_REMOUNTING_SIGTERM
:
1129 /* If we are already waiting for a hung remount, convert this to the matching unmounting state */
1130 mount_set_state(m
, MOUNT_UNMOUNTING_SIGTERM
);
1133 case MOUNT_REMOUNTING_SIGKILL
:
1135 mount_set_state(m
, MOUNT_UNMOUNTING_SIGKILL
);
1139 mount_enter_unmounting(m
);
1143 assert_not_reached("Unexpected state.");
1147 static int mount_reload(Unit
*u
) {
1148 Mount
*m
= MOUNT(u
);
1151 assert(m
->state
== MOUNT_MOUNTED
);
1153 mount_enter_remounting(m
);
1158 static int mount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1159 Mount
*m
= MOUNT(u
);
1165 (void) serialize_item(f
, "state", mount_state_to_string(m
->state
));
1166 (void) serialize_item(f
, "result", mount_result_to_string(m
->result
));
1167 (void) serialize_item(f
, "reload-result", mount_result_to_string(m
->reload_result
));
1168 (void) serialize_item_format(f
, "n-retry-umount", "%u", m
->n_retry_umount
);
1170 if (m
->control_pid
> 0)
1171 (void) serialize_item_format(f
, "control-pid", PID_FMT
, m
->control_pid
);
1173 if (m
->control_command_id
>= 0)
1174 (void) serialize_item(f
, "control-command", mount_exec_command_to_string(m
->control_command_id
));
1179 static int mount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1180 Mount
*m
= MOUNT(u
);
1188 if (streq(key
, "state")) {
1191 if ((state
= mount_state_from_string(value
)) < 0)
1192 log_unit_debug(u
, "Failed to parse state value: %s", value
);
1194 m
->deserialized_state
= state
;
1196 } else if (streq(key
, "result")) {
1199 f
= mount_result_from_string(value
);
1201 log_unit_debug(u
, "Failed to parse result value: %s", value
);
1202 else if (f
!= MOUNT_SUCCESS
)
1205 } else if (streq(key
, "reload-result")) {
1208 f
= mount_result_from_string(value
);
1210 log_unit_debug(u
, "Failed to parse reload result value: %s", value
);
1211 else if (f
!= MOUNT_SUCCESS
)
1212 m
->reload_result
= f
;
1214 } else if (streq(key
, "n-retry-umount")) {
1216 r
= safe_atou(value
, &m
->n_retry_umount
);
1218 log_unit_debug(u
, "Failed to parse n-retry-umount value: %s", value
);
1220 } else if (streq(key
, "control-pid")) {
1222 if (parse_pid(value
, &m
->control_pid
) < 0)
1223 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
1225 } else if (streq(key
, "control-command")) {
1226 MountExecCommand id
;
1228 id
= mount_exec_command_from_string(value
);
1230 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
1232 m
->control_command_id
= id
;
1233 m
->control_command
= m
->exec_command
+ id
;
1236 log_unit_debug(u
, "Unknown serialization key: %s", key
);
1241 _pure_
static UnitActiveState
mount_active_state(Unit
*u
) {
1244 return state_translation_table
[MOUNT(u
)->state
];
1247 _pure_
static const char *mount_sub_state_to_string(Unit
*u
) {
1250 return mount_state_to_string(MOUNT(u
)->state
);
1253 _pure_
static bool mount_may_gc(Unit
*u
) {
1254 Mount
*m
= MOUNT(u
);
1258 if (m
->from_proc_self_mountinfo
)
1264 static void mount_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1265 Mount
*m
= MOUNT(u
);
1271 if (pid
!= m
->control_pid
)
1276 if (is_clean_exit(code
, status
, EXIT_CLEAN_COMMAND
, NULL
))
1278 else if (code
== CLD_EXITED
)
1279 f
= MOUNT_FAILURE_EXIT_CODE
;
1280 else if (code
== CLD_KILLED
)
1281 f
= MOUNT_FAILURE_SIGNAL
;
1282 else if (code
== CLD_DUMPED
)
1283 f
= MOUNT_FAILURE_CORE_DUMP
;
1285 assert_not_reached("Unknown code");
1287 if (IN_SET(m
->state
, MOUNT_REMOUNTING
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_REMOUNTING_SIGTERM
))
1288 mount_set_reload_result(m
, f
);
1289 else if (m
->result
== MOUNT_SUCCESS
)
1292 if (m
->control_command
) {
1293 exec_status_exit(&m
->control_command
->exec_status
, &m
->exec_context
, pid
, code
, status
);
1295 m
->control_command
= NULL
;
1296 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
1299 unit_log_process_exit(
1300 u
, f
== MOUNT_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
1302 mount_exec_command_to_string(m
->control_command_id
),
1305 /* Note that due to the io event priority logic, we can be sure the new mountinfo is loaded
1306 * before we process the SIGCHLD for the mount command. */
1310 case MOUNT_MOUNTING
:
1311 /* Our mount point has not appeared in mountinfo. Something went wrong. */
1313 if (f
== MOUNT_SUCCESS
) {
1314 /* Either /bin/mount has an unexpected definition of success,
1315 * or someone raced us and we lost. */
1316 log_unit_warning(UNIT(m
), "Mount process finished, but there is no mount.");
1317 f
= MOUNT_FAILURE_PROTOCOL
;
1319 mount_enter_dead(m
, f
);
1322 case MOUNT_MOUNTING_DONE
:
1323 mount_enter_mounted(m
, f
);
1326 case MOUNT_REMOUNTING
:
1327 case MOUNT_REMOUNTING_SIGTERM
:
1328 case MOUNT_REMOUNTING_SIGKILL
:
1329 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1332 case MOUNT_UNMOUNTING
:
1334 if (f
== MOUNT_SUCCESS
&& m
->from_proc_self_mountinfo
) {
1336 /* Still a mount point? If so, let's try again. Most likely there were multiple mount points
1337 * stacked on top of each other. We might exceed the timeout specified by the user overall,
1338 * but we will stop as soon as any one umount times out. */
1340 if (m
->n_retry_umount
< RETRY_UMOUNT_MAX
) {
1341 log_unit_debug(u
, "Mount still present, trying again.");
1342 m
->n_retry_umount
++;
1343 mount_enter_unmounting(m
);
1345 log_unit_warning(u
, "Mount still present after %u attempts to unmount, giving up.", m
->n_retry_umount
);
1346 mount_enter_mounted(m
, f
);
1349 mount_enter_dead_or_mounted(m
, f
);
1353 case MOUNT_UNMOUNTING_SIGKILL
:
1354 case MOUNT_UNMOUNTING_SIGTERM
:
1355 mount_enter_dead_or_mounted(m
, f
);
1359 assert_not_reached("Uh, control process died at wrong time.");
1362 /* Notify clients about changed exit status */
1363 unit_add_to_dbus_queue(u
);
1366 static int mount_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
1367 Mount
*m
= MOUNT(userdata
);
1370 assert(m
->timer_event_source
== source
);
1374 case MOUNT_MOUNTING
:
1375 case MOUNT_MOUNTING_DONE
:
1376 log_unit_warning(UNIT(m
), "Mounting timed out. Terminating.");
1377 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1380 case MOUNT_REMOUNTING
:
1381 log_unit_warning(UNIT(m
), "Remounting timed out. Terminating remount process.");
1382 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1383 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGTERM
, MOUNT_SUCCESS
);
1386 case MOUNT_REMOUNTING_SIGTERM
:
1387 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1389 if (m
->kill_context
.send_sigkill
) {
1390 log_unit_warning(UNIT(m
), "Remounting timed out. Killing.");
1391 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
1393 log_unit_warning(UNIT(m
), "Remounting timed out. Skipping SIGKILL. Ignoring.");
1394 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1398 case MOUNT_REMOUNTING_SIGKILL
:
1399 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1401 log_unit_warning(UNIT(m
), "Mount process still around after SIGKILL. Ignoring.");
1402 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1405 case MOUNT_UNMOUNTING
:
1406 log_unit_warning(UNIT(m
), "Unmounting timed out. Terminating.");
1407 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1410 case MOUNT_UNMOUNTING_SIGTERM
:
1411 if (m
->kill_context
.send_sigkill
) {
1412 log_unit_warning(UNIT(m
), "Mount process timed out. Killing.");
1413 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_FAILURE_TIMEOUT
);
1415 log_unit_warning(UNIT(m
), "Mount process timed out. Skipping SIGKILL. Ignoring.");
1416 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1420 case MOUNT_UNMOUNTING_SIGKILL
:
1421 log_unit_warning(UNIT(m
), "Mount process still around after SIGKILL. Ignoring.");
1422 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1426 assert_not_reached("Timeout at wrong time.");
1432 static int update_parameters_proc_self_mount_info(
1435 const char *options
,
1436 const char *fstype
) {
1441 p
= &m
->parameters_proc_self_mountinfo
;
1443 r
= free_and_strdup(&p
->what
, what
);
1447 q
= free_and_strdup(&p
->options
, options
);
1451 w
= free_and_strdup(&p
->fstype
, fstype
);
1455 return r
> 0 || q
> 0 || w
> 0;
1458 static int mount_setup_new_unit(
1463 const char *options
,
1465 MountProcFlags
*ret_flags
,
1468 _cleanup_(unit_freep
) Unit
*u
= NULL
;
1476 r
= unit_new_for_name(m
, sizeof(Mount
), name
, &u
);
1480 r
= free_and_strdup(&u
->source_path
, "/proc/self/mountinfo");
1484 r
= free_and_strdup(&MOUNT(u
)->where
, where
);
1488 r
= update_parameters_proc_self_mount_info(MOUNT(u
), what
, options
, fstype
);
1492 /* This unit was generated because /proc/self/mountinfo reported it. Remember this, so that by the time we load
1493 * the unit file for it (and thus add in extra deps right after) we know what source to attributes the deps
1495 MOUNT(u
)->from_proc_self_mountinfo
= true;
1497 /* We have only allocated the stub now, let's enqueue this unit for loading now, so that everything else is
1499 unit_add_to_load_queue(u
);
1501 *ret_flags
= MOUNT_PROC_IS_MOUNTED
| MOUNT_PROC_JUST_MOUNTED
| MOUNT_PROC_JUST_CHANGED
;
1506 static int mount_setup_existing_unit(
1510 const char *options
,
1512 MountProcFlags
*ret_flags
) {
1514 MountProcFlags flags
= MOUNT_PROC_IS_MOUNTED
;
1520 if (!MOUNT(u
)->where
) {
1521 MOUNT(u
)->where
= strdup(where
);
1522 if (!MOUNT(u
)->where
)
1526 r
= update_parameters_proc_self_mount_info(MOUNT(u
), what
, options
, fstype
);
1530 flags
|= MOUNT_PROC_JUST_CHANGED
;
1532 if (!MOUNT(u
)->from_proc_self_mountinfo
) {
1533 flags
|= MOUNT_PROC_JUST_MOUNTED
;
1534 MOUNT(u
)->from_proc_self_mountinfo
= true;
1537 if (IN_SET(u
->load_state
, UNIT_NOT_FOUND
, UNIT_BAD_SETTING
, UNIT_ERROR
)) {
1538 /* The unit was previously not found or otherwise not loaded. Now that the unit shows up in
1539 * /proc/self/mountinfo we should reconsider it this, hence set it to UNIT_LOADED. */
1540 u
->load_state
= UNIT_LOADED
;
1543 flags
|= MOUNT_PROC_JUST_CHANGED
;
1546 if (FLAGS_SET(flags
, MOUNT_PROC_JUST_CHANGED
)) {
1547 /* If things changed, then make sure that all deps are regenerated. Let's
1548 * first remove all automatic deps, and then add in the new ones. */
1550 unit_remove_dependencies(u
, UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
);
1552 r
= mount_add_extras(MOUNT(u
));
1561 static int mount_setup_unit(
1565 const char *options
,
1569 _cleanup_free_
char *e
= NULL
;
1570 MountProcFlags flags
;
1580 /* Ignore API mount points. They should never be referenced in
1581 * dependencies ever. */
1582 if (mount_point_is_api(where
) || mount_point_ignore(where
))
1585 if (streq(fstype
, "autofs"))
1588 /* probably some kind of swap, ignore */
1589 if (!is_path(where
))
1592 r
= unit_name_from_path(where
, ".mount", &e
);
1594 return log_error_errno(r
, "Failed to generate unit name from path '%s': %m", where
);
1596 u
= manager_get_unit(m
, e
);
1598 r
= mount_setup_existing_unit(u
, what
, where
, options
, fstype
, &flags
);
1600 /* First time we see this mount point meaning that it's not been initiated by a mount unit but rather
1601 * by the sysadmin having called mount(8) directly. */
1602 r
= mount_setup_new_unit(m
, e
, what
, where
, options
, fstype
, &flags
, &u
);
1604 return log_warning_errno(r
, "Failed to set up mount unit: %m");
1606 /* If the mount changed properties or state, let's notify our clients */
1607 if (flags
& (MOUNT_PROC_JUST_CHANGED
|MOUNT_PROC_JUST_MOUNTED
))
1608 unit_add_to_dbus_queue(u
);
1611 MOUNT(u
)->proc_flags
= flags
;
1616 static int mount_load_proc_self_mountinfo(Manager
*m
, bool set_flags
) {
1617 _cleanup_(mnt_free_tablep
) struct libmnt_table
*t
= NULL
;
1618 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*i
= NULL
;
1623 t
= mnt_new_table();
1624 i
= mnt_new_iter(MNT_ITER_FORWARD
);
1628 r
= mnt_table_parse_mtab(t
, NULL
);
1630 return log_error_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
1633 struct libmnt_fs
*fs
;
1634 const char *device
, *path
, *options
, *fstype
;
1635 _cleanup_free_
char *d
= NULL
, *p
= NULL
;
1638 k
= mnt_table_next_fs(t
, i
, &fs
);
1642 return log_error_errno(k
, "Failed to get next entry from /proc/self/mountinfo: %m");
1644 device
= mnt_fs_get_source(fs
);
1645 path
= mnt_fs_get_target(fs
);
1646 options
= mnt_fs_get_options(fs
);
1647 fstype
= mnt_fs_get_fstype(fs
);
1649 if (!device
|| !path
)
1652 if (cunescape(device
, UNESCAPE_RELAX
, &d
) < 0)
1655 if (cunescape(path
, UNESCAPE_RELAX
, &p
) < 0)
1658 device_found_node(m
, d
, DEVICE_FOUND_MOUNT
, DEVICE_FOUND_MOUNT
);
1660 (void) mount_setup_unit(m
, d
, p
, options
, fstype
, set_flags
);
1666 static void mount_shutdown(Manager
*m
) {
1669 m
->mount_timeout_source
= sd_event_source_unref(m
->mount_timeout_source
);
1670 m
->mount_event_source
= sd_event_source_unref(m
->mount_event_source
);
1672 mnt_unref_monitor(m
->mount_monitor
);
1673 m
->mount_monitor
= NULL
;
1676 static int mount_get_timeout(Unit
*u
, usec_t
*timeout
) {
1677 Mount
*m
= MOUNT(u
);
1681 if (!m
->timer_event_source
)
1684 r
= sd_event_source_get_time(m
->timer_event_source
, &t
);
1687 if (t
== USEC_INFINITY
)
1694 static void mount_enumerate_perpetual(Manager
*m
) {
1700 /* Whatever happens, we know for sure that the root directory is around, and cannot go away. Let's
1701 * unconditionally synthesize it here and mark it as perpetual. */
1703 u
= manager_get_unit(m
, SPECIAL_ROOT_MOUNT
);
1705 r
= unit_new_for_name(m
, sizeof(Mount
), SPECIAL_ROOT_MOUNT
, &u
);
1707 log_error_errno(r
, "Failed to allocate the special " SPECIAL_ROOT_MOUNT
" unit: %m");
1712 u
->perpetual
= true;
1713 MOUNT(u
)->deserialized_state
= MOUNT_MOUNTED
;
1715 unit_add_to_load_queue(u
);
1716 unit_add_to_dbus_queue(u
);
1719 static bool mount_is_mounted(Mount
*m
) {
1722 return UNIT(m
)->perpetual
|| FLAGS_SET(m
->proc_flags
, MOUNT_PROC_IS_MOUNTED
);
1725 static void mount_enumerate(Manager
*m
) {
1732 if (!m
->mount_monitor
) {
1735 m
->mount_monitor
= mnt_new_monitor();
1736 if (!m
->mount_monitor
) {
1741 r
= mnt_monitor_enable_kernel(m
->mount_monitor
, 1);
1743 log_error_errno(r
, "Failed to enable watching of kernel mount events: %m");
1747 r
= mnt_monitor_enable_userspace(m
->mount_monitor
, 1, NULL
);
1749 log_error_errno(r
, "Failed to enable watching of userspace mount events: %m");
1753 /* mnt_unref_monitor() will close the fd */
1754 fd
= r
= mnt_monitor_get_fd(m
->mount_monitor
);
1756 log_error_errno(r
, "Failed to acquire watch file descriptor: %m");
1760 r
= sd_event_add_io(m
->event
, &m
->mount_event_source
, fd
, EPOLLIN
, mount_dispatch_io
, m
);
1762 log_error_errno(r
, "Failed to watch mount file descriptor: %m");
1766 r
= sd_event_source_set_priority(m
->mount_event_source
, SD_EVENT_PRIORITY_NORMAL
-10);
1768 log_error_errno(r
, "Failed to adjust mount watch priority: %m");
1772 (void) sd_event_source_set_description(m
->mount_event_source
, "mount-monitor-dispatch");
1775 r
= mount_load_proc_self_mountinfo(m
, false);
1785 static void mount_process_proc_self_mountinfo(Manager
*m
);
1787 static int mount_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
1788 Manager
*m
= userdata
;
1790 usec_t next_read
= usec_add(m
->mount_last_read_usec
,
1791 m
->mount_last_duration_usec
* 10);
1793 if (now(CLOCK_MONOTONIC
) < next_read
) {
1794 /* The (current) API for getting mount events from the Linux kernel
1795 * involves getting a "something changed" notification, and then having
1796 * to re-read the entire /proc/self/mountinfo file. When there are lots
1797 * of mountpoints, this file is large and parsing it can take noticeable
1798 * time. As most of the file won't have changed, this can be seen as wasted time.
1799 * If there is a "mount storm" such as 1000 mount points being created
1800 * in quick succession, this will result in 1000 successive notification.
1801 * If we respond to every notification, we will do quadratically more work
1802 * than if we respond just once after all the notifications have arrived.
1803 * In this (pathological) case, a delay in scheduling would actually
1804 * improve throughput as we would combine notifications and parse
1805 * the file less often. We cannot expect the scheduler to notice
1806 * this pathology without help.
1807 * So when the rate of notifications means we are spending more than
1808 * 10% of real time handling them, we set a timer and stop listening
1809 * to notifications for a while.
1810 * If/when Linux provides an API which provides only details of what
1811 * has changed, this rate-limiting can be removed.
1814 r
= sd_event_source_set_enabled(source
, SD_EVENT_OFF
);
1816 log_warning_errno(r
, "Failed to disable monitoring of /proc/self/mounting, ignoring: %m");
1817 if (!m
->mount_timeout_source
) {
1818 r
= sd_event_add_time(m
->event
, &m
->mount_timeout_source
,
1822 mount_dispatch_proc_self_mountinfo_timer
,
1825 log_warning_errno(r
, "Failed to set timeout to reread /proc/self/mounting, ignoring: %m");
1831 assert(revents
& EPOLLIN
);
1833 if (fd
== mnt_monitor_get_fd(m
->mount_monitor
)) {
1834 bool rescan
= false;
1836 /* Drain all events and verify that the event is valid.
1838 * Note that libmount also monitors /run/mount mkdir if the
1839 * directory does not exist yet. The mkdir may generate event
1840 * which is irrelevant for us.
1842 * error: r < 0; valid: r == 0, false positive: rc == 1 */
1844 r
= mnt_monitor_next_change(m
->mount_monitor
, NULL
, NULL
);
1848 return log_error_errno(r
, "Failed to drain libmount events: %m");
1851 log_debug("libmount event [rescan: %s]", yes_no(rescan
));
1856 mount_process_proc_self_mountinfo(m
);
1860 static int mount_dispatch_proc_self_mountinfo_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
1861 Manager
*m
= userdata
;
1864 r
= sd_event_source_set_enabled(m
->mount_event_source
, SD_EVENT_ON
);
1866 log_warning_errno(r
, "Failed to reenable /proc/self/mountinfo monitor, ignoring: %m");
1867 m
->mount_timeout_source
= sd_event_source_unref(source
);
1868 mount_process_proc_self_mountinfo(m
);
1872 static void mount_process_proc_self_mountinfo(Manager
*m
) {
1873 _cleanup_set_free_free_ Set
*around
= NULL
, *gone
= NULL
;
1879 m
->mount_last_read_usec
= now(CLOCK_MONOTONIC
);
1880 /* If an error occurs, assume 10ms */
1881 m
->mount_last_duration_usec
= 10 * USEC_PER_MSEC
;
1883 r
= mount_load_proc_self_mountinfo(m
, true);
1885 /* Reset flags, just in case, for later calls */
1886 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
])
1887 MOUNT(u
)->proc_flags
= 0;
1892 manager_dispatch_load_queue(m
);
1894 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
]) {
1895 Mount
*mount
= MOUNT(u
);
1897 if (!mount_is_mounted(mount
)) {
1899 /* A mount point is not around right now. It
1900 * might be gone, or might never have
1903 if (mount
->from_proc_self_mountinfo
&&
1904 mount
->parameters_proc_self_mountinfo
.what
) {
1906 /* Remember that this device might just have disappeared */
1907 if (set_ensure_allocated(&gone
, &path_hash_ops
) < 0 ||
1908 set_put_strdup(gone
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
1909 log_oom(); /* we don't care too much about OOM here... */
1912 mount
->from_proc_self_mountinfo
= false;
1914 switch (mount
->state
) {
1917 /* This has just been unmounted by somebody else, follow the state change. */
1918 mount_enter_dead(mount
, MOUNT_SUCCESS
);
1925 } else if (mount
->proc_flags
& (MOUNT_PROC_JUST_MOUNTED
|MOUNT_PROC_JUST_CHANGED
)) {
1927 /* A mount point was added or changed */
1929 switch (mount
->state
) {
1934 /* This has just been mounted by somebody else, follow the state change, but let's
1935 * generate a new invocation ID for this implicitly and automatically. */
1936 (void) unit_acquire_invocation_id(u
);
1937 mount_cycle_clear(mount
);
1938 mount_enter_mounted(mount
, MOUNT_SUCCESS
);
1941 case MOUNT_MOUNTING
:
1942 mount_set_state(mount
, MOUNT_MOUNTING_DONE
);
1946 /* Nothing really changed, but let's
1947 * issue an notification call
1948 * nonetheless, in case somebody is
1949 * waiting for this. (e.g. file system
1950 * ro/rw remounts.) */
1951 mount_set_state(mount
, mount
->state
);
1956 if (mount_is_mounted(mount
) &&
1957 mount
->from_proc_self_mountinfo
&&
1958 mount
->parameters_proc_self_mountinfo
.what
) {
1959 /* Track devices currently used */
1961 if (set_ensure_allocated(&around
, &path_hash_ops
) < 0 ||
1962 set_put_strdup(around
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
1966 /* Reset the flags for later calls */
1967 mount
->proc_flags
= 0;
1970 SET_FOREACH(what
, gone
, i
) {
1971 if (set_contains(around
, what
))
1974 /* Let the device units know that the device is no longer mounted */
1975 device_found_node(m
, what
, 0, DEVICE_FOUND_MOUNT
);
1977 m
->mount_last_duration_usec
= usec_sub_unsigned(now(CLOCK_MONOTONIC
),
1978 m
->mount_last_read_usec
);
1981 static void mount_reset_failed(Unit
*u
) {
1982 Mount
*m
= MOUNT(u
);
1986 if (m
->state
== MOUNT_FAILED
)
1987 mount_set_state(m
, MOUNT_DEAD
);
1989 m
->result
= MOUNT_SUCCESS
;
1990 m
->reload_result
= MOUNT_SUCCESS
;
1993 static int mount_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
1994 Mount
*m
= MOUNT(u
);
1998 return unit_kill_common(u
, who
, signo
, -1, MOUNT(u
)->control_pid
, error
);
2001 static int mount_control_pid(Unit
*u
) {
2002 Mount
*m
= MOUNT(u
);
2006 return m
->control_pid
;
2009 static const char* const mount_exec_command_table
[_MOUNT_EXEC_COMMAND_MAX
] = {
2010 [MOUNT_EXEC_MOUNT
] = "ExecMount",
2011 [MOUNT_EXEC_UNMOUNT
] = "ExecUnmount",
2012 [MOUNT_EXEC_REMOUNT
] = "ExecRemount",
2015 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command
, MountExecCommand
);
2017 static const char* const mount_result_table
[_MOUNT_RESULT_MAX
] = {
2018 [MOUNT_SUCCESS
] = "success",
2019 [MOUNT_FAILURE_RESOURCES
] = "resources",
2020 [MOUNT_FAILURE_TIMEOUT
] = "timeout",
2021 [MOUNT_FAILURE_EXIT_CODE
] = "exit-code",
2022 [MOUNT_FAILURE_SIGNAL
] = "signal",
2023 [MOUNT_FAILURE_CORE_DUMP
] = "core-dump",
2024 [MOUNT_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
2025 [MOUNT_FAILURE_PROTOCOL
] = "protocol",
2028 DEFINE_STRING_TABLE_LOOKUP(mount_result
, MountResult
);
2030 const UnitVTable mount_vtable
= {
2031 .object_size
= sizeof(Mount
),
2032 .exec_context_offset
= offsetof(Mount
, exec_context
),
2033 .cgroup_context_offset
= offsetof(Mount
, cgroup_context
),
2034 .kill_context_offset
= offsetof(Mount
, kill_context
),
2035 .exec_runtime_offset
= offsetof(Mount
, exec_runtime
),
2036 .dynamic_creds_offset
= offsetof(Mount
, dynamic_creds
),
2042 .private_section
= "Mount",
2048 .coldplug
= mount_coldplug
,
2052 .start
= mount_start
,
2054 .reload
= mount_reload
,
2058 .serialize
= mount_serialize
,
2059 .deserialize_item
= mount_deserialize_item
,
2061 .active_state
= mount_active_state
,
2062 .sub_state_to_string
= mount_sub_state_to_string
,
2064 .may_gc
= mount_may_gc
,
2066 .sigchld_event
= mount_sigchld_event
,
2068 .reset_failed
= mount_reset_failed
,
2070 .control_pid
= mount_control_pid
,
2072 .bus_vtable
= bus_mount_vtable
,
2073 .bus_set_property
= bus_mount_set_property
,
2074 .bus_commit_properties
= bus_mount_commit_properties
,
2076 .get_timeout
= mount_get_timeout
,
2078 .can_transient
= true,
2080 .enumerate_perpetual
= mount_enumerate_perpetual
,
2081 .enumerate
= mount_enumerate
,
2082 .shutdown
= mount_shutdown
,
2084 .status_message_formats
= {
2085 .starting_stopping
= {
2086 [0] = "Mounting %s...",
2087 [1] = "Unmounting %s...",
2089 .finished_start_job
= {
2090 [JOB_DONE
] = "Mounted %s.",
2091 [JOB_FAILED
] = "Failed to mount %s.",
2092 [JOB_TIMEOUT
] = "Timed out mounting %s.",
2094 .finished_stop_job
= {
2095 [JOB_DONE
] = "Unmounted %s.",
2096 [JOB_FAILED
] = "Failed unmounting %s.",
2097 [JOB_TIMEOUT
] = "Timed out unmounting %s.",