dbus 示例 - on_name_lost 在 on_name_acquired 之后

dbus example - on_name_lost intermediately after on_name_acquired

我能够使用 dbus 作为客户端,但是如果我编译 https://github.com/bratsche/glib/blob/master/gio/tests/gdbus-example-server.c on_name_acquired 回调在调用 on_name_lost 回调之后调用。 我所做的唯一更改是我使用 G_BUS_TYPE_SYSTEM 而不是 G_BUS_TYPE_SESSION

我只是猜测这是一些身份验证问题。

与会话总线不同,系统总线具有防止任意进程在总线上声明任意知名名称的安全策略。您需要为系统总线安装一个 configuration file 以允许您的服务拥有一个名称:

Rules with the own or own_prefix attribute are checked when a connection attempts to own a well-known bus names. As a special case, own="*" matches any well-known bus name. The well-known session bus normally allows any connection to own any name, while the well-known system bus normally does not allow any connection to own any name, except where allowed by further configuration. System services that will own a name must install configuration that allows them to do so, usually via rules of the form <policy user="some-system-user"><allow own="…"/></policy>.

这意味着在/usr/share/dbus-1/system.d/org.mydomain.MyService1.conf中安装如下配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE busconfig PUBLIC
 "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
 "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
  <!-- Only my-service-user can own the service -->
  <policy user="my-service-user">
    <allow own="org.mydomain.MyService1"/>
  </policy>

  <!-- Anyone can send messages to the service -->
  <policy context="default">
    <allow send_destination="org.mydomain.MyService1"/>
  </policy>
</busconfig>

然后您必须 运行 您的服务进程作为 my-service-user 用户。

D-Bus API design tutorial section on security policies 是相关阅读。