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.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2013-05-04 12:31:28 -04:00 committed by Lennart Poettering
parent b4bc041b17
commit 19adb8a320
15 changed files with 661 additions and 200 deletions

View File

@ -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 \

View File

@ -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 \

3
TODO
View File

@ -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.

181
man/systemd-sleep.conf.xml Normal file
View File

@ -0,0 +1,181 @@
<?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>

View File

@ -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>,

View File

@ -508,7 +508,7 @@
<refsect1>
<title>Directories</title>
<variablelist class='unit-directives'>
<variablelist>
<varlistentry>
<term>System unit directories</term>

View File

@ -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)

View File

@ -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

View File

@ -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);

182
src/shared/sleep-config.c Normal file
View File

@ -0,0 +1,182 @@
/*-*- 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;
/* If /sys is read-only we cannot sleep */
if (access("/sys/power/disk", W_OK) < 0)
return false;
r = read_one_line_file("/sys/power/disk", &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;
if (l == k + 2 && w[0] == '[' && memcmp(w + 1, *type, l - 2) == 0 && w[l-1] == ']')
return true;
}
}
return false;
}
int can_sleep(const char *verb) {
_cleanup_strv_free_ char **modes = NULL, **states = NULL;
int r;
assert(streq(verb, "suspend") ||
streq(verb, "hibernate") ||
streq(verb, "hybrid-sleep"));
r = parse_sleep_config(verb, &modes, &states);
if (r < 0)
return false;
return can_sleep_state(states) && can_sleep_disk(modes);
}

26
src/shared/sleep-config.h Normal file
View File

@ -0,0 +1,26 @@
/*-*- 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/>.
***/
int parse_sleep_config(const char *verb, char ***modes, char ***states);
int can_sleep(const char *verb);
int can_sleep_disk(char **types);
int can_sleep_state(char **types);

View File

