9

I use pam_usb and want to lock/unlock computer when USB stick is removed/inserted. For locking I use qdbus org.freedesktop.ScreenSaver /ScreenSaver Lock and it works perfectly. For unlocking I've tried a series of commands I've found (most of them are aggregated here), but they don't work. Specifically, if we exclude those with KDE 4 syntax, they fall into 2 categories: those using qdbus (relevant path here, several variations exists) /MainApplication quit which hang my Xorg completely, and those using killall against screenlocker process, but the process just restarts instantly after that!

So, my question is: how to unlock KDE5 screen locked with its standard locker programmatically?

5
  • I want to do the same thing but using BlueProximity {at 1.2.5 at present on my system - Debian Old-stable (Wheezy)} - the above, written more fully as qdbus org.freedesktop.ScreenSaver /ScreenSaver org.freedesktop.ScreenSaver.Lock does activate the screensaver in "locked" mode {you can replace the last fragment with org.freedesktop.ScreenSaver.SetActive true to put up the saver without a lock} and in either case you may get the screen back with that latter with false - but the monitor (not the whole screen as I have two monitor in one virtual...
    – SlySven
    Commented Apr 2, 2016 at 21:24
  • ... screen) is unresponsive to keystrokes or mouse clicks - the other monitor behaves normally and so do windows on the "inactive" once I have <alt>-<tab>bed to them from the working window and then used <alt><F3> (window operations) to move them with the arrow keys to the working window. I use KDE and have extra widgets showing on the saver screen (a clock and a CPUs activity monitor - for those long build jobs) and they and the "unlock/switch user" widget also remain. Normality is restored by entering the password in the dialog from the "unlock". I, too, need a command to unlock properly.
    – SlySven
    Commented Apr 2, 2016 at 21:32
  • after a bit of experimenting - Ah, I'm on KDE4 not 5 so my: kill `ps ax | grep "kscreenlocker" | grep -v grep | cut -d" " -f 1` is not going to be much help to you though it works in my particular case...
    – SlySven
    Commented Apr 2, 2016 at 22:34
  • Thank you for sharing an experience! But, unfortunately, qdbus org.freedesktop.ScreenSaver /ScreenSaver org.freedesktop.ScreenSaver.SetActive false doesn't unlock screen on my Arch Linux with KDE PLasma 5.6.1 Commented Apr 3, 2016 at 5:50
  • It doesn't for me either! That only kills the screeen saver if it has been activated with the ... org.freedesktop.SetActive true (i.e. in "saver" mode) rather than via ... org.freedesktop.Lock (i.e. in "lock" mode). That is why I had to resort to using kill on any kscreenlocker processes running under my UID. kscreenlocker is what runs the selected screensaver(s) at least in my setup, however YMMV.
    – SlySven
    Commented Apr 3, 2016 at 22:12

4 Answers 4

13

Not really my answer, but it might be useful to someone else too.
It comes from https://forum.kde.org/viewtopic.php?f=289&t=130691#p350000 and it works on latest archlinux with kde 5.7

loginctl lock-session
loginctl unlock-session
7
  • 1
    How can I do this WITHOUT systemd, just dbus? Commented Oct 2, 2016 at 4:35
  • @Luke-Jr This works for me: qdbus --system org.freedesktop.login1 /org/freedesktop/login1/session/self org.freedesktop.login1.Session.Lock (and Unlock similarly) - You can probably do the same with dbus-send, but I'm not familiar with that tool. As for systems that do not have systemd, there will still be a logind implementation because most desktop environments require logind these days. Commented May 24, 2017 at 11:41
  • 1
    Service 'org.freedesktop.login1' does not exist. logind is just part of systemd as far as I'm concerned... Commented May 25, 2017 at 21:31
  • (and looking at alternative logind implementations, none of them look like something I'd want to install) Commented May 25, 2017 at 21:40
  • @StefanMajewsky Neitherloginctl nor qdbus are unlocking my screen. killing kscreenlocker_greet also isn't working (it just respawns). any other options?
    – Jayen
    Commented Dec 6, 2017 at 8:49
2

The screen locker is broken and unlocking is not possible anymore. In order to unlock switch to a virtual terminal (e.g. Ctrl+Alt+F2), log in and execute the command: loginctl unlock-sessions Afterwards switch back to the running session (Ctrl+Alt+F7).

The above message appears sometimes on my laptop running Gentoo Stable with Plasma 5, OpenRC and ConsoleKit. The following script I launch from TTY1 successfully unlocks the X11 session on TTY7:

fitzcarraldo@clevow230ss ~ $ cat unlockKDEsession.sh
#!/bin/bash

# Screen locker broken in KDE with ConsoleKit
# See https://forums.gentoo.org/viewtopic-t-1046566.html
# and https://forums.gentoo.org/viewtopic-t-1054134.html

# Find which session is locked
session=Session$(ck-list-sessions | grep -B10 "x11-display = ':0" | grep -o -P '(?<=Session).*(?=:)')

# Create Bash script to unlock session
echo "#!/bin/bash" > /unlock.sh
echo "su -c 'dbus-send --system --print-reply --dest=\"org.freedesktop.ConsoleKit\" /org/freedesktop/ConsoleKit/$session org.freedesktop.ConsoleKit.Session.Unlock'" >> /unlock.sh
chmod +x /unlock.sh

# Run Bash script in another TTY
openvt -s -w /unlock.sh
1

On modern KDE Plasma the command is apparently:

qdbus --system org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/$session Unlock

The session name can be obtained from

qdbus --literal --system org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/Manager \
org.freedesktop.ConsoleKit.Manager.GetSessions | sed 's/^.*\(Session[0-9]*\).*$/\1/'

Beware, as there can be multiple sessions! There are several GetSession* functions for varying use cases though.

The problem is, that all these functions of course require root!

I don’t know how how loginctl would circumvent this, but it suggests it is possible. Otherwise maybe you can use sudo and allow a script that does this to be run without entering a password.

3
  • Evi1M4chine: please take a look at my question.
    – e-pirate
    Commented Jun 6, 2022 at 12:29
  • @e-pirate: I did re-check your question, and it seems my solution fits it perfectly. Please look at my answer. Did I miss something?
    – anon
    Commented Jun 6, 2022 at 23:10
  • After some googling I found this /usr/bin/dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager.LockSessions to work perfectly well. But thank you any way.
    – e-pirate
    Commented Jun 8, 2022 at 5:28
0

I've been using Evi1M4chine's recipe for quite a while to lock sessions on lid close (as a part of sophisticated script) like this:

for SESSION in $(qdbus --literal --system org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.GetSessions | sed 's/^.*\(Session[0-9]*\).*$/\1/'); do
  dbus-send --system --print-reply --dest=org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/$SESSION org.freedesktop.ConsoleKit.Session.Lock
done

But modern KDE5/Plasma dropped org.freedesktop.ConsoleKit rendering this approach non working. I spent some time trying to figure out how to adopt this to actual dbus -with no luck. Any ideas how to make this work on modern KDE5/Plasma?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .