(background: I recently saw an awesome Android app that is paired with linux which allows users to unlock and lock the lockscreen. However the project is no longer working and maintained anymore. I'm trying to make it work but had a problem with DBUS)
A daemon service is created which opens a socket and waits for incoming connection. When it receives a connection, it tries to obtain a DBUS_ADDRESS and UID and sets it in environ variables:
os.environ['DBUS_SESSION_BUS_ADDRESS'] = DBUS_ADDRESS
os.seteuid(UID)
And this is how I'm obtaining the DBUS and UID address:
def get_bus_and_uid():
'''Find the first running process with a DBUS session address run by
a non-daemon user, and return both the DBUS_SESSION_BUS_ADDRESS and the
uid.'''
DBUS_ADDRESS = 'DBUS_SESSION_BUS_ADDRESS'
UIDS = 'uids'
# Find all non-daemon users.
all_users = [i.split(':') for i in open('/etc/shadow').readlines()]
users = [i[0] for i in all_users if i[1] not in ('!', '*')]
# Find the first non-daemon user process with a DBUS_SESSION_BUS_ADDRESS
# in it's environment.
user_address = {}
for proc in psutil.process_iter():
try:
pinfo = proc.as_dict(attrs=['pid', 'username', UIDS])
except psutil.NoSuchProcess:
pass
user = pinfo['username']
# Ignore process run by daemons.
if user not in users:
continue
environ = psutil.Process(pid=pinfo['pid']).environ()
if DBUS_ADDRESS in environ:
# pinfo[uids] returns real, effective and saved uids.
return environ[DBUS_ADDRESS], pinfo[UIDS][0]
return None, None
However the problem is this works if the call to get_bus_and_uid() is made after atleast one user logins. This fails if any connection is made before first login.
What i tried: I tried to start a DBUS service with $dbus-launch through the daemon but still had no luck. Is there any alternative way to use DBUS to communicate with org.(gnome|freedesktop).screensaver?
(This is all done to comunicate with org.(gnome|freedesktop).screensaver)
Project in github: Remote-linux-unlocker/unlocker-daemon.py
Any help is appreciated, thanks in advance.
EDIT: If there are no desktop sessions active, there is nothing to unlock, either. Thanks for clarification @tripleee
If the session is integrated with systemd-logind (GNOME, Cinnamon, and KDE all support it),
loginctl unlock-sessions
will unlock all sessions.