1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include <sys/epoll.h>
27 #include <sys/inotify.h>
33 #include "sd-messages.h"
36 #include "path-util.h"
37 #include "mount-setup.h"
38 #include "unit-name.h"
39 #include "dbus-mount.h"
41 #include "exit-status.h"
42 #include "fstab-util.h"
43 #include "formats-util.h"
45 #define RETRY_UMOUNT_MAX 32
47 DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_table
*, mnt_free_table
);
48 DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_iter
*, mnt_free_iter
);
50 static const UnitActiveState state_translation_table
[_MOUNT_STATE_MAX
] = {
51 [MOUNT_DEAD
] = UNIT_INACTIVE
,
52 [MOUNT_MOUNTING
] = UNIT_ACTIVATING
,
53 [MOUNT_MOUNTING_DONE
] = UNIT_ACTIVE
,
54 [MOUNT_MOUNTED
] = UNIT_ACTIVE
,
55 [MOUNT_REMOUNTING
] = UNIT_RELOADING
,
56 [MOUNT_UNMOUNTING
] = UNIT_DEACTIVATING
,
57 [MOUNT_MOUNTING_SIGTERM
] = UNIT_DEACTIVATING
,
58 [MOUNT_MOUNTING_SIGKILL
] = UNIT_DEACTIVATING
,
59 [MOUNT_REMOUNTING_SIGTERM
] = UNIT_RELOADING
,
60 [MOUNT_REMOUNTING_SIGKILL
] = UNIT_RELOADING
,
61 [MOUNT_UNMOUNTING_SIGTERM
] = UNIT_DEACTIVATING
,
62 [MOUNT_UNMOUNTING_SIGKILL
] = UNIT_DEACTIVATING
,
63 [MOUNT_FAILED
] = UNIT_FAILED
66 static int mount_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
67 static int mount_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
);
69 static bool mount_needs_network(const char *options
, const char *fstype
) {
70 if (fstab_test_option(options
, "_netdev\0"))
73 if (fstype
&& fstype_is_network(fstype
))
79 static bool mount_is_network(const MountParameters
*p
) {
82 return mount_needs_network(p
->options
, p
->fstype
);
85 static bool mount_is_bind(const MountParameters
*p
) {
88 if (fstab_test_option(p
->options
, "bind\0" "rbind\0"))
91 if (p
->fstype
&& STR_IN_SET(p
->fstype
, "bind", "rbind"))
97 static bool mount_is_auto(const MountParameters
*p
) {
100 return !fstab_test_option(p
->options
, "noauto\0");
103 static bool needs_quota(const MountParameters
*p
) {
106 /* Quotas are not enabled on network filesystems,
107 * but we want them, for example, on storage connected via iscsi */
108 if (p
->fstype
&& fstype_is_network(p
->fstype
))
111 if (mount_is_bind(p
))
114 return fstab_test_option(p
->options
,
115 "usrquota\0" "grpquota\0" "quota\0" "usrjquota\0" "grpjquota\0");
118 static void mount_init(Unit
*u
) {
122 assert(u
->load_state
== UNIT_STUB
);
124 m
->timeout_usec
= u
->manager
->default_timeout_start_usec
;
125 m
->directory_mode
= 0755;
127 if (unit_has_name(u
, "-.mount")) {
128 /* Don't allow start/stop for root directory */
129 u
->refuse_manual_start
= true;
130 u
->refuse_manual_stop
= true;
132 /* The stdio/kmsg bridge socket is on /, in order to avoid a
133 * dep loop, don't use kmsg logging for -.mount */
134 m
->exec_context
.std_output
= u
->manager
->default_std_output
;
135 m
->exec_context
.std_error
= u
->manager
->default_std_error
;
138 /* We need to make sure that /usr/bin/mount is always called
139 * in the same process group as us, so that the autofs kernel
140 * side doesn't send us another mount request while we are
141 * already trying to comply its last one. */
142 m
->exec_context
.same_pgrp
= true;
144 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
146 u
->ignore_on_isolate
= true;
149 static int mount_arm_timer(Mount
*m
) {
154 if (m
->timeout_usec
<= 0) {
155 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
159 if (m
->timer_event_source
) {
160 r
= sd_event_source_set_time(m
->timer_event_source
, now(CLOCK_MONOTONIC
) + m
->timeout_usec
);
164 return sd_event_source_set_enabled(m
->timer_event_source
, SD_EVENT_ONESHOT
);
167 r
= sd_event_add_time(
168 UNIT(m
)->manager
->event
,
169 &m
->timer_event_source
,
171 now(CLOCK_MONOTONIC
) + m
->timeout_usec
, 0,
172 mount_dispatch_timer
, m
);
176 (void) sd_event_source_set_description(m
->timer_event_source
, "mount-timer");
181 static void mount_unwatch_control_pid(Mount
*m
) {
184 if (m
->control_pid
<= 0)
187 unit_unwatch_pid(UNIT(m
), m
->control_pid
);
191 static void mount_parameters_done(MountParameters
*p
) {
198 p
->what
= p
->options
= p
->fstype
= NULL
;
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
);
212 exec_command_done_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
213 m
->control_command
= NULL
;
215 mount_unwatch_control_pid(m
);
217 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
220 _pure_
static MountParameters
* get_mount_parameters_fragment(Mount
*m
) {
223 if (m
->from_fragment
)
224 return &m
->parameters_fragment
;
229 _pure_
static MountParameters
* get_mount_parameters(Mount
*m
) {
232 if (m
->from_proc_self_mountinfo
)
233 return &m
->parameters_proc_self_mountinfo
;
235 return get_mount_parameters_fragment(m
);
238 static int mount_add_mount_links(Mount
*m
) {
239 _cleanup_free_
char *parent
= NULL
;
248 if (!path_equal(m
->where
, "/")) {
249 /* Adds in links to other mount points that might lie further
250 * up in the hierarchy */
251 r
= path_get_parent(m
->where
, &parent
);
255 r
= unit_require_mounts_for(UNIT(m
), parent
);
260 /* Adds in links to other mount points that might be needed
261 * for the source path (if this is a bind mount) to be
263 pm
= get_mount_parameters_fragment(m
);
264 if (pm
&& pm
->what
&&
265 path_is_absolute(pm
->what
) &&
266 !mount_is_network(pm
)) {
268 r
= unit_require_mounts_for(UNIT(m
), pm
->what
);
273 /* Adds in links to other units that use this path or paths
274 * further down in the hierarchy */
275 s
= manager_get_units_requiring_mounts_for(UNIT(m
)->manager
, m
->where
);
276 SET_FOREACH(other
, s
, i
) {
278 if (other
->load_state
!= UNIT_LOADED
)
281 if (other
== UNIT(m
))
284 r
= unit_add_dependency(other
, UNIT_AFTER
, UNIT(m
), true);
288 if (UNIT(m
)->fragment_path
) {
289 /* If we have fragment configuration, then make this dependency required */
290 r
= unit_add_dependency(other
, UNIT_REQUIRES
, UNIT(m
), true);
299 static int mount_add_device_links(Mount
*m
) {
301 bool device_wants_mount
= false;
306 p
= get_mount_parameters(m
);
313 if (mount_is_bind(p
))
316 if (!is_device_path(p
->what
))
319 /* /dev/root is a really weird thing, it's not a real device,
320 * but just a path the kernel exports for the root file system
321 * specified on the kernel command line. Ignore it here. */
322 if (path_equal(p
->what
, "/dev/root"))
325 if (path_equal(m
->where
, "/"))
328 if (mount_is_auto(p
) && UNIT(m
)->manager
->running_as
== MANAGER_SYSTEM
)
329 device_wants_mount
= true;
331 r
= unit_add_node_link(UNIT(m
), p
->what
, device_wants_mount
);
338 static int mount_add_quota_links(Mount
*m
) {
344 if (UNIT(m
)->manager
->running_as
!= MANAGER_SYSTEM
)
347 p
= get_mount_parameters_fragment(m
);
354 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTACHECK_SERVICE
, NULL
, true);
358 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTAON_SERVICE
, NULL
, true);
365 static bool should_umount(Mount
*m
) {
368 if (path_equal(m
->where
, "/") ||
369 path_equal(m
->where
, "/usr"))
372 p
= get_mount_parameters(m
);
373 if (p
&& fstab_test_option(p
->options
, "x-initrd.mount\0") &&
380 static int mount_add_default_dependencies(Mount
*m
) {
381 const char *after
, *after2
, *online
;
387 if (UNIT(m
)->manager
->running_as
!= MANAGER_SYSTEM
)
390 /* We do not add any default dependencies to / and /usr, since
391 * they are guaranteed to stay mounted the whole time, since
392 * our system is on it. Also, don't bother with anything
393 * mounted below virtual file systems, it's also going to be
394 * virtual, and hence not worth the effort. */
395 if (path_equal(m
->where
, "/") ||
396 path_equal(m
->where
, "/usr") ||
397 path_startswith(m
->where
, "/proc") ||
398 path_startswith(m
->where
, "/sys") ||
399 path_startswith(m
->where
, "/dev"))
402 p
= get_mount_parameters(m
);
406 if (mount_is_network(p
)) {
407 after
= SPECIAL_REMOTE_FS_PRE_TARGET
;
408 after2
= SPECIAL_NETWORK_TARGET
;
409 online
= SPECIAL_NETWORK_ONLINE_TARGET
;
411 after
= SPECIAL_LOCAL_FS_PRE_TARGET
;
416 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, after
, NULL
, true);
421 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, after2
, NULL
, true);
427 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_WANTS
, UNIT_AFTER
, online
, NULL
, true);
432 if (should_umount(m
)) {
433 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, NULL
, true);
441 static int mount_verify(Mount
*m
) {
442 _cleanup_free_
char *e
= NULL
;
447 if (UNIT(m
)->load_state
!= UNIT_LOADED
)
450 if (!m
->from_fragment
&& !m
->from_proc_self_mountinfo
)
453 r
= unit_name_from_path(m
->where
, ".mount", &e
);
455 return log_unit_error_errno(UNIT(m
), r
, "Failed to generate unit name from mount path: %m");
457 if (!unit_has_name(UNIT(m
), e
)) {
458 log_unit_error(UNIT(m
), "Where= setting doesn't match unit name. Refusing.");
462 if (mount_point_is_api(m
->where
) || mount_point_ignore(m
->where
)) {
463 log_unit_error(UNIT(m
), "Cannot create mount unit for API file system %s. Refusing.", m
->where
);
467 if (UNIT(m
)->fragment_path
&& !m
->parameters_fragment
.what
) {
468 log_unit_error(UNIT(m
), "What= setting is missing. Refusing.");
472 if (m
->exec_context
.pam_name
&& m
->kill_context
.kill_mode
!= KILL_CONTROL_GROUP
) {
473 log_unit_error(UNIT(m
), "Unit has PAM enabled. Kill mode must be set to control-group'. Refusing.");
480 static int mount_add_extras(Mount
*m
) {
486 if (u
->fragment_path
)
487 m
->from_fragment
= true;
490 r
= unit_name_to_path(u
->id
, &m
->where
);
495 path_kill_slashes(m
->where
);
497 if (!u
->description
) {
498 r
= unit_set_description(u
, m
->where
);
503 r
= mount_add_device_links(m
);
507 r
= mount_add_mount_links(m
);
511 r
= mount_add_quota_links(m
);
515 r
= unit_patch_contexts(u
);
519 r
= unit_add_exec_dependencies(u
, &m
->exec_context
);
523 r
= unit_set_default_slice(u
);
527 if (u
->default_dependencies
) {
528 r
= mount_add_default_dependencies(m
);
536 static int mount_load(Unit
*u
) {
541 assert(u
->load_state
== UNIT_STUB
);
543 if (m
->from_proc_self_mountinfo
)
544 r
= unit_load_fragment_and_dropin_optional(u
);
546 r
= unit_load_fragment_and_dropin(u
);
551 /* This is a new unit? Then let's add in some extras */
552 if (u
->load_state
== UNIT_LOADED
) {
553 r
= mount_add_extras(m
);
558 return mount_verify(m
);
561 static int mount_notify_automount(Mount
*m
, MountState old_state
, MountState state
) {
568 SET_FOREACH(p
, UNIT(m
)->dependencies
[UNIT_TRIGGERED_BY
], i
)
569 if (p
->type
== UNIT_AUTOMOUNT
) {
570 r
= automount_update_mount(AUTOMOUNT(p
), old_state
, state
);
578 static void mount_set_state(Mount
*m
, MountState state
) {
579 MountState old_state
;
582 old_state
= m
->state
;
585 if (state
!= MOUNT_MOUNTING
&&
586 state
!= MOUNT_MOUNTING_DONE
&&
587 state
!= MOUNT_REMOUNTING
&&
588 state
!= MOUNT_UNMOUNTING
&&
589 state
!= MOUNT_MOUNTING_SIGTERM
&&
590 state
!= MOUNT_MOUNTING_SIGKILL
&&
591 state
!= MOUNT_UNMOUNTING_SIGTERM
&&
592 state
!= MOUNT_UNMOUNTING_SIGKILL
&&
593 state
!= MOUNT_REMOUNTING_SIGTERM
&&
594 state
!= MOUNT_REMOUNTING_SIGKILL
) {
595 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
596 mount_unwatch_control_pid(m
);
597 m
->control_command
= NULL
;
598 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
601 mount_notify_automount(m
, old_state
, state
);
603 if (state
!= old_state
)
604 log_unit_debug(UNIT(m
), "Changed %s -> %s", mount_state_to_string(old_state
), mount_state_to_string(state
));
606 unit_notify(UNIT(m
), state_translation_table
[old_state
], state_translation_table
[state
], m
->reload_result
== MOUNT_SUCCESS
);
607 m
->reload_result
= MOUNT_SUCCESS
;
610 static int mount_coldplug(Unit
*u
) {
612 MountState new_state
= MOUNT_DEAD
;
616 assert(m
->state
== MOUNT_DEAD
);
618 if (m
->deserialized_state
!= m
->state
)
619 new_state
= m
->deserialized_state
;
620 else if (m
->from_proc_self_mountinfo
)
621 new_state
= MOUNT_MOUNTED
;
623 if (new_state
== m
->state
)
626 if (new_state
== MOUNT_MOUNTING
||
627 new_state
== MOUNT_MOUNTING_DONE
||
628 new_state
== MOUNT_REMOUNTING
||
629 new_state
== MOUNT_UNMOUNTING
||
630 new_state
== MOUNT_MOUNTING_SIGTERM
||
631 new_state
== MOUNT_MOUNTING_SIGKILL
||
632 new_state
== MOUNT_UNMOUNTING_SIGTERM
||
633 new_state
== MOUNT_UNMOUNTING_SIGKILL
||
634 new_state
== MOUNT_REMOUNTING_SIGTERM
||
635 new_state
== MOUNT_REMOUNTING_SIGKILL
) {
637 if (m
->control_pid
<= 0)
640 r
= unit_watch_pid(UNIT(m
), m
->control_pid
);
644 r
= mount_arm_timer(m
);
649 mount_set_state(m
, new_state
);
653 static void mount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
660 p
= get_mount_parameters(m
);
663 "%sMount State: %s\n"
667 "%sFile System Type: %s\n"
669 "%sFrom /proc/self/mountinfo: %s\n"
670 "%sFrom fragment: %s\n"
671 "%sDirectoryMode: %04o\n",
672 prefix
, mount_state_to_string(m
->state
),
673 prefix
, mount_result_to_string(m
->result
),
675 prefix
, p
? strna(p
->what
) : "n/a",
676 prefix
, p
? strna(p
->fstype
) : "n/a",
677 prefix
, p
? strna(p
->options
) : "n/a",
678 prefix
, yes_no(m
->from_proc_self_mountinfo
),
679 prefix
, yes_no(m
->from_fragment
),
680 prefix
, m
->directory_mode
);
682 if (m
->control_pid
> 0)
684 "%sControl PID: "PID_FMT
"\n",
685 prefix
, m
->control_pid
);
687 exec_context_dump(&m
->exec_context
, f
, prefix
);
688 kill_context_dump(&m
->kill_context
, f
, prefix
);
691 static int mount_spawn(Mount
*m
, ExecCommand
*c
, pid_t
*_pid
) {
694 ExecParameters exec_params
= {
695 .apply_permissions
= true,
696 .apply_chroot
= true,
697 .apply_tty_stdin
= true,
698 .bus_endpoint_fd
= -1,
705 (void) unit_realize_cgroup(UNIT(m
));
706 if (m
->reset_cpu_usage
) {
707 (void) unit_reset_cpu_usage(UNIT(m
));
708 m
->reset_cpu_usage
= false;
711 r
= unit_setup_exec_runtime(UNIT(m
));
715 r
= mount_arm_timer(m
);
719 exec_params
.environment
= UNIT(m
)->manager
->environment
;
720 exec_params
.confirm_spawn
= UNIT(m
)->manager
->confirm_spawn
;
721 exec_params
.cgroup_supported
= UNIT(m
)->manager
->cgroup_supported
;
722 exec_params
.cgroup_path
= UNIT(m
)->cgroup_path
;
723 exec_params
.cgroup_delegate
= m
->cgroup_context
.delegate
;
724 exec_params
.runtime_prefix
= manager_get_runtime_prefix(UNIT(m
)->manager
);
726 r
= exec_spawn(UNIT(m
),
735 r
= unit_watch_pid(UNIT(m
), pid
);
737 /* FIXME: we need to do something here */
745 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
750 static void mount_enter_dead(Mount
*m
, MountResult f
) {
753 if (f
!= MOUNT_SUCCESS
)
756 exec_runtime_destroy(m
->exec_runtime
);
757 m
->exec_runtime
= exec_runtime_unref(m
->exec_runtime
);
759 exec_context_destroy_runtime_directory(&m
->exec_context
, manager_get_runtime_prefix(UNIT(m
)->manager
));
761 mount_set_state(m
, m
->result
!= MOUNT_SUCCESS
? MOUNT_FAILED
: MOUNT_DEAD
);
764 static void mount_enter_mounted(Mount
*m
, MountResult f
) {
767 if (f
!= MOUNT_SUCCESS
)
770 mount_set_state(m
, MOUNT_MOUNTED
);
773 static void mount_enter_signal(Mount
*m
, MountState state
, MountResult f
) {
778 if (f
!= MOUNT_SUCCESS
)
781 r
= unit_kill_context(
784 (state
!= MOUNT_MOUNTING_SIGTERM
&& state
!= MOUNT_UNMOUNTING_SIGTERM
&& state
!= MOUNT_REMOUNTING_SIGTERM
) ?
785 KILL_KILL
: KILL_TERMINATE
,
793 r
= mount_arm_timer(m
);
797 mount_set_state(m
, state
);
798 } else if (state
== MOUNT_REMOUNTING_SIGTERM
)
799 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
800 else if (state
== MOUNT_REMOUNTING_SIGKILL
)
801 mount_enter_mounted(m
, MOUNT_SUCCESS
);
802 else if (state
== MOUNT_MOUNTING_SIGTERM
)
803 mount_enter_signal(m
, MOUNT_MOUNTING_SIGKILL
, MOUNT_SUCCESS
);
804 else if (state
== MOUNT_UNMOUNTING_SIGTERM
)
805 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
807 mount_enter_dead(m
, MOUNT_SUCCESS
);
812 log_unit_warning_errno(UNIT(m
), r
, "Failed to kill processes: %m");
814 if (state
== MOUNT_REMOUNTING_SIGTERM
|| state
== MOUNT_REMOUNTING_SIGKILL
)
815 mount_enter_mounted(m
, MOUNT_FAILURE_RESOURCES
);
817 mount_enter_dead(m
, MOUNT_FAILURE_RESOURCES
);
820 static void mount_enter_unmounting(Mount
*m
) {
825 /* Start counting our attempts */
826 if (!IN_SET(m
->state
,
828 MOUNT_UNMOUNTING_SIGTERM
,
829 MOUNT_UNMOUNTING_SIGKILL
))
830 m
->n_retry_umount
= 0;
832 m
->control_command_id
= MOUNT_EXEC_UNMOUNT
;
833 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_UNMOUNT
;
835 r
= exec_command_set(m
->control_command
, UMOUNT_PATH
, m
->where
, NULL
);
839 mount_unwatch_control_pid(m
);
841 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
845 mount_set_state(m
, MOUNT_UNMOUNTING
);
850 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'umount' task: %m");
851 mount_enter_mounted(m
, MOUNT_FAILURE_RESOURCES
);
854 static void mount_enter_mounting(Mount
*m
) {
860 m
->control_command_id
= MOUNT_EXEC_MOUNT
;
861 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_MOUNT
;
863 r
= unit_fail_if_symlink(UNIT(m
), m
->where
);
867 (void) mkdir_p_label(m
->where
, m
->directory_mode
);
869 unit_warn_if_dir_nonempty(UNIT(m
), m
->where
);
871 /* Create the source directory for bind-mounts if needed */
872 p
= get_mount_parameters_fragment(m
);
873 if (p
&& mount_is_bind(p
))
874 (void) mkdir_p_label(p
->what
, m
->directory_mode
);
876 if (m
->from_fragment
) {
877 _cleanup_free_
char *opts
= NULL
;
879 r
= fstab_filter_options(m
->parameters_fragment
.options
,
880 "nofail\0" "noauto\0" "auto\0", NULL
, NULL
, &opts
);
884 r
= exec_command_set(m
->control_command
, MOUNT_PATH
,
885 m
->parameters_fragment
.what
, m
->where
, NULL
);
886 if (r
>= 0 && m
->sloppy_options
)
887 r
= exec_command_append(m
->control_command
, "-s", NULL
);
888 if (r
>= 0 && m
->parameters_fragment
.fstype
)
889 r
= exec_command_append(m
->control_command
, "-t", m
->parameters_fragment
.fstype
, NULL
);
890 if (r
>= 0 && !isempty(opts
))
891 r
= exec_command_append(m
->control_command
, "-o", opts
, NULL
);
898 mount_unwatch_control_pid(m
);
900 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
904 mount_set_state(m
, MOUNT_MOUNTING
);
909 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'mount' task: %m");
910 mount_enter_dead(m
, MOUNT_FAILURE_RESOURCES
);
913 static void mount_enter_remounting(Mount
*m
) {
918 m
->control_command_id
= MOUNT_EXEC_REMOUNT
;
919 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_REMOUNT
;
921 if (m
->from_fragment
) {
924 if (m
->parameters_fragment
.options
)
925 o
= strjoina("remount,", m
->parameters_fragment
.options
);
929 r
= exec_command_set(m
->control_command
, MOUNT_PATH
,
930 m
->parameters_fragment
.what
, m
->where
,
932 if (r
>= 0 && m
->sloppy_options
)
933 r
= exec_command_append(m
->control_command
, "-s", NULL
);
934 if (r
>= 0 && m
->parameters_fragment
.fstype
)
935 r
= exec_command_append(m
->control_command
, "-t", m
->parameters_fragment
.fstype
, NULL
);
942 mount_unwatch_control_pid(m
);
944 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
948 mount_set_state(m
, MOUNT_REMOUNTING
);
953 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'remount' task: %m");
954 m
->reload_result
= MOUNT_FAILURE_RESOURCES
;
955 mount_enter_mounted(m
, MOUNT_SUCCESS
);
958 static int mount_start(Unit
*u
) {
963 /* We cannot fulfill this request right now, try again later
965 if (m
->state
== MOUNT_UNMOUNTING
||
966 m
->state
== MOUNT_UNMOUNTING_SIGTERM
||
967 m
->state
== MOUNT_UNMOUNTING_SIGKILL
||
968 m
->state
== MOUNT_MOUNTING_SIGTERM
||
969 m
->state
== MOUNT_MOUNTING_SIGKILL
)
973 if (m
->state
== MOUNT_MOUNTING
)
976 assert(m
->state
== MOUNT_DEAD
|| m
->state
== MOUNT_FAILED
);
978 m
->result
= MOUNT_SUCCESS
;
979 m
->reload_result
= MOUNT_SUCCESS
;
980 m
->reset_cpu_usage
= true;
982 mount_enter_mounting(m
);
986 static int mount_stop(Unit
*u
) {
992 if (m
->state
== MOUNT_UNMOUNTING
||
993 m
->state
== MOUNT_UNMOUNTING_SIGKILL
||
994 m
->state
== MOUNT_UNMOUNTING_SIGTERM
||
995 m
->state
== MOUNT_MOUNTING_SIGTERM
||
996 m
->state
== MOUNT_MOUNTING_SIGKILL
)
999 assert(m
->state
== MOUNT_MOUNTING
||
1000 m
->state
== MOUNT_MOUNTING_DONE
||
1001 m
->state
== MOUNT_MOUNTED
||
1002 m
->state
== MOUNT_REMOUNTING
||
1003 m
->state
== MOUNT_REMOUNTING_SIGTERM
||
1004 m
->state
== MOUNT_REMOUNTING_SIGKILL
);
1006 mount_enter_unmounting(m
);
1010 static int mount_reload(Unit
*u
) {
1011 Mount
*m
= MOUNT(u
);
1015 if (m
->state
== MOUNT_MOUNTING_DONE
)
1018 assert(m
->state
== MOUNT_MOUNTED
);
1020 mount_enter_remounting(m
);
1024 static int mount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1025 Mount
*m
= MOUNT(u
);
1031 unit_serialize_item(u
, f
, "state", mount_state_to_string(m
->state
));
1032 unit_serialize_item(u
, f
, "result", mount_result_to_string(m
->result
));
1033 unit_serialize_item(u
, f
, "reload-result", mount_result_to_string(m
->reload_result
));
1035 if (m
->control_pid
> 0)
1036 unit_serialize_item_format(u
, f
, "control-pid", PID_FMT
, m
->control_pid
);
1038 if (m
->control_command_id
>= 0)
1039 unit_serialize_item(u
, f
, "control-command", mount_exec_command_to_string(m
->control_command_id
));
1044 static int mount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1045 Mount
*m
= MOUNT(u
);
1052 if (streq(key
, "state")) {
1055 if ((state
= mount_state_from_string(value
)) < 0)
1056 log_unit_debug(u
, "Failed to parse state value: %s", value
);
1058 m
->deserialized_state
= state
;
1059 } else if (streq(key
, "result")) {
1062 f
= mount_result_from_string(value
);
1064 log_unit_debug(u
, "Failed to parse result value: %s", value
);
1065 else if (f
!= MOUNT_SUCCESS
)
1068 } else if (streq(key
, "reload-result")) {
1071 f
= mount_result_from_string(value
);
1073 log_unit_debug(u
, "Failed to parse reload result value: %s", value
);
1074 else if (f
!= MOUNT_SUCCESS
)
1075 m
->reload_result
= f
;
1077 } else if (streq(key
, "control-pid")) {
1080 if (parse_pid(value
, &pid
) < 0)
1081 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
1083 m
->control_pid
= pid
;
1084 } else if (streq(key
, "control-command")) {
1085 MountExecCommand id
;
1087 id
= mount_exec_command_from_string(value
);
1089 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
1091 m
->control_command_id
= id
;
1092 m
->control_command
= m
->exec_command
+ id
;
1095 log_unit_debug(u
, "Unknown serialization key: %s", key
);
1100 _pure_
static UnitActiveState
mount_active_state(Unit
*u
) {
1103 return state_translation_table
[MOUNT(u
)->state
];
1106 _pure_
static const char *mount_sub_state_to_string(Unit
*u
) {
1109 return mount_state_to_string(MOUNT(u
)->state
);
1112 _pure_
static bool mount_check_gc(Unit
*u
) {
1113 Mount
*m
= MOUNT(u
);
1117 return m
->from_proc_self_mountinfo
;
1120 static void mount_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1121 Mount
*m
= MOUNT(u
);
1127 if (pid
!= m
->control_pid
)
1132 if (is_clean_exit(code
, status
, NULL
))
1134 else if (code
== CLD_EXITED
)
1135 f
= MOUNT_FAILURE_EXIT_CODE
;
1136 else if (code
== CLD_KILLED
)
1137 f
= MOUNT_FAILURE_SIGNAL
;
1138 else if (code
== CLD_DUMPED
)
1139 f
= MOUNT_FAILURE_CORE_DUMP
;
1141 assert_not_reached("Unknown code");
1143 if (f
!= MOUNT_SUCCESS
)
1146 if (m
->control_command
) {
1147 exec_status_exit(&m
->control_command
->exec_status
, &m
->exec_context
, pid
, code
, status
);
1149 m
->control_command
= NULL
;
1150 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
1153 log_unit_full(u
, f
== MOUNT_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
, 0,
1154 "Mount process exited, code=%s status=%i", sigchld_code_to_string(code
), status
);
1156 /* Note that mount(8) returning and the kernel sending us a
1157 * mount table change event might happen out-of-order. If an
1158 * operation succeed we assume the kernel will follow soon too
1159 * and already change into the resulting state. If it fails
1160 * we check if the kernel still knows about the mount. and
1161 * change state accordingly. */
1165 case MOUNT_MOUNTING
:
1166 case MOUNT_MOUNTING_DONE
:
1167 case MOUNT_MOUNTING_SIGKILL
:
1168 case MOUNT_MOUNTING_SIGTERM
:
1170 if (f
== MOUNT_SUCCESS
)
1171 mount_enter_mounted(m
, f
);
1172 else if (m
->from_proc_self_mountinfo
)
1173 mount_enter_mounted(m
, f
);
1175 mount_enter_dead(m
, f
);
1178 case MOUNT_REMOUNTING
:
1179 case MOUNT_REMOUNTING_SIGKILL
:
1180 case MOUNT_REMOUNTING_SIGTERM
:
1182 m
->reload_result
= f
;
1183 if (m
->from_proc_self_mountinfo
)
1184 mount_enter_mounted(m
, MOUNT_SUCCESS
);
1186 mount_enter_dead(m
, MOUNT_SUCCESS
);
1190 case MOUNT_UNMOUNTING
:
1191 case MOUNT_UNMOUNTING_SIGKILL
:
1192 case MOUNT_UNMOUNTING_SIGTERM
:
1194 if (f
== MOUNT_SUCCESS
) {
1196 if (m
->from_proc_self_mountinfo
) {
1198 /* Still a mount point? If so, let's
1199 * try again. Most likely there were
1200 * multiple mount points stacked on
1201 * top of each other. Note that due to
1202 * the io event priority logic we can
1203 * be sure the new mountinfo is loaded
1204 * before we process the SIGCHLD for
1205 * the mount command. */
1207 if (m
->n_retry_umount
< RETRY_UMOUNT_MAX
) {
1208 log_unit_debug(u
, "Mount still present, trying again.");
1209 m
->n_retry_umount
++;
1210 mount_enter_unmounting(m
);
1212 log_unit_debug(u
, "Mount still present after %u attempts to unmount, giving up.", m
->n_retry_umount
);
1213 mount_enter_mounted(m
, f
);
1216 mount_enter_dead(m
, f
);
1218 } else if (m
->from_proc_self_mountinfo
)
1219 mount_enter_mounted(m
, f
);
1221 mount_enter_dead(m
, f
);
1225 assert_not_reached("Uh, control process died at wrong time.");
1228 /* Notify clients about changed exit status */
1229 unit_add_to_dbus_queue(u
);
1232 static int mount_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
1233 Mount
*m
= MOUNT(userdata
);
1236 assert(m
->timer_event_source
== source
);
1240 case MOUNT_MOUNTING
:
1241 case MOUNT_MOUNTING_DONE
:
1242 log_unit_warning(UNIT(m
), "Mounting timed out. Stopping.");
1243 mount_enter_signal(m
, MOUNT_MOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1246 case MOUNT_REMOUNTING
:
1247 log_unit_warning(UNIT(m
), "Remounting timed out. Stopping.");
1248 m
->reload_result
= MOUNT_FAILURE_TIMEOUT
;
1249 mount_enter_mounted(m
, MOUNT_SUCCESS
);
1252 case MOUNT_UNMOUNTING
:
1253 log_unit_warning(UNIT(m
), "Unmounting timed out. Stopping.");
1254 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1257 case MOUNT_MOUNTING_SIGTERM
:
1258 if (m
->kill_context
.send_sigkill
) {
1259 log_unit_warning(UNIT(m
), "Mounting timed out. Killing.");
1260 mount_enter_signal(m
, MOUNT_MOUNTING_SIGKILL
, MOUNT_FAILURE_TIMEOUT
);
1262 log_unit_warning(UNIT(m
), "Mounting timed out. Skipping SIGKILL. Ignoring.");
1264 if (m
->from_proc_self_mountinfo
)
1265 mount_enter_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1267 mount_enter_dead(m
, MOUNT_FAILURE_TIMEOUT
);
1271 case MOUNT_REMOUNTING_SIGTERM
:
1272 if (m
->kill_context
.send_sigkill
) {
1273 log_unit_warning(UNIT(m
), "Remounting timed out. Killing.");
1274 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_FAILURE_TIMEOUT
);
1276 log_unit_warning(UNIT(m
), "Remounting timed out. Skipping SIGKILL. Ignoring.");
1278 if (m
->from_proc_self_mountinfo
)
1279 mount_enter_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1281 mount_enter_dead(m
, MOUNT_FAILURE_TIMEOUT
);
1285 case MOUNT_UNMOUNTING_SIGTERM
:
1286 if (m
->kill_context
.send_sigkill
) {
1287 log_unit_warning(UNIT(m
), "Unmounting timed out. Killing.");
1288 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_FAILURE_TIMEOUT
);
1290 log_unit_warning(UNIT(m
), "Unmounting timed out. Skipping SIGKILL. Ignoring.");
1292 if (m
->from_proc_self_mountinfo
)
1293 mount_enter_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1295 mount_enter_dead(m
, MOUNT_FAILURE_TIMEOUT
);
1299 case MOUNT_MOUNTING_SIGKILL
:
1300 case MOUNT_REMOUNTING_SIGKILL
:
1301 case MOUNT_UNMOUNTING_SIGKILL
:
1302 log_unit_warning(UNIT(m
),"Mount process still around after SIGKILL. Ignoring.");
1304 if (m
->from_proc_self_mountinfo
)
1305 mount_enter_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1307 mount_enter_dead(m
, MOUNT_FAILURE_TIMEOUT
);
1311 assert_not_reached("Timeout at wrong time.");
1317 static int mount_setup_unit(
1321 const char *options
,
1325 _cleanup_free_
char *e
= NULL
, *w
= NULL
, *o
= NULL
, *f
= NULL
;
1326 bool load_extras
= false;
1328 bool delete, changed
= false;
1338 /* Ignore API mount points. They should never be referenced in
1339 * dependencies ever. */
1340 if (mount_point_is_api(where
) || mount_point_ignore(where
))
1343 if (streq(fstype
, "autofs"))
1346 /* probably some kind of swap, ignore */
1347 if (!is_path(where
))
1350 r
= unit_name_from_path(where
, ".mount", &e
);
1354 u
= manager_get_unit(m
, e
);
1358 u
= unit_new(m
, sizeof(Mount
));
1362 r
= unit_add_name(u
, e
);
1366 MOUNT(u
)->where
= strdup(where
);
1367 if (!MOUNT(u
)->where
) {
1372 u
->source_path
= strdup("/proc/self/mountinfo");
1373 if (!u
->source_path
) {
1378 if (m
->running_as
== MANAGER_SYSTEM
) {
1381 target
= mount_needs_network(options
, fstype
) ? SPECIAL_REMOTE_FS_TARGET
: SPECIAL_LOCAL_FS_TARGET
;
1382 r
= unit_add_dependency_by_name(u
, UNIT_BEFORE
, target
, NULL
, true);
1386 if (should_umount(MOUNT(u
))) {
1387 r
= unit_add_dependency_by_name(u
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, NULL
, true);
1393 unit_add_to_load_queue(u
);
1398 if (!MOUNT(u
)->where
) {
1399 MOUNT(u
)->where
= strdup(where
);
1400 if (!MOUNT(u
)->where
) {
1406 if (m
->running_as
== MANAGER_SYSTEM
&&
1407 mount_needs_network(options
, fstype
)) {
1408 /* _netdev option may have shown up late, or on a
1409 * remount. Add remote-fs dependencies, even though
1410 * local-fs ones may already be there. */
1411 unit_add_dependency_by_name(u
, UNIT_BEFORE
, SPECIAL_REMOTE_FS_TARGET
, NULL
, true);
1415 if (u
->load_state
== UNIT_NOT_FOUND
) {
1416 u
->load_state
= UNIT_LOADED
;
1419 /* Load in the extras later on, after we
1420 * finished initialization of the unit */
1427 o
= strdup(options
);
1429 if (!w
|| !o
|| !f
) {
1434 p
= &MOUNT(u
)->parameters_proc_self_mountinfo
;
1436 changed
= changed
||
1437 !streq_ptr(p
->options
, options
) ||
1438 !streq_ptr(p
->what
, what
) ||
1439 !streq_ptr(p
->fstype
, fstype
);
1442 MOUNT(u
)->is_mounted
= true;
1443 MOUNT(u
)->just_mounted
= !MOUNT(u
)->from_proc_self_mountinfo
;
1444 MOUNT(u
)->just_changed
= changed
;
1447 MOUNT(u
)->from_proc_self_mountinfo
= true;
1462 r
= mount_add_extras(MOUNT(u
));
1468 unit_add_to_dbus_queue(u
);
1473 log_warning_errno(r
, "Failed to set up mount unit: %m");
1481 static int mount_load_proc_self_mountinfo(Manager
*m
, bool set_flags
) {
1482 _cleanup_(mnt_free_tablep
) struct libmnt_table
*t
= NULL
;
1483 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*i
= NULL
;
1488 t
= mnt_new_table();
1492 i
= mnt_new_iter(MNT_ITER_FORWARD
);
1496 r
= mnt_table_parse_mtab(t
, NULL
);
1498 return log_error_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
1502 const char *device
, *path
, *options
, *fstype
;
1503 _cleanup_free_
char *d
= NULL
, *p
= NULL
;
1504 struct libmnt_fs
*fs
;
1507 k
= mnt_table_next_fs(t
, i
, &fs
);
1511 return log_error_errno(k
, "Failed to get next entry from /proc/self/mountinfo: %m");
1513 device
= mnt_fs_get_source(fs
);
1514 path
= mnt_fs_get_target(fs
);
1515 options
= mnt_fs_get_options(fs
);
1516 fstype
= mnt_fs_get_fstype(fs
);
1518 if (!device
|| !path
)
1521 if (cunescape(device
, UNESCAPE_RELAX
, &d
) < 0)
1524 if (cunescape(path
, UNESCAPE_RELAX
, &p
) < 0)
1527 (void) device_found_node(m
, d
, true, DEVICE_FOUND_MOUNT
, set_flags
);
1529 k
= mount_setup_unit(m
, d
, p
, options
, fstype
, set_flags
);
1530 if (r
== 0 && k
< 0)
1537 static void mount_shutdown(Manager
*m
) {
1540 m
->mount_event_source
= sd_event_source_unref(m
->mount_event_source
);
1541 m
->mount_utab_event_source
= sd_event_source_unref(m
->mount_utab_event_source
);
1543 if (m
->proc_self_mountinfo
) {
1544 fclose(m
->proc_self_mountinfo
);
1545 m
->proc_self_mountinfo
= NULL
;
1547 m
->utab_inotify_fd
= safe_close(m
->utab_inotify_fd
);
1550 static int mount_get_timeout(Unit
*u
, uint64_t *timeout
) {
1551 Mount
*m
= MOUNT(u
);
1554 if (!m
->timer_event_source
)
1557 r
= sd_event_source_get_time(m
->timer_event_source
, timeout
);
1564 static int mount_enumerate(Manager
*m
) {
1570 if (!m
->proc_self_mountinfo
) {
1571 m
->proc_self_mountinfo
= fopen("/proc/self/mountinfo", "re");
1572 if (!m
->proc_self_mountinfo
)
1575 r
= sd_event_add_io(m
->event
, &m
->mount_event_source
, fileno(m
->proc_self_mountinfo
), EPOLLPRI
, mount_dispatch_io
, m
);
1579 /* Dispatch this before we dispatch SIGCHLD, so that
1580 * we always get the events from /proc/self/mountinfo
1581 * before the SIGCHLD of /usr/bin/mount. */
1582 r
= sd_event_source_set_priority(m
->mount_event_source
, -10);
1586 (void) sd_event_source_set_description(m
->mount_event_source
, "mount-mountinfo-dispatch");
1589 if (m
->utab_inotify_fd
< 0) {
1590 m
->utab_inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
1591 if (m
->utab_inotify_fd
< 0) {
1596 (void) mkdir_p_label("/run/mount", 0755);
1598 r
= inotify_add_watch(m
->utab_inotify_fd
, "/run/mount", IN_MOVED_TO
);
1604 r
= sd_event_add_io(m
->event
, &m
->mount_utab_event_source
, m
->utab_inotify_fd
, EPOLLIN
, mount_dispatch_io
, m
);
1608 r
= sd_event_source_set_priority(m
->mount_utab_event_source
, -10);
1612 (void) sd_event_source_set_description(m
->mount_utab_event_source
, "mount-utab-dispatch");
1615 r
= mount_load_proc_self_mountinfo(m
, false);
1626 static int mount_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
1627 _cleanup_set_free_ Set
*around
= NULL
, *gone
= NULL
;
1628 Manager
*m
= userdata
;
1635 assert(revents
& (EPOLLPRI
| EPOLLIN
));
1637 /* The manager calls this for every fd event happening on the
1638 * /proc/self/mountinfo file, which informs us about mounting
1639 * table changes, and for /run/mount events which we watch
1640 * for mount options. */
1642 if (fd
== m
->utab_inotify_fd
) {
1643 bool rescan
= false;
1645 /* FIXME: We *really* need to replace this with
1646 * libmount's own API for this, we should not hardcode
1647 * internal behaviour of libmount here. */
1650 union inotify_event_buffer buffer
;
1651 struct inotify_event
*e
;
1654 l
= read(fd
, &buffer
, sizeof(buffer
));
1656 if (errno
== EAGAIN
|| errno
== EINTR
)
1659 log_error_errno(errno
, "Failed to read utab inotify: %m");
1663 FOREACH_INOTIFY_EVENT(e
, buffer
, l
) {
1664 /* Only care about changes to utab,
1665 * but we have to monitor the
1666 * directory to reliably get
1667 * notifications about when utab is
1668 * replaced using rename(2) */
1669 if ((e
->mask
& IN_Q_OVERFLOW
) || streq(e
->name
, "utab"))
1678 r
= mount_load_proc_self_mountinfo(m
, true);
1680 /* Reset flags, just in case, for later calls */
1681 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
]) {
1682 Mount
*mount
= MOUNT(u
);
1684 mount
->is_mounted
= mount
->just_mounted
= mount
->just_changed
= false;
1690 manager_dispatch_load_queue(m
);
1692 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
]) {
1693 Mount
*mount
= MOUNT(u
);
1695 if (!mount
->is_mounted
) {
1697 /* A mount point is not around right now. It
1698 * might be gone, or might never have
1701 if (mount
->from_proc_self_mountinfo
&&
1702 mount
->parameters_proc_self_mountinfo
.what
) {
1704 /* Remember that this device might just have disappeared */
1705 if (set_ensure_allocated(&gone
, &string_hash_ops
) < 0 ||
1706 set_put(gone
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
1707 log_oom(); /* we don't care too much about OOM here... */
1710 mount
->from_proc_self_mountinfo
= false;
1712 switch (mount
->state
) {
1715 /* This has just been unmounted by
1716 * somebody else, follow the state
1718 mount_enter_dead(mount
, MOUNT_SUCCESS
);
1725 } else if (mount
->just_mounted
|| mount
->just_changed
) {
1727 /* A mount point was added or changed */
1729 switch (mount
->state
) {
1733 /* This has just been mounted by
1734 * somebody else, follow the state
1736 mount_enter_mounted(mount
, MOUNT_SUCCESS
);
1739 case MOUNT_MOUNTING
:
1740 mount_set_state(mount
, MOUNT_MOUNTING_DONE
);
1744 /* Nothing really changed, but let's
1745 * issue an notification call
1746 * nonetheless, in case somebody is
1747 * waiting for this. (e.g. file system
1748 * ro/rw remounts.) */
1749 mount_set_state(mount
, mount
->state
);
1754 if (mount
->is_mounted
&&
1755 mount
->from_proc_self_mountinfo
&&
1756 mount
->parameters_proc_self_mountinfo
.what
) {
1758 if (set_ensure_allocated(&around
, &string_hash_ops
) < 0 ||
1759 set_put(around
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
1763 /* Reset the flags for later calls */
1764 mount
->is_mounted
= mount
->just_mounted
= mount
->just_changed
= false;
1767 SET_FOREACH(what
, gone
, i
) {
1768 if (set_contains(around
, what
))
1771 /* Let the device units know that the device is no longer mounted */
1772 (void) device_found_node(m
, what
, false, DEVICE_FOUND_MOUNT
, true);
1778 static void mount_reset_failed(Unit
*u
) {
1779 Mount
*m
= MOUNT(u
);
1783 if (m
->state
== MOUNT_FAILED
)
1784 mount_set_state(m
, MOUNT_DEAD
);
1786 m
->result
= MOUNT_SUCCESS
;
1787 m
->reload_result
= MOUNT_SUCCESS
;
1790 static int mount_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
1791 return unit_kill_common(u
, who
, signo
, -1, MOUNT(u
)->control_pid
, error
);
1794 static const char* const mount_state_table
[_MOUNT_STATE_MAX
] = {
1795 [MOUNT_DEAD
] = "dead",
1796 [MOUNT_MOUNTING
] = "mounting",
1797 [MOUNT_MOUNTING_DONE
] = "mounting-done",
1798 [MOUNT_MOUNTED
] = "mounted",
1799 [MOUNT_REMOUNTING
] = "remounting",
1800 [MOUNT_UNMOUNTING
] = "unmounting",
1801 [MOUNT_MOUNTING_SIGTERM
] = "mounting-sigterm",
1802 [MOUNT_MOUNTING_SIGKILL
] = "mounting-sigkill",
1803 [MOUNT_REMOUNTING_SIGTERM
] = "remounting-sigterm",
1804 [MOUNT_REMOUNTING_SIGKILL
] = "remounting-sigkill",
1805 [MOUNT_UNMOUNTING_SIGTERM
] = "unmounting-sigterm",
1806 [MOUNT_UNMOUNTING_SIGKILL
] = "unmounting-sigkill",
1807 [MOUNT_FAILED
] = "failed"
1810 DEFINE_STRING_TABLE_LOOKUP(mount_state
, MountState
);
1812 static const char* const mount_exec_command_table
[_MOUNT_EXEC_COMMAND_MAX
] = {
1813 [MOUNT_EXEC_MOUNT
] = "ExecMount",
1814 [MOUNT_EXEC_UNMOUNT
] = "ExecUnmount",
1815 [MOUNT_EXEC_REMOUNT
] = "ExecRemount",
1818 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command
, MountExecCommand
);
1820 static const char* const mount_result_table
[_MOUNT_RESULT_MAX
] = {
1821 [MOUNT_SUCCESS
] = "success",
1822 [MOUNT_FAILURE_RESOURCES
] = "resources",
1823 [MOUNT_FAILURE_TIMEOUT
] = "timeout",
1824 [MOUNT_FAILURE_EXIT_CODE
] = "exit-code",
1825 [MOUNT_FAILURE_SIGNAL
] = "signal",
1826 [MOUNT_FAILURE_CORE_DUMP
] = "core-dump"
1829 DEFINE_STRING_TABLE_LOOKUP(mount_result
, MountResult
);
1831 const UnitVTable mount_vtable
= {
1832 .object_size
= sizeof(Mount
),
1833 .exec_context_offset
= offsetof(Mount
, exec_context
),
1834 .cgroup_context_offset
= offsetof(Mount
, cgroup_context
),
1835 .kill_context_offset
= offsetof(Mount
, kill_context
),
1836 .exec_runtime_offset
= offsetof(Mount
, exec_runtime
),
1842 .private_section
= "Mount",
1845 .no_instances
= true,
1851 .coldplug
= mount_coldplug
,
1855 .start
= mount_start
,
1857 .reload
= mount_reload
,
1861 .serialize
= mount_serialize
,
1862 .deserialize_item
= mount_deserialize_item
,
1864 .active_state
= mount_active_state
,
1865 .sub_state_to_string
= mount_sub_state_to_string
,
1867 .check_gc
= mount_check_gc
,
1869 .sigchld_event
= mount_sigchld_event
,
1871 .reset_failed
= mount_reset_failed
,
1873 .bus_vtable
= bus_mount_vtable
,
1874 .bus_set_property
= bus_mount_set_property
,
1875 .bus_commit_properties
= bus_mount_commit_properties
,
1877 .get_timeout
= mount_get_timeout
,
1879 .can_transient
= true,
1881 .enumerate
= mount_enumerate
,
1882 .shutdown
= mount_shutdown
,
1884 .status_message_formats
= {
1885 .starting_stopping
= {
1886 [0] = "Mounting %s...",
1887 [1] = "Unmounting %s...",
1889 .finished_start_job
= {
1890 [JOB_DONE
] = "Mounted %s.",
1891 [JOB_FAILED
] = "Failed to mount %s.",
1892 [JOB_TIMEOUT
] = "Timed out mounting %s.",
1894 .finished_stop_job
= {
1895 [JOB_DONE
] = "Unmounted %s.",
1896 [JOB_FAILED
] = "Failed unmounting %s.",
1897 [JOB_TIMEOUT
] = "Timed out unmounting %s.",