/usr/sbin/on_ac_power incorrectly reporting ac power status

Bug #1980991 reported by Kevin Tate
78
This bug affects 12 people
Affects Status Importance Assigned to Milestone
powermgmt-base (Debian)
Fix Released
Unknown
powermgmt-base (Ubuntu)
Status tracked in Plucky
Focal
In Progress
Undecided
Ghadi Rahme
Jammy
In Progress
Undecided
Ghadi Rahme
Kinetic
Won't Fix
High
Unassigned
Lunar
Won't Fix
High
Unassigned
Mantic
Won't Fix
High
Ghadi Rahme
Noble
In Progress
High
Ghadi Rahme
Oracular
In Progress
High
Ghadi Rahme
Plucky
In Progress
High
Ghadi Rahme

Bug Description

Thank you @kevintate for the original bug report.

[Impact]

Currently there is an issue with the ac_on_power script where it thinks that USB-c ports with devices plugged in to them are plugged in to power. This is because the script does not check first if these usb-c ports are in sink or source mode first.
The solution is to check /sys/class/typec/* for the mode these usb ports are in, and ignore them if none of them are running in source mode.
This is causing tools such as unattended-upgrades to fail to run because on_ac_power is miss reporting the power state of the machine.

[Test Plan]

Part 1: Verifying if the script returns the correct result

On a device with a USB-c port (it does not matter if the port can be used for powering the device or not) run the following test:

1. Install the patched version of on_ac_power

2. run: $ on_ac_power

3. check the return value: $ echo $?

4. check the return value based on different scenarios:
- plugged in using usb-c
- plugged in using another non usb port
- not plugged it at all
- with a usb-c device plugged in that does not deliver power to the machine
- with a graphics card or any other PCIE device that happen to have a usb-c port (this with and without a usb-c device connected to this extra port)

compare the return value with the actual state of the machine. If the machine is not plugged in to power, you should expect 0 as the return code.

If the machine is plugged in then the return code should be 1.

If you receive 255 as an return code then the script was unable to determine the power profile of the machine and is related to the kernel not exposing enough information to user space. Consumers of on_ac_power generally consider such a return code as the machine being plugged in to power.

Part2: verifying that no regressions exist with consumers such as unattended-upgrades

One such consumer is unattended-upgrades. To verify if unattended upgrades is still affected by this issue, run the following steps:

1. first make sure unattended upgrades is installed:
$ sudo apt install unattended-upgrades

2. using the default config run the following command under different power scenarios:
$ sudo unattended-upgrade --dry-run -v
The different power scenarios to test for are:
- plugged in using usb-c
- plugged in using another non usb port
- not plugged it at all
- with a usb-c device plugged in that does not deliver power to the machine
- with a graphics card or any other PCIE device that happen to have a usb-c port (this with and without a usb-c device connected to this extra port)

3. Make sure that the command above returns the correct result for each status, for example when the machine is expected to be on main power, the following output should be returned:

ghadi@XPS-17-9720 ~ » sudo unattended-upgrades --dry-run -v
Starting unattended upgrades script
Allowed origins are: o=Ubuntu,a=noble, o=Ubuntu,a=noble-security, o=UbuntuESMApps,a=noble-apps-security, o=UbuntuESM,a=noble-infra-security
Initial blacklist:
Initial whitelist (not strict):
No packages found that can be upgraded unattended and no pending auto-removals
The list of kept packages can't be calculated in dry-run mode.

If the machine is not getting power from main but from it's battery, the following output should be seen:

ghadi@XPS-17-9720 ~ » sudo unattended-upgrades --dry-run -v
System is on battery power, stopping

Part3: Test plan conclusions

If the tests done in part 1 fail, this would mean that the proposed fix was not enough to resolve the issue.
If part 2 fails but part 1 passes, further investigations are needed into unattended-upgrades for why it's result might differ from on_ac_power.

[Where problems could occur]

* the script could still incorrectly return the state of power of the machine, specially if the kernel incorrectly advertises a usbc port to be in a different mode then it is in.
* Packages that depend on on_ac_power might fail to run or execute if they require the machine to be in a specific power state first, unattended-upgrades and anacron are two example packages.

[Original Description]
Good afternoon, folks.

I believe I discovered a bug in the /usr/sbin/on_ac_power script. I have a Dell OptiPlex 5090 host that has an entry in /sys/class/power_supply for "ucsi-source-psy-USBC000:001". I believe this is the USB-C power delivery port on the front of the chassis. The issue I'm encountering is that /usr/sbin/on_ac_power is exiting with code 1 which states: (1 (false) if not on AC power) when that isn't the case.

This looks to be because of the ucsi-source-psy-USBC000:001 entry reporting the "online" status as 0, presumably because nothing is currently connected to that USB-C port.

This causes /usr/sbin/on_ac_power to incorrectly report that the machine isn't connected to AC power and causes other utilities like unattended-upgrades to quit when using the default configuration since it believes the machine isn't connected to AC power.

There is a workaround with unattended-upgrades where you can specify it to run regardless of if AC power is connected, but as more and more chassis implement power-delivery USB-C ports I foresee this becoming more of an issue.

I'm not sure if it's anything to look into, but I figured I would share my findings. Please let me know if you have any questions or if I can provide any additional information, troubleshooting, or testing.

Thanks!
-Kevin

Related branches

Revision history for this message
Launchpad Janitor (janitor) wrote :

Status changed to 'Confirmed' because the bug affects multiple users.

Changed in powermgmt-base (Ubuntu):
status: New → Confirmed
Revision history for this message
Brian Murray (brian-murray) wrote :

Looking at the package, it hasn't been updated in 3 years in Debian.

tags: added: rls-kk-incoming
Changed in powermgmt-base (Ubuntu):
importance: Undecided → High
Lukas Märdian (slyon)
tags: added: fr-2548
tags: removed: rls-kk-incoming
Revision history for this message
Gordon Lack (gordon-lack) wrote :

I have this issue on an Intel NUC (NUC10i357FN).

The sysfs entry for the power supply does contain a usb_type entry for it, which contains:

[nuc]: cat usb_type
[C] PD

Revision history for this message
Gordon Lack (gordon-lack) wrote (last edit ):

Possibly this?

--- /bin/on_ac_power 2019-07-20 16:43:51.000000000 +0100
+++ ./on_ac_power 2022-09-22 01:44:53.412558622 +0100
@@ -28,6 +28,17 @@
            type="$(cat "${FN}/type")"
            case "${type}" in
            Mains|USB*|BrickID|Wireless)
+# If USB, is it input or output?
+#
+ case "${type}" in
+ USB*)
+ if test -r "${FN}/usb_type"; then
+ usb_type=`cat "${FN}/usb_type"`
+ case "${usb_type}" in
+ *PD*) continue ;;
+ esac
+ fi
+ esac
                if [ -r "${FN}/online" ]; then
                    online="$(cat "${FN}/online")"
                    [ "$online" = 1 ] && exit 0

(sorry - the indentation spaces after any + are being eaten)

Dan Bungert (dbungert)
Changed in powermgmt-base (Ubuntu Kinetic):
status: Confirmed → Triaged
Revision history for this message
Gordon Lack (gordon-lack) wrote :

This is still an issue in Kinetic.

And it's now stopping unattended_upgrades running, as these (now?) have a condition of ConditionACPower=true
which is not being met even though the system is running on AC (and has no other way of running).

Revision history for this message
Gordon Lack (gordon-lack) wrote (last edit ):

Actually, it turns out that systemd has its own code to check for AC power (same/similar logic - perhaps this should be done in a single system service rather than duplicated between sub-systems?).

So I'll open a bug for this against systemd as well and cross-reference them.

The systemd bug report for this is bug #1998872

Revision history for this message
Gordon Lack (gordon-lack) wrote :

/lib/systemd/systemd-ac-power works (on lunar). on_ac_power still does not.

Perhaps on_ac_power should just be made a symlink to this - or the script should exec that systemd executable if it exists?

Nick Rosbrook (enr0n)
tags: added: rls-mm-incoming
Revision history for this message
Bjorn Toft Madsen (sunbeam60) wrote :

It's worth adding that in #1973359 (was closed as duplicate of this bug; I was the author), the computer is plugged in with a barrel power plug, not via USB-C PD. I'm not sure how it still ends up being routed as a USB power source in /sys/class/power_supply. Specifically, it's this computer: https://imgur.com/a/0nUFAFH ... so it's very much in "has consistent power" range.

tags: added: foundations-todo
removed: rls-mm-incoming
Revision history for this message
Utkarsh Gupta (utkarsh) wrote :

Ubuntu 22.10 (Kinetic Kudu) has reached end of life, so this bug will not be fixed for that specific release.

Changed in powermgmt-base (Ubuntu Kinetic):
status: Triaged → Won't Fix
Revision history for this message
Gordon Lack (gordon-lack) wrote (last edit ):

It's *still* a bug in 23.04.

Do I need to open a new bug report?

Or do I have to wait until another LTS version comes along and report it against that (since it was also a problem with the previous LTS version, and hence still will be - note the comment above that it hasn't been changed in 3 years).

Revision history for this message
Nick Rosbrook (enr0n) wrote :

Gordon - Utkarsh's comment is just about cleaning up Kinetic-related items. We understand that this is still a problem in other releases.

Revision history for this message
Gordon Lack (gordon-lack) wrote :

However, I can't see any bug report for it it any other (later) release.

So this one is marked as High importance, but Won't fix.

There's a similar bug report about this from 2019 (bug #1823521). *That* was reported on 18.04.

Revision history for this message
Nick Rosbrook (enr0n) wrote :

This bug is only marked as "Won't Fix" for *Kinetic*; the "Triaged" state implicitly applies to the current development release (i.e. Mantic). In other words, this indicates that this bug has not been knowingly fixed in *any* release, and still needs attention.

Changed in powermgmt-base (Ubuntu Lunar):
status: New → Triaged
importance: Undecided → High
Revision history for this message
Nick Rosbrook (enr0n) wrote :

By the way - is the main problem for you behavior with unattended-upgrades? Or just use of the on_ac_power script in general?

If the main problem is unattended-upgrades, we can more quickly remove use of on_ac_power from that, as opposed move everything away from using on_ac_power.

Revision history for this message
Gordon Lack (gordon-lack) wrote :

I'm not the bug opener, but the main problem for *me* was with unattended-upgrades as I noticed it had stopped running (while my other systems were still updating).
But that is fixed (as it runs from systemd and the systemd AC power check was fixed in Lunar).

However, there are (at least) 3 crontab entries in /etc/cron.daily or weekly (apt-compat, plocate and apt-xapian-index) using on_ac_power (although 2 of those don't run from cron if systemd is running...). This isn't bothering me.

Revision history for this message
Bjorn Toft Madsen (sunbeam60) wrote :

FWIW, I opened the duplicate of this bug, and my problem was entirely with unattended upgrades.

Revision history for this message
jc00ke (jesse-jc00ke) wrote :

I'm seeing this issue as part of a dependency which relies on `on_ac_power`. Here's the GitHub issue: https://github.com/regolith-linux/regolith-powerd/issues/1

I'm on Lunar.

/lib/systemd/systemd-ac-power reports correctly on my desktop.

Revision history for this message
Brian Murray (brian-murray) wrote :

Ubuntu 23.04 (Lunar Lobster) has reached end of life, so this bug will not be fixed for that specific release.

Changed in powermgmt-base (Ubuntu Lunar):
status: Triaged → Won't Fix
Revision history for this message
Gordon Lack (gordon-lack) wrote :

This is a problem in Mantic too.

And if you never fix it it will be a problem in the next LTS release. Just as it is a problem in the current LTS release.

Revision history for this message
Alexander Fieroch (orclex) wrote :

I have the same problem in 22.04. Is it so difficult to fix it?

Changed in powermgmt-base (Ubuntu Oracular):
assignee: nobody → Ghadi Rahme (ghadi-rahme)
Changed in powermgmt-base (Ubuntu Noble):
assignee: nobody → Ghadi Rahme (ghadi-rahme)
Changed in powermgmt-base (Ubuntu Mantic):
assignee: nobody → Ghadi Rahme (ghadi-rahme)
Changed in powermgmt-base (Ubuntu Jammy):
assignee: nobody → Ghadi Rahme (ghadi-rahme)
Changed in powermgmt-base (Ubuntu Focal):
assignee: nobody → Ghadi Rahme (ghadi-rahme)
Changed in powermgmt-base (Ubuntu Noble):
importance: Undecided → High
Changed in powermgmt-base (Ubuntu Mantic):
importance: Undecided → High
Revision history for this message
Launchpad Janitor (janitor) wrote :

Status changed to 'Confirmed' because the bug affects multiple users.

Changed in powermgmt-base (Ubuntu Focal):
status: New → Confirmed
Changed in powermgmt-base (Ubuntu Jammy):
status: New → Confirmed
Changed in powermgmt-base (Ubuntu Mantic):
status: New → Confirmed
Changed in powermgmt-base (Ubuntu Noble):
status: New → Confirmed
Revision history for this message
Simon Chopin (schopin) wrote :

Unsubscribung ubuntu-sponsors since sponsorship is already handled in the attached MP https://code.launchpad.net/~ghadi-rahme/ubuntu/+source/powermgmt-base/+git/powermgmt-base/+merge/466231

Revision history for this message
Brian Murray (brian-murray) wrote :

Ubuntu 23.10 (Mantic Minotaur) has reached end of life, so this bug will not be fixed for that specific release.

Changed in powermgmt-base (Ubuntu Mantic):
status: Confirmed → Won't Fix
Changed in powermgmt-base (Debian):
status: Unknown → New
Revision history for this message
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package powermgmt-base - 1.37+nmu1ubuntu1

---------------
powermgmt-base (1.37+nmu1ubuntu1) oracular; urgency=medium

  * Fix on_ac_power incorrectly reporting AC state on some
    machines with USB type-C ports (LP: #1980991).

 -- Ghadi Elie Rahme <email address hidden> Tue, 30 Jul 2024 16:59:34 +0300

Changed in powermgmt-base (Ubuntu Oracular):
status: Triaged → Fix Released
Revision history for this message
Ghadi Rahme (ghadi-rahme) wrote :
Revision history for this message
Ghadi Rahme (ghadi-rahme) wrote :
Revision history for this message
Ghadi Rahme (ghadi-rahme) wrote :
Revision history for this message
Ghadi Rahme (ghadi-rahme) wrote :

Hello,

Uploaded are the focal, jammy and noble patch respectively.

Regards,
Ghadi

description: updated
Revision history for this message
Robie Basak (racb) wrote (last edit ):

Thank you for working on this!

SRU review

> Currently there is an issue with the ac_on_power script where it thinks that USB-c ports with devices plugged in to them are plugged in to power.

This isn't a statement of user impact, making it difficult to understand how changing the stable releases is justified.

But looking through the bug, I found a mention of unattended-upgrades not running on some systems because they are falsely believing that they are never on AC power. That implies a user impact of "system isn't automatically updated as expected". That does justify updating the stable release.

Is that the only user impact, or are there other cases we need to consider as well? This is important because if we identify other use cases then we will be able to know to test them.

> [Test Plan]

Please try to test the actual user impact, which in this case might mean running unattended-upgrades and ensuring that it does update on such a system. Running on_ac_power is a helpful diagnostic but it would be preferable to ensure that nothing else has been missed by testing the end goal directly as close as is possible.

Further, changing this code could regress existing systems into thinking they never are on AC power any more and therefore never run unattended-upgrades any more. This would stop security updates and so the consequences would be quite severe.

For this reason please further test:

a) Not just one power state of the machine, but that all reasonable power states of the machine return the expected result...

b) ...across some reasonable set of combinations of machines with and without USB-C sink and/or source capability...

c) ...considering different "online" states of sinks and sources, given my review point below and that the current separated code paths suggest that a bug may be present in this area.

On the actual upload:

1. There are spurious development artifacts being added - various combinations of {focal,jammy,noble}-powermgmt.debdiff. Please re-upload with these removed.

2.

> + power_type="USB*"

This does a glob expansion right here, which I think isn't what is intended. If there are files in the current directory with this pattern then they will be expanded here. This needs fixing.

3. I also don't follow the logic used here. What if a sink is found but it isn't online, and a source exists but is online? Wouldn't that incorrectly identify the system as being on AC power? As far as I can tell from just reading the code, the systemd example presented in the MP doesn't have this behaviour, but the code in this upload would. It's difficult to tell just from inspection though because the addition of this code makes it quite convoluted. Am I missing something?

Point 1 above requires re-upload, so I'm rejecting the current items from the Unapproved queue for now. Please address the other matters raised above before re-uploading.

Changed in powermgmt-base (Ubuntu Focal):
status: Confirmed → Incomplete
Changed in powermgmt-base (Ubuntu Jammy):
status: Confirmed → Incomplete
Changed in powermgmt-base (Ubuntu Noble):
status: Confirmed → Incomplete
Revision history for this message
Ghadi Rahme (ghadi-rahme) wrote :
Download full text (5.0 KiB)

Hello Robie,

Thank you for reviewing this!

I will update the test plan methodology to cover unattended upgrades and/or any other application that makes use of the script.

1. > There are spurious development artifacts being added

I have looked through the debdiffs and couldn't find such, are you referring to the "Maintainer" and "XSBC-Original-Maintainer" section maybe?

2. > This does a glob expansion right here...

Yes this is intended behavior as USBC is not the only USB power type, it is also the same value used before my changes.

3. > Wouldn't that incorrectly identify the system as being on AC power?

Yes you are correct! however this bug also exists in systemd. Systemd also checks if any of the ports are sink first before looking at all the USB power sources. The reason this works is because a USB-C port that is in source more should NOT show up as online under /sys/class/power_supply/* because only ports in sink more are supposed to show their online status here (ports in source mode will show up in the directory but will have their "online" state as 0 even if a device is plugged in to them). The reason I wrote this patch is because some of these source ports are mirroring their connection state inside the "online" sysfs. Discrete GPUs are the biggest culprits where those with USB-C ports are having their connection status mirrored inside "/sys/class/power_supply/ucsi-*/online" although they are labeled as a source device. Here is an upstream issue from systemd discussing such a case: https://github.com/systemd/systemd/issues/21988

