You are not logged in.

#1 2023-08-29 14:58:41

SanskritFritz
Member
From: Budapest, Hungary
Registered: 2009-01-08
Posts: 1,924
Website

[Solved] Trying to use WantedBy but no .device file is created

I'm using a UGREEN USB Sharing Switch for sharing my keyboard and mouse whith two computers.
My problem is that the keylogger crashes when I switch away:

root@frank-desktop ~# systemctl status logkeys
× logkeys.service - Keylogger daemon
     Loaded: loaded (/usr/lib/systemd/system/logkeys.service; enabled; preset: disabled)
     Active: failed (Result: exit-code) since Tue 2023-08-29 16:30:48 CEST; 13min ago
...
aug 29 16:30:48 frank-desktop systemd[1]: logkeys.service: Control process exited, code=exited, status=1/FAILURE
aug 29 16:30:48 frank-desktop systemd[1]: logkeys.service: Failed with result 'exit-code'.

I want to use the WantedBy directive so switching back would restart the service. But much to my surprise no .device file is created. I get the same output for 'systemctl --all --full -t device' in both cases. However 'lsusb' does show the difference, the following lines appear only if the keyboard and mouse are active on the computer:

Bus 001 Device 034: ID 045e:0084 Microsoft Corp. Basic Optical Mouse
Bus 001 Device 033: ID 413c:2107 Dell Computer Corp. KB212-B Quiet Key Keyboard
Bus 001 Device 032: ID 1a40:0101 Terminus Technology Inc. Hub

Should I rather use an Udev rule to restart the service?

Last edited by SanskritFritz (2023-09-11 16:54:40)


zʇıɹɟʇıɹʞsuɐs AUR || Cycling in Budapest with a helmet camera || Revised log levels proposal: "FYI" "WTF" and "OMG" (John Barnette)

Offline

#2 2023-08-30 19:54:15

seth
Member
Registered: 2012-09-03
Posts: 51,094

Re: [Solved] Trying to use WantedBy but no .device file is created

You'll *have* to use a udev rule and add

TAG+="systemd"

to get the device unit.

Online

#3 2023-09-02 20:03:52

SanskritFritz
Member
From: Budapest, Hungary
Registered: 2009-01-08
Posts: 1,924
Website

Re: [Solved] Trying to use WantedBy but no .device file is created

Thank you that helped! Using this udev rule I get a /dev/usbkeyboard simlink:

/etc/udev/rules.d/40-usbkeyboard.rules
SUBSYSTEMS=="usb", ATTRS{bInterfaceProtocol}=="01", TAG+="systemd", SYMLINK+="usbkeyboard"

I edited the service file as follows:

### Editing /etc/systemd/system/logkeys.service.d/override.conf
### Anything between here and the comment below will become the contents of the drop-in file

[Unit]
Description=Keylogger daemon (modified)
After=dev-usbkeyboard.device
BindsTo=dev-usbkeyboard.device

[Install]
WantedBy=dev-usbkeyboard.device

### Edits below this comment will be discarded

### /usr/lib/systemd/system/logkeys.service
# [Unit]
# Description=Keylogger daemon
# 
# [Service]
# Type=forking
# GuessMainPID=yes
# EnvironmentFile=/etc/conf.d/logkeysd
# ExecStart=/usr/bin/logkeys --start $LOGKEYS_PARAMS
# ExecStop=/usr/bin/logkeys --kill
#      
# [Install]
# WantedBy=multi-user.target

But logkeys is not started automatically when the keyboard is plugged.
My interpretation of this: systemd wants to start the daemon when /dev/usbkeyboard symlink is present and wants to stop it when the symlink disappears. However the logkeys daemon fails immediately when the keyboard is unplugged, and by the time systemd tries to stop logkeys it has already failed, hence the service enters failed state. That could be the reason why it is not restarted when the keyboard is plugged back again.
Am I right with this assumption? Is there a solution for this?


zʇıɹɟʇıɹʞsuɐs AUR || Cycling in Budapest with a helmet camera || Revised log levels proposal: "FYI" "WTF" and "OMG" (John Barnette)

Offline

#4 2023-09-02 20:20:23

seth
Member
Registered: 2012-09-03
Posts: 51,094

Re: [Solved] Trying to use WantedBy but no .device file is created

I'd add a synthetic service that just logs the invocation instead of doing anything w/ the keylogger.
If that works as expected you could use a script /usr/local/bin/logkeys.sh that masks the crash and other failures of /usr/bin/logkeys by an unconditional "exit 0"

Online

#5 2023-09-11 16:54:05

SanskritFritz
Member
From: Budapest, Hungary
Registered: 2009-01-08
Posts: 1,924
Website

Re: [Solved] Trying to use WantedBy but no .device file is created

Thank you, that way it works. I defined a simple service depending on the device file which just starts the keylogger again whenever the keyboard becomes active:

[Unit]
Description=Logkeys masked
After=dev-usbkeyboard.device
[Service]
ExecStart=systemctl start logkeys.service
[Install]
WantedBy=dev-usbkeyboard.device

zʇıɹɟʇıɹʞsuɐs AUR || Cycling in Budapest with a helmet camera || Revised log levels proposal: "FYI" "WTF" and "OMG" (John Barnette)

Offline

Board footer

Powered by FluxBB