如何检测dbus应用程序何时退出或崩溃?

时间:2015-12-22 20:36:59

标签: dbus

当dbus服务使用退出/崩溃/重启时,dbus用户如何得到通知?

教程建议有一种方法可以做到这一点,但在规范中我只发现了一个针对名称所有者的信号。

dbus tutorial说: Names have a second important use, other than routing messages. They are used to track lifecycle. When an application exits (or crashes), its connection to the message bus will be closed by the operating system kernel. The message bus then sends out notification messages telling remaining applications that the application's names have lost their owner. By tracking these notifications, your application can reliably monitor the lifetime of other applications. dbus specification有一个关于NameLost信号的部分: org.freedesktop.DBus.NameLost This signal is sent to a specific application when it loses ownership of a name.

1 个答案:

答案 0 :(得分:1)

找出这一点的一种方法是收听D-Bus specification

中指定的org.freedesktop.DBus.NameOwnerChanged信号

您的客户端需要实现一些逻辑来分析信号的参数,以确定何时声明名称,何时重新启动服务,什么时候消失等等。但上述信号可用于接收至少相关信息。

在处理程序函数中,您可以检查name参数是否与您想要了解的服务名称相匹配。如果old_owner参数为空,则服务刚刚声明了总线上的名称。如果new_owner为空,则服务已离开总线(无论出于何种原因)。