]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared/sleep-config: return a custom message when not enough swap for hibernation
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 11 Apr 2018 06:51:06 +0000 (08:51 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 11 Apr 2018 07:26:14 +0000 (09:26 +0200)
$ sudo swapoff -av
swapoff /dev/vda4
$ sudo systemctl hibernate
Failed to hibernate system via logind: Not enough swap space for hibernation

Fixes #6729.

src/login/logind-dbus.c
src/shared/sleep-config.c
src/test/test-sleep.c

index acf05601a21397f3d13cd6de94c268dd4ffed9f0..83f1aa76b9e814dacada3fda6ab240e2e11f4cc2 100644 (file)
@@ -1835,11 +1835,14 @@ static int method_do_shutdown_or_sleep(
 
         if (sleep_verb) {
                 r = can_sleep(sleep_verb);
+                if (r == -ENOSPC)
+                        return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
+                                                "Not enough swap space for hibernation");
+                if (r == 0)
+                        return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
+                                                 "Sleep verb \"%s\" not supported", sleep_verb);
                 if (r < 0)
                         return r;
-
-                if (r == 0)
-                        return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Sleep verb not supported");
         }
 
         r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
@@ -2262,10 +2265,10 @@ static int method_can_shutdown_or_sleep(
 
         if (sleep_verb) {
                 r = can_sleep(sleep_verb);
+                if (IN_SET(r,  0, -ENOSPC))
+                        return sd_bus_reply_method_return(message, "s", "na");
                 if (r < 0)
                         return r;
-                if (r == 0)
-                        return sd_bus_reply_method_return(message, "s", "na");
         }
 
         r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
index 03fd497f527b2136063b2570952fb1dbfe247db8..1b684b90cb1443c425396ee57b3e9be6b6d8e50c 100644 (file)
@@ -369,12 +369,12 @@ static bool can_s2h(void) {
 
         FOREACH_STRING(p, "suspend", "hibernate") {
                 r = can_sleep(p);
-                if (r < 0)
-                        return log_debug_errno(r, "Failed to check if %s is possible: %m", p);
-                if (r == 0) {
+                if (IN_SET(r, 0, -ENOSPC)) {
                         log_debug("Unable to %s system.", p);
                         return false;
                 }
+                if (r < 0)
+                        return log_debug_errno(r, "Failed to check if %s is possible: %m", p);
         }
 
         return true;
@@ -396,5 +396,11 @@ int can_sleep(const char *verb) {
         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())
+                return -ENOSPC;
+
+        return true;
 }
index 05fd6c0abfa7aa2d91afaf9db6948641fd24205c..d9326c94741802609f6e9df82568a0660be4a0d6 100644 (file)
@@ -65,7 +65,9 @@ static void test_sleep(void) {
                 **platform = strv_new("platform", NULL),
                 **shutdown = strv_new("shutdown", NULL),
                 **freez = strv_new("freeze", NULL);
+        int r;
 
+        log_info("/* configuration */");
         log_info("Standby configured: %s", yes_no(can_sleep_state(standby) > 0));
         log_info("Suspend configured: %s", yes_no(can_sleep_state(mem) > 0));
         log_info("Hibernate configured: %s", yes_no(can_sleep_state(disk) > 0));
@@ -75,10 +77,15 @@ static void test_sleep(void) {
         log_info("Hibernate+Shutdown configured: %s", yes_no(can_sleep_disk(shutdown) > 0));
         log_info("Freeze configured: %s", yes_no(can_sleep_state(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));
-        log_info("Suspend-then-Hibernate configured and possible: %s", yes_no(can_sleep("suspend-then-hibernate") > 0));
+        log_info("/* running system */");
+        r = can_sleep("suspend");
+        log_info("Suspend configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r));
+        r = can_sleep("hibernate");
+        log_info("Hibernation configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r));
+        r = can_sleep("hybrid-sleep");
+        log_info("Hybrid-sleep configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r));
+        r = can_sleep("suspend-then-hibernate");
+        log_info("Suspend-then-Hibernate configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r));
 }
 
 int main(int argc, char* argv[]) {