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 char* mount_test_option(const char *haystack, const char *needle) {
63 struct mntent me = { .mnt_opts = (char*) haystack };
67 /* Like glibc's hasmntopt(), but works on a string, not a
73 return hasmntopt(&me, needle);
76 static bool mount_is_network(MountParameters *p) {
79 if (mount_test_option(p->options, "_netdev"))
82 if (p->fstype && fstype_is_network(p->fstype))
88 static bool mount_is_bind(MountParameters *p) {
91 if (mount_test_option(p->options, "bind"))
94 if (p->fstype && streq(p->fstype, "bind"))
97 if (mount_test_option(p->options, "rbind"))
100 if (p->fstype && streq(p->fstype, "rbind"))
106 static bool mount_is_auto(MountParameters *p) {
109 return !mount_test_option(p->options, "noauto");
112 static bool needs_quota(MountParameters *p) {
115 if (mount_is_network(p))
118 if (mount_is_bind(p))
121 return mount_test_option(p->options, "usrquota") ||
122 mount_test_option(p->options, "grpquota") ||
123 mount_test_option(p->options, "quota") ||
124 mount_test_option(p->options, "usrjquota") ||
125 mount_test_option(p->options, "grpjquota");
128 static void mount_init(Unit *u) {
132 assert(u->load_state == UNIT_STUB);
134 m->timeout_usec = DEFAULT_TIMEOUT_USEC;
135 m->directory_mode = 0755;
137 exec_context_init(&m->exec_context);
139 if (unit_has_name(u, "-.mount")) {
140 /* Don't allow start/stop for root directory */
141 UNIT(m)->refuse_manual_start = true;
142 UNIT(m)->refuse_manual_stop = true;
144 /* The stdio/kmsg bridge socket is on /, in order to avoid a
145 * dep loop, don't use kmsg logging for -.mount */
146 m->exec_context.std_output = u->manager->default_std_output;
147 m->exec_context.std_error = u->manager->default_std_error;
150 kill_context_init(&m->kill_context);
151 cgroup_context_init(&m->cgroup_context);
153 /* We need to make sure that /bin/mount is always called in
154 * the same process group as us, so that the autofs kernel
155 * side doesn't send us another mount request while we are
156 * already trying to comply its last one. */
157 m->exec_context.same_pgrp = true;
159 m->timer_watch.type = WATCH_INVALID;
161 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
163 UNIT(m)->ignore_on_isolate = true;
166 static void mount_unwatch_control_pid(Mount *m) {
169 if (m->control_pid <= 0)
172 unit_unwatch_pid(UNIT(m), m->control_pid);
176 static void mount_parameters_done(MountParameters *p) {
183 p->what = p->options = p->fstype = NULL;
186 static void mount_done(Unit *u) {
194 mount_parameters_done(&m->parameters_proc_self_mountinfo);
195 mount_parameters_done(&m->parameters_fragment);
197 cgroup_context_done(&m->cgroup_context);
198 exec_context_done(&m->exec_context, manager_is_reloading_or_reexecuting(u->manager));
199 exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
200 m->control_command = NULL;
202 mount_unwatch_control_pid(m);
204 unit_unwatch_timer(u, &m->timer_watch);
207 _pure_ static MountParameters* get_mount_parameters_fragment(Mount *m) {
210 if (m->from_fragment)
211 return &m->parameters_fragment;
216 _pure_ static MountParameters* get_mount_parameters(Mount *m) {
219 if (m->from_proc_self_mountinfo)
220 return &m->parameters_proc_self_mountinfo;
222 return get_mount_parameters_fragment(m);
225 static int mount_add_mount_links(Mount *m) {
226 _cleanup_free_ char *parent = NULL;
235 if (!path_equal(m->where, "/")) {
236 /* Adds in links to other mount points that might lie further
237 * up in the hierarchy */
238 r = path_get_parent(m->where, &parent);
242 r = unit_require_mounts_for(UNIT(m), parent);
247 /* Adds in links to other mount points that might be needed
248 * for the source path (if this is a bind mount) to be
250 pm = get_mount_parameters_fragment(m);
251 if (pm && pm->what &&
252 path_is_absolute(pm->what) &&
253 !mount_is_network(pm)) {
255 r = unit_require_mounts_for(UNIT(m), pm->what);
260 /* Adds in links to other units that use this path or paths
261 * further down in the hierarchy */
262 s = manager_get_units_requiring_mounts_for(UNIT(m)->manager, m->where);
263 SET_FOREACH(other, s, i) {
265 if (other->load_state != UNIT_LOADED)
268 if (other == UNIT(m))
271 r = unit_add_dependency(other, UNIT_AFTER, UNIT(m), true);
275 if (UNIT(m)->fragment_path) {
276 /* If we have fragment configuration, then make this dependency required */
277 r = unit_add_dependency(other, UNIT_REQUIRES, UNIT(m), true);
286 static int mount_add_device_links(Mount *m) {
288 bool device_wants_mount = false;
293 p = get_mount_parameters_fragment(m);
300 if (mount_is_bind(p))
303 if (!is_device_path(p->what))
306 if (path_equal(m->where, "/"))
309 if (mount_is_auto(p) && UNIT(m)->manager->running_as == SYSTEMD_SYSTEM)
310 device_wants_mount = true;
312 r = unit_add_node_link(UNIT(m), p->what, device_wants_mount);
317 UNIT(m)->manager->running_as == SYSTEMD_SYSTEM) {
320 /* Let's add in the fsck service */
322 /* aka SPECIAL_FSCK_SERVICE */
323 name = unit_name_from_path_instance("systemd-fsck", p->what, ".service");
327 r = manager_load_unit_prepare(UNIT(m)->manager, name, NULL, NULL, &fsck);
329 log_warning_unit(name,
330 "Failed to prepare unit %s: %s", name, strerror(-r));
336 SERVICE(fsck)->fsck_passno = p->passno;
338 r = unit_add_two_dependencies(UNIT(m), UNIT_AFTER, UNIT_REQUIRES, fsck, true);
346 static int mount_add_quota_links(Mount *m) {
352 if (UNIT(m)->manager->running_as != SYSTEMD_SYSTEM)
355 p = get_mount_parameters_fragment(m);
362 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTACHECK_SERVICE, NULL, true);
366 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTAON_SERVICE, NULL, true);
373 static bool should_umount(Mount *m) {
376 if (path_equal(m->where, "/") ||
377 path_equal(m->where, "/usr"))
380 p = get_mount_parameters(m);
381 if (p && mount_test_option(p->options, "x-initrd.mount") &&
388 static int mount_add_default_dependencies(Mount *m) {
389 const char *after, *after2, *online;
395 if (UNIT(m)->manager->running_as != SYSTEMD_SYSTEM)
398 p = get_mount_parameters(m);
403 if (path_equal(m->where, "/"))
406 if (mount_is_network(p)) {
407 after = SPECIAL_REMOTE_FS_PRE_TARGET;
408 after2 = SPECIAL_NETWORK_TARGET;
409 online = SPECIAL_NETWORK_ONLINE_TARGET;
411 after = SPECIAL_LOCAL_FS_PRE_TARGET;
416 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after, NULL, true);
421 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after2, NULL, true);
427 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_WANTS, UNIT_AFTER, online, NULL, true);
432 if (should_umount(m)) {
433 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
441 static int mount_fix_timeouts(Mount *m) {
443 const char *timeout = NULL;
452 p = get_mount_parameters_fragment(m);
456 /* Allow configuration how long we wait for a device that
457 * backs a mount point to show up. This is useful to support
458 * endless device timeouts for devices that show up only after
459 * user input, like crypto devices. */
461 if ((timeout = mount_test_option(p->options, "comment=systemd.device-timeout")))
463 else if ((timeout = mount_test_option(p->options, "x-systemd.device-timeout")))
468 t = strndup(timeout, strcspn(timeout, ",;" WHITESPACE));
472 r = parse_sec(t, &u);
476 log_warning_unit(UNIT(m)->id,
477 "Failed to parse timeout for %s, ignoring: %s",
482 SET_FOREACH(other, UNIT(m)->dependencies[UNIT_AFTER], i) {
483 if (other->type != UNIT_DEVICE)
486 other->job_timeout = u;
492 static int mount_verify(Mount *m) {
493 _cleanup_free_ char *e = NULL;
498 if (UNIT(m)->load_state != UNIT_LOADED)
501 if (!m->from_fragment && !m->from_proc_self_mountinfo)
504 e = unit_name_from_path(m->where, ".mount");
508 b = unit_has_name(UNIT(m), e);
510 log_error_unit(UNIT(m)->id,
511 "%s's Where setting doesn't match unit name. Refusing.",
516 if (mount_point_is_api(m->where) || mount_point_ignore(m->where)) {
517 log_error_unit(UNIT(m)->id,
518 "Cannot create mount unit for API file system %s. Refusing.",
523 if (UNIT(m)->fragment_path && !m->parameters_fragment.what) {
524 log_error_unit(UNIT(m)->id,
525 "%s's What setting is missing. Refusing.", UNIT(m)->id);
529 if (m->exec_context.pam_name && m->kill_context.kill_mode != KILL_CONTROL_GROUP) {
530 log_error_unit(UNIT(m)->id,
531 "%s has PAM enabled. Kill mode must be set to control-group'. Refusing.",
539 static int mount_add_extras(Mount *m) {
543 if (UNIT(m)->fragment_path)
544 m->from_fragment = true;
547 m->where = unit_name_to_path(u->id);
552 path_kill_slashes(m->where);
554 r = unit_add_exec_dependencies(u, &m->exec_context);
558 if (!UNIT(m)->description) {
559 r = unit_set_description(u, m->where);
564 r = mount_add_device_links(m);
568 r = mount_add_mount_links(m);
572 r = mount_add_quota_links(m);
576 if (UNIT(m)->default_dependencies) {
577 r = mount_add_default_dependencies(m);
582 r = unit_add_default_slice(u);
586 r = mount_fix_timeouts(m);
593 static int mount_load(Unit *u) {
598 assert(u->load_state == UNIT_STUB);
600 if (m->from_proc_self_mountinfo)
601 r = unit_load_fragment_and_dropin_optional(u);
603 r = unit_load_fragment_and_dropin(u);
608 /* This is a new unit? Then let's add in some extras */
609 if (u->load_state == UNIT_LOADED) {
610 r = mount_add_extras(m);
614 r = unit_exec_context_defaults(u, &m->exec_context);
619 return mount_verify(m);
622 static int mount_notify_automount(Mount *m, int status) {
629 SET_FOREACH(p, UNIT(m)->dependencies[UNIT_TRIGGERED_BY], i)
630 if (p->type == UNIT_AUTOMOUNT) {
631 r = automount_send_ready(AUTOMOUNT(p), status);
639 static void mount_set_state(Mount *m, MountState state) {
640 MountState old_state;
643 old_state = m->state;
646 if (state != MOUNT_MOUNTING &&
647 state != MOUNT_MOUNTING_DONE &&
648 state != MOUNT_REMOUNTING &&
649 state != MOUNT_UNMOUNTING &&
650 state != MOUNT_MOUNTING_SIGTERM &&
651 state != MOUNT_MOUNTING_SIGKILL &&
652 state != MOUNT_UNMOUNTING_SIGTERM &&
653 state != MOUNT_UNMOUNTING_SIGKILL &&
654 state != MOUNT_REMOUNTING_SIGTERM &&
655 state != MOUNT_REMOUNTING_SIGKILL) {
656 unit_unwatch_timer(UNIT(m), &m->timer_watch);
657 mount_unwatch_control_pid(m);
658 m->control_command = NULL;
659 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
662 if (state == MOUNT_MOUNTED ||
663 state == MOUNT_REMOUNTING)
664 mount_notify_automount(m, 0);
665 else if (state == MOUNT_DEAD ||
666 state == MOUNT_UNMOUNTING ||
667 state == MOUNT_MOUNTING_SIGTERM ||
668 state == MOUNT_MOUNTING_SIGKILL ||
669 state == MOUNT_REMOUNTING_SIGTERM ||
670 state == MOUNT_REMOUNTING_SIGKILL ||
671 state == MOUNT_UNMOUNTING_SIGTERM ||
672 state == MOUNT_UNMOUNTING_SIGKILL ||
673 state == MOUNT_FAILED) {
674 if (state != old_state)
675 mount_notify_automount(m, -ENODEV);
678 if (state != old_state)
679 log_debug_unit(UNIT(m)->id,
680 "%s changed %s -> %s",
682 mount_state_to_string(old_state),
683 mount_state_to_string(state));
685 unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state], m->reload_result == MOUNT_SUCCESS);
686 m->reload_result = MOUNT_SUCCESS;
689 static int mount_coldplug(Unit *u) {
691 MountState new_state = MOUNT_DEAD;
695 assert(m->state == MOUNT_DEAD);
697 if (m->deserialized_state != m->state)
698 new_state = m->deserialized_state;
699 else if (m->from_proc_self_mountinfo)
700 new_state = MOUNT_MOUNTED;
702 if (new_state != m->state) {
704 if (new_state == MOUNT_MOUNTING ||
705 new_state == MOUNT_MOUNTING_DONE ||
706 new_state == MOUNT_REMOUNTING ||
707 new_state == MOUNT_UNMOUNTING ||
708 new_state == MOUNT_MOUNTING_SIGTERM ||
709 new_state == MOUNT_MOUNTING_SIGKILL ||
710 new_state == MOUNT_UNMOUNTING_SIGTERM ||
711 new_state == MOUNT_UNMOUNTING_SIGKILL ||
712 new_state == MOUNT_REMOUNTING_SIGTERM ||
713 new_state == MOUNT_REMOUNTING_SIGKILL) {
715 if (m->control_pid <= 0)
718 r = unit_watch_pid(UNIT(m), m->control_pid);
722 r = unit_watch_timer(UNIT(m), CLOCK_MONOTONIC, true, m->timeout_usec, &m->timer_watch);
727 mount_set_state(m, new_state);
733 static void mount_dump(Unit *u, FILE *f, const char *prefix) {
740 p = get_mount_parameters(m);
743 "%sMount State: %s\n"
747 "%sFile System Type: %s\n"
749 "%sFrom /proc/self/mountinfo: %s\n"
750 "%sFrom fragment: %s\n"
751 "%sDirectoryMode: %04o\n",
752 prefix, mount_state_to_string(m->state),
753 prefix, mount_result_to_string(m->result),
755 prefix, p ? strna(p->what) : "n/a",
756 prefix, p ? strna(p->fstype) : "n/a",
757 prefix, p ? strna(p->options) : "n/a",
758 prefix, yes_no(m->from_proc_self_mountinfo),
759 prefix, yes_no(m->from_fragment),
760 prefix, m->directory_mode);
762 if (m->control_pid > 0)
764 "%sControl PID: %lu\n",
765 prefix, (unsigned long) m->control_pid);
767 exec_context_dump(&m->exec_context, f, prefix);
768 kill_context_dump(&m->kill_context, f, prefix);
771 static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
779 unit_realize_cgroup(UNIT(m));
781 r = unit_watch_timer(UNIT(m), CLOCK_MONOTONIC, true, m->timeout_usec, &m->timer_watch);
789 UNIT(m)->manager->environment,
793 UNIT(m)->manager->confirm_spawn,
794 UNIT(m)->manager->cgroup_supported,
795 UNIT(m)->cgroup_path,
802 r = unit_watch_pid(UNIT(m), pid);
804 /* FIXME: we need to do something here */
812 unit_unwatch_timer(UNIT(m), &m->timer_watch);
817 static void mount_enter_dead(Mount *m, MountResult f) {
820 if (f != MOUNT_SUCCESS)
823 exec_context_tmp_dirs_done(&m->exec_context);
824 mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD);
827 static void mount_enter_mounted(Mount *m, MountResult f) {
830 if (f != MOUNT_SUCCESS)
833 mount_set_state(m, MOUNT_MOUNTED);
836 static void mount_enter_signal(Mount *m, MountState state, MountResult f) {
841 if (f != MOUNT_SUCCESS)
844 r = unit_kill_context(
847 state != MOUNT_MOUNTING_SIGTERM && state != MOUNT_UNMOUNTING_SIGTERM && state != MOUNT_REMOUNTING_SIGTERM,
855 r = unit_watch_timer(UNIT(m), CLOCK_MONOTONIC, true, m->timeout_usec, &m->timer_watch);
859 mount_set_state(m, state);
860 } else if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
861 mount_enter_mounted(m, MOUNT_SUCCESS);
863 mount_enter_dead(m, MOUNT_SUCCESS);
868 log_warning_unit(UNIT(m)->id,
869 "%s failed to kill processes: %s", UNIT(m)->id, strerror(-r));
871 if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
872 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
874 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
877 void warn_if_dir_nonempty(const char *unit, const char* where) {
881 if (dir_is_empty(where) > 0)
884 log_struct_unit(LOG_NOTICE,
886 "MESSAGE=%s: Directory %s to mount over is not empty, mounting anyway.",
889 MESSAGE_ID(SD_MESSAGE_OVERMOUNTING),
893 static void mount_enter_unmounting(Mount *m) {
898 m->control_command_id = MOUNT_EXEC_UNMOUNT;
899 m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
901 if ((r = exec_command_set(
908 mount_unwatch_control_pid(m);
910 if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
913 mount_set_state(m, MOUNT_UNMOUNTING);
918 log_warning_unit(UNIT(m)->id,
919 "%s failed to run 'umount' task: %s",
920 UNIT(m)->id, strerror(-r));
921 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
924 static void mount_enter_mounting(Mount *m) {
930 m->control_command_id = MOUNT_EXEC_MOUNT;
931 m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
933 mkdir_p_label(m->where, m->directory_mode);
935 warn_if_dir_nonempty(m->meta.id, m->where);
937 /* Create the source directory for bind-mounts if needed */
938 p = get_mount_parameters_fragment(m);
939 if (p && mount_is_bind(p))
940 mkdir_p_label(p->what, m->directory_mode);
942 if (m->from_fragment)
943 r = exec_command_set(
946 m->parameters_fragment.what,
948 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
949 m->parameters_fragment.options ? "-o" : NULL, m->parameters_fragment.options,
957 mount_unwatch_control_pid(m);
959 r = mount_spawn(m, m->control_command, &m->control_pid);
963 mount_set_state(m, MOUNT_MOUNTING);
968 log_warning_unit(UNIT(m)->id,
969 "%s failed to run 'mount' task: %s",
970 UNIT(m)->id, strerror(-r));
971 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
974 static void mount_enter_mounting_done(Mount *m) {
977 mount_set_state(m, MOUNT_MOUNTING_DONE);
980 static void mount_enter_remounting(Mount *m) {
985 m->control_command_id = MOUNT_EXEC_REMOUNT;
986 m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
988 if (m->from_fragment) {
992 if (m->parameters_fragment.options) {
993 if (!(buf = strappend("remount,", m->parameters_fragment.options))) {
1002 r = exec_command_set(
1005 m->parameters_fragment.what,
1007 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
1018 mount_unwatch_control_pid(m);
1020 if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
1023 mount_set_state(m, MOUNT_REMOUNTING);
1028 log_warning_unit(UNIT(m)->id,
1029 "%s failed to run 'remount' task: %s",
1030 UNIT(m)->id, strerror(-r));
1031 m->reload_result = MOUNT_FAILURE_RESOURCES;
1032 mount_enter_mounted(m, MOUNT_SUCCESS);
1035 static int mount_start(Unit *u) {
1036 Mount *m = MOUNT(u);
1040 /* We cannot fulfill this request right now, try again later
1042 if (m->state == MOUNT_UNMOUNTING ||
1043 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1044 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1045 m->state == MOUNT_MOUNTING_SIGTERM ||
1046 m->state == MOUNT_MOUNTING_SIGKILL)
1049 /* Already on it! */
1050 if (m->state == MOUNT_MOUNTING)
1053 assert(m->state == MOUNT_DEAD || m->state == MOUNT_FAILED);
1055 m->result = MOUNT_SUCCESS;
1056 m->reload_result = MOUNT_SUCCESS;
1058 mount_enter_mounting(m);
1062 static int mount_stop(Unit *u) {
1063 Mount *m = MOUNT(u);
1068 if (m->state == MOUNT_UNMOUNTING ||
1069 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1070 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1071 m->state == MOUNT_MOUNTING_SIGTERM ||
1072 m->state == MOUNT_MOUNTING_SIGKILL)
1075 assert(m->state == MOUNT_MOUNTING ||
1076 m->state == MOUNT_MOUNTING_DONE ||
1077 m->state == MOUNT_MOUNTED ||
1078 m->state == MOUNT_REMOUNTING ||
1079 m->state == MOUNT_REMOUNTING_SIGTERM ||
1080 m->state == MOUNT_REMOUNTING_SIGKILL);
1082 mount_enter_unmounting(m);
1086 static int mount_reload(Unit *u) {
1087 Mount *m = MOUNT(u);
1091 if (m->state == MOUNT_MOUNTING_DONE)
1094 assert(m->state == MOUNT_MOUNTED);
1096 mount_enter_remounting(m);
1100 static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
1101 Mount *m = MOUNT(u);
1107 unit_serialize_item(u, f, "state", mount_state_to_string(m->state));
1108 unit_serialize_item(u, f, "result", mount_result_to_string(m->result));
1109 unit_serialize_item(u, f, "reload-result", mount_result_to_string(m->reload_result));
1111 if (m->control_pid > 0)
1112 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) m->control_pid);
1114 if (m->control_command_id >= 0)
1115 unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(m->control_command_id));
1117 exec_context_serialize(&m->exec_context, UNIT(m), f);
1122 static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1123 Mount *m = MOUNT(u);
1130 if (streq(key, "state")) {
1133 if ((state = mount_state_from_string(value)) < 0)
1134 log_debug_unit(u->id, "Failed to parse state value %s", value);
1136 m->deserialized_state = state;
1137 } else if (streq(key, "result")) {
1140 f = mount_result_from_string(value);
1142 log_debug_unit(UNIT(m)->id,
1143 "Failed to parse result value %s", value);
1144 else if (f != MOUNT_SUCCESS)
1147 } else if (streq(key, "reload-result")) {
1150 f = mount_result_from_string(value);
1152 log_debug_unit(UNIT(m)->id,
1153 "Failed to parse reload result value %s", value);
1154 else if (f != MOUNT_SUCCESS)
1155 m->reload_result = f;
1157 } else if (streq(key, "control-pid")) {
1160 if (parse_pid(value, &pid) < 0)
1161 log_debug_unit(UNIT(m)->id,
1162 "Failed to parse control-pid value %s", value);
1164 m->control_pid = pid;
1165 } else if (streq(key, "control-command")) {
1166 MountExecCommand id;
1168 if ((id = mount_exec_command_from_string(value)) < 0)
1169 log_debug_unit(UNIT(m)->id,
1170 "Failed to parse exec-command value %s", value);
1172 m->control_command_id = id;
1173 m->control_command = m->exec_command + id;
1175 } else if (streq(key, "tmp-dir")) {
1182 m->exec_context.tmp_dir = t;
1183 } else if (streq(key, "var-tmp-dir")) {
1190 m->exec_context.var_tmp_dir = t;
1192 log_debug_unit(UNIT(m)->id,
1193 "Unknown serialization key '%s'", key);
1198 _pure_ static UnitActiveState mount_active_state(Unit *u) {
1201 return state_translation_table[MOUNT(u)->state];
1204 _pure_ static const char *mount_sub_state_to_string(Unit *u) {
1207 return mount_state_to_string(MOUNT(u)->state);
1210 _pure_ static bool mount_check_gc(Unit *u) {
1211 Mount *m = MOUNT(u);
1215 return m->from_proc_self_mountinfo;
1218 static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1219 Mount *m = MOUNT(u);
1225 if (pid != m->control_pid)
1230 if (is_clean_exit(code, status, NULL))
1232 else if (code == CLD_EXITED)
1233 f = MOUNT_FAILURE_EXIT_CODE;
1234 else if (code == CLD_KILLED)
1235 f = MOUNT_FAILURE_SIGNAL;
1236 else if (code == CLD_DUMPED)
1237 f = MOUNT_FAILURE_CORE_DUMP;
1239 assert_not_reached("Unknown code");
1241 if (f != MOUNT_SUCCESS)
1244 if (m->control_command) {
1245 exec_status_exit(&m->control_command->exec_status, &m->exec_context, pid, code, status);
1247 m->control_command = NULL;
1248 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
1251 log_full_unit(f == MOUNT_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
1252 "%s mount process exited, code=%s status=%i",
1253 u->id, sigchld_code_to_string(code), status);
1255 /* Note that mount(8) returning and the kernel sending us a
1256 * mount table change event might happen out-of-order. If an
1257 * operation succeed we assume the kernel will follow soon too
1258 * and already change into the resulting state. If it fails
1259 * we check if the kernel still knows about the mount. and
1260 * change state accordingly. */
1264 case MOUNT_MOUNTING:
1265 case MOUNT_MOUNTING_DONE:
1266 case MOUNT_MOUNTING_SIGKILL:
1267 case MOUNT_MOUNTING_SIGTERM:
1269 if (f == MOUNT_SUCCESS)
1270 mount_enter_mounted(m, f);
1271 else if (m->from_proc_self_mountinfo)
1272 mount_enter_mounted(m, f);
1274 mount_enter_dead(m, f);
1277 case MOUNT_REMOUNTING:
1278 case MOUNT_REMOUNTING_SIGKILL:
1279 case MOUNT_REMOUNTING_SIGTERM:
1281 m->reload_result = f;
1282 if (m->from_proc_self_mountinfo)
1283 mount_enter_mounted(m, MOUNT_SUCCESS);
1285 mount_enter_dead(m, MOUNT_SUCCESS);
1289 case MOUNT_UNMOUNTING:
1290 case MOUNT_UNMOUNTING_SIGKILL:
1291 case MOUNT_UNMOUNTING_SIGTERM:
1293 if (f == MOUNT_SUCCESS)
1294 mount_enter_dead(m, f);
1295 else if (m->from_proc_self_mountinfo)
1296 mount_enter_mounted(m, f);
1298 mount_enter_dead(m, f);
1302 assert_not_reached("Uh, control process died at wrong time.");
1305 /* Notify clients about changed exit status */
1306 unit_add_to_dbus_queue(u);
1309 static void mount_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
1310 Mount *m = MOUNT(u);
1313 assert(elapsed == 1);
1314 assert(w == &m->timer_watch);
1318 case MOUNT_MOUNTING:
1319 case MOUNT_MOUNTING_DONE:
1320 log_warning_unit(u->id,
1321 "%s mounting timed out. Stopping.", u->id);
1322 mount_enter_signal(m, MOUNT_MOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1325 case MOUNT_REMOUNTING:
1326 log_warning_unit(u->id,
1327 "%s remounting timed out. Stopping.", u->id);
1328 m->reload_result = MOUNT_FAILURE_TIMEOUT;
1329 mount_enter_mounted(m, MOUNT_SUCCESS);
1332 case MOUNT_UNMOUNTING:
1333 log_warning_unit(u->id,
1334 "%s unmounting timed out. Stopping.", u->id);
1335 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1338 case MOUNT_MOUNTING_SIGTERM:
1339 if (m->kill_context.send_sigkill) {
1340 log_warning_unit(u->id,
1341 "%s mounting timed out. Killing.", u->id);
1342 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1344 log_warning_unit(u->id,
1345 "%s mounting timed out. Skipping SIGKILL. Ignoring.",
1348 if (m->from_proc_self_mountinfo)
1349 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1351 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1355 case MOUNT_REMOUNTING_SIGTERM:
1356 if (m->kill_context.send_sigkill) {
1357 log_warning_unit(u->id,
1358 "%s remounting timed out. Killing.", u->id);
1359 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1361 log_warning_unit(u->id,
1362 "%s remounting timed out. Skipping SIGKILL. Ignoring.",
1365 if (m->from_proc_self_mountinfo)
1366 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1368 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1372 case MOUNT_UNMOUNTING_SIGTERM:
1373 if (m->kill_context.send_sigkill) {
1374 log_warning_unit(u->id,
1375 "%s unmounting timed out. Killing.", u->id);
1376 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1378 log_warning_unit(u->id,
1379 "%s unmounting timed out. Skipping SIGKILL. Ignoring.",
1382 if (m->from_proc_self_mountinfo)
1383 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1385 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1389 case MOUNT_MOUNTING_SIGKILL:
1390 case MOUNT_REMOUNTING_SIGKILL:
1391 case MOUNT_UNMOUNTING_SIGKILL:
1392 log_warning_unit(u->id,
1393 "%s mount process still around after SIGKILL. Ignoring.",
1396 if (m->from_proc_self_mountinfo)
1397 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1399 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1403 assert_not_reached("Timeout at wrong time.");
1407 static int mount_add_one(
1411 const char *options,
1418 char *e, *w = NULL, *o = NULL, *f = NULL;
1420 bool load_extras = false;
1428 /* Ignore API mount points. They should never be referenced in
1429 * dependencies ever. */
1430 if (mount_point_is_api(where) || mount_point_ignore(where))
1433 if (streq(fstype, "autofs"))
1436 /* probably some kind of swap, ignore */
1437 if (!is_path(where))
1440 e = unit_name_from_path(where, ".mount");
1444 u = manager_get_unit(m, e);
1446 const char* const target =
1447 fstype_is_network(fstype) ? SPECIAL_REMOTE_FS_TARGET : SPECIAL_LOCAL_FS_TARGET;
1451 u = unit_new(m, sizeof(Mount));
1457 r = unit_add_name(u, e);
1463 MOUNT(u)->where = strdup(where);
1464 if (!MOUNT(u)->where) {
1469 u->source_path = strdup("/proc/self/mountinfo");
1470 if (!u->source_path) {
1475 r = unit_add_dependency_by_name(u, UNIT_BEFORE, target, NULL, true);
1479 if (should_umount(MOUNT(u))) {
1480 r = unit_add_dependency_by_name(u, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
1485 unit_add_to_load_queue(u);
1490 if (!MOUNT(u)->where) {
1491 MOUNT(u)->where = strdup(where);
1492 if (!MOUNT(u)->where) {
1498 if (u->load_state == UNIT_NOT_FOUND) {
1499 u->load_state = UNIT_LOADED;
1502 /* Load in the extras later on, after we
1503 * finished initialization of the unit */
1508 if (!(w = strdup(what)) ||
1509 !(o = strdup(options)) ||
1510 !(f = strdup(fstype))) {
1515 p = &MOUNT(u)->parameters_proc_self_mountinfo;
1517 MOUNT(u)->is_mounted = true;
1518 MOUNT(u)->just_mounted = !MOUNT(u)->from_proc_self_mountinfo;
1519 MOUNT(u)->just_changed = !streq_ptr(p->options, o);
1522 MOUNT(u)->from_proc_self_mountinfo = true;
1536 r = mount_add_extras(MOUNT(u));
1541 unit_add_to_dbus_queue(u);
1556 static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
1562 rewind(m->proc_self_mountinfo);
1565 _cleanup_free_ char *device = NULL, *path = NULL, *options = NULL, *options2 = NULL, *fstype = NULL, *d = NULL, *p = NULL, *o = NULL;
1568 k = fscanf(m->proc_self_mountinfo,
1569 "%*s " /* (1) mount id */
1570 "%*s " /* (2) parent id */
1571 "%*s " /* (3) major:minor */
1572 "%*s " /* (4) root */
1573 "%ms " /* (5) mount point */
1574 "%ms" /* (6) mount options */
1575 "%*[^-]" /* (7) optional fields */
1576 "- " /* (8) separator */
1577 "%ms " /* (9) file system type */
1578 "%ms" /* (10) mount source */
1579 "%ms" /* (11) mount options 2 */
1580 "%*[^\n]", /* some rubbish at the end */
1591 log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
1595 o = strjoin(options, ",", options2, NULL);
1599 d = cunescape(device);
1600 p = cunescape(path);
1604 k = mount_add_one(m, d, p, o, fstype, 0, set_flags);
1612 static void mount_shutdown(Manager *m) {
1615 if (m->proc_self_mountinfo) {
1616 fclose(m->proc_self_mountinfo);
1617 m->proc_self_mountinfo = NULL;
1621 static int mount_enumerate(Manager *m) {
1625 if (!m->proc_self_mountinfo) {
1626 struct epoll_event ev = {
1628 .data.ptr = &m->mount_watch,
1631 m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
1632 if (!m->proc_self_mountinfo)
1635 m->mount_watch.type = WATCH_MOUNT;
1636 m->mount_watch.fd = fileno(m->proc_self_mountinfo);
1638 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->mount_watch.fd, &ev) < 0)
1642 r = mount_load_proc_self_mountinfo(m, false);
1653 void mount_fd_event(Manager *m, int events) {
1658 assert(events & EPOLLPRI);
1660 /* The manager calls this for every fd event happening on the
1661 * /proc/self/mountinfo file, which informs us about mounting
1664 r = mount_load_proc_self_mountinfo(m, true);
1666 log_error("Failed to reread /proc/self/mountinfo: %s", strerror(-r));
1668 /* Reset flags, just in case, for later calls */
1669 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1670 Mount *mount = MOUNT(u);
1672 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1678 manager_dispatch_load_queue(m);
1680 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1681 Mount *mount = MOUNT(u);
1683 if (!mount->is_mounted) {
1684 /* This has just been unmounted. */
1686 mount->from_proc_self_mountinfo = false;
1688 switch (mount->state) {
1691 mount_enter_dead(mount, MOUNT_SUCCESS);
1695 mount_set_state(mount, mount->state);
1700 } else if (mount->just_mounted || mount->just_changed) {
1702 /* New or changed mount entry */
1704 switch (mount->state) {
1708 mount_enter_mounted(mount, MOUNT_SUCCESS);
1711 case MOUNT_MOUNTING:
1712 mount_enter_mounting_done(mount);
1716 /* Nothing really changed, but let's
1717 * issue an notification call
1718 * nonetheless, in case somebody is
1719 * waiting for this. (e.g. file system
1720 * ro/rw remounts.) */
1721 mount_set_state(mount, mount->state);
1726 /* Reset the flags for later calls */
1727 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1731 static void mount_reset_failed(Unit *u) {
1732 Mount *m = MOUNT(u);
1736 if (m->state == MOUNT_FAILED)
1737 mount_set_state(m, MOUNT_DEAD);
1739 m->result = MOUNT_SUCCESS;
1740 m->reload_result = MOUNT_SUCCESS;
1743 static int mount_kill(Unit *u, KillWho who, int signo, DBusError *error) {
1744 return unit_kill_common(u, who, signo, -1, MOUNT(u)->control_pid, error);
1747 static const char* const mount_state_table[_MOUNT_STATE_MAX] = {
1748 [MOUNT_DEAD] = "dead",
1749 [MOUNT_MOUNTING] = "mounting",
1750 [MOUNT_MOUNTING_DONE] = "mounting-done",
1751 [MOUNT_MOUNTED] = "mounted",
1752 [MOUNT_REMOUNTING] = "remounting",
1753 [MOUNT_UNMOUNTING] = "unmounting",
1754 [MOUNT_MOUNTING_SIGTERM] = "mounting-sigterm",
1755 [MOUNT_MOUNTING_SIGKILL] = "mounting-sigkill",
1756 [MOUNT_REMOUNTING_SIGTERM] = "remounting-sigterm",
1757 [MOUNT_REMOUNTING_SIGKILL] = "remounting-sigkill",
1758 [MOUNT_UNMOUNTING_SIGTERM] = "unmounting-sigterm",
1759 [MOUNT_UNMOUNTING_SIGKILL] = "unmounting-sigkill",
1760 [MOUNT_FAILED] = "failed"
1763 DEFINE_STRING_TABLE_LOOKUP(mount_state, MountState);
1765 static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
1766 [MOUNT_EXEC_MOUNT] = "ExecMount",
1767 [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
1768 [MOUNT_EXEC_REMOUNT] = "ExecRemount",
1771 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
1773 static const char* const mount_result_table[_MOUNT_RESULT_MAX] = {
1774 [MOUNT_SUCCESS] = "success",
1775 [MOUNT_FAILURE_RESOURCES] = "resources",
1776 [MOUNT_FAILURE_TIMEOUT] = "timeout",
1777 [MOUNT_FAILURE_EXIT_CODE] = "exit-code",
1778 [MOUNT_FAILURE_SIGNAL] = "signal",
1779 [MOUNT_FAILURE_CORE_DUMP] = "core-dump"
1782 DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult);
1784 const UnitVTable mount_vtable = {
1785 .object_size = sizeof(Mount),
1792 .private_section = "Mount",
1793 .exec_context_offset = offsetof(Mount, exec_context),
1794 .cgroup_context_offset = offsetof(Mount, cgroup_context),
1797 .no_instances = true,
1803 .coldplug = mount_coldplug,
1807 .start = mount_start,
1809 .reload = mount_reload,
1813 .serialize = mount_serialize,
1814 .deserialize_item = mount_deserialize_item,
1816 .active_state = mount_active_state,
1817 .sub_state_to_string = mount_sub_state_to_string,
1819 .check_gc = mount_check_gc,
1821 .sigchld_event = mount_sigchld_event,
1822 .timer_event = mount_timer_event,
1824 .reset_failed = mount_reset_failed,
1826 .bus_interface = "org.freedesktop.systemd1.Mount",
1827 .bus_message_handler = bus_mount_message_handler,
1828 .bus_invalidating_properties = bus_mount_invalidating_properties,
1829 .bus_set_property = bus_mount_set_property,
1830 .bus_commit_properties = bus_mount_commit_properties,
1832 .enumerate = mount_enumerate,
1833 .shutdown = mount_shutdown,
1835 .status_message_formats = {
1836 .starting_stopping = {
1837 [0] = "Mounting %s...",
1838 [1] = "Unmounting %s...",
1840 .finished_start_job = {
1841 [JOB_DONE] = "Mounted %s.",
1842 [JOB_FAILED] = "Failed to mount %s.",
1843 [JOB_DEPENDENCY] = "Dependency failed for %s.",
1844 [JOB_TIMEOUT] = "Timed out mounting %s.",
1846 .finished_stop_job = {
1847 [JOB_DONE] = "Unmounted %s.",
1848 [JOB_FAILED] = "Failed unmounting %s.",
1849 [JOB_TIMEOUT] = "Timed out unmounting %s.",