1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "sd-messages.h"
12 #include "alloc-util.h"
13 #include "dbus-mount.h"
14 #include "dbus-unit.h"
17 #include "exit-status.h"
18 #include "format-util.h"
19 #include "fstab-util.h"
23 #include "mount-setup.h"
25 #include "mountpoint-util.h"
26 #include "parse-util.h"
27 #include "path-util.h"
28 #include "process-util.h"
29 #include "serialize.h"
31 #include "string-table.h"
32 #include "string-util.h"
34 #include "unit-name.h"
37 #define RETRY_UMOUNT_MAX 32
39 DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_table
*, mnt_free_table
);
40 DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_iter
*, mnt_free_iter
);
42 static const UnitActiveState state_translation_table
[_MOUNT_STATE_MAX
] = {
43 [MOUNT_DEAD
] = UNIT_INACTIVE
,
44 [MOUNT_MOUNTING
] = UNIT_ACTIVATING
,
45 [MOUNT_MOUNTING_DONE
] = UNIT_ACTIVATING
,
46 [MOUNT_MOUNTED
] = UNIT_ACTIVE
,
47 [MOUNT_REMOUNTING
] = UNIT_RELOADING
,
48 [MOUNT_UNMOUNTING
] = UNIT_DEACTIVATING
,
49 [MOUNT_REMOUNTING_SIGTERM
] = UNIT_RELOADING
,
50 [MOUNT_REMOUNTING_SIGKILL
] = UNIT_RELOADING
,
51 [MOUNT_UNMOUNTING_SIGTERM
] = UNIT_DEACTIVATING
,
52 [MOUNT_UNMOUNTING_SIGKILL
] = UNIT_DEACTIVATING
,
53 [MOUNT_FAILED
] = UNIT_FAILED
56 static int mount_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
57 static int mount_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
);
59 static bool MOUNT_STATE_WITH_PROCESS(MountState state
) {
64 MOUNT_REMOUNTING_SIGTERM
,
65 MOUNT_REMOUNTING_SIGKILL
,
67 MOUNT_UNMOUNTING_SIGTERM
,
68 MOUNT_UNMOUNTING_SIGKILL
);
71 static bool mount_is_network(const MountParameters
*p
) {
74 if (fstab_test_option(p
->options
, "_netdev\0"))
77 if (p
->fstype
&& fstype_is_network(p
->fstype
))
83 static bool mount_is_loop(const MountParameters
*p
) {
86 if (fstab_test_option(p
->options
, "loop\0"))
92 static bool mount_is_bind(const MountParameters
*p
) {
95 if (fstab_test_option(p
->options
, "bind\0" "rbind\0"))
98 if (p
->fstype
&& STR_IN_SET(p
->fstype
, "bind", "rbind"))
104 static bool mount_is_bound_to_device(const Mount
*m
) {
105 const MountParameters
*p
;
107 if (m
->from_fragment
)
110 p
= &m
->parameters_proc_self_mountinfo
;
111 return fstab_test_option(p
->options
, "x-systemd.device-bound\0");
114 static bool mount_needs_quota(const MountParameters
*p
) {
117 /* Quotas are not enabled on network filesystems, but we want them, for example, on storage connected via
118 * iscsi. We hence don't use mount_is_network() here, as that would also return true for _netdev devices. */
119 if (p
->fstype
&& fstype_is_network(p
->fstype
))
122 if (mount_is_bind(p
))
125 return fstab_test_option(p
->options
,
126 "usrquota\0" "grpquota\0" "quota\0" "usrjquota\0" "grpjquota\0");
129 static void mount_init(Unit
*u
) {
133 assert(u
->load_state
== UNIT_STUB
);
135 m
->timeout_usec
= u
->manager
->default_timeout_start_usec
;
137 m
->exec_context
.std_output
= u
->manager
->default_std_output
;
138 m
->exec_context
.std_error
= u
->manager
->default_std_error
;
140 m
->directory_mode
= 0755;
142 /* We need to make sure that /usr/bin/mount is always called
143 * in the same process group as us, so that the autofs kernel
144 * side doesn't send us another mount request while we are
145 * already trying to comply its last one. */
146 m
->exec_context
.same_pgrp
= true;
148 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
150 u
->ignore_on_isolate
= true;
153 static int mount_arm_timer(Mount
*m
, usec_t usec
) {
158 if (m
->timer_event_source
) {
159 r
= sd_event_source_set_time(m
->timer_event_source
, usec
);
163 return sd_event_source_set_enabled(m
->timer_event_source
, SD_EVENT_ONESHOT
);
166 if (usec
== USEC_INFINITY
)
169 r
= sd_event_add_time(
170 UNIT(m
)->manager
->event
,
171 &m
->timer_event_source
,
174 mount_dispatch_timer
, m
);
178 (void) sd_event_source_set_description(m
->timer_event_source
, "mount-timer");
183 static void mount_unwatch_control_pid(Mount
*m
) {
186 if (m
->control_pid
<= 0)
189 unit_unwatch_pid(UNIT(m
), m
->control_pid
);
193 static void mount_parameters_done(MountParameters
*p
) {
196 p
->what
= mfree(p
->what
);
197 p
->options
= mfree(p
->options
);
198 p
->fstype
= mfree(p
->fstype
);
201 static void mount_done(Unit
*u
) {
206 m
->where
= mfree(m
->where
);
208 mount_parameters_done(&m
->parameters_proc_self_mountinfo
);
209 mount_parameters_done(&m
->parameters_fragment
);
211 m
->exec_runtime
= exec_runtime_unref(m
->exec_runtime
, false);
212 exec_command_done_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
213 m
->control_command
= NULL
;
215 dynamic_creds_unref(&m
->dynamic_creds
);
217 mount_unwatch_control_pid(m
);
219 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
222 _pure_
static MountParameters
* get_mount_parameters_fragment(Mount
*m
) {
225 if (m
->from_fragment
)
226 return &m
->parameters_fragment
;
231 _pure_
static MountParameters
* get_mount_parameters(Mount
*m
) {
234 if (m
->from_proc_self_mountinfo
)
235 return &m
->parameters_proc_self_mountinfo
;
237 return get_mount_parameters_fragment(m
);
240 static int update_parameters_proc_self_mount_info(
244 const char *fstype
) {
249 p
= &m
->parameters_proc_self_mountinfo
;
251 r
= free_and_strdup(&p
->what
, what
);
255 q
= free_and_strdup(&p
->options
, options
);
259 w
= free_and_strdup(&p
->fstype
, fstype
);
263 return r
> 0 || q
> 0 || w
> 0;
266 static int mount_add_mount_dependencies(Mount
*m
) {
275 if (!path_equal(m
->where
, "/")) {
276 _cleanup_free_
char *parent
= NULL
;
278 /* Adds in links to other mount points that might lie further up in the hierarchy */
280 parent
= dirname_malloc(m
->where
);
284 r
= unit_require_mounts_for(UNIT(m
), parent
, UNIT_DEPENDENCY_IMPLICIT
);
289 /* Adds in dependencies to other mount points that might be needed for the source path (if this is a bind mount
290 * or a loop mount) to be available. */
291 pm
= get_mount_parameters_fragment(m
);
292 if (pm
&& pm
->what
&&
293 path_is_absolute(pm
->what
) &&
294 (mount_is_bind(pm
) || mount_is_loop(pm
) || !mount_is_network(pm
))) {
296 r
= unit_require_mounts_for(UNIT(m
), pm
->what
, UNIT_DEPENDENCY_FILE
);
301 /* Adds in dependencies to other units that use this path or paths further down in the hierarchy */
302 s
= manager_get_units_requiring_mounts_for(UNIT(m
)->manager
, m
->where
);
303 SET_FOREACH(other
, s
, i
) {
305 if (other
->load_state
!= UNIT_LOADED
)
308 if (other
== UNIT(m
))
311 r
= unit_add_dependency(other
, UNIT_AFTER
, UNIT(m
), true, UNIT_DEPENDENCY_PATH
);
315 if (UNIT(m
)->fragment_path
) {
316 /* If we have fragment configuration, then make this dependency required */
317 r
= unit_add_dependency(other
, UNIT_REQUIRES
, UNIT(m
), true, UNIT_DEPENDENCY_PATH
);
326 static int mount_add_device_dependencies(Mount
*m
) {
327 UnitDependencyMask mask
;
334 p
= get_mount_parameters(m
);
341 if (mount_is_bind(p
))
344 if (!is_device_path(p
->what
))
347 /* /dev/root is a really weird thing, it's not a real device,
348 * but just a path the kernel exports for the root file system
349 * specified on the kernel command line. Ignore it here. */
350 if (path_equal(p
->what
, "/dev/root"))
353 if (path_equal(m
->where
, "/"))
356 /* Mount units from /proc/self/mountinfo are not bound to devices
357 * by default since they're subject to races when devices are
358 * unplugged. But the user can still force this dep with an
359 * appropriate option (or udev property) so the mount units are
360 * automatically stopped when the device disappears suddenly. */
361 dep
= mount_is_bound_to_device(m
) ? UNIT_BINDS_TO
: UNIT_REQUIRES
;
363 /* We always use 'what' from /proc/self/mountinfo if mounted */
364 mask
= m
->from_proc_self_mountinfo
? UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
: UNIT_DEPENDENCY_FILE
;
366 r
= unit_add_node_dependency(UNIT(m
), p
->what
, false, dep
, mask
);
373 static int mount_add_quota_dependencies(Mount
*m
) {
374 UnitDependencyMask mask
;
380 if (!MANAGER_IS_SYSTEM(UNIT(m
)->manager
))
383 p
= get_mount_parameters_fragment(m
);
387 if (!mount_needs_quota(p
))
390 mask
= m
->from_fragment
? UNIT_DEPENDENCY_FILE
: UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
;
392 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTACHECK_SERVICE
, true, mask
);
396 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTAON_SERVICE
, true, mask
);
403 static bool mount_is_extrinsic(Mount
*m
) {
407 /* Returns true for all units that are "magic" and should be excluded from the usual start-up and shutdown
408 * dependencies. We call them "extrinsic" here, as they are generally mounted outside of the systemd dependency
409 * logic. We shouldn't attempt to manage them ourselves but it's fine if the user operates on them with us. */
411 if (!MANAGER_IS_SYSTEM(UNIT(m
)->manager
)) /* We only automatically manage mounts if we are in system mode */
414 if (PATH_IN_SET(m
->where
, /* Don't bother with the OS data itself */
419 if (PATH_STARTSWITH_SET(m
->where
,
420 "/run/initramfs", /* This should stay around from before we boot until after we shutdown */
421 "/proc", /* All of this is API VFS */
422 "/sys", /* … dito … */
423 "/dev")) /* … dito … */
426 /* If this is an initrd mount, and we are not in the initrd, then leave this around forever, too. */
427 p
= get_mount_parameters(m
);
428 if (p
&& fstab_test_option(p
->options
, "x-initrd.mount\0") && !in_initrd())
434 static int mount_add_default_dependencies(Mount
*m
) {
435 const char *after
, *before
;
436 UnitDependencyMask mask
;
443 if (!UNIT(m
)->default_dependencies
)
446 /* We do not add any default dependencies to /, /usr or /run/initramfs/, since they are guaranteed to stay
447 * mounted the whole time, since our system is on it. Also, don't bother with anything mounted below virtual
448 * file systems, it's also going to be virtual, and hence not worth the effort. */
449 if (mount_is_extrinsic(m
))
452 p
= get_mount_parameters(m
);
456 mask
= m
->from_fragment
? UNIT_DEPENDENCY_FILE
: UNIT_DEPENDENCY_MOUNTINFO_DEFAULT
;
457 nofail
= m
->from_fragment
? fstab_test_yes_no_option(m
->parameters_fragment
.options
, "nofail\0" "fail\0") : false;
459 if (mount_is_network(p
)) {
460 /* We order ourselves after network.target. This is
461 * primarily useful at shutdown: services that take
462 * down the network should order themselves before
463 * network.target, so that they are shut down only
464 * after this mount unit is stopped. */
466 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, SPECIAL_NETWORK_TARGET
, true, mask
);
470 /* We pull in network-online.target, and order
471 * ourselves after it. This is useful at start-up to
472 * actively pull in tools that want to be started
473 * before we start mounting network file systems, and
474 * whose purpose it is to delay this until the network
477 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_WANTS
, UNIT_AFTER
, SPECIAL_NETWORK_ONLINE_TARGET
, true, mask
);
481 after
= SPECIAL_REMOTE_FS_PRE_TARGET
;
482 before
= SPECIAL_REMOTE_FS_TARGET
;
484 after
= SPECIAL_LOCAL_FS_PRE_TARGET
;
485 before
= SPECIAL_LOCAL_FS_TARGET
;
489 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_BEFORE
, before
, true, mask
);
494 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, after
, true, mask
);
498 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, true, mask
);
502 /* If this is a tmpfs mount then we have to unmount it before we try to deactivate swaps */
503 if (streq_ptr(p
->fstype
, "tmpfs")) {
504 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, SPECIAL_SWAP_TARGET
, true, mask
);
512 static int mount_verify(Mount
*m
) {
513 _cleanup_free_
char *e
= NULL
;
519 if (UNIT(m
)->load_state
!= UNIT_LOADED
)
522 if (!m
->from_fragment
&& !m
->from_proc_self_mountinfo
&& !UNIT(m
)->perpetual
)
525 r
= unit_name_from_path(m
->where
, ".mount", &e
);
527 return log_unit_error_errno(UNIT(m
), r
, "Failed to generate unit name from mount path: %m");
529 if (!unit_has_name(UNIT(m
), e
)) {
530 log_unit_error(UNIT(m
), "Where= setting doesn't match unit name. Refusing.");
534 if (mount_point_is_api(m
->where
) || mount_point_ignore(m
->where
)) {
535 log_unit_error(UNIT(m
), "Cannot create mount unit for API file system %s. Refusing.", m
->where
);
539 p
= get_mount_parameters_fragment(m
);
541 log_unit_error(UNIT(m
), "What= setting is missing. Refusing.");
545 if (m
->exec_context
.pam_name
&& m
->kill_context
.kill_mode
!= KILL_CONTROL_GROUP
) {
546 log_unit_error(UNIT(m
), "Unit has PAM enabled. Kill mode must be set to control-group'. Refusing.");
553 static int mount_add_extras(Mount
*m
) {
559 /* Note: this call might be called after we already have been loaded once (and even when it has already been
560 * activated), in case data from /proc/self/mountinfo has changed. This means all code here needs to be ready
561 * to run with an already set up unit. */
563 if (u
->fragment_path
)
564 m
->from_fragment
= true;
567 r
= unit_name_to_path(u
->id
, &m
->where
);
572 path_simplify(m
->where
, false);
574 if (!u
->description
) {
575 r
= unit_set_description(u
, m
->where
);
580 r
= mount_add_device_dependencies(m
);
584 r
= mount_add_mount_dependencies(m
);
588 r
= mount_add_quota_dependencies(m
);
592 r
= unit_patch_contexts(u
);
596 r
= unit_add_exec_dependencies(u
, &m
->exec_context
);
600 r
= unit_set_default_slice(u
);
604 r
= mount_add_default_dependencies(m
);
611 static int mount_load_root_mount(Unit
*u
) {
614 if (!unit_has_name(u
, SPECIAL_ROOT_MOUNT
))
618 u
->default_dependencies
= false;
620 /* The stdio/kmsg bridge socket is on /, in order to avoid a dep loop, don't use kmsg logging for -.mount */
621 MOUNT(u
)->exec_context
.std_output
= EXEC_OUTPUT_NULL
;
622 MOUNT(u
)->exec_context
.std_input
= EXEC_INPUT_NULL
;
625 u
->description
= strdup("Root Mount");
630 static int mount_load(Unit
*u
) {
635 assert(u
->load_state
== UNIT_STUB
);
637 r
= mount_load_root_mount(u
);
639 if (m
->from_proc_self_mountinfo
|| u
->perpetual
)
640 q
= unit_load_fragment_and_dropin_optional(u
);
642 q
= unit_load_fragment_and_dropin(u
);
644 /* Add in some extras. Note we do this in all cases (even if we failed to load the unit) when announced by the
645 * kernel, because we need some things to be set up no matter what when the kernel establishes a mount and thus
646 * we need to update the state in our unit to track it. After all, consider that we don't allow changing the
647 * 'slice' field for a unit once it is active. */
648 if (u
->load_state
== UNIT_LOADED
|| m
->from_proc_self_mountinfo
|| u
->perpetual
)
649 w
= mount_add_extras(m
);
660 return mount_verify(m
);
663 static void mount_set_state(Mount
*m
, MountState state
) {
664 MountState old_state
;
667 if (m
->state
!= state
)
668 bus_unit_send_pending_change_signal(UNIT(m
), false);
670 old_state
= m
->state
;
673 if (!MOUNT_STATE_WITH_PROCESS(state
)) {
674 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
675 mount_unwatch_control_pid(m
);
676 m
->control_command
= NULL
;
677 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
680 if (state
!= old_state
)
681 log_unit_debug(UNIT(m
), "Changed %s -> %s", mount_state_to_string(old_state
), mount_state_to_string(state
));
683 unit_notify(UNIT(m
), state_translation_table
[old_state
], state_translation_table
[state
],
684 m
->reload_result
== MOUNT_SUCCESS
? 0 : UNIT_NOTIFY_RELOAD_FAILURE
);
687 static int mount_coldplug(Unit
*u
) {
689 MountState new_state
= MOUNT_DEAD
;
693 assert(m
->state
== MOUNT_DEAD
);
695 if (m
->deserialized_state
!= m
->state
)
696 new_state
= m
->deserialized_state
;
697 else if (m
->from_proc_self_mountinfo
)
698 new_state
= MOUNT_MOUNTED
;
700 if (new_state
== m
->state
)
703 if (m
->control_pid
> 0 &&
704 pid_is_unwaited(m
->control_pid
) &&
705 MOUNT_STATE_WITH_PROCESS(new_state
)) {
707 r
= unit_watch_pid(UNIT(m
), m
->control_pid
);
711 r
= mount_arm_timer(m
, usec_add(u
->state_change_timestamp
.monotonic
, m
->timeout_usec
));
716 if (!IN_SET(new_state
, MOUNT_DEAD
, MOUNT_FAILED
)) {
717 (void) unit_setup_dynamic_creds(u
);
718 (void) unit_setup_exec_runtime(u
);
721 mount_set_state(m
, new_state
);
725 static void mount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
726 char buf
[FORMAT_TIMESPAN_MAX
];
733 p
= get_mount_parameters(m
);
736 "%sMount State: %s\n"
740 "%sFile System Type: %s\n"
742 "%sFrom /proc/self/mountinfo: %s\n"
743 "%sFrom fragment: %s\n"
745 "%sDirectoryMode: %04o\n"
746 "%sSloppyOptions: %s\n"
747 "%sLazyUnmount: %s\n"
748 "%sForceUnmount: %s\n"
749 "%sTimeoutSec: %s\n",
750 prefix
, mount_state_to_string(m
->state
),
751 prefix
, mount_result_to_string(m
->result
),
753 prefix
, p
? strna(p
->what
) : "n/a",
754 prefix
, p
? strna(p
->fstype
) : "n/a",
755 prefix
, p
? strna(p
->options
) : "n/a",
756 prefix
, yes_no(m
->from_proc_self_mountinfo
),
757 prefix
, yes_no(m
->from_fragment
),
758 prefix
, yes_no(mount_is_extrinsic(m
)),
759 prefix
, m
->directory_mode
,
760 prefix
, yes_no(m
->sloppy_options
),
761 prefix
, yes_no(m
->lazy_unmount
),
762 prefix
, yes_no(m
->force_unmount
),
763 prefix
, format_timespan(buf
, sizeof(buf
), m
->timeout_usec
, USEC_PER_SEC
));
765 if (m
->control_pid
> 0)
767 "%sControl PID: "PID_FMT
"\n",
768 prefix
, m
->control_pid
);
770 exec_context_dump(&m
->exec_context
, f
, prefix
);
771 kill_context_dump(&m
->kill_context
, f
, prefix
);
772 cgroup_context_dump(&m
->cgroup_context
, f
, prefix
);
775 static int mount_spawn(Mount
*m
, ExecCommand
*c
, pid_t
*_pid
) {
777 _cleanup_(exec_params_clear
) ExecParameters exec_params
= {
778 .flags
= EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
,
791 r
= unit_prepare_exec(UNIT(m
));
795 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->timeout_usec
));
799 r
= unit_set_exec_params(UNIT(m
), &exec_params
);
803 r
= exec_spawn(UNIT(m
),
813 r
= unit_watch_pid(UNIT(m
), pid
);
815 /* FIXME: we need to do something here */
823 static void mount_enter_dead(Mount
*m
, MountResult f
) {
826 if (m
->result
== MOUNT_SUCCESS
)
829 unit_log_result(UNIT(m
), m
->result
== MOUNT_SUCCESS
, mount_result_to_string(m
->result
));
830 mount_set_state(m
, m
->result
!= MOUNT_SUCCESS
? MOUNT_FAILED
: MOUNT_DEAD
);
832 m
->exec_runtime
= exec_runtime_unref(m
->exec_runtime
, true);
834 exec_context_destroy_runtime_directory(&m
->exec_context
, UNIT(m
)->manager
->prefix
[EXEC_DIRECTORY_RUNTIME
]);
836 unit_unref_uid_gid(UNIT(m
), true);
838 dynamic_creds_destroy(&m
->dynamic_creds
);
840 /* Any dependencies based on /proc/self/mountinfo are now stale */
841 unit_remove_dependencies(UNIT(m
), UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
);
844 static void mount_enter_mounted(Mount
*m
, MountResult f
) {
847 if (m
->result
== MOUNT_SUCCESS
)
850 mount_set_state(m
, MOUNT_MOUNTED
);
853 static void mount_enter_dead_or_mounted(Mount
*m
, MountResult f
) {
856 /* Enter DEAD or MOUNTED state, depending on what the kernel currently says about the mount point. We use this
857 * whenever we executed an operation, so that our internal state reflects what the kernel says again, after all
858 * ultimately we just mirror the kernel's internal state on this. */
860 if (m
->from_proc_self_mountinfo
)
861 mount_enter_mounted(m
, f
);
863 mount_enter_dead(m
, f
);
866 static int state_to_kill_operation(MountState state
) {
869 case MOUNT_REMOUNTING_SIGTERM
:
870 case MOUNT_UNMOUNTING_SIGTERM
:
871 return KILL_TERMINATE
;
873 case MOUNT_REMOUNTING_SIGKILL
:
874 case MOUNT_UNMOUNTING_SIGKILL
:
878 return _KILL_OPERATION_INVALID
;
882 static void mount_enter_signal(Mount
*m
, MountState state
, MountResult f
) {
887 if (m
->result
== MOUNT_SUCCESS
)
890 r
= unit_kill_context(
893 state_to_kill_operation(state
),
901 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->timeout_usec
));
905 mount_set_state(m
, state
);
906 } else if (state
== MOUNT_REMOUNTING_SIGTERM
&& m
->kill_context
.send_sigkill
)
907 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
908 else if (IN_SET(state
, MOUNT_REMOUNTING_SIGTERM
, MOUNT_REMOUNTING_SIGKILL
))
909 mount_enter_mounted(m
, MOUNT_SUCCESS
);
910 else if (state
== MOUNT_UNMOUNTING_SIGTERM
&& m
->kill_context
.send_sigkill
)
911 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
913 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
918 log_unit_warning_errno(UNIT(m
), r
, "Failed to kill processes: %m");
919 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
922 static void mount_enter_unmounting(Mount
*m
) {
927 /* Start counting our attempts */
928 if (!IN_SET(m
->state
,
930 MOUNT_UNMOUNTING_SIGTERM
,
931 MOUNT_UNMOUNTING_SIGKILL
))
932 m
->n_retry_umount
= 0;
934 m
->control_command_id
= MOUNT_EXEC_UNMOUNT
;
935 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_UNMOUNT
;
937 r
= exec_command_set(m
->control_command
, UMOUNT_PATH
, m
->where
, "-c", NULL
);
938 if (r
>= 0 && m
->lazy_unmount
)
939 r
= exec_command_append(m
->control_command
, "-l", NULL
);
940 if (r
>= 0 && m
->force_unmount
)
941 r
= exec_command_append(m
->control_command
, "-f", NULL
);
945 mount_unwatch_control_pid(m
);
947 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
951 mount_set_state(m
, MOUNT_UNMOUNTING
);
956 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'umount' task: %m");
957 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
960 static void mount_enter_mounting(Mount
*m
) {
966 r
= unit_fail_if_noncanonical(UNIT(m
), m
->where
);
970 (void) mkdir_p_label(m
->where
, m
->directory_mode
);
972 unit_warn_if_dir_nonempty(UNIT(m
), m
->where
);
973 unit_warn_leftover_processes(UNIT(m
));
975 m
->control_command_id
= MOUNT_EXEC_MOUNT
;
976 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_MOUNT
;
978 /* Create the source directory for bind-mounts if needed */
979 p
= get_mount_parameters_fragment(m
);
980 if (p
&& mount_is_bind(p
))
981 (void) mkdir_p_label(p
->what
, m
->directory_mode
);
984 _cleanup_free_
char *opts
= NULL
;
986 r
= fstab_filter_options(p
->options
, "nofail\0" "noauto\0" "auto\0", NULL
, NULL
, &opts
);
990 r
= exec_command_set(m
->control_command
, MOUNT_PATH
, p
->what
, m
->where
, NULL
);
991 if (r
>= 0 && m
->sloppy_options
)
992 r
= exec_command_append(m
->control_command
, "-s", NULL
);
993 if (r
>= 0 && p
->fstype
)
994 r
= exec_command_append(m
->control_command
, "-t", p
->fstype
, NULL
);
995 if (r
>= 0 && !isempty(opts
))
996 r
= exec_command_append(m
->control_command
, "-o", opts
, NULL
);
1002 mount_unwatch_control_pid(m
);
1004 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
1008 mount_set_state(m
, MOUNT_MOUNTING
);
1013 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'mount' task: %m");
1014 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_RESOURCES
);
1017 static void mount_set_reload_result(Mount
*m
, MountResult result
) {
1020 /* Only store the first error we encounter */
1021 if (m
->reload_result
!= MOUNT_SUCCESS
)
1024 m
->reload_result
= result
;
1027 static void mount_enter_remounting(Mount
*m
) {
1033 /* Reset reload result when we are about to start a new remount operation */
1034 m
->reload_result
= MOUNT_SUCCESS
;
1036 m
->control_command_id
= MOUNT_EXEC_REMOUNT
;
1037 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_REMOUNT
;
1039 p
= get_mount_parameters_fragment(m
);
1044 o
= strjoina("remount,", p
->options
);
1048 r
= exec_command_set(m
->control_command
, MOUNT_PATH
,
1051 if (r
>= 0 && m
->sloppy_options
)
1052 r
= exec_command_append(m
->control_command
, "-s", NULL
);
1053 if (r
>= 0 && p
->fstype
)
1054 r
= exec_command_append(m
->control_command
, "-t", p
->fstype
, NULL
);
1060 mount_unwatch_control_pid(m
);
1062 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
1066 mount_set_state(m
, MOUNT_REMOUNTING
);
1071 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'remount' task: %m");
1072 mount_set_reload_result(m
, MOUNT_FAILURE_RESOURCES
);
1073 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1076 static void mount_cycle_clear(Mount
*m
) {
1079 /* Clear all state we shall forget for this new cycle */
1081 m
->result
= MOUNT_SUCCESS
;
1082 m
->reload_result
= MOUNT_SUCCESS
;
1083 exec_command_reset_status_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
1084 UNIT(m
)->reset_accounting
= true;
1087 static int mount_start(Unit
*u
) {
1088 Mount
*m
= MOUNT(u
);
1093 /* We cannot fulfill this request right now, try again later
1095 if (IN_SET(m
->state
,
1097 MOUNT_UNMOUNTING_SIGTERM
,
1098 MOUNT_UNMOUNTING_SIGKILL
))
1101 /* Already on it! */
1102 if (m
->state
== MOUNT_MOUNTING
)
1105 assert(IN_SET(m
->state
, MOUNT_DEAD
, MOUNT_FAILED
));
1107 r
= unit_start_limit_test(u
);
1109 mount_enter_dead(m
, MOUNT_FAILURE_START_LIMIT_HIT
);
1113 r
= unit_acquire_invocation_id(u
);
1117 mount_cycle_clear(m
);
1118 mount_enter_mounting(m
);
1123 static int mount_stop(Unit
*u
) {
1124 Mount
*m
= MOUNT(u
);
1130 case MOUNT_UNMOUNTING
:
1131 case MOUNT_UNMOUNTING_SIGKILL
:
1132 case MOUNT_UNMOUNTING_SIGTERM
:
1136 case MOUNT_MOUNTING
:
1137 case MOUNT_MOUNTING_DONE
:
1138 case MOUNT_REMOUNTING
:
1139 /* If we are still waiting for /bin/mount, we go directly into kill mode. */
1140 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_SUCCESS
);
1143 case MOUNT_REMOUNTING_SIGTERM
:
1144 /* If we are already waiting for a hung remount, convert this to the matching unmounting state */
1145 mount_set_state(m
, MOUNT_UNMOUNTING_SIGTERM
);
1148 case MOUNT_REMOUNTING_SIGKILL
:
1150 mount_set_state(m
, MOUNT_UNMOUNTING_SIGKILL
);
1154 mount_enter_unmounting(m
);
1158 assert_not_reached("Unexpected state.");
1162 static int mount_reload(Unit
*u
) {
1163 Mount
*m
= MOUNT(u
);
1166 assert(m
->state
== MOUNT_MOUNTED
);
1168 mount_enter_remounting(m
);
1173 static int mount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1174 Mount
*m
= MOUNT(u
);
1180 (void) serialize_item(f
, "state", mount_state_to_string(m
->state
));
1181 (void) serialize_item(f
, "result", mount_result_to_string(m
->result
));
1182 (void) serialize_item(f
, "reload-result", mount_result_to_string(m
->reload_result
));
1183 (void) serialize_item_format(f
, "n-retry-umount", "%u", m
->n_retry_umount
);
1185 if (m
->control_pid
> 0)
1186 (void) serialize_item_format(f
, "control-pid", PID_FMT
, m
->control_pid
);
1188 if (m
->control_command_id
>= 0)
1189 (void) serialize_item(f
, "control-command", mount_exec_command_to_string(m
->control_command_id
));
1194 static int mount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1195 Mount
*m
= MOUNT(u
);
1203 if (streq(key
, "state")) {
1206 if ((state
= mount_state_from_string(value
)) < 0)
1207 log_unit_debug(u
, "Failed to parse state value: %s", value
);
1209 m
->deserialized_state
= state
;
1211 } else if (streq(key
, "result")) {
1214 f
= mount_result_from_string(value
);
1216 log_unit_debug(u
, "Failed to parse result value: %s", value
);
1217 else if (f
!= MOUNT_SUCCESS
)
1220 } else if (streq(key
, "reload-result")) {
1223 f
= mount_result_from_string(value
);
1225 log_unit_debug(u
, "Failed to parse reload result value: %s", value
);
1226 else if (f
!= MOUNT_SUCCESS
)
1227 m
->reload_result
= f
;
1229 } else if (streq(key
, "n-retry-umount")) {
1231 r
= safe_atou(value
, &m
->n_retry_umount
);
1233 log_unit_debug(u
, "Failed to parse n-retry-umount value: %s", value
);
1235 } else if (streq(key
, "control-pid")) {
1237 if (parse_pid(value
, &m
->control_pid
) < 0)
1238 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
1240 } else if (streq(key
, "control-command")) {
1241 MountExecCommand id
;
1243 id
= mount_exec_command_from_string(value
);
1245 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
1247 m
->control_command_id
= id
;
1248 m
->control_command
= m
->exec_command
+ id
;
1251 log_unit_debug(u
, "Unknown serialization key: %s", key
);
1256 _pure_
static UnitActiveState
mount_active_state(Unit
*u
) {
1259 return state_translation_table
[MOUNT(u
)->state
];
1262 _pure_
static const char *mount_sub_state_to_string(Unit
*u
) {
1265 return mount_state_to_string(MOUNT(u
)->state
);
1268 _pure_
static bool mount_may_gc(Unit
*u
) {
1269 Mount
*m
= MOUNT(u
);
1273 if (m
->from_proc_self_mountinfo
)
1279 static void mount_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1280 Mount
*m
= MOUNT(u
);
1286 if (pid
!= m
->control_pid
)
1291 if (is_clean_exit(code
, status
, EXIT_CLEAN_COMMAND
, NULL
))
1293 else if (code
== CLD_EXITED
)
1294 f
= MOUNT_FAILURE_EXIT_CODE
;
1295 else if (code
== CLD_KILLED
)
1296 f
= MOUNT_FAILURE_SIGNAL
;
1297 else if (code
== CLD_DUMPED
)
1298 f
= MOUNT_FAILURE_CORE_DUMP
;
1300 assert_not_reached("Unknown code");
1302 if (IN_SET(m
->state
, MOUNT_REMOUNTING
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_REMOUNTING_SIGTERM
))
1303 mount_set_reload_result(m
, f
);
1304 else if (m
->result
== MOUNT_SUCCESS
)
1307 if (m
->control_command
) {
1308 exec_status_exit(&m
->control_command
->exec_status
, &m
->exec_context
, pid
, code
, status
);
1310 m
->control_command
= NULL
;
1311 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
1314 unit_log_process_exit(
1315 u
, f
== MOUNT_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
1317 mount_exec_command_to_string(m
->control_command_id
),
1320 /* Note that due to the io event priority logic, we can be sure the new mountinfo is loaded
1321 * before we process the SIGCHLD for the mount command. */
1325 case MOUNT_MOUNTING
:
1326 /* Our mount point has not appeared in mountinfo. Something went wrong. */
1328 if (f
== MOUNT_SUCCESS
) {
1329 /* Either /bin/mount has an unexpected definition of success,
1330 * or someone raced us and we lost. */
1331 log_unit_warning(UNIT(m
), "Mount process finished, but there is no mount.");
1332 f
= MOUNT_FAILURE_PROTOCOL
;
1334 mount_enter_dead(m
, f
);
1337 case MOUNT_MOUNTING_DONE
:
1338 mount_enter_mounted(m
, f
);
1341 case MOUNT_REMOUNTING
:
1342 case MOUNT_REMOUNTING_SIGTERM
:
1343 case MOUNT_REMOUNTING_SIGKILL
:
1344 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1347 case MOUNT_UNMOUNTING
:
1349 if (f
== MOUNT_SUCCESS
&& m
->from_proc_self_mountinfo
) {
1351 /* Still a mount point? If so, let's try again. Most likely there were multiple mount points
1352 * stacked on top of each other. We might exceed the timeout specified by the user overall,
1353 * but we will stop as soon as any one umount times out. */
1355 if (m
->n_retry_umount
< RETRY_UMOUNT_MAX
) {
1356 log_unit_debug(u
, "Mount still present, trying again.");
1357 m
->n_retry_umount
++;
1358 mount_enter_unmounting(m
);
1360 log_unit_warning(u
, "Mount still present after %u attempts to unmount, giving up.", m
->n_retry_umount
);
1361 mount_enter_mounted(m
, f
);
1364 mount_enter_dead_or_mounted(m
, f
);
1368 case MOUNT_UNMOUNTING_SIGKILL
:
1369 case MOUNT_UNMOUNTING_SIGTERM
:
1370 mount_enter_dead_or_mounted(m
, f
);
1374 assert_not_reached("Uh, control process died at wrong time.");
1377 /* Notify clients about changed exit status */
1378 unit_add_to_dbus_queue(u
);
1381 static int mount_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
1382 Mount
*m
= MOUNT(userdata
);
1385 assert(m
->timer_event_source
== source
);
1389 case MOUNT_MOUNTING
:
1390 case MOUNT_MOUNTING_DONE
:
1391 log_unit_warning(UNIT(m
), "Mounting timed out. Terminating.");
1392 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1395 case MOUNT_REMOUNTING
:
1396 log_unit_warning(UNIT(m
), "Remounting timed out. Terminating remount process.");
1397 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1398 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGTERM
, MOUNT_SUCCESS
);
1401 case MOUNT_REMOUNTING_SIGTERM
:
1402 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1404 if (m
->kill_context
.send_sigkill
) {
1405 log_unit_warning(UNIT(m
), "Remounting timed out. Killing.");
1406 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
1408 log_unit_warning(UNIT(m
), "Remounting timed out. Skipping SIGKILL. Ignoring.");
1409 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1413 case MOUNT_REMOUNTING_SIGKILL
:
1414 mount_set_reload_result(m
, MOUNT_FAILURE_TIMEOUT
);
1416 log_unit_warning(UNIT(m
), "Mount process still around after SIGKILL. Ignoring.");
1417 mount_enter_dead_or_mounted(m
, MOUNT_SUCCESS
);
1420 case MOUNT_UNMOUNTING
:
1421 log_unit_warning(UNIT(m
), "Unmounting timed out. Terminating.");
1422 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1425 case MOUNT_UNMOUNTING_SIGTERM
:
1426 if (m
->kill_context
.send_sigkill
) {
1427 log_unit_warning(UNIT(m
), "Mount process timed out. Killing.");
1428 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_FAILURE_TIMEOUT
);
1430 log_unit_warning(UNIT(m
), "Mount process timed out. Skipping SIGKILL. Ignoring.");
1431 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1435 case MOUNT_UNMOUNTING_SIGKILL
:
1436 log_unit_warning(UNIT(m
), "Mount process still around after SIGKILL. Ignoring.");
1437 mount_enter_dead_or_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1441 assert_not_reached("Timeout at wrong time.");
1447 static int mount_setup_new_unit(
1452 const char *options
,
1454 MountProcFlags
*ret_flags
,
1457 _cleanup_(unit_freep
) Unit
*u
= NULL
;
1465 r
= unit_new_for_name(m
, sizeof(Mount
), name
, &u
);
1469 r
= free_and_strdup(&u
->source_path
, "/proc/self/mountinfo");
1473 r
= free_and_strdup(&MOUNT(u
)->where
, where
);
1477 r
= update_parameters_proc_self_mount_info(MOUNT(u
), what
, options
, fstype
);
1481 /* This unit was generated because /proc/self/mountinfo reported it. Remember this, so that by the time we load
1482 * the unit file for it (and thus add in extra deps right after) we know what source to attributes the deps
1484 MOUNT(u
)->from_proc_self_mountinfo
= true;
1486 /* We have only allocated the stub now, let's enqueue this unit for loading now, so that everything else is
1488 unit_add_to_load_queue(u
);
1490 *ret_flags
= MOUNT_PROC_IS_MOUNTED
| MOUNT_PROC_JUST_MOUNTED
| MOUNT_PROC_JUST_CHANGED
;
1495 static int mount_setup_existing_unit(
1499 const char *options
,
1501 MountProcFlags
*ret_flags
) {
1503 MountProcFlags flags
= MOUNT_PROC_IS_MOUNTED
;
1509 if (!MOUNT(u
)->where
) {
1510 MOUNT(u
)->where
= strdup(where
);
1511 if (!MOUNT(u
)->where
)
1515 r
= update_parameters_proc_self_mount_info(MOUNT(u
), what
, options
, fstype
);
1519 flags
|= MOUNT_PROC_JUST_CHANGED
;
1521 if (!MOUNT(u
)->from_proc_self_mountinfo
|| FLAGS_SET(MOUNT(u
)->proc_flags
, MOUNT_PROC_JUST_MOUNTED
))
1522 flags
|= MOUNT_PROC_JUST_MOUNTED
;
1524 MOUNT(u
)->from_proc_self_mountinfo
= true;
1526 if (IN_SET(u
->load_state
, UNIT_NOT_FOUND
, UNIT_BAD_SETTING
, UNIT_ERROR
)) {
1527 /* The unit was previously not found or otherwise not loaded. Now that the unit shows up in
1528 * /proc/self/mountinfo we should reconsider it this, hence set it to UNIT_LOADED. */
1529 u
->load_state
= UNIT_LOADED
;
1532 flags
|= MOUNT_PROC_JUST_CHANGED
;
1535 if (FLAGS_SET(flags
, MOUNT_PROC_JUST_CHANGED
)) {
1536 /* If things changed, then make sure that all deps are regenerated. Let's
1537 * first remove all automatic deps, and then add in the new ones. */
1539 unit_remove_dependencies(u
, UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT
);
1541 r
= mount_add_extras(MOUNT(u
));
1550 static int mount_setup_unit(
1554 const char *options
,
1558 _cleanup_free_
char *e
= NULL
;
1559 MountProcFlags flags
;
1569 /* Ignore API mount points. They should never be referenced in
1570 * dependencies ever. */
1571 if (mount_point_is_api(where
) || mount_point_ignore(where
))
1574 if (streq(fstype
, "autofs"))
1577 /* probably some kind of swap, ignore */
1578 if (!is_path(where
))
1581 r
= unit_name_from_path(where
, ".mount", &e
);
1583 return log_error_errno(r
, "Failed to generate unit name from path '%s': %m", where
);
1585 u
= manager_get_unit(m
, e
);
1587 r
= mount_setup_existing_unit(u
, what
, where
, options
, fstype
, &flags
);
1589 /* First time we see this mount point meaning that it's not been initiated by a mount unit but rather
1590 * by the sysadmin having called mount(8) directly. */
1591 r
= mount_setup_new_unit(m
, e
, what
, where
, options
, fstype
, &flags
, &u
);
1593 return log_warning_errno(r
, "Failed to set up mount unit: %m");
1595 /* If the mount changed properties or state, let's notify our clients */
1596 if (flags
& (MOUNT_PROC_JUST_CHANGED
|MOUNT_PROC_JUST_MOUNTED
))
1597 unit_add_to_dbus_queue(u
);
1600 MOUNT(u
)->proc_flags
= flags
;
1605 static int mount_load_proc_self_mountinfo(Manager
*m
, bool set_flags
) {
1606 _cleanup_(mnt_free_tablep
) struct libmnt_table
*t
= NULL
;
1607 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*i
= NULL
;
1612 t
= mnt_new_table();
1613 i
= mnt_new_iter(MNT_ITER_FORWARD
);
1617 r
= mnt_table_parse_mtab(t
, NULL
);
1619 return log_error_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
1622 struct libmnt_fs
*fs
;
1623 const char *device
, *path
, *options
, *fstype
;
1624 _cleanup_free_
char *d
= NULL
, *p
= NULL
;
1627 k
= mnt_table_next_fs(t
, i
, &fs
);
1631 return log_error_errno(k
, "Failed to get next entry from /proc/self/mountinfo: %m");
1633 device
= mnt_fs_get_source(fs
);
1634 path
= mnt_fs_get_target(fs
);
1635 options
= mnt_fs_get_options(fs
);
1636 fstype
= mnt_fs_get_fstype(fs
);
1638 if (!device
|| !path
)
1641 if (cunescape(device
, UNESCAPE_RELAX
, &d
) < 0)
1644 if (cunescape(path
, UNESCAPE_RELAX
, &p
) < 0)
1647 device_found_node(m
, d
, DEVICE_FOUND_MOUNT
, DEVICE_FOUND_MOUNT
);
1649 (void) mount_setup_unit(m
, d
, p
, options
, fstype
, set_flags
);
1655 static void mount_shutdown(Manager
*m
) {
1658 m
->mount_event_source
= sd_event_source_unref(m
->mount_event_source
);
1660 mnt_unref_monitor(m
->mount_monitor
);
1661 m
->mount_monitor
= NULL
;
1664 static int mount_get_timeout(Unit
*u
, usec_t
*timeout
) {
1665 Mount
*m
= MOUNT(u
);
1669 if (!m
->timer_event_source
)
1672 r
= sd_event_source_get_time(m
->timer_event_source
, &t
);
1675 if (t
== USEC_INFINITY
)
1682 static void mount_enumerate_perpetual(Manager
*m
) {
1688 /* Whatever happens, we know for sure that the root directory is around, and cannot go away. Let's
1689 * unconditionally synthesize it here and mark it as perpetual. */
1691 u
= manager_get_unit(m
, SPECIAL_ROOT_MOUNT
);
1693 r
= unit_new_for_name(m
, sizeof(Mount
), SPECIAL_ROOT_MOUNT
, &u
);
1695 log_error_errno(r
, "Failed to allocate the special " SPECIAL_ROOT_MOUNT
" unit: %m");
1700 u
->perpetual
= true;
1701 MOUNT(u
)->deserialized_state
= MOUNT_MOUNTED
;
1703 unit_add_to_load_queue(u
);
1704 unit_add_to_dbus_queue(u
);
1707 static bool mount_is_mounted(Mount
*m
) {
1710 return UNIT(m
)->perpetual
|| FLAGS_SET(m
->proc_flags
, MOUNT_PROC_IS_MOUNTED
);
1713 static void mount_enumerate(Manager
*m
) {
1720 if (!m
->mount_monitor
) {
1723 m
->mount_monitor
= mnt_new_monitor();
1724 if (!m
->mount_monitor
) {
1729 r
= mnt_monitor_enable_kernel(m
->mount_monitor
, 1);
1731 log_error_errno(r
, "Failed to enable watching of kernel mount events: %m");
1735 r
= mnt_monitor_enable_userspace(m
->mount_monitor
, 1, NULL
);
1737 log_error_errno(r
, "Failed to enable watching of userspace mount events: %m");
1741 /* mnt_unref_monitor() will close the fd */
1742 fd
= r
= mnt_monitor_get_fd(m
->mount_monitor
);
1744 log_error_errno(r
, "Failed to acquire watch file descriptor: %m");
1748 r
= sd_event_add_io(m
->event
, &m
->mount_event_source
, fd
, EPOLLIN
, mount_dispatch_io
, m
);
1750 log_error_errno(r
, "Failed to watch mount file descriptor: %m");
1754 r
= sd_event_source_set_priority(m
->mount_event_source
, SD_EVENT_PRIORITY_NORMAL
-10);
1756 log_error_errno(r
, "Failed to adjust mount watch priority: %m");
1760 (void) sd_event_source_set_description(m
->mount_event_source
, "mount-monitor-dispatch");
1763 r
= mount_load_proc_self_mountinfo(m
, false);
1773 static int mount_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
1774 _cleanup_set_free_free_ Set
*around
= NULL
, *gone
= NULL
;
1775 Manager
*m
= userdata
;
1782 assert(revents
& EPOLLIN
);
1784 if (fd
== mnt_monitor_get_fd(m
->mount_monitor
)) {
1785 bool rescan
= false;
1787 /* Drain all events and verify that the event is valid.
1789 * Note that libmount also monitors /run/mount mkdir if the
1790 * directory does not exist yet. The mkdir may generate event
1791 * which is irrelevant for us.
1793 * error: r < 0; valid: r == 0, false positive: rc == 1 */
1795 r
= mnt_monitor_next_change(m
->mount_monitor
, NULL
, NULL
);
1799 return log_error_errno(r
, "Failed to drain libmount events: %m");
1802 log_debug("libmount event [rescan: %s]", yes_no(rescan
));
1807 r
= mount_load_proc_self_mountinfo(m
, true);
1809 /* Reset flags, just in case, for later calls */
1810 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
])
1811 MOUNT(u
)->proc_flags
= 0;
1816 manager_dispatch_load_queue(m
);
1818 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
]) {
1819 Mount
*mount
= MOUNT(u
);
1821 if (!mount_is_mounted(mount
)) {
1823 /* A mount point is not around right now. It
1824 * might be gone, or might never have
1827 if (mount
->from_proc_self_mountinfo
&&
1828 mount
->parameters_proc_self_mountinfo
.what
) {
1830 /* Remember that this device might just have disappeared */
1831 if (set_ensure_allocated(&gone
, &path_hash_ops
) < 0 ||
1832 set_put_strdup(gone
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
1833 log_oom(); /* we don't care too much about OOM here... */
1836 mount
->from_proc_self_mountinfo
= false;
1837 assert_se(update_parameters_proc_self_mount_info(mount
, NULL
, NULL
, NULL
) >= 0);
1839 switch (mount
->state
) {
1842 /* This has just been unmounted by somebody else, follow the state change. */
1843 mount_enter_dead(mount
, MOUNT_SUCCESS
);
1850 } else if (mount
->proc_flags
& (MOUNT_PROC_JUST_MOUNTED
|MOUNT_PROC_JUST_CHANGED
)) {
1852 /* A mount point was added or changed */
1854 switch (mount
->state
) {
1859 /* This has just been mounted by somebody else, follow the state change, but let's
1860 * generate a new invocation ID for this implicitly and automatically. */
1861 (void) unit_acquire_invocation_id(u
);
1862 mount_cycle_clear(mount
);
1863 mount_enter_mounted(mount
, MOUNT_SUCCESS
);
1866 case MOUNT_MOUNTING
:
1867 mount_set_state(mount
, MOUNT_MOUNTING_DONE
);
1871 /* Nothing really changed, but let's
1872 * issue an notification call
1873 * nonetheless, in case somebody is
1874 * waiting for this. (e.g. file system
1875 * ro/rw remounts.) */
1876 mount_set_state(mount
, mount
->state
);
1881 if (mount_is_mounted(mount
) &&
1882 mount
->from_proc_self_mountinfo
&&
1883 mount
->parameters_proc_self_mountinfo
.what
) {
1884 /* Track devices currently used */
1886 if (set_ensure_allocated(&around
, &path_hash_ops
) < 0 ||
1887 set_put_strdup(around
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
1891 /* Reset the flags for later calls */
1892 mount
->proc_flags
= 0;
1895 SET_FOREACH(what
, gone
, i
) {
1896 if (set_contains(around
, what
))
1899 /* Let the device units know that the device is no longer mounted */
1900 device_found_node(m
, what
, 0, DEVICE_FOUND_MOUNT
);
1906 static void mount_reset_failed(Unit
*u
) {
1907 Mount
*m
= MOUNT(u
);
1911 if (m
->state
== MOUNT_FAILED
)
1912 mount_set_state(m
, MOUNT_DEAD
);
1914 m
->result
= MOUNT_SUCCESS
;
1915 m
->reload_result
= MOUNT_SUCCESS
;
1918 static int mount_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
1919 Mount
*m
= MOUNT(u
);
1923 return unit_kill_common(u
, who
, signo
, -1, MOUNT(u
)->control_pid
, error
);
1926 static int mount_control_pid(Unit
*u
) {
1927 Mount
*m
= MOUNT(u
);
1931 return m
->control_pid
;
1934 static const char* const mount_exec_command_table
[_MOUNT_EXEC_COMMAND_MAX
] = {
1935 [MOUNT_EXEC_MOUNT
] = "ExecMount",
1936 [MOUNT_EXEC_UNMOUNT
] = "ExecUnmount",
1937 [MOUNT_EXEC_REMOUNT
] = "ExecRemount",
1940 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command
, MountExecCommand
);
1942 static const char* const mount_result_table
[_MOUNT_RESULT_MAX
] = {
1943 [MOUNT_SUCCESS
] = "success",
1944 [MOUNT_FAILURE_RESOURCES
] = "resources",
1945 [MOUNT_FAILURE_TIMEOUT
] = "timeout",
1946 [MOUNT_FAILURE_EXIT_CODE
] = "exit-code",
1947 [MOUNT_FAILURE_SIGNAL
] = "signal",
1948 [MOUNT_FAILURE_CORE_DUMP
] = "core-dump",
1949 [MOUNT_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
1950 [MOUNT_FAILURE_PROTOCOL
] = "protocol",
1953 DEFINE_STRING_TABLE_LOOKUP(mount_result
, MountResult
);
1955 const UnitVTable mount_vtable
= {
1956 .object_size
= sizeof(Mount
),
1957 .exec_context_offset
= offsetof(Mount
, exec_context
),
1958 .cgroup_context_offset
= offsetof(Mount
, cgroup_context
),
1959 .kill_context_offset
= offsetof(Mount
, kill_context
),
1960 .exec_runtime_offset
= offsetof(Mount
, exec_runtime
),
1961 .dynamic_creds_offset
= offsetof(Mount
, dynamic_creds
),
1967 .private_section
= "Mount",
1973 .coldplug
= mount_coldplug
,
1977 .start
= mount_start
,
1979 .reload
= mount_reload
,
1983 .serialize
= mount_serialize
,
1984 .deserialize_item
= mount_deserialize_item
,
1986 .active_state
= mount_active_state
,
1987 .sub_state_to_string
= mount_sub_state_to_string
,
1989 .may_gc
= mount_may_gc
,
1991 .sigchld_event
= mount_sigchld_event
,
1993 .reset_failed
= mount_reset_failed
,
1995 .control_pid
= mount_control_pid
,
1997 .bus_vtable
= bus_mount_vtable
,
1998 .bus_set_property
= bus_mount_set_property
,
1999 .bus_commit_properties
= bus_mount_commit_properties
,
2001 .get_timeout
= mount_get_timeout
,
2003 .can_transient
= true,
2005 .enumerate_perpetual
= mount_enumerate_perpetual
,
2006 .enumerate
= mount_enumerate
,
2007 .shutdown
= mount_shutdown
,
2009 .status_message_formats
= {
2010 .starting_stopping
= {
2011 [0] = "Mounting %s...",
2012 [1] = "Unmounting %s...",
2014 .finished_start_job
= {
2015 [JOB_DONE
] = "Mounted %s.",
2016 [JOB_FAILED
] = "Failed to mount %s.",
2017 [JOB_TIMEOUT
] = "Timed out mounting %s.",
2019 .finished_stop_job
= {
2020 [JOB_DONE
] = "Unmounted %s.",
2021 [JOB_FAILED
] = "Failed unmounting %s.",
2022 [JOB_TIMEOUT
] = "Timed out unmounting %s.",