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);
86 /* We need to make sure that /bin/mount is always called in
87 * the same process group as us, so that the autofs kernel
88 * side doesn't send us another mount request while we are
89 * already trying to comply its last one. */
90 m->exec_context.same_pgrp = true;
92 m->timer_watch.type = WATCH_INVALID;
94 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
96 UNIT(m)->ignore_on_isolate = true;
99 static void mount_unwatch_control_pid(Mount *m) {
102 if (m->control_pid <= 0)
105 unit_unwatch_pid(UNIT(m), m->control_pid);
109 static void mount_parameters_done(MountParameters *p) {
116 p->what = p->options = p->fstype = NULL;
119 static void mount_done(Unit *u) {
127 mount_parameters_done(&m->parameters_proc_self_mountinfo);
128 mount_parameters_done(&m->parameters_fragment);
130 exec_context_done(&m->exec_context, manager_is_reloading_or_reexecuting(u->manager));
131 exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
132 m->control_command = NULL;
134 mount_unwatch_control_pid(m);
136 unit_unwatch_timer(u, &m->timer_watch);
139 static MountParameters* get_mount_parameters_fragment(Mount *m) {
142 if (m->from_fragment)
143 return &m->parameters_fragment;
148 static MountParameters* get_mount_parameters(Mount *m) {
151 if (m->from_proc_self_mountinfo)
152 return &m->parameters_proc_self_mountinfo;
154 return get_mount_parameters_fragment(m);
157 static int mount_add_mount_links(Mount *m) {
164 pm = get_mount_parameters_fragment(m);
166 /* Adds in links to other mount points that might lie below or
167 * above us in the hierarchy */
169 LIST_FOREACH(units_by_type, other, UNIT(m)->manager->units_by_type[UNIT_MOUNT]) {
170 Mount *n = MOUNT(other);
176 if (UNIT(n)->load_state != UNIT_LOADED)
179 pn = get_mount_parameters_fragment(n);
181 if (path_startswith(m->where, n->where)) {
183 if ((r = unit_add_dependency(UNIT(m), UNIT_AFTER, UNIT(n), true)) < 0)
187 if ((r = unit_add_dependency(UNIT(m), UNIT_REQUIRES, UNIT(n), true)) < 0)
190 } else if (path_startswith(n->where, m->where)) {
192 if ((r = unit_add_dependency(UNIT(n), UNIT_AFTER, UNIT(m), true)) < 0)
196 if ((r = unit_add_dependency(UNIT(n), UNIT_REQUIRES, UNIT(m), true)) < 0)
199 } else if (pm && pm->what && path_startswith(pm->what, n->where)) {
201 if ((r = unit_add_dependency(UNIT(m), UNIT_AFTER, UNIT(n), true)) < 0)
204 if ((r = unit_add_dependency(UNIT(m), UNIT_REQUIRES, UNIT(n), true)) < 0)
207 } else if (pn && pn->what && path_startswith(pn->what, m->where)) {
209 if ((r = unit_add_dependency(UNIT(n), UNIT_AFTER, UNIT(m), true)) < 0)
212 if ((r = unit_add_dependency(UNIT(n), UNIT_REQUIRES, UNIT(m), true)) < 0)
220 static int mount_add_swap_links(Mount *m) {
226 LIST_FOREACH(units_by_type, other, UNIT(m)->manager->units_by_type[UNIT_SWAP]) {
227 r = swap_add_one_mount_link(SWAP(other), m);
235 static int mount_add_path_links(Mount *m) {
241 LIST_FOREACH(units_by_type, other, UNIT(m)->manager->units_by_type[UNIT_PATH]) {
242 r = path_add_one_mount_link(PATH(other), m);
250 static int mount_add_automount_links(Mount *m) {
256 LIST_FOREACH(units_by_type, other, UNIT(m)->manager->units_by_type[UNIT_AUTOMOUNT]) {
257 r = automount_add_one_mount_link(AUTOMOUNT(other), m);
265 static int mount_add_socket_links(Mount *m) {
271 LIST_FOREACH(units_by_type, other, UNIT(m)->manager->units_by_type[UNIT_SOCKET]) {
272 r = socket_add_one_mount_link(SOCKET(other), m);
280 static int mount_add_requires_mounts_links(Mount *m) {
286 LIST_FOREACH(has_requires_mounts_for, other, UNIT(m)->manager->has_requires_mounts_for) {
287 r = unit_add_one_mount_link(other, m);
295 static char* mount_test_option(const char *haystack, const char *needle) {
300 /* Like glibc's hasmntopt(), but works on a string, not a
307 me.mnt_opts = (char*) haystack;
309 return hasmntopt(&me, needle);
312 static bool mount_is_network(MountParameters *p) {
315 if (mount_test_option(p->options, "_netdev"))
318 if (p->fstype && fstype_is_network(p->fstype))
324 static bool mount_is_bind(MountParameters *p) {
327 if (mount_test_option(p->options, "bind"))
330 if (p->fstype && streq(p->fstype, "bind"))
333 if (mount_test_option(p->options, "rbind"))
336 if (p->fstype && streq(p->fstype, "rbind"))
342 static bool needs_quota(MountParameters *p) {
345 if (mount_is_network(p))
348 if (mount_is_bind(p))
351 return mount_test_option(p->options, "usrquota") ||
352 mount_test_option(p->options, "grpquota") ||
353 mount_test_option(p->options, "quota") ||
354 mount_test_option(p->options, "usrjquota") ||
355 mount_test_option(p->options, "grpjquota");
358 static int mount_add_device_links(Mount *m) {
364 p = get_mount_parameters_fragment(m);
371 if (mount_is_bind(p))
374 if (!is_device_path(p->what))
377 if (path_equal(m->where, "/"))
380 r = unit_add_node_link(UNIT(m), p->what, false);
385 UNIT(m)->manager->running_as == SYSTEMD_SYSTEM) {
388 /* Let's add in the fsck service */
390 /* aka SPECIAL_FSCK_SERVICE */
391 name = unit_name_from_path_instance("systemd-fsck", p->what, ".service");
395 r = manager_load_unit_prepare(UNIT(m)->manager, name, NULL, NULL, &fsck);
397 log_warning_unit(name,
398 "Failed to prepare unit %s: %s", name, strerror(-r));
404 SERVICE(fsck)->fsck_passno = p->passno;
406 r = unit_add_two_dependencies(UNIT(m), UNIT_AFTER, UNIT_REQUIRES, fsck, true);
414 static int mount_add_quota_links(Mount *m) {
420 if (UNIT(m)->manager->running_as != SYSTEMD_SYSTEM)
423 p = get_mount_parameters_fragment(m);
430 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTACHECK_SERVICE, NULL, true);
434 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTAON_SERVICE, NULL, true);
441 static int mount_add_default_dependencies(Mount *m) {
448 if (UNIT(m)->manager->running_as != SYSTEMD_SYSTEM)
451 p = get_mount_parameters(m);
456 if (path_equal(m->where, "/"))
459 if (mount_is_network(p))
460 after = SPECIAL_REMOTE_FS_PRE_TARGET;
462 after = SPECIAL_LOCAL_FS_PRE_TARGET;
464 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_WANTS, UNIT_AFTER, after, NULL, true);
468 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
475 static int mount_fix_timeouts(Mount *m) {
477 const char *timeout = NULL;
486 p = get_mount_parameters_fragment(m);
490 /* Allow configuration how long we wait for a device that
491 * backs a mount point to show up. This is useful to support
492 * endless device timeouts for devices that show up only after
493 * user input, like crypto devices. */
495 if ((timeout = mount_test_option(p->options, "comment=systemd.device-timeout")))
497 else if ((timeout = mount_test_option(p->options, "x-systemd.device-timeout")))
502 t = strndup(timeout, strcspn(timeout, ",;" WHITESPACE));
506 r = parse_usec(t, &u);
510 log_warning_unit(UNIT(m)->id,
511 "Failed to parse timeout for %s, ignoring: %s",
516 SET_FOREACH(other, UNIT(m)->dependencies[UNIT_AFTER], i) {
517 if (other->type != UNIT_DEVICE)
520 other->job_timeout = u;
526 static int mount_verify(Mount *m) {
531 if (UNIT(m)->load_state != UNIT_LOADED)
534 if (!m->from_fragment && !m->from_proc_self_mountinfo)
537 if (!(e = unit_name_from_path(m->where, ".mount")))
540 b = unit_has_name(UNIT(m), e);
544 log_error_unit(UNIT(m)->id,
545 "%s's Where setting doesn't match unit name. Refusing.",
550 if (mount_point_is_api(m->where) || mount_point_ignore(m->where)) {
551 log_error_unit(UNIT(m)->id,
552 "Cannot create mount unit for API file system %s. Refusing.",
557 if (UNIT(m)->fragment_path && !m->parameters_fragment.what) {
558 log_error_unit(UNIT(m)->id,
559 "%s's What setting is missing. Refusing.", UNIT(m)->id);
563 if (m->exec_context.pam_name && m->kill_context.kill_mode != KILL_CONTROL_GROUP) {
564 log_error_unit(UNIT(m)->id,
565 "%s has PAM enabled. Kill mode must be set to control-group'. Refusing.",
573 static int mount_add_extras(Mount *m) {
577 if (UNIT(m)->fragment_path)
578 m->from_fragment = true;
581 m->where = unit_name_to_path(u->id);
586 path_kill_slashes(m->where);
588 r = unit_add_exec_dependencies(u, &m->exec_context);
592 if (!UNIT(m)->description) {
593 r = unit_set_description(u, m->where);
598 r = mount_add_device_links(m);
602 r = mount_add_mount_links(m);
606 r = mount_add_socket_links(m);
610 r = mount_add_swap_links(m);
614 r = mount_add_path_links(m);
618 r = mount_add_requires_mounts_links(m);
622 r = mount_add_automount_links(m);
626 r = mount_add_quota_links(m);
630 if (UNIT(m)->default_dependencies) {
631 r = mount_add_default_dependencies(m);
636 r = unit_add_default_cgroups(u);
640 r = mount_fix_timeouts(m);
647 static int mount_load(Unit *u) {
652 assert(u->load_state == UNIT_STUB);
654 if (m->from_proc_self_mountinfo)
655 r = unit_load_fragment_and_dropin_optional(u);
657 r = unit_load_fragment_and_dropin(u);
662 /* This is a new unit? Then let's add in some extras */
663 if (u->load_state == UNIT_LOADED) {
664 r = mount_add_extras(m);
668 r = unit_exec_context_defaults(u, &m->exec_context);
673 return mount_verify(m);
676 static int mount_notify_automount(Mount *m, int status) {
683 SET_FOREACH(p, UNIT(m)->dependencies[UNIT_TRIGGERED_BY], i)
684 if (p->type == UNIT_AUTOMOUNT) {
685 r = automount_send_ready(AUTOMOUNT(p), status);
693 static void mount_set_state(Mount *m, MountState state) {
694 MountState old_state;
697 old_state = m->state;
700 if (state != MOUNT_MOUNTING &&
701 state != MOUNT_MOUNTING_DONE &&
702 state != MOUNT_REMOUNTING &&
703 state != MOUNT_UNMOUNTING &&
704 state != MOUNT_MOUNTING_SIGTERM &&
705 state != MOUNT_MOUNTING_SIGKILL &&
706 state != MOUNT_UNMOUNTING_SIGTERM &&
707 state != MOUNT_UNMOUNTING_SIGKILL &&
708 state != MOUNT_REMOUNTING_SIGTERM &&
709 state != MOUNT_REMOUNTING_SIGKILL) {
710 unit_unwatch_timer(UNIT(m), &m->timer_watch);
711 mount_unwatch_control_pid(m);
712 m->control_command = NULL;
713 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
716 if (state == MOUNT_MOUNTED ||
717 state == MOUNT_REMOUNTING)
718 mount_notify_automount(m, 0);
719 else if (state == MOUNT_DEAD ||
720 state == MOUNT_UNMOUNTING ||
721 state == MOUNT_MOUNTING_SIGTERM ||
722 state == MOUNT_MOUNTING_SIGKILL ||
723 state == MOUNT_REMOUNTING_SIGTERM ||
724 state == MOUNT_REMOUNTING_SIGKILL ||
725 state == MOUNT_UNMOUNTING_SIGTERM ||
726 state == MOUNT_UNMOUNTING_SIGKILL ||
727 state == MOUNT_FAILED) {
728 if (state != old_state)
729 mount_notify_automount(m, -ENODEV);
732 if (state != old_state)
733 log_debug_unit(UNIT(m)->id,
734 "%s changed %s -> %s",
736 mount_state_to_string(old_state),
737 mount_state_to_string(state));
739 unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state], m->reload_result == MOUNT_SUCCESS);
740 m->reload_result = MOUNT_SUCCESS;
743 static int mount_coldplug(Unit *u) {
745 MountState new_state = MOUNT_DEAD;
749 assert(m->state == MOUNT_DEAD);
751 if (m->deserialized_state != m->state)
752 new_state = m->deserialized_state;
753 else if (m->from_proc_self_mountinfo)
754 new_state = MOUNT_MOUNTED;
756 if (new_state != m->state) {
758 if (new_state == MOUNT_MOUNTING ||
759 new_state == MOUNT_MOUNTING_DONE ||
760 new_state == MOUNT_REMOUNTING ||
761 new_state == MOUNT_UNMOUNTING ||
762 new_state == MOUNT_MOUNTING_SIGTERM ||
763 new_state == MOUNT_MOUNTING_SIGKILL ||
764 new_state == MOUNT_UNMOUNTING_SIGTERM ||
765 new_state == MOUNT_UNMOUNTING_SIGKILL ||
766 new_state == MOUNT_REMOUNTING_SIGTERM ||
767 new_state == MOUNT_REMOUNTING_SIGKILL) {
769 if (m->control_pid <= 0)
772 r = unit_watch_pid(UNIT(m), m->control_pid);
776 r = unit_watch_timer(UNIT(m), CLOCK_MONOTONIC, true, m->timeout_usec, &m->timer_watch);
781 mount_set_state(m, new_state);
787 static void mount_dump(Unit *u, FILE *f, const char *prefix) {
794 p = get_mount_parameters(m);
797 "%sMount State: %s\n"
801 "%sFile System Type: %s\n"
803 "%sFrom /proc/self/mountinfo: %s\n"
804 "%sFrom fragment: %s\n"
805 "%sDirectoryMode: %04o\n",
806 prefix, mount_state_to_string(m->state),
807 prefix, mount_result_to_string(m->result),
809 prefix, strna(p->what),
810 prefix, strna(p->fstype),
811 prefix, strna(p->options),
812 prefix, yes_no(m->from_proc_self_mountinfo),
813 prefix, yes_no(m->from_fragment),
814 prefix, m->directory_mode);
816 if (m->control_pid > 0)
818 "%sControl PID: %lu\n",
819 prefix, (unsigned long) m->control_pid);
821 exec_context_dump(&m->exec_context, f, prefix);
822 kill_context_dump(&m->kill_context, f, prefix);
825 static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
833 r = unit_watch_timer(UNIT(m), CLOCK_MONOTONIC, true, m->timeout_usec, &m->timer_watch);
837 if ((r = exec_spawn(c,
841 UNIT(m)->manager->environment,
845 UNIT(m)->manager->confirm_spawn,
846 UNIT(m)->cgroup_bondings,
847 UNIT(m)->cgroup_attributes,
854 if ((r = unit_watch_pid(UNIT(m), pid)) < 0)
855 /* FIXME: we need to do something here */
863 unit_unwatch_timer(UNIT(m), &m->timer_watch);
868 static void mount_enter_dead(Mount *m, MountResult f) {
871 if (f != MOUNT_SUCCESS)
874 exec_context_tmp_dirs_done(&m->exec_context);
875 mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD);
878 static void mount_enter_mounted(Mount *m, MountResult f) {
881 if (f != MOUNT_SUCCESS)
884 mount_set_state(m, MOUNT_MOUNTED);
887 static void mount_enter_signal(Mount *m, MountState state, MountResult f) {
892 if (f != MOUNT_SUCCESS)
895 r = unit_kill_context(
898 state != MOUNT_MOUNTING_SIGTERM && state != MOUNT_UNMOUNTING_SIGTERM && state != MOUNT_REMOUNTING_SIGTERM,
906 r = unit_watch_timer(UNIT(m), CLOCK_MONOTONIC, true, m->timeout_usec, &m->timer_watch);
910 mount_set_state(m, state);
911 } else if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
912 mount_enter_mounted(m, MOUNT_SUCCESS);
914 mount_enter_dead(m, MOUNT_SUCCESS);
919 log_warning_unit(UNIT(m)->id,
920 "%s failed to kill processes: %s", UNIT(m)->id, strerror(-r));
922 if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
923 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
925 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
928 void warn_if_dir_nonempty(const char *unit, const char* where) {
932 if (dir_is_empty(where) > 0)
935 log_struct_unit(LOG_NOTICE,
937 "MESSAGE=%s: Directory %s to mount over is not empty, mounting anyway.",
940 MESSAGE_ID(SD_MESSAGE_OVERMOUNTING),
944 static void mount_enter_unmounting(Mount *m) {
949 m->control_command_id = MOUNT_EXEC_UNMOUNT;
950 m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
952 if ((r = exec_command_set(
959 mount_unwatch_control_pid(m);
961 if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
964 mount_set_state(m, MOUNT_UNMOUNTING);
969 log_warning_unit(UNIT(m)->id,
970 "%s failed to run 'umount' task: %s",
971 UNIT(m)->id, strerror(-r));
972 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
975 static void mount_enter_mounting(Mount *m) {
981 m->control_command_id = MOUNT_EXEC_MOUNT;
982 m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
984 mkdir_p_label(m->where, m->directory_mode);
986 warn_if_dir_nonempty(m->meta.id, m->where);
988 /* Create the source directory for bind-mounts if needed */
989 p = get_mount_parameters_fragment(m);
990 if (p && mount_is_bind(p))
991 mkdir_p_label(p->what, m->directory_mode);
993 if (m->from_fragment)
994 r = exec_command_set(
997 m->parameters_fragment.what,
999 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
1000 m->parameters_fragment.options ? "-o" : NULL, m->parameters_fragment.options,
1008 mount_unwatch_control_pid(m);
1010 r = mount_spawn(m, m->control_command, &m->control_pid);
1014 mount_set_state(m, MOUNT_MOUNTING);
1019 log_warning_unit(UNIT(m)->id,
1020 "%s failed to run 'mount' task: %s",
1021 UNIT(m)->id, strerror(-r));
1022 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
1025 static void mount_enter_mounting_done(Mount *m) {
1028 mount_set_state(m, MOUNT_MOUNTING_DONE);
1031 static void mount_enter_remounting(Mount *m) {
1036 m->control_command_id = MOUNT_EXEC_REMOUNT;
1037 m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
1039 if (m->from_fragment) {
1043 if (m->parameters_fragment.options) {
1044 if (!(buf = strappend("remount,", m->parameters_fragment.options))) {
1053 r = exec_command_set(
1056 m->parameters_fragment.what,
1058 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
1069 mount_unwatch_control_pid(m);
1071 if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
1074 mount_set_state(m, MOUNT_REMOUNTING);
1079 log_warning_unit(UNIT(m)->id,
1080 "%s failed to run 'remount' task: %s",
1081 UNIT(m)->id, strerror(-r));
1082 m->reload_result = MOUNT_FAILURE_RESOURCES;
1083 mount_enter_mounted(m, MOUNT_SUCCESS);
1086 static int mount_start(Unit *u) {
1087 Mount *m = MOUNT(u);
1091 /* We cannot fulfill this request right now, try again later
1093 if (m->state == MOUNT_UNMOUNTING ||
1094 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1095 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1096 m->state == MOUNT_MOUNTING_SIGTERM ||
1097 m->state == MOUNT_MOUNTING_SIGKILL)
1100 /* Already on it! */
1101 if (m->state == MOUNT_MOUNTING)
1104 assert(m->state == MOUNT_DEAD || m->state == MOUNT_FAILED);
1106 m->result = MOUNT_SUCCESS;
1107 m->reload_result = MOUNT_SUCCESS;
1109 mount_enter_mounting(m);
1113 static int mount_stop(Unit *u) {
1114 Mount *m = MOUNT(u);
1119 if (m->state == MOUNT_UNMOUNTING ||
1120 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1121 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1122 m->state == MOUNT_MOUNTING_SIGTERM ||
1123 m->state == MOUNT_MOUNTING_SIGKILL)
1126 assert(m->state == MOUNT_MOUNTING ||
1127 m->state == MOUNT_MOUNTING_DONE ||
1128 m->state == MOUNT_MOUNTED ||
1129 m->state == MOUNT_REMOUNTING ||
1130 m->state == MOUNT_REMOUNTING_SIGTERM ||
1131 m->state == MOUNT_REMOUNTING_SIGKILL);
1133 mount_enter_unmounting(m);
1137 static int mount_reload(Unit *u) {
1138 Mount *m = MOUNT(u);
1142 if (m->state == MOUNT_MOUNTING_DONE)
1145 assert(m->state == MOUNT_MOUNTED);
1147 mount_enter_remounting(m);
1151 static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
1152 Mount *m = MOUNT(u);
1158 unit_serialize_item(u, f, "state", mount_state_to_string(m->state));
1159 unit_serialize_item(u, f, "result", mount_result_to_string(m->result));
1160 unit_serialize_item(u, f, "reload-result", mount_result_to_string(m->reload_result));
1162 if (m->control_pid > 0)
1163 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) m->control_pid);
1165 if (m->control_command_id >= 0)
1166 unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(m->control_command_id));
1168 exec_context_serialize(&m->exec_context, UNIT(m), f);
1173 static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1174 Mount *m = MOUNT(u);
1181 if (streq(key, "state")) {
1184 if ((state = mount_state_from_string(value)) < 0)
1185 log_debug_unit(u->id, "Failed to parse state value %s", value);
1187 m->deserialized_state = state;
1188 } else if (streq(key, "result")) {
1191 f = mount_result_from_string(value);
1193 log_debug_unit(UNIT(m)->id,
1194 "Failed to parse result value %s", value);
1195 else if (f != MOUNT_SUCCESS)
1198 } else if (streq(key, "reload-result")) {
1201 f = mount_result_from_string(value);
1203 log_debug_unit(UNIT(m)->id,
1204 "Failed to parse reload result value %s", value);
1205 else if (f != MOUNT_SUCCESS)
1206 m->reload_result = f;
1208 } else if (streq(key, "control-pid")) {
1211 if (parse_pid(value, &pid) < 0)
1212 log_debug_unit(UNIT(m)->id,
1213 "Failed to parse control-pid value %s", value);
1215 m->control_pid = pid;
1216 } else if (streq(key, "control-command")) {
1217 MountExecCommand id;
1219 if ((id = mount_exec_command_from_string(value)) < 0)
1220 log_debug_unit(UNIT(m)->id,
1221 "Failed to parse exec-command value %s", value);
1223 m->control_command_id = id;
1224 m->control_command = m->exec_command + id;
1226 } else if (streq(key, "tmp-dir")) {
1233 m->exec_context.tmp_dir = t;
1234 } else if (streq(key, "var-tmp-dir")) {
1241 m->exec_context.var_tmp_dir = t;
1243 log_debug_unit(UNIT(m)->id,
1244 "Unknown serialization key '%s'", key);
1249 static UnitActiveState mount_active_state(Unit *u) {
1252 return state_translation_table[MOUNT(u)->state];
1255 static const char *mount_sub_state_to_string(Unit *u) {
1258 return mount_state_to_string(MOUNT(u)->state);
1261 static bool mount_check_gc(Unit *u) {
1262 Mount *m = MOUNT(u);
1266 return m->from_proc_self_mountinfo;
1269 static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1270 Mount *m = MOUNT(u);
1276 if (pid != m->control_pid)
1281 if (is_clean_exit(code, status, NULL))
1283 else if (code == CLD_EXITED)
1284 f = MOUNT_FAILURE_EXIT_CODE;
1285 else if (code == CLD_KILLED)
1286 f = MOUNT_FAILURE_SIGNAL;
1287 else if (code == CLD_DUMPED)
1288 f = MOUNT_FAILURE_CORE_DUMP;
1290 assert_not_reached("Unknown code");
1292 if (f != MOUNT_SUCCESS)
1295 if (m->control_command) {
1296 exec_status_exit(&m->control_command->exec_status, &m->exec_context, pid, code, status);
1298 m->control_command = NULL;
1299 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
1302 log_full_unit(f == MOUNT_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
1303 "%s mount process exited, code=%s status=%i",
1304 u->id, sigchld_code_to_string(code), status);
1306 /* Note that mount(8) returning and the kernel sending us a
1307 * mount table change event might happen out-of-order. If an
1308 * operation succeed we assume the kernel will follow soon too
1309 * and already change into the resulting state. If it fails
1310 * we check if the kernel still knows about the mount. and
1311 * change state accordingly. */
1315 case MOUNT_MOUNTING:
1316 case MOUNT_MOUNTING_DONE:
1317 case MOUNT_MOUNTING_SIGKILL:
1318 case MOUNT_MOUNTING_SIGTERM:
1320 if (f == MOUNT_SUCCESS)
1321 mount_enter_mounted(m, f);
1322 else if (m->from_proc_self_mountinfo)
1323 mount_enter_mounted(m, f);
1325 mount_enter_dead(m, f);
1328 case MOUNT_REMOUNTING:
1329 case MOUNT_REMOUNTING_SIGKILL:
1330 case MOUNT_REMOUNTING_SIGTERM:
1332 m->reload_result = f;
1333 if (m->from_proc_self_mountinfo)
1334 mount_enter_mounted(m, MOUNT_SUCCESS);
1336 mount_enter_dead(m, MOUNT_SUCCESS);
1340 case MOUNT_UNMOUNTING:
1341 case MOUNT_UNMOUNTING_SIGKILL:
1342 case MOUNT_UNMOUNTING_SIGTERM:
1344 if (f == MOUNT_SUCCESS)
1345 mount_enter_dead(m, f);
1346 else if (m->from_proc_self_mountinfo)
1347 mount_enter_mounted(m, f);
1349 mount_enter_dead(m, f);
1353 assert_not_reached("Uh, control process died at wrong time.");
1356 /* Notify clients about changed exit status */
1357 unit_add_to_dbus_queue(u);
1360 static void mount_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
1361 Mount *m = MOUNT(u);
1364 assert(elapsed == 1);
1365 assert(w == &m->timer_watch);
1369 case MOUNT_MOUNTING:
1370 case MOUNT_MOUNTING_DONE:
1371 log_warning_unit(u->id,
1372 "%s mounting timed out. Stopping.", u->id);
1373 mount_enter_signal(m, MOUNT_MOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1376 case MOUNT_REMOUNTING:
1377 log_warning_unit(u->id,
1378 "%s remounting timed out. Stopping.", u->id);
1379 m->reload_result = MOUNT_FAILURE_TIMEOUT;
1380 mount_enter_mounted(m, MOUNT_SUCCESS);
1383 case MOUNT_UNMOUNTING:
1384 log_warning_unit(u->id,
1385 "%s unmounting timed out. Stopping.", u->id);
1386 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1389 case MOUNT_MOUNTING_SIGTERM:
1390 if (m->kill_context.send_sigkill) {
1391 log_warning_unit(u->id,
1392 "%s mounting timed out. Killing.", u->id);
1393 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1395 log_warning_unit(u->id,
1396 "%s mounting timed out. Skipping SIGKILL. Ignoring.",
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_REMOUNTING_SIGTERM:
1407 if (m->kill_context.send_sigkill) {
1408 log_warning_unit(u->id,
1409 "%s remounting timed out. Killing.", u->id);
1410 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1412 log_warning_unit(u->id,
1413 "%s remounting timed out. Skipping SIGKILL. Ignoring.",
1416 if (m->from_proc_self_mountinfo)
1417 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1419 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1423 case MOUNT_UNMOUNTING_SIGTERM:
1424 if (m->kill_context.send_sigkill) {
1425 log_warning_unit(u->id,
1426 "%s unmounting timed out. Killing.", u->id);
1427 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1429 log_warning_unit(u->id,
1430 "%s unmounting timed out. Skipping SIGKILL. Ignoring.",
1433 if (m->from_proc_self_mountinfo)
1434 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1436 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1440 case MOUNT_MOUNTING_SIGKILL:
1441 case MOUNT_REMOUNTING_SIGKILL:
1442 case MOUNT_UNMOUNTING_SIGKILL:
1443 log_warning_unit(u->id,
1444 "%s mount process still around after SIGKILL. Ignoring.",
1447 if (m->from_proc_self_mountinfo)
1448 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1450 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1454 assert_not_reached("Timeout at wrong time.");
1458 static int mount_add_one(
1462 const char *options,
1469 char *e, *w = NULL, *o = NULL, *f = NULL;
1471 bool load_extras = false;
1479 /* Ignore API mount points. They should never be referenced in
1480 * dependencies ever. */
1481 if (mount_point_is_api(where) || mount_point_ignore(where))
1484 if (streq(fstype, "autofs"))
1487 /* probably some kind of swap, ignore */
1488 if (!is_path(where))
1491 e = unit_name_from_path(where, ".mount");
1495 u = manager_get_unit(m, e);
1499 u = unit_new(m, sizeof(Mount));
1505 r = unit_add_name(u, e);
1511 MOUNT(u)->where = strdup(where);
1512 if (!MOUNT(u)->where) {
1517 u->source_path = strdup("/proc/self/mountinfo");
1518 if (!u->source_path) {
1523 r = unit_add_dependency_by_name(u, UNIT_BEFORE, SPECIAL_LOCAL_FS_TARGET, NULL, true);
1527 r = unit_add_dependency_by_name(u, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
1531 unit_add_to_load_queue(u);
1536 if (!MOUNT(u)->where) {
1537 MOUNT(u)->where = strdup(where);
1538 if (!MOUNT(u)->where) {
1544 if (u->load_state == UNIT_ERROR) {
1545 u->load_state = UNIT_LOADED;
1548 /* Load in the extras later on, after we
1549 * finished initialization of the unit */
1554 if (!(w = strdup(what)) ||
1555 !(o = strdup(options)) ||
1556 !(f = strdup(fstype))) {
1561 p = &MOUNT(u)->parameters_proc_self_mountinfo;
1563 MOUNT(u)->is_mounted = true;
1564 MOUNT(u)->just_mounted = !MOUNT(u)->from_proc_self_mountinfo;
1565 MOUNT(u)->just_changed = !streq_ptr(p->options, o);
1568 MOUNT(u)->from_proc_self_mountinfo = true;
1582 r = mount_add_extras(MOUNT(u));
1587 unit_add_to_dbus_queue(u);
1602 static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
1605 char *device, *path, *options, *options2, *fstype, *d, *p, *o;
1609 rewind(m->proc_self_mountinfo);
1614 device = path = options = options2 = fstype = d = p = o = NULL;
1616 if ((k = fscanf(m->proc_self_mountinfo,
1617 "%*s " /* (1) mount id */
1618 "%*s " /* (2) parent id */
1619 "%*s " /* (3) major:minor */
1620 "%*s " /* (4) root */
1621 "%ms " /* (5) mount point */
1622 "%ms" /* (6) mount options */
1623 "%*[^-]" /* (7) optional fields */
1624 "- " /* (8) separator */
1625 "%ms " /* (9) file system type */
1626 "%ms" /* (10) mount source */
1627 "%ms" /* (11) mount options 2 */
1628 "%*[^\n]", /* some rubbish at the end */
1638 log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
1642 o = strjoin(options, ",", options2, NULL);
1648 if (!(d = cunescape(device)) ||
1649 !(p = cunescape(path))) {
1654 if ((k = mount_add_one(m, d, p, o, fstype, 0, set_flags)) < 0)
1681 static void mount_shutdown(Manager *m) {
1684 if (m->proc_self_mountinfo) {
1685 fclose(m->proc_self_mountinfo);
1686 m->proc_self_mountinfo = NULL;
1690 static int mount_enumerate(Manager *m) {
1692 struct epoll_event ev;
1695 if (!m->proc_self_mountinfo) {
1696 if (!(m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "re")))
1699 m->mount_watch.type = WATCH_MOUNT;
1700 m->mount_watch.fd = fileno(m->proc_self_mountinfo);
1703 ev.events = EPOLLPRI;
1704 ev.data.ptr = &m->mount_watch;
1706 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->mount_watch.fd, &ev) < 0)
1710 if ((r = mount_load_proc_self_mountinfo(m, false)) < 0)
1720 void mount_fd_event(Manager *m, int events) {
1725 assert(events & EPOLLPRI);
1727 /* The manager calls this for every fd event happening on the
1728 * /proc/self/mountinfo file, which informs us about mounting
1731 r = mount_load_proc_self_mountinfo(m, true);
1733 log_error("Failed to reread /proc/self/mountinfo: %s", strerror(-r));
1735 /* Reset flags, just in case, for later calls */
1736 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1737 Mount *mount = MOUNT(u);
1739 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1745 manager_dispatch_load_queue(m);
1747 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1748 Mount *mount = MOUNT(u);
1750 if (!mount->is_mounted) {
1751 /* This has just been unmounted. */
1753 mount->from_proc_self_mountinfo = false;
1755 switch (mount->state) {
1758 mount_enter_dead(mount, MOUNT_SUCCESS);
1762 mount_set_state(mount, mount->state);
1767 } else if (mount->just_mounted || mount->just_changed) {
1769 /* New or changed mount entry */
1771 switch (mount->state) {
1775 mount_enter_mounted(mount, MOUNT_SUCCESS);
1778 case MOUNT_MOUNTING:
1779 mount_enter_mounting_done(mount);
1783 /* Nothing really changed, but let's
1784 * issue an notification call
1785 * nonetheless, in case somebody is
1786 * waiting for this. (e.g. file system
1787 * ro/rw remounts.) */
1788 mount_set_state(mount, mount->state);
1793 /* Reset the flags for later calls */
1794 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1798 static void mount_reset_failed(Unit *u) {
1799 Mount *m = MOUNT(u);
1803 if (m->state == MOUNT_FAILED)
1804 mount_set_state(m, MOUNT_DEAD);
1806 m->result = MOUNT_SUCCESS;
1807 m->reload_result = MOUNT_SUCCESS;
1810 static int mount_kill(Unit *u, KillWho who, int signo, DBusError *error) {
1811 return unit_kill_common(u, who, signo, -1, MOUNT(u)->control_pid, error);
1814 static const char* const mount_state_table[_MOUNT_STATE_MAX] = {
1815 [MOUNT_DEAD] = "dead",
1816 [MOUNT_MOUNTING] = "mounting",
1817 [MOUNT_MOUNTING_DONE] = "mounting-done",
1818 [MOUNT_MOUNTED] = "mounted",
1819 [MOUNT_REMOUNTING] = "remounting",
1820 [MOUNT_UNMOUNTING] = "unmounting",
1821 [MOUNT_MOUNTING_SIGTERM] = "mounting-sigterm",
1822 [MOUNT_MOUNTING_SIGKILL] = "mounting-sigkill",
1823 [MOUNT_REMOUNTING_SIGTERM] = "remounting-sigterm",
1824 [MOUNT_REMOUNTING_SIGKILL] = "remounting-sigkill",
1825 [MOUNT_UNMOUNTING_SIGTERM] = "unmounting-sigterm",
1826 [MOUNT_UNMOUNTING_SIGKILL] = "unmounting-sigkill",
1827 [MOUNT_FAILED] = "failed"
1830 DEFINE_STRING_TABLE_LOOKUP(mount_state, MountState);
1832 static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
1833 [MOUNT_EXEC_MOUNT] = "ExecMount",
1834 [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
1835 [MOUNT_EXEC_REMOUNT] = "ExecRemount",
1838 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
1840 static const char* const mount_result_table[_MOUNT_RESULT_MAX] = {
1841 [MOUNT_SUCCESS] = "success",
1842 [MOUNT_FAILURE_RESOURCES] = "resources",
1843 [MOUNT_FAILURE_TIMEOUT] = "timeout",
1844 [MOUNT_FAILURE_EXIT_CODE] = "exit-code",
1845 [MOUNT_FAILURE_SIGNAL] = "signal",
1846 [MOUNT_FAILURE_CORE_DUMP] = "core-dump"
1849 DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult);
1851 const UnitVTable mount_vtable = {
1852 .object_size = sizeof(Mount),
1859 .exec_context_offset = offsetof(Mount, exec_context),
1860 .exec_section = "Mount",
1863 .no_instances = true,
1869 .coldplug = mount_coldplug,
1873 .start = mount_start,
1875 .reload = mount_reload,
1879 .serialize = mount_serialize,
1880 .deserialize_item = mount_deserialize_item,
1882 .active_state = mount_active_state,
1883 .sub_state_to_string = mount_sub_state_to_string,
1885 .check_gc = mount_check_gc,
1887 .sigchld_event = mount_sigchld_event,
1888 .timer_event = mount_timer_event,
1890 .reset_failed = mount_reset_failed,
1892 .bus_interface = "org.freedesktop.systemd1.Mount",
1893 .bus_message_handler = bus_mount_message_handler,
1894 .bus_invalidating_properties = bus_mount_invalidating_properties,
1896 .enumerate = mount_enumerate,
1897 .shutdown = mount_shutdown,
1899 .status_message_formats = {
1900 .starting_stopping = {
1901 [0] = "Mounting %s...",
1902 [1] = "Unmounting %s...",
1904 .finished_start_job = {
1905 [JOB_DONE] = "Mounted %s.",
1906 [JOB_FAILED] = "Failed to mount %s.",
1907 [JOB_DEPENDENCY] = "Dependency failed for %s.",
1908 [JOB_TIMEOUT] = "Timed out mounting %s.",
1910 .finished_stop_job = {
1911 [JOB_DONE] = "Unmounted %s.",
1912 [JOB_FAILED] = "Failed unmounting %s.",
1913 [JOB_TIMEOUT] = "Timed out unmounting %s.",