@ -5090,59 +5090,6 @@ int getenv_for_pid(pid_t pid, const char *field, char **_value) {
return r;
}
int can_sleep(const char *type) {
char *w, *state;
size_t l, k;
int r;
_cleanup_free_ char *p = NULL;
assert(type);
/* 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;
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(const char *type) {
char *w, *state;
size_t l, k;
int r;
_cleanup_free_ char *p = NULL;
assert(type);
/* If /sys is read-only we cannot sleep */
if (access("/sys/power/state", W_OK) < 0 ||
access("/sys/power/disk", W_OK) < 0)
return false;
r = read_one_line_file("/sys/power/disk", &p);
if (r < 0)
return false;
k = strlen(type);
FOREACH_WORD_SEPARATOR(w, l, p, WHITESPACE, state) {
if (l == k && memcmp(w, type, l) == 0)
return true;
if (l == k + 2 && w[0] == '[' && memcmp(w + 1, type, l - 2) == 0 && w[l-1] == ']')
return true;
}
return false;
}
bool is_valid_documentation_url(const char *url) {
assert(url);

View File

@ -513,9 +513,6 @@ int setrlimit_closest(int resource, const struct rlimit *rlim);
int getenv_for_pid(pid_t pid, const char *field, char **_value);
int can_sleep(const char *type);
int can_sleep_disk(const char *type);
bool is_valid_documentation_url(const char *url) _pure_;
bool in_initrd(void);

View File

@ -4,6 +4,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
@ -22,107 +23,200 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <getopt.h>
#include "log.h"
#include "util.h"
#include "systemd/sd-id128.h"
#include "systemd/sd-messages.h"
#include "log.h"
#include "util.h"
#include "strv.h"
#include "fileio.h"
#include "build.h"
#include "sleep-config.h"
int main(int argc, char *argv[]) {
const char *verb;
static char* arg_verb = NULL;
static int write_mode(char **modes) {
int r = 0;
char **mode;
STRV_FOREACH(mode, modes) {
int k = write_string_file("/sys/power/disk", *mode);
if (k == 0)
return 0;
log_debug("Failed to write '%s' to /sys/power/disk: %s",
*mode, strerror(-k));
if (r == 0)
r = k;
}
if (r < 0)
log_error("Failed to write mode to /sys/power/disk: %s",
strerror(-r));
return r;
}
static int write_state(FILE *f0, char **states) {
FILE _cleanup_fclose_ *f = f0;
char **state;
int r = 0;
STRV_FOREACH(state, states) {
int k;
k = write_string_to_file(f, *state);
if (k == 0)
return 0;
log_debug("Failed to write '%s' to /sys/power/state: %s",
*state, strerror(-k));
if (r == 0)
r = k;
fclose(f);
f = fopen("/sys/power/state", "we");
if (!f) {
log_error("Failed to open /sys/power/state: %m");
return -errno;
}
}
return r;
}
static int execute(char **modes, char **states) {
char* arguments[4];
int r;
FILE *f;
const char* note = strappenda("SLEEP=", arg_verb);
/* This file is opened first, so that if we hit an error,
* we can abort before modyfing any state. */
f = fopen("/sys/power/state", "we");
if (!f) {
log_error("Failed to open /sys/power/state: %m");
return -errno;
}
/* Configure the hibernation mode */
r = write_mode(modes);
if (r < 0)
return r;
arguments[0] = NULL;
arguments[1] = (char*) "pre";
arguments[2] = arg_verb;
arguments[3] = NULL;
execute_directory(SYSTEM_SLEEP_PATH, NULL, arguments);
log_struct(LOG_INFO,
MESSAGE_ID(SD_MESSAGE_SLEEP_START),
"MESSAGE=Suspending system...",
note,
NULL);
r = write_state(f, states);
if (r < 0)
return r;
log_struct(LOG_INFO,
MESSAGE_ID(SD_MESSAGE_SLEEP_STOP),
"MESSAGE=System resumed.",
note,
NULL);
arguments[1] = (char*) "post";
execute_directory(SYSTEM_SLEEP_PATH, NULL, arguments);
return r;
}
static int help(void) {
printf("%s COMMAND\n\n"
"Suspend the system, hibernate the system, or both.\n\n"
"Commands:\n"
" -h --help Show this help and exit\n"
" --version Print version string and exit\n"
" suspend Suspend the system\n"
" hibernate Hibernate the system\n"
" hybrid-sleep Both hibernate and suspend the system\n"
, program_invocation_short_name
);
return 0;
}
static int parse_argv(int argc, char *argv[]) {
enum {
ARG_VERSION = 0x100,
};
static const struct option options[] = {
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, ARG_VERSION },
{ NULL, 0, NULL, 0 }
};
int c;
assert(argc >= 0);
assert(argv);
while ((c = getopt_long(argc, argv, "+h", options, NULL)) >= 0)
switch(c) {
case 'h':
help();
return 0 /* done */;
case ARG_VERSION:
puts(PACKAGE_STRING);
puts(SYSTEMD_FEATURES);
return 0 /* done */;
case '?':
return -EINVAL;
default:
log_error("Unknown option code %c", c);
return -EINVAL;
}
if (argc - optind != 1) {
log_error("Usage: %s COMMAND",
program_invocation_short_name);
return -EINVAL;
}
arg_verb = argv[optind];
if (!streq(arg_verb, "suspend") &&
!streq(arg_verb, "hibernate") &&
!streq(arg_verb, "hybrid-sleep")) {
log_error("Unknown command '%s'.", arg_verb);
return -EINVAL;
}
return 1 /* work to do */;
}
int main(int argc, char *argv[]) {
_cleanup_strv_free_ char **modes = NULL, **states = NULL;
int r;
log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
if (argc != 2) {
log_error("Invalid number of arguments.");
r = -EINVAL;
r = parse_argv(argc, argv);
if (r <= 0)
goto finish;
}
if (streq(argv[1], "suspend"))
verb = "mem";
else if (streq(argv[1], "hibernate") || streq(argv[1], "hybrid-sleep"))
verb = "disk";
else {
log_error("Unknown action '%s'.", argv[1]);
r = -EINVAL;
r = parse_sleep_config(arg_verb, &modes, &states);
if (r < 0)
goto finish;
}
/* Configure the hibernation mode */
if (streq(argv[1], "hibernate")) {
if (write_string_file("/sys/power/disk", "platform") < 0)
write_string_file("/sys/power/disk", "shutdown");
} else if (streq(argv[1], "hybrid-sleep")) {
if (write_string_file("/sys/power/disk", "suspend") < 0)
if (write_string_file("/sys/power/disk", "platform") < 0)
write_string_file("/sys/power/disk", "shutdown");
}
f = fopen("/sys/power/state", "we");
if (!f) {
log_error("Failed to open /sys/power/state: %m");
r = -errno;
goto finish;
}
arguments[0] = NULL;
arguments[1] = (char*) "pre";
arguments[2] = argv[1];
arguments[3] = NULL;
execute_directory(SYSTEM_SLEEP_PATH, NULL, arguments);
if (streq(argv[1], "suspend"))
log_struct(LOG_INFO,
MESSAGE_ID(SD_MESSAGE_SLEEP_START),
"MESSAGE=Suspending system...",
"SLEEP=suspend",
NULL);
else if (streq(argv[1], "hibernate"))
log_struct(LOG_INFO,
MESSAGE_ID(SD_MESSAGE_SLEEP_START),
"MESSAGE=Hibernating system...",
"SLEEP=hibernate",
NULL);
else
log_struct(LOG_INFO,
MESSAGE_ID(SD_MESSAGE_SLEEP_START),
"MESSAGE=Hibernating and suspending system...",
"SLEEP=hybrid-sleep",
NULL);
fputs(verb, f);
fputc('\n', f);
fflush(f);
r = ferror(f) ? -errno : 0;
if (streq(argv[1], "suspend"))
log_struct(LOG_INFO,
MESSAGE_ID(SD_MESSAGE_SLEEP_STOP),
"MESSAGE=System resumed.",
"SLEEP=suspend",
NULL);
else
log_struct(LOG_INFO,
MESSAGE_ID(SD_MESSAGE_SLEEP_STOP),
"MESSAGE=System thawed.",
"SLEEP=hibernate",
NULL);
arguments[1] = (char*) "post";
execute_directory(SYSTEM_SLEEP_PATH, NULL, arguments);
fclose(f);
r = execute(modes, states);
finish:
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}

View File

@ -26,14 +26,32 @@
#include "util.h"
#include "log.h"
#include "sleep-config.h"
#include "strv.h"
int main(int argc, char* argv[]) {
log_info("Can Suspend: %s", yes_no(can_sleep("mem") > 0));
log_info("Can Hibernate: %s", yes_no(can_sleep("disk") > 0));
log_info("Can Hibernate+Suspend (Hybrid-Sleep): %s", yes_no(can_sleep_disk("suspend") > 0));
log_info("Can Hibernate+Reboot: %s", yes_no(can_sleep_disk("reboot") > 0));
log_info("Can Hibernate+Platform: %s", yes_no(can_sleep_disk("platform") > 0));
log_info("Can Hibernate+Shutdown: %s", yes_no(can_sleep_disk("shutdown") > 0));
_cleanup_strv_free_ char
**standby = strv_new("standby", NULL),
**mem = strv_new("mem", NULL),
**disk = strv_new("disk", NULL),
**suspend = strv_new("suspend", NULL),
**reboot = strv_new("reboot", NULL),
**platform = strv_new("platform", NULL),
**shutdown = strv_new("shutdown", NULL),
**freez = strv_new("freeze", NULL);
log_info("Can Standby: %s", yes_no(can_sleep_state(standby) > 0));
log_info("Can Suspend: %s", yes_no(can_sleep_state(mem) > 0));
log_info("Can Hibernate: %s", yes_no(can_sleep_state(disk) > 0));
log_info("Can Hibernate+Suspend (Hybrid-Sleep): %s", yes_no(can_sleep_disk(suspend) > 0));
log_info("Can Hibernate+Reboot: %s", yes_no(can_sleep_disk(reboot) > 0));
log_info("Can Hibernate+Platform: %s", yes_no(can_sleep_disk(platform) > 0));
log_info("Can Hibernate+Shutdown: %s", yes_no(can_sleep_disk(shutdown) > 0));
log_info("Can Freeze: %s", yes_no(can_sleep_disk(freez) > 0));
log_info("Suspend configured and possible: %s", yes_no(can_sleep("suspend") > 0));
log_info("Hibernation configured and possible: %s", yes_no(can_sleep("hibernate") > 0));
log_info("Hybrid-sleep configured and possible: %s", yes_no(can_sleep("hybrid-sleep") > 0));
return 0;
}