Systemd-lock-handler does not seem to be doing his job

Greatings Manjarians,

I am trying to make a simplistic security system that records movement when I am away from the computer, specifically when I lock the computer.

To record the movement I chose motion.

To start this program I was attempting to write a systemd service, that runs the code. To do this I used the AUR package systemd-lock-handler.

This package should create a lock.target, to easily write services that depend on locking and unlocking activities.

My unit file is the following:

[Unit]
Description=Personal lock, with added security
Requisite=graphical.target
PartOf=lock.target
StopWhenUnneeded=yes

[Service]
Type=oneshot
ExecStart=motion
#ExecStop=

[Install]
WantedBy=lock.target

The status of the AUR service is given by:

 systemctl status systemd-lock-handler                                                                                                                                                                                                                            ✔ 
● systemd-lock-handler.service - Logind lock event to systemd target translation
     Loaded: loaded (/etc/systemd/system/systemd-lock-handler.service; enabled; vendor preset: disabled)
     Active: active (running) since Tue 2022-02-01 15:27:00 WET; 1min 52s ago
       Docs: https://sr.ht/~whynothugo/systemd-lock-handler
   Main PID: 531 (systemd-lock-ha)
      Tasks: 6 (limit: 18836)
     Memory: 3.7M
        CPU: 5ms
     CGroup: /session.slice/systemd-lock-handler.service
             └─531 /usr/lib/systemd-lock-handler

fev 01 15:26:58 [host] systemd[1]: Starting Logind lock event to systemd target translation...
fev 01 15:26:58 [host] systemd-lock-handler[531]: main.go:138: Running for user: root
fev 01 15:27:00 [host] systemd-lock-handler[531]: main.go:62: Listening for sleep events...
fev 01 15:27:00 [host] systemd-lock-handler[531]: main.go:128: Listening for lock events...
fev 01 15:27:00 [host] systemd-lock-handler[531]: main.go:143: Initialization complete.
fev 01 15:27:00 [host] systemd[1]: Started Logind lock event to systemd target translation.

Unfortunately the result of attempting to enable my lock service is the following:

 sudo systemctl enable --now my-lock                                                                                                                                                                                                                            3 ✘ 
Unit /etc/systemd/system/my-lock.service is added as a dependency to a non-existent unit lock.target.
^C

Which just hangs until I kill it.

As anybody dealt with this before? Does anybody know how I can get this system set up?

Thank you very much in advance

Hi @JohnnyG,

I’m presuming you did set

HandleLidSwitch=lock

in /etc/systemd/logind.conf?

1 Like

Hi @Mirdarthos,

I did not, but that flag seems to set the behaviour on closing the lid right? I want the computer to remain open so the webcam can record any malicious activity.

In that case, I don’t know if that’s the answer. It might be, and it also might not. As I understand it, you need that to use ‘lock.target’. So maybe don’t use it.

Rather look on how to change/modify/customize the lock shortcut/command and run motion that way…

1 Like

I did not know that, thank you!

Do you (or anyone) have any pointer you could give towards that shortcut? I am not sure where it is defined in Manjaro.

(If not once I manage I will leave the solution here)

No idea, sorry. But yes, rather look into that. I think that’s the route you’ll have to go.

Edit:

If you lock your laptop with a keyboard shortcut, try changing the shortcut to

motion; gnome-screensaver --lock

Don’t know if it’ll work, and don’t have Gnome to test, but just thought of it and decided I’d share.

Edit 2:

Welp, @ishaanbhimwal’s idea seems better.

1 Like
sudo nano /etc/systemd/system/my-lock.service
[Unit]
Description=Personal lock with added security
Requisite=graphical-session.target
PartOf=lock.target

[Service]
ExecStart=/usr/bin/motion
ExecStopPost=/usr/bin/loginctl unlock-session

[Install]
WantedBy=lock.target
systemctl --user enable --now systemd-lock-handler.service
systemctl --user enable --now my-lock.service
1 Like

Thanks for the reply @ishaanbhimwal!

I’ve started out with the grapical-session.target as a requisite, but to my own suprise got:

systemctl enable --now my-lock.service

Unit /etc/systemd/system/my-lock.service is added as a dependency to a non-existent unit lock.target.
Failed to start my-lock.service: Unit graphical-session.target not found.

using the same command with the user flag yields:

systemctl --user enable --now my-lock.service

Failed to enable unit: Unit file my-lock.service does not exist.

So it might need to be set in the .config folder?

In any case I have full acess rights to the machine, so I don’t think I’ll have a problem in having the services in /etc/systemd/system/my-lock.service and not using the user flag.

(Again if anyone as any idea where in the system is the lock command executed, meaning from what file or folder, I would really appreciate it)

ps: I also created a symbolic link to have the AUR service for all users in /etc/systemd/system/, with no effect.

Have you enabled this service?

Change graphical-session.target to graphical.target

Yes, the status is the following:

