Ubuntu – Script run by udev rule fails

18.04libinputscriptsudevusb

I'm running Ubuntu 18.4 and i got a problem with a script that refuses to run when triggered by udev rule.

This is the Situation:
I am running on a Surface Pro 4. This Device has a detachable keyboard. I run a software called libinput-gestures. Whenever the keyboard is detached and re-attached, gestures won't work. It's quite easy to fix manually with:

libinput-gestures-setup restart

So the script i try to run does exactly this. Here is it in its full glorry:

#!/bin/sh

libinput-gestures-setup restart

I want to run it on the add event of an udev rule. Here is the .rules file:

ACTION=="add", ATTRS{idVendor}=="045e", ATTRS{idProduct}=="07e8", RUN+="/home/$myuser/.bin/gestures-restart"

and finding the script upon attaching the keyboard works fine. But this is what is logged:

Apr 20 22:20:51 mb-Surface-Pro-4 kernel: [ 2248.005043] usb 1-7: New USB device strings: Mfr=1, Product=2, SerialNumber=0
Apr 20 22:20:51 mb-Surface-Pro-4 kernel: [ 2248.005047] usb 1-7: Product: Surface Type Cover
Apr 20 22:20:51 mb-Surface-Pro-4 kernel: [ 2248.005050] usb 1-7: Manufacturer: Microsoft
Apr 20 22:20:51 mb-Surface-Pro-4 kernel: [ 2248.013813] input: Microsoft Surface Type Cover Keyboard as /devices/pci0000:00/0000:00:14.0/usb1/1-7/1-7:1.0/0003:045E:07E8.000D/input/input95
Apr 20 22:20:51 mb-Surface-Pro-4 kernel: [ 2248.072209] input: Microsoft Surface Type Cover Consumer Control as /devices/pci0000:00/0000:00:14.0/usb1/1-7/1-7:1.0/0003:045E:07E8.000D/input/input97
Apr 20 22:20:51 mb-Surface-Pro-4 kernel: [ 2248.072444] input: Microsoft Surface Type Cover Touchpad as /devices/pci0000:00/0000:00:14.0/usb1/1-7/1-7:1.0/0003:045E:07E8.000D/input/input99
Apr 20 22:20:51 mb-Surface-Pro-4 kernel: [ 2248.072881] hid-multitouch 0003:045E:07E8.000D: input,hiddev1,hidraw2: USB HID v1.11 Keyboard [Microsoft Surface Type Cover] on usb-0000:00:14.0-7/input0
Apr 20 22:20:51 mb-Surface-Pro-4 mtp-probe: checking bus 1, device 11: "/sys/devices/pci0000:00/0000:00:14.0/usb1/1-7"
Apr 20 22:20:51 mb-Surface-Pro-4 mtp-probe: bus: 1, device: 11 was not an MTP device
Apr 20 22:20:51 mb-Surface-Pro-4 systemd-udevd[5965]: Process '/home/mariusb/.bin/gestures-restart' failed with exit code 1.
Apr 20 22:20:51 mb-Surface-Pro-4 systemd-udevd[5965]: message repeated 2 times: [ Process '/home/mariusb/.bin/gestures-restart' failed with exit code 1.]
Apr 20 22:20:51 mb-Surface-Pro-4 systemd-udevd[5982]: Process '/home/mariusb/.bin/gestures-restart' failed with exit code 1.
Apr 20 22:20:51 mb-Surface-Pro-4 systemd-udevd[5993]: Process '/home/mariusb/.bin/gestures-restart' failed with exit code 1.
Apr 20 22:20:51 mb-Surface-Pro-4 systemd-udevd[5965]: Process '/home/mariusb/.bin/gestures-restart' failed with exit code 1.
Apr 20 22:20:51 mb-Surface-Pro-4 systemd-udevd[5992]: Process '/home/mariusb/.bin/gestures-restart' failed with exit code 1.
Apr 20 22:20:51 mb-Surface-Pro-4 systemd-udevd[5991]: Process '/home/mariusb/.bin/gestures-restart' failed with exit code 1.
Apr 20 22:20:51 mb-Surface-Pro-4 systemd-udevd[5982]: Process '/home/mariusb/.bin/gestures-restart' failed with exit code 1.
Apr 20 22:20:51 mb-Surface-Pro-4 systemd-udevd[6011]: Process '/home/mariusb/.bin/gestures-restart' failed with exit code 1.
Apr 20 22:20:51 mb-Surface-Pro-4 systemd-udevd[5965]: Process '/home/mariusb/.bin/gestures-restart' failed with exit code 1.
Apr 20 22:20:51 mb-Surface-Pro-4 systemd-udevd[5992]: Process '/home/mariusb/.bin/gestures-restart' failed with exit code 1.
Apr 20 22:20:51 mb-Surface-Pro-4 systemd-udevd[5993]: Process '/home/mariusb/.bin/gestures-restart' failed with exit code 1.
Apr 20 22:20:51 mb-Surface-Pro-4 systemd-udevd[5991]: Process '/home/mariusb/.bin/gestures-restart' failed with exit code 1.
Apr 20 22:20:51 mb-Surface-Pro-4 systemd-udevd[6015]: Process '/home/mariusb/.bin/gestures-restart' failed with exit code 1.
Apr 20 22:20:51 mb-Surface-Pro-4 upowerd[868]: unhandled action 'bind' on /sys/devices/pci0000:00/0000:00:14.0/usb1/1-7/1-7:1.0/0003:045E:07E8.000D
Apr 20 22:20:51 mb-Surface-Pro-4 upowerd[868]: unhandled action 'bind' on /sys/devices/pci0000:00/0000:00:14.0/usb1/1-7/1-7:1.0
Apr 20 22:20:51 mb-Surface-Pro-4 upowerd[868]: unhandled action 'bind' on /sys/devices/pci0000:00/0000:00:14.0/usb1/1-7

