naccyde / rpms / systemd

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