chiark / gitweb /
Prep v239: Add support for the new 'suspend-then-hibernate' method.
[elogind.git] / src / sleep / sleep.c
index b958e5c6f9ab7c6c7a2bf222be6784d87062e614..fba7b9baf59d415a72b65ca46922609faf86012b 100644 (file)
@@ -1,42 +1,25 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 /***
-  This file is part of systemd.
-
-  Copyright 2012 Lennart Poettering
-  Copyright 2013 Zbigniew Jędrzejewski-Szmek
-  Copyright 2010-2017 Canonical
-  Copyright 2018 Dell Inc.
-
-  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/>.
+  Copyright © 2010-2017 Canonical
+  Copyright © 2018 Dell Inc.
 ***/
 
 //#include <errno.h>
 //#include <getopt.h>
-//#include <linux/fiemap.h>
+#include <linux/fiemap.h>
 //#include <stdio.h>
 
 #include "sd-messages.h"
 
-//#include "parse-util.h"
+#include "parse-util.h"
 #include "def.h"
 #include "exec-util.h"
 #include "fd-util.h"
 #include "fileio.h"
 //#include "log.h"
-//#include "sleep-config.h"
-//#include "stdio-util.h"
-//#include "string-util.h"
+#include "sleep-config.h"
+#include "stdio-util.h"
+#include "string-util.h"
 #include "strv.h"
 //#include "util.h"
 
@@ -123,9 +106,6 @@ static int write_mode(char **modes) {
                         r = k;
         }
 
-        if (r < 0)
-                log_error_errno(r, "Failed to write mode to /sys/power/disk: %m");
-
         return r;
 }
 
@@ -147,13 +127,28 @@ static int write_state(FILE **f, char **states) {
                 fclose(*f);
                 *f = fopen("/sys/power/state", "we");
                 if (!*f)
-                        return log_error_errno(errno, "Failed to open /sys/power/state: %m");
+                        return -errno;
         }
 
         return r;
 }
 
+#if 0 /// elogind uses the values stored in its manager instance
 static int execute(char **modes, char **states) {
+#else
+static int execute(Manager *m, const char *verb) {
+        assert(m);
+
+        if (verb)
+                arg_verb = (char*)verb;
+
+        char **modes  = streq(arg_verb, "suspend")   ? m->suspend_mode     :
+                        streq(arg_verb, "hibernate") ? m->hibernate_mode   :
+                                                       m->hybrid_sleep_mode;
+        char **states = streq(arg_verb, "suspend")   ? m->suspend_state     :
+                        streq(arg_verb, "hibernate") ? m->hibernate_state   :
+                                                       m->hybrid_sleep_state;
+#endif // 0
 
         char *arguments[] = {
                 NULL,
@@ -182,7 +177,7 @@ static int execute(char **modes, char **states) {
                         return log_error_errno(r, "Failed to write hibernation disk offset: %m");
                 r = write_mode(modes);
                 if (r < 0)
-                        return r;
+                        return log_error_errno(r, "Failed to write mode to /sys/power/disk: %m");;
         }
 
         execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments);
@@ -190,18 +185,16 @@ static int execute(char **modes, char **states) {
         log_struct(LOG_INFO,
                    "MESSAGE_ID=" SD_MESSAGE_SLEEP_START_STR,
                    LOG_MESSAGE("Suspending system..."),
-                   "SLEEP=%s", arg_verb,
-                   NULL);
+                   "SLEEP=%s", arg_verb);
 
         r = write_state(&f, states);
         if (r < 0)
-                return r;
+                return log_error_errno(r, "Failed to write /sys/power/state: %m");
 
         log_struct(LOG_INFO,
                    "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR,
                    LOG_MESSAGE("System resumed."),
-                   "SLEEP=%s", arg_verb,
-                   NULL);
+                   "SLEEP=%s", arg_verb);
 
         arguments[1] = (char*) "post";
         execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments);
@@ -233,14 +226,22 @@ static int write_wakealarm(const char *str) {
         return 0;
 }
 
+#if 0 /// elogind uses the values stored in its manager instance
 static int execute_s2h(usec_t hibernate_delay_sec) {
 
         _cleanup_strv_free_ char **hibernate_modes = NULL, **hibernate_states = NULL,
                                  **suspend_modes = NULL, **suspend_states = NULL;
+#else
+static int execute_s2h(Manager *m) {
+        assert(m);
+
+        usec_t hibernate_delay_sec = m->hibernate_delay_sec;
+#endif // 0
         usec_t orig_time, cmp_time;
         char time_str[DECIMAL_STR_MAX(uint64_t)];
         int r;
 
+#if 0 /// Already parsed by elogind config
         r = parse_sleep_config("suspend", &suspend_modes, &suspend_states,
                                NULL);
         if (r < 0)
@@ -250,6 +251,7 @@ static int execute_s2h(usec_t hibernate_delay_sec) {
                                &hibernate_states, NULL);
         if (r < 0)
                 return r;
+#endif // 0
 
         r = read_wakealarm(&orig_time);
         if (r < 0)
@@ -264,7 +266,11 @@ static int execute_s2h(usec_t hibernate_delay_sec) {
 
         log_debug("Set RTC wake alarm for %s", time_str);
 
+#if 0 /// elogind uses its manager instance values
         r = execute(suspend_modes, suspend_states);
+#else
+        r = execute(m, "suspend");
+#endif // 0
         if (r < 0)
                 return r;
 
@@ -281,7 +287,11 @@ static int execute_s2h(usec_t hibernate_delay_sec) {
 
         /* if woken up after alarm time, hibernate */
         if (cmp_time >= orig_time)
+#if 0 /// elogind uses its manager instance values
                 r = execute(hibernate_modes, hibernate_states);
+#else
+                r = execute(m, "hibernate");
+#endif // 0
 
         return r;
 }
@@ -373,13 +383,18 @@ int main(int argc, char *argv[]) {
                 r = execute_s2h(delay);
         else
                 r = execute(modes, states);
-finish:
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 }
 #else
-int do_sleep(const char *verb, char **modes, char **states) {
+int do_sleep(Manager *m, const char *verb) {
         assert(verb);
+        assert(m);
+
         arg_verb = (char*)verb;
-        return execute(modes, states);
+
+        if (streq(arg_verb, "suspend-then-hibernate"))
+                return execute_s2h(m);
+
+        return execute(m, NULL);
 }
 #endif // 0