How to unlock screen from script in KDE Plasma 5?

6

3

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?

Ruslan Prokopchuk

Posted 2016-02-27T02:17:11.220

Reputation: 163

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 – 2016-04-02T21:24:36.147

... 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 – 2016-04-02T21:32:59.603

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 – 2016-04-02T22:34:38.280

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 – Ruslan Prokopchuk – 2016-04-03T05:50:26.600

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 – 2016-04-03T22:12:26.747

Answers

11

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

Syco

Posted 2016-02-27T02:17:11.220

Reputation: 226

1How can I do this WITHOUT systemd, just dbus? – Luke-Jr – 2016-10-02T04:35:17.627

@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. – Stefan Majewsky – 2017-05-24T11:41:30.273

1Service 'org.freedesktop.login1' does not exist.

logind is just part of systemd as far as I'm concerned... – Luke-Jr – 2017-05-25T21:31:39.983

(and looking at alternative logind implementations, none of them look like something I'd want to install) – Luke-Jr – 2017-05-25T21:40:03.800

@StefanMajewsky Neitherloginctl nor qdbus are unlocking my screen. killing kscreenlocker_greet also isn't working (it just respawns). any other options? – Jayen – 2017-12-06T08:49:50.853

If the DBus API is not working, I'd suggest that you file a bug report at https://bugs.kde.org

– Stefan Majewsky – 2017-12-06T13:23:48.467

0

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.

Evi1M4chine

Posted 2016-02-27T02:17:11.220

Reputation: 115

0

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

Fitzcarraldo

Posted 2016-02-27T02:17:11.220

Reputation: 11