#885623 Acpitool can't handle more than 2 batteries (with patch)

Package:
acpitool
Source:
acpitool
Description:
command line ACPI client
Submitter:
Ryan Thoryk
Date:
2021-12-08 11:33:02 UTC
Severity:
important
Tags:
#885623#5
Date:
2017-12-28 17:27:45 UTC
From:
To:
I've been having an issue with acpitool's battery reporting on my laptop.  I'm using a ThinkPad T460, which has 2 batteries, and I have a USB wireless mouse that reports as a third battery.  I found that acpitool can't handle reporting more than 2 batteries, resulting in this output:
---
ryan@t460:~$ acpitool -B
   Battery #1     : slot empty
   Battery #2     : slot empty
Invalid battery number, fix this
   Battery #3     : slot empty
---

I've attached a patch which fixes this issue, by correcting some bad code.

Output from the patched version:
---
ryan@t460:~$ acpitool -B
   Battery #1     : present
     Remaining capacity : 22620 mWh, 99.78%
     Design capacity    : 23480 mWh
     Last full capacity : 22670 mWh, 96.55% of design capacity
     Capacity loss      : 3.450%
     Present rate       : 0 mW
     Charging state     : Unknown
     Battery type       : Li-ion
     Model number       : 45N1113
     Serial number      : 4292
   Battery #2     : present
     Remaining capacity : 22810 mWh, 96.65%, 00:22:49
     Design capacity    : 23200 mWh
     Last full capacity : 23600 mWh
     Present rate       : 2077 mW
     Charging state     : Charging
     Battery type       : Li-ion
     Model number       : 45N1775
     Serial number      : 7141
   Battery #3     : slot empty
---

#885623#10
Date:
2021-09-08 08:05:12 UTC
From:
To:
acpitool breaks with a Lenovo L480 and Linux version 5.10.0-8-amd64:

     # acpitool
     acpitool: battery.cpp:816: int Count_Batteries_SysFS(): Assertion
`findex < 4' failed.
     Abgebrochen

     # gdb --args acpitool
     GNU gdb (Debian 10.1-2) 10.1.90.20210103-git
     ...
     (gdb) r
     Starting program: /usr/bin/acpitool
     acpitool: battery.cpp:816: int Count_Batteries_SysFS(): Assertion
`findex < 4' failed.

     Program received signal SIGABRT, Aborted.
     __GI_raise (sig=sig@entry=6) at 
../sysdeps/unix/sysv/linux/raise.c:50
     50      ../sysdeps/unix/sysv/linux/raise.c: Datei oder Verzeichnis
nicht gefunden.
     (gdb) bt full
     #0  __GI_raise (sig=sig@entry=6) at
../sysdeps/unix/sysv/linux/raise.c:50
             ...
     #4  0x0000555555566b17 in Count_Batteries_SysFS () at
battery.cpp:816
             namelist = 0x5555555969c0
             n = <optimized out>
             battery_dir = 0x555555585090
             filename =
{"/sys/class/power_supply/ucsi-source-psy-USBC000:0099/type\000\000\000\000\000\000\000",

"/sys/class/power_supply/ucsi-source-psy-USBC000:0098/type\000\377\377\177\000\000@g",

"/sys/class/power_supply/ucsi-source-psy-USBC000:0097/type\000\000\000\000\000\000\000",

"/sys/class/power_supply/ucsi-source-psy-USBC000:0096/type\000\000\000\000\000\000\000"}
             str =
"\000\000\000\000\000\000\000\000\000\246K\fN\236\267\"\001\377\377\377\377\377\377\377",
'\000' <repeats 33 times>, "\246K\fN\236\267\"\000
\000\000\000\000\000\000\001", '\000' <repeats 15 times>,
"\301hVUUU\000\000\000\000\000"
             name = <optimized out>
             dirname = 0x55555556c472 "/sys/class/power_supply/"
             findex = 4
             batcount = 0
             __PRETTY_FUNCTION__ = "int Count_Batteries_SysFS()"
     #5  0x000055555556922d in Do_Battery_Stuff (show_empty=0,
