为什么该udev规则在移除设备后不触发?


12

我有两只鼠标,其中一只鼠标中键和button6被同时触发。因此,我将我配置~/.Xmodmap为禁用此按钮。但是,与此同时,我希望其他鼠标仍然可以使用此按钮。因此,我写了一条udev规则,要求xmodmap根据所连接的鼠标来更改按钮的布局。


设定

这是我当前的设置:

/etc/udev/rules.d/logitech-g7.rules

# deactivate MB 6 when mouse 1 is connected
ACTION=="add", ENV{DEVTYPE}=="usb_device", ENV{ID_VENDOR_ID}=="046d", ENV{ID_MODEL_ID}=="c51a", RUN+="/home/user/.scripts/Troubleshooting/Bugfixes/mouseswitcher_wrapper 1", OWNER="user"
# restore defaults when mouse 1 is disconnected
ACTION=="remove", ENV{DEVTYPE}=="usb_device", ENV{ID_VENDOR_ID}=="046d", ENV{ID_MODEL_ID}=="c51a", RUN+="/home/user/.scripts/Troubleshooting/Bugfixes/mouseswitcher_wrapper 0", OWNER="user"

/home/user/.scripts/疑难解答/修正/ mouseswitcher_wrapper

#!/bin/bash
/home/user/.scripts/Troubleshooting/Bugfixes/mouseswitcher "$1"&

/home/user/.scripts/问题排查/错误修复/ mouseswitcher

#!/bin/bash
sleep 1
DISPLAY=":0.0"
HOME=/home/user/
XAUTHORITY=$HOME/.Xauthority
export DISPLAY XAUTHORITY HOME

if [[ "$1" == 1 ]] #differentiate between ADD (1) event and REMOVE (0) event
  then
      /usr/bin/xmodmap /home/user/.XmodmapG7 > /dev/null 2>&1
  else
      /usr/bin/xmodmap /home/user/.Xmodmap > /dev/null 2>&1
fi

如果这个问题有点复杂化,那仅仅是因为我偶然发现了udev,xmodmap和DISPLAY变量等一系列问题。我的脚本基于此问答


当前状态

ADD操作正常,而REMOVE操作则无效。在终端中手动执行任何RUN命令都可以正常工作。


到目前为止我尝试过的

我在这个论坛帖子中发现了为什么udev的删除操作存在问题,并因此用ENV变量替换了ATTRS。但是所有这些都无济于事,REMOVE触发器仍然不起作用。

我必须做错了什么。如果周围的人更精通udev和Linux,通常可以看看我的情况,我将不胜感激。


附加信息

udevadm monitor --environment --udev接收器插入时的输出:

######ADD#######

monitor will print the received events for:
UDEV - the event which udev sends out after rule processing

UDEV  [21728.392805] add      /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3 (usb)
ACTION=add
BUSNUM=002
DEVNAME=/dev/bus/usb/002/037
DEVNUM=037
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3
DEVTYPE=usb_device
ID_BUS=usb
ID_MODEL=USB_Receiver
ID_MODEL_ENC=USB\x20Receiver
ID_MODEL_ID=c51a
ID_REVISION=4100
ID_SERIAL=Logitech_USB_Receiver
ID_USB_INTERFACES=:030102:030000:
ID_VENDOR=Logitech
ID_VENDOR_ENC=Logitech
ID_VENDOR_ID=046d
MAJOR=189
MINOR=164
PRODUCT=46d/c51a/4100
SEQNUM=3139
SUBSYSTEM=usb
TYPE=0/0/0
UDEV_LOG=3
UPOWER_VENDOR=Logitech, Inc.
USEC_INITIALIZED=21727880859

UDEV  [21728.393864] add      /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0 (usb)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0
DEVTYPE=usb_interface
INTERFACE=3/1/2
MODALIAS=usb:v046DpC51Ad4100dc00dsc00dp00ic03isc01ip02in00
PRODUCT=46d/c51a/4100
SEQNUM=3140
SUBSYSTEM=usb
TYPE=0/0/0
UDEV_LOG=3
USEC_INITIALIZED=21728393779

