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
) {
289 if (!path_equal(m
->where
, "/")) {
290 _cleanup_free_
char *parent
= NULL
;
292 /* Adds in links to other mount points that might lie further up in the hierarchy */
294 parent
= dirname_malloc(m
->where
);
298 r
= unit_require_mounts_for(UNIT(m
), parent
, UNIT_DEPENDENCY_IMPLICIT
);
303 /* Adds in dependencies to other mount points that might be needed for the source path (if this is a bind mount
304 * or a loop mount) to be available. */
305 pm
= get_mount_parameters_fragment(m
);
306 if (pm
&& pm
->what
&&
307 path_is_absolute(pm
->what
) &&
308 (mount_is_bind(pm
) || mount_is_loop(pm
) || !mount_is_network(pm
))) {
310 r
= unit_require_mounts_for(UNIT(m
), pm
->what
, UNIT_DEPENDENCY_FILE
);
315 /* Adds in dependencies to other units that use this path or paths further down in the hierarchy */
316 s
= manager_get_units_requiring_mounts_for(UNIT(m
)->manager
, m
->where
);
317 SET_FOREACH(other
, s
) {
319 if (other
->load_state
!= UNIT_LOADED
)
322 if (other
== UNIT(m
))
325 r
= unit_add_dependency(other
, UNIT_AFTER
, UNIT(m
), true, UNIT_DEPENDENCY_PATH
);
329 if (UNIT(m
)->fragment_path
) {
330 /* If we have fragment configuration, then make this dependency required */
331 r
= unit_add_dependency(other
, UNIT_REQUIRES
, UNIT(m
), true, UNIT_DEPENDENCY_PATH
);
340 static int mount_add_device_dependencies(Mount
*m
) {
341 UnitDependencyMask mask
;
348 p
= get_mount_parameters(m
);
355 if (mount_is_bind(p
))
358 if (!is_device_path(p
->what
))
361 /* /dev/root is a really weird thing, it's not a real device, but just a path the kernel exports for
362 * the root file system specified on the kernel command line. Ignore it here. */
363 if (PATH_IN_SET(p
->what
, "/dev/root", "/dev/nfs"))
366 if (path_equal(m
->where
, "/"))
369 /* Mount units from /proc/self/mountinfo are not bound to devices by default since they're subject to
370 * races when devices are unplugged. But the user can still force this dep with an appropriate option
371 * (or udev property) so the mount units are automatically stopped when the device disappears
373 dep
= mount_is_bound_to_device(m
) ? UNIT_BINDS_TO
: UNIT_REQUIRES
;
375 /* We always use 'what' from /proc/self/mountinfo if mounted */
376 mask
= m
->from_proc_self_mountinfo
? UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
: UNIT_DEPENDENCY_FILE
;
378 r
= unit_add_node_dependency(UNIT(m
), p
->what
, dep
, mask
);
382 return unit_add_blockdev_dependency(UNIT(m
), p
->what
, mask
);
385 static int mount_add_quota_dependencies(Mount
*m
) {
386 UnitDependencyMask mask
;
392 if (!MANAGER_IS_SYSTEM(UNIT(m
)->manager
))
395 p
= get_mount_parameters_fragment(m
);
399 if (!mount_needs_quota(p
))
402 mask
= m
->from_fragment
? UNIT_DEPENDENCY_FILE
: UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
;
404 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTACHECK_SERVICE
, true, mask
);
408 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTAON_SERVICE
, true, mask
);
415 static bool mount_is_extrinsic(Mount
*m
) {
419 /* Returns true for all units that are "magic" and should be excluded from the usual
420 * start-up and shutdown dependencies. We call them "extrinsic" here, as they are generally
421 * mounted outside of the systemd dependency logic. We shouldn't attempt to manage them
422 * ourselves but it's fine if the user operates on them with us. */
424 /* We only automatically manage mounts if we are in system mode */
425 if (!MANAGER_IS_SYSTEM(UNIT(m
)->manager
))
428 if (UNIT(m
)->perpetual
) /* All perpetual units never change state */
431 p
= get_mount_parameters(m
);
432 if (p
&& fstab_is_extrinsic(m
->where
, p
->options
))
438 static int mount_add_default_ordering_dependencies(
441 UnitDependencyMask mask
) {
443 const char *after
, *before
, *e
;
448 e
= path_startswith(m
->where
, "/sysroot");
449 if (e
&& in_initrd()) {
450 /* All mounts under /sysroot need to happen later, at initrd-fs.target time. IOW,
451 * it's not technically part of the basic initrd filesystem itself, and so
452 * shouldn't inherit the default Before=local-fs.target dependency. */
455 before
= isempty(e
) ? SPECIAL_INITRD_ROOT_FS_TARGET
: SPECIAL_INITRD_FS_TARGET
;
457 } else if (mount_is_network(p
)) {
458 after
= SPECIAL_REMOTE_FS_PRE_TARGET
;
459 before
= SPECIAL_REMOTE_FS_TARGET
;
462 after
= SPECIAL_LOCAL_FS_PRE_TARGET
;
463 before
= SPECIAL_LOCAL_FS_TARGET
;
466 if (!mount_is_nofail(m
) && !mount_is_automount(p
)) {
467 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_BEFORE
, before
, true, mask
);
473 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, after
, true, mask
);
478 return unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_CONFLICTS
,
479 SPECIAL_UMOUNT_TARGET
, true, mask
);
482 static int mount_add_default_dependencies(Mount
*m
) {
483 UnitDependencyMask mask
;
489 if (!UNIT(m
)->default_dependencies
)
492 /* We do not add any default dependencies to /, /usr or /run/initramfs/, since they are
493 * guaranteed to stay mounted the whole time, since our system is on it. Also, don't
494 * bother with anything mounted below virtual file systems, it's also going to be virtual,
495 * and hence not worth the effort. */
496 if (mount_is_extrinsic(m
))
499 p
= get_mount_parameters(m
);
503 mask
= m
->from_fragment
? UNIT_DEPENDENCY_FILE
: UNIT_DEPENDENCY_MOUNTINFO_DEFAULT
;
505 r
= mount_add_default_ordering_dependencies(m
, p
, mask
);
509 if (mount_is_network(p
)) {
510 /* We order ourselves after network.target. This is primarily useful at shutdown:
511 * services that take down the network should order themselves before
512 * network.target, so that they are shut down only after this mount unit is
515 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, SPECIAL_NETWORK_TARGET
, true, mask
);
519 /* We pull in network-online.target, and order ourselves after it. This is useful
520 * at start-up to actively pull in tools that want to be started before we start
521 * mounting network file systems, and whose purpose it is to delay this until the
522 * network is "up". */
524 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_WANTS
, UNIT_AFTER
, SPECIAL_NETWORK_ONLINE_TARGET
, true, mask
);
529 /* If this is a tmpfs mount then we have to unmount it before we try to deactivate swaps */
530 if (streq_ptr(p
->fstype
, "tmpfs")) {
531 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, SPECIAL_SWAP_TARGET
, true, mask
);
539 static int mount_verify(Mount
*m
) {
540 _cleanup_free_
char *e
= NULL
;
545 assert(UNIT(m
)->load_state
== UNIT_LOADED
);
547 if (!m
->from_fragment
&& !m
->from_proc_self_mountinfo
&& !UNIT(m
)->perpetual
)
550 r
= unit_name_from_path(m
->where
, ".mount", &e
);
552 return log_unit_error_errno(UNIT(m
), r
, "Failed to generate unit name from mount path: %m");
554 if (!unit_has_name(UNIT(m
), e
)) {
555 log_unit_error(UNIT(m
), "Where= setting doesn't match unit name. Refusing.");
559 if (mount_point_is_api(m
->where
) || mount_point_ignore(m
->where
)) {
560 log_unit_error(UNIT(m
), "Cannot create mount unit for API file system %s. Refusing.", m
->where
);
564 p
= get_mount_parameters_fragment(m
);
565 if (p
&& !p
->what
&& !UNIT(m
)->perpetual
)
566 return log_unit_error_errno(UNIT(m
), SYNTHETIC_ERRNO(ENOEXEC
),
567 "What= setting is missing. Refusing.");
569 if (m
->exec_context
.pam_name
&& m
->kill_context
.kill_mode
!= KILL_CONTROL_GROUP
) {
570 log_unit_error(UNIT(m
), "Unit has PAM enabled. Kill mode must be set to control-group'. Refusing.");
577 static int mount_add_non_exec_dependencies(Mount
*m
) {
581 /* Adds in all dependencies directly responsible for ordering the mount, as opposed to dependencies
582 * resulting from the ExecContext and such. */
584 r
= mount_add_device_dependencies(m
);
588 r
= mount_add_mount_dependencies(m
);
592 r
= mount_add_quota_dependencies(m
);
596 r
= mount_add_default_dependencies(m
);
603 static int mount_add_extras(Mount
*m
) {
609 /* Note: this call might be called after we already have been loaded once (and even when it has already been
610 * activated), in case data from /proc/self/mountinfo has changed. This means all code here needs to be ready
611 * to run with an already set up unit. */
613 if (u
->fragment_path
)
614 m
->from_fragment
= true;
617 r
= unit_name_to_path(u
->id
, &m
->where
);
622 path_simplify(m
->where
, false);
624 if (!u
->description
) {
625 r
= unit_set_description(u
, m
->where
);
630 r
= unit_patch_contexts(u
);
634 r
= unit_add_exec_dependencies(u
, &m
->exec_context
);
638 r
= unit_set_default_slice(u
);
642 r
= mount_add_non_exec_dependencies(m
);
649 static void mount_load_root_mount(Unit
*u
) {
652 if (!unit_has_name(u
, SPECIAL_ROOT_MOUNT
))
656 u
->default_dependencies
= false;
658 /* The stdio/kmsg bridge socket is on /, in order to avoid a dep loop, don't use kmsg logging for -.mount */
659 MOUNT(u
)->exec_context
.std_output
= EXEC_OUTPUT_NULL
;
660 MOUNT(u
)->exec_context
.std_input
= EXEC_INPUT_NULL
;
663 u
->description
= strdup("Root Mount");
666 static int mount_load(Unit
*u
) {
671 assert(u
->load_state
== UNIT_STUB
);
673 mount_load_root_mount(u
);
675 bool fragment_optional
= m
->from_proc_self_mountinfo
|| u
->perpetual
;
676 r
= unit_load_fragment_and_dropin(u
, !fragment_optional
);
678 /* Add in some extras. Note we do this in all cases (even if we failed to load the unit) when announced by the
679 * kernel, because we need some things to be set up no matter what when the kernel establishes a mount and thus
680 * we need to update the state in our unit to track it. After all, consider that we don't allow changing the
681 * 'slice' field for a unit once it is active. */
682 if (u
->load_state
== UNIT_LOADED
|| m
->from_proc_self_mountinfo
|| u
->perpetual
)
683 q
= mount_add_extras(m
);
689 if (u
->load_state
!= UNIT_LOADED
)
692 return mount_verify(m
);
695 static void mount_set_state(Mount
*m
, MountState state
) {
696 MountState old_state
;
699 if (m
->state
!= state
)
700 bus_unit_send_pending_change_signal(UNIT(m
), false);
702 old_state
= m
->state
;
705 if (!MOUNT_STATE_WITH_PROCESS(state
)) {
706 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
707 mount_unwatch_control_pid(m
);
708 m
->control_command
= NULL
;
709 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
712 if (state
!= old_state
)
713 log_unit_debug(UNIT(m
), "Changed %s -> %s", mount_state_to_string(old_state
), mount_state_to_string(state
));
715 unit_notify(UNIT(m
), state_translation_table
[old_state
], state_translation_table
[state
],
716 m
->reload_result
== MOUNT_SUCCESS
? 0 : UNIT_NOTIFY_RELOAD_FAILURE
);
719 static int mount_coldplug(Unit
*u
) {
721 MountState new_state
= MOUNT_DEAD
;
725 assert(m
->state
== MOUNT_DEAD
);
727 if (m
->deserialized_state
!= m
->state
)
728 new_state
= m
->deserialized_state
;
729 else if (m
->from_proc_self_mountinfo
)
730 new_state
= MOUNT_MOUNTED
;
732 if (new_state
== m
->state
)
735 if (m
->control_pid
> 0 &&
736 pid_is_unwaited(m
->control_pid
) &&
737 MOUNT_STATE_WITH_PROCESS(new_state
)) {
739 r
= unit_watch_pid(UNIT(m
), m
->control_pid
, false);
743 r
= mount_arm_timer(m
, usec_add(u
->state_change_timestamp
.monotonic
, m
->timeout_usec
));
748 if (!IN_SET(new_state
, MOUNT_DEAD
, MOUNT_FAILED
)) {
749 (void) unit_setup_dynamic_creds(u
);
750 (void) unit_setup_exec_runtime(u
);
753 mount_set_state(m
, new_state
);
757 static void mount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
758 char buf
[FORMAT_TIMESPAN_MAX
];
765 p
= get_mount_parameters(m
);
768 "%sMount State: %s\n"
770 "%sClean Result: %s\n"
773 "%sFile System Type: %s\n"
775 "%sFrom /proc/self/mountinfo: %s\n"
776 "%sFrom fragment: %s\n"
778 "%sDirectoryMode: %04o\n"
779 "%sSloppyOptions: %s\n"
780 "%sLazyUnmount: %s\n"
781 "%sForceUnmount: %s\n"
782 "%sReadWriteOnly: %s\n"
783 "%sTimeoutSec: %s\n",
784 prefix
, mount_state_to_string(m
->state
),
785 prefix
, mount_result_to_string(m
->result
),
786 prefix
, mount_result_to_string(m
->clean_result
),
788 prefix
, p
? strna(p
->what
) : "n/a",
789 prefix
, p
? strna(p
->fstype
) : "n/a",
790 prefix
, p
? strna(p
->options
) : "n/a",
791 prefix
, yes_no(m
->from_proc_self_mountinfo
),
792 prefix
, yes_no(m
->from_fragment
),
793 prefix
, yes_no(mount_is_extrinsic(m
)),
794 prefix
, m
->directory_mode
,
795 prefix
, yes_no(m
->sloppy_options
),
796 prefix
, yes_no(m
->lazy_unmount
),
797 prefix
, yes_no(m
->force_unmount
),
798 prefix
, yes_no(m
->read_write_only
),
799 prefix
, format_timespan(buf
, sizeof(buf
), m
->timeout_usec
, USEC_PER_SEC
));
801 if (m
->control_pid
> 0)
803 "%sControl PID: "PID_FMT
"\n",
804 prefix
, m
->control_pid
);
806 exec_context_dump(&m
->exec_context
, f
, prefix
);
807 kill_context_dump(&m
->kill_context
, f
, prefix
);
808 cgroup_context_dump(UNIT(m
), f
, prefix
);
811 static int mount_spawn(Mount
*m
, ExecCommand
*c
, pid_t
*_pid
) {
813 _cleanup_(exec_params_clear
) ExecParameters exec_params
= {
814 .flags
= EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
,
827 r
= unit_prepare_exec(UNIT(m
));
831 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->timeout_usec
));
835 r
= unit_set_exec_params(UNIT(m
), &exec_params
);
839 r
= exec_spawn(UNIT(m
),
849 r
= unit_watch_pid(UNIT(m
), pid
, true);
858 static void mount_enter_dead(Mount
*m
, MountResult f
) {
861 if (m
->result
== MOUNT_SUCCESS
)
864 unit_log_result(UNIT(m
), m
->result
== MOUNT_SUCCESS
, mount_result_to_string(m
->result
));
865 unit_warn_leftover_processes(UNIT(m
), unit_log_leftover_process_stop
);
867 mount_set_state(m
, m
->result
!= MOUNT_SUCCESS
? MOUNT_FAILED
: MOUNT_DEAD
);
869 m
->exec_runtime
= exec_runtime_unref(m
->exec_runtime
, true);
871 unit_destroy_runtime_data(UNIT(m
), &m
->exec_context
);
873 unit_unref_uid_gid(UNIT(m
), true);
875 dynamic_creds_destroy(&m
->dynamic_creds
);
877 /* Any dependencies based on /proc/self/mountinfo are now stale */
878 unit_remove_dependencies(UNIT(m
), UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
);
881 static void mount_enter_mounted(Mount
*m
, MountResult f
) {
884 if (m
->result
== MOUNT_SUCCESS
)
887 mount_set_state(m
, MOUNT_MOUNTED
);
890 static void mount_enter_dead_or_mounted(Mount
*m
, MountResult f
) {
893 /* Enter DEAD or MOUNTED state, depending on what the kernel currently says about the mount point. We use this
894 * whenever we executed an operation, so that our internal state reflects what the kernel says again, after all
895 * ultimately we just mirror the kernel's internal state on this. */
897 if (m
->from_proc_self_mountinfo
)
898 mount_enter_mounted(m
, f
);
900 mount_enter_dead(m
, f
);
903 static int state_to_kill_operation(MountState state
) {
906 case MOUNT_REMOUNTING_SIGTERM
:
909 case MOUNT_UNMOUNTING_SIGTERM
:
910 return KILL_TERMINATE
;
912 case MOUNT_REMOUNTING_SIGKILL
:
913 case MOUNT_UNMOUNTING_SIGKILL
:
917 return _KILL_OPERATION_INVALID
;
921 static void mount_enter_signal(Mount
*m
, MountState state
, MountResult f
) {
926 if (m
->result
== MOUNT_SUCCESS
)
929 r
= unit_kill_context(
932 state_to_kill_operation(state
),
940 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->timeout_usec
));
944 mount_set_state(m
, state
);
945 } else if (state
== MOUNT_REMOUNTING_SIGTERM
&& m
->kill_context
.send_sigkill
)
946 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
947 else if (IN_SET(state
, MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
))
948 mount_enter_mounted(m
, MOUNT_SUCCESS
);
949 else if (state
== MOUNT_UNMOUNTING_SIGTERM
&& m
->kill_context
.send_sigkill
)
950 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
952 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
957 log_unit_warning_errno(UNIT(m
), r
, "Failed to kill processes: %m");
958 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
961 static void mount_enter_unmounting(Mount
*m
) {
966 /* Start counting our attempts */
967 if (!IN_SET(m
->state
,
969 MOUNT_UNMOUNTING_SIGTERM
,
970 MOUNT_UNMOUNTING_SIGKILL
))
971 m
->n_retry_umount
= 0;
973 m
->control_command_id
= MOUNT_EXEC_UNMOUNT
;
974 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_UNMOUNT
;
976 r
= exec_command_set(m
->control_command
, UMOUNT_PATH
, m
->where
, "-c", NULL
);
977 if (r
>= 0 && m
->lazy_unmount
)
978 r
= exec_command_append(m
->control_command
, "-l", NULL
);
979 if (r
>= 0 && m
->force_unmount
)
980 r
= exec_command_append(m
->control_command
, "-f", NULL
);
984 mount_unwatch_control_pid(m
);
986 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
990 mount_set_state(m
, MOUNT_UNMOUNTING
);
995 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'umount' task: %m");
996 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
999 static void mount_enter_mounting(Mount
*m
) {
1005 r
= unit_fail_if_noncanonical(UNIT(m
), m
->where
);
1009 (void) mkdir_p_label(m
->where
, m
->directory_mode
);
1011 unit_warn_if_dir_nonempty(UNIT(m
), m
->where
);
1012 unit_warn_leftover_processes(UNIT(m
), unit_log_leftover_process_start
);
1014 m
->control_command_id
= MOUNT_EXEC_MOUNT
;
1015 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_MOUNT
;
1017 /* Create the source directory for bind-mounts if needed */
1018 p
= get_mount_parameters_fragment(m
);
1019 if (p
&& mount_is_bind(p
)) {
1020 r
= mkdir_p_label(p
->what
, m
->directory_mode
);
1022 log_unit_error_errno(UNIT(m
), r
, "Failed to make bind mount source '%s': %m", p
->what
);
1026 _cleanup_free_
char *opts
= NULL
;
1028 r
= fstab_filter_options(p
->options
, "nofail\0" "noauto\0" "auto\0", NULL
, NULL
, &opts
);
1032 r
= exec_command_set(m
->control_command
, MOUNT_PATH
, p
->what
, m
->where
, NULL
);
1033 if (r
>= 0 && m
->sloppy_options
)
1034 r
= exec_command_append(m
->control_command
, "-s", NULL
);
1035 if (r
>= 0 && m
->read_write_only
)
1036 r
= exec_command_append(m
->control_command
, "-w", NULL
);
1037 if (r
>= 0 && p
->fstype
)
1038 r
= exec_command_append(m
->control_command
, "-t", p
->fstype
, NULL
);
1039 if (r
>= 0 && !isempty(opts
))
1040 r
= exec_command_append(m
->control_command
, "-o", opts
, NULL
);
1046 mount_unwatch_control_pid(m
);
1048 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
1052 mount_set_state(m
, MOUNT_MOUNTING
);
1057 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'mount' task: %m");
1058 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
1061 static void mount_set_reload_result(Mount
*m
, MountResult result
) {
1064 /* Only store the first error we encounter */
1065 if (m
->reload_result
!= MOUNT_SUCCESS
)
1068 m
->reload_result
= result
;
1071 static void mount_enter_remounting(Mount
*m
) {
1077 /* Reset reload result when we are about to start a new remount operation */
1078 m
->reload_result
= MOUNT_SUCCESS
;
1080 m
->control_command_id
= MOUNT_EXEC_REMOUNT
;
1081 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_REMOUNT
;
1083 p
= get_mount_parameters_fragment(m
);
1088 o
= strjoina("remount,", p
->options
);
1092 r
= exec_command_set(m
->control_command
, MOUNT_PATH
,
1095 if (r
>= 0 && m
->sloppy_options
)
1096 r
= exec_command_append(m
->control_command
, "-s", NULL
);
1097 if (r
>= 0 && m
->read_write_only
)
1098 r
= exec_command_append(m
->control_command
, "-w", NULL
);
1099 if (r
>= 0 && p
->fstype
)
1100 r
= exec_command_append(m
->control_command
, "-t", p
->fstype
, NULL
);
1106 mount_unwatch_control_pid(m
);
1108 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
1112 mount_set_state(m
, MOUNT_REMOUNTING
);
1117 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'remount' task: %m");
1118 mount_set_reload_result(m
, MOUNT_FAILURE_RESOURCES
);
1119 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1122 static void mount_cycle_clear(Mount
*m
) {
1125 /* Clear all state we shall forget for this new cycle */
1127 m
->result
= MOUNT_SUCCESS
;
1128 m
->reload_result
= MOUNT_SUCCESS
;
1129 exec_command_reset_status_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
1130 UNIT(m
)->reset_accounting
= true;
1133 static int mount_start(Unit
*u
) {
1134 Mount
*m
= MOUNT(u
);
1139 /* We cannot fulfill this request right now, try again later
1141 if (IN_SET(m
->state
,
1143 MOUNT_UNMOUNTING_SIGTERM
,
1144 MOUNT_UNMOUNTING_SIGKILL
,
1148 /* Already on it! */
1149 if (m
->state
== MOUNT_MOUNTING
)
1152 assert(IN_SET(m
->state
, MOUNT_DEAD
, MOUNT_FAILED
));
1154 r
= unit_test_start_limit(u
);
1156 mount_enter_dead(m
, MOUNT_FAILURE_START_LIMIT_HIT
);
1160 r
= unit_acquire_invocation_id(u
);
1164 mount_cycle_clear(m
);
1165 mount_enter_mounting(m
);
1170 static int mount_stop(Unit
*u
) {
1171 Mount
*m
= MOUNT(u
);
1177 case MOUNT_UNMOUNTING
:
1178 case MOUNT_UNMOUNTING_SIGKILL
:
1179 case MOUNT_UNMOUNTING_SIGTERM
:
1183 case MOUNT_MOUNTING
:
1184 case MOUNT_MOUNTING_DONE
:
1185 case MOUNT_REMOUNTING
:
1186 /* If we are still waiting for /bin/mount, we go directly into kill mode. */
1187 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_SUCCESS
);
1190 case MOUNT_REMOUNTING_SIGTERM
:
1191 /* If we are already waiting for a hung remount, convert this to the matching unmounting state */
1192 mount_set_state(m
, MOUNT_UNMOUNTING_SIGTERM
);
1195 case MOUNT_REMOUNTING_SIGKILL
:
1197 mount_set_state(m
, MOUNT_UNMOUNTING_SIGKILL
);
1201 mount_enter_unmounting(m
);
1204 case MOUNT_CLEANING
:
1205 /* If we are currently cleaning, then abort it, brutally. */
1206 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
1210 assert_not_reached("Unexpected state.");
1214 static int mount_reload(Unit
*u
) {
1215 Mount
*m
= MOUNT(u
);
1218 assert(m
->state
== MOUNT_MOUNTED
);
1220 mount_enter_remounting(m
);
1225 static int mount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1226 Mount
*m
= MOUNT(u
);
1232 (void) serialize_item(f
, "state", mount_state_to_string(m
->state
));
1233 (void) serialize_item(f
, "result", mount_result_to_string(m
->result
));
1234 (void) serialize_item(f
, "reload-result", mount_result_to_string(m
->reload_result
));
1235 (void) serialize_item_format(f
, "n-retry-umount", "%u", m
->n_retry_umount
);
1237 if (m
->control_pid
> 0)
1238 (void) serialize_item_format(f
, "control-pid", PID_FMT
, m
->control_pid
);
1240 if (m
->control_command_id
>= 0)
1241 (void) serialize_item(f
, "control-command", mount_exec_command_to_string(m
->control_command_id
));
1246 static int mount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1247 Mount
*m
= MOUNT(u
);
1255 if (streq(key
, "state")) {
1258 if ((state
= mount_state_from_string(value
)) < 0)
1259 log_unit_debug(u
, "Failed to parse state value: %s", value
);
1261 m
->deserialized_state
= state
;
1263 } else if (streq(key
, "result")) {
1266 f
= mount_result_from_string(value
);
1268 log_unit_debug(u
, "Failed to parse result value: %s", value
);
1269 else if (f
!= MOUNT_SUCCESS
)
1272 } else if (streq(key
, "reload-result")) {
1275 f
= mount_result_from_string(value
);
1277 log_unit_debug(u
, "Failed to parse reload result value: %s", value
);
1278 else if (f
!= MOUNT_SUCCESS
)
1279 m
->reload_result
= f
;
1281 } else if (streq(key
, "n-retry-umount")) {
1283 r
= safe_atou(value
, &m
->n_retry_umount
);
1285 log_unit_debug(u
, "Failed to parse n-retry-umount value: %s", value
);
1287 } else if (streq(key
, "control-pid")) {
1289 if (parse_pid(value
, &m
->control_pid
) < 0)
1290 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
1292 } else if (streq(key
, "control-command")) {
1293 MountExecCommand id
;
1295 id
= mount_exec_command_from_string(value
);
1297 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
1299 m
->control_command_id
= id
;
1300 m
->control_command
= m
->exec_command
+ id
;
1303 log_unit_debug(u
, "Unknown serialization key: %s", key
);
1308 _pure_
static UnitActiveState
mount_active_state(Unit
*u
) {
1311 return state_translation_table
[MOUNT(u
)->state
];
1314 _pure_
static const char *mount_sub_state_to_string(Unit
*u
) {
1317 return mount_state_to_string(MOUNT(u
)->state
);
1320 _pure_
static bool mount_may_gc(Unit
*u
) {
1321 Mount
*m
= MOUNT(u
);
1325 if (m
->from_proc_self_mountinfo
)
1331 static void mount_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1332 Mount
*m
= MOUNT(u
);
1338 if (pid
!= m
->control_pid
)
1341 /* So here's the thing, we really want to know before /usr/bin/mount or /usr/bin/umount exit whether
1342 * they established/remove a mount. This is important when mounting, but even more so when unmounting
1343 * since we need to deal with nested mounts and otherwise cannot safely determine whether to repeat
1344 * the unmounts. In theory, the kernel fires /proc/self/mountinfo changes off before returning from
1345 * the mount() or umount() syscalls, and thus we should see the changes to the proc file before we
1346 * process the waitid() for the /usr/bin/(u)mount processes. However, this is unfortunately racy: we
1347 * have to waitid() for processes using P_ALL (since we need to reap unexpected children that got
1348 * reparented to PID 1), but when using P_ALL we might end up reaping processes that terminated just
1349 * instants ago, i.e. already after our last event loop iteration (i.e. after the last point we might
1350 * have noticed /proc/self/mountinfo events via epoll). This means event loop priorities for
1351 * processing SIGCHLD vs. /proc/self/mountinfo IO events are not as relevant as we want. To fix that
1352 * race, let's explicitly scan /proc/self/mountinfo before we start processing /usr/bin/(u)mount
1353 * dying. It's ugly, but it makes our ordering systematic again, and makes sure we always see
1354 * /proc/self/mountinfo changes before our mount/umount exits. */
1355 (void) mount_process_proc_self_mountinfo(u
->manager
);
1359 if (is_clean_exit(code
, status
, EXIT_CLEAN_COMMAND
, NULL
))
1361 else if (code
== CLD_EXITED
)
1362 f
= MOUNT_FAILURE_EXIT_CODE
;
1363 else if (code
== CLD_KILLED
)
1364 f
= MOUNT_FAILURE_SIGNAL
;
1365 else if (code
== CLD_DUMPED
)
1366 f
= MOUNT_FAILURE_CORE_DUMP
;
1368 assert_not_reached("Unknown code");
1370 if (IN_SET(m
->state
, MOUNT_REMOUNTING
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_REMOUNTING_SIGTERM
))
1371 mount_set_reload_result(m
, f
);
1372 else if (m
->result
== MOUNT_SUCCESS
)
1375 if (m
->control_command
) {
1376 exec_status_exit(&m
->control_command
->exec_status
, &m
->exec_context
, pid
, code
, status
);
1378 m
->control_command
= NULL
;
1379 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
1382 unit_log_process_exit(
1385 mount_exec_command_to_string(m
->control_command_id
),
1389 /* Note that due to the io event priority logic, we can be sure the new mountinfo is loaded
1390 * before we process the SIGCHLD for the mount command. */
1394 case MOUNT_MOUNTING
:
1395 /* Our mount point has not appeared in mountinfo. Something went wrong. */
1397 if (f
== MOUNT_SUCCESS
) {
1398 /* Either /bin/mount has an unexpected definition of success,
1399 * or someone raced us and we lost. */
1400 log_unit_warning(UNIT(m
), "Mount process finished, but there is no mount.");
1401 f
= MOUNT_FAILURE_PROTOCOL
;
1403 mount_enter_dead(m
, f
);
1406 case MOUNT_MOUNTING_DONE
:
1407 mount_enter_mounted(m
, f
);
1410 case MOUNT_REMOUNTING
:
1411 case MOUNT_REMOUNTING_SIGTERM
:
1412 case MOUNT_REMOUNTING_SIGKILL
:
1413 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1416 case MOUNT_UNMOUNTING
:
1418 if (f
== MOUNT_SUCCESS
&& m
->from_proc_self_mountinfo
) {
1420 /* Still a mount point? If so, let's try again. Most likely there were multiple mount points
1421 * stacked on top of each other. We might exceed the timeout specified by the user overall,
1422 * but we will stop as soon as any one umount times out. */
1424 if (m
->n_retry_umount
< RETRY_UMOUNT_MAX
) {
1425 log_unit_debug(u
, "Mount still present, trying again.");
1426 m
->n_retry_umount
++;
1427 mount_enter_unmounting(m
);
1429 log_unit_warning(u
, "Mount still present after %u attempts to unmount, giving up.", m
->n_retry_umount
);
1430 mount_enter_mounted(m
, f
);
1433 mount_enter_dead_or_mounted(m
, f
);
1437 case MOUNT_UNMOUNTING_SIGKILL
:
1438 case MOUNT_UNMOUNTING_SIGTERM
:
1439 mount_enter_dead_or_mounted(m
, f
);
1442 case MOUNT_CLEANING
:
1443 if (m
->clean_result
== MOUNT_SUCCESS
)
1444 m
->clean_result
= f
;
1446 mount_enter_dead(m
, MOUNT_SUCCESS
);
1450 assert_not_reached("Uh, control process died at wrong time.");
1453 /* Notify clients about changed exit status */
1454 unit_add_to_dbus_queue(u
);
1457 static int mount_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
1458 Mount
*m
= MOUNT(userdata
);
1461 assert(m
->timer_event_source
== source
);
1465 case MOUNT_MOUNTING
:
1466 case MOUNT_MOUNTING_DONE
:
1467 log_unit_warning(UNIT(m
), "Mounting timed out. Terminating.");
1468 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1471 case MOUNT_REMOUNTING
:
1472 log_unit_warning(UNIT(m
), "Remounting timed out. Terminating remount process.");
1473 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1474 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGTERM
, MOUNT_SUCCESS
);
1477 case MOUNT_REMOUNTING_SIGTERM
:
1478 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1480 if (m
->kill_context
.send_sigkill
) {
1481 log_unit_warning(UNIT(m
), "Remounting timed out. Killing.");
1482 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
1484 log_unit_warning(UNIT(m
), "Remounting timed out. Skipping SIGKILL. Ignoring.");
1485 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1489 case MOUNT_REMOUNTING_SIGKILL
:
1490 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1492 log_unit_warning(UNIT(m
), "Mount process still around after SIGKILL. Ignoring.");
1493 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1496 case MOUNT_UNMOUNTING
:
1497 log_unit_warning(UNIT(m
), "Unmounting timed out. Terminating.");
1498 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1501 case MOUNT_UNMOUNTING_SIGTERM
:
1502 if (m
->kill_context
.send_sigkill
) {
1503 log_unit_warning(UNIT(m
), "Mount process timed out. Killing.");
1504 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_FAILURE_TIMEOUT
);
1506 log_unit_warning(UNIT(m
), "Mount process timed out. Skipping SIGKILL. Ignoring.");
1507 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1511 case MOUNT_UNMOUNTING_SIGKILL
:
1512 log_unit_warning(UNIT(m
), "Mount process still around after SIGKILL. Ignoring.");
1513 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1516 case MOUNT_CLEANING
:
1517 log_unit_warning(UNIT(m
), "Cleaning timed out. killing.");
1519 if (m
->clean_result
== MOUNT_SUCCESS
)
1520 m
->clean_result
= MOUNT_FAILURE_TIMEOUT
;
1522 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, 0);
1526 assert_not_reached("Timeout at wrong time.");
1532 static int mount_setup_new_unit(
1537 const char *options
,
1539 MountProcFlags
*ret_flags
,
1542 _cleanup_(unit_freep
) Unit
*u
= NULL
;
1550 r
= unit_new_for_name(m
, sizeof(Mount
), name
, &u
);
1554 r
= free_and_strdup(&u
->source_path
, "/proc/self/mountinfo");
1558 r
= free_and_strdup(&MOUNT(u
)->where
, where
);
1562 r
= update_parameters_proc_self_mountinfo(MOUNT(u
), what
, options
, fstype
);
1566 /* This unit was generated because /proc/self/mountinfo reported it. Remember this, so that by the time we load
1567 * the unit file for it (and thus add in extra deps right after) we know what source to attributes the deps
1569 MOUNT(u
)->from_proc_self_mountinfo
= true;
1571 /* We have only allocated the stub now, let's enqueue this unit for loading now, so that everything else is
1573 unit_add_to_load_queue(u
);
1575 *ret_flags
= MOUNT_PROC_IS_MOUNTED
| MOUNT_PROC_JUST_MOUNTED
| MOUNT_PROC_JUST_CHANGED
;
1580 static int mount_setup_existing_unit(
1584 const char *options
,
1586 MountProcFlags
*ret_flags
) {
1593 if (!MOUNT(u
)->where
) {
1594 MOUNT(u
)->where
= strdup(where
);
1595 if (!MOUNT(u
)->where
)
1599 /* In case we have multiple mounts established on the same mount point, let's merge flags set already
1600 * for the current unit. Note that the flags field is reset on each iteration of reading
1601 * /proc/self/mountinfo, hence we know for sure anything already set here is from the current
1602 * iteration and thus worthy of taking into account. */
1603 MountProcFlags flags
=
1604 MOUNT(u
)->proc_flags
| MOUNT_PROC_IS_MOUNTED
;
1606 r
= update_parameters_proc_self_mountinfo(MOUNT(u
), what
, options
, fstype
);
1610 flags
|= MOUNT_PROC_JUST_CHANGED
;
1612 /* There are two conditions when we consider a mount point just mounted: when we haven't seen it in
1613 * /proc/self/mountinfo before or when MOUNT_MOUNTING is our current state. Why bother with the
1614 * latter? Shouldn't that be covered by the former? No, during reload it is not because we might then
1615 * encounter a new /proc/self/mountinfo in combination with an old mount unit state (since it stems
1616 * from the serialized state), and need to catch up. Since we know that the MOUNT_MOUNTING state is
1617 * reached when we wait for the mount to appear we hence can assume that if we are in it, we are
1618 * actually seeing it established for the first time. */
1619 if (!MOUNT(u
)->from_proc_self_mountinfo
|| MOUNT(u
)->state
== MOUNT_MOUNTING
)
1620 flags
|= MOUNT_PROC_JUST_MOUNTED
;
1622 MOUNT(u
)->from_proc_self_mountinfo
= true;
1624 if (IN_SET(u
->load_state
, UNIT_NOT_FOUND
, UNIT_BAD_SETTING
, UNIT_ERROR
)) {
1625 /* The unit was previously not found or otherwise not loaded. Now that the unit shows up in
1626 * /proc/self/mountinfo we should reconsider it this, hence set it to UNIT_LOADED. */
1627 u
->load_state
= UNIT_LOADED
;
1630 flags
|= MOUNT_PROC_JUST_CHANGED
;
1633 if (FLAGS_SET(flags
, MOUNT_PROC_JUST_CHANGED
)) {
1634 /* If things changed, then make sure that all deps are regenerated. Let's
1635 * first remove all automatic deps, and then add in the new ones. */
1637 unit_remove_dependencies(u
, UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
);
1639 r
= mount_add_non_exec_dependencies(MOUNT(u
));
1648 static int mount_setup_unit(
1652 const char *options
,
1656 _cleanup_free_
char *e
= NULL
;
1657 MountProcFlags flags
;
1667 /* Ignore API mount points. They should never be referenced in
1668 * dependencies ever. */
1669 if (mount_point_is_api(where
) || mount_point_ignore(where
))
1672 if (streq(fstype
, "autofs"))
1675 /* probably some kind of swap, ignore */
1676 if (!is_path(where
))
1679 /* Mount unit names have to be (like all other unit names) short enough to fit into file names. This
1680 * means there's a good chance that overly long mount point paths after mangling them to look like a
1681 * unit name would result in unit names we don't actually consider valid. This should be OK however
1682 * as such long mount point paths should not happen on regular systems — and if they appear
1683 * nonetheless they are generally synthesized by software, and thus managed by that other
1684 * software. Having such long names just means you cannot use systemd to manage those specific mount
1685 * points, which should be an OK restriction to make. After all we don't have to be able to manage
1686 * all mount points in the world — as long as we don't choke on them when we encounter them. */
1687 r
= unit_name_from_path(where
, ".mount", &e
);
1689 static RateLimit rate_limit
= { /* Let's log about this at warning level at most once every
1690 * 5s. Given that we generate this whenever we read the file
1691 * otherwise we probably shouldn't flood the logs with
1693 .interval
= 5 * USEC_PER_SEC
,
1697 return log_struct_errno(
1698 ratelimit_below(&rate_limit
) ? LOG_WARNING
: LOG_DEBUG
, r
,
1699 "MESSAGE_ID=" SD_MESSAGE_MOUNT_POINT_PATH_NOT_SUITABLE_STR
,
1700 "MOUNT_POINT=%s", where
,
1701 LOG_MESSAGE("Failed to generate valid unit name from path '%s', ignoring mount point: %m", where
));
1704 u
= manager_get_unit(m
, e
);
1706 r
= mount_setup_existing_unit(u
, what
, where
, options
, fstype
, &flags
);
1708 /* First time we see this mount point meaning that it's not been initiated by a mount unit but rather
1709 * by the sysadmin having called mount(8) directly. */
1710 r
= mount_setup_new_unit(m
, e
, what
, where
, options
, fstype
, &flags
, &u
);
1712 return log_warning_errno(r
, "Failed to set up mount unit for '%s': %m", where
);
1714 /* If the mount changed properties or state, let's notify our clients */
1715 if (flags
& (MOUNT_PROC_JUST_CHANGED
|MOUNT_PROC_JUST_MOUNTED
))
1716 unit_add_to_dbus_queue(u
);
1719 MOUNT(u
)->proc_flags
= flags
;
1724 static int mount_load_proc_self_mountinfo(Manager
*m
, bool set_flags
) {
1725 _cleanup_(mnt_free_tablep
) struct libmnt_table
*table
= NULL
;
1726 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*iter
= NULL
;
1731 r
= libmount_parse(NULL
, NULL
, &table
, &iter
);
1733 return log_error_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
1736 struct libmnt_fs
*fs
;
1737 const char *device
, *path
, *options
, *fstype
;
1739 r
= mnt_table_next_fs(table
, iter
, &fs
);
1743 return log_error_errno(r
, "Failed to get next entry from /proc/self/mountinfo: %m");
1745 device
= mnt_fs_get_source(fs
);
1746 path
= mnt_fs_get_target(fs
);
1747 options
= mnt_fs_get_options(fs
);
1748 fstype
= mnt_fs_get_fstype(fs
);
1750 if (!device
|| !path
)
1753 device_found_node(m
, device
, DEVICE_FOUND_MOUNT
, DEVICE_FOUND_MOUNT
);
1755 (void) mount_setup_unit(m
, device
, path
, options
, fstype
, set_flags
);
1761 static void mount_shutdown(Manager
*m
) {
1764 m
->mount_event_source
= sd_event_source_unref(m
->mount_event_source
);
1766 mnt_unref_monitor(m
->mount_monitor
);
1767 m
->mount_monitor
= NULL
;
1770 static int mount_get_timeout(Unit
*u
, usec_t
*timeout
) {
1771 Mount
*m
= MOUNT(u
);
1775 if (!m
->timer_event_source
)
1778 r
= sd_event_source_get_time(m
->timer_event_source
, &t
);
1781 if (t
== USEC_INFINITY
)
1788 static void mount_enumerate_perpetual(Manager
*m
) {
1794 /* Whatever happens, we know for sure that the root directory is around, and cannot go away. Let's
1795 * unconditionally synthesize it here and mark it as perpetual. */
1797 u
= manager_get_unit(m
, SPECIAL_ROOT_MOUNT
);
1799 r
= unit_new_for_name(m
, sizeof(Mount
), SPECIAL_ROOT_MOUNT
, &u
);
1801 log_error_errno(r
, "Failed to allocate the special " SPECIAL_ROOT_MOUNT
" unit: %m");
1806 u
->perpetual
= true;
1807 MOUNT(u
)->deserialized_state
= MOUNT_MOUNTED
;
1809 unit_add_to_load_queue(u
);
1810 unit_add_to_dbus_queue(u
);
1813 static bool mount_is_mounted(Mount
*m
) {
1816 return UNIT(m
)->perpetual
|| FLAGS_SET(m
->proc_flags
, MOUNT_PROC_IS_MOUNTED
);
1819 static void mount_enumerate(Manager
*m
) {
1826 if (!m
->mount_monitor
) {
1829 m
->mount_monitor
= mnt_new_monitor();
1830 if (!m
->mount_monitor
) {
1835 r
= mnt_monitor_enable_kernel(m
->mount_monitor
, 1);
1837 log_error_errno(r
, "Failed to enable watching of kernel mount events: %m");
1841 r
= mnt_monitor_enable_userspace(m
->mount_monitor
, 1, NULL
);
1843 log_error_errno(r
, "Failed to enable watching of userspace mount events: %m");
1847 /* mnt_unref_monitor() will close the fd */
1848 fd
= r
= mnt_monitor_get_fd(m
->mount_monitor
);
1850 log_error_errno(r
, "Failed to acquire watch file descriptor: %m");
1854 r
= sd_event_add_io(m
->event
, &m
->mount_event_source
, fd
, EPOLLIN
, mount_dispatch_io
, m
);
1856 log_error_errno(r
, "Failed to watch mount file descriptor: %m");
1860 r
= sd_event_source_set_priority(m
->mount_event_source
, SD_EVENT_PRIORITY_NORMAL
-10);
1862 log_error_errno(r
, "Failed to adjust mount watch priority: %m");
1866 (void) sd_event_source_set_description(m
->mount_event_source
, "mount-monitor-dispatch");
1869 r
= mount_load_proc_self_mountinfo(m
, false);
1879 static int drain_libmount(Manager
*m
) {
1880 bool rescan
= false;
1885 /* Drain all events and verify that the event is valid.
1887 * Note that libmount also monitors /run/mount mkdir if the directory does not exist yet. The mkdir
1888 * may generate event which is irrelevant for us.
1890 * error: r < 0; valid: r == 0, false positive: r == 1 */
1892 r
= mnt_monitor_next_change(m
->mount_monitor
, NULL
, NULL
);
1894 return log_error_errno(r
, "Failed to drain libmount events: %m");
1902 static int mount_process_proc_self_mountinfo(Manager
*m
) {
1903 _cleanup_set_free_free_ Set
*around
= NULL
, *gone
= NULL
;
1910 r
= drain_libmount(m
);
1914 r
= mount_load_proc_self_mountinfo(m
, true);
1916 /* Reset flags, just in case, for later calls */
1917 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
])
1918 MOUNT(u
)->proc_flags
= 0;
1923 manager_dispatch_load_queue(m
);
1925 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
]) {
1926 Mount
*mount
= MOUNT(u
);
1928 if (!mount_is_mounted(mount
)) {
1930 /* A mount point is not around right now. It
1931 * might be gone, or might never have
1934 if (mount
->from_proc_self_mountinfo
&&
1935 mount
->parameters_proc_self_mountinfo
.what
) {
1937 /* Remember that this device might just have disappeared */
1938 if (set_ensure_allocated(&gone
, &path_hash_ops
) < 0 ||
1939 set_put_strdup(&gone
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
1940 log_oom(); /* we don't care too much about OOM here... */
1943 mount
->from_proc_self_mountinfo
= false;
1944 assert_se(update_parameters_proc_self_mountinfo(mount
, NULL
, NULL
, NULL
) >= 0);
1946 switch (mount
->state
) {
1949 /* This has just been unmounted by somebody else, follow the state change. */
1950 mount_enter_dead(mount
, MOUNT_SUCCESS
);
1957 } else if (mount
->proc_flags
& (MOUNT_PROC_JUST_MOUNTED
|MOUNT_PROC_JUST_CHANGED
)) {
1959 /* A mount point was added or changed */
1961 switch (mount
->state
) {
1966 /* This has just been mounted by somebody else, follow the state change, but let's
1967 * generate a new invocation ID for this implicitly and automatically. */
1968 (void) unit_acquire_invocation_id(u
);
1969 mount_cycle_clear(mount
);
1970 mount_enter_mounted(mount
, MOUNT_SUCCESS
);
1973 case MOUNT_MOUNTING
:
1974 mount_set_state(mount
, MOUNT_MOUNTING_DONE
);
1978 /* Nothing really changed, but let's
1979 * issue an notification call
1980 * nonetheless, in case somebody is
1981 * waiting for this. (e.g. file system
1982 * ro/rw remounts.) */
1983 mount_set_state(mount
, mount
->state
);
1988 if (mount_is_mounted(mount
) &&
1989 mount
->from_proc_self_mountinfo
&&
1990 mount
->parameters_proc_self_mountinfo
.what
) {
1991 /* Track devices currently used */
1993 if (set_ensure_allocated(&around
, &path_hash_ops
) < 0 ||
1994 set_put_strdup(&around
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
1998 /* Reset the flags for later calls */
1999 mount
->proc_flags
= 0;
2002 SET_FOREACH(what
, gone
) {
2003 if (set_contains(around
, what
))
2006 /* Let the device units know that the device is no longer mounted */
2007 device_found_node(m
, what
, 0, DEVICE_FOUND_MOUNT
);
2013 static int mount_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
2014 Manager
*m
= userdata
;
2017 assert(revents
& EPOLLIN
);
2019 return mount_process_proc_self_mountinfo(m
);
2022 static void mount_reset_failed(Unit
*u
) {
2023 Mount
*m
= MOUNT(u
);
2027 if (m
->state
== MOUNT_FAILED
)
2028 mount_set_state(m
, MOUNT_DEAD
);
2030 m
->result
= MOUNT_SUCCESS
;
2031 m
->reload_result
= MOUNT_SUCCESS
;
2032 m
->clean_result
= MOUNT_SUCCESS
;
2035 static int mount_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
2036 Mount
*m
= MOUNT(u
);
2040 return unit_kill_common(u
, who
, signo
, -1, m
->control_pid
, error
);
2043 static int mount_control_pid(Unit
*u
) {
2044 Mount
*m
= MOUNT(u
);
2048 return m
->control_pid
;
2051 static int mount_clean(Unit
*u
, ExecCleanMask mask
) {
2052 _cleanup_strv_free_
char **l
= NULL
;
2053 Mount
*m
= MOUNT(u
);
2059 if (m
->state
!= MOUNT_DEAD
)
2062 r
= exec_context_get_clean_directories(&m
->exec_context
, u
->manager
->prefix
, mask
, &l
);
2066 if (strv_isempty(l
))
2069 mount_unwatch_control_pid(m
);
2070 m
->clean_result
= MOUNT_SUCCESS
;
2071 m
->control_command
= NULL
;
2072 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
2074 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->exec_context
.timeout_clean_usec
));
2078 r
= unit_fork_and_watch_rm_rf(u
, l
, &m
->control_pid
);
2082 mount_set_state(m
, MOUNT_CLEANING
);
2087 log_unit_warning_errno(u
, r
, "Failed to initiate cleaning: %m");
2088 m
->clean_result
= MOUNT_FAILURE_RESOURCES
;
2089 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
2093 static int mount_can_clean(Unit
*u
, ExecCleanMask
*ret
) {
2094 Mount
*m
= MOUNT(u
);
2098 return exec_context_get_clean_mask(&m
->exec_context
, ret
);
2101 static const char* const mount_exec_command_table
[_MOUNT_EXEC_COMMAND_MAX
] = {
2102 [MOUNT_EXEC_MOUNT
] = "ExecMount",
2103 [MOUNT_EXEC_UNMOUNT
] = "ExecUnmount",
2104 [MOUNT_EXEC_REMOUNT
] = "ExecRemount",
2107 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command
, MountExecCommand
);
2109 static const char* const mount_result_table
[_MOUNT_RESULT_MAX
] = {
2110 [MOUNT_SUCCESS
] = "success",
2111 [MOUNT_FAILURE_RESOURCES
] = "resources",
2112 [MOUNT_FAILURE_TIMEOUT
] = "timeout",
2113 [MOUNT_FAILURE_EXIT_CODE
] = "exit-code",
2114 [MOUNT_FAILURE_SIGNAL
] = "signal",
2115 [MOUNT_FAILURE_CORE_DUMP
] = "core-dump",
2116 [MOUNT_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
2117 [MOUNT_FAILURE_PROTOCOL
] = "protocol",
2120 DEFINE_STRING_TABLE_LOOKUP(mount_result
, MountResult
);
2122 const UnitVTable mount_vtable
= {
2123 .object_size
= sizeof(Mount
),
2124 .exec_context_offset
= offsetof(Mount
, exec_context
),
2125 .cgroup_context_offset
= offsetof(Mount
, cgroup_context
),
2126 .kill_context_offset
= offsetof(Mount
, kill_context
),
2127 .exec_runtime_offset
= offsetof(Mount
, exec_runtime
),
2128 .dynamic_creds_offset
= offsetof(Mount
, dynamic_creds
),
2134 .private_section
= "Mount",
2136 .can_transient
= true,
2143 .coldplug
= mount_coldplug
,
2147 .start
= mount_start
,
2149 .reload
= mount_reload
,
2152 .clean
= mount_clean
,
2153 .can_clean
= mount_can_clean
,
2155 .serialize
= mount_serialize
,
2156 .deserialize_item
= mount_deserialize_item
,
2158 .active_state
= mount_active_state
,
2159 .sub_state_to_string
= mount_sub_state_to_string
,
2161 .will_restart
= unit_will_restart_default
,
2163 .may_gc
= mount_may_gc
,
2165 .sigchld_event
= mount_sigchld_event
,
2167 .reset_failed
= mount_reset_failed
,
2169 .control_pid
= mount_control_pid
,
2171 .bus_set_property
= bus_mount_set_property
,
2172 .bus_commit_properties
= bus_mount_commit_properties
,
2174 .get_timeout
= mount_get_timeout
,
2176 .enumerate_perpetual
= mount_enumerate_perpetual
,
2177 .enumerate
= mount_enumerate
,
2178 .shutdown
= mount_shutdown
,
2180 .status_message_formats
= {
2181 .starting_stopping
= {
2182 [0] = "Mounting %s...",
2183 [1] = "Unmounting %s...",
2185 .finished_start_job
= {
2186 [JOB_DONE
] = "Mounted %s.",
2187 [JOB_FAILED
] = "Failed to mount %s.",
2188 [JOB_TIMEOUT
] = "Timed out mounting %s.",
2190 .finished_stop_job
= {
2191 [JOB_DONE
] = "Unmounted %s.",
2192 [JOB_FAILED
] = "Failed unmounting %s.",
2193 [JOB_TIMEOUT
] = "Timed out unmounting %s.",