[systemd-devel] [PATCH] systemd-sleep: add support for freeze and standby

Zbigniew Jędrzejewski-Szmek zbyszek at in.waw.pl
Wed May 1 05:53:46 PDT 2013


A new section [Sleep] is added in /etc/systemd/system.conf
and 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 in 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
---
 Makefile.am                     |   2 +
 TODO                            |   3 -
 man/systemd-suspend.service.xml |  94 +++++++++++++++-
 man/systemd-system.conf.xml     |  53 ++++++++-
 man/systemd.xml                 |   2 +-
 src/core/main.c                 |   3 +-
 src/login/logind-dbus.c         |  56 +++------
 src/shared/sleep-settings.c     | 182 ++++++++++++++++++++++++++++++
 src/shared/sleep-settings.h     |  26 +++++
 src/shared/util.c               |  53 ---------
 src/shared/util.h               |   3 -
 src/sleep/sleep.c               | 243 +++++++++++++++++++++++++++-------------
 src/test/test-sleep.c           |  28 ++++-
 13 files changed, 560 insertions(+), 188 deletions(-)
 create mode 100644 src/shared/sleep-settings.c
 create mode 100644 src/shared/sleep-settings.h

diff --git a/Makefile.am b/Makefile.am
index 9e0f5fb..8fc155d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -640,6 +640,8 @@ libsystemd_shared_la_SOURCES = \
 	src/shared/fdset.h \
 	src/shared/prioq.c \
 	src/shared/prioq.h \
+	src/shared/sleep-settings.c \
+	src/shared/sleep-settings.h \
 	src/shared/strv.c \
 	src/shared/strv.h \
 	src/shared/env-util.c \
diff --git a/TODO b/TODO
index 052ec13..7ccbf1b 100644
--- a/TODO
+++ b/TODO
@@ -196,9 +196,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.
 
diff --git a/man/systemd-suspend.service.xml b/man/systemd-suspend.service.xml
index 9b8bad4..08172f7 100644
--- a/man/systemd-suspend.service.xml
+++ b/man/systemd-suspend.service.xml
@@ -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
@@ -60,10 +61,54 @@
         <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><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 +133,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 +145,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 +153,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/system.conf</filename>.
+                See <citerefentry><refentrytitle>systemd-system.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-system.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>,
diff --git a/man/systemd-system.conf.xml b/man/systemd-system.conf.xml
index d8bfd12..b724df3 100644
--- a/man/systemd-system.conf.xml
+++ b/man/systemd-system.conf.xml
@@ -67,7 +67,7 @@
         <refsect1>
                 <title>Options</title>
 
-                <para>All options are configured in the
+                <para>The following options can be configured in the
                 <literal>[Manager]</literal> section:</para>
 
                 <variablelist class='systemd-directives'>
@@ -296,6 +296,57 @@
                                 itself.</para></listitem>
                         </varlistentry>
                 </variablelist>
+
+                <para>The following options can be configured in the
+                <literal>[Sleep]</literal> section of
+                <filename>/etc/systemd/system.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,
+                                <filename>systemd-suspend.service</filename>,
+                                <filename>systemd-hibernate.service</filename>, or
+                                <filename>systemd-hybrid-sleep.service</filename>.
+                                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,
+                                <filename>systemd-suspend.service</filename>,
+                                <filename>systemd-hibernate.service</filename>, or
+                                <filename>systemd-hybrid-sleep.service</filename>.
+                                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>
+
+                                <para>Example: to exploit the "freeze" mode added
+                                in Linux 3.9, one can use <command>systemctl suspend</command>
+                                with
+                                <programlisting>
+[Sleep]
+SuspendState=freeze
+                                </programlisting></para>
+                                </listitem>
+                        </varlistentry>
+                </variablelist>
         </refsect1>
 
         <refsect1>
diff --git a/man/systemd.xml b/man/systemd.xml
index cd38c16..d009ed8 100644
--- a/man/systemd.xml
+++ b/man/systemd.xml
@@ -508,7 +508,7 @@
         <refsect1>
                 <title>Directories</title>
 
-                <variablelist class='unit-directives'>
+                <variablelist>
                         <varlistentry>
                                 <term>System unit directories</term>
 
diff --git a/src/core/main.c b/src/core/main.c
index 1748722..652e9a5 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -678,7 +678,8 @@ static int parse_config_file(void) {
                 return 0;
         }
 