Also here is proof from my system that systemd does indeed only require one port in sink mode to make USBC a valid power delivery option (my system has 4 thunderbolt ports that can all be used for charging):

ghadi@XPS-17-9720 ~ » cat /sys/class/typec/*/power_role
[source] sink
[source] sink
source [sink]
source [sink]

ghadi@XPS-17-9720 ~ » cat /sys/class/power_supply/ucsi-*/online
0
0
0
1

ghadi@XPS-17-9720 ~ » SYSTEMD_LOG_LEVEL=debug systemd-ac-power
BAT0: The battery status is 'Charging', assuming the battery is not used as a power source of this machine.
hidpp_battery_1: The power supply is a device battery, ignoring device.
AC: The power supply is currently online.
port0: The USB type-C port is in power source mode.
port1: The USB type-C port is in power source mode.
port2: The USB type-C port is in power sink mode.
port3: The USB type-C port is in power sink mode.
ucsi-source-psy-USBC000:001: The USB type-C device has at least one port in power sink mode.
ucsi-source-psy-USBC000:001: The power supply is currently offline.
port0: The USB type-C port is in power source mode.
port1: The USB type-C port is in power source mode.
port2: The USB type-C port is in power sink mode.
port3: The USB type-C port is in power sink mode.
ucsi-source-psy-USBC000:002: The USB type-C device has at least one port in power sink mode.
ucsi-source-psy-USBC000:002: The power supply is currently offline.
port0: The USB type-C port is in power source mode.
port1: The USB type-C port is in power source mode.
port2: The USB type-C port is in power sink mode.
port3: The USB type-C port is in power sink mode.
ucsi-sourc...

