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 int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
63 static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
65 static bool mount_is_network(MountParameters *p) {
68 if (mount_test_option(p->options, "_netdev"))
71 if (p->fstype && fstype_is_network(p->fstype))
77 static bool mount_is_bind(MountParameters *p) {
80 if (mount_test_option(p->options, "bind"))
83 if (p->fstype && streq(p->fstype, "bind"))
86 if (mount_test_option(p->options, "rbind"))
89 if (p->fstype && streq(p->fstype, "rbind"))
95 static bool mount_is_auto(MountParameters *p) {
98 return !mount_test_option(p->options, "noauto");
101 static bool needs_quota(MountParameters *p) {
104 if (mount_is_network(p))
107 if (mount_is_bind(p))
110 return mount_test_option(p->options, "usrquota") ||
111 mount_test_option(p->options, "grpquota") ||
112 mount_test_option(p->options, "quota") ||
113 mount_test_option(p->options, "usrjquota") ||
114 mount_test_option(p->options, "grpjquota");
117 static void mount_init(Unit *u) {
121 assert(u->load_state == UNIT_STUB);
123 m->timeout_usec = u->manager->default_timeout_start_usec;
124 m->directory_mode = 0755;
126 if (unit_has_name(u, "-.mount")) {
127 /* Don't allow start/stop for root directory */
128 u->refuse_manual_start = true;
129 u->refuse_manual_stop = true;
131 /* The stdio/kmsg bridge socket is on /, in order to avoid a
132 * dep loop, don't use kmsg logging for -.mount */
133 m->exec_context.std_output = u->manager->default_std_output;
134 m->exec_context.std_error = u->manager->default_std_error;
137 /* We need to make sure that /bin/mount is always called in
138 * the same process group as us, so that the autofs kernel
139 * side doesn't send us another mount request while we are
140 * already trying to comply its last one. */
141 m->exec_context.same_pgrp = true;
143 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
145 u->ignore_on_isolate = true;
148 static int mount_arm_timer(Mount *m) {
153 if (m->timeout_usec <= 0) {
154 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
158 if (m->timer_event_source) {
159 r = sd_event_source_set_time(m->timer_event_source, now(CLOCK_MONOTONIC) + m->timeout_usec);
163 return sd_event_source_set_enabled(m->timer_event_source, SD_EVENT_ONESHOT);
166 return sd_event_add_monotonic(UNIT(m)->manager->event, &m->timer_event_source, now(CLOCK_MONOTONIC) + m->timeout_usec, 0, mount_dispatch_timer, m);
169 static void mount_unwatch_control_pid(Mount *m) {
172 if (m->control_pid <= 0)
175 unit_unwatch_pid(UNIT(m), m->control_pid);
179 static void mount_parameters_done(MountParameters *p) {
186 p->what = p->options = p->fstype = NULL;
189 static void mount_done(Unit *u) {
197 mount_parameters_done(&m->parameters_proc_self_mountinfo);
198 mount_parameters_done(&m->parameters_fragment);
200 m->exec_runtime = exec_runtime_unref(m->exec_runtime);
201 exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
202 m->control_command = NULL;
204 mount_unwatch_control_pid(m);
206 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
209 _pure_ static MountParameters* get_mount_parameters_fragment(Mount *m) {
212 if (m->from_fragment)
213 return &m->parameters_fragment;
218 _pure_ static MountParameters* get_mount_parameters(Mount *m) {
221 if (m->from_proc_self_mountinfo)
222 return &m->parameters_proc_self_mountinfo;
224 return get_mount_parameters_fragment(m);
227 static int mount_add_mount_links(Mount *m) {
228 _cleanup_free_ char *parent = NULL;
237 if (!path_equal(m->where, "/")) {
238 /* Adds in links to other mount points that might lie further
239 * up in the hierarchy */
240 r = path_get_parent(m->where, &parent);
244 r = unit_require_mounts_for(UNIT(m), parent);
249 /* Adds in links to other mount points that might be needed
250 * for the source path (if this is a bind mount) to be
252 pm = get_mount_parameters_fragment(m);
253 if (pm && pm->what &&
254 path_is_absolute(pm->what) &&
255 !mount_is_network(pm)) {
257 r = unit_require_mounts_for(UNIT(m), pm->what);
262 /* Adds in links to other units that use this path or paths
263 * further down in the hierarchy */
264 s = manager_get_units_requiring_mounts_for(UNIT(m)->manager, m->where);
265 SET_FOREACH(other, s, i) {
267 if (other->load_state != UNIT_LOADED)
270 if (other == UNIT(m))
273 r = unit_add_dependency(other, UNIT_AFTER, UNIT(m), true);
277 if (UNIT(m)->fragment_path) {
278 /* If we have fragment configuration, then make this dependency required */
279 r = unit_add_dependency(other, UNIT_REQUIRES, UNIT(m), true);
288 static int mount_add_device_links(Mount *m) {
290 bool device_wants_mount = false;
295 p = get_mount_parameters_fragment(m);
302 if (mount_is_bind(p))
305 if (!is_device_path(p->what))
308 if (path_equal(m->where, "/"))
311 if (mount_is_auto(p) && UNIT(m)->manager->running_as == SYSTEMD_SYSTEM)
312 device_wants_mount = true;
314 r = unit_add_node_link(UNIT(m), p->what, device_wants_mount);
321 static int mount_add_quota_links(Mount *m) {
327 if (UNIT(m)->manager->running_as != SYSTEMD_SYSTEM)
330 p = get_mount_parameters_fragment(m);
337 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTACHECK_SERVICE, NULL, true);
341 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTAON_SERVICE, NULL, true);
348 static bool should_umount(Mount *m) {
351 if (path_equal(m->where, "/") ||
352 path_equal(m->where, "/usr"))
355 p = get_mount_parameters(m);
356 if (p && mount_test_option(p->options, "x-initrd.mount") &&
363 static int mount_add_default_dependencies(Mount *m) {
364 const char *after, *after2, *online;
370 if (UNIT(m)->manager->running_as != SYSTEMD_SYSTEM)
373 p = get_mount_parameters(m);
378 if (path_equal(m->where, "/"))
381 if (mount_is_network(p)) {
382 after = SPECIAL_REMOTE_FS_PRE_TARGET;
383 after2 = SPECIAL_NETWORK_TARGET;
384 online = SPECIAL_NETWORK_ONLINE_TARGET;
386 after = SPECIAL_LOCAL_FS_PRE_TARGET;
391 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after, NULL, true);
396 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after2, NULL, true);
402 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_WANTS, UNIT_AFTER, online, NULL, true);
407 if (should_umount(m)) {
408 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
416 static int mount_fix_timeouts(Mount *m) {
418 const char *timeout = NULL;
427 p = get_mount_parameters_fragment(m);
431 /* Allow configuration how long we wait for a device that
432 * backs a mount point to show up. This is useful to support
433 * endless device timeouts for devices that show up only after
434 * user input, like crypto devices. */
436 if ((timeout = mount_test_option(p->options, "comment=systemd.device-timeout")))
438 else if ((timeout = mount_test_option(p->options, "x-systemd.device-timeout")))
443 t = strndup(timeout, strcspn(timeout, ",;" WHITESPACE));
447 r = parse_sec(t, &u);
451 log_warning_unit(UNIT(m)->id,
452 "Failed to parse timeout for %s, ignoring: %s",
457 SET_FOREACH(other, UNIT(m)->dependencies[UNIT_AFTER], i) {
458 if (other->type != UNIT_DEVICE)
461 other->job_timeout = u;
467 static int mount_verify(Mount *m) {
468 _cleanup_free_ char *e = NULL;
473 if (UNIT(m)->load_state != UNIT_LOADED)
476 if (!m->from_fragment && !m->from_proc_self_mountinfo)
479 e = unit_name_from_path(m->where, ".mount");
483 b = unit_has_name(UNIT(m), e);
485 log_error_unit(UNIT(m)->id, "%s's Where= setting doesn't match unit name. Refusing.", UNIT(m)->id);
489 if (mount_point_is_api(m->where) || mount_point_ignore(m->where)) {
490 log_error_unit(UNIT(m)->id, "Cannot create mount unit for API file system %s. Refusing.", m->where);
494 if (UNIT(m)->fragment_path && !m->parameters_fragment.what) {
495 log_error_unit(UNIT(m)->id, "%s's What setting is missing. Refusing.", UNIT(m)->id);
499 if (m->exec_context.pam_name && m->kill_context.kill_mode != KILL_CONTROL_GROUP) {
500 log_error_unit(UNIT(m)->id, "%s has PAM enabled. Kill mode must be set to control-group'. Refusing.",UNIT(m)->id);
507 static int mount_add_extras(Mount *m) {
513 if (u->fragment_path)
514 m->from_fragment = true;
517 m->where = unit_name_to_path(u->id);
522 path_kill_slashes(m->where);
524 if (!u->description) {
525 r = unit_set_description(u, m->where);
530 r = mount_add_device_links(m);
534 r = mount_add_mount_links(m);
538 r = mount_add_quota_links(m);
542 r = unit_patch_contexts(u);
546 r = unit_add_exec_dependencies(u, &m->exec_context);
550 r = unit_add_default_slice(u, &m->cgroup_context);
554 r = mount_fix_timeouts(m);
558 if (u->default_dependencies) {
559 r = mount_add_default_dependencies(m);
567 static int mount_load(Unit *u) {
572 assert(u->load_state == UNIT_STUB);
574 if (m->from_proc_self_mountinfo)
575 r = unit_load_fragment_and_dropin_optional(u);
577 r = unit_load_fragment_and_dropin(u);
582 /* This is a new unit? Then let's add in some extras */
583 if (u->load_state == UNIT_LOADED) {
584 r = mount_add_extras(m);
589 return mount_verify(m);
592 static int mount_notify_automount(Mount *m, int status) {
599 SET_FOREACH(p, UNIT(m)->dependencies[UNIT_TRIGGERED_BY], i)
600 if (p->type == UNIT_AUTOMOUNT) {
601 r = automount_send_ready(AUTOMOUNT(p), status);
609 static void mount_set_state(Mount *m, MountState state) {
610 MountState old_state;
613 old_state = m->state;
616 if (state != MOUNT_MOUNTING &&
617 state != MOUNT_MOUNTING_DONE &&
618 state != MOUNT_REMOUNTING &&
619 state != MOUNT_UNMOUNTING &&
620 state != MOUNT_MOUNTING_SIGTERM &&
621 state != MOUNT_MOUNTING_SIGKILL &&
622 state != MOUNT_UNMOUNTING_SIGTERM &&
623 state != MOUNT_UNMOUNTING_SIGKILL &&
624 state != MOUNT_REMOUNTING_SIGTERM &&
625 state != MOUNT_REMOUNTING_SIGKILL) {
626 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
627 mount_unwatch_control_pid(m);
628 m->control_command = NULL;
629 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
632 if (state == MOUNT_MOUNTED ||
633 state == MOUNT_REMOUNTING)
634 mount_notify_automount(m, 0);
635 else if (state == MOUNT_DEAD ||
636 state == MOUNT_UNMOUNTING ||
637 state == MOUNT_MOUNTING_SIGTERM ||
638 state == MOUNT_MOUNTING_SIGKILL ||
639 state == MOUNT_REMOUNTING_SIGTERM ||
640 state == MOUNT_REMOUNTING_SIGKILL ||
641 state == MOUNT_UNMOUNTING_SIGTERM ||
642 state == MOUNT_UNMOUNTING_SIGKILL ||
643 state == MOUNT_FAILED) {
644 if (state != old_state)
645 mount_notify_automount(m, -ENODEV);
648 if (state != old_state)
649 log_debug_unit(UNIT(m)->id,
650 "%s changed %s -> %s",
652 mount_state_to_string(old_state),
653 mount_state_to_string(state));
655 unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state], m->reload_result == MOUNT_SUCCESS);
656 m->reload_result = MOUNT_SUCCESS;
659 static int mount_coldplug(Unit *u) {
661 MountState new_state = MOUNT_DEAD;
665 assert(m->state == MOUNT_DEAD);
667 if (m->deserialized_state != m->state)
668 new_state = m->deserialized_state;
669 else if (m->from_proc_self_mountinfo)
670 new_state = MOUNT_MOUNTED;
672 if (new_state == m->state)
675 if (new_state == MOUNT_MOUNTING ||
676 new_state == MOUNT_MOUNTING_DONE ||
677 new_state == MOUNT_REMOUNTING ||
678 new_state == MOUNT_UNMOUNTING ||
679 new_state == MOUNT_MOUNTING_SIGTERM ||
680 new_state == MOUNT_MOUNTING_SIGKILL ||
681 new_state == MOUNT_UNMOUNTING_SIGTERM ||
682 new_state == MOUNT_UNMOUNTING_SIGKILL ||
683 new_state == MOUNT_REMOUNTING_SIGTERM ||
684 new_state == MOUNT_REMOUNTING_SIGKILL) {
686 if (m->control_pid <= 0)
689 r = unit_watch_pid(UNIT(m), m->control_pid);
693 r = mount_arm_timer(m);
698 mount_set_state(m, new_state);
702 static void mount_dump(Unit *u, FILE *f, const char *prefix) {
709 p = get_mount_parameters(m);
712 "%sMount State: %s\n"
716 "%sFile System Type: %s\n"
718 "%sFrom /proc/self/mountinfo: %s\n"
719 "%sFrom fragment: %s\n"
720 "%sDirectoryMode: %04o\n",
721 prefix, mount_state_to_string(m->state),
722 prefix, mount_result_to_string(m->result),
724 prefix, p ? strna(p->what) : "n/a",
725 prefix, p ? strna(p->fstype) : "n/a",
726 prefix, p ? strna(p->options) : "n/a",
727 prefix, yes_no(m->from_proc_self_mountinfo),
728 prefix, yes_no(m->from_fragment),
729 prefix, m->directory_mode);
731 if (m->control_pid > 0)
733 "%sControl PID: "PID_FMT"\n",
734 prefix, m->control_pid);
736 exec_context_dump(&m->exec_context, f, prefix);
737 kill_context_dump(&m->kill_context, f, prefix);
740 static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
748 unit_realize_cgroup(UNIT(m));
750 r = unit_setup_exec_runtime(UNIT(m));
754 r = mount_arm_timer(m);
762 UNIT(m)->manager->environment,
766 UNIT(m)->manager->confirm_spawn,
767 UNIT(m)->manager->cgroup_supported,
768 UNIT(m)->cgroup_path,
769 manager_get_runtime_prefix(UNIT(m)->manager),
778 r = unit_watch_pid(UNIT(m), pid);
780 /* FIXME: we need to do something here */
788 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
793 static void mount_enter_dead(Mount *m, MountResult f) {
796 if (f != MOUNT_SUCCESS)
799 exec_runtime_destroy(m->exec_runtime);
800 m->exec_runtime = exec_runtime_unref(m->exec_runtime);
802 exec_context_destroy_runtime_directory(&m->exec_context, manager_get_runtime_prefix(UNIT(m)->manager));
804 mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD);
807 static void mount_enter_mounted(Mount *m, MountResult f) {
810 if (f != MOUNT_SUCCESS)
813 mount_set_state(m, MOUNT_MOUNTED);
816 static void mount_enter_signal(Mount *m, MountState state, MountResult f) {
821 if (f != MOUNT_SUCCESS)
824 r = unit_kill_context(
827 state != MOUNT_MOUNTING_SIGTERM && state != MOUNT_UNMOUNTING_SIGTERM && state != MOUNT_REMOUNTING_SIGTERM,
835 r = mount_arm_timer(m);
839 mount_set_state(m, state);
840 } else if (state == MOUNT_REMOUNTING_SIGTERM)
841 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS);
842 else if (state == MOUNT_REMOUNTING_SIGKILL)
843 mount_enter_mounted(m, MOUNT_SUCCESS);
844 else if (state == MOUNT_MOUNTING_SIGTERM)
845 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, MOUNT_SUCCESS);
846 else if (state == MOUNT_UNMOUNTING_SIGTERM)
847 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_SUCCESS);
849 mount_enter_dead(m, MOUNT_SUCCESS);
854 log_warning_unit(UNIT(m)->id,
855 "%s failed to kill processes: %s", UNIT(m)->id, strerror(-r));
857 if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
858 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
860 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
863 void warn_if_dir_nonempty(const char *unit, const char* where) {
867 if (dir_is_empty(where) > 0)
870 log_struct_unit(LOG_NOTICE,
872 "MESSAGE=%s: Directory %s to mount over is not empty, mounting anyway.",
875 MESSAGE_ID(SD_MESSAGE_OVERMOUNTING),
879 static void mount_enter_unmounting(Mount *m) {
884 m->control_command_id = MOUNT_EXEC_UNMOUNT;
885 m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
887 if ((r = exec_command_set(
894 mount_unwatch_control_pid(m);
896 if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
899 mount_set_state(m, MOUNT_UNMOUNTING);
904 log_warning_unit(UNIT(m)->id,
905 "%s failed to run 'umount' task: %s",
906 UNIT(m)->id, strerror(-r));
907 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
910 static void mount_enter_mounting(Mount *m) {
916 m->control_command_id = MOUNT_EXEC_MOUNT;
917 m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
919 mkdir_p_label(m->where, m->directory_mode);
921 warn_if_dir_nonempty(m->meta.id, m->where);
923 /* Create the source directory for bind-mounts if needed */
924 p = get_mount_parameters_fragment(m);
925 if (p && mount_is_bind(p))
926 mkdir_p_label(p->what, m->directory_mode);
928 if (m->from_fragment)
929 r = exec_command_set(
932 m->parameters_fragment.what,
934 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
935 m->parameters_fragment.options ? "-o" : NULL, m->parameters_fragment.options,
943 mount_unwatch_control_pid(m);
945 r = mount_spawn(m, m->control_command, &m->control_pid);
949 mount_set_state(m, MOUNT_MOUNTING);
954 log_warning_unit(UNIT(m)->id,
955 "%s failed to run 'mount' task: %s",
956 UNIT(m)->id, strerror(-r));
957 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
960 static void mount_enter_remounting(Mount *m) {
965 m->control_command_id = MOUNT_EXEC_REMOUNT;
966 m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
968 if (m->from_fragment) {
971 if (m->parameters_fragment.options)
972 o = strappenda("remount,", m->parameters_fragment.options);
976 r = exec_command_set(
979 m->parameters_fragment.what,
981 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
990 mount_unwatch_control_pid(m);
992 r = mount_spawn(m, m->control_command, &m->control_pid);
996 mount_set_state(m, MOUNT_REMOUNTING);
1001 log_warning_unit(UNIT(m)->id,
1002 "%s failed to run 'remount' task: %s",
1003 UNIT(m)->id, strerror(-r));
1004 m->reload_result = MOUNT_FAILURE_RESOURCES;
1005 mount_enter_mounted(m, MOUNT_SUCCESS);
1008 static int mount_start(Unit *u) {
1009 Mount *m = MOUNT(u);
1013 /* We cannot fulfill this request right now, try again later
1015 if (m->state == MOUNT_UNMOUNTING ||
1016 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1017 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1018 m->state == MOUNT_MOUNTING_SIGTERM ||
1019 m->state == MOUNT_MOUNTING_SIGKILL)
1022 /* Already on it! */
1023 if (m->state == MOUNT_MOUNTING)
1026 assert(m->state == MOUNT_DEAD || m->state == MOUNT_FAILED);
1028 m->result = MOUNT_SUCCESS;
1029 m->reload_result = MOUNT_SUCCESS;
1031 mount_enter_mounting(m);
1035 static int mount_stop(Unit *u) {
1036 Mount *m = MOUNT(u);
1041 if (m->state == MOUNT_UNMOUNTING ||
1042 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1043 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1044 m->state == MOUNT_MOUNTING_SIGTERM ||
1045 m->state == MOUNT_MOUNTING_SIGKILL)
1048 assert(m->state == MOUNT_MOUNTING ||
1049 m->state == MOUNT_MOUNTING_DONE ||
1050 m->state == MOUNT_MOUNTED ||
1051 m->state == MOUNT_REMOUNTING ||
1052 m->state == MOUNT_REMOUNTING_SIGTERM ||
1053 m->state == MOUNT_REMOUNTING_SIGKILL);
1055 mount_enter_unmounting(m);
1059 static int mount_reload(Unit *u) {
1060 Mount *m = MOUNT(u);
1064 if (m->state == MOUNT_MOUNTING_DONE)
1067 assert(m->state == MOUNT_MOUNTED);
1069 mount_enter_remounting(m);
1073 static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
1074 Mount *m = MOUNT(u);
1080 unit_serialize_item(u, f, "state", mount_state_to_string(m->state));
1081 unit_serialize_item(u, f, "result", mount_result_to_string(m->result));
1082 unit_serialize_item(u, f, "reload-result", mount_result_to_string(m->reload_result));
1084 if (m->control_pid > 0)
1085 unit_serialize_item_format(u, f, "control-pid", PID_FMT, m->control_pid);
1087 if (m->control_command_id >= 0)
1088 unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(m->control_command_id));
1093 static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1094 Mount *m = MOUNT(u);
1101 if (streq(key, "state")) {
1104 if ((state = mount_state_from_string(value)) < 0)
1105 log_debug_unit(u->id, "Failed to parse state value %s", value);
1107 m->deserialized_state = state;
1108 } else if (streq(key, "result")) {
1111 f = mount_result_from_string(value);
1113 log_debug_unit(UNIT(m)->id,
1114 "Failed to parse result value %s", value);
1115 else if (f != MOUNT_SUCCESS)
1118 } else if (streq(key, "reload-result")) {
1121 f = mount_result_from_string(value);
1123 log_debug_unit(UNIT(m)->id,
1124 "Failed to parse reload result value %s", value);
1125 else if (f != MOUNT_SUCCESS)
1126 m->reload_result = f;
1128 } else if (streq(key, "control-pid")) {
1131 if (parse_pid(value, &pid) < 0)
1132 log_debug_unit(UNIT(m)->id,
1133 "Failed to parse control-pid value %s", value);
1135 m->control_pid = pid;
1136 } else if (streq(key, "control-command")) {
1137 MountExecCommand id;
1139 if ((id = mount_exec_command_from_string(value)) < 0)
1140 log_debug_unit(UNIT(m)->id,
1141 "Failed to parse exec-command value %s", value);
1143 m->control_command_id = id;
1144 m->control_command = m->exec_command + id;
1147 log_debug_unit(UNIT(m)->id,
1148 "Unknown serialization key '%s'", key);
1153 _pure_ static UnitActiveState mount_active_state(Unit *u) {
1156 return state_translation_table[MOUNT(u)->state];
1159 _pure_ static const char *mount_sub_state_to_string(Unit *u) {
1162 return mount_state_to_string(MOUNT(u)->state);
1165 _pure_ static bool mount_check_gc(Unit *u) {
1166 Mount *m = MOUNT(u);
1170 return m->from_proc_self_mountinfo;
1173 static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1174 Mount *m = MOUNT(u);
1180 if (pid != m->control_pid)
1185 if (is_clean_exit(code, status, NULL))
1187 else if (code == CLD_EXITED)
1188 f = MOUNT_FAILURE_EXIT_CODE;
1189 else if (code == CLD_KILLED)
1190 f = MOUNT_FAILURE_SIGNAL;
1191 else if (code == CLD_DUMPED)
1192 f = MOUNT_FAILURE_CORE_DUMP;
1194 assert_not_reached("Unknown code");
1196 if (f != MOUNT_SUCCESS)
1199 if (m->control_command) {
1200 exec_status_exit(&m->control_command->exec_status, &m->exec_context, pid, code, status);
1202 m->control_command = NULL;
1203 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
1206 log_full_unit(f == MOUNT_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
1207 "%s mount process exited, code=%s status=%i",
1208 u->id, sigchld_code_to_string(code), status);
1210 /* Note that mount(8) returning and the kernel sending us a
1211 * mount table change event might happen out-of-order. If an
1212 * operation succeed we assume the kernel will follow soon too
1213 * and already change into the resulting state. If it fails
1214 * we check if the kernel still knows about the mount. and
1215 * change state accordingly. */
1219 case MOUNT_MOUNTING:
1220 case MOUNT_MOUNTING_DONE:
1221 case MOUNT_MOUNTING_SIGKILL:
1222 case MOUNT_MOUNTING_SIGTERM:
1224 if (f == MOUNT_SUCCESS)
1225 mount_enter_mounted(m, f);
1226 else if (m->from_proc_self_mountinfo)
1227 mount_enter_mounted(m, f);
1229 mount_enter_dead(m, f);
1232 case MOUNT_REMOUNTING:
1233 case MOUNT_REMOUNTING_SIGKILL:
1234 case MOUNT_REMOUNTING_SIGTERM:
1236 m->reload_result = f;
1237 if (m->from_proc_self_mountinfo)
1238 mount_enter_mounted(m, MOUNT_SUCCESS);
1240 mount_enter_dead(m, MOUNT_SUCCESS);
1244 case MOUNT_UNMOUNTING:
1245 case MOUNT_UNMOUNTING_SIGKILL:
1246 case MOUNT_UNMOUNTING_SIGTERM:
1248 if (f == MOUNT_SUCCESS)
1249 mount_enter_dead(m, f);
1250 else if (m->from_proc_self_mountinfo)
1251 mount_enter_mounted(m, f);
1253 mount_enter_dead(m, f);
1257 assert_not_reached("Uh, control process died at wrong time.");
1260 /* Notify clients about changed exit status */
1261 unit_add_to_dbus_queue(u);
1264 static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
1265 Mount *m = MOUNT(userdata);
1268 assert(m->timer_event_source == source);
1272 case MOUNT_MOUNTING:
1273 case MOUNT_MOUNTING_DONE:
1274 log_warning_unit(UNIT(m)->id,
1275 "%s mounting timed out. Stopping.", UNIT(m)->id);
1276 mount_enter_signal(m, MOUNT_MOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1279 case MOUNT_REMOUNTING:
1280 log_warning_unit(UNIT(m)->id,
1281 "%s remounting timed out. Stopping.", UNIT(m)->id);
1282 m->reload_result = MOUNT_FAILURE_TIMEOUT;
1283 mount_enter_mounted(m, MOUNT_SUCCESS);
1286 case MOUNT_UNMOUNTING:
1287 log_warning_unit(UNIT(m)->id,
1288 "%s unmounting timed out. Stopping.", UNIT(m)->id);
1289 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1292 case MOUNT_MOUNTING_SIGTERM:
1293 if (m->kill_context.send_sigkill) {
1294 log_warning_unit(UNIT(m)->id,
1295 "%s mounting timed out. Killing.", UNIT(m)->id);
1296 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1298 log_warning_unit(UNIT(m)->id,
1299 "%s mounting timed out. Skipping SIGKILL. Ignoring.",
1302 if (m->from_proc_self_mountinfo)
1303 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1305 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1309 case MOUNT_REMOUNTING_SIGTERM:
1310 if (m->kill_context.send_sigkill) {
1311 log_warning_unit(UNIT(m)->id,
1312 "%s remounting timed out. Killing.", UNIT(m)->id);
1313 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1315 log_warning_unit(UNIT(m)->id,
1316 "%s remounting timed out. Skipping SIGKILL. Ignoring.",
1319 if (m->from_proc_self_mountinfo)
1320 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1322 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1326 case MOUNT_UNMOUNTING_SIGTERM:
1327 if (m->kill_context.send_sigkill) {
1328 log_warning_unit(UNIT(m)->id,
1329 "%s unmounting timed out. Killing.", UNIT(m)->id);
1330 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1332 log_warning_unit(UNIT(m)->id,
1333 "%s unmounting timed out. Skipping SIGKILL. Ignoring.",
1336 if (m->from_proc_self_mountinfo)
1337 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1339 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1343 case MOUNT_MOUNTING_SIGKILL:
1344 case MOUNT_REMOUNTING_SIGKILL:
1345 case MOUNT_UNMOUNTING_SIGKILL:
1346 log_warning_unit(UNIT(m)->id,
1347 "%s mount process still around after SIGKILL. Ignoring.",
1350 if (m->from_proc_self_mountinfo)
1351 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1353 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1357 assert_not_reached("Timeout at wrong time.");
1363 static int mount_add_one(
1367 const char *options,
1371 _cleanup_free_ char *e = NULL, *w = NULL, *o = NULL, *f = NULL;
1372 bool load_extras = false;
1374 bool delete, changed = false;
1384 /* Ignore API mount points. They should never be referenced in
1385 * dependencies ever. */
1386 if (mount_point_is_api(where) || mount_point_ignore(where))
1389 if (streq(fstype, "autofs"))
1392 /* probably some kind of swap, ignore */
1393 if (!is_path(where))
1396 e = unit_name_from_path(where, ".mount");
1400 u = manager_get_unit(m, e);
1404 u = unit_new(m, sizeof(Mount));
1408 r = unit_add_name(u, e);
1412 MOUNT(u)->where = strdup(where);
1413 if (!MOUNT(u)->where) {
1418 u->source_path = strdup("/proc/self/mountinfo");
1419 if (!u->source_path) {
1425 if (m->running_as == SYSTEMD_SYSTEM) {
1428 target = fstype_is_network(fstype) ? SPECIAL_REMOTE_FS_TARGET : SPECIAL_LOCAL_FS_TARGET;
1430 r = unit_add_dependency_by_name(u, UNIT_BEFORE, target, NULL, true);
1434 if (should_umount(MOUNT(u))) {
1435 r = unit_add_dependency_by_name(u, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
1441 unit_add_to_load_queue(u);
1446 if (!MOUNT(u)->where) {
1447 MOUNT(u)->where = strdup(where);
1448 if (!MOUNT(u)->where) {
1454 if (u->load_state == UNIT_NOT_FOUND) {
1455 u->load_state = UNIT_LOADED;
1458 /* Load in the extras later on, after we
1459 * finished initialization of the unit */
1466 o = strdup(options);
1468 if (!w || !o || !f) {
1473 p = &MOUNT(u)->parameters_proc_self_mountinfo;
1475 changed = changed ||
1476 !streq_ptr(p->options, options) ||
1477 !streq_ptr(p->what, what) ||
1478 !streq_ptr(p->fstype, fstype);
1481 MOUNT(u)->is_mounted = true;
1482 MOUNT(u)->just_mounted = !MOUNT(u)->from_proc_self_mountinfo;
1483 MOUNT(u)->just_changed = changed;
1486 MOUNT(u)->from_proc_self_mountinfo = true;
1501 r = mount_add_extras(MOUNT(u));
1507 unit_add_to_dbus_queue(u);
1518 static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
1524 rewind(m->proc_self_mountinfo);
1527 _cleanup_free_ char *device = NULL, *path = NULL, *options = NULL, *options2 = NULL, *fstype = NULL, *d = NULL, *p = NULL, *o = NULL;
1530 k = fscanf(m->proc_self_mountinfo,
1531 "%*s " /* (1) mount id */
1532 "%*s " /* (2) parent id */
1533 "%*s " /* (3) major:minor */
1534 "%*s " /* (4) root */
1535 "%ms " /* (5) mount point */
1536 "%ms" /* (6) mount options */
1537 "%*[^-]" /* (7) optional fields */
1538 "- " /* (8) separator */
1539 "%ms " /* (9) file system type */
1540 "%ms" /* (10) mount source */
1541 "%ms" /* (11) mount options 2 */
1542 "%*[^\n]", /* some rubbish at the end */
1553 log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
1557 o = strjoin(options, ",", options2, NULL);
1561 d = cunescape(device);
1562 p = cunescape(path);
1566 k = mount_add_one(m, d, p, o, fstype, set_flags);
1574 static void mount_shutdown(Manager *m) {
1577 m->mount_event_source = sd_event_source_unref(m->mount_event_source);
1579 if (m->proc_self_mountinfo) {
1580 fclose(m->proc_self_mountinfo);
1581 m->proc_self_mountinfo = NULL;
1585 static int mount_get_timeout(Unit *u, uint64_t *timeout) {
1586 Mount *m = MOUNT(u);
1589 if (!m->timer_event_source)
1592 r = sd_event_source_get_time(m->timer_event_source, timeout);
1599 static int mount_enumerate(Manager *m) {
1603 if (!m->proc_self_mountinfo) {
1604 m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
1605 if (!m->proc_self_mountinfo)
1608 r = sd_event_add_io(m->event, &m->mount_event_source, fileno(m->proc_self_mountinfo), EPOLLPRI, mount_dispatch_io, m);
1612 /* Dispatch this before we dispatch SIGCHLD, so that
1613 * we always get the events from /proc/self/mountinfo
1614 * before the SIGCHLD of /bin/mount. */
1615 r = sd_event_source_set_priority(m->mount_event_source, -10);
1620 r = mount_load_proc_self_mountinfo(m, false);
1631 static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
1632 Manager *m = userdata;
1637 assert(revents & EPOLLPRI);
1639 /* The manager calls this for every fd event happening on the
1640 * /proc/self/mountinfo file, which informs us about mounting
1643 r = mount_load_proc_self_mountinfo(m, true);
1645 log_error("Failed to reread /proc/self/mountinfo: %s", strerror(-r));
1647 /* Reset flags, just in case, for later calls */
1648 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1649 Mount *mount = MOUNT(u);
1651 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1657 manager_dispatch_load_queue(m);
1659 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1660 Mount *mount = MOUNT(u);
1662 if (!mount->is_mounted) {
1664 mount->from_proc_self_mountinfo = false;
1666 switch (mount->state) {
1669 /* This has just been unmounted by
1670 * somebody else, follow the state
1672 mount_enter_dead(mount, MOUNT_SUCCESS);
1679 } else if (mount->just_mounted || mount->just_changed) {
1681 /* New or changed mount entry */
1683 switch (mount->state) {
1687 /* This has just been mounted by
1688 * somebody else, follow the state
1690 mount_enter_mounted(mount, MOUNT_SUCCESS);
1693 case MOUNT_MOUNTING:
1694 mount_set_state(mount, MOUNT_MOUNTING_DONE);
1698 /* Nothing really changed, but let's
1699 * issue an notification call
1700 * nonetheless, in case somebody is
1701 * waiting for this. (e.g. file system
1702 * ro/rw remounts.) */
1703 mount_set_state(mount, mount->state);
1708 /* Reset the flags for later calls */
1709 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1715 static void mount_reset_failed(Unit *u) {
1716 Mount *m = MOUNT(u);
1720 if (m->state == MOUNT_FAILED)
1721 mount_set_state(m, MOUNT_DEAD);
1723 m->result = MOUNT_SUCCESS;
1724 m->reload_result = MOUNT_SUCCESS;
1727 static int mount_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
1728 return unit_kill_common(u, who, signo, -1, MOUNT(u)->control_pid, error);
1731 static const char* const mount_state_table[_MOUNT_STATE_MAX] = {
1732 [MOUNT_DEAD] = "dead",
1733 [MOUNT_MOUNTING] = "mounting",
1734 [MOUNT_MOUNTING_DONE] = "mounting-done",
1735 [MOUNT_MOUNTED] = "mounted",
1736 [MOUNT_REMOUNTING] = "remounting",
1737 [MOUNT_UNMOUNTING] = "unmounting",
1738 [MOUNT_MOUNTING_SIGTERM] = "mounting-sigterm",
1739 [MOUNT_MOUNTING_SIGKILL] = "mounting-sigkill",
1740 [MOUNT_REMOUNTING_SIGTERM] = "remounting-sigterm",
1741 [MOUNT_REMOUNTING_SIGKILL] = "remounting-sigkill",
1742 [MOUNT_UNMOUNTING_SIGTERM] = "unmounting-sigterm",
1743 [MOUNT_UNMOUNTING_SIGKILL] = "unmounting-sigkill",
1744 [MOUNT_FAILED] = "failed"
1747 DEFINE_STRING_TABLE_LOOKUP(mount_state, MountState);
1749 static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
1750 [MOUNT_EXEC_MOUNT] = "ExecMount",
1751 [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
1752 [MOUNT_EXEC_REMOUNT] = "ExecRemount",
1755 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
1757 static const char* const mount_result_table[_MOUNT_RESULT_MAX] = {
1758 [MOUNT_SUCCESS] = "success",
1759 [MOUNT_FAILURE_RESOURCES] = "resources",
1760 [MOUNT_FAILURE_TIMEOUT] = "timeout",
1761 [MOUNT_FAILURE_EXIT_CODE] = "exit-code",
1762 [MOUNT_FAILURE_SIGNAL] = "signal",
1763 [MOUNT_FAILURE_CORE_DUMP] = "core-dump"
1766 DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult);
1768 const UnitVTable mount_vtable = {
1769 .object_size = sizeof(Mount),
1770 .exec_context_offset = offsetof(Mount, exec_context),
1771 .cgroup_context_offset = offsetof(Mount, cgroup_context),
1772 .kill_context_offset = offsetof(Mount, kill_context),
1773 .exec_runtime_offset = offsetof(Mount, exec_runtime),
1779 .private_section = "Mount",
1782 .no_instances = true,
1788 .coldplug = mount_coldplug,
1792 .start = mount_start,
1794 .reload = mount_reload,
1798 .serialize = mount_serialize,
1799 .deserialize_item = mount_deserialize_item,
1801 .active_state = mount_active_state,
1802 .sub_state_to_string = mount_sub_state_to_string,
1804 .check_gc = mount_check_gc,
1806 .sigchld_event = mount_sigchld_event,
1808 .reset_failed = mount_reset_failed,
1810 .bus_interface = "org.freedesktop.systemd1.Mount",
1811 .bus_vtable = bus_mount_vtable,
1812 .bus_set_property = bus_mount_set_property,
1813 .bus_commit_properties = bus_mount_commit_properties,
1815 .get_timeout = mount_get_timeout,
1817 .enumerate = mount_enumerate,
1818 .shutdown = mount_shutdown,
1820 .status_message_formats = {
1821 .starting_stopping = {
1822 [0] = "Mounting %s...",
1823 [1] = "Unmounting %s...",
1825 .finished_start_job = {
1826 [JOB_DONE] = "Mounted %s.",
1827 [JOB_FAILED] = "Failed to mount %s.",
1828 [JOB_DEPENDENCY] = "Dependency failed for %s.",
1829 [JOB_TIMEOUT] = "Timed out mounting %s.",
1831 .finished_stop_job = {
1832 [JOB_DONE] = "Unmounted %s.",
1833 [JOB_FAILED] = "Failed unmounting %s.",
1834 [JOB_TIMEOUT] = "Timed out unmounting %s.",