D-bus:无法创建 属性:sd_bus_add_object_vtable 失败

D-bus: Cannot create a property: sd_bus_add_object_vtable fails

我正在尝试在系统总线上创建一个 属性,具有复杂的签名,在我的示例中是两个带符号的整数。源文件test.c:

#include <stdio.h>
#include <stdint.h>
#include <systemd/sd-bus.h>
#include <systemd/sd-bus-vtable.h>

#define DATA_INTERFACE "com.myexample.myproject"
#define DATA_PATH "/com/myexample/myproject"
#define DATA_PROPERTY "data"
#define DATA_SIGNATURE "ii"

static int get_property_cb(sd_bus*_bus,         
                const char* path,
                const char* interface,
                const char* property,
                sd_bus_message* reply,
                void* userdata,
                sd_bus_error* ret_error)
{
    int ret = 1;
    sd_bus_message_open_container(reply, 'r', DATA_SIGNATURE);
    sd_bus_message_append(reply, "i", 111);
    sd_bus_message_append(reply, "i", 222);
    sd_bus_message_close_container(reply);
    return ret;
}

static const sd_bus_vtable prop_spec[3] = {
    SD_BUS_VTABLE_START(0),
    SD_BUS_PROPERTY(DATA_PROPERTY, DATA_SIGNATURE, get_property_cb, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
    SD_BUS_VTABLE_END
};

int main ()
{
    sd_bus* _bus = NULL;
    sd_bus_slot* _slot = NULL;
    int ret = 0;
    ret = sd_bus_open_system(&_bus);
    if(ret < 0) {
        fprintf(stderr, "Error: sd_bus_open ret %d\n", ret);
        return -1;
    }
    ret = sd_bus_request_name(_bus, DATA_INTERFACE, 0);
    if(ret < 0) {
        fprintf(stderr, "Error: sd_bus_request_name ret %d\n", ret);
        return -1;
    }
    ret = sd_bus_add_object_vtable(_bus, &_slot, DATA_PATH, DATA_INTERFACE, prop_spec, NULL);
    if(ret < 0) {
        fprintf(stderr, "Error: sd_bus_add_object_vtable ret %d\n", ret);
        return -1;
    }
    printf("!!!Success!!!\n");
    sd_bus_slot_unref(_slot);
    sd_bus_unref(_bus);
    return 0;
}

我还创建了文件 /usr/share/dbus-1/system.d/com.myexample.myproject.conf (也试过放到/etc/dbus-1/system.d/)

<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN"
 "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
  <policy context="default">
    <allow own="com.myexample.myproject"/>
    <allow send_interface="*"/>
  </policy>
</busconfig>

接口文件/usr/share/dbus-1/interfaces/com.myexample.myproject.xml

<!DOCTYPE node PUBLIC
    "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
    "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd" >
<node xmlns:doc="http://www.freedesktop.org/dbus/1.0/doc.dtd">
    <interface name="com.myexample.myproject">
        <property name="data" type="ii" access="read">
            <annotation name="org.freedesktop.DBus.Property.EmitsChangedSignal" value="true"/>
        </property>
    </interface>
</node>

和/usr/share/dbus-1/services/com.myexample.myproject.service

[D-BUS Service]
Name=com.myexample.myproject
Exec=/home/user/test

我运行它作为root:

sudo ./test

绞尽脑汁为什么 sd_bus_add_object_vtable returns -22 (-EINVAL)

有人知道怎么回事吗?

签名必须写在括号内:

#define DATA_SIGNATURE "(ii)"

也不需要文件 /usr/share/dbus-1/services/com.myexample.myproject.service 和 /usr/share/dbus-1/interfaces/com.myexample.myproject.xml