So seemingly the script is the problem here. And this is where i need help because I don't understand whats wrong.

As additional information: When I installed libinput i had to add my user to the group libinput. Maybe it has something to do with that

Anyways, much thanks in advance

Edit:
It seems to have something to do with the script not being run by the user. journalctl -xe prints the following:

-- Unit gestures-restart.service has begun starting up.
Apr 20 23:05:44 mb-Surface-Pro-4 gestures-restart[3550]: Non-installation commands must be run as your own user.
Apr 20 23:05:44 mb-Surface-Pro-4 systemd[1]: gestures-restart.service: Main process exited, code=exited, status=1/FAILURE
Apr 20 23:05:44 mb-Surface-Pro-4 systemd[1]: gestures-restart.service: Failed with result 'exit-code'.
Apr 20 23:05:44 mb-Surface-Pro-4 systemd[1]: Failed to start Restart gestures.
-- Subject: Unit gestures-restart.service has failed
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
-- 
-- Unit gestures-restart.service has failed.
-- 
-- The result is RESULT.

does anyone now how to fix that?


Edit/Update 2:


I got the service detecting the connection of the my keyboard. It runs fine and performs the restart script as wanted. So to keep it up to date: this is my service right now:

[Unit]
Description=Service for restarting gestures
Requires=dev-input-by\x2dpath-pci\x2d0000:00:14.0\x2dusb\x2d0:7:1.0\x2devent\x2dmouse.device
After=dev-input-by\x2dpath-pci\x2d0000:00:14.0\x2dusb\x2d0:7:1.0\x2devent\x2dmouse.device

[Service]
Type=simple
User=$user
ExecStart=/home/$user/.bin/gestures-restart.sh

[Install]
WantedBy=dev-input-by\x2dpath-pci\x2d0000:00:14.0\x2dusb\x2d0:7:1.0\x2devent\x2dmouse.device

libinput-gestures -l provides some information about the application, one of them being:

libinput-gestures: device /dev/input/by-path/pci-0000:00:14.0-usb-0:7:1.0-event-mouse(event6): Microsoft Surface Type Cover Touchpad

So as I understand it, this is the device the software uses as input.
So I used systemctl list-units and found this device:

