检查D-Bus对象是否存在
问题描述:
我目前正在使用GDBus与ConsoleKit进行通话。我使用ConsoleKit2 XML文件和gdbus-codegen
来生成代码。一切工作正常。但是,我怎样才能检查一个对象是否存在?例如,我想查看是否有/org/freedesktop/ConsoleKit/Session2
(只是一个例子,我知道我可以枚举Seat对象中的所有会话)。检查D-Bus对象是否存在
我尝试使用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)
答
您有几种选择,从最优选的,以上市至少可取:
- 枚举座位所有会话对象使用
GetSessions()
。 - 尝试在该会话的对象路径上调用所需的方法,并查看它是否失败,并从
org.freedesktop.DBus.Error
发生错误。 - 在
/org/freedesktop/ConsoleKit
上调用Introspect()
方法,并从结果XML blob中解析<node>
元素以查看当前对象路径层次结构。
第一个选项可能是最容易实现的,也是您打算如何使用ConsoleKit API。请注意,座席和会话编号不是确定性的,因此您不应该在代码中硬编码会话对象路径,因为该路径可能会在将来的启动时更改。
另请注意,正如ConsoleKit website所述,ConsoleKit已弃用,因此您应该考虑使用systemd-logind。
谢谢你的帮助。第二个想法也出现在我的脑海里,但我认为它会很难看。然后它会是数字1.我知道它不是确定性的,我只是使用硬编码路径作为例子。我也知道logind是这样做的新方式,我的应用程序中的下一步将是logind支持,但由于某些发行版仍然使用consolekit(例如,Gentoo Linux仍然可以使用Consolekit),所以我想同时支持这两种。再次感谢你。 – Nidhoegger