swap.c revision e64d523579dc6ed48db7ccdc5441c0adc33cd4e2
90929d4c6b284dd318f4a78c2aa407afb7423747oliver.bradley/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
8ef97ae6da17266dc714102092c427da04c535efElizabeth This file is part of systemd.
along with systemd; If not, see <http://www.gnu.org/licenses/>.
#include <errno.h>
#include <unistd.h>
#include <libudev.h>
#include "unit.h"
#include "swap.h"
#include "unit-name.h"
#include "dbus-swap.h"
#include "special.h"
#include "exit-status.h"
#include "path-util.h"
#include "virt.h"
#include "udev-util.h"
#include "fstab-util.h"
assert(s);
if (!s->from_proc_swaps)
s->from_proc_swaps = false;
assert(s);
if (s->devnode) {
if (first)
if (devnode) {
if (!s->devnode)
return -ENOMEM;
assert(s);
u->ignore_on_isolate = true;
assert(s);
if (s->control_pid <= 0)
s->control_pid = 0;
assert(s);
assert(s);
if (s->timeout_usec <= 0) {
if (s->timer_event_source) {
return sd_event_add_time(
&s->timer_event_source,
swap_dispatch_timer, s);
assert(s);
if (!s->what)
if (!s->from_fragment)
* systemd-remount-fs.service, since they might need a
assert(s);
return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
return log_oom();
log_unit_error(UNIT(s)->id, "%s: Value of \"What\" and unit name do not match, not loading.", UNIT(s)->id);
return -EINVAL;
log_unit_error(UNIT(s)->id, "%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing to load.", UNIT(s)->id);
return -EINVAL;
assert(s);
p = udev_device_get_devnode(d);
return swap_set_devnode(s, p);
assert(s);
s->from_fragment = true;
if (!s->what) {
if (!s->what)
return -ENOMEM;
r = swap_add_device_links(s);
r = swap_load_devnode(s);
r = unit_patch_contexts(u);
r = swap_add_default_dependencies(s);
return swap_verify(s);
static int swap_setup_unit(
Manager *m,
const char *what,
const char *what_proc_swaps,
int priority,
bool set_flags) {
bool delete = false;
SwapParameters *p;
assert(m);
return log_oom();
u = manager_get_unit(m, e);
log_error("Swap %s appeared twice with different device paths %s and %s", e, SWAP(u)->parameters_proc_swaps.what, what_proc_swaps);
return -EEXIST;
delete = true;
return log_oom();
r = unit_add_name(u, e);
goto fail;
r = -ENOMEM;
goto fail;
delete = false;
if (!p->what) {
if (!p->what) {
r = -ENOMEM;
goto fail;
if (set_flags) {
fail:
if (delete && u)
unit_free(u);
const char *dn;
assert(m);
assert(s);
assert(s);
else if (s->from_proc_swaps)
if (s->control_pid <= 0)
return -EBADMSG;
r = swap_arm_timer(s);
SwapParameters *p;
assert(s);
assert(f);
if (s->from_proc_swaps)
p = &s->parameters_proc_swaps;
else if (s->from_fragment)
p = &s->parameters_fragment;
p = NULL;
fprintf(f,
if (s->devnode)
fprintf(f,
if (s->control_pid > 0)
fprintf(f,
.apply_permissions = true,
.apply_chroot = true,
.apply_tty_stdin = true,
assert(s);
assert(c);
if (s->reset_cpu_usage) {
s->reset_cpu_usage = false;
goto fail;
r = swap_arm_timer(s);
goto fail;
r = exec_spawn(c,
&s->exec_context,
s->exec_runtime,
&pid);
goto fail;
goto fail;
fail:
assert(s);
if (f != SWAP_SUCCESS)
s->result = f;
exec_context_destroy_runtime_directory(&s->exec_context, manager_get_runtime_prefix(UNIT(s)->manager));
assert(s);
if (f != SWAP_SUCCESS)
s->result = f;
assert(s);
if (f != SWAP_SUCCESS)
s->result = f;
r = unit_kill_context(
UNIT(s),
&s->kill_context,
s->control_pid,
goto fail;
r = swap_arm_timer(s);
goto fail;
fail:
assert(s);
if (s->from_fragment) {
if (priority < 0) {
goto fail;
if (priority >= 0) {
char p[DECIMAL_STR_MAX(int)];
goto fail;
const char *discard_arg;
goto fail;
goto fail;
goto fail;
fail:
assert(s);
s->what,
NULL);
goto fail;
goto fail;
fail:
assert(s);
return -EAGAIN;
return -EPERM;
return -EAGAIN;
s->reset_cpu_usage = true;
assert(s);
return -EPERM;
assert(s);
assert(f);
if (s->control_pid > 0)
if (s->control_command_id >= 0)
assert(s);
if (state < 0)
SwapResult f;
else if (f != SWAP_SUCCESS)
s->result = f;
if (id < 0)
assert(u);
assert(u);
assert(s);
return s->from_proc_swaps;
SwapResult f;
assert(s);
s->control_pid = 0;
f = SWAP_SUCCESS;
f = SWAP_FAILURE_SIGNAL;
if (f != SWAP_SUCCESS)
s->result = f;
if (s->control_command) {
switch (s->state) {
case SWAP_ACTIVATING:
case SWAP_ACTIVATING_DONE:
case SWAP_ACTIVATING_SIGTERM:
case SWAP_ACTIVATING_SIGKILL:
if (f == SWAP_SUCCESS)
swap_enter_active(s, f);
swap_enter_dead(s, f);
case SWAP_DEACTIVATING:
swap_enter_dead(s, f);
assert(s);
switch (s->state) {
case SWAP_ACTIVATING:
case SWAP_ACTIVATING_DONE:
case SWAP_DEACTIVATING:
case SWAP_ACTIVATING_SIGTERM:
log_unit_warning(UNIT(s)->id, "%s deactivation timed out. Skipping SIGKILL. Ignoring.", UNIT(s)->id);
case SWAP_ACTIVATING_SIGKILL:
log_unit_warning(UNIT(s)->id, "%s swap process still around after SIGKILL. Ignoring.", UNIT(s)->id);
assert(m);
int prio = 0, k;
if (k == EOF)
return log_oom();
Unit *u;
assert(m);
r = swap_load_proc_swaps(m, true);
case SWAP_ACTIVE:
case SWAP_DEAD:
case SWAP_FAILED:
case SWAP_ACTIVATING:
assert(s);
if (s->from_fragment)
return NULL;
return NULL;
assert(s);
if (!set)
return -ENOMEM;
goto fail;
fail:
assert(m);
if (m->proc_swaps) {
assert(m);
if (!m->proc_swaps) {
if (!m->proc_swaps)
r = sd_event_add_io(m->event, &m->swap_event_source, fileno(m->proc_swaps), EPOLLPRI, swap_dispatch_io, m);
goto fail;
goto fail;
r = swap_load_proc_swaps(m, false);
goto fail;
fail:
swap_shutdown(m);
const char *dn;
Swap *s;
assert(m);
if (!dn)
return -ENOMEM;
return -ENOMEM;
const char *dn;
Swap *s;
if (!dn)
assert(s);
if (!s->timer_event_source)
if (supported < 0)
return supported;
.sections =
.no_alias = true,
.no_instances = true,
.starting_stopping = {
.finished_start_job = {
.finished_stop_job = {