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
,
49 [MOUNT_CLEANING
] = UNIT_MAINTENANCE
,
52 static int mount_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
53 static int mount_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
);
54 static int mount_process_proc_self_mountinfo(Manager
*m
);
56 static bool MOUNT_STATE_WITH_PROCESS(MountState state
) {
61 MOUNT_REMOUNTING_SIGTERM
,
62 MOUNT_REMOUNTING_SIGKILL
,
64 MOUNT_UNMOUNTING_SIGTERM
,
65 MOUNT_UNMOUNTING_SIGKILL
,
69 static bool mount_is_automount(const MountParameters
*p
) {
72 return fstab_test_option(p
->options
,
73 "comment=systemd.automount\0"
74 "x-systemd.automount\0");
77 static bool mount_is_network(const MountParameters
*p
) {
80 if (fstab_test_option(p
->options
, "_netdev\0"))
83 if (p
->fstype
&& fstype_is_network(p
->fstype
))
89 static bool mount_is_nofail(const Mount
*m
) {
92 if (!m
->from_fragment
)
95 return fstab_test_yes_no_option(m
->parameters_fragment
.options
, "nofail\0" "fail\0");
98 static bool mount_is_loop(const MountParameters
*p
) {
101 if (fstab_test_option(p
->options
, "loop\0"))
107 static bool mount_is_bind(const MountParameters
*p
) {
110 if (fstab_test_option(p
->options
, "bind\0" "rbind\0"))
113 if (p
->fstype
&& STR_IN_SET(p
->fstype
, "bind", "rbind"))
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 static MountParameters
* get_mount_parameters_fragment(Mount
*m
) {
240 if (m
->from_fragment
)
241 return &m
->parameters_fragment
;
246 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 update_parameters_proc_self_mountinfo(
259 const char *fstype
) {
264 p
= &m
->parameters_proc_self_mountinfo
;
266 r
= free_and_strdup(&p
->what
, what
);
270 q
= free_and_strdup(&p
->options
, options
);
274 w
= free_and_strdup(&p
->fstype
, fstype
);
278 return r
> 0 || q
> 0 || w
> 0;
281 static int mount_add_mount_dependencies(Mount
*m
) {
290 if (!path_equal(m
->where
, "/")) {
291 _cleanup_free_
char *parent
= NULL
;
293 /* Adds in links to other mount points that might lie further up in the hierarchy */
295 parent
= dirname_malloc(m
->where
);
299 r
= unit_require_mounts_for(UNIT(m
), parent
, UNIT_DEPENDENCY_IMPLICIT
);
304 /* Adds in dependencies to other mount points that might be needed for the source path (if this is a bind mount
305 * or a loop mount) to be available. */
306 pm
= get_mount_parameters_fragment(m
);
307 if (pm
&& pm
->what
&&
308 path_is_absolute(pm
->what
) &&
309 (mount_is_bind(pm
) || mount_is_loop(pm
) || !mount_is_network(pm
))) {
311 r
= unit_require_mounts_for(UNIT(m
), pm
->what
, UNIT_DEPENDENCY_FILE
);
316 /* Adds in dependencies to other units that use this path or paths further down in the hierarchy */
317 s
= manager_get_units_requiring_mounts_for(UNIT(m
)->manager
, m
->where
);
318 SET_FOREACH(other
, s
, i
) {
320 if (other
->load_state
!= UNIT_LOADED
)
323 if (other
== UNIT(m
))
326 r
= unit_add_dependency(other
, UNIT_AFTER
, UNIT(m
), true, UNIT_DEPENDENCY_PATH
);
330 if (UNIT(m
)->fragment_path
) {
331 /* If we have fragment configuration, then make this dependency required */
332 r
= unit_add_dependency(other
, UNIT_REQUIRES
, UNIT(m
), true, UNIT_DEPENDENCY_PATH
);
341 static int mount_add_device_dependencies(Mount
*m
) {
342 UnitDependencyMask mask
;
349 p
= get_mount_parameters(m
);
356 if (mount_is_bind(p
))
359 if (!is_device_path(p
->what
))
362 /* /dev/root is a really weird thing, it's not a real device, but just a path the kernel exports for
363 * the root file system specified on the kernel command line. Ignore it here. */
364 if (PATH_IN_SET(p
->what
, "/dev/root", "/dev/nfs"))
367 if (path_equal(m
->where
, "/"))
370 /* Mount units from /proc/self/mountinfo are not bound to devices by default since they're subject to
371 * races when devices are unplugged. But the user can still force this dep with an appropriate option
372 * (or udev property) so the mount units are automatically stopped when the device disappears
374 dep
= mount_is_bound_to_device(m
) ? UNIT_BINDS_TO
: UNIT_REQUIRES
;
376 /* We always use 'what' from /proc/self/mountinfo if mounted */
377 mask
= m
->from_proc_self_mountinfo
? UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
: UNIT_DEPENDENCY_FILE
;
379 r
= unit_add_node_dependency(UNIT(m
), p
->what
, dep
, mask
);
383 return unit_add_blockdev_dependency(UNIT(m
), p
->what
, mask
);
386 static int mount_add_quota_dependencies(Mount
*m
) {
387 UnitDependencyMask mask
;
393 if (!MANAGER_IS_SYSTEM(UNIT(m
)->manager
))
396 p
= get_mount_parameters_fragment(m
);
400 if (!mount_needs_quota(p
))
403 mask
= m
->from_fragment
? UNIT_DEPENDENCY_FILE
: UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
;
405 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTACHECK_SERVICE
, true, mask
);
409 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTAON_SERVICE
, true, mask
);
416 static bool mount_is_extrinsic(Mount
*m
) {
420 /* Returns true for all units that are "magic" and should be excluded from the usual
421 * start-up and shutdown dependencies. We call them "extrinsic" here, as they are generally
422 * mounted outside of the systemd dependency logic. We shouldn't attempt to manage them
423 * ourselves but it's fine if the user operates on them with us. */
425 /* We only automatically manage mounts if we are in system mode */
426 if (!MANAGER_IS_SYSTEM(UNIT(m
)->manager
))
429 if (UNIT(m
)->perpetual
) /* All perpetual units never change state */
432 p
= get_mount_parameters(m
);
433 if (p
&& fstab_is_extrinsic(m
->where
, p
->options
))
439 static int mount_add_default_ordering_dependencies(
442 UnitDependencyMask mask
) {
444 const char *after
, *before
, *e
;
449 e
= path_startswith(m
->where
, "/sysroot");
450 if (e
&& in_initrd()) {
451 /* All mounts under /sysroot need to happen later, at initrd-fs.target time. IOW,
452 * it's not technically part of the basic initrd filesystem itself, and so
453 * shouldn't inherit the default Before=local-fs.target dependency. */
456 before
= isempty(e
) ? SPECIAL_INITRD_ROOT_FS_TARGET
: SPECIAL_INITRD_FS_TARGET
;
458 } else if (mount_is_network(p
)) {
459 after
= SPECIAL_REMOTE_FS_PRE_TARGET
;
460 before
= SPECIAL_REMOTE_FS_TARGET
;
463 after
= SPECIAL_LOCAL_FS_PRE_TARGET
;
464 before
= SPECIAL_LOCAL_FS_TARGET
;
467 if (!mount_is_nofail(m
) && !mount_is_automount(p
)) {
468 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_BEFORE
, before
, true, mask
);
474 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, after
, true, mask
);
479 return unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_CONFLICTS
,
480 SPECIAL_UMOUNT_TARGET
, true, mask
);
483 static int mount_add_default_dependencies(Mount
*m
) {
484 UnitDependencyMask mask
;
490 if (!UNIT(m
)->default_dependencies
)
493 /* We do not add any default dependencies to /, /usr or /run/initramfs/, since they are
494 * guaranteed to stay mounted the whole time, since our system is on it. Also, don't
495 * bother with anything mounted below virtual file systems, it's also going to be virtual,
496 * and hence not worth the effort. */
497 if (mount_is_extrinsic(m
))
500 p
= get_mount_parameters(m
);
504 mask
= m
->from_fragment
? UNIT_DEPENDENCY_FILE
: UNIT_DEPENDENCY_MOUNTINFO_DEFAULT
;
506 r
= mount_add_default_ordering_dependencies(m
, p
, mask
);
510 if (mount_is_network(p
)) {
511 /* We order ourselves after network.target. This is primarily useful at shutdown:
512 * services that take down the network should order themselves before
513 * network.target, so that they are shut down only after this mount unit is
516 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, SPECIAL_NETWORK_TARGET
, true, mask
);
520 /* We pull in network-online.target, and order ourselves after it. This is useful
521 * at start-up to actively pull in tools that want to be started before we start
522 * mounting network file systems, and whose purpose it is to delay this until the
523 * network is "up". */
525 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_WANTS
, UNIT_AFTER
, SPECIAL_NETWORK_ONLINE_TARGET
, true, mask
);
530 /* If this is a tmpfs mount then we have to unmount it before we try to deactivate swaps */
531 if (streq_ptr(p
->fstype
, "tmpfs")) {
532 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, SPECIAL_SWAP_TARGET
, true, mask
);
540 static int mount_verify(Mount
*m
) {
541 _cleanup_free_
char *e
= NULL
;
546 assert(UNIT(m
)->load_state
== UNIT_LOADED
);
548 if (!m
->from_fragment
&& !m
->from_proc_self_mountinfo
&& !UNIT(m
)->perpetual
)
551 r
= unit_name_from_path(m
->where
, ".mount", &e
);
553 return log_unit_error_errno(UNIT(m
), r
, "Failed to generate unit name from mount path: %m");
555 if (!unit_has_name(UNIT(m
), e
)) {
556 log_unit_error(UNIT(m
), "Where= setting doesn't match unit name. Refusing.");
560 if (mount_point_is_api(m
->where
) || mount_point_ignore(m
->where
)) {
561 log_unit_error(UNIT(m
), "Cannot create mount unit for API file system %s. Refusing.", m
->where
);
565 p
= get_mount_parameters_fragment(m
);
566 if (p
&& !p
->what
&& !UNIT(m
)->perpetual
)
567 return log_unit_error_errno(UNIT(m
), SYNTHETIC_ERRNO(ENOEXEC
),
568 "What= setting is missing. Refusing.");
570 if (m
->exec_context
.pam_name
&& m
->kill_context
.kill_mode
!= KILL_CONTROL_GROUP
) {
571 log_unit_error(UNIT(m
), "Unit has PAM enabled. Kill mode must be set to control-group'. Refusing.");
578 static int mount_add_non_exec_dependencies(Mount
*m
) {
582 /* Adds in all dependencies directly responsible for ordering the mount, as opposed to dependencies
583 * resulting from the ExecContext and such. */
585 r
= mount_add_device_dependencies(m
);
589 r
= mount_add_mount_dependencies(m
);
593 r
= mount_add_quota_dependencies(m
);
597 r
= mount_add_default_dependencies(m
);
604 static int mount_add_extras(Mount
*m
) {
610 /* Note: this call might be called after we already have been loaded once (and even when it has already been
611 * activated), in case data from /proc/self/mountinfo has changed. This means all code here needs to be ready
612 * to run with an already set up unit. */
614 if (u
->fragment_path
)
615 m
->from_fragment
= true;
618 r
= unit_name_to_path(u
->id
, &m
->where
);
623 path_simplify(m
->where
, false);
625 if (!u
->description
) {
626 r
= unit_set_description(u
, m
->where
);
631 r
= unit_patch_contexts(u
);
635 r
= unit_add_exec_dependencies(u
, &m
->exec_context
);
639 r
= unit_set_default_slice(u
);
643 r
= mount_add_non_exec_dependencies(m
);
650 static void mount_load_root_mount(Unit
*u
) {
653 if (!unit_has_name(u
, SPECIAL_ROOT_MOUNT
))
657 u
->default_dependencies
= false;
659 /* The stdio/kmsg bridge socket is on /, in order to avoid a dep loop, don't use kmsg logging for -.mount */
660 MOUNT(u
)->exec_context
.std_output
= EXEC_OUTPUT_NULL
;
661 MOUNT(u
)->exec_context
.std_input
= EXEC_INPUT_NULL
;
664 u
->description
= strdup("Root Mount");
667 static int mount_load(Unit
*u
) {
672 assert(u
->load_state
== UNIT_STUB
);
674 mount_load_root_mount(u
);
676 bool fragment_optional
= m
->from_proc_self_mountinfo
|| u
->perpetual
;
677 r
= unit_load_fragment_and_dropin(u
, !fragment_optional
);
679 /* Add in some extras. Note we do this in all cases (even if we failed to load the unit) when announced by the
680 * kernel, because we need some things to be set up no matter what when the kernel establishes a mount and thus
681 * we need to update the state in our unit to track it. After all, consider that we don't allow changing the
682 * 'slice' field for a unit once it is active. */
683 if (u
->load_state
== UNIT_LOADED
|| m
->from_proc_self_mountinfo
|| u
->perpetual
)
684 q
= mount_add_extras(m
);
690 if (u
->load_state
!= UNIT_LOADED
)
693 return mount_verify(m
);
696 static void mount_set_state(Mount
*m
, MountState state
) {
697 MountState old_state
;
700 if (m
->state
!= state
)
701 bus_unit_send_pending_change_signal(UNIT(m
), false);
703 old_state
= m
->state
;
706 if (!MOUNT_STATE_WITH_PROCESS(state
)) {
707 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
708 mount_unwatch_control_pid(m
);
709 m
->control_command
= NULL
;
710 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
713 if (state
!= old_state
)
714 log_unit_debug(UNIT(m
), "Changed %s -> %s", mount_state_to_string(old_state
), mount_state_to_string(state
));
716 unit_notify(UNIT(m
), state_translation_table
[old_state
], state_translation_table
[state
],
717 m
->reload_result
== MOUNT_SUCCESS
? 0 : UNIT_NOTIFY_RELOAD_FAILURE
);
720 static int mount_coldplug(Unit
*u
) {
722 MountState new_state
= MOUNT_DEAD
;
726 assert(m
->state
== MOUNT_DEAD
);
728 if (m
->deserialized_state
!= m
->state
)
729 new_state
= m
->deserialized_state
;
730 else if (m
->from_proc_self_mountinfo
)
731 new_state
= MOUNT_MOUNTED
;
733 if (new_state
== m
->state
)
736 if (m
->control_pid
> 0 &&
737 pid_is_unwaited(m
->control_pid
) &&
738 MOUNT_STATE_WITH_PROCESS(new_state
)) {
740 r
= unit_watch_pid(UNIT(m
), m
->control_pid
, false);
744 r
= mount_arm_timer(m
, usec_add(u
->state_change_timestamp
.monotonic
, m
->timeout_usec
));
749 if (!IN_SET(new_state
, MOUNT_DEAD
, MOUNT_FAILED
)) {
750 (void) unit_setup_dynamic_creds(u
);
751 (void) unit_setup_exec_runtime(u
);
754 mount_set_state(m
, new_state
);
758 static void mount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
759 char buf
[FORMAT_TIMESPAN_MAX
];
766 p
= get_mount_parameters(m
);
769 "%sMount State: %s\n"
771 "%sClean Result: %s\n"
774 "%sFile System Type: %s\n"
776 "%sFrom /proc/self/mountinfo: %s\n"
777 "%sFrom fragment: %s\n"
779 "%sDirectoryMode: %04o\n"
780 "%sSloppyOptions: %s\n"
781 "%sLazyUnmount: %s\n"
782 "%sForceUnmount: %s\n"
783 "%sReadWriteOnly: %s\n"
784 "%sTimeoutSec: %s\n",
785 prefix
, mount_state_to_string(m
->state
),
786 prefix
, mount_result_to_string(m
->result
),
787 prefix
, mount_result_to_string(m
->clean_result
),
789 prefix
, p
? strna(p
->what
) : "n/a",
790 prefix
, p
? strna(p
->fstype
) : "n/a",
791 prefix
, p
? strna(p
->options
) : "n/a",
792 prefix
, yes_no(m
->from_proc_self_mountinfo
),
793 prefix
, yes_no(m
->from_fragment
),
794 prefix
, yes_no(mount_is_extrinsic(m
)),
795 prefix
, m
->directory_mode
,
796 prefix
, yes_no(m
->sloppy_options
),
797 prefix
, yes_no(m
->lazy_unmount
),
798 prefix
, yes_no(m
->force_unmount
),
799 prefix
, yes_no(m
->read_write_only
),
800 prefix
, format_timespan(buf
, sizeof(buf
), m
->timeout_usec
, USEC_PER_SEC
));
802 if (m
->control_pid
> 0)
804 "%sControl PID: "PID_FMT
"\n",
805 prefix
, m
->control_pid
);
807 exec_context_dump(&m
->exec_context
, f
, prefix
);
808 kill_context_dump(&m
->kill_context
, f
, prefix
);
809 cgroup_context_dump(UNIT(m
), f
, prefix
);
812 static int mount_spawn(Mount
*m
, ExecCommand
*c
, pid_t
*_pid
) {
814 _cleanup_(exec_params_clear
) ExecParameters exec_params
= {
815 .flags
= EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
,
828 r
= unit_prepare_exec(UNIT(m
));
832 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->timeout_usec
));
836 r
= unit_set_exec_params(UNIT(m
), &exec_params
);
840 r
= exec_spawn(UNIT(m
),
850 r
= unit_watch_pid(UNIT(m
), pid
, true);
859 static void mount_enter_dead(Mount
*m
, MountResult f
) {
862 if (m
->result
== MOUNT_SUCCESS
)
865 unit_log_result(UNIT(m
), m
->result
== MOUNT_SUCCESS
, mount_result_to_string(m
->result
));
866 unit_warn_leftover_processes(UNIT(m
), unit_log_leftover_process_stop
);
868 mount_set_state(m
, m
->result
!= MOUNT_SUCCESS
? MOUNT_FAILED
: MOUNT_DEAD
);
870 m
->exec_runtime
= exec_runtime_unref(m
->exec_runtime
, true);
872 unit_destroy_runtime_data(UNIT(m
), &m
->exec_context
);
874 unit_unref_uid_gid(UNIT(m
), true);
876 dynamic_creds_destroy(&m
->dynamic_creds
);
878 /* Any dependencies based on /proc/self/mountinfo are now stale */
879 unit_remove_dependencies(UNIT(m
), UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
);
882 static void mount_enter_mounted(Mount
*m
, MountResult f
) {
885 if (m
->result
== MOUNT_SUCCESS
)
888 mount_set_state(m
, MOUNT_MOUNTED
);
891 static void mount_enter_dead_or_mounted(Mount
*m
, MountResult f
) {
894 /* Enter DEAD or MOUNTED state, depending on what the kernel currently says about the mount point. We use this
895 * whenever we executed an operation, so that our internal state reflects what the kernel says again, after all
896 * ultimately we just mirror the kernel's internal state on this. */
898 if (m
->from_proc_self_mountinfo
)
899 mount_enter_mounted(m
, f
);
901 mount_enter_dead(m
, f
);
904 static int state_to_kill_operation(MountState state
) {
907 case MOUNT_REMOUNTING_SIGTERM
:
910 case MOUNT_UNMOUNTING_SIGTERM
:
911 return KILL_TERMINATE
;
913 case MOUNT_REMOUNTING_SIGKILL
:
914 case MOUNT_UNMOUNTING_SIGKILL
:
918 return _KILL_OPERATION_INVALID
;
922 static void mount_enter_signal(Mount
*m
, MountState state
, MountResult f
) {
927 if (m
->result
== MOUNT_SUCCESS
)
930 r
= unit_kill_context(
933 state_to_kill_operation(state
),
941 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->timeout_usec
));
945 mount_set_state(m
, state
);
946 } else if (state
== MOUNT_REMOUNTING_SIGTERM
&& m
->kill_context
.send_sigkill
)
947 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
948 else if (IN_SET(state
, MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
))
949 mount_enter_mounted(m
, MOUNT_SUCCESS
);
950 else if (state
== MOUNT_UNMOUNTING_SIGTERM
&& m
->kill_context
.send_sigkill
)
951 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
953 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
958 log_unit_warning_errno(UNIT(m
), r
, "Failed to kill processes: %m");
959 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
962 static void mount_enter_unmounting(Mount
*m
) {
967 /* Start counting our attempts */
968 if (!IN_SET(m
->state
,
970 MOUNT_UNMOUNTING_SIGTERM
,
971 MOUNT_UNMOUNTING_SIGKILL
))
972 m
->n_retry_umount
= 0;
974 m
->control_command_id
= MOUNT_EXEC_UNMOUNT
;
975 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_UNMOUNT
;
977 r
= exec_command_set(m
->control_command
, UMOUNT_PATH
, m
->where
, "-c", NULL
);
978 if (r
>= 0 && m
->lazy_unmount
)
979 r
= exec_command_append(m
->control_command
, "-l", NULL
);
980 if (r
>= 0 && m
->force_unmount
)
981 r
= exec_command_append(m
->control_command
, "-f", NULL
);
985 mount_unwatch_control_pid(m
);
987 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
991 mount_set_state(m
, MOUNT_UNMOUNTING
);
996 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'umount' task: %m");
997 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
1000 static void mount_enter_mounting(Mount
*m
) {
1006 r
= unit_fail_if_noncanonical(UNIT(m
), m
->where
);
1010 (void) mkdir_p_label(m
->where
, m
->directory_mode
);
1012 unit_warn_if_dir_nonempty(UNIT(m
), m
->where
);
1013 unit_warn_leftover_processes(UNIT(m
), unit_log_leftover_process_start
);
1015 m
->control_command_id
= MOUNT_EXEC_MOUNT
;
1016 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_MOUNT
;
1018 /* Create the source directory for bind-mounts if needed */
1019 p
= get_mount_parameters_fragment(m
);
1020 if (p
&& mount_is_bind(p
)) {
1021 r
= mkdir_p_label(p
->what
, m
->directory_mode
);
1023 log_unit_error_errno(UNIT(m
), r
, "Failed to make bind mount source '%s': %m", p
->what
);
1027 _cleanup_free_
char *opts
= NULL
;
1029 r
= fstab_filter_options(p
->options
, "nofail\0" "noauto\0" "auto\0", NULL
, NULL
, &opts
);
1033 r
= exec_command_set(m
->control_command
, MOUNT_PATH
, p
->what
, m
->where
, NULL
);
1034 if (r
>= 0 && m
->sloppy_options
)
1035 r
= exec_command_append(m
->control_command
, "-s", NULL
);
1036 if (r
>= 0 && m
->read_write_only
)
1037 r
= exec_command_append(m
->control_command
, "-w", NULL
);
1038 if (r
>= 0 && p
->fstype
)
1039 r
= exec_command_append(m
->control_command
, "-t", p
->fstype
, NULL
);
1040 if (r
>= 0 && !isempty(opts
))
1041 r
= exec_command_append(m
->control_command
, "-o", opts
, NULL
);
1047 mount_unwatch_control_pid(m
);
1049 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
1053 mount_set_state(m
, MOUNT_MOUNTING
);
1058 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'mount' task: %m");
1059 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
1062 static void mount_set_reload_result(Mount
*m
, MountResult result
) {
1065 /* Only store the first error we encounter */
1066 if (m
->reload_result
!= MOUNT_SUCCESS
)
1069 m
->reload_result
= result
;
1072 static void mount_enter_remounting(Mount
*m
) {
1078 /* Reset reload result when we are about to start a new remount operation */
1079 m
->reload_result
= MOUNT_SUCCESS
;
1081 m
->control_command_id
= MOUNT_EXEC_REMOUNT
;
1082 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_REMOUNT
;
1084 p
= get_mount_parameters_fragment(m
);
1089 o
= strjoina("remount,", p
->options
);
1093 r
= exec_command_set(m
->control_command
, MOUNT_PATH
,
1096 if (r
>= 0 && m
->sloppy_options
)
1097 r
= exec_command_append(m
->control_command
, "-s", NULL
);
1098 if (r
>= 0 && m
->read_write_only
)
1099 r
= exec_command_append(m
->control_command
, "-w", NULL
);
1100 if (r
>= 0 && p
->fstype
)
1101 r
= exec_command_append(m
->control_command
, "-t", p
->fstype
, NULL
);
1107 mount_unwatch_control_pid(m
);
1109 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
1113 mount_set_state(m
, MOUNT_REMOUNTING
);
1118 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'remount' task: %m");
1119 mount_set_reload_result(m
, MOUNT_FAILURE_RESOURCES
);
1120 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1123 static void mount_cycle_clear(Mount
*m
) {
1126 /* Clear all state we shall forget for this new cycle */
1128 m
->result
= MOUNT_SUCCESS
;
1129 m
->reload_result
= MOUNT_SUCCESS
;
1130 exec_command_reset_status_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
1131 UNIT(m
)->reset_accounting
= true;
1134 static int mount_start(Unit
*u
) {
1135 Mount
*m
= MOUNT(u
);
1140 /* We cannot fulfill this request right now, try again later
1142 if (IN_SET(m
->state
,
1144 MOUNT_UNMOUNTING_SIGTERM
,
1145 MOUNT_UNMOUNTING_SIGKILL
,
1149 /* Already on it! */
1150 if (m
->state
== MOUNT_MOUNTING
)
1153 assert(IN_SET(m
->state
, MOUNT_DEAD
, MOUNT_FAILED
));
1155 r
= unit_test_start_limit(u
);
1157 mount_enter_dead(m
, MOUNT_FAILURE_START_LIMIT_HIT
);
1161 r
= unit_acquire_invocation_id(u
);
1165 mount_cycle_clear(m
);
1166 mount_enter_mounting(m
);
1171 static int mount_stop(Unit
*u
) {
1172 Mount
*m
= MOUNT(u
);
1178 case MOUNT_UNMOUNTING
:
1179 case MOUNT_UNMOUNTING_SIGKILL
:
1180 case MOUNT_UNMOUNTING_SIGTERM
:
1184 case MOUNT_MOUNTING
:
1185 case MOUNT_MOUNTING_DONE
:
1186 case MOUNT_REMOUNTING
:
1187 /* If we are still waiting for /bin/mount, we go directly into kill mode. */
1188 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_SUCCESS
);
1191 case MOUNT_REMOUNTING_SIGTERM
:
1192 /* If we are already waiting for a hung remount, convert this to the matching unmounting state */
1193 mount_set_state(m
, MOUNT_UNMOUNTING_SIGTERM
);
1196 case MOUNT_REMOUNTING_SIGKILL
:
1198 mount_set_state(m
, MOUNT_UNMOUNTING_SIGKILL
);
1202 mount_enter_unmounting(m
);
1205 case MOUNT_CLEANING
:
1206 /* If we are currently cleaning, then abort it, brutally. */
1207 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
1211 assert_not_reached("Unexpected state.");
1215 static int mount_reload(Unit
*u
) {
1216 Mount
*m
= MOUNT(u
);
1219 assert(m
->state
== MOUNT_MOUNTED
);
1221 mount_enter_remounting(m
);
1226 static int mount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1227 Mount
*m
= MOUNT(u
);
1233 (void) serialize_item(f
, "state", mount_state_to_string(m
->state
));
1234 (void) serialize_item(f
, "result", mount_result_to_string(m
->result
));
1235 (void) serialize_item(f
, "reload-result", mount_result_to_string(m
->reload_result
));
1236 (void) serialize_item_format(f
, "n-retry-umount", "%u", m
->n_retry_umount
);
1238 if (m
->control_pid
> 0)
1239 (void) serialize_item_format(f
, "control-pid", PID_FMT
, m
->control_pid
);
1241 if (m
->control_command_id
>= 0)
1242 (void) serialize_item(f
, "control-command", mount_exec_command_to_string(m
->control_command_id
));
1247 static int mount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1248 Mount
*m
= MOUNT(u
);
1256 if (streq(key
, "state")) {
1259 if ((state
= mount_state_from_string(value
)) < 0)
1260 log_unit_debug(u
, "Failed to parse state value: %s", value
);
1262 m
->deserialized_state
= state
;
1264 } else if (streq(key
, "result")) {
1267 f
= mount_result_from_string(value
);
1269 log_unit_debug(u
, "Failed to parse result value: %s", value
);
1270 else if (f
!= MOUNT_SUCCESS
)
1273 } else if (streq(key
, "reload-result")) {
1276 f
= mount_result_from_string(value
);
1278 log_unit_debug(u
, "Failed to parse reload result value: %s", value
);
1279 else if (f
!= MOUNT_SUCCESS
)
1280 m
->reload_result
= f
;
1282 } else if (streq(key
, "n-retry-umount")) {
1284 r
= safe_atou(value
, &m
->n_retry_umount
);
1286 log_unit_debug(u
, "Failed to parse n-retry-umount value: %s", value
);
1288 } else if (streq(key
, "control-pid")) {
1290 if (parse_pid(value
, &m
->control_pid
) < 0)
1291 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
1293 } else if (streq(key
, "control-command")) {
1294 MountExecCommand id
;
1296 id
= mount_exec_command_from_string(value
);
1298 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
1300 m
->control_command_id
= id
;
1301 m
->control_command
= m
->exec_command
+ id
;
1304 log_unit_debug(u
, "Unknown serialization key: %s", key
);
1309 _pure_
static UnitActiveState
mount_active_state(Unit
*u
) {
1312 return state_translation_table
[MOUNT(u
)->state
];
1315 _pure_
static const char *mount_sub_state_to_string(Unit
*u
) {
1318 return mount_state_to_string(MOUNT(u
)->state
);
1321 _pure_
static bool mount_may_gc(Unit
*u
) {
1322 Mount
*m
= MOUNT(u
);
1326 if (m
->from_proc_self_mountinfo
)
1332 static void mount_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1333 Mount
*m
= MOUNT(u
);
1339 if (pid
!= m
->control_pid
)
1342 /* So here's the thing, we really want to know before /usr/bin/mount or /usr/bin/umount exit whether
1343 * they established/remove a mount. This is important when mounting, but even more so when unmounting
1344 * since we need to deal with nested mounts and otherwise cannot safely determine whether to repeat
1345 * the unmounts. In theory, the kernel fires /proc/self/mountinfo changes off before returning from
1346 * the mount() or umount() syscalls, and thus we should see the changes to the proc file before we
1347 * process the waitid() for the /usr/bin/(u)mount processes. However, this is unfortunately racy: we
1348 * have to waitid() for processes using P_ALL (since we need to reap unexpected children that got
1349 * reparented to PID 1), but when using P_ALL we might end up reaping processes that terminated just
1350 * instants ago, i.e. already after our last event loop iteration (i.e. after the last point we might
1351 * have noticed /proc/self/mountinfo events via epoll). This means event loop priorities for
1352 * processing SIGCHLD vs. /proc/self/mountinfo IO events are not as relevant as we want. To fix that
1353 * race, let's explicitly scan /proc/self/mountinfo before we start processing /usr/bin/(u)mount
1354 * dying. It's ugly, but it makes our ordering systematic again, and makes sure we always see
1355 * /proc/self/mountinfo changes before our mount/umount exits. */
1356 (void) mount_process_proc_self_mountinfo(u
->manager
);
1360 if (is_clean_exit(code
, status
, EXIT_CLEAN_COMMAND
, NULL
))
1362 else if (code
== CLD_EXITED
)
1363 f
= MOUNT_FAILURE_EXIT_CODE
;
1364 else if (code
== CLD_KILLED
)
1365 f
= MOUNT_FAILURE_SIGNAL
;
1366 else if (code
== CLD_DUMPED
)
1367 f
= MOUNT_FAILURE_CORE_DUMP
;
1369 assert_not_reached("Unknown code");
1371 if (IN_SET(m
->state
, MOUNT_REMOUNTING
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_REMOUNTING_SIGTERM
))
1372 mount_set_reload_result(m
, f
);
1373 else if (m
->result
== MOUNT_SUCCESS
)
1376 if (m
->control_command
) {
1377 exec_status_exit(&m
->control_command
->exec_status
, &m
->exec_context
, pid
, code
, status
);
1379 m
->control_command
= NULL
;
1380 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
1383 unit_log_process_exit(
1386 mount_exec_command_to_string(m
->control_command_id
),
1390 /* Note that due to the io event priority logic, we can be sure the new mountinfo is loaded
1391 * before we process the SIGCHLD for the mount command. */
1395 case MOUNT_MOUNTING
:
1396 /* Our mount point has not appeared in mountinfo. Something went wrong. */
1398 if (f
== MOUNT_SUCCESS
) {
1399 /* Either /bin/mount has an unexpected definition of success,
1400 * or someone raced us and we lost. */
1401 log_unit_warning(UNIT(m
), "Mount process finished, but there is no mount.");
1402 f
= MOUNT_FAILURE_PROTOCOL
;
1404 mount_enter_dead(m
, f
);
1407 case MOUNT_MOUNTING_DONE
:
1408 mount_enter_mounted(m
, f
);
1411 case MOUNT_REMOUNTING
:
1412 case MOUNT_REMOUNTING_SIGTERM
:
1413 case MOUNT_REMOUNTING_SIGKILL
:
1414 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1417 case MOUNT_UNMOUNTING
:
1419 if (f
== MOUNT_SUCCESS
&& m
->from_proc_self_mountinfo
) {
1421 /* Still a mount point? If so, let's try again. Most likely there were multiple mount points
1422 * stacked on top of each other. We might exceed the timeout specified by the user overall,
1423 * but we will stop as soon as any one umount times out. */
1425 if (m
->n_retry_umount
< RETRY_UMOUNT_MAX
) {
1426 log_unit_debug(u
, "Mount still present, trying again.");
1427 m
->n_retry_umount
++;
1428 mount_enter_unmounting(m
);
1430 log_unit_warning(u
, "Mount still present after %u attempts to unmount, giving up.", m
->n_retry_umount
);
1431 mount_enter_mounted(m
, f
);
1434 mount_enter_dead_or_mounted(m
, f
);
1438 case MOUNT_UNMOUNTING_SIGKILL
:
1439 case MOUNT_UNMOUNTING_SIGTERM
:
1440 mount_enter_dead_or_mounted(m
, f
);
1443 case MOUNT_CLEANING
:
1444 if (m
->clean_result
== MOUNT_SUCCESS
)
1445 m
->clean_result
= f
;
1447 mount_enter_dead(m
, MOUNT_SUCCESS
);
1451 assert_not_reached("Uh, control process died at wrong time.");
1454 /* Notify clients about changed exit status */
1455 unit_add_to_dbus_queue(u
);
1458 static int mount_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
1459 Mount
*m
= MOUNT(userdata
);
1462 assert(m
->timer_event_source
== source
);
1466 case MOUNT_MOUNTING
:
1467 case MOUNT_MOUNTING_DONE
:
1468 log_unit_warning(UNIT(m
), "Mounting timed out. Terminating.");
1469 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1472 case MOUNT_REMOUNTING
:
1473 log_unit_warning(UNIT(m
), "Remounting timed out. Terminating remount process.");
1474 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1475 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGTERM
, MOUNT_SUCCESS
);
1478 case MOUNT_REMOUNTING_SIGTERM
:
1479 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1481 if (m
->kill_context
.send_sigkill
) {
1482 log_unit_warning(UNIT(m
), "Remounting timed out. Killing.");
1483 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
1485 log_unit_warning(UNIT(m
), "Remounting timed out. Skipping SIGKILL. Ignoring.");
1486 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1490 case MOUNT_REMOUNTING_SIGKILL
:
1491 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1493 log_unit_warning(UNIT(m
), "Mount process still around after SIGKILL. Ignoring.");
1494 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1497 case MOUNT_UNMOUNTING
:
1498 log_unit_warning(UNIT(m
), "Unmounting timed out. Terminating.");
1499 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1502 case MOUNT_UNMOUNTING_SIGTERM
:
1503 if (m
->kill_context
.send_sigkill
) {
1504 log_unit_warning(UNIT(m
), "Mount process timed out. Killing.");
1505 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_FAILURE_TIMEOUT
);
1507 log_unit_warning(UNIT(m
), "Mount process timed out. Skipping SIGKILL. Ignoring.");
1508 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1512 case MOUNT_UNMOUNTING_SIGKILL
:
1513 log_unit_warning(UNIT(m
), "Mount process still around after SIGKILL. Ignoring.");
1514 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1517 case MOUNT_CLEANING
:
1518 log_unit_warning(UNIT(m
), "Cleaning timed out. killing.");
1520 if (m
->clean_result
== MOUNT_SUCCESS
)
1521 m
->clean_result
= MOUNT_FAILURE_TIMEOUT
;
1523 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, 0);
1527 assert_not_reached("Timeout at wrong time.");
1533 static int mount_setup_new_unit(
1538 const char *options
,
1540 MountProcFlags
*ret_flags
,
1543 _cleanup_(unit_freep
) Unit
*u
= NULL
;
1551 r
= unit_new_for_name(m
, sizeof(Mount
), name
, &u
);
1555 r
= free_and_strdup(&u
->source_path
, "/proc/self/mountinfo");
1559 r
= free_and_strdup(&MOUNT(u
)->where
, where
);
1563 r
= update_parameters_proc_self_mountinfo(MOUNT(u
), what
, options
, fstype
);
1567 /* This unit was generated because /proc/self/mountinfo reported it. Remember this, so that by the time we load
1568 * the unit file for it (and thus add in extra deps right after) we know what source to attributes the deps
1570 MOUNT(u
)->from_proc_self_mountinfo
= true;
1572 /* We have only allocated the stub now, let's enqueue this unit for loading now, so that everything else is
1574 unit_add_to_load_queue(u
);
1576 *ret_flags
= MOUNT_PROC_IS_MOUNTED
| MOUNT_PROC_JUST_MOUNTED
| MOUNT_PROC_JUST_CHANGED
;
1581 static int mount_setup_existing_unit(
1585 const char *options
,
1587 MountProcFlags
*ret_flags
) {
1594 if (!MOUNT(u
)->where
) {
1595 MOUNT(u
)->where
= strdup(where
);
1596 if (!MOUNT(u
)->where
)
1600 /* In case we have multiple mounts established on the same mount point, let's merge flags set already
1601 * for the current unit. Note that the flags field is reset on each iteration of reading
1602 * /proc/self/mountinfo, hence we know for sure anything already set here is from the current
1603 * iteration and thus worthy of taking into account. */
1604 MountProcFlags flags
=
1605 MOUNT(u
)->proc_flags
| MOUNT_PROC_IS_MOUNTED
;
1607 r
= update_parameters_proc_self_mountinfo(MOUNT(u
), what
, options
, fstype
);
1611 flags
|= MOUNT_PROC_JUST_CHANGED
;
1613 /* There are two conditions when we consider a mount point just mounted: when we haven't seen it in
1614 * /proc/self/mountinfo before or when MOUNT_MOUNTING is our current state. Why bother with the
1615 * latter? Shouldn't that be covered by the former? No, during reload it is not because we might then
1616 * encounter a new /proc/self/mountinfo in combination with an old mount unit state (since it stems
1617 * from the serialized state), and need to catch up. Since we know that the MOUNT_MOUNTING state is
1618 * reached when we wait for the mount to appear we hence can assume that if we are in it, we are
1619 * actually seeing it established for the first time. */
1620 if (!MOUNT(u
)->from_proc_self_mountinfo
|| MOUNT(u
)->state
== MOUNT_MOUNTING
)
1621 flags
|= MOUNT_PROC_JUST_MOUNTED
;
1623 MOUNT(u
)->from_proc_self_mountinfo
= true;
1625 if (IN_SET(u
->load_state
, UNIT_NOT_FOUND
, UNIT_BAD_SETTING
, UNIT_ERROR
)) {
1626 /* The unit was previously not found or otherwise not loaded. Now that the unit shows up in
1627 * /proc/self/mountinfo we should reconsider it this, hence set it to UNIT_LOADED. */
1628 u
->load_state
= UNIT_LOADED
;
1631 flags
|= MOUNT_PROC_JUST_CHANGED
;
1634 if (FLAGS_SET(flags
, MOUNT_PROC_JUST_CHANGED
)) {
1635 /* If things changed, then make sure that all deps are regenerated. Let's
1636 * first remove all automatic deps, and then add in the new ones. */
1638 unit_remove_dependencies(u
, UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
);
1640 r
= mount_add_non_exec_dependencies(MOUNT(u
));
1649 static int mount_setup_unit(
1653 const char *options
,
1657 _cleanup_free_
char *e
= NULL
;
1658 MountProcFlags flags
;
1668 /* Ignore API mount points. They should never be referenced in
1669 * dependencies ever. */
1670 if (mount_point_is_api(where
) || mount_point_ignore(where
))
1673 if (streq(fstype
, "autofs"))
1676 /* probably some kind of swap, ignore */
1677 if (!is_path(where
))
1680 /* Mount unit names have to be (like all other unit names) short enough to fit into file names. This
1681 * means there's a good chance that overly long mount point paths after mangling them to look like a
1682 * unit name would result in unit names we don't actually consider valid. This should be OK however
1683 * as such long mount point paths should not happen on regular systems — and if they appear
1684 * nonetheless they are generally synthesized by software, and thus managed by that other
1685 * software. Having such long names just means you cannot use systemd to manage those specific mount
1686 * points, which should be an OK restriction to make. After all we don't have to be able to manage
1687 * all mount points in the world — as long as we don't choke on them when we encounter them. */
1688 r
= unit_name_from_path(where
, ".mount", &e
);
1690 static RateLimit rate_limit
= { /* Let's log about this at warning level at most once every
1691 * 5s. Given that we generate this whenever we read the file
1692 * otherwise we probably shouldn't flood the logs with
1694 .interval
= 5 * USEC_PER_SEC
,
1698 return log_struct_errno(
1699 ratelimit_below(&rate_limit
) ? LOG_WARNING
: LOG_DEBUG
, r
,
1700 "MESSAGE_ID=" SD_MESSAGE_MOUNT_POINT_PATH_NOT_SUITABLE_STR
,
1701 "MOUNT_POINT=%s", where
,
1702 LOG_MESSAGE("Failed to generate valid unit name from path '%s', ignoring mount point: %m", where
));
1705 u
= manager_get_unit(m
, e
);
1707 r
= mount_setup_existing_unit(u
, what
, where
, options
, fstype
, &flags
);
1709 /* First time we see this mount point meaning that it's not been initiated by a mount unit but rather
1710 * by the sysadmin having called mount(8) directly. */
1711 r
= mount_setup_new_unit(m
, e
, what
, where
, options
, fstype
, &flags
, &u
);
1713 return log_warning_errno(r
, "Failed to set up mount unit for '%s': %m", where
);
1715 /* If the mount changed properties or state, let's notify our clients */
1716 if (flags
& (MOUNT_PROC_JUST_CHANGED
|MOUNT_PROC_JUST_MOUNTED
))
1717 unit_add_to_dbus_queue(u
);
1720 MOUNT(u
)->proc_flags
= flags
;
1725 static int mount_load_proc_self_mountinfo(Manager
*m
, bool set_flags
) {
1726 _cleanup_(mnt_free_tablep
) struct libmnt_table
*table
= NULL
;
1727 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*iter
= NULL
;
1732 r
= libmount_parse(NULL
, NULL
, &table
, &iter
);
1734 return log_error_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
1737 struct libmnt_fs
*fs
;
1738 const char *device
, *path
, *options
, *fstype
;
1740 r
= mnt_table_next_fs(table
, iter
, &fs
);
1744 return log_error_errno(r
, "Failed to get next entry from /proc/self/mountinfo: %m");
1746 device
= mnt_fs_get_source(fs
);
1747 path
= mnt_fs_get_target(fs
);
1748 options
= mnt_fs_get_options(fs
);
1749 fstype
= mnt_fs_get_fstype(fs
);
1751 if (!device
|| !path
)
1754 device_found_node(m
, device
, DEVICE_FOUND_MOUNT
, DEVICE_FOUND_MOUNT
);
1756 (void) mount_setup_unit(m
, device
, path
, options
, fstype
, set_flags
);
1762 static void mount_shutdown(Manager
*m
) {
1765 m
->mount_event_source
= sd_event_source_unref(m
->mount_event_source
);
1767 mnt_unref_monitor(m
->mount_monitor
);
1768 m
->mount_monitor
= NULL
;
1771 static int mount_get_timeout(Unit
*u
, usec_t
*timeout
) {
1772 Mount
*m
= MOUNT(u
);
1776 if (!m
->timer_event_source
)
1779 r
= sd_event_source_get_time(m
->timer_event_source
, &t
);
1782 if (t
== USEC_INFINITY
)
1789 static void mount_enumerate_perpetual(Manager
*m
) {
1795 /* Whatever happens, we know for sure that the root directory is around, and cannot go away. Let's
1796 * unconditionally synthesize it here and mark it as perpetual. */
1798 u
= manager_get_unit(m
, SPECIAL_ROOT_MOUNT
);
1800 r
= unit_new_for_name(m
, sizeof(Mount
), SPECIAL_ROOT_MOUNT
, &u
);
1802 log_error_errno(r
, "Failed to allocate the special " SPECIAL_ROOT_MOUNT
" unit: %m");
1807 u
->perpetual
= true;
1808 MOUNT(u
)->deserialized_state
= MOUNT_MOUNTED
;
1810 unit_add_to_load_queue(u
);
1811 unit_add_to_dbus_queue(u
);
1814 static bool mount_is_mounted(Mount
*m
) {
1817 return UNIT(m
)->perpetual
|| FLAGS_SET(m
->proc_flags
, MOUNT_PROC_IS_MOUNTED
);
1820 static void mount_enumerate(Manager
*m
) {
1827 if (!m
->mount_monitor
) {
1830 m
->mount_monitor
= mnt_new_monitor();
1831 if (!m
->mount_monitor
) {
1836 r
= mnt_monitor_enable_kernel(m
->mount_monitor
, 1);
1838 log_error_errno(r
, "Failed to enable watching of kernel mount events: %m");
1842 r
= mnt_monitor_enable_userspace(m
->mount_monitor
, 1, NULL
);
1844 log_error_errno(r
, "Failed to enable watching of userspace mount events: %m");
1848 /* mnt_unref_monitor() will close the fd */
1849 fd
= r
= mnt_monitor_get_fd(m
->mount_monitor
);
1851 log_error_errno(r
, "Failed to acquire watch file descriptor: %m");
1855 r
= sd_event_add_io(m
->event
, &m
->mount_event_source
, fd
, EPOLLIN
, mount_dispatch_io
, m
);
1857 log_error_errno(r
, "Failed to watch mount file descriptor: %m");
1861 r
= sd_event_source_set_priority(m
->mount_event_source
, SD_EVENT_PRIORITY_NORMAL
-10);
1863 log_error_errno(r
, "Failed to adjust mount watch priority: %m");
1867 (void) sd_event_source_set_description(m
->mount_event_source
, "mount-monitor-dispatch");
1870 r
= mount_load_proc_self_mountinfo(m
, false);
1880 static int drain_libmount(Manager
*m
) {
1881 bool rescan
= false;
1886 /* Drain all events and verify that the event is valid.
1888 * Note that libmount also monitors /run/mount mkdir if the directory does not exist yet. The mkdir
1889 * may generate event which is irrelevant for us.
1891 * error: r < 0; valid: r == 0, false positive: r == 1 */
1893 r
= mnt_monitor_next_change(m
->mount_monitor
, NULL
, NULL
);
1895 return log_error_errno(r
, "Failed to drain libmount events: %m");
1903 static int mount_process_proc_self_mountinfo(Manager
*m
) {
1904 _cleanup_set_free_free_ Set
*around
= NULL
, *gone
= NULL
;
1912 r
= drain_libmount(m
);
1916 r
= mount_load_proc_self_mountinfo(m
, true);
1918 /* Reset flags, just in case, for later calls */
1919 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
])
1920 MOUNT(u
)->proc_flags
= 0;
1925 manager_dispatch_load_queue(m
);
1927 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
]) {
1928 Mount
*mount
= MOUNT(u
);
1930 if (!mount_is_mounted(mount
)) {
1932 /* A mount point is not around right now. It
1933 * might be gone, or might never have
1936 if (mount
->from_proc_self_mountinfo
&&
1937 mount
->parameters_proc_self_mountinfo
.what
) {
1939 /* Remember that this device might just have disappeared */
1940 if (set_ensure_allocated(&gone
, &path_hash_ops
) < 0 ||
1941 set_put_strdup(&gone
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
1942 log_oom(); /* we don't care too much about OOM here... */
1945 mount
->from_proc_self_mountinfo
= false;
1946 assert_se(update_parameters_proc_self_mountinfo(mount
, NULL
, NULL
, NULL
) >= 0);
1948 switch (mount
->state
) {
1951 /* This has just been unmounted by somebody else, follow the state change. */
1952 mount_enter_dead(mount
, MOUNT_SUCCESS
);
1959 } else if (mount
->proc_flags
& (MOUNT_PROC_JUST_MOUNTED
|MOUNT_PROC_JUST_CHANGED
)) {
1961 /* A mount point was added or changed */
1963 switch (mount
->state
) {
1968 /* This has just been mounted by somebody else, follow the state change, but let's
1969 * generate a new invocation ID for this implicitly and automatically. */
1970 (void) unit_acquire_invocation_id(u
);
1971 mount_cycle_clear(mount
);
1972 mount_enter_mounted(mount
, MOUNT_SUCCESS
);
1975 case MOUNT_MOUNTING
:
1976 mount_set_state(mount
, MOUNT_MOUNTING_DONE
);
1980 /* Nothing really changed, but let's
1981 * issue an notification call
1982 * nonetheless, in case somebody is
1983 * waiting for this. (e.g. file system
1984 * ro/rw remounts.) */
1985 mount_set_state(mount
, mount
->state
);
1990 if (mount_is_mounted(mount
) &&
1991 mount
->from_proc_self_mountinfo
&&
1992 mount
->parameters_proc_self_mountinfo
.what
) {
1993 /* Track devices currently used */
1995 if (set_ensure_allocated(&around
, &path_hash_ops
) < 0 ||
1996 set_put_strdup(&around
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
2000 /* Reset the flags for later calls */
2001 mount
->proc_flags
= 0;
2004 SET_FOREACH(what
, gone
, i
) {
2005 if (set_contains(around
, what
))
2008 /* Let the device units know that the device is no longer mounted */
2009 device_found_node(m
, what
, 0, DEVICE_FOUND_MOUNT
);
2015 static int mount_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
2016 Manager
*m
= userdata
;
2019 assert(revents
& EPOLLIN
);
2021 return mount_process_proc_self_mountinfo(m
);
2024 static void mount_reset_failed(Unit
*u
) {
2025 Mount
*m
= MOUNT(u
);
2029 if (m
->state
== MOUNT_FAILED
)
2030 mount_set_state(m
, MOUNT_DEAD
);
2032 m
->result
= MOUNT_SUCCESS
;
2033 m
->reload_result
= MOUNT_SUCCESS
;
2034 m
->clean_result
= MOUNT_SUCCESS
;
2037 static int mount_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
2038 Mount
*m
= MOUNT(u
);
2042 return unit_kill_common(u
, who
, signo
, -1, m
->control_pid
, error
);
2045 static int mount_control_pid(Unit
*u
) {
2046 Mount
*m
= MOUNT(u
);
2050 return m
->control_pid
;
2053 static int mount_clean(Unit
*u
, ExecCleanMask mask
) {
2054 _cleanup_strv_free_
char **l
= NULL
;
2055 Mount
*m
= MOUNT(u
);
2061 if (m
->state
!= MOUNT_DEAD
)
2064 r
= exec_context_get_clean_directories(&m
->exec_context
, u
->manager
->prefix
, mask
, &l
);
2068 if (strv_isempty(l
))
2071 mount_unwatch_control_pid(m
);
2072 m
->clean_result
= MOUNT_SUCCESS
;
2073 m
->control_command
= NULL
;
2074 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
2076 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->exec_context
.timeout_clean_usec
));
2080 r
= unit_fork_and_watch_rm_rf(u
, l
, &m
->control_pid
);
2084 mount_set_state(m
, MOUNT_CLEANING
);
2089 log_unit_warning_errno(u
, r
, "Failed to initiate cleaning: %m");
2090 m
->clean_result
= MOUNT_FAILURE_RESOURCES
;
2091 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
2095 static int mount_can_clean(Unit
*u
, ExecCleanMask
*ret
) {
2096 Mount
*m
= MOUNT(u
);
2100 return exec_context_get_clean_mask(&m
->exec_context
, ret
);
2103 static const char* const mount_exec_command_table
[_MOUNT_EXEC_COMMAND_MAX
] = {
2104 [MOUNT_EXEC_MOUNT
] = "ExecMount",
2105 [MOUNT_EXEC_UNMOUNT
] = "ExecUnmount",
2106 [MOUNT_EXEC_REMOUNT
] = "ExecRemount",
2109 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command
, MountExecCommand
);
2111 static const char* const mount_result_table
[_MOUNT_RESULT_MAX
] = {
2112 [MOUNT_SUCCESS
] = "success",
2113 [MOUNT_FAILURE_RESOURCES
] = "resources",
2114 [MOUNT_FAILURE_TIMEOUT
] = "timeout",
2115 [MOUNT_FAILURE_EXIT_CODE
] = "exit-code",
2116 [MOUNT_FAILURE_SIGNAL
] = "signal",
2117 [MOUNT_FAILURE_CORE_DUMP
] = "core-dump",
2118 [MOUNT_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
2119 [MOUNT_FAILURE_PROTOCOL
] = "protocol",
2122 DEFINE_STRING_TABLE_LOOKUP(mount_result
, MountResult
);
2124 const UnitVTable mount_vtable
= {
2125 .object_size
= sizeof(Mount
),
2126 .exec_context_offset
= offsetof(Mount
, exec_context
),
2127 .cgroup_context_offset
= offsetof(Mount
, cgroup_context
),
2128 .kill_context_offset
= offsetof(Mount
, kill_context
),
2129 .exec_runtime_offset
= offsetof(Mount
, exec_runtime
),
2130 .dynamic_creds_offset
= offsetof(Mount
, dynamic_creds
),
2136 .private_section
= "Mount",
2138 .can_transient
= true,
2145 .coldplug
= mount_coldplug
,
2149 .start
= mount_start
,
2151 .reload
= mount_reload
,
2154 .clean
= mount_clean
,
2155 .can_clean
= mount_can_clean
,
2157 .serialize
= mount_serialize
,
2158 .deserialize_item
= mount_deserialize_item
,
2160 .active_state
= mount_active_state
,
2161 .sub_state_to_string
= mount_sub_state_to_string
,
2163 .will_restart
= unit_will_restart_default
,
2165 .may_gc
= mount_may_gc
,
2167 .sigchld_event
= mount_sigchld_event
,
2169 .reset_failed
= mount_reset_failed
,
2171 .control_pid
= mount_control_pid
,
2173 .bus_set_property
= bus_mount_set_property
,
2174 .bus_commit_properties
= bus_mount_commit_properties
,
2176 .get_timeout
= mount_get_timeout
,
2178 .enumerate_perpetual
= mount_enumerate_perpetual
,
2179 .enumerate
= mount_enumerate
,
2180 .shutdown
= mount_shutdown
,
2182 .status_message_formats
= {
2183 .starting_stopping
= {
2184 [0] = "Mounting %s...",
2185 [1] = "Unmounting %s...",
2187 .finished_start_job
= {
2188 [JOB_DONE
] = "Mounted %s.",
2189 [JOB_FAILED
] = "Failed to mount %s.",
2190 [JOB_TIMEOUT
] = "Timed out mounting %s.",
2192 .finished_stop_job
= {
2193 [JOB_DONE
] = "Unmounted %s.",
2194 [JOB_FAILED
] = "Failed unmounting %s.",
2195 [JOB_TIMEOUT
] = "Timed out unmounting %s.",