diff --git a/src/shared/sleep-config.c b/src/shared/sleep-config.c index 81862c776d..6b9074ce2b 100644 --- a/src/shared/sleep-config.c +++ b/src/shared/sleep-config.c @@ -108,12 +108,16 @@ int can_sleep_state(char **types) { return true; /* If /sys is read-only we cannot sleep */ - if (access("/sys/power/state", W_OK) < 0) + if (access("/sys/power/state", W_OK) < 0) { + log_debug_errno(errno, "/sys/power/state is not writable, cannot sleep: %m"); return false; + } r = read_one_line_file("/sys/power/state", &p); - if (r < 0) + if (r < 0) { + log_debug_errno(r, "Failed to read /sys/power/state, cannot sleep: %m"); return false; + } STRV_FOREACH(type, types) { const char *word, *state; @@ -129,9 +133,9 @@ int can_sleep_state(char **types) { } int can_sleep_disk(char **types) { + _cleanup_free_ char *p = NULL; char **type; int r; - _cleanup_free_ char *p = NULL; if (strv_isempty(types)) return true; @@ -199,16 +203,17 @@ static int swap_device_to_device_id(const SwapEntry *swap, dev_t *ret_dev) { r = stat(swap->device, &sb); if (r < 0) - return r; + return -errno; if (streq(swap->type, "partition")) { if (!S_ISBLK(sb.st_mode)) return -ENOTBLK; + *ret_dev = sb.st_rdev; return 0; + } - } else - return get_block_device(swap->device, ret_dev); + return get_block_device(swap->device, ret_dev); } /* @@ -227,15 +232,15 @@ static int calculate_swap_file_offset(const SwapEntry *swap, uint64_t *ret_offse fd = open(swap->device, O_RDONLY|O_CLOEXEC|O_NOCTTY); if (fd < 0) - return log_error_errno(errno, "Failed to open swap file %s to determine on-disk offset: %m", swap->device); + return log_debug_errno(errno, "Failed to open swap file %s to determine on-disk offset: %m", swap->device); if (fstat(fd, &sb) < 0) - return log_error_errno(errno, "Failed to stat %s: %m", swap->device); + return log_debug_errno(errno, "Failed to stat %s: %m", swap->device); btrfs = btrfs_is_filesystem(fd); if (btrfs < 0) - return log_error_errno(btrfs, "Error checking %s for Btrfs filesystem: %m", swap->device); - else if (btrfs > 0) { + return log_debug_errno(btrfs, "Error checking %s for Btrfs filesystem: %m", swap->device); + if (btrfs > 0) { log_debug("%s: detection of swap file offset on Btrfs is not supported", swap->device); *ret_offset = UINT64_MAX; return 0; @@ -246,14 +251,13 @@ static int calculate_swap_file_offset(const SwapEntry *swap, uint64_t *ret_offse return log_debug_errno(r, "Unable to read extent map for '%s': %m", swap->device); *ret_offset = fiemap->fm_extents[0].fe_physical / page_size(); - return 0; } static int read_resume_files(dev_t *ret_resume, uint64_t *ret_resume_offset) { _cleanup_free_ char *resume_str = NULL, *resume_offset_str = NULL; - dev_t resume; uint64_t resume_offset = 0; + dev_t resume; int r; r = read_one_line_file("/sys/power/resume", &resume_str); @@ -266,13 +270,13 @@ static int read_resume_files(dev_t *ret_resume, uint64_t *ret_resume_offset) { r = read_one_line_file("/sys/power/resume_offset", &resume_offset_str); if (r == -ENOENT) - log_debug("Kernel does not support resume_offset; swap file offset detection will be skipped."); + log_debug_errno(r, "Kernel does not support resume_offset; swap file offset detection will be skipped."); else if (r < 0) return log_debug_errno(r, "Error reading /sys/power/resume_offset: %m"); else { r = safe_atou64(resume_offset_str, &resume_offset); if (r < 0) - return log_error_errno(r, "Failed to parse value in /sys/power/resume_offset \"%s\": %m", resume_offset_str); + return log_debug_errno(r, "Failed to parse value in /sys/power/resume_offset \"%s\": %m", resume_offset_str); } if (resume_offset > 0 && resume == 0) @@ -325,9 +329,8 @@ int find_hibernate_location(HibernateLocation **ret_hibernate_location) { f = fopen("/proc/swaps", "re"); if (!f) { - log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, - "Failed to open /proc/swaps: %m"); - return negative_errno(); + log_debug_errno(errno, "Failed to open /proc/swaps: %m"); + return errno == ENOENT ? -EOPNOTSUPP : -errno; /* Convert swap not supported to a recognizable error */ } (void) fscanf(f, "%*s %*s %*s %*s %*s\n"); @@ -338,7 +341,7 @@ int find_hibernate_location(HibernateLocation **ret_hibernate_location) { swap = new0(SwapEntry, 1); if (!swap) - return log_oom(); + return -ENOMEM; k = fscanf(f, "%ms " /* device/file */ @@ -350,13 +353,13 @@ int find_hibernate_location(HibernateLocation **ret_hibernate_location) { if (k == EOF) break; if (k != 5) { - log_warning("Failed to parse /proc/swaps:%u", i); + log_debug("Failed to parse /proc/swaps:%u, ignoring", i); continue; } if (streq(swap->type, "file")) { if (endswith(swap->device, "\\040(deleted)")) { - log_warning("Ignoring deleted swap file '%s'.", swap->device); + log_debug("Ignoring deleted swap file '%s'.", swap->device); continue; } @@ -393,12 +396,12 @@ int find_hibernate_location(HibernateLocation **ret_hibernate_location) { dev_t swap_device; r = swap_device_to_device_id(swap, &swap_device); if (r < 0) - return log_error_errno(r, "%s: failed to query device number: %m", swap->device); + return log_debug_errno(r, "%s: failed to query device number: %m", swap->device); hibernate_location = hibernate_location_free(hibernate_location); hibernate_location = new(HibernateLocation, 1); if (!hibernate_location) - return log_oom(); + return -ENOMEM; *hibernate_location = (HibernateLocation) { .devno = swap_device, @@ -585,8 +588,7 @@ static bool can_s2h(const SleepConfig *sleep_config) { int r; if (!clock_supported(CLOCK_BOOTTIME_ALARM)) { - log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, - "CLOCK_BOOTTIME_ALARM is not supported"); + log_debug("CLOCK_BOOTTIME_ALARM is not supported."); return false; } @@ -675,9 +677,9 @@ int sleep_settings(const char *verb, const SleepConfig *sleep_config, bool *ret_ return 0; } -void free_sleep_config(SleepConfig *sc) { +SleepConfig* free_sleep_config(SleepConfig *sc) { if (!sc) - return; + return NULL; strv_free(sc->suspend_modes); strv_free(sc->suspend_states); @@ -688,5 +690,5 @@ void free_sleep_config(SleepConfig *sc) { strv_free(sc->hybrid_modes); strv_free(sc->hybrid_states); - free(sc); + return mfree(sc); } diff --git a/src/shared/sleep-config.h b/src/shared/sleep-config.h index faa3c0519a..86dfbf19be 100644 --- a/src/shared/sleep-config.h +++ b/src/shared/sleep-config.h @@ -20,7 +20,7 @@ typedef struct SleepConfig { usec_t hibernate_delay_sec; /* HibernateDelaySec */ } SleepConfig; -void free_sleep_config(SleepConfig *sc); +SleepConfig* free_sleep_config(SleepConfig *sc); DEFINE_TRIVIAL_CLEANUP_FUNC(SleepConfig*, free_sleep_config); /* entry in /proc/swaps */ diff --git a/src/sleep/sleep.c b/src/sleep/sleep.c index 11c51fb32b..f494ca6ffb 100644 --- a/src/sleep/sleep.c +++ b/src/sleep/sleep.c @@ -108,6 +108,9 @@ static int write_state(FILE **f, char **states) { char **state; int r = 0; + assert(f); + assert(*f); + STRV_FOREACH(state, states) { int k; @@ -194,12 +197,14 @@ static int execute(char **modes, char **states) { setvbuf(f, NULL, _IONBF, 0); - /* Configure the hibernation mode */ + /* Configure hibernation settings if we are supposed to hibernate */ if (!strv_isempty(modes)) { r = find_hibernate_location(&hibernate_location); if (r < 0) - return r; - else if (r == 0) { + return log_error_errno(r, "Failed to find location to hibernate to: %m"); + if (r == 0) { /* 0 means: no hibernation location was configured in the kernel so far, let's + * do it ourselves then. > 0 means: kernel already had a configured hibernation + * location which we shouldn't touch. */ r = write_hibernate_location_info(hibernate_location); if (r < 0) return log_error_errno(r, "Failed to prepare for hibernation: %m"); @@ -275,11 +280,11 @@ static int execute_s2h(const SleepConfig *sleep_config) { r = execute(sleep_config->hibernate_modes, sleep_config->hibernate_states); if (r < 0) { - log_notice_errno(r, "Couldn't hibernate, will try to suspend again."); + log_notice_errno(r, "Couldn't hibernate, will try to suspend again: %m"); r = execute(sleep_config->suspend_modes, sleep_config->suspend_states); if (r < 0) - return log_notice_errno(r, "Could neither hibernate nor suspend again, giving up."); + return log_error_errno(r, "Could neither hibernate nor suspend, giving up: %m"); } return 0;