linux – sd-bus API,sd_bus_request_name返回Permission denied

systemd 221中的总线API.

当我在系统总线中请求对象的名称时,它会输出一个错误,说“权限被拒绝”.我以root身份运行输出文件.行“sd_bus_request_name(bus,”net.poettering.Calculator“,0)”抛出错误:“无法获取服务名称..:权限被拒绝”

我认为root应该有权获取对象的名称.有谁知道如何解决这个问题?

先感谢您.

以下是http://0pointer.net/blog/the-new-sd-bus-api-of-systemd.html的示例代码:

int main(int argc, char *argv[]) {
sd_bus_slot *slot = NULL;
sd_bus *bus = NULL;
int r;

r = sd_bus_default_system(&bus);
if (r < 0) {
    fprintf(stderr, "Failed to connect to system bus: %s\n", strerror(-r));
    goto finish;
}

/* Install the object */
r = sd_bus_add_object_vtable(bus,
                             &slot,
                             "/net/poettering/Calculator",
                             "net.poettering.Calculator",   /* interface name                             */calculator_vtable,
                             NULL);
if (r < 0) {
    fprintf(stderr, "Failed to issue method call: %s\n", strerror(-r));
    goto finish;
}

/* Take a well-known service name so that clients can find us */
r = sd_bus_request_name(bus, "net.poettering.Calculator", 0);
if (r < 0) {
    fprintf(stderr, "Failed to acquire service name: %s\n", strerror(-r));
    goto finish;
} 

最佳答案 除明确允许外,典型的默认D-Bus配置不允许注册服务.您需要允许root注册您的服务.创建/etc/dbus-1/system.d/net.poettering.Calculator.conf:

<!DOCTYPE busconfig PUBLIC
 "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
 "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
  <policy user="root">
    <allow own="net.poettering.Calculator"/>
  </policy>
</busconfig>

阅读man dbus-daemon了解详情.

点赞