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/>.
25 #include <sys/epoll.h>
31 #include "load-fragment.h"
32 #include "load-dropin.h"
34 #include "sd-messages.h"
37 #include "path-util.h"
38 #include "mount-setup.h"
39 #include "unit-name.h"
40 #include "dbus-mount.h"
42 #include "bus-errors.h"
43 #include "exit-status.h"
46 static const UnitActiveState state_translation_table[_MOUNT_STATE_MAX] = {
47 [MOUNT_DEAD] = UNIT_INACTIVE,
48 [MOUNT_MOUNTING] = UNIT_ACTIVATING,
49 [MOUNT_MOUNTING_DONE] = UNIT_ACTIVE,
50 [MOUNT_MOUNTED] = UNIT_ACTIVE,
51 [MOUNT_REMOUNTING] = UNIT_RELOADING,
52 [MOUNT_UNMOUNTING] = UNIT_DEACTIVATING,
53 [MOUNT_MOUNTING_SIGTERM] = UNIT_DEACTIVATING,
54 [MOUNT_MOUNTING_SIGKILL] = UNIT_DEACTIVATING,
55 [MOUNT_REMOUNTING_SIGTERM] = UNIT_RELOADING,
56 [MOUNT_REMOUNTING_SIGKILL] = UNIT_RELOADING,
57 [MOUNT_UNMOUNTING_SIGTERM] = UNIT_DEACTIVATING,
58 [MOUNT_UNMOUNTING_SIGKILL] = UNIT_DEACTIVATING,
59 [MOUNT_FAILED] = UNIT_FAILED
62 static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
63 static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
65 static char* mount_test_option(const char *haystack, const char *needle) {
66 struct mntent me = { .mnt_opts = (char*) haystack };
70 /* Like glibc's hasmntopt(), but works on a string, not a
76 return hasmntopt(&me, needle);
79 static bool mount_is_network(MountParameters *p) {
82 if (mount_test_option(p->options, "_netdev"))
85 if (p->fstype && fstype_is_network(p->fstype))
91 static bool mount_is_bind(MountParameters *p) {
94 if (mount_test_option(p->options, "bind"))
97 if (p->fstype && streq(p->fstype, "bind"))
100 if (mount_test_option(p->options, "rbind"))
103 if (p->fstype && streq(p->fstype, "rbind"))
109 static bool mount_is_auto(MountParameters *p) {
112 return !mount_test_option(p->options, "noauto");
115 static bool needs_quota(MountParameters *p) {
118 if (mount_is_network(p))
121 if (mount_is_bind(p))
124 return mount_test_option(p->options, "usrquota") ||
125 mount_test_option(p->options, "grpquota") ||
126 mount_test_option(p->options, "quota") ||
127 mount_test_option(p->options, "usrjquota") ||
128 mount_test_option(p->options, "grpjquota");
131 static void mount_init(Unit *u) {
135 assert(u->load_state == UNIT_STUB);
137 m->timeout_usec = u->manager->default_timeout_start_usec;
138 m->directory_mode = 0755;
140 exec_context_init(&m->exec_context);
141 kill_context_init(&m->kill_context);
142 cgroup_context_init(&m->cgroup_context);
144 if (unit_has_name(u, "-.mount")) {
145 /* Don't allow start/stop for root directory */
146 u->refuse_manual_start = true;
147 u->refuse_manual_stop = true;
149 /* The stdio/kmsg bridge socket is on /, in order to avoid a
150 * dep loop, don't use kmsg logging for -.mount */
151 m->exec_context.std_output = u->manager->default_std_output;
152 m->exec_context.std_error = u->manager->default_std_error;
155 /* We need to make sure that /bin/mount is always called in
156 * the same process group as us, so that the autofs kernel
157 * side doesn't send us another mount request while we are
158 * already trying to comply its last one. */
159 m->exec_context.same_pgrp = true;
161 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
163 u->ignore_on_isolate = true;
166 static int mount_arm_timer(Mount *m) {
171 if (m->timeout_usec <= 0) {
172 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
176 if (m->timer_event_source) {
177 r = sd_event_source_set_time(m->timer_event_source, now(CLOCK_MONOTONIC) + m->timeout_usec);
181 return sd_event_source_set_enabled(m->timer_event_source, SD_EVENT_ONESHOT);
184 return sd_event_add_monotonic(UNIT(m)->manager->event, now(CLOCK_MONOTONIC) + m->timeout_usec, 0, mount_dispatch_timer, m, &m->timer_event_source);
187 static void mount_unwatch_control_pid(Mount *m) {
190 if (m->control_pid <= 0)
193 unit_unwatch_pid(UNIT(m), m->control_pid);
197 static void mount_parameters_done(MountParameters *p) {
204 p->what = p->options = p->fstype = NULL;
207 static void mount_done(Unit *u) {
215 mount_parameters_done(&m->parameters_proc_self_mountinfo);
216 mount_parameters_done(&m->parameters_fragment);
218 cgroup_context_done(&m->cgroup_context);
219 exec_context_done(&m->exec_context);
220 m->exec_runtime = exec_runtime_unref(m->exec_runtime);
221 exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
222 m->control_command = NULL;
224 mount_unwatch_control_pid(m);
226 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
229 _pure_ static MountParameters* get_mount_parameters_fragment(Mount *m) {
232 if (m->from_fragment)
233 return &m->parameters_fragment;
238 _pure_ static MountParameters* get_mount_parameters(Mount *m) {
241 if (m->from_proc_self_mountinfo)
242 return &m->parameters_proc_self_mountinfo;
244 return get_mount_parameters_fragment(m);
247 static int mount_add_mount_links(Mount *m) {
248 _cleanup_free_ char *parent = NULL;
257 if (!path_equal(m->where, "/")) {
258 /* Adds in links to other mount points that might lie further
259 * up in the hierarchy */
260 r = path_get_parent(m->where, &parent);
264 r = unit_require_mounts_for(UNIT(m), parent);
269 /* Adds in links to other mount points that might be needed
270 * for the source path (if this is a bind mount) to be
272 pm = get_mount_parameters_fragment(m);
273 if (pm && pm->what &&
274 path_is_absolute(pm->what) &&
275 !mount_is_network(pm)) {
277 r = unit_require_mounts_for(UNIT(m), pm->what);
282 /* Adds in links to other units that use this path or paths
283 * further down in the hierarchy */
284 s = manager_get_units_requiring_mounts_for(UNIT(m)->manager, m->where);
285 SET_FOREACH(other, s, i) {
287 if (other->load_state != UNIT_LOADED)
290 if (other == UNIT(m))
293 r = unit_add_dependency(other, UNIT_AFTER, UNIT(m), true);
297 if (UNIT(m)->fragment_path) {
298 /* If we have fragment configuration, then make this dependency required */
299 r = unit_add_dependency(other, UNIT_REQUIRES, UNIT(m), true);
308 static int mount_add_device_links(Mount *m) {
310 bool device_wants_mount = false;
315 p = get_mount_parameters_fragment(m);
322 if (mount_is_bind(p))
325 if (!is_device_path(p->what))
328 if (path_equal(m->where, "/"))
331 if (mount_is_auto(p) && UNIT(m)->manager->running_as == SYSTEMD_SYSTEM)
332 device_wants_mount = true;
334 r = unit_add_node_link(UNIT(m), p->what, device_wants_mount);
341 static int mount_add_quota_links(Mount *m) {
347 if (UNIT(m)->manager->running_as != SYSTEMD_SYSTEM)
350 p = get_mount_parameters_fragment(m);
357 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTACHECK_SERVICE, NULL, true);
361 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTAON_SERVICE, NULL, true);
368 static bool should_umount(Mount *m) {
371 if (path_equal(m->where, "/") ||
372 path_equal(m->where, "/usr"))
375 p = get_mount_parameters(m);
376 if (p && mount_test_option(p->options, "x-initrd.mount") &&
383 static int mount_add_default_dependencies(Mount *m) {
384 const char *after, *after2, *online;
390 if (UNIT(m)->manager->running_as != SYSTEMD_SYSTEM)
393 p = get_mount_parameters(m);
398 if (path_equal(m->where, "/"))
401 if (mount_is_network(p)) {
402 after = SPECIAL_REMOTE_FS_PRE_TARGET;
403 after2 = SPECIAL_NETWORK_TARGET;
404 online = SPECIAL_NETWORK_ONLINE_TARGET;
406 after = SPECIAL_LOCAL_FS_PRE_TARGET;
411 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after, NULL, true);
416 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after2, NULL, true);
422 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_WANTS, UNIT_AFTER, online, NULL, true);
427 if (should_umount(m)) {
428 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
436 static int mount_fix_timeouts(Mount *m) {
438 const char *timeout = NULL;
447 p = get_mount_parameters_fragment(m);
451 /* Allow configuration how long we wait for a device that
452 * backs a mount point to show up. This is useful to support
453 * endless device timeouts for devices that show up only after
454 * user input, like crypto devices. */
456 if ((timeout = mount_test_option(p->options, "comment=systemd.device-timeout")))
458 else if ((timeout = mount_test_option(p->options, "x-systemd.device-timeout")))
463 t = strndup(timeout, strcspn(timeout, ",;" WHITESPACE));
467 r = parse_sec(t, &u);
471 log_warning_unit(UNIT(m)->id,
472 "Failed to parse timeout for %s, ignoring: %s",
477 SET_FOREACH(other, UNIT(m)->dependencies[UNIT_AFTER], i) {
478 if (other->type != UNIT_DEVICE)
481 other->job_timeout = u;
487 static int mount_verify(Mount *m) {
488 _cleanup_free_ char *e = NULL;
493 if (UNIT(m)->load_state != UNIT_LOADED)
496 if (!m->from_fragment && !m->from_proc_self_mountinfo)
499 e = unit_name_from_path(m->where, ".mount");
503 b = unit_has_name(UNIT(m), e);
505 log_error_unit(UNIT(m)->id, "%s's Where= setting doesn't match unit name. Refusing.", UNIT(m)->id);
509 if (mount_point_is_api(m->where) || mount_point_ignore(m->where)) {
510 log_error_unit(UNIT(m)->id, "Cannot create mount unit for API file system %s. Refusing.", m->where);
514 if (UNIT(m)->fragment_path && !m->parameters_fragment.what) {
515 log_error_unit(UNIT(m)->id, "%s's What setting is missing. Refusing.", UNIT(m)->id);
519 if (m->exec_context.pam_name && m->kill_context.kill_mode != KILL_CONTROL_GROUP) {
520 log_error_unit(UNIT(m)->id, "%s has PAM enabled. Kill mode must be set to control-group'. Refusing.",UNIT(m)->id);
527 static int mount_add_extras(Mount *m) {
533 if (u->fragment_path)
534 m->from_fragment = true;
537 m->where = unit_name_to_path(u->id);
542 path_kill_slashes(m->where);
544 r = unit_add_exec_dependencies(u, &m->exec_context);
548 if (!u->description) {
549 r = unit_set_description(u, m->where);
554 r = mount_add_device_links(m);
558 r = mount_add_mount_links(m);
562 r = mount_add_quota_links(m);
566 if (u->default_dependencies) {
567 r = mount_add_default_dependencies(m);
572 r = unit_add_default_slice(u);
576 r = mount_fix_timeouts(m);
580 r = unit_exec_context_defaults(u, &m->exec_context);
587 static int mount_load(Unit *u) {
592 assert(u->load_state == UNIT_STUB);
594 if (m->from_proc_self_mountinfo)
595 r = unit_load_fragment_and_dropin_optional(u);
597 r = unit_load_fragment_and_dropin(u);
602 /* This is a new unit? Then let's add in some extras */
603 if (u->load_state == UNIT_LOADED) {
604 r = mount_add_extras(m);
609 return mount_verify(m);
612 static int mount_notify_automount(Mount *m, int status) {
619 SET_FOREACH(p, UNIT(m)->dependencies[UNIT_TRIGGERED_BY], i)
620 if (p->type == UNIT_AUTOMOUNT) {
621 r = automount_send_ready(AUTOMOUNT(p), status);
629 static void mount_set_state(Mount *m, MountState state) {
630 MountState old_state;
633 old_state = m->state;
636 if (state != MOUNT_MOUNTING &&
637 state != MOUNT_MOUNTING_DONE &&
638 state != MOUNT_REMOUNTING &&
639 state != MOUNT_UNMOUNTING &&
640 state != MOUNT_MOUNTING_SIGTERM &&
641 state != MOUNT_MOUNTING_SIGKILL &&
642 state != MOUNT_UNMOUNTING_SIGTERM &&
643 state != MOUNT_UNMOUNTING_SIGKILL &&
644 state != MOUNT_REMOUNTING_SIGTERM &&
645 state != MOUNT_REMOUNTING_SIGKILL) {
646 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
647 mount_unwatch_control_pid(m);
648 m->control_command = NULL;
649 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
652 if (state == MOUNT_MOUNTED ||
653 state == MOUNT_REMOUNTING)
654 mount_notify_automount(m, 0);
655 else if (state == MOUNT_DEAD ||
656 state == MOUNT_UNMOUNTING ||
657 state == MOUNT_MOUNTING_SIGTERM ||
658 state == MOUNT_MOUNTING_SIGKILL ||
659 state == MOUNT_REMOUNTING_SIGTERM ||
660 state == MOUNT_REMOUNTING_SIGKILL ||
661 state == MOUNT_UNMOUNTING_SIGTERM ||
662 state == MOUNT_UNMOUNTING_SIGKILL ||
663 state == MOUNT_FAILED) {
664 if (state != old_state)
665 mount_notify_automount(m, -ENODEV);
668 if (state != old_state)
669 log_debug_unit(UNIT(m)->id,
670 "%s changed %s -> %s",
672 mount_state_to_string(old_state),
673 mount_state_to_string(state));
675 unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state], m->reload_result == MOUNT_SUCCESS);
676 m->reload_result = MOUNT_SUCCESS;
679 static int mount_coldplug(Unit *u) {
681 MountState new_state = MOUNT_DEAD;
685 assert(m->state == MOUNT_DEAD);
687 if (m->deserialized_state != m->state)
688 new_state = m->deserialized_state;
689 else if (m->from_proc_self_mountinfo)
690 new_state = MOUNT_MOUNTED;
692 if (new_state == m->state)
695 if (new_state == MOUNT_MOUNTING ||
696 new_state == MOUNT_MOUNTING_DONE ||
697 new_state == MOUNT_REMOUNTING ||
698 new_state == MOUNT_UNMOUNTING ||
699 new_state == MOUNT_MOUNTING_SIGTERM ||
700 new_state == MOUNT_MOUNTING_SIGKILL ||
701 new_state == MOUNT_UNMOUNTING_SIGTERM ||
702 new_state == MOUNT_UNMOUNTING_SIGKILL ||
703 new_state == MOUNT_REMOUNTING_SIGTERM ||
704 new_state == MOUNT_REMOUNTING_SIGKILL) {
706 if (m->control_pid <= 0)
709 r = unit_watch_pid(UNIT(m), m->control_pid);
713 r = mount_arm_timer(m);
718 mount_set_state(m, new_state);
722 static void mount_dump(Unit *u, FILE *f, const char *prefix) {
729 p = get_mount_parameters(m);
732 "%sMount State: %s\n"
736 "%sFile System Type: %s\n"
738 "%sFrom /proc/self/mountinfo: %s\n"
739 "%sFrom fragment: %s\n"
740 "%sDirectoryMode: %04o\n",
741 prefix, mount_state_to_string(m->state),
742 prefix, mount_result_to_string(m->result),
744 prefix, p ? strna(p->what) : "n/a",
745 prefix, p ? strna(p->fstype) : "n/a",
746 prefix, p ? strna(p->options) : "n/a",
747 prefix, yes_no(m->from_proc_self_mountinfo),
748 prefix, yes_no(m->from_fragment),
749 prefix, m->directory_mode);
751 if (m->control_pid > 0)
753 "%sControl PID: "PID_FMT"\n",
754 prefix, m->control_pid);
756 exec_context_dump(&m->exec_context, f, prefix);
757 kill_context_dump(&m->kill_context, f, prefix);
760 static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
768 unit_realize_cgroup(UNIT(m));
770 r = unit_setup_exec_runtime(UNIT(m));
774 r = mount_arm_timer(m);
782 UNIT(m)->manager->environment,
786 UNIT(m)->manager->confirm_spawn,
787 UNIT(m)->manager->cgroup_supported,
788 UNIT(m)->cgroup_path,
797 r = unit_watch_pid(UNIT(m), pid);
799 /* FIXME: we need to do something here */
807 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
812 static void mount_enter_dead(Mount *m, MountResult f) {
815 if (f != MOUNT_SUCCESS)
818 exec_runtime_destroy(m->exec_runtime);
819 m->exec_runtime = exec_runtime_unref(m->exec_runtime);
821 mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD);
824 static void mount_enter_mounted(Mount *m, MountResult f) {
827 if (f != MOUNT_SUCCESS)
830 mount_set_state(m, MOUNT_MOUNTED);
833 static void mount_enter_signal(Mount *m, MountState state, MountResult f) {
838 if (f != MOUNT_SUCCESS)
841 r = unit_kill_context(
844 state != MOUNT_MOUNTING_SIGTERM && state != MOUNT_UNMOUNTING_SIGTERM && state != MOUNT_REMOUNTING_SIGTERM,
852 r = mount_arm_timer(m);
856 mount_set_state(m, state);
857 } else if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
858 mount_enter_mounted(m, MOUNT_SUCCESS);
860 mount_enter_dead(m, MOUNT_SUCCESS);
865 log_warning_unit(UNIT(m)->id,
866 "%s failed to kill processes: %s", UNIT(m)->id, strerror(-r));
868 if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
869 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
871 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
874 void warn_if_dir_nonempty(const char *unit, const char* where) {
878 if (dir_is_empty(where) > 0)
881 log_struct_unit(LOG_NOTICE,
883 "MESSAGE=%s: Directory %s to mount over is not empty, mounting anyway.",
886 MESSAGE_ID(SD_MESSAGE_OVERMOUNTING),
890 static void mount_enter_unmounting(Mount *m) {
895 m->control_command_id = MOUNT_EXEC_UNMOUNT;
896 m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
898 if ((r = exec_command_set(
905 mount_unwatch_control_pid(m);
907 if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
910 mount_set_state(m, MOUNT_UNMOUNTING);
915 log_warning_unit(UNIT(m)->id,
916 "%s failed to run 'umount' task: %s",
917 UNIT(m)->id, strerror(-r));
918 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
921 static void mount_enter_mounting(Mount *m) {
927 m->control_command_id = MOUNT_EXEC_MOUNT;
928 m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
930 mkdir_p_label(m->where, m->directory_mode);
932 warn_if_dir_nonempty(m->meta.id, m->where);
934 /* Create the source directory for bind-mounts if needed */
935 p = get_mount_parameters_fragment(m);
936 if (p && mount_is_bind(p))
937 mkdir_p_label(p->what, m->directory_mode);
939 if (m->from_fragment)
940 r = exec_command_set(
943 m->parameters_fragment.what,
945 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
946 m->parameters_fragment.options ? "-o" : NULL, m->parameters_fragment.options,
954 mount_unwatch_control_pid(m);
956 r = mount_spawn(m, m->control_command, &m->control_pid);
960 mount_set_state(m, MOUNT_MOUNTING);
965 log_warning_unit(UNIT(m)->id,
966 "%s failed to run 'mount' task: %s",
967 UNIT(m)->id, strerror(-r));
968 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
971 static void mount_enter_remounting(Mount *m) {
976 m->control_command_id = MOUNT_EXEC_REMOUNT;
977 m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
979 if (m->from_fragment) {
982 if (m->parameters_fragment.options)
983 o = strappenda("remount,", m->parameters_fragment.options);
987 r = exec_command_set(
990 m->parameters_fragment.what,
992 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
1001 mount_unwatch_control_pid(m);
1003 r = mount_spawn(m, m->control_command, &m->control_pid);
1007 mount_set_state(m, MOUNT_REMOUNTING);
1012 log_warning_unit(UNIT(m)->id,
1013 "%s failed to run 'remount' task: %s",
1014 UNIT(m)->id, strerror(-r));
1015 m->reload_result = MOUNT_FAILURE_RESOURCES;
1016 mount_enter_mounted(m, MOUNT_SUCCESS);
1019 static int mount_start(Unit *u) {
1020 Mount *m = MOUNT(u);
1024 /* We cannot fulfill this request right now, try again later
1026 if (m->state == MOUNT_UNMOUNTING ||
1027 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1028 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1029 m->state == MOUNT_MOUNTING_SIGTERM ||
1030 m->state == MOUNT_MOUNTING_SIGKILL)
1033 /* Already on it! */
1034 if (m->state == MOUNT_MOUNTING)
1037 assert(m->state == MOUNT_DEAD || m->state == MOUNT_FAILED);
1039 m->result = MOUNT_SUCCESS;
1040 m->reload_result = MOUNT_SUCCESS;
1042 mount_enter_mounting(m);
1046 static int mount_stop(Unit *u) {
1047 Mount *m = MOUNT(u);
1052 if (m->state == MOUNT_UNMOUNTING ||
1053 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1054 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1055 m->state == MOUNT_MOUNTING_SIGTERM ||
1056 m->state == MOUNT_MOUNTING_SIGKILL)
1059 assert(m->state == MOUNT_MOUNTING ||
1060 m->state == MOUNT_MOUNTING_DONE ||
1061 m->state == MOUNT_MOUNTED ||
1062 m->state == MOUNT_REMOUNTING ||
1063 m->state == MOUNT_REMOUNTING_SIGTERM ||
1064 m->state == MOUNT_REMOUNTING_SIGKILL);
1066 mount_enter_unmounting(m);
1070 static int mount_reload(Unit *u) {
1071 Mount *m = MOUNT(u);
1075 if (m->state == MOUNT_MOUNTING_DONE)
1078 assert(m->state == MOUNT_MOUNTED);
1080 mount_enter_remounting(m);
1084 static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
1085 Mount *m = MOUNT(u);
1091 unit_serialize_item(u, f, "state", mount_state_to_string(m->state));
1092 unit_serialize_item(u, f, "result", mount_result_to_string(m->result));
1093 unit_serialize_item(u, f, "reload-result", mount_result_to_string(m->reload_result));
1095 if (m->control_pid > 0)
1096 unit_serialize_item_format(u, f, "control-pid", PID_FMT, m->control_pid);
1098 if (m->control_command_id >= 0)
1099 unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(m->control_command_id));
1104 static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1105 Mount *m = MOUNT(u);
1112 if (streq(key, "state")) {
1115 if ((state = mount_state_from_string(value)) < 0)
1116 log_debug_unit(u->id, "Failed to parse state value %s", value);
1118 m->deserialized_state = state;
1119 } else if (streq(key, "result")) {
1122 f = mount_result_from_string(value);
1124 log_debug_unit(UNIT(m)->id,
1125 "Failed to parse result value %s", value);
1126 else if (f != MOUNT_SUCCESS)
1129 } else if (streq(key, "reload-result")) {
1132 f = mount_result_from_string(value);
1134 log_debug_unit(UNIT(m)->id,
1135 "Failed to parse reload result value %s", value);
1136 else if (f != MOUNT_SUCCESS)
1137 m->reload_result = f;
1139 } else if (streq(key, "control-pid")) {
1142 if (parse_pid(value, &pid) < 0)
1143 log_debug_unit(UNIT(m)->id,
1144 "Failed to parse control-pid value %s", value);
1146 m->control_pid = pid;
1147 } else if (streq(key, "control-command")) {
1148 MountExecCommand id;
1150 if ((id = mount_exec_command_from_string(value)) < 0)
1151 log_debug_unit(UNIT(m)->id,
1152 "Failed to parse exec-command value %s", value);
1154 m->control_command_id = id;
1155 m->control_command = m->exec_command + id;
1158 log_debug_unit(UNIT(m)->id,
1159 "Unknown serialization key '%s'", key);
1164 _pure_ static UnitActiveState mount_active_state(Unit *u) {
1167 return state_translation_table[MOUNT(u)->state];
1170 _pure_ static const char *mount_sub_state_to_string(Unit *u) {
1173 return mount_state_to_string(MOUNT(u)->state);
1176 _pure_ static bool mount_check_gc(Unit *u) {
1177 Mount *m = MOUNT(u);
1181 return m->from_proc_self_mountinfo;
1184 static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1185 Mount *m = MOUNT(u);
1191 if (pid != m->control_pid)
1196 if (is_clean_exit(code, status, NULL))
1198 else if (code == CLD_EXITED)
1199 f = MOUNT_FAILURE_EXIT_CODE;
1200 else if (code == CLD_KILLED)
1201 f = MOUNT_FAILURE_SIGNAL;
1202 else if (code == CLD_DUMPED)
1203 f = MOUNT_FAILURE_CORE_DUMP;
1205 assert_not_reached("Unknown code");
1207 if (f != MOUNT_SUCCESS)
1210 if (m->control_command) {
1211 exec_status_exit(&m->control_command->exec_status, &m->exec_context, pid, code, status);
1213 m->control_command = NULL;
1214 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
1217 log_full_unit(f == MOUNT_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
1218 "%s mount process exited, code=%s status=%i",
1219 u->id, sigchld_code_to_string(code), status);
1221 /* Note that mount(8) returning and the kernel sending us a
1222 * mount table change event might happen out-of-order. If an
1223 * operation succeed we assume the kernel will follow soon too
1224 * and already change into the resulting state. If it fails
1225 * we check if the kernel still knows about the mount. and
1226 * change state accordingly. */
1230 case MOUNT_MOUNTING:
1231 case MOUNT_MOUNTING_DONE:
1232 case MOUNT_MOUNTING_SIGKILL:
1233 case MOUNT_MOUNTING_SIGTERM:
1235 if (f == MOUNT_SUCCESS)
1236 mount_enter_mounted(m, f);
1237 else if (m->from_proc_self_mountinfo)
1238 mount_enter_mounted(m, f);
1240 mount_enter_dead(m, f);
1243 case MOUNT_REMOUNTING:
1244 case MOUNT_REMOUNTING_SIGKILL:
1245 case MOUNT_REMOUNTING_SIGTERM:
1247 m->reload_result = f;
1248 if (m->from_proc_self_mountinfo)
1249 mount_enter_mounted(m, MOUNT_SUCCESS);
1251 mount_enter_dead(m, MOUNT_SUCCESS);
1255 case MOUNT_UNMOUNTING:
1256 case MOUNT_UNMOUNTING_SIGKILL:
1257 case MOUNT_UNMOUNTING_SIGTERM:
1259 if (f == MOUNT_SUCCESS)
1260 mount_enter_dead(m, f);
1261 else if (m->from_proc_self_mountinfo)
1262 mount_enter_mounted(m, f);
1264 mount_enter_dead(m, f);
1268 assert_not_reached("Uh, control process died at wrong time.");
1271 /* Notify clients about changed exit status */
1272 unit_add_to_dbus_queue(u);
1275 static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
1276 Mount *m = MOUNT(userdata);
1279 assert(m->timer_event_source == source);
1283 case MOUNT_MOUNTING:
1284 case MOUNT_MOUNTING_DONE:
1285 log_warning_unit(UNIT(m)->id,
1286 "%s mounting timed out. Stopping.", UNIT(m)->id);
1287 mount_enter_signal(m, MOUNT_MOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1290 case MOUNT_REMOUNTING:
1291 log_warning_unit(UNIT(m)->id,
1292 "%s remounting timed out. Stopping.", UNIT(m)->id);
1293 m->reload_result = MOUNT_FAILURE_TIMEOUT;
1294 mount_enter_mounted(m, MOUNT_SUCCESS);
1297 case MOUNT_UNMOUNTING:
1298 log_warning_unit(UNIT(m)->id,
1299 "%s unmounting timed out. Stopping.", UNIT(m)->id);
1300 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1303 case MOUNT_MOUNTING_SIGTERM:
1304 if (m->kill_context.send_sigkill) {
1305 log_warning_unit(UNIT(m)->id,
1306 "%s mounting timed out. Killing.", UNIT(m)->id);
1307 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1309 log_warning_unit(UNIT(m)->id,
1310 "%s mounting timed out. Skipping SIGKILL. Ignoring.",
1313 if (m->from_proc_self_mountinfo)
1314 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1316 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1320 case MOUNT_REMOUNTING_SIGTERM:
1321 if (m->kill_context.send_sigkill) {
1322 log_warning_unit(UNIT(m)->id,
1323 "%s remounting timed out. Killing.", UNIT(m)->id);
1324 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1326 log_warning_unit(UNIT(m)->id,
1327 "%s remounting timed out. Skipping SIGKILL. Ignoring.",
1330 if (m->from_proc_self_mountinfo)
1331 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1333 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1337 case MOUNT_UNMOUNTING_SIGTERM:
1338 if (m->kill_context.send_sigkill) {
1339 log_warning_unit(UNIT(m)->id,
1340 "%s unmounting timed out. Killing.", UNIT(m)->id);
1341 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1343 log_warning_unit(UNIT(m)->id,
1344 "%s unmounting timed out. Skipping SIGKILL. Ignoring.",
1347 if (m->from_proc_self_mountinfo)
1348 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1350 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1354 case MOUNT_MOUNTING_SIGKILL:
1355 case MOUNT_REMOUNTING_SIGKILL:
1356 case MOUNT_UNMOUNTING_SIGKILL:
1357 log_warning_unit(UNIT(m)->id,
1358 "%s mount process still around after SIGKILL. Ignoring.",
1361 if (m->from_proc_self_mountinfo)
1362 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1364 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1368 assert_not_reached("Timeout at wrong time.");
1374 static int mount_add_one(
1378 const char *options,
1382 _cleanup_free_ char *e = NULL, *w = NULL, *o = NULL, *f = NULL;
1383 bool load_extras = false;
1395 /* Ignore API mount points. They should never be referenced in
1396 * dependencies ever. */
1397 if (mount_point_is_api(where) || mount_point_ignore(where))
1400 if (streq(fstype, "autofs"))
1403 /* probably some kind of swap, ignore */
1404 if (!is_path(where))
1407 e = unit_name_from_path(where, ".mount");
1411 u = manager_get_unit(m, e);
1415 u = unit_new(m, sizeof(Mount));
1419 r = unit_add_name(u, e);
1423 MOUNT(u)->where = strdup(where);
1424 if (!MOUNT(u)->where) {
1429 u->source_path = strdup("/proc/self/mountinfo");
1430 if (!u->source_path) {
1436 if (m->running_as == SYSTEMD_SYSTEM) {
1439 target = fstype_is_network(fstype) ? SPECIAL_REMOTE_FS_TARGET : SPECIAL_LOCAL_FS_TARGET;
1441 r = unit_add_dependency_by_name(u, UNIT_BEFORE, target, NULL, true);
1445 if (should_umount(MOUNT(u))) {
1446 r = unit_add_dependency_by_name(u, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
1452 unit_add_to_load_queue(u);
1456 if (!MOUNT(u)->where) {
1457 MOUNT(u)->where = strdup(where);
1458 if (!MOUNT(u)->where) {
1464 if (u->load_state == UNIT_NOT_FOUND) {
1465 u->load_state = UNIT_LOADED;
1468 /* Load in the extras later on, after we
1469 * finished initialization of the unit */
1474 if (!(w = strdup(what)) ||
1475 !(o = strdup(options)) ||
1476 !(f = strdup(fstype))) {
1481 p = &MOUNT(u)->parameters_proc_self_mountinfo;
1483 MOUNT(u)->is_mounted = true;
1484 MOUNT(u)->just_mounted = !MOUNT(u)->from_proc_self_mountinfo;
1485 MOUNT(u)->just_changed = !streq_ptr(p->options, o);
1488 MOUNT(u)->from_proc_self_mountinfo = true;
1503 r = mount_add_extras(MOUNT(u));
1508 unit_add_to_dbus_queue(u);
1519 static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
1525 rewind(m->proc_self_mountinfo);
1528 _cleanup_free_ char *device = NULL, *path = NULL, *options = NULL, *options2 = NULL, *fstype = NULL, *d = NULL, *p = NULL, *o = NULL;
1531 k = fscanf(m->proc_self_mountinfo,
1532 "%*s " /* (1) mount id */
1533 "%*s " /* (2) parent id */
1534 "%*s " /* (3) major:minor */
1535 "%*s " /* (4) root */
1536 "%ms " /* (5) mount point */
1537 "%ms" /* (6) mount options */
1538 "%*[^-]" /* (7) optional fields */
1539 "- " /* (8) separator */
1540 "%ms " /* (9) file system type */
1541 "%ms" /* (10) mount source */
1542 "%ms" /* (11) mount options 2 */
1543 "%*[^\n]", /* some rubbish at the end */
1554 log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
1558 o = strjoin(options, ",", options2, NULL);
1562 d = cunescape(device);
1563 p = cunescape(path);
1567 k = mount_add_one(m, d, p, o, fstype, set_flags);
1575 static void mount_shutdown(Manager *m) {
1578 m->mount_event_source = sd_event_source_unref(m->mount_event_source);
1580 if (m->proc_self_mountinfo) {
1581 fclose(m->proc_self_mountinfo);
1582 m->proc_self_mountinfo = NULL;
1586 static int mount_enumerate(Manager *m) {
1590 if (!m->proc_self_mountinfo) {
1591 m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
1592 if (!m->proc_self_mountinfo)
1595 r = sd_event_add_io(m->event, fileno(m->proc_self_mountinfo), EPOLLPRI, mount_dispatch_io, m, &m->mount_event_source);
1599 /* Dispatch this before we dispatch SIGCHLD, so that
1600 * we always get the events from /proc/self/mountinfo
1601 * before the SIGCHLD of /bin/mount. */
1602 r = sd_event_source_set_priority(m->mount_event_source, -10);
1607 r = mount_load_proc_self_mountinfo(m, false);
1618 static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
1619 Manager *m = userdata;
1624 assert(revents & EPOLLPRI);
1626 /* The manager calls this for every fd event happening on the
1627 * /proc/self/mountinfo file, which informs us about mounting
1630 r = mount_load_proc_self_mountinfo(m, true);
1632 log_error("Failed to reread /proc/self/mountinfo: %s", strerror(-r));
1634 /* Reset flags, just in case, for later calls */
1635 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1636 Mount *mount = MOUNT(u);
1638 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1644 manager_dispatch_load_queue(m);
1646 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1647 Mount *mount = MOUNT(u);
1649 if (!mount->is_mounted) {
1650 /* This has just been unmounted. */
1652 mount->from_proc_self_mountinfo = false;
1654 switch (mount->state) {
1657 mount_enter_dead(mount, MOUNT_SUCCESS);
1661 mount_set_state(mount, mount->state);
1666 } else if (mount->just_mounted || mount->just_changed) {
1668 /* New or changed mount entry */
1670 switch (mount->state) {
1674 mount_enter_mounted(mount, MOUNT_SUCCESS);
1677 case MOUNT_MOUNTING:
1678 mount_set_state(mount, MOUNT_MOUNTING_DONE);
1682 /* Nothing really changed, but let's
1683 * issue an notification call
1684 * nonetheless, in case somebody is
1685 * waiting for this. (e.g. file system
1686 * ro/rw remounts.) */
1687 mount_set_state(mount, mount->state);
1692 /* Reset the flags for later calls */
1693 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1699 static void mount_reset_failed(Unit *u) {
1700 Mount *m = MOUNT(u);
1704 if (m->state == MOUNT_FAILED)
1705 mount_set_state(m, MOUNT_DEAD);
1707 m->result = MOUNT_SUCCESS;
1708 m->reload_result = MOUNT_SUCCESS;
1711 static int mount_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
1712 return unit_kill_common(u, who, signo, -1, MOUNT(u)->control_pid, error);
1715 static const char* const mount_state_table[_MOUNT_STATE_MAX] = {
1716 [MOUNT_DEAD] = "dead",
1717 [MOUNT_MOUNTING] = "mounting",
1718 [MOUNT_MOUNTING_DONE] = "mounting-done",
1719 [MOUNT_MOUNTED] = "mounted",
1720 [MOUNT_REMOUNTING] = "remounting",
1721 [MOUNT_UNMOUNTING] = "unmounting",
1722 [MOUNT_MOUNTING_SIGTERM] = "mounting-sigterm",
1723 [MOUNT_MOUNTING_SIGKILL] = "mounting-sigkill",
1724 [MOUNT_REMOUNTING_SIGTERM] = "remounting-sigterm",
1725 [MOUNT_REMOUNTING_SIGKILL] = "remounting-sigkill",
1726 [MOUNT_UNMOUNTING_SIGTERM] = "unmounting-sigterm",
1727 [MOUNT_UNMOUNTING_SIGKILL] = "unmounting-sigkill",
1728 [MOUNT_FAILED] = "failed"
1731 DEFINE_STRING_TABLE_LOOKUP(mount_state, MountState);
1733 static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
1734 [MOUNT_EXEC_MOUNT] = "ExecMount",
1735 [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
1736 [MOUNT_EXEC_REMOUNT] = "ExecRemount",
1739 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
1741 static const char* const mount_result_table[_MOUNT_RESULT_MAX] = {
1742 [MOUNT_SUCCESS] = "success",
1743 [MOUNT_FAILURE_RESOURCES] = "resources",
1744 [MOUNT_FAILURE_TIMEOUT] = "timeout",
1745 [MOUNT_FAILURE_EXIT_CODE] = "exit-code",
1746 [MOUNT_FAILURE_SIGNAL] = "signal",
1747 [MOUNT_FAILURE_CORE_DUMP] = "core-dump"
1750 DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult);
1752 const UnitVTable mount_vtable = {
1753 .object_size = sizeof(Mount),
1754 .exec_context_offset = offsetof(Mount, exec_context),
1755 .cgroup_context_offset = offsetof(Mount, cgroup_context),
1756 .kill_context_offset = offsetof(Mount, kill_context),
1757 .exec_runtime_offset = offsetof(Mount, exec_runtime),
1763 .private_section = "Mount",
1766 .no_instances = true,
1772 .coldplug = mount_coldplug,
1776 .start = mount_start,
1778 .reload = mount_reload,
1782 .serialize = mount_serialize,
1783 .deserialize_item = mount_deserialize_item,
1785 .active_state = mount_active_state,
1786 .sub_state_to_string = mount_sub_state_to_string,
1788 .check_gc = mount_check_gc,
1790 .sigchld_event = mount_sigchld_event,
1792 .reset_failed = mount_reset_failed,
1794 .bus_interface = "org.freedesktop.systemd1.Mount",
1795 .bus_vtable = bus_mount_vtable,
1796 .bus_set_property = bus_mount_set_property,
1797 .bus_commit_properties = bus_mount_commit_properties,
1799 .enumerate = mount_enumerate,
1800 .shutdown = mount_shutdown,
1802 .status_message_formats = {
1803 .starting_stopping = {
1804 [0] = "Mounting %s...",
1805 [1] = "Unmounting %s...",
1807 .finished_start_job = {
1808 [JOB_DONE] = "Mounted %s.",
1809 [JOB_FAILED] = "Failed to mount %s.",
1810 [JOB_DEPENDENCY] = "Dependency failed for %s.",
1811 [JOB_TIMEOUT] = "Timed out mounting %s.",
1813 .finished_stop_job = {
1814 [JOB_DONE] = "Unmounted %s.",
1815 [JOB_FAILED] = "Failed unmounting %s.",
1816 [JOB_TIMEOUT] = "Timed out unmounting %s.",