● systemd-lock-handler.service - Logind lock event to systemd target translation
     Loaded: loaded (/etc/systemd/system/systemd-lock-handler.service; enabled; vendor preset: disabled)
     Active: active (running) since Tue 2022-02-01 15:27:00 WET; 2h 36min ago
       Docs: https://sr.ht/~whynothugo/systemd-lock-handler
   Main PID: [PID] (systemd-lock-ha)
      Tasks: 7 (limit: 18836)
     Memory: 5.4M
        CPU: 25ms
     CGroup: /session.slice/systemd-lock-handler.service
             ├─  531 /usr/lib/systemd-lock-handler
             └─32857 /usr/bin/dbus-daemon --syslog --fork --print-pid 4 --print-address 6 --session

fev 01 15:27:00 [host] systemd-lock-handler[531]: main.go:143: Initialization complete.
fev 01 15:27:00 [host] systemd[1]: Started Logind lock event to systemd target translation.
fev 01 16:26:03 [host] systemd-lock-handler[531]: main.go:52: The system is going to sleep
fev 01 16:26:03 [host] dbus-daemon[32857]: [session uid=0 pid=32855] Activating service name='org.freedesktop.systemd1' requested by ':1.0' (uid=0 pid=531 comm="/usr/lib/systemd-lock-handler ")
fev 01 16:26:03 [host] dbus-daemon[32857]: [session uid=0 pid=32855] Activated service 'org.freedesktop.systemd1' failed: Process org.freedesktop.systemd1 exited with status 1
fev 01 16:26:03 [host] systemd-lock-handler[531]: main.go:56: Error starting sleep.target: failed to start unit: Process org.freedesktop.systemd1 exited with status 1
fev 01 16:30:59 [host] systemd-lock-handler[531]: main.go:52: The system is going to sleep
fev 01 16:30:59 [host] dbus-daemon[32857]: [session uid=0 pid=32855] Activating service name='org.freedesktop.systemd1' requested by ':1.2' (uid=0 pid=531 comm="/usr/lib/systemd-lock-handler ")
fev 01 16:30:59 [host] dbus-daemon[32857]: [session uid=0 pid=32855] Activated service 'org.freedesktop.systemd1' failed: Process org.freedesktop.systemd1 exited with status 1
fev 01 16:30:59 [host] systemd-lock-handler[531]: main.go:56: Error starting sleep.target: failed to start unit: Process org.freedesktop.systemd1 exited with status 1

I’ll change it back.

I am! And I tried your code in a custom keyboard hotkey, but I don’t seem to have gnome-screensaver. The locking maybe comes from gdm? I really don’t know. I think this would fix the problem for sure! If I can find it I’ll post the final solution on an edit of the first post.

It would be cool if you could edit standard shortcuts in gnome. That way you could just add the programs you want to run directly before the system defaults. But this is most likely a gnome level thing.

According to this page:

  1. Open settings, keyboard, shortcuts
  2. Deactivate existing “lock screen”
  3. Create own shortcut and name it as you want. Command line “dm-tool lock”, shortcut “Super + L” (or what you want).
    Maybe you have to install dm-tool with yay

In former times there was a toll called light-locker and a gui tool light-locker-settings to configure. Sadly it does not work any more (for me).

Also, you could see if it’s not possible with Gnome Tweak Tool.

:man_shrugging:

1 Like

Hello all,

I am trying to implement a solution I had implemented on Manjaro sway on a rpi 400. The solution used a similar script to the one I want to use:

#!/usr/bin/bash
trap 'kill $PID;exit' EXIT
motion  &> /tmp/motion.log &
PID=$!
xdg-screensaver lock

I then will set a custom shortcut that runs this program directly (located on /usr/bin).

The problem I have now is that as soon as the screen is locked the camera stops recording (the camera light goes off).

I did not encounter this problem on sway where I set the keybind directly on the sway .config. It worked in a very satisfactory manner.

I don’t know if there is some permission I have to set on gnome to allow the locking program to be able to access the camera. Does anyone have any idea?

Okay so I got a temporary and very breakable solution, but seems to work okay. The problem was the command xdg-screensaver lock exits immediately after locking, as opposed to swaylock.

I had to figure out how to check the locked status somehow and loop on it. And I got this in my lock script:

#!/usr/bin/bash
trap 'kill $PID;exit' EXIT
motion  &> /tmp/motion.log &
PID=$!
xdg-screensaver lock
lock=true
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" | \
(
  while $lock; do
    read X
    if echo $X | grep "member=WakeUpScreen"  &> /dev/null; then
      echo SCREEN_LOCKED;
      lock=true
    elif echo $X | grep "member=ActiveChanged" &> /dev/null; then
      echo SCREEN_UNLOCKED;
      lock=false
    fi
  done
)

I think this is really a bad looking hack/workaround, since I am not even sure if the dbus-monitor command is the best for this purpose, or if the strings I am grepping on are the ones that make sense, etc., etc.

But it works for now.

ps: I got inspiration for the lock checking mechanism here

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.