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>
30 #include "load-fragment.h"
31 #include "load-dropin.h"
33 #include "sd-messages.h"
36 #include "path-util.h"
37 #include "mount-setup.h"
38 #include "unit-name.h"
39 #include "dbus-mount.h"
41 #include "bus-errors.h"
42 #include "exit-status.h"
45 static const UnitActiveState state_translation_table[_MOUNT_STATE_MAX] = {
46 [MOUNT_DEAD] = UNIT_INACTIVE,
47 [MOUNT_MOUNTING] = UNIT_ACTIVATING,
48 [MOUNT_MOUNTING_DONE] = UNIT_ACTIVE,
49 [MOUNT_MOUNTED] = UNIT_ACTIVE,
50 [MOUNT_REMOUNTING] = UNIT_RELOADING,
51 [MOUNT_UNMOUNTING] = UNIT_DEACTIVATING,
52 [MOUNT_MOUNTING_SIGTERM] = UNIT_DEACTIVATING,
53 [MOUNT_MOUNTING_SIGKILL] = UNIT_DEACTIVATING,
54 [MOUNT_REMOUNTING_SIGTERM] = UNIT_RELOADING,
55 [MOUNT_REMOUNTING_SIGKILL] = UNIT_RELOADING,
56 [MOUNT_UNMOUNTING_SIGTERM] = UNIT_DEACTIVATING,
57 [MOUNT_UNMOUNTING_SIGKILL] = UNIT_DEACTIVATING,
58 [MOUNT_FAILED] = UNIT_FAILED
61 static void mount_init(Unit *u) {
65 assert(u->load_state == UNIT_STUB);
67 m->timeout_usec = DEFAULT_TIMEOUT_USEC;
68 m->directory_mode = 0755;
70 exec_context_init(&m->exec_context);
72 if (unit_has_name(u, "-.mount")) {
73 /* Don't allow start/stop for root directory */
74 UNIT(m)->refuse_manual_start = true;
75 UNIT(m)->refuse_manual_stop = true;
77 /* The stdio/kmsg bridge socket is on /, in order to avoid a
78 * dep loop, don't use kmsg logging for -.mount */
79 m->exec_context.std_output = u->manager->default_std_output;
80 m->exec_context.std_error = u->manager->default_std_error;
83 kill_context_init(&m->kill_context);
85 /* We need to make sure that /bin/mount is always called in
86 * the same process group as us, so that the autofs kernel
87 * side doesn't send us another mount request while we are
88 * already trying to comply its last one. */
89 m->exec_context.same_pgrp = true;
91 m->timer_watch.type = WATCH_INVALID;
93 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
95 UNIT(m)->ignore_on_isolate = true;
98 static void mount_unwatch_control_pid(Mount *m) {
101 if (m->control_pid <= 0)
104 unit_unwatch_pid(UNIT(m), m->control_pid);
108 static void mount_parameters_done(MountParameters *p) {
115 p->what = p->options = p->fstype = NULL;
118 static void mount_done(Unit *u) {
126 mount_parameters_done(&m->parameters_proc_self_mountinfo);
127 mount_parameters_done(&m->parameters_fragment);
129 exec_context_done(&m->exec_context);
130 exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
131 m->control_command = NULL;
133 mount_unwatch_control_pid(m);
135 unit_unwatch_timer(u, &m->timer_watch);
138 static MountParameters* get_mount_parameters_fragment(Mount *m) {
141 if (m->from_fragment)
142 return &m->parameters_fragment;
147 static MountParameters* get_mount_parameters(Mount *m) {
150 if (m->from_proc_self_mountinfo)
151 return &m->parameters_proc_self_mountinfo;
153 return get_mount_parameters_fragment(m);
156 static int mount_add_mount_links(Mount *m) {
163 pm = get_mount_parameters_fragment(m);
165 /* Adds in links to other mount points that might lie below or
166 * above us in the hierarchy */
168 LIST_FOREACH(units_by_type, other, UNIT(m)->manager->units_by_type[UNIT_MOUNT]) {
169 Mount *n = MOUNT(other);
175 if (UNIT(n)->load_state != UNIT_LOADED)
178 pn = get_mount_parameters_fragment(n);
180 if (path_startswith(m->where, n->where)) {
182 if ((r = unit_add_dependency(UNIT(m), UNIT_AFTER, UNIT(n), true)) < 0)
186 if ((r = unit_add_dependency(UNIT(m), UNIT_REQUIRES, UNIT(n), true)) < 0)
189 } else if (path_startswith(n->where, m->where)) {
191 if ((r = unit_add_dependency(UNIT(n), UNIT_AFTER, UNIT(m), true)) < 0)
195 if ((r = unit_add_dependency(UNIT(n), UNIT_REQUIRES, UNIT(m), true)) < 0)
198 } else if (pm && pm->what && path_startswith(pm->what, n->where)) {
200 if ((r = unit_add_dependency(UNIT(m), UNIT_AFTER, UNIT(n), true)) < 0)
203 if ((r = unit_add_dependency(UNIT(m), UNIT_REQUIRES, UNIT(n), true)) < 0)
206 } else if (pn && pn->what && path_startswith(pn->what, m->where)) {
208 if ((r = unit_add_dependency(UNIT(n), UNIT_AFTER, UNIT(m), true)) < 0)
211 if ((r = unit_add_dependency(UNIT(n), UNIT_REQUIRES, UNIT(m), true)) < 0)
219 static int mount_add_swap_links(Mount *m) {
225 LIST_FOREACH(units_by_type, other, UNIT(m)->manager->units_by_type[UNIT_SWAP]) {
226 r = swap_add_one_mount_link(SWAP(other), m);
234 static int mount_add_path_links(Mount *m) {
240 LIST_FOREACH(units_by_type, other, UNIT(m)->manager->units_by_type[UNIT_PATH]) {
241 r = path_add_one_mount_link(PATH(other), m);
249 static int mount_add_automount_links(Mount *m) {
255 LIST_FOREACH(units_by_type, other, UNIT(m)->manager->units_by_type[UNIT_AUTOMOUNT]) {
256 r = automount_add_one_mount_link(AUTOMOUNT(other), m);
264 static int mount_add_socket_links(Mount *m) {
270 LIST_FOREACH(units_by_type, other, UNIT(m)->manager->units_by_type[UNIT_SOCKET]) {
271 r = socket_add_one_mount_link(SOCKET(other), m);
279 static int mount_add_requires_mounts_links(Mount *m) {
285 LIST_FOREACH(has_requires_mounts_for, other, UNIT(m)->manager->has_requires_mounts_for) {
286 r = unit_add_one_mount_link(other, m);
294 static char* mount_test_option(const char *haystack, const char *needle) {
299 /* Like glibc's hasmntopt(), but works on a string, not a
306 me.mnt_opts = (char*) haystack;
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"))
335 static bool needs_quota(MountParameters *p) {
338 if (mount_is_network(p))
341 if (mount_is_bind(p))
344 return mount_test_option(p->options, "usrquota") ||
345 mount_test_option(p->options, "grpquota") ||
346 mount_test_option(p->options, "quota") ||
347 mount_test_option(p->options, "usrjquota") ||
348 mount_test_option(p->options, "grpjquota");
351 static int mount_add_device_links(Mount *m) {
357 p = get_mount_parameters_fragment(m);
364 if (mount_is_bind(p))
367 if (!is_device_path(p->what))
370 if (path_equal(m->where, "/"))
373 r = unit_add_node_link(UNIT(m), p->what, false);
378 UNIT(m)->manager->running_as == SYSTEMD_SYSTEM) {
381 /* Let's add in the fsck service */
383 /* aka SPECIAL_FSCK_SERVICE */
384 name = unit_name_from_path_instance("systemd-fsck", p->what, ".service");
388 r = manager_load_unit_prepare(UNIT(m)->manager, name, NULL, NULL, &fsck);
390 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
396 SERVICE(fsck)->fsck_passno = p->passno;
398 r = unit_add_two_dependencies(UNIT(m), UNIT_AFTER, UNIT_REQUIRES, fsck, true);
406 static int mount_add_quota_links(Mount *m) {
412 if (UNIT(m)->manager->running_as != SYSTEMD_SYSTEM)
415 p = get_mount_parameters_fragment(m);
422 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTACHECK_SERVICE, NULL, true);
426 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTAON_SERVICE, NULL, true);
433 static int mount_add_default_dependencies(Mount *m) {
440 if (UNIT(m)->manager->running_as != SYSTEMD_SYSTEM)
443 p = get_mount_parameters_fragment(m);
447 if (path_equal(m->where, "/"))
450 if (mount_is_network(p))
451 after = SPECIAL_REMOTE_FS_PRE_TARGET;
453 after = SPECIAL_LOCAL_FS_PRE_TARGET;
455 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_WANTS, UNIT_AFTER, after, NULL, true);
459 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
466 static int mount_fix_timeouts(Mount *m) {
468 const char *timeout = NULL;
477 p = get_mount_parameters_fragment(m);
481 /* Allow configuration how long we wait for a device that
482 * backs a mount point to show up. This is useful to support
483 * endless device timeouts for devices that show up only after
484 * user input, like crypto devices. */
486 if ((timeout = mount_test_option(p->options, "comment=systemd.device-timeout")))
488 else if ((timeout = mount_test_option(p->options, "x-systemd.device-timeout")))
493 t = strndup(timeout, strcspn(timeout, ",;" WHITESPACE));
497 r = parse_usec(t, &u);
501 log_warning("Failed to parse timeout for %s, ignoring: %s", m->where, timeout);
505 SET_FOREACH(other, UNIT(m)->dependencies[UNIT_AFTER], i) {
506 if (other->type != UNIT_DEVICE)
509 other->job_timeout = u;
515 static int mount_verify(Mount *m) {
520 if (UNIT(m)->load_state != UNIT_LOADED)
523 if (!m->from_fragment && !m->from_proc_self_mountinfo)
526 if (!(e = unit_name_from_path(m->where, ".mount")))
529 b = unit_has_name(UNIT(m), e);
533 log_error("%s's Where setting doesn't match unit name. Refusing.", UNIT(m)->id);
537 if (mount_point_is_api(m->where) || mount_point_ignore(m->where)) {
538 log_error("Cannot create mount unit for API file system %s. Refusing.", m->where);
542 if (UNIT(m)->fragment_path && !m->parameters_fragment.what) {
543 log_error("%s's What setting is missing. Refusing.", UNIT(m)->id);
547 if (m->exec_context.pam_name && m->kill_context.kill_mode != KILL_CONTROL_GROUP) {
548 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(m)->id);
555 static int mount_add_extras(Mount *m) {
559 if (UNIT(m)->fragment_path)
560 m->from_fragment = true;
563 m->where = unit_name_to_path(u->id);
568 path_kill_slashes(m->where);
570 r = unit_add_exec_dependencies(u, &m->exec_context);
574 if (!UNIT(m)->description) {
575 r = unit_set_description(u, m->where);
580 r = mount_add_device_links(m);
584 r = mount_add_mount_links(m);
588 r = mount_add_socket_links(m);
592 r = mount_add_swap_links(m);
596 r = mount_add_path_links(m);
600 r = mount_add_requires_mounts_links(m);
604 r = mount_add_automount_links(m);
608 r = mount_add_quota_links(m);
612 if (UNIT(m)->default_dependencies) {
613 r = mount_add_default_dependencies(m);
618 r = unit_add_default_cgroups(u);
622 r = mount_fix_timeouts(m);
629 static int mount_load(Unit *u) {
634 assert(u->load_state == UNIT_STUB);
636 if (m->from_proc_self_mountinfo)
637 r = unit_load_fragment_and_dropin_optional(u);
639 r = unit_load_fragment_and_dropin(u);
644 /* This is a new unit? Then let's add in some extras */
645 if (u->load_state == UNIT_LOADED) {
646 r = mount_add_extras(m);
650 r = unit_exec_context_defaults(u, &m->exec_context);
655 return mount_verify(m);
658 static int mount_notify_automount(Mount *m, int status) {
665 SET_FOREACH(p, UNIT(m)->dependencies[UNIT_TRIGGERED_BY], i)
666 if (p->type == UNIT_AUTOMOUNT) {
667 r = automount_send_ready(AUTOMOUNT(p), status);
675 static void mount_set_state(Mount *m, MountState state) {
676 MountState old_state;
679 old_state = m->state;
682 if (state != MOUNT_MOUNTING &&
683 state != MOUNT_MOUNTING_DONE &&
684 state != MOUNT_REMOUNTING &&
685 state != MOUNT_UNMOUNTING &&
686 state != MOUNT_MOUNTING_SIGTERM &&
687 state != MOUNT_MOUNTING_SIGKILL &&
688 state != MOUNT_UNMOUNTING_SIGTERM &&
689 state != MOUNT_UNMOUNTING_SIGKILL &&
690 state != MOUNT_REMOUNTING_SIGTERM &&
691 state != MOUNT_REMOUNTING_SIGKILL) {
692 unit_unwatch_timer(UNIT(m), &m->timer_watch);
693 mount_unwatch_control_pid(m);
694 m->control_command = NULL;
695 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
698 if (state == MOUNT_MOUNTED ||
699 state == MOUNT_REMOUNTING)
700 mount_notify_automount(m, 0);
701 else if (state == MOUNT_DEAD ||
702 state == MOUNT_UNMOUNTING ||
703 state == MOUNT_MOUNTING_SIGTERM ||
704 state == MOUNT_MOUNTING_SIGKILL ||
705 state == MOUNT_REMOUNTING_SIGTERM ||
706 state == MOUNT_REMOUNTING_SIGKILL ||
707 state == MOUNT_UNMOUNTING_SIGTERM ||
708 state == MOUNT_UNMOUNTING_SIGKILL ||
709 state == MOUNT_FAILED) {
710 if (state != old_state)
711 mount_notify_automount(m, -ENODEV);
714 if (state != old_state)
715 log_debug("%s changed %s -> %s",
717 mount_state_to_string(old_state),
718 mount_state_to_string(state));
720 unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state], m->reload_result == MOUNT_SUCCESS);
721 m->reload_result = MOUNT_SUCCESS;
724 static int mount_coldplug(Unit *u) {
726 MountState new_state = MOUNT_DEAD;
730 assert(m->state == MOUNT_DEAD);
732 if (m->deserialized_state != m->state)
733 new_state = m->deserialized_state;
734 else if (m->from_proc_self_mountinfo)
735 new_state = MOUNT_MOUNTED;
737 if (new_state != m->state) {
739 if (new_state == MOUNT_MOUNTING ||
740 new_state == MOUNT_MOUNTING_DONE ||
741 new_state == MOUNT_REMOUNTING ||
742 new_state == MOUNT_UNMOUNTING ||
743 new_state == MOUNT_MOUNTING_SIGTERM ||
744 new_state == MOUNT_MOUNTING_SIGKILL ||
745 new_state == MOUNT_UNMOUNTING_SIGTERM ||
746 new_state == MOUNT_UNMOUNTING_SIGKILL ||
747 new_state == MOUNT_REMOUNTING_SIGTERM ||
748 new_state == MOUNT_REMOUNTING_SIGKILL) {
750 if (m->control_pid <= 0)
753 r = unit_watch_pid(UNIT(m), m->control_pid);
757 r = unit_watch_timer(UNIT(m), CLOCK_MONOTONIC, true, m->timeout_usec, &m->timer_watch);
762 mount_set_state(m, new_state);
768 static void mount_dump(Unit *u, FILE *f, const char *prefix) {
775 p = get_mount_parameters(m);
778 "%sMount State: %s\n"
782 "%sFile System Type: %s\n"
784 "%sFrom /proc/self/mountinfo: %s\n"
785 "%sFrom fragment: %s\n"
786 "%sDirectoryMode: %04o\n",
787 prefix, mount_state_to_string(m->state),
788 prefix, mount_result_to_string(m->result),
790 prefix, strna(p->what),
791 prefix, strna(p->fstype),
792 prefix, strna(p->options),
793 prefix, yes_no(m->from_proc_self_mountinfo),
794 prefix, yes_no(m->from_fragment),
795 prefix, m->directory_mode);
797 if (m->control_pid > 0)
799 "%sControl PID: %lu\n",
800 prefix, (unsigned long) m->control_pid);
802 exec_context_dump(&m->exec_context, f, prefix);
803 kill_context_dump(&m->kill_context, f, prefix);
806 static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
814 r = unit_watch_timer(UNIT(m), CLOCK_MONOTONIC, true, m->timeout_usec, &m->timer_watch);
818 if ((r = exec_spawn(c,
822 UNIT(m)->manager->environment,
826 UNIT(m)->manager->confirm_spawn,
827 UNIT(m)->cgroup_bondings,
828 UNIT(m)->cgroup_attributes,
835 if ((r = unit_watch_pid(UNIT(m), pid)) < 0)
836 /* FIXME: we need to do something here */
844 unit_unwatch_timer(UNIT(m), &m->timer_watch);
849 static void mount_enter_dead(Mount *m, MountResult f) {
852 if (f != MOUNT_SUCCESS)
855 mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD);
858 static void mount_enter_mounted(Mount *m, MountResult f) {
861 if (f != MOUNT_SUCCESS)
864 mount_set_state(m, MOUNT_MOUNTED);
867 static void mount_enter_signal(Mount *m, MountState state, MountResult f) {
870 bool wait_for_exit = false;
874 if (f != MOUNT_SUCCESS)
877 if (m->kill_context.kill_mode != KILL_NONE) {
878 int sig = (state == MOUNT_MOUNTING_SIGTERM ||
879 state == MOUNT_UNMOUNTING_SIGTERM ||
880 state == MOUNT_REMOUNTING_SIGTERM) ? m->kill_context.kill_signal : SIGKILL;
882 if (m->control_pid > 0) {
883 if (kill_and_sigcont(m->control_pid, sig) < 0 && errno != ESRCH)
885 log_warning("Failed to kill control process %li: %m", (long) m->control_pid);
887 wait_for_exit = true;
890 if (m->kill_context.kill_mode == KILL_CONTROL_GROUP) {
892 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
897 /* Exclude the control pid from being killed via the cgroup */
898 if (m->control_pid > 0)
899 if ((r = set_put(pid_set, LONG_TO_PTR(m->control_pid))) < 0)
902 r = cgroup_bonding_kill_list(UNIT(m)->cgroup_bondings, sig, true, false, pid_set, NULL);
904 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
905 log_warning("Failed to kill control group: %s", strerror(-r));
907 wait_for_exit = true;
915 r = unit_watch_timer(UNIT(m), CLOCK_MONOTONIC, true, m->timeout_usec, &m->timer_watch);
919 mount_set_state(m, state);
920 } else if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
921 mount_enter_mounted(m, MOUNT_SUCCESS);
923 mount_enter_dead(m, MOUNT_SUCCESS);
928 log_warning("%s failed to kill processes: %s", UNIT(m)->id, strerror(-r));
930 if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
931 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
933 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
939 void warn_if_dir_nonempty(const char *unit, const char* where) {
940 if (dir_is_empty(where) > 0)
942 log_struct(LOG_NOTICE,
943 "MESSAGE=%s: Directory %s to mount over is not empty, mounting anyway.",
946 "_SYSTEMD_UNIT=%s", unit,
947 MESSAGE_ID(SD_MESSAGE_OVERMOUNTING),
951 static void mount_enter_unmounting(Mount *m) {
956 m->control_command_id = MOUNT_EXEC_UNMOUNT;
957 m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
959 if ((r = exec_command_set(
966 mount_unwatch_control_pid(m);
968 if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
971 mount_set_state(m, MOUNT_UNMOUNTING);
976 log_warning("%s failed to run 'umount' task: %s", UNIT(m)->id, strerror(-r));
977 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
980 static void mount_enter_mounting(Mount *m) {
986 m->control_command_id = MOUNT_EXEC_MOUNT;
987 m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
989 mkdir_p_label(m->where, m->directory_mode);
991 warn_if_dir_nonempty(m->meta.id, m->where);
993 /* Create the source directory for bind-mounts if needed */
994 p = get_mount_parameters_fragment(m);
995 if (p && mount_is_bind(p))
996 mkdir_p_label(p->what, m->directory_mode);
998 if (m->from_fragment)
999 r = exec_command_set(
1002 m->parameters_fragment.what,
1004 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
1005 m->parameters_fragment.options ? "-o" : NULL, m->parameters_fragment.options,
1013 mount_unwatch_control_pid(m);
1015 r = mount_spawn(m, m->control_command, &m->control_pid);
1019 mount_set_state(m, MOUNT_MOUNTING);
1024 log_warning("%s failed to run 'mount' task: %s", UNIT(m)->id, strerror(-r));
1025 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
1028 static void mount_enter_mounting_done(Mount *m) {
1031 mount_set_state(m, MOUNT_MOUNTING_DONE);
1034 static void mount_enter_remounting(Mount *m) {
1039 m->control_command_id = MOUNT_EXEC_REMOUNT;
1040 m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
1042 if (m->from_fragment) {
1046 if (m->parameters_fragment.options) {
1047 if (!(buf = strappend("remount,", m->parameters_fragment.options))) {
1056 r = exec_command_set(
1059 m->parameters_fragment.what,
1061 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
1072 mount_unwatch_control_pid(m);
1074 if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
1077 mount_set_state(m, MOUNT_REMOUNTING);
1082 log_warning("%s failed to run 'remount' task: %s", UNIT(m)->id, strerror(-r));
1083 m->reload_result = MOUNT_FAILURE_RESOURCES;
1084 mount_enter_mounted(m, MOUNT_SUCCESS);
1087 static int mount_start(Unit *u) {
1088 Mount *m = MOUNT(u);
1092 /* We cannot fulfill this request right now, try again later
1094 if (m->state == MOUNT_UNMOUNTING ||
1095 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1096 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1097 m->state == MOUNT_MOUNTING_SIGTERM ||
1098 m->state == MOUNT_MOUNTING_SIGKILL)
1101 /* Already on it! */
1102 if (m->state == MOUNT_MOUNTING)
1105 assert(m->state == MOUNT_DEAD || m->state == MOUNT_FAILED);
1107 m->result = MOUNT_SUCCESS;
1108 m->reload_result = MOUNT_SUCCESS;
1110 mount_enter_mounting(m);
1114 static int mount_stop(Unit *u) {
1115 Mount *m = MOUNT(u);
1120 if (m->state == MOUNT_UNMOUNTING ||
1121 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1122 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1123 m->state == MOUNT_MOUNTING_SIGTERM ||
1124 m->state == MOUNT_MOUNTING_SIGKILL)
1127 assert(m->state == MOUNT_MOUNTING ||
1128 m->state == MOUNT_MOUNTING_DONE ||
1129 m->state == MOUNT_MOUNTED ||
1130 m->state == MOUNT_REMOUNTING ||
1131 m->state == MOUNT_REMOUNTING_SIGTERM ||
1132 m->state == MOUNT_REMOUNTING_SIGKILL);
1134 mount_enter_unmounting(m);
1138 static int mount_reload(Unit *u) {
1139 Mount *m = MOUNT(u);
1143 if (m->state == MOUNT_MOUNTING_DONE)
1146 assert(m->state == MOUNT_MOUNTED);
1148 mount_enter_remounting(m);
1152 static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
1153 Mount *m = MOUNT(u);
1159 unit_serialize_item(u, f, "state", mount_state_to_string(m->state));
1160 unit_serialize_item(u, f, "result", mount_result_to_string(m->result));
1161 unit_serialize_item(u, f, "reload-result", mount_result_to_string(m->reload_result));
1163 if (m->control_pid > 0)
1164 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) m->control_pid);
1166 if (m->control_command_id >= 0)
1167 unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(m->control_command_id));
1172 static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1173 Mount *m = MOUNT(u);
1180 if (streq(key, "state")) {
1183 if ((state = mount_state_from_string(value)) < 0)
1184 log_debug("Failed to parse state value %s", value);
1186 m->deserialized_state = state;
1187 } else if (streq(key, "result")) {
1190 f = mount_result_from_string(value);
1192 log_debug("Failed to parse result value %s", value);
1193 else if (f != MOUNT_SUCCESS)
1196 } else if (streq(key, "reload-result")) {
1199 f = mount_result_from_string(value);
1201 log_debug("Failed to parse reload result value %s", value);
1202 else if (f != MOUNT_SUCCESS)
1203 m->reload_result = f;
1205 } else if (streq(key, "control-pid")) {
1208 if (parse_pid(value, &pid) < 0)
1209 log_debug("Failed to parse control-pid value %s", value);
1211 m->control_pid = pid;
1212 } else if (streq(key, "control-command")) {
1213 MountExecCommand id;
1215 if ((id = mount_exec_command_from_string(value)) < 0)
1216 log_debug("Failed to parse exec-command value %s", value);
1218 m->control_command_id = id;
1219 m->control_command = m->exec_command + id;
1223 log_debug("Unknown serialization key '%s'", key);
1228 static UnitActiveState mount_active_state(Unit *u) {
1231 return state_translation_table[MOUNT(u)->state];
1234 static const char *mount_sub_state_to_string(Unit *u) {
1237 return mount_state_to_string(MOUNT(u)->state);
1240 static bool mount_check_gc(Unit *u) {
1241 Mount *m = MOUNT(u);
1245 return m->from_proc_self_mountinfo;
1248 static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1249 Mount *m = MOUNT(u);
1255 if (pid != m->control_pid)
1260 if (is_clean_exit(code, status, NULL))
1262 else if (code == CLD_EXITED)
1263 f = MOUNT_FAILURE_EXIT_CODE;
1264 else if (code == CLD_KILLED)
1265 f = MOUNT_FAILURE_SIGNAL;
1266 else if (code == CLD_DUMPED)
1267 f = MOUNT_FAILURE_CORE_DUMP;
1269 assert_not_reached("Unknown code");
1271 if (f != MOUNT_SUCCESS)
1274 if (m->control_command) {
1275 exec_status_exit(&m->control_command->exec_status, &m->exec_context, pid, code, status);
1277 m->control_command = NULL;
1278 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
1281 log_full(f == MOUNT_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
1282 "%s mount process exited, code=%s status=%i", u->id, sigchld_code_to_string(code), status);
1284 /* Note that mount(8) returning and the kernel sending us a
1285 * mount table change event might happen out-of-order. If an
1286 * operation succeed we assume the kernel will follow soon too
1287 * and already change into the resulting state. If it fails
1288 * we check if the kernel still knows about the mount. and
1289 * change state accordingly. */
1293 case MOUNT_MOUNTING:
1294 case MOUNT_MOUNTING_DONE:
1295 case MOUNT_MOUNTING_SIGKILL:
1296 case MOUNT_MOUNTING_SIGTERM:
1298 if (f == MOUNT_SUCCESS)
1299 mount_enter_mounted(m, f);
1300 else if (m->from_proc_self_mountinfo)
1301 mount_enter_mounted(m, f);
1303 mount_enter_dead(m, f);
1306 case MOUNT_REMOUNTING:
1307 case MOUNT_REMOUNTING_SIGKILL:
1308 case MOUNT_REMOUNTING_SIGTERM:
1310 m->reload_result = f;
1311 if (m->from_proc_self_mountinfo)
1312 mount_enter_mounted(m, MOUNT_SUCCESS);
1314 mount_enter_dead(m, MOUNT_SUCCESS);
1318 case MOUNT_UNMOUNTING:
1319 case MOUNT_UNMOUNTING_SIGKILL:
1320 case MOUNT_UNMOUNTING_SIGTERM:
1322 if (f == MOUNT_SUCCESS)
1323 mount_enter_dead(m, f);
1324 else if (m->from_proc_self_mountinfo)
1325 mount_enter_mounted(m, f);
1327 mount_enter_dead(m, f);
1331 assert_not_reached("Uh, control process died at wrong time.");
1334 /* Notify clients about changed exit status */
1335 unit_add_to_dbus_queue(u);
1338 static void mount_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
1339 Mount *m = MOUNT(u);
1342 assert(elapsed == 1);
1343 assert(w == &m->timer_watch);
1347 case MOUNT_MOUNTING:
1348 case MOUNT_MOUNTING_DONE:
1349 log_warning("%s mounting timed out. Stopping.", u->id);
1350 mount_enter_signal(m, MOUNT_MOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1353 case MOUNT_REMOUNTING:
1354 log_warning("%s remounting timed out. Stopping.", u->id);
1355 m->reload_result = MOUNT_FAILURE_TIMEOUT;
1356 mount_enter_mounted(m, MOUNT_SUCCESS);
1359 case MOUNT_UNMOUNTING:
1360 log_warning("%s unmounting timed out. Stopping.", u->id);
1361 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1364 case MOUNT_MOUNTING_SIGTERM:
1365 if (m->kill_context.send_sigkill) {
1366 log_warning("%s mounting timed out. Killing.", u->id);
1367 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1369 log_warning("%s mounting timed out. Skipping SIGKILL. Ignoring.", u->id);
1371 if (m->from_proc_self_mountinfo)
1372 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1374 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1378 case MOUNT_REMOUNTING_SIGTERM:
1379 if (m->kill_context.send_sigkill) {
1380 log_warning("%s remounting timed out. Killing.", u->id);
1381 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1383 log_warning("%s remounting timed out. Skipping SIGKILL. Ignoring.", u->id);
1385 if (m->from_proc_self_mountinfo)
1386 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1388 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1392 case MOUNT_UNMOUNTING_SIGTERM:
1393 if (m->kill_context.send_sigkill) {
1394 log_warning("%s unmounting timed out. Killing.", u->id);
1395 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1397 log_warning("%s unmounting timed out. Skipping SIGKILL. Ignoring.", u->id);
1399 if (m->from_proc_self_mountinfo)
1400 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1402 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1406 case MOUNT_MOUNTING_SIGKILL:
1407 case MOUNT_REMOUNTING_SIGKILL:
1408 case MOUNT_UNMOUNTING_SIGKILL:
1409 log_warning("%s mount process still around after SIGKILL. Ignoring.", u->id);
1411 if (m->from_proc_self_mountinfo)
1412 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1414 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1418 assert_not_reached("Timeout at wrong time.");
1422 static int mount_add_one(
1426 const char *options,
1433 char *e, *w = NULL, *o = NULL, *f = NULL;
1435 bool load_extras = false;
1443 /* Ignore API mount points. They should never be referenced in
1444 * dependencies ever. */
1445 if (mount_point_is_api(where) || mount_point_ignore(where))
1448 if (streq(fstype, "autofs"))
1451 /* probably some kind of swap, ignore */
1452 if (!is_path(where))
1455 e = unit_name_from_path(where, ".mount");
1459 u = manager_get_unit(m, e);
1463 u = unit_new(m, sizeof(Mount));
1469 r = unit_add_name(u, e);
1475 MOUNT(u)->where = strdup(where);
1476 if (!MOUNT(u)->where) {
1481 unit_add_to_load_queue(u);
1486 if (!MOUNT(u)->where) {
1487 MOUNT(u)->where = strdup(where);
1488 if (!MOUNT(u)->where) {
1494 if (u->load_state == UNIT_ERROR) {
1495 u->load_state = UNIT_LOADED;
1498 /* Load in the extras later on, after we
1499 * finished initialization of the unit */
1504 if (!(w = strdup(what)) ||
1505 !(o = strdup(options)) ||
1506 !(f = strdup(fstype))) {
1511 p = &MOUNT(u)->parameters_proc_self_mountinfo;
1513 MOUNT(u)->is_mounted = true;
1514 MOUNT(u)->just_mounted = !MOUNT(u)->from_proc_self_mountinfo;
1515 MOUNT(u)->just_changed = !streq_ptr(p->options, o);
1518 MOUNT(u)->from_proc_self_mountinfo = true;
1532 r = mount_add_extras(MOUNT(u));
1537 unit_add_to_dbus_queue(u);
1552 static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
1555 char *device, *path, *options, *options2, *fstype, *d, *p, *o;
1559 rewind(m->proc_self_mountinfo);
1564 device = path = options = options2 = fstype = d = p = o = NULL;
1566 if ((k = fscanf(m->proc_self_mountinfo,
1567 "%*s " /* (1) mount id */
1568 "%*s " /* (2) parent id */
1569 "%*s " /* (3) major:minor */
1570 "%*s " /* (4) root */
1571 "%ms " /* (5) mount point */
1572 "%ms" /* (6) mount options */
1573 "%*[^-]" /* (7) optional fields */
1574 "- " /* (8) separator */
1575 "%ms " /* (9) file system type */
1576 "%ms" /* (10) mount source */
1577 "%ms" /* (11) mount options 2 */
1578 "%*[^\n]", /* some rubbish at the end */
1588 log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
1592 o = strjoin(options, ",", options2, NULL);
1598 if (!(d = cunescape(device)) ||
1599 !(p = cunescape(path))) {
1604 if ((k = mount_add_one(m, d, p, o, fstype, 0, set_flags)) < 0)
1631 static void mount_shutdown(Manager *m) {
1634 if (m->proc_self_mountinfo) {
1635 fclose(m->proc_self_mountinfo);
1636 m->proc_self_mountinfo = NULL;
1640 static int mount_enumerate(Manager *m) {
1642 struct epoll_event ev;
1645 if (!m->proc_self_mountinfo) {
1646 if (!(m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "re")))
1649 m->mount_watch.type = WATCH_MOUNT;
1650 m->mount_watch.fd = fileno(m->proc_self_mountinfo);
1653 ev.events = EPOLLPRI;
1654 ev.data.ptr = &m->mount_watch;
1656 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->mount_watch.fd, &ev) < 0)
1660 if ((r = mount_load_proc_self_mountinfo(m, false)) < 0)
1670 void mount_fd_event(Manager *m, int events) {
1675 assert(events & EPOLLPRI);
1677 /* The manager calls this for every fd event happening on the
1678 * /proc/self/mountinfo file, which informs us about mounting
1681 r = mount_load_proc_self_mountinfo(m, true);
1683 log_error("Failed to reread /proc/self/mountinfo: %s", strerror(-r));
1685 /* Reset flags, just in case, for later calls */
1686 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1687 Mount *mount = MOUNT(u);
1689 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1695 manager_dispatch_load_queue(m);
1697 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1698 Mount *mount = MOUNT(u);
1700 if (!mount->is_mounted) {
1701 /* This has just been unmounted. */
1703 mount->from_proc_self_mountinfo = false;
1705 switch (mount->state) {
1708 mount_enter_dead(mount, MOUNT_SUCCESS);
1712 mount_set_state(mount, mount->state);
1717 } else if (mount->just_mounted || mount->just_changed) {
1719 /* New or changed mount entry */
1721 switch (mount->state) {
1725 mount_enter_mounted(mount, MOUNT_SUCCESS);
1728 case MOUNT_MOUNTING:
1729 mount_enter_mounting_done(mount);
1733 /* Nothing really changed, but let's
1734 * issue an notification call
1735 * nonetheless, in case somebody is
1736 * waiting for this. (e.g. file system
1737 * ro/rw remounts.) */
1738 mount_set_state(mount, mount->state);
1743 /* Reset the flags for later calls */
1744 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1748 static void mount_reset_failed(Unit *u) {
1749 Mount *m = MOUNT(u);
1753 if (m->state == MOUNT_FAILED)
1754 mount_set_state(m, MOUNT_DEAD);
1756 m->result = MOUNT_SUCCESS;
1757 m->reload_result = MOUNT_SUCCESS;
1760 static int mount_kill(Unit *u, KillWho who, int signo, DBusError *error) {
1761 Mount *m = MOUNT(u);
1763 Set *pid_set = NULL;
1767 if (who == KILL_MAIN) {
1768 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "Mount units have no main processes");
1772 if (m->control_pid <= 0 && who == KILL_CONTROL) {
1773 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
1777 if (who == KILL_CONTROL || who == KILL_ALL)
1778 if (m->control_pid > 0)
1779 if (kill(m->control_pid, signo) < 0)
1782 if (who == KILL_ALL) {
1785 pid_set = set_new(trivial_hash_func, trivial_compare_func);
1789 /* Exclude the control pid from being killed via the cgroup */
1790 if (m->control_pid > 0) {
1791 q = set_put(pid_set, LONG_TO_PTR(m->control_pid));
1798 q = cgroup_bonding_kill_list(UNIT(m)->cgroup_bondings, signo, false, false, pid_set, NULL);
1799 if (q < 0 && q != -EAGAIN && q != -ESRCH && q != -ENOENT)
1810 static const char* const mount_state_table[_MOUNT_STATE_MAX] = {
1811 [MOUNT_DEAD] = "dead",
1812 [MOUNT_MOUNTING] = "mounting",
1813 [MOUNT_MOUNTING_DONE] = "mounting-done",
1814 [MOUNT_MOUNTED] = "mounted",
1815 [MOUNT_REMOUNTING] = "remounting",
1816 [MOUNT_UNMOUNTING] = "unmounting",
1817 [MOUNT_MOUNTING_SIGTERM] = "mounting-sigterm",
1818 [MOUNT_MOUNTING_SIGKILL] = "mounting-sigkill",
1819 [MOUNT_REMOUNTING_SIGTERM] = "remounting-sigterm",
1820 [MOUNT_REMOUNTING_SIGKILL] = "remounting-sigkill",
1821 [MOUNT_UNMOUNTING_SIGTERM] = "unmounting-sigterm",
1822 [MOUNT_UNMOUNTING_SIGKILL] = "unmounting-sigkill",
1823 [MOUNT_FAILED] = "failed"
1826 DEFINE_STRING_TABLE_LOOKUP(mount_state, MountState);
1828 static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
1829 [MOUNT_EXEC_MOUNT] = "ExecMount",
1830 [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
1831 [MOUNT_EXEC_REMOUNT] = "ExecRemount",
1834 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
1836 static const char* const mount_result_table[_MOUNT_RESULT_MAX] = {
1837 [MOUNT_SUCCESS] = "success",
1838 [MOUNT_FAILURE_RESOURCES] = "resources",
1839 [MOUNT_FAILURE_TIMEOUT] = "timeout",
1840 [MOUNT_FAILURE_EXIT_CODE] = "exit-code",
1841 [MOUNT_FAILURE_SIGNAL] = "signal",
1842 [MOUNT_FAILURE_CORE_DUMP] = "core-dump"
1845 DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult);
1847 const UnitVTable mount_vtable = {
1848 .object_size = sizeof(Mount),
1849 .exec_context_offset = offsetof(Mount, exec_context),
1857 .no_instances = true,
1863 .coldplug = mount_coldplug,
1867 .start = mount_start,
1869 .reload = mount_reload,
1873 .serialize = mount_serialize,
1874 .deserialize_item = mount_deserialize_item,
1876 .active_state = mount_active_state,
1877 .sub_state_to_string = mount_sub_state_to_string,
1879 .check_gc = mount_check_gc,
1881 .sigchld_event = mount_sigchld_event,
1882 .timer_event = mount_timer_event,
1884 .reset_failed = mount_reset_failed,
1886 .bus_interface = "org.freedesktop.systemd1.Mount",
1887 .bus_message_handler = bus_mount_message_handler,
1888 .bus_invalidating_properties = bus_mount_invalidating_properties,
1890 .enumerate = mount_enumerate,
1891 .shutdown = mount_shutdown,
1893 .status_message_formats = {
1894 .starting_stopping = {
1895 [0] = "Mounting %s...",
1896 [1] = "Unmounting %s...",
1898 .finished_start_job = {
1899 [JOB_DONE] = "Mounted %s.",
1900 [JOB_FAILED] = "Failed to mount %s.",
1901 [JOB_DEPENDENCY] = "Dependency failed for %s.",
1902 [JOB_TIMEOUT] = "Timed out mounting %s.",
1904 .finished_stop_job = {
1905 [JOB_DONE] = "Unmounted %s.",
1906 [JOB_FAILED] = "Failed unmounting %s.",
1907 [JOB_TIMEOUT] = "Timed out unmounting %s.",