udev 事件多次触发且不遵守匹配条件

udev 事件多次触发且不遵守匹配条件

我正在尝试编写一个 udev 规则,当我的笔记本电脑电源线插入时触发,但是我遇到了一些奇怪的问题,它在插入和拔出时多次触发。

首先,我获取设备的名称:

$ udevadm monitor --subsystem-match power_supply

我碰巧已经知道子系统的名称,但这实际上并不重要。然后我插入电源线。

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)

由此我可以看到有 4 个事件,其中 2 个属于 BAT0 设备,2 个属于 AC 设备。
然后我查看交流设备的属性,看看可以触发什么。

$ 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"

由此,尝试尽可能具体,使用设备本身以及父级的属性,我构建了此规则:

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"

保存它并拔掉/重新插入我的笔记本电脑后,我发现该脚本已经运行了 10 次。当监视器仅显示 4 个事件(除了其中一个之外的所有事件都不应该匹配)时,有什么方法可以追踪为什么会发生这种情况?

然而,也许存在一个更普遍的问题,因为我发现即使其中包含明显不匹配的条件,规则也会触发,例如

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"

我猜我的匹配方式有问题,是不是跟我想的不一样?我确实阅读了 udev 手册页,所有在线示例似乎都遵循类似的模式。

相关内容