-        r = config_parse(NULL, fn, f, "Manager\0", NULL,
+        r = config_parse(NULL, fn, f, "Manager\0",
+                         arg_running_as == SYSTEMD_SYSTEM ? "Sleep\0" : NULL,
                          config_item_table_lookup, (void*) items, false, false, NULL);
         if (r < 0)
                 log_warning("Failed to parse configuration file: %s", strerror(-r));
diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c
index 68e499f..92fbc39 100644
--- a/src/login/logind-dbus.c
+++ b/src/login/logind-dbus.c
@@ -31,6 +31,7 @@
 #include "path-util.h"
 #include "polkit.h"
 #include "special.h"
+#include "sleep-settings.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);
diff --git a/src/shared/sleep-settings.c b/src/shared/sleep-settings.c
new file mode 100644
index 0000000..e569849
--- /dev/null
+++ b/src/shared/sleep-settings.c
@@ -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 "fileio.h"
+#include "log.h"
+#include "strv.h"
+#include "util.h"
+#include "sleep-settings.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(SYSTEM_CONFIG_FILE, "re");
+        if (!f) {
+                if (errno == ENOENT)
+                        return 0;
+
+                log_warning("Failed to open configuration file '" SYSTEM_CONFIG_FILE "': %m");
+                return 0;
+        }
+
+        r = config_parse(NULL, SYSTEM_CONFIG_FILE, f, "Sleep\0", "Manager\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")) {
+                if (suspend_mode)
+                        *modes = suspend_mode;
+                else
+                        *modes = strv_split_nulstr("\0");
+
+                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;
+
+        assert(types);
+
+        /* 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;
+
+        assert(types);
+
+        /* 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);
+}
diff --git a/src/shared/sleep-settings.h b/src/shared/sleep-settings.h
new file mode 100644
index 0000000..51d2dec
--- /dev/null
+++ b/src/shared/sleep-settings.h
@@ -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);
diff --git a/src/shared/util.c b/src/shared/util.c
index f9ec14a..b1ef637 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -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);
 
diff --git a/src/shared/util.h b/src/shared/util.h
index 5d1b0b1..d7e05d9 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -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);
 
 bool in_initrd(void);
diff --git a/src/sleep/sleep.c b/src/sleep/sleep.c
index f5e78c1..eed4e50 100644
--- a/src/sleep/sleep.c
+++ b/src/sleep/sleep.c
@@ -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,199 @@
 #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"
+
+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;
+        }
 
-int main(int argc, char *argv[]) {
-        const char *verb;
-        char* arguments[4];
-        int r;
-        FILE *f;
+        if (r < 0)
+                log_error("Failed to write mode to /sys/power/disk: %s",
+                          strerror(-r));
 
-        log_set_target(LOG_TARGET_AUTO);
-        log_parse_environment();
-        log_open();
+        return r;
+}
 
-        if (argc != 2) {
-                log_error("Invalid number of arguments.");
-                r = -EINVAL;
-                goto finish;
+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;
+                }
         }
 
-        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;
-                goto finish;
-        }
+        return r;
+}
 
-        /* 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");
-        }
+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");
-                r = -errno;
-                goto finish;
+                return -errno;
         }
 
+        /* Configure the hibernation mode */
+        r = write_mode(modes);
+        if (r < 0)
+                return r;
+
         arguments[0] = NULL;
         arguments[1] = (char*) "pre";
-        arguments[2] = argv[1];
+        arguments[2] = arg_verb;
         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);
+        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);
 
-        fclose(f);
+        return r;
+}
 
-finish:
+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;
+}
 
-        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
+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();
+
+        r = parse_argv(argc, argv);
+        if (r <= 0)
+                goto finish;
+
+        r = parse_sleep_config(arg_verb, &modes, &states);
+        if (r < 0)
+                goto finish;
+
+        r = execute(modes, states);
+
+finish:
+        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 }
diff --git a/src/test/test-sleep.c b/src/test/test-sleep.c
index 5a98ecd..048fcd4 100644
--- a/src/test/test-sleep.c
+++ b/src/test/test-sleep.c
@@ -26,14 +26,30 @@
 
 #include "util.h"
 #include "log.h"
+#include "sleep-settings.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
+                **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 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;
 }
-- 
1.8.2.562.g931e949



More information about the systemd-devel mailing list