Read more...

description: updated
Revision history for this message
Robie Basak (racb) wrote :

> 1. > There are spurious development artifacts being added

> I have looked through the debdiffs and couldn't find such, are you referring to the "Maintainer" and "XSBC-Original-Maintainer" section maybe?

Not the debdiffs, the actual upload. For example, from the Rejected queue, http://launchpadlibrarian.net/745154626/powermgmt-base_1.36_1.36ubuntu1.20.04.1.diff.gz contains:

> diff -Nru powermgmt-base-1.36/jammy-powermgmt.debdiff powermgmt-base-1.36ubuntu1.20.04.1/jammy-powermgmt.debdiff
> --- powermgmt-base-1.36/jammy-powermgmt.debdiff 1970-01-01 00:00:00.000000000 +0000
> +++ powermgmt-base-1.36ubuntu1.20.04.1/jammy-powermgmt.debdiff 2024-08-12 14:17:10.000000000 +0000

...so debdiff files were being added to the source package upload.

> 2. > This does a glob expansion right here...

> Yes this is intended behavior as USBC is not the only USB power type, it is also the same value used before my changes.

No I think you misunderstand. The script is running from some arbitrary current directory. The result of `power_type="USB*"` depends on the contents of that arbitrary current directory, which is probably not what you intend. For example, if I happen to be in /home/racb/some_project/ when I run on_ac_power, and the file /home/racb/some_project/USB_gotcha exists, then $power_type will get set to "USB_gotcha".

> 3. > Wouldn't that incorrectly identify the system as being on AC power?

Thank you for the detailed reply! This is going to take some time to read and understand. I'll reply to this separately so as not to lose other replies above.

Revision history for this message
Gordon Lack (gordon-lack) wrote (last edit ):

This is noted as Fix Released in Oracular.

This is INCORRECT. This command is STILL BROKEN in Oracular.

This is the result of running it on one of my Intel NUCs (which, I can assure you, is *always* on AC power, as it has no battery).

[ganuc]: sh -x /usr/sbin/on_ac_power
+ set -e
+ OFF_LINE_P=no
+ USB_IS_SINK=no
+ USB_IS_SOURCE=no
+ power_type=Mains
+ [ -d /sys/class/typec/ ]
+ test -d /sys/class/typec/port0
+ test -r /sys/class/typec/port0/power_role
+ cat /sys/class/typec/port0/power_role
+ power_role=[sink]
+ echo [sink]
+ grep -q \[source\]
+ + echo [sink]
grep -q \[sink\]
+ USB_IS_SINK=yes
+ test -d /sys/class/typec/port1
+ test -r /sys/class/typec/port1/power_role
+ cat /sys/class/typec/port1/power_role
+ power_role=[sink]
+ + echo [sink]
grep -q \[source\]
+ + grep -qecho \[sink\] [sink]

+ USB_IS_SINK=yes
+ [ yes = yes ]
+ power_type=USB*
+ [ -d /sys/class/power_supply/ ]
+ test -d /sys/class/power_supply/ucsi-source-psy-USBC000:001
+ test -r /sys/class/power_supply/ucsi-source-psy-USBC000:001/type
+ cat /sys/class/power_supply/ucsi-source-psy-USBC000:001/type
+ type=USB
+ [ -r /sys/class/power_supply/ucsi-source-psy-USBC000:001/online ]
+ cat /sys/class/power_supply/ucsi-source-psy-USBC000:001/online
+ online=0
+ [ 0 = 1 ]
+ [ 0 = 0 ]
+ OFF_LINE_P=yes
+ test -d /sys/class/power_supply/ucsi-source-psy-USBC000:002
+ test -r /sys/class/power_supply/ucsi-source-psy-USBC000:002/type
+ cat /sys/class/power_supply/ucsi-source-psy-USBC000:002/type
+ type=USB
+ [ -r /sys/class/power_supply/ucsi-source-psy-USBC000:002/online ]
+ cat /sys/class/power_supply/ucsi-source-psy-USBC000:002/online
+ online=0
+ [ 0 = 1 ]
+ [ 0 = 0 ]
+ OFF_LINE_P=yes
+ [ yes = yes ]
+ exit 1

So the result is 1, meaning "not on mains power". Which is WRONG.
It seems that the /sys/class/power_supply directory no longer contains info on always-on AC power.

As I have noted (somewhere). The systemd code gets it roght.

[ganuc]: systemd-ac-power
[ganuc]: echo $?
0

Note that on_ac_power code *does* get the correct result on a laptop (where there is a battery to check).

But it gets the incorrect result when running on a system that only has AC power.

Revision history for this message
Ghadi Rahme (ghadi-rahme) wrote :

Thank you for the report @gordon-lack.

Could you run the systemd-ac-power command with SYSTEMD_LOG_LEVEL=debug?

$ SYSTEMD_LOG_LEVEL=debug systemd-ac-power

And share the logs printed on screen.

Thanks!

Revision history for this message
Gordon Lack (gordon-lack) wrote (last edit ):

[ganuc]: SYSTEMD_LOG_LEVEL=debug systemd-ac-power
port0: The USB type-C port is in power sink mode.
port1: The USB type-C port is in power sink mode.
ucsi-source-psy-USBC000:001: The USB type-C device has at least one port in power sink mode.
ucsi-source-psy-USBC000:001: The power supply is currently offline.
port0: The USB type-C port is in power sink mode.
port1: The USB type-C port is in power sink mode.
ucsi-source-psy-USBC000:002: The USB type-C device has at least one port in power sink mode.
ucsi-source-psy-USBC000:002: The power supply is currently offline.
No power supply reported online and no discharging battery found, assuming system is running on AC.

In the absence of any other power supply, and with no battery, it assumes AC power is on.
So it's the (almost) default setting in on_ac_power which is incorrect.

Revision history for this message
Gordon Lack (gordon-lack) wrote (last edit ):

Invert the logic?

Perhaps just look for any /sys/class/power_supply/BAT?/current_now that is non-zero?
If any found, it's running on battery.
Else it must be on a mains-supply.

=====
#!/bin/sh
#

cd /sys/class/power_supply || exit 255

for bd in BAT*; do
    [ -f "$bd"/current_now ] || continue
    bc=`cat "$bd"/current_now 2>/dev/null`
    [ "$bc" -gt 0 ] && exit 1 # Not on AC.
done
exit 0 # Is on AC
=====

Revision history for this message
Ghadi Rahme (ghadi-rahme) wrote :

Interesting, when fixing the script I relied on the mode the USB ports are in (which they should always be in source mode for a desktop machine but apparently each vendor does things differently).

The USB port being in sink mode means that it is configured to power the system and not power peripherals.

It seems that systemd devs were aware of this behavior in some machines and added an extra check that verifies battery status.

Currently on_ac_power has no such checks and completely ignores batteries.
I will update the script to also check for batteries and battery status before returning that the system is in offline mode.

Thanks!

Revision history for this message
Gordon Lack (gordon-lack) wrote (last edit ):

All of my Intel NUcs report the type-C port being in sink mode.
None of them can be powered by a USB-C port.

Even plugging in an external drive leaves the system reporting them all in sink mode.

According to https://www.ti.com/lit/wp/slyy109b/slyy109b.pdf that is correct.
A sink is a port that will supply power to a peripheral.
A source is something that can "obtain" power for the system.

So I think you have your definitions reversed?

Revision history for this message
Ghadi Rahme (ghadi-rahme) wrote :

From the definition of the TI documentation page 3:

- A sink is a port that when attached consumes power from VBUS and a sink is most often a device. A sink could include USB peripherals such as a USB powered light or fan.

- A source is a port that when attached provides power over VBUS. Common sources are a host or hub DFP. An example of a source application is a USB Type-C wall charger.

The definitions are a bit confusing because when connecting two devices together, one port will be in sink and the other in source, so the definition needs to be read based on the perspective of the device being mentioned.

For example a webcam plugged in to the usb-c port of laptop, will have it's port in sink mode since power "sinks" to it from the laptop, but the laptop port will be in source mode since it is a "source" of power for the webcam.

When charging a laptop, it is the one consuming power so it's port is in sink mode while the usb charger will have it's port in source mode (a USB wall charger will always be a source of power).

Thus although a laptop's USB-C port will always be in source mode when a webcam or keyboard is plugged in to it, the webcam and the keyboard are called "sink" devices since their ports are in sink mode and the laptop will be the "source" device.

You can also confirm this view by looking at the systemd logs of systemd-ac-power, where it first verified that the port was in sink mode before checking if there was anything connected to it (they would have been ignored if they were in source mode). And when nothing was connected is did a fallback where it checked the battery status.

I have looked at systemd upstream and found an issue [1] mentioning that intel NUC devices have their ports in sink mode by default (none are in source mode), this lead to a PR [2] that added the battery check you saw when enabling the systemd logs

[1] https://github.com/systemd/systemd/issues/24214
[2] https://github.com/systemd/systemd/pull/24220

Revision history for this message
Ghadi Rahme (ghadi-rahme) wrote :

