检查D-Bus对象是否存在

标签 c glib dbus gio

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

我尝试使用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() 枚举席位对象中的所有 session .
  2. 尝试在该 session 的对象路径上调用您想要的方法,看看它是否会失败并出现来自 org.freedesktop.DBus.Error 的错误。 .
  3. 调用 Introspect() 方法/org/freedesktop/ConsoleKit并解析<node>来自生成的 XML blob 的元素以查看当前对象路径层次结构。

第一个选项可能是最容易实现的,也是您使用 ConsoleKit API 的方式。请注意,席位和 session 编号不是确定性的,因此您不应只在代码中硬编码 session 对象路径,因为该路径可能会在将来启动时发生变化。

另请注意,如 ConsoleKit website说,ConsoleKit 已被弃用,取而代之的是 systemd-logind ,您应该考虑使用它。

关于检查D-Bus对象是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46583906/

相关文章:

c - 你如何检查一个目录是否存在于 C 的 Windows 上?

c - 确定流是否已满缓冲

c - 使用 struct 与 pthread 进行合并排序

c++ - 跨线程发送 glib 信号

连接到示例 DBus 守护进程的 C# 程序总是得到 'Access is denied: DBus.BusObject'

c - 如何确定 C 中的 Unix 文本文件?

没有gettext的Linux Glib安装?

c - 将 wordexp 的输出提供给 getopt_long 会使我的 linux cli 应用程序崩溃

go - 使用 Golang 启动 D-Bus 服务

python - 在python中动态创建DBus信号