1

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.

6
  • run script like this: systemd .service instead. – bac0n Apr 20 '20 at 20:43
  • Thanks @bac0n unfortunately this didn't work but the issue you mentioned brought me closer to the reason of failure. journalctl -xe tells me that "Non-installation commands must be run as your own user. " after that it exits the script with status=1/FAILURE do you know how to fix that? – Marius Biegger Apr 20 '20 at 21:12
  • Your script depends on $PATH, which is inherited from udevd. Use the complete path when specifying libinput-gestures-setup. Ask your terminal with type -p libinput-gestures-setup. – waltinator Apr 20 '20 at 23:21
  • @waltinator thanks for the suggestion. libinput-gestures-setup restart has 2 processes. 1. STOP and 2. START. With the current script i got it to run the STOP process. So I don't think this is an issue with the path. Now the challenge is to also get the START part going. I feel like bac0n solution might lead us somewhere. – Marius Biegger Apr 20 '20 at 23:39
  • ls /dev/input/by-id/ – bac0n Apr 21 '20 at 15:19
1

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
7
  • thanks for your time! This would make sense! I got it working partly. See, the libinput-gestures-setup restart function consists of 2 seperate functions. 1. STOP 2. START. So far, my script only did the STOP event. So i assume when going to start gestures up again the touchpad isnt initialized, causing it to fail. Hoever the list of connected devices outputs this: – Marius Biegger Apr 20 '20 at 23:28
  • dev-input-by\x2did-usb\x2dMicrosoft_Surface_Type_Cover\x2devent\x2dmouse.device dev-input-by\x2did-usb\x2dMicrosoft_Surface_Type_Cover\x2dmouse.device – Marius Biegger Apr 20 '20 at 23:31
  • i tried your method with both devices. The service in both cases failed because of dependency issues. – Marius Biegger Apr 20 '20 at 23:32
  • Apr 21 01:17:07 mb-Surface-Pro-4 systemd[1]: Timed out waiting for device dev-input-by\x2did-usb\x2dMicrosoft_Surface_Type_Cover\x2dmouse.device. Subject: Unit dev-input-by\x2did-usb\x2dMicrosoft_Surface_Type_Cover\x2dmouse.device has failed Apr 21 01:17:07 mb-Surface-Pro-4 systemd[1]: Dependency failed for Restart gestures. – Marius Biegger Apr 20 '20 at 23:34
  • Also: when reconnecting journalctl -xe lists these: Apr 21 01:43:59 mb-Surface-Pro-4 kernel: input: Microsoft Surface Type Cover Keyboard as /devices/pci0000:00/0000:00:14.0/usb1/1-7/1-7:1.0/0003:045E:07E8.0 Apr 21 01:43:59 mb-Surface-Pro-4 kernel: input: Microsoft Surface Type Cover Consumer Control as /devices/pci0000:00/0000:00:14.0/usb1/1-7/1-7:1.0/0003:045 Apr 21 01:43:59 mb-Surface-Pro-4 kernel: input: Microsoft Surface Type Cover Touchpad as /devices/pci0000:00/0000:00:14.0/usb1/1-7/1-7:1.0/0003:045E:07E8.0 – Marius Biegger Apr 20 '20 at 23:47
0

So with quite some delay here is a suggestion on how to solve this kind of problem.

First of all you should read all of this thread with its comments as it gives insight into how to setup

The right D-Bus message has been detected and the script started accordingly. But there still was a problem with libinput-gestures as it was able to stop but not to be started up again! The issue is that the script runs the restart operation as system. But to start up, libinput-gestures needs a user-session so the script has to be run by user.

For that, the creator of libinput has created a seperate tool. dbus-action. I was not able to test this application as my os did not meet the required enviroment. But I hope you might be able to use it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.