UDEV  [21728.395536] add      /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1 (usb)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1
DEVTYPE=usb_interface
INTERFACE=3/0/0
MODALIAS=usb:v046DpC51Ad4100dc00dsc00dp00ic03isc00ip00in01
PRODUCT=46d/c51a/4100
SEQNUM=3146
SUBSYSTEM=usb
TYPE=0/0/0
UDEV_LOG=3
USEC_INITIALIZED=21728395426

UDEV  [21728.395737] add      /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/0003:046D:C51A.004B (hid)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/0003:046D:C51A.004B
HID_ID=0003:0000046D:0000C51A
HID_NAME=Logitech USB Receiver
HID_PHYS=usb-0000:00:1d.0-1.3/input0
MODALIAS=hid:b0003g0001v0000046Dp0000C51A
SEQNUM=3141
SUBSYSTEM=hid
UDEV_LOG=3
USEC_INITIALIZED=21728395467

UDEV  [21728.398102] add      /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/0003:046D:C51A.004C (hid)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/0003:046D:C51A.004C
HID_ID=0003:0000046D:0000C51A
HID_NAME=Logitech USB Receiver
HID_PHYS=usb-0000:00:1d.0-1.3/input1
MODALIAS=hid:b0003g0001v0000046Dp0000C51A
SEQNUM=3147
SUBSYSTEM=hid
UDEV_LOG=3
USEC_INITIALIZED=21728397927

UDEV  [21728.398650] add      /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/0003:046D:C51A.004B/hidraw/hidraw2 (hidraw)
ACTION=add
DEVNAME=/dev/hidraw2
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/0003:046D:C51A.004B/hidraw/hidraw2
MAJOR=251
MINOR=2
SEQNUM=3145
SUBSYSTEM=hidraw
UDEV_LOG=3
USEC_INITIALIZED=21728398188

UDEV  [21728.399406] add      /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/0003:046D:C51A.004C/hidraw/hidraw3 (hidraw)
ACTION=add
DEVNAME=/dev/hidraw3
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/0003:046D:C51A.004C/hidraw/hidraw3
MAJOR=251
MINOR=3
SEQNUM=3151
SUBSYSTEM=hidraw
UDEV_LOG=3
USEC_INITIALIZED=21728399273

UDEV  [21728.400119] add      /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/usbmisc/hiddev1 (usbmisc)
ACTION=add
DEVNAME=/dev/usb/hiddev1
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/usbmisc/hiddev1
MAJOR=180
MINOR=1
SEQNUM=3150
SUBSYSTEM=usbmisc
UDEV_LOG=3
USEC_INITIALIZED=21727874845

UDEV  [21728.401438] add      /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/input/input73 (input)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/input/input73
EV=17
ID_BUS=usb
ID_INPUT=1
ID_INPUT_MOUSE=1
ID_MODEL=USB_Receiver
ID_MODEL_ENC=USB\x20Receiver
ID_MODEL_ID=c51a
ID_PATH=pci-0000:00:1d.0-usb-0:1.3:1.0
ID_PATH_TAG=pci-0000_00_1d_0-usb-0_1_3_1_0
ID_REVISION=4100
ID_SERIAL=Logitech_USB_Receiver
ID_TYPE=hid
ID_USB_DRIVER=usbhid
ID_USB_INTERFACES=:030102:030000:
ID_USB_INTERFACE_NUM=00
ID_VENDOR=Logitech
ID_VENDOR_ENC=Logitech
ID_VENDOR_ID=046d
KEY=ffff0000 0 0 0 0
MODALIAS=input:b0003v046DpC51Ae0111-e0,1,2,4,k110,111,112,113,114,115,116,117,118,119,11A,11B,11C,11D,11E,11F,r0,1,6,8,am4,lsfw
MSC=10
NAME="Logitech USB Receiver"
PHYS="usb-0000:00:1

udevadm monitor --environment --udev卸下接收器时的输出:

#######REMOVE#######


monitor will print the received events for:
UDEV - the event which udev sends out after rule processing

