HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/core/mount.c
Git blob: c05df5d10cff84a1ed7414c2a7c520c6e0845fdb
Lines 2001–2581 of 2,581
Mount *m = ASSERT_PTR(MOUNT(u));
usec_t t;
int r;
assert(timeout);
if (!m->timer_event_source)
return 0;
r = sd_event_source_get_time(m->timer_event_source, &t);
if (r < 0)
return r;
if (t == USEC_INFINITY)
return 0;
*timeout = t;
return 1;
}
static void mount_enumerate_perpetual(Manager *m) {
Unit *u;
int r;
assert(m);
assert(unit_type_supported(UNIT_MOUNT));
/* Whatever happens, we know for sure that the root directory is around, and cannot go away. Let's
* unconditionally synthesize it here and mark it as perpetual. */
u = manager_get_unit(m, SPECIAL_ROOT_MOUNT);
if (!u) {
r = unit_new_for_name(m, sizeof(Mount), SPECIAL_ROOT_MOUNT, &u);
if (r < 0)
return (void) log_error_errno(r, "Failed to allocate the special %s unit: %m",
SPECIAL_ROOT_MOUNT);
}
u->perpetual = true;
MOUNT(u)->deserialized_state = MOUNT_MOUNTED;
unit_add_to_load_queue(u);
unit_add_to_dbus_queue(u);
}
#if HAVE_LIBMOUNT
static bool mount_is_mounted(Mount *m) {
assert(m);
return UNIT(m)->perpetual || FLAGS_SET(m->proc_flags, MOUNT_PROC_IS_MOUNTED);
}
static int mount_on_ratelimit_expire(sd_event_source *s, void *userdata) {
Manager *m = ASSERT_PTR(userdata);
Job *j;
/* Let's enqueue all start jobs that were previously skipped because of active ratelimit. */
HASHMAP_FOREACH(j, m->jobs) {
if (j->unit->type != UNIT_MOUNT)
continue;
job_add_to_run_queue(j);
}
/* By entering ratelimited state we made all mount start jobs not runnable, now rate limit is over so
* let's make sure we dispatch them in the next iteration. */
manager_trigger_run_queue(m);
return 0;
}
#endif
static void mount_enumerate(Manager *m) {
assert(m);
assert(unit_type_supported(UNIT_MOUNT));
#if HAVE_LIBMOUNT
int r;
sym_mnt_init_debug(0);
if (!m->mount_monitor) {
usec_t mount_rate_limit_interval = 1 * USEC_PER_SEC;
unsigned mount_rate_limit_burst = 5;
int fd;
m->mount_monitor = sym_mnt_new_monitor();
if (!m->mount_monitor) {
log_oom();
goto fail;
}
r = sym_mnt_monitor_enable_kernel(m->mount_monitor, 1);
if (r < 0) {
log_error_errno(r, "Failed to enable watching of kernel mount events: %m");
goto fail;
}
r = sym_mnt_monitor_enable_userspace(m->mount_monitor, 1, NULL);
if (r < 0) {
log_error_errno(r, "Failed to enable watching of userspace mount events: %m");
goto fail;
}
/* mnt_unref_monitor() will close the fd */
fd = r = sym_mnt_monitor_get_fd(m->mount_monitor);
if (r < 0) {
log_error_errno(r, "Failed to acquire watch file descriptor: %m");
goto fail;
}
r = sd_event_add_io(m->event, &m->mount_event_source, fd, EPOLLIN, mount_dispatch_io, m);
if (r < 0) {
log_error_errno(r, "Failed to watch mount file descriptor: %m");
goto fail;
}
r = sd_event_source_set_priority(m->mount_event_source, EVENT_PRIORITY_MOUNT_TABLE);
if (r < 0) {
log_error_errno(r, "Failed to adjust mount watch priority: %m");
goto fail;
}
/* Let users override the default (5 in 1s), as it stalls the boot sequence on busy systems. */
const char *e = secure_getenv("SYSTEMD_DEFAULT_MOUNT_RATE_LIMIT_INTERVAL_SEC");
if (e) {
r = parse_sec(e, &mount_rate_limit_interval);
if (r < 0)
log_debug_errno(r, "Invalid value in $SYSTEMD_DEFAULT_MOUNT_RATE_LIMIT_INTERVAL_SEC, ignoring: %s", e);
}
e = secure_getenv("SYSTEMD_DEFAULT_MOUNT_RATE_LIMIT_BURST");
if (e) {
r = safe_atou(e, &mount_rate_limit_burst);
if (r < 0)
log_debug_errno(r, "Invalid value in $SYSTEMD_DEFAULT_MOUNT_RATE_LIMIT_BURST, ignoring: %s", e);
}
r = sd_event_source_set_ratelimit(m->mount_event_source, mount_rate_limit_interval, mount_rate_limit_burst);
if (r < 0) {
log_error_errno(r, "Failed to enable rate limit for mount events: %m");
goto fail;
}
r = sd_event_source_set_ratelimit_expire_callback(m->mount_event_source, mount_on_ratelimit_expire);
if (r < 0) {
log_error_errno(r, "Failed to enable rate limit for mount events: %m");
goto fail;
}
(void) sd_event_source_set_description(m->mount_event_source, "mount-monitor-dispatch");
}
r = mount_load_proc_self_mountinfo(m, false);
if (r < 0)
goto fail;
return;
fail:
#endif
mount_shutdown(m);
}
static int drain_libmount(Manager *m) {
assert(m);
if (!m->mount_monitor)
return false;
#if HAVE_LIBMOUNT
bool rescan = false;
int r;
/* Drain all events and verify that the event is valid.
*
* Note that libmount also monitors /run/mount mkdir if the directory does not exist yet. The mkdir
* may generate event which is irrelevant for us.
*
* error: r < 0; valid: r == 0, false positive: r == 1 */
do {
r = sym_mnt_monitor_next_change(m->mount_monitor, NULL, NULL);
if (r < 0)
return log_error_errno(r, "Failed to drain libmount events: %m");
if (r == 0)
rescan = true;
} while (r == 0);
return rescan;
#else
assert_not_reached();
#endif
}
static int mount_process_proc_self_mountinfo(Manager *m) {
int r;
assert(m);
r = drain_libmount(m);
if (r <= 0)
return r;
#if HAVE_LIBMOUNT
r = mount_load_proc_self_mountinfo(m, true);
if (r < 0) {
/* Reset flags, just in case, for later calls */
LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT])
MOUNT(u)->proc_flags = 0;
return 0;
}
manager_dispatch_load_queue(m);
_cleanup_set_free_ Set *around = NULL, *gone = NULL;
LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
Mount *mount = MOUNT(u);
if (!mount_is_mounted(mount)) {
/* A mount point is not around right now. It might be gone, or might never have
* existed. */
if (mount->from_proc_self_mountinfo &&
mount->parameters_proc_self_mountinfo.what)
/* Remember that this device might just have disappeared */
if (set_put_strdup_full(&gone, &path_hash_ops_free, mount->parameters_proc_self_mountinfo.what) < 0)
log_oom(); /* we don't care too much about OOM here... */
mount->from_proc_self_mountinfo = false;
assert_se(update_parameters_proc_self_mountinfo(mount, NULL, NULL, NULL) >= 0);
switch (mount->state) {
case MOUNT_MOUNTED:
/* This has just been unmounted by somebody else, follow the state change.
* Also explicitly override the result (see the comment in mount_sigchld_event()),
* but more aggressively here since the state change is extrinsic. */
mount_cycle_clear(mount);
mount_enter_dead(mount, MOUNT_SUCCESS, /* flush_result= */ true);
break;
case MOUNT_MOUNTING_DONE:
/* The mount command may add the corresponding proc mountinfo entry and
* then remove it because of an internal error. E.g., fuse.sshfs seems
* to do that when the connection fails. See #17617. To handle such the
* case, let's once set the state back to mounting. Then, the unit can
* correctly enter the failed state later in mount_sigchld_event(). */
mount_set_state(mount, MOUNT_MOUNTING);
break;
default:
;
}
} else if (mount->proc_flags & (MOUNT_PROC_JUST_MOUNTED|MOUNT_PROC_JUST_CHANGED)) {
/* A mount point was added or changed */
switch (mount->state) {
case MOUNT_DEAD:
case MOUNT_FAILED:
/* This has just been mounted by somebody else, follow the state change, but let's
* generate a new invocation ID for this implicitly and automatically. */
(void) unit_acquire_invocation_id(u);
mount_cycle_clear(mount);
mount_enter_mounted(mount, MOUNT_SUCCESS);
break;
case MOUNT_MOUNTING:
mount_set_state(mount, MOUNT_MOUNTING_DONE);
break;
default:
/* Nothing really changed, but let's issue an notification call nonetheless,
* in case somebody is waiting for this. (e.g. file system ro/rw
* remounts.) */
mount_set_state(mount, mount->state);
}
}
if (mount_is_mounted(mount) &&
mount->from_proc_self_mountinfo &&
mount->parameters_proc_self_mountinfo.what)
/* Track devices currently used */
if (set_put_strdup_full(&around, &path_hash_ops_free, mount->parameters_proc_self_mountinfo.what) < 0)
log_oom();
/* Reset the flags for later calls */
mount->proc_flags = 0;
}
const char *what;
SET_FOREACH(what, gone) {
if (set_contains(around, what))
continue;
/* Let the device units know that the device is no longer mounted */
device_found_node(m, what, DEVICE_NOT_FOUND, DEVICE_FOUND_MOUNT);
}
return 0;
#else
assert_not_reached();
#endif
}
#if HAVE_LIBMOUNT
static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
Manager *m = ASSERT_PTR(userdata);
assert(revents & EPOLLIN);
return mount_process_proc_self_mountinfo(m);
}
#endif
static void mount_reset_failed(Unit *u) {
Mount *m = MOUNT(u);
assert(m);
if (m->state == MOUNT_FAILED)
mount_set_state(m, MOUNT_DEAD);
m->result = MOUNT_SUCCESS;
m->reload_result = MOUNT_SUCCESS;
m->clean_result = MOUNT_SUCCESS;
}
static PidRef* mount_control_pid(Unit *u) {
return &ASSERT_PTR(MOUNT(u))->control_pid;
}
static int mount_clean(Unit *u, ExecCleanMask mask) {
_cleanup_strv_free_ char **l = NULL;
Mount *m = MOUNT(u);
int r;
assert(m);
assert(mask != 0);
if (m->state != MOUNT_DEAD)
return -EBUSY;
r = exec_context_get_clean_directories(&m->exec_context, u->manager->prefix, mask, &l);
if (r < 0)
return r;
if (strv_isempty(l))
return -EUNATCH;
mount_unwatch_control_pid(m);
m->clean_result = MOUNT_SUCCESS;
m->control_command = NULL;
m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
r = mount_arm_timer(m, /* relative= */ true, m->exec_context.timeout_clean_usec);
if (r < 0) {
log_unit_warning_errno(u, r, "Failed to install timer: %m");
goto fail;
}
r = unit_fork_and_watch_rm_rf(u, l, &m->control_pid);
if (r < 0) {
log_unit_warning_errno(u, r, "Failed to spawn cleaning task: %m");
goto fail;
}
mount_set_state(m, MOUNT_CLEANING);
return 0;
fail:
m->clean_result = MOUNT_FAILURE_RESOURCES;
m->timer_event_source = sd_event_source_disable_unref(m->timer_event_source);
return r;
}
static int mount_can_clean(Unit *u, ExecCleanMask *ret) {
Mount *m = ASSERT_PTR(MOUNT(u));
return exec_context_get_clean_mask(&m->exec_context, ret);
}
static int mount_test_startable(Unit *u) {
Mount *m = ASSERT_PTR(MOUNT(u));
int r;
/* It is already being started. */
if (IN_SET(m->state, MOUNT_MOUNTING, MOUNT_MOUNTING_DONE))
return false;
r = unit_test_start_limit(u);
if (r < 0) {
mount_enter_dead(m, MOUNT_FAILURE_START_LIMIT_HIT, /* flush_result= */ false);
return r;
}
if (!get_mount_parameters_fragment(m))
return -ENOENT;
return true;
}
static bool mount_supported(void) {
return dlopen_libmount(LOG_DEBUG) >= 0;
}
static int mount_subsystem_ratelimited(Manager *m) {
assert(m);
if (!m->mount_event_source)
return false;
return sd_event_source_is_ratelimited(m->mount_event_source);
}
char* mount_get_where_escaped(const Mount *m) {
assert(m);
if (!m->where)
return strdup("");
return utf8_escape_invalid(m->where);
}
char* mount_get_what_escaped(const Mount *m) {
const char *s = NULL;
assert(m);
if (m->from_proc_self_mountinfo && m->parameters_proc_self_mountinfo.what)
s = m->parameters_proc_self_mountinfo.what;
else if (m->from_fragment && m->parameters_fragment.what)
s = m->parameters_fragment.what;
if (!s)
return strdup("");
return utf8_escape_invalid(s);
}
char* mount_get_options_escaped(const Mount *m) {
const char *s = NULL;
assert(m);
if (m->from_proc_self_mountinfo && m->parameters_proc_self_mountinfo.options)
s = m->parameters_proc_self_mountinfo.options;
else if (m->from_fragment && m->parameters_fragment.options)
s = m->parameters_fragment.options;
if (!s)
return strdup("");
return utf8_escape_invalid(s);
}
const char* mount_get_fstype(const Mount *m) {
assert(m);
if (m->from_proc_self_mountinfo && m->parameters_proc_self_mountinfo.fstype)
return m->parameters_proc_self_mountinfo.fstype;
if (m->from_fragment && m->parameters_fragment.fstype)
return m->parameters_fragment.fstype;
return NULL;
}
static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
[MOUNT_EXEC_MOUNT] = "ExecMount",
[MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
[MOUNT_EXEC_REMOUNT] = "ExecRemount",
};
DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
static const char* const mount_result_table[_MOUNT_RESULT_MAX] = {
[MOUNT_SUCCESS] = "success",
[MOUNT_FAILURE_RESOURCES] = "resources",
[MOUNT_FAILURE_TIMEOUT] = "timeout",
[MOUNT_FAILURE_EXIT_CODE] = "exit-code",
[MOUNT_FAILURE_SIGNAL] = "signal",
[MOUNT_FAILURE_CORE_DUMP] = "core-dump",
[MOUNT_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
[MOUNT_FAILURE_PROTOCOL] = "protocol",
};
DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult);
const UnitVTable mount_vtable = {
.object_size = sizeof(Mount),
.exec_context_offset = offsetof(Mount, exec_context),
.cgroup_context_offset = offsetof(Mount, cgroup_context),
.kill_context_offset = offsetof(Mount, kill_context),
.exec_runtime_offset = offsetof(Mount, exec_runtime),
.cgroup_runtime_offset = offsetof(Mount, cgroup_runtime),
.sections =
"Unit\0"
"Mount\0"
"Install\0",
.private_section = "Mount",
.can_transient = true,
.can_fail = true,
.exclude_from_switch_root_serialization = true,
.notify_plymouth = true,
.track_orphaned = true,
.init = mount_init,
.load = mount_load,
.done = mount_done,
.coldplug = mount_coldplug,
.catchup = mount_catchup,
.dump = mount_dump,
.start = mount_start,
.stop = mount_stop,
.reload = mount_reload,
.can_reload = mount_can_reload,
.clean = mount_clean,
.can_clean = mount_can_clean,
.serialize = mount_serialize,
.deserialize_item = mount_deserialize_item,
.active_state = mount_active_state,
.sub_state_to_string = mount_sub_state_to_string,
.will_restart = unit_will_restart_default,
.may_gc = mount_may_gc,
.is_extrinsic = mount_is_extrinsic,
.sigchld_event = mount_sigchld_event,
.reset_failed = mount_reset_failed,
.notify_handoff_timestamp = mount_handoff_timestamp,
.control_pid = mount_control_pid,
.bus_set_property = bus_mount_set_property,
.bus_commit_properties = bus_mount_commit_properties,
.get_timeout = mount_get_timeout,
.enumerate_perpetual = mount_enumerate_perpetual,
.enumerate = mount_enumerate,
.shutdown = mount_shutdown,
.supported = mount_supported,
.subsystem_ratelimited = mount_subsystem_ratelimited,
.status_message_formats = {
.starting_stopping = {
[0] = "Mounting %s...",
[1] = "Unmounting %s...",
},
.finished_start_job = {
[JOB_DONE] = "Mounted %s.",
[JOB_FAILED] = "Failed to mount %s.",
[JOB_TIMEOUT] = "Timed out mounting %s.",
},
.finished_stop_job = {
[JOB_DONE] = "Unmounted %s.",
[JOB_FAILED] = "Failed unmounting %s.",
[JOB_TIMEOUT] = "Timed out unmounting %s.",
},
},
.test_startable = mount_test_startable,
};