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_time(
167 UNIT(m)->manager->event,
168 &m->timer_event_source,
170 now(CLOCK_MONOTONIC) + m->timeout_usec, 0,
171 mount_dispatch_timer, m);
174 static void mount_unwatch_control_pid(Mount *m) {
177 if (m->control_pid <= 0)
180 unit_unwatch_pid(UNIT(m), m->control_pid);
184 static void mount_parameters_done(MountParameters *p) {
191 p->what = p->options = p->fstype = NULL;
194 static void mount_done(Unit *u) {
202 mount_parameters_done(&m->parameters_proc_self_mountinfo);
203 mount_parameters_done(&m->parameters_fragment);
205 m->exec_runtime = exec_runtime_unref(m->exec_runtime);
206 exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
207 m->control_command = NULL;
209 mount_unwatch_control_pid(m);
211 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
214 _pure_ static MountParameters* get_mount_parameters_fragment(Mount *m) {
217 if (m->from_fragment)
218 return &m->parameters_fragment;
223 _pure_ static MountParameters* get_mount_parameters(Mount *m) {
226 if (m->from_proc_self_mountinfo)
227 return &m->parameters_proc_self_mountinfo;
229 return get_mount_parameters_fragment(m);
232 static int mount_add_mount_links(Mount *m) {
233 _cleanup_free_ char *parent = NULL;
242 if (!path_equal(m->where, "/")) {
243 /* Adds in links to other mount points that might lie further
244 * up in the hierarchy */
245 r = path_get_parent(m->where, &parent);
249 r = unit_require_mounts_for(UNIT(m), parent);
254 /* Adds in links to other mount points that might be needed
255 * for the source path (if this is a bind mount) to be
257 pm = get_mount_parameters_fragment(m);
258 if (pm && pm->what &&
259 path_is_absolute(pm->what) &&
260 !mount_is_network(pm)) {
262 r = unit_require_mounts_for(UNIT(m), pm->what);
267 /* Adds in links to other units that use this path or paths
268 * further down in the hierarchy */
269 s = manager_get_units_requiring_mounts_for(UNIT(m)->manager, m->where);
270 SET_FOREACH(other, s, i) {
272 if (other->load_state != UNIT_LOADED)
275 if (other == UNIT(m))
278 r = unit_add_dependency(other, UNIT_AFTER, UNIT(m), true);
282 if (UNIT(m)->fragment_path) {
283 /* If we have fragment configuration, then make this dependency required */
284 r = unit_add_dependency(other, UNIT_REQUIRES, UNIT(m), true);
293 static int mount_add_device_links(Mount *m) {
295 bool device_wants_mount = false;
300 p = get_mount_parameters_fragment(m);
307 if (mount_is_bind(p))
310 if (!is_device_path(p->what))
313 if (path_equal(m->where, "/"))
316 if (mount_is_auto(p) && UNIT(m)->manager->running_as == SYSTEMD_SYSTEM)
317 device_wants_mount = true;
319 r = unit_add_node_link(UNIT(m), p->what, device_wants_mount);
326 static int mount_add_quota_links(Mount *m) {
332 if (UNIT(m)->manager->running_as != SYSTEMD_SYSTEM)
335 p = get_mount_parameters_fragment(m);
342 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTACHECK_SERVICE, NULL, true);
346 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTAON_SERVICE, NULL, true);
353 static bool should_umount(Mount *m) {
356 if (path_equal(m->where, "/") ||
357 path_equal(m->where, "/usr"))
360 p = get_mount_parameters(m);
361 if (p && mount_test_option(p->options, "x-initrd.mount") &&
368 static int mount_add_default_dependencies(Mount *m) {
369 const char *after, *after2, *online;
375 if (UNIT(m)->manager->running_as != SYSTEMD_SYSTEM)
378 p = get_mount_parameters(m);
383 if (path_equal(m->where, "/") ||
384 path_equal(m->where, "/usr"))
387 if (mount_is_network(p)) {
388 after = SPECIAL_REMOTE_FS_PRE_TARGET;
389 after2 = SPECIAL_NETWORK_TARGET;
390 online = SPECIAL_NETWORK_ONLINE_TARGET;
392 after = SPECIAL_LOCAL_FS_PRE_TARGET;
397 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after, NULL, true);
402 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after2, NULL, true);
408 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_WANTS, UNIT_AFTER, online, NULL, true);
413 if (should_umount(m)) {
414 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
422 static int mount_verify(Mount *m) {
423 _cleanup_free_ char *e = NULL;
428 if (UNIT(m)->load_state != UNIT_LOADED)
431 if (!m->from_fragment && !m->from_proc_self_mountinfo)
434 e = unit_name_from_path(m->where, ".mount");
438 b = unit_has_name(UNIT(m), e);
440 log_error_unit(UNIT(m)->id, "%s's Where= setting doesn't match unit name. Refusing.", UNIT(m)->id);
444 if (mount_point_is_api(m->where) || mount_point_ignore(m->where)) {
445 log_error_unit(UNIT(m)->id, "Cannot create mount unit for API file system %s. Refusing.", m->where);
449 if (UNIT(m)->fragment_path && !m->parameters_fragment.what) {
450 log_error_unit(UNIT(m)->id, "%s's What setting is missing. Refusing.", UNIT(m)->id);
454 if (m->exec_context.pam_name && m->kill_context.kill_mode != KILL_CONTROL_GROUP) {
455 log_error_unit(UNIT(m)->id, "%s has PAM enabled. Kill mode must be set to control-group'. Refusing.",UNIT(m)->id);
462 static int mount_add_extras(Mount *m) {
468 if (u->fragment_path)
469 m->from_fragment = true;
472 m->where = unit_name_to_path(u->id);
477 path_kill_slashes(m->where);
479 if (!u->description) {
480 r = unit_set_description(u, m->where);
485 r = mount_add_device_links(m);
489 r = mount_add_mount_links(m);
493 r = mount_add_quota_links(m);
497 r = unit_patch_contexts(u);
501 r = unit_add_exec_dependencies(u, &m->exec_context);
505 r = unit_add_default_slice(u, &m->cgroup_context);
509 if (u->default_dependencies) {
510 r = mount_add_default_dependencies(m);
518 static int mount_load(Unit *u) {
523 assert(u->load_state == UNIT_STUB);
525 if (m->from_proc_self_mountinfo)
526 r = unit_load_fragment_and_dropin_optional(u);
528 r = unit_load_fragment_and_dropin(u);
533 /* This is a new unit? Then let's add in some extras */
534 if (u->load_state == UNIT_LOADED) {
535 r = mount_add_extras(m);
540 return mount_verify(m);
543 static int mount_notify_automount(Mount *m, int status) {
550 SET_FOREACH(p, UNIT(m)->dependencies[UNIT_TRIGGERED_BY], i)
551 if (p->type == UNIT_AUTOMOUNT) {
552 r = automount_send_ready(AUTOMOUNT(p), status);
560 static void mount_set_state(Mount *m, MountState state) {
561 MountState old_state;
564 old_state = m->state;
567 if (state != MOUNT_MOUNTING &&
568 state != MOUNT_MOUNTING_DONE &&
569 state != MOUNT_REMOUNTING &&
570 state != MOUNT_UNMOUNTING &&
571 state != MOUNT_MOUNTING_SIGTERM &&
572 state != MOUNT_MOUNTING_SIGKILL &&
573 state != MOUNT_UNMOUNTING_SIGTERM &&
574 state != MOUNT_UNMOUNTING_SIGKILL &&
575 state != MOUNT_REMOUNTING_SIGTERM &&
576 state != MOUNT_REMOUNTING_SIGKILL) {
577 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
578 mount_unwatch_control_pid(m);
579 m->control_command = NULL;
580 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
583 if (state == MOUNT_MOUNTED ||
584 state == MOUNT_REMOUNTING)
585 mount_notify_automount(m, 0);
586 else if (state == MOUNT_DEAD ||
587 state == MOUNT_UNMOUNTING ||
588 state == MOUNT_MOUNTING_SIGTERM ||
589 state == MOUNT_MOUNTING_SIGKILL ||
590 state == MOUNT_REMOUNTING_SIGTERM ||
591 state == MOUNT_REMOUNTING_SIGKILL ||
592 state == MOUNT_UNMOUNTING_SIGTERM ||
593 state == MOUNT_UNMOUNTING_SIGKILL ||
594 state == MOUNT_FAILED) {
595 if (state != old_state)
596 mount_notify_automount(m, -ENODEV);
599 if (state != old_state)
600 log_debug_unit(UNIT(m)->id,
601 "%s changed %s -> %s",
603 mount_state_to_string(old_state),
604 mount_state_to_string(state));
606 unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state], m->reload_result == MOUNT_SUCCESS);
607 m->reload_result = MOUNT_SUCCESS;
610 static int mount_coldplug(Unit *u) {
612 MountState new_state = MOUNT_DEAD;
616 assert(m->state == MOUNT_DEAD);
618 if (m->deserialized_state != m->state)
619 new_state = m->deserialized_state;
620 else if (m->from_proc_self_mountinfo)
621 new_state = MOUNT_MOUNTED;
623 if (new_state == m->state)
626 if (new_state == MOUNT_MOUNTING ||
627 new_state == MOUNT_MOUNTING_DONE ||
628 new_state == MOUNT_REMOUNTING ||
629 new_state == MOUNT_UNMOUNTING ||
630 new_state == MOUNT_MOUNTING_SIGTERM ||
631 new_state == MOUNT_MOUNTING_SIGKILL ||
632 new_state == MOUNT_UNMOUNTING_SIGTERM ||
633 new_state == MOUNT_UNMOUNTING_SIGKILL ||
634 new_state == MOUNT_REMOUNTING_SIGTERM ||
635 new_state == MOUNT_REMOUNTING_SIGKILL) {
637 if (m->control_pid <= 0)
640 r = unit_watch_pid(UNIT(m), m->control_pid);
644 r = mount_arm_timer(m);
649 mount_set_state(m, new_state);
653 static void mount_dump(Unit *u, FILE *f, const char *prefix) {
660 p = get_mount_parameters(m);
663 "%sMount State: %s\n"
667 "%sFile System Type: %s\n"
669 "%sFrom /proc/self/mountinfo: %s\n"
670 "%sFrom fragment: %s\n"
671 "%sDirectoryMode: %04o\n",
672 prefix, mount_state_to_string(m->state),
673 prefix, mount_result_to_string(m->result),
675 prefix, p ? strna(p->what) : "n/a",
676 prefix, p ? strna(p->fstype) : "n/a",
677 prefix, p ? strna(p->options) : "n/a",
678 prefix, yes_no(m->from_proc_self_mountinfo),
679 prefix, yes_no(m->from_fragment),
680 prefix, m->directory_mode);
682 if (m->control_pid > 0)
684 "%sControl PID: "PID_FMT"\n",
685 prefix, m->control_pid);
687 exec_context_dump(&m->exec_context, f, prefix);
688 kill_context_dump(&m->kill_context, f, prefix);
691 static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
694 ExecParameters exec_params = {
695 .apply_permissions = true,
696 .apply_chroot = true,
697 .apply_tty_stdin = true,
704 unit_realize_cgroup(UNIT(m));
706 r = unit_setup_exec_runtime(UNIT(m));
710 r = mount_arm_timer(m);
714 exec_params.environment = UNIT(m)->manager->environment;
715 exec_params.confirm_spawn = UNIT(m)->manager->confirm_spawn;
716 exec_params.cgroup_supported = UNIT(m)->manager->cgroup_supported;
717 exec_params.cgroup_path = UNIT(m)->cgroup_path;
718 exec_params.runtime_prefix = manager_get_runtime_prefix(UNIT(m)->manager);
719 exec_params.unit_id = UNIT(m)->id;
729 r = unit_watch_pid(UNIT(m), pid);
731 /* FIXME: we need to do something here */
739 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
744 static void mount_enter_dead(Mount *m, MountResult f) {
747 if (f != MOUNT_SUCCESS)
750 exec_runtime_destroy(m->exec_runtime);
751 m->exec_runtime = exec_runtime_unref(m->exec_runtime);
753 exec_context_destroy_runtime_directory(&m->exec_context, manager_get_runtime_prefix(UNIT(m)->manager));
755 mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD);
758 static void mount_enter_mounted(Mount *m, MountResult f) {
761 if (f != MOUNT_SUCCESS)
764 mount_set_state(m, MOUNT_MOUNTED);
767 static void mount_enter_signal(Mount *m, MountState state, MountResult f) {
772 if (f != MOUNT_SUCCESS)
775 r = unit_kill_context(
778 state != MOUNT_MOUNTING_SIGTERM && state != MOUNT_UNMOUNTING_SIGTERM && state != MOUNT_REMOUNTING_SIGTERM,
786 r = mount_arm_timer(m);
790 mount_set_state(m, state);
791 } else if (state == MOUNT_REMOUNTING_SIGTERM)
792 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS);
793 else if (state == MOUNT_REMOUNTING_SIGKILL)
794 mount_enter_mounted(m, MOUNT_SUCCESS);
795 else if (state == MOUNT_MOUNTING_SIGTERM)
796 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, MOUNT_SUCCESS);
797 else if (state == MOUNT_UNMOUNTING_SIGTERM)
798 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_SUCCESS);
800 mount_enter_dead(m, MOUNT_SUCCESS);
805 log_warning_unit(UNIT(m)->id,
806 "%s failed to kill processes: %s", UNIT(m)->id, strerror(-r));
808 if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
809 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
811 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
814 void warn_if_dir_nonempty(const char *unit, const char* where) {
818 if (dir_is_empty(where) > 0)
821 log_struct_unit(LOG_NOTICE,
823 "MESSAGE=%s: Directory %s to mount over is not empty, mounting anyway.",
826 MESSAGE_ID(SD_MESSAGE_OVERMOUNTING),
830 static int fail_if_symlink(const char *unit, const char* where) {
833 if (is_symlink(where) > 0) {
834 log_struct_unit(LOG_WARNING,
836 "MESSAGE=%s: Mount on symlink %s not allowed.",
839 MESSAGE_ID(SD_MESSAGE_OVERMOUNTING),
847 static void mount_enter_unmounting(Mount *m) {
852 m->control_command_id = MOUNT_EXEC_UNMOUNT;
853 m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
855 if ((r = exec_command_set(
863 mount_unwatch_control_pid(m);
865 if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
868 mount_set_state(m, MOUNT_UNMOUNTING);
873 log_warning_unit(UNIT(m)->id,
874 "%s failed to run 'umount' task: %s",
875 UNIT(m)->id, strerror(-r));
876 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
879 static void mount_enter_mounting(Mount *m) {
885 m->control_command_id = MOUNT_EXEC_MOUNT;
886 m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
888 mkdir_p_label(m->where, m->directory_mode);
890 warn_if_dir_nonempty(m->meta.id, m->where);
892 /* Create the source directory for bind-mounts if needed */
893 p = get_mount_parameters_fragment(m);
894 if (p && mount_is_bind(p))
895 mkdir_p_label(p->what, m->directory_mode);
897 r = fail_if_symlink(m->meta.id, m->where);
901 if (m->from_fragment)
902 r = exec_command_set(
905 m->sloppy_options ? "-ns" : "-n",
906 m->parameters_fragment.what,
908 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
909 m->parameters_fragment.options ? "-o" : NULL, m->parameters_fragment.options,
917 mount_unwatch_control_pid(m);
919 r = mount_spawn(m, m->control_command, &m->control_pid);
923 mount_set_state(m, MOUNT_MOUNTING);
928 log_warning_unit(UNIT(m)->id,
929 "%s failed to run 'mount' task: %s",
930 UNIT(m)->id, strerror(-r));
931 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
934 static void mount_enter_remounting(Mount *m) {
939 m->control_command_id = MOUNT_EXEC_REMOUNT;
940 m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
942 if (m->from_fragment) {
945 if (m->parameters_fragment.options)
946 o = strappenda("remount,", m->parameters_fragment.options);
950 r = exec_command_set(
953 m->sloppy_options ? "-ns" : "-n",
954 m->parameters_fragment.what,
956 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
965 mount_unwatch_control_pid(m);
967 r = mount_spawn(m, m->control_command, &m->control_pid);
971 mount_set_state(m, MOUNT_REMOUNTING);
976 log_warning_unit(UNIT(m)->id,
977 "%s failed to run 'remount' task: %s",
978 UNIT(m)->id, strerror(-r));
979 m->reload_result = MOUNT_FAILURE_RESOURCES;
980 mount_enter_mounted(m, MOUNT_SUCCESS);
983 static int mount_start(Unit *u) {
988 /* We cannot fulfill this request right now, try again later
990 if (m->state == MOUNT_UNMOUNTING ||
991 m->state == MOUNT_UNMOUNTING_SIGTERM ||
992 m->state == MOUNT_UNMOUNTING_SIGKILL ||
993 m->state == MOUNT_MOUNTING_SIGTERM ||
994 m->state == MOUNT_MOUNTING_SIGKILL)
998 if (m->state == MOUNT_MOUNTING)
1001 assert(m->state == MOUNT_DEAD || m->state == MOUNT_FAILED);
1003 m->result = MOUNT_SUCCESS;
1004 m->reload_result = MOUNT_SUCCESS;
1006 mount_enter_mounting(m);
1010 static int mount_stop(Unit *u) {
1011 Mount *m = MOUNT(u);
1016 if (m->state == MOUNT_UNMOUNTING ||
1017 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1018 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1019 m->state == MOUNT_MOUNTING_SIGTERM ||
1020 m->state == MOUNT_MOUNTING_SIGKILL)
1023 assert(m->state == MOUNT_MOUNTING ||
1024 m->state == MOUNT_MOUNTING_DONE ||
1025 m->state == MOUNT_MOUNTED ||
1026 m->state == MOUNT_REMOUNTING ||
1027 m->state == MOUNT_REMOUNTING_SIGTERM ||
1028 m->state == MOUNT_REMOUNTING_SIGKILL);
1030 mount_enter_unmounting(m);
1034 static int mount_reload(Unit *u) {
1035 Mount *m = MOUNT(u);
1039 if (m->state == MOUNT_MOUNTING_DONE)
1042 assert(m->state == MOUNT_MOUNTED);
1044 mount_enter_remounting(m);
1048 static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
1049 Mount *m = MOUNT(u);
1055 unit_serialize_item(u, f, "state", mount_state_to_string(m->state));
1056 unit_serialize_item(u, f, "result", mount_result_to_string(m->result));
1057 unit_serialize_item(u, f, "reload-result", mount_result_to_string(m->reload_result));
1059 if (m->control_pid > 0)
1060 unit_serialize_item_format(u, f, "control-pid", PID_FMT, m->control_pid);
1062 if (m->control_command_id >= 0)
1063 unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(m->control_command_id));
1068 static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1069 Mount *m = MOUNT(u);
1076 if (streq(key, "state")) {
1079 if ((state = mount_state_from_string(value)) < 0)
1080 log_debug_unit(u->id, "Failed to parse state value %s", value);
1082 m->deserialized_state = state;
1083 } else if (streq(key, "result")) {
1086 f = mount_result_from_string(value);
1088 log_debug_unit(UNIT(m)->id,
1089 "Failed to parse result value %s", value);
1090 else if (f != MOUNT_SUCCESS)
1093 } else if (streq(key, "reload-result")) {
1096 f = mount_result_from_string(value);
1098 log_debug_unit(UNIT(m)->id,
1099 "Failed to parse reload result value %s", value);
1100 else if (f != MOUNT_SUCCESS)
1101 m->reload_result = f;
1103 } else if (streq(key, "control-pid")) {
1106 if (parse_pid(value, &pid) < 0)
1107 log_debug_unit(UNIT(m)->id,
1108 "Failed to parse control-pid value %s", value);
1110 m->control_pid = pid;
1111 } else if (streq(key, "control-command")) {
1112 MountExecCommand id;
1114 if ((id = mount_exec_command_from_string(value)) < 0)
1115 log_debug_unit(UNIT(m)->id,
1116 "Failed to parse exec-command value %s", value);
1118 m->control_command_id = id;
1119 m->control_command = m->exec_command + id;
1122 log_debug_unit(UNIT(m)->id,
1123 "Unknown serialization key '%s'", key);
1128 _pure_ static UnitActiveState mount_active_state(Unit *u) {
1131 return state_translation_table[MOUNT(u)->state];
1134 _pure_ static const char *mount_sub_state_to_string(Unit *u) {
1137 return mount_state_to_string(MOUNT(u)->state);
1140 _pure_ static bool mount_check_gc(Unit *u) {
1141 Mount *m = MOUNT(u);
1145 return m->from_proc_self_mountinfo;
1148 static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1149 Mount *m = MOUNT(u);
1155 if (pid != m->control_pid)
1160 if (is_clean_exit(code, status, NULL))
1162 else if (code == CLD_EXITED)
1163 f = MOUNT_FAILURE_EXIT_CODE;
1164 else if (code == CLD_KILLED)
1165 f = MOUNT_FAILURE_SIGNAL;
1166 else if (code == CLD_DUMPED)
1167 f = MOUNT_FAILURE_CORE_DUMP;
1169 assert_not_reached("Unknown code");
1171 if (f != MOUNT_SUCCESS)
1174 if (m->control_command) {
1175 exec_status_exit(&m->control_command->exec_status, &m->exec_context, pid, code, status);
1177 m->control_command = NULL;
1178 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
1181 log_full_unit(f == MOUNT_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
1182 "%s mount process exited, code=%s status=%i",
1183 u->id, sigchld_code_to_string(code), status);
1185 /* Note that mount(8) returning and the kernel sending us a
1186 * mount table change event might happen out-of-order. If an
1187 * operation succeed we assume the kernel will follow soon too
1188 * and already change into the resulting state. If it fails
1189 * we check if the kernel still knows about the mount. and
1190 * change state accordingly. */
1194 case MOUNT_MOUNTING:
1195 case MOUNT_MOUNTING_DONE:
1196 case MOUNT_MOUNTING_SIGKILL:
1197 case MOUNT_MOUNTING_SIGTERM:
1199 if (f == MOUNT_SUCCESS)
1200 mount_enter_mounted(m, f);
1201 else if (m->from_proc_self_mountinfo)
1202 mount_enter_mounted(m, f);
1204 mount_enter_dead(m, f);
1207 case MOUNT_REMOUNTING:
1208 case MOUNT_REMOUNTING_SIGKILL:
1209 case MOUNT_REMOUNTING_SIGTERM:
1211 m->reload_result = f;
1212 if (m->from_proc_self_mountinfo)
1213 mount_enter_mounted(m, MOUNT_SUCCESS);
1215 mount_enter_dead(m, MOUNT_SUCCESS);
1219 case MOUNT_UNMOUNTING:
1220 case MOUNT_UNMOUNTING_SIGKILL:
1221 case MOUNT_UNMOUNTING_SIGTERM:
1223 if (f == MOUNT_SUCCESS)
1224 mount_enter_dead(m, f);
1225 else if (m->from_proc_self_mountinfo)
1226 mount_enter_mounted(m, f);
1228 mount_enter_dead(m, f);
1232 assert_not_reached("Uh, control process died at wrong time.");
1235 /* Notify clients about changed exit status */
1236 unit_add_to_dbus_queue(u);
1239 static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
1240 Mount *m = MOUNT(userdata);
1243 assert(m->timer_event_source == source);
1247 case MOUNT_MOUNTING:
1248 case MOUNT_MOUNTING_DONE:
1249 log_warning_unit(UNIT(m)->id,
1250 "%s mounting timed out. Stopping.", UNIT(m)->id);
1251 mount_enter_signal(m, MOUNT_MOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1254 case MOUNT_REMOUNTING:
1255 log_warning_unit(UNIT(m)->id,
1256 "%s remounting timed out. Stopping.", UNIT(m)->id);
1257 m->reload_result = MOUNT_FAILURE_TIMEOUT;
1258 mount_enter_mounted(m, MOUNT_SUCCESS);
1261 case MOUNT_UNMOUNTING:
1262 log_warning_unit(UNIT(m)->id,
1263 "%s unmounting timed out. Stopping.", UNIT(m)->id);
1264 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1267 case MOUNT_MOUNTING_SIGTERM:
1268 if (m->kill_context.send_sigkill) {
1269 log_warning_unit(UNIT(m)->id,
1270 "%s mounting timed out. Killing.", UNIT(m)->id);
1271 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1273 log_warning_unit(UNIT(m)->id,
1274 "%s mounting timed out. Skipping SIGKILL. Ignoring.",
1277 if (m->from_proc_self_mountinfo)
1278 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1280 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1284 case MOUNT_REMOUNTING_SIGTERM:
1285 if (m->kill_context.send_sigkill) {
1286 log_warning_unit(UNIT(m)->id,
1287 "%s remounting timed out. Killing.", UNIT(m)->id);
1288 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1290 log_warning_unit(UNIT(m)->id,
1291 "%s remounting timed out. Skipping SIGKILL. Ignoring.",
1294 if (m->from_proc_self_mountinfo)
1295 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1297 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1301 case MOUNT_UNMOUNTING_SIGTERM:
1302 if (m->kill_context.send_sigkill) {
1303 log_warning_unit(UNIT(m)->id,
1304 "%s unmounting timed out. Killing.", UNIT(m)->id);
1305 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1307 log_warning_unit(UNIT(m)->id,
1308 "%s unmounting timed out. Skipping SIGKILL. Ignoring.",
1311 if (m->from_proc_self_mountinfo)
1312 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1314 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1318 case MOUNT_MOUNTING_SIGKILL:
1319 case MOUNT_REMOUNTING_SIGKILL:
1320 case MOUNT_UNMOUNTING_SIGKILL:
1321 log_warning_unit(UNIT(m)->id,
1322 "%s mount process still around after SIGKILL. Ignoring.",
1325 if (m->from_proc_self_mountinfo)
1326 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1328 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1332 assert_not_reached("Timeout at wrong time.");
1338 static int mount_add_one(
1342 const char *options,
1346 _cleanup_free_ char *e = NULL, *w = NULL, *o = NULL, *f = NULL;
1347 bool load_extras = false;
1349 bool delete, changed = false;
1359 /* Ignore API mount points. They should never be referenced in
1360 * dependencies ever. */
1361 if (mount_point_is_api(where) || mount_point_ignore(where))
1364 if (streq(fstype, "autofs"))
1367 /* probably some kind of swap, ignore */
1368 if (!is_path(where))
1371 e = unit_name_from_path(where, ".mount");
1375 u = manager_get_unit(m, e);
1379 u = unit_new(m, sizeof(Mount));
1383 r = unit_add_name(u, e);
1387 MOUNT(u)->where = strdup(where);
1388 if (!MOUNT(u)->where) {
1393 u->source_path = strdup("/proc/self/mountinfo");
1394 if (!u->source_path) {
1400 if (m->running_as == SYSTEMD_SYSTEM) {
1403 target = fstype_is_network(fstype) ? SPECIAL_REMOTE_FS_TARGET : SPECIAL_LOCAL_FS_TARGET;
1405 r = unit_add_dependency_by_name(u, UNIT_BEFORE, target, NULL, true);
1409 if (should_umount(MOUNT(u))) {
1410 r = unit_add_dependency_by_name(u, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
1416 unit_add_to_load_queue(u);
1421 if (!MOUNT(u)->where) {
1422 MOUNT(u)->where = strdup(where);
1423 if (!MOUNT(u)->where) {
1429 if (u->load_state == UNIT_NOT_FOUND) {
1430 u->load_state = UNIT_LOADED;
1433 /* Load in the extras later on, after we
1434 * finished initialization of the unit */
1441 o = strdup(options);
1443 if (!w || !o || !f) {
1448 p = &MOUNT(u)->parameters_proc_self_mountinfo;
1450 changed = changed ||
1451 !streq_ptr(p->options, options) ||
1452 !streq_ptr(p->what, what) ||
1453 !streq_ptr(p->fstype, fstype);
1456 MOUNT(u)->is_mounted = true;
1457 MOUNT(u)->just_mounted = !MOUNT(u)->from_proc_self_mountinfo;
1458 MOUNT(u)->just_changed = changed;
1461 MOUNT(u)->from_proc_self_mountinfo = true;
1476 r = mount_add_extras(MOUNT(u));
1482 unit_add_to_dbus_queue(u);
1493 static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
1499 rewind(m->proc_self_mountinfo);
1502 _cleanup_free_ char *device = NULL, *path = NULL, *options = NULL, *options2 = NULL, *fstype = NULL, *d = NULL, *p = NULL, *o = NULL;
1505 k = fscanf(m->proc_self_mountinfo,
1506 "%*s " /* (1) mount id */
1507 "%*s " /* (2) parent id */
1508 "%*s " /* (3) major:minor */
1509 "%*s " /* (4) root */
1510 "%ms " /* (5) mount point */
1511 "%ms" /* (6) mount options */
1512 "%*[^-]" /* (7) optional fields */
1513 "- " /* (8) separator */
1514 "%ms " /* (9) file system type */
1515 "%ms" /* (10) mount source */
1516 "%ms" /* (11) mount options 2 */
1517 "%*[^\n]", /* some rubbish at the end */
1528 log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
1532 o = strjoin(options, ",", options2, NULL);
1536 d = cunescape(device);
1537 p = cunescape(path);
1541 k = mount_add_one(m, d, p, o, fstype, set_flags);
1549 static void mount_shutdown(Manager *m) {
1552 m->mount_event_source = sd_event_source_unref(m->mount_event_source);
1554 if (m->proc_self_mountinfo) {
1555 fclose(m->proc_self_mountinfo);
1556 m->proc_self_mountinfo = NULL;
1560 static int mount_get_timeout(Unit *u, uint64_t *timeout) {
1561 Mount *m = MOUNT(u);
1564 if (!m->timer_event_source)
1567 r = sd_event_source_get_time(m->timer_event_source, timeout);
1574 static int mount_enumerate(Manager *m) {
1578 if (!m->proc_self_mountinfo) {
1579 m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
1580 if (!m->proc_self_mountinfo)
1583 r = sd_event_add_io(m->event, &m->mount_event_source, fileno(m->proc_self_mountinfo), EPOLLPRI, mount_dispatch_io, m);
1587 /* Dispatch this before we dispatch SIGCHLD, so that
1588 * we always get the events from /proc/self/mountinfo
1589 * before the SIGCHLD of /bin/mount. */
1590 r = sd_event_source_set_priority(m->mount_event_source, -10);
1595 r = mount_load_proc_self_mountinfo(m, false);
1606 static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
1607 Manager *m = userdata;
1612 assert(revents & EPOLLPRI);
1614 /* The manager calls this for every fd event happening on the
1615 * /proc/self/mountinfo file, which informs us about mounting
1618 r = mount_load_proc_self_mountinfo(m, true);
1620 log_error("Failed to reread /proc/self/mountinfo: %s", strerror(-r));
1622 /* Reset flags, just in case, for later calls */
1623 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1624 Mount *mount = MOUNT(u);
1626 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1632 manager_dispatch_load_queue(m);
1634 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1635 Mount *mount = MOUNT(u);
1637 if (!mount->is_mounted) {
1639 mount->from_proc_self_mountinfo = false;
1641 switch (mount->state) {
1644 /* This has just been unmounted by
1645 * somebody else, follow the state
1647 mount_enter_dead(mount, MOUNT_SUCCESS);
1654 } else if (mount->just_mounted || mount->just_changed) {
1656 /* New or changed mount entry */
1658 switch (mount->state) {
1662 /* This has just been mounted by
1663 * somebody else, follow the state
1665 mount_enter_mounted(mount, MOUNT_SUCCESS);
1668 case MOUNT_MOUNTING:
1669 mount_set_state(mount, MOUNT_MOUNTING_DONE);
1673 /* Nothing really changed, but let's
1674 * issue an notification call
1675 * nonetheless, in case somebody is
1676 * waiting for this. (e.g. file system
1677 * ro/rw remounts.) */
1678 mount_set_state(mount, mount->state);
1683 /* Reset the flags for later calls */
1684 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1690 static void mount_reset_failed(Unit *u) {
1691 Mount *m = MOUNT(u);
1695 if (m->state == MOUNT_FAILED)
1696 mount_set_state(m, MOUNT_DEAD);
1698 m->result = MOUNT_SUCCESS;
1699 m->reload_result = MOUNT_SUCCESS;
1702 static int mount_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
1703 return unit_kill_common(u, who, signo, -1, MOUNT(u)->control_pid, error);
1706 static const char* const mount_state_table[_MOUNT_STATE_MAX] = {
1707 [MOUNT_DEAD] = "dead",
1708 [MOUNT_MOUNTING] = "mounting",
1709 [MOUNT_MOUNTING_DONE] = "mounting-done",
1710 [MOUNT_MOUNTED] = "mounted",
1711 [MOUNT_REMOUNTING] = "remounting",
1712 [MOUNT_UNMOUNTING] = "unmounting",
1713 [MOUNT_MOUNTING_SIGTERM] = "mounting-sigterm",
1714 [MOUNT_MOUNTING_SIGKILL] = "mounting-sigkill",
1715 [MOUNT_REMOUNTING_SIGTERM] = "remounting-sigterm",
1716 [MOUNT_REMOUNTING_SIGKILL] = "remounting-sigkill",
1717 [MOUNT_UNMOUNTING_SIGTERM] = "unmounting-sigterm",
1718 [MOUNT_UNMOUNTING_SIGKILL] = "unmounting-sigkill",
1719 [MOUNT_FAILED] = "failed"
1722 DEFINE_STRING_TABLE_LOOKUP(mount_state, MountState);
1724 static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
1725 [MOUNT_EXEC_MOUNT] = "ExecMount",
1726 [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
1727 [MOUNT_EXEC_REMOUNT] = "ExecRemount",
1730 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
1732 static const char* const mount_result_table[_MOUNT_RESULT_MAX] = {
1733 [MOUNT_SUCCESS] = "success",
1734 [MOUNT_FAILURE_RESOURCES] = "resources",
1735 [MOUNT_FAILURE_TIMEOUT] = "timeout",
1736 [MOUNT_FAILURE_EXIT_CODE] = "exit-code",
1737 [MOUNT_FAILURE_SIGNAL] = "signal",
1738 [MOUNT_FAILURE_CORE_DUMP] = "core-dump"
1741 DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult);
1743 const UnitVTable mount_vtable = {
1744 .object_size = sizeof(Mount),
1745 .exec_context_offset = offsetof(Mount, exec_context),
1746 .cgroup_context_offset = offsetof(Mount, cgroup_context),
1747 .kill_context_offset = offsetof(Mount, kill_context),
1748 .exec_runtime_offset = offsetof(Mount, exec_runtime),
1754 .private_section = "Mount",
1757 .no_instances = true,
1763 .coldplug = mount_coldplug,
1767 .start = mount_start,
1769 .reload = mount_reload,
1773 .serialize = mount_serialize,
1774 .deserialize_item = mount_deserialize_item,
1776 .active_state = mount_active_state,
1777 .sub_state_to_string = mount_sub_state_to_string,
1779 .check_gc = mount_check_gc,
1781 .sigchld_event = mount_sigchld_event,
1783 .reset_failed = mount_reset_failed,
1785 .bus_interface = "org.freedesktop.systemd1.Mount",
1786 .bus_vtable = bus_mount_vtable,
1787 .bus_set_property = bus_mount_set_property,
1788 .bus_commit_properties = bus_mount_commit_properties,
1790 .get_timeout = mount_get_timeout,
1792 .can_transient = true,
1794 .enumerate = mount_enumerate,
1795 .shutdown = mount_shutdown,
1797 .status_message_formats = {
1798 .starting_stopping = {
1799 [0] = "Mounting %s...",
1800 [1] = "Unmounting %s...",
1802 .finished_start_job = {
1803 [JOB_DONE] = "Mounted %s.",
1804 [JOB_FAILED] = "Failed to mount %s.",
1805 [JOB_DEPENDENCY] = "Dependency failed for %s.",
1806 [JOB_TIMEOUT] = "Timed out mounting %s.",
1808 .finished_stop_job = {
1809 [JOB_DONE] = "Unmounted %s.",
1810 [JOB_FAILED] = "Failed unmounting %s.",
1811 [JOB_TIMEOUT] = "Timed out unmounting %s.",