UDEV  [21800.789239] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/input/input75/mouse1 (input)
ACTION=remove
DEVLINKS=/dev/input/by-id/usb-Logitech_USB_Receiver-mouse /dev/input/by-path/pci-0000:00:1d.0-usb-0:1.3:1.0-mouse
DEVNAME=/dev/input/mouse1
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/input/input75/mouse1
ID_BUS=usb
ID_INPUT=1
ID_INPUT_MOUSE=1
ID_MODEL=USB_Receiver
ID_MODEL_ENC=USB\x20Receiver
ID_MODEL_ID=c51a
ID_PATH=pci-0000:00:1d.0-usb-0:1.3:1.0
ID_PATH_TAG=pci-0000_00_1d_0-usb-0_1_3_1_0
ID_REVISION=4100
ID_SERIAL=Logitech_USB_Receiver
ID_TYPE=hid
ID_USB_DRIVER=usbhid
ID_USB_INTERFACES=:030102:030000:
ID_USB_INTERFACE_NUM=00
ID_VENDOR=Logitech
ID_VENDOR_ENC=Logitech
ID_VENDOR_ID=046d
MAJOR=13
MINOR=33
SEQNUM=3178
SUBSYSTEM=input
UDEV_LOG=3
USEC_INITIALIZED=21797014959

UDEV  [21800.792866] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/input/input75/event6 (input)
ACTION=remove
DEVLINKS=/dev/input/by-id/usb-Logitech_USB_Receiver-event-mouse /dev/input/by-path/pci-0000:00:1d.0-usb-0:1.3:1.0-event-mouse
DEVNAME=/dev/input/event6
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/input/input75/event6
ID_BUS=usb
ID_INPUT=1
ID_INPUT_MOUSE=1
ID_MODEL=USB_Receiver
ID_MODEL_ENC=USB\x20Receiver
ID_MODEL_ID=c51a
ID_PATH=pci-0000:00:1d.0-usb-0:1.3:1.0
ID_PATH_TAG=pci-0000_00_1d_0-usb-0_1_3_1_0
ID_REVISION=4100
ID_SERIAL=Logitech_USB_Receiver
ID_TYPE=hid
ID_USB_DRIVER=usbhid
ID_USB_INTERFACES=:030102:030000:
ID_USB_INTERFACE_NUM=00
ID_VENDOR=Logitech
ID_VENDOR_ENC=Logitech
ID_VENDOR_ID=046d
MAJOR=13
MINOR=70
SEQNUM=3179
SUBSYSTEM=input
UDEV_LOG=3
USEC_INITIALIZED=21797013896

UDEV  [21800.797061] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/input/input75 (input)
ACTION=remove
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/input/input75
EV=17
ID_BUS=usb
ID_INPUT=1
ID_INPUT_MOUSE=1
ID_MODEL=USB_Receiver
ID_MODEL_ENC=USB\x20Receiver
ID_MODEL_ID=c51a
ID_PATH=pci-0000:00:1d.0-usb-0:1.3:1.0
ID_PATH_TAG=pci-0000_00_1d_0-usb-0_1_3_1_0
ID_REVISION=4100
ID_SERIAL=Logitech_USB_Receiver
ID_TYPE=hid
ID_USB_DRIVER=usbhid
ID_USB_INTERFACES=:030102:030000:
ID_USB_INTERFACE_NUM=00
ID_VENDOR=Logitech
ID_VENDOR_ENC=Logitech
ID_VENDOR_ID=046d
KEY=ffff0000 0 0 0 0 0 0 0 0
MODALIAS=input:b0003v046DpC51Ae0111-e0,1,2,4,k110,111,112,113,114,115,116,117,118,119,11A,11B,11C,11D,11E,11F,r0,1,6,8,am4,lsfw
MSC=10
NAME="Logitech USB Receiver"
PHYS="usb-0000:00:1d.0-1.3/input0"
PRODUCT=3/46d/c51a/111
PROP=0
REL=143
SEQNUM=3180
SUBSYSTEM=input
UDEV_LOG=3
UNIQ=""
USEC_INITIALIZED=21796479085

UDEV  [21800.797132] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/0003:046D:C51A.004D/hidraw/hidraw2 (hidraw)
ACTION=remove
DEVNAME=/dev/hidraw2
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/0003:046D:C51A.004D/hidraw/hidraw2
MAJOR=251
MINOR=2
SEQNUM=3181
SUBSYSTEM=hidraw
UDEV_LOG=3
USEC_INITIALIZED=21800796369

