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 int mount_add_default_dependencies(Mount *m) {
451 const char *after, *after2, *online;
457 if (UNIT(m)->manager->running_as != SYSTEMD_SYSTEM)
460 p = get_mount_parameters(m);
465 if (path_equal(m->where, "/"))
468 if (mount_is_network(p)) {
469 after = SPECIAL_REMOTE_FS_PRE_TARGET;
470 after2 = SPECIAL_NETWORK_TARGET;
471 online = SPECIAL_NETWORK_ONLINE_TARGET;
473 after = SPECIAL_LOCAL_FS_PRE_TARGET;
478 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after, NULL, true);
483 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after2, NULL, true);
489 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_WANTS, UNIT_AFTER, online, NULL, true);
494 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
501 static int mount_fix_timeouts(Mount *m) {
503 const char *timeout = NULL;
512 p = get_mount_parameters_fragment(m);
516 /* Allow configuration how long we wait for a device that
517 * backs a mount point to show up. This is useful to support
518 * endless device timeouts for devices that show up only after
519 * user input, like crypto devices. */
521 if ((timeout = mount_test_option(p->options, "comment=systemd.device-timeout")))
523 else if ((timeout = mount_test_option(p->options, "x-systemd.device-timeout")))
528 t = strndup(timeout, strcspn(timeout, ",;" WHITESPACE));
532 r = parse_sec(t, &u);
536 log_warning_unit(UNIT(m)->id,
537 "Failed to parse timeout for %s, ignoring: %s",
542 SET_FOREACH(other, UNIT(m)->dependencies[UNIT_AFTER], i) {
543 if (other->type != UNIT_DEVICE)
546 other->job_timeout = u;
552 static int mount_verify(Mount *m) {
557 if (UNIT(m)->load_state != UNIT_LOADED)
560 if (!m->from_fragment && !m->from_proc_self_mountinfo)
563 if (!(e = unit_name_from_path(m->where, ".mount")))
566 b = unit_has_name(UNIT(m), e);
570 log_error_unit(UNIT(m)->id,
571 "%s's Where setting doesn't match unit name. Refusing.",
576 if (mount_point_is_api(m->where) || mount_point_ignore(m->where)) {
577 log_error_unit(UNIT(m)->id,
578 "Cannot create mount unit for API file system %s. Refusing.",
583 if (UNIT(m)->fragment_path && !m->parameters_fragment.what) {
584 log_error_unit(UNIT(m)->id,
585 "%s's What setting is missing. Refusing.", UNIT(m)->id);
589 if (m->exec_context.pam_name && m->kill_context.kill_mode != KILL_CONTROL_GROUP) {
590 log_error_unit(UNIT(m)->id,
591 "%s has PAM enabled. Kill mode must be set to control-group'. Refusing.",
599 static int mount_add_extras(Mount *m) {
603 if (UNIT(m)->fragment_path)
604 m->from_fragment = true;
607 m->where = unit_name_to_path(u->id);
612 path_kill_slashes(m->where);
614 r = unit_add_exec_dependencies(u, &m->exec_context);
618 if (!UNIT(m)->description) {
619 r = unit_set_description(u, m->where);
624 r = mount_add_device_links(m);
628 r = mount_add_mount_links(m);
632 r = mount_add_socket_links(m);
636 r = mount_add_swap_links(m);
640 r = mount_add_path_links(m);
644 r = mount_add_requires_mounts_links(m);
648 r = mount_add_automount_links(m);
652 r = mount_add_quota_links(m);
656 if (UNIT(m)->default_dependencies) {
657 r = mount_add_default_dependencies(m);
662 r = unit_add_default_slice(u);
666 r = mount_fix_timeouts(m);
673 static int mount_load(Unit *u) {
678 assert(u->load_state == UNIT_STUB);
680 if (m->from_proc_self_mountinfo)
681 r = unit_load_fragment_and_dropin_optional(u);
683 r = unit_load_fragment_and_dropin(u);
688 /* This is a new unit? Then let's add in some extras */
689 if (u->load_state == UNIT_LOADED) {
690 r = mount_add_extras(m);
694 r = unit_exec_context_defaults(u, &m->exec_context);
699 return mount_verify(m);
702 static int mount_notify_automount(Mount *m, int status) {
709 SET_FOREACH(p, UNIT(m)->dependencies[UNIT_TRIGGERED_BY], i)
710 if (p->type == UNIT_AUTOMOUNT) {
711 r = automount_send_ready(AUTOMOUNT(p), status);
719 static void mount_set_state(Mount *m, MountState state) {
720 MountState old_state;
723 old_state = m->state;
726 if (state != MOUNT_MOUNTING &&
727 state != MOUNT_MOUNTING_DONE &&
728 state != MOUNT_REMOUNTING &&
729 state != MOUNT_UNMOUNTING &&
730 state != MOUNT_MOUNTING_SIGTERM &&
731 state != MOUNT_MOUNTING_SIGKILL &&
732 state != MOUNT_UNMOUNTING_SIGTERM &&
733 state != MOUNT_UNMOUNTING_SIGKILL &&
734 state != MOUNT_REMOUNTING_SIGTERM &&
735 state != MOUNT_REMOUNTING_SIGKILL) {
736 unit_unwatch_timer(UNIT(m), &m->timer_watch);
737 mount_unwatch_control_pid(m);
738 m->control_command = NULL;
739 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
742 if (state == MOUNT_MOUNTED ||
743 state == MOUNT_REMOUNTING)
744 mount_notify_automount(m, 0);
745 else if (state == MOUNT_DEAD ||
746 state == MOUNT_UNMOUNTING ||
747 state == MOUNT_MOUNTING_SIGTERM ||
748 state == MOUNT_MOUNTING_SIGKILL ||
749 state == MOUNT_REMOUNTING_SIGTERM ||
750 state == MOUNT_REMOUNTING_SIGKILL ||
751 state == MOUNT_UNMOUNTING_SIGTERM ||
752 state == MOUNT_UNMOUNTING_SIGKILL ||
753 state == MOUNT_FAILED) {
754 if (state != old_state)
755 mount_notify_automount(m, -ENODEV);
758 if (state != old_state)
759 log_debug_unit(UNIT(m)->id,
760 "%s changed %s -> %s",
762 mount_state_to_string(old_state),
763 mount_state_to_string(state));
765 unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state], m->reload_result == MOUNT_SUCCESS);
766 m->reload_result = MOUNT_SUCCESS;
769 static int mount_coldplug(Unit *u) {
771 MountState new_state = MOUNT_DEAD;
775 assert(m->state == MOUNT_DEAD);
777 if (m->deserialized_state != m->state)
778 new_state = m->deserialized_state;
779 else if (m->from_proc_self_mountinfo)
780 new_state = MOUNT_MOUNTED;
782 if (new_state != m->state) {
784 if (new_state == MOUNT_MOUNTING ||
785 new_state == MOUNT_MOUNTING_DONE ||
786 new_state == MOUNT_REMOUNTING ||
787 new_state == MOUNT_UNMOUNTING ||
788 new_state == MOUNT_MOUNTING_SIGTERM ||
789 new_state == MOUNT_MOUNTING_SIGKILL ||
790 new_state == MOUNT_UNMOUNTING_SIGTERM ||
791 new_state == MOUNT_UNMOUNTING_SIGKILL ||
792 new_state == MOUNT_REMOUNTING_SIGTERM ||
793 new_state == MOUNT_REMOUNTING_SIGKILL) {
795 if (m->control_pid <= 0)
798 r = unit_watch_pid(UNIT(m), m->control_pid);
802 r = unit_watch_timer(UNIT(m), CLOCK_MONOTONIC, true, m->timeout_usec, &m->timer_watch);
807 mount_set_state(m, new_state);
813 static void mount_dump(Unit *u, FILE *f, const char *prefix) {
820 p = get_mount_parameters(m);
823 "%sMount State: %s\n"
827 "%sFile System Type: %s\n"
829 "%sFrom /proc/self/mountinfo: %s\n"
830 "%sFrom fragment: %s\n"
831 "%sDirectoryMode: %04o\n",
832 prefix, mount_state_to_string(m->state),
833 prefix, mount_result_to_string(m->result),
835 prefix, p ? strna(p->what) : "n/a",
836 prefix, p ? strna(p->fstype) : "n/a",
837 prefix, p ? strna(p->options) : "n/a",
838 prefix, yes_no(m->from_proc_self_mountinfo),
839 prefix, yes_no(m->from_fragment),
840 prefix, m->directory_mode);
842 if (m->control_pid > 0)
844 "%sControl PID: %lu\n",
845 prefix, (unsigned long) m->control_pid);
847 exec_context_dump(&m->exec_context, f, prefix);
848 kill_context_dump(&m->kill_context, f, prefix);
851 static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
859 unit_realize_cgroup(UNIT(m));
861 r = unit_watch_timer(UNIT(m), CLOCK_MONOTONIC, true, m->timeout_usec, &m->timer_watch);
869 UNIT(m)->manager->environment,
873 UNIT(m)->manager->confirm_spawn,
874 UNIT(m)->cgroup_mask,
875 UNIT(m)->cgroup_path,
882 r = unit_watch_pid(UNIT(m), pid);
884 /* FIXME: we need to do something here */
892 unit_unwatch_timer(UNIT(m), &m->timer_watch);
897 static void mount_enter_dead(Mount *m, MountResult f) {
900 if (f != MOUNT_SUCCESS)
903 exec_context_tmp_dirs_done(&m->exec_context);
904 mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD);
907 static void mount_enter_mounted(Mount *m, MountResult f) {
910 if (f != MOUNT_SUCCESS)
913 mount_set_state(m, MOUNT_MOUNTED);
916 static void mount_enter_signal(Mount *m, MountState state, MountResult f) {
921 if (f != MOUNT_SUCCESS)
924 r = unit_kill_context(
927 state != MOUNT_MOUNTING_SIGTERM && state != MOUNT_UNMOUNTING_SIGTERM && state != MOUNT_REMOUNTING_SIGTERM,
935 r = unit_watch_timer(UNIT(m), CLOCK_MONOTONIC, true, m->timeout_usec, &m->timer_watch);
939 mount_set_state(m, state);
940 } else if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
941 mount_enter_mounted(m, MOUNT_SUCCESS);
943 mount_enter_dead(m, MOUNT_SUCCESS);
948 log_warning_unit(UNIT(m)->id,
949 "%s failed to kill processes: %s", UNIT(m)->id, strerror(-r));
951 if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
952 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
954 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
957 void warn_if_dir_nonempty(const char *unit, const char* where) {
961 if (dir_is_empty(where) > 0)
964 log_struct_unit(LOG_NOTICE,
966 "MESSAGE=%s: Directory %s to mount over is not empty, mounting anyway.",
969 MESSAGE_ID(SD_MESSAGE_OVERMOUNTING),
973 static void mount_enter_unmounting(Mount *m) {
978 m->control_command_id = MOUNT_EXEC_UNMOUNT;
979 m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
981 if ((r = exec_command_set(
988 mount_unwatch_control_pid(m);
990 if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
993 mount_set_state(m, MOUNT_UNMOUNTING);
998 log_warning_unit(UNIT(m)->id,
999 "%s failed to run 'umount' task: %s",
1000 UNIT(m)->id, strerror(-r));
1001 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
1004 static void mount_enter_mounting(Mount *m) {
1010 m->control_command_id = MOUNT_EXEC_MOUNT;
1011 m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
1013 mkdir_p_label(m->where, m->directory_mode);
1015 warn_if_dir_nonempty(m->meta.id, m->where);
1017 /* Create the source directory for bind-mounts if needed */
1018 p = get_mount_parameters_fragment(m);
1019 if (p && mount_is_bind(p))
1020 mkdir_p_label(p->what, m->directory_mode);
1022 if (m->from_fragment)
1023 r = exec_command_set(
1026 m->parameters_fragment.what,
1028 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
1029 m->parameters_fragment.options ? "-o" : NULL, m->parameters_fragment.options,
1037 mount_unwatch_control_pid(m);
1039 r = mount_spawn(m, m->control_command, &m->control_pid);
1043 mount_set_state(m, MOUNT_MOUNTING);
1048 log_warning_unit(UNIT(m)->id,
1049 "%s failed to run 'mount' task: %s",
1050 UNIT(m)->id, strerror(-r));
1051 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
1054 static void mount_enter_mounting_done(Mount *m) {
1057 mount_set_state(m, MOUNT_MOUNTING_DONE);
1060 static void mount_enter_remounting(Mount *m) {
1065 m->control_command_id = MOUNT_EXEC_REMOUNT;
1066 m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
1068 if (m->from_fragment) {
1072 if (m->parameters_fragment.options) {
1073 if (!(buf = strappend("remount,", m->parameters_fragment.options))) {
1082 r = exec_command_set(
1085 m->parameters_fragment.what,
1087 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
1098 mount_unwatch_control_pid(m);
1100 if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
1103 mount_set_state(m, MOUNT_REMOUNTING);
1108 log_warning_unit(UNIT(m)->id,
1109 "%s failed to run 'remount' task: %s",
1110 UNIT(m)->id, strerror(-r));
1111 m->reload_result = MOUNT_FAILURE_RESOURCES;
1112 mount_enter_mounted(m, MOUNT_SUCCESS);
1115 static int mount_start(Unit *u) {
1116 Mount *m = MOUNT(u);
1120 /* We cannot fulfill this request right now, try again later
1122 if (m->state == MOUNT_UNMOUNTING ||
1123 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1124 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1125 m->state == MOUNT_MOUNTING_SIGTERM ||
1126 m->state == MOUNT_MOUNTING_SIGKILL)
1129 /* Already on it! */
1130 if (m->state == MOUNT_MOUNTING)
1133 assert(m->state == MOUNT_DEAD || m->state == MOUNT_FAILED);
1135 m->result = MOUNT_SUCCESS;
1136 m->reload_result = MOUNT_SUCCESS;
1138 mount_enter_mounting(m);
1142 static int mount_stop(Unit *u) {
1143 Mount *m = MOUNT(u);
1148 if (m->state == MOUNT_UNMOUNTING ||
1149 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1150 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1151 m->state == MOUNT_MOUNTING_SIGTERM ||
1152 m->state == MOUNT_MOUNTING_SIGKILL)
1155 assert(m->state == MOUNT_MOUNTING ||
1156 m->state == MOUNT_MOUNTING_DONE ||
1157 m->state == MOUNT_MOUNTED ||
1158 m->state == MOUNT_REMOUNTING ||
1159 m->state == MOUNT_REMOUNTING_SIGTERM ||
1160 m->state == MOUNT_REMOUNTING_SIGKILL);
1162 mount_enter_unmounting(m);
1166 static int mount_reload(Unit *u) {
1167 Mount *m = MOUNT(u);
1171 if (m->state == MOUNT_MOUNTING_DONE)
1174 assert(m->state == MOUNT_MOUNTED);
1176 mount_enter_remounting(m);
1180 static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
1181 Mount *m = MOUNT(u);
1187 unit_serialize_item(u, f, "state", mount_state_to_string(m->state));
1188 unit_serialize_item(u, f, "result", mount_result_to_string(m->result));
1189 unit_serialize_item(u, f, "reload-result", mount_result_to_string(m->reload_result));
1191 if (m->control_pid > 0)
1192 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) m->control_pid);
1194 if (m->control_command_id >= 0)
1195 unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(m->control_command_id));
1197 exec_context_serialize(&m->exec_context, UNIT(m), f);
1202 static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1203 Mount *m = MOUNT(u);
1210 if (streq(key, "state")) {
1213 if ((state = mount_state_from_string(value)) < 0)
1214 log_debug_unit(u->id, "Failed to parse state value %s", value);
1216 m->deserialized_state = state;
1217 } else if (streq(key, "result")) {
1220 f = mount_result_from_string(value);
1222 log_debug_unit(UNIT(m)->id,
1223 "Failed to parse result value %s", value);
1224 else if (f != MOUNT_SUCCESS)
1227 } else if (streq(key, "reload-result")) {
1230 f = mount_result_from_string(value);
1232 log_debug_unit(UNIT(m)->id,
1233 "Failed to parse reload result value %s", value);
1234 else if (f != MOUNT_SUCCESS)
1235 m->reload_result = f;
1237 } else if (streq(key, "control-pid")) {
1240 if (parse_pid(value, &pid) < 0)
1241 log_debug_unit(UNIT(m)->id,
1242 "Failed to parse control-pid value %s", value);
1244 m->control_pid = pid;
1245 } else if (streq(key, "control-command")) {
1246 MountExecCommand id;
1248 if ((id = mount_exec_command_from_string(value)) < 0)
1249 log_debug_unit(UNIT(m)->id,
1250 "Failed to parse exec-command value %s", value);
1252 m->control_command_id = id;
1253 m->control_command = m->exec_command + id;
1255 } else if (streq(key, "tmp-dir")) {
1262 m->exec_context.tmp_dir = t;
1263 } else if (streq(key, "var-tmp-dir")) {
1270 m->exec_context.var_tmp_dir = t;
1272 log_debug_unit(UNIT(m)->id,
1273 "Unknown serialization key '%s'", key);
1278 _pure_ static UnitActiveState mount_active_state(Unit *u) {
1281 return state_translation_table[MOUNT(u)->state];
1284 _pure_ static const char *mount_sub_state_to_string(Unit *u) {
1287 return mount_state_to_string(MOUNT(u)->state);
1290 _pure_ static bool mount_check_gc(Unit *u) {
1291 Mount *m = MOUNT(u);
1295 return m->from_proc_self_mountinfo;
1298 static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1299 Mount *m = MOUNT(u);
1305 if (pid != m->control_pid)
1310 if (is_clean_exit(code, status, NULL))
1312 else if (code == CLD_EXITED)
1313 f = MOUNT_FAILURE_EXIT_CODE;
1314 else if (code == CLD_KILLED)
1315 f = MOUNT_FAILURE_SIGNAL;
1316 else if (code == CLD_DUMPED)
1317 f = MOUNT_FAILURE_CORE_DUMP;
1319 assert_not_reached("Unknown code");
1321 if (f != MOUNT_SUCCESS)
1324 if (m->control_command) {
1325 exec_status_exit(&m->control_command->exec_status, &m->exec_context, pid, code, status);
1327 m->control_command = NULL;
1328 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
1331 log_full_unit(f == MOUNT_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
1332 "%s mount process exited, code=%s status=%i",
1333 u->id, sigchld_code_to_string(code), status);
1335 /* Note that mount(8) returning and the kernel sending us a
1336 * mount table change event might happen out-of-order. If an
1337 * operation succeed we assume the kernel will follow soon too
1338 * and already change into the resulting state. If it fails
1339 * we check if the kernel still knows about the mount. and
1340 * change state accordingly. */
1344 case MOUNT_MOUNTING:
1345 case MOUNT_MOUNTING_DONE:
1346 case MOUNT_MOUNTING_SIGKILL:
1347 case MOUNT_MOUNTING_SIGTERM:
1349 if (f == MOUNT_SUCCESS)
1350 mount_enter_mounted(m, f);
1351 else if (m->from_proc_self_mountinfo)
1352 mount_enter_mounted(m, f);
1354 mount_enter_dead(m, f);
1357 case MOUNT_REMOUNTING:
1358 case MOUNT_REMOUNTING_SIGKILL:
1359 case MOUNT_REMOUNTING_SIGTERM:
1361 m->reload_result = f;
1362 if (m->from_proc_self_mountinfo)
1363 mount_enter_mounted(m, MOUNT_SUCCESS);
1365 mount_enter_dead(m, MOUNT_SUCCESS);
1369 case MOUNT_UNMOUNTING:
1370 case MOUNT_UNMOUNTING_SIGKILL:
1371 case MOUNT_UNMOUNTING_SIGTERM:
1373 if (f == MOUNT_SUCCESS)
1374 mount_enter_dead(m, f);
1375 else if (m->from_proc_self_mountinfo)
1376 mount_enter_mounted(m, f);
1378 mount_enter_dead(m, f);
1382 assert_not_reached("Uh, control process died at wrong time.");
1385 /* Notify clients about changed exit status */
1386 unit_add_to_dbus_queue(u);
1389 static void mount_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
1390 Mount *m = MOUNT(u);
1393 assert(elapsed == 1);
1394 assert(w == &m->timer_watch);
1398 case MOUNT_MOUNTING:
1399 case MOUNT_MOUNTING_DONE:
1400 log_warning_unit(u->id,
1401 "%s mounting timed out. Stopping.", u->id);
1402 mount_enter_signal(m, MOUNT_MOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1405 case MOUNT_REMOUNTING:
1406 log_warning_unit(u->id,
1407 "%s remounting timed out. Stopping.", u->id);
1408 m->reload_result = MOUNT_FAILURE_TIMEOUT;
1409 mount_enter_mounted(m, MOUNT_SUCCESS);
1412 case MOUNT_UNMOUNTING:
1413 log_warning_unit(u->id,
1414 "%s unmounting timed out. Stopping.", u->id);
1415 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1418 case MOUNT_MOUNTING_SIGTERM:
1419 if (m->kill_context.send_sigkill) {
1420 log_warning_unit(u->id,
1421 "%s mounting timed out. Killing.", u->id);
1422 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1424 log_warning_unit(u->id,
1425 "%s mounting timed out. Skipping SIGKILL. Ignoring.",
1428 if (m->from_proc_self_mountinfo)
1429 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1431 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1435 case MOUNT_REMOUNTING_SIGTERM:
1436 if (m->kill_context.send_sigkill) {
1437 log_warning_unit(u->id,
1438 "%s remounting timed out. Killing.", u->id);
1439 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1441 log_warning_unit(u->id,
1442 "%s remounting 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_UNMOUNTING_SIGTERM:
1453 if (m->kill_context.send_sigkill) {
1454 log_warning_unit(u->id,
1455 "%s unmounting timed out. Killing.", u->id);
1456 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1458 log_warning_unit(u->id,
1459 "%s unmounting 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_MOUNTING_SIGKILL:
1470 case MOUNT_REMOUNTING_SIGKILL:
1471 case MOUNT_UNMOUNTING_SIGKILL:
1472 log_warning_unit(u->id,
1473 "%s mount process still around after SIGKILL. Ignoring.",
1476 if (m->from_proc_self_mountinfo)
1477 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1479 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1483 assert_not_reached("Timeout at wrong time.");
1487 static int mount_add_one(
1491 const char *options,
1498 char *e, *w = NULL, *o = NULL, *f = NULL;
1500 bool load_extras = false;
1508 /* Ignore API mount points. They should never be referenced in
1509 * dependencies ever. */
1510 if (mount_point_is_api(where) || mount_point_ignore(where))
1513 if (streq(fstype, "autofs"))
1516 /* probably some kind of swap, ignore */
1517 if (!is_path(where))
1520 e = unit_name_from_path(where, ".mount");
1524 u = manager_get_unit(m, e);
1528 u = unit_new(m, sizeof(Mount));
1534 r = unit_add_name(u, e);
1540 MOUNT(u)->where = strdup(where);
1541 if (!MOUNT(u)->where) {
1546 u->source_path = strdup("/proc/self/mountinfo");
1547 if (!u->source_path) {
1552 r = unit_add_dependency_by_name(u, UNIT_BEFORE, SPECIAL_LOCAL_FS_TARGET, NULL, true);
1556 if (!path_equal(where, "/") &&
1557 !path_equal(where, "/usr")) {
1558 r = unit_add_dependency_by_name(u, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
1563 unit_add_to_load_queue(u);
1568 if (!MOUNT(u)->where) {
1569 MOUNT(u)->where = strdup(where);
1570 if (!MOUNT(u)->where) {
1576 if (u->load_state == UNIT_NOT_FOUND) {
1577 u->load_state = UNIT_LOADED;
1580 /* Load in the extras later on, after we
1581 * finished initialization of the unit */
1586 if (!(w = strdup(what)) ||
1587 !(o = strdup(options)) ||
1588 !(f = strdup(fstype))) {
1593 p = &MOUNT(u)->parameters_proc_self_mountinfo;
1595 MOUNT(u)->is_mounted = true;
1596 MOUNT(u)->just_mounted = !MOUNT(u)->from_proc_self_mountinfo;
1597 MOUNT(u)->just_changed = !streq_ptr(p->options, o);
1600 MOUNT(u)->from_proc_self_mountinfo = true;
1614 r = mount_add_extras(MOUNT(u));
1619 unit_add_to_dbus_queue(u);
1634 static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
1637 char *device, *path, *options, *options2, *fstype, *d, *p, *o;
1641 rewind(m->proc_self_mountinfo);
1646 device = path = options = options2 = fstype = d = p = o = NULL;
1648 if ((k = fscanf(m->proc_self_mountinfo,
1649 "%*s " /* (1) mount id */
1650 "%*s " /* (2) parent id */
1651 "%*s " /* (3) major:minor */
1652 "%*s " /* (4) root */
1653 "%ms " /* (5) mount point */
1654 "%ms" /* (6) mount options */
1655 "%*[^-]" /* (7) optional fields */
1656 "- " /* (8) separator */
1657 "%ms " /* (9) file system type */
1658 "%ms" /* (10) mount source */
1659 "%ms" /* (11) mount options 2 */
1660 "%*[^\n]", /* some rubbish at the end */
1670 log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
1674 o = strjoin(options, ",", options2, NULL);
1680 if (!(d = cunescape(device)) ||
1681 !(p = cunescape(path))) {
1686 if ((k = mount_add_one(m, d, p, o, fstype, 0, set_flags)) < 0)
1713 static void mount_shutdown(Manager *m) {
1716 if (m->proc_self_mountinfo) {
1717 fclose(m->proc_self_mountinfo);
1718 m->proc_self_mountinfo = NULL;
1722 static int mount_enumerate(Manager *m) {
1726 if (!m->proc_self_mountinfo) {
1727 struct epoll_event ev = {
1729 .data.ptr = &m->mount_watch,
1732 m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
1733 if (!m->proc_self_mountinfo)
1736 m->mount_watch.type = WATCH_MOUNT;
1737 m->mount_watch.fd = fileno(m->proc_self_mountinfo);
1739 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->mount_watch.fd, &ev) < 0)
1743 r = mount_load_proc_self_mountinfo(m, false);
1754 void mount_fd_event(Manager *m, int events) {
1759 assert(events & EPOLLPRI);
1761 /* The manager calls this for every fd event happening on the
1762 * /proc/self/mountinfo file, which informs us about mounting
1765 r = mount_load_proc_self_mountinfo(m, true);
1767 log_error("Failed to reread /proc/self/mountinfo: %s", strerror(-r));
1769 /* Reset flags, just in case, for later calls */
1770 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1771 Mount *mount = MOUNT(u);
1773 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1779 manager_dispatch_load_queue(m);
1781 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1782 Mount *mount = MOUNT(u);
1784 if (!mount->is_mounted) {
1785 /* This has just been unmounted. */
1787 mount->from_proc_self_mountinfo = false;
1789 switch (mount->state) {
1792 mount_enter_dead(mount, MOUNT_SUCCESS);
1796 mount_set_state(mount, mount->state);
1801 } else if (mount->just_mounted || mount->just_changed) {
1803 /* New or changed mount entry */
1805 switch (mount->state) {
1809 mount_enter_mounted(mount, MOUNT_SUCCESS);
1812 case MOUNT_MOUNTING:
1813 mount_enter_mounting_done(mount);
1817 /* Nothing really changed, but let's
1818 * issue an notification call
1819 * nonetheless, in case somebody is
1820 * waiting for this. (e.g. file system
1821 * ro/rw remounts.) */
1822 mount_set_state(mount, mount->state);
1827 /* Reset the flags for later calls */
1828 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1832 static void mount_reset_failed(Unit *u) {
1833 Mount *m = MOUNT(u);
1837 if (m->state == MOUNT_FAILED)
1838 mount_set_state(m, MOUNT_DEAD);
1840 m->result = MOUNT_SUCCESS;
1841 m->reload_result = MOUNT_SUCCESS;
1844 static int mount_kill(Unit *u, KillWho who, int signo, DBusError *error) {
1845 return unit_kill_common(u, who, signo, -1, MOUNT(u)->control_pid, error);
1848 static const char* const mount_state_table[_MOUNT_STATE_MAX] = {
1849 [MOUNT_DEAD] = "dead",
1850 [MOUNT_MOUNTING] = "mounting",
1851 [MOUNT_MOUNTING_DONE] = "mounting-done",
1852 [MOUNT_MOUNTED] = "mounted",
1853 [MOUNT_REMOUNTING] = "remounting",
1854 [MOUNT_UNMOUNTING] = "unmounting",
1855 [MOUNT_MOUNTING_SIGTERM] = "mounting-sigterm",
1856 [MOUNT_MOUNTING_SIGKILL] = "mounting-sigkill",
1857 [MOUNT_REMOUNTING_SIGTERM] = "remounting-sigterm",
1858 [MOUNT_REMOUNTING_SIGKILL] = "remounting-sigkill",
1859 [MOUNT_UNMOUNTING_SIGTERM] = "unmounting-sigterm",
1860 [MOUNT_UNMOUNTING_SIGKILL] = "unmounting-sigkill",
1861 [MOUNT_FAILED] = "failed"
1864 DEFINE_STRING_TABLE_LOOKUP(mount_state, MountState);
1866 static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
1867 [MOUNT_EXEC_MOUNT] = "ExecMount",
1868 [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
1869 [MOUNT_EXEC_REMOUNT] = "ExecRemount",
1872 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
1874 static const char* const mount_result_table[_MOUNT_RESULT_MAX] = {
1875 [MOUNT_SUCCESS] = "success",
1876 [MOUNT_FAILURE_RESOURCES] = "resources",
1877 [MOUNT_FAILURE_TIMEOUT] = "timeout",
1878 [MOUNT_FAILURE_EXIT_CODE] = "exit-code",
1879 [MOUNT_FAILURE_SIGNAL] = "signal",
1880 [MOUNT_FAILURE_CORE_DUMP] = "core-dump"
1883 DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult);
1885 const UnitVTable mount_vtable = {
1886 .object_size = sizeof(Mount),
1893 .private_section = "Mount",
1894 .exec_context_offset = offsetof(Mount, exec_context),
1895 .cgroup_context_offset = offsetof(Mount, cgroup_context),
1898 .no_instances = true,
1904 .coldplug = mount_coldplug,
1908 .start = mount_start,
1910 .reload = mount_reload,
1914 .serialize = mount_serialize,
1915 .deserialize_item = mount_deserialize_item,
1917 .active_state = mount_active_state,
1918 .sub_state_to_string = mount_sub_state_to_string,
1920 .check_gc = mount_check_gc,
1922 .sigchld_event = mount_sigchld_event,
1923 .timer_event = mount_timer_event,
1925 .reset_failed = mount_reset_failed,
1927 .bus_interface = "org.freedesktop.systemd1.Mount",
1928 .bus_message_handler = bus_mount_message_handler,
1929 .bus_invalidating_properties = bus_mount_invalidating_properties,
1930 .bus_set_property = bus_mount_set_property,
1931 .bus_commit_properties = bus_mount_commit_properties,
1933 .enumerate = mount_enumerate,
1934 .shutdown = mount_shutdown,
1936 .status_message_formats = {
1937 .starting_stopping = {
1938 [0] = "Mounting %s...",
1939 [1] = "Unmounting %s...",
1941 .finished_start_job = {
1942 [JOB_DONE] = "Mounted %s.",
1943 [JOB_FAILED] = "Failed to mount %s.",
1944 [JOB_DEPENDENCY] = "Dependency failed for %s.",
1945 [JOB_TIMEOUT] = "Timed out mounting %s.",
1947 .finished_stop_job = {
1948 [JOB_DONE] = "Unmounted %s.",
1949 [JOB_FAILED] = "Failed unmounting %s.",
1950 [JOB_TIMEOUT] = "Timed out unmounting %s.",