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

How to detect when dbus app exits or craches?

当 dbus 用户正在使用 exits/crashes/restarts 服务时,如何通知 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.

找出这一点的一种方法是监听 org.freedesktop.DBus.NameOwnerChanged 信号,如 D-Bus specification

中指定的

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

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