#include "bus-type.h"
#include "bus-signature.h"
#include "bus-gvariant.h"
+#include "bus-util.h"
static int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored);
* can't be sealed yet. */
if (!part->sealed)
- bus_kernel_push_memfd(m->bus, part->memfd, part->data, part->mapped);
+ bus_kernel_push_memfd(m->bus, part->memfd, part->data, part->mapped, part->allocated);
else {
if (part->mapped > 0)
assert_se(munmap(part->data, part->mapped) == 0);
assert(m);
- for (i = 0; i < m->n_containers; i++)
+ for (i = 0; i < m->n_containers; i++) {
free(m->containers[i].signature);
+ free(m->containers[i].offsets);
+ }
free(m->containers);
m->containers = NULL;
- m->n_containers = 0;
+ m->n_containers = m->containers_allocated = 0;
m->root_container.index = 0;
}
message_reset_containers(m);
free(m->root_container.signature);
+ free(m->root_container.offsets);
- free(m->peeked_signature);
+ free(m->root_container.peeked_signature);
bus_creds_done(&m->creds);
free(m);
/* Signature "(yv)" where the variant contains "s" */
- if (m->is_gvariant) {
+ if (BUS_MESSAGE_IS_GVARIANT(m)) {
/* (field id byte + 7x padding, ((string + NUL) + NUL + signature string 's') */
p = message_extend_fields(m, 8, 1 + 7 + l + 1 + 1 + 1, true);
p[8+l+2] = type;
if (ret)
- *ret = (char*) p + 1;
+ *ret = (char*) p + 8;
} else {
/* (field id byte + (signature length + signature 's' + NUL) + (string length + string + NUL)) */
/* Signature "(yv)" where the variant contains "g" */
- if (m->is_gvariant)
+ if (BUS_MESSAGE_IS_GVARIANT(m))
/* For gvariant the serialization is the same as for normal strings */
return message_append_field_string(m, h, 'g', s, ret);
else {
assert(m);
- if (m->is_gvariant) {
+ if (BUS_MESSAGE_IS_GVARIANT(m)) {
/* (field id byte + 7x padding + ((value + NUL + signature string 'u') */
p = message_extend_fields(m, 8, 1 + 7 + 4 + 1 + 1, true);
return -EBADMSG;
h = buffer;
- if (h->version != 1)
+ if (h->version != 1 &&
+ h->version != 2)
return -EBADMSG;
if (h->serial == 0)
m->header->type = type;
m->header->version = bus ? bus->message_version : 1;
m->allow_fds = !bus || bus->can_fds || (bus->state != BUS_HELLO && bus->state != BUS_RUNNING);
- m->root_container.need_offsets = m->is_gvariant = bus ? bus->use_gvariant : false;
+ m->root_container.need_offsets = BUS_MESSAGE_IS_GVARIANT(m);
if (bus)
m->bus = sd_bus_ref(bus);
return -ENOMEM;
t->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED;
- t->reply_serial = BUS_MESSAGE_SERIAL(call);
+ t->reply_cookie = BUS_MESSAGE_COOKIE(call);
- r = message_append_field_uint32(t, BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_serial);
+ r = message_append_field_uint32(t, BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_cookie);
if (r < 0)
goto fail;
int bus_message_new_synthetic_error(
sd_bus *bus,
- uint64_t serial,
+ uint64_t cookie,
const sd_bus_error *e,
sd_bus_message **m) {
return -ENOMEM;
t->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED;
- t->reply_serial = serial;
+ t->reply_cookie = cookie;
- r = message_append_field_uint32(t, BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_serial);
+ r = message_append_field_uint32(t, BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_cookie);
if (r < 0)
goto fail;
}
_public_ sd_bus_message* sd_bus_message_unref(sd_bus_message *m) {
- assert_return(m, NULL);
+
+ if (!m)
+ return NULL;
assert(m->n_ref > 0);
m->n_ref--;
return 0;
}
-_public_ int sd_bus_message_get_serial(sd_bus_message *m, uint64_t *serial) {
+_public_ int sd_bus_message_get_cookie(sd_bus_message *m, uint64_t *cookie) {
assert_return(m, -EINVAL);
- assert_return(serial, -EINVAL);
- assert_return(m->header->serial != 0, -ENOENT);
+ assert_return(cookie, -EINVAL);
+ assert_return(m->header->serial != 0, -ENODATA);
- *serial = BUS_MESSAGE_SERIAL(m);
+ *cookie = BUS_MESSAGE_COOKIE(m);
return 0;
}
-_public_ int sd_bus_message_get_reply_serial(sd_bus_message *m, uint64_t *serial) {
+_public_ int sd_bus_message_get_reply_cookie(sd_bus_message *m, uint64_t *cookie) {
assert_return(m, -EINVAL);
- assert_return(serial, -EINVAL);
- assert_return(m->reply_serial != 0, -ENOENT);
+ assert_return(cookie, -EINVAL);
+ assert_return(m->reply_cookie != 0, -ENODATA);
- *serial = m->reply_serial;
+ *cookie = m->reply_cookie;
return 0;
}
return -ENOMEM;
if (!part->data && part->memfd < 0)
- part->memfd = bus_kernel_pop_memfd(m->bus, &part->data, &part->mapped);
+ part->memfd = bus_kernel_pop_memfd(m->bus, &part->data, &part->mapped, &part->allocated);
if (part->memfd >= 0) {
- uint64_t u = sz;
- r = ioctl(part->memfd, KDBUS_CMD_MEMFD_SIZE_SET, &u);
- if (r < 0) {
- m->poisoned = true;
- return -errno;
+ if (part->allocated == 0 || sz > part->allocated) {
+ uint64_t new_allocated;
+
+ new_allocated = PAGE_ALIGN(sz > 0 ? 2 * sz : 1);
+ r = ioctl(part->memfd, KDBUS_CMD_MEMFD_SIZE_SET, &new_allocated);
+ if (r < 0) {
+ m->poisoned = true;
+ return -errno;
+ }
+
+ part->allocated = new_allocated;
}
if (!part->data || sz > part->mapped) {
- size_t psz = PAGE_ALIGN(sz > 0 ? sz : 1);
+ size_t psz;
+ psz = PAGE_ALIGN(sz > 0 ? sz : 1);
if (part->mapped <= 0)
n = mmap(NULL, psz, PROT_READ|PROT_WRITE, MAP_SHARED, part->memfd, 0);
else
part->munmap_this = true;
} else {
- n = realloc(part->data, MAX(sz, 1u));
- if (!n) {
- m->poisoned = true;
- return -ENOMEM;
- }
+ if (part->allocated == 0 || sz > part->allocated) {
+ size_t new_allocated;
- part->data = n;
- part->free_this = true;
+ new_allocated = sz > 0 ? 2 * sz : 64;
+ n = realloc(part->data, new_allocated);
+ if (!n) {
+ m->poisoned = true;
+ return -ENOMEM;
+ }
+
+ part->data = n;
+ part->allocated = new_allocated;
+ part->free_this = true;
+ }
}
if (q)
struct bus_container *c;
assert(m);
- assert(m->is_gvariant);
+ assert(BUS_MESSAGE_IS_GVARIANT(m));
/* Add offset to current container, unless this is the first
* item in it, which will have the 0 offset, which we can
if (!c->need_offsets)
return 0;
- if (!GREEDY_REALLOC(c->offsets, c->n_allocated, c->n_offsets + 1))
+ if (!GREEDY_REALLOC(c->offsets, c->offsets_allocated, c->n_offsets + 1))
return -ENOMEM;
c->offsets[c->n_offsets++] = offset;
}
static void *message_extend_body(sd_bus_message *m, size_t align, size_t sz, bool add_offset) {
- struct bus_body_part *part = NULL;
- size_t start_body, end_body, padding, start_part, end_part, added;
- bool add_new_part;
+ size_t start_body, end_body, padding, added;
void *p;
int r;
return NULL;
}
- add_new_part =
- m->n_body_parts <= 0 ||
- m->body_end->sealed ||
- padding != ALIGN_TO(m->body_end->size, align) - m->body_end->size;
+ if (added > 0) {
+ struct bus_body_part *part = NULL;
+ bool add_new_part;
+
+ add_new_part =
+ m->n_body_parts <= 0 ||
+ m->body_end->sealed ||
+ padding != ALIGN_TO(m->body_end->size, align) - m->body_end->size;
+
+ if (add_new_part) {
+ if (padding > 0) {
+ part = message_append_part(m);
+ if (!part)
+ return NULL;
+
+ part_zero(part, padding);
+ }
- if (add_new_part) {
- if (padding > 0) {
part = message_append_part(m);
if (!part)
return NULL;
- part_zero(part, padding);
- }
+ r = part_make_space(m, part, sz, &p);
+ if (r < 0)
+ return NULL;
+ } else {
+ struct bus_container *c;
+ void *op;
+ size_t os, start_part, end_part;
- part = message_append_part(m);
- if (!part)
- return NULL;
+ part = m->body_end;
+ op = part->data;
+ os = part->size;
- r = part_make_space(m, part, sz, &p);
- if (r < 0)
- return NULL;
- } else {
- struct bus_container *c;
- void *op;
- size_t os;
+ start_part = ALIGN_TO(part->size, align);
+ end_part = start_part + sz;
- part = m->body_end;
- op = part->data;
- os = part->size;
+ r = part_make_space(m, part, end_part, &p);
+ if (r < 0)
+ return NULL;
- start_part = ALIGN_TO(part->size, align);
- end_part = start_part + sz;
+ if (padding > 0) {
+ memset(p, 0, padding);
+ p = (uint8_t*) p + padding;
+ }
- r = part_make_space(m, part, end_part, &p);
- if (r < 0)
- return NULL;
+ /* Readjust pointers */
+ for (c = m->containers; c < m->containers + m->n_containers; c++)
+ c->array_size = adjust_pointer(c->array_size, op, os, part->data);
- if (padding > 0) {
- memset(p, 0, padding);
- p = (uint8_t*) p + padding;
+ m->error.message = (const char*) adjust_pointer(m->error.message, op, os, part->data);
}
-
- /* Readjust pointers */
- for (c = m->containers; c < m->containers + m->n_containers; c++)
- c->array_size = adjust_pointer(c->array_size, op, os, part->data);
-
- m->error.message = (const char*) adjust_pointer(m->error.message, op, os, part->data);
- }
+ } else
+ /* Return something that is not NULL and is aligned */
+ p = (uint8_t *) NULL + align;
m->header->body_size = end_body;
message_extend_containers(m, added);
}
}
- if (m->is_gvariant) {
+ if (BUS_MESSAGE_IS_GVARIANT(m)) {
uint8_t u8;
uint32_t u32;
default:
align = bus_gvariant_get_alignment(CHAR_TO_STR(type));
- sz = bus_gvariant_get_size(type);
+ sz = bus_gvariant_get_size(CHAR_TO_STR(type));
break;
}
case SD_BUS_TYPE_BOOLEAN:
u32 = p && *(int*) p;
+ p = &u32;
+
align = sz = 4;
break;
}
}
- if (m->is_gvariant) {
+ if (BUS_MESSAGE_IS_GVARIANT(m)) {
a = message_extend_body(m, 1, size + 1, true);
if (!a)
return -ENOMEM;
nindex = e - c->signature;
}
- if (m->is_gvariant) {
+ if (BUS_MESSAGE_IS_GVARIANT(m)) {
alignment = bus_gvariant_get_alignment(contents);
if (alignment < 0)
return alignment;
}
}
- if (m->is_gvariant) {
+ if (BUS_MESSAGE_IS_GVARIANT(m)) {
/* Variants are always aligned to 8 */
if (!message_extend_body(m, 8, 0, false))
nindex = e - c->signature;
}
- if (m->is_gvariant) {
+ if (BUS_MESSAGE_IS_GVARIANT(m)) {
int alignment;
alignment = bus_gvariant_get_alignment(contents);
} else
return -ENXIO;
- if (m->is_gvariant) {
+ if (BUS_MESSAGE_IS_GVARIANT(m)) {
int alignment;
alignment = bus_gvariant_get_alignment(contents);
assert_return(!m->poisoned, -ESTALE);
/* Make sure we have space for one more container */
- w = realloc(m->containers, sizeof(struct bus_container) * (m->n_containers + 1));
- if (!w) {
+ if (!GREEDY_REALLOC(m->containers, m->containers_allocated, m->n_containers + 1)) {
m->poisoned = true;
return -ENOMEM;
}
- m->containers = w;
-
c = message_get_container(m);
signature = strdup(contents);
}
/* OK, let's fill it in */
- w += m->n_containers++;
+ w = m->containers + m->n_containers++;
w->enclosing = type;
w->signature = signature;
w->index = 0;
w->array_size = array_size;
w->before = before;
w->begin = begin;
- w->n_offsets = w->n_allocated = 0;
+ w->n_offsets = w->offsets_allocated = 0;
w->offsets = NULL;
w->need_offsets = need_offsets;
return 0;
}
+static size_t determine_word_size(size_t sz, size_t extra) {
+ if (sz + extra <= 0xFF)
+ return 1;
+ else if (sz + extra*2 <= 0xFFFF)
+ return 2;
+ else if (sz + extra*4 <= 0xFFFFFFFF)
+ return 4;
+ else
+ return 8;
+}
+
+static size_t read_word_le(void *p, size_t sz) {
+ union {
+ uint16_t u16;
+ uint32_t u32;
+ uint64_t u64;
+ } x;
+
+ assert(p);
+
+ if (sz == 1)
+ return *(uint8_t*) p;
+
+ memcpy(&x, p, sz);
+
+ if (sz == 2)
+ return le16toh(x.u16);
+ else if (sz == 4)
+ return le32toh(x.u32);
+ else if (sz == 4)
+ return le64toh(x.u64);
+
+ assert_not_reached("unknown word width");
+}
+
+static void write_word_le(void *p, size_t sz, size_t value) {
+ union {
+ uint16_t u16;
+ uint32_t u32;
+ uint64_t u64;
+ } x;
+
+ assert(p);
+ assert(sz == 8 || (value < (1ULL << (sz*8))));
+
+ if (sz == 1) {
+ *(uint8_t*) p = value;
+ return;
+ } else if (sz == 2)
+ x.u16 = htole16((uint16_t) value);
+ else if (sz == 4)
+ x.u32 = htole32((uint32_t) value);
+ else if (sz == 8)
+ x.u64 = htole64((uint64_t) value);
+ else
+ assert_not_reached("unknown word width");
+
+ memcpy(p, &x, sz);
+}
+
static int bus_message_close_array(sd_bus_message *m, struct bus_container *c) {
assert(m);
assert(c);
- if (!m->is_gvariant)
+ if (!BUS_MESSAGE_IS_GVARIANT(m))
return 0;
if (c->need_offsets) {
+ size_t payload, sz, i;
uint8_t *a;
- unsigned i;
- size_t sz;
/* Variable-width arrays */
- sz = c->n_offsets > 0 ? c->offsets[c->n_offsets-1] - c->begin : 0;
- if (sz + c->n_offsets <= 0xFF) {
- a = message_extend_body(m, 1, c->n_offsets, true);
- if (!a)
- return -ENOMEM;
-
- for (i = 0; i < c->n_offsets; i++)
- a[i] = (uint8_t) (c->offsets[i] - c->begin);
-
- } else if (sz + c->n_offsets*2 <= 0xFFFF) {
- a = message_extend_body(m, 1, c->n_offsets * 2, true);
- if (!a)
- return -ENOMEM;
-
- for (i = 0; i < c->n_offsets; i++) {
- uint16_t x = htole16((uint16_t) (c->offsets[i] - c->begin));
- memcpy(a + (i*2), &x, 2);
- }
-
- } else if (sz + c->n_offsets*4 <= 0xFFFFFFFF) {
- a = message_extend_body(m, 1, c->n_offsets * 4, true);
- if (!a)
- return -ENOMEM;
-
- for (i = 0; i < c->n_offsets; i++) {
- uint32_t x = htole32((uint32_t) (c->offsets[i] - c->begin));
- memcpy(a + (i*4), &x, 4);
- }
- } else {
- a = message_extend_body(m, 1, c->n_offsets * 8, true);
- if (!a)
- return -ENOMEM;
+ payload = c->n_offsets > 0 ? c->offsets[c->n_offsets-1] - c->begin : 0;
+ sz = determine_word_size(payload, c->n_offsets);
- for (i = 0; i < c->n_offsets; i++) {
- uint64_t x = htole64((uint64_t) (c->offsets[i] - c->begin));
- memcpy(a + (i*8), &x, 8);
- }
- }
+ a = message_extend_body(m, 1, sz * c->n_offsets, true);
+ if (!a)
+ return -ENOMEM;
+ for (i = 0; i < c->n_offsets; i++)
+ write_word_le(a + sz*i, sz, c->offsets[i] - c->begin);
} else {
void *a;
assert(m);
assert(c);
- if (!m->is_gvariant)
+ if (!BUS_MESSAGE_IS_GVARIANT(m))
return 0;
l = strlen(c->signature);
assert(m);
assert(c);
- if (!m->is_gvariant)
+ if (!BUS_MESSAGE_IS_GVARIANT(m))
return 0;
- p = c->signature;
+ p = strempty(c->signature);
while (*p != 0) {
size_t n;
return r;
}
- assert(i <= c->n_offsets);
+ assert(!c->need_offsets || i <= c->n_offsets);
/* We need to add an offset for each item that has a
* variable size and that is not the last one in the
* list */
if (r == 0 && p[n] != 0)
n_variable++;
- else
- c->offsets[i] = (size_t) -1;
i++;
p += n;
}
+ assert(!c->need_offsets || i == c->n_offsets);
+ assert(c->need_offsets || n_variable == 0);
+
if (n_variable <= 0) {
- a = message_extend_body(m, 1, 0, true);
+ a = message_extend_body(m, 1, 0, add_offset);
if (!a)
return -ENOMEM;
} else {
- size_t sz, z;
+ size_t sz;
unsigned j;
- assert(c->offsets[c->n_offsets-1] == m->header->body_size ||
- c->offsets[c->n_offsets-1] == (size_t) -1);
+ assert(c->offsets[c->n_offsets-1] == m->header->body_size);
- sz = m->header->body_size - c->begin;
- if (sz + n_variable <= 0xFF)
- z = 1;
- else if (sz + (n_variable)*2 <= 0xFFFF)
- z = 2;
- else if (sz + (n_variable)*4 <= 0xFFFFFFFF)
- z = 4;
- else
- z = 8;
+ sz = determine_word_size(m->header->body_size - c->begin, n_variable);
- a = message_extend_body(m, 1, z * n_variable, add_offset);
+ a = message_extend_body(m, 1, sz * n_variable, add_offset);
if (!a)
return -ENOMEM;
+ p = strempty(c->signature);
for (i = 0, j = 0; i < c->n_offsets; i++) {
- size_t v;
unsigned k;
+ size_t n;
+
+ r = signature_element_length(p, &n);
+ if (r < 0)
+ return r;
+ else {
+ char t[n+1];
- if (c->offsets[i] == (size_t) -1)
- continue;
+ memcpy(t, p, n);
+ t[n] = 0;
+
+ p += n;
+
+ r = bus_gvariant_is_fixed_size(t);
+ if (r < 0)
+ return r;
+ if (r > 0 || p[0] == 0)
+ continue;
+ }
- v = c->offsets[i] - c->begin;
k = n_variable - 1 - j;
- if (z == 1)
- ((uint8_t*) a)[k] = (uint8_t) v;
- else if (z == 2) {
- uint16_t x = htole16((uint16_t) v);
- memcpy(a + k * 2, &x, 2);
- } else if (z == 4) {
- uint32_t x = htole32((uint32_t) v);
- memcpy(a + k * 4, &x, 4);
- } else if (z == 8) {
- uint64_t x = htole64((uint64_t) v);
- memcpy(a + k * 8, &x, 8);
- } else
- assert_not_reached("Wrong offset width");
+ write_word_le(a + k * sz, sz, c->offsets[i] - c->begin);
j++;
}
assert_not_reached("Unknown container type");
free(c->signature);
+ free(c->offsets);
+
return r;
}
assert_return(m, -EINVAL);
assert_return(!m->sealed, -EPERM);
- assert_return(bus_type_is_trivial(type), -EINVAL);
+ assert_return(bus_type_is_trivial(type) && type != SD_BUS_TYPE_BOOLEAN, -EINVAL);
assert_return(ptr || size == 0, -EINVAL);
assert_return(!m->poisoned, -ESTALE);
+ /* alignment and size of the trivial types (except bool) is
+ * identical for gvariant and dbus1 marshalling */
align = bus_type_get_alignment(type);
sz = bus_type_get_size(type);
part->size = size;
copy_fd = -1;
- message_extend_containers(m, size);
m->header->body_size += size;
+ message_extend_containers(m, size);
return sd_bus_message_close_container(m);
}
}
}
- if (!m->is_gvariant) {
+ if (!BUS_MESSAGE_IS_GVARIANT(m)) {
a = message_extend_body(m, 4, 4, false);
if (!a)
return -ENOMEM;
m->header->body_size += size;
message_extend_containers(m, size);
- if (m->is_gvariant) {
+ if (BUS_MESSAGE_IS_GVARIANT(m)) {
r = message_add_offset(m, m->header->body_size);
if (r < 0) {
m->poisoned = true;
return sd_bus_message_close_container(m);
}
+static int bus_message_close_header(sd_bus_message *m) {
+ uint8_t *a;
+ size_t sz, i;
+
+ assert(m);
+
+ if (!BUS_MESSAGE_IS_GVARIANT(m))
+ return 0;
+
+ if (m->n_header_offsets < 1)
+ return 0;
+
+ assert(m->header->fields_size == m->header_offsets[m->n_header_offsets-1]);
+
+ sz = determine_word_size(m->header->fields_size, m->n_header_offsets);
+
+ a = message_extend_fields(m, 1, sz * m->n_header_offsets, false);
+ if (!a)
+ return -ENOMEM;
+
+ for (i = 0; i < m->n_header_offsets; i++)
+ write_word_le(a + sz*i, sz, m->header_offsets[i]);
+
+ return 0;
+}
+
+int bus_message_seal(sd_bus_message *m, uint64_t cookie, usec_t timeout) {
+ struct bus_body_part *part;
+ size_t l, a;
+ unsigned i;
+ int r;
+
+ assert(m);
+
+ if (m->sealed)
+ return -EPERM;
+
+ if (m->n_containers > 0)
+ return -EBADMSG;
+
+ if (m->poisoned)
+ return -ESTALE;
+
+ /* In vtables the return signature of method calls is listed,
+ * let's check if they match if this is a response */
+ if (m->header->type == SD_BUS_MESSAGE_METHOD_RETURN &&
+ m->enforced_reply_signature &&
+ !streq(strempty(m->root_container.signature), m->enforced_reply_signature))
+ return -ENOMSG;
+
+ /* If gvariant marshalling is used we need to close the body structure */
+ r = bus_message_close_struct(m, &m->root_container, false);
+ if (r < 0)
+ return r;
+
+ /* If there's a non-trivial signature set, then add it in here */
+ if (!isempty(m->root_container.signature)) {
+ r = message_append_field_signature(m, BUS_MESSAGE_HEADER_SIGNATURE, m->root_container.signature, NULL);
+ if (r < 0)
+ return r;
+ }
+
+ if (m->n_fds > 0) {
+ r = message_append_field_uint32(m, BUS_MESSAGE_HEADER_UNIX_FDS, m->n_fds);
+ if (r < 0)
+ return r;
+ }
+
+ r = bus_message_close_header(m);
+ if (r < 0)
+ return r;
+
+ m->header->serial = (uint32_t) cookie;
+ m->timeout = m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED ? 0 : timeout;
+
+ /* Add padding at the end of the fields part, since we know
+ * the body needs to start at an 8 byte alignment. We made
+ * sure we allocated enough space for this, so all we need to
+ * do here is to zero it out. */
+ l = BUS_MESSAGE_FIELDS_SIZE(m);
+ a = ALIGN8(l) - l;
+ if (a > 0)
+ memset((uint8_t*) BUS_MESSAGE_FIELDS(m) + l, 0, a);
+
+ /* If this is something we can send as memfd, then let's seal
+ the memfd now. Note that we can send memfds as payload only
+ for directed messages, and not for broadcasts. */
+ if (m->destination && m->bus && m->bus->use_memfd) {
+ MESSAGE_FOREACH_PART(part, i, m)
+ if (part->memfd >= 0 && !part->sealed && (part->size > MEMFD_MIN_SIZE || m->bus->use_memfd < 0)) {
+ uint64_t sz;
+
+ /* Try to seal it if that makes
+ * sense. First, unmap our own map to
+ * make sure we don't keep it busy. */
+ bus_body_part_unmap(part);
+
+ /* Then, sync up real memfd size */
+ sz = part->size;
+ if (ioctl(part->memfd, KDBUS_CMD_MEMFD_SIZE_SET, &sz) < 0)
+ return -errno;
+
+ /* Finally, try to seal */
+ if (ioctl(part->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 1) >= 0)
+ part->sealed = true;
+ }
+ }
+
+ m->root_container.end = BUS_MESSAGE_BODY_SIZE(m);
+ m->root_container.index = 0;
+ m->root_container.offset_index = 0;
+ m->root_container.item_size = m->root_container.n_offsets > 0 ? m->root_container.offsets[0] : 0;
+
+ m->sealed = true;
+
+ return 0;
+}
+
int bus_body_part_map(struct bus_body_part *part) {
void *p;
size_t psz;
assert(m);
c = message_get_container(m);
- if (!c->array_size)
+ if (c->enclosing != SD_BUS_TYPE_ARRAY)
return false;
- return index >= c->begin + BUS_MESSAGE_BSWAP32(m, *c->array_size);
+ if (BUS_MESSAGE_IS_GVARIANT(m))
+ return index >= c->end;
+ else {
+ assert(c->array_size);
+ return index >= c->begin + BUS_MESSAGE_BSWAP32(m, *c->array_size);
+ }
}
_public_ int sd_bus_message_at_end(sd_bus_message *m, int complete) {
return NULL;
}
+static int container_next_item(sd_bus_message *m, struct bus_container *c, size_t *rindex) {
+ int r;
+
+ assert(m);
+ assert(c);
+ assert(rindex);
+
+ if (!BUS_MESSAGE_IS_GVARIANT(m))
+ return 0;
+
+ if (c->enclosing == SD_BUS_TYPE_ARRAY) {
+ int sz;
+
+ sz = bus_gvariant_get_size(c->signature);
+ if (sz < 0) {
+ int alignment;
+
+ if (c->offset_index+1 >= c->n_offsets)
+ goto end;
+
+ /* Variable-size array */
+
+ alignment = bus_gvariant_get_alignment(c->signature);
+ assert(alignment > 0);
+
+ *rindex = ALIGN_TO(c->offsets[c->offset_index], alignment);
+ c->item_size = c->offsets[c->offset_index+1] - *rindex;
+ } else {
+
+ if (c->offset_index+1 >= (c->end-c->begin)/sz)
+ goto end;
+
+ /* Fixed-size array */
+ *rindex = c->begin + (c->offset_index+1) * sz;
+ c->item_size = sz;
+ }
+
+ c->offset_index++;
+
+ } else if (c->enclosing == 0 ||
+ c->enclosing == SD_BUS_TYPE_STRUCT ||
+ c->enclosing == SD_BUS_TYPE_DICT_ENTRY) {
+
+ int alignment;
+ size_t n, j;
+
+ if (c->offset_index+1 >= c->n_offsets)
+ goto end;
+
+ r = signature_element_length(c->signature + c->index, &n);
+ if (r < 0)
+ return r;
+
+ r = signature_element_length(c->signature + c->index + n, &j);
+ if (r < 0)
+ return r;
+ else {
+ char t[j+1];
+ memcpy(t, c->signature + c->index + n, j);
+ t[j] = 0;
+
+ alignment = bus_gvariant_get_alignment(t);
+ }
+
+ assert(alignment > 0);
+
+ *rindex = ALIGN_TO(c->offsets[c->offset_index], alignment);
+ c->item_size = c->offsets[c->offset_index+1] - *rindex;
+
+ c->offset_index++;
+
+ } else if (c->enclosing == SD_BUS_TYPE_VARIANT)
+ goto end;
+ else
+ assert_not_reached("Unknown container type");
+
+ return 0;
+
+end:
+ /* Reached the end */
+ *rindex = c->end;
+ c->item_size = 0;
+ return 0;
+}
+
+
static int message_peek_body(
sd_bus_message *m,
size_t *rindex,
assert(rindex);
assert(align > 0);
- if (message_end_of_array(m, *rindex))
- return 0;
-
start = ALIGN_TO((size_t) *rindex, align);
padding = start - *rindex;
end = start + nbytes;
}
part = find_part(m, start, nbytes, (void**) &q);
- if (!part || !q)
+ if (!part || (nbytes > 0 && !q))
return -EBADMSG;
*rindex = end;
if (ret)
*ret = q;
- return 1;
+ return 0;
}
static bool validate_nul(const char *s, size_t l) {
_public_ int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p) {
struct bus_container *c;
+ size_t rindex;
void *q;
int r;
if (c->signature[c->index] != type)
return -ENXIO;
- switch (type) {
+ rindex = m->rindex;
- case SD_BUS_TYPE_STRING:
- case SD_BUS_TYPE_OBJECT_PATH: {
- uint32_t l;
- size_t rindex;
+ if (BUS_MESSAGE_IS_GVARIANT(m)) {
- rindex = m->rindex;
- r = message_peek_body(m, &rindex, 4, 4, &q);
- if (r <= 0)
- return r;
+ if (IN_SET(type, SD_BUS_TYPE_STRING, SD_BUS_TYPE_OBJECT_PATH, SD_BUS_TYPE_SIGNATURE)) {
+ bool ok;
- l = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
- r = message_peek_body(m, &rindex, 1, l+1, &q);
- if (r < 0)
- return r;
- if (r == 0)
- return -EBADMSG;
+ r = message_peek_body(m, &rindex, 1, c->item_size, &q);
+ if (r < 0)
+ return r;
+
+ if (type == SD_BUS_TYPE_STRING)
+ ok = validate_string(q, c->item_size-1);
+ else if (type == SD_BUS_TYPE_OBJECT_PATH)
+ ok = validate_object_path(q, c->item_size-1);
+ else
+ ok = validate_signature(q, c->item_size-1);
- if (type == SD_BUS_TYPE_OBJECT_PATH) {
- if (!validate_object_path(q, l))
+ if (!ok)
return -EBADMSG;
+
+ if (p)
+ *(const char**) p = q;
} else {
- if (!validate_string(q, l))
+ int sz, align;
+
+ sz = bus_gvariant_get_size(CHAR_TO_STR(type));
+ assert(sz > 0);
+ if ((size_t) sz != c->item_size)
return -EBADMSG;
- }
- m->rindex = rindex;
- if (p)
- *(const char**) p = q;
+ align = bus_gvariant_get_alignment(CHAR_TO_STR(type));
+ assert(align > 0);
- break;
- }
+ r = message_peek_body(m, &rindex, align, c->item_size, &q);
+ if (r < 0)
+ return r;
- case SD_BUS_TYPE_SIGNATURE: {
- uint8_t l;
- size_t rindex;
+ switch (type) {
- rindex = m->rindex;
- r = message_peek_body(m, &rindex, 1, 1, &q);
- if (r <= 0)
- return r;
+ case SD_BUS_TYPE_BYTE:
+ if (p)
+ *(uint8_t*) p = *(uint8_t*) q;
+ break;
- l = *(uint8_t*) q;
- r = message_peek_body(m, &rindex, 1, l+1, &q);
- if (r < 0)
- return r;
- if (r == 0)
- return -EBADMSG;
+ case SD_BUS_TYPE_BOOLEAN:
+ if (p)
+ *(int*) p = !!*(uint8_t*) q;
+ break;
- if (!validate_signature(q, l))
- return -EBADMSG;
+ case SD_BUS_TYPE_INT16:
+ case SD_BUS_TYPE_UINT16:
+ if (p)
+ *(uint16_t*) p = BUS_MESSAGE_BSWAP16(m, *(uint16_t*) q);
+ break;
- m->rindex = rindex;
+ case SD_BUS_TYPE_INT32:
+ case SD_BUS_TYPE_UINT32:
+ if (p)
+ *(uint32_t*) p = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
+ break;
- if (p)
- *(const char**) p = q;
- break;
- }
+ case SD_BUS_TYPE_INT64:
+ case SD_BUS_TYPE_UINT64:
+ case SD_BUS_TYPE_DOUBLE:
+ if (p)
+ *(uint64_t*) p = BUS_MESSAGE_BSWAP64(m, *(uint64_t*) q);
+ break;
- default: {
- ssize_t sz, align;
- size_t rindex;
+ case SD_BUS_TYPE_UNIX_FD: {
+ uint32_t j;
- align = bus_type_get_alignment(type);
- sz = bus_type_get_size(type);
- assert(align > 0 && sz > 0);
+ j = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
+ if (j >= m->n_fds)
+ return -EBADMSG;
- rindex = m->rindex;
- r = message_peek_body(m, &rindex, align, sz, &q);
- if (r <= 0)
+ if (p)
+ *(int*) p = m->fds[j];
+
+ break;
+ }
+
+ default:
+ assert_not_reached("unexpected type");
+ }
+ }
+
+ r = container_next_item(m, c, &rindex);
+ if (r < 0)
return r;
+ } else {
- switch (type) {
+ rindex = m->rindex;
- case SD_BUS_TYPE_BYTE:
- if (p)
- *(uint8_t*) p = *(uint8_t*) q;
- break;
+ if (IN_SET(type, SD_BUS_TYPE_STRING, SD_BUS_TYPE_OBJECT_PATH)) {
+ uint32_t l;
+ bool ok;
- case SD_BUS_TYPE_BOOLEAN:
- if (p)
- *(int*) p = !!*(uint32_t*) q;
- break;
+ r = message_peek_body(m, &rindex, 4, 4, &q);
+ if (r < 0)
+ return r;
- case SD_BUS_TYPE_INT16:
- case SD_BUS_TYPE_UINT16:
- if (p)
- *(uint16_t*) p = BUS_MESSAGE_BSWAP16(m, *(uint16_t*) q);
- break;
+ l = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
+ r = message_peek_body(m, &rindex, 1, l+1, &q);
+ if (r < 0)
+ return r;
- case SD_BUS_TYPE_INT32:
- case SD_BUS_TYPE_UINT32:
- if (p)
- *(uint32_t*) p = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
- break;
+ if (type == SD_BUS_TYPE_OBJECT_PATH)
+ ok = validate_object_path(q, l);
+ else
+ ok = validate_string(q, l);
+ if (!ok)
+ return -EBADMSG;
- case SD_BUS_TYPE_INT64:
- case SD_BUS_TYPE_UINT64:
- case SD_BUS_TYPE_DOUBLE:
if (p)
- *(uint64_t*) p = BUS_MESSAGE_BSWAP64(m, *(uint64_t*) q);
- break;
+ *(const char**) p = q;
- case SD_BUS_TYPE_UNIX_FD: {
- uint32_t j;
+ } else if (type == SD_BUS_TYPE_SIGNATURE) {
+ uint8_t l;
- j = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
- if (j >= m->n_fds)
+ r = message_peek_body(m, &rindex, 1, 1, &q);
+ if (r < 0)
+ return r;
+
+ l = *(uint8_t*) q;
+ r = message_peek_body(m, &rindex, 1, l+1, &q);
+ if (r < 0)
+ return r;
+
+ if (!validate_signature(q, l))
return -EBADMSG;
if (p)
- *(int*) p = m->fds[j];
- break;
- }
+ *(const char**) p = q;
- default:
- assert_not_reached("Unknown basic type...");
- }
+ } else {
+ ssize_t sz, align;
- m->rindex = rindex;
+ align = bus_type_get_alignment(type);
+ assert(align > 0);
- break;
- }
+ sz = bus_type_get_size(type);
+ assert(sz > 0);
+
+ r = message_peek_body(m, &rindex, align, sz, &q);
+ if (r < 0)
+ return r;
+
+ switch (type) {
+
+ case SD_BUS_TYPE_BYTE:
+ if (p)
+ *(uint8_t*) p = *(uint8_t*) q;
+ break;
+
+ case SD_BUS_TYPE_BOOLEAN:
+ if (p)
+ *(int*) p = !!*(uint32_t*) q;
+ break;
+
+ case SD_BUS_TYPE_INT16:
+ case SD_BUS_TYPE_UINT16:
+ if (p)
+ *(uint16_t*) p = BUS_MESSAGE_BSWAP16(m, *(uint16_t*) q);
+ break;
+
+ case SD_BUS_TYPE_INT32:
+ case SD_BUS_TYPE_UINT32:
+ if (p)
+ *(uint32_t*) p = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
+ break;
+
+ case SD_BUS_TYPE_INT64:
+ case SD_BUS_TYPE_UINT64:
+ case SD_BUS_TYPE_DOUBLE:
+ if (p)
+ *(uint64_t*) p = BUS_MESSAGE_BSWAP64(m, *(uint64_t*) q);
+ break;
+
+ case SD_BUS_TYPE_UNIX_FD: {
+ uint32_t j;
+
+ j = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
+ if (j >= m->n_fds)
+ return -EBADMSG;
+
+ if (p)
+ *(int*) p = m->fds[j];
+ break;
+ }
+
+ default:
+ assert_not_reached("Unknown basic type...");
+ }
+ }
}
+ m->rindex = rindex;
+
if (c->enclosing != SD_BUS_TYPE_ARRAY)
c->index++;
sd_bus_message *m,
struct bus_container *c,
const char *contents,
- uint32_t **array_size) {
+ uint32_t **array_size,
+ size_t *item_size,
+ size_t **offsets,
+ size_t *n_offsets) {
size_t rindex;
void *q;
assert(c);
assert(contents);
assert(array_size);
+ assert(item_size);
+ assert(offsets);
+ assert(n_offsets);
if (!signature_is_single(contents, true))
return -EINVAL;
- alignment = bus_type_get_alignment(contents[0]);
- if (alignment < 0)
- return alignment;
-
if (!c->signature || c->signature[c->index] == 0)
- return 0;
+ return -ENXIO;
if (c->signature[c->index] != SD_BUS_TYPE_ARRAY)
return -ENXIO;
return -ENXIO;
rindex = m->rindex;
- r = message_peek_body(m, &rindex, 4, 4, &q);
- if (r <= 0)
- return r;
- if (BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q) > BUS_ARRAY_MAX_SIZE)
- return -EBADMSG;
+ if (!BUS_MESSAGE_IS_GVARIANT(m)) {
+ /* dbus1 */
- r = message_peek_body(m, &rindex, alignment, 0, NULL);
- if (r < 0)
- return r;
- if (r == 0)
- return -EBADMSG;
+ r = message_peek_body(m, &rindex, 4, 4, &q);
+ if (r < 0)
+ return r;
- if (c->enclosing != SD_BUS_TYPE_ARRAY)
- c->index += 1 + strlen(contents);
+ if (BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q) > BUS_ARRAY_MAX_SIZE)
+ return -EBADMSG;
+
+ alignment = bus_type_get_alignment(contents[0]);
+ if (alignment < 0)
+ return alignment;
+
+ r = message_peek_body(m, &rindex, alignment, 0, NULL);
+ if (r < 0)
+ return r;
+
+ *array_size = (uint32_t*) q;
+
+ } else if (c->item_size <= 0) {
+
+ /* gvariant: empty array */
+ *item_size = 0;
+ *offsets = NULL;
+ *n_offsets = 0;
+
+ } else if (bus_gvariant_is_fixed_size(contents)) {
+
+ /* gvariant: fixed length array */
+ *item_size = bus_gvariant_get_size(contents);
+ *offsets = NULL;
+ *n_offsets = 0;
+
+ } else {
+ size_t where, p = 0, framing, sz;
+ unsigned i;
+
+ /* gvariant: variable length array */
+ sz = determine_word_size(c->item_size, 0);
+
+ where = rindex + c->item_size - sz;
+ r = message_peek_body(m, &where, 1, sz, &q);
+ if (r < 0)
+ return r;
+
+ framing = read_word_le(q, sz);
+ if (framing > c->item_size - sz)
+ return -EBADMSG;
+ if ((c->item_size - framing) % sz != 0)
+ return -EBADMSG;
+
+ *n_offsets = (c->item_size - framing) / sz;
+
+ where = rindex + framing;
+ r = message_peek_body(m, &where, 1, *n_offsets * sz, &q);
+ if (r < 0)
+ return r;
+
+ *offsets = new(size_t, *n_offsets);
+ if (!*offsets)
+ return -ENOMEM;
+
+ for (i = 0; i < *n_offsets; i++) {
+ size_t x;
+
+ x = read_word_le((uint8_t*) q + i * sz, sz);
+ if (x > c->item_size - sz)
+ return -EBADMSG;
+ if (x < p)
+ return -EBADMSG;
+
+ (*offsets)[i] = rindex + x;
+ p = x;
+ }
+
+ *item_size = (*offsets)[0] - rindex;
+ }
m->rindex = rindex;
- *array_size = (uint32_t*) q;
+ if (c->enclosing != SD_BUS_TYPE_ARRAY)
+ c->index += 1 + strlen(contents);
return 1;
}
static int bus_message_enter_variant(
sd_bus_message *m,
struct bus_container *c,
- const char *contents) {
+ const char *contents,
+ size_t *item_size) {
size_t rindex;
uint8_t l;
assert(m);
assert(c);
assert(contents);
+ assert(item_size);
if (!signature_is_single(contents, false))
return -EINVAL;
return -EINVAL;
if (!c->signature || c->signature[c->index] == 0)
- return 0;
+ return -ENXIO;
if (c->signature[c->index] != SD_BUS_TYPE_VARIANT)
return -ENXIO;
rindex = m->rindex;
- r = message_peek_body(m, &rindex, 1, 1, &q);
- if (r <= 0)
- return r;
- l = *(uint8_t*) q;
- r = message_peek_body(m, &rindex, 1, l+1, &q);
- if (r < 0)
- return r;
- if (r == 0)
- return -EBADMSG;
+ if (BUS_MESSAGE_IS_GVARIANT(m)) {
+ size_t k, where;
- if (!validate_signature(q, l))
- return -EBADMSG;
+ k = strlen(contents);
+ if (1+k > c->item_size)
+ return -EBADMSG;
- if (!streq(q, contents))
- return -ENXIO;
+ where = rindex + c->item_size - (1+k);
+ r = message_peek_body(m, &where, 1, 1+k, &q);
+ if (r < 0)
+ return r;
- if (c->enclosing != SD_BUS_TYPE_ARRAY)
- c->index++;
+ if (*(char*) q != 0)
+ return -EBADMSG;
+
+ if (memcmp((uint8_t*) q+1, contents, k))
+ return -ENXIO;
+
+ *item_size = c->item_size - (1+k);
+
+ } else {
+ r = message_peek_body(m, &rindex, 1, 1, &q);
+ if (r < 0)
+ return r;
+
+ l = *(uint8_t*) q;
+ r = message_peek_body(m, &rindex, 1, l+1, &q);
+ if (r < 0)
+ return r;
+
+ if (!validate_signature(q, l))
+ return -EBADMSG;
+
+ if (!streq(q, contents))
+ return -ENXIO;
+ }
m->rindex = rindex;
+ if (c->enclosing != SD_BUS_TYPE_ARRAY)
+ c->index++;
+
return 1;
}
+static int build_struct_offsets(
+ sd_bus_message *m,
+ const char *signature,
+ size_t size,
+ size_t *item_size,
+ size_t **offsets,
+ size_t *n_offsets) {
+
+ unsigned n_variable = 0, n_total = 0, v;
+ size_t previous = 0, where;
+ const char *p;
+ size_t sz;
+ void *q;
+ int r;
+
+ assert(m);
+ assert(item_size);
+ assert(offsets);
+ assert(n_offsets);
+
+ if (isempty(signature)) {
+ *item_size = 0;
+ *offsets = NULL;
+ *n_offsets = 0;
+ return 0;
+ }
+
+ sz = determine_word_size(size, 0);
+ if (sz <= 0)
+ return -EBADMSG;
+
+ /* First, loop over signature and count variable elements and
+ * elements in general. We use this to know how large the
+ * offset array is at the end of the structure. Note that
+ * GVariant only stores offsets for all variable size elements
+ * that are not the last item. */
+
+ p = signature;
+ while (*p != 0) {
+ size_t n;
+
+ r = signature_element_length(p, &n);
+ if (r < 0)
+ return r;
+ else {
+ char t[n+1];
+
+ memcpy(t, p, n);
+ t[n] = 0;
+
+ r = bus_gvariant_is_fixed_size(t);
+ }
+
+ if (r < 0)
+ return r;
+ if (r == 0 && p[n] != 0) /* except the last item */
+ n_variable ++;
+ n_total++;
+
+ p += n;
+ }
+
+ if (size < n_variable * sz)
+ return -EBADMSG;
+
+ where = m->rindex + size - (n_variable * sz);
+ r = message_peek_body(m, &where, 1, n_variable * sz, &q);
+ if (r < 0)
+ return r;
+
+ v = n_variable;
+
+ *offsets = new(size_t, n_total);
+ if (!*offsets)
+ return -ENOMEM;
+
+ *n_offsets = 0;
+
+ /* Second, loop again and build an offset table */
+ p = signature;
+ while (*p != 0) {
+ size_t n, offset;
+ int k;
+
+ r = signature_element_length(p, &n);
+ if (r < 0)
+ return r;
+ else {
+ char t[n+1];
+
+ memcpy(t, p, n);
+ t[n] = 0;
+
+ k = bus_gvariant_get_size(t);
+ if (k < 0) {
+ size_t x;
+
+ /* variable size */
+ if (v > 0) {
+ v--;
+
+ x = read_word_le((uint8_t*) q + v*sz, sz);
+ if (x >= size)
+ return -EBADMSG;
+ if (m->rindex + x < previous)
+ return -EBADMSG;
+ } else
+ /* The last item's end
+ * is determined from
+ * the start of the
+ * offset array */
+ x = size - (n_variable * sz);
+
+ offset = m->rindex + x;
+
+ } else {
+ size_t align;
+
+ /* fixed size */
+ align = bus_gvariant_get_alignment(t);
+ assert(align > 0);
+
+ offset = (*n_offsets == 0 ? m->rindex : ALIGN_TO((*offsets)[*n_offsets-1], align)) + k;
+ }
+ }
+
+ previous = (*offsets)[(*n_offsets)++] = offset;
+ p += n;
+ }
+
+ assert(v == 0);
+ assert(*n_offsets == n_total);
+
+ *item_size = (*offsets)[0] - m->rindex;
+ return 0;
+}
+
+static int enter_struct_or_dict_entry(
+ sd_bus_message *m,
+ struct bus_container *c,
+ const char *contents,
+ size_t *item_size,
+ size_t **offsets,
+ size_t *n_offsets) {
+
+ int r;
+
+ assert(m);
+ assert(c);
+ assert(contents);
+ assert(item_size);
+ assert(offsets);
+ assert(n_offsets);
+
+ if (!BUS_MESSAGE_IS_GVARIANT(m)) {
+
+ /* dbus1 */
+ r = message_peek_body(m, &m->rindex, 8, 0, NULL);
+ if (r < 0)
+ return r;
+
+ } else if (c->item_size <= 0) {
+
+ /* gvariant empty struct */
+ *item_size = 0;
+ *offsets = NULL;
+ *n_offsets = 0;
+ } else
+ /* gvariant with contents */
+ return build_struct_offsets(m, contents, c->item_size, item_size, offsets, n_offsets);
+
+ return 0;
+}
+
static int bus_message_enter_struct(
sd_bus_message *m,
struct bus_container *c,
- const char *contents) {
+ const char *contents,
+ size_t *item_size,
+ size_t **offsets,
+ size_t *n_offsets) {
size_t l;
int r;
assert(m);
assert(c);
assert(contents);
+ assert(item_size);
+ assert(offsets);
+ assert(n_offsets);
if (!signature_is_valid(contents, false))
return -EINVAL;
if (!c->signature || c->signature[c->index] == 0)
- return 0;
+ return -ENXIO;
l = strlen(contents);
c->signature[c->index + 1 + l] != SD_BUS_TYPE_STRUCT_END)
return -ENXIO;
- r = message_peek_body(m, &m->rindex, 8, 0, NULL);
- if (r <= 0)
+ r = enter_struct_or_dict_entry(m, c, contents, item_size, offsets, n_offsets);
+ if (r < 0)
return r;
if (c->enclosing != SD_BUS_TYPE_ARRAY)
static int bus_message_enter_dict_entry(
sd_bus_message *m,
struct bus_container *c,
- const char *contents) {
+ const char *contents,
+ size_t *item_size,
+ size_t **offsets,
+ size_t *n_offsets) {
size_t l;
int r;
c->signature[c->index + 1 + l] != SD_BUS_TYPE_DICT_ENTRY_END)
return -ENXIO;
- r = message_peek_body(m, &m->rindex, 8, 0, NULL);
- if (r <= 0)
+ r = enter_struct_or_dict_entry(m, c, contents, item_size, offsets, n_offsets);
+ if (r < 0)
return r;
if (c->enclosing != SD_BUS_TYPE_ARRAY)
uint32_t *array_size = NULL;
char *signature;
size_t before;
+ size_t *offsets = NULL;
+ size_t n_offsets = 0, item_size = 0;
int r;
assert_return(m, -EINVAL);
/* Allow entering into anonymous containers */
r = sd_bus_message_peek_type(m, &tt, &cc);
- if (r <= 0)
+ if (r < 0)
return r;
if (type != 0 && type != tt)
if (m->n_containers >= BUS_CONTAINER_DEPTH)
return -EBADMSG;
- w = realloc(m->containers, sizeof(struct bus_container) * (m->n_containers + 1));
- if (!w)
+ if (!GREEDY_REALLOC(m->containers, m->containers_allocated, m->n_containers + 1))
return -ENOMEM;
- m->containers = w;
if (message_end_of_signature(m))
return -ENXIO;
before = m->rindex;
if (type == SD_BUS_TYPE_ARRAY)
- r = bus_message_enter_array(m, c, contents, &array_size);
+ r = bus_message_enter_array(m, c, contents, &array_size, &item_size, &offsets, &n_offsets);
else if (type == SD_BUS_TYPE_VARIANT)
- r = bus_message_enter_variant(m, c, contents);
+ r = bus_message_enter_variant(m, c, contents, &item_size);
else if (type == SD_BUS_TYPE_STRUCT)
- r = bus_message_enter_struct(m, c, contents);
+ r = bus_message_enter_struct(m, c, contents, &item_size, &offsets, &n_offsets);
else if (type == SD_BUS_TYPE_DICT_ENTRY)
- r = bus_message_enter_dict_entry(m, c, contents);
+ r = bus_message_enter_dict_entry(m, c, contents, &item_size, &offsets, &n_offsets);
else
r = -EINVAL;
if (r <= 0) {
free(signature);
+ free(offsets);
return r;
}
/* OK, let's fill it in */
- w += m->n_containers++;
+ w = m->containers + m->n_containers++;
w->enclosing = type;
w->signature = signature;
+ w->peeked_signature = NULL;
w->index = 0;
- w->array_size = array_size;
+
w->before = before;
w->begin = m->rindex;
+ w->end = m->rindex + c->item_size;
+
+ w->array_size = array_size;
+ w->item_size = item_size;
+ w->offsets = offsets;
+ w->n_offsets = n_offsets;
+ w->offset_index = 0;
return 1;
}
_public_ int sd_bus_message_exit_container(sd_bus_message *m) {
struct bus_container *c;
+ unsigned saved;
+ int r;
assert_return(m, -EINVAL);
assert_return(m->sealed, -EPERM);
assert_return(m->n_containers > 0, -ENXIO);
c = message_get_container(m);
- if (c->enclosing == SD_BUS_TYPE_ARRAY) {
+
+ if (c->enclosing != SD_BUS_TYPE_ARRAY) {
+ if (c->signature && c->signature[c->index] != 0)
+ return -EBUSY;
+ }
+
+ if (BUS_MESSAGE_IS_GVARIANT(m)) {
+ if (m->rindex < c->end)
+ return -EBUSY;
+
+ } else if (c->enclosing == SD_BUS_TYPE_ARRAY) {
uint32_t l;
l = BUS_MESSAGE_BSWAP32(m, *c->array_size);
if (c->begin + l != m->rindex)
return -EBUSY;
-
- } else {
- if (c->signature && c->signature[c->index] != 0)
- return -EBUSY;
}
free(c->signature);
+ free(c->peeked_signature);
+ free(c->offsets);
m->n_containers--;
+ c = message_get_container(m);
+
+ saved = c->index;
+ c->index = c->saved_index;
+ r = container_next_item(m, c, &m->rindex);
+ c->index = saved;
+ if (r < 0)
+ return r;
+
return 1;
}
/* Free container */
free(c->signature);
+ free(c->offsets);
m->n_containers--;
/* Correct index of new top-level container */
if (!sig)
return -ENOMEM;
- free(m->peeked_signature);
- m->peeked_signature = sig;
-
- *contents = sig;
+ free(c->peeked_signature);
+ *contents = c->peeked_signature = sig;
}
if (type)
if (!sig)
return -ENOMEM;
- free(m->peeked_signature);
- m->peeked_signature = sig;
-
- *contents = sig;
+ free(c->peeked_signature);
+ *contents = c->peeked_signature = sig;
}
if (type)
if (c->signature[c->index] == SD_BUS_TYPE_VARIANT) {
if (contents) {
- size_t rindex, l;
void *q;
- rindex = m->rindex;
- r = message_peek_body(m, &rindex, 1, 1, &q);
- if (r < 0)
- return r;
- if (r == 0)
- goto eof;
+ if (BUS_MESSAGE_IS_GVARIANT(m)) {
+ size_t k;
- l = *(uint8_t*) q;
- r = message_peek_body(m, &rindex, 1, l+1, &q);
- if (r < 0)
- return r;
- if (r == 0)
- return -EBADMSG;
+ if (c->item_size < 2)
+ return -EBADMSG;
+
+ /* Look for the NUL delimiter that
+ separates the payload from the
+ signature. Since the body might be
+ in a different part that then the
+ signature we map byte by byte. */
+
+ for (k = 2; k <= c->item_size; k++) {
+ size_t where;
+
+ where = m->rindex + c->item_size - k;
+ r = message_peek_body(m, &where, 1, k, &q);
+ if (r < 0)
+ return r;
+
+ if (*(char*) q == 0)
+ break;
+ }
+
+ if (k > c->item_size)
+ return -EBADMSG;
+
+ free(c->peeked_signature);
+ c->peeked_signature = strndup((char*) q + 1, k - 1);
+ if (!c->peeked_signature)
+ return -ENOMEM;
+
+ if (!signature_is_valid(c->peeked_signature, true))
+ return -EBADMSG;
+
+ *contents = c->peeked_signature;
+ } else {
+ size_t rindex, l;
+
+ rindex = m->rindex;
+ r = message_peek_body(m, &rindex, 1, 1, &q);
+ if (r < 0)
+ return r;
- if (!validate_signature(q, l))
- return -EBADMSG;
+ l = *(uint8_t*) q;
+ r = message_peek_body(m, &rindex, 1, l+1, &q);
+ if (r < 0)
+ return r;
+
+ if (!validate_signature(q, l))
+ return -EBADMSG;
- *contents = q;
+ *contents = q;
+ }
}
if (type)
if (complete) {
message_reset_containers(m);
m->rindex = 0;
- m->root_container.index = 0;
c = message_get_container(m);
} else {
c = message_get_container(m);
+ c->offset_index = 0;
c->index = 0;
m->rindex = c->begin;
}
+ c->offset_index = 0;
+ c->item_size = (c->n_offsets > 0 ? c->offsets[0] : c->end) - c->begin;
+
return !isempty(c->signature);
}
assert_return(size, -EINVAL);
assert_return(!BUS_MESSAGE_NEED_BSWAP(m), -ENOTSUP);
- align = bus_type_get_alignment(type);
- if (align < 0)
- return align;
-
r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, CHAR_TO_STR(type));
if (r <= 0)
return r;
c = message_get_container(m);
- sz = BUS_MESSAGE_BSWAP32(m, *c->array_size);
+
+ if (BUS_MESSAGE_IS_GVARIANT(m)) {
+ align = bus_gvariant_get_alignment(CHAR_TO_STR(type));
+ if (align < 0)
+ return align;
+
+ sz = c->end - c->begin;
+ } else {
+ align = bus_type_get_alignment(type);
+ if (align < 0)
+ return align;
+
+ sz = BUS_MESSAGE_BSWAP32(m, *c->array_size);
+ }
if (sz == 0)
/* Zero length array, let's return some aligned
r = message_peek_body(m, &m->rindex, align, sz, &p);
if (r < 0)
goto fail;
- if (r == 0) {
- r = -EBADMSG;
- goto fail;
- }
}
r = sd_bus_message_exit_container(m);
static int message_peek_field_uint32(
sd_bus_message *m,
size_t *ri,
+ size_t item_size,
uint32_t *ret) {
int r;
assert(m);
assert(ri);
+ if (BUS_MESSAGE_IS_GVARIANT(m) && item_size != 4)
+ return -EBADMSG;
+
+ /* identical for gvariant and dbus1 */
+
r = message_peek_fields(m, ri, 4, 4, &q);
if (r < 0)
return r;
sd_bus_message *m,
bool (*validate)(const char *p),
size_t *ri,
+ size_t item_size,
const char **ret) {
uint32_t l;
assert(m);
assert(ri);
- r = message_peek_field_uint32(m, ri, &l);
- if (r < 0)
- return r;
+ if (BUS_MESSAGE_IS_GVARIANT(m)) {
- r = message_peek_fields(m, ri, 1, l+1, &q);
- if (r < 0)
- return r;
+ if (item_size <= 0)
+ return -EBADMSG;
+
+ r = message_peek_fields(m, ri, 1, item_size, &q);
+ if (r < 0)
+ return r;
+
+ l = item_size - 1;
+ } else {
+ r = message_peek_field_uint32(m, ri, 4, &l);
+ if (r < 0)
+ return r;
+
+ r = message_peek_fields(m, ri, 1, l+1, &q);
+ if (r < 0)
+ return r;
+ }
if (validate) {
if (!validate_nul(q, l))
static int message_peek_field_signature(
sd_bus_message *m,
size_t *ri,
+ size_t item_size,
const char **ret) {
size_t l;
assert(m);
assert(ri);
- r = message_peek_fields(m, ri, 1, 1, &q);
- if (r < 0)
- return r;
+ if (BUS_MESSAGE_IS_GVARIANT(m)) {
- l = *(uint8_t*) q;
- r = message_peek_fields(m, ri, 1, l+1, &q);
- if (r < 0)
- return r;
+ if (item_size <= 0)
+ return -EBADMSG;
+
+ r = message_peek_fields(m, ri, 1, item_size, &q);
+ if (r < 0)
+ return r;
+
+ l = item_size - 1;
+ } else {
+ r = message_peek_fields(m, ri, 1, 1, &q);
+ if (r < 0)
+ return r;
+
+ l = *(uint8_t*) q;
+ r = message_peek_fields(m, ri, 1, l+1, &q);
+ if (r < 0)
+ return r;
+ }
if (!validate_signature(q, l))
return -EBADMSG;
assert(m);
assert(ri);
assert(signature);
+ assert(!BUS_MESSAGE_IS_GVARIANT(m));
original_index = *ri;
if (t == SD_BUS_TYPE_STRING) {
- r = message_peek_field_string(m, NULL, ri, NULL);
+ r = message_peek_field_string(m, NULL, ri, 0, NULL);
if (r < 0)
return r;
} else if (t == SD_BUS_TYPE_OBJECT_PATH) {
- r = message_peek_field_string(m, object_path_is_valid, ri, NULL);
+ r = message_peek_field_string(m, object_path_is_valid, ri, 0, NULL);
if (r < 0)
return r;
} else if (t == SD_BUS_TYPE_SIGNATURE) {
- r = message_peek_field_signature(m, ri, NULL);
+ r = message_peek_field_signature(m, ri, 0, NULL);
if (r < 0)
return r;
if (alignment < 0)
return alignment;
- r = message_peek_field_uint32(m, ri, &nas);
+ r = message_peek_field_uint32(m, ri, 0, &nas);
if (r < 0)
return r;
if (nas > BUS_ARRAY_MAX_SIZE)
} else if (t == SD_BUS_TYPE_VARIANT) {
const char *s;
- r = message_peek_field_signature(m, ri, &s);
+ r = message_peek_field_signature(m, ri, 0, &s);
if (r < 0)
return r;
size_t ri;
int r;
uint32_t unix_fds = 0;
+ void *offsets = NULL;
+ unsigned n_offsets = 0;
+ size_t sz = 0;
+ unsigned i = 0;
assert(m);
- for (ri = 0; ri < BUS_MESSAGE_FIELDS_SIZE(m); ) {
+ if (BUS_MESSAGE_IS_GVARIANT(m)) {
+ void *q;
+
+ sz = determine_word_size(BUS_MESSAGE_FIELDS_SIZE(m), 0);
+ if (sz > 0) {
+ size_t framing;
+
+ ri = BUS_MESSAGE_FIELDS_SIZE(m) - sz;
+ r = message_peek_fields(m, &ri, 1, sz, &q);
+ if (r < 0)
+ return r;
+
+ framing = read_word_le(q, sz);
+ if (framing >= BUS_MESSAGE_FIELDS_SIZE(m) - sz)
+ return -EBADMSG;
+ if ((BUS_MESSAGE_FIELDS_SIZE(m) - framing) % sz != 0)
+ return -EBADMSG;
+
+ ri = framing;
+ r = message_peek_fields(m, &ri, 1, BUS_MESSAGE_FIELDS_SIZE(m) - framing, &offsets);
+ if (r < 0)
+ return r;
+
+ n_offsets = (BUS_MESSAGE_FIELDS_SIZE(m) - framing) / sz;
+ }
+ }
+
+ ri = 0;
+ while (ri < BUS_MESSAGE_FIELDS_SIZE(m)) {
+ _cleanup_free_ char *sig = NULL;
const char *signature;
uint8_t *header;
+ size_t item_size = (size_t) -1;
+
+ if (BUS_MESSAGE_IS_GVARIANT(m)) {
+ if (i >= n_offsets)
+ break;
+
+ if (i == 0)
+ ri = 0;
+ else
+ ri = ALIGN_TO(read_word_le((uint8_t*) offsets + (i-1)*sz, sz), 8);
+ }
r = message_peek_fields(m, &ri, 8, 1, (void**) &header);
if (r < 0)
return r;
- r = message_peek_field_signature(m, &ri, &signature);
- if (r < 0)
- return r;
+ if (BUS_MESSAGE_IS_GVARIANT(m)) {
+ size_t where, end;
+ char *b;
+ void *q;
+
+ end = read_word_le((uint8_t*) offsets + i*sz, sz);
+
+ if (end < ri)
+ return -EBADMSG;
+
+ where = ri = ALIGN_TO(ri, 8);
+ item_size = end - ri;
+ r = message_peek_fields(m, &where, 1, item_size, &q);
+ if (r < 0)
+ return r;
+
+ b = memrchr(q, 0, item_size);
+ if (!b)
+ return -EBADMSG;
+
+ sig = strndup(b+1, item_size - (b+1-(char*) q));
+ if (!sig)
+ return -ENOMEM;
+
+ signature = sig;
+ item_size = b - (char*) q;
+ } else {
+ r = message_peek_field_signature(m, &ri, 0, &signature);
+ if (r < 0)
+ return r;
+ }
switch (*header) {
case _BUS_MESSAGE_HEADER_INVALID:
if (!streq(signature, "o"))
return -EBADMSG;
- r = message_peek_field_string(m, object_path_is_valid, &ri, &m->path);
+ r = message_peek_field_string(m, object_path_is_valid, &ri, item_size, &m->path);
break;
case BUS_MESSAGE_HEADER_INTERFACE:
if (!streq(signature, "s"))
return -EBADMSG;
- r = message_peek_field_string(m, interface_name_is_valid, &ri, &m->interface);
+ r = message_peek_field_string(m, interface_name_is_valid, &ri, item_size, &m->interface);
break;
case BUS_MESSAGE_HEADER_MEMBER:
if (!streq(signature, "s"))
return -EBADMSG;
- r = message_peek_field_string(m, member_name_is_valid, &ri, &m->member);
+ r = message_peek_field_string(m, member_name_is_valid, &ri, item_size, &m->member);
break;
case BUS_MESSAGE_HEADER_ERROR_NAME:
if (!streq(signature, "s"))
return -EBADMSG;
- r = message_peek_field_string(m, error_name_is_valid, &ri, &m->error.name);
+ r = message_peek_field_string(m, error_name_is_valid, &ri, item_size, &m->error.name);
if (r >= 0)
m->error._need_free = -1;
if (!streq(signature, "s"))
return -EBADMSG;
- r = message_peek_field_string(m, service_name_is_valid, &ri, &m->destination);
+ r = message_peek_field_string(m, service_name_is_valid, &ri, item_size, &m->destination);
break;
case BUS_MESSAGE_HEADER_SENDER:
if (!streq(signature, "s"))
return -EBADMSG;
- r = message_peek_field_string(m, service_name_is_valid, &ri, &m->sender);
+ r = message_peek_field_string(m, service_name_is_valid, &ri, item_size, &m->sender);
if (r >= 0 && m->sender[0] == ':' && m->bus && m->bus->bus_client && !m->bus->is_kernel) {
m->creds.unique_name = (char*) m->sender;
if (!streq(signature, "g"))
return -EBADMSG;
- r = message_peek_field_signature(m, &ri, &s);
+ r = message_peek_field_signature(m, &ri, item_size, &s);
if (r < 0)
return r;
}
case BUS_MESSAGE_HEADER_REPLY_SERIAL:
- if (m->reply_serial != 0)
+ if (m->reply_cookie != 0)
return -EBADMSG;
if (!streq(signature, "u"))
return -EBADMSG;
- r = message_peek_field_uint32(m, &ri, &m->reply_serial);
+ r = message_peek_field_uint32(m, &ri, item_size, &m->reply_cookie);
if (r < 0)
return r;
- if (m->reply_serial == 0)
+ if (m->reply_cookie == 0)
return -EBADMSG;
break;
if (!streq(signature, "u"))
return -EBADMSG;
- r = message_peek_field_uint32(m, &ri, &unix_fds);
+ r = message_peek_field_uint32(m, &ri, item_size, &unix_fds);
if (r < 0)
return -EBADMSG;
break;
default:
- r = message_skip_fields(m, &ri, (uint32_t) -1, (const char **) &signature);
+ if (!BUS_MESSAGE_IS_GVARIANT(m))
+ r = message_skip_fields(m, &ri, (uint32_t) -1, (const char **) &signature);
}
if (r < 0)
return r;
+
+ i++;
}
if (m->n_fds != unix_fds)
return -EBADMSG;
- if (isempty(m->root_container.signature) != (BUS_MESSAGE_BODY_SIZE(m) == 0))
- return -EBADMSG;
-
switch (m->header->type) {
case SD_BUS_MESSAGE_SIGNAL:
case SD_BUS_MESSAGE_METHOD_RETURN:
- if (m->reply_serial == 0)
+ if (m->reply_cookie == 0)
return -EBADMSG;
break;
case SD_BUS_MESSAGE_METHOD_ERROR:
- if (m->reply_serial == 0 || !m->error.name)
+ if (m->reply_cookie == 0 || !m->error.name)
return -EBADMSG;
break;
}
- /* Try to read the error message, but if we can't it's a non-issue */
- if (m->header->type == SD_BUS_MESSAGE_METHOD_ERROR)
- sd_bus_message_read(m, "s", &m->error.message);
-
- return 0;
-}
-
-static int bus_message_close_header(sd_bus_message *m) {
- uint8_t *a;
- size_t z, i;
-
- assert(m);
-
- if (!m->is_gvariant)
- return 0;
-
- if (m->n_header_offsets <= 1)
- return 0;
-
- assert(m->header->fields_size == m->header_offsets[m->n_header_offsets-1]);
-
- if (m->header->fields_size + m->n_header_offsets <= 0xFF)
- z = 1;
- else if (m->header->fields_size + 2*m->n_header_offsets <= 0xFFFF)
- z = 2;
- else if (m->header->fields_size + 4*m->n_header_offsets <= 0xFFFFFFFF)
- z = 4;
- else
- z = 8;
-
- a = message_extend_fields(m, 1, z * m->n_header_offsets, false);
- if (!a)
- return -ENOMEM;
-
- for (i = 0; i < m->n_header_offsets; i++) {
- if (z == 1)
- ((uint8_t*) a)[i] = (uint8_t) m->header_offsets[i];
- else if (z == 2) {
- uint16_t x = htole16((uint16_t) m->header_offsets[i]);
- memcpy(a + 2*i, &x, 2);
- } else if (z == 4) {
- uint32_t x = htole32((uint32_t) m->header_offsets[i]);
- memcpy(a + 4*i, &x, 4);
- } else if (z == 8) {
- uint64_t x = htole64((uint64_t) m->header_offsets[i]);
- memcpy(a + 8*i, &x, 8);
- } else
- assert_not_reached("unknown type");
- }
-
- return 0;
-}
-
-int bus_message_seal(sd_bus_message *m, uint64_t serial) {
- struct bus_body_part *part;
- size_t l, a;
- unsigned i;
- int r;
-
- assert(m);
-
- if (m->sealed)
- return -EPERM;
-
- if (m->n_containers > 0)
+ /* Refuse non-local messages that claim they are local */
+ if (streq_ptr(m->path, "/org/freedesktop/DBus/Local"))
+ return -EBADMSG;
+ if (streq_ptr(m->interface, "org.freedesktop.DBus.Local"))
+ return -EBADMSG;
+ if (streq_ptr(m->sender, "org.freedesktop.DBus.Local"))
return -EBADMSG;
- if (m->poisoned)
- return -ESTALE;
-
- /* In vtables the return signature of method calls is listed,
- * let's check if they match if this is a response */
- if (m->header->type == SD_BUS_MESSAGE_METHOD_RETURN &&
- m->enforced_reply_signature &&
- !streq(strempty(m->root_container.signature), m->enforced_reply_signature))
- return -ENOMSG;
-
- /* If gvariant marshalling is used we need to close the body structure */
- r = bus_message_close_struct(m, &m->root_container, false);
- if (r < 0)
- return r;
-
- /* If there's a non-trivial signature set, then add it in here */
- if (!isempty(m->root_container.signature)) {
- r = message_append_field_signature(m, BUS_MESSAGE_HEADER_SIGNATURE, m->root_container.signature, NULL);
- if (r < 0)
- return r;
- }
+ m->root_container.end = BUS_MESSAGE_BODY_SIZE(m);
- if (m->n_fds > 0) {
- r = message_append_field_uint32(m, BUS_MESSAGE_HEADER_UNIX_FDS, m->n_fds);
+ if (BUS_MESSAGE_IS_GVARIANT(m)) {
+ r = build_struct_offsets(
+ m,
+ m->root_container.signature,
+ BUS_MESSAGE_BODY_SIZE(m),
+ &m->root_container.item_size,
+ &m->root_container.offsets,
+ &m->root_container.n_offsets);
if (r < 0)
return r;
}
- r = bus_message_close_header(m);
- if (r < 0)
- return r;
-
- /* Add padding at the end of the fields part, since we know
- * the body needs to start at an 8 byte alignment. We made
- * sure we allocated enough space for this, so all we need to
- * do here is to zero it out. */
- l = BUS_MESSAGE_FIELDS_SIZE(m);
- a = ALIGN8(l) - l;
- if (a > 0)
- memset((uint8_t*) BUS_MESSAGE_FIELDS(m) + l, 0, a);
-
- /* If this is something we can send as memfd, then let's seal
- the memfd now. Note that we can send memfds as payload only
- for directed messages, and not for broadcasts. */
- if (m->destination && m->bus && m->bus->use_memfd) {
- MESSAGE_FOREACH_PART(part, i, m)
- if (part->memfd >= 0 && !part->sealed && (part->size > MEMFD_MIN_SIZE || m->bus->use_memfd < 0)) {
- bus_body_part_unmap(part);
-
- if (ioctl(part->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 1) >= 0)
- part->sealed = true;
- }
- }
-
- m->header->serial = serial;
- m->sealed = true;
+ /* Try to read the error message, but if we can't it's a non-issue */
+ if (m->header->type == SD_BUS_MESSAGE_METHOD_ERROR)
+ sd_bus_message_read(m, "s", &m->error.message);
return 0;
}
assert_return(m, NULL);
c = complete ? &m->root_container : message_get_container(m);
- return c->signature ?: "";
+ return strempty(c->signature);
}
_public_ int sd_bus_message_copy(sd_bus_message *m, sd_bus_message *source, int all) {
return m->bus;
}
+
+int bus_message_remarshal(sd_bus *bus, sd_bus_message **m) {
+ _cleanup_bus_message_unref_ sd_bus_message *n = NULL;
+ usec_t timeout;
+ int r;
+
+ assert(bus);
+ assert(m);
+ assert(*m);
+
+ switch ((*m)->header->type) {
+
+ case SD_BUS_MESSAGE_SIGNAL:
+ r = sd_bus_message_new_signal(bus, (*m)->path, (*m)->interface, (*m)->member, &n);
+ if (r < 0)
+ return r;
+
+ break;
+
+ case SD_BUS_MESSAGE_METHOD_CALL:
+ r = sd_bus_message_new_method_call(bus, (*m)->destination, (*m)->path, (*m)->interface, (*m)->member, &n);
+ if (r < 0)
+ return r;
+
+ break;
+
+ case SD_BUS_MESSAGE_METHOD_RETURN:
+ case SD_BUS_MESSAGE_METHOD_ERROR:
+
+ n = message_new(bus, (*m)->header->type);
+ if (!n)
+ return -ENOMEM;
+
+ n->reply_cookie = (*m)->reply_cookie;
+ r = message_append_field_uint32(n, BUS_MESSAGE_HEADER_REPLY_SERIAL, n->reply_cookie);
+ if (r < 0)
+ return r;
+
+ if ((*m)->header->type == SD_BUS_MESSAGE_METHOD_ERROR && (*m)->error.name) {
+ r = message_append_field_string(n, BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, (*m)->error.name, &n->error.message);
+ if (r < 0)
+ return r;
+
+ n->error._need_free = -1;
+ }
+
+ break;
+
+ default:
+ return -EINVAL;
+ }
+
+ if ((*m)->destination && !n->destination) {
+ r = message_append_field_string(n, BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, (*m)->destination, &n->destination);
+ if (r < 0)
+ return r;
+ }
+
+ if ((*m)->sender && !n->sender) {
+ r = message_append_field_string(n, BUS_MESSAGE_HEADER_SENDER, SD_BUS_TYPE_STRING, (*m)->sender, &n->sender);
+ if (r < 0)
+ return r;
+ }
+
+ n->header->flags |= (*m)->header->flags & (BUS_MESSAGE_NO_REPLY_EXPECTED|BUS_MESSAGE_NO_AUTO_START);
+
+ r = sd_bus_message_copy(n, *m, true);
+ if (r < 0)
+ return r;
+
+ timeout = (*m)->timeout;
+ if (timeout == 0 && !((*m)->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED))
+ timeout = BUS_DEFAULT_TIMEOUT;
+
+ r = bus_message_seal(n, BUS_MESSAGE_COOKIE(*m), timeout);
+ if (r < 0)
+ return r;
+
+ sd_bus_message_unref(*m);
+ *m = n;
+ n = NULL;
+
+ return 0;
+}
+
+int bus_message_append_sender(sd_bus_message *m, const char *sender) {
+ assert(m);
+ assert(sender);
+
+ assert_return(!m->sealed, -EPERM);
+ assert_return(!m->sender, -EPERM);
+
+ return message_append_field_string(m, BUS_MESSAGE_HEADER_SENDER, SD_BUS_TYPE_STRING, sender, &m->sender);
+}