sys-devices-pci0000:00-0000:00:14.0-usb1-1\x2d7-1\x2d7:1.0-0003:045E:07E8.0004-input-input29-event6.device

(I identified it by the event6 property) and used it for the services REQUIRE/AFTER…

This all looks good so far. When i detach and re-attach the keyboard journalctl -xe is outputting the following:

Apr 21 15:22:13 mb-Surface-Pro-4 systemd[1]: Found device Surface_Type_Cover.
-- Subject: Unit dev-input-by\x2dpath-pci\x2d0000:00:14.0\x2dusb\x2d0:7:1.0\x2devent\x2dmouse.device has finished start-up
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
-- 
-- Unit dev-input-by\x2dpath-pci\x2d0000:00:14.0\x2dusb\x2d0:7:1.0\x2devent\x2dmouse.device has finished starting up.
-- 
-- The start-up result is RESULT.
Apr 21 15:22:13 mb-Surface-Pro-4 upowerd[886]: unhandled action 'bind' on /sys/devices/pci0000:00/0000:00:14.0/usb1/1-7/1-7:1.0/0003:045E:07E8.0011
Apr 21 15:22:13 mb-Surface-Pro-4 systemd[1]: Started Service for restarting gestures.
-- Subject: Unit gestures-restart.service has finished start-up
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
-- 
-- Unit gestures-restart.service has finished starting up.
-- 
-- The start-up result is RESULT.
Apr 21 15:22:13 mb-Surface-Pro-4 upowerd[886]: unhandled action 'bind' on /sys/devices/pci0000:00/0000:00:14.0/usb1/1-7/1-7:1.0
Apr 21 15:22:13 mb-Surface-Pro-4 upowerd[886]: unhandled action 'bind' on /sys/devices/pci0000:00/0000:00:14.0/usb1/1-7
Apr 21 15:22:13 mb-Surface-Pro-4 gestures-restart.sh[5532]: libinput-debug-events stopped.
Apr 21 15:22:13 mb-Surface-Pro-4 gestures-restart.sh[5532]: libinput-gestures stopped.

As shown, the service recognizes the connection and launches the sctipt. The scrpt then runs its STOP functionality but it doesn't start up again.
I know that this might be beyond the scope of this Issue but do you see an obvious reason for this?
Thank you 🙂

Last Update

I contacted the developer of the utility. Most probably the start action fails because the application needs to be run in an actual user session. Not by a system service.
Thanks for your time.

Best Answer

Method 1:

$ cat 99-logitech-custom.rules
# Start autorun systemd .service
ACTION=="add", \
SUBSYSTEMS=="input", \
ATTR{name}=="Logitech Performance MX", \
TAG+="systemd", ENV{SYSTEMD_USER_WANTS}="logitech_custom.service"

Use udevadm control --reload && udevadm trigger to reload udev.

...and a simple oneshot systemd .service

$ cat ~/.config/systemd/user/logitech_custom.service
Description=Autorun actions for Logitech

[Service]
Type=oneshot
ExecStart=/opt/bin/autorun_logitech.sh

You do not need to start or enable the .service.


Method 2

When connecting your device it should be visible in the systemctl list-units list (if it doesn't show up as an unit, you need to TAG+= your device first, basically same as the first method but without env{systemd...}). Now you can use this unit name with udevadm info $(systemd-escape -u '<unit>' --path | sed 's/.device$//') to get the DEVLINKS=, once again run it through systemd-escape '<devlink>' --path --suffix=device to get the escaped .device name.

$ cat ~/.config/systemd/user/logitech_custom.service
[Unit]
Description=Autorun actions for Logitech mouse
Requires=dev-input-by\x2did-usb\x2dLogitech_USB_Receiver\x2dif02\x2dmouse.device
After=dev-input-by\x2did-usb\x2dLogitech_USB_Receiver\x2dif02\x2dmouse.device

[Service]
ExecStart=/opt/bin/autorun_logitech.sh

[Install]
WantedBy=dev-input-by\x2did-usb\x2dLogitech_USB_Receiver\x2dif02\x2dmouse.device


... and last enable your systemd .service

$ systemctl --user enable logitech_custom.service
Related Question