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 void mount_init(Unit *u) {
66 assert(u->load_state == UNIT_STUB);
68 m->timeout_usec = DEFAULT_TIMEOUT_USEC;
69 m->directory_mode = 0755;
71 exec_context_init(&m->exec_context);
73 if (unit_has_name(u, "-.mount")) {
74 /* Don't allow start/stop for root directory */
75 UNIT(m)->refuse_manual_start = true;
76 UNIT(m)->refuse_manual_stop = true;
78 /* The stdio/kmsg bridge socket is on /, in order to avoid a
79 * dep loop, don't use kmsg logging for -.mount */
80 m->exec_context.std_output = u->manager->default_std_output;
81 m->exec_context.std_error = u->manager->default_std_error;
84 kill_context_init(&m->kill_context);
85 cgroup_context_init(&m->cgroup_context);
87 /* We need to make sure that /bin/mount is always called in
88 * the same process group as us, so that the autofs kernel
89 * side doesn't send us another mount request while we are
90 * already trying to comply its last one. */
91 m->exec_context.same_pgrp = true;
93 m->timer_watch.type = WATCH_INVALID;
95 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
97 UNIT(m)->ignore_on_isolate = true;
100 static void mount_unwatch_control_pid(Mount *m) {
103 if (m->control_pid <= 0)
106 unit_unwatch_pid(UNIT(m), m->control_pid);
110 static void mount_parameters_done(MountParameters *p) {
117 p->what = p->options = p->fstype = NULL;
120 static void mount_done(Unit *u) {
128 mount_parameters_done(&m->parameters_proc_self_mountinfo);
129 mount_parameters_done(&m->parameters_fragment);
131 cgroup_context_done(&m->cgroup_context);
132 exec_context_done(&m->exec_context, manager_is_reloading_or_reexecuting(u->manager));
133 exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
134 m->control_command = NULL;
136 mount_unwatch_control_pid(m);
138 unit_unwatch_timer(u, &m->timer_watch);
141 _pure_ static MountParameters* get_mount_parameters_fragment(Mount *m) {
144 if (m->from_fragment)
145 return &m->parameters_fragment;
150 _pure_ static MountParameters* get_mount_parameters(Mount *m) {
153 if (m->from_proc_self_mountinfo)
154 return &m->parameters_proc_self_mountinfo;
156 return get_mount_parameters_fragment(m);
159 static int mount_add_mount_links(Mount *m) {
160 _cleanup_free_ char *parent = NULL;
169 if (!path_equal(m->where, "/")) {
170 /* Adds in links to other mount points that might lie further
171 * up in the hierarchy */
172 r = path_get_parent(m->where, &parent);
176 r = unit_require_mounts_for(UNIT(m), parent);
181 /* Adds in links to other mount points that might be needed
182 * for the source path (if this is a bind mount) to be
184 pm = get_mount_parameters_fragment(m);
185 if (pm && path_is_absolute(pm->what)) {
186 r = unit_require_mounts_for(UNIT(m), pm->what);
191 /* Adds in links to other units that use this path or paths
192 * further down in the hierarchy */
193 s = manager_get_units_requiring_mounts_for(UNIT(m)->manager, m->where);
194 SET_FOREACH(other, s, i) {
196 if (other->load_state != UNIT_LOADED)
199 if (other == UNIT(m))
202 r = unit_add_dependency(other, UNIT_AFTER, UNIT(m), true);
206 if (UNIT(m)->fragment_path) {
207 /* If we have fragment configuration, then make this dependency required */
208 r = unit_add_dependency(other, UNIT_REQUIRES, UNIT(m), true);
217 static char* mount_test_option(const char *haystack, const char *needle) {
218 struct mntent me = { .mnt_opts = (char*) haystack };
222 /* Like glibc's hasmntopt(), but works on a string, not a
228 return hasmntopt(&me, needle);
231 static bool mount_is_network(MountParameters *p) {
234 if (mount_test_option(p->options, "_netdev"))
237 if (p->fstype && fstype_is_network(p->fstype))
243 static bool mount_is_bind(MountParameters *p) {
246 if (mount_test_option(p->options, "bind"))
249 if (p->fstype && streq(p->fstype, "bind"))
252 if (mount_test_option(p->options, "rbind"))
255 if (p->fstype && streq(p->fstype, "rbind"))
261 static bool mount_is_auto(MountParameters *p) {
264 return !mount_test_option(p->options, "noauto");
267 static bool needs_quota(MountParameters *p) {
270 if (mount_is_network(p))
273 if (mount_is_bind(p))
276 return mount_test_option(p->options, "usrquota") ||
277 mount_test_option(p->options, "grpquota") ||
278 mount_test_option(p->options, "quota") ||
279 mount_test_option(p->options, "usrjquota") ||
280 mount_test_option(p->options, "grpjquota");
283 static int mount_add_device_links(Mount *m) {
285 bool device_wants_mount = false;
290 p = get_mount_parameters_fragment(m);
297 if (mount_is_bind(p))
300 if (!is_device_path(p->what))
303 if (path_equal(m->where, "/"))
306 if (mount_is_auto(p) && UNIT(m)->manager->running_as == SYSTEMD_SYSTEM)
307 device_wants_mount = true;
309 r = unit_add_node_link(UNIT(m), p->what, device_wants_mount);
314 UNIT(m)->manager->running_as == SYSTEMD_SYSTEM) {
317 /* Let's add in the fsck service */
319 /* aka SPECIAL_FSCK_SERVICE */
320 name = unit_name_from_path_instance("systemd-fsck", p->what, ".service");
324 r = manager_load_unit_prepare(UNIT(m)->manager, name, NULL, NULL, &fsck);
326 log_warning_unit(name,
327 "Failed to prepare unit %s: %s", name, strerror(-r));
333 SERVICE(fsck)->fsck_passno = p->passno;
335 r = unit_add_two_dependencies(UNIT(m), UNIT_AFTER, UNIT_REQUIRES, fsck, true);
343 static int mount_add_quota_links(Mount *m) {
349 if (UNIT(m)->manager->running_as != SYSTEMD_SYSTEM)
352 p = get_mount_parameters_fragment(m);
359 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTACHECK_SERVICE, NULL, true);
363 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTAON_SERVICE, NULL, true);
370 static bool should_umount(Mount *m) {
373 if (path_equal(m->where, "/") ||
374 path_equal(m->where, "/usr"))
377 p = get_mount_parameters(m);
378 if (p && mount_test_option(p->options, "x-initrd.mount") &&
385 static int mount_add_default_dependencies(Mount *m) {
386 const char *after, *after2, *online;
392 if (UNIT(m)->manager->running_as != SYSTEMD_SYSTEM)
395 p = get_mount_parameters(m);
400 if (path_equal(m->where, "/"))
403 if (mount_is_network(p)) {
404 after = SPECIAL_REMOTE_FS_PRE_TARGET;
405 after2 = SPECIAL_NETWORK_TARGET;
406 online = SPECIAL_NETWORK_ONLINE_TARGET;
408 after = SPECIAL_LOCAL_FS_PRE_TARGET;
413 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after, NULL, true);
418 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after2, NULL, true);
424 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_WANTS, UNIT_AFTER, online, NULL, true);
429 if (should_umount(m)) {
430 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
438 static int mount_fix_timeouts(Mount *m) {
440 const char *timeout = NULL;
449 p = get_mount_parameters_fragment(m);
453 /* Allow configuration how long we wait for a device that
454 * backs a mount point to show up. This is useful to support
455 * endless device timeouts for devices that show up only after
456 * user input, like crypto devices. */
458 if ((timeout = mount_test_option(p->options, "comment=systemd.device-timeout")))
460 else if ((timeout = mount_test_option(p->options, "x-systemd.device-timeout")))
465 t = strndup(timeout, strcspn(timeout, ",;" WHITESPACE));
469 r = parse_sec(t, &u);
473 log_warning_unit(UNIT(m)->id,
474 "Failed to parse timeout for %s, ignoring: %s",
479 SET_FOREACH(other, UNIT(m)->dependencies[UNIT_AFTER], i) {
480 if (other->type != UNIT_DEVICE)
483 other->job_timeout = u;
489 static int mount_verify(Mount *m) {
490 _cleanup_free_ char *e = NULL;
495 if (UNIT(m)->load_state != UNIT_LOADED)
498 if (!m->from_fragment && !m->from_proc_self_mountinfo)
501 e = unit_name_from_path(m->where, ".mount");
505 b = unit_has_name(UNIT(m), e);
507 log_error_unit(UNIT(m)->id,
508 "%s's Where setting doesn't match unit name. Refusing.",
513 if (mount_point_is_api(m->where) || mount_point_ignore(m->where)) {
514 log_error_unit(UNIT(m)->id,
515 "Cannot create mount unit for API file system %s. Refusing.",
520 if (UNIT(m)->fragment_path && !m->parameters_fragment.what) {
521 log_error_unit(UNIT(m)->id,
522 "%s's What setting is missing. Refusing.", UNIT(m)->id);
526 if (m->exec_context.pam_name && m->kill_context.kill_mode != KILL_CONTROL_GROUP) {
527 log_error_unit(UNIT(m)->id,
528 "%s has PAM enabled. Kill mode must be set to control-group'. Refusing.",
536 static int mount_add_extras(Mount *m) {
540 if (UNIT(m)->fragment_path)
541 m->from_fragment = true;
544 m->where = unit_name_to_path(u->id);
549 path_kill_slashes(m->where);
551 r = unit_add_exec_dependencies(u, &m->exec_context);
555 if (!UNIT(m)->description) {
556 r = unit_set_description(u, m->where);
561 r = mount_add_device_links(m);
565 r = mount_add_mount_links(m);
569 r = mount_add_quota_links(m);
573 if (UNIT(m)->default_dependencies) {
574 r = mount_add_default_dependencies(m);
579 r = unit_add_default_slice(u);
583 r = mount_fix_timeouts(m);
590 static int mount_load(Unit *u) {
595 assert(u->load_state == UNIT_STUB);
597 if (m->from_proc_self_mountinfo)
598 r = unit_load_fragment_and_dropin_optional(u);
600 r = unit_load_fragment_and_dropin(u);
605 /* This is a new unit? Then let's add in some extras */
606 if (u->load_state == UNIT_LOADED) {
607 r = mount_add_extras(m);
611 r = unit_exec_context_defaults(u, &m->exec_context);
616 return mount_verify(m);
619 static int mount_notify_automount(Mount *m, int status) {
626 SET_FOREACH(p, UNIT(m)->dependencies[UNIT_TRIGGERED_BY], i)
627 if (p->type == UNIT_AUTOMOUNT) {
628 r = automount_send_ready(AUTOMOUNT(p), status);
636 static void mount_set_state(Mount *m, MountState state) {
637 MountState old_state;
640 old_state = m->state;
643 if (state != MOUNT_MOUNTING &&
644 state != MOUNT_MOUNTING_DONE &&
645 state != MOUNT_REMOUNTING &&
646 state != MOUNT_UNMOUNTING &&
647 state != MOUNT_MOUNTING_SIGTERM &&
648 state != MOUNT_MOUNTING_SIGKILL &&
649 state != MOUNT_UNMOUNTING_SIGTERM &&
650 state != MOUNT_UNMOUNTING_SIGKILL &&
651 state != MOUNT_REMOUNTING_SIGTERM &&
652 state != MOUNT_REMOUNTING_SIGKILL) {
653 unit_unwatch_timer(UNIT(m), &m->timer_watch);
654 mount_unwatch_control_pid(m);
655 m->control_command = NULL;
656 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
659 if (state == MOUNT_MOUNTED ||
660 state == MOUNT_REMOUNTING)
661 mount_notify_automount(m, 0);
662 else if (state == MOUNT_DEAD ||
663 state == MOUNT_UNMOUNTING ||
664 state == MOUNT_MOUNTING_SIGTERM ||
665 state == MOUNT_MOUNTING_SIGKILL ||
666 state == MOUNT_REMOUNTING_SIGTERM ||
667 state == MOUNT_REMOUNTING_SIGKILL ||
668 state == MOUNT_UNMOUNTING_SIGTERM ||
669 state == MOUNT_UNMOUNTING_SIGKILL ||
670 state == MOUNT_FAILED) {
671 if (state != old_state)
672 mount_notify_automount(m, -ENODEV);
675 if (state != old_state)
676 log_debug_unit(UNIT(m)->id,
677 "%s changed %s -> %s",
679 mount_state_to_string(old_state),
680 mount_state_to_string(state));
682 unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state], m->reload_result == MOUNT_SUCCESS);
683 m->reload_result = MOUNT_SUCCESS;
686 static int mount_coldplug(Unit *u) {
688 MountState new_state = MOUNT_DEAD;
692 assert(m->state == MOUNT_DEAD);
694 if (m->deserialized_state != m->state)
695 new_state = m->deserialized_state;
696 else if (m->from_proc_self_mountinfo)
697 new_state = MOUNT_MOUNTED;
699 if (new_state != m->state) {
701 if (new_state == MOUNT_MOUNTING ||
702 new_state == MOUNT_MOUNTING_DONE ||
703 new_state == MOUNT_REMOUNTING ||
704 new_state == MOUNT_UNMOUNTING ||
705 new_state == MOUNT_MOUNTING_SIGTERM ||
706 new_state == MOUNT_MOUNTING_SIGKILL ||
707 new_state == MOUNT_UNMOUNTING_SIGTERM ||
708 new_state == MOUNT_UNMOUNTING_SIGKILL ||
709 new_state == MOUNT_REMOUNTING_SIGTERM ||
710 new_state == MOUNT_REMOUNTING_SIGKILL) {
712 if (m->control_pid <= 0)
715 r = unit_watch_pid(UNIT(m), m->control_pid);
719 r = unit_watch_timer(UNIT(m), CLOCK_MONOTONIC, true, m->timeout_usec, &m->timer_watch);
724 mount_set_state(m, new_state);
730 static void mount_dump(Unit *u, FILE *f, const char *prefix) {
737 p = get_mount_parameters(m);
740 "%sMount State: %s\n"
744 "%sFile System Type: %s\n"
746 "%sFrom /proc/self/mountinfo: %s\n"
747 "%sFrom fragment: %s\n"
748 "%sDirectoryMode: %04o\n",
749 prefix, mount_state_to_string(m->state),
750 prefix, mount_result_to_string(m->result),
752 prefix, p ? strna(p->what) : "n/a",
753 prefix, p ? strna(p->fstype) : "n/a",
754 prefix, p ? strna(p->options) : "n/a",
755 prefix, yes_no(m->from_proc_self_mountinfo),
756 prefix, yes_no(m->from_fragment),
757 prefix, m->directory_mode);
759 if (m->control_pid > 0)
761 "%sControl PID: %lu\n",
762 prefix, (unsigned long) m->control_pid);
764 exec_context_dump(&m->exec_context, f, prefix);
765 kill_context_dump(&m->kill_context, f, prefix);
768 static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
776 unit_realize_cgroup(UNIT(m));
778 r = unit_watch_timer(UNIT(m), CLOCK_MONOTONIC, true, m->timeout_usec, &m->timer_watch);
786 UNIT(m)->manager->environment,
790 UNIT(m)->manager->confirm_spawn,
791 UNIT(m)->manager->cgroup_supported,
792 UNIT(m)->cgroup_path,
799 r = unit_watch_pid(UNIT(m), pid);
801 /* FIXME: we need to do something here */
809 unit_unwatch_timer(UNIT(m), &m->timer_watch);
814 static void mount_enter_dead(Mount *m, MountResult f) {
817 if (f != MOUNT_SUCCESS)
820 exec_context_tmp_dirs_done(&m->exec_context);
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 = unit_watch_timer(UNIT(m), CLOCK_MONOTONIC, true, m->timeout_usec, &m->timer_watch);
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_mounting_done(Mount *m) {
974 mount_set_state(m, MOUNT_MOUNTING_DONE);
977 static void mount_enter_remounting(Mount *m) {
982 m->control_command_id = MOUNT_EXEC_REMOUNT;
983 m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
985 if (m->from_fragment) {
989 if (m->parameters_fragment.options) {
990 if (!(buf = strappend("remount,", m->parameters_fragment.options))) {
999 r = exec_command_set(
1002 m->parameters_fragment.what,
1004 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
1015 mount_unwatch_control_pid(m);
1017 if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
1020 mount_set_state(m, MOUNT_REMOUNTING);
1025 log_warning_unit(UNIT(m)->id,
1026 "%s failed to run 'remount' task: %s",
1027 UNIT(m)->id, strerror(-r));
1028 m->reload_result = MOUNT_FAILURE_RESOURCES;
1029 mount_enter_mounted(m, MOUNT_SUCCESS);
1032 static int mount_start(Unit *u) {
1033 Mount *m = MOUNT(u);
1037 /* We cannot fulfill this request right now, try again later
1039 if (m->state == MOUNT_UNMOUNTING ||
1040 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1041 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1042 m->state == MOUNT_MOUNTING_SIGTERM ||
1043 m->state == MOUNT_MOUNTING_SIGKILL)
1046 /* Already on it! */
1047 if (m->state == MOUNT_MOUNTING)
1050 assert(m->state == MOUNT_DEAD || m->state == MOUNT_FAILED);
1052 m->result = MOUNT_SUCCESS;
1053 m->reload_result = MOUNT_SUCCESS;
1055 mount_enter_mounting(m);
1059 static int mount_stop(Unit *u) {
1060 Mount *m = MOUNT(u);
1065 if (m->state == MOUNT_UNMOUNTING ||
1066 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1067 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1068 m->state == MOUNT_MOUNTING_SIGTERM ||
1069 m->state == MOUNT_MOUNTING_SIGKILL)
1072 assert(m->state == MOUNT_MOUNTING ||
1073 m->state == MOUNT_MOUNTING_DONE ||
1074 m->state == MOUNT_MOUNTED ||
1075 m->state == MOUNT_REMOUNTING ||
1076 m->state == MOUNT_REMOUNTING_SIGTERM ||
1077 m->state == MOUNT_REMOUNTING_SIGKILL);
1079 mount_enter_unmounting(m);
1083 static int mount_reload(Unit *u) {
1084 Mount *m = MOUNT(u);
1088 if (m->state == MOUNT_MOUNTING_DONE)
1091 assert(m->state == MOUNT_MOUNTED);
1093 mount_enter_remounting(m);
1097 static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
1098 Mount *m = MOUNT(u);
1104 unit_serialize_item(u, f, "state", mount_state_to_string(m->state));
1105 unit_serialize_item(u, f, "result", mount_result_to_string(m->result));
1106 unit_serialize_item(u, f, "reload-result", mount_result_to_string(m->reload_result));
1108 if (m->control_pid > 0)
1109 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) m->control_pid);
1111 if (m->control_command_id >= 0)
1112 unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(m->control_command_id));
1114 exec_context_serialize(&m->exec_context, UNIT(m), f);
1119 static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1120 Mount *m = MOUNT(u);
1127 if (streq(key, "state")) {
1130 if ((state = mount_state_from_string(value)) < 0)
1131 log_debug_unit(u->id, "Failed to parse state value %s", value);
1133 m->deserialized_state = state;
1134 } else if (streq(key, "result")) {
1137 f = mount_result_from_string(value);
1139 log_debug_unit(UNIT(m)->id,
1140 "Failed to parse result value %s", value);
1141 else if (f != MOUNT_SUCCESS)
1144 } else if (streq(key, "reload-result")) {
1147 f = mount_result_from_string(value);
1149 log_debug_unit(UNIT(m)->id,
1150 "Failed to parse reload result value %s", value);
1151 else if (f != MOUNT_SUCCESS)
1152 m->reload_result = f;
1154 } else if (streq(key, "control-pid")) {
1157 if (parse_pid(value, &pid) < 0)
1158 log_debug_unit(UNIT(m)->id,
1159 "Failed to parse control-pid value %s", value);
1161 m->control_pid = pid;
1162 } else if (streq(key, "control-command")) {
1163 MountExecCommand id;
1165 if ((id = mount_exec_command_from_string(value)) < 0)
1166 log_debug_unit(UNIT(m)->id,
1167 "Failed to parse exec-command value %s", value);
1169 m->control_command_id = id;
1170 m->control_command = m->exec_command + id;
1172 } else if (streq(key, "tmp-dir")) {
1179 m->exec_context.tmp_dir = t;
1180 } else if (streq(key, "var-tmp-dir")) {
1187 m->exec_context.var_tmp_dir = t;
1189 log_debug_unit(UNIT(m)->id,
1190 "Unknown serialization key '%s'", key);
1195 _pure_ static UnitActiveState mount_active_state(Unit *u) {
1198 return state_translation_table[MOUNT(u)->state];
1201 _pure_ static const char *mount_sub_state_to_string(Unit *u) {
1204 return mount_state_to_string(MOUNT(u)->state);
1207 _pure_ static bool mount_check_gc(Unit *u) {
1208 Mount *m = MOUNT(u);
1212 return m->from_proc_self_mountinfo;
1215 static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1216 Mount *m = MOUNT(u);
1222 if (pid != m->control_pid)
1227 if (is_clean_exit(code, status, NULL))
1229 else if (code == CLD_EXITED)
1230 f = MOUNT_FAILURE_EXIT_CODE;
1231 else if (code == CLD_KILLED)
1232 f = MOUNT_FAILURE_SIGNAL;
1233 else if (code == CLD_DUMPED)
1234 f = MOUNT_FAILURE_CORE_DUMP;
1236 assert_not_reached("Unknown code");
1238 if (f != MOUNT_SUCCESS)
1241 if (m->control_command) {
1242 exec_status_exit(&m->control_command->exec_status, &m->exec_context, pid, code, status);
1244 m->control_command = NULL;
1245 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
1248 log_full_unit(f == MOUNT_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
1249 "%s mount process exited, code=%s status=%i",
1250 u->id, sigchld_code_to_string(code), status);
1252 /* Note that mount(8) returning and the kernel sending us a
1253 * mount table change event might happen out-of-order. If an
1254 * operation succeed we assume the kernel will follow soon too
1255 * and already change into the resulting state. If it fails
1256 * we check if the kernel still knows about the mount. and
1257 * change state accordingly. */
1261 case MOUNT_MOUNTING:
1262 case MOUNT_MOUNTING_DONE:
1263 case MOUNT_MOUNTING_SIGKILL:
1264 case MOUNT_MOUNTING_SIGTERM:
1266 if (f == MOUNT_SUCCESS)
1267 mount_enter_mounted(m, f);
1268 else if (m->from_proc_self_mountinfo)
1269 mount_enter_mounted(m, f);
1271 mount_enter_dead(m, f);
1274 case MOUNT_REMOUNTING:
1275 case MOUNT_REMOUNTING_SIGKILL:
1276 case MOUNT_REMOUNTING_SIGTERM:
1278 m->reload_result = f;
1279 if (m->from_proc_self_mountinfo)
1280 mount_enter_mounted(m, MOUNT_SUCCESS);
1282 mount_enter_dead(m, MOUNT_SUCCESS);
1286 case MOUNT_UNMOUNTING:
1287 case MOUNT_UNMOUNTING_SIGKILL:
1288 case MOUNT_UNMOUNTING_SIGTERM:
1290 if (f == MOUNT_SUCCESS)
1291 mount_enter_dead(m, f);
1292 else if (m->from_proc_self_mountinfo)
1293 mount_enter_mounted(m, f);
1295 mount_enter_dead(m, f);
1299 assert_not_reached("Uh, control process died at wrong time.");
1302 /* Notify clients about changed exit status */
1303 unit_add_to_dbus_queue(u);
1306 static void mount_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
1307 Mount *m = MOUNT(u);
1310 assert(elapsed == 1);
1311 assert(w == &m->timer_watch);
1315 case MOUNT_MOUNTING:
1316 case MOUNT_MOUNTING_DONE:
1317 log_warning_unit(u->id,
1318 "%s mounting timed out. Stopping.", u->id);
1319 mount_enter_signal(m, MOUNT_MOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1322 case MOUNT_REMOUNTING:
1323 log_warning_unit(u->id,
1324 "%s remounting timed out. Stopping.", u->id);
1325 m->reload_result = MOUNT_FAILURE_TIMEOUT;
1326 mount_enter_mounted(m, MOUNT_SUCCESS);
1329 case MOUNT_UNMOUNTING:
1330 log_warning_unit(u->id,
1331 "%s unmounting timed out. Stopping.", u->id);
1332 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1335 case MOUNT_MOUNTING_SIGTERM:
1336 if (m->kill_context.send_sigkill) {
1337 log_warning_unit(u->id,
1338 "%s mounting timed out. Killing.", u->id);
1339 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1341 log_warning_unit(u->id,
1342 "%s mounting 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_REMOUNTING_SIGTERM:
1353 if (m->kill_context.send_sigkill) {
1354 log_warning_unit(u->id,
1355 "%s remounting timed out. Killing.", u->id);
1356 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1358 log_warning_unit(u->id,
1359 "%s remounting 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_UNMOUNTING_SIGTERM:
1370 if (m->kill_context.send_sigkill) {
1371 log_warning_unit(u->id,
1372 "%s unmounting timed out. Killing.", u->id);
1373 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1375 log_warning_unit(u->id,
1376 "%s unmounting timed out. Skipping SIGKILL. Ignoring.",
1379 if (m->from_proc_self_mountinfo)
1380 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1382 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1386 case MOUNT_MOUNTING_SIGKILL:
1387 case MOUNT_REMOUNTING_SIGKILL:
1388 case MOUNT_UNMOUNTING_SIGKILL:
1389 log_warning_unit(u->id,
1390 "%s mount process still around after SIGKILL. Ignoring.",
1393 if (m->from_proc_self_mountinfo)
1394 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1396 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1400 assert_not_reached("Timeout at wrong time.");
1404 static int mount_add_one(
1408 const char *options,
1415 char *e, *w = NULL, *o = NULL, *f = NULL;
1417 bool load_extras = false;
1425 /* Ignore API mount points. They should never be referenced in
1426 * dependencies ever. */
1427 if (mount_point_is_api(where) || mount_point_ignore(where))
1430 if (streq(fstype, "autofs"))
1433 /* probably some kind of swap, ignore */
1434 if (!is_path(where))
1437 e = unit_name_from_path(where, ".mount");
1441 u = manager_get_unit(m, e);
1445 u = unit_new(m, sizeof(Mount));
1451 r = unit_add_name(u, e);
1457 MOUNT(u)->where = strdup(where);
1458 if (!MOUNT(u)->where) {
1463 u->source_path = strdup("/proc/self/mountinfo");
1464 if (!u->source_path) {
1469 r = unit_add_dependency_by_name(u, UNIT_BEFORE, SPECIAL_LOCAL_FS_TARGET, NULL, true);
1473 if (should_umount(MOUNT(u))) {
1474 r = unit_add_dependency_by_name(u, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
1479 unit_add_to_load_queue(u);
1484 if (!MOUNT(u)->where) {
1485 MOUNT(u)->where = strdup(where);
1486 if (!MOUNT(u)->where) {
1492 if (u->load_state == UNIT_NOT_FOUND) {
1493 u->load_state = UNIT_LOADED;
1496 /* Load in the extras later on, after we
1497 * finished initialization of the unit */
1502 if (!(w = strdup(what)) ||
1503 !(o = strdup(options)) ||
1504 !(f = strdup(fstype))) {
1509 p = &MOUNT(u)->parameters_proc_self_mountinfo;
1511 MOUNT(u)->is_mounted = true;
1512 MOUNT(u)->just_mounted = !MOUNT(u)->from_proc_self_mountinfo;
1513 MOUNT(u)->just_changed = !streq_ptr(p->options, o);
1516 MOUNT(u)->from_proc_self_mountinfo = true;
1530 r = mount_add_extras(MOUNT(u));
1535 unit_add_to_dbus_queue(u);
1550 static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
1556 rewind(m->proc_self_mountinfo);
1559 _cleanup_free_ char *device = NULL, *path = NULL, *options = NULL, *options2 = NULL, *fstype = NULL, *d = NULL, *p = NULL, *o = NULL;
1562 k = fscanf(m->proc_self_mountinfo,
1563 "%*s " /* (1) mount id */
1564 "%*s " /* (2) parent id */
1565 "%*s " /* (3) major:minor */
1566 "%*s " /* (4) root */
1567 "%ms " /* (5) mount point */
1568 "%ms" /* (6) mount options */
1569 "%*[^-]" /* (7) optional fields */
1570 "- " /* (8) separator */
1571 "%ms " /* (9) file system type */
1572 "%ms" /* (10) mount source */
1573 "%ms" /* (11) mount options 2 */
1574 "%*[^\n]", /* some rubbish at the end */
1585 log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
1589 o = strjoin(options, ",", options2, NULL);
1593 d = cunescape(device);
1594 p = cunescape(path);
1598 k = mount_add_one(m, d, p, o, fstype, 0, set_flags);
1606 static void mount_shutdown(Manager *m) {
1609 if (m->proc_self_mountinfo) {
1610 fclose(m->proc_self_mountinfo);
1611 m->proc_self_mountinfo = NULL;
1615 static int mount_enumerate(Manager *m) {
1619 if (!m->proc_self_mountinfo) {
1620 struct epoll_event ev = {
1622 .data.ptr = &m->mount_watch,
1625 m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
1626 if (!m->proc_self_mountinfo)
1629 m->mount_watch.type = WATCH_MOUNT;
1630 m->mount_watch.fd = fileno(m->proc_self_mountinfo);
1632 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->mount_watch.fd, &ev) < 0)
1636 r = mount_load_proc_self_mountinfo(m, false);
1647 void mount_fd_event(Manager *m, int events) {
1652 assert(events & EPOLLPRI);
1654 /* The manager calls this for every fd event happening on the
1655 * /proc/self/mountinfo file, which informs us about mounting
1658 r = mount_load_proc_self_mountinfo(m, true);
1660 log_error("Failed to reread /proc/self/mountinfo: %s", strerror(-r));
1662 /* Reset flags, just in case, for later calls */
1663 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1664 Mount *mount = MOUNT(u);
1666 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1672 manager_dispatch_load_queue(m);
1674 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1675 Mount *mount = MOUNT(u);
1677 if (!mount->is_mounted) {
1678 /* This has just been unmounted. */
1680 mount->from_proc_self_mountinfo = false;
1682 switch (mount->state) {
1685 mount_enter_dead(mount, MOUNT_SUCCESS);
1689 mount_set_state(mount, mount->state);
1694 } else if (mount->just_mounted || mount->just_changed) {
1696 /* New or changed mount entry */
1698 switch (mount->state) {
1702 mount_enter_mounted(mount, MOUNT_SUCCESS);
1705 case MOUNT_MOUNTING:
1706 mount_enter_mounting_done(mount);
1710 /* Nothing really changed, but let's
1711 * issue an notification call
1712 * nonetheless, in case somebody is
1713 * waiting for this. (e.g. file system
1714 * ro/rw remounts.) */
1715 mount_set_state(mount, mount->state);
1720 /* Reset the flags for later calls */
1721 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1725 static void mount_reset_failed(Unit *u) {
1726 Mount *m = MOUNT(u);
1730 if (m->state == MOUNT_FAILED)
1731 mount_set_state(m, MOUNT_DEAD);
1733 m->result = MOUNT_SUCCESS;
1734 m->reload_result = MOUNT_SUCCESS;
1737 static int mount_kill(Unit *u, KillWho who, int signo, DBusError *error) {
1738 return unit_kill_common(u, who, signo, -1, MOUNT(u)->control_pid, error);
1741 static const char* const mount_state_table[_MOUNT_STATE_MAX] = {
1742 [MOUNT_DEAD] = "dead",
1743 [MOUNT_MOUNTING] = "mounting",
1744 [MOUNT_MOUNTING_DONE] = "mounting-done",
1745 [MOUNT_MOUNTED] = "mounted",
1746 [MOUNT_REMOUNTING] = "remounting",
1747 [MOUNT_UNMOUNTING] = "unmounting",
1748 [MOUNT_MOUNTING_SIGTERM] = "mounting-sigterm",
1749 [MOUNT_MOUNTING_SIGKILL] = "mounting-sigkill",
1750 [MOUNT_REMOUNTING_SIGTERM] = "remounting-sigterm",
1751 [MOUNT_REMOUNTING_SIGKILL] = "remounting-sigkill",
1752 [MOUNT_UNMOUNTING_SIGTERM] = "unmounting-sigterm",
1753 [MOUNT_UNMOUNTING_SIGKILL] = "unmounting-sigkill",
1754 [MOUNT_FAILED] = "failed"
1757 DEFINE_STRING_TABLE_LOOKUP(mount_state, MountState);
1759 static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
1760 [MOUNT_EXEC_MOUNT] = "ExecMount",
1761 [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
1762 [MOUNT_EXEC_REMOUNT] = "ExecRemount",
1765 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
1767 static const char* const mount_result_table[_MOUNT_RESULT_MAX] = {
1768 [MOUNT_SUCCESS] = "success",
1769 [MOUNT_FAILURE_RESOURCES] = "resources",
1770 [MOUNT_FAILURE_TIMEOUT] = "timeout",
1771 [MOUNT_FAILURE_EXIT_CODE] = "exit-code",
1772 [MOUNT_FAILURE_SIGNAL] = "signal",
1773 [MOUNT_FAILURE_CORE_DUMP] = "core-dump"
1776 DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult);
1778 const UnitVTable mount_vtable = {
1779 .object_size = sizeof(Mount),
1786 .private_section = "Mount",
1787 .exec_context_offset = offsetof(Mount, exec_context),
1788 .cgroup_context_offset = offsetof(Mount, cgroup_context),
1791 .no_instances = true,
1797 .coldplug = mount_coldplug,
1801 .start = mount_start,
1803 .reload = mount_reload,
1807 .serialize = mount_serialize,
1808 .deserialize_item = mount_deserialize_item,
1810 .active_state = mount_active_state,
1811 .sub_state_to_string = mount_sub_state_to_string,
1813 .check_gc = mount_check_gc,
1815 .sigchld_event = mount_sigchld_event,
1816 .timer_event = mount_timer_event,
1818 .reset_failed = mount_reset_failed,
1820 .bus_interface = "org.freedesktop.systemd1.Mount",
1821 .bus_message_handler = bus_mount_message_handler,
1822 .bus_invalidating_properties = bus_mount_invalidating_properties,
1823 .bus_set_property = bus_mount_set_property,
1824 .bus_commit_properties = bus_mount_commit_properties,
1826 .enumerate = mount_enumerate,
1827 .shutdown = mount_shutdown,
1829 .status_message_formats = {
1830 .starting_stopping = {
1831 [0] = "Mounting %s...",
1832 [1] = "Unmounting %s...",
1834 .finished_start_job = {
1835 [JOB_DONE] = "Mounted %s.",
1836 [JOB_FAILED] = "Failed to mount %s.",
1837 [JOB_DEPENDENCY] = "Dependency failed for %s.",
1838 [JOB_TIMEOUT] = "Timed out mounting %s.",
1840 .finished_stop_job = {
1841 [JOB_DONE] = "Unmounted %s.",
1842 [JOB_FAILED] = "Failed unmounting %s.",
1843 [JOB_TIMEOUT] = "Timed out unmounting %s.",