From: Frederic Crozat Date: Tue, 19 Feb 2013 11:20:31 +0100 Subject: Forward suspend / hibernate calls to pm-utils forward suspend/hibernation calls to pm-utils, if installed (bnc#790157) --- src/sleep/sleep.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/sleep/sleep.c b/src/sleep/sleep.c index f5e78c1..8d91117 100644 --- a/src/sleep/sleep.c +++ b/src/sleep/sleep.c @@ -22,6 +22,7 @@ #include #include #include +#include #include "log.h" #include "util.h" @@ -32,6 +33,9 @@ int main(int argc, char *argv[]) { const char *verb; char* arguments[4]; + const char *pmtools; + bool delegate_to_pmutils = false; + struct stat buf; int r; FILE *f; @@ -45,17 +49,27 @@ int main(int argc, char *argv[]) { goto finish; } - if (streq(argv[1], "suspend")) + if (streq(argv[1], "suspend")) { verb = "mem"; - else if (streq(argv[1], "hibernate") || streq(argv[1], "hybrid-sleep")) + pmtools = "/usr/sbin/pm-suspend"; + } + else if (streq(argv[1], "hibernate") || streq(argv[1], "hybrid-sleep")) { verb = "disk"; + if (streq(argv[1], "hibernate")) + pmtools = "/usr/sbin/pm-hibernate"; + else + pmtools = "/usr/sbin/pm-suspend-hybrid"; + } else { log_error("Unknown action '%s'.", argv[1]); r = -EINVAL; goto finish; } + delegate_to_pmutils = (stat(pmtools, &buf) >= 0 && S_ISREG(buf.st_mode) && (buf.st_mode & 0111)); + /* Configure the hibernation mode */ + if (!delegate_to_pmutils) { if (streq(argv[1], "hibernate")) { if (write_string_file("/sys/power/disk", "platform") < 0) write_string_file("/sys/power/disk", "shutdown"); @@ -65,13 +79,14 @@ int main(int argc, char *argv[]) { write_string_file("/sys/power/disk", "shutdown"); } + f = fopen("/sys/power/state", "we"); if (!f) { log_error("Failed to open /sys/power/state: %m"); r = -errno; goto finish; } - + } arguments[0] = NULL; arguments[1] = (char*) "pre"; arguments[2] = argv[1]; @@ -97,11 +112,16 @@ int main(int argc, char *argv[]) { "SLEEP=hybrid-sleep", NULL); + if (delegate_to_pmutils) { + r = -system(pmtools); + } + else { fputs(verb, f); fputc('\n', f); fflush(f); r = ferror(f) ? -errno : 0; + } if (streq(argv[1], "suspend")) log_struct(LOG_INFO, @@ -119,6 +139,7 @@ int main(int argc, char *argv[]) { arguments[1] = (char*) "post"; execute_directory(SYSTEM_SLEEP_PATH, NULL, arguments); + if (!delegate_to_pmutils) fclose(f); finish: