2

I'm attempting to write a udev rule that triggers when my laptop power cable is plugged in, however I'm having some odd issues with it triggering multiple times, on both plugging and unplugging.

First, I get the name of the device:

$ udevadm monitor --subsystem-match power_supply

I happen to know the name of the subsystem already, but it doesn't actually matter. I then plug in the power cable.

KERNEL[771.036377] change   /devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:08/PNP0C09:00/PNP0C0A:00/power_supply/BAT0 (power_supply)
KERNEL[771.553813] change   /devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:08/PNP0C09:00/ACPI0003:00/power_supply/AC (power_supply)
UDEV  [771.603337] change   /devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:08/PNP0C09:00/ACPI0003:00/power_supply/AC (power_supply)
UDEV  [771.791301] change   /devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:08/PNP0C09:00/PNP0C0A:00/power_supply/BAT0 (power_supply)

From this I can see there are 4 events, 2 of which belong to the BAT0 device and 2 the AC device.
Then I look at the attributes of the AC device to see what I can trigger off of.

$ udevadm info -a /sys/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:08/PNP0C09:00/ACPI0003:00/power_supply/AC

looking at device '/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:08/PNP0C09:00/ACPI0003:00/power_supply/AC':
KERNEL=="AC"
SUBSYSTEM=="power_supply"
DRIVER==""
ATTR{SUBSYSTEM}=="power_supply"
ATTR{POWER_SUPPLY_NAME}=="AC"
ATTR{POWER_SUPPLY_ONLINE}=="1"

looking at parent device '/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:08/PNP0C09:00/ACPI0003:00':
KERNELS=="ACPI0003:00"
SUBSYSTEMS=="acpi"
DRIVERS=="ac"
ATTRS{SUBSYSTEM}=="acpi"
ATTRS{DRIVER}=="ac"
ATTRS{MODALIAS}=="acpi:ACPI0003:"
ATTRS{USEC_INITIALIZED}=="3654374"
ATTRS{ID_VENDOR_FROM_DATABASE}=="The Linux Foundation"

looking at parent device '/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:08/PNP0C09:00':
KERNELS=="PNP0C09:00"
SUBSYSTEMS=="acpi"
DRIVERS=="ec"
ATTRS{SUBSYSTEM}=="acpi"
ATTRS{DRIVER}=="ec"
ATTRS{MODALIAS}=="acpi:PNP0C09:"
ATTRS{USEC_INITIALIZED}=="3588332"
ATTRS{ID_VENDOR_FROM_DATABASE}=="The Linux Foundation"

looking at parent device '/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:08':
KERNELS=="device:08"
SUBSYSTEMS=="acpi"
DRIVERS==""
ATTRS{SUBSYSTEM}=="acpi"

looking at parent device '/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00':
KERNELS=="PNP0A08:00"
SUBSYSTEMS=="acpi"
DRIVERS==""
ATTRS{SUBSYSTEM}=="acpi"
ATTRS{MODALIAS}=="acpi:PNP0A08:PNP0A03:"
ATTRS{USEC_INITIALIZED}=="3301031"
ATTRS{ID_VENDOR_FROM_DATABASE}=="The Linux Foundation"

looking at parent device '/devices/LNXSYSTM:00/LNXSYBUS:00':
KERNELS=="LNXSYBUS:00"
SUBSYSTEMS=="acpi"
DRIVERS==""
ATTRS{SUBSYSTEM}=="acpi"
ATTRS{MODALIAS}=="acpi:LNXSYBUS:"
ATTRS{USEC_INITIALIZED}=="3246891"
ATTRS{ID_VENDOR_FROM_DATABASE}=="The Linux Foundation"

looking at parent device '/devices/LNXSYSTM:00':
KERNELS=="LNXSYSTM:00"
SUBSYSTEMS=="acpi"
DRIVERS==""
ATTRS{SUBSYSTEM}=="acpi"
ATTRS{MODALIAS}=="acpi:LNXSYSTM:"
ATTRS{USEC_INITIALIZED}=="3107859"
ATTRS{ID_VENDOR_FROM_DATABASE}=="The Linux Foundation"

From this, trying to be as specific as possible, using attributes from the device itself as well as the parents I construct this rule:

ACTION=="change"
SUBSYSTEM=="power_supply"
DRIVERS=="ac"
ATTR{POWER_SUPPLY_NAME}=="AC"
ATTR{POWER_SUPPLY_ONLINE}=="1"
RUN+="/usr/bin/bash /home/josh/scripts/udev_sleep.sh"

Upon saving it and unplugging/replugging my laptop, I find that the script has ran 10's of times. Is there any way to trace why this is happening when the monitor only showed 4 events (all but one of which shouldn't have matched anyway?)

However perhaps there is a more general problem, as I find the rule triggers even if obviously unmatching conditions are contained within it, such as

ACTION=="change"
SUBSYSTEM=="Not even the right subsystem!"
DRIVERS=="ac"
ATTR{POWER_SUPPLY_NAME}=="Totally Fake Name"
ATTR{POWER_SUPPLY_ONLINE}=="Definitely not right"
RUN+="/usr/bin/bash /home/josh/scripts/udev_sleep.sh"

I'm guessing that there is something wrong with how I'm matching things, does it not work how I thought? I did read the udev man page and all examples online seem to follow a similar pattern.

  • 3
    this may be completely stupid (I hate udev & I'm on a phone) but weren't udev rules supposed to be separated by commas and written on the same line (or multiple lines terminated by a backslash line continuation)? – mosvy Feb 10 at 18:55
  • Try adding comma + backslash at the end of each line but the last RUN+="...". (The stupid web interface of this silly website won't let me enter them in a comment) – mosvy Feb 10 at 19:04
  • @mosvy Oh silly me, that was indeed the issue. I think I misread a part of the man page on that bit. Thanks. – JoshuaO Feb 10 at 22:29

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.