API sd-bus, sd_bus_request_name returns Permission denied

The bus APIs are in systemd 221. When I request a name for an object on the system bus, it displays the "Permission denied" error message. I am running the output file as root. The string "sd_bus_request_name (bus," net.poettering.Calculator ", 0)" gives an error: "Failed to get the name servie ..: Permission denied"

I think root should have permission to get a name for the object. Does anyone know how to solve this?

thank you in advance.

Here is a sample code from 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;
} 
+4
source share
2

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.

+4
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;
    }
}
-2

Source: https://habr.com/ru/post/1609349/


All Articles