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, "/"))
386 if (mount_is_network(p)) {
387 after = SPECIAL_REMOTE_FS_PRE_TARGET;
388 after2 = SPECIAL_NETWORK_TARGET;
389 online = SPECIAL_NETWORK_ONLINE_TARGET;
391 after = SPECIAL_LOCAL_FS_PRE_TARGET;
396 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after, NULL, true);
401 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after2, NULL, true);
407 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_WANTS, UNIT_AFTER, online, NULL, true);
412 if (should_umount(m)) {
413 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
421 static int mount_fix_timeouts(Mount *m) {
423 const char *timeout = NULL;
432 p = get_mount_parameters_fragment(m);
436 /* Allow configuration how long we wait for a device that
437 * backs a mount point to show up. This is useful to support
438 * endless device timeouts for devices that show up only after
439 * user input, like crypto devices. */
441 if ((timeout = mount_test_option(p->options, "comment=systemd.device-timeout")))
443 else if ((timeout = mount_test_option(p->options, "x-systemd.device-timeout")))
448 t = strndup(timeout, strcspn(timeout, ",;" WHITESPACE));
452 r = parse_sec(t, &u);
456 log_warning_unit(UNIT(m)->id,
457 "Failed to parse timeout for %s, ignoring: %s",
462 SET_FOREACH(other, UNIT(m)->dependencies[UNIT_AFTER], i) {
463 if (other->type != UNIT_DEVICE)
466 other->job_timeout = u;
472 static int mount_verify(Mount *m) {
473 _cleanup_free_ char *e = NULL;
478 if (UNIT(m)->load_state != UNIT_LOADED)
481 if (!m->from_fragment && !m->from_proc_self_mountinfo)
484 e = unit_name_from_path(m->where, ".mount");
488 b = unit_has_name(UNIT(m), e);
490 log_error_unit(UNIT(m)->id, "%s's Where= setting doesn't match unit name. Refusing.", UNIT(m)->id);
494 if (mount_point_is_api(m->where) || mount_point_ignore(m->where)) {
495 log_error_unit(UNIT(m)->id, "Cannot create mount unit for API file system %s. Refusing.", m->where);
499 if (UNIT(m)->fragment_path && !m->parameters_fragment.what) {
500 log_error_unit(UNIT(m)->id, "%s's What setting is missing. Refusing.", UNIT(m)->id);
504 if (m->exec_context.pam_name && m->kill_context.kill_mode != KILL_CONTROL_GROUP) {
505 log_error_unit(UNIT(m)->id, "%s has PAM enabled. Kill mode must be set to control-group'. Refusing.",UNIT(m)->id);
512 static int mount_add_extras(Mount *m) {
518 if (u->fragment_path)
519 m->from_fragment = true;
522 m->where = unit_name_to_path(u->id);
527 path_kill_slashes(m->where);
529 if (!u->description) {
530 r = unit_set_description(u, m->where);
535 r = mount_add_device_links(m);
539 r = mount_add_mount_links(m);
543 r = mount_add_quota_links(m);
547 r = unit_patch_contexts(u);
551 r = unit_add_exec_dependencies(u, &m->exec_context);
555 r = unit_add_default_slice(u, &m->cgroup_context);
559 r = mount_fix_timeouts(m);
563 if (u->default_dependencies) {
564 r = mount_add_default_dependencies(m);
572 static int mount_load(Unit *u) {
577 assert(u->load_state == UNIT_STUB);
579 if (m->from_proc_self_mountinfo)
580 r = unit_load_fragment_and_dropin_optional(u);
582 r = unit_load_fragment_and_dropin(u);
587 /* This is a new unit? Then let's add in some extras */
588 if (u->load_state == UNIT_LOADED) {
589 r = mount_add_extras(m);
594 return mount_verify(m);
597 static int mount_notify_automount(Mount *m, int status) {
604 SET_FOREACH(p, UNIT(m)->dependencies[UNIT_TRIGGERED_BY], i)
605 if (p->type == UNIT_AUTOMOUNT) {
606 r = automount_send_ready(AUTOMOUNT(p), status);
614 static void mount_set_state(Mount *m, MountState state) {
615 MountState old_state;
618 old_state = m->state;
621 if (state != MOUNT_MOUNTING &&
622 state != MOUNT_MOUNTING_DONE &&
623 state != MOUNT_REMOUNTING &&
624 state != MOUNT_UNMOUNTING &&
625 state != MOUNT_MOUNTING_SIGTERM &&
626 state != MOUNT_MOUNTING_SIGKILL &&
627 state != MOUNT_UNMOUNTING_SIGTERM &&
628 state != MOUNT_UNMOUNTING_SIGKILL &&
629 state != MOUNT_REMOUNTING_SIGTERM &&
630 state != MOUNT_REMOUNTING_SIGKILL) {
631 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
632 mount_unwatch_control_pid(m);
633 m->control_command = NULL;
634 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
637 if (state == MOUNT_MOUNTED ||
638 state == MOUNT_REMOUNTING)
639 mount_notify_automount(m, 0);
640 else if (state == MOUNT_DEAD ||
641 state == MOUNT_UNMOUNTING ||
642 state == MOUNT_MOUNTING_SIGTERM ||
643 state == MOUNT_MOUNTING_SIGKILL ||
644 state == MOUNT_REMOUNTING_SIGTERM ||
645 state == MOUNT_REMOUNTING_SIGKILL ||
646 state == MOUNT_UNMOUNTING_SIGTERM ||
647 state == MOUNT_UNMOUNTING_SIGKILL ||
648 state == MOUNT_FAILED) {
649 if (state != old_state)
650 mount_notify_automount(m, -ENODEV);
653 if (state != old_state)
654 log_debug_unit(UNIT(m)->id,
655 "%s changed %s -> %s",
657 mount_state_to_string(old_state),
658 mount_state_to_string(state));
660 unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state], m->reload_result == MOUNT_SUCCESS);
661 m->reload_result = MOUNT_SUCCESS;
664 static int mount_coldplug(Unit *u) {
666 MountState new_state = MOUNT_DEAD;
670 assert(m->state == MOUNT_DEAD);
672 if (m->deserialized_state != m->state)
673 new_state = m->deserialized_state;
674 else if (m->from_proc_self_mountinfo)
675 new_state = MOUNT_MOUNTED;
677 if (new_state == m->state)
680 if (new_state == MOUNT_MOUNTING ||
681 new_state == MOUNT_MOUNTING_DONE ||
682 new_state == MOUNT_REMOUNTING ||
683 new_state == MOUNT_UNMOUNTING ||
684 new_state == MOUNT_MOUNTING_SIGTERM ||
685 new_state == MOUNT_MOUNTING_SIGKILL ||
686 new_state == MOUNT_UNMOUNTING_SIGTERM ||
687 new_state == MOUNT_UNMOUNTING_SIGKILL ||
688 new_state == MOUNT_REMOUNTING_SIGTERM ||
689 new_state == MOUNT_REMOUNTING_SIGKILL) {
691 if (m->control_pid <= 0)
694 r = unit_watch_pid(UNIT(m), m->control_pid);
698 r = mount_arm_timer(m);
703 mount_set_state(m, new_state);
707 static void mount_dump(Unit *u, FILE *f, const char *prefix) {
714 p = get_mount_parameters(m);
717 "%sMount State: %s\n"
721 "%sFile System Type: %s\n"
723 "%sFrom /proc/self/mountinfo: %s\n"
724 "%sFrom fragment: %s\n"
725 "%sDirectoryMode: %04o\n",
726 prefix, mount_state_to_string(m->state),
727 prefix, mount_result_to_string(m->result),
729 prefix, p ? strna(p->what) : "n/a",
730 prefix, p ? strna(p->fstype) : "n/a",
731 prefix, p ? strna(p->options) : "n/a",
732 prefix, yes_no(m->from_proc_self_mountinfo),
733 prefix, yes_no(m->from_fragment),
734 prefix, m->directory_mode);
736 if (m->control_pid > 0)
738 "%sControl PID: "PID_FMT"\n",
739 prefix, m->control_pid);
741 exec_context_dump(&m->exec_context, f, prefix);
742 kill_context_dump(&m->kill_context, f, prefix);
745 static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
753 unit_realize_cgroup(UNIT(m));
755 r = unit_setup_exec_runtime(UNIT(m));
759 r = mount_arm_timer(m);
767 UNIT(m)->manager->environment,
771 UNIT(m)->manager->confirm_spawn,
772 UNIT(m)->manager->cgroup_supported,
773 UNIT(m)->cgroup_path,
774 manager_get_runtime_prefix(UNIT(m)->manager),
783 r = unit_watch_pid(UNIT(m), pid);
785 /* FIXME: we need to do something here */
793 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
798 static void mount_enter_dead(Mount *m, MountResult f) {
801 if (f != MOUNT_SUCCESS)
804 exec_runtime_destroy(m->exec_runtime);
805 m->exec_runtime = exec_runtime_unref(m->exec_runtime);
807 exec_context_destroy_runtime_directory(&m->exec_context, manager_get_runtime_prefix(UNIT(m)->manager));
809 mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD);
812 static void mount_enter_mounted(Mount *m, MountResult f) {
815 if (f != MOUNT_SUCCESS)
818 mount_set_state(m, MOUNT_MOUNTED);
821 static void mount_enter_signal(Mount *m, MountState state, MountResult f) {
826 if (f != MOUNT_SUCCESS)
829 r = unit_kill_context(
832 state != MOUNT_MOUNTING_SIGTERM && state != MOUNT_UNMOUNTING_SIGTERM && state != MOUNT_REMOUNTING_SIGTERM,
840 r = mount_arm_timer(m);
844 mount_set_state(m, state);
845 } else if (state == MOUNT_REMOUNTING_SIGTERM)
846 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS);
847 else if (state == MOUNT_REMOUNTING_SIGKILL)
848 mount_enter_mounted(m, MOUNT_SUCCESS);
849 else if (state == MOUNT_MOUNTING_SIGTERM)
850 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, MOUNT_SUCCESS);
851 else if (state == MOUNT_UNMOUNTING_SIGTERM)
852 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_SUCCESS);
854 mount_enter_dead(m, MOUNT_SUCCESS);
859 log_warning_unit(UNIT(m)->id,
860 "%s failed to kill processes: %s", UNIT(m)->id, strerror(-r));
862 if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
863 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
865 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
868 void warn_if_dir_nonempty(const char *unit, const char* where) {
872 if (dir_is_empty(where) > 0)
875 log_struct_unit(LOG_NOTICE,
877 "MESSAGE=%s: Directory %s to mount over is not empty, mounting anyway.",
880 MESSAGE_ID(SD_MESSAGE_OVERMOUNTING),
884 static void mount_enter_unmounting(Mount *m) {
889 m->control_command_id = MOUNT_EXEC_UNMOUNT;
890 m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
892 if ((r = exec_command_set(
899 mount_unwatch_control_pid(m);
901 if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
904 mount_set_state(m, MOUNT_UNMOUNTING);
909 log_warning_unit(UNIT(m)->id,
910 "%s failed to run 'umount' task: %s",
911 UNIT(m)->id, strerror(-r));
912 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
915 static void mount_enter_mounting(Mount *m) {
921 m->control_command_id = MOUNT_EXEC_MOUNT;
922 m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
924 mkdir_p_label(m->where, m->directory_mode);
926 warn_if_dir_nonempty(m->meta.id, m->where);
928 /* Create the source directory for bind-mounts if needed */
929 p = get_mount_parameters_fragment(m);
930 if (p && mount_is_bind(p))
931 mkdir_p_label(p->what, m->directory_mode);
933 if (m->from_fragment)
934 r = exec_command_set(
937 m->parameters_fragment.what,
939 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
940 m->parameters_fragment.options ? "-o" : NULL, m->parameters_fragment.options,
948 mount_unwatch_control_pid(m);
950 r = mount_spawn(m, m->control_command, &m->control_pid);
954 mount_set_state(m, MOUNT_MOUNTING);
959 log_warning_unit(UNIT(m)->id,
960 "%s failed to run 'mount' task: %s",
961 UNIT(m)->id, strerror(-r));
962 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
965 static void mount_enter_remounting(Mount *m) {
970 m->control_command_id = MOUNT_EXEC_REMOUNT;
971 m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
973 if (m->from_fragment) {
976 if (m->parameters_fragment.options)
977 o = strappenda("remount,", m->parameters_fragment.options);
981 r = exec_command_set(
984 m->parameters_fragment.what,
986 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
995 mount_unwatch_control_pid(m);
997 r = mount_spawn(m, m->control_command, &m->control_pid);
1001 mount_set_state(m, MOUNT_REMOUNTING);
1006 log_warning_unit(UNIT(m)->id,
1007 "%s failed to run 'remount' task: %s",
1008 UNIT(m)->id, strerror(-r));
1009 m->reload_result = MOUNT_FAILURE_RESOURCES;
1010 mount_enter_mounted(m, MOUNT_SUCCESS);
1013 static int mount_start(Unit *u) {
1014 Mount *m = MOUNT(u);
1018 /* We cannot fulfill this request right now, try again later
1020 if (m->state == MOUNT_UNMOUNTING ||
1021 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1022 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1023 m->state == MOUNT_MOUNTING_SIGTERM ||
1024 m->state == MOUNT_MOUNTING_SIGKILL)
1027 /* Already on it! */
1028 if (m->state == MOUNT_MOUNTING)
1031 assert(m->state == MOUNT_DEAD || m->state == MOUNT_FAILED);
1033 m->result = MOUNT_SUCCESS;
1034 m->reload_result = MOUNT_SUCCESS;
1036 mount_enter_mounting(m);
1040 static int mount_stop(Unit *u) {
1041 Mount *m = MOUNT(u);
1046 if (m->state == MOUNT_UNMOUNTING ||
1047 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1048 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1049 m->state == MOUNT_MOUNTING_SIGTERM ||
1050 m->state == MOUNT_MOUNTING_SIGKILL)
1053 assert(m->state == MOUNT_MOUNTING ||
1054 m->state == MOUNT_MOUNTING_DONE ||
1055 m->state == MOUNT_MOUNTED ||
1056 m->state == MOUNT_REMOUNTING ||
1057 m->state == MOUNT_REMOUNTING_SIGTERM ||
1058 m->state == MOUNT_REMOUNTING_SIGKILL);
1060 mount_enter_unmounting(m);
1064 static int mount_reload(Unit *u) {
1065 Mount *m = MOUNT(u);
1069 if (m->state == MOUNT_MOUNTING_DONE)
1072 assert(m->state == MOUNT_MOUNTED);
1074 mount_enter_remounting(m);
1078 static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
1079 Mount *m = MOUNT(u);
1085 unit_serialize_item(u, f, "state", mount_state_to_string(m->state));
1086 unit_serialize_item(u, f, "result", mount_result_to_string(m->result));
1087 unit_serialize_item(u, f, "reload-result", mount_result_to_string(m->reload_result));
1089 if (m->control_pid > 0)
1090 unit_serialize_item_format(u, f, "control-pid", PID_FMT, m->control_pid);
1092 if (m->control_command_id >= 0)
1093 unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(m->control_command_id));
1098 static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1099 Mount *m = MOUNT(u);
1106 if (streq(key, "state")) {
1109 if ((state = mount_state_from_string(value)) < 0)
1110 log_debug_unit(u->id, "Failed to parse state value %s", value);
1112 m->deserialized_state = state;
1113 } else if (streq(key, "result")) {
1116 f = mount_result_from_string(value);
1118 log_debug_unit(UNIT(m)->id,
1119 "Failed to parse result value %s", value);
1120 else if (f != MOUNT_SUCCESS)
1123 } else if (streq(key, "reload-result")) {
1126 f = mount_result_from_string(value);
1128 log_debug_unit(UNIT(m)->id,
1129 "Failed to parse reload result value %s", value);
1130 else if (f != MOUNT_SUCCESS)
1131 m->reload_result = f;
1133 } else if (streq(key, "control-pid")) {
1136 if (parse_pid(value, &pid) < 0)
1137 log_debug_unit(UNIT(m)->id,
1138 "Failed to parse control-pid value %s", value);
1140 m->control_pid = pid;
1141 } else if (streq(key, "control-command")) {
1142 MountExecCommand id;
1144 if ((id = mount_exec_command_from_string(value)) < 0)
1145 log_debug_unit(UNIT(m)->id,
1146 "Failed to parse exec-command value %s", value);
1148 m->control_command_id = id;
1149 m->control_command = m->exec_command + id;
1152 log_debug_unit(UNIT(m)->id,
1153 "Unknown serialization key '%s'", key);
1158 _pure_ static UnitActiveState mount_active_state(Unit *u) {
1161 return state_translation_table[MOUNT(u)->state];
1164 _pure_ static const char *mount_sub_state_to_string(Unit *u) {
1167 return mount_state_to_string(MOUNT(u)->state);
1170 _pure_ static bool mount_check_gc(Unit *u) {
1171 Mount *m = MOUNT(u);
1175 return m->from_proc_self_mountinfo;
1178 static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1179 Mount *m = MOUNT(u);
1185 if (pid != m->control_pid)
1190 if (is_clean_exit(code, status, NULL))
1192 else if (code == CLD_EXITED)
1193 f = MOUNT_FAILURE_EXIT_CODE;
1194 else if (code == CLD_KILLED)
1195 f = MOUNT_FAILURE_SIGNAL;
1196 else if (code == CLD_DUMPED)
1197 f = MOUNT_FAILURE_CORE_DUMP;
1199 assert_not_reached("Unknown code");
1201 if (f != MOUNT_SUCCESS)
1204 if (m->control_command) {
1205 exec_status_exit(&m->control_command->exec_status, &m->exec_context, pid, code, status);
1207 m->control_command = NULL;
1208 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
1211 log_full_unit(f == MOUNT_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
1212 "%s mount process exited, code=%s status=%i",
1213 u->id, sigchld_code_to_string(code), status);
1215 /* Note that mount(8) returning and the kernel sending us a
1216 * mount table change event might happen out-of-order. If an
1217 * operation succeed we assume the kernel will follow soon too
1218 * and already change into the resulting state. If it fails
1219 * we check if the kernel still knows about the mount. and
1220 * change state accordingly. */
1224 case MOUNT_MOUNTING:
1225 case MOUNT_MOUNTING_DONE:
1226 case MOUNT_MOUNTING_SIGKILL:
1227 case MOUNT_MOUNTING_SIGTERM:
1229 if (f == MOUNT_SUCCESS)
1230 mount_enter_mounted(m, f);
1231 else if (m->from_proc_self_mountinfo)
1232 mount_enter_mounted(m, f);
1234 mount_enter_dead(m, f);
1237 case MOUNT_REMOUNTING:
1238 case MOUNT_REMOUNTING_SIGKILL:
1239 case MOUNT_REMOUNTING_SIGTERM:
1241 m->reload_result = f;
1242 if (m->from_proc_self_mountinfo)
1243 mount_enter_mounted(m, MOUNT_SUCCESS);
1245 mount_enter_dead(m, MOUNT_SUCCESS);
1249 case MOUNT_UNMOUNTING:
1250 case MOUNT_UNMOUNTING_SIGKILL:
1251 case MOUNT_UNMOUNTING_SIGTERM:
1253 if (f == MOUNT_SUCCESS)
1254 mount_enter_dead(m, f);
1255 else if (m->from_proc_self_mountinfo)
1256 mount_enter_mounted(m, f);
1258 mount_enter_dead(m, f);
1262 assert_not_reached("Uh, control process died at wrong time.");
1265 /* Notify clients about changed exit status */
1266 unit_add_to_dbus_queue(u);
1269 static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
1270 Mount *m = MOUNT(userdata);
1273 assert(m->timer_event_source == source);
1277 case MOUNT_MOUNTING:
1278 case MOUNT_MOUNTING_DONE:
1279 log_warning_unit(UNIT(m)->id,
1280 "%s mounting timed out. Stopping.", UNIT(m)->id);
1281 mount_enter_signal(m, MOUNT_MOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1284 case MOUNT_REMOUNTING:
1285 log_warning_unit(UNIT(m)->id,
1286 "%s remounting timed out. Stopping.", UNIT(m)->id);
1287 m->reload_result = MOUNT_FAILURE_TIMEOUT;
1288 mount_enter_mounted(m, MOUNT_SUCCESS);
1291 case MOUNT_UNMOUNTING:
1292 log_warning_unit(UNIT(m)->id,
1293 "%s unmounting timed out. Stopping.", UNIT(m)->id);
1294 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1297 case MOUNT_MOUNTING_SIGTERM:
1298 if (m->kill_context.send_sigkill) {
1299 log_warning_unit(UNIT(m)->id,
1300 "%s mounting timed out. Killing.", UNIT(m)->id);
1301 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1303 log_warning_unit(UNIT(m)->id,
1304 "%s mounting timed out. Skipping SIGKILL. Ignoring.",
1307 if (m->from_proc_self_mountinfo)
1308 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1310 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1314 case MOUNT_REMOUNTING_SIGTERM:
1315 if (m->kill_context.send_sigkill) {
1316 log_warning_unit(UNIT(m)->id,
1317 "%s remounting timed out. Killing.", UNIT(m)->id);
1318 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1320 log_warning_unit(UNIT(m)->id,
1321 "%s remounting timed out. Skipping SIGKILL. Ignoring.",
1324 if (m->from_proc_self_mountinfo)
1325 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1327 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1331 case MOUNT_UNMOUNTING_SIGTERM:
1332 if (m->kill_context.send_sigkill) {
1333 log_warning_unit(UNIT(m)->id,
1334 "%s unmounting timed out. Killing.", UNIT(m)->id);
1335 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1337 log_warning_unit(UNIT(m)->id,
1338 "%s unmounting timed out. Skipping SIGKILL. Ignoring.",
1341 if (m->from_proc_self_mountinfo)
1342 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1344 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1348 case MOUNT_MOUNTING_SIGKILL:
1349 case MOUNT_REMOUNTING_SIGKILL:
1350 case MOUNT_UNMOUNTING_SIGKILL:
1351 log_warning_unit(UNIT(m)->id,
1352 "%s mount process still around after SIGKILL. Ignoring.",
1355 if (m->from_proc_self_mountinfo)
1356 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1358 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1362 assert_not_reached("Timeout at wrong time.");
1368 static int mount_add_one(
1372 const char *options,
1376 _cleanup_free_ char *e = NULL, *w = NULL, *o = NULL, *f = NULL;
1377 bool load_extras = false;
1379 bool delete, changed = false;
1389 /* Ignore API mount points. They should never be referenced in
1390 * dependencies ever. */
1391 if (mount_point_is_api(where) || mount_point_ignore(where))
1394 if (streq(fstype, "autofs"))
1397 /* probably some kind of swap, ignore */
1398 if (!is_path(where))
1401 e = unit_name_from_path(where, ".mount");
1405 u = manager_get_unit(m, e);
1409 u = unit_new(m, sizeof(Mount));
1413 r = unit_add_name(u, e);
1417 MOUNT(u)->where = strdup(where);
1418 if (!MOUNT(u)->where) {
1423 u->source_path = strdup("/proc/self/mountinfo");
1424 if (!u->source_path) {
1430 if (m->running_as == SYSTEMD_SYSTEM) {
1433 target = fstype_is_network(fstype) ? SPECIAL_REMOTE_FS_TARGET : SPECIAL_LOCAL_FS_TARGET;
1435 r = unit_add_dependency_by_name(u, UNIT_BEFORE, target, NULL, true);
1439 if (should_umount(MOUNT(u))) {
1440 r = unit_add_dependency_by_name(u, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
1446 unit_add_to_load_queue(u);
1451 if (!MOUNT(u)->where) {
1452 MOUNT(u)->where = strdup(where);
1453 if (!MOUNT(u)->where) {
1459 if (u->load_state == UNIT_NOT_FOUND) {
1460 u->load_state = UNIT_LOADED;
1463 /* Load in the extras later on, after we
1464 * finished initialization of the unit */
1471 o = strdup(options);
1473 if (!w || !o || !f) {
1478 p = &MOUNT(u)->parameters_proc_self_mountinfo;
1480 changed = changed ||
1481 !streq_ptr(p->options, options) ||
1482 !streq_ptr(p->what, what) ||
1483 !streq_ptr(p->fstype, fstype);
1486 MOUNT(u)->is_mounted = true;
1487 MOUNT(u)->just_mounted = !MOUNT(u)->from_proc_self_mountinfo;
1488 MOUNT(u)->just_changed = changed;
1491 MOUNT(u)->from_proc_self_mountinfo = true;
1506 r = mount_add_extras(MOUNT(u));
1512 unit_add_to_dbus_queue(u);
1523 static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
1529 rewind(m->proc_self_mountinfo);
1532 _cleanup_free_ char *device = NULL, *path = NULL, *options = NULL, *options2 = NULL, *fstype = NULL, *d = NULL, *p = NULL, *o = NULL;
1535 k = fscanf(m->proc_self_mountinfo,
1536 "%*s " /* (1) mount id */
1537 "%*s " /* (2) parent id */
1538 "%*s " /* (3) major:minor */
1539 "%*s " /* (4) root */
1540 "%ms " /* (5) mount point */
1541 "%ms" /* (6) mount options */
1542 "%*[^-]" /* (7) optional fields */
1543 "- " /* (8) separator */
1544 "%ms " /* (9) file system type */
1545 "%ms" /* (10) mount source */
1546 "%ms" /* (11) mount options 2 */
1547 "%*[^\n]", /* some rubbish at the end */
1558 log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
1562 o = strjoin(options, ",", options2, NULL);
1566 d = cunescape(device);
1567 p = cunescape(path);
1571 k = mount_add_one(m, d, p, o, fstype, set_flags);
1579 static void mount_shutdown(Manager *m) {
1582 m->mount_event_source = sd_event_source_unref(m->mount_event_source);
1584 if (m->proc_self_mountinfo) {
1585 fclose(m->proc_self_mountinfo);
1586 m->proc_self_mountinfo = NULL;
1590 static int mount_get_timeout(Unit *u, uint64_t *timeout) {
1591 Mount *m = MOUNT(u);
1594 if (!m->timer_event_source)
1597 r = sd_event_source_get_time(m->timer_event_source, timeout);
1604 static int mount_enumerate(Manager *m) {
1608 if (!m->proc_self_mountinfo) {
1609 m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
1610 if (!m->proc_self_mountinfo)
1613 r = sd_event_add_io(m->event, &m->mount_event_source, fileno(m->proc_self_mountinfo), EPOLLPRI, mount_dispatch_io, m);
1617 /* Dispatch this before we dispatch SIGCHLD, so that
1618 * we always get the events from /proc/self/mountinfo
1619 * before the SIGCHLD of /bin/mount. */
1620 r = sd_event_source_set_priority(m->mount_event_source, -10);
1625 r = mount_load_proc_self_mountinfo(m, false);
1636 static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
1637 Manager *m = userdata;
1642 assert(revents & EPOLLPRI);
1644 /* The manager calls this for every fd event happening on the
1645 * /proc/self/mountinfo file, which informs us about mounting
1648 r = mount_load_proc_self_mountinfo(m, true);
1650 log_error("Failed to reread /proc/self/mountinfo: %s", strerror(-r));
1652 /* Reset flags, just in case, for later calls */
1653 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1654 Mount *mount = MOUNT(u);
1656 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1662 manager_dispatch_load_queue(m);
1664 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1665 Mount *mount = MOUNT(u);
1667 if (!mount->is_mounted) {
1669 mount->from_proc_self_mountinfo = false;
1671 switch (mount->state) {
1674 /* This has just been unmounted by
1675 * somebody else, follow the state
1677 mount_enter_dead(mount, MOUNT_SUCCESS);
1684 } else if (mount->just_mounted || mount->just_changed) {
1686 /* New or changed mount entry */
1688 switch (mount->state) {
1692 /* This has just been mounted by
1693 * somebody else, follow the state
1695 mount_enter_mounted(mount, MOUNT_SUCCESS);
1698 case MOUNT_MOUNTING:
1699 mount_set_state(mount, MOUNT_MOUNTING_DONE);
1703 /* Nothing really changed, but let's
1704 * issue an notification call
1705 * nonetheless, in case somebody is
1706 * waiting for this. (e.g. file system
1707 * ro/rw remounts.) */
1708 mount_set_state(mount, mount->state);
1713 /* Reset the flags for later calls */
1714 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1720 static void mount_reset_failed(Unit *u) {
1721 Mount *m = MOUNT(u);
1725 if (m->state == MOUNT_FAILED)
1726 mount_set_state(m, MOUNT_DEAD);
1728 m->result = MOUNT_SUCCESS;
1729 m->reload_result = MOUNT_SUCCESS;
1732 static int mount_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
1733 return unit_kill_common(u, who, signo, -1, MOUNT(u)->control_pid, error);
1736 static const char* const mount_state_table[_MOUNT_STATE_MAX] = {
1737 [MOUNT_DEAD] = "dead",
1738 [MOUNT_MOUNTING] = "mounting",
1739 [MOUNT_MOUNTING_DONE] = "mounting-done",
1740 [MOUNT_MOUNTED] = "mounted",
1741 [MOUNT_REMOUNTING] = "remounting",
1742 [MOUNT_UNMOUNTING] = "unmounting",
1743 [MOUNT_MOUNTING_SIGTERM] = "mounting-sigterm",
1744 [MOUNT_MOUNTING_SIGKILL] = "mounting-sigkill",
1745 [MOUNT_REMOUNTING_SIGTERM] = "remounting-sigterm",
1746 [MOUNT_REMOUNTING_SIGKILL] = "remounting-sigkill",
1747 [MOUNT_UNMOUNTING_SIGTERM] = "unmounting-sigterm",
1748 [MOUNT_UNMOUNTING_SIGKILL] = "unmounting-sigkill",
1749 [MOUNT_FAILED] = "failed"
1752 DEFINE_STRING_TABLE_LOOKUP(mount_state, MountState);
1754 static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
1755 [MOUNT_EXEC_MOUNT] = "ExecMount",
1756 [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
1757 [MOUNT_EXEC_REMOUNT] = "ExecRemount",
1760 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
1762 static const char* const mount_result_table[_MOUNT_RESULT_MAX] = {
1763 [MOUNT_SUCCESS] = "success",
1764 [MOUNT_FAILURE_RESOURCES] = "resources",
1765 [MOUNT_FAILURE_TIMEOUT] = "timeout",
1766 [MOUNT_FAILURE_EXIT_CODE] = "exit-code",
1767 [MOUNT_FAILURE_SIGNAL] = "signal",
1768 [MOUNT_FAILURE_CORE_DUMP] = "core-dump"
1771 DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult);
1773 const UnitVTable mount_vtable = {
1774 .object_size = sizeof(Mount),
1775 .exec_context_offset = offsetof(Mount, exec_context),
1776 .cgroup_context_offset = offsetof(Mount, cgroup_context),
1777 .kill_context_offset = offsetof(Mount, kill_context),
1778 .exec_runtime_offset = offsetof(Mount, exec_runtime),
1784 .private_section = "Mount",
1787 .no_instances = true,
1793 .coldplug = mount_coldplug,
1797 .start = mount_start,
1799 .reload = mount_reload,
1803 .serialize = mount_serialize,
1804 .deserialize_item = mount_deserialize_item,
1806 .active_state = mount_active_state,
1807 .sub_state_to_string = mount_sub_state_to_string,
1809 .check_gc = mount_check_gc,
1811 .sigchld_event = mount_sigchld_event,
1813 .reset_failed = mount_reset_failed,
1815 .bus_interface = "org.freedesktop.systemd1.Mount",
1816 .bus_vtable = bus_mount_vtable,
1817 .bus_set_property = bus_mount_set_property,
1818 .bus_commit_properties = bus_mount_commit_properties,
1820 .get_timeout = mount_get_timeout,
1822 .enumerate = mount_enumerate,
1823 .shutdown = mount_shutdown,
1825 .status_message_formats = {
1826 .starting_stopping = {
1827 [0] = "Mounting %s...",
1828 [1] = "Unmounting %s...",
1830 .finished_start_job = {
1831 [JOB_DONE] = "Mounted %s.",
1832 [JOB_FAILED] = "Failed to mount %s.",
1833 [JOB_DEPENDENCY] = "Dependency failed for %s.",
1834 [JOB_TIMEOUT] = "Timed out mounting %s.",
1836 .finished_stop_job = {
1837 [JOB_DONE] = "Unmounted %s.",
1838 [JOB_FAILED] = "Failed unmounting %s.",
1839 [JOB_TIMEOUT] = "Timed out unmounting %s.",