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"
44 #define RETRY_UMOUNT_MAX 32
46 DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_table
*, mnt_free_table
);
47 DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_iter
*, mnt_free_iter
);
49 static const UnitActiveState state_translation_table
[_MOUNT_STATE_MAX
] = {
50 [MOUNT_DEAD
] = UNIT_INACTIVE
,
51 [MOUNT_MOUNTING
] = UNIT_ACTIVATING
,
52 [MOUNT_MOUNTING_DONE
] = UNIT_ACTIVE
,
53 [MOUNT_MOUNTED
] = UNIT_ACTIVE
,
54 [MOUNT_REMOUNTING
] = UNIT_RELOADING
,
55 [MOUNT_UNMOUNTING
] = UNIT_DEACTIVATING
,
56 [MOUNT_MOUNTING_SIGTERM
] = UNIT_DEACTIVATING
,
57 [MOUNT_MOUNTING_SIGKILL
] = UNIT_DEACTIVATING
,
58 [MOUNT_REMOUNTING_SIGTERM
] = UNIT_RELOADING
,
59 [MOUNT_REMOUNTING_SIGKILL
] = UNIT_RELOADING
,
60 [MOUNT_UNMOUNTING_SIGTERM
] = UNIT_DEACTIVATING
,
61 [MOUNT_UNMOUNTING_SIGKILL
] = UNIT_DEACTIVATING
,
62 [MOUNT_FAILED
] = UNIT_FAILED
65 static int mount_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
66 static int mount_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
);
68 static bool mount_needs_network(const char *options
, const char *fstype
) {
69 if (fstab_test_option(options
, "_netdev\0"))
72 if (fstype
&& fstype_is_network(fstype
))
78 static bool mount_is_network(const MountParameters
*p
) {
81 return mount_needs_network(p
->options
, p
->fstype
);
84 static bool mount_is_bind(const MountParameters
*p
) {
87 if (fstab_test_option(p
->options
, "bind\0" "rbind\0"))
90 if (p
->fstype
&& STR_IN_SET(p
->fstype
, "bind", "rbind"))
96 static bool mount_is_auto(const MountParameters
*p
) {
99 return !fstab_test_option(p
->options
, "noauto\0");
102 static bool needs_quota(const MountParameters
*p
) {
105 if (mount_is_network(p
))
108 if (mount_is_bind(p
))
111 return fstab_test_option(p
->options
,
112 "usrquota\0" "grpquota\0" "quota\0" "usrjquota\0" "grpjquota\0");
115 static void mount_init(Unit
*u
) {
119 assert(u
->load_state
== UNIT_STUB
);
121 m
->timeout_usec
= u
->manager
->default_timeout_start_usec
;
122 m
->directory_mode
= 0755;
124 if (unit_has_name(u
, "-.mount")) {
125 /* Don't allow start/stop for root directory */
126 u
->refuse_manual_start
= true;
127 u
->refuse_manual_stop
= true;
129 /* The stdio/kmsg bridge socket is on /, in order to avoid a
130 * dep loop, don't use kmsg logging for -.mount */
131 m
->exec_context
.std_output
= u
->manager
->default_std_output
;
132 m
->exec_context
.std_error
= u
->manager
->default_std_error
;
135 /* We need to make sure that /bin/mount is always called in
136 * the same process group as us, so that the autofs kernel
137 * side doesn't send us another mount request while we are
138 * already trying to comply its last one. */
139 m
->exec_context
.same_pgrp
= true;
141 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
143 u
->ignore_on_isolate
= true;
146 static int mount_arm_timer(Mount
*m
) {
151 if (m
->timeout_usec
<= 0) {
152 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
156 if (m
->timer_event_source
) {
157 r
= sd_event_source_set_time(m
->timer_event_source
, now(CLOCK_MONOTONIC
) + m
->timeout_usec
);
161 return sd_event_source_set_enabled(m
->timer_event_source
, SD_EVENT_ONESHOT
);
164 return sd_event_add_time(
165 UNIT(m
)->manager
->event
,
166 &m
->timer_event_source
,
168 now(CLOCK_MONOTONIC
) + m
->timeout_usec
, 0,
169 mount_dispatch_timer
, m
);
172 static void mount_unwatch_control_pid(Mount
*m
) {
175 if (m
->control_pid
<= 0)
178 unit_unwatch_pid(UNIT(m
), m
->control_pid
);
182 static void mount_parameters_done(MountParameters
*p
) {
189 p
->what
= p
->options
= p
->fstype
= NULL
;
192 static void mount_done(Unit
*u
) {
200 mount_parameters_done(&m
->parameters_proc_self_mountinfo
);
201 mount_parameters_done(&m
->parameters_fragment
);
203 m
->exec_runtime
= exec_runtime_unref(m
->exec_runtime
);
204 exec_command_done_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
205 m
->control_command
= NULL
;
207 mount_unwatch_control_pid(m
);
209 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
212 _pure_
static MountParameters
* get_mount_parameters_fragment(Mount
*m
) {
215 if (m
->from_fragment
)
216 return &m
->parameters_fragment
;
221 _pure_
static MountParameters
* get_mount_parameters(Mount
*m
) {
224 if (m
->from_proc_self_mountinfo
)
225 return &m
->parameters_proc_self_mountinfo
;
227 return get_mount_parameters_fragment(m
);
230 static int mount_add_mount_links(Mount
*m
) {
231 _cleanup_free_
char *parent
= NULL
;
240 if (!path_equal(m
->where
, "/")) {
241 /* Adds in links to other mount points that might lie further
242 * up in the hierarchy */
243 r
= path_get_parent(m
->where
, &parent
);
247 r
= unit_require_mounts_for(UNIT(m
), parent
);
252 /* Adds in links to other mount points that might be needed
253 * for the source path (if this is a bind mount) to be
255 pm
= get_mount_parameters_fragment(m
);
256 if (pm
&& pm
->what
&&
257 path_is_absolute(pm
->what
) &&
258 !mount_is_network(pm
)) {
260 r
= unit_require_mounts_for(UNIT(m
), pm
->what
);
265 /* Adds in links to other units that use this path or paths
266 * further down in the hierarchy */
267 s
= manager_get_units_requiring_mounts_for(UNIT(m
)->manager
, m
->where
);
268 SET_FOREACH(other
, s
, i
) {
270 if (other
->load_state
!= UNIT_LOADED
)
273 if (other
== UNIT(m
))
276 r
= unit_add_dependency(other
, UNIT_AFTER
, UNIT(m
), true);
280 if (UNIT(m
)->fragment_path
) {
281 /* If we have fragment configuration, then make this dependency required */
282 r
= unit_add_dependency(other
, UNIT_REQUIRES
, UNIT(m
), true);
291 static int mount_add_device_links(Mount
*m
) {
293 bool device_wants_mount
= false;
298 p
= get_mount_parameters(m
);
305 if (mount_is_bind(p
))
308 if (!is_device_path(p
->what
))
311 if (path_equal(m
->where
, "/"))
314 if (mount_is_auto(p
) && UNIT(m
)->manager
->running_as
== SYSTEMD_SYSTEM
)
315 device_wants_mount
= true;
317 r
= unit_add_node_link(UNIT(m
), p
->what
, device_wants_mount
);
324 static int mount_add_quota_links(Mount
*m
) {
330 if (UNIT(m
)->manager
->running_as
!= SYSTEMD_SYSTEM
)
333 p
= get_mount_parameters_fragment(m
);
340 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTACHECK_SERVICE
, NULL
, true);
344 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_WANTS
, SPECIAL_QUOTAON_SERVICE
, NULL
, true);
351 static bool should_umount(Mount
*m
) {
354 if (path_equal(m
->where
, "/") ||
355 path_equal(m
->where
, "/usr"))
358 p
= get_mount_parameters(m
);
359 if (p
&& fstab_test_option(p
->options
, "x-initrd.mount\0") &&
366 static int mount_add_default_dependencies(Mount
*m
) {
367 const char *after
, *after2
, *online
;
373 if (UNIT(m
)->manager
->running_as
!= SYSTEMD_SYSTEM
)
376 /* We do not add any default dependencies to / and /usr, since
377 * they are guaranteed to stay mounted the whole time, since
378 * our system is on it. Also, don't bother with anything
379 * mounted below virtual file systems, it's also going to be
380 * virtual, and hence not worth the effort. */
381 if (path_equal(m
->where
, "/") ||
382 path_equal(m
->where
, "/usr") ||
383 path_startswith(m
->where
, "/proc") ||
384 path_startswith(m
->where
, "/sys") ||
385 path_startswith(m
->where
, "/dev"))
388 p
= get_mount_parameters(m
);
392 if (mount_is_network(p
)) {
393 after
= SPECIAL_REMOTE_FS_PRE_TARGET
;
394 after2
= SPECIAL_NETWORK_TARGET
;
395 online
= SPECIAL_NETWORK_ONLINE_TARGET
;
397 after
= SPECIAL_LOCAL_FS_PRE_TARGET
;
402 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, after
, NULL
, true);
407 r
= unit_add_dependency_by_name(UNIT(m
), UNIT_AFTER
, after2
, NULL
, true);
413 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_WANTS
, UNIT_AFTER
, online
, NULL
, true);
418 if (should_umount(m
)) {
419 r
= unit_add_two_dependencies_by_name(UNIT(m
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, NULL
, true);
427 static int mount_verify(Mount
*m
) {
428 _cleanup_free_
char *e
= NULL
;
433 if (UNIT(m
)->load_state
!= UNIT_LOADED
)
436 if (!m
->from_fragment
&& !m
->from_proc_self_mountinfo
)
439 e
= unit_name_from_path(m
->where
, ".mount");
443 b
= unit_has_name(UNIT(m
), e
);
445 log_unit_error(UNIT(m
)->id
, "%s's Where= setting doesn't match unit name. Refusing.", UNIT(m
)->id
);
449 if (mount_point_is_api(m
->where
) || mount_point_ignore(m
->where
)) {
450 log_unit_error(UNIT(m
)->id
, "Cannot create mount unit for API file system %s. Refusing.", m
->where
);
454 if (UNIT(m
)->fragment_path
&& !m
->parameters_fragment
.what
) {
455 log_unit_error(UNIT(m
)->id
, "%s's What setting is missing. Refusing.", UNIT(m
)->id
);
459 if (m
->exec_context
.pam_name
&& m
->kill_context
.kill_mode
!= KILL_CONTROL_GROUP
) {
460 log_unit_error(UNIT(m
)->id
, "%s has PAM enabled. Kill mode must be set to control-group'. Refusing.",UNIT(m
)->id
);
467 static int mount_add_extras(Mount
*m
) {
473 if (u
->fragment_path
)
474 m
->from_fragment
= true;
477 m
->where
= unit_name_to_path(u
->id
);
482 path_kill_slashes(m
->where
);
484 if (!u
->description
) {
485 r
= unit_set_description(u
, m
->where
);
490 r
= mount_add_device_links(m
);
494 r
= mount_add_mount_links(m
);
498 r
= mount_add_quota_links(m
);
502 r
= unit_patch_contexts(u
);
506 r
= unit_add_exec_dependencies(u
, &m
->exec_context
);
510 r
= unit_add_default_slice(u
, &m
->cgroup_context
);
514 if (u
->default_dependencies
) {
515 r
= mount_add_default_dependencies(m
);
523 static int mount_load(Unit
*u
) {
528 assert(u
->load_state
== UNIT_STUB
);
530 if (m
->from_proc_self_mountinfo
)
531 r
= unit_load_fragment_and_dropin_optional(u
);
533 r
= unit_load_fragment_and_dropin(u
);
538 /* This is a new unit? Then let's add in some extras */
539 if (u
->load_state
== UNIT_LOADED
) {
540 r
= mount_add_extras(m
);
545 return mount_verify(m
);
548 static int mount_notify_automount(Mount
*m
, int status
) {
555 SET_FOREACH(p
, UNIT(m
)->dependencies
[UNIT_TRIGGERED_BY
], i
)
556 if (p
->type
== UNIT_AUTOMOUNT
) {
557 r
= automount_send_ready(AUTOMOUNT(p
), status
);
565 static void mount_set_state(Mount
*m
, MountState state
) {
566 MountState old_state
;
569 old_state
= m
->state
;
572 if (state
!= MOUNT_MOUNTING
&&
573 state
!= MOUNT_MOUNTING_DONE
&&
574 state
!= MOUNT_REMOUNTING
&&
575 state
!= MOUNT_UNMOUNTING
&&
576 state
!= MOUNT_MOUNTING_SIGTERM
&&
577 state
!= MOUNT_MOUNTING_SIGKILL
&&
578 state
!= MOUNT_UNMOUNTING_SIGTERM
&&
579 state
!= MOUNT_UNMOUNTING_SIGKILL
&&
580 state
!= MOUNT_REMOUNTING_SIGTERM
&&
581 state
!= MOUNT_REMOUNTING_SIGKILL
) {
582 m
->timer_event_source
= sd_event_source_unref(m
->timer_event_source
);
583 mount_unwatch_control_pid(m
);
584 m
->control_command
= NULL
;
585 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
588 if (state
== MOUNT_MOUNTED
||
589 state
== MOUNT_REMOUNTING
)
590 mount_notify_automount(m
, 0);
591 else if (state
== MOUNT_DEAD
||
592 state
== MOUNT_UNMOUNTING
||
593 state
== MOUNT_MOUNTING_SIGTERM
||
594 state
== MOUNT_MOUNTING_SIGKILL
||
595 state
== MOUNT_REMOUNTING_SIGTERM
||
596 state
== MOUNT_REMOUNTING_SIGKILL
||
597 state
== MOUNT_UNMOUNTING_SIGTERM
||
598 state
== MOUNT_UNMOUNTING_SIGKILL
||
599 state
== MOUNT_FAILED
) {
600 if (state
!= old_state
)
601 mount_notify_automount(m
, -ENODEV
);
604 if (state
!= old_state
)
605 log_unit_debug(UNIT(m
)->id
,
606 "%s changed %s -> %s",
608 mount_state_to_string(old_state
),
609 mount_state_to_string(state
));
611 unit_notify(UNIT(m
), state_translation_table
[old_state
], state_translation_table
[state
], m
->reload_result
== MOUNT_SUCCESS
);
612 m
->reload_result
= MOUNT_SUCCESS
;
615 static int mount_coldplug(Unit
*u
) {
617 MountState new_state
= MOUNT_DEAD
;
621 assert(m
->state
== MOUNT_DEAD
);
623 if (m
->deserialized_state
!= m
->state
)
624 new_state
= m
->deserialized_state
;
625 else if (m
->from_proc_self_mountinfo
)
626 new_state
= MOUNT_MOUNTED
;
628 if (new_state
== m
->state
)
631 if (new_state
== MOUNT_MOUNTING
||
632 new_state
== MOUNT_MOUNTING_DONE
||
633 new_state
== MOUNT_REMOUNTING
||
634 new_state
== MOUNT_UNMOUNTING
||
635 new_state
== MOUNT_MOUNTING_SIGTERM
||
636 new_state
== MOUNT_MOUNTING_SIGKILL
||
637 new_state
== MOUNT_UNMOUNTING_SIGTERM
||
638 new_state
== MOUNT_UNMOUNTING_SIGKILL
||
639 new_state
== MOUNT_REMOUNTING_SIGTERM
||
640 new_state
== MOUNT_REMOUNTING_SIGKILL
) {
642 if (m
->control_pid
<= 0)
645 r
= unit_watch_pid(UNIT(m
), m
->control_pid
);
649 r
= mount_arm_timer(m
);
654 mount_set_state(m
, new_state
);
658 static void mount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
665 p
= get_mount_parameters(m
);
668 "%sMount State: %s\n"
672 "%sFile System Type: %s\n"
674 "%sFrom /proc/self/mountinfo: %s\n"
675 "%sFrom fragment: %s\n"
676 "%sDirectoryMode: %04o\n",
677 prefix
, mount_state_to_string(m
->state
),
678 prefix
, mount_result_to_string(m
->result
),
680 prefix
, p
? strna(p
->what
) : "n/a",
681 prefix
, p
? strna(p
->fstype
) : "n/a",
682 prefix
, p
? strna(p
->options
) : "n/a",
683 prefix
, yes_no(m
->from_proc_self_mountinfo
),
684 prefix
, yes_no(m
->from_fragment
),
685 prefix
, m
->directory_mode
);
687 if (m
->control_pid
> 0)
689 "%sControl PID: "PID_FMT
"\n",
690 prefix
, m
->control_pid
);
692 exec_context_dump(&m
->exec_context
, f
, prefix
);
693 kill_context_dump(&m
->kill_context
, f
, prefix
);
696 static int mount_spawn(Mount
*m
, ExecCommand
*c
, pid_t
*_pid
) {
699 ExecParameters exec_params
= {
700 .apply_permissions
= true,
701 .apply_chroot
= true,
702 .apply_tty_stdin
= true,
709 unit_realize_cgroup(UNIT(m
));
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
);
725 exec_params
.unit_id
= UNIT(m
)->id
;
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(UNIT(m
)->id
,
813 "%s failed to kill processes: %s", UNIT(m
)->id
, strerror(-r
));
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 void warn_if_dir_nonempty(const char *unit
, const char* where
) {
827 r
= dir_is_empty(where
);
831 log_unit_struct(unit
,
833 LOG_MESSAGE_ID(SD_MESSAGE_OVERMOUNTING
),
834 LOG_MESSAGE("%s: Directory %s to mount over is not empty, mounting anyway.",
839 log_unit_warning(unit
,
840 "MESSAGE=Failed to check directory %s: %s",
841 where
, strerror(-r
));
844 static int fail_if_symlink(const char *unit
, const char* where
) {
847 if (is_symlink(where
) > 0) {
848 log_unit_struct(unit
,
850 LOG_MESSAGE_ID(SD_MESSAGE_OVERMOUNTING
),
851 LOG_MESSAGE("%s: Mount on symlink %s not allowed.",
861 static void mount_enter_unmounting(Mount
*m
) {
866 /* Start counting our attempts */
867 if (!IN_SET(m
->state
,
869 MOUNT_UNMOUNTING_SIGTERM
,
870 MOUNT_UNMOUNTING_SIGKILL
))
871 m
->n_retry_umount
= 0;
873 m
->control_command_id
= MOUNT_EXEC_UNMOUNT
;
874 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_UNMOUNT
;
876 r
= exec_command_set(m
->control_command
, "/bin/umount", m
->where
, NULL
);
877 if (r
>= 0 && UNIT(m
)->manager
->running_as
== SYSTEMD_SYSTEM
)
878 r
= exec_command_append(m
->control_command
, "-n", NULL
);
882 mount_unwatch_control_pid(m
);
884 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
888 mount_set_state(m
, MOUNT_UNMOUNTING
);
893 log_unit_warning(UNIT(m
)->id
,
894 "%s failed to run 'umount' task: %s",
895 UNIT(m
)->id
, strerror(-r
));
896 mount_enter_mounted(m
, MOUNT_FAILURE_RESOURCES
);
899 static void mount_enter_mounting(Mount
*m
) {
905 m
->control_command_id
= MOUNT_EXEC_MOUNT
;
906 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_MOUNT
;
908 mkdir_p_label(m
->where
, m
->directory_mode
);
910 warn_if_dir_nonempty(m
->meta
.id
, m
->where
);
912 /* Create the source directory for bind-mounts if needed */
913 p
= get_mount_parameters_fragment(m
);
914 if (p
&& mount_is_bind(p
))
915 mkdir_p_label(p
->what
, m
->directory_mode
);
917 r
= fail_if_symlink(m
->meta
.id
, m
->where
);
921 if (m
->from_fragment
) {
922 _cleanup_free_
char *opts
= NULL
;
924 r
= fstab_filter_options(m
->parameters_fragment
.options
,
925 "nofail\0" "noauto\0" "auto\0", NULL
, NULL
, &opts
);
929 r
= exec_command_set(m
->control_command
, "/bin/mount",
930 m
->parameters_fragment
.what
, m
->where
, NULL
);
931 if (r
>= 0 && UNIT(m
)->manager
->running_as
== SYSTEMD_SYSTEM
)
932 r
= exec_command_append(m
->control_command
, "-n", NULL
);
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
);
937 if (r
>= 0 && !isempty(opts
))
938 r
= exec_command_append(m
->control_command
, "-o", opts
, NULL
);
945 mount_unwatch_control_pid(m
);
947 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
951 mount_set_state(m
, MOUNT_MOUNTING
);
956 log_unit_warning(UNIT(m
)->id
,
957 "%s failed to run 'mount' task: %s",
958 UNIT(m
)->id
, strerror(-r
));
959 mount_enter_dead(m
, MOUNT_FAILURE_RESOURCES
);
962 static void mount_enter_remounting(Mount
*m
) {
967 m
->control_command_id
= MOUNT_EXEC_REMOUNT
;
968 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_REMOUNT
;
970 if (m
->from_fragment
) {
973 if (m
->parameters_fragment
.options
)
974 o
= strjoina("remount,", m
->parameters_fragment
.options
);
978 r
= exec_command_set(m
->control_command
, "/bin/mount",
979 m
->parameters_fragment
.what
, m
->where
,
981 if (r
>= 0 && UNIT(m
)->manager
->running_as
== SYSTEMD_SYSTEM
)
982 r
= exec_command_append(m
->control_command
, "-n", NULL
);
983 if (r
>= 0 && m
->sloppy_options
)
984 r
= exec_command_append(m
->control_command
, "-s", NULL
);
985 if (r
>= 0 && m
->parameters_fragment
.fstype
)
986 r
= exec_command_append(m
->control_command
, "-t", m
->parameters_fragment
.fstype
, NULL
);
993 mount_unwatch_control_pid(m
);
995 r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
);
999 mount_set_state(m
, MOUNT_REMOUNTING
);
1004 log_unit_warning(UNIT(m
)->id
,
1005 "%s failed to run 'remount' task: %s",
1006 UNIT(m
)->id
, strerror(-r
));
1007 m
->reload_result
= MOUNT_FAILURE_RESOURCES
;
1008 mount_enter_mounted(m
, MOUNT_SUCCESS
);
1011 static int mount_start(Unit
*u
) {
1012 Mount
*m
= MOUNT(u
);
1016 /* We cannot fulfill this request right now, try again later
1018 if (m
->state
== MOUNT_UNMOUNTING
||
1019 m
->state
== MOUNT_UNMOUNTING_SIGTERM
||
1020 m
->state
== MOUNT_UNMOUNTING_SIGKILL
||
1021 m
->state
== MOUNT_MOUNTING_SIGTERM
||
1022 m
->state
== MOUNT_MOUNTING_SIGKILL
)
1025 /* Already on it! */
1026 if (m
->state
== MOUNT_MOUNTING
)
1029 assert(m
->state
== MOUNT_DEAD
|| m
->state
== MOUNT_FAILED
);
1031 m
->result
= MOUNT_SUCCESS
;
1032 m
->reload_result
= MOUNT_SUCCESS
;
1034 mount_enter_mounting(m
);
1038 static int mount_stop(Unit
*u
) {
1039 Mount
*m
= MOUNT(u
);
1044 if (m
->state
== MOUNT_UNMOUNTING
||
1045 m
->state
== MOUNT_UNMOUNTING_SIGKILL
||
1046 m
->state
== MOUNT_UNMOUNTING_SIGTERM
||
1047 m
->state
== MOUNT_MOUNTING_SIGTERM
||
1048 m
->state
== MOUNT_MOUNTING_SIGKILL
)
1051 assert(m
->state
== MOUNT_MOUNTING
||
1052 m
->state
== MOUNT_MOUNTING_DONE
||
1053 m
->state
== MOUNT_MOUNTED
||
1054 m
->state
== MOUNT_REMOUNTING
||
1055 m
->state
== MOUNT_REMOUNTING_SIGTERM
||
1056 m
->state
== MOUNT_REMOUNTING_SIGKILL
);
1058 mount_enter_unmounting(m
);
1062 static int mount_reload(Unit
*u
) {
1063 Mount
*m
= MOUNT(u
);
1067 if (m
->state
== MOUNT_MOUNTING_DONE
)
1070 assert(m
->state
== MOUNT_MOUNTED
);
1072 mount_enter_remounting(m
);
1076 static int mount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1077 Mount
*m
= MOUNT(u
);
1083 unit_serialize_item(u
, f
, "state", mount_state_to_string(m
->state
));
1084 unit_serialize_item(u
, f
, "result", mount_result_to_string(m
->result
));
1085 unit_serialize_item(u
, f
, "reload-result", mount_result_to_string(m
->reload_result
));
1087 if (m
->control_pid
> 0)
1088 unit_serialize_item_format(u
, f
, "control-pid", PID_FMT
, m
->control_pid
);
1090 if (m
->control_command_id
>= 0)
1091 unit_serialize_item(u
, f
, "control-command", mount_exec_command_to_string(m
->control_command_id
));
1096 static int mount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1097 Mount
*m
= MOUNT(u
);
1104 if (streq(key
, "state")) {
1107 if ((state
= mount_state_from_string(value
)) < 0)
1108 log_unit_debug(u
->id
, "Failed to parse state value %s", value
);
1110 m
->deserialized_state
= state
;
1111 } else if (streq(key
, "result")) {
1114 f
= mount_result_from_string(value
);
1116 log_unit_debug(UNIT(m
)->id
,
1117 "Failed to parse result value %s", value
);
1118 else if (f
!= MOUNT_SUCCESS
)
1121 } else if (streq(key
, "reload-result")) {
1124 f
= mount_result_from_string(value
);
1126 log_unit_debug(UNIT(m
)->id
,
1127 "Failed to parse reload result value %s", value
);
1128 else if (f
!= MOUNT_SUCCESS
)
1129 m
->reload_result
= f
;
1131 } else if (streq(key
, "control-pid")) {
1134 if (parse_pid(value
, &pid
) < 0)
1135 log_unit_debug(UNIT(m
)->id
,
1136 "Failed to parse control-pid value %s", value
);
1138 m
->control_pid
= pid
;
1139 } else if (streq(key
, "control-command")) {
1140 MountExecCommand id
;
1142 if ((id
= mount_exec_command_from_string(value
)) < 0)
1143 log_unit_debug(UNIT(m
)->id
,
1144 "Failed to parse exec-command value %s", value
);
1146 m
->control_command_id
= id
;
1147 m
->control_command
= m
->exec_command
+ id
;
1150 log_unit_debug(UNIT(m
)->id
,
1151 "Unknown serialization key '%s'", key
);
1156 _pure_
static UnitActiveState
mount_active_state(Unit
*u
) {
1159 return state_translation_table
[MOUNT(u
)->state
];
1162 _pure_
static const char *mount_sub_state_to_string(Unit
*u
) {
1165 return mount_state_to_string(MOUNT(u
)->state
);
1168 _pure_
static bool mount_check_gc(Unit
*u
) {
1169 Mount
*m
= MOUNT(u
);
1173 return m
->from_proc_self_mountinfo
;
1176 static void mount_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1177 Mount
*m
= MOUNT(u
);
1183 if (pid
!= m
->control_pid
)
1188 if (is_clean_exit(code
, status
, NULL
))
1190 else if (code
== CLD_EXITED
)
1191 f
= MOUNT_FAILURE_EXIT_CODE
;
1192 else if (code
== CLD_KILLED
)
1193 f
= MOUNT_FAILURE_SIGNAL
;
1194 else if (code
== CLD_DUMPED
)
1195 f
= MOUNT_FAILURE_CORE_DUMP
;
1197 assert_not_reached("Unknown code");
1199 if (f
!= MOUNT_SUCCESS
)
1202 if (m
->control_command
) {
1203 exec_status_exit(&m
->control_command
->exec_status
, &m
->exec_context
, pid
, code
, status
);
1205 m
->control_command
= NULL
;
1206 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
1209 log_unit_full(u
->id
,
1210 f
== MOUNT_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
1211 "%s mount process exited, code=%s status=%i",
1212 u
->id
, sigchld_code_to_string(code
), status
);
1214 /* Note that mount(8) returning and the kernel sending us a
1215 * mount table change event might happen out-of-order. If an
1216 * operation succeed we assume the kernel will follow soon too
1217 * and already change into the resulting state. If it fails
1218 * we check if the kernel still knows about the mount. and
1219 * change state accordingly. */
1223 case MOUNT_MOUNTING
:
1224 case MOUNT_MOUNTING_DONE
:
1225 case MOUNT_MOUNTING_SIGKILL
:
1226 case MOUNT_MOUNTING_SIGTERM
:
1228 if (f
== MOUNT_SUCCESS
)
1229 mount_enter_mounted(m
, f
);
1230 else if (m
->from_proc_self_mountinfo
)
1231 mount_enter_mounted(m
, f
);
1233 mount_enter_dead(m
, f
);
1236 case MOUNT_REMOUNTING
:
1237 case MOUNT_REMOUNTING_SIGKILL
:
1238 case MOUNT_REMOUNTING_SIGTERM
:
1240 m
->reload_result
= f
;
1241 if (m
->from_proc_self_mountinfo
)
1242 mount_enter_mounted(m
, MOUNT_SUCCESS
);
1244 mount_enter_dead(m
, MOUNT_SUCCESS
);
1248 case MOUNT_UNMOUNTING
:
1249 case MOUNT_UNMOUNTING_SIGKILL
:
1250 case MOUNT_UNMOUNTING_SIGTERM
:
1252 if (f
== MOUNT_SUCCESS
) {
1254 if (m
->from_proc_self_mountinfo
) {
1256 /* Still a mount point? If so, let's
1257 * try again. Most likely there were
1258 * multiple mount points stacked on
1259 * top of each other. Note that due to
1260 * the io event priority logic we can
1261 * be sure the new mountinfo is loaded
1262 * before we process the SIGCHLD for
1263 * the mount command. */
1265 if (m
->n_retry_umount
< RETRY_UMOUNT_MAX
) {
1266 log_unit_debug(u
->id
, "%s: mount still present, trying again.", u
->id
);
1267 m
->n_retry_umount
++;
1268 mount_enter_unmounting(m
);
1270 log_unit_debug(u
->id
, "%s: mount still present after %u attempts to unmount, giving up.", u
->id
, m
->n_retry_umount
);
1271 mount_enter_mounted(m
, f
);
1274 mount_enter_dead(m
, f
);
1276 } else if (m
->from_proc_self_mountinfo
)
1277 mount_enter_mounted(m
, f
);
1279 mount_enter_dead(m
, f
);
1283 assert_not_reached("Uh, control process died at wrong time.");
1286 /* Notify clients about changed exit status */
1287 unit_add_to_dbus_queue(u
);
1290 static int mount_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
1291 Mount
*m
= MOUNT(userdata
);
1294 assert(m
->timer_event_source
== source
);
1298 case MOUNT_MOUNTING
:
1299 case MOUNT_MOUNTING_DONE
:
1300 log_unit_warning(UNIT(m
)->id
,
1301 "%s mounting timed out. Stopping.", UNIT(m
)->id
);
1302 mount_enter_signal(m
, MOUNT_MOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1305 case MOUNT_REMOUNTING
:
1306 log_unit_warning(UNIT(m
)->id
,
1307 "%s remounting timed out. Stopping.", UNIT(m
)->id
);
1308 m
->reload_result
= MOUNT_FAILURE_TIMEOUT
;
1309 mount_enter_mounted(m
, MOUNT_SUCCESS
);
1312 case MOUNT_UNMOUNTING
:
1313 log_unit_warning(UNIT(m
)->id
,
1314 "%s unmounting timed out. Stopping.", UNIT(m
)->id
);
1315 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, MOUNT_FAILURE_TIMEOUT
);
1318 case MOUNT_MOUNTING_SIGTERM
:
1319 if (m
->kill_context
.send_sigkill
) {
1320 log_unit_warning(UNIT(m
)->id
,
1321 "%s mounting timed out. Killing.", UNIT(m
)->id
);
1322 mount_enter_signal(m
, MOUNT_MOUNTING_SIGKILL
, MOUNT_FAILURE_TIMEOUT
);
1324 log_unit_warning(UNIT(m
)->id
,
1325 "%s mounting timed out. Skipping SIGKILL. Ignoring.",
1328 if (m
->from_proc_self_mountinfo
)
1329 mount_enter_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1331 mount_enter_dead(m
, MOUNT_FAILURE_TIMEOUT
);
1335 case MOUNT_REMOUNTING_SIGTERM
:
1336 if (m
->kill_context
.send_sigkill
) {
1337 log_unit_warning(UNIT(m
)->id
,
1338 "%s remounting timed out. Killing.", UNIT(m
)->id
);
1339 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, MOUNT_FAILURE_TIMEOUT
);
1341 log_unit_warning(UNIT(m
)->id
,
1342 "%s remounting timed out. Skipping SIGKILL. Ignoring.",
1345 if (m
->from_proc_self_mountinfo
)
1346 mount_enter_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1348 mount_enter_dead(m
, MOUNT_FAILURE_TIMEOUT
);
1352 case MOUNT_UNMOUNTING_SIGTERM
:
1353 if (m
->kill_context
.send_sigkill
) {
1354 log_unit_warning(UNIT(m
)->id
,
1355 "%s unmounting timed out. Killing.", UNIT(m
)->id
);
1356 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, MOUNT_FAILURE_TIMEOUT
);
1358 log_unit_warning(UNIT(m
)->id
,
1359 "%s unmounting timed out. Skipping SIGKILL. Ignoring.",
1362 if (m
->from_proc_self_mountinfo
)
1363 mount_enter_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1365 mount_enter_dead(m
, MOUNT_FAILURE_TIMEOUT
);
1369 case MOUNT_MOUNTING_SIGKILL
:
1370 case MOUNT_REMOUNTING_SIGKILL
:
1371 case MOUNT_UNMOUNTING_SIGKILL
:
1372 log_unit_warning(UNIT(m
)->id
,
1373 "%s mount process still around after SIGKILL. Ignoring.",
1376 if (m
->from_proc_self_mountinfo
)
1377 mount_enter_mounted(m
, MOUNT_FAILURE_TIMEOUT
);
1379 mount_enter_dead(m
, MOUNT_FAILURE_TIMEOUT
);
1383 assert_not_reached("Timeout at wrong time.");
1389 static int mount_setup_unit(
1393 const char *options
,
1397 _cleanup_free_
char *e
= NULL
, *w
= NULL
, *o
= NULL
, *f
= NULL
;
1398 bool load_extras
= false;
1400 bool delete, changed
= false;
1410 /* Ignore API mount points. They should never be referenced in
1411 * dependencies ever. */
1412 if (mount_point_is_api(where
) || mount_point_ignore(where
))
1415 if (streq(fstype
, "autofs"))
1418 /* probably some kind of swap, ignore */
1419 if (!is_path(where
))
1422 e
= unit_name_from_path(where
, ".mount");
1426 u
= manager_get_unit(m
, e
);
1430 u
= unit_new(m
, sizeof(Mount
));
1434 r
= unit_add_name(u
, e
);
1438 MOUNT(u
)->where
= strdup(where
);
1439 if (!MOUNT(u
)->where
) {
1444 u
->source_path
= strdup("/proc/self/mountinfo");
1445 if (!u
->source_path
) {
1450 if (m
->running_as
== SYSTEMD_SYSTEM
) {
1453 target
= mount_needs_network(options
, fstype
) ? SPECIAL_REMOTE_FS_TARGET
: SPECIAL_LOCAL_FS_TARGET
;
1454 r
= unit_add_dependency_by_name(u
, UNIT_BEFORE
, target
, NULL
, true);
1458 if (should_umount(MOUNT(u
))) {
1459 r
= unit_add_dependency_by_name(u
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, NULL
, true);
1465 unit_add_to_load_queue(u
);
1470 if (!MOUNT(u
)->where
) {
1471 MOUNT(u
)->where
= strdup(where
);
1472 if (!MOUNT(u
)->where
) {
1478 if (m
->running_as
== SYSTEMD_SYSTEM
&&
1479 mount_needs_network(options
, fstype
)) {
1480 /* _netdev option may have shown up late, or on a
1481 * remount. Add remote-fs dependencies, even though
1482 * local-fs ones may already be there. */
1483 unit_add_dependency_by_name(u
, UNIT_BEFORE
, SPECIAL_REMOTE_FS_TARGET
, NULL
, true);
1487 if (u
->load_state
== UNIT_NOT_FOUND
) {
1488 u
->load_state
= UNIT_LOADED
;
1491 /* Load in the extras later on, after we
1492 * finished initialization of the unit */
1499 o
= strdup(options
);
1501 if (!w
|| !o
|| !f
) {
1506 p
= &MOUNT(u
)->parameters_proc_self_mountinfo
;
1508 changed
= changed
||
1509 !streq_ptr(p
->options
, options
) ||
1510 !streq_ptr(p
->what
, what
) ||
1511 !streq_ptr(p
->fstype
, fstype
);
1514 MOUNT(u
)->is_mounted
= true;
1515 MOUNT(u
)->just_mounted
= !MOUNT(u
)->from_proc_self_mountinfo
;
1516 MOUNT(u
)->just_changed
= changed
;
1519 MOUNT(u
)->from_proc_self_mountinfo
= true;
1534 r
= mount_add_extras(MOUNT(u
));
1540 unit_add_to_dbus_queue(u
);
1545 log_warning_errno(r
, "Failed to set up mount unit: %m");
1553 static int mount_load_proc_self_mountinfo(Manager
*m
, bool set_flags
) {
1554 _cleanup_(mnt_free_tablep
) struct libmnt_table
*t
= NULL
;
1555 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*i
= NULL
;
1560 t
= mnt_new_table();
1564 i
= mnt_new_iter(MNT_ITER_FORWARD
);
1568 r
= mnt_table_parse_mtab(t
, NULL
);
1570 return log_error_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
1574 const char *device
, *path
, *options
, *fstype
;
1575 _cleanup_free_
const char *d
= NULL
, *p
= NULL
;
1576 struct libmnt_fs
*fs
;
1579 k
= mnt_table_next_fs(t
, i
, &fs
);
1583 return log_error_errno(k
, "Failed to get next entry from /proc/self/mountinfo: %m");
1585 device
= mnt_fs_get_source(fs
);
1586 path
= mnt_fs_get_target(fs
);
1587 options
= mnt_fs_get_options(fs
);
1588 fstype
= mnt_fs_get_fstype(fs
);
1590 d
= cunescape(device
);
1594 p
= cunescape(path
);
1598 (void) device_found_node(m
, d
, true, DEVICE_FOUND_MOUNT
, set_flags
);
1600 k
= mount_setup_unit(m
, d
, p
, options
, fstype
, set_flags
);
1601 if (r
== 0 && k
< 0)
1608 static void mount_shutdown(Manager
*m
) {
1611 m
->mount_event_source
= sd_event_source_unref(m
->mount_event_source
);
1612 m
->mount_utab_event_source
= sd_event_source_unref(m
->mount_utab_event_source
);
1614 if (m
->proc_self_mountinfo
) {
1615 fclose(m
->proc_self_mountinfo
);
1616 m
->proc_self_mountinfo
= NULL
;
1618 m
->utab_inotify_fd
= safe_close(m
->utab_inotify_fd
);
1621 static int mount_get_timeout(Unit
*u
, uint64_t *timeout
) {
1622 Mount
*m
= MOUNT(u
);
1625 if (!m
->timer_event_source
)
1628 r
= sd_event_source_get_time(m
->timer_event_source
, timeout
);
1635 static int mount_enumerate(Manager
*m
) {
1641 if (!m
->proc_self_mountinfo
) {
1642 m
->proc_self_mountinfo
= fopen("/proc/self/mountinfo", "re");
1643 if (!m
->proc_self_mountinfo
)
1646 r
= sd_event_add_io(m
->event
, &m
->mount_event_source
, fileno(m
->proc_self_mountinfo
), EPOLLPRI
, mount_dispatch_io
, m
);
1650 /* Dispatch this before we dispatch SIGCHLD, so that
1651 * we always get the events from /proc/self/mountinfo
1652 * before the SIGCHLD of /bin/mount. */
1653 r
= sd_event_source_set_priority(m
->mount_event_source
, -10);
1658 if (m
->utab_inotify_fd
< 0) {
1659 m
->utab_inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
1660 if (m
->utab_inotify_fd
< 0) {
1665 (void) mkdir_p_label("/run/mount", 0755);
1667 r
= inotify_add_watch(m
->utab_inotify_fd
, "/run/mount", IN_MOVED_TO
);
1673 r
= sd_event_add_io(m
->event
, &m
->mount_utab_event_source
, m
->utab_inotify_fd
, EPOLLIN
, mount_dispatch_io
, m
);
1677 r
= sd_event_source_set_priority(m
->mount_utab_event_source
, -10);
1682 r
= mount_load_proc_self_mountinfo(m
, false);
1693 static int mount_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
1694 Manager
*m
= userdata
;
1699 assert(revents
& (EPOLLPRI
| EPOLLIN
));
1701 /* The manager calls this for every fd event happening on the
1702 * /proc/self/mountinfo file, which informs us about mounting
1703 * table changes, and for /run/mount events which we watch
1704 * for mount options. */
1706 if (fd
== m
->utab_inotify_fd
) {
1707 bool rescan
= false;
1709 /* FIXME: We *really* need to replace this with
1710 * libmount's own API for this, we should not hardcode
1711 * internal behaviour of libmount here. */
1714 union inotify_event_buffer buffer
;
1715 struct inotify_event
*e
;
1718 l
= read(fd
, &buffer
, sizeof(buffer
));
1720 if (errno
== EAGAIN
|| errno
== EINTR
)
1723 log_error_errno(errno
, "Failed to read utab inotify: %m");
1727 FOREACH_INOTIFY_EVENT(e
, buffer
, l
) {
1728 /* Only care about changes to utab,
1729 * but we have to monitor the
1730 * directory to reliably get
1731 * notifications about when utab is
1732 * replaced using rename(2) */
1733 if ((e
->mask
& IN_Q_OVERFLOW
) || streq(e
->name
, "utab"))
1742 r
= mount_load_proc_self_mountinfo(m
, true);
1744 /* Reset flags, just in case, for later calls */
1745 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
]) {
1746 Mount
*mount
= MOUNT(u
);
1748 mount
->is_mounted
= mount
->just_mounted
= mount
->just_changed
= false;
1754 manager_dispatch_load_queue(m
);
1756 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_MOUNT
]) {
1757 Mount
*mount
= MOUNT(u
);
1759 if (!mount
->is_mounted
) {
1761 mount
->from_proc_self_mountinfo
= false;
1763 switch (mount
->state
) {
1766 /* This has just been unmounted by
1767 * somebody else, follow the state
1769 mount_enter_dead(mount
, MOUNT_SUCCESS
);
1776 if (mount
->parameters_proc_self_mountinfo
.what
)
1777 (void) device_found_node(m
, mount
->parameters_proc_self_mountinfo
.what
, false, DEVICE_FOUND_MOUNT
, true);
1780 } else if (mount
->just_mounted
|| mount
->just_changed
) {
1782 /* New or changed mount entry */
1784 switch (mount
->state
) {
1788 /* This has just been mounted by
1789 * somebody else, follow the state
1791 mount_enter_mounted(mount
, MOUNT_SUCCESS
);
1794 case MOUNT_MOUNTING
:
1795 mount_set_state(mount
, MOUNT_MOUNTING_DONE
);
1799 /* Nothing really changed, but let's
1800 * issue an notification call
1801 * nonetheless, in case somebody is
1802 * waiting for this. (e.g. file system
1803 * ro/rw remounts.) */
1804 mount_set_state(mount
, mount
->state
);
1809 /* Reset the flags for later calls */
1810 mount
->is_mounted
= mount
->just_mounted
= mount
->just_changed
= false;
1816 static void mount_reset_failed(Unit
*u
) {
1817 Mount
*m
= MOUNT(u
);
1821 if (m
->state
== MOUNT_FAILED
)
1822 mount_set_state(m
, MOUNT_DEAD
);
1824 m
->result
= MOUNT_SUCCESS
;
1825 m
->reload_result
= MOUNT_SUCCESS
;
1828 static int mount_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
1829 return unit_kill_common(u
, who
, signo
, -1, MOUNT(u
)->control_pid
, error
);
1832 static const char* const mount_state_table
[_MOUNT_STATE_MAX
] = {
1833 [MOUNT_DEAD
] = "dead",
1834 [MOUNT_MOUNTING
] = "mounting",
1835 [MOUNT_MOUNTING_DONE
] = "mounting-done",
1836 [MOUNT_MOUNTED
] = "mounted",
1837 [MOUNT_REMOUNTING
] = "remounting",
1838 [MOUNT_UNMOUNTING
] = "unmounting",
1839 [MOUNT_MOUNTING_SIGTERM
] = "mounting-sigterm",
1840 [MOUNT_MOUNTING_SIGKILL
] = "mounting-sigkill",
1841 [MOUNT_REMOUNTING_SIGTERM
] = "remounting-sigterm",
1842 [MOUNT_REMOUNTING_SIGKILL
] = "remounting-sigkill",
1843 [MOUNT_UNMOUNTING_SIGTERM
] = "unmounting-sigterm",
1844 [MOUNT_UNMOUNTING_SIGKILL
] = "unmounting-sigkill",
1845 [MOUNT_FAILED
] = "failed"
1848 DEFINE_STRING_TABLE_LOOKUP(mount_state
, MountState
);
1850 static const char* const mount_exec_command_table
[_MOUNT_EXEC_COMMAND_MAX
] = {
1851 [MOUNT_EXEC_MOUNT
] = "ExecMount",
1852 [MOUNT_EXEC_UNMOUNT
] = "ExecUnmount",
1853 [MOUNT_EXEC_REMOUNT
] = "ExecRemount",
1856 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command
, MountExecCommand
);
1858 static const char* const mount_result_table
[_MOUNT_RESULT_MAX
] = {
1859 [MOUNT_SUCCESS
] = "success",
1860 [MOUNT_FAILURE_RESOURCES
] = "resources",
1861 [MOUNT_FAILURE_TIMEOUT
] = "timeout",
1862 [MOUNT_FAILURE_EXIT_CODE
] = "exit-code",
1863 [MOUNT_FAILURE_SIGNAL
] = "signal",
1864 [MOUNT_FAILURE_CORE_DUMP
] = "core-dump"
1867 DEFINE_STRING_TABLE_LOOKUP(mount_result
, MountResult
);
1869 const UnitVTable mount_vtable
= {
1870 .object_size
= sizeof(Mount
),
1871 .exec_context_offset
= offsetof(Mount
, exec_context
),
1872 .cgroup_context_offset
= offsetof(Mount
, cgroup_context
),
1873 .kill_context_offset
= offsetof(Mount
, kill_context
),
1874 .exec_runtime_offset
= offsetof(Mount
, exec_runtime
),
1880 .private_section
= "Mount",
1883 .no_instances
= true,
1889 .coldplug
= mount_coldplug
,
1893 .start
= mount_start
,
1895 .reload
= mount_reload
,
1899 .serialize
= mount_serialize
,
1900 .deserialize_item
= mount_deserialize_item
,
1902 .active_state
= mount_active_state
,
1903 .sub_state_to_string
= mount_sub_state_to_string
,
1905 .check_gc
= mount_check_gc
,
1907 .sigchld_event
= mount_sigchld_event
,
1909 .reset_failed
= mount_reset_failed
,
1911 .bus_interface
= "org.freedesktop.systemd1.Mount",
1912 .bus_vtable
= bus_mount_vtable
,
1913 .bus_set_property
= bus_mount_set_property
,
1914 .bus_commit_properties
= bus_mount_commit_properties
,
1916 .get_timeout
= mount_get_timeout
,
1918 .can_transient
= true,
1920 .enumerate
= mount_enumerate
,
1921 .shutdown
= mount_shutdown
,
1923 .status_message_formats
= {
1924 .starting_stopping
= {
1925 [0] = "Mounting %s...",
1926 [1] = "Unmounting %s...",
1928 .finished_start_job
= {
1929 [JOB_DONE
] = "Mounted %s.",
1930 [JOB_FAILED
] = "Failed to mount %s.",
1931 [JOB_DEPENDENCY
] = "Dependency failed for %s.",
1932 [JOB_TIMEOUT
] = "Timed out mounting %s.",
1934 .finished_stop_job
= {
1935 [JOB_DONE
] = "Unmounted %s.",
1936 [JOB_FAILED
] = "Failed unmounting %s.",
1937 [JOB_TIMEOUT
] = "Timed out unmounting %s.",