diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2018-04-11 08:51:06 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2018-04-11 09:26:14 +0200 |
commit | b71c9758d131e5f2b38fcbc6393579a4aab3cb82 (patch) | |
tree | 367a46d547e5ea6f16b9aa0e1f79469688557cf9 | |
parent | logind: refuse operations if the target unit is masked or unavailable (diff) | |
download | systemd-b71c9758d131e5f2b38fcbc6393579a4aab3cb82.tar.xz systemd-b71c9758d131e5f2b38fcbc6393579a4aab3cb82.zip |
shared/sleep-config: return a custom message when not enough swap for hibernation
$ sudo swapoff -av
swapoff /dev/vda4
$ sudo systemctl hibernate
Failed to hibernate system via logind: Not enough swap space for hibernation
Fixes #6729.
-rw-r--r-- | src/login/logind-dbus.c | 13 | ||||
-rw-r--r-- | src/shared/sleep-config.c | 14 | ||||
-rw-r--r-- | src/test/test-sleep.c | 15 |
3 files changed, 29 insertions, 13 deletions
diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index acf05601a2..83f1aa76b9 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -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); diff --git a/src/shared/sleep-config.c b/src/shared/sleep-config.c index 03fd497f52..1b684b90cb 100644 --- a/src/shared/sleep-config.c +++ b/src/shared/sleep-config.c @@ -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; } diff --git a/src/test/test-sleep.c b/src/test/test-sleep.c index 05fd6c0abf..d9326c9474 100644 --- a/src/test/test-sleep.c +++ b/src/test/test-sleep.c @@ -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[]) { |