UDEV  [21800.797172] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/0003:046D:C51A.004D (hid)
ACTION=remove
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/0003:046D:C51A.004D
HID_ID=0003:0000046D:0000C51A
HID_NAME=Logitech USB Receiver
HID_PHYS=usb-0000:00:1d.0-1.3/input0
MODALIAS=hid:b0003g0001v0000046Dp0000C51A
SEQNUM=3182
SUBSYSTEM=hid
UDEV_LOG=3
USEC_INITIALIZED=21800796501

UDEV  [21800.798537] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0 (usb)
ACTION=remove
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0
DEVTYPE=usb_interface
INTERFACE=3/1/2
MODALIAS=usb:v046DpC51Ad4100dc00dsc00dp00ic03isc01ip02in00
PRODUCT=46d/c51a/4100
SEQNUM=3183
SUBSYSTEM=usb
TYPE=0/0/0
UDEV_LOG=3
USEC_INITIALIZED=21800796538

UDEV  [21800.814181] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/input/input76/event7 (input)
ACTION=remove
DEVLINKS=/dev/input/by-id/usb-Logitech_USB_Receiver-event-if01 /dev/input/by-path/pci-0000:00:1d.0-usb-0:1.3:1.1-event
DEVNAME=/dev/input/event7
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/input/input76/event7
ID_BUS=usb
ID_INPUT=1
ID_INPUT_KEY=1
ID_MODEL=USB_Receiver
ID_MODEL_ENC=USB\x20Receiver
ID_MODEL_ID=c51a
ID_PATH=pci-0000:00:1d.0-usb-0:1.3:1.1
ID_PATH_TAG=pci-0000_00_1d_0-usb-0_1_3_1_1
ID_REVISION=4100
ID_SERIAL=Logitech_USB_Receiver
ID_TYPE=hid
ID_USB_DRIVER=usbhid
ID_USB_INTERFACES=:030102:030000:
ID_USB_INTERFACE_NUM=01
ID_VENDOR=Logitech
ID_VENDOR_ENC=Logitech
ID_VENDOR_ID=046d
MAJOR=13
MINOR=71
SEQNUM=3184
SUBSYSTEM=input
UDEV_LOG=3
USEC_INITIALIZED=21797013933
XKBLAYOUT=de
XKBMODEL=pc105

UDEV  [21800.816765] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/input/input76 (input)
ABS=1 0
ACTION=remove
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/input/input76
EV=1f
ID_BUS=usb
ID_INPUT=1
ID_INPUT_KEY=1
ID_MODEL=USB_Receiver
ID_MODEL_ENC=USB\x20Receiver
ID_MODEL_ID=c51a
ID_PATH=pci-0000:00:1d.0-usb-0:1.3:1.1
ID_PATH_TAG=pci-0000_00_1d_0-usb-0_1_3_1_1
ID_REVISION=4100
ID_SERIAL=Logitech_USB_Receiver
ID_TYPE=hid
ID_USB_DRIVER=usbhid
ID_USB_INTERFACES=:030102:030000:
ID_USB_INTERFACE_NUM=01
ID_VENDOR=Logitech
ID_VENDOR_ENC=Logitech
ID_VENDOR_ID=046d
KEY=4837fff 72ff32d bf544446 0 0 1 20f90 8b17c000 677bfa d9415fed 9ed680 4400 0 10000002
MODALIAS=input:b0003v046DpC51Ae0111-e0,1,2,3,4,k71,72,73,74,77,80,82,83,85,86,87,88,89,8A,8B,8C,8E,90,96,98,9B,9C,9E,9F,A1,A3,A4,A5,A6,A7,A8,A9,AB,AC,AD,AE,B0,B1,B2,B5,B6,CE,CF,D0,D1,D2,D4,D8,D9,DB,DF,E4,E7,E8,E9,EA,EB,F1,100,161,162,166,16A,16E,172,174,176,178,179,17A,17B,17C,17D,17F,180,182,183,185,188,189,18C,18D,18E,18F,190,191,192,193,195,198,199,19A,1A0,1A1,1A2,1A3,1A4,1A5,1A6,1A7,1A8,1A9,1AA,1AB,1AC,1AD,1AE,1B0,1B1,1B7,1BA,r6,a20,m4,lsfw
MSC=10
NAME="Logitech USB Receiver"
PHYS="usb-0000:00:1d.0-1.3/input1"
PRODUCT=3/46d/c51a/111
PROP=0
REL=40
SEQNUM=3185
SUBSYSTEM=input
UDEV_LOG=3
UNIQ=""
USEC_INITIALIZED=21796482192

