2aacef
From d46e3dedada3d57db518ae3f9f857fd26050a4dd Mon Sep 17 00:00:00 2001
2aacef
From: Yu Watanabe <watanabe.yu+github@gmail.com>
2aacef
Date: Mon, 14 Nov 2022 08:31:09 +0900
2aacef
Subject: [PATCH] sleep: fetch_batteries_capacity_by_name() does not return
2aacef
 -ENOENT
2aacef
2aacef
(cherry picked from commit d812e104c7c62648747d3ffe37db33dde319d15c)
2aacef
2aacef
Related: #2151612
2aacef
---
2aacef
 src/sleep/sleep.c | 29 +++++++++++++++--------------
2aacef
 1 file changed, 15 insertions(+), 14 deletions(-)
2aacef
2aacef
diff --git a/src/sleep/sleep.c b/src/sleep/sleep.c
2aacef
index 7679f2e3be..11a2ba507d 100644
2aacef
--- a/src/sleep/sleep.c
2aacef
+++ b/src/sleep/sleep.c
2aacef
@@ -275,21 +275,16 @@ static int custom_timer_suspend(const SleepConfig *sleep_config) {
2aacef
         while (battery_is_low() == 0) {
2aacef
                 _cleanup_close_ int tfd = -1;
2aacef
                 struct itimerspec ts = {};
2aacef
-                usec_t suspend_interval = sleep_config->hibernate_delay_usec, before_timestamp = 0, after_timestamp = 0, total_suspend_interval;
2aacef
+                usec_t suspend_interval = sleep_config->hibernate_delay_usec, total_suspend_interval;
2aacef
                 bool woken_by_timer;
2aacef
 
2aacef
                 tfd = timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_NONBLOCK|TFD_CLOEXEC);
2aacef
                 if (tfd < 0)
2aacef
                         return log_error_errno(errno, "Error creating timerfd: %m");
2aacef
 
2aacef
-                /* Store current battery capacity and current time before suspension */
2aacef
+                /* Store current battery capacity before suspension */
2aacef
                 r = fetch_batteries_capacity_by_name(&last_capacity);
2aacef
-                if (r >= 0)
2aacef
-                        before_timestamp = now(CLOCK_BOOTTIME);
2aacef
-                else if (r == -ENOENT)
2aacef
-                        /* In case of no battery, system suspend interval will be set to HibernateDelaySec=. */
2aacef
-                        log_debug_errno(r, "Suspend Interval value set to %s: %m", FORMAT_TIMESPAN(suspend_interval, USEC_PER_SEC));
2aacef
-                else
2aacef
+                if (r < 0)
2aacef
                         return log_error_errno(r, "Error fetching battery capacity percentage: %m");
2aacef
 
2aacef
                 r = get_total_suspend_interval(last_capacity, &total_suspend_interval);
2aacef
@@ -298,6 +293,8 @@ static int custom_timer_suspend(const SleepConfig *sleep_config) {
2aacef
                 else
2aacef
                         suspend_interval = total_suspend_interval;
2aacef
 
2aacef
+                usec_t before_timestamp = now(CLOCK_BOOTTIME);
2aacef
+
2aacef
                 log_debug("Set timerfd wake alarm for %s", FORMAT_TIMESPAN(suspend_interval, USEC_PER_SEC));
2aacef
                 /* Wake alarm for system with or without battery to hibernate or estimate discharge rate whichever is applicable */
2aacef
                 timespec_store(&ts.it_value, suspend_interval);
2aacef
@@ -316,18 +313,22 @@ static int custom_timer_suspend(const SleepConfig *sleep_config) {
2aacef
                 woken_by_timer = FLAGS_SET(r, POLLIN);
2aacef
 
2aacef
                 r = fetch_batteries_capacity_by_name(&current_capacity);
2aacef
-                if (r < 0) {
2aacef
+                if (r < 0 || hashmap_isempty(current_capacity)) {
2aacef
                         /* In case of no battery or error while getting charge level, no need to measure
2aacef
-                         * discharge rate. Instead system should wakeup if it is manual wakeup or
2aacef
-                         * hibernate if this is a timer wakeup.   */
2aacef
-                        log_debug_errno(r, "Battery capacity percentage unavailable, cannot estimate discharge rate: %m");
2aacef
+                         * discharge rate. Instead the system should wake up if it is manual wakeup or
2aacef
+                         * hibernate if this is a timer wakeup. */
2aacef
+                        if (r < 0)
2aacef
+                                log_debug_errno(r, "Battery capacity percentage unavailable, cannot estimate discharge rate: %m");
2aacef
+                        else
2aacef
+                                log_debug("No battery found.");
2aacef
                         if (!woken_by_timer)
2aacef
                                 return 0;
2aacef
                         break;
2aacef
                 }
2aacef
 
2aacef
-                after_timestamp = now(CLOCK_BOOTTIME);
2aacef
-                log_debug("Attempting to estimate battery discharge rate after wakeup from %s sleep", FORMAT_TIMESPAN(after_timestamp - before_timestamp, USEC_PER_HOUR));
2aacef
+                usec_t after_timestamp = now(CLOCK_BOOTTIME);
2aacef
+                log_debug("Attempting to estimate battery discharge rate after wakeup from %s sleep",
2aacef
+                          FORMAT_TIMESPAN(after_timestamp - before_timestamp, USEC_PER_HOUR));
2aacef
 
2aacef
                 if (after_timestamp != before_timestamp) {
2aacef
                         r = estimate_battery_discharge_rate_per_hour(last_capacity, current_capacity, before_timestamp, after_timestamp);