C-检查D -Bus对象是否存在



我目前正在与GDBUS与Consolekit进行交谈。我使用ConsoleKit2 XML文件和gdbus-codegen来生成代码。一切正常。但是如何检查对象是否存在?例如,我想看看是否有/org/freedesktop/ConsoleKit/Session2(只是一个例子,我知道我可以列举座位对象中的所有会话)。

我尝试使用org.freedesktop.DBus.Peer.Ping函数,但这将返回

dbus-send --system --print-reply --reply-timeout=2000 --type=method_call --dest=org.freedesktop.DBus /org/freedesktop/ConsoleKit/Seat1  org.freedesktop.DBus.Peer.Ping
Error org.freedesktop.DBus.Error.AccessDenied: Rejected send message, 1 matched rules; type="method_call", sender=":1.168" (uid=1000 pid=18279 comm="dbus-send --system --print-reply --reply-timeout=2") interface="org.freedesktop.DBus.Peer" member="Ping" error name="(unset)" requested_reply="0" destination="org.freedesktop.DBus" (bus)

您有几个选项,从最可取到最不可行的顺序列出:

  1. 使用GetSessions()列举座位对象中的所有会话。
  2. 尝试在该会话的对象路径上调用您想要的方法,并查看是否在org.freedesktop.DBus.Error中的错误中失败。
  3. /org/freedesktop/ConsoleKit上调用Introspect()方法,并从结果XML Blob中解析<node>元素以查看当前对象路径层次结构。

第一个选项可能是最容易实现的,并且是您打算使用ConsoleKit API的方式。请注意,座位和会话编号不是确定性的,因此您不应该仅仅在代码中硬编码会话对象路径,因为该路径可能会在未来的启动上更改。

还要注意,正如Consolekit网站所说,ConsoleKit被弃用以支持SystemD-Logind,您应该考虑使用。

最新更新