sd-bus API,sd_bus_request_name返回Permission denied

时间:2015-09-28 17:17:45

标签: linux dbus systemd bus

系统221中的

总线API。 当我在系统总线中请求某个对象的名称时,它会输出一个错误,说明" Permission denied"。我以root身份运行输出文件。行" sd_bus_request_name(总线," 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;
} 

2 个答案:

答案 0 :(得分:4)

典型的默认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了解详情。

答案 1 :(得分:-2)

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;
    }
}