UDEV  [21800.817249] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/usbmisc/hiddev1 (usbmisc)
ACTION=remove
DEVNAME=/dev/usb/hiddev1
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/usbmisc/hiddev1
MAJOR=180
MINOR=1
SEQNUM=3186
SUBSYSTEM=usbmisc
UDEV_LOG=3
USEC_INITIALIZED=21800816392

UDEV  [21800.818490] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/0003:046D:C51A.004E/hidraw/hidraw3 (hidraw)
ACTION=remove
DEVNAME=/dev/hidraw3
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/0003:046D:C51A.004E/hidraw/hidraw3
MAJOR=251
MINOR=3
SEQNUM=3187
SUBSYSTEM=hidraw
UDEV_LOG=3
USEC_INITIALIZED=21800816472

UDEV  [21800.818536] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/0003:046D:C51A.004E (hid)
ACTION=remove
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/0003:046D:C51A.004E
HID_ID=0003:0000046D:0000C51A
HID_NAME=Logitech USB Receiver
HID_PHYS=usb-0000:00:1d.0-1.3/input1
MODALIAS=hid:b0003g0001v0000046Dp0000C51A
SEQNUM=3188
SUBSYSTEM=hid
UDEV_LOG=3
USEC_INITIALIZED=21800816908

UDEV  [21800.818580] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1 (usb)
ACTION=remove
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1
DEVTYPE=usb_interface
INTERFACE=3/0/0
MODALIAS=usb:v046DpC51Ad4100dc00dsc00dp00ic03isc00ip00in01
PRODUCT=46d/c51a/4100
SEQNUM=3189
SUBSYSTEM=usb
TYPE=0/0/0
UDEV_LOG=3
USEC_INITIALIZED=21800816945

UDEV  [21800.818645] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3 (usb)
ACTION=remove
BUSNUM=002
DEVNAME=/dev/bus/usb/002/038
DEVNUM=038
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3
DEVTYPE=usb_device
ID_BUS=usb
ID_MODEL=USB_Receiver
ID_MODEL_ENC=USB\x20Receiver
ID_MODEL_ID=c51a
ID_REVISION=4100
ID_SERIAL=Logitech_USB_Receiver
ID_USB_INTERFACES=:030102:030000:
ID_VENDOR=Logitech
ID_VENDOR_ENC=Logitech
ID_VENDOR_ID=046d
MAJOR=189
MINOR=165
PRODUCT=46d/c51a/4100
SEQNUM=3190
SUBSYSTEM=usb
TYPE=0/0/0
UDEV_LOG=3
UPOWER_VENDOR=Logitech, Inc.
USEC_INITIALIZED=21796488421

1
您能否检查您的规则是否与设备和删除事件匹配,我无法从监视器输出中确认这一点。您可以看到它们是否与usinng相匹配:udevadm test --action=add $(udevadm info --query=path -n /dev/input/mouse0)
zorlem

@zorlem谢谢您的评论。我的问题已达到字符数限制,因此这是输出的粘贴框。以我有限的理解,我想我可以看到该规则不适用。您知道为什么会这样吗?
Glutanimate

Answers:


13

替代解决方案

在写完下面的答案后,我意识到,xinput借助Xorg的配置,甚至可以更优雅地完成您要达到的目标。确保阅读有关在Xorg中控制输入设备的文档。

使用udev(问题的答案)

根据我的测试,您的规则存在两个问题:

  1. 至少在我的Ubuntu GNU / Linux 12.04上,从未进行ENV{DEVTYPE}过匹配检查(即使udevadm infoand 正确报告了该检查udevadm monitor)。这就是您甚至看不到add规则匹配的原因。
  2. 您应该OWNERremove规则中删除分配。这没有任何意义,udev会完全忽略该规则。

请尝试以下两个规则,看看它们是否可以解决问题。

ACTION=="add", ENV{ID_MODEL}=="USB_Receiver", ENV{ID_VENDOR_ID}=="046d", ENV{ID_MODEL_ID}=="c51a", RUN+="/home/user/.scripts/Troubleshooting/Bugfixes/mouseswitcher_wrapper 1", OWNER="user"
ACTION=="remove", ENV{ID_MODEL}=="USB_Receiver", ENV{ID_VENDOR_ID}=="046d", ENV{ID_MODEL_ID}=="c51a", RUN+="/home/user/.scripts/Troubleshooting/Bugfixes/mouseswitcher_wrapper 0"

