From b0e3b85a7c8a2b9a1a977caa3c47aaae3defc288 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Wed, 27 Mar 2024 00:24:58 +0800 Subject: [PATCH] sleep-config: make sleep_mode_supported take a path Preparation for later commits. --- src/shared/sleep-config.c | 25 ++++++++++++++----------- src/shared/sleep-config.h | 4 ++-- src/test/test-sleep-config.c | 8 ++++---- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/shared/sleep-config.c b/src/shared/sleep-config.c index f99c38bc79a..be20f0aae26 100644 --- a/src/shared/sleep-config.c +++ b/src/shared/sleep-config.c @@ -183,7 +183,7 @@ int parse_sleep_config(SleepConfig **ret) { return 0; } -int sleep_state_supported(char **states) { +int sleep_state_supported(char * const *states) { _cleanup_free_ char *supported_sysfs = NULL; const char *found; int r; @@ -213,22 +213,24 @@ int sleep_state_supported(char **states) { return false; } -int sleep_mode_supported(char **modes) { +int sleep_mode_supported(const char *path, char * const *modes) { _cleanup_free_ char *supported_sysfs = NULL; int r; + assert(path); + /* Unlike state, kernel has its own default choice if not configured */ if (strv_isempty(modes)) { - log_debug("No sleep mode configured, using kernel default."); + log_debug("No sleep mode configured, using kernel default for %s.", path); return true; } - if (access("/sys/power/disk", W_OK) < 0) - return log_debug_errno(errno, "/sys/power/disk is not writable: %m"); + if (access(path, W_OK) < 0) + return log_debug_errno(errno, "%s is not writable: %m", path); - r = read_one_line_file("/sys/power/disk", &supported_sysfs); + r = read_one_line_file(path, &supported_sysfs); if (r < 0) - return log_debug_errno(r, "Failed to read /sys/power/disk: %m"); + return log_debug_errno(r, "Failed to read %s: %m", path); for (const char *p = supported_sysfs;;) { _cleanup_free_ char *word = NULL; @@ -237,7 +239,7 @@ int sleep_mode_supported(char **modes) { r = extract_first_word(&p, &word, NULL, 0); if (r < 0) - return log_debug_errno(r, "Failed to parse /sys/power/disk: %m"); + return log_debug_errno(r, "Failed to parse %s: %m", path); if (r == 0) break; @@ -250,14 +252,15 @@ int sleep_mode_supported(char **modes) { } if (strv_contains(modes, mode)) { - log_debug("Disk sleep mode '%s' is supported by kernel.", mode); + log_debug("Sleep mode '%s' is supported by kernel (%s).", mode, path); return true; } } if (DEBUG_LOGGING) { _cleanup_free_ char *joined = strv_join(modes, " "); - log_debug("None of the configured hibernation power modes are supported by kernel: %s", strnull(joined)); + log_debug("None of the configured modes are supported by kernel (%s): %s", + path, strnull(joined)); } return false; } @@ -342,7 +345,7 @@ static int sleep_supported_internal( } if (SLEEP_OPERATION_IS_HIBERNATION(operation)) { - r = sleep_mode_supported(sleep_config->modes[operation]); + r = sleep_mode_supported("/sys/power/disk", sleep_config->modes[operation]); if (r < 0) return r; if (r == 0) { diff --git a/src/shared/sleep-config.h b/src/shared/sleep-config.h index 93811c63a21..12239a8a019 100644 --- a/src/shared/sleep-config.h +++ b/src/shared/sleep-config.h @@ -55,5 +55,5 @@ static inline int sleep_supported(SleepOperation operation) { } /* Only for test-sleep-config */ -int sleep_state_supported(char **states); -int sleep_mode_supported(char **modes); +int sleep_state_supported(char * const *states); +int sleep_mode_supported(const char *path, char * const *modes); diff --git a/src/test/test-sleep-config.c b/src/test/test-sleep-config.c index 112fec63461..dc1a8fb71cc 100644 --- a/src/test/test-sleep-config.c +++ b/src/test/test-sleep-config.c @@ -52,10 +52,10 @@ TEST(sleep_supported) { log_info("Standby configured: %s", yes_no(sleep_state_supported(standby) > 0)); log_info("Suspend configured: %s", yes_no(sleep_state_supported(mem) > 0)); log_info("Hibernate configured: %s", yes_no(sleep_state_supported(disk) > 0)); - log_info("Hibernate+Suspend (Hybrid-Sleep) configured: %s", yes_no(sleep_mode_supported(suspend) > 0)); - log_info("Hibernate+Reboot configured: %s", yes_no(sleep_mode_supported(reboot) > 0)); - log_info("Hibernate+Platform configured: %s", yes_no(sleep_mode_supported(platform) > 0)); - log_info("Hibernate+Shutdown configured: %s", yes_no(sleep_mode_supported(shutdown) > 0)); + log_info("Hibernate+Suspend (Hybrid-Sleep) configured: %s", yes_no(sleep_mode_supported("/sys/power/disk", suspend) > 0)); + log_info("Hibernate+Reboot configured: %s", yes_no(sleep_mode_supported("/sys/power/disk", reboot) > 0)); + log_info("Hibernate+Platform configured: %s", yes_no(sleep_mode_supported("/sys/power/disk", platform) > 0)); + log_info("Hibernate+Shutdown configured: %s", yes_no(sleep_mode_supported("/sys/power/disk", shutdown) > 0)); log_info("Freeze configured: %s", yes_no(sleep_state_supported(freeze) > 0)); log_info("/= high-level sleep verbs =/"); -- 2.47.3