D-bus:无法创建属性:sd_bus_add_object_vtable 失败
Posted
技术标签:
【中文标题】D-bus:无法创建属性:sd_bus_add_object_vtable 失败【英文标题】:D-bus: Cannot create a property: sd_bus_add_object_vtable fails 【发布时间】:2020-06-30 12:32:50 【问题描述】:我正在尝试在系统总线上创建一个具有复杂签名的属性,在我的示例中是两个有符号整数。源文件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 返回 -22 (-EINVAL)
有人知道怎么回事吗?
【问题讨论】:
【参考方案1】:签名必须写在括号中:
#define DATA_SIGNATURE "(ii)"
也不需要文件 /usr/share/dbus-1/services/com.myexample.myproject.service 和 /usr/share/dbus-1/interfaces/com.myexample.myproject.xml
【讨论】:
以上是关于D-bus:无法创建属性:sd_bus_add_object_vtable 失败的主要内容,如果未能解决你的问题,请参考以下文章
使用 gdbus-codegen 骨架时如何验证 D-Bus 属性