HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/core/swap.c
Git blob: 38a2b347e2bfe315886a141833a5e9a1ae4b2720
Lines 1001–1715 of 1,715
Swap *s = ASSERT_PTR(SWAP(u));
assert(fds);
if (streq(key, "state")) {
SwapState state;
state = swap_state_from_string(value);
if (state < 0)
log_unit_debug(u, "Failed to parse state value: %s", value);
else
s->deserialized_state = state;
} else if (streq(key, "result")) {
SwapResult f;
f = swap_result_from_string(value);
if (f < 0)
log_unit_debug(u, "Failed to parse result value: %s", value);
else if (f != SWAP_SUCCESS)
s->result = f;
} else if (streq(key, "control-pid")) {
if (!pidref_is_set(&s->control_pid))
(void) deserialize_pidref(fds, value, &s->control_pid);
} else if (streq(key, "control-command")) {
SwapExecCommand id;
id = swap_exec_command_from_string(value);
if (id < 0)
log_unit_debug(u, "Failed to parse exec-command value: %s", value);
else {
s->control_command_id = id;
s->control_command = s->exec_command + id;
}
} else
log_unit_debug(u, "Unknown serialization key: %s", key);
return 0;
}
static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) {
Swap *s = ASSERT_PTR(SWAP(u));
SwapResult f;
assert(pid >= 0);
if (pid != s->control_pid.pid)
return;
/* Let's scan /proc/swaps before we process SIGCHLD. For the reasoning see the similar code in
* mount.c */
(void) swap_process_proc_swaps(u->manager);
pidref_done(&s->control_pid);
if (is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL))
f = SWAP_SUCCESS;
else if (code == CLD_EXITED)
f = SWAP_FAILURE_EXIT_CODE;
else if (code == CLD_KILLED)
f = SWAP_FAILURE_SIGNAL;
else if (code == CLD_DUMPED)
f = SWAP_FAILURE_CORE_DUMP;
else
assert_not_reached();
if (s->result == SWAP_SUCCESS)
s->result = f;
if (s->control_command) {
exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
s->control_command = NULL;
s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
}
unit_log_process_exit(
u,
"Swap process",
swap_exec_command_to_string(s->control_command_id),
f == SWAP_SUCCESS,
code, status);
switch (s->state) {
case SWAP_ACTIVATING:
case SWAP_ACTIVATING_DONE:
if (f == SWAP_SUCCESS || s->from_proc_swaps)
swap_enter_active(s, f);
else
swap_enter_dead(s, f);
break;
case SWAP_DEACTIVATING:
case SWAP_DEACTIVATING_SIGKILL:
case SWAP_DEACTIVATING_SIGTERM:
swap_enter_dead_or_active(s, f);
break;
case SWAP_CLEANING:
if (s->clean_result == SWAP_SUCCESS)
s->clean_result = f;
swap_enter_dead(s, SWAP_SUCCESS);
break;
default:
assert_not_reached();
}
/* Notify clients about changed exit status */
unit_add_to_dbus_queue(u);
}
static int swap_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
Swap *s = ASSERT_PTR(SWAP(userdata));
assert(s->timer_event_source == source);
switch (s->state) {
case SWAP_ACTIVATING:
case SWAP_ACTIVATING_DONE:
log_unit_warning(UNIT(s), "Activation timed out. Stopping.");
swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
break;
case SWAP_DEACTIVATING:
log_unit_warning(UNIT(s), "Deactivation timed out. Stopping.");
swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
break;
case SWAP_DEACTIVATING_SIGTERM:
if (s->kill_context.send_sigkill) {
log_unit_warning(UNIT(s), "Swap process timed out. Killing.");
swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT);
} else {
log_unit_warning(UNIT(s), "Swap process timed out. Skipping SIGKILL. Ignoring.");
swap_enter_dead_or_active(s, SWAP_FAILURE_TIMEOUT);
}
break;
case SWAP_DEACTIVATING_SIGKILL:
log_unit_warning(UNIT(s), "Swap process still around after SIGKILL. Ignoring.");
swap_enter_dead_or_active(s, SWAP_FAILURE_TIMEOUT);
break;
case SWAP_CLEANING:
log_unit_warning(UNIT(s), "Cleaning timed out. killing.");
if (s->clean_result == SWAP_SUCCESS)
s->clean_result = SWAP_FAILURE_TIMEOUT;
swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, 0);
break;
default:
assert_not_reached();
}
return 0;
}
static int swap_load_proc_swaps(Manager *m, bool set_flags) {
assert(m);
rewind(m->proc_swaps);
(void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
for (unsigned i = 1;; i++) {
_cleanup_free_ char *dev = NULL, *d = NULL;
int prio = 0, k;
k = fscanf(m->proc_swaps,
"%ms " /* device/file */
"%*s " /* type of swap */
"%*s " /* swap size */
"%*s " /* used */
"%i\n", /* priority */
&dev, &prio);
if (k != 2) {
if (k == EOF)
break;
log_warning("Failed to parse /proc/swaps:%u, skipping.", i);
continue;
}
ssize_t l = cunescape(dev, UNESCAPE_RELAX, &d);
if (l < 0)
return log_error_errno(l, "Failed to unescape device path: %m");
device_found_node(m, d, DEVICE_FOUND_SWAP, DEVICE_FOUND_SWAP);
swap_process_new(m, d, prio, set_flags);
}
return 0;
}
static int swap_process_proc_swaps(Manager *m) {
int r;
assert(m);
r = swap_load_proc_swaps(m, true);
if (r < 0) {
/* Reset flags, just in case, for late calls */
LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
Swap *swap = SWAP(u);
assert(swap);
swap->is_active = swap->just_activated = false;
}
return 0;
}
manager_dispatch_load_queue(m);
LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
Swap *swap = SWAP(u);
assert(swap);
if (!swap->is_active) {
swap_unset_proc_swaps(swap);
switch (swap->state) {
case SWAP_ACTIVE:
/* This has just been deactivated */
swap_enter_dead(swap, SWAP_SUCCESS);
break;
default:
/* Fire again */
swap_set_state(swap, swap->state);
}
if (swap->what)
device_found_node(m, swap->what, DEVICE_NOT_FOUND, DEVICE_FOUND_SWAP);
} else if (swap->just_activated) {
/* New swap entry */
switch (swap->state) {
case SWAP_DEAD:
case SWAP_FAILED:
(void) unit_acquire_invocation_id(u);
swap_cycle_clear(swap);
swap_enter_active(swap, SWAP_SUCCESS);
break;
case SWAP_ACTIVATING:
swap_set_state(swap, SWAP_ACTIVATING_DONE);
break;
default:
/* Nothing really changed, but let's issue an notification call nonetheless,
* in case somebody is waiting for this. */
swap_set_state(swap, swap->state);
}
}
/* Reset the flags for later calls */
swap->is_active = swap->just_activated = false;
}
return 1;
}
static int swap_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
Manager *m = ASSERT_PTR(userdata);
assert(revents & EPOLLPRI);
return swap_process_proc_swaps(m);
}
static Unit* swap_following(Unit *u) {
Swap *s = ASSERT_PTR(SWAP(u));
Swap *first = NULL;
/* If the user configured the swap through /etc/fstab or
* a device unit, follow that. */
if (s->from_fragment)
return NULL;
LIST_FOREACH_OTHERS(same_devnode, other, s)
if (other->from_fragment)
return UNIT(other);
/* Otherwise, make everybody follow the unit that's named after
* the swap device in the kernel */
if (streq_ptr(s->what, s->devnode))
return NULL;
LIST_FOREACH(same_devnode, other, s->same_devnode_next)
if (streq_ptr(other->what, other->devnode))
return UNIT(other);
LIST_FOREACH_BACKWARDS(same_devnode, other, s->same_devnode_prev) {
if (streq_ptr(other->what, other->devnode))
return UNIT(other);
first = other;
}
/* Fall back to the first on the list */
return UNIT(first);
}
static int swap_following_set(Unit *u, Set **ret) {
Swap *s = ASSERT_PTR(SWAP(u));
_cleanup_set_free_ Set *set = NULL;
int r;
assert(ret);
if (LIST_JUST_US(same_devnode, s)) {
*ret = NULL;
return 0;
}
set = set_new(NULL);
if (!set)
return -ENOMEM;
LIST_FOREACH_OTHERS(same_devnode, other, s) {
r = set_put(set, other);
if (r < 0)
return r;
}
*ret = TAKE_PTR(set);
return 1;
}
static void swap_shutdown(Manager *m) {
assert(m);
m->swap_event_source = sd_event_source_disable_unref(m->swap_event_source);
m->proc_swaps = safe_fclose(m->proc_swaps);
m->swaps_by_devnode = hashmap_free(m->swaps_by_devnode);
}
static void swap_enumerate(Manager *m) {
int r;
assert(m);
if (!m->proc_swaps) {
m->proc_swaps = fopen("/proc/swaps", "re");
if (!m->proc_swaps) {
if (errno == ENOENT)
log_debug_errno(errno, "Not swap enabled, skipping enumeration.");
else
log_warning_errno(errno, "Failed to open %s, ignoring: %m", "/proc/swaps");
return;
}
r = sd_event_add_io(m->event, &m->swap_event_source, fileno(m->proc_swaps), EPOLLPRI, swap_dispatch_io, m);
if (r < 0) {
log_error_errno(r, "Failed to watch /proc/swaps: %m");
goto fail;
}
/* Dispatch this before we dispatch SIGCHLD, so that
* we always get the events from /proc/swaps before
* the SIGCHLD of /sbin/swapon. */
r = sd_event_source_set_priority(m->swap_event_source, EVENT_PRIORITY_SWAP_TABLE);
if (r < 0) {
log_error_errno(r, "Failed to change /proc/swaps priority: %m");
goto fail;
}
(void) sd_event_source_set_description(m->swap_event_source, "swap-proc");
}
r = swap_load_proc_swaps(m, false);
if (r < 0)
goto fail;
return;
fail:
swap_shutdown(m);
}
int swap_process_device_new(Manager *m, sd_device *dev) {
_cleanup_free_ char *e = NULL;
const char *dn;
Unit *u;
int r;
assert(m);
assert(dev);
if (sd_device_get_devname(dev, &dn) < 0)
return 0;
r = unit_name_from_path(dn, ".swap", &e);
if (r < 0) {
log_debug_errno(r, "Cannot convert device name '%s' to unit name, ignoring: %m", dn);
return 0;
}
u = manager_get_unit(m, e);
if (u)
r = swap_set_devnode(SWAP(u), dn);
FOREACH_DEVICE_DEVLINK(dev, devlink) {
_cleanup_free_ char *n = NULL;
int q;
q = unit_name_from_path(devlink, ".swap", &n);
if (q == -EINVAL) /* If the name is not convertible to unit name, we can't manage it */
continue;
if (q < 0)
return q;
u = manager_get_unit(m, n);
if (u) {
q = swap_set_devnode(SWAP(u), dn);
if (q < 0)
r = q;
}
}
return r;
}
int swap_process_device_remove(Manager *m, sd_device *dev) {
const char *dn;
Swap *s;
int r;
r = sd_device_get_devname(dev, &dn);
if (r < 0)
return 0;
r = 0;
while ((s = hashmap_get(m->swaps_by_devnode, dn)))
RET_GATHER(r, swap_set_devnode(s, NULL));
return r;
}
static void swap_reset_failed(Unit *u) {
Swap *s = ASSERT_PTR(SWAP(u));
if (s->state == SWAP_FAILED)
swap_set_state(s, SWAP_DEAD);
s->result = SWAP_SUCCESS;
s->clean_result = SWAP_SUCCESS;
}
static void swap_handoff_timestamp(
Unit *u,
const struct ucred *ucred,
const dual_timestamp *ts) {
Swap *s = ASSERT_PTR(SWAP(u));
assert(ucred);
assert(ts);
if (s->control_pid.pid == ucred->pid && s->control_command) {
exec_status_handoff(&s->control_command->exec_status, ucred, ts);
unit_add_to_dbus_queue(u);
}
}
static int swap_get_timeout(Unit *u, usec_t *timeout) {
Swap *s = ASSERT_PTR(SWAP(u));
usec_t t;
int r;
assert(timeout);
if (!s->timer_event_source)
return 0;
r = sd_event_source_get_time(s->timer_event_source, &t);
if (r < 0)
return r;
if (t == USEC_INFINITY)
return 0;
*timeout = t;
return 1;
}
static bool swap_supported(void) {
static int supported = -1;
/* If swap support is not available in the kernel, or we are
* running in a container we don't support swap units, and any
* attempts to starting one should fail immediately. */
if (supported < 0)
supported =
access("/proc/swaps", F_OK) >= 0 &&
detect_container() <= 0;
return supported;
}
static PidRef* swap_control_pid(Unit *u) {
return &ASSERT_PTR(SWAP(u))->control_pid;
}
static int swap_clean(Unit *u, ExecCleanMask mask) {
Swap *s = ASSERT_PTR(SWAP(u));
_cleanup_strv_free_ char **l = NULL;
int r;
assert(mask != 0);
if (s->state != SWAP_DEAD)
return -EBUSY;
r = exec_context_get_clean_directories(&s->exec_context, u->manager->prefix, mask, &l);
if (r < 0)
return r;
if (strv_isempty(l))
return -EUNATCH;
swap_unwatch_control_pid(s);
s->clean_result = SWAP_SUCCESS;
s->control_command = NULL;
s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
r = swap_arm_timer(s, /* relative= */ true, s->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, &s->control_pid);
if (r < 0) {
log_unit_warning_errno(u, r, "Failed to spawn cleaning task: %m");
goto fail;
}
swap_set_state(s, SWAP_CLEANING);
return 0;
fail:
s->clean_result = SWAP_FAILURE_RESOURCES;
s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source);
return r;
}
static int swap_can_clean(Unit *u, ExecCleanMask *ret) {
Swap *s = ASSERT_PTR(SWAP(u));
return exec_context_get_clean_mask(&s->exec_context, ret);
}
static int swap_test_startable(Unit *u) {
Swap *s = ASSERT_PTR(SWAP(u));
int r;
/* It is already being started. */
if (s->state == SWAP_ACTIVATING)
return false;
r = unit_test_start_limit(u);
if (r < 0) {
swap_enter_dead(s, SWAP_FAILURE_START_LIMIT_HIT);
return r;
}
return true;
}
int swap_get_priority(const Swap *s) {
assert(s);
if (s->from_proc_swaps && s->parameters_proc_swaps.priority_set)
return s->parameters_proc_swaps.priority;
if (s->from_fragment && s->parameters_fragment.priority_set)
return s->parameters_fragment.priority;
return -1;
}
const char* swap_get_options(const Swap *s) {
assert(s);
if (s->from_fragment)
return s->parameters_fragment.options;
return NULL;
}
static const char* const swap_exec_command_table[_SWAP_EXEC_COMMAND_MAX] = {
[SWAP_EXEC_ACTIVATE] = "ExecActivate",
[SWAP_EXEC_DEACTIVATE] = "ExecDeactivate",
};
DEFINE_STRING_TABLE_LOOKUP(swap_exec_command, SwapExecCommand);
static const char* const swap_result_table[_SWAP_RESULT_MAX] = {
[SWAP_SUCCESS] = "success",
[SWAP_FAILURE_RESOURCES] = "resources",
[SWAP_FAILURE_TIMEOUT] = "timeout",
[SWAP_FAILURE_EXIT_CODE] = "exit-code",
[SWAP_FAILURE_SIGNAL] = "signal",
[SWAP_FAILURE_CORE_DUMP] = "core-dump",
[SWAP_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
};
DEFINE_STRING_TABLE_LOOKUP(swap_result, SwapResult);
const UnitVTable swap_vtable = {
.object_size = sizeof(Swap),
.exec_context_offset = offsetof(Swap, exec_context),
.cgroup_context_offset = offsetof(Swap, cgroup_context),
.kill_context_offset = offsetof(Swap, kill_context),
.exec_runtime_offset = offsetof(Swap, exec_runtime),
.cgroup_runtime_offset = offsetof(Swap, cgroup_runtime),
.sections =
"Unit\0"
"Swap\0"
"Install\0",
.private_section = "Swap",
.can_fail = true,
.track_orphaned = true,
.init = swap_init,
.load = swap_load,
.done = swap_done,
.coldplug = swap_coldplug,
.dump = swap_dump,
.start = swap_start,
.stop = swap_stop,
.clean = swap_clean,
.can_clean = swap_can_clean,
.get_timeout = swap_get_timeout,
.serialize = swap_serialize,
.deserialize_item = swap_deserialize_item,
.active_state = swap_active_state,
.sub_state_to_string = swap_sub_state_to_string,
.will_restart = unit_will_restart_default,
.may_gc = swap_may_gc,
.is_extrinsic = swap_is_extrinsic,
.sigchld_event = swap_sigchld_event,
.reset_failed = swap_reset_failed,
.notify_handoff_timestamp = swap_handoff_timestamp,
.control_pid = swap_control_pid,
.bus_set_property = bus_swap_set_property,
.bus_commit_properties = bus_swap_commit_properties,
.following = swap_following,
.following_set = swap_following_set,
.enumerate = swap_enumerate,
.shutdown = swap_shutdown,
.supported = swap_supported,
.status_message_formats = {
.starting_stopping = {
[0] = "Activating swap %s...",
[1] = "Deactivating swap %s...",
},
.finished_start_job = {
[JOB_DONE] = "Activated swap %s.",
[JOB_FAILED] = "Failed to activate swap %s.",
[JOB_TIMEOUT] = "Timed out activating swap %s.",
},
.finished_stop_job = {
[JOB_DONE] = "Deactivated swap %s.",
[JOB_FAILED] = "Failed deactivating swap %s.",
[JOB_TIMEOUT] = "Timed out deactivating swap %s.",
},
},
.test_startable = swap_test_startable,
.notify_plymouth = true,
};