Commit 19adb8a3 authored by Zbigniew Jędrzejewski-Szmek's avatar Zbigniew Jędrzejewski-Szmek Committed by Lennart Poettering

systemd-sleep: add support for freeze and standby

A new config file /etc/systemd/sleep.conf is added.
It is parsed by systemd-sleep and logind. The strings written
to /sys/power/disk and /sys/power/state can be configured.
This allows people to use different modes of suspend on
systems with broken or special hardware.

Configuration is shared between systemd-sleep and logind
to enable logind to answer the question "can the system be
put to sleep" as correctly as possible without actually
invoking the action. If the user configured systemd-sleep
to only use 'freeze', but current kernel does not support it,
logind will properly report that the system cannot be put
to sleep.

https://bugs.freedesktop.org/show_bug.cgi?id=57793
https://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=commit;h=7e73c5ae6e7991a6c01f6d096ff8afaef4458c36
http://lists.freedesktop.org/archives/systemd-devel/2013-February/009238.html

SYSTEM_CONFIG_FILE and USER_CONFIG_FILE defines were removed
since they were used in only a few places and with the
addition of /etc/systemd/sleep.conf it becomes easier to just
append the name of each file to the dir name.
parent b4bc041b
......@@ -64,6 +64,7 @@ MANPAGES += \
man/systemd-nspawn.1 \
man/systemd-remount-fs.service.8 \
man/systemd-shutdownd.service.8 \
man/systemd-sleep.conf.5 \
man/systemd-suspend.service.8 \
man/systemd-sysctl.service.8 \
man/systemd-system-update-generator.8 \
......
......@@ -138,12 +138,11 @@ udevlibexec_PROGRAMS =
AM_CPPFLAGS = \
-include $(top_builddir)/config.h \
-DSYSTEM_CONFIG_FILE=\"$(pkgsysconfdir)/system.conf\" \
-DPKGSYSCONFDIR=\"$(pkgsysconfdir)\" \
-DSYSTEM_CONFIG_UNIT_PATH=\"$(pkgsysconfdir)/system\" \
-DSYSTEM_DATA_UNIT_PATH=\"$(systemunitdir)\" \
-DSYSTEM_SYSVINIT_PATH=\"$(SYSTEM_SYSVINIT_PATH)\" \
-DSYSTEM_SYSVRCND_PATH=\"$(SYSTEM_SYSVRCND_PATH)\" \
-DUSER_CONFIG_FILE=\"$(pkgsysconfdir)/user.conf\" \
-DUSER_CONFIG_UNIT_PATH=\"$(pkgsysconfdir)/user\" \
-DUSER_DATA_UNIT_PATH=\"$(userunitdir)\" \
-DCATALOG_DATABASE=\"$(catalogstatedir)/database\" \
......@@ -643,6 +642,8 @@ libsystemd_shared_la_SOURCES = \
src/shared/fdset.h \
src/shared/prioq.c \
src/shared/prioq.h \
src/shared/sleep-config.c \
src/shared/sleep-config.h \
src/shared/strv.c \
src/shared/strv.h \
src/shared/env-util.c \
......
......@@ -202,9 +202,6 @@ Features:
- pam: when leaving a session explicitly exclude the ReleaseSession() caller process from the killing spree
- logind: GetSessionByPID() should accept 0 as PID value
- we should probably handle SIGTERM/SIGINT to not leave dot files around, just in case
- add configuration/switches to use
freeze (http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git) and
standby (https://bugs.freedesktop.org/show_bug.cgi?id=57793) as suspend modes
* exec: when deinitializating a tty device fix the perms and group, too, not only when initializing. Set access mode/gid to 0620/tty.
......
<?xml version='1.0'?> <!--*-nxml-*-->
<?xml-stylesheet type="text/xsl" href="http://docbook.sourceforge.net/release/xsl/current/xhtml/docbook.xsl"?>
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
<!--
This file is part of systemd.
Copyright 2013 Zbigniew Jędrzejewski-Szmek
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
-->
<refentry id="systemd-sleep.conf">
<refentryinfo>
<title>systemd-sleep.conf</title>
<productname>systemd</productname>
<authorgroup>
<author>
<contrib>Developer</contrib>
<firstname>Zbigniew</firstname>
<surname>Jędrzejewski-Szmek</surname>
<email>zbyszek@in.waw.pl</email>
</author>
</authorgroup>
</refentryinfo>
<refmeta>
<refentrytitle>systemd-sleep.conf</refentrytitle>
<manvolnum>5</manvolnum>
</refmeta>
<refnamediv>
<refname>systemd-sleep.conf</refname>
<refpurpose>Suspend and hibernation configuration file</refpurpose>
</refnamediv>
<refsynopsisdiv>
<para><filename>/etc/systemd/sleep.conf</filename></para>
</refsynopsisdiv>
<refsect1>
<title>Description</title>
<para><command>systemd</command> supports three general
power-saving modes:</para>
<variablelist>
<varlistentry>
<term>suspend</term>
<listitem><para>a low-power state
where execution of the OS is paused,
and complete power loss might result
in lost data, and which is fast to
enter and exit. This corresponds to
suspend, standby, or freeze states as
understood by the kernel.
</para></listitem>
</varlistentry>
<varlistentry>
<term>hibernate</term>
<listitem><para>a low-power state
where execution of the OS is paused,
and complete power loss does not
result in lost data, and which might
be slow to enter and exit. This
corresponds to the hibernation as
understood by the kernel.
</para></listitem>
</varlistentry>
<varlistentry>
<term>hybrid-sleep</term>
<listitem><para>a low-power state
where execution of the OS is paused,
which might be slow to enter, and on
complete power loss does not result in
lost data but might be slower to exit
in that case. This mode is called
suspend-to-both by the kernel.
</para></listitem>
</varlistentry>
</variablelist>
<para>Settings in this file determine what strings
will be written to
<filename>/sys/power/disk</filename> and
<filename>/sys/power/state</filename> by
<citerefentry><refentrytitle>systemd-sleep</refentrytitle><manvolnum>8</manvolnum></citerefentry>
when
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>
attempts to suspend or hibernate the machine.</para>
</refsect1>
<refsect1>
<title>Options</title>
<para>The following options can be configured in the
<literal>[Sleep]</literal> section of
<filename>/etc/systemd/sleep.conf</filename>:</para>
<variablelist class='systemd-directives'>
<varlistentry>
<term><varname>SuspendMode=</varname></term>
<term><varname>HibernateMode=</varname></term>
<term><varname>HybridSleepMode=</varname></term>
<listitem><para>The string to be written to
<filename>/sys/power/disk</filename> by,
respectively,
<citerefentry><refentrytitle>systemd-suspend.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
<citerefentry><refentrytitle>systemd-hibernate.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>, or
<citerefentry><refentrytitle>systemd-hybrid-sleep.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>.
More than one value can be specified by seperating
multiple values with commas. They will be tried
in turn, until one is written without error. If
neither suceeds, the operation will be aborted.
</para></listitem>
</varlistentry>
<varlistentry>
<term><varname>SuspendState=</varname></term>
<term><varname>HibernateState=</varname></term>
<term><varname>HybridSleepState=</varname></term>
<listitem><para>The string to be written to
<filename>/sys/power/state</filename> by,
respectively,
<citerefentry><refentrytitle>systemd-suspend.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
<citerefentry><refentrytitle>systemd-hibernate.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>, or
<citerefentry><refentrytitle>systemd-hybrid-sleep.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>.
More than one value can be specified by seperating
multiple values with commas. They will be tried
in turn, until one is written without error. If
neither suceeds, the operation will be aborted.
</para></listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1>
<title>Example: freeze</title>
<para>Example: to exploit the <quote>freeze</quote> mode added
in Linux 3.9, one can use <command>systemctl suspend</command>
with
<programlisting>
[Sleep]
SuspendState=freeze
</programlisting></para>
</refsect1>
<refsect1>
<title>See Also</title>
<para>
<citerefentry><refentrytitle>systemd-sleep</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
<citerefentry><refentrytitle>systemd-suspend.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
<citerefentry><refentrytitle>systemd-hibernate.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
<citerefentry><refentrytitle>systemd-hybrid-sleep.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry><refentrytitle>systemd.directives</refentrytitle><manvolnum>7</manvolnum></citerefentry>
</para>
</refsect1>
</refentry>
......@@ -6,6 +6,7 @@
This file is part of systemd.
Copyright 2012 Lennart Poettering
Copyright 2013 Zbigniew Jędrzejewski-Szmek
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
......@@ -63,7 +64,7 @@
<para><filename>systemd-suspend.service</filename> is
a system service that is pulled in by
<filename>suspend.target</filename> and is responsible
for the actual system suspend. Similar,
for the actual system suspend. Similarly,
<filename>systemd-hibernate.service</filename> is
pulled in by <filename>hibernate.target</filename> to
execute the actual hibernation. Finally,
......@@ -88,8 +89,8 @@
but the first argument is now
"<literal>post</literal>". All executables in this
directory are executed in parallel, and execution of
the action is not continued before all executables
finished.</para>
the action is not continued until all executables
have finished.</para>
<para>Note that scripts or binaries dropped in
<filename>/usr/lib/systemd/system-sleep/</filename>
......@@ -100,7 +101,7 @@
<para>Note that
<filename>systemd-suspend.service</filename>,
<filename>systemd-hibernate.service</filename> and
<filename>systemd-hibernate.service</filename>, and
<filename>systemd-hybrid-sleep.service</filename>
should never be executed directly. Instead, trigger
system sleep states with a command such as
......@@ -108,14 +109,51 @@
similar.</para>
<para>Internally, this service will echo a string like
<literal>mem</literal> into
"<literal>mem</literal>" into
<filename>/sys/power/state</filename>, to trigger the
actual system suspend.</para>
actual system suspend. What exactly is written
where can be configured in the <literal>[Sleep]</literal>
section of <filename>/etc/systemd/sleep.conf</filename>.
See <citerefentry><refentrytitle>systemd-sleep.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
</para>
</refsect1>
<refsect1>
<title>Options</title>
<para><command>systemd-sleep</command> understands the
following commands:</para>
<variablelist>
<varlistentry>
<term><option>-h</option></term>
<term><option>--help</option></term>
<listitem><para>Print a short help
text and exit.</para></listitem>
</varlistentry>
<varlistentry>
<term><option>--version</option></term>
<listitem><para>Print the systemd version
identifier and exit.</para></listitem>
</varlistentry>
<varlistentry>
<term><option>suspend</option></term>
<term><option>hibernate</option></term>
<term><option>hybrid-sleep</option></term>
<listitem><para>Suspend, hibernate, or
put the system to hybrid sleep.</para>
</listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1>
<title>See Also</title>
<para>
<citerefentry><refentrytitle>systemd-sleep.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry><refentrytitle>systemd.special</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
......
......@@ -508,7 +508,7 @@
<refsect1>
<title>Directories</title>
<variablelist class='unit-directives'>
<variablelist>
<varlistentry>
<term>System unit directories</term>
......
......@@ -668,7 +668,7 @@ static int parse_config_file(void) {
const char *fn;
int r;
fn = arg_running_as == SYSTEMD_SYSTEM ? SYSTEM_CONFIG_FILE : USER_CONFIG_FILE;
fn = arg_running_as == SYSTEMD_SYSTEM ? PKGSYSCONFDIR "/system.conf" : PKGSYSCONFDIR "/user.conf";
f = fopen(fn, "re");
if (!f) {
if (errno == ENOENT)
......
......@@ -27,6 +27,7 @@
#include "special.h"
#include "dbus-common.h"
#include "logind-action.h"
#include "sleep-config.h"
int manager_handle_action(
Manager *m,
......@@ -74,11 +75,11 @@ int manager_handle_action(
}
if (handle == HANDLE_SUSPEND)
supported = can_sleep("mem") > 0;
supported = can_sleep("suspend") > 0;
else if (handle == HANDLE_HIBERNATE)
supported = can_sleep("disk") > 0;
supported = can_sleep("hibernate") > 0;
else if (handle == HANDLE_HYBRID_SLEEP)
supported = can_sleep("disk") > 0 && can_sleep_disk("suspend") > 0;
supported = can_sleep("hybrid-sleep") > 0;
else if (handle == HANDLE_KEXEC)
supported = access("/sbin/kexec", X_OK) >= 0;
else
......
......@@ -31,6 +31,7 @@
#include "path-util.h"
#include "polkit.h"
#include "special.h"
#include "sleep-config.h"
#include "systemd/sd-id128.h"
#include "systemd/sd-messages.h"
#include "fileio-label.h"
......@@ -1131,8 +1132,7 @@ static int bus_manager_can_shutdown_or_sleep(
const char *action,
const char *action_multiple_sessions,
const char *action_ignore_inhibit,
const char *sleep_type,
const char *sleep_disk_type,
const char *sleep_verb,
DBusError *error,
DBusMessage **_reply) {
......@@ -1153,22 +1153,10 @@ static int bus_manager_can_shutdown_or_sleep(
assert(error);
assert(_reply);
if (sleep_type) {
r = can_sleep(sleep_type);
if (sleep_verb) {
r = can_sleep(sleep_verb);
if (r < 0)
return r;
if (r == 0) {
result = "na";
goto finish;
}
}
if (sleep_disk_type) {
r = can_sleep_disk(sleep_disk_type);
if (r < 0)
return r;
if (r == 0) {
result = "na";
goto finish;
......@@ -1313,8 +1301,7 @@ static int bus_manager_do_shutdown_or_sleep(
const char *action,
const char *action_multiple_sessions,
const char *action_ignore_inhibit,
const char *sleep_type,
const char *sleep_disk_type,
const char *sleep_verb,
DBusError *error,
DBusMessage **_reply) {
......@@ -1347,17 +1334,8 @@ static int bus_manager_do_shutdown_or_sleep(
DBUS_TYPE_INVALID))
return -EINVAL;
if (sleep_type) {
r = can_sleep(sleep_type);
if (r < 0)
return r;
if (r == 0)
return -ENOTSUP;
}
if (sleep_disk_type) {
r = can_sleep_disk(sleep_disk_type);
if (sleep_verb) {
r = can_sleep(sleep_verb);
if (r < 0)
return r;
......@@ -2160,7 +2138,7 @@ static DBusHandlerResult manager_message_handler(
"org.freedesktop.login1.power-off",
"org.freedesktop.login1.power-off-multiple-sessions",
"org.freedesktop.login1.power-off-ignore-inhibit",
NULL, NULL,
NULL,
&error, &reply);
if (r < 0)
return bus_send_error_reply(connection, message, &error, r);
......@@ -2172,7 +2150,7 @@ static DBusHandlerResult manager_message_handler(
"org.freedesktop.login1.reboot",
"org.freedesktop.login1.reboot-multiple-sessions",
"org.freedesktop.login1.reboot-ignore-inhibit",
NULL, NULL,
NULL,
&error, &reply);
if (r < 0)
return bus_send_error_reply(connection, message, &error, r);
......@@ -2185,7 +2163,7 @@ static DBusHandlerResult manager_message_handler(
"org.freedesktop.login1.suspend",
"org.freedesktop.login1.suspend-multiple-sessions",
"org.freedesktop.login1.suspend-ignore-inhibit",
"mem", NULL,
"suspend",
&error, &reply);
if (r < 0)
return bus_send_error_reply(connection, message, &error, r);
......@@ -2197,7 +2175,7 @@ static DBusHandlerResult manager_message_handler(
"org.freedesktop.login1.hibernate",
"org.freedesktop.login1.hibernate-multiple-sessions",
"org.freedesktop.login1.hibernate-ignore-inhibit",
"disk", NULL,
"hibernate",
&error, &reply);
if (r < 0)
return bus_send_error_reply(connection, message, &error, r);
......@@ -2210,7 +2188,7 @@ static DBusHandlerResult manager_message_handler(
"org.freedesktop.login1.hibernate",
"org.freedesktop.login1.hibernate-multiple-sessions",
"org.freedesktop.login1.hibernate-ignore-inhibit",
"disk", "suspend",
"hybrid-sleep",
&error, &reply);
if (r < 0)
return bus_send_error_reply(connection, message, &error, r);
......@@ -2223,7 +2201,7 @@ static DBusHandlerResult manager_message_handler(
"org.freedesktop.login1.power-off",
"org.freedesktop.login1.power-off-multiple-sessions",
"org.freedesktop.login1.power-off-ignore-inhibit",
NULL, NULL,
NULL,
&error, &reply);
if (r < 0)
return bus_send_error_reply(connection, message, &error, r);
......@@ -2234,7 +2212,7 @@ static DBusHandlerResult manager_message_handler(
"org.freedesktop.login1.reboot",
"org.freedesktop.login1.reboot-multiple-sessions",
"org.freedesktop.login1.reboot-ignore-inhibit",
NULL, NULL,
NULL,
&error, &reply);
if (r < 0)
return bus_send_error_reply(connection, message, &error, r);
......@@ -2246,7 +2224,7 @@ static DBusHandlerResult manager_message_handler(
"org.freedesktop.login1.suspend",
"org.freedesktop.login1.suspend-multiple-sessions",
"org.freedesktop.login1.suspend-ignore-inhibit",
"mem", NULL,
"suspend",
&error, &reply);
if (r < 0)
return bus_send_error_reply(connection, message, &error, r);
......@@ -2258,7 +2236,7 @@ static DBusHandlerResult manager_message_handler(
"org.freedesktop.login1.hibernate",
"org.freedesktop.login1.hibernate-multiple-sessions",
"org.freedesktop.login1.hibernate-ignore-inhibit",
"disk", NULL,
"hibernate",
&error, &reply);
if (r < 0)
return bus_send_error_reply(connection, message, &error, r);
......@@ -2270,7 +2248,7 @@ static DBusHandlerResult manager_message_handler(
"org.freedesktop.login1.hibernate",
"org.freedesktop.login1.hibernate-multiple-sessions",
"org.freedesktop.login1.hibernate-ignore-inhibit",
"disk", "suspend",
"hybrid-sleep",
&error, &reply);
if (r < 0)
return bus_send_error_reply(connection, message, &error, r);
......
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Zbigniew Jędrzejewski-Szmek
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdio.h>
#include "conf-parser.h"
#include "sleep-config.h"
#include "fileio.h"
#include "log.h"
#include "strv.h"
#include "util.h"
int parse_sleep_config(const char *verb, char ***modes, char ***states) {
_cleanup_strv_free_ char
**suspend_mode = NULL, **suspend_state = NULL,
**hibernate_mode = NULL, **hibernate_state = NULL,
**hybrid_mode = NULL, **hybrid_state = NULL;
const ConfigTableItem items[] = {
{ "Sleep", "SuspendMode", config_parse_strv, 0, &suspend_mode },
{ "Sleep", "SuspendState", config_parse_strv, 0, &suspend_state },
{ "Sleep", "HibernateMode", config_parse_strv, 0, &hibernate_mode },
{ "Sleep", "HibernateState", config_parse_strv, 0, &hibernate_state },
{ "Sleep", "HybridSleepMode", config_parse_strv, 0, &hybrid_mode },
{ "Sleep", "HybridSleepState", config_parse_strv, 0, &hybrid_state },
{}};
int r;
FILE _cleanup_fclose_ *f;
f = fopen(PKGSYSCONFDIR "/sleep.conf", "re");
if (!f) {
if (errno == ENOENT)
return 0;
log_warning("Failed to open configuration file " PKGSYSCONFDIR "/sleep.conf: %m");
return 0;
}
r = config_parse(NULL, PKGSYSCONFDIR "/sleep.conf", f, "Sleep\0",
config_item_table_lookup, (void*) items, false, false, NULL);
if (r < 0)
log_warning("Failed to parse configuration file: %s", strerror(-r));
if (streq(verb, "suspend")) {
/* empty by default */
*modes = suspend_mode;
if (suspend_state)
*states = suspend_state;
else
*states = strv_split_nulstr("mem\0standby\0freeze\0");
suspend_mode = suspend_state = NULL;
} else if (streq(verb, "hibernate")) {
if (hibernate_mode)
*modes = hibernate_mode;
else
*modes = strv_split_nulstr("platform\0shutdown\0");
if (hibernate_state)
*states = hibernate_state;
else
*states = strv_split_nulstr("disk\0");
hibernate_mode = hibernate_state = NULL;
} else if (streq(verb, "hybrid-sleep")) {
if (hybrid_mode)
*modes = hybrid_mode;
else
*modes = strv_split_nulstr("suspend\0platform\0shutdown\0");
if (hybrid_state)
*states = hybrid_state;
else
*states = strv_split_nulstr("disk\0");
hybrid_mode = hybrid_state = NULL;
} else
assert_not_reached("what verb");
if (!modes || !states) {
strv_free(*modes);
strv_free(*states);
return log_oom();
}
return 0;
}
int can_sleep_state(char **types) {
char *w, *state, **type;
int r;
_cleanup_free_ char *p = NULL;
if (strv_isempty(types))
return true;
/* If /sys is read-only we cannot sleep */
if (access("/sys/power/state", W_OK) < 0)
return false;
r = read_one_line_file("/sys/power/state", &p);
if (r < 0)
return false;
STRV_FOREACH(type, types) {
size_t l, k;
k = strlen(*type);
FOREACH_WORD_SEPARATOR(w, l, p, WHITESPACE, state)
if (l == k && memcmp(w, *type, l) == 0)
return true;
}
return false;
}
int can_sleep_disk(char **types) {
char *w, *state, **type;
int r;
_cleanup_free_ char *p = NULL;
if (strv_isempty(types))
return true;