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 char* mount_test_option(const char *haystack, const char *needle) {
66 struct mntent me = { .mnt_opts = (char*) haystack };
70 /* Like glibc's hasmntopt(), but works on a string, not a
76 return hasmntopt(&me, needle);
79 static bool mount_is_network(MountParameters *p) {
82 if (mount_test_option(p->options, "_netdev"))
85 if (p->fstype && fstype_is_network(p->fstype))
91 static bool mount_is_bind(MountParameters *p) {
94 if (mount_test_option(p->options, "bind"))
97 if (p->fstype && streq(p->fstype, "bind"))
100 if (mount_test_option(p->options, "rbind"))
103 if (p->fstype && streq(p->fstype, "rbind"))
109 static bool mount_is_auto(MountParameters *p) {
112 return !mount_test_option(p->options, "noauto");
115 static bool needs_quota(MountParameters *p) {
118 if (mount_is_network(p))
121 if (mount_is_bind(p))
124 return mount_test_option(p->options, "usrquota") ||
125 mount_test_option(p->options, "grpquota") ||
126 mount_test_option(p->options, "quota") ||
127 mount_test_option(p->options, "usrjquota") ||
128 mount_test_option(p->options, "grpjquota");
131 static void mount_init(Unit *u) {
135 assert(u->load_state == UNIT_STUB);
137 m->timeout_usec = u->manager->default_timeout_start_usec;
138 m->directory_mode = 0755;
140 exec_context_init(&m->exec_context);
141 kill_context_init(&m->kill_context);
142 cgroup_context_init(&m->cgroup_context);
144 unit_cgroup_context_init_defaults(u, &m->cgroup_context);
146 if (unit_has_name(u, "-.mount")) {
147 /* Don't allow start/stop for root directory */
148 u->refuse_manual_start = true;
149 u->refuse_manual_stop = true;
151 /* The stdio/kmsg bridge socket is on /, in order to avoid a
152 * dep loop, don't use kmsg logging for -.mount */
153 m->exec_context.std_output = u->manager->default_std_output;
154 m->exec_context.std_error = u->manager->default_std_error;
157 /* We need to make sure that /bin/mount is always called in
158 * the same process group as us, so that the autofs kernel
159 * side doesn't send us another mount request while we are
160 * already trying to comply its last one. */
161 m->exec_context.same_pgrp = true;
163 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
165 u->ignore_on_isolate = true;
168 static int mount_arm_timer(Mount *m) {
173 if (m->timeout_usec <= 0) {
174 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
178 if (m->timer_event_source) {
179 r = sd_event_source_set_time(m->timer_event_source, now(CLOCK_MONOTONIC) + m->timeout_usec);
183 return sd_event_source_set_enabled(m->timer_event_source, SD_EVENT_ONESHOT);
186 return sd_event_add_monotonic(UNIT(m)->manager->event, &m->timer_event_source, now(CLOCK_MONOTONIC) + m->timeout_usec, 0, mount_dispatch_timer, m);
189 static void mount_unwatch_control_pid(Mount *m) {
192 if (m->control_pid <= 0)
195 unit_unwatch_pid(UNIT(m), m->control_pid);
199 static void mount_parameters_done(MountParameters *p) {
206 p->what = p->options = p->fstype = NULL;
209 static void mount_done(Unit *u) {
217 mount_parameters_done(&m->parameters_proc_self_mountinfo);
218 mount_parameters_done(&m->parameters_fragment);
220 cgroup_context_done(&m->cgroup_context);
221 exec_context_done(&m->exec_context);
222 m->exec_runtime = exec_runtime_unref(m->exec_runtime);
223 exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
224 m->control_command = NULL;
226 mount_unwatch_control_pid(m);
228 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
231 _pure_ static MountParameters* get_mount_parameters_fragment(Mount *m) {
234 if (m->from_fragment)
235 return &m->parameters_fragment;
240 _pure_ static MountParameters* get_mount_parameters(Mount *m) {
243 if (m->from_proc_self_mountinfo)
244 return &m->parameters_proc_self_mountinfo;
246 return get_mount_parameters_fragment(m);
249 static int mount_add_mount_links(Mount *m) {
250 _cleanup_free_ char *parent = NULL;
259 if (!path_equal(m->where, "/")) {
260 /* Adds in links to other mount points that might lie further
261 * up in the hierarchy */
262 r = path_get_parent(m->where, &parent);
266 r = unit_require_mounts_for(UNIT(m), parent);
271 /* Adds in links to other mount points that might be needed
272 * for the source path (if this is a bind mount) to be
274 pm = get_mount_parameters_fragment(m);
275 if (pm && pm->what &&
276 path_is_absolute(pm->what) &&
277 !mount_is_network(pm)) {
279 r = unit_require_mounts_for(UNIT(m), pm->what);
284 /* Adds in links to other units that use this path or paths
285 * further down in the hierarchy */
286 s = manager_get_units_requiring_mounts_for(UNIT(m)->manager, m->where);
287 SET_FOREACH(other, s, i) {
289 if (other->load_state != UNIT_LOADED)
292 if (other == UNIT(m))
295 r = unit_add_dependency(other, UNIT_AFTER, UNIT(m), true);
299 if (UNIT(m)->fragment_path) {
300 /* If we have fragment configuration, then make this dependency required */
301 r = unit_add_dependency(other, UNIT_REQUIRES, UNIT(m), true);
310 static int mount_add_device_links(Mount *m) {
312 bool device_wants_mount = false;
317 p = get_mount_parameters_fragment(m);
324 if (mount_is_bind(p))
327 if (!is_device_path(p->what))
330 if (path_equal(m->where, "/"))
333 if (mount_is_auto(p) && UNIT(m)->manager->running_as == SYSTEMD_SYSTEM)
334 device_wants_mount = true;
336 r = unit_add_node_link(UNIT(m), p->what, device_wants_mount);
343 static int mount_add_quota_links(Mount *m) {
349 if (UNIT(m)->manager->running_as != SYSTEMD_SYSTEM)
352 p = get_mount_parameters_fragment(m);
359 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTACHECK_SERVICE, NULL, true);
363 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTAON_SERVICE, NULL, true);
370 static bool should_umount(Mount *m) {
373 if (path_equal(m->where, "/") ||
374 path_equal(m->where, "/usr"))
377 p = get_mount_parameters(m);
378 if (p && mount_test_option(p->options, "x-initrd.mount") &&
385 static int mount_add_default_dependencies(Mount *m) {
386 const char *after, *after2, *online;
392 if (UNIT(m)->manager->running_as != SYSTEMD_SYSTEM)
395 p = get_mount_parameters(m);
400 if (path_equal(m->where, "/"))
403 if (mount_is_network(p)) {
404 after = SPECIAL_REMOTE_FS_PRE_TARGET;
405 after2 = SPECIAL_NETWORK_TARGET;
406 online = SPECIAL_NETWORK_ONLINE_TARGET;
408 after = SPECIAL_LOCAL_FS_PRE_TARGET;
413 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after, NULL, true);
418 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after2, NULL, true);
424 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_WANTS, UNIT_AFTER, online, NULL, true);
429 if (should_umount(m)) {
430 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
438 static int mount_fix_timeouts(Mount *m) {
440 const char *timeout = NULL;
449 p = get_mount_parameters_fragment(m);
453 /* Allow configuration how long we wait for a device that
454 * backs a mount point to show up. This is useful to support
455 * endless device timeouts for devices that show up only after
456 * user input, like crypto devices. */
458 if ((timeout = mount_test_option(p->options, "comment=systemd.device-timeout")))
460 else if ((timeout = mount_test_option(p->options, "x-systemd.device-timeout")))
465 t = strndup(timeout, strcspn(timeout, ",;" WHITESPACE));
469 r = parse_sec(t, &u);
473 log_warning_unit(UNIT(m)->id,
474 "Failed to parse timeout for %s, ignoring: %s",
479 SET_FOREACH(other, UNIT(m)->dependencies[UNIT_AFTER], i) {
480 if (other->type != UNIT_DEVICE)
483 other->job_timeout = u;
489 static int mount_verify(Mount *m) {
490 _cleanup_free_ char *e = NULL;
495 if (UNIT(m)->load_state != UNIT_LOADED)
498 if (!m->from_fragment && !m->from_proc_self_mountinfo)
501 e = unit_name_from_path(m->where, ".mount");
505 b = unit_has_name(UNIT(m), e);
507 log_error_unit(UNIT(m)->id, "%s's Where= setting doesn't match unit name. Refusing.", UNIT(m)->id);
511 if (mount_point_is_api(m->where) || mount_point_ignore(m->where)) {
512 log_error_unit(UNIT(m)->id, "Cannot create mount unit for API file system %s. Refusing.", m->where);
516 if (UNIT(m)->fragment_path && !m->parameters_fragment.what) {
517 log_error_unit(UNIT(m)->id, "%s's What setting is missing. Refusing.", UNIT(m)->id);
521 if (m->exec_context.pam_name && m->kill_context.kill_mode != KILL_CONTROL_GROUP) {
522 log_error_unit(UNIT(m)->id, "%s has PAM enabled. Kill mode must be set to control-group'. Refusing.",UNIT(m)->id);
529 static int mount_add_extras(Mount *m) {
535 if (u->fragment_path)
536 m->from_fragment = true;
539 m->where = unit_name_to_path(u->id);
544 path_kill_slashes(m->where);
546 r = unit_add_exec_dependencies(u, &m->exec_context);
550 if (!u->description) {
551 r = unit_set_description(u, m->where);
556 r = mount_add_device_links(m);
560 r = mount_add_mount_links(m);
564 r = mount_add_quota_links(m);
568 if (u->default_dependencies) {
569 r = mount_add_default_dependencies(m);
574 r = unit_add_default_slice(u);
578 r = mount_fix_timeouts(m);
582 r = unit_exec_context_patch_defaults(u, &m->exec_context);
589 static int mount_load(Unit *u) {
594 assert(u->load_state == UNIT_STUB);
596 if (m->from_proc_self_mountinfo)
597 r = unit_load_fragment_and_dropin_optional(u);
599 r = unit_load_fragment_and_dropin(u);
604 /* This is a new unit? Then let's add in some extras */
605 if (u->load_state == UNIT_LOADED) {
606 r = mount_add_extras(m);
611 return mount_verify(m);
614 static int mount_notify_automount(Mount *m, int status) {
621 SET_FOREACH(p, UNIT(m)->dependencies[UNIT_TRIGGERED_BY], i)
622 if (p->type == UNIT_AUTOMOUNT) {
623 r = automount_send_ready(AUTOMOUNT(p), status);
631 static void mount_set_state(Mount *m, MountState state) {
632 MountState old_state;
635 old_state = m->state;
638 if (state != MOUNT_MOUNTING &&
639 state != MOUNT_MOUNTING_DONE &&
640 state != MOUNT_REMOUNTING &&
641 state != MOUNT_UNMOUNTING &&
642 state != MOUNT_MOUNTING_SIGTERM &&
643 state != MOUNT_MOUNTING_SIGKILL &&
644 state != MOUNT_UNMOUNTING_SIGTERM &&
645 state != MOUNT_UNMOUNTING_SIGKILL &&
646 state != MOUNT_REMOUNTING_SIGTERM &&
647 state != MOUNT_REMOUNTING_SIGKILL) {
648 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
649 mount_unwatch_control_pid(m);
650 m->control_command = NULL;
651 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
654 if (state == MOUNT_MOUNTED ||
655 state == MOUNT_REMOUNTING)
656 mount_notify_automount(m, 0);
657 else if (state == MOUNT_DEAD ||
658 state == MOUNT_UNMOUNTING ||
659 state == MOUNT_MOUNTING_SIGTERM ||
660 state == MOUNT_MOUNTING_SIGKILL ||
661 state == MOUNT_REMOUNTING_SIGTERM ||
662 state == MOUNT_REMOUNTING_SIGKILL ||
663 state == MOUNT_UNMOUNTING_SIGTERM ||
664 state == MOUNT_UNMOUNTING_SIGKILL ||
665 state == MOUNT_FAILED) {
666 if (state != old_state)
667 mount_notify_automount(m, -ENODEV);
670 if (state != old_state)
671 log_debug_unit(UNIT(m)->id,
672 "%s changed %s -> %s",
674 mount_state_to_string(old_state),
675 mount_state_to_string(state));
677 unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state], m->reload_result == MOUNT_SUCCESS);
678 m->reload_result = MOUNT_SUCCESS;
681 static int mount_coldplug(Unit *u) {
683 MountState new_state = MOUNT_DEAD;
687 assert(m->state == MOUNT_DEAD);
689 if (m->deserialized_state != m->state)
690 new_state = m->deserialized_state;
691 else if (m->from_proc_self_mountinfo)
692 new_state = MOUNT_MOUNTED;
694 if (new_state == m->state)
697 if (new_state == MOUNT_MOUNTING ||
698 new_state == MOUNT_MOUNTING_DONE ||
699 new_state == MOUNT_REMOUNTING ||
700 new_state == MOUNT_UNMOUNTING ||
701 new_state == MOUNT_MOUNTING_SIGTERM ||
702 new_state == MOUNT_MOUNTING_SIGKILL ||
703 new_state == MOUNT_UNMOUNTING_SIGTERM ||
704 new_state == MOUNT_UNMOUNTING_SIGKILL ||
705 new_state == MOUNT_REMOUNTING_SIGTERM ||
706 new_state == MOUNT_REMOUNTING_SIGKILL) {
708 if (m->control_pid <= 0)
711 r = unit_watch_pid(UNIT(m), m->control_pid);
715 r = mount_arm_timer(m);
720 mount_set_state(m, new_state);
724 static void mount_dump(Unit *u, FILE *f, const char *prefix) {
731 p = get_mount_parameters(m);
734 "%sMount State: %s\n"
738 "%sFile System Type: %s\n"
740 "%sFrom /proc/self/mountinfo: %s\n"
741 "%sFrom fragment: %s\n"
742 "%sDirectoryMode: %04o\n",
743 prefix, mount_state_to_string(m->state),
744 prefix, mount_result_to_string(m->result),
746 prefix, p ? strna(p->what) : "n/a",
747 prefix, p ? strna(p->fstype) : "n/a",
748 prefix, p ? strna(p->options) : "n/a",
749 prefix, yes_no(m->from_proc_self_mountinfo),
750 prefix, yes_no(m->from_fragment),
751 prefix, m->directory_mode);
753 if (m->control_pid > 0)
755 "%sControl PID: "PID_FMT"\n",
756 prefix, m->control_pid);
758 exec_context_dump(&m->exec_context, f, prefix);
759 kill_context_dump(&m->kill_context, f, prefix);
762 static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
770 unit_realize_cgroup(UNIT(m));
772 r = unit_setup_exec_runtime(UNIT(m));
776 r = mount_arm_timer(m);
784 UNIT(m)->manager->environment,
788 UNIT(m)->manager->confirm_spawn,
789 UNIT(m)->manager->cgroup_supported,
790 UNIT(m)->cgroup_path,
799 r = unit_watch_pid(UNIT(m), pid);
801 /* FIXME: we need to do something here */
809 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
814 static void mount_enter_dead(Mount *m, MountResult f) {
817 if (f != MOUNT_SUCCESS)
820 exec_runtime_destroy(m->exec_runtime);
821 m->exec_runtime = exec_runtime_unref(m->exec_runtime);
823 mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD);
826 static void mount_enter_mounted(Mount *m, MountResult f) {
829 if (f != MOUNT_SUCCESS)
832 mount_set_state(m, MOUNT_MOUNTED);
835 static void mount_enter_signal(Mount *m, MountState state, MountResult f) {
840 if (f != MOUNT_SUCCESS)
843 r = unit_kill_context(
846 state != MOUNT_MOUNTING_SIGTERM && state != MOUNT_UNMOUNTING_SIGTERM && state != MOUNT_REMOUNTING_SIGTERM,
854 r = mount_arm_timer(m);
858 mount_set_state(m, state);
859 } else if (state == MOUNT_REMOUNTING_SIGTERM)
860 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS);
861 else if (state == MOUNT_REMOUNTING_SIGKILL)
862 mount_enter_mounted(m, MOUNT_SUCCESS);
863 else if (state == MOUNT_MOUNTING_SIGTERM)
864 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, MOUNT_SUCCESS);
865 else if (state == MOUNT_UNMOUNTING_SIGTERM)
866 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_SUCCESS);
868 mount_enter_dead(m, MOUNT_SUCCESS);
873 log_warning_unit(UNIT(m)->id,
874 "%s failed to kill processes: %s", UNIT(m)->id, strerror(-r));
876 if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
877 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
879 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
882 void warn_if_dir_nonempty(const char *unit, const char* where) {
886 if (dir_is_empty(where) > 0)
889 log_struct_unit(LOG_NOTICE,
891 "MESSAGE=%s: Directory %s to mount over is not empty, mounting anyway.",
894 MESSAGE_ID(SD_MESSAGE_OVERMOUNTING),
898 static void mount_enter_unmounting(Mount *m) {
903 m->control_command_id = MOUNT_EXEC_UNMOUNT;
904 m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
906 if ((r = exec_command_set(
913 mount_unwatch_control_pid(m);
915 if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
918 mount_set_state(m, MOUNT_UNMOUNTING);
923 log_warning_unit(UNIT(m)->id,
924 "%s failed to run 'umount' task: %s",
925 UNIT(m)->id, strerror(-r));
926 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
929 static void mount_enter_mounting(Mount *m) {
935 m->control_command_id = MOUNT_EXEC_MOUNT;
936 m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
938 mkdir_p_label(m->where, m->directory_mode);
940 warn_if_dir_nonempty(m->meta.id, m->where);
942 /* Create the source directory for bind-mounts if needed */
943 p = get_mount_parameters_fragment(m);
944 if (p && mount_is_bind(p))
945 mkdir_p_label(p->what, m->directory_mode);
947 if (m->from_fragment)
948 r = exec_command_set(
951 m->parameters_fragment.what,
953 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
954 m->parameters_fragment.options ? "-o" : NULL, m->parameters_fragment.options,
962 mount_unwatch_control_pid(m);
964 r = mount_spawn(m, m->control_command, &m->control_pid);
968 mount_set_state(m, MOUNT_MOUNTING);
973 log_warning_unit(UNIT(m)->id,
974 "%s failed to run 'mount' task: %s",
975 UNIT(m)->id, strerror(-r));
976 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
979 static void mount_enter_remounting(Mount *m) {
984 m->control_command_id = MOUNT_EXEC_REMOUNT;
985 m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
987 if (m->from_fragment) {
990 if (m->parameters_fragment.options)
991 o = strappenda("remount,", m->parameters_fragment.options);
995 r = exec_command_set(
998 m->parameters_fragment.what,
1000 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
1009 mount_unwatch_control_pid(m);
1011 r = mount_spawn(m, m->control_command, &m->control_pid);
1015 mount_set_state(m, MOUNT_REMOUNTING);
1020 log_warning_unit(UNIT(m)->id,
1021 "%s failed to run 'remount' task: %s",
1022 UNIT(m)->id, strerror(-r));
1023 m->reload_result = MOUNT_FAILURE_RESOURCES;
1024 mount_enter_mounted(m, MOUNT_SUCCESS);
1027 static int mount_start(Unit *u) {
1028 Mount *m = MOUNT(u);
1032 /* We cannot fulfill this request right now, try again later
1034 if (m->state == MOUNT_UNMOUNTING ||
1035 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1036 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1037 m->state == MOUNT_MOUNTING_SIGTERM ||
1038 m->state == MOUNT_MOUNTING_SIGKILL)
1041 /* Already on it! */
1042 if (m->state == MOUNT_MOUNTING)
1045 assert(m->state == MOUNT_DEAD || m->state == MOUNT_FAILED);
1047 m->result = MOUNT_SUCCESS;
1048 m->reload_result = MOUNT_SUCCESS;
1050 mount_enter_mounting(m);
1054 static int mount_stop(Unit *u) {
1055 Mount *m = MOUNT(u);
1060 if (m->state == MOUNT_UNMOUNTING ||
1061 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1062 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1063 m->state == MOUNT_MOUNTING_SIGTERM ||
1064 m->state == MOUNT_MOUNTING_SIGKILL)
1067 assert(m->state == MOUNT_MOUNTING ||
1068 m->state == MOUNT_MOUNTING_DONE ||
1069 m->state == MOUNT_MOUNTED ||
1070 m->state == MOUNT_REMOUNTING ||
1071 m->state == MOUNT_REMOUNTING_SIGTERM ||
1072 m->state == MOUNT_REMOUNTING_SIGKILL);
1074 mount_enter_unmounting(m);
1078 static int mount_reload(Unit *u) {
1079 Mount *m = MOUNT(u);
1083 if (m->state == MOUNT_MOUNTING_DONE)
1086 assert(m->state == MOUNT_MOUNTED);
1088 mount_enter_remounting(m);
1092 static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
1093 Mount *m = MOUNT(u);
1099 unit_serialize_item(u, f, "state", mount_state_to_string(m->state));
1100 unit_serialize_item(u, f, "result", mount_result_to_string(m->result));
1101 unit_serialize_item(u, f, "reload-result", mount_result_to_string(m->reload_result));
1103 if (m->control_pid > 0)
1104 unit_serialize_item_format(u, f, "control-pid", PID_FMT, m->control_pid);
1106 if (m->control_command_id >= 0)
1107 unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(m->control_command_id));
1112 static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1113 Mount *m = MOUNT(u);
1120 if (streq(key, "state")) {
1123 if ((state = mount_state_from_string(value)) < 0)
1124 log_debug_unit(u->id, "Failed to parse state value %s", value);
1126 m->deserialized_state = state;
1127 } else if (streq(key, "result")) {
1130 f = mount_result_from_string(value);
1132 log_debug_unit(UNIT(m)->id,
1133 "Failed to parse result value %s", value);
1134 else if (f != MOUNT_SUCCESS)
1137 } else if (streq(key, "reload-result")) {
1140 f = mount_result_from_string(value);
1142 log_debug_unit(UNIT(m)->id,
1143 "Failed to parse reload result value %s", value);
1144 else if (f != MOUNT_SUCCESS)
1145 m->reload_result = f;
1147 } else if (streq(key, "control-pid")) {
1150 if (parse_pid(value, &pid) < 0)
1151 log_debug_unit(UNIT(m)->id,
1152 "Failed to parse control-pid value %s", value);
1154 m->control_pid = pid;
1155 } else if (streq(key, "control-command")) {
1156 MountExecCommand id;
1158 if ((id = mount_exec_command_from_string(value)) < 0)
1159 log_debug_unit(UNIT(m)->id,
1160 "Failed to parse exec-command value %s", value);
1162 m->control_command_id = id;
1163 m->control_command = m->exec_command + id;
1166 log_debug_unit(UNIT(m)->id,
1167 "Unknown serialization key '%s'", key);
1172 _pure_ static UnitActiveState mount_active_state(Unit *u) {
1175 return state_translation_table[MOUNT(u)->state];
1178 _pure_ static const char *mount_sub_state_to_string(Unit *u) {
1181 return mount_state_to_string(MOUNT(u)->state);
1184 _pure_ static bool mount_check_gc(Unit *u) {
1185 Mount *m = MOUNT(u);
1189 return m->from_proc_self_mountinfo;
1192 static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1193 Mount *m = MOUNT(u);
1199 if (pid != m->control_pid)
1204 if (is_clean_exit(code, status, NULL))
1206 else if (code == CLD_EXITED)
1207 f = MOUNT_FAILURE_EXIT_CODE;
1208 else if (code == CLD_KILLED)
1209 f = MOUNT_FAILURE_SIGNAL;
1210 else if (code == CLD_DUMPED)
1211 f = MOUNT_FAILURE_CORE_DUMP;
1213 assert_not_reached("Unknown code");
1215 if (f != MOUNT_SUCCESS)
1218 if (m->control_command) {
1219 exec_status_exit(&m->control_command->exec_status, &m->exec_context, pid, code, status);
1221 m->control_command = NULL;
1222 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
1225 log_full_unit(f == MOUNT_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
1226 "%s mount process exited, code=%s status=%i",
1227 u->id, sigchld_code_to_string(code), status);
1229 /* Note that mount(8) returning and the kernel sending us a
1230 * mount table change event might happen out-of-order. If an
1231 * operation succeed we assume the kernel will follow soon too
1232 * and already change into the resulting state. If it fails
1233 * we check if the kernel still knows about the mount. and
1234 * change state accordingly. */
1238 case MOUNT_MOUNTING:
1239 case MOUNT_MOUNTING_DONE:
1240 case MOUNT_MOUNTING_SIGKILL:
1241 case MOUNT_MOUNTING_SIGTERM:
1243 if (f == MOUNT_SUCCESS)
1244 mount_enter_mounted(m, f);
1245 else if (m->from_proc_self_mountinfo)
1246 mount_enter_mounted(m, f);
1248 mount_enter_dead(m, f);
1251 case MOUNT_REMOUNTING:
1252 case MOUNT_REMOUNTING_SIGKILL:
1253 case MOUNT_REMOUNTING_SIGTERM:
1255 m->reload_result = f;
1256 if (m->from_proc_self_mountinfo)
1257 mount_enter_mounted(m, MOUNT_SUCCESS);
1259 mount_enter_dead(m, MOUNT_SUCCESS);
1263 case MOUNT_UNMOUNTING:
1264 case MOUNT_UNMOUNTING_SIGKILL:
1265 case MOUNT_UNMOUNTING_SIGTERM:
1267 if (f == MOUNT_SUCCESS)
1268 mount_enter_dead(m, f);
1269 else if (m->from_proc_self_mountinfo)
1270 mount_enter_mounted(m, f);
1272 mount_enter_dead(m, f);
1276 assert_not_reached("Uh, control process died at wrong time.");
1279 /* Notify clients about changed exit status */
1280 unit_add_to_dbus_queue(u);
1283 static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
1284 Mount *m = MOUNT(userdata);
1287 assert(m->timer_event_source == source);
1291 case MOUNT_MOUNTING:
1292 case MOUNT_MOUNTING_DONE:
1293 log_warning_unit(UNIT(m)->id,
1294 "%s mounting timed out. Stopping.", UNIT(m)->id);
1295 mount_enter_signal(m, MOUNT_MOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1298 case MOUNT_REMOUNTING:
1299 log_warning_unit(UNIT(m)->id,
1300 "%s remounting timed out. Stopping.", UNIT(m)->id);
1301 m->reload_result = MOUNT_FAILURE_TIMEOUT;
1302 mount_enter_mounted(m, MOUNT_SUCCESS);
1305 case MOUNT_UNMOUNTING:
1306 log_warning_unit(UNIT(m)->id,
1307 "%s unmounting timed out. Stopping.", UNIT(m)->id);
1308 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1311 case MOUNT_MOUNTING_SIGTERM:
1312 if (m->kill_context.send_sigkill) {
1313 log_warning_unit(UNIT(m)->id,
1314 "%s mounting timed out. Killing.", UNIT(m)->id);
1315 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1317 log_warning_unit(UNIT(m)->id,
1318 "%s mounting timed out. Skipping SIGKILL. Ignoring.",
1321 if (m->from_proc_self_mountinfo)
1322 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1324 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1328 case MOUNT_REMOUNTING_SIGTERM:
1329 if (m->kill_context.send_sigkill) {
1330 log_warning_unit(UNIT(m)->id,
1331 "%s remounting timed out. Killing.", UNIT(m)->id);
1332 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1334 log_warning_unit(UNIT(m)->id,
1335 "%s remounting timed out. Skipping SIGKILL. Ignoring.",
1338 if (m->from_proc_self_mountinfo)
1339 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1341 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1345 case MOUNT_UNMOUNTING_SIGTERM:
1346 if (m->kill_context.send_sigkill) {
1347 log_warning_unit(UNIT(m)->id,
1348 "%s unmounting timed out. Killing.", UNIT(m)->id);
1349 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1351 log_warning_unit(UNIT(m)->id,
1352 "%s unmounting timed out. Skipping 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 case MOUNT_MOUNTING_SIGKILL:
1363 case MOUNT_REMOUNTING_SIGKILL:
1364 case MOUNT_UNMOUNTING_SIGKILL:
1365 log_warning_unit(UNIT(m)->id,
1366 "%s mount process still around after SIGKILL. Ignoring.",
1369 if (m->from_proc_self_mountinfo)
1370 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1372 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1376 assert_not_reached("Timeout at wrong time.");
1382 static int mount_add_one(
1386 const char *options,
1390 _cleanup_free_ char *e = NULL, *w = NULL, *o = NULL, *f = NULL;
1391 bool load_extras = false;
1393 bool delete, changed = false;
1403 /* Ignore API mount points. They should never be referenced in
1404 * dependencies ever. */
1405 if (mount_point_is_api(where) || mount_point_ignore(where))
1408 if (streq(fstype, "autofs"))
1411 /* probably some kind of swap, ignore */
1412 if (!is_path(where))
1415 e = unit_name_from_path(where, ".mount");
1419 u = manager_get_unit(m, e);
1423 u = unit_new(m, sizeof(Mount));
1427 r = unit_add_name(u, e);
1431 MOUNT(u)->where = strdup(where);
1432 if (!MOUNT(u)->where) {
1437 u->source_path = strdup("/proc/self/mountinfo");
1438 if (!u->source_path) {
1444 if (m->running_as == SYSTEMD_SYSTEM) {
1447 target = fstype_is_network(fstype) ? SPECIAL_REMOTE_FS_TARGET : SPECIAL_LOCAL_FS_TARGET;
1449 r = unit_add_dependency_by_name(u, UNIT_BEFORE, target, NULL, true);
1453 if (should_umount(MOUNT(u))) {
1454 r = unit_add_dependency_by_name(u, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
1460 unit_add_to_load_queue(u);
1465 if (!MOUNT(u)->where) {
1466 MOUNT(u)->where = strdup(where);
1467 if (!MOUNT(u)->where) {
1473 if (u->load_state == UNIT_NOT_FOUND) {
1474 u->load_state = UNIT_LOADED;
1477 /* Load in the extras later on, after we
1478 * finished initialization of the unit */
1485 o = strdup(options);
1487 if (!w || !o || !f) {
1492 p = &MOUNT(u)->parameters_proc_self_mountinfo;
1494 changed = changed ||
1495 !streq_ptr(p->options, options) ||
1496 !streq_ptr(p->what, what) ||
1497 !streq_ptr(p->fstype, fstype);
1500 MOUNT(u)->is_mounted = true;
1501 MOUNT(u)->just_mounted = !MOUNT(u)->from_proc_self_mountinfo;
1502 MOUNT(u)->just_changed = changed;
1505 MOUNT(u)->from_proc_self_mountinfo = true;
1520 r = mount_add_extras(MOUNT(u));
1526 unit_add_to_dbus_queue(u);
1537 static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
1543 rewind(m->proc_self_mountinfo);
1546 _cleanup_free_ char *device = NULL, *path = NULL, *options = NULL, *options2 = NULL, *fstype = NULL, *d = NULL, *p = NULL, *o = NULL;
1549 k = fscanf(m->proc_self_mountinfo,
1550 "%*s " /* (1) mount id */
1551 "%*s " /* (2) parent id */
1552 "%*s " /* (3) major:minor */
1553 "%*s " /* (4) root */
1554 "%ms " /* (5) mount point */
1555 "%ms" /* (6) mount options */
1556 "%*[^-]" /* (7) optional fields */
1557 "- " /* (8) separator */
1558 "%ms " /* (9) file system type */
1559 "%ms" /* (10) mount source */
1560 "%ms" /* (11) mount options 2 */
1561 "%*[^\n]", /* some rubbish at the end */
1572 log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
1576 o = strjoin(options, ",", options2, NULL);
1580 d = cunescape(device);
1581 p = cunescape(path);
1585 k = mount_add_one(m, d, p, o, fstype, set_flags);
1593 static void mount_shutdown(Manager *m) {
1596 m->mount_event_source = sd_event_source_unref(m->mount_event_source);
1598 if (m->proc_self_mountinfo) {
1599 fclose(m->proc_self_mountinfo);
1600 m->proc_self_mountinfo = NULL;
1604 static int mount_get_timeout(Unit *u, uint64_t *timeout) {
1605 Mount *m = MOUNT(u);
1608 if (!m->timer_event_source)
1611 r = sd_event_source_get_time(m->timer_event_source, timeout);
1618 static int mount_enumerate(Manager *m) {
1622 if (!m->proc_self_mountinfo) {
1623 m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
1624 if (!m->proc_self_mountinfo)
1627 r = sd_event_add_io(m->event, &m->mount_event_source, fileno(m->proc_self_mountinfo), EPOLLPRI, mount_dispatch_io, m);
1631 /* Dispatch this before we dispatch SIGCHLD, so that
1632 * we always get the events from /proc/self/mountinfo
1633 * before the SIGCHLD of /bin/mount. */
1634 r = sd_event_source_set_priority(m->mount_event_source, -10);
1639 r = mount_load_proc_self_mountinfo(m, false);
1650 static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
1651 Manager *m = userdata;
1656 assert(revents & EPOLLPRI);
1658 /* The manager calls this for every fd event happening on the
1659 * /proc/self/mountinfo file, which informs us about mounting
1662 r = mount_load_proc_self_mountinfo(m, true);
1664 log_error("Failed to reread /proc/self/mountinfo: %s", strerror(-r));
1666 /* Reset flags, just in case, for later calls */
1667 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1668 Mount *mount = MOUNT(u);
1670 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1676 manager_dispatch_load_queue(m);
1678 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1679 Mount *mount = MOUNT(u);
1681 if (!mount->is_mounted) {
1683 mount->from_proc_self_mountinfo = false;
1685 switch (mount->state) {
1688 /* This has just been unmounted by
1689 * somebody else, follow the state
1691 mount_enter_dead(mount, MOUNT_SUCCESS);
1698 } else if (mount->just_mounted || mount->just_changed) {
1700 /* New or changed mount entry */
1702 switch (mount->state) {
1706 /* This has just been mounted by
1707 * somebody else, follow the state
1709 mount_enter_mounted(mount, MOUNT_SUCCESS);
1712 case MOUNT_MOUNTING:
1713 mount_set_state(mount, MOUNT_MOUNTING_DONE);
1717 /* Nothing really changed, but let's
1718 * issue an notification call
1719 * nonetheless, in case somebody is
1720 * waiting for this. (e.g. file system
1721 * ro/rw remounts.) */
1722 mount_set_state(mount, mount->state);
1727 /* Reset the flags for later calls */
1728 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1734 static void mount_reset_failed(Unit *u) {
1735 Mount *m = MOUNT(u);
1739 if (m->state == MOUNT_FAILED)
1740 mount_set_state(m, MOUNT_DEAD);
1742 m->result = MOUNT_SUCCESS;
1743 m->reload_result = MOUNT_SUCCESS;
1746 static int mount_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
1747 return unit_kill_common(u, who, signo, -1, MOUNT(u)->control_pid, error);
1750 static const char* const mount_state_table[_MOUNT_STATE_MAX] = {
1751 [MOUNT_DEAD] = "dead",
1752 [MOUNT_MOUNTING] = "mounting",
1753 [MOUNT_MOUNTING_DONE] = "mounting-done",
1754 [MOUNT_MOUNTED] = "mounted",
1755 [MOUNT_REMOUNTING] = "remounting",
1756 [MOUNT_UNMOUNTING] = "unmounting",
1757 [MOUNT_MOUNTING_SIGTERM] = "mounting-sigterm",
1758 [MOUNT_MOUNTING_SIGKILL] = "mounting-sigkill",
1759 [MOUNT_REMOUNTING_SIGTERM] = "remounting-sigterm",
1760 [MOUNT_REMOUNTING_SIGKILL] = "remounting-sigkill",
1761 [MOUNT_UNMOUNTING_SIGTERM] = "unmounting-sigterm",
1762 [MOUNT_UNMOUNTING_SIGKILL] = "unmounting-sigkill",
1763 [MOUNT_FAILED] = "failed"
1766 DEFINE_STRING_TABLE_LOOKUP(mount_state, MountState);
1768 static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
1769 [MOUNT_EXEC_MOUNT] = "ExecMount",
1770 [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
1771 [MOUNT_EXEC_REMOUNT] = "ExecRemount",
1774 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
1776 static const char* const mount_result_table[_MOUNT_RESULT_MAX] = {
1777 [MOUNT_SUCCESS] = "success",
1778 [MOUNT_FAILURE_RESOURCES] = "resources",
1779 [MOUNT_FAILURE_TIMEOUT] = "timeout",
1780 [MOUNT_FAILURE_EXIT_CODE] = "exit-code",
1781 [MOUNT_FAILURE_SIGNAL] = "signal",
1782 [MOUNT_FAILURE_CORE_DUMP] = "core-dump"
1785 DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult);
1787 const UnitVTable mount_vtable = {
1788 .object_size = sizeof(Mount),
1789 .exec_context_offset = offsetof(Mount, exec_context),
1790 .cgroup_context_offset = offsetof(Mount, cgroup_context),
1791 .kill_context_offset = offsetof(Mount, kill_context),
1792 .exec_runtime_offset = offsetof(Mount, exec_runtime),
1798 .private_section = "Mount",
1801 .no_instances = true,
1807 .coldplug = mount_coldplug,
1811 .start = mount_start,
1813 .reload = mount_reload,
1817 .serialize = mount_serialize,
1818 .deserialize_item = mount_deserialize_item,
1820 .active_state = mount_active_state,
1821 .sub_state_to_string = mount_sub_state_to_string,
1823 .check_gc = mount_check_gc,
1825 .sigchld_event = mount_sigchld_event,
1827 .reset_failed = mount_reset_failed,
1829 .bus_interface = "org.freedesktop.systemd1.Mount",
1830 .bus_vtable = bus_mount_vtable,
1831 .bus_set_property = bus_mount_set_property,
1832 .bus_commit_properties = bus_mount_commit_properties,
1834 .get_timeout = mount_get_timeout,
1836 .enumerate = mount_enumerate,
1837 .shutdown = mount_shutdown,
1839 .status_message_formats = {
1840 .starting_stopping = {
1841 [0] = "Mounting %s...",
1842 [1] = "Unmounting %s...",
1844 .finished_start_job = {
1845 [JOB_DONE] = "Mounted %s.",
1846 [JOB_FAILED] = "Failed to mount %s.",
1847 [JOB_DEPENDENCY] = "Dependency failed for %s.",
1848 [JOB_TIMEOUT] = "Timed out mounting %s.",
1850 .finished_stop_job = {
1851 [JOB_DONE] = "Unmounted %s.",
1852 [JOB_FAILED] = "Failed unmounting %s.",
1853 [JOB_TIMEOUT] = "Timed out unmounting %s.",