2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/epoll.h>
25 #include "sd-messages.h"
27 #include "alloc-util.h"
28 #include "dbus-mount.h"
30 #include "exit-status.h"
31 #include "formats-util.h"
32 #include "fstab-util.h"
36 #include "mount-setup.h"
37 #include "mount-util.h"
39 #include "parse-util.h"
40 #include "path-util.h"
41 #include "process-util.h"
43 #include "string-table.h"
44 #include "string-util.h"
46 #include "unit-name.h"
49 #define RETRY_UMOUNT_MAX 32
51 DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_table
*, mnt_free_table
);
52 DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_iter
*, mnt_free_iter
);
54 static const UnitActiveState state_translation_table
[_MOUNT_STATE_MAX
] = {
55 [MOUNT_DEAD
] = UNIT_INACTIVE
,
56 [MOUNT_MOUNTING
] = UNIT_ACTIVATING
,
57 [MOUNT_MOUNTING_DONE
] = UNIT_ACTIVE
,
58 [MOUNT_MOUNTED
] = UNIT_ACTIVE
,
59 [MOUNT_REMOUNTING
] = UNIT_RELOADING
,
60 [MOUNT_UNMOUNTING
] = UNIT_DEACTIVATING
,
61 [MOUNT_MOUNTING_SIGTERM
] = UNIT_DEACTIVATING
,
62 [MOUNT_MOUNTING_SIGKILL
] = UNIT_DEACTIVATING
,
63 [MOUNT_REMOUNTING_SIGTERM
] = UNIT_RELOADING
,
64 [MOUNT_REMOUNTING_SIGKILL
] = UNIT_RELOADING
,
65 [MOUNT_UNMOUNTING_SIGTERM
] = UNIT_DEACTIVATING
,
66 [MOUNT_UNMOUNTING_SIGKILL
] = UNIT_DEACTIVATING
,
67 [MOUNT_FAILED
] = UNIT_FAILED
70 static int mount_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
71 static int mount_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
);
73 static bool mount_needs_network(const char *options
, const char *fstype
) {
74 if (fstab_test_option(options
, "_netdev\0"))
77 if (fstype
&& fstype_is_network(fstype
))
83 static bool mount_is_network(const MountParameters
*p
) {
86 return mount_needs_network(p
->options
, p
->fstype
);
89 static bool mount_is_loop(const MountParameters
*p
) {
92 if (fstab_test_option(p
->options
, "loop\0"))
98 static bool mount_is_bind(const MountParameters
*p
) {
101 if (fstab_test_option(p
->options
, "bind\0" "rbind\0"))
104 if (p
->fstype
&& STR_IN_SET(p
->fstype
, "bind", "rbind"))
110 static bool mount_is_auto(const MountParameters
*p
) {
113 return !fstab_test_option(p
->options
, "noauto\0");
116 static bool mount_is_automount(const MountParameters
*p
) {
119 return fstab_test_option(p
->options
,
120 "comment=systemd.automount\0"
121 "x-systemd.automount\0");
124 static bool needs_quota(const MountParameters
*p
) {
127 /* Quotas are not enabled on network filesystems,
128 * but we want them, for example, on storage connected via iscsi */
129 if (p
->fstype
&& fstype_is_network(p
->fstype
))
132 if (mount_is_bind(p
))
135 return fstab_test_option(p
->options
,
136 "usrquota\0" "grpquota\0" "quota\0" "usrjquota\0" "grpjquota\0");
139 static void mount_init(Unit
*u
) {
143 assert(u
->load_state
== UNIT_STUB
);
145 m
->timeout_usec
= u
->manager
->default_timeout_start_usec
;
146 m
->directory_mode
= 0755;
148 if (unit_has_name(u
, "-.mount")) {
149 /* Don't allow start/stop for root directory */
150 u
->refuse_manual_start
= true;
151 u
->refuse_manual_stop
= true;
153 /* The stdio/kmsg bridge socket is on /, in order to avoid a
154 * dep loop, don't use kmsg logging for -.mount */
155 m
->exec_context
.std_output
= u
->manager
->default_std_output
;
156 m
->exec_context
.std_error
= u
->manager
->default_std_error
;
159 /* We need to make sure that /usr/bin/mount is always called
160 * in the same process group as us, so that the autofs kernel
161 * side doesn't send us another mount request while we are
162 * already trying to comply its last one. */
163 m
->exec_context
.same_pgrp
= true;
165 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
167 u
->ignore_on_isolate
= true;
170 static int mount_arm_timer(Mount
*m
, usec_t usec
) {
175 if (m
->timer_event_source
) {
176 r
= sd_event_source_set_time(m
->timer_event_source
, usec
);
180 return sd_event_source_set_enabled(m
->timer_event_source
, SD_EVENT_ONESHOT
);
183 if (usec
== USEC_INFINITY
)
186 r
= sd_event_add_time(
187 UNIT(m
)->manager
->event
,
188 &m
->timer_event_source
,
191 mount_dispatch_timer
, m
);
195 (void) sd_event_source_set_description(m
->timer_event_source
, "mount-timer");
200 static void mount_unwatch_control_pid(Mount
*m
) {
203 if (m
->control_pid
<= 0)
206 unit_unwatch_pid(UNIT(m
), m
->control_pid
);
210 static void mount_parameters_done(MountParameters
*p
) {
217 p
->what
= p
->options
= p
->fstype
= NULL
;
220 static void mount_done(Unit
*u
) {
225 m
->where
= mfree(m
->where
);
227 mount_parameters_done(&m
->parameters_proc_self_mountinfo
);
228 mount_parameters_done(&m
->parameters_fragment
);
230 m
->exec_runtime
= exec_runtime_unref(m
->exec_runtime
);
231 exec_command_done_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
232 m
->control_command
= NULL
;
234 mount_unwatch_control_pid(m
);
236 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
239 _pure_
static MountParameters
* get_mount_parameters_fragment(Mount
*m
) {
242 if (m
->from_fragment
)
243 return &m
->parameters_fragment
;
248 _pure_
static MountParameters
* get_mount_parameters(Mount
*m
) {
251 if (m
->from_proc_self_mountinfo
)
252 return &m
->parameters_proc_self_mountinfo
;
254 return get_mount_parameters_fragment(m
);
257 static int mount_add_mount_links(Mount
*m
) {
258 _cleanup_free_
char *parent
= NULL
;
267 if (!path_equal(m
->where
, "/")) {
268 /* Adds in links to other mount points that might lie further
269 * up in the hierarchy */
271 parent
= dirname_malloc(m
->where
);
275 r
= unit_require_mounts_for(UNIT(m
), parent
);
280 /* Adds in links to other mount points that might be needed
281 * for the source path (if this is a bind mount or a loop mount) to be
283 pm
= get_mount_parameters_fragment(m
);
284 if (pm
&& pm
->what
&&
285 path_is_absolute(pm
->what
) &&
286 (mount_is_bind(pm
) || mount_is_loop(pm
) || !mount_is_network(pm
))) {
288 r
= unit_require_mounts_for(UNIT(m
), pm
->what
);
293 /* Adds in links to other units that use this path or paths
294 * further down in the hierarchy */
295 s
= manager_get_units_requiring_mounts_for(UNIT(m
)->manager
, m
->where
);
296 SET_FOREACH(other
, s
, i
) {
298 if (other
->load_state
!= UNIT_LOADED
)
301 if (other
== UNIT(m
))
304 r
= unit_add_dependency(other
, UNIT_AFTER
, UNIT(m
), true);
308 if (UNIT(m
)->fragment_path
) {
309 /* If we have fragment configuration, then make this dependency required */
310 r
= unit_add_dependency(other
, UNIT_REQUIRES
, UNIT(m
), true);
319 static int mount_add_device_links(Mount
*m
) {
321 bool device_wants_mount
= false;
326 p
= get_mount_parameters(m
);
333 if (mount_is_bind(p
))
336 if (!is_device_path(p
->what
))
339 /* /dev/root is a really weird thing, it's not a real device,
340 * but just a path the kernel exports for the root file system
341 * specified on the kernel command line. Ignore it here. */
342 if (path_equal(p
->what
, "/dev/root"))
345 if (path_equal(m
->where
, "/"))
348 if (mount_is_auto(p
) && !mount_is_automount(p
) && MANAGER_IS_SYSTEM(UNIT(m
)->manager
))
349 device_wants_mount
= true;
351 r
= unit_add_node_link(UNIT(m
), p
->what
, device_wants_mount
, m
->from_fragment
? UNIT_BINDS_TO
: UNIT_REQUIRES
);
358 static int mount_add_quota_links(Mount
*m
) {
364 if (!MANAGER_IS_SYSTEM(UNIT(m
)->manager
))
367 p
= get_mount_parameters_fragment(m
);
374 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTACHECK_SERVICE
, NULL
, true);
378 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTAON_SERVICE
, NULL
, true);
385 static bool should_umount(Mount
*m
) {
388 if (PATH_IN_SET(m
->where
, "/", "/usr") ||
389 path_startswith(m
->where
, "/run/initramfs"))
392 p
= get_mount_parameters(m
);
393 if (p
&& fstab_test_option(p
->options
, "x-initrd.mount\0") &&
400 static int mount_add_default_dependencies(Mount
*m
) {
407 if (!UNIT(m
)->default_dependencies
)
410 if (!MANAGER_IS_SYSTEM(UNIT(m
)->manager
))
413 /* We do not add any default dependencies to /, /usr or
414 * /run/initramfs/, since they are guaranteed to stay
415 * mounted the whole time, since our system is on it.
416 * Also, don't bother with anything mounted below virtual
417 * file systems, it's also going to be virtual, and hence
418 * not worth the effort. */
419 if (PATH_IN_SET(m
->where
, "/", "/usr") ||
420 path_startswith(m
->where
, "/run/initramfs") ||
421 path_startswith(m
->where
, "/proc") ||
422 path_startswith(m
->where
, "/sys") ||
423 path_startswith(m
->where
, "/dev"))
426 p
= get_mount_parameters(m
);
430 if (mount_is_network(p
)) {
431 /* We order ourselves after network.target. This is
432 * primarily useful at shutdown: services that take
433 * down the network should order themselves before
434 * network.target, so that they are shut down only
435 * after this mount unit is stopped. */
437 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, SPECIAL_NETWORK_TARGET
, NULL
, true);
441 /* We pull in network-online.target, and order
442 * ourselves after it. This is useful at start-up to
443 * actively pull in tools that want to be started
444 * before we start mounting network file systems, and
445 * whose purpose it is to delay this until the network
448 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_WANTS
, UNIT_AFTER
, SPECIAL_NETWORK_ONLINE_TARGET
, NULL
, true);
452 after
= SPECIAL_REMOTE_FS_PRE_TARGET
;
454 after
= SPECIAL_LOCAL_FS_PRE_TARGET
;
456 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, after
, NULL
, true);
460 if (should_umount(m
)) {
461 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, NULL
, true);
469 static int mount_verify(Mount
*m
) {
470 _cleanup_free_
char *e
= NULL
;
475 if (UNIT(m
)->load_state
!= UNIT_LOADED
)
478 if (!m
->from_fragment
&& !m
->from_proc_self_mountinfo
)
481 r
= unit_name_from_path(m
->where
, ".mount", &e
);
483 return log_unit_error_errno(UNIT(m
), r
, "Failed to generate unit name from mount path: %m");
485 if (!unit_has_name(UNIT(m
), e
)) {
486 log_unit_error(UNIT(m
), "Where= setting doesn't match unit name. Refusing.");
490 if (mount_point_is_api(m
->where
) || mount_point_ignore(m
->where
)) {
491 log_unit_error(UNIT(m
), "Cannot create mount unit for API file system %s. Refusing.", m
->where
);
495 if (UNIT(m
)->fragment_path
&& !m
->parameters_fragment
.what
) {
496 log_unit_error(UNIT(m
), "What= setting is missing. Refusing.");
500 if (m
->exec_context
.pam_name
&& m
->kill_context
.kill_mode
!= KILL_CONTROL_GROUP
) {
501 log_unit_error(UNIT(m
), "Unit has PAM enabled. Kill mode must be set to control-group'. Refusing.");
508 static int mount_add_extras(Mount
*m
) {
514 if (u
->fragment_path
)
515 m
->from_fragment
= true;
518 r
= unit_name_to_path(u
->id
, &m
->where
);
523 path_kill_slashes(m
->where
);
525 if (!u
->description
) {
526 r
= unit_set_description(u
, m
->where
);
531 r
= mount_add_device_links(m
);
535 r
= mount_add_mount_links(m
);
539 r
= mount_add_quota_links(m
);
543 r
= unit_patch_contexts(u
);
547 r
= unit_add_exec_dependencies(u
, &m
->exec_context
);
551 r
= unit_set_default_slice(u
);
555 r
= mount_add_default_dependencies(m
);
562 static int mount_load(Unit
*u
) {
567 assert(u
->load_state
== UNIT_STUB
);
569 if (m
->from_proc_self_mountinfo
)
570 r
= unit_load_fragment_and_dropin_optional(u
);
572 r
= unit_load_fragment_and_dropin(u
);
577 /* This is a new unit? Then let's add in some extras */
578 if (u
->load_state
== UNIT_LOADED
) {
579 r
= mount_add_extras(m
);
584 return mount_verify(m
);
587 static int mount_notify_automount(Mount
*m
, MountState old_state
, MountState state
) {
594 SET_FOREACH(p
, UNIT(m
)->dependencies
[UNIT_TRIGGERED_BY
], i
)
595 if (p
->type
== UNIT_AUTOMOUNT
) {
596 r
= automount_update_mount(AUTOMOUNT(p
), old_state
, state
);
604 static void mount_set_state(Mount
*m
, MountState state
) {
605 MountState old_state
;
608 old_state
= m
->state
;
611 if (state
!= MOUNT_MOUNTING
&&
612 state
!= MOUNT_MOUNTING_DONE
&&
613 state
!= MOUNT_REMOUNTING
&&
614 state
!= MOUNT_UNMOUNTING
&&
615 state
!= MOUNT_MOUNTING_SIGTERM
&&
616 state
!= MOUNT_MOUNTING_SIGKILL
&&
617 state
!= MOUNT_UNMOUNTING_SIGTERM
&&
618 state
!= MOUNT_UNMOUNTING_SIGKILL
&&
619 state
!= MOUNT_REMOUNTING_SIGTERM
&&
620 state
!= MOUNT_REMOUNTING_SIGKILL
) {
621 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
622 mount_unwatch_control_pid(m
);
623 m
->control_command
= NULL
;
624 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
627 mount_notify_automount(m
, old_state
, state
);
629 if (state
!= old_state
)
630 log_unit_debug(UNIT(m
), "Changed %s -> %s", mount_state_to_string(old_state
), mount_state_to_string(state
));
632 unit_notify(UNIT(m
), state_translation_table
[old_state
], state_translation_table
[state
], m
->reload_result
== MOUNT_SUCCESS
);
633 m
->reload_result
= MOUNT_SUCCESS
;
636 static int mount_coldplug(Unit
*u
) {
638 MountState new_state
= MOUNT_DEAD
;
642 assert(m
->state
== MOUNT_DEAD
);
644 if (m
->deserialized_state
!= m
->state
)
645 new_state
= m
->deserialized_state
;
646 else if (m
->from_proc_self_mountinfo
)
647 new_state
= MOUNT_MOUNTED
;
649 if (new_state
== m
->state
)
652 if (m
->control_pid
> 0 &&
653 pid_is_unwaited(m
->control_pid
) &&
659 MOUNT_MOUNTING_SIGTERM
,
660 MOUNT_MOUNTING_SIGKILL
,
661 MOUNT_UNMOUNTING_SIGTERM
,
662 MOUNT_UNMOUNTING_SIGKILL
,
663 MOUNT_REMOUNTING_SIGTERM
,
664 MOUNT_REMOUNTING_SIGKILL
)) {
666 r
= unit_watch_pid(UNIT(m
), m
->control_pid
);
670 r
= mount_arm_timer(m
, usec_add(u
->state_change_timestamp
.monotonic
, m
->timeout_usec
));
675 mount_set_state(m
, new_state
);
679 static void mount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
686 p
= get_mount_parameters(m
);
689 "%sMount State: %s\n"
693 "%sFile System Type: %s\n"
695 "%sFrom /proc/self/mountinfo: %s\n"
696 "%sFrom fragment: %s\n"
697 "%sDirectoryMode: %04o\n",
698 prefix
, mount_state_to_string(m
->state
),
699 prefix
, mount_result_to_string(m
->result
),
701 prefix
, p
? strna(p
->what
) : "n/a",
702 prefix
, p
? strna(p
->fstype
) : "n/a",
703 prefix
, p
? strna(p
->options
) : "n/a",
704 prefix
, yes_no(m
->from_proc_self_mountinfo
),
705 prefix
, yes_no(m
->from_fragment
),
706 prefix
, m
->directory_mode
);
708 if (m
->control_pid
> 0)
710 "%sControl PID: "PID_FMT
"\n",
711 prefix
, m
->control_pid
);
713 exec_context_dump(&m
->exec_context
, f
, prefix
);
714 kill_context_dump(&m
->kill_context
, f
, prefix
);
717 static int mount_spawn(Mount
*m
, ExecCommand
*c
, pid_t
*_pid
) {
720 ExecParameters exec_params
= {
721 .apply_permissions
= true,
722 .apply_chroot
= true,
723 .apply_tty_stdin
= true,
733 (void) unit_realize_cgroup(UNIT(m
));
734 if (m
->reset_cpu_usage
) {
735 (void) unit_reset_cpu_usage(UNIT(m
));
736 m
->reset_cpu_usage
= false;
739 r
= unit_setup_exec_runtime(UNIT(m
));
743 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->timeout_usec
));
747 exec_params
.environment
= UNIT(m
)->manager
->environment
;
748 exec_params
.confirm_spawn
= UNIT(m
)->manager
->confirm_spawn
;
749 exec_params
.cgroup_supported
= UNIT(m
)->manager
->cgroup_supported
;
750 exec_params
.cgroup_path
= UNIT(m
)->cgroup_path
;
751 exec_params
.cgroup_delegate
= m
->cgroup_context
.delegate
;
752 exec_params
.runtime_prefix
= manager_get_runtime_prefix(UNIT(m
)->manager
);
754 r
= exec_spawn(UNIT(m
),
763 r
= unit_watch_pid(UNIT(m
), pid
);
765 /* FIXME: we need to do something here */
773 static void mount_enter_dead(Mount
*m
, MountResult f
) {
776 if (f
!= MOUNT_SUCCESS
)
779 exec_runtime_destroy(m
->exec_runtime
);
780 m
->exec_runtime
= exec_runtime_unref(m
->exec_runtime
);
782 exec_context_destroy_runtime_directory(&m
->exec_context
, manager_get_runtime_prefix(UNIT(m
)->manager
));
784 mount_set_state(m
, m
->result
!= MOUNT_SUCCESS
? MOUNT_FAILED
: MOUNT_DEAD
);
787 static void mount_enter_mounted(Mount
*m
, MountResult f
) {
790 if (f
!= MOUNT_SUCCESS
)
793 mount_set_state(m
, MOUNT_MOUNTED
);
796 static void mount_enter_signal(Mount
*m
, MountState state
, MountResult f
) {
801 if (f
!= MOUNT_SUCCESS
)
804 r
= unit_kill_context(
807 (state
!= MOUNT_MOUNTING_SIGTERM
&& state
!= MOUNT_UNMOUNTING_SIGTERM
&& state
!= MOUNT_REMOUNTING_SIGTERM
) ?
808 KILL_KILL
: KILL_TERMINATE
,
816 r
= mount_arm_timer(m
, usec_add(now(CLOCK_MONOTONIC
), m
->timeout_usec
));
820 mount_set_state(m
, state
);
821 } else if (state
== MOUNT_REMOUNTING_SIGTERM
)
822 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
823 else if (state
== MOUNT_REMOUNTING_SIGKILL
)
824 mount_enter_mounted(m
, MOUNT_SUCCESS
);
825 else if (state
== MOUNT_MOUNTING_SIGTERM
)
826 mount_enter_signal(m
, MOUNT_MOUNTING_SIGKILL
, MOUNT_SUCCESS
);
827 else if (state
== MOUNT_UNMOUNTING_SIGTERM
)
828 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
830 mount_enter_dead(m
, MOUNT_SUCCESS
);
835 log_unit_warning_errno(UNIT(m
), r
, "Failed to kill processes: %m");
837 if (state
== MOUNT_REMOUNTING_SIGTERM
|| state
== MOUNT_REMOUNTING_SIGKILL
)
838 mount_enter_mounted(m
, MOUNT_FAILURE_RESOURCES
);
840 mount_enter_dead(m
, MOUNT_FAILURE_RESOURCES
);
843 static void mount_enter_unmounting(Mount
*m
) {
848 /* Start counting our attempts */
849 if (!IN_SET(m
->state
,
851 MOUNT_UNMOUNTING_SIGTERM
,
852 MOUNT_UNMOUNTING_SIGKILL
))
853 m
->n_retry_umount
= 0;
855 m
->control_command_id
= MOUNT_EXEC_UNMOUNT
;
856 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_UNMOUNT
;
858 r
= exec_command_set(m
->control_command
, UMOUNT_PATH
, m
->where
, NULL
);
862 mount_unwatch_control_pid(m
);
864 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
868 mount_set_state(m
, MOUNT_UNMOUNTING
);
873 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'umount' task: %m");
874 mount_enter_mounted(m
, MOUNT_FAILURE_RESOURCES
);
877 static int mount_get_opts(Mount
*m
, char **ret
) {
878 return fstab_filter_options(m
->parameters_fragment
.options
,
879 "nofail\0" "noauto\0" "auto\0", NULL
, NULL
, ret
);
882 static void mount_enter_mounting(Mount
*m
) {
888 m
->control_command_id
= MOUNT_EXEC_MOUNT
;
889 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_MOUNT
;
891 r
= unit_fail_if_symlink(UNIT(m
), m
->where
);
895 (void) mkdir_p_label(m
->where
, m
->directory_mode
);
897 unit_warn_if_dir_nonempty(UNIT(m
), m
->where
);
899 /* Create the source directory for bind-mounts if needed */
900 p
= get_mount_parameters_fragment(m
);
901 if (p
&& mount_is_bind(p
))
902 (void) mkdir_p_label(p
->what
, m
->directory_mode
);
904 if (m
->from_fragment
) {
905 _cleanup_free_
char *opts
= NULL
;
907 r
= mount_get_opts(m
, &opts
);
911 r
= exec_command_set(m
->control_command
, MOUNT_PATH
,
912 m
->parameters_fragment
.what
, m
->where
, NULL
);
913 if (r
>= 0 && m
->sloppy_options
)
914 r
= exec_command_append(m
->control_command
, "-s", NULL
);
915 if (r
>= 0 && m
->parameters_fragment
.fstype
)
916 r
= exec_command_append(m
->control_command
, "-t", m
->parameters_fragment
.fstype
, NULL
);
917 if (r
>= 0 && !isempty(opts
))
918 r
= exec_command_append(m
->control_command
, "-o", opts
, NULL
);
925 mount_unwatch_control_pid(m
);
927 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
931 mount_set_state(m
, MOUNT_MOUNTING
);
936 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'mount' task: %m");
937 mount_enter_dead(m
, MOUNT_FAILURE_RESOURCES
);
940 static void mount_enter_remounting(Mount
*m
) {
945 m
->control_command_id
= MOUNT_EXEC_REMOUNT
;
946 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_REMOUNT
;
948 if (m
->from_fragment
) {
951 if (m
->parameters_fragment
.options
)
952 o
= strjoina("remount,", m
->parameters_fragment
.options
);
956 r
= exec_command_set(m
->control_command
, MOUNT_PATH
,
957 m
->parameters_fragment
.what
, m
->where
,
959 if (r
>= 0 && m
->sloppy_options
)
960 r
= exec_command_append(m
->control_command
, "-s", NULL
);
961 if (r
>= 0 && m
->parameters_fragment
.fstype
)
962 r
= exec_command_append(m
->control_command
, "-t", m
->parameters_fragment
.fstype
, NULL
);
969 mount_unwatch_control_pid(m
);
971 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
975 mount_set_state(m
, MOUNT_REMOUNTING
);
980 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'remount' task: %m");
981 m
->reload_result
= MOUNT_FAILURE_RESOURCES
;
982 mount_enter_mounted(m
, MOUNT_SUCCESS
);
985 static int mount_start(Unit
*u
) {
990 /* We cannot fulfill this request right now, try again later
992 if (m
->state
== MOUNT_UNMOUNTING
||
993 m
->state
== MOUNT_UNMOUNTING_SIGTERM
||
994 m
->state
== MOUNT_UNMOUNTING_SIGKILL
||
995 m
->state
== MOUNT_MOUNTING_SIGTERM
||
996 m
->state
== MOUNT_MOUNTING_SIGKILL
)
1000 if (m
->state
== MOUNT_MOUNTING
)
1003 assert(m
->state
== MOUNT_DEAD
|| m
->state
== MOUNT_FAILED
);
1005 m
->result
= MOUNT_SUCCESS
;
1006 m
->reload_result
= MOUNT_SUCCESS
;
1007 m
->reset_cpu_usage
= true;
1009 mount_enter_mounting(m
);
1013 static int mount_stop(Unit
*u
) {
1014 Mount
*m
= MOUNT(u
);
1019 if (m
->state
== MOUNT_UNMOUNTING
||
1020 m
->state
== MOUNT_UNMOUNTING_SIGKILL
||
1021 m
->state
== MOUNT_UNMOUNTING_SIGTERM
||
1022 m
->state
== MOUNT_MOUNTING_SIGTERM
||
1023 m
->state
== MOUNT_MOUNTING_SIGKILL
)
1026 assert(m
->state
== MOUNT_MOUNTING
||
1027 m
->state
== MOUNT_MOUNTING_DONE
||
1028 m
->state
== MOUNT_MOUNTED
||
1029 m
->state
== MOUNT_REMOUNTING
||
1030 m
->state
== MOUNT_REMOUNTING_SIGTERM
||
1031 m
->state
== MOUNT_REMOUNTING_SIGKILL
);
1033 mount_enter_unmounting(m
);
1037 static int mount_reload(Unit
*u
) {
1038 Mount
*m
= MOUNT(u
);
1042 if (m
->state
== MOUNT_MOUNTING_DONE
)
1045 assert(m
->state
== MOUNT_MOUNTED
);
1047 mount_enter_remounting(m
);
1051 static int mount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1052 Mount
*m
= MOUNT(u
);
1058 unit_serialize_item(u
, f
, "state", mount_state_to_string(m
->state
));
1059 unit_serialize_item(u
, f
, "result", mount_result_to_string(m
->result
));
1060 unit_serialize_item(u
, f
, "reload-result", mount_result_to_string(m
->reload_result
));
1062 if (m
->control_pid
> 0)
1063 unit_serialize_item_format(u
, f
, "control-pid", PID_FMT
, m
->control_pid
);
1065 if (m
->control_command_id
>= 0)
1066 unit_serialize_item(u
, f
, "control-command", mount_exec_command_to_string(m
->control_command_id
));
1071 static int mount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1072 Mount
*m
= MOUNT(u
);
1079 if (streq(key
, "state")) {
1082 if ((state
= mount_state_from_string(value
)) < 0)
1083 log_unit_debug(u
, "Failed to parse state value: %s", value
);
1085 m
->deserialized_state
= state
;
1086 } else if (streq(key
, "result")) {
1089 f
= mount_result_from_string(value
);
1091 log_unit_debug(u
, "Failed to parse result value: %s", value
);
1092 else if (f
!= MOUNT_SUCCESS
)
1095 } else if (streq(key
, "reload-result")) {
1098 f
= mount_result_from_string(value
);
1100 log_unit_debug(u
, "Failed to parse reload result value: %s", value
);
1101 else if (f
!= MOUNT_SUCCESS
)
1102 m
->reload_result
= f
;
1104 } else if (streq(key
, "control-pid")) {
1107 if (parse_pid(value
, &pid
) < 0)
1108 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
1110 m
->control_pid
= pid
;
1111 } else if (streq(key
, "control-command")) {
1112 MountExecCommand id
;
1114 id
= mount_exec_command_from_string(value
);
1116 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
1118 m
->control_command_id
= id
;
1119 m
->control_command
= m
->exec_command
+ id
;
1122 log_unit_debug(u
, "Unknown serialization key: %s", key
);
1127 _pure_
static UnitActiveState
mount_active_state(Unit
*u
) {
1130 return state_translation_table
[MOUNT(u
)->state
];
1133 _pure_
static const char *mount_sub_state_to_string(Unit
*u
) {
1136 return mount_state_to_string(MOUNT(u
)->state
);
1139 _pure_
static bool mount_check_gc(Unit
*u
) {
1140 Mount
*m
= MOUNT(u
);
1144 return m
->from_proc_self_mountinfo
;
1147 static void mount_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1148 Mount
*m
= MOUNT(u
);
1154 if (pid
!= m
->control_pid
)
1159 if (is_clean_exit(code
, status
, NULL
))
1161 else if (code
== CLD_EXITED
)
1162 f
= MOUNT_FAILURE_EXIT_CODE
;
1163 else if (code
== CLD_KILLED
)
1164 f
= MOUNT_FAILURE_SIGNAL
;
1165 else if (code
== CLD_DUMPED
)
1166 f
= MOUNT_FAILURE_CORE_DUMP
;
1168 assert_not_reached("Unknown code");
1170 if (f
!= MOUNT_SUCCESS
)
1173 if (m
->control_command
) {
1174 exec_status_exit(&m
->control_command
->exec_status
, &m
->exec_context
, pid
, code
, status
);
1176 m
->control_command
= NULL
;
1177 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
1180 log_unit_full(u
, f
== MOUNT_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
, 0,
1181 "Mount process exited, code=%s status=%i", sigchld_code_to_string(code
), status
);
1183 /* Note that mount(8) returning and the kernel sending us a
1184 * mount table change event might happen out-of-order. If an
1185 * operation succeed we assume the kernel will follow soon too
1186 * and already change into the resulting state. If it fails
1187 * we check if the kernel still knows about the mount. and
1188 * change state accordingly. */
1192 case MOUNT_MOUNTING
:
1193 case MOUNT_MOUNTING_DONE
:
1194 case MOUNT_MOUNTING_SIGKILL
:
1195 case MOUNT_MOUNTING_SIGTERM
:
1197 if (f
== MOUNT_SUCCESS
)
1198 mount_enter_mounted(m
, f
);
1199 else if (m
->from_proc_self_mountinfo
)
1200 mount_enter_mounted(m
, f
);
1202 mount_enter_dead(m
, f
);
1205 case MOUNT_REMOUNTING
:
1206 case MOUNT_REMOUNTING_SIGKILL
:
1207 case MOUNT_REMOUNTING_SIGTERM
:
1209 m
->reload_result
= f
;
1210 if (m
->from_proc_self_mountinfo
)
1211 mount_enter_mounted(m
, MOUNT_SUCCESS
);
1213 mount_enter_dead(m
, MOUNT_SUCCESS
);
1217 case MOUNT_UNMOUNTING
:
1218 case MOUNT_UNMOUNTING_SIGKILL
:
1219 case MOUNT_UNMOUNTING_SIGTERM
:
1221 if (f
== MOUNT_SUCCESS
) {
1223 if (m
->from_proc_self_mountinfo
) {
1225 /* Still a mount point? If so, let's
1226 * try again. Most likely there were
1227 * multiple mount points stacked on
1228 * top of each other. Note that due to
1229 * the io event priority logic we can
1230 * be sure the new mountinfo is loaded
1231 * before we process the SIGCHLD for
1232 * the mount command. */
1234 if (m
->n_retry_umount
< RETRY_UMOUNT_MAX
) {
1235 log_unit_debug(u
, "Mount still present, trying again.");
1236 m
->n_retry_umount
++;
1237 mount_enter_unmounting(m
);
1239 log_unit_debug(u
, "Mount still present after %u attempts to unmount, giving up.", m
->n_retry_umount
);
1240 mount_enter_mounted(m
, f
);
1243 mount_enter_dead(m
, f
);
1245 } else if (m
->from_proc_self_mountinfo
)
1246 mount_enter_mounted(m
, f
);
1248 mount_enter_dead(m
, f
);
1252 assert_not_reached("Uh, control process died at wrong time.");
1255 /* Notify clients about changed exit status */
1256 unit_add_to_dbus_queue(u
);
1259 static int mount_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
1260 Mount
*m
= MOUNT(userdata
);
1263 assert(m
->timer_event_source
== source
);
1267 case MOUNT_MOUNTING
:
1268 case MOUNT_MOUNTING_DONE
:
1269 log_unit_warning(UNIT(m
), "Mounting timed out. Stopping.");
1270 mount_enter_signal(m
, MOUNT_MOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1273 case MOUNT_REMOUNTING
:
1274 log_unit_warning(UNIT(m
), "Remounting timed out. Stopping.");
1275 m
->reload_result
= MOUNT_FAILURE_TIMEOUT
;
1276 mount_enter_mounted(m
, MOUNT_SUCCESS
);
1279 case MOUNT_UNMOUNTING
:
1280 log_unit_warning(UNIT(m
), "Unmounting timed out. Stopping.");
1281 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1284 case MOUNT_MOUNTING_SIGTERM
:
1285 if (m
->kill_context
.send_sigkill
) {
1286 log_unit_warning(UNIT(m
), "Mounting timed out. Killing.");
1287 mount_enter_signal(m
, MOUNT_MOUNTING_SIGKILL
, MOUNT_FAILURE_TIMEOUT
);
1289 log_unit_warning(UNIT(m
), "Mounting timed out. Skipping SIGKILL. Ignoring.");
1291 if (m
->from_proc_self_mountinfo
)
1292 mount_enter_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1294 mount_enter_dead(m
, MOUNT_FAILURE_TIMEOUT
);
1298 case MOUNT_REMOUNTING_SIGTERM
:
1299 if (m
->kill_context
.send_sigkill
) {
1300 log_unit_warning(UNIT(m
), "Remounting timed out. Killing.");
1301 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_FAILURE_TIMEOUT
);
1303 log_unit_warning(UNIT(m
), "Remounting timed out. Skipping SIGKILL. Ignoring.");
1305 if (m
->from_proc_self_mountinfo
)
1306 mount_enter_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1308 mount_enter_dead(m
, MOUNT_FAILURE_TIMEOUT
);
1312 case MOUNT_UNMOUNTING_SIGTERM
:
1313 if (m
->kill_context
.send_sigkill
) {
1314 log_unit_warning(UNIT(m
), "Unmounting timed out. Killing.");
1315 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_FAILURE_TIMEOUT
);
1317 log_unit_warning(UNIT(m
), "Unmounting timed out. Skipping SIGKILL. Ignoring.");
1319 if (m
->from_proc_self_mountinfo
)
1320 mount_enter_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1322 mount_enter_dead(m
, MOUNT_FAILURE_TIMEOUT
);
1326 case MOUNT_MOUNTING_SIGKILL
:
1327 case MOUNT_REMOUNTING_SIGKILL
:
1328 case MOUNT_UNMOUNTING_SIGKILL
:
1329 log_unit_warning(UNIT(m
),"Mount process still around after SIGKILL. Ignoring.");
1331 if (m
->from_proc_self_mountinfo
)
1332 mount_enter_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1334 mount_enter_dead(m
, MOUNT_FAILURE_TIMEOUT
);
1338 assert_not_reached("Timeout at wrong time.");
1344 static int mount_setup_unit(
1348 const char *options
,
1352 _cleanup_free_
char *e
= NULL
, *w
= NULL
, *o
= NULL
, *f
= NULL
;
1353 bool load_extras
= false;
1355 bool delete, changed
= false;
1365 /* Ignore API mount points. They should never be referenced in
1366 * dependencies ever. */
1367 if (mount_point_is_api(where
) || mount_point_ignore(where
))
1370 if (streq(fstype
, "autofs"))
1373 /* probably some kind of swap, ignore */
1374 if (!is_path(where
))
1377 r
= unit_name_from_path(where
, ".mount", &e
);
1381 u
= manager_get_unit(m
, e
);
1385 u
= unit_new(m
, sizeof(Mount
));
1389 r
= unit_add_name(u
, e
);
1393 MOUNT(u
)->where
= strdup(where
);
1394 if (!MOUNT(u
)->where
) {
1399 u
->source_path
= strdup("/proc/self/mountinfo");
1400 if (!u
->source_path
) {
1405 if (MANAGER_IS_SYSTEM(m
)) {
1408 target
= mount_needs_network(options
, fstype
) ? SPECIAL_REMOTE_FS_TARGET
: SPECIAL_LOCAL_FS_TARGET
;
1409 r
= unit_add_dependency_by_name(u
, UNIT_BEFORE
, target
, NULL
, true);
1413 if (should_umount(MOUNT(u
))) {
1414 r
= unit_add_dependency_by_name(u
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, NULL
, true);
1420 unit_add_to_load_queue(u
);
1425 if (!MOUNT(u
)->where
) {
1426 MOUNT(u
)->where
= strdup(where
);
1427 if (!MOUNT(u
)->where
) {
1433 if (MANAGER_IS_SYSTEM(m
) &&
1434 mount_needs_network(options
, fstype
)) {
1435 /* _netdev option may have shown up late, or on a
1436 * remount. Add remote-fs dependencies, even though
1437 * local-fs ones may already be there. */
1438 unit_add_dependency_by_name(u
, UNIT_BEFORE
, SPECIAL_REMOTE_FS_TARGET
, NULL
, true);
1442 if (u
->load_state
== UNIT_NOT_FOUND
) {
1443 u
->load_state
= UNIT_LOADED
;
1446 /* Load in the extras later on, after we
1447 * finished initialization of the unit */
1454 o
= strdup(options
);
1456 if (!w
|| !o
|| !f
) {
1461 p
= &MOUNT(u
)->parameters_proc_self_mountinfo
;
1463 changed
= changed
||
1464 !streq_ptr(p
->options
, options
) ||
1465 !streq_ptr(p
->what
, what
) ||
1466 !streq_ptr(p
->fstype
, fstype
);
1469 MOUNT(u
)->is_mounted
= true;
1470 MOUNT(u
)->just_mounted
= !MOUNT(u
)->from_proc_self_mountinfo
;
1471 MOUNT(u
)->just_changed
= changed
;
1474 MOUNT(u
)->from_proc_self_mountinfo
= true;
1489 r
= mount_add_extras(MOUNT(u
));
1495 unit_add_to_dbus_queue(u
);
1500 log_warning_errno(r
, "Failed to set up mount unit: %m");
1508 static int mount_load_proc_self_mountinfo(Manager
*m
, bool set_flags
) {
1509 _cleanup_(mnt_free_tablep
) struct libmnt_table
*t
= NULL
;
1510 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*i
= NULL
;
1515 t
= mnt_new_table();
1519 i
= mnt_new_iter(MNT_ITER_FORWARD
);
1523 r
= mnt_table_parse_mtab(t
, NULL
);
1525 return log_error_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
1529 const char *device
, *path
, *options
, *fstype
;
1530 _cleanup_free_
char *d
= NULL
, *p
= NULL
;
1531 struct libmnt_fs
*fs
;
1534 k
= mnt_table_next_fs(t
, i
, &fs
);
1538 return log_error_errno(k
, "Failed to get next entry from /proc/self/mountinfo: %m");
1540 device
= mnt_fs_get_source(fs
);
1541 path
= mnt_fs_get_target(fs
);
1542 options
= mnt_fs_get_options(fs
);
1543 fstype
= mnt_fs_get_fstype(fs
);
1545 if (!device
|| !path
)
1548 if (cunescape(device
, UNESCAPE_RELAX
, &d
) < 0)
1551 if (cunescape(path
, UNESCAPE_RELAX
, &p
) < 0)
1554 (void) device_found_node(m
, d
, true, DEVICE_FOUND_MOUNT
, set_flags
);
1556 k
= mount_setup_unit(m
, d
, p
, options
, fstype
, set_flags
);
1557 if (r
== 0 && k
< 0)
1564 static void mount_shutdown(Manager
*m
) {
1568 m
->mount_event_source
= sd_event_source_unref(m
->mount_event_source
);
1570 mnt_unref_monitor(m
->mount_monitor
);
1571 m
->mount_monitor
= NULL
;
1574 static int mount_get_timeout(Unit
*u
, usec_t
*timeout
) {
1575 Mount
*m
= MOUNT(u
);
1579 if (!m
->timer_event_source
)
1582 r
= sd_event_source_get_time(m
->timer_event_source
, &t
);
1585 if (t
== USEC_INFINITY
)
1592 static void mount_enumerate(Manager
*m
) {
1599 if (!m
->mount_monitor
) {
1602 m
->mount_monitor
= mnt_new_monitor();
1603 if (!m
->mount_monitor
) {
1608 r
= mnt_monitor_enable_kernel(m
->mount_monitor
, 1);
1610 log_error_errno(r
, "Failed to enable watching of kernel mount events: %m");
1614 r
= mnt_monitor_enable_userspace(m
->mount_monitor
, 1, NULL
);
1616 log_error_errno(r
, "Failed to enable watching of userspace mount events: %m");
1620 /* mnt_unref_monitor() will close the fd */
1621 fd
= r
= mnt_monitor_get_fd(m
->mount_monitor
);
1623 log_error_errno(r
, "Failed to acquire watch file descriptor: %m");
1627 r
= sd_event_add_io(m
->event
, &m
->mount_event_source
, fd
, EPOLLIN
, mount_dispatch_io
, m
);
1629 log_error_errno(r
, "Failed to watch mount file descriptor: %m");
1633 r
= sd_event_source_set_priority(m
->mount_event_source
, -10);
1635 log_error_errno(r
, "Failed to adjust mount watch priority: %m");
1639 (void) sd_event_source_set_description(m
->mount_event_source
, "mount-monitor-dispatch");
1642 r
= mount_load_proc_self_mountinfo(m
, false);
1652 static int mount_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
1653 _cleanup_set_free_ Set
*around
= NULL
, *gone
= NULL
;
1654 Manager
*m
= userdata
;
1661 assert(revents
& EPOLLIN
);
1663 if (fd
== mnt_monitor_get_fd(m
->mount_monitor
)) {
1664 bool rescan
= false;
1666 /* Drain all events and verify that the event is valid.
1668 * Note that libmount also monitors /run/mount mkdir if the
1669 * directory does not exist yet. The mkdir may generate event
1670 * which is irrelevant for us.
1672 * error: r < 0; valid: r == 0, false positive: rc == 1 */
1674 r
= mnt_monitor_next_change(m
->mount_monitor
, NULL
, NULL
);
1678 return log_error_errno(r
, "Failed to drain libmount events");
1681 log_debug("libmount event [rescan: %s]", yes_no(rescan
));
1686 r
= mount_load_proc_self_mountinfo(m
, true);
1688 /* Reset flags, just in case, for later calls */
1689 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
]) {
1690 Mount
*mount
= MOUNT(u
);
1692 mount
->is_mounted
= mount
->just_mounted
= mount
->just_changed
= false;
1698 manager_dispatch_load_queue(m
);
1700 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
]) {
1701 Mount
*mount
= MOUNT(u
);
1703 if (!mount
->is_mounted
) {
1705 /* A mount point is not around right now. It
1706 * might be gone, or might never have
1709 if (mount
->from_proc_self_mountinfo
&&
1710 mount
->parameters_proc_self_mountinfo
.what
) {
1712 /* Remember that this device might just have disappeared */
1713 if (set_ensure_allocated(&gone
, &string_hash_ops
) < 0 ||
1714 set_put(gone
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
1715 log_oom(); /* we don't care too much about OOM here... */
1718 mount
->from_proc_self_mountinfo
= false;
1720 switch (mount
->state
) {
1723 /* This has just been unmounted by
1724 * somebody else, follow the state
1726 mount_enter_dead(mount
, MOUNT_SUCCESS
);
1733 } else if (mount
->just_mounted
|| mount
->just_changed
) {
1735 /* A mount point was added or changed */
1737 switch (mount
->state
) {
1741 /* This has just been mounted by
1742 * somebody else, follow the state
1744 mount_enter_mounted(mount
, MOUNT_SUCCESS
);
1747 case MOUNT_MOUNTING
:
1748 mount_set_state(mount
, MOUNT_MOUNTING_DONE
);
1752 /* Nothing really changed, but let's
1753 * issue an notification call
1754 * nonetheless, in case somebody is
1755 * waiting for this. (e.g. file system
1756 * ro/rw remounts.) */
1757 mount_set_state(mount
, mount
->state
);
1762 if (mount
->is_mounted
&&
1763 mount
->from_proc_self_mountinfo
&&
1764 mount
->parameters_proc_self_mountinfo
.what
) {
1766 if (set_ensure_allocated(&around
, &string_hash_ops
) < 0 ||
1767 set_put(around
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
1771 /* Reset the flags for later calls */
1772 mount
->is_mounted
= mount
->just_mounted
= mount
->just_changed
= false;
1775 SET_FOREACH(what
, gone
, i
) {
1776 if (set_contains(around
, what
))
1779 /* Let the device units know that the device is no longer mounted */
1780 (void) device_found_node(m
, what
, false, DEVICE_FOUND_MOUNT
, true);
1786 static void mount_reset_failed(Unit
*u
) {
1787 Mount
*m
= MOUNT(u
);
1791 if (m
->state
== MOUNT_FAILED
)
1792 mount_set_state(m
, MOUNT_DEAD
);
1794 m
->result
= MOUNT_SUCCESS
;
1795 m
->reload_result
= MOUNT_SUCCESS
;
1798 static int mount_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
1799 return unit_kill_common(u
, who
, signo
, -1, MOUNT(u
)->control_pid
, error
);
1802 static int mount_control_pid(Unit
*u
) {
1803 Mount
*m
= MOUNT(u
);
1807 return m
->control_pid
;
1810 static const char* const mount_exec_command_table
[_MOUNT_EXEC_COMMAND_MAX
] = {
1811 [MOUNT_EXEC_MOUNT
] = "ExecMount",
1812 [MOUNT_EXEC_UNMOUNT
] = "ExecUnmount",
1813 [MOUNT_EXEC_REMOUNT
] = "ExecRemount",
1816 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command
, MountExecCommand
);
1818 static const char* const mount_result_table
[_MOUNT_RESULT_MAX
] = {
1819 [MOUNT_SUCCESS
] = "success",
1820 [MOUNT_FAILURE_RESOURCES
] = "resources",
1821 [MOUNT_FAILURE_TIMEOUT
] = "timeout",
1822 [MOUNT_FAILURE_EXIT_CODE
] = "exit-code",
1823 [MOUNT_FAILURE_SIGNAL
] = "signal",
1824 [MOUNT_FAILURE_CORE_DUMP
] = "core-dump"
1827 DEFINE_STRING_TABLE_LOOKUP(mount_result
, MountResult
);
1829 const UnitVTable mount_vtable
= {
1830 .object_size
= sizeof(Mount
),
1831 .exec_context_offset
= offsetof(Mount
, exec_context
),
1832 .cgroup_context_offset
= offsetof(Mount
, cgroup_context
),
1833 .kill_context_offset
= offsetof(Mount
, kill_context
),
1834 .exec_runtime_offset
= offsetof(Mount
, exec_runtime
),
1840 .private_section
= "Mount",
1842 .no_instances
= true,
1848 .coldplug
= mount_coldplug
,
1852 .start
= mount_start
,
1854 .reload
= mount_reload
,
1858 .serialize
= mount_serialize
,
1859 .deserialize_item
= mount_deserialize_item
,
1861 .active_state
= mount_active_state
,
1862 .sub_state_to_string
= mount_sub_state_to_string
,
1864 .check_gc
= mount_check_gc
,
1866 .sigchld_event
= mount_sigchld_event
,
1868 .reset_failed
= mount_reset_failed
,
1870 .control_pid
= mount_control_pid
,
1872 .bus_vtable
= bus_mount_vtable
,
1873 .bus_set_property
= bus_mount_set_property
,
1874 .bus_commit_properties
= bus_mount_commit_properties
,
1876 .get_timeout
= mount_get_timeout
,
1878 .can_transient
= true,
1880 .enumerate
= mount_enumerate
,
1881 .shutdown
= mount_shutdown
,
1883 .status_message_formats
= {
1884 .starting_stopping
= {
1885 [0] = "Mounting %s...",
1886 [1] = "Unmounting %s...",
1888 .finished_start_job
= {
1889 [JOB_DONE
] = "Mounted %s.",
1890 [JOB_FAILED
] = "Failed to mount %s.",
1891 [JOB_TIMEOUT
] = "Timed out mounting %s.",
1893 .finished_stop_job
= {
1894 [JOB_DONE
] = "Unmounted %s.",
1895 [JOB_FAILED
] = "Failed unmounting %s.",
1896 [JOB_TIMEOUT
] = "Timed out unmounting %s.",