其他注意事项

要考虑的几件事:

  1. 使用起来更方便,ATTR{idVendor}并且ATTR{idProduct}可以对设备进行分类。您可以安全地更改add规则以使用它们而不是ENV,但是为了简单起见,我这样保留它们。当前addremove规则几乎相同。
  2. 考虑以root用户身份在其他用户可写的目录中运行脚本的安全隐患。在您的特定情况下,这不是一个严重的问题,但我不会将其称为良好的安全实践。IMO最好将脚本放在/ usr / local / bin /中,使其由root.root和mode 0755拥有。
  3. 确保您希望鼠标设备归用户所有,实际上并不需要这样做,PolicyKit和Xorg应该能够处理根目录拥有的设备,而不会出现任何问题。

如果您不需要更改设备的所有者并且您的设置可用于拥有根设备的设备,则可以将两个udev规则简化为以下一个:

ACTION=="add|remove", ENV{ID_MODEL}=="USB_Receiver", ENV{ID_VENDOR_ID}=="046d", ENV{ID_MODEL_ID}=="c51a", RUN+="/home/user/.scripts/Troubleshooting/Bugfixes/mouseswitcher_wrapper $env{ACTION}"

这将使用相应的动作- remove或调用脚本add,因此您需要修改脚本以处理这些参数。

为了防止该规则匹配(和脚本运行)不止一次,您需要使该规则更具体:该规则与鼠标的每个“输入”(按钮等)匹配。这就是为什么它反复执行的原因。尝试添加ENV{ID_TYPE}!="hid"ENV{ID_USB_DRIVER}!="*hid*",看看它是否可以工作,因为只有一个不是HID的设备-顶部usb_device

PS:如果您想使mouseswitcher脚本更加灵活,并且正在使用ConsoleKit运行系统,则可以ck-list-sessions用来获取当前登录的用户,并使用该信息来设置XAUTHORITY变量。


感谢您的精彩回答!在没有OWNER分配的情况下,组合的udev规则可以很好地工作,我认为原始海报已设置为解决环境变量错误。我还可以通过删除包装脚本来简化设置(说实话,我不太确定为什么OP首先要添加它)。剩下的唯一问题是,在删除或连接鼠标时会多次调用该脚本。这是syslog输出,其中udev的日志记录级别设置为info
Glutanimate

您能想到我可以进一步指定规则以便仅执行一次的任何方法吗?另外,感谢您对脚本放置的建议。我不知道安全问题。从现在开始,我一定会考虑到这一点!
Glutanimate

1
尝试添加ENV{ID_TYPE}!="hid"ENV{ID_USB_DRIVER}!="*hid*",看看是否可行。
zorlem 2013年

是的,就是这样。ENV{ID_TYPE}!="hid"工作正常。谢谢!
谷氨酰胺

罕见的答案之一udev就是不仅说“只是增加ENV{DEVTYPE}=="usb_device"您的规则”,而且还要解释原因。谢谢!
德米特里·格里戈里耶夫

2

自从上一个答案被写出来已经过去了几年。从那以后看来,udev发生了变化,因此udev解决方案不再起作用,至少对于具有udev软件包版本237-3ubuntu10.29的Ubuntu 18.04而言。我发布此答案是为了以后人们遇到此问题时的利益,因为我还没有发现其他有关此问题的报告。

在此版本的udev中,ENV{ID_VENDOR_ID} ENV{ID_MODEL_ID}未为remove事件设置,因此规则将永远不会与这些变量匹配。一种解决方法是使用ENV{PRODUCT},它为添加和删除事件设置了。ENV{PRODUCT}格式为idVendor/idProduct/bcdDevice,因此可以与包含以下内容的规则匹配:

ENV{PRODUCT}=="xxxx/yyyy*"

xxxx供应商ID和yyyy设备产品ID 在哪里。

与上一个答案的时间相比,还有一个注意事项是,udev现在ENV{DEVTYPE}可以正确设置并且可以在规则中进行匹配。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.