1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include <sys/epoll.h>
28 #include <sys/inotify.h>
33 #include "load-fragment.h"
34 #include "load-dropin.h"
36 #include "sd-messages.h"
39 #include "path-util.h"
40 #include "mount-setup.h"
41 #include "unit-name.h"
42 #include "dbus-mount.h"
44 #include "bus-errors.h"
45 #include "exit-status.h"
48 static const UnitActiveState state_translation_table[_MOUNT_STATE_MAX] = {
49 [MOUNT_DEAD] = UNIT_INACTIVE,
50 [MOUNT_MOUNTING] = UNIT_ACTIVATING,
51 [MOUNT_MOUNTING_DONE] = UNIT_ACTIVE,
52 [MOUNT_MOUNTED] = UNIT_ACTIVE,
53 [MOUNT_REMOUNTING] = UNIT_RELOADING,
54 [MOUNT_UNMOUNTING] = UNIT_DEACTIVATING,
55 [MOUNT_MOUNTING_SIGTERM] = UNIT_DEACTIVATING,
56 [MOUNT_MOUNTING_SIGKILL] = UNIT_DEACTIVATING,
57 [MOUNT_REMOUNTING_SIGTERM] = UNIT_RELOADING,
58 [MOUNT_REMOUNTING_SIGKILL] = UNIT_RELOADING,
59 [MOUNT_UNMOUNTING_SIGTERM] = UNIT_DEACTIVATING,
60 [MOUNT_UNMOUNTING_SIGKILL] = UNIT_DEACTIVATING,
61 [MOUNT_FAILED] = UNIT_FAILED
64 static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
65 static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
67 static bool mount_needs_network(const char *options, const char *fstype) {
68 if (mount_test_option(options, "_netdev"))
71 if (fstype && fstype_is_network(fstype))
77 static bool mount_is_network(MountParameters *p) {
80 return mount_needs_network(p->options, p->fstype);
83 static bool mount_is_bind(MountParameters *p) {
86 if (mount_test_option(p->options, "bind"))
89 if (p->fstype && streq(p->fstype, "bind"))
92 if (mount_test_option(p->options, "rbind"))
95 if (p->fstype && streq(p->fstype, "rbind"))
101 static bool mount_is_auto(MountParameters *p) {
104 return !mount_test_option(p->options, "noauto");
107 static bool needs_quota(MountParameters *p) {
110 if (mount_is_network(p))
113 if (mount_is_bind(p))
116 return mount_test_option(p->options, "usrquota") ||
117 mount_test_option(p->options, "grpquota") ||
118 mount_test_option(p->options, "quota") ||
119 mount_test_option(p->options, "usrjquota") ||
120 mount_test_option(p->options, "grpjquota");
123 static void mount_init(Unit *u) {
127 assert(u->load_state == UNIT_STUB);
129 m->timeout_usec = u->manager->default_timeout_start_usec;
130 m->directory_mode = 0755;
132 if (unit_has_name(u, "-.mount")) {
133 /* Don't allow start/stop for root directory */
134 u->refuse_manual_start = true;
135 u->refuse_manual_stop = true;
137 /* The stdio/kmsg bridge socket is on /, in order to avoid a
138 * dep loop, don't use kmsg logging for -.mount */
139 m->exec_context.std_output = u->manager->default_std_output;
140 m->exec_context.std_error = u->manager->default_std_error;
143 /* We need to make sure that /bin/mount is always called in
144 * the same process group as us, so that the autofs kernel
145 * side doesn't send us another mount request while we are
146 * already trying to comply its last one. */
147 m->exec_context.same_pgrp = true;
149 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
151 u->ignore_on_isolate = true;
154 static int mount_arm_timer(Mount *m) {
159 if (m->timeout_usec <= 0) {
160 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
164 if (m->timer_event_source) {
165 r = sd_event_source_set_time(m->timer_event_source, now(CLOCK_MONOTONIC) + m->timeout_usec);
169 return sd_event_source_set_enabled(m->timer_event_source, SD_EVENT_ONESHOT);
172 return sd_event_add_time(
173 UNIT(m)->manager->event,
174 &m->timer_event_source,
176 now(CLOCK_MONOTONIC) + m->timeout_usec, 0,
177 mount_dispatch_timer, m);
180 static void mount_unwatch_control_pid(Mount *m) {
183 if (m->control_pid <= 0)
186 unit_unwatch_pid(UNIT(m), m->control_pid);
190 static void mount_parameters_done(MountParameters *p) {
197 p->what = p->options = p->fstype = NULL;
200 static void mount_done(Unit *u) {
208 mount_parameters_done(&m->parameters_proc_self_mountinfo);
209 mount_parameters_done(&m->parameters_fragment);
211 m->exec_runtime = exec_runtime_unref(m->exec_runtime);
212 exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
213 m->control_command = NULL;
215 mount_unwatch_control_pid(m);
217 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
220 _pure_ static MountParameters* get_mount_parameters_fragment(Mount *m) {
223 if (m->from_fragment)
224 return &m->parameters_fragment;
229 _pure_ static MountParameters* get_mount_parameters(Mount *m) {
232 if (m->from_proc_self_mountinfo)
233 return &m->parameters_proc_self_mountinfo;
235 return get_mount_parameters_fragment(m);
238 static int mount_add_mount_links(Mount *m) {
239 _cleanup_free_ char *parent = NULL;
248 if (!path_equal(m->where, "/")) {
249 /* Adds in links to other mount points that might lie further
250 * up in the hierarchy */
251 r = path_get_parent(m->where, &parent);
255 r = unit_require_mounts_for(UNIT(m), parent);
260 /* Adds in links to other mount points that might be needed
261 * for the source path (if this is a bind mount) to be
263 pm = get_mount_parameters_fragment(m);
264 if (pm && pm->what &&
265 path_is_absolute(pm->what) &&
266 !mount_is_network(pm)) {
268 r = unit_require_mounts_for(UNIT(m), pm->what);
273 /* Adds in links to other units that use this path or paths
274 * further down in the hierarchy */
275 s = manager_get_units_requiring_mounts_for(UNIT(m)->manager, m->where);
276 SET_FOREACH(other, s, i) {
278 if (other->load_state != UNIT_LOADED)
281 if (other == UNIT(m))
284 r = unit_add_dependency(other, UNIT_AFTER, UNIT(m), true);
288 if (UNIT(m)->fragment_path) {
289 /* If we have fragment configuration, then make this dependency required */
290 r = unit_add_dependency(other, UNIT_REQUIRES, UNIT(m), true);
299 static int mount_add_device_links(Mount *m) {
301 bool device_wants_mount = false;
306 p = get_mount_parameters_fragment(m);
313 if (mount_is_bind(p))
316 if (!is_device_path(p->what))
319 if (path_equal(m->where, "/"))
322 if (mount_is_auto(p) && UNIT(m)->manager->running_as == SYSTEMD_SYSTEM)
323 device_wants_mount = true;
325 r = unit_add_node_link(UNIT(m), p->what, device_wants_mount);
332 static int mount_add_quota_links(Mount *m) {
338 if (UNIT(m)->manager->running_as != SYSTEMD_SYSTEM)
341 p = get_mount_parameters_fragment(m);
348 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTACHECK_SERVICE, NULL, true);
352 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTAON_SERVICE, NULL, true);
359 static bool should_umount(Mount *m) {
362 if (path_equal(m->where, "/") ||
363 path_equal(m->where, "/usr"))
366 p = get_mount_parameters(m);
367 if (p && mount_test_option(p->options, "x-initrd.mount") &&
374 static int mount_add_default_dependencies(Mount *m) {
375 const char *after, *after2, *online;
381 if (UNIT(m)->manager->running_as != SYSTEMD_SYSTEM)
384 p = get_mount_parameters(m);
389 if (path_equal(m->where, "/") ||
390 path_equal(m->where, "/usr"))
393 if (mount_is_network(p)) {
394 after = SPECIAL_REMOTE_FS_PRE_TARGET;
395 after2 = SPECIAL_NETWORK_TARGET;
396 online = SPECIAL_NETWORK_ONLINE_TARGET;
398 after = SPECIAL_LOCAL_FS_PRE_TARGET;
403 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after, NULL, true);
408 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after2, NULL, true);
414 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_WANTS, UNIT_AFTER, online, NULL, true);
419 if (should_umount(m)) {
420 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
428 static int mount_verify(Mount *m) {
429 _cleanup_free_ char *e = NULL;
434 if (UNIT(m)->load_state != UNIT_LOADED)
437 if (!m->from_fragment && !m->from_proc_self_mountinfo)
440 e = unit_name_from_path(m->where, ".mount");
444 b = unit_has_name(UNIT(m), e);
446 log_unit_error(UNIT(m)->id, "%s's Where= setting doesn't match unit name. Refusing.", UNIT(m)->id);
450 if (mount_point_is_api(m->where) || mount_point_ignore(m->where)) {
451 log_unit_error(UNIT(m)->id, "Cannot create mount unit for API file system %s. Refusing.", m->where);
455 if (UNIT(m)->fragment_path && !m->parameters_fragment.what) {
456 log_unit_error(UNIT(m)->id, "%s's What setting is missing. Refusing.", UNIT(m)->id);
460 if (m->exec_context.pam_name && m->kill_context.kill_mode != KILL_CONTROL_GROUP) {
461 log_unit_error(UNIT(m)->id, "%s has PAM enabled. Kill mode must be set to control-group'. Refusing.",UNIT(m)->id);
468 static int mount_add_extras(Mount *m) {
474 if (u->fragment_path)
475 m->from_fragment = true;
478 m->where = unit_name_to_path(u->id);
483 path_kill_slashes(m->where);
485 if (!u->description) {
486 r = unit_set_description(u, m->where);
491 r = mount_add_device_links(m);
495 r = mount_add_mount_links(m);
499 r = mount_add_quota_links(m);
503 r = unit_patch_contexts(u);
507 r = unit_add_exec_dependencies(u, &m->exec_context);
511 r = unit_add_default_slice(u, &m->cgroup_context);
515 if (u->default_dependencies) {
516 r = mount_add_default_dependencies(m);
524 static int mount_load(Unit *u) {
529 assert(u->load_state == UNIT_STUB);
531 if (m->from_proc_self_mountinfo)
532 r = unit_load_fragment_and_dropin_optional(u);
534 r = unit_load_fragment_and_dropin(u);
539 /* This is a new unit? Then let's add in some extras */
540 if (u->load_state == UNIT_LOADED) {
541 r = mount_add_extras(m);
546 return mount_verify(m);
549 static int mount_notify_automount(Mount *m, int status) {
556 SET_FOREACH(p, UNIT(m)->dependencies[UNIT_TRIGGERED_BY], i)
557 if (p->type == UNIT_AUTOMOUNT) {
558 r = automount_send_ready(AUTOMOUNT(p), status);
566 static void mount_set_state(Mount *m, MountState state) {
567 MountState old_state;
570 old_state = m->state;
573 if (state != MOUNT_MOUNTING &&
574 state != MOUNT_MOUNTING_DONE &&
575 state != MOUNT_REMOUNTING &&
576 state != MOUNT_UNMOUNTING &&
577 state != MOUNT_MOUNTING_SIGTERM &&
578 state != MOUNT_MOUNTING_SIGKILL &&
579 state != MOUNT_UNMOUNTING_SIGTERM &&
580 state != MOUNT_UNMOUNTING_SIGKILL &&
581 state != MOUNT_REMOUNTING_SIGTERM &&
582 state != MOUNT_REMOUNTING_SIGKILL) {
583 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
584 mount_unwatch_control_pid(m);
585 m->control_command = NULL;
586 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
589 if (state == MOUNT_MOUNTED ||
590 state == MOUNT_REMOUNTING)
591 mount_notify_automount(m, 0);
592 else if (state == MOUNT_DEAD ||
593 state == MOUNT_UNMOUNTING ||
594 state == MOUNT_MOUNTING_SIGTERM ||
595 state == MOUNT_MOUNTING_SIGKILL ||
596 state == MOUNT_REMOUNTING_SIGTERM ||
597 state == MOUNT_REMOUNTING_SIGKILL ||
598 state == MOUNT_UNMOUNTING_SIGTERM ||
599 state == MOUNT_UNMOUNTING_SIGKILL ||
600 state == MOUNT_FAILED) {
601 if (state != old_state)
602 mount_notify_automount(m, -ENODEV);
605 if (state != old_state)
606 log_unit_debug(UNIT(m)->id,
607 "%s changed %s -> %s",
609 mount_state_to_string(old_state),
610 mount_state_to_string(state));
612 unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state], m->reload_result == MOUNT_SUCCESS);
613 m->reload_result = MOUNT_SUCCESS;
616 static int mount_coldplug(Unit *u) {
618 MountState new_state = MOUNT_DEAD;
622 assert(m->state == MOUNT_DEAD);
624 if (m->deserialized_state != m->state)
625 new_state = m->deserialized_state;
626 else if (m->from_proc_self_mountinfo)
627 new_state = MOUNT_MOUNTED;
629 if (new_state == m->state)
632 if (new_state == MOUNT_MOUNTING ||
633 new_state == MOUNT_MOUNTING_DONE ||
634 new_state == MOUNT_REMOUNTING ||
635 new_state == MOUNT_UNMOUNTING ||
636 new_state == MOUNT_MOUNTING_SIGTERM ||
637 new_state == MOUNT_MOUNTING_SIGKILL ||
638 new_state == MOUNT_UNMOUNTING_SIGTERM ||
639 new_state == MOUNT_UNMOUNTING_SIGKILL ||
640 new_state == MOUNT_REMOUNTING_SIGTERM ||
641 new_state == MOUNT_REMOUNTING_SIGKILL) {
643 if (m->control_pid <= 0)
646 r = unit_watch_pid(UNIT(m), m->control_pid);
650 r = mount_arm_timer(m);
655 mount_set_state(m, new_state);
659 static void mount_dump(Unit *u, FILE *f, const char *prefix) {
666 p = get_mount_parameters(m);
669 "%sMount State: %s\n"
673 "%sFile System Type: %s\n"
675 "%sFrom /proc/self/mountinfo: %s\n"
676 "%sFrom fragment: %s\n"
677 "%sDirectoryMode: %04o\n",
678 prefix, mount_state_to_string(m->state),
679 prefix, mount_result_to_string(m->result),
681 prefix, p ? strna(p->what) : "n/a",
682 prefix, p ? strna(p->fstype) : "n/a",
683 prefix, p ? strna(p->options) : "n/a",
684 prefix, yes_no(m->from_proc_self_mountinfo),
685 prefix, yes_no(m->from_fragment),
686 prefix, m->directory_mode);
688 if (m->control_pid > 0)
690 "%sControl PID: "PID_FMT"\n",
691 prefix, m->control_pid);
693 exec_context_dump(&m->exec_context, f, prefix);
694 kill_context_dump(&m->kill_context, f, prefix);
697 static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
700 ExecParameters exec_params = {
701 .apply_permissions = true,
702 .apply_chroot = true,
703 .apply_tty_stdin = true,
710 unit_realize_cgroup(UNIT(m));
712 r = unit_setup_exec_runtime(UNIT(m));
716 r = mount_arm_timer(m);
720 exec_params.environment = UNIT(m)->manager->environment;
721 exec_params.confirm_spawn = UNIT(m)->manager->confirm_spawn;
722 exec_params.cgroup_supported = UNIT(m)->manager->cgroup_supported;
723 exec_params.cgroup_path = UNIT(m)->cgroup_path;
724 exec_params.cgroup_delegate = m->cgroup_context.delegate;
725 exec_params.runtime_prefix = manager_get_runtime_prefix(UNIT(m)->manager);
726 exec_params.unit_id = UNIT(m)->id;
736 r = unit_watch_pid(UNIT(m), pid);
738 /* FIXME: we need to do something here */
746 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
751 static void mount_enter_dead(Mount *m, MountResult f) {
754 if (f != MOUNT_SUCCESS)
757 exec_runtime_destroy(m->exec_runtime);
758 m->exec_runtime = exec_runtime_unref(m->exec_runtime);
760 exec_context_destroy_runtime_directory(&m->exec_context, manager_get_runtime_prefix(UNIT(m)->manager));
762 mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD);
765 static void mount_enter_mounted(Mount *m, MountResult f) {
768 if (f != MOUNT_SUCCESS)
771 mount_set_state(m, MOUNT_MOUNTED);
774 static void mount_enter_signal(Mount *m, MountState state, MountResult f) {
779 if (f != MOUNT_SUCCESS)
782 r = unit_kill_context(
785 (state != MOUNT_MOUNTING_SIGTERM && state != MOUNT_UNMOUNTING_SIGTERM && state != MOUNT_REMOUNTING_SIGTERM) ?
786 KILL_KILL : KILL_TERMINATE,
794 r = mount_arm_timer(m);
798 mount_set_state(m, state);
799 } else if (state == MOUNT_REMOUNTING_SIGTERM)
800 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS);
801 else if (state == MOUNT_REMOUNTING_SIGKILL)
802 mount_enter_mounted(m, MOUNT_SUCCESS);
803 else if (state == MOUNT_MOUNTING_SIGTERM)
804 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, MOUNT_SUCCESS);
805 else if (state == MOUNT_UNMOUNTING_SIGTERM)
806 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_SUCCESS);
808 mount_enter_dead(m, MOUNT_SUCCESS);
813 log_unit_warning(UNIT(m)->id,
814 "%s failed to kill processes: %s", UNIT(m)->id, strerror(-r));
816 if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
817 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
819 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
822 void warn_if_dir_nonempty(const char *unit, const char* where) {
828 r = dir_is_empty(where);
832 log_unit_struct(unit,
834 LOG_MESSAGE_ID(SD_MESSAGE_OVERMOUNTING),
835 LOG_MESSAGE("%s: Directory %s to mount over is not empty, mounting anyway.",
840 log_unit_warning(unit,
841 "MESSAGE=Failed to check directory %s: %s",
842 where, strerror(-r));
845 static int fail_if_symlink(const char *unit, const char* where) {
848 if (is_symlink(where) > 0) {
849 log_unit_struct(unit,
851 LOG_MESSAGE_ID(SD_MESSAGE_OVERMOUNTING),
852 LOG_MESSAGE("%s: Mount on symlink %s not allowed.",
862 static void mount_enter_unmounting(Mount *m) {
867 m->control_command_id = MOUNT_EXEC_UNMOUNT;
868 m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
870 if ((r = exec_command_set(
878 mount_unwatch_control_pid(m);
880 if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
883 mount_set_state(m, MOUNT_UNMOUNTING);
888 log_unit_warning(UNIT(m)->id,
889 "%s failed to run 'umount' task: %s",
890 UNIT(m)->id, strerror(-r));
891 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
894 static void mount_enter_mounting(Mount *m) {
900 m->control_command_id = MOUNT_EXEC_MOUNT;
901 m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
903 mkdir_p_label(m->where, m->directory_mode);
905 warn_if_dir_nonempty(m->meta.id, m->where);
907 /* Create the source directory for bind-mounts if needed */
908 p = get_mount_parameters_fragment(m);
909 if (p && mount_is_bind(p))
910 mkdir_p_label(p->what, m->directory_mode);
912 r = fail_if_symlink(m->meta.id, m->where);
916 if (m->from_fragment)
917 r = exec_command_set(
920 m->sloppy_options ? "-ns" : "-n",
921 m->parameters_fragment.what,
923 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
924 m->parameters_fragment.options ? "-o" : NULL, m->parameters_fragment.options,
932 mount_unwatch_control_pid(m);
934 r = mount_spawn(m, m->control_command, &m->control_pid);
938 mount_set_state(m, MOUNT_MOUNTING);
943 log_unit_warning(UNIT(m)->id,
944 "%s failed to run 'mount' task: %s",
945 UNIT(m)->id, strerror(-r));
946 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
949 static void mount_enter_remounting(Mount *m) {
954 m->control_command_id = MOUNT_EXEC_REMOUNT;
955 m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
957 if (m->from_fragment) {
960 if (m->parameters_fragment.options)
961 o = strappenda("remount,", m->parameters_fragment.options);
965 r = exec_command_set(
968 m->sloppy_options ? "-ns" : "-n",
969 m->parameters_fragment.what,
971 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
980 mount_unwatch_control_pid(m);
982 r = mount_spawn(m, m->control_command, &m->control_pid);
986 mount_set_state(m, MOUNT_REMOUNTING);
991 log_unit_warning(UNIT(m)->id,
992 "%s failed to run 'remount' task: %s",
993 UNIT(m)->id, strerror(-r));
994 m->reload_result = MOUNT_FAILURE_RESOURCES;
995 mount_enter_mounted(m, MOUNT_SUCCESS);
998 static int mount_start(Unit *u) {
1003 /* We cannot fulfill this request right now, try again later
1005 if (m->state == MOUNT_UNMOUNTING ||
1006 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1007 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1008 m->state == MOUNT_MOUNTING_SIGTERM ||
1009 m->state == MOUNT_MOUNTING_SIGKILL)
1012 /* Already on it! */
1013 if (m->state == MOUNT_MOUNTING)
1016 assert(m->state == MOUNT_DEAD || m->state == MOUNT_FAILED);
1018 m->result = MOUNT_SUCCESS;
1019 m->reload_result = MOUNT_SUCCESS;
1021 mount_enter_mounting(m);
1025 static int mount_stop(Unit *u) {
1026 Mount *m = MOUNT(u);
1031 if (m->state == MOUNT_UNMOUNTING ||
1032 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1033 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1034 m->state == MOUNT_MOUNTING_SIGTERM ||
1035 m->state == MOUNT_MOUNTING_SIGKILL)
1038 assert(m->state == MOUNT_MOUNTING ||
1039 m->state == MOUNT_MOUNTING_DONE ||
1040 m->state == MOUNT_MOUNTED ||
1041 m->state == MOUNT_REMOUNTING ||
1042 m->state == MOUNT_REMOUNTING_SIGTERM ||
1043 m->state == MOUNT_REMOUNTING_SIGKILL);
1045 mount_enter_unmounting(m);
1049 static int mount_reload(Unit *u) {
1050 Mount *m = MOUNT(u);
1054 if (m->state == MOUNT_MOUNTING_DONE)
1057 assert(m->state == MOUNT_MOUNTED);
1059 mount_enter_remounting(m);
1063 static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
1064 Mount *m = MOUNT(u);
1070 unit_serialize_item(u, f, "state", mount_state_to_string(m->state));
1071 unit_serialize_item(u, f, "result", mount_result_to_string(m->result));
1072 unit_serialize_item(u, f, "reload-result", mount_result_to_string(m->reload_result));
1074 if (m->control_pid > 0)
1075 unit_serialize_item_format(u, f, "control-pid", PID_FMT, m->control_pid);
1077 if (m->control_command_id >= 0)
1078 unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(m->control_command_id));
1083 static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1084 Mount *m = MOUNT(u);
1091 if (streq(key, "state")) {
1094 if ((state = mount_state_from_string(value)) < 0)
1095 log_unit_debug(u->id, "Failed to parse state value %s", value);
1097 m->deserialized_state = state;
1098 } else if (streq(key, "result")) {
1101 f = mount_result_from_string(value);
1103 log_unit_debug(UNIT(m)->id,
1104 "Failed to parse result value %s", value);
1105 else if (f != MOUNT_SUCCESS)
1108 } else if (streq(key, "reload-result")) {
1111 f = mount_result_from_string(value);
1113 log_unit_debug(UNIT(m)->id,
1114 "Failed to parse reload result value %s", value);
1115 else if (f != MOUNT_SUCCESS)
1116 m->reload_result = f;
1118 } else if (streq(key, "control-pid")) {
1121 if (parse_pid(value, &pid) < 0)
1122 log_unit_debug(UNIT(m)->id,
1123 "Failed to parse control-pid value %s", value);
1125 m->control_pid = pid;
1126 } else if (streq(key, "control-command")) {
1127 MountExecCommand id;
1129 if ((id = mount_exec_command_from_string(value)) < 0)
1130 log_unit_debug(UNIT(m)->id,
1131 "Failed to parse exec-command value %s", value);
1133 m->control_command_id = id;
1134 m->control_command = m->exec_command + id;
1137 log_unit_debug(UNIT(m)->id,
1138 "Unknown serialization key '%s'", key);
1143 _pure_ static UnitActiveState mount_active_state(Unit *u) {
1146 return state_translation_table[MOUNT(u)->state];
1149 _pure_ static const char *mount_sub_state_to_string(Unit *u) {
1152 return mount_state_to_string(MOUNT(u)->state);
1155 _pure_ static bool mount_check_gc(Unit *u) {
1156 Mount *m = MOUNT(u);
1160 return m->from_proc_self_mountinfo;
1163 static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1164 Mount *m = MOUNT(u);
1170 if (pid != m->control_pid)
1175 if (is_clean_exit(code, status, NULL))
1177 else if (code == CLD_EXITED)
1178 f = MOUNT_FAILURE_EXIT_CODE;
1179 else if (code == CLD_KILLED)
1180 f = MOUNT_FAILURE_SIGNAL;
1181 else if (code == CLD_DUMPED)
1182 f = MOUNT_FAILURE_CORE_DUMP;
1184 assert_not_reached("Unknown code");
1186 if (f != MOUNT_SUCCESS)
1189 if (m->control_command) {
1190 exec_status_exit(&m->control_command->exec_status, &m->exec_context, pid, code, status);
1192 m->control_command = NULL;
1193 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
1196 log_unit_full(u->id,
1197 f == MOUNT_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
1198 "%s mount process exited, code=%s status=%i",
1199 u->id, sigchld_code_to_string(code), status);
1201 /* Note that mount(8) returning and the kernel sending us a
1202 * mount table change event might happen out-of-order. If an
1203 * operation succeed we assume the kernel will follow soon too
1204 * and already change into the resulting state. If it fails
1205 * we check if the kernel still knows about the mount. and
1206 * change state accordingly. */
1210 case MOUNT_MOUNTING:
1211 case MOUNT_MOUNTING_DONE:
1212 case MOUNT_MOUNTING_SIGKILL:
1213 case MOUNT_MOUNTING_SIGTERM:
1215 if (f == MOUNT_SUCCESS)
1216 mount_enter_mounted(m, f);
1217 else if (m->from_proc_self_mountinfo)
1218 mount_enter_mounted(m, f);
1220 mount_enter_dead(m, f);
1223 case MOUNT_REMOUNTING:
1224 case MOUNT_REMOUNTING_SIGKILL:
1225 case MOUNT_REMOUNTING_SIGTERM:
1227 m->reload_result = f;
1228 if (m->from_proc_self_mountinfo)
1229 mount_enter_mounted(m, MOUNT_SUCCESS);
1231 mount_enter_dead(m, MOUNT_SUCCESS);
1235 case MOUNT_UNMOUNTING:
1236 case MOUNT_UNMOUNTING_SIGKILL:
1237 case MOUNT_UNMOUNTING_SIGTERM:
1239 if (f == MOUNT_SUCCESS)
1240 mount_enter_dead(m, f);
1241 else if (m->from_proc_self_mountinfo)
1242 mount_enter_mounted(m, f);
1244 mount_enter_dead(m, f);
1248 assert_not_reached("Uh, control process died at wrong time.");
1251 /* Notify clients about changed exit status */
1252 unit_add_to_dbus_queue(u);
1255 static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
1256 Mount *m = MOUNT(userdata);
1259 assert(m->timer_event_source == source);
1263 case MOUNT_MOUNTING:
1264 case MOUNT_MOUNTING_DONE:
1265 log_unit_warning(UNIT(m)->id,
1266 "%s mounting timed out. Stopping.", UNIT(m)->id);
1267 mount_enter_signal(m, MOUNT_MOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1270 case MOUNT_REMOUNTING:
1271 log_unit_warning(UNIT(m)->id,
1272 "%s remounting timed out. Stopping.", UNIT(m)->id);
1273 m->reload_result = MOUNT_FAILURE_TIMEOUT;
1274 mount_enter_mounted(m, MOUNT_SUCCESS);
1277 case MOUNT_UNMOUNTING:
1278 log_unit_warning(UNIT(m)->id,
1279 "%s unmounting timed out. Stopping.", UNIT(m)->id);
1280 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1283 case MOUNT_MOUNTING_SIGTERM:
1284 if (m->kill_context.send_sigkill) {
1285 log_unit_warning(UNIT(m)->id,
1286 "%s mounting timed out. Killing.", UNIT(m)->id);
1287 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1289 log_unit_warning(UNIT(m)->id,
1290 "%s mounting timed out. Skipping SIGKILL. Ignoring.",
1293 if (m->from_proc_self_mountinfo)
1294 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1296 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1300 case MOUNT_REMOUNTING_SIGTERM:
1301 if (m->kill_context.send_sigkill) {
1302 log_unit_warning(UNIT(m)->id,
1303 "%s remounting timed out. Killing.", UNIT(m)->id);
1304 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1306 log_unit_warning(UNIT(m)->id,
1307 "%s remounting timed out. Skipping SIGKILL. Ignoring.",
1310 if (m->from_proc_self_mountinfo)
1311 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1313 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1317 case MOUNT_UNMOUNTING_SIGTERM:
1318 if (m->kill_context.send_sigkill) {
1319 log_unit_warning(UNIT(m)->id,
1320 "%s unmounting timed out. Killing.", UNIT(m)->id);
1321 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1323 log_unit_warning(UNIT(m)->id,
1324 "%s unmounting timed out. Skipping SIGKILL. Ignoring.",
1327 if (m->from_proc_self_mountinfo)
1328 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1330 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1334 case MOUNT_MOUNTING_SIGKILL:
1335 case MOUNT_REMOUNTING_SIGKILL:
1336 case MOUNT_UNMOUNTING_SIGKILL:
1337 log_unit_warning(UNIT(m)->id,
1338 "%s mount process still around after 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 assert_not_reached("Timeout at wrong time.");
1354 static int mount_add_one(
1358 const char *options,
1362 _cleanup_free_ char *e = NULL, *w = NULL, *o = NULL, *f = NULL;
1363 bool load_extras = false;
1365 bool delete, changed = false;
1375 /* Ignore API mount points. They should never be referenced in
1376 * dependencies ever. */
1377 if (mount_point_is_api(where) || mount_point_ignore(where))
1380 if (streq(fstype, "autofs"))
1383 /* probably some kind of swap, ignore */
1384 if (!is_path(where))
1387 e = unit_name_from_path(where, ".mount");
1391 u = manager_get_unit(m, e);
1395 u = unit_new(m, sizeof(Mount));
1399 r = unit_add_name(u, e);
1403 MOUNT(u)->where = strdup(where);
1404 if (!MOUNT(u)->where) {
1409 u->source_path = strdup("/proc/self/mountinfo");
1410 if (!u->source_path) {
1416 if (m->running_as == SYSTEMD_SYSTEM) {
1419 target = mount_needs_network(options, fstype) ? SPECIAL_REMOTE_FS_TARGET : SPECIAL_LOCAL_FS_TARGET;
1420 r = unit_add_dependency_by_name(u, UNIT_BEFORE, target, NULL, true);
1424 if (should_umount(MOUNT(u))) {
1425 r = unit_add_dependency_by_name(u, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
1431 unit_add_to_load_queue(u);
1436 if (!MOUNT(u)->where) {
1437 MOUNT(u)->where = strdup(where);
1438 if (!MOUNT(u)->where) {
1444 if (m->running_as == SYSTEMD_SYSTEM &&
1445 mount_needs_network(options, fstype)) {
1446 /* _netdev option may have shown up late, or on a
1447 * remount. Add remote-fs dependencies, even though
1448 * local-fs ones may already be there. */
1449 unit_add_dependency_by_name(u, UNIT_BEFORE, SPECIAL_REMOTE_FS_TARGET, NULL, true);
1453 if (u->load_state == UNIT_NOT_FOUND) {
1454 u->load_state = UNIT_LOADED;
1457 /* Load in the extras later on, after we
1458 * finished initialization of the unit */
1465 o = strdup(options);
1467 if (!w || !o || !f) {
1472 p = &MOUNT(u)->parameters_proc_self_mountinfo;
1474 changed = changed ||
1475 !streq_ptr(p->options, options) ||
1476 !streq_ptr(p->what, what) ||
1477 !streq_ptr(p->fstype, fstype);
1480 MOUNT(u)->is_mounted = true;
1481 MOUNT(u)->just_mounted = !MOUNT(u)->from_proc_self_mountinfo;
1482 MOUNT(u)->just_changed = changed;
1485 MOUNT(u)->from_proc_self_mountinfo = true;
1500 r = mount_add_extras(MOUNT(u));
1506 unit_add_to_dbus_queue(u);
1517 static inline void mnt_free_table_p(struct libmnt_table **tb) {
1518 mnt_free_table(*tb);
1521 static inline void mnt_free_iter_p(struct libmnt_iter **itr) {
1522 mnt_free_iter(*itr);
1525 static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
1526 _cleanup_(mnt_free_table_p) struct libmnt_table *tb = NULL;
1527 _cleanup_(mnt_free_iter_p) struct libmnt_iter *itr = NULL;
1528 struct libmnt_fs *fs;
1533 tb = mnt_new_table();
1534 itr = mnt_new_iter(MNT_ITER_FORWARD);
1538 mnt_table_parse_mtab(tb, NULL);
1542 while (mnt_table_next_fs(tb, itr, &fs) == 0) {
1543 const char *device, *path, *options, *fstype;
1544 _cleanup_free_ const char *d = NULL, *p = NULL;
1547 device = mnt_fs_get_source(fs);
1548 path = mnt_fs_get_target(fs);
1549 options = mnt_fs_get_options(fs);
1550 fstype = mnt_fs_get_fstype(fs);
1552 d = cunescape(device);
1553 p = cunescape(path);
1557 k = mount_add_one(m, d, p, options, fstype, set_flags);
1565 static void mount_shutdown(Manager *m) {
1568 m->mount_event_source = sd_event_source_unref(m->mount_event_source);
1569 m->mount_utab_event_source = sd_event_source_unref(m->mount_utab_event_source);
1571 if (m->proc_self_mountinfo) {
1572 fclose(m->proc_self_mountinfo);
1573 m->proc_self_mountinfo = NULL;
1575 m->utab_inotify_fd = safe_close(m->utab_inotify_fd);
1578 static int mount_get_timeout(Unit *u, uint64_t *timeout) {
1579 Mount *m = MOUNT(u);
1582 if (!m->timer_event_source)
1585 r = sd_event_source_get_time(m->timer_event_source, timeout);
1592 static int mount_enumerate(Manager *m) {
1598 if (!m->proc_self_mountinfo) {
1599 m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
1600 if (!m->proc_self_mountinfo)
1603 r = sd_event_add_io(m->event, &m->mount_event_source, fileno(m->proc_self_mountinfo), EPOLLPRI, mount_dispatch_io, m);
1607 /* Dispatch this before we dispatch SIGCHLD, so that
1608 * we always get the events from /proc/self/mountinfo
1609 * before the SIGCHLD of /bin/mount. */
1610 r = sd_event_source_set_priority(m->mount_event_source, -10);
1615 if (m->utab_inotify_fd < 0) {
1616 m->utab_inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
1617 if (m->utab_inotify_fd < 0)
1618 goto fail_with_errno;
1620 r = inotify_add_watch(m->utab_inotify_fd, "/run/mount", IN_MOVED_TO);
1622 goto fail_with_errno;
1624 r = sd_event_add_io(m->event, &m->mount_utab_event_source, m->utab_inotify_fd, EPOLLIN, mount_dispatch_io, m);
1628 r = sd_event_source_set_priority(m->mount_utab_event_source, -10);
1633 r = mount_load_proc_self_mountinfo(m, false);
1646 static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
1647 Manager *m = userdata;
1652 assert(revents & (EPOLLPRI | EPOLLIN));
1654 /* The manager calls this for every fd event happening on the
1655 * /proc/self/mountinfo file, which informs us about mounting
1657 * This may also be called for /run/mount events */
1659 if (fd == m->utab_inotify_fd) {
1660 char inotify_buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
1661 struct inotify_event *event;
1665 while ((r = read(fd, inotify_buffer, sizeof(inotify_buffer))) > 0) {
1666 for (p = inotify_buffer; p < inotify_buffer + r; ) {
1667 event = (struct inotify_event *) p;
1668 /* only care about changes to utab, but we have
1669 * to monitor the directory to reliably get
1670 * notifications about when utab is replaced
1671 * using rename(2) */
1672 if (strcmp(event->name, "utab") == 0)
1674 p += sizeof(struct inotify_event) + event->len;
1681 r = mount_load_proc_self_mountinfo(m, true);
1683 log_error_errno(r, "Failed to reread /proc/self/mountinfo: %m");
1685 /* Reset flags, just in case, for later calls */
1686 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1687 Mount *mount = MOUNT(u);
1689 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1695 manager_dispatch_load_queue(m);
1697 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1698 Mount *mount = MOUNT(u);
1700 if (!mount->is_mounted) {
1702 mount->from_proc_self_mountinfo = false;
1704 switch (mount->state) {
1707 /* This has just been unmounted by
1708 * somebody else, follow the state
1710 mount_enter_dead(mount, MOUNT_SUCCESS);
1717 } else if (mount->just_mounted || mount->just_changed) {
1719 /* New or changed mount entry */
1721 switch (mount->state) {
1725 /* This has just been mounted by
1726 * somebody else, follow the state
1728 mount_enter_mounted(mount, MOUNT_SUCCESS);
1731 case MOUNT_MOUNTING:
1732 mount_set_state(mount, MOUNT_MOUNTING_DONE);
1736 /* Nothing really changed, but let's
1737 * issue an notification call
1738 * nonetheless, in case somebody is
1739 * waiting for this. (e.g. file system
1740 * ro/rw remounts.) */
1741 mount_set_state(mount, mount->state);
1746 /* Reset the flags for later calls */
1747 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1753 static void mount_reset_failed(Unit *u) {
1754 Mount *m = MOUNT(u);
1758 if (m->state == MOUNT_FAILED)
1759 mount_set_state(m, MOUNT_DEAD);
1761 m->result = MOUNT_SUCCESS;
1762 m->reload_result = MOUNT_SUCCESS;
1765 static int mount_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
1766 return unit_kill_common(u, who, signo, -1, MOUNT(u)->control_pid, error);
1769 static const char* const mount_state_table[_MOUNT_STATE_MAX] = {
1770 [MOUNT_DEAD] = "dead",
1771 [MOUNT_MOUNTING] = "mounting",
1772 [MOUNT_MOUNTING_DONE] = "mounting-done",
1773 [MOUNT_MOUNTED] = "mounted",
1774 [MOUNT_REMOUNTING] = "remounting",
1775 [MOUNT_UNMOUNTING] = "unmounting",
1776 [MOUNT_MOUNTING_SIGTERM] = "mounting-sigterm",
1777 [MOUNT_MOUNTING_SIGKILL] = "mounting-sigkill",
1778 [MOUNT_REMOUNTING_SIGTERM] = "remounting-sigterm",
1779 [MOUNT_REMOUNTING_SIGKILL] = "remounting-sigkill",
1780 [MOUNT_UNMOUNTING_SIGTERM] = "unmounting-sigterm",
1781 [MOUNT_UNMOUNTING_SIGKILL] = "unmounting-sigkill",
1782 [MOUNT_FAILED] = "failed"
1785 DEFINE_STRING_TABLE_LOOKUP(mount_state, MountState);
1787 static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
1788 [MOUNT_EXEC_MOUNT] = "ExecMount",
1789 [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
1790 [MOUNT_EXEC_REMOUNT] = "ExecRemount",
1793 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
1795 static const char* const mount_result_table[_MOUNT_RESULT_MAX] = {
1796 [MOUNT_SUCCESS] = "success",
1797 [MOUNT_FAILURE_RESOURCES] = "resources",
1798 [MOUNT_FAILURE_TIMEOUT] = "timeout",
1799 [MOUNT_FAILURE_EXIT_CODE] = "exit-code",
1800 [MOUNT_FAILURE_SIGNAL] = "signal",
1801 [MOUNT_FAILURE_CORE_DUMP] = "core-dump"
1804 DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult);
1806 const UnitVTable mount_vtable = {
1807 .object_size = sizeof(Mount),
1808 .exec_context_offset = offsetof(Mount, exec_context),
1809 .cgroup_context_offset = offsetof(Mount, cgroup_context),
1810 .kill_context_offset = offsetof(Mount, kill_context),
1811 .exec_runtime_offset = offsetof(Mount, exec_runtime),
1817 .private_section = "Mount",
1820 .no_instances = true,
1826 .coldplug = mount_coldplug,
1830 .start = mount_start,
1832 .reload = mount_reload,
1836 .serialize = mount_serialize,
1837 .deserialize_item = mount_deserialize_item,
1839 .active_state = mount_active_state,
1840 .sub_state_to_string = mount_sub_state_to_string,
1842 .check_gc = mount_check_gc,
1844 .sigchld_event = mount_sigchld_event,
1846 .reset_failed = mount_reset_failed,
1848 .bus_interface = "org.freedesktop.systemd1.Mount",
1849 .bus_vtable = bus_mount_vtable,
1850 .bus_set_property = bus_mount_set_property,
1851 .bus_commit_properties = bus_mount_commit_properties,
1853 .get_timeout = mount_get_timeout,
1855 .can_transient = true,
1857 .enumerate = mount_enumerate,
1858 .shutdown = mount_shutdown,
1860 .status_message_formats = {
1861 .starting_stopping = {
1862 [0] = "Mounting %s...",
1863 [1] = "Unmounting %s...",
1865 .finished_start_job = {
1866 [JOB_DONE] = "Mounted %s.",
1867 [JOB_FAILED] = "Failed to mount %s.",
1868 [JOB_DEPENDENCY] = "Dependency failed for %s.",
1869 [JOB_TIMEOUT] = "Timed out mounting %s.",
1871 .finished_stop_job = {
1872 [JOB_DONE] = "Unmounted %s.",
1873 [JOB_FAILED] = "Failed unmounting %s.",
1874 [JOB_TIMEOUT] = "Timed out unmounting %s.",