info_level=info_level@entry=0, verbose=verbose@entry=0) at
battery.cpp:81
             Batt_Info = {0x0, 0x0}
             Bat_Nr = <optimized out>
             Remaining_Percentage = <optimized out>
             Remaining_Time = <optimized out>
             Battery_Left_Percent = <optimized out>
             Time_In_Seconds = 0
             Hours = 0
             Minutes = 0


The problem is (135 entries total) - though I believe that the
kernel-side could use a better representation:

     # ls /sys/class/power_supply/
     AC/                            ucsi-source-psy-USBC000:00121/
     BAT0/                          ucsi-source-psy-USBC000:00122/
     ucsi-source-psy-USBC000:001/   ucsi-source-psy-USBC000:00123/
     ucsi-source-psy-USBC000:0010/  ucsi-source-psy-USBC000:00124/
     ucsi-source-psy-USBC000:00100/ ucsi-source-psy-USBC000:00125/
     ucsi-source-psy-USBC000:00101/ ucsi-source-psy-USBC000:00126/

Eg.
     lrwxrwxrwx  1 root root 0  8. Sep 09:55 ucsi-source-psy-USBC000:0077
->
../../devices/platform/USBC000:00/power_supply/ucsi-source-psy-USBC000:0077
     lrwxrwxrwx  1 root root 0  8. Sep 09:55 ucsi-source-psy-USBC000:0078
->
../../devices/platform/USBC000:00/power_supply/ucsi-source-psy-USBC000:0078

#885623#15
Date:
2021-11-05 13:28:20 UTC
From:
To:
Hi,

* Philipp Marek [Wed Sep 08, 2021 at 10:05:12AM +0200]:
[...]
[...]

I can confirm this behavior also for a ThinkPad X280 with latest
bullseye kernel 5.10.0-9-amd64, exactly the same behavior.

The `acpi` tool (not acpitool) at least doesn't segfault:

| % acpi
| Battery 0: Unknown, 96%
| Battery 1: Discharging, 0%, rate information unavailable

FTR:

| % ls -la /proc/acpi/
| total 0
| dr-xr-xr-x   6 root root 0 Nov  5 13:58 .
| dr-xr-xr-x 414 root root 0 Oct 21 15:43 ..
| dr-xr-xr-x   3 root root 0 Nov  5 14:00 button
| -rw-rw----   1 root root 0 Nov  5 14:00 call
| dr-xr-xr-x  15 root root 0 Nov  5 14:00 ibm
| -rw-r--r--   1 root root 0 Nov  5 14:00 wakeup
| % ls -la /sys/class/power_supply
| total 0
| drwxr-xr-x  2 root root 0 Nov  5 12:31 .
| drwxr-xr-x 66 root root 0 Nov  5 12:31 ..
| lrwxrwxrwx  1 root root 0 Nov  5 13:26 AC -> ../../devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:19/PNP0C09:00/ACPI0003:00/power_supply/AC
| lrwxrwxrwx  1 root root 0 Nov  5 13:30 BAT0 -> ../../devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:19/PNP0C09:00/PNP0C0A:00/power_supply/BAT0
| lrwxrwxrwx  1 root root 0 Nov  5 13:26 hidpp_battery_3 -> ../../devices/pci0000:00/0000:00:1c.0/0000:02:00.0/0000:03:02.0/0000:3a:00.0/usb3/3-1/3-1.2/3-1.2.2/3-1.2.2:1.2/0003:046D:C52B.0036/0003:046D:4069.0037/power_supply/hidpp_battery_3
| lrwxrwxrwx  1 root root 0 Nov  5 13:26 ucsi-source-psy-USBC000:001 -> ../../devices/platform/USBC000:00/power_supply/ucsi-source-psy-USBC000:001
| lrwxrwxrwx  1 root root 0 Nov  5 13:26 ucsi-source-psy-USBC000:002 -> ../../devices/platform/USBC000:00/power_supply/ucsi-source-psy-USBC000:002

regards
-mika-

#885623#20
Date:
2021-12-08 11:23:12 UTC
From:
To:
Howdy,

that assert stems from a different problem I think. I can reproduce it on a
Thinkpad T580 and there exists a separate bug report for that
(https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1001153), with a patch
attached

Regards
	Dietz