如何在登录ubuntu之前在python守护进程中使用DBUS



(背景:我最近看到了一个很棒的Android应用程序,它与linux配对,允许用户解锁和锁定锁屏。然而,该项目不再工作和维护。我正在努力使其工作,但DBUS出现了问题(

将创建一个守护进程服务,打开一个套接字并等待传入连接。当它接收到连接时,它会尝试获取DBUS_ADDRESS和UID,并将其设置在环境变量中:

os.environ['DBUS_SESSION_BUS_ADDRESS'] = DBUS_ADDRESS
os.seteuid(UID)

这就是我获取DBUS和UID地址的方式:

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

然而,问题是,如果在至少一个用户登录后调用get_bus_and_uid((,则这种方法有效。如果在第一次登录之前进行了任何连接,则此操作将失败。

我尝试过的:我试图通过守护进程启动$DBUS的DBUS服务,但仍然没有成功。有没有其他方法可以使用DBUS与org.(gnome|freedesktop(.screensaver进行通信?

(这一切都是为了与组织通信。(gnome|freedesktop(.screensaver(

github中的项目:远程linux解锁器/ulocker-daemon.py

如有任何帮助,我们将不胜感激,并提前表示感谢。

编辑:如果没有活动的桌面会话,也没有什么可解锁的。感谢澄清@triplee

如果会话与systemd logind集成(GNOME、Cinnamon和KDE都支持它(,

loginctl unlock-sessions

将解锁所有会话。

最新更新