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);
142 if (unit_has_name(u, "-.mount")) {
143 /* Don't allow start/stop for root directory */
144 UNIT(m)->refuse_manual_start = true;
145 UNIT(m)->refuse_manual_stop = true;
147 /* The stdio/kmsg bridge socket is on /, in order to avoid a
148 * dep loop, don't use kmsg logging for -.mount */
149 m->exec_context.std_output = u->manager->default_std_output;
150 m->exec_context.std_error = u->manager->default_std_error;
153 kill_context_init(&m->kill_context);
154 cgroup_context_init(&m->cgroup_context);
156 /* We need to make sure that /bin/mount is always called in
157 * the same process group as us, so that the autofs kernel
158 * side doesn't send us another mount request while we are
159 * already trying to comply its last one. */
160 m->exec_context.same_pgrp = true;
162 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
164 UNIT(m)->ignore_on_isolate = true;
167 static int mount_arm_timer(Mount *m) {
172 if (m->timeout_usec <= 0) {
173 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
177 if (m->timer_event_source) {
178 r = sd_event_source_set_time(m->timer_event_source, now(CLOCK_MONOTONIC) + m->timeout_usec);
182 return sd_event_source_set_enabled(m->timer_event_source, SD_EVENT_ONESHOT);
185 return sd_event_add_monotonic(UNIT(m)->manager->event, now(CLOCK_MONOTONIC) + m->timeout_usec, 0, mount_dispatch_timer, m, &m->timer_event_source);
188 static void mount_unwatch_control_pid(Mount *m) {
191 if (m->control_pid <= 0)
194 unit_unwatch_pid(UNIT(m), m->control_pid);
198 static void mount_parameters_done(MountParameters *p) {
205 p->what = p->options = p->fstype = NULL;
208 static void mount_done(Unit *u) {
216 mount_parameters_done(&m->parameters_proc_self_mountinfo);
217 mount_parameters_done(&m->parameters_fragment);
219 cgroup_context_done(&m->cgroup_context);
220 exec_context_done(&m->exec_context, manager_is_reloading_or_reexecuting(u->manager));
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,
506 "%s's Where setting doesn't match unit name. Refusing.",
511 if (mount_point_is_api(m->where) || mount_point_ignore(m->where)) {
512 log_error_unit(UNIT(m)->id,
513 "Cannot create mount unit for API file system %s. Refusing.",
518 if (UNIT(m)->fragment_path && !m->parameters_fragment.what) {
519 log_error_unit(UNIT(m)->id,
520 "%s's What setting is missing. Refusing.", UNIT(m)->id);
524 if (m->exec_context.pam_name && m->kill_context.kill_mode != KILL_CONTROL_GROUP) {
525 log_error_unit(UNIT(m)->id,
526 "%s has PAM enabled. Kill mode must be set to control-group'. Refusing.",
534 static int mount_add_extras(Mount *m) {
538 if (UNIT(m)->fragment_path)
539 m->from_fragment = true;
542 m->where = unit_name_to_path(u->id);
547 path_kill_slashes(m->where);
549 r = unit_add_exec_dependencies(u, &m->exec_context);
553 if (!UNIT(m)->description) {
554 r = unit_set_description(u, m->where);
559 r = mount_add_device_links(m);
563 r = mount_add_mount_links(m);
567 r = mount_add_quota_links(m);
571 if (UNIT(m)->default_dependencies) {
572 r = mount_add_default_dependencies(m);
577 r = unit_add_default_slice(u);
581 r = mount_fix_timeouts(m);
588 static int mount_load(Unit *u) {
593 assert(u->load_state == UNIT_STUB);
595 if (m->from_proc_self_mountinfo)
596 r = unit_load_fragment_and_dropin_optional(u);
598 r = unit_load_fragment_and_dropin(u);
603 /* This is a new unit? Then let's add in some extras */
604 if (u->load_state == UNIT_LOADED) {
605 r = mount_add_extras(m);
609 r = unit_exec_context_defaults(u, &m->exec_context);
614 return mount_verify(m);
617 static int mount_notify_automount(Mount *m, int status) {
624 SET_FOREACH(p, UNIT(m)->dependencies[UNIT_TRIGGERED_BY], i)
625 if (p->type == UNIT_AUTOMOUNT) {
626 r = automount_send_ready(AUTOMOUNT(p), status);
634 static void mount_set_state(Mount *m, MountState state) {
635 MountState old_state;
638 old_state = m->state;
641 if (state != MOUNT_MOUNTING &&
642 state != MOUNT_MOUNTING_DONE &&
643 state != MOUNT_REMOUNTING &&
644 state != MOUNT_UNMOUNTING &&
645 state != MOUNT_MOUNTING_SIGTERM &&
646 state != MOUNT_MOUNTING_SIGKILL &&
647 state != MOUNT_UNMOUNTING_SIGTERM &&
648 state != MOUNT_UNMOUNTING_SIGKILL &&
649 state != MOUNT_REMOUNTING_SIGTERM &&
650 state != MOUNT_REMOUNTING_SIGKILL) {
651 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
652 mount_unwatch_control_pid(m);
653 m->control_command = NULL;
654 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
657 if (state == MOUNT_MOUNTED ||
658 state == MOUNT_REMOUNTING)
659 mount_notify_automount(m, 0);
660 else if (state == MOUNT_DEAD ||
661 state == MOUNT_UNMOUNTING ||
662 state == MOUNT_MOUNTING_SIGTERM ||
663 state == MOUNT_MOUNTING_SIGKILL ||
664 state == MOUNT_REMOUNTING_SIGTERM ||
665 state == MOUNT_REMOUNTING_SIGKILL ||
666 state == MOUNT_UNMOUNTING_SIGTERM ||
667 state == MOUNT_UNMOUNTING_SIGKILL ||
668 state == MOUNT_FAILED) {
669 if (state != old_state)
670 mount_notify_automount(m, -ENODEV);
673 if (state != old_state)
674 log_debug_unit(UNIT(m)->id,
675 "%s changed %s -> %s",
677 mount_state_to_string(old_state),
678 mount_state_to_string(state));
680 unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state], m->reload_result == MOUNT_SUCCESS);
681 m->reload_result = MOUNT_SUCCESS;
684 static int mount_coldplug(Unit *u) {
686 MountState new_state = MOUNT_DEAD;
690 assert(m->state == MOUNT_DEAD);
692 if (m->deserialized_state != m->state)
693 new_state = m->deserialized_state;
694 else if (m->from_proc_self_mountinfo)
695 new_state = MOUNT_MOUNTED;
697 if (new_state != m->state) {
699 if (new_state == MOUNT_MOUNTING ||
700 new_state == MOUNT_MOUNTING_DONE ||
701 new_state == MOUNT_REMOUNTING ||
702 new_state == MOUNT_UNMOUNTING ||
703 new_state == MOUNT_MOUNTING_SIGTERM ||
704 new_state == MOUNT_MOUNTING_SIGKILL ||
705 new_state == MOUNT_UNMOUNTING_SIGTERM ||
706 new_state == MOUNT_UNMOUNTING_SIGKILL ||
707 new_state == MOUNT_REMOUNTING_SIGTERM ||
708 new_state == MOUNT_REMOUNTING_SIGKILL) {
710 if (m->control_pid <= 0)
713 r = unit_watch_pid(UNIT(m), m->control_pid);
717 r = mount_arm_timer(m);
722 mount_set_state(m, new_state);
728 static void mount_dump(Unit *u, FILE *f, const char *prefix) {
735 p = get_mount_parameters(m);
738 "%sMount State: %s\n"
742 "%sFile System Type: %s\n"
744 "%sFrom /proc/self/mountinfo: %s\n"
745 "%sFrom fragment: %s\n"
746 "%sDirectoryMode: %04o\n",
747 prefix, mount_state_to_string(m->state),
748 prefix, mount_result_to_string(m->result),
750 prefix, p ? strna(p->what) : "n/a",
751 prefix, p ? strna(p->fstype) : "n/a",
752 prefix, p ? strna(p->options) : "n/a",
753 prefix, yes_no(m->from_proc_self_mountinfo),
754 prefix, yes_no(m->from_fragment),
755 prefix, m->directory_mode);
757 if (m->control_pid > 0)
759 "%sControl PID: %lu\n",
760 prefix, (unsigned long) m->control_pid);
762 exec_context_dump(&m->exec_context, f, prefix);
763 kill_context_dump(&m->kill_context, f, prefix);
766 static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
774 unit_realize_cgroup(UNIT(m));
776 r = mount_arm_timer(m);
784 UNIT(m)->manager->environment,
788 UNIT(m)->manager->confirm_spawn,
789 UNIT(m)->manager->cgroup_supported,
790 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_context_tmp_dirs_done(&m->exec_context);
819 mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD);
822 static void mount_enter_mounted(Mount *m, MountResult f) {
825 if (f != MOUNT_SUCCESS)
828 mount_set_state(m, MOUNT_MOUNTED);
831 static void mount_enter_signal(Mount *m, MountState state, MountResult f) {
836 if (f != MOUNT_SUCCESS)
839 r = unit_kill_context(
842 state != MOUNT_MOUNTING_SIGTERM && state != MOUNT_UNMOUNTING_SIGTERM && state != MOUNT_REMOUNTING_SIGTERM,
850 r = mount_arm_timer(m);
854 mount_set_state(m, state);
855 } else if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
856 mount_enter_mounted(m, MOUNT_SUCCESS);
858 mount_enter_dead(m, MOUNT_SUCCESS);
863 log_warning_unit(UNIT(m)->id,
864 "%s failed to kill processes: %s", UNIT(m)->id, strerror(-r));
866 if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
867 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
869 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
872 void warn_if_dir_nonempty(const char *unit, const char* where) {
876 if (dir_is_empty(where) > 0)
879 log_struct_unit(LOG_NOTICE,
881 "MESSAGE=%s: Directory %s to mount over is not empty, mounting anyway.",
884 MESSAGE_ID(SD_MESSAGE_OVERMOUNTING),
888 static void mount_enter_unmounting(Mount *m) {
893 m->control_command_id = MOUNT_EXEC_UNMOUNT;
894 m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
896 if ((r = exec_command_set(
903 mount_unwatch_control_pid(m);
905 if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
908 mount_set_state(m, MOUNT_UNMOUNTING);
913 log_warning_unit(UNIT(m)->id,
914 "%s failed to run 'umount' task: %s",
915 UNIT(m)->id, strerror(-r));
916 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
919 static void mount_enter_mounting(Mount *m) {
925 m->control_command_id = MOUNT_EXEC_MOUNT;
926 m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
928 mkdir_p_label(m->where, m->directory_mode);
930 warn_if_dir_nonempty(m->meta.id, m->where);
932 /* Create the source directory for bind-mounts if needed */
933 p = get_mount_parameters_fragment(m);
934 if (p && mount_is_bind(p))
935 mkdir_p_label(p->what, m->directory_mode);
937 if (m->from_fragment)
938 r = exec_command_set(
941 m->parameters_fragment.what,
943 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
944 m->parameters_fragment.options ? "-o" : NULL, m->parameters_fragment.options,
952 mount_unwatch_control_pid(m);
954 r = mount_spawn(m, m->control_command, &m->control_pid);
958 mount_set_state(m, MOUNT_MOUNTING);
963 log_warning_unit(UNIT(m)->id,
964 "%s failed to run 'mount' task: %s",
965 UNIT(m)->id, strerror(-r));
966 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
969 static void mount_enter_mounting_done(Mount *m) {
972 mount_set_state(m, MOUNT_MOUNTING_DONE);
975 static void mount_enter_remounting(Mount *m) {
980 m->control_command_id = MOUNT_EXEC_REMOUNT;
981 m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
983 if (m->from_fragment) {
986 if (m->parameters_fragment.options)
987 o = strappenda("remount,", m->parameters_fragment.options);
991 r = exec_command_set(
994 m->parameters_fragment.what,
996 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
1005 mount_unwatch_control_pid(m);
1007 r = mount_spawn(m, m->control_command, &m->control_pid);
1011 mount_set_state(m, MOUNT_REMOUNTING);
1016 log_warning_unit(UNIT(m)->id,
1017 "%s failed to run 'remount' task: %s",
1018 UNIT(m)->id, strerror(-r));
1019 m->reload_result = MOUNT_FAILURE_RESOURCES;
1020 mount_enter_mounted(m, MOUNT_SUCCESS);
1023 static int mount_start(Unit *u) {
1024 Mount *m = MOUNT(u);
1028 /* We cannot fulfill this request right now, try again later
1030 if (m->state == MOUNT_UNMOUNTING ||
1031 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1032 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1033 m->state == MOUNT_MOUNTING_SIGTERM ||
1034 m->state == MOUNT_MOUNTING_SIGKILL)
1037 /* Already on it! */
1038 if (m->state == MOUNT_MOUNTING)
1041 assert(m->state == MOUNT_DEAD || m->state == MOUNT_FAILED);
1043 m->result = MOUNT_SUCCESS;
1044 m->reload_result = MOUNT_SUCCESS;
1046 mount_enter_mounting(m);
1050 static int mount_stop(Unit *u) {
1051 Mount *m = MOUNT(u);
1056 if (m->state == MOUNT_UNMOUNTING ||
1057 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1058 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1059 m->state == MOUNT_MOUNTING_SIGTERM ||
1060 m->state == MOUNT_MOUNTING_SIGKILL)
1063 assert(m->state == MOUNT_MOUNTING ||
1064 m->state == MOUNT_MOUNTING_DONE ||
1065 m->state == MOUNT_MOUNTED ||
1066 m->state == MOUNT_REMOUNTING ||
1067 m->state == MOUNT_REMOUNTING_SIGTERM ||
1068 m->state == MOUNT_REMOUNTING_SIGKILL);
1070 mount_enter_unmounting(m);
1074 static int mount_reload(Unit *u) {
1075 Mount *m = MOUNT(u);
1079 if (m->state == MOUNT_MOUNTING_DONE)
1082 assert(m->state == MOUNT_MOUNTED);
1084 mount_enter_remounting(m);
1088 static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
1089 Mount *m = MOUNT(u);
1095 unit_serialize_item(u, f, "state", mount_state_to_string(m->state));
1096 unit_serialize_item(u, f, "result", mount_result_to_string(m->result));
1097 unit_serialize_item(u, f, "reload-result", mount_result_to_string(m->reload_result));
1099 if (m->control_pid > 0)
1100 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) m->control_pid);
1102 if (m->control_command_id >= 0)
1103 unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(m->control_command_id));
1105 exec_context_serialize(&m->exec_context, UNIT(m), f);
1110 static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1111 Mount *m = MOUNT(u);
1118 if (streq(key, "state")) {
1121 if ((state = mount_state_from_string(value)) < 0)
1122 log_debug_unit(u->id, "Failed to parse state value %s", value);
1124 m->deserialized_state = state;
1125 } else if (streq(key, "result")) {
1128 f = mount_result_from_string(value);
1130 log_debug_unit(UNIT(m)->id,
1131 "Failed to parse result value %s", value);
1132 else if (f != MOUNT_SUCCESS)
1135 } else if (streq(key, "reload-result")) {
1138 f = mount_result_from_string(value);
1140 log_debug_unit(UNIT(m)->id,
1141 "Failed to parse reload result value %s", value);
1142 else if (f != MOUNT_SUCCESS)
1143 m->reload_result = f;
1145 } else if (streq(key, "control-pid")) {
1148 if (parse_pid(value, &pid) < 0)
1149 log_debug_unit(UNIT(m)->id,
1150 "Failed to parse control-pid value %s", value);
1152 m->control_pid = pid;
1153 } else if (streq(key, "control-command")) {
1154 MountExecCommand id;
1156 if ((id = mount_exec_command_from_string(value)) < 0)
1157 log_debug_unit(UNIT(m)->id,
1158 "Failed to parse exec-command value %s", value);
1160 m->control_command_id = id;
1161 m->control_command = m->exec_command + id;
1163 } else if (streq(key, "tmp-dir")) {
1170 m->exec_context.tmp_dir = t;
1171 } else if (streq(key, "var-tmp-dir")) {
1178 m->exec_context.var_tmp_dir = t;
1180 log_debug_unit(UNIT(m)->id,
1181 "Unknown serialization key '%s'", key);
1186 _pure_ static UnitActiveState mount_active_state(Unit *u) {
1189 return state_translation_table[MOUNT(u)->state];
1192 _pure_ static const char *mount_sub_state_to_string(Unit *u) {
1195 return mount_state_to_string(MOUNT(u)->state);
1198 _pure_ static bool mount_check_gc(Unit *u) {
1199 Mount *m = MOUNT(u);
1203 return m->from_proc_self_mountinfo;
1206 static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1207 Mount *m = MOUNT(u);
1213 if (pid != m->control_pid)
1218 if (is_clean_exit(code, status, NULL))
1220 else if (code == CLD_EXITED)
1221 f = MOUNT_FAILURE_EXIT_CODE;
1222 else if (code == CLD_KILLED)
1223 f = MOUNT_FAILURE_SIGNAL;
1224 else if (code == CLD_DUMPED)
1225 f = MOUNT_FAILURE_CORE_DUMP;
1227 assert_not_reached("Unknown code");
1229 if (f != MOUNT_SUCCESS)
1232 if (m->control_command) {
1233 exec_status_exit(&m->control_command->exec_status, &m->exec_context, pid, code, status);
1235 m->control_command = NULL;
1236 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
1239 log_full_unit(f == MOUNT_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
1240 "%s mount process exited, code=%s status=%i",
1241 u->id, sigchld_code_to_string(code), status);
1243 /* Note that mount(8) returning and the kernel sending us a
1244 * mount table change event might happen out-of-order. If an
1245 * operation succeed we assume the kernel will follow soon too
1246 * and already change into the resulting state. If it fails
1247 * we check if the kernel still knows about the mount. and
1248 * change state accordingly. */
1252 case MOUNT_MOUNTING:
1253 case MOUNT_MOUNTING_DONE:
1254 case MOUNT_MOUNTING_SIGKILL:
1255 case MOUNT_MOUNTING_SIGTERM:
1257 if (f == MOUNT_SUCCESS)
1258 mount_enter_mounted(m, f);
1259 else if (m->from_proc_self_mountinfo)
1260 mount_enter_mounted(m, f);
1262 mount_enter_dead(m, f);
1265 case MOUNT_REMOUNTING:
1266 case MOUNT_REMOUNTING_SIGKILL:
1267 case MOUNT_REMOUNTING_SIGTERM:
1269 m->reload_result = f;
1270 if (m->from_proc_self_mountinfo)
1271 mount_enter_mounted(m, MOUNT_SUCCESS);
1273 mount_enter_dead(m, MOUNT_SUCCESS);
1277 case MOUNT_UNMOUNTING:
1278 case MOUNT_UNMOUNTING_SIGKILL:
1279 case MOUNT_UNMOUNTING_SIGTERM:
1281 if (f == MOUNT_SUCCESS)
1282 mount_enter_dead(m, f);
1283 else if (m->from_proc_self_mountinfo)
1284 mount_enter_mounted(m, f);
1286 mount_enter_dead(m, f);
1290 assert_not_reached("Uh, control process died at wrong time.");
1293 /* Notify clients about changed exit status */
1294 unit_add_to_dbus_queue(u);
1297 static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
1298 Mount *m = MOUNT(userdata);
1301 assert(m->timer_event_source == source);
1305 case MOUNT_MOUNTING:
1306 case MOUNT_MOUNTING_DONE:
1307 log_warning_unit(UNIT(m)->id,
1308 "%s mounting timed out. Stopping.", UNIT(m)->id);
1309 mount_enter_signal(m, MOUNT_MOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1312 case MOUNT_REMOUNTING:
1313 log_warning_unit(UNIT(m)->id,
1314 "%s remounting timed out. Stopping.", UNIT(m)->id);
1315 m->reload_result = MOUNT_FAILURE_TIMEOUT;
1316 mount_enter_mounted(m, MOUNT_SUCCESS);
1319 case MOUNT_UNMOUNTING:
1320 log_warning_unit(UNIT(m)->id,
1321 "%s unmounting timed out. Stopping.", UNIT(m)->id);
1322 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1325 case MOUNT_MOUNTING_SIGTERM:
1326 if (m->kill_context.send_sigkill) {
1327 log_warning_unit(UNIT(m)->id,
1328 "%s mounting timed out. Killing.", UNIT(m)->id);
1329 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1331 log_warning_unit(UNIT(m)->id,
1332 "%s mounting timed out. Skipping SIGKILL. Ignoring.",
1335 if (m->from_proc_self_mountinfo)
1336 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1338 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1342 case MOUNT_REMOUNTING_SIGTERM:
1343 if (m->kill_context.send_sigkill) {
1344 log_warning_unit(UNIT(m)->id,
1345 "%s remounting timed out. Killing.", UNIT(m)->id);
1346 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1348 log_warning_unit(UNIT(m)->id,
1349 "%s remounting timed out. Skipping SIGKILL. Ignoring.",
1352 if (m->from_proc_self_mountinfo)
1353 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1355 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1359 case MOUNT_UNMOUNTING_SIGTERM:
1360 if (m->kill_context.send_sigkill) {
1361 log_warning_unit(UNIT(m)->id,
1362 "%s unmounting timed out. Killing.", UNIT(m)->id);
1363 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1365 log_warning_unit(UNIT(m)->id,
1366 "%s unmounting timed out. Skipping SIGKILL. Ignoring.",
1369 if (m->from_proc_self_mountinfo)
1370 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1372 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1376 case MOUNT_MOUNTING_SIGKILL:
1377 case MOUNT_REMOUNTING_SIGKILL:
1378 case MOUNT_UNMOUNTING_SIGKILL:
1379 log_warning_unit(UNIT(m)->id,
1380 "%s mount process still around after SIGKILL. Ignoring.",
1383 if (m->from_proc_self_mountinfo)
1384 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1386 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1390 assert_not_reached("Timeout at wrong time.");
1396 static int mount_add_one(
1400 const char *options,
1406 char *e, *w = NULL, *o = NULL, *f = NULL;
1408 bool load_extras = false;
1416 /* Ignore API mount points. They should never be referenced in
1417 * dependencies ever. */
1418 if (mount_point_is_api(where) || mount_point_ignore(where))
1421 if (streq(fstype, "autofs"))
1424 /* probably some kind of swap, ignore */
1425 if (!is_path(where))
1428 e = unit_name_from_path(where, ".mount");
1432 u = manager_get_unit(m, e);
1434 const char* const target =
1435 fstype_is_network(fstype) ? SPECIAL_REMOTE_FS_TARGET : SPECIAL_LOCAL_FS_TARGET;
1439 u = unit_new(m, sizeof(Mount));
1445 r = unit_add_name(u, e);
1451 MOUNT(u)->where = strdup(where);
1452 if (!MOUNT(u)->where) {
1457 u->source_path = strdup("/proc/self/mountinfo");
1458 if (!u->source_path) {
1463 r = unit_add_dependency_by_name(u, UNIT_BEFORE, target, NULL, true);
1467 if (should_umount(MOUNT(u))) {
1468 r = unit_add_dependency_by_name(u, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
1473 unit_add_to_load_queue(u);
1478 if (!MOUNT(u)->where) {
1479 MOUNT(u)->where = strdup(where);
1480 if (!MOUNT(u)->where) {
1486 if (u->load_state == UNIT_NOT_FOUND) {
1487 u->load_state = UNIT_LOADED;
1490 /* Load in the extras later on, after we
1491 * finished initialization of the unit */
1496 if (!(w = strdup(what)) ||
1497 !(o = strdup(options)) ||
1498 !(f = strdup(fstype))) {
1503 p = &MOUNT(u)->parameters_proc_self_mountinfo;
1505 MOUNT(u)->is_mounted = true;
1506 MOUNT(u)->just_mounted = !MOUNT(u)->from_proc_self_mountinfo;
1507 MOUNT(u)->just_changed = !streq_ptr(p->options, o);
1510 MOUNT(u)->from_proc_self_mountinfo = true;
1522 r = mount_add_extras(MOUNT(u));
1527 unit_add_to_dbus_queue(u);
1542 static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
1548 rewind(m->proc_self_mountinfo);
1551 _cleanup_free_ char *device = NULL, *path = NULL, *options = NULL, *options2 = NULL, *fstype = NULL, *d = NULL, *p = NULL, *o = NULL;
1554 k = fscanf(m->proc_self_mountinfo,
1555 "%*s " /* (1) mount id */
1556 "%*s " /* (2) parent id */
1557 "%*s " /* (3) major:minor */
1558 "%*s " /* (4) root */
1559 "%ms " /* (5) mount point */
1560 "%ms" /* (6) mount options */
1561 "%*[^-]" /* (7) optional fields */
1562 "- " /* (8) separator */
1563 "%ms " /* (9) file system type */
1564 "%ms" /* (10) mount source */
1565 "%ms" /* (11) mount options 2 */
1566 "%*[^\n]", /* some rubbish at the end */
1577 log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
1581 o = strjoin(options, ",", options2, NULL);
1585 d = cunescape(device);
1586 p = cunescape(path);
1590 k = mount_add_one(m, d, p, o, fstype, set_flags);
1598 static void mount_shutdown(Manager *m) {
1601 m->mount_event_source = sd_event_source_unref(m->mount_event_source);
1603 if (m->proc_self_mountinfo) {
1604 fclose(m->proc_self_mountinfo);
1605 m->proc_self_mountinfo = NULL;
1609 static int mount_enumerate(Manager *m) {
1613 if (!m->proc_self_mountinfo) {
1614 m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
1615 if (!m->proc_self_mountinfo)
1618 r = sd_event_add_io(m->event, fileno(m->proc_self_mountinfo), EPOLLPRI, mount_dispatch_io, m, &m->mount_event_source);
1623 r = mount_load_proc_self_mountinfo(m, false);
1634 static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
1635 Manager *m = userdata;
1640 assert(revents & EPOLLPRI);
1642 /* The manager calls this for every fd event happening on the
1643 * /proc/self/mountinfo file, which informs us about mounting
1646 r = mount_load_proc_self_mountinfo(m, true);
1648 log_error("Failed to reread /proc/self/mountinfo: %s", strerror(-r));
1650 /* Reset flags, just in case, for later calls */
1651 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1652 Mount *mount = MOUNT(u);
1654 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1660 manager_dispatch_load_queue(m);
1662 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1663 Mount *mount = MOUNT(u);
1665 if (!mount->is_mounted) {
1666 /* This has just been unmounted. */
1668 mount->from_proc_self_mountinfo = false;
1670 switch (mount->state) {
1673 mount_enter_dead(mount, MOUNT_SUCCESS);
1677 mount_set_state(mount, mount->state);
1682 } else if (mount->just_mounted || mount->just_changed) {
1684 /* New or changed mount entry */
1686 switch (mount->state) {
1690 mount_enter_mounted(mount, MOUNT_SUCCESS);
1693 case MOUNT_MOUNTING:
1694 mount_enter_mounting_done(mount);
1698 /* Nothing really changed, but let's
1699 * issue an notification call
1700 * nonetheless, in case somebody is
1701 * waiting for this. (e.g. file system
1702 * ro/rw remounts.) */
1703 mount_set_state(mount, mount->state);
1708 /* Reset the flags for later calls */
1709 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1715 static void mount_reset_failed(Unit *u) {
1716 Mount *m = MOUNT(u);
1720 if (m->state == MOUNT_FAILED)
1721 mount_set_state(m, MOUNT_DEAD);
1723 m->result = MOUNT_SUCCESS;
1724 m->reload_result = MOUNT_SUCCESS;
1727 static int mount_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
1728 return unit_kill_common(u, who, signo, -1, MOUNT(u)->control_pid, error);
1731 static const char* const mount_state_table[_MOUNT_STATE_MAX] = {
1732 [MOUNT_DEAD] = "dead",
1733 [MOUNT_MOUNTING] = "mounting",
1734 [MOUNT_MOUNTING_DONE] = "mounting-done",
1735 [MOUNT_MOUNTED] = "mounted",
1736 [MOUNT_REMOUNTING] = "remounting",
1737 [MOUNT_UNMOUNTING] = "unmounting",
1738 [MOUNT_MOUNTING_SIGTERM] = "mounting-sigterm",
1739 [MOUNT_MOUNTING_SIGKILL] = "mounting-sigkill",
1740 [MOUNT_REMOUNTING_SIGTERM] = "remounting-sigterm",
1741 [MOUNT_REMOUNTING_SIGKILL] = "remounting-sigkill",
1742 [MOUNT_UNMOUNTING_SIGTERM] = "unmounting-sigterm",
1743 [MOUNT_UNMOUNTING_SIGKILL] = "unmounting-sigkill",
1744 [MOUNT_FAILED] = "failed"
1747 DEFINE_STRING_TABLE_LOOKUP(mount_state, MountState);
1749 static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
1750 [MOUNT_EXEC_MOUNT] = "ExecMount",
1751 [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
1752 [MOUNT_EXEC_REMOUNT] = "ExecRemount",
1755 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
1757 static const char* const mount_result_table[_MOUNT_RESULT_MAX] = {
1758 [MOUNT_SUCCESS] = "success",
1759 [MOUNT_FAILURE_RESOURCES] = "resources",
1760 [MOUNT_FAILURE_TIMEOUT] = "timeout",
1761 [MOUNT_FAILURE_EXIT_CODE] = "exit-code",
1762 [MOUNT_FAILURE_SIGNAL] = "signal",
1763 [MOUNT_FAILURE_CORE_DUMP] = "core-dump"
1766 DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult);
1768 const UnitVTable mount_vtable = {
1769 .object_size = sizeof(Mount),
1770 .exec_context_offset = offsetof(Mount, exec_context),
1771 .cgroup_context_offset = offsetof(Mount, cgroup_context),
1772 .kill_context_offset = offsetof(Mount, kill_context),
1778 .private_section = "Mount",
1781 .no_instances = true,
1787 .coldplug = mount_coldplug,
1791 .start = mount_start,
1793 .reload = mount_reload,
1797 .serialize = mount_serialize,
1798 .deserialize_item = mount_deserialize_item,
1800 .active_state = mount_active_state,
1801 .sub_state_to_string = mount_sub_state_to_string,
1803 .check_gc = mount_check_gc,
1805 .sigchld_event = mount_sigchld_event,
1807 .reset_failed = mount_reset_failed,
1809 .bus_interface = "org.freedesktop.systemd1.Mount",
1810 .bus_vtable = bus_mount_vtable,
1811 .bus_changing_properties = bus_mount_changing_properties,
1812 .bus_set_property = bus_mount_set_property,
1813 .bus_commit_properties = bus_mount_commit_properties,
1815 .enumerate = mount_enumerate,
1816 .shutdown = mount_shutdown,
1818 .status_message_formats = {
1819 .starting_stopping = {
1820 [0] = "Mounting %s...",
1821 [1] = "Unmounting %s...",
1823 .finished_start_job = {
1824 [JOB_DONE] = "Mounted %s.",
1825 [JOB_FAILED] = "Failed to mount %s.",
1826 [JOB_DEPENDENCY] = "Dependency failed for %s.",
1827 [JOB_TIMEOUT] = "Timed out mounting %s.",
1829 .finished_stop_job = {
1830 [JOB_DONE] = "Unmounted %s.",
1831 [JOB_FAILED] = "Failed unmounting %s.",
1832 [JOB_TIMEOUT] = "Timed out unmounting %s.",