簡體   English   中英

如何在登錄ubuntu之前在python守護進程中使用DBUS

[英]How to use DBUS in python daemon before login in ubuntu

(背景:我最近看到了一個與 linux 配對的很棒的 Android 應用程序,它允許用戶解鎖和鎖定鎖屏。但是該項目不再工作和維護。我正在努力使其工作,但 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-launch 啟動 DBUS 服務,但仍然沒有運氣。 有沒有其他方法可以使用 DBUS 與 org.(gnome|freedesktop).screensaver 進行通信?

(這一切都是為了與 org.(gnome|freedesktop).screensaver 通信)

github項目: Remote-linux-unlocker/unlocker-daemon.py

任何幫助表示贊賞,提前致謝。

編輯:如果沒有桌面會話處於活動狀態,也沒有什么可以解鎖的。 感謝您的澄清@tripleee

如果會話與systemd-logind集成(GNOME、Cinnamon 和 KDE 都支持它),

loginctl unlock-sessions

將解鎖所有會話。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM