chiark / gitweb /
sleep-config: replace USE() macro with TAKE_PTR() usage
authorLennart Poettering <lennart@poettering.net>
Thu, 22 Mar 2018 15:59:46 +0000 (16:59 +0100)
committerSven Eden <yamakuzure@gmx.net>
Fri, 24 Aug 2018 14:47:08 +0000 (16:47 +0200)
let's use the new generic macor instead of the locally defined, specific
one.

src/shared/sleep-config.c

index a22c86db75c872791d480978544fa3950651746d..1f7792ae5b3098508bf5dacb004a623b0a31cd84 100644 (file)
@@ -3,6 +3,7 @@
   This file is part of systemd.
 
   Copyright 2013 Zbigniew JÄ™drzejewski-Szmek
+  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
 #include "strv.h"
 
 #if 0 /// UNNEEDED by elogind
-#define USE(x, y) do { (x) = (y); (y) = NULL; } while (0)
-
-int parse_sleep_config(const char *verb, char ***_modes, char ***_states) {
+int parse_sleep_config(const char *verb, char ***_modes, char ***_states, usec_t *_delay) {
 
         _cleanup_strv_free_ char
                 **suspend_mode = NULL, **suspend_state = NULL,
                 **hibernate_mode = NULL, **hibernate_state = NULL,
                 **hybrid_mode = NULL, **hybrid_state = NULL;
         char **modes, **states;
+        usec_t delay = 180 * USEC_PER_MINUTE;
 
         const ConfigTableItem items[] = {
                 { "Sleep",   "SuspendMode",      config_parse_strv,  0, &suspend_mode  },
@@ -57,6 +57,7 @@ int parse_sleep_config(const char *verb, char ***_modes, char ***_states) {
                 { "Sleep",   "HibernateState",   config_parse_strv,  0, &hibernate_state },
                 { "Sleep",   "HybridSleepMode",  config_parse_strv,  0, &hybrid_mode  },
                 { "Sleep",   "HybridSleepState", config_parse_strv,  0, &hybrid_state },
+                { "Sleep",   "HibernateDelaySec", config_parse_sec,  0, &delay},
                 {}
         };
 
@@ -67,46 +68,54 @@ int parse_sleep_config(const char *verb, char ***_modes, char ***_states) {
 
         if (streq(verb, "suspend")) {
                 /* empty by default */
-                USE(modes, suspend_mode);
+                modes = TAKE_PTR(suspend_mode);
 
                 if (suspend_state)
-                        USE(states, suspend_state);
+                        states = TAKE_PTR(suspend_state);
                 else
                         states = strv_new("mem", "standby", "freeze", NULL);
 
         } else if (streq(verb, "hibernate")) {
                 if (hibernate_mode)
-                        USE(modes, hibernate_mode);
+                        modes = TAKE_PTR(hibernate_mode);
                 else
                         modes = strv_new("platform", "shutdown", NULL);
 
                 if (hibernate_state)
-                        USE(states, hibernate_state);
+                        states = TAKE_PTR(hibernate_state);
                 else
                         states = strv_new("disk", NULL);
 
         } else if (streq(verb, "hybrid-sleep")) {
                 if (hybrid_mode)
-                        USE(modes, hybrid_mode);
+                        modes = TAKE_PTR(hybrid_mode);
                 else
                         modes = strv_new("suspend", "platform", "shutdown", NULL);
 
                 if (hybrid_state)
-                        USE(states, hybrid_state);
+                        states = TAKE_PTR(hybrid_state);
                 else
                         states = strv_new("disk", NULL);
 
-        } else
+        } else if (streq(verb, "suspend-to-hibernate"))
+                modes = states = NULL;
+        else
                 assert_not_reached("what verb");
 
-        if ((!modes && !streq(verb, "suspend")) || !states) {
+        if ((!modes && STR_IN_SET(verb, "hibernate", "hybrid-sleep")) ||
+            (!states && !streq(verb, "suspend-to-hibernate"))) {
                 strv_free(modes);
                 strv_free(states);
                 return log_oom();
         }
 
-        *_modes = modes;
-        *_states = states;
+        if (_modes)
+                *_modes = modes;
+        if (_states)
+                *_states = states;
+        if (_delay)
+                *_delay = delay;
+
         return 0;
 }
 #endif // 0
@@ -271,22 +280,29 @@ static bool enough_memory_for_hibernation(void) {
 }
 
 #if 0 /// elogind has to do, or better, *can* do it differently
-int can_sleep(const char *verb) {
-        _cleanup_strv_free_ char **modes = NULL, **states = NULL;
+static bool can_s2h(void) {
         int r;
 
-        assert(streq(verb, "suspend") ||
-               streq(verb, "hibernate") ||
-               streq(verb, "hybrid-sleep"));
+        r = access("/sys/class/rtc/rtc0/wakealarm", W_OK);
+        if (r < 0) {
+                log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING,
+                         "/sys/class/rct/rct0/wakealarm is not writable %m");
+                return false;
+        }
 
-        r = parse_sleep_config(verb, &modes, &states);
-        if (r < 0)
+        r = can_sleep("suspend");
+        if (r < 0) {
+                log_debug_errno(r, "Unable to suspend system.");
                 return false;
+        }
 
-        if (!can_sleep_state(states) || !can_sleep_disk(modes))
+        r = can_sleep("hibernate");
+        if (r < 0) {
+                log_debug_errno(r, "Unable to hibernate system.");
                 return false;
+        }
 
-        return streq(verb, "suspend") || enough_memory_for_hibernation();
+        return true;
 }
 #else
 int can_sleep(Manager *m, const char *verb) {
@@ -294,22 +310,32 @@ int can_sleep(Manager *m, const char *verb) {
         assert(streq(verb, "suspend") ||
                streq(verb, "hibernate") ||
                streq(verb, "hybrid-sleep"));
+int can_sleep(const char *verb) {
+        _cleanup_strv_free_ char **modes = NULL, **states = NULL;
+        int r;
 
         if ( streq(verb, "suspend")
           && ( !can_sleep_state(m->suspend_state)
             || !can_sleep_disk(m->suspend_mode) ) )
                 return false;
+        assert(STR_IN_SET(verb, "suspend", "hibernate", "hybrid-sleep", "suspend-to-hibernate"));
 
         if ( streq(verb, "hibernate")
           && ( !can_sleep_state(m->hibernate_state)
             || !can_sleep_disk(m->hibernate_mode) ) )
                 return false;
+        if (streq(verb, "suspend-to-hibernate"))
+                return can_s2h();
 
         if ( streq(verb, "hybrid-sleep")
           && ( !can_sleep_state(m->hybrid_sleep_state)
             || !can_sleep_disk(m->hybrid_sleep_mode) ) )
+        r = parse_sleep_config(verb, &modes, &states, NULL);
+        if (r < 0)
                 return false;
 
+        if (!can_sleep_state(states) || !can_sleep_disk(modes))
+                return false;
 
         return streq(verb, "suspend") || enough_memory_for_hibernation();
 }