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) {
166 pm = get_mount_parameters_fragment(m);
168 /* Adds in links to other mount points that might lie below or
169 * above us in the hierarchy */
171 LIST_FOREACH(units_by_type, other, UNIT(m)->manager->units_by_type[UNIT_MOUNT]) {
172 Mount *n = MOUNT(other);
178 if (UNIT(n)->load_state != UNIT_LOADED)
181 pn = get_mount_parameters_fragment(n);
183 if (path_startswith(m->where, n->where)) {
185 if ((r = unit_add_dependency(UNIT(m), UNIT_AFTER, UNIT(n), true)) < 0)
189 if ((r = unit_add_dependency(UNIT(m), UNIT_REQUIRES, UNIT(n), true)) < 0)
192 } else if (path_startswith(n->where, m->where)) {
194 if ((r = unit_add_dependency(UNIT(n), UNIT_AFTER, UNIT(m), true)) < 0)
198 if ((r = unit_add_dependency(UNIT(n), UNIT_REQUIRES, UNIT(m), true)) < 0)
201 } else if (pm && pm->what && path_startswith(pm->what, n->where)) {
203 if ((r = unit_add_dependency(UNIT(m), UNIT_AFTER, UNIT(n), true)) < 0)
206 if ((r = unit_add_dependency(UNIT(m), UNIT_REQUIRES, UNIT(n), true)) < 0)
209 } else if (pn && pn->what && path_startswith(pn->what, m->where)) {
211 if ((r = unit_add_dependency(UNIT(n), UNIT_AFTER, UNIT(m), true)) < 0)
214 if ((r = unit_add_dependency(UNIT(n), UNIT_REQUIRES, UNIT(m), true)) < 0)
222 static int mount_add_swap_links(Mount *m) {
228 LIST_FOREACH(units_by_type, other, UNIT(m)->manager->units_by_type[UNIT_SWAP]) {
229 r = swap_add_one_mount_link(SWAP(other), m);
237 static int mount_add_path_links(Mount *m) {
243 LIST_FOREACH(units_by_type, other, UNIT(m)->manager->units_by_type[UNIT_PATH]) {
244 r = path_add_one_mount_link(PATH(other), m);
252 static int mount_add_automount_links(Mount *m) {
258 LIST_FOREACH(units_by_type, other, UNIT(m)->manager->units_by_type[UNIT_AUTOMOUNT]) {
259 r = automount_add_one_mount_link(AUTOMOUNT(other), m);
267 static int mount_add_socket_links(Mount *m) {
273 LIST_FOREACH(units_by_type, other, UNIT(m)->manager->units_by_type[UNIT_SOCKET]) {
274 r = socket_add_one_mount_link(SOCKET(other), m);
282 static int mount_add_requires_mounts_links(Mount *m) {
288 LIST_FOREACH(has_requires_mounts_for, other, UNIT(m)->manager->has_requires_mounts_for) {
289 r = unit_add_one_mount_link(other, m);
297 static char* mount_test_option(const char *haystack, const char *needle) {
298 struct mntent me = { .mnt_opts = (char*) haystack };
302 /* Like glibc's hasmntopt(), but works on a string, not a
308 return hasmntopt(&me, needle);
311 static bool mount_is_network(MountParameters *p) {
314 if (mount_test_option(p->options, "_netdev"))
317 if (p->fstype && fstype_is_network(p->fstype))
323 static bool mount_is_bind(MountParameters *p) {
326 if (mount_test_option(p->options, "bind"))
329 if (p->fstype && streq(p->fstype, "bind"))
332 if (mount_test_option(p->options, "rbind"))
335 if (p->fstype && streq(p->fstype, "rbind"))
341 static bool mount_is_auto(MountParameters *p) {
344 return !mount_test_option(p->options, "noauto");
347 static bool needs_quota(MountParameters *p) {
350 if (mount_is_network(p))
353 if (mount_is_bind(p))
356 return mount_test_option(p->options, "usrquota") ||
357 mount_test_option(p->options, "grpquota") ||
358 mount_test_option(p->options, "quota") ||
359 mount_test_option(p->options, "usrjquota") ||
360 mount_test_option(p->options, "grpjquota");
363 static int mount_add_device_links(Mount *m) {
365 bool device_wants_mount = false;
370 p = get_mount_parameters_fragment(m);
377 if (mount_is_bind(p))
380 if (!is_device_path(p->what))
383 if (path_equal(m->where, "/"))
386 if (mount_is_auto(p) && UNIT(m)->manager->running_as == SYSTEMD_SYSTEM)
387 device_wants_mount = true;
389 r = unit_add_node_link(UNIT(m), p->what, device_wants_mount);
394 UNIT(m)->manager->running_as == SYSTEMD_SYSTEM) {
397 /* Let's add in the fsck service */
399 /* aka SPECIAL_FSCK_SERVICE */
400 name = unit_name_from_path_instance("systemd-fsck", p->what, ".service");
404 r = manager_load_unit_prepare(UNIT(m)->manager, name, NULL, NULL, &fsck);
406 log_warning_unit(name,
407 "Failed to prepare unit %s: %s", name, strerror(-r));
413 SERVICE(fsck)->fsck_passno = p->passno;
415 r = unit_add_two_dependencies(UNIT(m), UNIT_AFTER, UNIT_REQUIRES, fsck, true);
423 static int mount_add_quota_links(Mount *m) {
429 if (UNIT(m)->manager->running_as != SYSTEMD_SYSTEM)
432 p = get_mount_parameters_fragment(m);
439 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTACHECK_SERVICE, NULL, true);
443 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTAON_SERVICE, NULL, true);
450 static bool should_umount(Mount *m) {
453 if (path_equal(m->where, "/") ||
454 path_equal(m->where, "/usr"))
457 p = get_mount_parameters(m);
458 if (p && mount_test_option(p->options, "x-initrd.mount") &&
465 static int mount_add_default_dependencies(Mount *m) {
466 const char *after, *after2, *online;
472 if (UNIT(m)->manager->running_as != SYSTEMD_SYSTEM)
475 p = get_mount_parameters(m);
480 if (path_equal(m->where, "/"))
483 if (mount_is_network(p)) {
484 after = SPECIAL_REMOTE_FS_PRE_TARGET;
485 after2 = SPECIAL_NETWORK_TARGET;
486 online = SPECIAL_NETWORK_ONLINE_TARGET;
488 after = SPECIAL_LOCAL_FS_PRE_TARGET;
493 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after, NULL, true);
498 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after2, NULL, true);
504 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_WANTS, UNIT_AFTER, online, NULL, true);
509 if (should_umount(m)) {
510 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
518 static int mount_fix_timeouts(Mount *m) {
520 const char *timeout = NULL;
529 p = get_mount_parameters_fragment(m);
533 /* Allow configuration how long we wait for a device that
534 * backs a mount point to show up. This is useful to support
535 * endless device timeouts for devices that show up only after
536 * user input, like crypto devices. */
538 if ((timeout = mount_test_option(p->options, "comment=systemd.device-timeout")))
540 else if ((timeout = mount_test_option(p->options, "x-systemd.device-timeout")))
545 t = strndup(timeout, strcspn(timeout, ",;" WHITESPACE));
549 r = parse_sec(t, &u);
553 log_warning_unit(UNIT(m)->id,
554 "Failed to parse timeout for %s, ignoring: %s",
559 SET_FOREACH(other, UNIT(m)->dependencies[UNIT_AFTER], i) {
560 if (other->type != UNIT_DEVICE)
563 other->job_timeout = u;
569 static int mount_verify(Mount *m) {
574 if (UNIT(m)->load_state != UNIT_LOADED)
577 if (!m->from_fragment && !m->from_proc_self_mountinfo)
580 if (!(e = unit_name_from_path(m->where, ".mount")))
583 b = unit_has_name(UNIT(m), e);
587 log_error_unit(UNIT(m)->id,
588 "%s's Where setting doesn't match unit name. Refusing.",
593 if (mount_point_is_api(m->where) || mount_point_ignore(m->where)) {
594 log_error_unit(UNIT(m)->id,
595 "Cannot create mount unit for API file system %s. Refusing.",
600 if (UNIT(m)->fragment_path && !m->parameters_fragment.what) {
601 log_error_unit(UNIT(m)->id,
602 "%s's What setting is missing. Refusing.", UNIT(m)->id);
606 if (m->exec_context.pam_name && m->kill_context.kill_mode != KILL_CONTROL_GROUP) {
607 log_error_unit(UNIT(m)->id,
608 "%s has PAM enabled. Kill mode must be set to control-group'. Refusing.",
616 static int mount_add_extras(Mount *m) {
620 if (UNIT(m)->fragment_path)
621 m->from_fragment = true;
624 m->where = unit_name_to_path(u->id);
629 path_kill_slashes(m->where);
631 r = unit_add_exec_dependencies(u, &m->exec_context);
635 if (!UNIT(m)->description) {
636 r = unit_set_description(u, m->where);
641 r = mount_add_device_links(m);
645 r = mount_add_mount_links(m);
649 r = mount_add_socket_links(m);
653 r = mount_add_swap_links(m);
657 r = mount_add_path_links(m);
661 r = mount_add_requires_mounts_links(m);
665 r = mount_add_automount_links(m);
669 r = mount_add_quota_links(m);
673 if (UNIT(m)->default_dependencies) {
674 r = mount_add_default_dependencies(m);
679 r = unit_add_default_slice(u);
683 r = mount_fix_timeouts(m);
690 static int mount_load(Unit *u) {
695 assert(u->load_state == UNIT_STUB);
697 if (m->from_proc_self_mountinfo)
698 r = unit_load_fragment_and_dropin_optional(u);
700 r = unit_load_fragment_and_dropin(u);
705 /* This is a new unit? Then let's add in some extras */
706 if (u->load_state == UNIT_LOADED) {
707 r = mount_add_extras(m);
711 r = unit_exec_context_defaults(u, &m->exec_context);
716 return mount_verify(m);
719 static int mount_notify_automount(Mount *m, int status) {
726 SET_FOREACH(p, UNIT(m)->dependencies[UNIT_TRIGGERED_BY], i)
727 if (p->type == UNIT_AUTOMOUNT) {
728 r = automount_send_ready(AUTOMOUNT(p), status);
736 static void mount_set_state(Mount *m, MountState state) {
737 MountState old_state;
740 old_state = m->state;
743 if (state != MOUNT_MOUNTING &&
744 state != MOUNT_MOUNTING_DONE &&
745 state != MOUNT_REMOUNTING &&
746 state != MOUNT_UNMOUNTING &&
747 state != MOUNT_MOUNTING_SIGTERM &&
748 state != MOUNT_MOUNTING_SIGKILL &&
749 state != MOUNT_UNMOUNTING_SIGTERM &&
750 state != MOUNT_UNMOUNTING_SIGKILL &&
751 state != MOUNT_REMOUNTING_SIGTERM &&
752 state != MOUNT_REMOUNTING_SIGKILL) {
753 unit_unwatch_timer(UNIT(m), &m->timer_watch);
754 mount_unwatch_control_pid(m);
755 m->control_command = NULL;
756 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
759 if (state == MOUNT_MOUNTED ||
760 state == MOUNT_REMOUNTING)
761 mount_notify_automount(m, 0);
762 else if (state == MOUNT_DEAD ||
763 state == MOUNT_UNMOUNTING ||
764 state == MOUNT_MOUNTING_SIGTERM ||
765 state == MOUNT_MOUNTING_SIGKILL ||
766 state == MOUNT_REMOUNTING_SIGTERM ||
767 state == MOUNT_REMOUNTING_SIGKILL ||
768 state == MOUNT_UNMOUNTING_SIGTERM ||
769 state == MOUNT_UNMOUNTING_SIGKILL ||
770 state == MOUNT_FAILED) {
771 if (state != old_state)
772 mount_notify_automount(m, -ENODEV);
775 if (state != old_state)
776 log_debug_unit(UNIT(m)->id,
777 "%s changed %s -> %s",
779 mount_state_to_string(old_state),
780 mount_state_to_string(state));
782 unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state], m->reload_result == MOUNT_SUCCESS);
783 m->reload_result = MOUNT_SUCCESS;
786 static int mount_coldplug(Unit *u) {
788 MountState new_state = MOUNT_DEAD;
792 assert(m->state == MOUNT_DEAD);
794 if (m->deserialized_state != m->state)
795 new_state = m->deserialized_state;
796 else if (m->from_proc_self_mountinfo)
797 new_state = MOUNT_MOUNTED;
799 if (new_state != m->state) {
801 if (new_state == MOUNT_MOUNTING ||
802 new_state == MOUNT_MOUNTING_DONE ||
803 new_state == MOUNT_REMOUNTING ||
804 new_state == MOUNT_UNMOUNTING ||
805 new_state == MOUNT_MOUNTING_SIGTERM ||
806 new_state == MOUNT_MOUNTING_SIGKILL ||
807 new_state == MOUNT_UNMOUNTING_SIGTERM ||
808 new_state == MOUNT_UNMOUNTING_SIGKILL ||
809 new_state == MOUNT_REMOUNTING_SIGTERM ||
810 new_state == MOUNT_REMOUNTING_SIGKILL) {
812 if (m->control_pid <= 0)
815 r = unit_watch_pid(UNIT(m), m->control_pid);
819 r = unit_watch_timer(UNIT(m), CLOCK_MONOTONIC, true, m->timeout_usec, &m->timer_watch);
824 mount_set_state(m, new_state);
830 static void mount_dump(Unit *u, FILE *f, const char *prefix) {
837 p = get_mount_parameters(m);
840 "%sMount State: %s\n"
844 "%sFile System Type: %s\n"
846 "%sFrom /proc/self/mountinfo: %s\n"
847 "%sFrom fragment: %s\n"
848 "%sDirectoryMode: %04o\n",
849 prefix, mount_state_to_string(m->state),
850 prefix, mount_result_to_string(m->result),
852 prefix, p ? strna(p->what) : "n/a",
853 prefix, p ? strna(p->fstype) : "n/a",
854 prefix, p ? strna(p->options) : "n/a",
855 prefix, yes_no(m->from_proc_self_mountinfo),
856 prefix, yes_no(m->from_fragment),
857 prefix, m->directory_mode);
859 if (m->control_pid > 0)
861 "%sControl PID: %lu\n",
862 prefix, (unsigned long) m->control_pid);
864 exec_context_dump(&m->exec_context, f, prefix);
865 kill_context_dump(&m->kill_context, f, prefix);
868 static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
876 unit_realize_cgroup(UNIT(m));
878 r = unit_watch_timer(UNIT(m), CLOCK_MONOTONIC, true, m->timeout_usec, &m->timer_watch);
886 UNIT(m)->manager->environment,
890 UNIT(m)->manager->confirm_spawn,
891 UNIT(m)->cgroup_mask,
892 UNIT(m)->cgroup_path,
899 r = unit_watch_pid(UNIT(m), pid);
901 /* FIXME: we need to do something here */
909 unit_unwatch_timer(UNIT(m), &m->timer_watch);
914 static void mount_enter_dead(Mount *m, MountResult f) {
917 if (f != MOUNT_SUCCESS)
920 exec_context_tmp_dirs_done(&m->exec_context);
921 mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD);
924 static void mount_enter_mounted(Mount *m, MountResult f) {
927 if (f != MOUNT_SUCCESS)
930 mount_set_state(m, MOUNT_MOUNTED);
933 static void mount_enter_signal(Mount *m, MountState state, MountResult f) {
938 if (f != MOUNT_SUCCESS)
941 r = unit_kill_context(
944 state != MOUNT_MOUNTING_SIGTERM && state != MOUNT_UNMOUNTING_SIGTERM && state != MOUNT_REMOUNTING_SIGTERM,
952 r = unit_watch_timer(UNIT(m), CLOCK_MONOTONIC, true, m->timeout_usec, &m->timer_watch);
956 mount_set_state(m, state);
957 } else if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
958 mount_enter_mounted(m, MOUNT_SUCCESS);
960 mount_enter_dead(m, MOUNT_SUCCESS);
965 log_warning_unit(UNIT(m)->id,
966 "%s failed to kill processes: %s", UNIT(m)->id, strerror(-r));
968 if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
969 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
971 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
974 void warn_if_dir_nonempty(const char *unit, const char* where) {
978 if (dir_is_empty(where) > 0)
981 log_struct_unit(LOG_NOTICE,
983 "MESSAGE=%s: Directory %s to mount over is not empty, mounting anyway.",
986 MESSAGE_ID(SD_MESSAGE_OVERMOUNTING),
990 static void mount_enter_unmounting(Mount *m) {
995 m->control_command_id = MOUNT_EXEC_UNMOUNT;
996 m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
998 if ((r = exec_command_set(
1005 mount_unwatch_control_pid(m);
1007 if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
1010 mount_set_state(m, MOUNT_UNMOUNTING);
1015 log_warning_unit(UNIT(m)->id,
1016 "%s failed to run 'umount' task: %s",
1017 UNIT(m)->id, strerror(-r));
1018 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
1021 static void mount_enter_mounting(Mount *m) {
1027 m->control_command_id = MOUNT_EXEC_MOUNT;
1028 m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
1030 mkdir_p_label(m->where, m->directory_mode);
1032 warn_if_dir_nonempty(m->meta.id, m->where);
1034 /* Create the source directory for bind-mounts if needed */
1035 p = get_mount_parameters_fragment(m);
1036 if (p && mount_is_bind(p))
1037 mkdir_p_label(p->what, m->directory_mode);
1039 if (m->from_fragment)
1040 r = exec_command_set(
1043 m->parameters_fragment.what,
1045 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
1046 m->parameters_fragment.options ? "-o" : NULL, m->parameters_fragment.options,
1054 mount_unwatch_control_pid(m);
1056 r = mount_spawn(m, m->control_command, &m->control_pid);
1060 mount_set_state(m, MOUNT_MOUNTING);
1065 log_warning_unit(UNIT(m)->id,
1066 "%s failed to run 'mount' task: %s",
1067 UNIT(m)->id, strerror(-r));
1068 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
1071 static void mount_enter_mounting_done(Mount *m) {
1074 mount_set_state(m, MOUNT_MOUNTING_DONE);
1077 static void mount_enter_remounting(Mount *m) {
1082 m->control_command_id = MOUNT_EXEC_REMOUNT;
1083 m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
1085 if (m->from_fragment) {
1089 if (m->parameters_fragment.options) {
1090 if (!(buf = strappend("remount,", m->parameters_fragment.options))) {
1099 r = exec_command_set(
1102 m->parameters_fragment.what,
1104 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
1115 mount_unwatch_control_pid(m);
1117 if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
1120 mount_set_state(m, MOUNT_REMOUNTING);
1125 log_warning_unit(UNIT(m)->id,
1126 "%s failed to run 'remount' task: %s",
1127 UNIT(m)->id, strerror(-r));
1128 m->reload_result = MOUNT_FAILURE_RESOURCES;
1129 mount_enter_mounted(m, MOUNT_SUCCESS);
1132 static int mount_start(Unit *u) {
1133 Mount *m = MOUNT(u);
1137 /* We cannot fulfill this request right now, try again later
1139 if (m->state == MOUNT_UNMOUNTING ||
1140 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1141 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1142 m->state == MOUNT_MOUNTING_SIGTERM ||
1143 m->state == MOUNT_MOUNTING_SIGKILL)
1146 /* Already on it! */
1147 if (m->state == MOUNT_MOUNTING)
1150 assert(m->state == MOUNT_DEAD || m->state == MOUNT_FAILED);
1152 m->result = MOUNT_SUCCESS;
1153 m->reload_result = MOUNT_SUCCESS;
1155 mount_enter_mounting(m);
1159 static int mount_stop(Unit *u) {
1160 Mount *m = MOUNT(u);
1165 if (m->state == MOUNT_UNMOUNTING ||
1166 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1167 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1168 m->state == MOUNT_MOUNTING_SIGTERM ||
1169 m->state == MOUNT_MOUNTING_SIGKILL)
1172 assert(m->state == MOUNT_MOUNTING ||
1173 m->state == MOUNT_MOUNTING_DONE ||
1174 m->state == MOUNT_MOUNTED ||
1175 m->state == MOUNT_REMOUNTING ||
1176 m->state == MOUNT_REMOUNTING_SIGTERM ||
1177 m->state == MOUNT_REMOUNTING_SIGKILL);
1179 mount_enter_unmounting(m);
1183 static int mount_reload(Unit *u) {
1184 Mount *m = MOUNT(u);
1188 if (m->state == MOUNT_MOUNTING_DONE)
1191 assert(m->state == MOUNT_MOUNTED);
1193 mount_enter_remounting(m);
1197 static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
1198 Mount *m = MOUNT(u);
1204 unit_serialize_item(u, f, "state", mount_state_to_string(m->state));
1205 unit_serialize_item(u, f, "result", mount_result_to_string(m->result));
1206 unit_serialize_item(u, f, "reload-result", mount_result_to_string(m->reload_result));
1208 if (m->control_pid > 0)
1209 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) m->control_pid);
1211 if (m->control_command_id >= 0)
1212 unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(m->control_command_id));
1214 exec_context_serialize(&m->exec_context, UNIT(m), f);
1219 static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1220 Mount *m = MOUNT(u);
1227 if (streq(key, "state")) {
1230 if ((state = mount_state_from_string(value)) < 0)
1231 log_debug_unit(u->id, "Failed to parse state value %s", value);
1233 m->deserialized_state = state;
1234 } else if (streq(key, "result")) {
1237 f = mount_result_from_string(value);
1239 log_debug_unit(UNIT(m)->id,
1240 "Failed to parse result value %s", value);
1241 else if (f != MOUNT_SUCCESS)
1244 } else if (streq(key, "reload-result")) {
1247 f = mount_result_from_string(value);
1249 log_debug_unit(UNIT(m)->id,
1250 "Failed to parse reload result value %s", value);
1251 else if (f != MOUNT_SUCCESS)
1252 m->reload_result = f;
1254 } else if (streq(key, "control-pid")) {
1257 if (parse_pid(value, &pid) < 0)
1258 log_debug_unit(UNIT(m)->id,
1259 "Failed to parse control-pid value %s", value);
1261 m->control_pid = pid;
1262 } else if (streq(key, "control-command")) {
1263 MountExecCommand id;
1265 if ((id = mount_exec_command_from_string(value)) < 0)
1266 log_debug_unit(UNIT(m)->id,
1267 "Failed to parse exec-command value %s", value);
1269 m->control_command_id = id;
1270 m->control_command = m->exec_command + id;
1272 } else if (streq(key, "tmp-dir")) {
1279 m->exec_context.tmp_dir = t;
1280 } else if (streq(key, "var-tmp-dir")) {
1287 m->exec_context.var_tmp_dir = t;
1289 log_debug_unit(UNIT(m)->id,
1290 "Unknown serialization key '%s'", key);
1295 _pure_ static UnitActiveState mount_active_state(Unit *u) {
1298 return state_translation_table[MOUNT(u)->state];
1301 _pure_ static const char *mount_sub_state_to_string(Unit *u) {
1304 return mount_state_to_string(MOUNT(u)->state);
1307 _pure_ static bool mount_check_gc(Unit *u) {
1308 Mount *m = MOUNT(u);
1312 return m->from_proc_self_mountinfo;
1315 static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1316 Mount *m = MOUNT(u);
1322 if (pid != m->control_pid)
1327 if (is_clean_exit(code, status, NULL))
1329 else if (code == CLD_EXITED)
1330 f = MOUNT_FAILURE_EXIT_CODE;
1331 else if (code == CLD_KILLED)
1332 f = MOUNT_FAILURE_SIGNAL;
1333 else if (code == CLD_DUMPED)
1334 f = MOUNT_FAILURE_CORE_DUMP;
1336 assert_not_reached("Unknown code");
1338 if (f != MOUNT_SUCCESS)
1341 if (m->control_command) {
1342 exec_status_exit(&m->control_command->exec_status, &m->exec_context, pid, code, status);
1344 m->control_command = NULL;
1345 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
1348 log_full_unit(f == MOUNT_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
1349 "%s mount process exited, code=%s status=%i",
1350 u->id, sigchld_code_to_string(code), status);
1352 /* Note that mount(8) returning and the kernel sending us a
1353 * mount table change event might happen out-of-order. If an
1354 * operation succeed we assume the kernel will follow soon too
1355 * and already change into the resulting state. If it fails
1356 * we check if the kernel still knows about the mount. and
1357 * change state accordingly. */
1361 case MOUNT_MOUNTING:
1362 case MOUNT_MOUNTING_DONE:
1363 case MOUNT_MOUNTING_SIGKILL:
1364 case MOUNT_MOUNTING_SIGTERM:
1366 if (f == MOUNT_SUCCESS)
1367 mount_enter_mounted(m, f);
1368 else if (m->from_proc_self_mountinfo)
1369 mount_enter_mounted(m, f);
1371 mount_enter_dead(m, f);
1374 case MOUNT_REMOUNTING:
1375 case MOUNT_REMOUNTING_SIGKILL:
1376 case MOUNT_REMOUNTING_SIGTERM:
1378 m->reload_result = f;
1379 if (m->from_proc_self_mountinfo)
1380 mount_enter_mounted(m, MOUNT_SUCCESS);
1382 mount_enter_dead(m, MOUNT_SUCCESS);
1386 case MOUNT_UNMOUNTING:
1387 case MOUNT_UNMOUNTING_SIGKILL:
1388 case MOUNT_UNMOUNTING_SIGTERM:
1390 if (f == MOUNT_SUCCESS)
1391 mount_enter_dead(m, f);
1392 else if (m->from_proc_self_mountinfo)
1393 mount_enter_mounted(m, f);
1395 mount_enter_dead(m, f);
1399 assert_not_reached("Uh, control process died at wrong time.");
1402 /* Notify clients about changed exit status */
1403 unit_add_to_dbus_queue(u);
1406 static void mount_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
1407 Mount *m = MOUNT(u);
1410 assert(elapsed == 1);
1411 assert(w == &m->timer_watch);
1415 case MOUNT_MOUNTING:
1416 case MOUNT_MOUNTING_DONE:
1417 log_warning_unit(u->id,
1418 "%s mounting timed out. Stopping.", u->id);
1419 mount_enter_signal(m, MOUNT_MOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1422 case MOUNT_REMOUNTING:
1423 log_warning_unit(u->id,
1424 "%s remounting timed out. Stopping.", u->id);
1425 m->reload_result = MOUNT_FAILURE_TIMEOUT;
1426 mount_enter_mounted(m, MOUNT_SUCCESS);
1429 case MOUNT_UNMOUNTING:
1430 log_warning_unit(u->id,
1431 "%s unmounting timed out. Stopping.", u->id);
1432 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1435 case MOUNT_MOUNTING_SIGTERM:
1436 if (m->kill_context.send_sigkill) {
1437 log_warning_unit(u->id,
1438 "%s mounting timed out. Killing.", u->id);
1439 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1441 log_warning_unit(u->id,
1442 "%s mounting timed out. Skipping SIGKILL. Ignoring.",
1445 if (m->from_proc_self_mountinfo)
1446 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1448 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1452 case MOUNT_REMOUNTING_SIGTERM:
1453 if (m->kill_context.send_sigkill) {
1454 log_warning_unit(u->id,
1455 "%s remounting timed out. Killing.", u->id);
1456 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1458 log_warning_unit(u->id,
1459 "%s remounting timed out. Skipping SIGKILL. Ignoring.",
1462 if (m->from_proc_self_mountinfo)
1463 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1465 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1469 case MOUNT_UNMOUNTING_SIGTERM:
1470 if (m->kill_context.send_sigkill) {
1471 log_warning_unit(u->id,
1472 "%s unmounting timed out. Killing.", u->id);
1473 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1475 log_warning_unit(u->id,
1476 "%s unmounting timed out. Skipping SIGKILL. Ignoring.",
1479 if (m->from_proc_self_mountinfo)
1480 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1482 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1486 case MOUNT_MOUNTING_SIGKILL:
1487 case MOUNT_REMOUNTING_SIGKILL:
1488 case MOUNT_UNMOUNTING_SIGKILL:
1489 log_warning_unit(u->id,
1490 "%s mount process still around after SIGKILL. Ignoring.",
1493 if (m->from_proc_self_mountinfo)
1494 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1496 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1500 assert_not_reached("Timeout at wrong time.");
1504 static int mount_add_one(
1508 const char *options,
1515 char *e, *w = NULL, *o = NULL, *f = NULL;
1517 bool load_extras = false;
1525 /* Ignore API mount points. They should never be referenced in
1526 * dependencies ever. */
1527 if (mount_point_is_api(where) || mount_point_ignore(where))
1530 if (streq(fstype, "autofs"))
1533 /* probably some kind of swap, ignore */
1534 if (!is_path(where))
1537 e = unit_name_from_path(where, ".mount");
1541 u = manager_get_unit(m, e);
1545 u = unit_new(m, sizeof(Mount));
1551 r = unit_add_name(u, e);
1557 MOUNT(u)->where = strdup(where);
1558 if (!MOUNT(u)->where) {
1563 u->source_path = strdup("/proc/self/mountinfo");
1564 if (!u->source_path) {
1569 r = unit_add_dependency_by_name(u, UNIT_BEFORE, SPECIAL_LOCAL_FS_TARGET, NULL, true);
1573 if (should_umount(MOUNT(u))) {
1574 r = unit_add_dependency_by_name(u, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
1579 unit_add_to_load_queue(u);
1584 if (!MOUNT(u)->where) {
1585 MOUNT(u)->where = strdup(where);
1586 if (!MOUNT(u)->where) {
1592 if (u->load_state == UNIT_NOT_FOUND) {
1593 u->load_state = UNIT_LOADED;
1596 /* Load in the extras later on, after we
1597 * finished initialization of the unit */
1602 if (!(w = strdup(what)) ||
1603 !(o = strdup(options)) ||
1604 !(f = strdup(fstype))) {
1609 p = &MOUNT(u)->parameters_proc_self_mountinfo;
1611 MOUNT(u)->is_mounted = true;
1612 MOUNT(u)->just_mounted = !MOUNT(u)->from_proc_self_mountinfo;
1613 MOUNT(u)->just_changed = !streq_ptr(p->options, o);
1616 MOUNT(u)->from_proc_self_mountinfo = true;
1630 r = mount_add_extras(MOUNT(u));
1635 unit_add_to_dbus_queue(u);
1650 static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
1653 char *device, *path, *options, *options2, *fstype, *d, *p, *o;
1657 rewind(m->proc_self_mountinfo);
1662 device = path = options = options2 = fstype = d = p = o = NULL;
1664 if ((k = fscanf(m->proc_self_mountinfo,
1665 "%*s " /* (1) mount id */
1666 "%*s " /* (2) parent id */
1667 "%*s " /* (3) major:minor */
1668 "%*s " /* (4) root */
1669 "%ms " /* (5) mount point */
1670 "%ms" /* (6) mount options */
1671 "%*[^-]" /* (7) optional fields */
1672 "- " /* (8) separator */
1673 "%ms " /* (9) file system type */
1674 "%ms" /* (10) mount source */
1675 "%ms" /* (11) mount options 2 */
1676 "%*[^\n]", /* some rubbish at the end */
1686 log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
1690 o = strjoin(options, ",", options2, NULL);
1696 if (!(d = cunescape(device)) ||
1697 !(p = cunescape(path))) {
1702 if ((k = mount_add_one(m, d, p, o, fstype, 0, set_flags)) < 0)
1729 static void mount_shutdown(Manager *m) {
1732 if (m->proc_self_mountinfo) {
1733 fclose(m->proc_self_mountinfo);
1734 m->proc_self_mountinfo = NULL;
1738 static int mount_enumerate(Manager *m) {
1742 if (!m->proc_self_mountinfo) {
1743 struct epoll_event ev = {
1745 .data.ptr = &m->mount_watch,
1748 m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
1749 if (!m->proc_self_mountinfo)
1752 m->mount_watch.type = WATCH_MOUNT;
1753 m->mount_watch.fd = fileno(m->proc_self_mountinfo);
1755 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->mount_watch.fd, &ev) < 0)
1759 r = mount_load_proc_self_mountinfo(m, false);
1770 void mount_fd_event(Manager *m, int events) {
1775 assert(events & EPOLLPRI);
1777 /* The manager calls this for every fd event happening on the
1778 * /proc/self/mountinfo file, which informs us about mounting
1781 r = mount_load_proc_self_mountinfo(m, true);
1783 log_error("Failed to reread /proc/self/mountinfo: %s", strerror(-r));
1785 /* Reset flags, just in case, for later calls */
1786 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1787 Mount *mount = MOUNT(u);
1789 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1795 manager_dispatch_load_queue(m);
1797 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1798 Mount *mount = MOUNT(u);
1800 if (!mount->is_mounted) {
1801 /* This has just been unmounted. */
1803 mount->from_proc_self_mountinfo = false;
1805 switch (mount->state) {
1808 mount_enter_dead(mount, MOUNT_SUCCESS);
1812 mount_set_state(mount, mount->state);
1817 } else if (mount->just_mounted || mount->just_changed) {
1819 /* New or changed mount entry */
1821 switch (mount->state) {
1825 mount_enter_mounted(mount, MOUNT_SUCCESS);
1828 case MOUNT_MOUNTING:
1829 mount_enter_mounting_done(mount);
1833 /* Nothing really changed, but let's
1834 * issue an notification call
1835 * nonetheless, in case somebody is
1836 * waiting for this. (e.g. file system
1837 * ro/rw remounts.) */
1838 mount_set_state(mount, mount->state);
1843 /* Reset the flags for later calls */
1844 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1848 static void mount_reset_failed(Unit *u) {
1849 Mount *m = MOUNT(u);
1853 if (m->state == MOUNT_FAILED)
1854 mount_set_state(m, MOUNT_DEAD);
1856 m->result = MOUNT_SUCCESS;
1857 m->reload_result = MOUNT_SUCCESS;
1860 static int mount_kill(Unit *u, KillWho who, int signo, DBusError *error) {
1861 return unit_kill_common(u, who, signo, -1, MOUNT(u)->control_pid, error);
1864 static const char* const mount_state_table[_MOUNT_STATE_MAX] = {
1865 [MOUNT_DEAD] = "dead",
1866 [MOUNT_MOUNTING] = "mounting",
1867 [MOUNT_MOUNTING_DONE] = "mounting-done",
1868 [MOUNT_MOUNTED] = "mounted",
1869 [MOUNT_REMOUNTING] = "remounting",
1870 [MOUNT_UNMOUNTING] = "unmounting",
1871 [MOUNT_MOUNTING_SIGTERM] = "mounting-sigterm",
1872 [MOUNT_MOUNTING_SIGKILL] = "mounting-sigkill",
1873 [MOUNT_REMOUNTING_SIGTERM] = "remounting-sigterm",
1874 [MOUNT_REMOUNTING_SIGKILL] = "remounting-sigkill",
1875 [MOUNT_UNMOUNTING_SIGTERM] = "unmounting-sigterm",
1876 [MOUNT_UNMOUNTING_SIGKILL] = "unmounting-sigkill",
1877 [MOUNT_FAILED] = "failed"
1880 DEFINE_STRING_TABLE_LOOKUP(mount_state, MountState);
1882 static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
1883 [MOUNT_EXEC_MOUNT] = "ExecMount",
1884 [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
1885 [MOUNT_EXEC_REMOUNT] = "ExecRemount",
1888 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
1890 static const char* const mount_result_table[_MOUNT_RESULT_MAX] = {
1891 [MOUNT_SUCCESS] = "success",
1892 [MOUNT_FAILURE_RESOURCES] = "resources",
1893 [MOUNT_FAILURE_TIMEOUT] = "timeout",
1894 [MOUNT_FAILURE_EXIT_CODE] = "exit-code",
1895 [MOUNT_FAILURE_SIGNAL] = "signal",
1896 [MOUNT_FAILURE_CORE_DUMP] = "core-dump"
1899 DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult);
1901 const UnitVTable mount_vtable = {
1902 .object_size = sizeof(Mount),
1909 .private_section = "Mount",
1910 .exec_context_offset = offsetof(Mount, exec_context),
1911 .cgroup_context_offset = offsetof(Mount, cgroup_context),
1914 .no_instances = true,
1920 .coldplug = mount_coldplug,
1924 .start = mount_start,
1926 .reload = mount_reload,
1930 .serialize = mount_serialize,
1931 .deserialize_item = mount_deserialize_item,
1933 .active_state = mount_active_state,
1934 .sub_state_to_string = mount_sub_state_to_string,
1936 .check_gc = mount_check_gc,
1938 .sigchld_event = mount_sigchld_event,
1939 .timer_event = mount_timer_event,
1941 .reset_failed = mount_reset_failed,
1943 .bus_interface = "org.freedesktop.systemd1.Mount",
1944 .bus_message_handler = bus_mount_message_handler,
1945 .bus_invalidating_properties = bus_mount_invalidating_properties,
1946 .bus_set_property = bus_mount_set_property,
1947 .bus_commit_properties = bus_mount_commit_properties,
1949 .enumerate = mount_enumerate,
1950 .shutdown = mount_shutdown,
1952 .status_message_formats = {
1953 .starting_stopping = {
1954 [0] = "Mounting %s...",
1955 [1] = "Unmounting %s...",
1957 .finished_start_job = {
1958 [JOB_DONE] = "Mounted %s.",
1959 [JOB_FAILED] = "Failed to mount %s.",
1960 [JOB_DEPENDENCY] = "Dependency failed for %s.",
1961 [JOB_TIMEOUT] = "Timed out mounting %s.",
1963 .finished_stop_job = {
1964 [JOB_DONE] = "Unmounted %s.",
1965 [JOB_FAILED] = "Failed unmounting %s.",
1966 [JOB_TIMEOUT] = "Timed out unmounting %s.",