1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "sd-messages.h"
10 #include "alloc-util.h"
11 #include "dbus-mount.h"
12 #include "dbus-unit.h"
14 #include "exit-status.h"
15 #include "format-util.h"
16 #include "fstab-util.h"
17 #include "libmount-util.h"
21 #include "mount-setup.h"
23 #include "mountpoint-util.h"
24 #include "parse-util.h"
25 #include "path-util.h"
26 #include "process-util.h"
27 #include "serialize.h"
29 #include "string-table.h"
30 #include "string-util.h"
32 #include "unit-name.h"
35 #define RETRY_UMOUNT_MAX 32
37 static const UnitActiveState state_translation_table
[_MOUNT_STATE_MAX
] = {
38 [MOUNT_DEAD
] = UNIT_INACTIVE
,
39 [MOUNT_MOUNTING
] = UNIT_ACTIVATING
,
40 [MOUNT_MOUNTING_DONE
] = UNIT_ACTIVATING
,
41 [MOUNT_MOUNTED
] = UNIT_ACTIVE
,
42 [MOUNT_REMOUNTING
] = UNIT_RELOADING
,
43 [MOUNT_UNMOUNTING
] = UNIT_DEACTIVATING
,
44 [MOUNT_REMOUNTING_SIGTERM
] = UNIT_RELOADING
,
45 [MOUNT_REMOUNTING_SIGKILL
] = UNIT_RELOADING
,
46 [MOUNT_UNMOUNTING_SIGTERM
] = UNIT_DEACTIVATING
,
47 [MOUNT_UNMOUNTING_SIGKILL
] = UNIT_DEACTIVATING
,
48 [MOUNT_FAILED
] = UNIT_FAILED
51 static int mount_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
52 static int mount_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
);
53 static int mount_process_proc_self_mountinfo(Manager
*m
);
55 static bool MOUNT_STATE_WITH_PROCESS(MountState state
) {
60 MOUNT_REMOUNTING_SIGTERM
,
61 MOUNT_REMOUNTING_SIGKILL
,
63 MOUNT_UNMOUNTING_SIGTERM
,
64 MOUNT_UNMOUNTING_SIGKILL
);
67 static bool mount_is_network(const MountParameters
*p
) {
70 if (fstab_test_option(p
->options
, "_netdev\0"))
73 if (p
->fstype
&& fstype_is_network(p
->fstype
))
79 static bool mount_is_loop(const MountParameters
*p
) {
82 if (fstab_test_option(p
->options
, "loop\0"))
88 static bool mount_is_bind(const MountParameters
*p
) {
91 if (fstab_test_option(p
->options
, "bind\0" "rbind\0"))
94 if (p
->fstype
&& STR_IN_SET(p
->fstype
, "bind", "rbind"))
100 static bool mount_is_bound_to_device(const Mount
*m
) {
101 const MountParameters
*p
;
103 if (m
->from_fragment
)
106 p
= &m
->parameters_proc_self_mountinfo
;
107 return fstab_test_option(p
->options
, "x-systemd.device-bound\0");
110 static bool mount_needs_quota(const MountParameters
*p
) {
113 /* Quotas are not enabled on network filesystems, but we want them, for example, on storage connected via
114 * iscsi. We hence don't use mount_is_network() here, as that would also return true for _netdev devices. */
115 if (p
->fstype
&& fstype_is_network(p
->fstype
))
118 if (mount_is_bind(p
))
121 return fstab_test_option(p
->options
,
122 "usrquota\0" "grpquota\0" "quota\0" "usrjquota\0" "grpjquota\0");
125 static void mount_init(Unit
*u
) {
129 assert(u
->load_state
== UNIT_STUB
);
131 m
->timeout_usec
= u
->manager
->default_timeout_start_usec
;
133 m
->exec_context
.std_output
= u
->manager
->default_std_output
;
134 m
->exec_context
.std_error
= u
->manager
->default_std_error
;
136 m
->directory_mode
= 0755;
138 /* We need to make sure that /usr/bin/mount is always called
139 * in the same process group as us, so that the autofs kernel
140 * side doesn't send us another mount request while we are
141 * already trying to comply its last one. */
142 m
->exec_context
.same_pgrp
= true;
144 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
146 u
->ignore_on_isolate
= true;
149 static int mount_arm_timer(Mount
*m
, usec_t usec
) {
154 if (m
->timer_event_source
) {
155 r
= sd_event_source_set_time(m
->timer_event_source
, usec
);
159 return sd_event_source_set_enabled(m
->timer_event_source
, SD_EVENT_ONESHOT
);
162 if (usec
== USEC_INFINITY
)
165 r
= sd_event_add_time(
166 UNIT(m
)->manager
->event
,
167 &m
->timer_event_source
,
170 mount_dispatch_timer
, m
);
174 (void) sd_event_source_set_description(m
->timer_event_source
, "mount-timer");
179 static void mount_unwatch_control_pid(Mount
*m
) {
182 if (m
->control_pid
<= 0)
185 unit_unwatch_pid(UNIT(m
), m
->control_pid
);
189 static void mount_parameters_done(MountParameters
*p
) {
192 p
->what
= mfree(p
->what
);
193 p
->options
= mfree(p
->options
);
194 p
->fstype
= mfree(p
->fstype
);
197 static void mount_done(Unit
*u
) {
202 m
->where
= mfree(m
->where
);
204 mount_parameters_done(&m
->parameters_proc_self_mountinfo
);
205 mount_parameters_done(&m
->parameters_fragment
);
207 m
->exec_runtime
= exec_runtime_unref(m
->exec_runtime
, false);
208 exec_command_done_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
209 m
->control_command
= NULL
;
211 dynamic_creds_unref(&m
->dynamic_creds
);
213 mount_unwatch_control_pid(m
);
215 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
218 _pure_
static MountParameters
* get_mount_parameters_fragment(Mount
*m
) {
221 if (m
->from_fragment
)
222 return &m
->parameters_fragment
;
227 _pure_
static MountParameters
* get_mount_parameters(Mount
*m
) {
230 if (m
->from_proc_self_mountinfo
)
231 return &m
->parameters_proc_self_mountinfo
;
233 return get_mount_parameters_fragment(m
);
236 static int update_parameters_proc_self_mountinfo(
240 const char *fstype
) {
245 p
= &m
->parameters_proc_self_mountinfo
;
247 r
= free_and_strdup(&p
->what
, what
);
251 q
= free_and_strdup(&p
->options
, options
);
255 w
= free_and_strdup(&p
->fstype
, fstype
);
259 return r
> 0 || q
> 0 || w
> 0;
262 static int mount_add_mount_dependencies(Mount
*m
) {
271 if (!path_equal(m
->where
, "/")) {
272 _cleanup_free_
char *parent
= NULL
;
274 /* Adds in links to other mount points that might lie further up in the hierarchy */
276 parent
= dirname_malloc(m
->where
);
280 r
= unit_require_mounts_for(UNIT(m
), parent
, UNIT_DEPENDENCY_IMPLICIT
);
285 /* Adds in dependencies to other mount points that might be needed for the source path (if this is a bind mount
286 * or a loop mount) to be available. */
287 pm
= get_mount_parameters_fragment(m
);
288 if (pm
&& pm
->what
&&
289 path_is_absolute(pm
->what
) &&
290 (mount_is_bind(pm
) || mount_is_loop(pm
) || !mount_is_network(pm
))) {
292 r
= unit_require_mounts_for(UNIT(m
), pm
->what
, UNIT_DEPENDENCY_FILE
);
297 /* Adds in dependencies to other units that use this path or paths further down in the hierarchy */
298 s
= manager_get_units_requiring_mounts_for(UNIT(m
)->manager
, m
->where
);
299 SET_FOREACH(other
, s
, i
) {
301 if (other
->load_state
!= UNIT_LOADED
)
304 if (other
== UNIT(m
))
307 r
= unit_add_dependency(other
, UNIT_AFTER
, UNIT(m
), true, UNIT_DEPENDENCY_PATH
);
311 if (UNIT(m
)->fragment_path
) {
312 /* If we have fragment configuration, then make this dependency required */
313 r
= unit_add_dependency(other
, UNIT_REQUIRES
, UNIT(m
), true, UNIT_DEPENDENCY_PATH
);
322 static int mount_add_device_dependencies(Mount
*m
) {
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
, "/"))
352 /* Mount units from /proc/self/mountinfo are not bound to devices
353 * by default since they're subject to races when devices are
354 * unplugged. But the user can still force this dep with an
355 * appropriate option (or udev property) so the mount units are
356 * automatically stopped when the device disappears suddenly. */
357 dep
= mount_is_bound_to_device(m
) ? UNIT_BINDS_TO
: UNIT_REQUIRES
;
359 /* We always use 'what' from /proc/self/mountinfo if mounted */
360 mask
= m
->from_proc_self_mountinfo
? UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
: UNIT_DEPENDENCY_FILE
;
362 r
= unit_add_node_dependency(UNIT(m
), p
->what
, false, dep
, mask
);
369 static int mount_add_quota_dependencies(Mount
*m
) {
370 UnitDependencyMask mask
;
376 if (!MANAGER_IS_SYSTEM(UNIT(m
)->manager
))
379 p
= get_mount_parameters_fragment(m
);
383 if (!mount_needs_quota(p
))
386 mask
= m
->from_fragment
? UNIT_DEPENDENCY_FILE
: UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
;
388 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTACHECK_SERVICE
, true, mask
);
392 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTAON_SERVICE
, true, mask
);
399 static bool mount_is_extrinsic(Mount
*m
) {
403 /* Returns true for all units that are "magic" and should be excluded from the usual start-up and shutdown
404 * dependencies. We call them "extrinsic" here, as they are generally mounted outside of the systemd dependency
405 * logic. We shouldn't attempt to manage them ourselves but it's fine if the user operates on them with us. */
407 if (!MANAGER_IS_SYSTEM(UNIT(m
)->manager
)) /* We only automatically manage mounts if we are in system mode */
410 if (PATH_IN_SET(m
->where
, /* Don't bother with the OS data itself */
415 if (PATH_STARTSWITH_SET(m
->where
,
416 "/run/initramfs", /* This should stay around from before we boot until after we shutdown */
417 "/proc", /* All of this is API VFS */
418 "/sys", /* … dito … */
419 "/dev")) /* … dito … */
422 /* If this is an initrd mount, and we are not in the initrd, then leave this around forever, too. */
423 p
= get_mount_parameters(m
);
424 if (p
&& fstab_test_option(p
->options
, "x-initrd.mount\0") && !in_initrd())
430 static int mount_add_default_dependencies(Mount
*m
) {
431 const char *after
, *before
;
432 UnitDependencyMask mask
;
439 if (!UNIT(m
)->default_dependencies
)
442 /* We do not add any default dependencies to /, /usr or /run/initramfs/, since they are guaranteed to stay
443 * mounted the whole time, since our system is on it. Also, don't bother with anything mounted below virtual
444 * file systems, it's also going to be virtual, and hence not worth the effort. */
445 if (mount_is_extrinsic(m
))
448 p
= get_mount_parameters(m
);
452 mask
= m
->from_fragment
? UNIT_DEPENDENCY_FILE
: UNIT_DEPENDENCY_MOUNTINFO_DEFAULT
;
453 nofail
= m
->from_fragment
? fstab_test_yes_no_option(m
->parameters_fragment
.options
, "nofail\0" "fail\0") : false;
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
;
485 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_BEFORE
, before
, true, mask
);
490 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, after
, true, mask
);
494 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, true, mask
);
498 /* If this is a tmpfs mount then we have to unmount it before we try to deactivate swaps */
499 if (streq_ptr(p
->fstype
, "tmpfs")) {
500 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, SPECIAL_SWAP_TARGET
, true, mask
);
508 static int mount_verify(Mount
*m
) {
509 _cleanup_free_
char *e
= NULL
;
515 if (UNIT(m
)->load_state
!= UNIT_LOADED
)
518 if (!m
->from_fragment
&& !m
->from_proc_self_mountinfo
&& !UNIT(m
)->perpetual
)
521 r
= unit_name_from_path(m
->where
, ".mount", &e
);
523 return log_unit_error_errno(UNIT(m
), r
, "Failed to generate unit name from mount path: %m");
525 if (!unit_has_name(UNIT(m
), e
)) {
526 log_unit_error(UNIT(m
), "Where= setting doesn't match unit name. Refusing.");
530 if (mount_point_is_api(m
->where
) || mount_point_ignore(m
->where
)) {
531 log_unit_error(UNIT(m
), "Cannot create mount unit for API file system %s. Refusing.", m
->where
);
535 p
= get_mount_parameters_fragment(m
);
537 log_unit_error(UNIT(m
), "What= setting is missing. Refusing.");
541 if (m
->exec_context
.pam_name
&& m
->kill_context
.kill_mode
!= KILL_CONTROL_GROUP
) {
542 log_unit_error(UNIT(m
), "Unit has PAM enabled. Kill mode must be set to control-group'. Refusing.");
549 static int mount_add_extras(Mount
*m
) {
555 /* Note: this call might be called after we already have been loaded once (and even when it has already been
556 * activated), in case data from /proc/self/mountinfo has changed. This means all code here needs to be ready
557 * to run with an already set up unit. */
559 if (u
->fragment_path
)
560 m
->from_fragment
= true;
563 r
= unit_name_to_path(u
->id
, &m
->where
);
568 path_simplify(m
->where
, false);
570 if (!u
->description
) {
571 r
= unit_set_description(u
, m
->where
);
576 r
= mount_add_device_dependencies(m
);
580 r
= mount_add_mount_dependencies(m
);
584 r
= mount_add_quota_dependencies(m
);
588 r
= unit_patch_contexts(u
);
592 r
= unit_add_exec_dependencies(u
, &m
->exec_context
);
596 r
= unit_set_default_slice(u
);
600 r
= mount_add_default_dependencies(m
);
607 static int mount_load_root_mount(Unit
*u
) {
610 if (!unit_has_name(u
, SPECIAL_ROOT_MOUNT
))
614 u
->default_dependencies
= false;
616 /* The stdio/kmsg bridge socket is on /, in order to avoid a dep loop, don't use kmsg logging for -.mount */
617 MOUNT(u
)->exec_context
.std_output
= EXEC_OUTPUT_NULL
;
618 MOUNT(u
)->exec_context
.std_input
= EXEC_INPUT_NULL
;
621 u
->description
= strdup("Root Mount");
626 static int mount_load(Unit
*u
) {
631 assert(u
->load_state
== UNIT_STUB
);
633 r
= mount_load_root_mount(u
);
635 if (m
->from_proc_self_mountinfo
|| u
->perpetual
)
636 q
= unit_load_fragment_and_dropin_optional(u
);
638 q
= unit_load_fragment_and_dropin(u
);
640 /* Add in some extras. Note we do this in all cases (even if we failed to load the unit) when announced by the
641 * kernel, because we need some things to be set up no matter what when the kernel establishes a mount and thus
642 * we need to update the state in our unit to track it. After all, consider that we don't allow changing the
643 * 'slice' field for a unit once it is active. */
644 if (u
->load_state
== UNIT_LOADED
|| m
->from_proc_self_mountinfo
|| u
->perpetual
)
645 w
= mount_add_extras(m
);
656 return mount_verify(m
);
659 static void mount_set_state(Mount
*m
, MountState state
) {
660 MountState old_state
;
663 if (m
->state
!= state
)
664 bus_unit_send_pending_change_signal(UNIT(m
), false);
666 old_state
= m
->state
;
669 if (!MOUNT_STATE_WITH_PROCESS(state
)) {
670 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
671 mount_unwatch_control_pid(m
);
672 m
->control_command
= NULL
;
673 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
676 if (state
!= old_state
)
677 log_unit_debug(UNIT(m
), "Changed %s -> %s", mount_state_to_string(old_state
), mount_state_to_string(state
));
679 unit_notify(UNIT(m
), state_translation_table
[old_state
], state_translation_table
[state
],
680 m
->reload_result
== MOUNT_SUCCESS
? 0 : UNIT_NOTIFY_RELOAD_FAILURE
);
683 static int mount_coldplug(Unit
*u
) {
685 MountState new_state
= MOUNT_DEAD
;
689 assert(m
->state
== MOUNT_DEAD
);
691 if (m
->deserialized_state
!= m
->state
)
692 new_state
= m
->deserialized_state
;
693 else if (m
->from_proc_self_mountinfo
)
694 new_state
= MOUNT_MOUNTED
;
696 if (new_state
== m
->state
)
699 if (m
->control_pid
> 0 &&
700 pid_is_unwaited(m
->control_pid
) &&
701 MOUNT_STATE_WITH_PROCESS(new_state
)) {
703 r
= unit_watch_pid(UNIT(m
), m
->control_pid
, false);
707 r
= mount_arm_timer(m
, usec_add(u
->state_change_timestamp
.monotonic
, m
->timeout_usec
));
712 if (!IN_SET(new_state
, MOUNT_DEAD
, MOUNT_FAILED
)) {
713 (void) unit_setup_dynamic_creds(u
);
714 (void) unit_setup_exec_runtime(u
);
717 mount_set_state(m
, new_state
);
721 static void mount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
722 char buf
[FORMAT_TIMESPAN_MAX
];
729 p
= get_mount_parameters(m
);
732 "%sMount State: %s\n"
736 "%sFile System Type: %s\n"
738 "%sFrom /proc/self/mountinfo: %s\n"
739 "%sFrom fragment: %s\n"
741 "%sDirectoryMode: %04o\n"
742 "%sSloppyOptions: %s\n"
743 "%sLazyUnmount: %s\n"
744 "%sForceUnmount: %s\n"
745 "%sTimeoutSec: %s\n",
746 prefix
, mount_state_to_string(m
->state
),
747 prefix
, mount_result_to_string(m
->result
),
749 prefix
, p
? strna(p
->what
) : "n/a",
750 prefix
, p
? strna(p
->fstype
) : "n/a",
751 prefix
, p
? strna(p
->options
) : "n/a",
752 prefix
, yes_no(m
->from_proc_self_mountinfo
),
753 prefix
, yes_no(m
->from_fragment
),
754 prefix
, yes_no(mount_is_extrinsic(m
)),
755 prefix
, m
->directory_mode
,
756 prefix
, yes_no(m
->sloppy_options
),
757 prefix
, yes_no(m
->lazy_unmount
),
758 prefix
, yes_no(m
->force_unmount
),
759 prefix
, format_timespan(buf
, sizeof(buf
), m
->timeout_usec
, USEC_PER_SEC
));
761 if (m
->control_pid
> 0)
763 "%sControl PID: "PID_FMT
"\n",
764 prefix
, m
->control_pid
);
766 exec_context_dump(&m
->exec_context
, f
, prefix
);
767 kill_context_dump(&m
->kill_context
, f
, prefix
);
768 cgroup_context_dump(&m
->cgroup_context
, f
, prefix
);
771 static int mount_spawn(Mount
*m
, ExecCommand
*c
, pid_t
*_pid
) {
773 _cleanup_(exec_params_clear
) ExecParameters exec_params
= {
774 .flags
= EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
,
787 r
= unit_prepare_exec(UNIT(m
));
791 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->timeout_usec
));
795 r
= unit_set_exec_params(UNIT(m
), &exec_params
);
799 r
= exec_spawn(UNIT(m
),
809 r
= unit_watch_pid(UNIT(m
), pid
, true);
818 static void mount_enter_dead(Mount
*m
, MountResult f
) {
821 if (m
->result
== MOUNT_SUCCESS
)
824 unit_log_result(UNIT(m
), m
->result
== MOUNT_SUCCESS
, mount_result_to_string(m
->result
));
825 mount_set_state(m
, m
->result
!= MOUNT_SUCCESS
? MOUNT_FAILED
: MOUNT_DEAD
);
827 m
->exec_runtime
= exec_runtime_unref(m
->exec_runtime
, true);
829 unit_destroy_runtime_directory(UNIT(m
), &m
->exec_context
);
831 unit_unref_uid_gid(UNIT(m
), true);
833 dynamic_creds_destroy(&m
->dynamic_creds
);
835 /* Any dependencies based on /proc/self/mountinfo are now stale */
836 unit_remove_dependencies(UNIT(m
), UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
);
839 static void mount_enter_mounted(Mount
*m
, MountResult f
) {
842 if (m
->result
== MOUNT_SUCCESS
)
845 mount_set_state(m
, MOUNT_MOUNTED
);
848 static void mount_enter_dead_or_mounted(Mount
*m
, MountResult f
) {
851 /* Enter DEAD or MOUNTED state, depending on what the kernel currently says about the mount point. We use this
852 * whenever we executed an operation, so that our internal state reflects what the kernel says again, after all
853 * ultimately we just mirror the kernel's internal state on this. */
855 if (m
->from_proc_self_mountinfo
)
856 mount_enter_mounted(m
, f
);
858 mount_enter_dead(m
, f
);
861 static int state_to_kill_operation(MountState state
) {
864 case MOUNT_REMOUNTING_SIGTERM
:
865 case MOUNT_UNMOUNTING_SIGTERM
:
866 return KILL_TERMINATE
;
868 case MOUNT_REMOUNTING_SIGKILL
:
869 case MOUNT_UNMOUNTING_SIGKILL
:
873 return _KILL_OPERATION_INVALID
;
877 static void mount_enter_signal(Mount
*m
, MountState state
, MountResult f
) {
882 if (m
->result
== MOUNT_SUCCESS
)
885 r
= unit_kill_context(
888 state_to_kill_operation(state
),
896 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->timeout_usec
));
900 mount_set_state(m
, state
);
901 } else if (state
== MOUNT_REMOUNTING_SIGTERM
&& m
->kill_context
.send_sigkill
)
902 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
903 else if (IN_SET(state
, MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
))
904 mount_enter_mounted(m
, MOUNT_SUCCESS
);
905 else if (state
== MOUNT_UNMOUNTING_SIGTERM
&& m
->kill_context
.send_sigkill
)
906 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
908 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
913 log_unit_warning_errno(UNIT(m
), r
, "Failed to kill processes: %m");
914 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
917 static void mount_enter_unmounting(Mount
*m
) {
922 /* Start counting our attempts */
923 if (!IN_SET(m
->state
,
925 MOUNT_UNMOUNTING_SIGTERM
,
926 MOUNT_UNMOUNTING_SIGKILL
))
927 m
->n_retry_umount
= 0;
929 m
->control_command_id
= MOUNT_EXEC_UNMOUNT
;
930 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_UNMOUNT
;
932 r
= exec_command_set(m
->control_command
, UMOUNT_PATH
, m
->where
, "-c", NULL
);
933 if (r
>= 0 && m
->lazy_unmount
)
934 r
= exec_command_append(m
->control_command
, "-l", NULL
);
935 if (r
>= 0 && m
->force_unmount
)
936 r
= exec_command_append(m
->control_command
, "-f", NULL
);
940 mount_unwatch_control_pid(m
);
942 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
946 mount_set_state(m
, MOUNT_UNMOUNTING
);
951 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'umount' task: %m");
952 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
955 static void mount_enter_mounting(Mount
*m
) {
961 r
= unit_fail_if_noncanonical(UNIT(m
), m
->where
);
965 (void) mkdir_p_label(m
->where
, m
->directory_mode
);
967 unit_warn_if_dir_nonempty(UNIT(m
), m
->where
);
968 unit_warn_leftover_processes(UNIT(m
));
970 m
->control_command_id
= MOUNT_EXEC_MOUNT
;
971 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_MOUNT
;
973 /* Create the source directory for bind-mounts if needed */
974 p
= get_mount_parameters_fragment(m
);
975 if (p
&& mount_is_bind(p
))
976 (void) mkdir_p_label(p
->what
, m
->directory_mode
);
979 _cleanup_free_
char *opts
= NULL
;
981 r
= fstab_filter_options(p
->options
, "nofail\0" "noauto\0" "auto\0", NULL
, NULL
, &opts
);
985 r
= exec_command_set(m
->control_command
, MOUNT_PATH
, p
->what
, m
->where
, NULL
);
986 if (r
>= 0 && m
->sloppy_options
)
987 r
= exec_command_append(m
->control_command
, "-s", NULL
);
988 if (r
>= 0 && p
->fstype
)
989 r
= exec_command_append(m
->control_command
, "-t", p
->fstype
, NULL
);
990 if (r
>= 0 && !isempty(opts
))
991 r
= exec_command_append(m
->control_command
, "-o", opts
, NULL
);
997 mount_unwatch_control_pid(m
);
999 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
1003 mount_set_state(m
, MOUNT_MOUNTING
);
1008 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'mount' task: %m");
1009 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
1012 static void mount_set_reload_result(Mount
*m
, MountResult result
) {
1015 /* Only store the first error we encounter */
1016 if (m
->reload_result
!= MOUNT_SUCCESS
)
1019 m
->reload_result
= result
;
1022 static void mount_enter_remounting(Mount
*m
) {
1028 /* Reset reload result when we are about to start a new remount operation */
1029 m
->reload_result
= MOUNT_SUCCESS
;
1031 m
->control_command_id
= MOUNT_EXEC_REMOUNT
;
1032 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_REMOUNT
;
1034 p
= get_mount_parameters_fragment(m
);
1039 o
= strjoina("remount,", p
->options
);
1043 r
= exec_command_set(m
->control_command
, MOUNT_PATH
,
1046 if (r
>= 0 && m
->sloppy_options
)
1047 r
= exec_command_append(m
->control_command
, "-s", NULL
);
1048 if (r
>= 0 && p
->fstype
)
1049 r
= exec_command_append(m
->control_command
, "-t", p
->fstype
, NULL
);
1055 mount_unwatch_control_pid(m
);
1057 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
1061 mount_set_state(m
, MOUNT_REMOUNTING
);
1066 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'remount' task: %m");
1067 mount_set_reload_result(m
, MOUNT_FAILURE_RESOURCES
);
1068 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1071 static void mount_cycle_clear(Mount
*m
) {
1074 /* Clear all state we shall forget for this new cycle */
1076 m
->result
= MOUNT_SUCCESS
;
1077 m
->reload_result
= MOUNT_SUCCESS
;
1078 exec_command_reset_status_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
1079 UNIT(m
)->reset_accounting
= true;
1082 static int mount_start(Unit
*u
) {
1083 Mount
*m
= MOUNT(u
);
1088 /* We cannot fulfill this request right now, try again later
1090 if (IN_SET(m
->state
,
1092 MOUNT_UNMOUNTING_SIGTERM
,
1093 MOUNT_UNMOUNTING_SIGKILL
))
1096 /* Already on it! */
1097 if (m
->state
== MOUNT_MOUNTING
)
1100 assert(IN_SET(m
->state
, MOUNT_DEAD
, MOUNT_FAILED
));
1102 r
= unit_test_start_limit(u
);
1104 mount_enter_dead(m
, MOUNT_FAILURE_START_LIMIT_HIT
);
1108 r
= unit_acquire_invocation_id(u
);
1112 mount_cycle_clear(m
);
1113 mount_enter_mounting(m
);
1118 static int mount_stop(Unit
*u
) {
1119 Mount
*m
= MOUNT(u
);
1125 case MOUNT_UNMOUNTING
:
1126 case MOUNT_UNMOUNTING_SIGKILL
:
1127 case MOUNT_UNMOUNTING_SIGTERM
:
1131 case MOUNT_MOUNTING
:
1132 case MOUNT_MOUNTING_DONE
:
1133 case MOUNT_REMOUNTING
:
1134 /* If we are still waiting for /bin/mount, we go directly into kill mode. */
1135 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_SUCCESS
);
1138 case MOUNT_REMOUNTING_SIGTERM
:
1139 /* If we are already waiting for a hung remount, convert this to the matching unmounting state */
1140 mount_set_state(m
, MOUNT_UNMOUNTING_SIGTERM
);
1143 case MOUNT_REMOUNTING_SIGKILL
:
1145 mount_set_state(m
, MOUNT_UNMOUNTING_SIGKILL
);
1149 mount_enter_unmounting(m
);
1153 assert_not_reached("Unexpected state.");
1157 static int mount_reload(Unit
*u
) {
1158 Mount
*m
= MOUNT(u
);
1161 assert(m
->state
== MOUNT_MOUNTED
);
1163 mount_enter_remounting(m
);
1168 static int mount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1169 Mount
*m
= MOUNT(u
);
1175 (void) serialize_item(f
, "state", mount_state_to_string(m
->state
));
1176 (void) serialize_item(f
, "result", mount_result_to_string(m
->result
));
1177 (void) serialize_item(f
, "reload-result", mount_result_to_string(m
->reload_result
));
1178 (void) serialize_item_format(f
, "n-retry-umount", "%u", m
->n_retry_umount
);
1180 if (m
->control_pid
> 0)
1181 (void) serialize_item_format(f
, "control-pid", PID_FMT
, m
->control_pid
);
1183 if (m
->control_command_id
>= 0)
1184 (void) serialize_item(f
, "control-command", mount_exec_command_to_string(m
->control_command_id
));
1189 static int mount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1190 Mount
*m
= MOUNT(u
);
1198 if (streq(key
, "state")) {
1201 if ((state
= mount_state_from_string(value
)) < 0)
1202 log_unit_debug(u
, "Failed to parse state value: %s", value
);
1204 m
->deserialized_state
= state
;
1206 } else if (streq(key
, "result")) {
1209 f
= mount_result_from_string(value
);
1211 log_unit_debug(u
, "Failed to parse result value: %s", value
);
1212 else if (f
!= MOUNT_SUCCESS
)
1215 } else if (streq(key
, "reload-result")) {
1218 f
= mount_result_from_string(value
);
1220 log_unit_debug(u
, "Failed to parse reload result value: %s", value
);
1221 else if (f
!= MOUNT_SUCCESS
)
1222 m
->reload_result
= f
;
1224 } else if (streq(key
, "n-retry-umount")) {
1226 r
= safe_atou(value
, &m
->n_retry_umount
);
1228 log_unit_debug(u
, "Failed to parse n-retry-umount value: %s", value
);
1230 } else if (streq(key
, "control-pid")) {
1232 if (parse_pid(value
, &m
->control_pid
) < 0)
1233 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
1235 } else if (streq(key
, "control-command")) {
1236 MountExecCommand id
;
1238 id
= mount_exec_command_from_string(value
);
1240 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
1242 m
->control_command_id
= id
;
1243 m
->control_command
= m
->exec_command
+ id
;
1246 log_unit_debug(u
, "Unknown serialization key: %s", key
);
1251 _pure_
static UnitActiveState
mount_active_state(Unit
*u
) {
1254 return state_translation_table
[MOUNT(u
)->state
];
1257 _pure_
static const char *mount_sub_state_to_string(Unit
*u
) {
1260 return mount_state_to_string(MOUNT(u
)->state
);
1263 _pure_
static bool mount_may_gc(Unit
*u
) {
1264 Mount
*m
= MOUNT(u
);
1268 if (m
->from_proc_self_mountinfo
)
1274 static void mount_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1275 Mount
*m
= MOUNT(u
);
1281 if (pid
!= m
->control_pid
)
1284 /* So here's the thing, we really want to know before /usr/bin/mount or /usr/bin/umount exit whether
1285 * they established/remove a mount. This is important when mounting, but even more so when unmounting
1286 * since we need to deal with nested mounts and otherwise cannot safely determine whether to repeat
1287 * the unmounts. In theory, the kernel fires /proc/self/mountinfo changes off before returning from
1288 * the mount() or umount() syscalls, and thus we should see the changes to the proc file before we
1289 * process the waitid() for the /usr/bin/(u)mount processes. However, this is unfortunately racy: we
1290 * have to waitid() for processes using P_ALL (since we need to reap unexpected children that got
1291 * reparented to PID 1), but when using P_ALL we might end up reaping processes that terminated just
1292 * instants ago, i.e. already after our last event loop iteration (i.e. after the last point we might
1293 * have noticed /proc/self/mountinfo events via epoll). This means event loop priorities for
1294 * processing SIGCHLD vs. /proc/self/mountinfo IO events are not as relevant as we want. To fix that
1295 * race, let's explicitly scan /proc/self/mountinfo before we start processing /usr/bin/(u)mount
1296 * dying. It's ugly, but it makes our ordering systematic again, and makes sure we always see
1297 * /proc/self/mountinfo changes before our mount/umount exits. */
1298 (void) mount_process_proc_self_mountinfo(u
->manager
);
1302 if (is_clean_exit(code
, status
, EXIT_CLEAN_COMMAND
, NULL
))
1304 else if (code
== CLD_EXITED
)
1305 f
= MOUNT_FAILURE_EXIT_CODE
;
1306 else if (code
== CLD_KILLED
)
1307 f
= MOUNT_FAILURE_SIGNAL
;
1308 else if (code
== CLD_DUMPED
)
1309 f
= MOUNT_FAILURE_CORE_DUMP
;
1311 assert_not_reached("Unknown code");
1313 if (IN_SET(m
->state
, MOUNT_REMOUNTING
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_REMOUNTING_SIGTERM
))
1314 mount_set_reload_result(m
, f
);
1315 else if (m
->result
== MOUNT_SUCCESS
)
1318 if (m
->control_command
) {
1319 exec_status_exit(&m
->control_command
->exec_status
, &m
->exec_context
, pid
, code
, status
);
1321 m
->control_command
= NULL
;
1322 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
1325 unit_log_process_exit(
1328 mount_exec_command_to_string(m
->control_command_id
),
1332 /* Note that due to the io event priority logic, we can be sure the new mountinfo is loaded
1333 * before we process the SIGCHLD for the mount command. */
1337 case MOUNT_MOUNTING
:
1338 /* Our mount point has not appeared in mountinfo. Something went wrong. */
1340 if (f
== MOUNT_SUCCESS
) {
1341 /* Either /bin/mount has an unexpected definition of success,
1342 * or someone raced us and we lost. */
1343 log_unit_warning(UNIT(m
), "Mount process finished, but there is no mount.");
1344 f
= MOUNT_FAILURE_PROTOCOL
;
1346 mount_enter_dead(m
, f
);
1349 case MOUNT_MOUNTING_DONE
:
1350 mount_enter_mounted(m
, f
);
1353 case MOUNT_REMOUNTING
:
1354 case MOUNT_REMOUNTING_SIGTERM
:
1355 case MOUNT_REMOUNTING_SIGKILL
:
1356 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1359 case MOUNT_UNMOUNTING
:
1361 if (f
== MOUNT_SUCCESS
&& m
->from_proc_self_mountinfo
) {
1363 /* Still a mount point? If so, let's try again. Most likely there were multiple mount points
1364 * stacked on top of each other. We might exceed the timeout specified by the user overall,
1365 * but we will stop as soon as any one umount times out. */
1367 if (m
->n_retry_umount
< RETRY_UMOUNT_MAX
) {
1368 log_unit_debug(u
, "Mount still present, trying again.");
1369 m
->n_retry_umount
++;
1370 mount_enter_unmounting(m
);
1372 log_unit_warning(u
, "Mount still present after %u attempts to unmount, giving up.", m
->n_retry_umount
);
1373 mount_enter_mounted(m
, f
);
1376 mount_enter_dead_or_mounted(m
, f
);
1380 case MOUNT_UNMOUNTING_SIGKILL
:
1381 case MOUNT_UNMOUNTING_SIGTERM
:
1382 mount_enter_dead_or_mounted(m
, f
);
1386 assert_not_reached("Uh, control process died at wrong time.");
1389 /* Notify clients about changed exit status */
1390 unit_add_to_dbus_queue(u
);
1393 static int mount_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
1394 Mount
*m
= MOUNT(userdata
);
1397 assert(m
->timer_event_source
== source
);
1401 case MOUNT_MOUNTING
:
1402 case MOUNT_MOUNTING_DONE
:
1403 log_unit_warning(UNIT(m
), "Mounting timed out. Terminating.");
1404 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1407 case MOUNT_REMOUNTING
:
1408 log_unit_warning(UNIT(m
), "Remounting timed out. Terminating remount process.");
1409 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1410 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGTERM
, MOUNT_SUCCESS
);
1413 case MOUNT_REMOUNTING_SIGTERM
:
1414 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1416 if (m
->kill_context
.send_sigkill
) {
1417 log_unit_warning(UNIT(m
), "Remounting timed out. Killing.");
1418 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
1420 log_unit_warning(UNIT(m
), "Remounting timed out. Skipping SIGKILL. Ignoring.");
1421 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1425 case MOUNT_REMOUNTING_SIGKILL
:
1426 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1428 log_unit_warning(UNIT(m
), "Mount process still around after SIGKILL. Ignoring.");
1429 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1432 case MOUNT_UNMOUNTING
:
1433 log_unit_warning(UNIT(m
), "Unmounting timed out. Terminating.");
1434 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1437 case MOUNT_UNMOUNTING_SIGTERM
:
1438 if (m
->kill_context
.send_sigkill
) {
1439 log_unit_warning(UNIT(m
), "Mount process timed out. Killing.");
1440 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_FAILURE_TIMEOUT
);
1442 log_unit_warning(UNIT(m
), "Mount process timed out. Skipping SIGKILL. Ignoring.");
1443 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1447 case MOUNT_UNMOUNTING_SIGKILL
:
1448 log_unit_warning(UNIT(m
), "Mount process still around after SIGKILL. Ignoring.");
1449 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1453 assert_not_reached("Timeout at wrong time.");
1459 static int mount_setup_new_unit(
1464 const char *options
,
1466 MountProcFlags
*ret_flags
,
1469 _cleanup_(unit_freep
) Unit
*u
= NULL
;
1477 r
= unit_new_for_name(m
, sizeof(Mount
), name
, &u
);
1481 r
= free_and_strdup(&u
->source_path
, "/proc/self/mountinfo");
1485 r
= free_and_strdup(&MOUNT(u
)->where
, where
);
1489 r
= update_parameters_proc_self_mountinfo(MOUNT(u
), what
, options
, fstype
);
1493 /* This unit was generated because /proc/self/mountinfo reported it. Remember this, so that by the time we load
1494 * the unit file for it (and thus add in extra deps right after) we know what source to attributes the deps
1496 MOUNT(u
)->from_proc_self_mountinfo
= true;
1498 /* We have only allocated the stub now, let's enqueue this unit for loading now, so that everything else is
1500 unit_add_to_load_queue(u
);
1502 *ret_flags
= MOUNT_PROC_IS_MOUNTED
| MOUNT_PROC_JUST_MOUNTED
| MOUNT_PROC_JUST_CHANGED
;
1507 static int mount_setup_existing_unit(
1511 const char *options
,
1513 MountProcFlags
*ret_flags
) {
1515 MountProcFlags flags
= MOUNT_PROC_IS_MOUNTED
;
1521 if (!MOUNT(u
)->where
) {
1522 MOUNT(u
)->where
= strdup(where
);
1523 if (!MOUNT(u
)->where
)
1527 r
= update_parameters_proc_self_mountinfo(MOUNT(u
), what
, options
, fstype
);
1531 flags
|= MOUNT_PROC_JUST_CHANGED
;
1533 if (!MOUNT(u
)->from_proc_self_mountinfo
|| FLAGS_SET(MOUNT(u
)->proc_flags
, MOUNT_PROC_JUST_MOUNTED
))
1534 flags
|= MOUNT_PROC_JUST_MOUNTED
;
1536 MOUNT(u
)->from_proc_self_mountinfo
= true;
1538 if (IN_SET(u
->load_state
, UNIT_NOT_FOUND
, UNIT_BAD_SETTING
, UNIT_ERROR
)) {
1539 /* The unit was previously not found or otherwise not loaded. Now that the unit shows up in
1540 * /proc/self/mountinfo we should reconsider it this, hence set it to UNIT_LOADED. */
1541 u
->load_state
= UNIT_LOADED
;
1544 flags
|= MOUNT_PROC_JUST_CHANGED
;
1547 if (FLAGS_SET(flags
, MOUNT_PROC_JUST_CHANGED
)) {
1548 /* If things changed, then make sure that all deps are regenerated. Let's
1549 * first remove all automatic deps, and then add in the new ones. */
1551 unit_remove_dependencies(u
, UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
);
1553 r
= mount_add_extras(MOUNT(u
));
1562 static int mount_setup_unit(
1566 const char *options
,
1570 _cleanup_free_
char *e
= NULL
;
1571 MountProcFlags flags
;
1581 /* Ignore API mount points. They should never be referenced in
1582 * dependencies ever. */
1583 if (mount_point_is_api(where
) || mount_point_ignore(where
))
1586 if (streq(fstype
, "autofs"))
1589 /* probably some kind of swap, ignore */
1590 if (!is_path(where
))
1593 r
= unit_name_from_path(where
, ".mount", &e
);
1595 return log_error_errno(r
, "Failed to generate unit name from path '%s': %m", where
);
1597 u
= manager_get_unit(m
, e
);
1599 r
= mount_setup_existing_unit(u
, what
, where
, options
, fstype
, &flags
);
1601 /* First time we see this mount point meaning that it's not been initiated by a mount unit but rather
1602 * by the sysadmin having called mount(8) directly. */
1603 r
= mount_setup_new_unit(m
, e
, what
, where
, options
, fstype
, &flags
, &u
);
1605 return log_warning_errno(r
, "Failed to set up mount unit: %m");
1607 /* If the mount changed properties or state, let's notify our clients */
1608 if (flags
& (MOUNT_PROC_JUST_CHANGED
|MOUNT_PROC_JUST_MOUNTED
))
1609 unit_add_to_dbus_queue(u
);
1612 MOUNT(u
)->proc_flags
= flags
;
1617 static int mount_load_proc_self_mountinfo(Manager
*m
, bool set_flags
) {
1618 _cleanup_(mnt_free_tablep
) struct libmnt_table
*table
= NULL
;
1619 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*iter
= NULL
;
1624 r
= libmount_parse(NULL
, NULL
, &table
, &iter
);
1626 return log_error_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
1629 struct libmnt_fs
*fs
;
1630 const char *device
, *path
, *options
, *fstype
;
1632 r
= mnt_table_next_fs(table
, iter
, &fs
);
1636 return log_error_errno(r
, "Failed to get next entry from /proc/self/mountinfo: %m");
1638 device
= mnt_fs_get_source(fs
);
1639 path
= mnt_fs_get_target(fs
);
1640 options
= mnt_fs_get_options(fs
);
1641 fstype
= mnt_fs_get_fstype(fs
);
1643 if (!device
|| !path
)
1646 device_found_node(m
, device
, DEVICE_FOUND_MOUNT
, DEVICE_FOUND_MOUNT
);
1648 (void) mount_setup_unit(m
, device
, path
, options
, fstype
, set_flags
);
1654 static void mount_shutdown(Manager
*m
) {
1657 m
->mount_event_source
= sd_event_source_unref(m
->mount_event_source
);
1659 mnt_unref_monitor(m
->mount_monitor
);
1660 m
->mount_monitor
= NULL
;
1663 static int mount_get_timeout(Unit
*u
, usec_t
*timeout
) {
1664 Mount
*m
= MOUNT(u
);
1668 if (!m
->timer_event_source
)
1671 r
= sd_event_source_get_time(m
->timer_event_source
, &t
);
1674 if (t
== USEC_INFINITY
)
1681 static void mount_enumerate_perpetual(Manager
*m
) {
1687 /* Whatever happens, we know for sure that the root directory is around, and cannot go away. Let's
1688 * unconditionally synthesize it here and mark it as perpetual. */
1690 u
= manager_get_unit(m
, SPECIAL_ROOT_MOUNT
);
1692 r
= unit_new_for_name(m
, sizeof(Mount
), SPECIAL_ROOT_MOUNT
, &u
);
1694 log_error_errno(r
, "Failed to allocate the special " SPECIAL_ROOT_MOUNT
" unit: %m");
1699 u
->perpetual
= true;
1700 MOUNT(u
)->deserialized_state
= MOUNT_MOUNTED
;
1702 unit_add_to_load_queue(u
);
1703 unit_add_to_dbus_queue(u
);
1706 static bool mount_is_mounted(Mount
*m
) {
1709 return UNIT(m
)->perpetual
|| FLAGS_SET(m
->proc_flags
, MOUNT_PROC_IS_MOUNTED
);
1712 static void mount_enumerate(Manager
*m
) {
1719 if (!m
->mount_monitor
) {
1722 m
->mount_monitor
= mnt_new_monitor();
1723 if (!m
->mount_monitor
) {
1728 r
= mnt_monitor_enable_kernel(m
->mount_monitor
, 1);
1730 log_error_errno(r
, "Failed to enable watching of kernel mount events: %m");
1734 r
= mnt_monitor_enable_userspace(m
->mount_monitor
, 1, NULL
);
1736 log_error_errno(r
, "Failed to enable watching of userspace mount events: %m");
1740 /* mnt_unref_monitor() will close the fd */
1741 fd
= r
= mnt_monitor_get_fd(m
->mount_monitor
);
1743 log_error_errno(r
, "Failed to acquire watch file descriptor: %m");
1747 r
= sd_event_add_io(m
->event
, &m
->mount_event_source
, fd
, EPOLLIN
, mount_dispatch_io
, m
);
1749 log_error_errno(r
, "Failed to watch mount file descriptor: %m");
1753 r
= sd_event_source_set_priority(m
->mount_event_source
, SD_EVENT_PRIORITY_NORMAL
-10);
1755 log_error_errno(r
, "Failed to adjust mount watch priority: %m");
1759 (void) sd_event_source_set_description(m
->mount_event_source
, "mount-monitor-dispatch");
1762 r
= mount_load_proc_self_mountinfo(m
, false);
1772 static int drain_libmount(Manager
*m
) {
1773 bool rescan
= false;
1778 /* Drain all events and verify that the event is valid.
1780 * Note that libmount also monitors /run/mount mkdir if the directory does not exist yet. The mkdir
1781 * may generate event which is irrelevant for us.
1783 * error: r < 0; valid: r == 0, false positive: r == 1 */
1785 r
= mnt_monitor_next_change(m
->mount_monitor
, NULL
, NULL
);
1787 return log_error_errno(r
, "Failed to drain libmount events: %m");
1795 static int mount_process_proc_self_mountinfo(Manager
*m
) {
1796 _cleanup_set_free_free_ Set
*around
= NULL
, *gone
= NULL
;
1804 r
= drain_libmount(m
);
1808 r
= mount_load_proc_self_mountinfo(m
, true);
1810 /* Reset flags, just in case, for later calls */
1811 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
])
1812 MOUNT(u
)->proc_flags
= 0;
1817 manager_dispatch_load_queue(m
);
1819 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
]) {
1820 Mount
*mount
= MOUNT(u
);
1822 if (!mount_is_mounted(mount
)) {
1824 /* A mount point is not around right now. It
1825 * might be gone, or might never have
1828 if (mount
->from_proc_self_mountinfo
&&
1829 mount
->parameters_proc_self_mountinfo
.what
) {
1831 /* Remember that this device might just have disappeared */
1832 if (set_ensure_allocated(&gone
, &path_hash_ops
) < 0 ||
1833 set_put_strdup(gone
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
1834 log_oom(); /* we don't care too much about OOM here... */
1837 mount
->from_proc_self_mountinfo
= false;
1838 assert_se(update_parameters_proc_self_mountinfo(mount
, NULL
, NULL
, NULL
) >= 0);
1840 switch (mount
->state
) {
1843 /* This has just been unmounted by somebody else, follow the state change. */
1844 mount_enter_dead(mount
, MOUNT_SUCCESS
);
1851 } else if (mount
->proc_flags
& (MOUNT_PROC_JUST_MOUNTED
|MOUNT_PROC_JUST_CHANGED
)) {
1853 /* A mount point was added or changed */
1855 switch (mount
->state
) {
1860 /* This has just been mounted by somebody else, follow the state change, but let's
1861 * generate a new invocation ID for this implicitly and automatically. */
1862 (void) unit_acquire_invocation_id(u
);
1863 mount_cycle_clear(mount
);
1864 mount_enter_mounted(mount
, MOUNT_SUCCESS
);
1867 case MOUNT_MOUNTING
:
1868 mount_set_state(mount
, MOUNT_MOUNTING_DONE
);
1872 /* Nothing really changed, but let's
1873 * issue an notification call
1874 * nonetheless, in case somebody is
1875 * waiting for this. (e.g. file system
1876 * ro/rw remounts.) */
1877 mount_set_state(mount
, mount
->state
);
1882 if (mount_is_mounted(mount
) &&
1883 mount
->from_proc_self_mountinfo
&&
1884 mount
->parameters_proc_self_mountinfo
.what
) {
1885 /* Track devices currently used */
1887 if (set_ensure_allocated(&around
, &path_hash_ops
) < 0 ||
1888 set_put_strdup(around
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
1892 /* Reset the flags for later calls */
1893 mount
->proc_flags
= 0;
1896 SET_FOREACH(what
, gone
, i
) {
1897 if (set_contains(around
, what
))
1900 /* Let the device units know that the device is no longer mounted */
1901 device_found_node(m
, what
, 0, DEVICE_FOUND_MOUNT
);
1907 static int mount_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
1908 Manager
*m
= userdata
;
1911 assert(revents
& EPOLLIN
);
1913 return mount_process_proc_self_mountinfo(m
);
1916 static void mount_reset_failed(Unit
*u
) {
1917 Mount
*m
= MOUNT(u
);
1921 if (m
->state
== MOUNT_FAILED
)
1922 mount_set_state(m
, MOUNT_DEAD
);
1924 m
->result
= MOUNT_SUCCESS
;
1925 m
->reload_result
= MOUNT_SUCCESS
;
1928 static int mount_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
1929 Mount
*m
= MOUNT(u
);
1933 return unit_kill_common(u
, who
, signo
, -1, m
->control_pid
, error
);
1936 static int mount_control_pid(Unit
*u
) {
1937 Mount
*m
= MOUNT(u
);
1941 return m
->control_pid
;
1944 static const char* const mount_exec_command_table
[_MOUNT_EXEC_COMMAND_MAX
] = {
1945 [MOUNT_EXEC_MOUNT
] = "ExecMount",
1946 [MOUNT_EXEC_UNMOUNT
] = "ExecUnmount",
1947 [MOUNT_EXEC_REMOUNT
] = "ExecRemount",
1950 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command
, MountExecCommand
);
1952 static const char* const mount_result_table
[_MOUNT_RESULT_MAX
] = {
1953 [MOUNT_SUCCESS
] = "success",
1954 [MOUNT_FAILURE_RESOURCES
] = "resources",
1955 [MOUNT_FAILURE_TIMEOUT
] = "timeout",
1956 [MOUNT_FAILURE_EXIT_CODE
] = "exit-code",
1957 [MOUNT_FAILURE_SIGNAL
] = "signal",
1958 [MOUNT_FAILURE_CORE_DUMP
] = "core-dump",
1959 [MOUNT_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
1960 [MOUNT_FAILURE_PROTOCOL
] = "protocol",
1963 DEFINE_STRING_TABLE_LOOKUP(mount_result
, MountResult
);
1965 const UnitVTable mount_vtable
= {
1966 .object_size
= sizeof(Mount
),
1967 .exec_context_offset
= offsetof(Mount
, exec_context
),
1968 .cgroup_context_offset
= offsetof(Mount
, cgroup_context
),
1969 .kill_context_offset
= offsetof(Mount
, kill_context
),
1970 .exec_runtime_offset
= offsetof(Mount
, exec_runtime
),
1971 .dynamic_creds_offset
= offsetof(Mount
, dynamic_creds
),
1977 .private_section
= "Mount",
1983 .coldplug
= mount_coldplug
,
1987 .start
= mount_start
,
1989 .reload
= mount_reload
,
1993 .serialize
= mount_serialize
,
1994 .deserialize_item
= mount_deserialize_item
,
1996 .active_state
= mount_active_state
,
1997 .sub_state_to_string
= mount_sub_state_to_string
,
1999 .will_restart
= unit_will_restart_default
,
2001 .may_gc
= mount_may_gc
,
2003 .sigchld_event
= mount_sigchld_event
,
2005 .reset_failed
= mount_reset_failed
,
2007 .control_pid
= mount_control_pid
,
2009 .bus_vtable
= bus_mount_vtable
,
2010 .bus_set_property
= bus_mount_set_property
,
2011 .bus_commit_properties
= bus_mount_commit_properties
,
2013 .get_timeout
= mount_get_timeout
,
2015 .can_transient
= true,
2017 .enumerate_perpetual
= mount_enumerate_perpetual
,
2018 .enumerate
= mount_enumerate
,
2019 .shutdown
= mount_shutdown
,
2021 .status_message_formats
= {
2022 .starting_stopping
= {
2023 [0] = "Mounting %s...",
2024 [1] = "Unmounting %s...",
2026 .finished_start_job
= {
2027 [JOB_DONE
] = "Mounted %s.",
2028 [JOB_FAILED
] = "Failed to mount %s.",
2029 [JOB_TIMEOUT
] = "Timed out mounting %s.",
2031 .finished_stop_job
= {
2032 [JOB_DONE
] = "Unmounted %s.",
2033 [JOB_FAILED
] = "Failed unmounting %s.",
2034 [JOB_TIMEOUT
] = "Timed out unmounting %s.",