chiark / gitweb /
Prep v239: A few cosmetic upgrades
[elogind.git] / src / shared / sleep-config.c
index 28d19c59b70e201cd754aac08dcc672e081c6c8c..77b380ca3240dc11ed90d19da48cbf1abc0bd32f 100644 (file)
@@ -1,26 +1,10 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 /***
-  This file is part of systemd.
-
-  Copyright 2013 Zbigniew Jędrzejewski-Szmek
-  Copyright 2018 Dell Inc.
-
-  elogind 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.
-
-  elogind 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 elogind; If not, see <http://www.gnu.org/licenses/>.
+  Copyright © 2018 Dell Inc.
 ***/
 
 //#include <errno.h>
-//#include <linux/fs.h>
+#include <linux/fs.h>
 //#include <stdbool.h>
 //#include <stddef.h>
 //#include <stdio.h>
@@ -48,7 +32,7 @@ int parse_sleep_config(const char *verb, char ***_modes, char ***_states, usec_t
                 **suspend_mode = NULL, **suspend_state = NULL,
                 **hibernate_mode = NULL, **hibernate_state = NULL,
                 **hybrid_mode = NULL, **hybrid_state = NULL;
-        char **modes, **states;
+        _cleanup_strv_free_ char **modes, **states; /* always initialized below */
         usec_t delay = 180 * USEC_PER_MINUTE;
 
         const ConfigTableItem items[] = {
@@ -104,16 +88,13 @@ int parse_sleep_config(const char *verb, char ***_modes, char ***_states, usec_t
                 assert_not_reached("what verb");
 
         if ((!modes && STR_IN_SET(verb, "hibernate", "hybrid-sleep")) ||
-            (!states && !streq(verb, "suspend-then-hibernate"))) {
-                strv_free(modes);
-                strv_free(states);
+            (!states && !streq(verb, "suspend-then-hibernate")))
                 return log_oom();
-        }
 
         if (_modes)
-                *_modes = modes;
+                *_modes = TAKE_PTR(modes);
         if (_states)
-                *_states = states;
+                *_states = TAKE_PTR(states);
         if (_delay)
                 *_delay = delay;
 
@@ -247,7 +228,7 @@ int find_hibernate_location(char **device, char **type, size_t *size, size_t *us
         return -ENOSYS;
 }
 
-static bool enough_memory_for_hibernation(void) {
+static bool enough_swap_for_hibernation(void) {
         _cleanup_free_ char *active = NULL;
         unsigned long long act = 0;
         size_t size = 0, used = 0;
@@ -366,8 +347,11 @@ int read_fiemap(int fd, struct fiemap **ret) {
         return 0;
 }
 
-#if 0 /// elogind has to do, or better, *can* do it differently
+#if 0 /// elogind has to ask the manager for some stuff
 static bool can_s2h(void) {
+#else
+static bool can_s2h(Manager *m) {
+#endif // 0
         const char *p;
         int r;
 
@@ -379,7 +363,11 @@ static bool can_s2h(void) {
         }
 
         FOREACH_STRING(p, "suspend", "hibernate") {
+#if 0 /// elogind must transport a pointer to its managers instance
                 r = can_sleep(p);
+#else
+                r = can_sleep(m, p);
+#endif // 0
                 if (IN_SET(r, 0, -ENOSPC)) {
                         log_debug("Unable to %s system.", p);
                         return false;
@@ -390,48 +378,47 @@ static bool can_s2h(void) {
 
         return true;
 }
-#else
-int can_sleep(Manager *m, const char *verb) {
 
-        assert(streq(verb, "suspend") ||
-               streq(verb, "hibernate") ||
-               streq(verb, "hybrid-sleep"));
+
+#if 0 /// elogind has to ask the manager for some stuff
 int can_sleep(const char *verb) {
         _cleanup_strv_free_ char **modes = NULL, **states = NULL;
         int r;
+#else
+int can_sleep(Manager *m, const char *verb) {
+        assert(m);
+
+        char **modes  = streq(verb, "suspend")   ? m->suspend_mode     :
+                        streq(verb, "hibernate") ? m->hibernate_mode   :
+                                                       m->hybrid_sleep_mode;
+        char **states = streq(verb, "suspend")   ? m->suspend_state     :
+                        streq(verb, "hibernate") ? m->hibernate_state   :
+                                                   m->hybrid_sleep_state;
+#endif // 0
 
-        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"));
         assert(STR_IN_SET(verb, "suspend", "hibernate", "hybrid-sleep", "suspend-then-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"))
         if (streq(verb, "suspend-then-hibernate"))
+#if 0 /// elogind must transport a pointer to its managers instance
                 return can_s2h();
+#else
+                return can_s2h(m);
+#endif // 0
 
-        if ( streq(verb, "hybrid-sleep")
-          && ( !can_sleep_state(m->hybrid_sleep_state)
-            || !can_sleep_disk(m->hybrid_sleep_mode) ) )
+#if 0 /// already parsed by elogind config
         r = parse_sleep_config(verb, &modes, &states, NULL);
         if (r < 0)
                 return false;
+#endif // 0
 
         if (!can_sleep_state(states) || !can_sleep_disk(modes))
                 return false;
 
-        return streq(verb, "suspend") || enough_memory_for_hibernation();
         if (streq(verb, "suspend"))
                 return true;
 
-        if (!enough_memory_for_hibernation())
+        if (!enough_swap_for_hibernation())
                 return -ENOSPC;
 
         return true;
 }
-#endif // 0