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
) {
209 mount_parameters_done(&m
->parameters_proc_self_mountinfo
);
210 mount_parameters_done(&m
->parameters_fragment
);
212 m
->exec_runtime
= exec_runtime_unref(m
->exec_runtime
);
213 exec_command_done_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
214 m
->control_command
= NULL
;
216 mount_unwatch_control_pid(m
);
218 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
221 _pure_
static MountParameters
* get_mount_parameters_fragment(Mount
*m
) {
224 if (m
->from_fragment
)
225 return &m
->parameters_fragment
;
230 _pure_
static MountParameters
* get_mount_parameters(Mount
*m
) {
233 if (m
->from_proc_self_mountinfo
)
234 return &m
->parameters_proc_self_mountinfo
;
236 return get_mount_parameters_fragment(m
);
239 static int mount_add_mount_links(Mount
*m
) {
240 _cleanup_free_
char *parent
= NULL
;
249 if (!path_equal(m
->where
, "/")) {
250 /* Adds in links to other mount points that might lie further
251 * up in the hierarchy */
252 r
= path_get_parent(m
->where
, &parent
);
256 r
= unit_require_mounts_for(UNIT(m
), parent
);
261 /* Adds in links to other mount points that might be needed
262 * for the source path (if this is a bind mount) to be
264 pm
= get_mount_parameters_fragment(m
);
265 if (pm
&& pm
->what
&&
266 path_is_absolute(pm
->what
) &&
267 !mount_is_network(pm
)) {
269 r
= unit_require_mounts_for(UNIT(m
), pm
->what
);
274 /* Adds in links to other units that use this path or paths
275 * further down in the hierarchy */
276 s
= manager_get_units_requiring_mounts_for(UNIT(m
)->manager
, m
->where
);
277 SET_FOREACH(other
, s
, i
) {
279 if (other
->load_state
!= UNIT_LOADED
)
282 if (other
== UNIT(m
))
285 r
= unit_add_dependency(other
, UNIT_AFTER
, UNIT(m
), true);
289 if (UNIT(m
)->fragment_path
) {
290 /* If we have fragment configuration, then make this dependency required */
291 r
= unit_add_dependency(other
, UNIT_REQUIRES
, UNIT(m
), true);
300 static int mount_add_device_links(Mount
*m
) {
302 bool device_wants_mount
= false;
307 p
= get_mount_parameters(m
);
314 if (mount_is_bind(p
))
317 if (!is_device_path(p
->what
))
320 /* /dev/root is a really weird thing, it's not a real device,
321 * but just a path the kernel exports for the root file system
322 * specified on the kernel command line. Ignore it here. */
323 if (path_equal(p
->what
, "/dev/root"))
326 if (path_equal(m
->where
, "/"))
329 if (mount_is_auto(p
) && UNIT(m
)->manager
->running_as
== MANAGER_SYSTEM
)
330 device_wants_mount
= true;
332 r
= unit_add_node_link(UNIT(m
), p
->what
, device_wants_mount
);
339 static int mount_add_quota_links(Mount
*m
) {
345 if (UNIT(m
)->manager
->running_as
!= MANAGER_SYSTEM
)
348 p
= get_mount_parameters_fragment(m
);
355 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTACHECK_SERVICE
, NULL
, true);
359 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTAON_SERVICE
, NULL
, true);
366 static bool should_umount(Mount
*m
) {
369 if (path_equal(m
->where
, "/") ||
370 path_equal(m
->where
, "/usr"))
373 p
= get_mount_parameters(m
);
374 if (p
&& fstab_test_option(p
->options
, "x-initrd.mount\0") &&
381 static int mount_add_default_dependencies(Mount
*m
) {
382 const char *after
, *after2
, *online
;
388 if (UNIT(m
)->manager
->running_as
!= MANAGER_SYSTEM
)
391 /* We do not add any default dependencies to / and /usr, since
392 * they are guaranteed to stay mounted the whole time, since
393 * our system is on it. Also, don't bother with anything
394 * mounted below virtual file systems, it's also going to be
395 * virtual, and hence not worth the effort. */
396 if (path_equal(m
->where
, "/") ||
397 path_equal(m
->where
, "/usr") ||
398 path_startswith(m
->where
, "/proc") ||
399 path_startswith(m
->where
, "/sys") ||
400 path_startswith(m
->where
, "/dev"))
403 p
= get_mount_parameters(m
);
407 if (mount_is_network(p
)) {
408 after
= SPECIAL_REMOTE_FS_PRE_TARGET
;
409 after2
= SPECIAL_NETWORK_TARGET
;
410 online
= SPECIAL_NETWORK_ONLINE_TARGET
;
412 after
= SPECIAL_LOCAL_FS_PRE_TARGET
;
417 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, after
, NULL
, true);
422 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, after2
, NULL
, true);
428 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_WANTS
, UNIT_AFTER
, online
, NULL
, true);
433 if (should_umount(m
)) {
434 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, NULL
, true);
442 static int mount_verify(Mount
*m
) {
443 _cleanup_free_
char *e
= NULL
;
448 if (UNIT(m
)->load_state
!= UNIT_LOADED
)
451 if (!m
->from_fragment
&& !m
->from_proc_self_mountinfo
)
454 r
= unit_name_from_path(m
->where
, ".mount", &e
);
456 return log_unit_error_errno(UNIT(m
), r
, "Failed to generate unit name from mount path: %m");
458 if (!unit_has_name(UNIT(m
), e
)) {
459 log_unit_error(UNIT(m
), "Where= setting doesn't match unit name. Refusing.");
463 if (mount_point_is_api(m
->where
) || mount_point_ignore(m
->where
)) {
464 log_unit_error(UNIT(m
), "Cannot create mount unit for API file system %s. Refusing.", m
->where
);
468 if (UNIT(m
)->fragment_path
&& !m
->parameters_fragment
.what
) {
469 log_unit_error(UNIT(m
), "What= setting is missing. Refusing.");
473 if (m
->exec_context
.pam_name
&& m
->kill_context
.kill_mode
!= KILL_CONTROL_GROUP
) {
474 log_unit_error(UNIT(m
), "Unit has PAM enabled. Kill mode must be set to control-group'. Refusing.");
481 static int mount_add_extras(Mount
*m
) {
487 if (u
->fragment_path
)
488 m
->from_fragment
= true;
491 r
= unit_name_to_path(u
->id
, &m
->where
);
496 path_kill_slashes(m
->where
);
498 if (!u
->description
) {
499 r
= unit_set_description(u
, m
->where
);
504 r
= mount_add_device_links(m
);
508 r
= mount_add_mount_links(m
);
512 r
= mount_add_quota_links(m
);
516 r
= unit_patch_contexts(u
);
520 r
= unit_add_exec_dependencies(u
, &m
->exec_context
);
524 r
= unit_set_default_slice(u
);
528 if (u
->default_dependencies
) {
529 r
= mount_add_default_dependencies(m
);
537 static int mount_load(Unit
*u
) {
542 assert(u
->load_state
== UNIT_STUB
);
544 if (m
->from_proc_self_mountinfo
)
545 r
= unit_load_fragment_and_dropin_optional(u
);
547 r
= unit_load_fragment_and_dropin(u
);
552 /* This is a new unit? Then let's add in some extras */
553 if (u
->load_state
== UNIT_LOADED
) {
554 r
= mount_add_extras(m
);
559 return mount_verify(m
);
562 static int mount_notify_automount(Mount
*m
, MountState old_state
, MountState state
) {
569 SET_FOREACH(p
, UNIT(m
)->dependencies
[UNIT_TRIGGERED_BY
], i
)
570 if (p
->type
== UNIT_AUTOMOUNT
) {
571 r
= automount_update_mount(AUTOMOUNT(p
), old_state
, state
);
579 static void mount_set_state(Mount
*m
, MountState state
) {
580 MountState old_state
;
583 old_state
= m
->state
;
586 if (state
!= MOUNT_MOUNTING
&&
587 state
!= MOUNT_MOUNTING_DONE
&&
588 state
!= MOUNT_REMOUNTING
&&
589 state
!= MOUNT_UNMOUNTING
&&
590 state
!= MOUNT_MOUNTING_SIGTERM
&&
591 state
!= MOUNT_MOUNTING_SIGKILL
&&
592 state
!= MOUNT_UNMOUNTING_SIGTERM
&&
593 state
!= MOUNT_UNMOUNTING_SIGKILL
&&
594 state
!= MOUNT_REMOUNTING_SIGTERM
&&
595 state
!= MOUNT_REMOUNTING_SIGKILL
) {
596 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
597 mount_unwatch_control_pid(m
);
598 m
->control_command
= NULL
;
599 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
602 mount_notify_automount(m
, old_state
, state
);
604 if (state
!= old_state
)
605 log_unit_debug(UNIT(m
), "Changed %s -> %s", mount_state_to_string(old_state
), mount_state_to_string(state
));
607 unit_notify(UNIT(m
), state_translation_table
[old_state
], state_translation_table
[state
], m
->reload_result
== MOUNT_SUCCESS
);
608 m
->reload_result
= MOUNT_SUCCESS
;
611 static int mount_coldplug(Unit
*u
) {
613 MountState new_state
= MOUNT_DEAD
;
617 assert(m
->state
== MOUNT_DEAD
);
619 if (m
->deserialized_state
!= m
->state
)
620 new_state
= m
->deserialized_state
;
621 else if (m
->from_proc_self_mountinfo
)
622 new_state
= MOUNT_MOUNTED
;
624 if (new_state
== m
->state
)
627 if (new_state
== MOUNT_MOUNTING
||
628 new_state
== MOUNT_MOUNTING_DONE
||
629 new_state
== MOUNT_REMOUNTING
||
630 new_state
== MOUNT_UNMOUNTING
||
631 new_state
== MOUNT_MOUNTING_SIGTERM
||
632 new_state
== MOUNT_MOUNTING_SIGKILL
||
633 new_state
== MOUNT_UNMOUNTING_SIGTERM
||
634 new_state
== MOUNT_UNMOUNTING_SIGKILL
||
635 new_state
== MOUNT_REMOUNTING_SIGTERM
||
636 new_state
== MOUNT_REMOUNTING_SIGKILL
) {
638 if (m
->control_pid
<= 0)
641 r
= unit_watch_pid(UNIT(m
), m
->control_pid
);
645 r
= mount_arm_timer(m
);
650 mount_set_state(m
, new_state
);
654 static void mount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
661 p
= get_mount_parameters(m
);
664 "%sMount State: %s\n"
668 "%sFile System Type: %s\n"
670 "%sFrom /proc/self/mountinfo: %s\n"
671 "%sFrom fragment: %s\n"
672 "%sDirectoryMode: %04o\n",
673 prefix
, mount_state_to_string(m
->state
),
674 prefix
, mount_result_to_string(m
->result
),
676 prefix
, p
? strna(p
->what
) : "n/a",
677 prefix
, p
? strna(p
->fstype
) : "n/a",
678 prefix
, p
? strna(p
->options
) : "n/a",
679 prefix
, yes_no(m
->from_proc_self_mountinfo
),
680 prefix
, yes_no(m
->from_fragment
),
681 prefix
, m
->directory_mode
);
683 if (m
->control_pid
> 0)
685 "%sControl PID: "PID_FMT
"\n",
686 prefix
, m
->control_pid
);
688 exec_context_dump(&m
->exec_context
, f
, prefix
);
689 kill_context_dump(&m
->kill_context
, f
, prefix
);
692 static int mount_spawn(Mount
*m
, ExecCommand
*c
, pid_t
*_pid
) {
695 ExecParameters exec_params
= {
696 .apply_permissions
= true,
697 .apply_chroot
= true,
698 .apply_tty_stdin
= true,
699 .bus_endpoint_fd
= -1,
706 (void) unit_realize_cgroup(UNIT(m
));
707 if (m
->reset_cpu_usage
) {
708 (void) unit_reset_cpu_usage(UNIT(m
));
709 m
->reset_cpu_usage
= false;
712 r
= unit_setup_exec_runtime(UNIT(m
));
716 r
= mount_arm_timer(m
);
720 exec_params
.environment
= UNIT(m
)->manager
->environment
;
721 exec_params
.confirm_spawn
= UNIT(m
)->manager
->confirm_spawn
;
722 exec_params
.cgroup_supported
= UNIT(m
)->manager
->cgroup_supported
;
723 exec_params
.cgroup_path
= UNIT(m
)->cgroup_path
;
724 exec_params
.cgroup_delegate
= m
->cgroup_context
.delegate
;
725 exec_params
.runtime_prefix
= manager_get_runtime_prefix(UNIT(m
)->manager
);
727 r
= exec_spawn(UNIT(m
),
736 r
= unit_watch_pid(UNIT(m
), pid
);
738 /* FIXME: we need to do something here */
746 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
751 static void mount_enter_dead(Mount
*m
, MountResult f
) {
754 if (f
!= MOUNT_SUCCESS
)
757 exec_runtime_destroy(m
->exec_runtime
);
758 m
->exec_runtime
= exec_runtime_unref(m
->exec_runtime
);
760 exec_context_destroy_runtime_directory(&m
->exec_context
, manager_get_runtime_prefix(UNIT(m
)->manager
));
762 mount_set_state(m
, m
->result
!= MOUNT_SUCCESS
? MOUNT_FAILED
: MOUNT_DEAD
);
765 static void mount_enter_mounted(Mount
*m
, MountResult f
) {
768 if (f
!= MOUNT_SUCCESS
)
771 mount_set_state(m
, MOUNT_MOUNTED
);
774 static void mount_enter_signal(Mount
*m
, MountState state
, MountResult f
) {
779 if (f
!= MOUNT_SUCCESS
)
782 r
= unit_kill_context(
785 (state
!= MOUNT_MOUNTING_SIGTERM
&& state
!= MOUNT_UNMOUNTING_SIGTERM
&& state
!= MOUNT_REMOUNTING_SIGTERM
) ?
786 KILL_KILL
: KILL_TERMINATE
,
794 r
= mount_arm_timer(m
);
798 mount_set_state(m
, state
);
799 } else if (state
== MOUNT_REMOUNTING_SIGTERM
)
800 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
801 else if (state
== MOUNT_REMOUNTING_SIGKILL
)
802 mount_enter_mounted(m
, MOUNT_SUCCESS
);
803 else if (state
== MOUNT_MOUNTING_SIGTERM
)
804 mount_enter_signal(m
, MOUNT_MOUNTING_SIGKILL
, MOUNT_SUCCESS
);
805 else if (state
== MOUNT_UNMOUNTING_SIGTERM
)
806 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
808 mount_enter_dead(m
, MOUNT_SUCCESS
);
813 log_unit_warning_errno(UNIT(m
), r
, "Failed to kill processes: %m");
815 if (state
== MOUNT_REMOUNTING_SIGTERM
|| state
== MOUNT_REMOUNTING_SIGKILL
)
816 mount_enter_mounted(m
, MOUNT_FAILURE_RESOURCES
);
818 mount_enter_dead(m
, MOUNT_FAILURE_RESOURCES
);
821 static void mount_enter_unmounting(Mount
*m
) {
826 /* Start counting our attempts */
827 if (!IN_SET(m
->state
,
829 MOUNT_UNMOUNTING_SIGTERM
,
830 MOUNT_UNMOUNTING_SIGKILL
))
831 m
->n_retry_umount
= 0;
833 m
->control_command_id
= MOUNT_EXEC_UNMOUNT
;
834 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_UNMOUNT
;
836 r
= exec_command_set(m
->control_command
, UMOUNT_PATH
, m
->where
, NULL
);
840 mount_unwatch_control_pid(m
);
842 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
846 mount_set_state(m
, MOUNT_UNMOUNTING
);
851 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'umount' task: %m");
852 mount_enter_mounted(m
, MOUNT_FAILURE_RESOURCES
);
855 static void mount_enter_mounting(Mount
*m
) {
861 m
->control_command_id
= MOUNT_EXEC_MOUNT
;
862 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_MOUNT
;
864 r
= unit_fail_if_symlink(UNIT(m
), m
->where
);
868 (void) mkdir_p_label(m
->where
, m
->directory_mode
);
870 unit_warn_if_dir_nonempty(UNIT(m
), m
->where
);
872 /* Create the source directory for bind-mounts if needed */
873 p
= get_mount_parameters_fragment(m
);
874 if (p
&& mount_is_bind(p
))
875 (void) mkdir_p_label(p
->what
, m
->directory_mode
);
877 if (m
->from_fragment
) {
878 _cleanup_free_
char *opts
= NULL
;
880 r
= fstab_filter_options(m
->parameters_fragment
.options
,
881 "nofail\0" "noauto\0" "auto\0", NULL
, NULL
, &opts
);
885 r
= exec_command_set(m
->control_command
, MOUNT_PATH
,
886 m
->parameters_fragment
.what
, m
->where
, NULL
);
887 if (r
>= 0 && m
->sloppy_options
)
888 r
= exec_command_append(m
->control_command
, "-s", NULL
);
889 if (r
>= 0 && m
->parameters_fragment
.fstype
)
890 r
= exec_command_append(m
->control_command
, "-t", m
->parameters_fragment
.fstype
, NULL
);
891 if (r
>= 0 && !isempty(opts
))
892 r
= exec_command_append(m
->control_command
, "-o", opts
, NULL
);
899 mount_unwatch_control_pid(m
);
901 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
905 mount_set_state(m
, MOUNT_MOUNTING
);
910 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'mount' task: %m");
911 mount_enter_dead(m
, MOUNT_FAILURE_RESOURCES
);
914 static void mount_enter_remounting(Mount
*m
) {
919 m
->control_command_id
= MOUNT_EXEC_REMOUNT
;
920 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_REMOUNT
;
922 if (m
->from_fragment
) {
925 if (m
->parameters_fragment
.options
)
926 o
= strjoina("remount,", m
->parameters_fragment
.options
);
930 r
= exec_command_set(m
->control_command
, MOUNT_PATH
,
931 m
->parameters_fragment
.what
, m
->where
,
933 if (r
>= 0 && m
->sloppy_options
)
934 r
= exec_command_append(m
->control_command
, "-s", NULL
);
935 if (r
>= 0 && m
->parameters_fragment
.fstype
)
936 r
= exec_command_append(m
->control_command
, "-t", m
->parameters_fragment
.fstype
, NULL
);
943 mount_unwatch_control_pid(m
);
945 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
949 mount_set_state(m
, MOUNT_REMOUNTING
);
954 log_unit_warning_errno(UNIT(m
), r
, "Failed to run 'remount' task: %m");
955 m
->reload_result
= MOUNT_FAILURE_RESOURCES
;
956 mount_enter_mounted(m
, MOUNT_SUCCESS
);
959 static int mount_start(Unit
*u
) {
964 /* We cannot fulfill this request right now, try again later
966 if (m
->state
== MOUNT_UNMOUNTING
||
967 m
->state
== MOUNT_UNMOUNTING_SIGTERM
||
968 m
->state
== MOUNT_UNMOUNTING_SIGKILL
||
969 m
->state
== MOUNT_MOUNTING_SIGTERM
||
970 m
->state
== MOUNT_MOUNTING_SIGKILL
)
974 if (m
->state
== MOUNT_MOUNTING
)
977 assert(m
->state
== MOUNT_DEAD
|| m
->state
== MOUNT_FAILED
);
979 m
->result
= MOUNT_SUCCESS
;
980 m
->reload_result
= MOUNT_SUCCESS
;
981 m
->reset_cpu_usage
= true;
983 mount_enter_mounting(m
);
987 static int mount_stop(Unit
*u
) {
993 if (m
->state
== MOUNT_UNMOUNTING
||
994 m
->state
== MOUNT_UNMOUNTING_SIGKILL
||
995 m
->state
== MOUNT_UNMOUNTING_SIGTERM
||
996 m
->state
== MOUNT_MOUNTING_SIGTERM
||
997 m
->state
== MOUNT_MOUNTING_SIGKILL
)
1000 assert(m
->state
== MOUNT_MOUNTING
||
1001 m
->state
== MOUNT_MOUNTING_DONE
||
1002 m
->state
== MOUNT_MOUNTED
||
1003 m
->state
== MOUNT_REMOUNTING
||
1004 m
->state
== MOUNT_REMOUNTING_SIGTERM
||
1005 m
->state
== MOUNT_REMOUNTING_SIGKILL
);
1007 mount_enter_unmounting(m
);
1011 static int mount_reload(Unit
*u
) {
1012 Mount
*m
= MOUNT(u
);
1016 if (m
->state
== MOUNT_MOUNTING_DONE
)
1019 assert(m
->state
== MOUNT_MOUNTED
);
1021 mount_enter_remounting(m
);
1025 static int mount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1026 Mount
*m
= MOUNT(u
);
1032 unit_serialize_item(u
, f
, "state", mount_state_to_string(m
->state
));
1033 unit_serialize_item(u
, f
, "result", mount_result_to_string(m
->result
));
1034 unit_serialize_item(u
, f
, "reload-result", mount_result_to_string(m
->reload_result
));
1036 if (m
->control_pid
> 0)
1037 unit_serialize_item_format(u
, f
, "control-pid", PID_FMT
, m
->control_pid
);
1039 if (m
->control_command_id
>= 0)
1040 unit_serialize_item(u
, f
, "control-command", mount_exec_command_to_string(m
->control_command_id
));
1045 static int mount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1046 Mount
*m
= MOUNT(u
);
1053 if (streq(key
, "state")) {
1056 if ((state
= mount_state_from_string(value
)) < 0)
1057 log_unit_debug(u
, "Failed to parse state value: %s", value
);
1059 m
->deserialized_state
= state
;
1060 } else if (streq(key
, "result")) {
1063 f
= mount_result_from_string(value
);
1065 log_unit_debug(u
, "Failed to parse result value: %s", value
);
1066 else if (f
!= MOUNT_SUCCESS
)
1069 } else if (streq(key
, "reload-result")) {
1072 f
= mount_result_from_string(value
);
1074 log_unit_debug(u
, "Failed to parse reload result value: %s", value
);
1075 else if (f
!= MOUNT_SUCCESS
)
1076 m
->reload_result
= f
;
1078 } else if (streq(key
, "control-pid")) {
1081 if (parse_pid(value
, &pid
) < 0)
1082 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
1084 m
->control_pid
= pid
;
1085 } else if (streq(key
, "control-command")) {
1086 MountExecCommand id
;
1088 id
= mount_exec_command_from_string(value
);
1090 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
1092 m
->control_command_id
= id
;
1093 m
->control_command
= m
->exec_command
+ id
;
1096 log_unit_debug(u
, "Unknown serialization key: %s", key
);
1101 _pure_
static UnitActiveState
mount_active_state(Unit
*u
) {
1104 return state_translation_table
[MOUNT(u
)->state
];
1107 _pure_
static const char *mount_sub_state_to_string(Unit
*u
) {
1110 return mount_state_to_string(MOUNT(u
)->state
);
1113 _pure_
static bool mount_check_gc(Unit
*u
) {
1114 Mount
*m
= MOUNT(u
);
1118 return m
->from_proc_self_mountinfo
;
1121 static void mount_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1122 Mount
*m
= MOUNT(u
);
1128 if (pid
!= m
->control_pid
)
1133 if (is_clean_exit(code
, status
, NULL
))
1135 else if (code
== CLD_EXITED
)
1136 f
= MOUNT_FAILURE_EXIT_CODE
;
1137 else if (code
== CLD_KILLED
)
1138 f
= MOUNT_FAILURE_SIGNAL
;
1139 else if (code
== CLD_DUMPED
)
1140 f
= MOUNT_FAILURE_CORE_DUMP
;
1142 assert_not_reached("Unknown code");
1144 if (f
!= MOUNT_SUCCESS
)
1147 if (m
->control_command
) {
1148 exec_status_exit(&m
->control_command
->exec_status
, &m
->exec_context
, pid
, code
, status
);
1150 m
->control_command
= NULL
;
1151 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
1154 log_unit_full(u
, f
== MOUNT_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
, 0,
1155 "Mount process exited, code=%s status=%i", sigchld_code_to_string(code
), status
);
1157 /* Note that mount(8) returning and the kernel sending us a
1158 * mount table change event might happen out-of-order. If an
1159 * operation succeed we assume the kernel will follow soon too
1160 * and already change into the resulting state. If it fails
1161 * we check if the kernel still knows about the mount. and
1162 * change state accordingly. */
1166 case MOUNT_MOUNTING
:
1167 case MOUNT_MOUNTING_DONE
:
1168 case MOUNT_MOUNTING_SIGKILL
:
1169 case MOUNT_MOUNTING_SIGTERM
:
1171 if (f
== MOUNT_SUCCESS
)
1172 mount_enter_mounted(m
, f
);
1173 else if (m
->from_proc_self_mountinfo
)
1174 mount_enter_mounted(m
, f
);
1176 mount_enter_dead(m
, f
);
1179 case MOUNT_REMOUNTING
:
1180 case MOUNT_REMOUNTING_SIGKILL
:
1181 case MOUNT_REMOUNTING_SIGTERM
:
1183 m
->reload_result
= f
;
1184 if (m
->from_proc_self_mountinfo
)
1185 mount_enter_mounted(m
, MOUNT_SUCCESS
);
1187 mount_enter_dead(m
, MOUNT_SUCCESS
);
1191 case MOUNT_UNMOUNTING
:
1192 case MOUNT_UNMOUNTING_SIGKILL
:
1193 case MOUNT_UNMOUNTING_SIGTERM
:
1195 if (f
== MOUNT_SUCCESS
) {
1197 if (m
->from_proc_self_mountinfo
) {
1199 /* Still a mount point? If so, let's
1200 * try again. Most likely there were
1201 * multiple mount points stacked on
1202 * top of each other. Note that due to
1203 * the io event priority logic we can
1204 * be sure the new mountinfo is loaded
1205 * before we process the SIGCHLD for
1206 * the mount command. */
1208 if (m
->n_retry_umount
< RETRY_UMOUNT_MAX
) {
1209 log_unit_debug(u
, "Mount still present, trying again.");
1210 m
->n_retry_umount
++;
1211 mount_enter_unmounting(m
);
1213 log_unit_debug(u
, "Mount still present after %u attempts to unmount, giving up.", m
->n_retry_umount
);
1214 mount_enter_mounted(m
, f
);
1217 mount_enter_dead(m
, f
);
1219 } else if (m
->from_proc_self_mountinfo
)
1220 mount_enter_mounted(m
, f
);
1222 mount_enter_dead(m
, f
);
1226 assert_not_reached("Uh, control process died at wrong time.");
1229 /* Notify clients about changed exit status */
1230 unit_add_to_dbus_queue(u
);
1233 static int mount_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
1234 Mount
*m
= MOUNT(userdata
);
1237 assert(m
->timer_event_source
== source
);
1241 case MOUNT_MOUNTING
:
1242 case MOUNT_MOUNTING_DONE
:
1243 log_unit_warning(UNIT(m
), "Mounting timed out. Stopping.");
1244 mount_enter_signal(m
, MOUNT_MOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1247 case MOUNT_REMOUNTING
:
1248 log_unit_warning(UNIT(m
), "Remounting timed out. Stopping.");
1249 m
->reload_result
= MOUNT_FAILURE_TIMEOUT
;
1250 mount_enter_mounted(m
, MOUNT_SUCCESS
);
1253 case MOUNT_UNMOUNTING
:
1254 log_unit_warning(UNIT(m
), "Unmounting timed out. Stopping.");
1255 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1258 case MOUNT_MOUNTING_SIGTERM
:
1259 if (m
->kill_context
.send_sigkill
) {
1260 log_unit_warning(UNIT(m
), "Mounting timed out. Killing.");
1261 mount_enter_signal(m
, MOUNT_MOUNTING_SIGKILL
, MOUNT_FAILURE_TIMEOUT
);
1263 log_unit_warning(UNIT(m
), "Mounting timed out. Skipping SIGKILL. Ignoring.");
1265 if (m
->from_proc_self_mountinfo
)
1266 mount_enter_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1268 mount_enter_dead(m
, MOUNT_FAILURE_TIMEOUT
);
1272 case MOUNT_REMOUNTING_SIGTERM
:
1273 if (m
->kill_context
.send_sigkill
) {
1274 log_unit_warning(UNIT(m
), "Remounting timed out. Killing.");
1275 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_FAILURE_TIMEOUT
);
1277 log_unit_warning(UNIT(m
), "Remounting timed out. Skipping SIGKILL. Ignoring.");
1279 if (m
->from_proc_self_mountinfo
)
1280 mount_enter_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1282 mount_enter_dead(m
, MOUNT_FAILURE_TIMEOUT
);
1286 case MOUNT_UNMOUNTING_SIGTERM
:
1287 if (m
->kill_context
.send_sigkill
) {
1288 log_unit_warning(UNIT(m
), "Unmounting timed out. Killing.");
1289 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_FAILURE_TIMEOUT
);
1291 log_unit_warning(UNIT(m
), "Unmounting timed out. Skipping SIGKILL. Ignoring.");
1293 if (m
->from_proc_self_mountinfo
)
1294 mount_enter_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1296 mount_enter_dead(m
, MOUNT_FAILURE_TIMEOUT
);
1300 case MOUNT_MOUNTING_SIGKILL
:
1301 case MOUNT_REMOUNTING_SIGKILL
:
1302 case MOUNT_UNMOUNTING_SIGKILL
:
1303 log_unit_warning(UNIT(m
),"Mount process still around after 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 assert_not_reached("Timeout at wrong time.");
1318 static int mount_setup_unit(
1322 const char *options
,
1326 _cleanup_free_
char *e
= NULL
, *w
= NULL
, *o
= NULL
, *f
= NULL
;
1327 bool load_extras
= false;
1329 bool delete, changed
= false;
1339 /* Ignore API mount points. They should never be referenced in
1340 * dependencies ever. */
1341 if (mount_point_is_api(where
) || mount_point_ignore(where
))
1344 if (streq(fstype
, "autofs"))
1347 /* probably some kind of swap, ignore */
1348 if (!is_path(where
))
1351 r
= unit_name_from_path(where
, ".mount", &e
);
1355 u
= manager_get_unit(m
, e
);
1359 u
= unit_new(m
, sizeof(Mount
));
1363 r
= unit_add_name(u
, e
);
1367 MOUNT(u
)->where
= strdup(where
);
1368 if (!MOUNT(u
)->where
) {
1373 u
->source_path
= strdup("/proc/self/mountinfo");
1374 if (!u
->source_path
) {
1379 if (m
->running_as
== MANAGER_SYSTEM
) {
1382 target
= mount_needs_network(options
, fstype
) ? SPECIAL_REMOTE_FS_TARGET
: SPECIAL_LOCAL_FS_TARGET
;
1383 r
= unit_add_dependency_by_name(u
, UNIT_BEFORE
, target
, NULL
, true);
1387 if (should_umount(MOUNT(u
))) {
1388 r
= unit_add_dependency_by_name(u
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, NULL
, true);
1394 unit_add_to_load_queue(u
);
1399 if (!MOUNT(u
)->where
) {
1400 MOUNT(u
)->where
= strdup(where
);
1401 if (!MOUNT(u
)->where
) {
1407 if (m
->running_as
== MANAGER_SYSTEM
&&
1408 mount_needs_network(options
, fstype
)) {
1409 /* _netdev option may have shown up late, or on a
1410 * remount. Add remote-fs dependencies, even though
1411 * local-fs ones may already be there. */
1412 unit_add_dependency_by_name(u
, UNIT_BEFORE
, SPECIAL_REMOTE_FS_TARGET
, NULL
, true);
1416 if (u
->load_state
== UNIT_NOT_FOUND
) {
1417 u
->load_state
= UNIT_LOADED
;
1420 /* Load in the extras later on, after we
1421 * finished initialization of the unit */
1428 o
= strdup(options
);
1430 if (!w
|| !o
|| !f
) {
1435 p
= &MOUNT(u
)->parameters_proc_self_mountinfo
;
1437 changed
= changed
||
1438 !streq_ptr(p
->options
, options
) ||
1439 !streq_ptr(p
->what
, what
) ||
1440 !streq_ptr(p
->fstype
, fstype
);
1443 MOUNT(u
)->is_mounted
= true;
1444 MOUNT(u
)->just_mounted
= !MOUNT(u
)->from_proc_self_mountinfo
;
1445 MOUNT(u
)->just_changed
= changed
;
1448 MOUNT(u
)->from_proc_self_mountinfo
= true;
1463 r
= mount_add_extras(MOUNT(u
));
1469 unit_add_to_dbus_queue(u
);
1474 log_warning_errno(r
, "Failed to set up mount unit: %m");
1482 static int mount_load_proc_self_mountinfo(Manager
*m
, bool set_flags
) {
1483 _cleanup_(mnt_free_tablep
) struct libmnt_table
*t
= NULL
;
1484 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*i
= NULL
;
1489 t
= mnt_new_table();
1493 i
= mnt_new_iter(MNT_ITER_FORWARD
);
1497 r
= mnt_table_parse_mtab(t
, NULL
);
1499 return log_error_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
1503 const char *device
, *path
, *options
, *fstype
;
1504 _cleanup_free_
char *d
= NULL
, *p
= NULL
;
1505 struct libmnt_fs
*fs
;
1508 k
= mnt_table_next_fs(t
, i
, &fs
);
1512 return log_error_errno(k
, "Failed to get next entry from /proc/self/mountinfo: %m");
1514 device
= mnt_fs_get_source(fs
);
1515 path
= mnt_fs_get_target(fs
);
1516 options
= mnt_fs_get_options(fs
);
1517 fstype
= mnt_fs_get_fstype(fs
);
1519 if (!device
|| !path
)
1522 if (cunescape(device
, UNESCAPE_RELAX
, &d
) < 0)
1525 if (cunescape(path
, UNESCAPE_RELAX
, &p
) < 0)
1528 (void) device_found_node(m
, d
, true, DEVICE_FOUND_MOUNT
, set_flags
);
1530 k
= mount_setup_unit(m
, d
, p
, options
, fstype
, set_flags
);
1531 if (r
== 0 && k
< 0)
1538 static void mount_shutdown(Manager
*m
) {
1541 m
->mount_event_source
= sd_event_source_unref(m
->mount_event_source
);
1542 m
->mount_utab_event_source
= sd_event_source_unref(m
->mount_utab_event_source
);
1544 if (m
->proc_self_mountinfo
) {
1545 fclose(m
->proc_self_mountinfo
);
1546 m
->proc_self_mountinfo
= NULL
;
1548 m
->utab_inotify_fd
= safe_close(m
->utab_inotify_fd
);
1551 static int mount_get_timeout(Unit
*u
, uint64_t *timeout
) {
1552 Mount
*m
= MOUNT(u
);
1555 if (!m
->timer_event_source
)
1558 r
= sd_event_source_get_time(m
->timer_event_source
, timeout
);
1565 static int mount_enumerate(Manager
*m
) {
1571 if (!m
->proc_self_mountinfo
) {
1572 m
->proc_self_mountinfo
= fopen("/proc/self/mountinfo", "re");
1573 if (!m
->proc_self_mountinfo
)
1576 r
= sd_event_add_io(m
->event
, &m
->mount_event_source
, fileno(m
->proc_self_mountinfo
), EPOLLPRI
, mount_dispatch_io
, m
);
1580 /* Dispatch this before we dispatch SIGCHLD, so that
1581 * we always get the events from /proc/self/mountinfo
1582 * before the SIGCHLD of /usr/bin/mount. */
1583 r
= sd_event_source_set_priority(m
->mount_event_source
, -10);
1587 (void) sd_event_source_set_description(m
->mount_event_source
, "mount-mountinfo-dispatch");
1590 if (m
->utab_inotify_fd
< 0) {
1591 m
->utab_inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
1592 if (m
->utab_inotify_fd
< 0) {
1597 (void) mkdir_p_label("/run/mount", 0755);
1599 r
= inotify_add_watch(m
->utab_inotify_fd
, "/run/mount", IN_MOVED_TO
);
1605 r
= sd_event_add_io(m
->event
, &m
->mount_utab_event_source
, m
->utab_inotify_fd
, EPOLLIN
, mount_dispatch_io
, m
);
1609 r
= sd_event_source_set_priority(m
->mount_utab_event_source
, -10);
1613 (void) sd_event_source_set_description(m
->mount_utab_event_source
, "mount-utab-dispatch");
1616 r
= mount_load_proc_self_mountinfo(m
, false);
1627 static int mount_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
1628 _cleanup_set_free_ Set
*around
= NULL
, *gone
= NULL
;
1629 Manager
*m
= userdata
;
1636 assert(revents
& (EPOLLPRI
| EPOLLIN
));
1638 /* The manager calls this for every fd event happening on the
1639 * /proc/self/mountinfo file, which informs us about mounting
1640 * table changes, and for /run/mount events which we watch
1641 * for mount options. */
1643 if (fd
== m
->utab_inotify_fd
) {
1644 bool rescan
= false;
1646 /* FIXME: We *really* need to replace this with
1647 * libmount's own API for this, we should not hardcode
1648 * internal behaviour of libmount here. */
1651 union inotify_event_buffer buffer
;
1652 struct inotify_event
*e
;
1655 l
= read(fd
, &buffer
, sizeof(buffer
));
1657 if (errno
== EAGAIN
|| errno
== EINTR
)
1660 log_error_errno(errno
, "Failed to read utab inotify: %m");
1664 FOREACH_INOTIFY_EVENT(e
, buffer
, l
) {
1665 /* Only care about changes to utab,
1666 * but we have to monitor the
1667 * directory to reliably get
1668 * notifications about when utab is
1669 * replaced using rename(2) */
1670 if ((e
->mask
& IN_Q_OVERFLOW
) || streq(e
->name
, "utab"))
1679 r
= mount_load_proc_self_mountinfo(m
, true);
1681 /* Reset flags, just in case, for later calls */
1682 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
]) {
1683 Mount
*mount
= MOUNT(u
);
1685 mount
->is_mounted
= mount
->just_mounted
= mount
->just_changed
= false;
1691 manager_dispatch_load_queue(m
);
1693 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
]) {
1694 Mount
*mount
= MOUNT(u
);
1696 if (!mount
->is_mounted
) {
1698 /* A mount point is not around right now. It
1699 * might be gone, or might never have
1702 if (mount
->from_proc_self_mountinfo
&&
1703 mount
->parameters_proc_self_mountinfo
.what
) {
1705 /* Remember that this device might just have disappeared */
1706 if (set_ensure_allocated(&gone
, &string_hash_ops
) < 0 ||
1707 set_put(gone
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
1708 log_oom(); /* we don't care too much about OOM here... */
1711 mount
->from_proc_self_mountinfo
= false;
1713 switch (mount
->state
) {
1716 /* This has just been unmounted by
1717 * somebody else, follow the state
1719 mount_enter_dead(mount
, MOUNT_SUCCESS
);
1726 } else if (mount
->just_mounted
|| mount
->just_changed
) {
1728 /* A mount point was added or changed */
1730 switch (mount
->state
) {
1734 /* This has just been mounted by
1735 * somebody else, follow the state
1737 mount_enter_mounted(mount
, MOUNT_SUCCESS
);
1740 case MOUNT_MOUNTING
:
1741 mount_set_state(mount
, MOUNT_MOUNTING_DONE
);
1745 /* Nothing really changed, but let's
1746 * issue an notification call
1747 * nonetheless, in case somebody is
1748 * waiting for this. (e.g. file system
1749 * ro/rw remounts.) */
1750 mount_set_state(mount
, mount
->state
);
1755 if (mount
->is_mounted
&&
1756 mount
->from_proc_self_mountinfo
&&
1757 mount
->parameters_proc_self_mountinfo
.what
) {
1759 if (set_ensure_allocated(&around
, &string_hash_ops
) < 0 ||
1760 set_put(around
, mount
->parameters_proc_self_mountinfo
.what
) < 0)
1764 /* Reset the flags for later calls */
1765 mount
->is_mounted
= mount
->just_mounted
= mount
->just_changed
= false;
1768 SET_FOREACH(what
, gone
, i
) {
1769 if (set_contains(around
, what
))
1772 /* Let the device units know that the device is no longer mounted */
1773 (void) device_found_node(m
, what
, false, DEVICE_FOUND_MOUNT
, true);
1779 static void mount_reset_failed(Unit
*u
) {
1780 Mount
*m
= MOUNT(u
);
1784 if (m
->state
== MOUNT_FAILED
)
1785 mount_set_state(m
, MOUNT_DEAD
);
1787 m
->result
= MOUNT_SUCCESS
;
1788 m
->reload_result
= MOUNT_SUCCESS
;
1791 static int mount_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
1792 return unit_kill_common(u
, who
, signo
, -1, MOUNT(u
)->control_pid
, error
);
1795 static const char* const mount_state_table
[_MOUNT_STATE_MAX
] = {
1796 [MOUNT_DEAD
] = "dead",
1797 [MOUNT_MOUNTING
] = "mounting",
1798 [MOUNT_MOUNTING_DONE
] = "mounting-done",
1799 [MOUNT_MOUNTED
] = "mounted",
1800 [MOUNT_REMOUNTING
] = "remounting",
1801 [MOUNT_UNMOUNTING
] = "unmounting",
1802 [MOUNT_MOUNTING_SIGTERM
] = "mounting-sigterm",
1803 [MOUNT_MOUNTING_SIGKILL
] = "mounting-sigkill",
1804 [MOUNT_REMOUNTING_SIGTERM
] = "remounting-sigterm",
1805 [MOUNT_REMOUNTING_SIGKILL
] = "remounting-sigkill",
1806 [MOUNT_UNMOUNTING_SIGTERM
] = "unmounting-sigterm",
1807 [MOUNT_UNMOUNTING_SIGKILL
] = "unmounting-sigkill",
1808 [MOUNT_FAILED
] = "failed"
1811 DEFINE_STRING_TABLE_LOOKUP(mount_state
, MountState
);
1813 static const char* const mount_exec_command_table
[_MOUNT_EXEC_COMMAND_MAX
] = {
1814 [MOUNT_EXEC_MOUNT
] = "ExecMount",
1815 [MOUNT_EXEC_UNMOUNT
] = "ExecUnmount",
1816 [MOUNT_EXEC_REMOUNT
] = "ExecRemount",
1819 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command
, MountExecCommand
);
1821 static const char* const mount_result_table
[_MOUNT_RESULT_MAX
] = {
1822 [MOUNT_SUCCESS
] = "success",
1823 [MOUNT_FAILURE_RESOURCES
] = "resources",
1824 [MOUNT_FAILURE_TIMEOUT
] = "timeout",
1825 [MOUNT_FAILURE_EXIT_CODE
] = "exit-code",
1826 [MOUNT_FAILURE_SIGNAL
] = "signal",
1827 [MOUNT_FAILURE_CORE_DUMP
] = "core-dump"
1830 DEFINE_STRING_TABLE_LOOKUP(mount_result
, MountResult
);
1832 const UnitVTable mount_vtable
= {
1833 .object_size
= sizeof(Mount
),
1834 .exec_context_offset
= offsetof(Mount
, exec_context
),
1835 .cgroup_context_offset
= offsetof(Mount
, cgroup_context
),
1836 .kill_context_offset
= offsetof(Mount
, kill_context
),
1837 .exec_runtime_offset
= offsetof(Mount
, exec_runtime
),
1843 .private_section
= "Mount",
1846 .no_instances
= true,
1852 .coldplug
= mount_coldplug
,
1856 .start
= mount_start
,
1858 .reload
= mount_reload
,
1862 .serialize
= mount_serialize
,
1863 .deserialize_item
= mount_deserialize_item
,
1865 .active_state
= mount_active_state
,
1866 .sub_state_to_string
= mount_sub_state_to_string
,
1868 .check_gc
= mount_check_gc
,
1870 .sigchld_event
= mount_sigchld_event
,
1872 .reset_failed
= mount_reset_failed
,
1874 .bus_vtable
= bus_mount_vtable
,
1875 .bus_set_property
= bus_mount_set_property
,
1876 .bus_commit_properties
= bus_mount_commit_properties
,
1878 .get_timeout
= mount_get_timeout
,
1880 .can_transient
= true,
1882 .enumerate
= mount_enumerate
,
1883 .shutdown
= mount_shutdown
,
1885 .status_message_formats
= {
1886 .starting_stopping
= {
1887 [0] = "Mounting %s...",
1888 [1] = "Unmounting %s...",
1890 .finished_start_job
= {
1891 [JOB_DONE
] = "Mounted %s.",
1892 [JOB_FAILED
] = "Failed to mount %s.",
1893 [JOB_TIMEOUT
] = "Timed out mounting %s.",
1895 .finished_stop_job
= {
1896 [JOB_DONE
] = "Unmounted %s.",
1897 [JOB_FAILED
] = "Failed unmounting %s.",
1898 [JOB_TIMEOUT
] = "Timed out unmounting %s.",