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>
28 #include <sys/inotify.h>
33 #include "load-fragment.h"
34 #include "load-dropin.h"
36 #include "sd-messages.h"
39 #include "path-util.h"
40 #include "mount-setup.h"
41 #include "unit-name.h"
42 #include "dbus-mount.h"
44 #include "bus-errors.h"
45 #include "exit-status.h"
48 static const UnitActiveState state_translation_table[_MOUNT_STATE_MAX] = {
49 [MOUNT_DEAD] = UNIT_INACTIVE,
50 [MOUNT_MOUNTING] = UNIT_ACTIVATING,
51 [MOUNT_MOUNTING_DONE] = UNIT_ACTIVE,
52 [MOUNT_MOUNTED] = UNIT_ACTIVE,
53 [MOUNT_REMOUNTING] = UNIT_RELOADING,
54 [MOUNT_UNMOUNTING] = UNIT_DEACTIVATING,
55 [MOUNT_MOUNTING_SIGTERM] = UNIT_DEACTIVATING,
56 [MOUNT_MOUNTING_SIGKILL] = UNIT_DEACTIVATING,
57 [MOUNT_REMOUNTING_SIGTERM] = UNIT_RELOADING,
58 [MOUNT_REMOUNTING_SIGKILL] = UNIT_RELOADING,
59 [MOUNT_UNMOUNTING_SIGTERM] = UNIT_DEACTIVATING,
60 [MOUNT_UNMOUNTING_SIGKILL] = UNIT_DEACTIVATING,
61 [MOUNT_FAILED] = UNIT_FAILED
64 static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
65 static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
67 static bool mount_is_network(MountParameters *p) {
70 if (mount_test_option(p->options, "_netdev"))
73 if (p->fstype && fstype_is_network(p->fstype))
79 static bool mount_is_bind(MountParameters *p) {
82 if (mount_test_option(p->options, "bind"))
85 if (p->fstype && streq(p->fstype, "bind"))
88 if (mount_test_option(p->options, "rbind"))
91 if (p->fstype && streq(p->fstype, "rbind"))
97 static bool mount_is_auto(MountParameters *p) {
100 return !mount_test_option(p->options, "noauto");
103 static bool needs_quota(MountParameters *p) {
106 if (mount_is_network(p))
109 if (mount_is_bind(p))
112 return mount_test_option(p->options, "usrquota") ||
113 mount_test_option(p->options, "grpquota") ||
114 mount_test_option(p->options, "quota") ||
115 mount_test_option(p->options, "usrjquota") ||
116 mount_test_option(p->options, "grpjquota");
119 static void mount_init(Unit *u) {
123 assert(u->load_state == UNIT_STUB);
125 m->timeout_usec = u->manager->default_timeout_start_usec;
126 m->directory_mode = 0755;
128 if (unit_has_name(u, "-.mount")) {
129 /* Don't allow start/stop for root directory */
130 u->refuse_manual_start = true;
131 u->refuse_manual_stop = true;
133 /* The stdio/kmsg bridge socket is on /, in order to avoid a
134 * dep loop, don't use kmsg logging for -.mount */
135 m->exec_context.std_output = u->manager->default_std_output;
136 m->exec_context.std_error = u->manager->default_std_error;
139 /* We need to make sure that /bin/mount is always called in
140 * the same process group as us, so that the autofs kernel
141 * side doesn't send us another mount request while we are
142 * already trying to comply its last one. */
143 m->exec_context.same_pgrp = true;
145 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
147 u->ignore_on_isolate = true;
150 static int mount_arm_timer(Mount *m) {
155 if (m->timeout_usec <= 0) {
156 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
160 if (m->timer_event_source) {
161 r = sd_event_source_set_time(m->timer_event_source, now(CLOCK_MONOTONIC) + m->timeout_usec);
165 return sd_event_source_set_enabled(m->timer_event_source, SD_EVENT_ONESHOT);
168 return sd_event_add_time(
169 UNIT(m)->manager->event,
170 &m->timer_event_source,
172 now(CLOCK_MONOTONIC) + m->timeout_usec, 0,
173 mount_dispatch_timer, m);
176 static void mount_unwatch_control_pid(Mount *m) {
179 if (m->control_pid <= 0)
182 unit_unwatch_pid(UNIT(m), m->control_pid);
186 static void mount_parameters_done(MountParameters *p) {
193 p->what = p->options = p->fstype = NULL;
196 static void mount_done(Unit *u) {
204 mount_parameters_done(&m->parameters_proc_self_mountinfo);
205 mount_parameters_done(&m->parameters_fragment);
207 m->exec_runtime = exec_runtime_unref(m->exec_runtime);
208 exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
209 m->control_command = NULL;
211 mount_unwatch_control_pid(m);
213 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
216 _pure_ static MountParameters* get_mount_parameters_fragment(Mount *m) {
219 if (m->from_fragment)
220 return &m->parameters_fragment;
225 _pure_ static MountParameters* get_mount_parameters(Mount *m) {
228 if (m->from_proc_self_mountinfo)
229 return &m->parameters_proc_self_mountinfo;
231 return get_mount_parameters_fragment(m);
234 static int mount_add_mount_links(Mount *m) {
235 _cleanup_free_ char *parent = NULL;
244 if (!path_equal(m->where, "/")) {
245 /* Adds in links to other mount points that might lie further
246 * up in the hierarchy */
247 r = path_get_parent(m->where, &parent);
251 r = unit_require_mounts_for(UNIT(m), parent);
256 /* Adds in links to other mount points that might be needed
257 * for the source path (if this is a bind mount) to be
259 pm = get_mount_parameters_fragment(m);
260 if (pm && pm->what &&
261 path_is_absolute(pm->what) &&
262 !mount_is_network(pm)) {
264 r = unit_require_mounts_for(UNIT(m), pm->what);
269 /* Adds in links to other units that use this path or paths
270 * further down in the hierarchy */
271 s = manager_get_units_requiring_mounts_for(UNIT(m)->manager, m->where);
272 SET_FOREACH(other, s, i) {
274 if (other->load_state != UNIT_LOADED)
277 if (other == UNIT(m))
280 r = unit_add_dependency(other, UNIT_AFTER, UNIT(m), true);
284 if (UNIT(m)->fragment_path) {
285 /* If we have fragment configuration, then make this dependency required */
286 r = unit_add_dependency(other, UNIT_REQUIRES, UNIT(m), true);
295 static int mount_add_device_links(Mount *m) {
297 bool device_wants_mount = false;
302 p = get_mount_parameters_fragment(m);
309 if (mount_is_bind(p))
312 if (!is_device_path(p->what))
315 if (path_equal(m->where, "/"))
318 if (mount_is_auto(p) && UNIT(m)->manager->running_as == SYSTEMD_SYSTEM)
319 device_wants_mount = true;
321 r = unit_add_node_link(UNIT(m), p->what, device_wants_mount);
328 static int mount_add_quota_links(Mount *m) {
334 if (UNIT(m)->manager->running_as != SYSTEMD_SYSTEM)
337 p = get_mount_parameters_fragment(m);
344 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTACHECK_SERVICE, NULL, true);
348 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTAON_SERVICE, NULL, true);
355 static bool should_umount(Mount *m) {
358 if (path_equal(m->where, "/") ||
359 path_equal(m->where, "/usr"))
362 p = get_mount_parameters(m);
363 if (p && mount_test_option(p->options, "x-initrd.mount") &&
370 static int mount_add_default_dependencies(Mount *m) {
371 const char *after, *after2, *online;
377 if (UNIT(m)->manager->running_as != SYSTEMD_SYSTEM)
380 p = get_mount_parameters(m);
385 if (path_equal(m->where, "/") ||
386 path_equal(m->where, "/usr"))
389 if (mount_is_network(p)) {
390 after = SPECIAL_REMOTE_FS_PRE_TARGET;
391 after2 = SPECIAL_NETWORK_TARGET;
392 online = SPECIAL_NETWORK_ONLINE_TARGET;
394 after = SPECIAL_LOCAL_FS_PRE_TARGET;
399 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after, NULL, true);
404 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after2, NULL, true);
410 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_WANTS, UNIT_AFTER, online, NULL, true);
415 if (should_umount(m)) {
416 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
424 static int mount_verify(Mount *m) {
425 _cleanup_free_ char *e = NULL;
430 if (UNIT(m)->load_state != UNIT_LOADED)
433 if (!m->from_fragment && !m->from_proc_self_mountinfo)
436 e = unit_name_from_path(m->where, ".mount");
440 b = unit_has_name(UNIT(m), e);
442 log_unit_error(UNIT(m)->id, "%s's Where= setting doesn't match unit name. Refusing.", UNIT(m)->id);
446 if (mount_point_is_api(m->where) || mount_point_ignore(m->where)) {
447 log_unit_error(UNIT(m)->id, "Cannot create mount unit for API file system %s. Refusing.", m->where);
451 if (UNIT(m)->fragment_path && !m->parameters_fragment.what) {
452 log_unit_error(UNIT(m)->id, "%s's What setting is missing. Refusing.", UNIT(m)->id);
456 if (m->exec_context.pam_name && m->kill_context.kill_mode != KILL_CONTROL_GROUP) {
457 log_unit_error(UNIT(m)->id, "%s has PAM enabled. Kill mode must be set to control-group'. Refusing.",UNIT(m)->id);
464 static int mount_add_extras(Mount *m) {
470 if (u->fragment_path)
471 m->from_fragment = true;
474 m->where = unit_name_to_path(u->id);
479 path_kill_slashes(m->where);
481 if (!u->description) {
482 r = unit_set_description(u, m->where);
487 r = mount_add_device_links(m);
491 r = mount_add_mount_links(m);
495 r = mount_add_quota_links(m);
499 r = unit_patch_contexts(u);
503 r = unit_add_exec_dependencies(u, &m->exec_context);
507 r = unit_add_default_slice(u, &m->cgroup_context);
511 if (u->default_dependencies) {
512 r = mount_add_default_dependencies(m);
520 static int mount_load(Unit *u) {
525 assert(u->load_state == UNIT_STUB);
527 if (m->from_proc_self_mountinfo)
528 r = unit_load_fragment_and_dropin_optional(u);
530 r = unit_load_fragment_and_dropin(u);
535 /* This is a new unit? Then let's add in some extras */
536 if (u->load_state == UNIT_LOADED) {
537 r = mount_add_extras(m);
542 return mount_verify(m);
545 static int mount_notify_automount(Mount *m, int status) {
552 SET_FOREACH(p, UNIT(m)->dependencies[UNIT_TRIGGERED_BY], i)
553 if (p->type == UNIT_AUTOMOUNT) {
554 r = automount_send_ready(AUTOMOUNT(p), status);
562 static void mount_set_state(Mount *m, MountState state) {
563 MountState old_state;
566 old_state = m->state;
569 if (state != MOUNT_MOUNTING &&
570 state != MOUNT_MOUNTING_DONE &&
571 state != MOUNT_REMOUNTING &&
572 state != MOUNT_UNMOUNTING &&
573 state != MOUNT_MOUNTING_SIGTERM &&
574 state != MOUNT_MOUNTING_SIGKILL &&
575 state != MOUNT_UNMOUNTING_SIGTERM &&
576 state != MOUNT_UNMOUNTING_SIGKILL &&
577 state != MOUNT_REMOUNTING_SIGTERM &&
578 state != MOUNT_REMOUNTING_SIGKILL) {
579 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
580 mount_unwatch_control_pid(m);
581 m->control_command = NULL;
582 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
585 if (state == MOUNT_MOUNTED ||
586 state == MOUNT_REMOUNTING)
587 mount_notify_automount(m, 0);
588 else if (state == MOUNT_DEAD ||
589 state == MOUNT_UNMOUNTING ||
590 state == MOUNT_MOUNTING_SIGTERM ||
591 state == MOUNT_MOUNTING_SIGKILL ||
592 state == MOUNT_REMOUNTING_SIGTERM ||
593 state == MOUNT_REMOUNTING_SIGKILL ||
594 state == MOUNT_UNMOUNTING_SIGTERM ||
595 state == MOUNT_UNMOUNTING_SIGKILL ||
596 state == MOUNT_FAILED) {
597 if (state != old_state)
598 mount_notify_automount(m, -ENODEV);
601 if (state != old_state)
602 log_unit_debug(UNIT(m)->id,
603 "%s changed %s -> %s",
605 mount_state_to_string(old_state),
606 mount_state_to_string(state));
608 unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state], m->reload_result == MOUNT_SUCCESS);
609 m->reload_result = MOUNT_SUCCESS;
612 static int mount_coldplug(Unit *u) {
614 MountState new_state = MOUNT_DEAD;
618 assert(m->state == MOUNT_DEAD);
620 if (m->deserialized_state != m->state)
621 new_state = m->deserialized_state;
622 else if (m->from_proc_self_mountinfo)
623 new_state = MOUNT_MOUNTED;
625 if (new_state == m->state)
628 if (new_state == MOUNT_MOUNTING ||
629 new_state == MOUNT_MOUNTING_DONE ||
630 new_state == MOUNT_REMOUNTING ||
631 new_state == MOUNT_UNMOUNTING ||
632 new_state == MOUNT_MOUNTING_SIGTERM ||
633 new_state == MOUNT_MOUNTING_SIGKILL ||
634 new_state == MOUNT_UNMOUNTING_SIGTERM ||
635 new_state == MOUNT_UNMOUNTING_SIGKILL ||
636 new_state == MOUNT_REMOUNTING_SIGTERM ||
637 new_state == MOUNT_REMOUNTING_SIGKILL) {
639 if (m->control_pid <= 0)
642 r = unit_watch_pid(UNIT(m), m->control_pid);
646 r = mount_arm_timer(m);
651 mount_set_state(m, new_state);
655 static void mount_dump(Unit *u, FILE *f, const char *prefix) {
662 p = get_mount_parameters(m);
665 "%sMount State: %s\n"
669 "%sFile System Type: %s\n"
671 "%sFrom /proc/self/mountinfo: %s\n"
672 "%sFrom fragment: %s\n"
673 "%sDirectoryMode: %04o\n",
674 prefix, mount_state_to_string(m->state),
675 prefix, mount_result_to_string(m->result),
677 prefix, p ? strna(p->what) : "n/a",
678 prefix, p ? strna(p->fstype) : "n/a",
679 prefix, p ? strna(p->options) : "n/a",
680 prefix, yes_no(m->from_proc_self_mountinfo),
681 prefix, yes_no(m->from_fragment),
682 prefix, m->directory_mode);
684 if (m->control_pid > 0)
686 "%sControl PID: "PID_FMT"\n",
687 prefix, m->control_pid);
689 exec_context_dump(&m->exec_context, f, prefix);
690 kill_context_dump(&m->kill_context, f, prefix);
693 static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
696 ExecParameters exec_params = {
697 .apply_permissions = true,
698 .apply_chroot = true,
699 .apply_tty_stdin = true,
706 unit_realize_cgroup(UNIT(m));
708 r = unit_setup_exec_runtime(UNIT(m));
712 r = mount_arm_timer(m);
716 exec_params.environment = UNIT(m)->manager->environment;
717 exec_params.confirm_spawn = UNIT(m)->manager->confirm_spawn;
718 exec_params.cgroup_supported = UNIT(m)->manager->cgroup_supported;
719 exec_params.cgroup_path = UNIT(m)->cgroup_path;
720 exec_params.cgroup_delegate = m->cgroup_context.delegate;
721 exec_params.runtime_prefix = manager_get_runtime_prefix(UNIT(m)->manager);
722 exec_params.unit_id = UNIT(m)->id;
732 r = unit_watch_pid(UNIT(m), pid);
734 /* FIXME: we need to do something here */
742 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
747 static void mount_enter_dead(Mount *m, MountResult f) {
750 if (f != MOUNT_SUCCESS)
753 exec_runtime_destroy(m->exec_runtime);
754 m->exec_runtime = exec_runtime_unref(m->exec_runtime);
756 exec_context_destroy_runtime_directory(&m->exec_context, manager_get_runtime_prefix(UNIT(m)->manager));
758 mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD);
761 static void mount_enter_mounted(Mount *m, MountResult f) {
764 if (f != MOUNT_SUCCESS)
767 mount_set_state(m, MOUNT_MOUNTED);
770 static void mount_enter_signal(Mount *m, MountState state, MountResult f) {
775 if (f != MOUNT_SUCCESS)
778 r = unit_kill_context(
781 (state != MOUNT_MOUNTING_SIGTERM && state != MOUNT_UNMOUNTING_SIGTERM && state != MOUNT_REMOUNTING_SIGTERM) ?
782 KILL_KILL : KILL_TERMINATE,
790 r = mount_arm_timer(m);
794 mount_set_state(m, state);
795 } else if (state == MOUNT_REMOUNTING_SIGTERM)
796 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS);
797 else if (state == MOUNT_REMOUNTING_SIGKILL)
798 mount_enter_mounted(m, MOUNT_SUCCESS);
799 else if (state == MOUNT_MOUNTING_SIGTERM)
800 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, MOUNT_SUCCESS);
801 else if (state == MOUNT_UNMOUNTING_SIGTERM)
802 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_SUCCESS);
804 mount_enter_dead(m, MOUNT_SUCCESS);
809 log_unit_warning(UNIT(m)->id,
810 "%s failed to kill processes: %s", UNIT(m)->id, strerror(-r));
812 if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
813 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
815 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
818 void warn_if_dir_nonempty(const char *unit, const char* where) {
824 r = dir_is_empty(where);
828 log_unit_struct(unit,
830 LOG_MESSAGE_ID(SD_MESSAGE_OVERMOUNTING),
831 LOG_MESSAGE("%s: Directory %s to mount over is not empty, mounting anyway.",
836 log_unit_warning(unit,
837 "MESSAGE=Failed to check directory %s: %s",
838 where, strerror(-r));
841 static int fail_if_symlink(const char *unit, const char* where) {
844 if (is_symlink(where) > 0) {
845 log_unit_struct(unit,
847 LOG_MESSAGE_ID(SD_MESSAGE_OVERMOUNTING),
848 LOG_MESSAGE("%s: Mount on symlink %s not allowed.",
858 static void mount_enter_unmounting(Mount *m) {
863 m->control_command_id = MOUNT_EXEC_UNMOUNT;
864 m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
866 if ((r = exec_command_set(
874 mount_unwatch_control_pid(m);
876 if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
879 mount_set_state(m, MOUNT_UNMOUNTING);
884 log_unit_warning(UNIT(m)->id,
885 "%s failed to run 'umount' task: %s",
886 UNIT(m)->id, strerror(-r));
887 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
890 static void mount_enter_mounting(Mount *m) {
896 m->control_command_id = MOUNT_EXEC_MOUNT;
897 m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
899 mkdir_p_label(m->where, m->directory_mode);
901 warn_if_dir_nonempty(m->meta.id, m->where);
903 /* Create the source directory for bind-mounts if needed */
904 p = get_mount_parameters_fragment(m);
905 if (p && mount_is_bind(p))
906 mkdir_p_label(p->what, m->directory_mode);
908 r = fail_if_symlink(m->meta.id, m->where);
912 if (m->from_fragment)
913 r = exec_command_set(
916 m->sloppy_options ? "-ns" : "-n",
917 m->parameters_fragment.what,
919 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
920 m->parameters_fragment.options ? "-o" : NULL, m->parameters_fragment.options,
928 mount_unwatch_control_pid(m);
930 r = mount_spawn(m, m->control_command, &m->control_pid);
934 mount_set_state(m, MOUNT_MOUNTING);
939 log_unit_warning(UNIT(m)->id,
940 "%s failed to run 'mount' task: %s",
941 UNIT(m)->id, strerror(-r));
942 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
945 static void mount_enter_remounting(Mount *m) {
950 m->control_command_id = MOUNT_EXEC_REMOUNT;
951 m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
953 if (m->from_fragment) {
956 if (m->parameters_fragment.options)
957 o = strappenda("remount,", m->parameters_fragment.options);
961 r = exec_command_set(
964 m->sloppy_options ? "-ns" : "-n",
965 m->parameters_fragment.what,
967 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
976 mount_unwatch_control_pid(m);
978 r = mount_spawn(m, m->control_command, &m->control_pid);
982 mount_set_state(m, MOUNT_REMOUNTING);
987 log_unit_warning(UNIT(m)->id,
988 "%s failed to run 'remount' task: %s",
989 UNIT(m)->id, strerror(-r));
990 m->reload_result = MOUNT_FAILURE_RESOURCES;
991 mount_enter_mounted(m, MOUNT_SUCCESS);
994 static int mount_start(Unit *u) {
999 /* We cannot fulfill this request right now, try again later
1001 if (m->state == MOUNT_UNMOUNTING ||
1002 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1003 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1004 m->state == MOUNT_MOUNTING_SIGTERM ||
1005 m->state == MOUNT_MOUNTING_SIGKILL)
1008 /* Already on it! */
1009 if (m->state == MOUNT_MOUNTING)
1012 assert(m->state == MOUNT_DEAD || m->state == MOUNT_FAILED);
1014 m->result = MOUNT_SUCCESS;
1015 m->reload_result = MOUNT_SUCCESS;
1017 mount_enter_mounting(m);
1021 static int mount_stop(Unit *u) {
1022 Mount *m = MOUNT(u);
1027 if (m->state == MOUNT_UNMOUNTING ||
1028 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1029 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1030 m->state == MOUNT_MOUNTING_SIGTERM ||
1031 m->state == MOUNT_MOUNTING_SIGKILL)
1034 assert(m->state == MOUNT_MOUNTING ||
1035 m->state == MOUNT_MOUNTING_DONE ||
1036 m->state == MOUNT_MOUNTED ||
1037 m->state == MOUNT_REMOUNTING ||
1038 m->state == MOUNT_REMOUNTING_SIGTERM ||
1039 m->state == MOUNT_REMOUNTING_SIGKILL);
1041 mount_enter_unmounting(m);
1045 static int mount_reload(Unit *u) {
1046 Mount *m = MOUNT(u);
1050 if (m->state == MOUNT_MOUNTING_DONE)
1053 assert(m->state == MOUNT_MOUNTED);
1055 mount_enter_remounting(m);
1059 static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
1060 Mount *m = MOUNT(u);
1066 unit_serialize_item(u, f, "state", mount_state_to_string(m->state));
1067 unit_serialize_item(u, f, "result", mount_result_to_string(m->result));
1068 unit_serialize_item(u, f, "reload-result", mount_result_to_string(m->reload_result));
1070 if (m->control_pid > 0)
1071 unit_serialize_item_format(u, f, "control-pid", PID_FMT, m->control_pid);
1073 if (m->control_command_id >= 0)
1074 unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(m->control_command_id));
1079 static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1080 Mount *m = MOUNT(u);
1087 if (streq(key, "state")) {
1090 if ((state = mount_state_from_string(value)) < 0)
1091 log_unit_debug(u->id, "Failed to parse state value %s", value);
1093 m->deserialized_state = state;
1094 } else if (streq(key, "result")) {
1097 f = mount_result_from_string(value);
1099 log_unit_debug(UNIT(m)->id,
1100 "Failed to parse result value %s", value);
1101 else if (f != MOUNT_SUCCESS)
1104 } else if (streq(key, "reload-result")) {
1107 f = mount_result_from_string(value);
1109 log_unit_debug(UNIT(m)->id,
1110 "Failed to parse reload result value %s", value);
1111 else if (f != MOUNT_SUCCESS)
1112 m->reload_result = f;
1114 } else if (streq(key, "control-pid")) {
1117 if (parse_pid(value, &pid) < 0)
1118 log_unit_debug(UNIT(m)->id,
1119 "Failed to parse control-pid value %s", value);
1121 m->control_pid = pid;
1122 } else if (streq(key, "control-command")) {
1123 MountExecCommand id;
1125 if ((id = mount_exec_command_from_string(value)) < 0)
1126 log_unit_debug(UNIT(m)->id,
1127 "Failed to parse exec-command value %s", value);
1129 m->control_command_id = id;
1130 m->control_command = m->exec_command + id;
1133 log_unit_debug(UNIT(m)->id,
1134 "Unknown serialization key '%s'", key);
1139 _pure_ static UnitActiveState mount_active_state(Unit *u) {
1142 return state_translation_table[MOUNT(u)->state];
1145 _pure_ static const char *mount_sub_state_to_string(Unit *u) {
1148 return mount_state_to_string(MOUNT(u)->state);
1151 _pure_ static bool mount_check_gc(Unit *u) {
1152 Mount *m = MOUNT(u);
1156 return m->from_proc_self_mountinfo;
1159 static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1160 Mount *m = MOUNT(u);
1166 if (pid != m->control_pid)
1171 if (is_clean_exit(code, status, NULL))
1173 else if (code == CLD_EXITED)
1174 f = MOUNT_FAILURE_EXIT_CODE;
1175 else if (code == CLD_KILLED)
1176 f = MOUNT_FAILURE_SIGNAL;
1177 else if (code == CLD_DUMPED)
1178 f = MOUNT_FAILURE_CORE_DUMP;
1180 assert_not_reached("Unknown code");
1182 if (f != MOUNT_SUCCESS)
1185 if (m->control_command) {
1186 exec_status_exit(&m->control_command->exec_status, &m->exec_context, pid, code, status);
1188 m->control_command = NULL;
1189 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
1192 log_unit_full(u->id,
1193 f == MOUNT_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
1194 "%s mount process exited, code=%s status=%i",
1195 u->id, sigchld_code_to_string(code), status);
1197 /* Note that mount(8) returning and the kernel sending us a
1198 * mount table change event might happen out-of-order. If an
1199 * operation succeed we assume the kernel will follow soon too
1200 * and already change into the resulting state. If it fails
1201 * we check if the kernel still knows about the mount. and
1202 * change state accordingly. */
1206 case MOUNT_MOUNTING:
1207 case MOUNT_MOUNTING_DONE:
1208 case MOUNT_MOUNTING_SIGKILL:
1209 case MOUNT_MOUNTING_SIGTERM:
1211 if (f == MOUNT_SUCCESS)
1212 mount_enter_mounted(m, f);
1213 else if (m->from_proc_self_mountinfo)
1214 mount_enter_mounted(m, f);
1216 mount_enter_dead(m, f);
1219 case MOUNT_REMOUNTING:
1220 case MOUNT_REMOUNTING_SIGKILL:
1221 case MOUNT_REMOUNTING_SIGTERM:
1223 m->reload_result = f;
1224 if (m->from_proc_self_mountinfo)
1225 mount_enter_mounted(m, MOUNT_SUCCESS);
1227 mount_enter_dead(m, MOUNT_SUCCESS);
1231 case MOUNT_UNMOUNTING:
1232 case MOUNT_UNMOUNTING_SIGKILL:
1233 case MOUNT_UNMOUNTING_SIGTERM:
1235 if (f == MOUNT_SUCCESS)
1236 mount_enter_dead(m, f);
1237 else if (m->from_proc_self_mountinfo)
1238 mount_enter_mounted(m, f);
1240 mount_enter_dead(m, f);
1244 assert_not_reached("Uh, control process died at wrong time.");
1247 /* Notify clients about changed exit status */
1248 unit_add_to_dbus_queue(u);
1251 static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
1252 Mount *m = MOUNT(userdata);
1255 assert(m->timer_event_source == source);
1259 case MOUNT_MOUNTING:
1260 case MOUNT_MOUNTING_DONE:
1261 log_unit_warning(UNIT(m)->id,
1262 "%s mounting timed out. Stopping.", UNIT(m)->id);
1263 mount_enter_signal(m, MOUNT_MOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1266 case MOUNT_REMOUNTING:
1267 log_unit_warning(UNIT(m)->id,
1268 "%s remounting timed out. Stopping.", UNIT(m)->id);
1269 m->reload_result = MOUNT_FAILURE_TIMEOUT;
1270 mount_enter_mounted(m, MOUNT_SUCCESS);
1273 case MOUNT_UNMOUNTING:
1274 log_unit_warning(UNIT(m)->id,
1275 "%s unmounting timed out. Stopping.", UNIT(m)->id);
1276 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1279 case MOUNT_MOUNTING_SIGTERM:
1280 if (m->kill_context.send_sigkill) {
1281 log_unit_warning(UNIT(m)->id,
1282 "%s mounting timed out. Killing.", UNIT(m)->id);
1283 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1285 log_unit_warning(UNIT(m)->id,
1286 "%s mounting timed out. Skipping SIGKILL. Ignoring.",
1289 if (m->from_proc_self_mountinfo)
1290 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1292 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1296 case MOUNT_REMOUNTING_SIGTERM:
1297 if (m->kill_context.send_sigkill) {
1298 log_unit_warning(UNIT(m)->id,
1299 "%s remounting timed out. Killing.", UNIT(m)->id);
1300 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1302 log_unit_warning(UNIT(m)->id,
1303 "%s remounting timed out. Skipping SIGKILL. Ignoring.",
1306 if (m->from_proc_self_mountinfo)
1307 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1309 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1313 case MOUNT_UNMOUNTING_SIGTERM:
1314 if (m->kill_context.send_sigkill) {
1315 log_unit_warning(UNIT(m)->id,
1316 "%s unmounting timed out. Killing.", UNIT(m)->id);
1317 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1319 log_unit_warning(UNIT(m)->id,
1320 "%s unmounting timed out. Skipping SIGKILL. Ignoring.",
1323 if (m->from_proc_self_mountinfo)
1324 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1326 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1330 case MOUNT_MOUNTING_SIGKILL:
1331 case MOUNT_REMOUNTING_SIGKILL:
1332 case MOUNT_UNMOUNTING_SIGKILL:
1333 log_unit_warning(UNIT(m)->id,
1334 "%s mount process still around after SIGKILL. Ignoring.",
1337 if (m->from_proc_self_mountinfo)
1338 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1340 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1344 assert_not_reached("Timeout at wrong time.");
1350 static int mount_add_one(
1354 const char *options,
1358 _cleanup_free_ char *e = NULL, *w = NULL, *o = NULL, *f = NULL;
1359 bool load_extras = false;
1361 bool delete, changed = false;
1371 /* Ignore API mount points. They should never be referenced in
1372 * dependencies ever. */
1373 if (mount_point_is_api(where) || mount_point_ignore(where))
1376 if (streq(fstype, "autofs"))
1379 /* probably some kind of swap, ignore */
1380 if (!is_path(where))
1383 e = unit_name_from_path(where, ".mount");
1387 u = manager_get_unit(m, e);
1391 u = unit_new(m, sizeof(Mount));
1395 r = unit_add_name(u, e);
1399 MOUNT(u)->where = strdup(where);
1400 if (!MOUNT(u)->where) {
1405 u->source_path = strdup("/proc/self/mountinfo");
1406 if (!u->source_path) {
1412 if (m->running_as == SYSTEMD_SYSTEM) {
1415 target = fstype_is_network(fstype) ? SPECIAL_REMOTE_FS_TARGET : SPECIAL_LOCAL_FS_TARGET;
1417 r = unit_add_dependency_by_name(u, UNIT_BEFORE, target, NULL, true);
1421 if (should_umount(MOUNT(u))) {
1422 r = unit_add_dependency_by_name(u, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
1428 unit_add_to_load_queue(u);
1433 if (!MOUNT(u)->where) {
1434 MOUNT(u)->where = strdup(where);
1435 if (!MOUNT(u)->where) {
1441 if (u->load_state == UNIT_NOT_FOUND) {
1442 u->load_state = UNIT_LOADED;
1445 /* Load in the extras later on, after we
1446 * finished initialization of the unit */
1453 o = strdup(options);
1455 if (!w || !o || !f) {
1460 p = &MOUNT(u)->parameters_proc_self_mountinfo;
1462 changed = changed ||
1463 !streq_ptr(p->options, options) ||
1464 !streq_ptr(p->what, what) ||
1465 !streq_ptr(p->fstype, fstype);
1468 MOUNT(u)->is_mounted = true;
1469 MOUNT(u)->just_mounted = !MOUNT(u)->from_proc_self_mountinfo;
1470 MOUNT(u)->just_changed = changed;
1473 MOUNT(u)->from_proc_self_mountinfo = true;
1488 r = mount_add_extras(MOUNT(u));
1494 unit_add_to_dbus_queue(u);
1505 static inline void mnt_free_table_p(struct libmnt_table **tb) {
1506 mnt_free_table(*tb);
1509 static inline void mnt_free_iter_p(struct libmnt_iter **itr) {
1510 mnt_free_iter(*itr);
1513 static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
1514 _cleanup_(mnt_free_table_p) struct libmnt_table *tb = NULL;
1515 _cleanup_(mnt_free_iter_p) struct libmnt_iter *itr = NULL;
1516 struct libmnt_fs *fs;
1521 tb = mnt_new_table();
1522 itr = mnt_new_iter(MNT_ITER_FORWARD);
1526 mnt_table_parse_mtab(tb, NULL);
1530 while (mnt_table_next_fs(tb, itr, &fs) == 0) {
1531 const char *device, *path, *options, *fstype;
1532 _cleanup_free_ const char *d = NULL, *p = NULL;
1535 device = mnt_fs_get_source(fs);
1536 path = mnt_fs_get_target(fs);
1537 options = mnt_fs_get_options(fs);
1538 fstype = mnt_fs_get_fstype(fs);
1540 d = cunescape(device);
1541 p = cunescape(path);
1545 k = mount_add_one(m, d, p, options, fstype, set_flags);
1553 static void mount_shutdown(Manager *m) {
1556 m->mount_event_source = sd_event_source_unref(m->mount_event_source);
1557 m->mount_utab_event_source = sd_event_source_unref(m->mount_utab_event_source);
1559 if (m->proc_self_mountinfo) {
1560 fclose(m->proc_self_mountinfo);
1561 m->proc_self_mountinfo = NULL;
1563 m->utab_inotify_fd = safe_close(m->utab_inotify_fd);
1566 static int mount_get_timeout(Unit *u, uint64_t *timeout) {
1567 Mount *m = MOUNT(u);
1570 if (!m->timer_event_source)
1573 r = sd_event_source_get_time(m->timer_event_source, timeout);
1580 static int mount_enumerate(Manager *m) {
1586 if (!m->proc_self_mountinfo) {
1587 m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
1588 if (!m->proc_self_mountinfo)
1591 r = sd_event_add_io(m->event, &m->mount_event_source, fileno(m->proc_self_mountinfo), EPOLLPRI, mount_dispatch_io, m);
1595 /* Dispatch this before we dispatch SIGCHLD, so that
1596 * we always get the events from /proc/self/mountinfo
1597 * before the SIGCHLD of /bin/mount. */
1598 r = sd_event_source_set_priority(m->mount_event_source, -10);
1603 if (m->utab_inotify_fd < 0) {
1604 m->utab_inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
1605 if (m->utab_inotify_fd < 0)
1606 goto fail_with_errno;
1608 r = inotify_add_watch(m->utab_inotify_fd, "/run/mount", IN_MOVED_TO);
1610 goto fail_with_errno;
1612 r = sd_event_add_io(m->event, &m->mount_utab_event_source, m->utab_inotify_fd, EPOLLIN, mount_dispatch_io, m);
1616 r = sd_event_source_set_priority(m->mount_utab_event_source, -10);
1621 r = mount_load_proc_self_mountinfo(m, false);
1634 static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
1635 Manager *m = userdata;
1640 assert(revents & (EPOLLPRI | EPOLLIN));
1642 /* The manager calls this for every fd event happening on the
1643 * /proc/self/mountinfo file, which informs us about mounting
1645 * This may also be called for /run/mount events */
1647 if (fd == m->utab_inotify_fd) {
1648 char inotify_buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
1649 struct inotify_event *event;
1653 while ((r = read(fd, inotify_buffer, sizeof(inotify_buffer))) > 0) {
1654 for (p = inotify_buffer; p < inotify_buffer + r; ) {
1655 event = (struct inotify_event *) p;
1656 /* only care about changes to utab, but we have
1657 * to monitor the directory to reliably get
1658 * notifications about when utab is replaced
1659 * using rename(2) */
1660 if (strcmp(event->name, "utab") == 0)
1662 p += sizeof(struct inotify_event) + event->len;
1669 r = mount_load_proc_self_mountinfo(m, true);
1671 log_error_errno(r, "Failed to reread /proc/self/mountinfo: %m");
1673 /* Reset flags, just in case, for later calls */
1674 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1675 Mount *mount = MOUNT(u);
1677 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1683 manager_dispatch_load_queue(m);
1685 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1686 Mount *mount = MOUNT(u);
1688 if (!mount->is_mounted) {
1690 mount->from_proc_self_mountinfo = false;
1692 switch (mount->state) {
1695 /* This has just been unmounted by
1696 * somebody else, follow the state
1698 mount_enter_dead(mount, MOUNT_SUCCESS);
1705 } else if (mount->just_mounted || mount->just_changed) {
1707 /* New or changed mount entry */
1709 switch (mount->state) {
1713 /* This has just been mounted by
1714 * somebody else, follow the state
1716 mount_enter_mounted(mount, MOUNT_SUCCESS);
1719 case MOUNT_MOUNTING:
1720 mount_set_state(mount, MOUNT_MOUNTING_DONE);
1724 /* Nothing really changed, but let's
1725 * issue an notification call
1726 * nonetheless, in case somebody is
1727 * waiting for this. (e.g. file system
1728 * ro/rw remounts.) */
1729 mount_set_state(mount, mount->state);
1734 /* Reset the flags for later calls */
1735 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1741 static void mount_reset_failed(Unit *u) {
1742 Mount *m = MOUNT(u);
1746 if (m->state == MOUNT_FAILED)
1747 mount_set_state(m, MOUNT_DEAD);
1749 m->result = MOUNT_SUCCESS;
1750 m->reload_result = MOUNT_SUCCESS;
1753 static int mount_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
1754 return unit_kill_common(u, who, signo, -1, MOUNT(u)->control_pid, error);
1757 static const char* const mount_state_table[_MOUNT_STATE_MAX] = {
1758 [MOUNT_DEAD] = "dead",
1759 [MOUNT_MOUNTING] = "mounting",
1760 [MOUNT_MOUNTING_DONE] = "mounting-done",
1761 [MOUNT_MOUNTED] = "mounted",
1762 [MOUNT_REMOUNTING] = "remounting",
1763 [MOUNT_UNMOUNTING] = "unmounting",
1764 [MOUNT_MOUNTING_SIGTERM] = "mounting-sigterm",
1765 [MOUNT_MOUNTING_SIGKILL] = "mounting-sigkill",
1766 [MOUNT_REMOUNTING_SIGTERM] = "remounting-sigterm",
1767 [MOUNT_REMOUNTING_SIGKILL] = "remounting-sigkill",
1768 [MOUNT_UNMOUNTING_SIGTERM] = "unmounting-sigterm",
1769 [MOUNT_UNMOUNTING_SIGKILL] = "unmounting-sigkill",
1770 [MOUNT_FAILED] = "failed"
1773 DEFINE_STRING_TABLE_LOOKUP(mount_state, MountState);
1775 static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
1776 [MOUNT_EXEC_MOUNT] = "ExecMount",
1777 [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
1778 [MOUNT_EXEC_REMOUNT] = "ExecRemount",
1781 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
1783 static const char* const mount_result_table[_MOUNT_RESULT_MAX] = {
1784 [MOUNT_SUCCESS] = "success",
1785 [MOUNT_FAILURE_RESOURCES] = "resources",
1786 [MOUNT_FAILURE_TIMEOUT] = "timeout",
1787 [MOUNT_FAILURE_EXIT_CODE] = "exit-code",
1788 [MOUNT_FAILURE_SIGNAL] = "signal",
1789 [MOUNT_FAILURE_CORE_DUMP] = "core-dump"
1792 DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult);
1794 const UnitVTable mount_vtable = {
1795 .object_size = sizeof(Mount),
1796 .exec_context_offset = offsetof(Mount, exec_context),
1797 .cgroup_context_offset = offsetof(Mount, cgroup_context),
1798 .kill_context_offset = offsetof(Mount, kill_context),
1799 .exec_runtime_offset = offsetof(Mount, exec_runtime),
1805 .private_section = "Mount",
1808 .no_instances = true,
1814 .coldplug = mount_coldplug,
1818 .start = mount_start,
1820 .reload = mount_reload,
1824 .serialize = mount_serialize,
1825 .deserialize_item = mount_deserialize_item,
1827 .active_state = mount_active_state,
1828 .sub_state_to_string = mount_sub_state_to_string,
1830 .check_gc = mount_check_gc,
1832 .sigchld_event = mount_sigchld_event,
1834 .reset_failed = mount_reset_failed,
1836 .bus_interface = "org.freedesktop.systemd1.Mount",
1837 .bus_vtable = bus_mount_vtable,
1838 .bus_set_property = bus_mount_set_property,
1839 .bus_commit_properties = bus_mount_commit_properties,
1841 .get_timeout = mount_get_timeout,
1843 .can_transient = true,
1845 .enumerate = mount_enumerate,
1846 .shutdown = mount_shutdown,
1848 .status_message_formats = {
1849 .starting_stopping = {
1850 [0] = "Mounting %s...",
1851 [1] = "Unmounting %s...",
1853 .finished_start_job = {
1854 [JOB_DONE] = "Mounted %s.",
1855 [JOB_FAILED] = "Failed to mount %s.",
1856 [JOB_DEPENDENCY] = "Dependency failed for %s.",
1857 [JOB_TIMEOUT] = "Timed out mounting %s.",
1859 .finished_stop_job = {
1860 [JOB_DONE] = "Unmounted %s.",
1861 [JOB_FAILED] = "Failed unmounting %s.",
1862 [JOB_TIMEOUT] = "Timed out unmounting %s.",