1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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(Unit
*u
) {
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_USER(u
->manager
))
429 p
= get_mount_parameters(m
);
430 if (p
&& fstab_is_extrinsic(m
->where
, p
->options
))
436 static int mount_add_default_ordering_dependencies(
439 UnitDependencyMask mask
) {
441 const char *after
, *before
, *e
;
446 e
= path_startswith(m
->where
, "/sysroot");
447 if (e
&& in_initrd()) {
448 /* All mounts under /sysroot need to happen later, at initrd-fs.target time. IOW,
449 * it's not technically part of the basic initrd filesystem itself, and so
450 * shouldn't inherit the default Before=local-fs.target dependency. */
453 before
= isempty(e
) ? SPECIAL_INITRD_ROOT_FS_TARGET
: SPECIAL_INITRD_FS_TARGET
;
455 } else if (mount_is_network(p
)) {
456 after
= SPECIAL_REMOTE_FS_PRE_TARGET
;
457 before
= SPECIAL_REMOTE_FS_TARGET
;
460 after
= SPECIAL_LOCAL_FS_PRE_TARGET
;
461 before
= SPECIAL_LOCAL_FS_TARGET
;
464 if (!mount_is_nofail(m
) && !mount_is_automount(p
)) {
465 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_BEFORE
, before
, true, mask
);
471 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, after
, true, mask
);
476 return unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_CONFLICTS
,
477 SPECIAL_UMOUNT_TARGET
, true, mask
);
480 static int mount_add_default_dependencies(Mount
*m
) {
481 UnitDependencyMask mask
;
487 if (!UNIT(m
)->default_dependencies
)
490 /* We do not add any default dependencies to /, /usr or /run/initramfs/, since they are
491 * guaranteed to stay mounted the whole time, since our system is on it. Also, don't
492 * bother with anything mounted below virtual file systems, it's also going to be virtual,
493 * and hence not worth the effort. */
494 if (mount_is_extrinsic(UNIT(m
)))
497 p
= get_mount_parameters(m
);
501 mask
= m
->from_fragment
? UNIT_DEPENDENCY_FILE
: UNIT_DEPENDENCY_MOUNTINFO_DEFAULT
;
503 r
= mount_add_default_ordering_dependencies(m
, p
, mask
);
507 if (mount_is_network(p
)) {
508 /* We order ourselves after network.target. This is primarily useful at shutdown:
509 * services that take down the network should order themselves before
510 * network.target, so that they are shut down only after this mount unit is
513 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, SPECIAL_NETWORK_TARGET
, true, mask
);
517 /* We pull in network-online.target, and order ourselves after it. This is useful
518 * at start-up to actively pull in tools that want to be started before we start
519 * mounting network file systems, and whose purpose it is to delay this until the
520 * network is "up". */
522 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_WANTS
, UNIT_AFTER
, SPECIAL_NETWORK_ONLINE_TARGET
, true, mask
);
527 /* If this is a tmpfs mount then we have to unmount it before we try to deactivate swaps */
528 if (streq_ptr(p
->fstype
, "tmpfs")) {
529 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, SPECIAL_SWAP_TARGET
, true, mask
);
537 static int mount_verify(Mount
*m
) {
538 _cleanup_free_
char *e
= NULL
;
543 assert(UNIT(m
)->load_state
== UNIT_LOADED
);
545 if (!m
->from_fragment
&& !m
->from_proc_self_mountinfo
&& !UNIT(m
)->perpetual
)
548 r
= unit_name_from_path(m
->where
, ".mount", &e
);
550 return log_unit_error_errno(UNIT(m
), r
, "Failed to generate unit name from mount path: %m");
552 if (!unit_has_name(UNIT(m
), e
))
553 return log_unit_error_errno(UNIT(m
), SYNTHETIC_ERRNO(ENOEXEC
), "Where= setting doesn't match unit name. Refusing.");
555 if (mount_point_is_api(m
->where
) || mount_point_ignore(m
->where
))
556 return log_unit_error_errno(UNIT(m
), SYNTHETIC_ERRNO(ENOEXEC
), "Cannot create mount unit for API file system %s. Refusing.", m
->where
);
558 p
= get_mount_parameters_fragment(m
);
559 if (p
&& !p
->what
&& !UNIT(m
)->perpetual
)
560 return log_unit_error_errno(UNIT(m
), SYNTHETIC_ERRNO(ENOEXEC
),
561 "What= setting is missing. Refusing.");
563 if (m
->exec_context
.pam_name
&& m
->kill_context
.kill_mode
!= KILL_CONTROL_GROUP
)
564 return log_unit_error_errno(UNIT(m
), SYNTHETIC_ERRNO(ENOEXEC
), "Unit has PAM enabled. Kill mode must be set to control-group'. Refusing.");
569 static int mount_add_non_exec_dependencies(Mount
*m
) {
573 /* Adds in all dependencies directly responsible for ordering the mount, as opposed to dependencies
574 * resulting from the ExecContext and such. */
576 r
= mount_add_device_dependencies(m
);
580 r
= mount_add_mount_dependencies(m
);
584 r
= mount_add_quota_dependencies(m
);
588 r
= mount_add_default_dependencies(m
);
595 static int mount_add_extras(Mount
*m
) {
601 /* Note: this call might be called after we already have been loaded once (and even when it has already been
602 * activated), in case data from /proc/self/mountinfo has changed. This means all code here needs to be ready
603 * to run with an already set up unit. */
605 if (u
->fragment_path
)
606 m
->from_fragment
= true;
609 r
= unit_name_to_path(u
->id
, &m
->where
);
614 path_simplify(m
->where
, false);
616 if (!u
->description
) {
617 r
= unit_set_description(u
, m
->where
);
622 r
= unit_patch_contexts(u
);
626 r
= unit_add_exec_dependencies(u
, &m
->exec_context
);
630 r
= unit_set_default_slice(u
);
634 r
= mount_add_non_exec_dependencies(m
);
641 static void mount_load_root_mount(Unit
*u
) {
644 if (!unit_has_name(u
, SPECIAL_ROOT_MOUNT
))
648 u
->default_dependencies
= false;
650 /* The stdio/kmsg bridge socket is on /, in order to avoid a dep loop, don't use kmsg logging for -.mount */
651 MOUNT(u
)->exec_context
.std_output
= EXEC_OUTPUT_NULL
;
652 MOUNT(u
)->exec_context
.std_input
= EXEC_INPUT_NULL
;
655 u
->description
= strdup("Root Mount");
658 static int mount_load(Unit
*u
) {
663 assert(u
->load_state
== UNIT_STUB
);
665 mount_load_root_mount(u
);
667 bool fragment_optional
= m
->from_proc_self_mountinfo
|| u
->perpetual
;
668 r
= unit_load_fragment_and_dropin(u
, !fragment_optional
);
670 /* Add in some extras. Note we do this in all cases (even if we failed to load the unit) when announced by the
671 * kernel, because we need some things to be set up no matter what when the kernel establishes a mount and thus
672 * we need to update the state in our unit to track it. After all, consider that we don't allow changing the
673 * 'slice' field for a unit once it is active. */
674 if (u
->load_state
== UNIT_LOADED
|| m
->from_proc_self_mountinfo
|| u
->perpetual
)
675 q
= mount_add_extras(m
);
681 if (u
->load_state
!= UNIT_LOADED
)
684 return mount_verify(m
);
687 static void mount_set_state(Mount
*m
, MountState state
) {
688 MountState old_state
;
691 if (m
->state
!= state
)
692 bus_unit_send_pending_change_signal(UNIT(m
), false);
694 old_state
= m
->state
;
697 if (!MOUNT_STATE_WITH_PROCESS(state
)) {
698 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
699 mount_unwatch_control_pid(m
);
700 m
->control_command
= NULL
;
701 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
704 if (state
!= old_state
)
705 log_unit_debug(UNIT(m
), "Changed %s -> %s", mount_state_to_string(old_state
), mount_state_to_string(state
));
707 unit_notify(UNIT(m
), state_translation_table
[old_state
], state_translation_table
[state
],
708 m
->reload_result
== MOUNT_SUCCESS
? 0 : UNIT_NOTIFY_RELOAD_FAILURE
);
711 static int mount_coldplug(Unit
*u
) {
713 MountState new_state
= MOUNT_DEAD
;
717 assert(m
->state
== MOUNT_DEAD
);
719 if (m
->deserialized_state
!= m
->state
)
720 new_state
= m
->deserialized_state
;
721 else if (m
->from_proc_self_mountinfo
)
722 new_state
= MOUNT_MOUNTED
;
724 if (new_state
== m
->state
)
727 if (m
->control_pid
> 0 &&
728 pid_is_unwaited(m
->control_pid
) &&
729 MOUNT_STATE_WITH_PROCESS(new_state
)) {
731 r
= unit_watch_pid(UNIT(m
), m
->control_pid
, false);
735 r
= mount_arm_timer(m
, usec_add(u
->state_change_timestamp
.monotonic
, m
->timeout_usec
));
740 if (!IN_SET(new_state
, MOUNT_DEAD
, MOUNT_FAILED
)) {
741 (void) unit_setup_dynamic_creds(u
);
742 (void) unit_setup_exec_runtime(u
);
745 mount_set_state(m
, new_state
);
749 static void mount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
750 char buf
[FORMAT_TIMESPAN_MAX
];
757 p
= get_mount_parameters(m
);
760 "%sMount State: %s\n"
762 "%sClean Result: %s\n"
765 "%sFile System Type: %s\n"
767 "%sFrom /proc/self/mountinfo: %s\n"
768 "%sFrom fragment: %s\n"
770 "%sDirectoryMode: %04o\n"
771 "%sSloppyOptions: %s\n"
772 "%sLazyUnmount: %s\n"
773 "%sForceUnmount: %s\n"
774 "%sReadWriteOnly: %s\n"
775 "%sTimeoutSec: %s\n",
776 prefix
, mount_state_to_string(m
->state
),
777 prefix
, mount_result_to_string(m
->result
),
778 prefix
, mount_result_to_string(m
->clean_result
),
780 prefix
, p
? strna(p
->what
) : "n/a",
781 prefix
, p
? strna(p
->fstype
) : "n/a",
782 prefix
, p
? strna(p
->options
) : "n/a",
783 prefix
, yes_no(m
->from_proc_self_mountinfo
),
784 prefix
, yes_no(m
->from_fragment
),
785 prefix
, yes_no(mount_is_extrinsic(u
)),
786 prefix
, m
->directory_mode
,
787 prefix
, yes_no(m
->sloppy_options
),
788 prefix
, yes_no(m
->lazy_unmount
),
789 prefix
, yes_no(m
->force_unmount
),
790 prefix
, yes_no(m
->read_write_only
),
791 prefix
, format_timespan(buf
, sizeof(buf
), m
->timeout_usec
, USEC_PER_SEC
));
793 if (m
->control_pid
> 0)
795 "%sControl PID: "PID_FMT
"\n",
796 prefix
, m
->control_pid
);
798 exec_context_dump(&m
->exec_context
, f
, prefix
);
799 kill_context_dump(&m
->kill_context
, f
, prefix
);
800 cgroup_context_dump(UNIT(m
), f
, prefix
);
803 static int mount_spawn(Mount
*m
, ExecCommand
*c
, pid_t
*_pid
) {
805 _cleanup_(exec_params_clear
) ExecParameters exec_params
= {
806 .flags
= EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
,
819 r
= unit_prepare_exec(UNIT(m
));
823 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->timeout_usec
));
827 r
= unit_set_exec_params(UNIT(m
), &exec_params
);
831 r
= exec_spawn(UNIT(m
),
841 r
= unit_watch_pid(UNIT(m
), pid
, true);
850 static void mount_enter_dead(Mount
*m
, MountResult f
) {
853 if (m
->result
== MOUNT_SUCCESS
)
856 unit_log_result(UNIT(m
), m
->result
== MOUNT_SUCCESS
, mount_result_to_string(m
->result
));
857 unit_warn_leftover_processes(UNIT(m
), unit_log_leftover_process_stop
);
859 mount_set_state(m
, m
->result
!= MOUNT_SUCCESS
? MOUNT_FAILED
: MOUNT_DEAD
);
861 m
->exec_runtime
= exec_runtime_unref(m
->exec_runtime
, true);
863 unit_destroy_runtime_data(UNIT(m
), &m
->exec_context
);
865 unit_unref_uid_gid(UNIT(m
), true);
867 dynamic_creds_destroy(&m
->dynamic_creds
);
869 /* Any dependencies based on /proc/self/mountinfo are now stale */
870 unit_remove_dependencies(UNIT(m
), UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
);
873 static void mount_enter_mounted(Mount
*m
, MountResult f
) {
876 if (m
->result
== MOUNT_SUCCESS
)
879 mount_set_state(m
, MOUNT_MOUNTED
);
882 static void mount_enter_dead_or_mounted(Mount
*m
, MountResult f
) {
885 /* Enter DEAD or MOUNTED state, depending on what the kernel currently says about the mount point. We use this
886 * whenever we executed an operation, so that our internal state reflects what the kernel says again, after all
887 * ultimately we just mirror the kernel's internal state on this. */
889 if (m
->from_proc_self_mountinfo
)
890 mount_enter_mounted(m
, f
);
892 mount_enter_dead(m
, f
);
895 static int state_to_kill_operation(MountState state
) {
898 case MOUNT_REMOUNTING_SIGTERM
:
901 case MOUNT_UNMOUNTING_SIGTERM
:
902 return KILL_TERMINATE
;
904 case MOUNT_REMOUNTING_SIGKILL
:
905 case MOUNT_UNMOUNTING_SIGKILL
:
909 return _KILL_OPERATION_INVALID
;
913 static void mount_enter_signal(Mount
*m
, MountState state
, MountResult f
) {
918 if (m
->result
== MOUNT_SUCCESS
)
921 r
= unit_kill_context(
924 state_to_kill_operation(state
),
932 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->timeout_usec
));
936 mount_set_state(m
, state
);
937 } else if (state
== MOUNT_REMOUNTING_SIGTERM
&& m
->kill_context
.send_sigkill
)
938 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
939 else if (IN_SET(state
, MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
))
940 mount_enter_mounted(m
, MOUNT_SUCCESS
);
941 else if (state
== MOUNT_UNMOUNTING_SIGTERM
&& m
->kill_context
.send_sigkill
)
942 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
944 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
949 log_unit_warning_errno(UNIT(m
), r
, "Failed to kill processes: %m");
950 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
953 static void mount_enter_unmounting(Mount
*m
) {
958 /* Start counting our attempts */
959 if (!IN_SET(m
->state
,
961 MOUNT_UNMOUNTING_SIGTERM
,
962 MOUNT_UNMOUNTING_SIGKILL
))
963 m
->n_retry_umount
= 0;
965 m
->control_command_id
= MOUNT_EXEC_UNMOUNT
;
966 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_UNMOUNT
;
968 r
= exec_command_set(m
->control_command
, UMOUNT_PATH
, m
->where
, "-c", NULL
);
969 if (r
>= 0 && m
->lazy_unmount
)
970 r
= exec_command_append(m
->control_command
, "-l", NULL
);
971 if (r
>= 0 && m
->force_unmount
)
972 r
= exec_command_append(m
->control_command
, "-f", NULL
);
976 mount_unwatch_control_pid(m
);
978 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
982 mount_set_state(m
, MOUNT_UNMOUNTING
);
987 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'umount' task: %m");
988 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
991 static void mount_enter_mounting(Mount
*m
) {
997 r
= unit_fail_if_noncanonical(UNIT(m
), m
->where
);
1001 (void) mkdir_p_label(m
->where
, m
->directory_mode
);
1003 unit_warn_if_dir_nonempty(UNIT(m
), m
->where
);
1004 unit_warn_leftover_processes(UNIT(m
), unit_log_leftover_process_start
);
1006 m
->control_command_id
= MOUNT_EXEC_MOUNT
;
1007 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_MOUNT
;
1009 /* Create the source directory for bind-mounts if needed */
1010 p
= get_mount_parameters_fragment(m
);
1011 if (p
&& mount_is_bind(p
)) {
1012 r
= mkdir_p_label(p
->what
, m
->directory_mode
);
1014 log_unit_error_errno(UNIT(m
), r
, "Failed to make bind mount source '%s': %m", p
->what
);
1018 _cleanup_free_
char *opts
= NULL
;
1020 r
= fstab_filter_options(p
->options
, "nofail\0" "noauto\0" "auto\0", NULL
, NULL
, &opts
);
1024 r
= exec_command_set(m
->control_command
, MOUNT_PATH
, p
->what
, m
->where
, NULL
);
1025 if (r
>= 0 && m
->sloppy_options
)
1026 r
= exec_command_append(m
->control_command
, "-s", NULL
);
1027 if (r
>= 0 && m
->read_write_only
)
1028 r
= exec_command_append(m
->control_command
, "-w", NULL
);
1029 if (r
>= 0 && p
->fstype
)
1030 r
= exec_command_append(m
->control_command
, "-t", p
->fstype
, NULL
);
1031 if (r
>= 0 && !isempty(opts
))
1032 r
= exec_command_append(m
->control_command
, "-o", opts
, NULL
);
1038 mount_unwatch_control_pid(m
);
1040 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
1044 mount_set_state(m
, MOUNT_MOUNTING
);
1049 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'mount' task: %m");
1050 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
1053 static void mount_set_reload_result(Mount
*m
, MountResult result
) {
1056 /* Only store the first error we encounter */
1057 if (m
->reload_result
!= MOUNT_SUCCESS
)
1060 m
->reload_result
= result
;
1063 static void mount_enter_remounting(Mount
*m
) {
1069 /* Reset reload result when we are about to start a new remount operation */
1070 m
->reload_result
= MOUNT_SUCCESS
;
1072 m
->control_command_id
= MOUNT_EXEC_REMOUNT
;
1073 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_REMOUNT
;
1075 p
= get_mount_parameters_fragment(m
);
1080 o
= strjoina("remount,", p
->options
);
1084 r
= exec_command_set(m
->control_command
, MOUNT_PATH
,
1087 if (r
>= 0 && m
->sloppy_options
)
1088 r
= exec_command_append(m
->control_command
, "-s", NULL
);
1089 if (r
>= 0 && m
->read_write_only
)
1090 r
= exec_command_append(m
->control_command
, "-w", NULL
);
1091 if (r
>= 0 && p
->fstype
)
1092 r
= exec_command_append(m
->control_command
, "-t", p
->fstype
, NULL
);
1098 mount_unwatch_control_pid(m
);
1100 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
1104 mount_set_state(m
, MOUNT_REMOUNTING
);
1109 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'remount' task: %m");
1110 mount_set_reload_result(m
, MOUNT_FAILURE_RESOURCES
);
1111 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1114 static void mount_cycle_clear(Mount
*m
) {
1117 /* Clear all state we shall forget for this new cycle */
1119 m
->result
= MOUNT_SUCCESS
;
1120 m
->reload_result
= MOUNT_SUCCESS
;
1121 exec_command_reset_status_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
1122 UNIT(m
)->reset_accounting
= true;
1125 static int mount_start(Unit
*u
) {
1126 Mount
*m
= MOUNT(u
);
1131 /* We cannot fulfill this request right now, try again later
1133 if (IN_SET(m
->state
,
1135 MOUNT_UNMOUNTING_SIGTERM
,
1136 MOUNT_UNMOUNTING_SIGKILL
,
1140 /* Already on it! */
1141 if (IN_SET(m
->state
, MOUNT_MOUNTING
, MOUNT_MOUNTING_DONE
))
1144 assert(IN_SET(m
->state
, MOUNT_DEAD
, MOUNT_FAILED
));
1146 r
= unit_test_start_limit(u
);
1148 mount_enter_dead(m
, MOUNT_FAILURE_START_LIMIT_HIT
);
1152 r
= unit_acquire_invocation_id(u
);
1156 mount_cycle_clear(m
);
1157 mount_enter_mounting(m
);
1162 static int mount_stop(Unit
*u
) {
1163 Mount
*m
= MOUNT(u
);
1169 case MOUNT_UNMOUNTING
:
1170 case MOUNT_UNMOUNTING_SIGKILL
:
1171 case MOUNT_UNMOUNTING_SIGTERM
:
1175 case MOUNT_MOUNTING
:
1176 case MOUNT_MOUNTING_DONE
:
1177 case MOUNT_REMOUNTING
:
1178 /* If we are still waiting for /bin/mount, we go directly into kill mode. */
1179 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_SUCCESS
);
1182 case MOUNT_REMOUNTING_SIGTERM
:
1183 /* If we are already waiting for a hung remount, convert this to the matching unmounting state */
1184 mount_set_state(m
, MOUNT_UNMOUNTING_SIGTERM
);
1187 case MOUNT_REMOUNTING_SIGKILL
:
1189 mount_set_state(m
, MOUNT_UNMOUNTING_SIGKILL
);
1193 mount_enter_unmounting(m
);
1196 case MOUNT_CLEANING
:
1197 /* If we are currently cleaning, then abort it, brutally. */
1198 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
1202 assert_not_reached("Unexpected state.");
1206 static int mount_reload(Unit
*u
) {
1207 Mount
*m
= MOUNT(u
);
1210 assert(m
->state
== MOUNT_MOUNTED
);
1212 mount_enter_remounting(m
);
1217 static int mount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1218 Mount
*m
= MOUNT(u
);
1224 (void) serialize_item(f
, "state", mount_state_to_string(m
->state
));
1225 (void) serialize_item(f
, "result", mount_result_to_string(m
->result
));
1226 (void) serialize_item(f
, "reload-result", mount_result_to_string(m
->reload_result
));
1227 (void) serialize_item_format(f
, "n-retry-umount", "%u", m
->n_retry_umount
);
1229 if (m
->control_pid
> 0)
1230 (void) serialize_item_format(f
, "control-pid", PID_FMT
, m
->control_pid
);
1232 if (m
->control_command_id
>= 0)
1233 (void) serialize_item(f
, "control-command", mount_exec_command_to_string(m
->control_command_id
));
1238 static int mount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1239 Mount
*m
= MOUNT(u
);
1247 if (streq(key
, "state")) {
1250 if ((state
= mount_state_from_string(value
)) < 0)
1251 log_unit_debug(u
, "Failed to parse state value: %s", value
);
1253 m
->deserialized_state
= state
;
1255 } else if (streq(key
, "result")) {
1258 f
= mount_result_from_string(value
);
1260 log_unit_debug(u
, "Failed to parse result value: %s", value
);
1261 else if (f
!= MOUNT_SUCCESS
)
1264 } else if (streq(key
, "reload-result")) {
1267 f
= mount_result_from_string(value
);
1269 log_unit_debug(u
, "Failed to parse reload result value: %s", value
);
1270 else if (f
!= MOUNT_SUCCESS
)
1271 m
->reload_result
= f
;
1273 } else if (streq(key
, "n-retry-umount")) {
1275 r
= safe_atou(value
, &m
->n_retry_umount
);
1277 log_unit_debug(u
, "Failed to parse n-retry-umount value: %s", value
);
1279 } else if (streq(key
, "control-pid")) {
1281 if (parse_pid(value
, &m
->control_pid
) < 0)
1282 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
1284 } else if (streq(key
, "control-command")) {
1285 MountExecCommand id
;
1287 id
= mount_exec_command_from_string(value
);
1289 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
1291 m
->control_command_id
= id
;
1292 m
->control_command
= m
->exec_command
+ id
;
1295 log_unit_debug(u
, "Unknown serialization key: %s", key
);
1300 _pure_
static UnitActiveState
mount_active_state(Unit
*u
) {
1303 return state_translation_table
[MOUNT(u
)->state
];
1306 _pure_
static const char *mount_sub_state_to_string(Unit
*u
) {
1309 return mount_state_to_string(MOUNT(u
)->state
);
1312 _pure_
static bool mount_may_gc(Unit
*u
) {
1313 Mount
*m
= MOUNT(u
);
1317 if (m
->from_proc_self_mountinfo
)
1323 static void mount_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1324 Mount
*m
= MOUNT(u
);
1330 if (pid
!= m
->control_pid
)
1333 /* So here's the thing, we really want to know before /usr/bin/mount or /usr/bin/umount exit whether
1334 * they established/remove a mount. This is important when mounting, but even more so when unmounting
1335 * since we need to deal with nested mounts and otherwise cannot safely determine whether to repeat
1336 * the unmounts. In theory, the kernel fires /proc/self/mountinfo changes off before returning from
1337 * the mount() or umount() syscalls, and thus we should see the changes to the proc file before we
1338 * process the waitid() for the /usr/bin/(u)mount processes. However, this is unfortunately racy: we
1339 * have to waitid() for processes using P_ALL (since we need to reap unexpected children that got
1340 * reparented to PID 1), but when using P_ALL we might end up reaping processes that terminated just
1341 * instants ago, i.e. already after our last event loop iteration (i.e. after the last point we might
1342 * have noticed /proc/self/mountinfo events via epoll). This means event loop priorities for
1343 * processing SIGCHLD vs. /proc/self/mountinfo IO events are not as relevant as we want. To fix that
1344 * race, let's explicitly scan /proc/self/mountinfo before we start processing /usr/bin/(u)mount
1345 * dying. It's ugly, but it makes our ordering systematic again, and makes sure we always see
1346 * /proc/self/mountinfo changes before our mount/umount exits. */
1347 (void) mount_process_proc_self_mountinfo(u
->manager
);
1351 if (is_clean_exit(code
, status
, EXIT_CLEAN_COMMAND
, NULL
))
1353 else if (code
== CLD_EXITED
)
1354 f
= MOUNT_FAILURE_EXIT_CODE
;
1355 else if (code
== CLD_KILLED
)
1356 f
= MOUNT_FAILURE_SIGNAL
;
1357 else if (code
== CLD_DUMPED
)
1358 f
= MOUNT_FAILURE_CORE_DUMP
;
1360 assert_not_reached("Unknown code");
1362 if (IN_SET(m
->state
, MOUNT_REMOUNTING
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_REMOUNTING_SIGTERM
))
1363 mount_set_reload_result(m
, f
);
1364 else if (m
->result
== MOUNT_SUCCESS
)
1367 if (m
->control_command
) {
1368 exec_status_exit(&m
->control_command
->exec_status
, &m
->exec_context
, pid
, code
, status
);
1370 m
->control_command
= NULL
;
1371 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
1374 unit_log_process_exit(
1377 mount_exec_command_to_string(m
->control_command_id
),
1381 /* Note that due to the io event priority logic, we can be sure the new mountinfo is loaded
1382 * before we process the SIGCHLD for the mount command. */
1386 case MOUNT_MOUNTING
:
1387 /* Our mount point has not appeared in mountinfo. Something went wrong. */
1389 if (f
== MOUNT_SUCCESS
) {
1390 /* Either /bin/mount has an unexpected definition of success,
1391 * or someone raced us and we lost. */
1392 log_unit_warning(UNIT(m
), "Mount process finished, but there is no mount.");
1393 f
= MOUNT_FAILURE_PROTOCOL
;
1395 mount_enter_dead(m
, f
);
1398 case MOUNT_MOUNTING_DONE
:
1399 mount_enter_mounted(m
, f
);
1402 case MOUNT_REMOUNTING
:
1403 case MOUNT_REMOUNTING_SIGTERM
:
1404 case MOUNT_REMOUNTING_SIGKILL
:
1405 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1408 case MOUNT_UNMOUNTING
:
1410 if (f
== MOUNT_SUCCESS
&& m
->from_proc_self_mountinfo
) {
1412 /* Still a mount point? If so, let's try again. Most likely there were multiple mount points
1413 * stacked on top of each other. We might exceed the timeout specified by the user overall,
1414 * but we will stop as soon as any one umount times out. */
1416 if (m
->n_retry_umount
< RETRY_UMOUNT_MAX
) {
1417 log_unit_debug(u
, "Mount still present, trying again.");
1418 m
->n_retry_umount
++;
1419 mount_enter_unmounting(m
);
1421 log_unit_warning(u
, "Mount still present after %u attempts to unmount, giving up.", m
->n_retry_umount
);
1422 mount_enter_mounted(m
, f
);
1425 mount_enter_dead_or_mounted(m
, f
);
1429 case MOUNT_UNMOUNTING_SIGKILL
:
1430 case MOUNT_UNMOUNTING_SIGTERM
:
1431 mount_enter_dead_or_mounted(m
, f
);
1434 case MOUNT_CLEANING
:
1435 if (m
->clean_result
== MOUNT_SUCCESS
)
1436 m
->clean_result
= f
;
1438 mount_enter_dead(m
, MOUNT_SUCCESS
);
1442 assert_not_reached("Uh, control process died at wrong time.");
1445 /* Notify clients about changed exit status */
1446 unit_add_to_dbus_queue(u
);
1449 static int mount_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
1450 Mount
*m
= MOUNT(userdata
);
1453 assert(m
->timer_event_source
== source
);
1457 case MOUNT_MOUNTING
:
1458 case MOUNT_MOUNTING_DONE
:
1459 log_unit_warning(UNIT(m
), "Mounting timed out. Terminating.");
1460 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1463 case MOUNT_REMOUNTING
:
1464 log_unit_warning(UNIT(m
), "Remounting timed out. Terminating remount process.");
1465 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1466 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGTERM
, MOUNT_SUCCESS
);
1469 case MOUNT_REMOUNTING_SIGTERM
:
1470 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1472 if (m
->kill_context
.send_sigkill
) {
1473 log_unit_warning(UNIT(m
), "Remounting timed out. Killing.");
1474 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
1476 log_unit_warning(UNIT(m
), "Remounting timed out. Skipping SIGKILL. Ignoring.");
1477 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1481 case MOUNT_REMOUNTING_SIGKILL
:
1482 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1484 log_unit_warning(UNIT(m
), "Mount process still around after SIGKILL. Ignoring.");
1485 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1488 case MOUNT_UNMOUNTING
:
1489 log_unit_warning(UNIT(m
), "Unmounting timed out. Terminating.");
1490 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1493 case MOUNT_UNMOUNTING_SIGTERM
:
1494 if (m
->kill_context
.send_sigkill
) {
1495 log_unit_warning(UNIT(m
), "Mount process timed out. Killing.");
1496 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_FAILURE_TIMEOUT
);
1498 log_unit_warning(UNIT(m
), "Mount process timed out. Skipping SIGKILL. Ignoring.");
1499 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1503 case MOUNT_UNMOUNTING_SIGKILL
:
1504 log_unit_warning(UNIT(m
), "Mount process still around after SIGKILL. Ignoring.");
1505 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1508 case MOUNT_CLEANING
:
1509 log_unit_warning(UNIT(m
), "Cleaning timed out. killing.");
1511 if (m
->clean_result
== MOUNT_SUCCESS
)
1512 m
->clean_result
= MOUNT_FAILURE_TIMEOUT
;
1514 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, 0);
1518 assert_not_reached("Timeout at wrong time.");
1524 static int mount_setup_new_unit(
1529 const char *options
,
1531 MountProcFlags
*ret_flags
,
1534 _cleanup_(unit_freep
) Unit
*u
= NULL
;
1542 r
= unit_new_for_name(m
, sizeof(Mount
), name
, &u
);
1546 r
= free_and_strdup(&u
->source_path
, "/proc/self/mountinfo");
1550 r
= free_and_strdup(&MOUNT(u
)->where
, where
);
1554 r
= update_parameters_proc_self_mountinfo(MOUNT(u
), what
, options
, fstype
);
1558 /* This unit was generated because /proc/self/mountinfo reported it. Remember this, so that by the time we load
1559 * the unit file for it (and thus add in extra deps right after) we know what source to attributes the deps
1561 MOUNT(u
)->from_proc_self_mountinfo
= true;
1563 /* We have only allocated the stub now, let's enqueue this unit for loading now, so that everything else is
1565 unit_add_to_load_queue(u
);
1567 *ret_flags
= MOUNT_PROC_IS_MOUNTED
| MOUNT_PROC_JUST_MOUNTED
| MOUNT_PROC_JUST_CHANGED
;
1572 static int mount_setup_existing_unit(
1576 const char *options
,
1578 MountProcFlags
*ret_flags
) {
1585 if (!MOUNT(u
)->where
) {
1586 MOUNT(u
)->where
= strdup(where
);
1587 if (!MOUNT(u
)->where
)
1591 /* In case we have multiple mounts established on the same mount point, let's merge flags set already
1592 * for the current unit. Note that the flags field is reset on each iteration of reading
1593 * /proc/self/mountinfo, hence we know for sure anything already set here is from the current
1594 * iteration and thus worthy of taking into account. */
1595 MountProcFlags flags
=
1596 MOUNT(u
)->proc_flags
| MOUNT_PROC_IS_MOUNTED
;
1598 r
= update_parameters_proc_self_mountinfo(MOUNT(u
), what
, options
, fstype
);
1602 flags
|= MOUNT_PROC_JUST_CHANGED
;
1604 /* There are two conditions when we consider a mount point just mounted: when we haven't seen it in
1605 * /proc/self/mountinfo before or when MOUNT_MOUNTING is our current state. Why bother with the
1606 * latter? Shouldn't that be covered by the former? No, during reload it is not because we might then
1607 * encounter a new /proc/self/mountinfo in combination with an old mount unit state (since it stems
1608 * from the serialized state), and need to catch up. Since we know that the MOUNT_MOUNTING state is
1609 * reached when we wait for the mount to appear we hence can assume that if we are in it, we are
1610 * actually seeing it established for the first time. */
1611 if (!MOUNT(u
)->from_proc_self_mountinfo
|| MOUNT(u
)->state
== MOUNT_MOUNTING
)
1612 flags
|= MOUNT_PROC_JUST_MOUNTED
;
1614 MOUNT(u
)->from_proc_self_mountinfo
= true;
1616 if (IN_SET(u
->load_state
, UNIT_NOT_FOUND
, UNIT_BAD_SETTING
, UNIT_ERROR
)) {
1617 /* The unit was previously not found or otherwise not loaded. Now that the unit shows up in
1618 * /proc/self/mountinfo we should reconsider it this, hence set it to UNIT_LOADED. */
1619 u
->load_state
= UNIT_LOADED
;
1622 flags
|= MOUNT_PROC_JUST_CHANGED
;
1625 if (FLAGS_SET(flags
, MOUNT_PROC_JUST_CHANGED
)) {
1626 /* If things changed, then make sure that all deps are regenerated. Let's
1627 * first remove all automatic deps, and then add in the new ones. */
1629 unit_remove_dependencies(u
, UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
);
1631 r
= mount_add_non_exec_dependencies(MOUNT(u
));
1640 static int mount_setup_unit(
1644 const char *options
,
1648 _cleanup_free_
char *e
= NULL
;
1649 MountProcFlags flags
;
1659 /* Ignore API mount points. They should never be referenced in
1660 * dependencies ever. */
1661 if (mount_point_is_api(where
) || mount_point_ignore(where
))
1664 if (streq(fstype
, "autofs"))
1667 /* probably some kind of swap, ignore */
1668 if (!is_path(where
))
1671 /* Mount unit names have to be (like all other unit names) short enough to fit into file names. This
1672 * means there's a good chance that overly long mount point paths after mangling them to look like a
1673 * unit name would result in unit names we don't actually consider valid. This should be OK however
1674 * as such long mount point paths should not happen on regular systems — and if they appear
1675 * nonetheless they are generally synthesized by software, and thus managed by that other
1676 * software. Having such long names just means you cannot use systemd to manage those specific mount
1677 * points, which should be an OK restriction to make. After all we don't have to be able to manage
1678 * all mount points in the world — as long as we don't choke on them when we encounter them. */
1679 r
= unit_name_from_path(where
, ".mount", &e
);
1681 static RateLimit rate_limit
= { /* Let's log about this at warning level at most once every
1682 * 5s. Given that we generate this whenever we read the file
1683 * otherwise we probably shouldn't flood the logs with
1685 .interval
= 5 * USEC_PER_SEC
,
1689 return log_struct_errno(
1690 ratelimit_below(&rate_limit
) ? LOG_WARNING
: LOG_DEBUG
, r
,
1691 "MESSAGE_ID=" SD_MESSAGE_MOUNT_POINT_PATH_NOT_SUITABLE_STR
,
1692 "MOUNT_POINT=%s", where
,
1693 LOG_MESSAGE("Failed to generate valid unit name from path '%s', ignoring mount point: %m", where
));
1696 u
= manager_get_unit(m
, e
);
1698 r
= mount_setup_existing_unit(u
, what
, where
, options
, fstype
, &flags
);
1700 /* First time we see this mount point meaning that it's not been initiated by a mount unit but rather
1701 * by the sysadmin having called mount(8) directly. */
1702 r
= mount_setup_new_unit(m
, e
, what
, where
, options
, fstype
, &flags
, &u
);
1704 return log_warning_errno(r
, "Failed to set up mount unit for '%s': %m", where
);
1706 /* If the mount changed properties or state, let's notify our clients */
1707 if (flags
& (MOUNT_PROC_JUST_CHANGED
|MOUNT_PROC_JUST_MOUNTED
))
1708 unit_add_to_dbus_queue(u
);
1711 MOUNT(u
)->proc_flags
= flags
;
1716 static int mount_load_proc_self_mountinfo(Manager
*m
, bool set_flags
) {
1717 _cleanup_(mnt_free_tablep
) struct libmnt_table
*table
= NULL
;
1718 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*iter
= NULL
;
1723 r
= libmount_parse(NULL
, NULL
, &table
, &iter
);
1725 return log_error_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
1728 struct libmnt_fs
*fs
;
1729 const char *device
, *path
, *options
, *fstype
;
1731 r
= mnt_table_next_fs(table
, iter
, &fs
);
1735 return log_error_errno(r
, "Failed to get next entry from /proc/self/mountinfo: %m");
1737 device
= mnt_fs_get_source(fs
);
1738 path
= mnt_fs_get_target(fs
);
1739 options
= mnt_fs_get_options(fs
);
1740 fstype
= mnt_fs_get_fstype(fs
);
1742 if (!device
|| !path
)
1745 device_found_node(m
, device
, DEVICE_FOUND_MOUNT
, DEVICE_FOUND_MOUNT
);
1747 (void) mount_setup_unit(m
, device
, path
, options
, fstype
, set_flags
);
1753 static void mount_shutdown(Manager
*m
) {
1756 m
->mount_event_source
= sd_event_source_unref(m
->mount_event_source
);
1758 mnt_unref_monitor(m
->mount_monitor
);
1759 m
->mount_monitor
= NULL
;
1762 static int mount_get_timeout(Unit
*u
, usec_t
*timeout
) {
1763 Mount
*m
= MOUNT(u
);
1767 if (!m
->timer_event_source
)
1770 r
= sd_event_source_get_time(m
->timer_event_source
, &t
);
1773 if (t
== USEC_INFINITY
)
1780 static void mount_enumerate_perpetual(Manager
*m
) {
1786 /* Whatever happens, we know for sure that the root directory is around, and cannot go away. Let's
1787 * unconditionally synthesize it here and mark it as perpetual. */
1789 u
= manager_get_unit(m
, SPECIAL_ROOT_MOUNT
);
1791 r
= unit_new_for_name(m
, sizeof(Mount
), SPECIAL_ROOT_MOUNT
, &u
);
1793 log_error_errno(r
, "Failed to allocate the special " SPECIAL_ROOT_MOUNT
" unit: %m");
1798 u
->perpetual
= true;
1799 MOUNT(u
)->deserialized_state
= MOUNT_MOUNTED
;
1801 unit_add_to_load_queue(u
);
1802 unit_add_to_dbus_queue(u
);
1805 static bool mount_is_mounted(Mount
*m
) {
1808 return UNIT(m
)->perpetual
|| FLAGS_SET(m
->proc_flags
, MOUNT_PROC_IS_MOUNTED
);
1811 static void mount_enumerate(Manager
*m
) {
1818 if (!m
->mount_monitor
) {
1821 m
->mount_monitor
= mnt_new_monitor();
1822 if (!m
->mount_monitor
) {
1827 r
= mnt_monitor_enable_kernel(m
->mount_monitor
, 1);
1829 log_error_errno(r
, "Failed to enable watching of kernel mount events: %m");
1833 r
= mnt_monitor_enable_userspace(m
->mount_monitor
, 1, NULL
);
1835 log_error_errno(r
, "Failed to enable watching of userspace mount events: %m");
1839 /* mnt_unref_monitor() will close the fd */
1840 fd
= r
= mnt_monitor_get_fd(m
->mount_monitor
);
1842 log_error_errno(r
, "Failed to acquire watch file descriptor: %m");
1846 r
= sd_event_add_io(m
->event
, &m
->mount_event_source
, fd
, EPOLLIN
, mount_dispatch_io
, m
);
1848 log_error_errno(r
, "Failed to watch mount file descriptor: %m");
1852 r
= sd_event_source_set_priority(m
->mount_event_source
, SD_EVENT_PRIORITY_NORMAL
-10);
1854 log_error_errno(r
, "Failed to adjust mount watch priority: %m");
1858 r
= sd_event_source_set_ratelimit(m
->mount_event_source
, 1 * USEC_PER_SEC
, 5);
1860 log_error_errno(r
, "Failed to enable rate limit for mount events: %m");
1864 (void) sd_event_source_set_description(m
->mount_event_source
, "mount-monitor-dispatch");
1867 r
= mount_load_proc_self_mountinfo(m
, false);
1877 static int drain_libmount(Manager
*m
) {
1878 bool rescan
= false;
1883 /* Drain all events and verify that the event is valid.
1885 * Note that libmount also monitors /run/mount mkdir if the directory does not exist yet. The mkdir
1886 * may generate event which is irrelevant for us.
1888 * error: r < 0; valid: r == 0, false positive: r == 1 */
1890 r
= mnt_monitor_next_change(m
->mount_monitor
, NULL
, NULL
);
1892 return log_error_errno(r
, "Failed to drain libmount events: %m");
1900 static int mount_process_proc_self_mountinfo(Manager
*m
) {
1901 _cleanup_set_free_free_ Set
*around
= NULL
, *gone
= NULL
;
1908 r
= drain_libmount(m
);
1912 r
= mount_load_proc_self_mountinfo(m
, true);
1914 /* Reset flags, just in case, for later calls */
1915 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
])
1916 MOUNT(u
)->proc_flags
= 0;
1921 manager_dispatch_load_queue(m
);
1923 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
]) {
1924 Mount
*mount
= MOUNT(u
);
1926 if (!mount_is_mounted(mount
)) {
1928 /* A mount point is not around right now. It
1929 * might be gone, or might never have
1932 if (mount
->from_proc_self_mountinfo
&&
1933 mount
->parameters_proc_self_mountinfo
.what
) {
1935 /* Remember that this device might just have disappeared */
1936 if (set_ensure_allocated(&gone
, &path_hash_ops
) < 0 ||
1937 set_put_strdup(&gone
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
1938 log_oom(); /* we don't care too much about OOM here... */
1941 mount
->from_proc_self_mountinfo
= false;
1942 assert_se(update_parameters_proc_self_mountinfo(mount
, NULL
, NULL
, NULL
) >= 0);
1944 switch (mount
->state
) {
1947 /* This has just been unmounted by somebody else, follow the state change. */
1948 mount_enter_dead(mount
, MOUNT_SUCCESS
);
1951 case MOUNT_MOUNTING_DONE
:
1952 /* The mount command may add the corresponding proc mountinfo entry and
1953 * then remove it because of an internal error. E.g., fuse.sshfs seems
1954 * to do that when the connection fails. See #17617. To handle such the
1955 * case, let's once set the state back to mounting. Then, the unit can
1956 * correctly enter the failed state later in mount_sigchld(). */
1957 mount_set_state(mount
, MOUNT_MOUNTING
);
1964 } else if (mount
->proc_flags
& (MOUNT_PROC_JUST_MOUNTED
|MOUNT_PROC_JUST_CHANGED
)) {
1966 /* A mount point was added or changed */
1968 switch (mount
->state
) {
1973 /* This has just been mounted by somebody else, follow the state change, but let's
1974 * generate a new invocation ID for this implicitly and automatically. */
1975 (void) unit_acquire_invocation_id(u
);
1976 mount_cycle_clear(mount
);
1977 mount_enter_mounted(mount
, MOUNT_SUCCESS
);
1980 case MOUNT_MOUNTING
:
1981 mount_set_state(mount
, MOUNT_MOUNTING_DONE
);
1985 /* Nothing really changed, but let's
1986 * issue an notification call
1987 * nonetheless, in case somebody is
1988 * waiting for this. (e.g. file system
1989 * ro/rw remounts.) */
1990 mount_set_state(mount
, mount
->state
);
1995 if (mount_is_mounted(mount
) &&
1996 mount
->from_proc_self_mountinfo
&&
1997 mount
->parameters_proc_self_mountinfo
.what
) {
1998 /* Track devices currently used */
2000 if (set_ensure_allocated(&around
, &path_hash_ops
) < 0 ||
2001 set_put_strdup(&around
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
2005 /* Reset the flags for later calls */
2006 mount
->proc_flags
= 0;
2009 SET_FOREACH(what
, gone
) {
2010 if (set_contains(around
, what
))
2013 /* Let the device units know that the device is no longer mounted */
2014 device_found_node(m
, what
, 0, DEVICE_FOUND_MOUNT
);
2020 static int mount_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
2021 Manager
*m
= userdata
;
2024 assert(revents
& EPOLLIN
);
2026 return mount_process_proc_self_mountinfo(m
);
2029 static void mount_reset_failed(Unit
*u
) {
2030 Mount
*m
= MOUNT(u
);
2034 if (m
->state
== MOUNT_FAILED
)
2035 mount_set_state(m
, MOUNT_DEAD
);
2037 m
->result
= MOUNT_SUCCESS
;
2038 m
->reload_result
= MOUNT_SUCCESS
;
2039 m
->clean_result
= MOUNT_SUCCESS
;
2042 static int mount_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
2043 Mount
*m
= MOUNT(u
);
2047 return unit_kill_common(u
, who
, signo
, -1, m
->control_pid
, error
);
2050 static int mount_control_pid(Unit
*u
) {
2051 Mount
*m
= MOUNT(u
);
2055 return m
->control_pid
;
2058 static int mount_clean(Unit
*u
, ExecCleanMask mask
) {
2059 _cleanup_strv_free_
char **l
= NULL
;
2060 Mount
*m
= MOUNT(u
);
2066 if (m
->state
!= MOUNT_DEAD
)
2069 r
= exec_context_get_clean_directories(&m
->exec_context
, u
->manager
->prefix
, mask
, &l
);
2073 if (strv_isempty(l
))
2076 mount_unwatch_control_pid(m
);
2077 m
->clean_result
= MOUNT_SUCCESS
;
2078 m
->control_command
= NULL
;
2079 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
2081 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->exec_context
.timeout_clean_usec
));
2085 r
= unit_fork_and_watch_rm_rf(u
, l
, &m
->control_pid
);
2089 mount_set_state(m
, MOUNT_CLEANING
);
2094 log_unit_warning_errno(u
, r
, "Failed to initiate cleaning: %m");
2095 m
->clean_result
= MOUNT_FAILURE_RESOURCES
;
2096 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
2100 static int mount_can_clean(Unit
*u
, ExecCleanMask
*ret
) {
2101 Mount
*m
= MOUNT(u
);
2105 return exec_context_get_clean_mask(&m
->exec_context
, ret
);
2108 static const char* const mount_exec_command_table
[_MOUNT_EXEC_COMMAND_MAX
] = {
2109 [MOUNT_EXEC_MOUNT
] = "ExecMount",
2110 [MOUNT_EXEC_UNMOUNT
] = "ExecUnmount",
2111 [MOUNT_EXEC_REMOUNT
] = "ExecRemount",
2114 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command
, MountExecCommand
);
2116 static const char* const mount_result_table
[_MOUNT_RESULT_MAX
] = {
2117 [MOUNT_SUCCESS
] = "success",
2118 [MOUNT_FAILURE_RESOURCES
] = "resources",
2119 [MOUNT_FAILURE_TIMEOUT
] = "timeout",
2120 [MOUNT_FAILURE_EXIT_CODE
] = "exit-code",
2121 [MOUNT_FAILURE_SIGNAL
] = "signal",
2122 [MOUNT_FAILURE_CORE_DUMP
] = "core-dump",
2123 [MOUNT_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
2124 [MOUNT_FAILURE_PROTOCOL
] = "protocol",
2127 DEFINE_STRING_TABLE_LOOKUP(mount_result
, MountResult
);
2129 const UnitVTable mount_vtable
= {
2130 .object_size
= sizeof(Mount
),
2131 .exec_context_offset
= offsetof(Mount
, exec_context
),
2132 .cgroup_context_offset
= offsetof(Mount
, cgroup_context
),
2133 .kill_context_offset
= offsetof(Mount
, kill_context
),
2134 .exec_runtime_offset
= offsetof(Mount
, exec_runtime
),
2135 .dynamic_creds_offset
= offsetof(Mount
, dynamic_creds
),
2141 .private_section
= "Mount",
2143 .can_transient
= true,
2150 .coldplug
= mount_coldplug
,
2154 .start
= mount_start
,
2156 .reload
= mount_reload
,
2159 .clean
= mount_clean
,
2160 .can_clean
= mount_can_clean
,
2162 .serialize
= mount_serialize
,
2163 .deserialize_item
= mount_deserialize_item
,
2165 .active_state
= mount_active_state
,
2166 .sub_state_to_string
= mount_sub_state_to_string
,
2168 .will_restart
= unit_will_restart_default
,
2170 .may_gc
= mount_may_gc
,
2171 .is_extrinsic
= mount_is_extrinsic
,
2173 .sigchld_event
= mount_sigchld_event
,
2175 .reset_failed
= mount_reset_failed
,
2177 .control_pid
= mount_control_pid
,
2179 .bus_set_property
= bus_mount_set_property
,
2180 .bus_commit_properties
= bus_mount_commit_properties
,
2182 .get_timeout
= mount_get_timeout
,
2184 .enumerate_perpetual
= mount_enumerate_perpetual
,
2185 .enumerate
= mount_enumerate
,
2186 .shutdown
= mount_shutdown
,
2188 .status_message_formats
= {
2189 .starting_stopping
= {
2190 [0] = "Mounting %s...",
2191 [1] = "Unmounting %s...",
2193 .finished_start_job
= {
2194 [JOB_DONE
] = "Mounted %s.",
2195 [JOB_FAILED
] = "Failed to mount %s.",
2196 [JOB_TIMEOUT
] = "Timed out mounting %s.",
2198 .finished_stop_job
= {
2199 [JOB_DONE
] = "Unmounted %s.",
2200 [JOB_FAILED
] = "Failed unmounting %s.",
2201 [JOB_TIMEOUT
] = "Timed out unmounting %s.",