summaryrefslogtreecommitdiff
path: root/src/shared/sleep-config.c
diff options
context:
space:
mode:
authorMike Yuan <me@yhndnzj.com>2023-02-20 20:12:19 +0800
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2023-02-21 20:48:33 +0100
commite0b3a70fabb871bf55678e9e177445b1df2aee88 (patch)
treeae28c74109133b4db42cd1ab6c17f498280c1e41 /src/shared/sleep-config.c
parentf8979e869812988835f6951fb73a68e30a4c608c (diff)
downloadsystemd-e0b3a70fabb871bf55678e9e177445b1df2aee88.tar.gz
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
Diffstat (limited to 'src/shared/sleep-config.c')
-rw-r--r--src/shared/sleep-config.c11
1 files changed, 9 insertions, 2 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");