【问题标题】:Detect inhibited shutdown on D-Bus检测 D-Bus 上的禁止关闭
【发布时间】:2019-05-05 08:04:42
【问题描述】:

我使用 x86_64 Debian 9 Stretch。我运行systemd-inhibit cat,然后在另一个控制台中运行systemctl poweroff。正确关机被禁止。根据this doc 应该发出信号PrepareForShutdown(false),但我看不到它。我用dbus-monitor --system 观看dbus 并使用python 程序:

#!/usr/bin/env python

import dbus
import gobject
from dbus.mainloop.glib import DBusGMainLoop

def handle(*args):
    print "PrepareForShutdown %s" % (args)

DBusGMainLoop(set_as_default=True)     # integrate into gobject main loop
bus = dbus.SystemBus()                 # connect to system wide dbus
bus.add_signal_receiver(               # define the signal to listen to
    handle,                            # callback function
    'PrepareForShutdown',              # signal name
    'org.freedesktop.login1.Manager',  # interface
    'org.freedesktop.login1'           # bus name
)

loop = gobject.MainLoop()
loop.run()

程序什么也不打印。 dbus-monitor 输出一些晦涩的消息(看起来像是在调用 ListInhibitors)。

是没有发出信号还是我无法捕捉到它?我的目标是通过监听 D-Bus 来检测禁止关机,我该怎么做?

编辑:当使用非延迟抑制时,关闭请求会被丢弃,信号不会触发。但是如果我通过systemd-inhibit --mode=delay --what=shutdown cat 使用延迟锁定,那么 PrepareForShutdown 信号就会触发。

【问题讨论】:

    标签: linux systemd dbus


    【解决方案1】:

    是没有发出信号还是我无法捕捉到它?

    不确定。我的猜测是 systemd 只会将信号发送给已采取延迟锁定(单播信号发射)的进程,因为如果您在不听 PrepareForShutdown 而没有 em> 先获取延迟锁。

    检查的方法是阅读systemd source code

    我的目标是通过监听 D-Bus 来检测禁止关机,我该怎么做?

    如果我在一个终端运行sudo dbus-monitor --system,然后在另一个终端运行systemd-inhibit cat,我会看到以下信号发射:

    signal time=1543917703.712998 sender=:1.9 -> destination=(null destination) serial=1150 path=/org/freedesktop/login1; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
       string "org.freedesktop.login1.Manager"
       array [
          dict entry(
             string "BlockInhibited"
             variant             string "shutdown:sleep:idle:handle-power-key:handle-suspend-key:handle-hibernate-key:handle-lid-switch"
          )
       ]
       array [
       ]
    

    因此,您可以观察服务org.freedesktop.login1 公开的/org/freedesktop/login1 对象的属性更改,并查看其BlockInhibitedDelayInhibited 属性何时更改。当这些属性中的任何一个包含shutdown 时,将禁止关闭。它们记录在the same documentation page

    BlockInhibitedDelayInhibited 属性编码什么类型 当前已锁定。这些字段是冒号分隔的列表 shutdown, sleep, idle, handle-power-key, handle-suspend-key, handle-hibernate-keyhandle-lid-switch。清单基本上是 特定的所有当前活动锁的What 字段的联合 模式。

    【讨论】:

    • 不太了解第二部分。每次“按下电源按钮或调用systemctl poweroff”时都需要通知我,而不是在“有人安装抑制锁”时通知我。
    • 关于延迟锁的好点。当我使用延迟锁时,我可以看到 PrepareForShutdown(true) 发出。它只发出一次,所以我只能检测到第一个关闭事件,但这应该足够了。
    猜你喜欢
    • 2016-03-06
    • 1970-01-01
    • 2010-12-01
    • 2011-10-29
    • 2018-03-16
    • 2014-12-14
    • 2015-08-17
    • 2011-12-08
    • 1970-01-01
    相关资源
    最近更新 更多