dcavalca / rpms / systemd

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