2aacef
From 2ac7d7a818788110342a99978680485fbe27cc25 Mon Sep 17 00:00:00 2001
2aacef
From: Yu Watanabe <watanabe.yu+github@gmail.com>
2aacef
Date: Fri, 11 Nov 2022 13:54:03 +0900
2aacef
Subject: [PATCH] ac-power: check battery existence and status
2aacef
2aacef
If a battery is not present or its status is not discharging, then
2aacef
the battery should not be used as a power source.
2aacef
Let's count batteries currently discharging.
2aacef
2aacef
Fixes #25316.
2aacef
2aacef
(cherry picked from commit 1c03f7f4ba419aa65997e90accc0d935ae1cfbc5)
2aacef
2aacef
Related: #2138081
2aacef
---
2aacef
 src/shared/udev-util.c | 58 ++++++++++++++++++++++++++++++++----------
2aacef
 1 file changed, 44 insertions(+), 14 deletions(-)
2aacef
2aacef
diff --git a/src/shared/udev-util.c b/src/shared/udev-util.c
2aacef
index aac02cd61b..7d95353452 100644
2aacef
--- a/src/shared/udev-util.c
2aacef
+++ b/src/shared/udev-util.c
2aacef
@@ -642,9 +642,46 @@ static int device_is_power_sink(sd_device *device) {
2aacef
         return found_sink || !found_source;
2aacef
 }
2aacef
 
2aacef
+static bool battery_is_discharging(sd_device *d) {
2aacef
+        const char *val;
2aacef
+        int r;
2aacef
+
2aacef
+        assert(d);
2aacef
+
2aacef
+        r = sd_device_get_sysattr_value(d, "scope", &val;;
2aacef
+        if (r < 0) {
2aacef
+                if (r != -ENOENT)
2aacef
+                        log_device_debug_errno(d, r, "Failed to read 'scope' sysfs attribute, ignoring: %m");
2aacef
+        } else if (streq(val, "Device")) {
2aacef
+                log_device_debug(d, "The power supply is a device battery, ignoring device.");
2aacef
+                return false;
2aacef
+        }
2aacef
+
2aacef
+        r = device_get_sysattr_bool(d, "present");
2aacef
+        if (r < 0)
2aacef
+                log_device_debug_errno(d, r, "Failed to read 'present' sysfs attribute, assuming the battery is present: %m");
2aacef
+        else if (r == 0) {
2aacef
+                log_device_debug(d, "The battery is not present, ignoring the power supply.");
2aacef
+                return false;
2aacef
+        }
2aacef
+
2aacef
+        /* Possible values: "Unknown", "Charging", "Discharging", "Not charging", "Full" */
2aacef
+        r = sd_device_get_sysattr_value(d, "status", &val;;
2aacef
+        if (r < 0) {
2aacef
+                log_device_debug_errno(d, r, "Failed to read 'status' sysfs attribute, assuming the battery is discharging: %m");
2aacef
+                return true;
2aacef
+        }
2aacef
+        if (!streq(val, "Discharging")) {
2aacef
+                log_device_debug(d, "The battery status is '%s', assuming the battery is not used as a power source of this machine.", val);
2aacef
+                return false;
2aacef
+        }
2aacef
+
2aacef
+        return true;
2aacef
+}
2aacef
+
2aacef
 int on_ac_power(void) {
2aacef
         _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
2aacef
-        bool found_ac_online = false, found_battery = false;
2aacef
+        bool found_ac_online = false, found_discharging_battery = false;
2aacef
         sd_device *d;
2aacef
         int r;
2aacef
 
2aacef
@@ -686,17 +723,10 @@ int on_ac_power(void) {
2aacef
                 }
2aacef
 
2aacef
                 if (streq(val, "Battery")) {
2aacef
-                        r = sd_device_get_sysattr_value(d, "scope", &val;;
2aacef
-                        if (r < 0) {
2aacef
-                                if (r != -ENOENT)
2aacef
-                                        log_device_debug_errno(d, r, "Failed to read 'scope' sysfs attribute, ignoring: %m");
2aacef
-                        } else if (streq(val, "Device")) {
2aacef
-                                log_device_debug(d, "The power supply is a device battery, ignoring device.");
2aacef
-                                continue;
2aacef
+                        if (battery_is_discharging(d)) {
2aacef
+                                found_discharging_battery = true;
2aacef
+                                log_device_debug(d, "The power supply is a battery and currently discharging.");
2aacef
                         }
2aacef
-
2aacef
-                        found_battery = true;
2aacef
-                        log_device_debug(d, "The power supply is battery.");
2aacef
                         continue;
2aacef
                 }
2aacef
 
2aacef
@@ -713,11 +743,11 @@ int on_ac_power(void) {
2aacef
         if (found_ac_online) {
2aacef
                 log_debug("Found at least one online non-battery power supply, system is running on AC.");
2aacef
                 return true;
2aacef
-        } else if (found_battery) {
2aacef
-                log_debug("Found battery and no online power sources, assuming system is running from battery.");
2aacef
+        } else if (found_discharging_battery) {
2aacef
+                log_debug("Found at least one discharging battery and no online power sources, assuming system is running from battery.");
2aacef
                 return false;
2aacef
         } else {
2aacef
-                log_debug("No power supply reported online and no battery, assuming system is running on AC.");
2aacef
+                log_debug("No power supply reported online and no discharging battery found, assuming system is running on AC.");
2aacef
                 return true;
2aacef
         }
2aacef
 }