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 /bin/mount is always called in
139 * 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 return 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
);
175 static void mount_unwatch_control_pid(Mount
*m
) {
178 if (m
->control_pid
<= 0)
181 unit_unwatch_pid(UNIT(m
), m
->control_pid
);
185 static void mount_parameters_done(MountParameters
*p
) {
192 p
->what
= p
->options
= p
->fstype
= NULL
;
195 static void mount_done(Unit
*u
) {
203 mount_parameters_done(&m
->parameters_proc_self_mountinfo
);
204 mount_parameters_done(&m
->parameters_fragment
);
206 m
->exec_runtime
= exec_runtime_unref(m
->exec_runtime
);
207 exec_command_done_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
208 m
->control_command
= NULL
;
210 mount_unwatch_control_pid(m
);
212 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
215 _pure_
static MountParameters
* get_mount_parameters_fragment(Mount
*m
) {
218 if (m
->from_fragment
)
219 return &m
->parameters_fragment
;
224 _pure_
static MountParameters
* get_mount_parameters(Mount
*m
) {
227 if (m
->from_proc_self_mountinfo
)
228 return &m
->parameters_proc_self_mountinfo
;
230 return get_mount_parameters_fragment(m
);
233 static int mount_add_mount_links(Mount
*m
) {
234 _cleanup_free_
char *parent
= NULL
;
243 if (!path_equal(m
->where
, "/")) {
244 /* Adds in links to other mount points that might lie further
245 * up in the hierarchy */
246 r
= path_get_parent(m
->where
, &parent
);
250 r
= unit_require_mounts_for(UNIT(m
), parent
);
255 /* Adds in links to other mount points that might be needed
256 * for the source path (if this is a bind mount) to be
258 pm
= get_mount_parameters_fragment(m
);
259 if (pm
&& pm
->what
&&
260 path_is_absolute(pm
->what
) &&
261 !mount_is_network(pm
)) {
263 r
= unit_require_mounts_for(UNIT(m
), pm
->what
);
268 /* Adds in links to other units that use this path or paths
269 * further down in the hierarchy */
270 s
= manager_get_units_requiring_mounts_for(UNIT(m
)->manager
, m
->where
);
271 SET_FOREACH(other
, s
, i
) {
273 if (other
->load_state
!= UNIT_LOADED
)
276 if (other
== UNIT(m
))
279 r
= unit_add_dependency(other
, UNIT_AFTER
, UNIT(m
), true);
283 if (UNIT(m
)->fragment_path
) {
284 /* If we have fragment configuration, then make this dependency required */
285 r
= unit_add_dependency(other
, UNIT_REQUIRES
, UNIT(m
), true);
294 static int mount_add_device_links(Mount
*m
) {
296 bool device_wants_mount
= false;
301 p
= get_mount_parameters(m
);
308 if (mount_is_bind(p
))
311 if (!is_device_path(p
->what
))
314 if (path_equal(m
->where
, "/"))
317 if (mount_is_auto(p
) && UNIT(m
)->manager
->running_as
== SYSTEMD_SYSTEM
)
318 device_wants_mount
= true;
320 r
= unit_add_node_link(UNIT(m
), p
->what
, device_wants_mount
);
327 static int mount_add_quota_links(Mount
*m
) {
333 if (UNIT(m
)->manager
->running_as
!= SYSTEMD_SYSTEM
)
336 p
= get_mount_parameters_fragment(m
);
343 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTACHECK_SERVICE
, NULL
, true);
347 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTAON_SERVICE
, NULL
, true);
354 static bool should_umount(Mount
*m
) {
357 if (path_equal(m
->where
, "/") ||
358 path_equal(m
->where
, "/usr"))
361 p
= get_mount_parameters(m
);
362 if (p
&& fstab_test_option(p
->options
, "x-initrd.mount\0") &&
369 static int mount_add_default_dependencies(Mount
*m
) {
370 const char *after
, *after2
, *online
;
376 if (UNIT(m
)->manager
->running_as
!= SYSTEMD_SYSTEM
)
379 /* We do not add any default dependencies to / and /usr, since
380 * they are guaranteed to stay mounted the whole time, since
381 * our system is on it. Also, don't bother with anything
382 * mounted below virtual file systems, it's also going to be
383 * virtual, and hence not worth the effort. */
384 if (path_equal(m
->where
, "/") ||
385 path_equal(m
->where
, "/usr") ||
386 path_startswith(m
->where
, "/proc") ||
387 path_startswith(m
->where
, "/sys") ||
388 path_startswith(m
->where
, "/dev"))
391 p
= get_mount_parameters(m
);
395 if (mount_is_network(p
)) {
396 after
= SPECIAL_REMOTE_FS_PRE_TARGET
;
397 after2
= SPECIAL_NETWORK_TARGET
;
398 online
= SPECIAL_NETWORK_ONLINE_TARGET
;
400 after
= SPECIAL_LOCAL_FS_PRE_TARGET
;
405 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, after
, NULL
, true);
410 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, after2
, NULL
, true);
416 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_WANTS
, UNIT_AFTER
, online
, NULL
, true);
421 if (should_umount(m
)) {
422 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, NULL
, true);
430 static int mount_verify(Mount
*m
) {
431 _cleanup_free_
char *e
= NULL
;
436 if (UNIT(m
)->load_state
!= UNIT_LOADED
)
439 if (!m
->from_fragment
&& !m
->from_proc_self_mountinfo
)
442 e
= unit_name_from_path(m
->where
, ".mount");
446 b
= unit_has_name(UNIT(m
), e
);
448 log_unit_error(UNIT(m
)->id
, "%s's Where= setting doesn't match unit name. Refusing.", UNIT(m
)->id
);
452 if (mount_point_is_api(m
->where
) || mount_point_ignore(m
->where
)) {
453 log_unit_error(UNIT(m
)->id
, "Cannot create mount unit for API file system %s. Refusing.", m
->where
);
457 if (UNIT(m
)->fragment_path
&& !m
->parameters_fragment
.what
) {
458 log_unit_error(UNIT(m
)->id
, "%s's What setting is missing. Refusing.", UNIT(m
)->id
);
462 if (m
->exec_context
.pam_name
&& m
->kill_context
.kill_mode
!= KILL_CONTROL_GROUP
) {
463 log_unit_error(UNIT(m
)->id
, "%s has PAM enabled. Kill mode must be set to control-group'. Refusing.",UNIT(m
)->id
);
470 static int mount_add_extras(Mount
*m
) {
476 if (u
->fragment_path
)
477 m
->from_fragment
= true;
480 m
->where
= unit_name_to_path(u
->id
);
485 path_kill_slashes(m
->where
);
487 if (!u
->description
) {
488 r
= unit_set_description(u
, m
->where
);
493 r
= mount_add_device_links(m
);
497 r
= mount_add_mount_links(m
);
501 r
= mount_add_quota_links(m
);
505 r
= unit_patch_contexts(u
);
509 r
= unit_add_exec_dependencies(u
, &m
->exec_context
);
513 r
= unit_add_default_slice(u
, &m
->cgroup_context
);
517 if (u
->default_dependencies
) {
518 r
= mount_add_default_dependencies(m
);
526 static int mount_load(Unit
*u
) {
531 assert(u
->load_state
== UNIT_STUB
);
533 if (m
->from_proc_self_mountinfo
)
534 r
= unit_load_fragment_and_dropin_optional(u
);
536 r
= unit_load_fragment_and_dropin(u
);
541 /* This is a new unit? Then let's add in some extras */
542 if (u
->load_state
== UNIT_LOADED
) {
543 r
= mount_add_extras(m
);
548 return mount_verify(m
);
551 static int mount_notify_automount(Mount
*m
, MountState old_state
, MountState state
) {
558 SET_FOREACH(p
, UNIT(m
)->dependencies
[UNIT_TRIGGERED_BY
], i
)
559 if (p
->type
== UNIT_AUTOMOUNT
) {
560 r
= automount_update_mount(AUTOMOUNT(p
), old_state
, state
);
568 static void mount_set_state(Mount
*m
, MountState state
) {
569 MountState old_state
;
572 old_state
= m
->state
;
575 if (state
!= MOUNT_MOUNTING
&&
576 state
!= MOUNT_MOUNTING_DONE
&&
577 state
!= MOUNT_REMOUNTING
&&
578 state
!= MOUNT_UNMOUNTING
&&
579 state
!= MOUNT_MOUNTING_SIGTERM
&&
580 state
!= MOUNT_MOUNTING_SIGKILL
&&
581 state
!= MOUNT_UNMOUNTING_SIGTERM
&&
582 state
!= MOUNT_UNMOUNTING_SIGKILL
&&
583 state
!= MOUNT_REMOUNTING_SIGTERM
&&
584 state
!= MOUNT_REMOUNTING_SIGKILL
) {
585 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
586 mount_unwatch_control_pid(m
);
587 m
->control_command
= NULL
;
588 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
591 mount_notify_automount(m
, old_state
, state
);
593 if (state
!= old_state
)
594 log_unit_debug(UNIT(m
)->id
,
595 "%s changed %s -> %s",
597 mount_state_to_string(old_state
),
598 mount_state_to_string(state
));
600 unit_notify(UNIT(m
), state_translation_table
[old_state
], state_translation_table
[state
], m
->reload_result
== MOUNT_SUCCESS
);
601 m
->reload_result
= MOUNT_SUCCESS
;
604 static int mount_coldplug(Unit
*u
, Hashmap
*deferred_work
) {
606 MountState new_state
= MOUNT_DEAD
;
610 assert(m
->state
== MOUNT_DEAD
);
612 if (m
->deserialized_state
!= m
->state
)
613 new_state
= m
->deserialized_state
;
614 else if (m
->from_proc_self_mountinfo
)
615 new_state
= MOUNT_MOUNTED
;
617 if (new_state
== m
->state
)
620 if (new_state
== MOUNT_MOUNTING
||
621 new_state
== MOUNT_MOUNTING_DONE
||
622 new_state
== MOUNT_REMOUNTING
||
623 new_state
== MOUNT_UNMOUNTING
||
624 new_state
== MOUNT_MOUNTING_SIGTERM
||
625 new_state
== MOUNT_MOUNTING_SIGKILL
||
626 new_state
== MOUNT_UNMOUNTING_SIGTERM
||
627 new_state
== MOUNT_UNMOUNTING_SIGKILL
||
628 new_state
== MOUNT_REMOUNTING_SIGTERM
||
629 new_state
== MOUNT_REMOUNTING_SIGKILL
) {
631 if (m
->control_pid
<= 0)
634 r
= unit_watch_pid(UNIT(m
), m
->control_pid
);
638 r
= mount_arm_timer(m
);
643 mount_set_state(m
, new_state
);
647 static void mount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
654 p
= get_mount_parameters(m
);
657 "%sMount State: %s\n"
661 "%sFile System Type: %s\n"
663 "%sFrom /proc/self/mountinfo: %s\n"
664 "%sFrom fragment: %s\n"
665 "%sDirectoryMode: %04o\n",
666 prefix
, mount_state_to_string(m
->state
),
667 prefix
, mount_result_to_string(m
->result
),
669 prefix
, p
? strna(p
->what
) : "n/a",
670 prefix
, p
? strna(p
->fstype
) : "n/a",
671 prefix
, p
? strna(p
->options
) : "n/a",
672 prefix
, yes_no(m
->from_proc_self_mountinfo
),
673 prefix
, yes_no(m
->from_fragment
),
674 prefix
, m
->directory_mode
);
676 if (m
->control_pid
> 0)
678 "%sControl PID: "PID_FMT
"\n",
679 prefix
, m
->control_pid
);
681 exec_context_dump(&m
->exec_context
, f
, prefix
);
682 kill_context_dump(&m
->kill_context
, f
, prefix
);
685 static int mount_spawn(Mount
*m
, ExecCommand
*c
, pid_t
*_pid
) {
688 ExecParameters exec_params
= {
689 .apply_permissions
= true,
690 .apply_chroot
= true,
691 .apply_tty_stdin
= true,
698 (void) unit_realize_cgroup(UNIT(m
));
699 if (m
->reset_cpu_usage
) {
700 (void) unit_reset_cpu_usage(UNIT(m
));
701 m
->reset_cpu_usage
= false;
704 r
= unit_setup_exec_runtime(UNIT(m
));
708 r
= mount_arm_timer(m
);
712 exec_params
.environment
= UNIT(m
)->manager
->environment
;
713 exec_params
.confirm_spawn
= UNIT(m
)->manager
->confirm_spawn
;
714 exec_params
.cgroup_supported
= UNIT(m
)->manager
->cgroup_supported
;
715 exec_params
.cgroup_path
= UNIT(m
)->cgroup_path
;
716 exec_params
.cgroup_delegate
= m
->cgroup_context
.delegate
;
717 exec_params
.runtime_prefix
= manager_get_runtime_prefix(UNIT(m
)->manager
);
718 exec_params
.unit_id
= UNIT(m
)->id
;
728 r
= unit_watch_pid(UNIT(m
), pid
);
730 /* FIXME: we need to do something here */
738 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
743 static void mount_enter_dead(Mount
*m
, MountResult f
) {
746 if (f
!= MOUNT_SUCCESS
)
749 exec_runtime_destroy(m
->exec_runtime
);
750 m
->exec_runtime
= exec_runtime_unref(m
->exec_runtime
);
752 exec_context_destroy_runtime_directory(&m
->exec_context
, manager_get_runtime_prefix(UNIT(m
)->manager
));
754 mount_set_state(m
, m
->result
!= MOUNT_SUCCESS
? MOUNT_FAILED
: MOUNT_DEAD
);
757 static void mount_enter_mounted(Mount
*m
, MountResult f
) {
760 if (f
!= MOUNT_SUCCESS
)
763 mount_set_state(m
, MOUNT_MOUNTED
);
766 static void mount_enter_signal(Mount
*m
, MountState state
, MountResult f
) {
771 if (f
!= MOUNT_SUCCESS
)
774 r
= unit_kill_context(
777 (state
!= MOUNT_MOUNTING_SIGTERM
&& state
!= MOUNT_UNMOUNTING_SIGTERM
&& state
!= MOUNT_REMOUNTING_SIGTERM
) ?
778 KILL_KILL
: KILL_TERMINATE
,
786 r
= mount_arm_timer(m
);
790 mount_set_state(m
, state
);
791 } else if (state
== MOUNT_REMOUNTING_SIGTERM
)
792 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
793 else if (state
== MOUNT_REMOUNTING_SIGKILL
)
794 mount_enter_mounted(m
, MOUNT_SUCCESS
);
795 else if (state
== MOUNT_MOUNTING_SIGTERM
)
796 mount_enter_signal(m
, MOUNT_MOUNTING_SIGKILL
, MOUNT_SUCCESS
);
797 else if (state
== MOUNT_UNMOUNTING_SIGTERM
)
798 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_SUCCESS
);
800 mount_enter_dead(m
, MOUNT_SUCCESS
);
805 log_unit_warning(UNIT(m
)->id
,
806 "%s failed to kill processes: %s", UNIT(m
)->id
, strerror(-r
));
808 if (state
== MOUNT_REMOUNTING_SIGTERM
|| state
== MOUNT_REMOUNTING_SIGKILL
)
809 mount_enter_mounted(m
, MOUNT_FAILURE_RESOURCES
);
811 mount_enter_dead(m
, MOUNT_FAILURE_RESOURCES
);
814 void warn_if_dir_nonempty(const char *unit
, const char* where
) {
820 r
= dir_is_empty(where
);
824 log_unit_struct(unit
,
826 LOG_MESSAGE_ID(SD_MESSAGE_OVERMOUNTING
),
827 LOG_MESSAGE("%s: Directory %s to mount over is not empty, mounting anyway.",
832 log_unit_warning(unit
,
833 "MESSAGE=Failed to check directory %s: %s",
834 where
, strerror(-r
));
837 static int fail_if_symlink(const char *unit
, const char* where
) {
840 if (is_symlink(where
) > 0) {
841 log_unit_struct(unit
,
843 LOG_MESSAGE_ID(SD_MESSAGE_OVERMOUNTING
),
844 LOG_MESSAGE("%s: Mount on symlink %s not allowed.",
854 static void mount_enter_unmounting(Mount
*m
) {
859 /* Start counting our attempts */
860 if (!IN_SET(m
->state
,
862 MOUNT_UNMOUNTING_SIGTERM
,
863 MOUNT_UNMOUNTING_SIGKILL
))
864 m
->n_retry_umount
= 0;
866 m
->control_command_id
= MOUNT_EXEC_UNMOUNT
;
867 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_UNMOUNT
;
869 r
= exec_command_set(m
->control_command
, "/bin/umount", m
->where
, NULL
);
870 if (r
>= 0 && UNIT(m
)->manager
->running_as
== SYSTEMD_SYSTEM
)
871 r
= exec_command_append(m
->control_command
, "-n", NULL
);
875 mount_unwatch_control_pid(m
);
877 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
881 mount_set_state(m
, MOUNT_UNMOUNTING
);
886 log_unit_warning(UNIT(m
)->id
,
887 "%s failed to run 'umount' task: %s",
888 UNIT(m
)->id
, strerror(-r
));
889 mount_enter_mounted(m
, MOUNT_FAILURE_RESOURCES
);
892 static void mount_enter_mounting(Mount
*m
) {
898 m
->control_command_id
= MOUNT_EXEC_MOUNT
;
899 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_MOUNT
;
901 mkdir_p_label(m
->where
, m
->directory_mode
);
903 warn_if_dir_nonempty(m
->meta
.id
, m
->where
);
905 /* Create the source directory for bind-mounts if needed */
906 p
= get_mount_parameters_fragment(m
);
907 if (p
&& mount_is_bind(p
))
908 mkdir_p_label(p
->what
, m
->directory_mode
);
910 r
= fail_if_symlink(m
->meta
.id
, m
->where
);
914 if (m
->from_fragment
) {
915 _cleanup_free_
char *opts
= NULL
;
917 r
= fstab_filter_options(m
->parameters_fragment
.options
,
918 "nofail\0" "noauto\0" "auto\0", NULL
, NULL
, &opts
);
922 r
= exec_command_set(m
->control_command
, "/bin/mount",
923 m
->parameters_fragment
.what
, m
->where
, NULL
);
924 if (r
>= 0 && UNIT(m
)->manager
->running_as
== SYSTEMD_SYSTEM
)
925 r
= exec_command_append(m
->control_command
, "-n", NULL
);
926 if (r
>= 0 && m
->sloppy_options
)
927 r
= exec_command_append(m
->control_command
, "-s", NULL
);
928 if (r
>= 0 && m
->parameters_fragment
.fstype
)
929 r
= exec_command_append(m
->control_command
, "-t", m
->parameters_fragment
.fstype
, NULL
);
930 if (r
>= 0 && !isempty(opts
))
931 r
= exec_command_append(m
->control_command
, "-o", opts
, NULL
);
938 mount_unwatch_control_pid(m
);
940 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
944 mount_set_state(m
, MOUNT_MOUNTING
);
949 log_unit_warning(UNIT(m
)->id
,
950 "%s failed to run 'mount' task: %s",
951 UNIT(m
)->id
, strerror(-r
));
952 mount_enter_dead(m
, MOUNT_FAILURE_RESOURCES
);
955 static void mount_enter_remounting(Mount
*m
) {
960 m
->control_command_id
= MOUNT_EXEC_REMOUNT
;
961 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_REMOUNT
;
963 if (m
->from_fragment
) {
966 if (m
->parameters_fragment
.options
)
967 o
= strjoina("remount,", m
->parameters_fragment
.options
);
971 r
= exec_command_set(m
->control_command
, "/bin/mount",
972 m
->parameters_fragment
.what
, m
->where
,
974 if (r
>= 0 && UNIT(m
)->manager
->running_as
== SYSTEMD_SYSTEM
)
975 r
= exec_command_append(m
->control_command
, "-n", NULL
);
976 if (r
>= 0 && m
->sloppy_options
)
977 r
= exec_command_append(m
->control_command
, "-s", NULL
);
978 if (r
>= 0 && m
->parameters_fragment
.fstype
)
979 r
= exec_command_append(m
->control_command
, "-t", m
->parameters_fragment
.fstype
, NULL
);
986 mount_unwatch_control_pid(m
);
988 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
992 mount_set_state(m
, MOUNT_REMOUNTING
);
997 log_unit_warning(UNIT(m
)->id
,
998 "%s failed to run 'remount' task: %s",
999 UNIT(m
)->id
, strerror(-r
));
1000 m
->reload_result
= MOUNT_FAILURE_RESOURCES
;
1001 mount_enter_mounted(m
, MOUNT_SUCCESS
);
1004 static int mount_start(Unit
*u
) {
1005 Mount
*m
= MOUNT(u
);
1009 /* We cannot fulfill this request right now, try again later
1011 if (m
->state
== MOUNT_UNMOUNTING
||
1012 m
->state
== MOUNT_UNMOUNTING_SIGTERM
||
1013 m
->state
== MOUNT_UNMOUNTING_SIGKILL
||
1014 m
->state
== MOUNT_MOUNTING_SIGTERM
||
1015 m
->state
== MOUNT_MOUNTING_SIGKILL
)
1018 /* Already on it! */
1019 if (m
->state
== MOUNT_MOUNTING
)
1022 assert(m
->state
== MOUNT_DEAD
|| m
->state
== MOUNT_FAILED
);
1024 m
->result
= MOUNT_SUCCESS
;
1025 m
->reload_result
= MOUNT_SUCCESS
;
1026 m
->reset_cpu_usage
= true;
1028 mount_enter_mounting(m
);
1032 static int mount_stop(Unit
*u
) {
1033 Mount
*m
= MOUNT(u
);
1038 if (m
->state
== MOUNT_UNMOUNTING
||
1039 m
->state
== MOUNT_UNMOUNTING_SIGKILL
||
1040 m
->state
== MOUNT_UNMOUNTING_SIGTERM
||
1041 m
->state
== MOUNT_MOUNTING_SIGTERM
||
1042 m
->state
== MOUNT_MOUNTING_SIGKILL
)
1045 assert(m
->state
== MOUNT_MOUNTING
||
1046 m
->state
== MOUNT_MOUNTING_DONE
||
1047 m
->state
== MOUNT_MOUNTED
||
1048 m
->state
== MOUNT_REMOUNTING
||
1049 m
->state
== MOUNT_REMOUNTING_SIGTERM
||
1050 m
->state
== MOUNT_REMOUNTING_SIGKILL
);
1052 mount_enter_unmounting(m
);
1056 static int mount_reload(Unit
*u
) {
1057 Mount
*m
= MOUNT(u
);
1061 if (m
->state
== MOUNT_MOUNTING_DONE
)
1064 assert(m
->state
== MOUNT_MOUNTED
);
1066 mount_enter_remounting(m
);
1070 static int mount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1071 Mount
*m
= MOUNT(u
);
1077 unit_serialize_item(u
, f
, "state", mount_state_to_string(m
->state
));
1078 unit_serialize_item(u
, f
, "result", mount_result_to_string(m
->result
));
1079 unit_serialize_item(u
, f
, "reload-result", mount_result_to_string(m
->reload_result
));
1081 if (m
->control_pid
> 0)
1082 unit_serialize_item_format(u
, f
, "control-pid", PID_FMT
, m
->control_pid
);
1084 if (m
->control_command_id
>= 0)
1085 unit_serialize_item(u
, f
, "control-command", mount_exec_command_to_string(m
->control_command_id
));
1090 static int mount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1091 Mount
*m
= MOUNT(u
);
1098 if (streq(key
, "state")) {
1101 if ((state
= mount_state_from_string(value
)) < 0)
1102 log_unit_debug(u
->id
, "Failed to parse state value %s", value
);
1104 m
->deserialized_state
= state
;
1105 } else if (streq(key
, "result")) {
1108 f
= mount_result_from_string(value
);
1110 log_unit_debug(UNIT(m
)->id
,
1111 "Failed to parse result value %s", value
);
1112 else if (f
!= MOUNT_SUCCESS
)
1115 } else if (streq(key
, "reload-result")) {
1118 f
= mount_result_from_string(value
);
1120 log_unit_debug(UNIT(m
)->id
,
1121 "Failed to parse reload result value %s", value
);
1122 else if (f
!= MOUNT_SUCCESS
)
1123 m
->reload_result
= f
;
1125 } else if (streq(key
, "control-pid")) {
1128 if (parse_pid(value
, &pid
) < 0)
1129 log_unit_debug(UNIT(m
)->id
,
1130 "Failed to parse control-pid value %s", value
);
1132 m
->control_pid
= pid
;
1133 } else if (streq(key
, "control-command")) {
1134 MountExecCommand id
;
1136 if ((id
= mount_exec_command_from_string(value
)) < 0)
1137 log_unit_debug(UNIT(m
)->id
,
1138 "Failed to parse exec-command value %s", value
);
1140 m
->control_command_id
= id
;
1141 m
->control_command
= m
->exec_command
+ id
;
1144 log_unit_debug(UNIT(m
)->id
,
1145 "Unknown serialization key '%s'", key
);
1150 _pure_
static UnitActiveState
mount_active_state(Unit
*u
) {
1153 return state_translation_table
[MOUNT(u
)->state
];
1156 _pure_
static const char *mount_sub_state_to_string(Unit
*u
) {
1159 return mount_state_to_string(MOUNT(u
)->state
);
1162 _pure_
static bool mount_check_gc(Unit
*u
) {
1163 Mount
*m
= MOUNT(u
);
1167 return m
->from_proc_self_mountinfo
;
1170 static void mount_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1171 Mount
*m
= MOUNT(u
);
1177 if (pid
!= m
->control_pid
)
1182 if (is_clean_exit(code
, status
, NULL
))
1184 else if (code
== CLD_EXITED
)
1185 f
= MOUNT_FAILURE_EXIT_CODE
;
1186 else if (code
== CLD_KILLED
)
1187 f
= MOUNT_FAILURE_SIGNAL
;
1188 else if (code
== CLD_DUMPED
)
1189 f
= MOUNT_FAILURE_CORE_DUMP
;
1191 assert_not_reached("Unknown code");
1193 if (f
!= MOUNT_SUCCESS
)
1196 if (m
->control_command
) {
1197 exec_status_exit(&m
->control_command
->exec_status
, &m
->exec_context
, pid
, code
, status
);
1199 m
->control_command
= NULL
;
1200 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
1203 log_unit_full(u
->id
,
1204 f
== MOUNT_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
1205 "%s mount process exited, code=%s status=%i",
1206 u
->id
, sigchld_code_to_string(code
), status
);
1208 /* Note that mount(8) returning and the kernel sending us a
1209 * mount table change event might happen out-of-order. If an
1210 * operation succeed we assume the kernel will follow soon too
1211 * and already change into the resulting state. If it fails
1212 * we check if the kernel still knows about the mount. and
1213 * change state accordingly. */
1217 case MOUNT_MOUNTING
:
1218 case MOUNT_MOUNTING_DONE
:
1219 case MOUNT_MOUNTING_SIGKILL
:
1220 case MOUNT_MOUNTING_SIGTERM
:
1222 if (f
== MOUNT_SUCCESS
)
1223 mount_enter_mounted(m
, f
);
1224 else if (m
->from_proc_self_mountinfo
)
1225 mount_enter_mounted(m
, f
);
1227 mount_enter_dead(m
, f
);
1230 case MOUNT_REMOUNTING
:
1231 case MOUNT_REMOUNTING_SIGKILL
:
1232 case MOUNT_REMOUNTING_SIGTERM
:
1234 m
->reload_result
= f
;
1235 if (m
->from_proc_self_mountinfo
)
1236 mount_enter_mounted(m
, MOUNT_SUCCESS
);
1238 mount_enter_dead(m
, MOUNT_SUCCESS
);
1242 case MOUNT_UNMOUNTING
:
1243 case MOUNT_UNMOUNTING_SIGKILL
:
1244 case MOUNT_UNMOUNTING_SIGTERM
:
1246 if (f
== MOUNT_SUCCESS
) {
1248 if (m
->from_proc_self_mountinfo
) {
1250 /* Still a mount point? If so, let's
1251 * try again. Most likely there were
1252 * multiple mount points stacked on
1253 * top of each other. Note that due to
1254 * the io event priority logic we can
1255 * be sure the new mountinfo is loaded
1256 * before we process the SIGCHLD for
1257 * the mount command. */
1259 if (m
->n_retry_umount
< RETRY_UMOUNT_MAX
) {
1260 log_unit_debug(u
->id
, "%s: mount still present, trying again.", u
->id
);
1261 m
->n_retry_umount
++;
1262 mount_enter_unmounting(m
);
1264 log_unit_debug(u
->id
, "%s: mount still present after %u attempts to unmount, giving up.", u
->id
, m
->n_retry_umount
);
1265 mount_enter_mounted(m
, f
);
1268 mount_enter_dead(m
, f
);
1270 } else if (m
->from_proc_self_mountinfo
)
1271 mount_enter_mounted(m
, f
);
1273 mount_enter_dead(m
, f
);
1277 assert_not_reached("Uh, control process died at wrong time.");
1280 /* Notify clients about changed exit status */
1281 unit_add_to_dbus_queue(u
);
1284 static int mount_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
1285 Mount
*m
= MOUNT(userdata
);
1288 assert(m
->timer_event_source
== source
);
1292 case MOUNT_MOUNTING
:
1293 case MOUNT_MOUNTING_DONE
:
1294 log_unit_warning(UNIT(m
)->id
,
1295 "%s mounting timed out. Stopping.", UNIT(m
)->id
);
1296 mount_enter_signal(m
, MOUNT_MOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1299 case MOUNT_REMOUNTING
:
1300 log_unit_warning(UNIT(m
)->id
,
1301 "%s remounting timed out. Stopping.", UNIT(m
)->id
);
1302 m
->reload_result
= MOUNT_FAILURE_TIMEOUT
;
1303 mount_enter_mounted(m
, MOUNT_SUCCESS
);
1306 case MOUNT_UNMOUNTING
:
1307 log_unit_warning(UNIT(m
)->id
,
1308 "%s unmounting timed out. Stopping.", UNIT(m
)->id
);
1309 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1312 case MOUNT_MOUNTING_SIGTERM
:
1313 if (m
->kill_context
.send_sigkill
) {
1314 log_unit_warning(UNIT(m
)->id
,
1315 "%s mounting timed out. Killing.", UNIT(m
)->id
);
1316 mount_enter_signal(m
, MOUNT_MOUNTING_SIGKILL
, MOUNT_FAILURE_TIMEOUT
);
1318 log_unit_warning(UNIT(m
)->id
,
1319 "%s mounting timed out. Skipping SIGKILL. Ignoring.",
1322 if (m
->from_proc_self_mountinfo
)
1323 mount_enter_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1325 mount_enter_dead(m
, MOUNT_FAILURE_TIMEOUT
);
1329 case MOUNT_REMOUNTING_SIGTERM
:
1330 if (m
->kill_context
.send_sigkill
) {
1331 log_unit_warning(UNIT(m
)->id
,
1332 "%s remounting timed out. Killing.", UNIT(m
)->id
);
1333 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_FAILURE_TIMEOUT
);
1335 log_unit_warning(UNIT(m
)->id
,
1336 "%s remounting timed out. Skipping SIGKILL. Ignoring.",
1339 if (m
->from_proc_self_mountinfo
)
1340 mount_enter_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1342 mount_enter_dead(m
, MOUNT_FAILURE_TIMEOUT
);
1346 case MOUNT_UNMOUNTING_SIGTERM
:
1347 if (m
->kill_context
.send_sigkill
) {
1348 log_unit_warning(UNIT(m
)->id
,
1349 "%s unmounting timed out. Killing.", UNIT(m
)->id
);
1350 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_FAILURE_TIMEOUT
);
1352 log_unit_warning(UNIT(m
)->id
,
1353 "%s unmounting timed out. Skipping SIGKILL. Ignoring.",
1356 if (m
->from_proc_self_mountinfo
)
1357 mount_enter_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1359 mount_enter_dead(m
, MOUNT_FAILURE_TIMEOUT
);
1363 case MOUNT_MOUNTING_SIGKILL
:
1364 case MOUNT_REMOUNTING_SIGKILL
:
1365 case MOUNT_UNMOUNTING_SIGKILL
:
1366 log_unit_warning(UNIT(m
)->id
,
1367 "%s mount process still around after SIGKILL. Ignoring.",
1370 if (m
->from_proc_self_mountinfo
)
1371 mount_enter_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1373 mount_enter_dead(m
, MOUNT_FAILURE_TIMEOUT
);
1377 assert_not_reached("Timeout at wrong time.");
1383 static int mount_setup_unit(
1387 const char *options
,
1391 _cleanup_free_
char *e
= NULL
, *w
= NULL
, *o
= NULL
, *f
= NULL
;
1392 bool load_extras
= false;
1394 bool delete, changed
= false;
1404 /* Ignore API mount points. They should never be referenced in
1405 * dependencies ever. */
1406 if (mount_point_is_api(where
) || mount_point_ignore(where
))
1409 if (streq(fstype
, "autofs"))
1412 /* probably some kind of swap, ignore */
1413 if (!is_path(where
))
1416 e
= unit_name_from_path(where
, ".mount");
1420 u
= manager_get_unit(m
, e
);
1424 u
= unit_new(m
, sizeof(Mount
));
1428 r
= unit_add_name(u
, e
);
1432 MOUNT(u
)->where
= strdup(where
);
1433 if (!MOUNT(u
)->where
) {
1438 u
->source_path
= strdup("/proc/self/mountinfo");
1439 if (!u
->source_path
) {
1444 if (m
->running_as
== SYSTEMD_SYSTEM
) {
1447 target
= mount_needs_network(options
, fstype
) ? SPECIAL_REMOTE_FS_TARGET
: SPECIAL_LOCAL_FS_TARGET
;
1448 r
= unit_add_dependency_by_name(u
, UNIT_BEFORE
, target
, NULL
, true);
1452 if (should_umount(MOUNT(u
))) {
1453 r
= unit_add_dependency_by_name(u
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, NULL
, true);
1459 unit_add_to_load_queue(u
);
1464 if (!MOUNT(u
)->where
) {
1465 MOUNT(u
)->where
= strdup(where
);
1466 if (!MOUNT(u
)->where
) {
1472 if (m
->running_as
== SYSTEMD_SYSTEM
&&
1473 mount_needs_network(options
, fstype
)) {
1474 /* _netdev option may have shown up late, or on a
1475 * remount. Add remote-fs dependencies, even though
1476 * local-fs ones may already be there. */
1477 unit_add_dependency_by_name(u
, UNIT_BEFORE
, SPECIAL_REMOTE_FS_TARGET
, NULL
, true);
1481 if (u
->load_state
== UNIT_NOT_FOUND
) {
1482 u
->load_state
= UNIT_LOADED
;
1485 /* Load in the extras later on, after we
1486 * finished initialization of the unit */
1493 o
= strdup(options
);
1495 if (!w
|| !o
|| !f
) {
1500 p
= &MOUNT(u
)->parameters_proc_self_mountinfo
;
1502 changed
= changed
||
1503 !streq_ptr(p
->options
, options
) ||
1504 !streq_ptr(p
->what
, what
) ||
1505 !streq_ptr(p
->fstype
, fstype
);
1508 MOUNT(u
)->is_mounted
= true;
1509 MOUNT(u
)->just_mounted
= !MOUNT(u
)->from_proc_self_mountinfo
;
1510 MOUNT(u
)->just_changed
= changed
;
1513 MOUNT(u
)->from_proc_self_mountinfo
= true;
1528 r
= mount_add_extras(MOUNT(u
));
1534 unit_add_to_dbus_queue(u
);
1539 log_warning_errno(r
, "Failed to set up mount unit: %m");
1547 static int mount_load_proc_self_mountinfo(Manager
*m
, bool set_flags
) {
1548 _cleanup_(mnt_free_tablep
) struct libmnt_table
*t
= NULL
;
1549 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*i
= NULL
;
1554 t
= mnt_new_table();
1558 i
= mnt_new_iter(MNT_ITER_FORWARD
);
1562 r
= mnt_table_parse_mtab(t
, NULL
);
1564 return log_error_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
1568 const char *device
, *path
, *options
, *fstype
;
1569 _cleanup_free_
char *d
= NULL
, *p
= NULL
;
1570 struct libmnt_fs
*fs
;
1573 k
= mnt_table_next_fs(t
, i
, &fs
);
1577 return log_error_errno(k
, "Failed to get next entry from /proc/self/mountinfo: %m");
1579 device
= mnt_fs_get_source(fs
);
1580 path
= mnt_fs_get_target(fs
);
1581 options
= mnt_fs_get_options(fs
);
1582 fstype
= mnt_fs_get_fstype(fs
);
1584 if (cunescape(device
, UNESCAPE_RELAX
, &d
) < 0)
1587 if (cunescape(path
, UNESCAPE_RELAX
, &p
) < 0)
1590 (void) device_found_node(m
, d
, true, DEVICE_FOUND_MOUNT
, set_flags
);
1592 k
= mount_setup_unit(m
, d
, p
, options
, fstype
, set_flags
);
1593 if (r
== 0 && k
< 0)
1600 static void mount_shutdown(Manager
*m
) {
1603 m
->mount_event_source
= sd_event_source_unref(m
->mount_event_source
);
1604 m
->mount_utab_event_source
= sd_event_source_unref(m
->mount_utab_event_source
);
1606 if (m
->proc_self_mountinfo
) {
1607 fclose(m
->proc_self_mountinfo
);
1608 m
->proc_self_mountinfo
= NULL
;
1610 m
->utab_inotify_fd
= safe_close(m
->utab_inotify_fd
);
1613 static int mount_get_timeout(Unit
*u
, uint64_t *timeout
) {
1614 Mount
*m
= MOUNT(u
);
1617 if (!m
->timer_event_source
)
1620 r
= sd_event_source_get_time(m
->timer_event_source
, timeout
);
1627 static int mount_enumerate(Manager
*m
) {
1633 if (!m
->proc_self_mountinfo
) {
1634 m
->proc_self_mountinfo
= fopen("/proc/self/mountinfo", "re");
1635 if (!m
->proc_self_mountinfo
)
1638 r
= sd_event_add_io(m
->event
, &m
->mount_event_source
, fileno(m
->proc_self_mountinfo
), EPOLLPRI
, mount_dispatch_io
, m
);
1642 /* Dispatch this before we dispatch SIGCHLD, so that
1643 * we always get the events from /proc/self/mountinfo
1644 * before the SIGCHLD of /bin/mount. */
1645 r
= sd_event_source_set_priority(m
->mount_event_source
, -10);
1650 if (m
->utab_inotify_fd
< 0) {
1651 m
->utab_inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
1652 if (m
->utab_inotify_fd
< 0) {
1657 (void) mkdir_p_label("/run/mount", 0755);
1659 r
= inotify_add_watch(m
->utab_inotify_fd
, "/run/mount", IN_MOVED_TO
);
1665 r
= sd_event_add_io(m
->event
, &m
->mount_utab_event_source
, m
->utab_inotify_fd
, EPOLLIN
, mount_dispatch_io
, m
);
1669 r
= sd_event_source_set_priority(m
->mount_utab_event_source
, -10);
1674 r
= mount_load_proc_self_mountinfo(m
, false);
1685 static int mount_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
1686 Manager
*m
= userdata
;
1691 assert(revents
& (EPOLLPRI
| EPOLLIN
));
1693 /* The manager calls this for every fd event happening on the
1694 * /proc/self/mountinfo file, which informs us about mounting
1695 * table changes, and for /run/mount events which we watch
1696 * for mount options. */
1698 if (fd
== m
->utab_inotify_fd
) {
1699 bool rescan
= false;
1701 /* FIXME: We *really* need to replace this with
1702 * libmount's own API for this, we should not hardcode
1703 * internal behaviour of libmount here. */
1706 union inotify_event_buffer buffer
;
1707 struct inotify_event
*e
;
1710 l
= read(fd
, &buffer
, sizeof(buffer
));
1712 if (errno
== EAGAIN
|| errno
== EINTR
)
1715 log_error_errno(errno
, "Failed to read utab inotify: %m");
1719 FOREACH_INOTIFY_EVENT(e
, buffer
, l
) {
1720 /* Only care about changes to utab,
1721 * but we have to monitor the
1722 * directory to reliably get
1723 * notifications about when utab is
1724 * replaced using rename(2) */
1725 if ((e
->mask
& IN_Q_OVERFLOW
) || streq(e
->name
, "utab"))
1734 r
= mount_load_proc_self_mountinfo(m
, true);
1736 /* Reset flags, just in case, for later calls */
1737 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
]) {
1738 Mount
*mount
= MOUNT(u
);
1740 mount
->is_mounted
= mount
->just_mounted
= mount
->just_changed
= false;
1746 manager_dispatch_load_queue(m
);
1748 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
]) {
1749 Mount
*mount
= MOUNT(u
);
1751 if (!mount
->is_mounted
) {
1753 mount
->from_proc_self_mountinfo
= false;
1755 switch (mount
->state
) {
1758 /* This has just been unmounted by
1759 * somebody else, follow the state
1761 mount_enter_dead(mount
, MOUNT_SUCCESS
);
1768 if (mount
->parameters_proc_self_mountinfo
.what
)
1769 (void) device_found_node(m
, mount
->parameters_proc_self_mountinfo
.what
, false, DEVICE_FOUND_MOUNT
, true);
1772 } else if (mount
->just_mounted
|| mount
->just_changed
) {
1774 /* New or changed mount entry */
1776 switch (mount
->state
) {
1780 /* This has just been mounted by
1781 * somebody else, follow the state
1783 mount_enter_mounted(mount
, MOUNT_SUCCESS
);
1786 case MOUNT_MOUNTING
:
1787 mount_set_state(mount
, MOUNT_MOUNTING_DONE
);
1791 /* Nothing really changed, but let's
1792 * issue an notification call
1793 * nonetheless, in case somebody is
1794 * waiting for this. (e.g. file system
1795 * ro/rw remounts.) */
1796 mount_set_state(mount
, mount
->state
);
1801 /* Reset the flags for later calls */
1802 mount
->is_mounted
= mount
->just_mounted
= mount
->just_changed
= false;
1808 static void mount_reset_failed(Unit
*u
) {
1809 Mount
*m
= MOUNT(u
);
1813 if (m
->state
== MOUNT_FAILED
)
1814 mount_set_state(m
, MOUNT_DEAD
);
1816 m
->result
= MOUNT_SUCCESS
;
1817 m
->reload_result
= MOUNT_SUCCESS
;
1820 static int mount_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
1821 return unit_kill_common(u
, who
, signo
, -1, MOUNT(u
)->control_pid
, error
);
1824 static const char* const mount_state_table
[_MOUNT_STATE_MAX
] = {
1825 [MOUNT_DEAD
] = "dead",
1826 [MOUNT_MOUNTING
] = "mounting",
1827 [MOUNT_MOUNTING_DONE
] = "mounting-done",
1828 [MOUNT_MOUNTED
] = "mounted",
1829 [MOUNT_REMOUNTING
] = "remounting",
1830 [MOUNT_UNMOUNTING
] = "unmounting",
1831 [MOUNT_MOUNTING_SIGTERM
] = "mounting-sigterm",
1832 [MOUNT_MOUNTING_SIGKILL
] = "mounting-sigkill",
1833 [MOUNT_REMOUNTING_SIGTERM
] = "remounting-sigterm",
1834 [MOUNT_REMOUNTING_SIGKILL
] = "remounting-sigkill",
1835 [MOUNT_UNMOUNTING_SIGTERM
] = "unmounting-sigterm",
1836 [MOUNT_UNMOUNTING_SIGKILL
] = "unmounting-sigkill",
1837 [MOUNT_FAILED
] = "failed"
1840 DEFINE_STRING_TABLE_LOOKUP(mount_state
, MountState
);
1842 static const char* const mount_exec_command_table
[_MOUNT_EXEC_COMMAND_MAX
] = {
1843 [MOUNT_EXEC_MOUNT
] = "ExecMount",
1844 [MOUNT_EXEC_UNMOUNT
] = "ExecUnmount",
1845 [MOUNT_EXEC_REMOUNT
] = "ExecRemount",
1848 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command
, MountExecCommand
);
1850 static const char* const mount_result_table
[_MOUNT_RESULT_MAX
] = {
1851 [MOUNT_SUCCESS
] = "success",
1852 [MOUNT_FAILURE_RESOURCES
] = "resources",
1853 [MOUNT_FAILURE_TIMEOUT
] = "timeout",
1854 [MOUNT_FAILURE_EXIT_CODE
] = "exit-code",
1855 [MOUNT_FAILURE_SIGNAL
] = "signal",
1856 [MOUNT_FAILURE_CORE_DUMP
] = "core-dump"
1859 DEFINE_STRING_TABLE_LOOKUP(mount_result
, MountResult
);
1861 const UnitVTable mount_vtable
= {
1862 .object_size
= sizeof(Mount
),
1863 .exec_context_offset
= offsetof(Mount
, exec_context
),
1864 .cgroup_context_offset
= offsetof(Mount
, cgroup_context
),
1865 .kill_context_offset
= offsetof(Mount
, kill_context
),
1866 .exec_runtime_offset
= offsetof(Mount
, exec_runtime
),
1872 .private_section
= "Mount",
1875 .no_instances
= true,
1881 .coldplug
= mount_coldplug
,
1885 .start
= mount_start
,
1887 .reload
= mount_reload
,
1891 .serialize
= mount_serialize
,
1892 .deserialize_item
= mount_deserialize_item
,
1894 .active_state
= mount_active_state
,
1895 .sub_state_to_string
= mount_sub_state_to_string
,
1897 .check_gc
= mount_check_gc
,
1899 .sigchld_event
= mount_sigchld_event
,
1901 .reset_failed
= mount_reset_failed
,
1903 .bus_interface
= "org.freedesktop.systemd1.Mount",
1904 .bus_vtable
= bus_mount_vtable
,
1905 .bus_set_property
= bus_mount_set_property
,
1906 .bus_commit_properties
= bus_mount_commit_properties
,
1908 .get_timeout
= mount_get_timeout
,
1910 .can_transient
= true,
1912 .enumerate
= mount_enumerate
,
1913 .shutdown
= mount_shutdown
,
1915 .status_message_formats
= {
1916 .starting_stopping
= {
1917 [0] = "Mounting %s...",
1918 [1] = "Unmounting %s...",
1920 .finished_start_job
= {
1921 [JOB_DONE
] = "Mounted %s.",
1922 [JOB_FAILED
] = "Failed to mount %s.",
1923 [JOB_DEPENDENCY
] = "Dependency failed for %s.",
1924 [JOB_TIMEOUT
] = "Timed out mounting %s.",
1926 .finished_stop_job
= {
1927 [JOB_DONE
] = "Unmounted %s.",
1928 [JOB_FAILED
] = "Failed unmounting %s.",
1929 [JOB_TIMEOUT
] = "Timed out unmounting %s.",