From e0b3a70fabb871bf55678e9e177445b1df2aee88 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Mon, 20 Feb 2023 20:12:19 +0800 Subject: [PATCH] sleep: check if we're on AC power before checking battery capacity Before this commit, battery_is_low() returns true if there's no battery on the system. It's now modified to check if the system is on AC power first, and returns false early if that's the case. Fixes #26492 --- src/shared/sleep-config.c | 11 +++++++++-- src/shared/sleep-config.h | 2 +- src/sleep/sleep.c | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/shared/sleep-config.c b/src/shared/sleep-config.c index 232d1db7f5..1eb00717ca 100644 --- a/src/shared/sleep-config.c +++ b/src/shared/sleep-config.c @@ -42,6 +42,7 @@ #include "string-util.h" #include "strv.h" #include "time-util.h" +#include "udev-util.h" #define BATTERY_LOW_CAPACITY_LEVEL 5 #define DISCHARGE_RATE_FILEPATH "/var/lib/systemd/sleep/battery_discharge_percentage_rate_per_hour" @@ -196,8 +197,8 @@ static int read_battery_capacity_percentage(sd_device *dev) { return battery_capacity; } -/* If battery percentage capacity is <= 5%, return success */ -int battery_is_low(void) { +/* If a battery whose percentage capacity is <= 5% exists, and we're not on AC power, return success */ +int battery_is_discharging_and_low(void) { _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; sd_device *dev; int r; @@ -206,6 +207,12 @@ int battery_is_low(void) { * or Normal in case ACPI is not working properly. In case of no battery * 0 will be returned and system will be suspended for 1st cycle then hibernated */ + r = on_ac_power(); + if (r < 0) + log_debug_errno(r, "Failed to check if the system is running on AC, assuming it is not: %m"); + if (r > 0) + return false; + r = battery_enumerator_new(&e); if (r < 0) return log_debug_errno(r, "Failed to initialize battery enumerator: %m"); diff --git a/src/shared/sleep-config.h b/src/shared/sleep-config.h index 99423fe3d3..5c528b890b 100644 --- a/src/shared/sleep-config.h +++ b/src/shared/sleep-config.h @@ -60,7 +60,7 @@ int find_hibernate_location(HibernateLocation **ret_hibernate_location); int can_sleep(SleepOperation operation); int can_sleep_disk(char **types); int can_sleep_state(char **types); -int battery_is_low(void); +int battery_is_discharging_and_low(void); int get_total_suspend_interval(Hashmap *last_capacity, usec_t *ret); int fetch_batteries_capacity_by_name(Hashmap **ret_current_capacity); int get_capacity_by_name(Hashmap *capacities_by_name, const char *name); diff --git a/src/sleep/sleep.c b/src/sleep/sleep.c index 9018188cba..f7fee4a14c 100644 --- a/src/sleep/sleep.c +++ b/src/sleep/sleep.c @@ -274,7 +274,7 @@ static int custom_timer_suspend(const SleepConfig *sleep_config) { hibernate_timestamp = usec_add(now(CLOCK_BOOTTIME), sleep_config->hibernate_delay_usec); - while (battery_is_low() == 0) { + while (battery_is_discharging_and_low() == 0) { _cleanup_hashmap_free_ Hashmap *last_capacity = NULL, *current_capacity = NULL; _cleanup_close_ int tfd = -EBADF; struct itimerspec ts = {};