I will make sure to update the on_ac_power script to fulfill such scenarios similar to systemd ;)

I am just waiting for feedback from @racb first before working on it.

Revision history for this message
Andreas Hasenack (ahasenack) wrote :

This is all veeeeery confusing. And making a mistake here can have drastic consequences, as was mentioned: unattended-upgrades can stop applying security updates.

I wonder if we wouldn't be in a better shape if we switched unattended-upgrades to use systemd-ac-power. That seems to be at least maintained (systemd), whereas powermgmt-base seems unmaintained.

Question to the reporter: does systemd-ac-power work correctly in the system you have where unpatched on_ac_power is incorrect?

Revision history for this message
Gordon Lack (gordon-lack) wrote :

>>> Question to the reporter: does systemd-ac-power work correctly in the system you have where unpatched on_ac_power is incorrect?

Yes. See #7, where I suggested this (once the systemd version was fixed).

Revision history for this message
Ghadi Rahme (ghadi-rahme) wrote :

Hello @ahasenack,

Using systemd-ac-power is only an option on noble and later releases since it is not available in earlier releases of systemd. So jammy and focal would not be able to make use of it.

This is the main reason I put time on getting the current script to work.

Another solution for unattended-upgrades is for it to not depend on external programs/libraries to determine the power state of a system and instead have it's own built in implementation. (I would happily work on implementing that!)

I find it more suitable for such a critical tool to depend as little as possible on external libraries especially when those are not guaranteed to be maintained.

There is also other packages that depend on on-ac-power that are not working as expected because of these issues.

Revision history for this message
Gordon Lack (gordon-lack) wrote :

Would it not be simpler to assume that on_ac_power is True unless you can find a working battery?

Revision history for this message
Ghadi Rahme (ghadi-rahme) wrote :

Not necessarily because not all systems might expose their battery through their ACPI table.

The reason there are so many checks is because not all systems have their firmware expose their components to the kernel.

Revision history for this message
Elliott Friedman (elliott-friedman) wrote :

Focal and Jammy (and Bionic) have /lib/systemd/systemd-ac-power:

# echo "$(grep VERSION= /etc/os-release) $(dpkg -S systemd-ac-power)"
VERSION="20.04.6 LTS (Focal Fossa)" systemd: /lib/systemd/systemd-ac-power

# echo "$(grep VERSION= /etc/os-release) $(dpkg -S systemd-ac-power)"
VERSION="22.04.3 LTS (Jammy Jellyfish)" systemd: /lib/systemd/systemd-ac-power

# echo "$(grep VERSION= /etc/os-release) $(dpkg -S systemd-ac-power)"
VERSION="18.04.6 LTS (Bionic Beaver)" systemd: /lib/systemd/systemd-ac-power

Revision history for this message
Ghadi Rahme (ghadi-rahme) wrote :

Interesting, on newer releases it's located in /bin and /usr/bin which explains why I missed it.

@ahasenack I can modify unattended upgrades to utilize systemd-ac-power, but this will also require me to backport any missing systemd-ac-power patches to it to make sure the script covers as many scenarios as possible.

However other applications that also depend on on_ac_power will still suffer from the same issue, (and this also does not resolve the actual package being broken).

Which solution would you prefer?

Changed in powermgmt-base (Debian):
status: New → Fix Released
Revision history for this message
Andreas Hasenack (ahasenack) wrote :

> @ahasenack I can modify unattended upgrades to utilize systemd-ac-power, but this will also require me to
> backport any missing systemd-ac-power patches to it to make sure the script covers as many scenarios as
> possible.

I rather have to SRU powermgmt-base for a on-power-detection issue or regression than systemd.

Revision history for this message
Ghadi Rahme (ghadi-rahme) wrote :

I have posted a new MR with the requested changes.
Let me know how it looks like!

Changed in powermgmt-base (Ubuntu Oracular):
status: Fix Released → In Progress
Changed in powermgmt-base (Ubuntu Noble):
status: Incomplete → In Progress
Changed in powermgmt-base (Ubuntu Jammy):
status: Incomplete → In Progress
Changed in powermgmt-base (Ubuntu Focal):
status: Incomplete → In Progress
Changed in powermgmt-base (Ubuntu Plucky):
status: Fix Released → In Progress
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.