bus: demarshal gvariant

This commit is contained in:
Lennart Poettering 2013-12-09 23:03:21 +01:00
parent d3a485135a
commit 6647dc6659
10 changed files with 1157 additions and 368 deletions

View File

@ -2162,11 +2162,15 @@ test_bus_gvariant_LDADD = \
libsystemd-id128-internal.la \
libsystemd-daemon-internal.la \
libsystemd-shared.la \
$(GLIB_LIBS)
libsystemd-bus-dump.la \
libsystemd-capability.la \
$(GLIB_LIBS) \
$(CAP_LIBS)
test_bus_gvariant_CFLAGS = \
$(AM_CFLAGS) \
$(GLIB_CFLAGS)
$(CAP_CFLAGS)
test_bus_creds_SOURCES = \
src/libsystemd-bus/test-bus-creds.c

View File

@ -57,3 +57,7 @@ And that's already it.
Note: on kdbus only native endian messages marshalled in gvariant may
be sent. If a client receives a message in non-native endianness
or in dbus1 marshalling it shall ignore the message.
Note: The GVariant "MAYBE" type is not supported, so that messages can
be fully converted forth and back between dbus1 and gvariant
representations.

View File

@ -24,30 +24,90 @@
#include "bus-gvariant.h"
#include "bus-signature.h"
int bus_gvariant_get_size(char c) {
int bus_gvariant_get_size(const char *signature) {
const char *p;
int sum = 0, r;
switch (c) {
/* For fixed size structs. Fails for variable size structs. */
case SD_BUS_TYPE_BOOLEAN:
case SD_BUS_TYPE_BYTE:
return 1;
p = signature;
while (*p != 0) {
size_t n;
case SD_BUS_TYPE_INT16:
case SD_BUS_TYPE_UINT16:
return 2;
r = signature_element_length(p, &n);
if (r < 0)
return r;
else {
char t[n+1];
case SD_BUS_TYPE_INT32:
case SD_BUS_TYPE_UINT32:
case SD_BUS_TYPE_UNIX_FD:
return 4;
memcpy(t, p, n);
t[n] = 0;
case SD_BUS_TYPE_INT64:
case SD_BUS_TYPE_UINT64:
case SD_BUS_TYPE_DOUBLE:
return 8;
r = bus_gvariant_get_alignment(t);
if (r < 0)
return r;
sum = ALIGN_TO(sum, r);
}
switch (*p) {
case SD_BUS_TYPE_BOOLEAN:
case SD_BUS_TYPE_BYTE:
sum += 1;
break;
case SD_BUS_TYPE_INT16:
case SD_BUS_TYPE_UINT16:
sum += 2;
break;
case SD_BUS_TYPE_INT32:
case SD_BUS_TYPE_UINT32:
case SD_BUS_TYPE_UNIX_FD:
sum += 4;
break;
case SD_BUS_TYPE_INT64:
case SD_BUS_TYPE_UINT64:
case SD_BUS_TYPE_DOUBLE:
sum += 8;
break;
case SD_BUS_TYPE_STRUCT_BEGIN:
case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
char t[n-1];
memcpy(t, p + 1, n - 2);
t[n - 2] = 0;
r = bus_gvariant_get_size(t);
if (r < 0)
return r;
sum += r;
break;
}
case SD_BUS_TYPE_STRING:
case SD_BUS_TYPE_OBJECT_PATH:
case SD_BUS_TYPE_SIGNATURE:
case SD_BUS_TYPE_ARRAY:
case SD_BUS_TYPE_VARIANT:
return -EINVAL;
default:
assert_not_reached("Unknown signature type");
}
p += n;
}
return -EINVAL;
r = bus_gvariant_get_alignment(signature);
if (r < 0)
return r;
return ALIGN_TO(sum, r);
}
int bus_gvariant_get_alignment(const char *signature) {

View File

@ -21,6 +21,6 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
int bus_gvariant_get_size(char c);
int bus_gvariant_get_size(const char *signature);
int bus_gvariant_get_alignment(const char *signature);
int bus_gvariant_is_fixed_size(const char *signature);

View File

@ -159,7 +159,6 @@ struct sd_bus {
bool match_callbacks_modified:1;
bool filter_callbacks_modified:1;
bool nodes_modified:1;
bool use_gvariant:1;
int use_memfd;

View File

@ -254,7 +254,7 @@ static int bus_message_setup_kmsg(sd_bus *b, sd_bus_message *m) {
m->kdbus->dst_id =
well_known ? 0 :
m->destination ? unique : KDBUS_DST_ID_BROADCAST;
m->kdbus->payload_type = KDBUS_PAYLOAD_DBUS1;
m->kdbus->payload_type = KDBUS_PAYLOAD_DBUS;
m->kdbus->cookie = m->header->serial;
m->kdbus->timeout_ns = m->timeout * NSEC_PER_USEC;
@ -618,7 +618,7 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
assert(bus);
assert(k);
assert(k->payload_type == KDBUS_PAYLOAD_DBUS1);
assert(k->payload_type == KDBUS_PAYLOAD_DBUS);
KDBUS_ITEM_FOREACH(d, k, items) {
size_t l;
@ -885,7 +885,7 @@ int bus_kernel_read_message(sd_bus *bus) {
}
k = (struct kdbus_msg *)((uint8_t *)bus->kdbus_buffer + off);
if (k->payload_type == KDBUS_PAYLOAD_DBUS1)
if (k->payload_type == KDBUS_PAYLOAD_DBUS)
r = bus_kernel_make_message(bus, k);
else if (k->payload_type == KDBUS_PAYLOAD_KERNEL)
r = bus_kernel_translate_message(bus, k);

File diff suppressed because it is too large Load Diff

View File

@ -34,18 +34,20 @@
struct bus_container {
char enclosing;
bool need_offsets:1;
/* Indexes into the signature string */
unsigned index, saved_index;
char *signature;
size_t before, begin, end;
/* dbus1: pointer to the array size value, if this is a value */
uint32_t *array_size;
size_t before, begin;
size_t *offsets;
size_t n_offsets, n_allocated;
bool need_offsets;
/* gvariant: list of offsets to end of children if this is struct/dict entry/array */
size_t *offsets, n_offsets, n_offsets_allocated, offset_index;
size_t item_size;
};
struct bus_header {
@ -98,7 +100,6 @@ struct sd_bus_message {
bool free_fds:1;
bool release_kdbus:1;
bool poisoned:1;
bool is_gvariant:1;
struct bus_header *header;
struct bus_body_part body;
@ -180,6 +181,10 @@ static inline void* BUS_MESSAGE_FIELDS(sd_bus_message *m) {
return (uint8_t*) m->header + sizeof(struct bus_header);
}
static inline bool BUS_MESSAGE_IS_GVARIANT(sd_bus_message *m) {
return m->header->version == 2;
}
int bus_message_seal(sd_bus_message *m, uint64_t serial);
int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz);
int bus_message_read_strv_extend(sd_bus_message *m, char ***l);

View File

@ -331,8 +331,7 @@ enum kdbus_msg_flags {
*/
enum kdbus_payload_type {
KDBUS_PAYLOAD_KERNEL,
KDBUS_PAYLOAD_DBUS1 = 0x4442757356657231ULL, /* 'DBusVer1' */
KDBUS_PAYLOAD_GVARIANT = 0x4756617269616e74ULL, /* 'GVariant' */
KDBUS_PAYLOAD_DBUS = 0x4442757356657231ULL, /* 'DBusVer1' */
};
/**

View File

@ -29,9 +29,11 @@
#include "bus-util.h"
#include "bus-internal.h"
#include "bus-message.h"
#include "bus-dump.h"
static void test_bus_gvariant_is_fixed_size(void) {
assert(bus_gvariant_is_fixed_size("") > 0);
assert(bus_gvariant_is_fixed_size("()") > 0);
assert(bus_gvariant_is_fixed_size("y") > 0);
assert(bus_gvariant_is_fixed_size("u") > 0);
assert(bus_gvariant_is_fixed_size("b") > 0);
@ -54,8 +56,41 @@ static void test_bus_gvariant_is_fixed_size(void) {
assert(bus_gvariant_is_fixed_size("((u)yyy(b(iiivi)))") == 0);
}
static void test_bus_gvariant_get_size(void) {
assert(bus_gvariant_get_size("") == 0);
assert(bus_gvariant_get_size("()") == 0);
assert(bus_gvariant_get_size("y") == 1);
assert(bus_gvariant_get_size("u") == 4);
assert(bus_gvariant_get_size("b") == 1);
assert(bus_gvariant_get_size("n") == 2);
assert(bus_gvariant_get_size("q") == 2);
assert(bus_gvariant_get_size("i") == 4);
assert(bus_gvariant_get_size("t") == 8);
assert(bus_gvariant_get_size("d") == 8);
assert(bus_gvariant_get_size("s") < 0);
assert(bus_gvariant_get_size("o") < 0);
assert(bus_gvariant_get_size("g") < 0);
assert(bus_gvariant_get_size("h") == 4);
assert(bus_gvariant_get_size("ay") < 0);
assert(bus_gvariant_get_size("v") < 0);
assert(bus_gvariant_get_size("(u)") == 4);
assert(bus_gvariant_get_size("(uuuuy)") == 20);
assert(bus_gvariant_get_size("(uusuuy)") < 0);
assert(bus_gvariant_get_size("a{ss}") < 0);
assert(bus_gvariant_get_size("((u)yyy(b(iiii)))") == 28);
assert(bus_gvariant_get_size("((u)yyy(b(iiivi)))") < 0);
assert(bus_gvariant_get_size("((b)(t))") == 16);
assert(bus_gvariant_get_size("((b)(b)(t))") == 16);
assert(bus_gvariant_get_size("(bt)") == 16);
assert(bus_gvariant_get_size("((t)(b))") == 16);
assert(bus_gvariant_get_size("(tb)") == 16);
assert(bus_gvariant_get_size("((b)(b))") == 2);
assert(bus_gvariant_get_size("((t)(t))") == 16);
}
static void test_bus_gvariant_get_alignment(void) {
assert(bus_gvariant_get_alignment("") == 1);
assert(bus_gvariant_get_alignment("()") == 1);
assert(bus_gvariant_get_alignment("y") == 1);
assert(bus_gvariant_get_alignment("b") == 1);
assert(bus_gvariant_get_alignment("u") == 4);
@ -79,18 +114,32 @@ static void test_bus_gvariant_get_alignment(void) {
assert(bus_gvariant_get_alignment("(ss)") == 1);
assert(bus_gvariant_get_alignment("(ssu)") == 4);
assert(bus_gvariant_get_alignment("a(ssu)") == 4);
assert(bus_gvariant_get_alignment("(u)") == 4);
assert(bus_gvariant_get_alignment("(uuuuy)") == 4);
assert(bus_gvariant_get_alignment("(uusuuy)") == 4);
assert(bus_gvariant_get_alignment("a{ss}") == 1);
assert(bus_gvariant_get_alignment("((u)yyy(b(iiii)))") == 4);
assert(bus_gvariant_get_alignment("((u)yyy(b(iiivi)))") == 8);
assert(bus_gvariant_get_alignment("((b)(t))") == 8);
assert(bus_gvariant_get_alignment("((b)(b)(t))") == 8);
assert(bus_gvariant_get_alignment("(bt)") == 8);
assert(bus_gvariant_get_alignment("((t)(b))") == 8);
assert(bus_gvariant_get_alignment("(tb)") == 8);
assert(bus_gvariant_get_alignment("((b)(b))") == 1);
assert(bus_gvariant_get_alignment("((t)(t))") == 8);
}
static void test_marshal(void) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL;
_cleanup_bus_message_unref_ sd_bus_message *m = NULL, *n = NULL;
_cleanup_bus_unref_ sd_bus *bus = NULL;
_cleanup_free_ void *blob;
size_t sz;
assert_se(sd_bus_open_system(&bus) >= 0);
bus->use_gvariant = true; /* dirty hack */
bus->message_version = 2; /* dirty hack to enable gvariant*/
assert_se(sd_bus_message_new_method_call(bus, "a.service.name", "/an/object/path/which/is/really/really/long/so/that/we/hit/the/eight/bit/boundary/by/quite/some/margin/to/test/this/stuff/that/it/really/works", "an.interface.name", "AMethodName", &m) >= 0);
/* assert_se(sd_bus_message_append(m, "ssy(sts)v", "first-string-parameter", "second-string-parameter", 9, "a", (uint64_t) 7777, "b", "(su)", "xxx", 4712) >= 0); */
assert_se(sd_bus_message_append(m,
"a(usv)", 2,
4711, "first-string-parameter", "(st)", "X", (uint64_t) 1111,
@ -121,11 +170,20 @@ static void test_marshal(void) {
}
#endif
assert_se(bus_message_dump(m, NULL, true) >= 0);
assert_se(bus_message_get_blob(m, &blob, &sz) >= 0);
assert_se(bus_message_from_malloc(NULL, blob, sz, NULL, 0, NULL, NULL, &n) >= 0);
blob = NULL;
assert_se(bus_message_dump(n, NULL, true) >= 0);
}
int main(int argc, char *argv[]) {
test_bus_gvariant_is_fixed_size();
test_bus_gvariant_get_size();
test_bus_gvariant_get_alignment();
test_marshal();