-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
+/* SPDX-License-Identifier: LGPL-2.1+ */
/***
This file is part of systemd.
#include <fcntl.h>
#include <sys/mman.h>
-#include "util.h"
-#include "utf8.h"
-#include "strv.h"
-#include "time-util.h"
-#include "cgroup-util.h"
-#include "memfd-util.h"
-
#include "sd-bus.h"
-#include "bus-message.h"
+
+#include "alloc-util.h"
+#include "bus-gvariant.h"
#include "bus-internal.h"
-#include "bus-type.h"
+#include "bus-message.h"
#include "bus-signature.h"
-#include "bus-gvariant.h"
+#include "bus-type.h"
#include "bus-util.h"
+#include "fd-util.h"
+#include "io-util.h"
+#include "memfd-util.h"
+#include "string-util.h"
+#include "strv.h"
+#include "time-util.h"
+#include "utf8.h"
+#include "util.h"
static int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored);
assert(m);
assert(part);
- if (part->memfd >= 0) {
- /* If we can reuse the memfd, try that. For that it
- * can't be sealed yet. */
-
- if (!part->sealed)
- 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);
-
- safe_close(part->memfd);
- }
-
- } else if (part->munmap_this)
- munmap(part->data, part->mapped);
+ if (part->memfd >= 0)
+ close_and_munmap(part->memfd, part->mmap_begin, part->mapped);
+ else if (part->munmap_this)
+ munmap(part->mmap_begin, part->mapped);
else if (part->free_this)
free(part->data);
free(m->containers[i].offsets);
}
- free(m->containers);
- m->containers = NULL;
+ m->containers = mfree(m->containers);
m->n_containers = m->containers_allocated = 0;
m->root_container.index = 0;
message_reset_parts(m);
- if (m->release_kdbus) {
- struct kdbus_cmd_free cmd_free;
-
- cmd_free.flags = 0;
- cmd_free.offset = (uint8_t *)m->kdbus - (uint8_t *)m->bus->kdbus_buffer;
- (void) ioctl(m->bus->input_fd, KDBUS_CMD_FREE, &cmd_free);
- }
-
- if (m->free_kdbus)
- free(m->kdbus);
-
sd_bus_unref(m->bus);
if (m->free_fds) {
if (m->iovec != m->iovec_fixed)
free(m->iovec);
- if (m->destination_ptr) {
- free(m->destination_ptr);
- m->destination_ptr = NULL;
- }
-
+ m->destination_ptr = mfree(m->destination_ptr);
message_reset_containers(m);
free(m->root_container.signature);
free(m->root_container.offsets);
if (m->poisoned)
return NULL;
- old_size = sizeof(struct bus_header) + m->header->fields_size;
+ old_size = sizeof(struct bus_header) + m->fields_size;
start = ALIGN_TO(old_size, align);
new_size = start + sz;
+ if (new_size < start ||
+ new_size > (size_t) ((uint32_t) -1))
+ goto poison;
+
if (old_size == new_size)
return (uint8_t*) m->header + old_size;
- if (new_size > (size_t) ((uint32_t) -1))
- goto poison;
-
if (m->free_header) {
np = realloc(m->header, ALIGN8(new_size));
if (!np)
goto poison;
} else {
- /* Initially, the header is allocated as part of of
+ /* Initially, the header is allocated as part of
* the sd_bus_message itself, let's replace it by
* dynamic data */
op = m->header;
m->header = np;
- m->header->fields_size = new_size - sizeof(struct bus_header);
+ m->fields_size = new_size - sizeof(struct bus_header);
/* Adjust quick access pointers */
m->path = adjust_pointer(m->path, op, old_size, m->header);
static int message_append_field_string(
sd_bus_message *m,
- uint8_t h,
+ uint64_t h,
char type,
const char *s,
const char **ret) {
assert(m);
+ /* dbus1 only allows 8bit header field ids */
+ if (h > 0xFF)
+ return -EINVAL;
+
/* dbus1 doesn't allow strings over 32bit, let's enforce this
* globally, to not risk convertability */
l = strlen(s);
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);
+ /* (field id 64bit, ((string + NUL) + NUL + signature string 's') */
+ p = message_extend_fields(m, 8, 8 + l + 1 + 1 + 1, true);
if (!p)
return -ENOMEM;
- p[0] = h;
- memzero(p+1, 7);
+ *((uint64_t*) p) = h;
memcpy(p+8, s, l);
p[8+l] = 0;
p[8+l+1] = 0;
if (!p)
return -ENOMEM;
- p[0] = h;
+ p[0] = (uint8_t) h;
p[1] = 1;
p[2] = type;
p[3] = 0;
static int message_append_field_signature(
sd_bus_message *m,
- uint8_t h,
+ uint64_t h,
const char *s,
const char **ret) {
assert(m);
- /* dbus1 doesn't allow signatures over 32bit, let's enforce
+ /* dbus1 only allows 8bit header field ids */
+ if (h > 0xFF)
+ return -EINVAL;
+
+ /* dbus1 doesn't allow signatures over 8bit, let's enforce
* this globally, to not risk convertability */
l = strlen(s);
if (l > 255)
if (!p)
return -ENOMEM;
- p[0] = h;
+ p[0] = (uint8_t) h;
p[1] = 1;
p[2] = SD_BUS_TYPE_SIGNATURE;
p[3] = 0;
return 0;
}
-static int message_append_field_uint32(sd_bus_message *m, uint8_t h, uint32_t x) {
+static int message_append_field_uint32(sd_bus_message *m, uint64_t h, uint32_t x) {
uint8_t *p;
assert(m);
+ /* dbus1 only allows 8bit header field ids */
+ if (h > 0xFF)
+ return -EINVAL;
+
if (BUS_MESSAGE_IS_GVARIANT(m)) {
- /* (field id byte + 7x padding + ((value + NUL + signature string 'u') */
+ /* (field id 64bit + ((value + NUL + signature string 'u') */
- p = message_extend_fields(m, 8, 1 + 7 + 4 + 1 + 1, true);
+ p = message_extend_fields(m, 8, 8 + 4 + 1 + 1, true);
if (!p)
return -ENOMEM;
- p[0] = h;
- memzero(p+1, 7);
+ *((uint64_t*) p) = h;
*((uint32_t*) (p + 8)) = x;
p[12] = 0;
p[13] = 'u';
if (!p)
return -ENOMEM;
- p[0] = h;
+ p[0] = (uint8_t) h;
p[1] = 1;
- p[2] = SD_BUS_TYPE_UINT32;
+ p[2] = 'u';
p[3] = 0;
((uint32_t*) p)[1] = x;
return 0;
}
+static int message_append_field_uint64(sd_bus_message *m, uint64_t h, uint64_t x) {
+ uint8_t *p;
+
+ assert(m);
+
+ /* dbus1 only allows 8bit header field ids */
+ if (h > 0xFF)
+ return -EINVAL;
+
+ if (BUS_MESSAGE_IS_GVARIANT(m)) {
+ /* (field id 64bit + ((value + NUL + signature string 't') */
+
+ p = message_extend_fields(m, 8, 8 + 8 + 1 + 1, true);
+ if (!p)
+ return -ENOMEM;
+
+ *((uint64_t*) p) = h;
+ *((uint64_t*) (p + 8)) = x;
+ p[16] = 0;
+ p[17] = 't';
+ } else {
+ /* (field id byte + (signature length + signature 't' + NUL) + 4 byte padding + value) */
+ p = message_extend_fields(m, 8, 4 + 4 + 8, false);
+ if (!p)
+ return -ENOMEM;
+
+ p[0] = (uint8_t) h;
+ p[1] = 1;
+ p[2] = 't';
+ p[3] = 0;
+ p[4] = 0;
+ p[5] = 0;
+ p[6] = 0;
+ p[7] = 0;
+
+ ((uint64_t*) p)[1] = x;
+ }
+
+ return 0;
+}
+
+static int message_append_reply_cookie(sd_bus_message *m, uint64_t cookie) {
+ assert(m);
+
+ if (BUS_MESSAGE_IS_GVARIANT(m))
+ return message_append_field_uint64(m, BUS_MESSAGE_HEADER_REPLY_SERIAL, cookie);
+ else {
+ /* 64bit cookies are not supported on dbus1 */
+ if (cookie > 0xffffffffUL)
+ return -EOPNOTSUPP;
+
+ return message_append_field_uint32(m, BUS_MESSAGE_HEADER_REPLY_SERIAL, (uint32_t) cookie);
+ }
+}
+
int bus_message_from_header(
sd_bus *bus,
- void *buffer,
- size_t length,
+ void *header,
+ size_t header_accessible,
+ void *footer,
+ size_t footer_accessible,
+ size_t message_size,
int *fds,
unsigned n_fds,
- const struct ucred *ucred,
const char *label,
size_t extra,
sd_bus_message **ret) {
- sd_bus_message *m;
+ _cleanup_free_ sd_bus_message *m = NULL;
struct bus_header *h;
size_t a, label_sz;
assert(bus);
- assert(buffer || length <= 0);
+ assert(header || header_accessible <= 0);
+ assert(footer || footer_accessible <= 0);
assert(fds || n_fds <= 0);
assert(ret);
- if (length < sizeof(struct bus_header))
+ if (header_accessible < sizeof(struct bus_header))
return -EBADMSG;
- h = buffer;
- if (h->version != 1 &&
- h->version != 2)
+ if (header_accessible > message_size)
+ return -EBADMSG;
+ if (footer_accessible > message_size)
return -EBADMSG;
- if (h->serial == 0)
+ h = header;
+ if (!IN_SET(h->version, 1, 2))
return -EBADMSG;
if (h->type == _SD_BUS_MESSAGE_TYPE_INVALID)
return -EBADMSG;
- if (h->endian != BUS_LITTLE_ENDIAN &&
- h->endian != BUS_BIG_ENDIAN)
+ if (!IN_SET(h->endian, BUS_LITTLE_ENDIAN, BUS_BIG_ENDIAN))
return -EBADMSG;
+ /* Note that we are happy with unknown flags in the flags header! */
+
a = ALIGN(sizeof(sd_bus_message)) + ALIGN(extra);
if (label) {
m->n_ref = 1;
m->sealed = true;
- m->header = h;
- m->fds = fds;
- m->n_fds = n_fds;
+ m->header = header;
+ m->header_accessible = header_accessible;
+ m->footer = footer;
+ m->footer_accessible = footer_accessible;
- if (ucred) {
- m->creds.pid = ucred->pid;
- m->creds.uid = ucred->uid;
- m->creds.gid = ucred->gid;
+ if (BUS_MESSAGE_IS_GVARIANT(m)) {
+ size_t ws;
+
+ if (h->dbus2.cookie == 0)
+ return -EBADMSG;
- /* Due to namespace translations some data might be
- * missing from this ucred record. */
- if (m->creds.pid > 0)
- m->creds.mask |= SD_BUS_CREDS_PID;
+ /* dbus2 derives the sizes from the message size and
+ the offset table at the end, since it is formatted as
+ gvariant "yyyyuta{tv}v". Since the message itself is a
+ structure with precisely to variable sized entries,
+ there's only one offset in the table, which marks the
+ end of the fields array. */
- if (m->creds.uid != (uid_t) -1)
- m->creds.mask |= SD_BUS_CREDS_UID;
+ ws = bus_gvariant_determine_word_size(message_size, 0);
+ if (footer_accessible < ws)
+ return -EBADMSG;
+
+ m->fields_size = bus_gvariant_read_word_le((uint8_t*) footer + footer_accessible - ws, ws);
+ if (ALIGN8(m->fields_size) > message_size - ws)
+ return -EBADMSG;
+ if (m->fields_size < sizeof(struct bus_header))
+ return -EBADMSG;
- if (m->creds.gid != (gid_t) -1)
- m->creds.mask |= SD_BUS_CREDS_GID;
+ m->fields_size -= sizeof(struct bus_header);
+ m->body_size = message_size - (sizeof(struct bus_header) + ALIGN8(m->fields_size));
+ } else {
+ if (h->dbus1.serial == 0)
+ return -EBADMSG;
+
+ /* dbus1 has the sizes in the header */
+ m->fields_size = BUS_MESSAGE_BSWAP32(m, h->dbus1.fields_size);
+ m->body_size = BUS_MESSAGE_BSWAP32(m, h->dbus1.body_size);
+
+ if (sizeof(struct bus_header) + ALIGN8(m->fields_size) + m->body_size != message_size)
+ return -EBADMSG;
}
+ m->fds = fds;
+ m->n_fds = n_fds;
+
if (label) {
m->creds.label = (char*) m + ALIGN(sizeof(sd_bus_message)) + ALIGN(extra);
memcpy(m->creds.label, label, label_sz + 1);
m->bus = sd_bus_ref(bus);
*ret = m;
+ m = NULL;
return 0;
}
size_t length,
int *fds,
unsigned n_fds,
- const struct ucred *ucred,
const char *label,
sd_bus_message **ret) {
size_t sz;
int r;
- r = bus_message_from_header(bus, buffer, length, fds, n_fds, ucred, label, 0, &m);
+ r = bus_message_from_header(
+ bus,
+ buffer, length, /* in this case the initial bytes and the final bytes are the same */
+ buffer, length,
+ length,
+ fds, n_fds,
+ label,
+ 0, &m);
if (r < 0)
return r;
- if (length != BUS_MESSAGE_SIZE(m)) {
- r = -EBADMSG;
- goto fail;
- }
-
- sz = length - sizeof(struct bus_header) - ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m));
+ sz = length - sizeof(struct bus_header) - ALIGN8(m->fields_size);
if (sz > 0) {
m->n_body_parts = 1;
- m->body.data = (uint8_t*) buffer + sizeof(struct bus_header) + ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m));
+ m->body.data = (uint8_t*) buffer + sizeof(struct bus_header) + ALIGN8(m->fields_size);
m->body.size = sz;
m->body.sealed = true;
m->body.memfd = -1;
return r;
}
-static sd_bus_message *message_new(sd_bus *bus, uint8_t type) {
- sd_bus_message *m;
+_public_ int sd_bus_message_new(
+ sd_bus *bus,
+ sd_bus_message **m,
+ uint8_t type) {
- assert(bus);
+ sd_bus_message *t;
- m = malloc0(ALIGN(sizeof(sd_bus_message)) + sizeof(struct bus_header));
- if (!m)
- return NULL;
+ assert_return(bus, -ENOTCONN);
+ assert_return(bus->state != BUS_UNSET, -ENOTCONN);
+ assert_return(m, -EINVAL);
- m->n_ref = 1;
- m->header = (struct bus_header*) ((uint8_t*) m + ALIGN(sizeof(struct sd_bus_message)));
- m->header->endian = BUS_NATIVE_ENDIAN;
- 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 = BUS_MESSAGE_IS_GVARIANT(m);
- m->bus = sd_bus_ref(bus);
+ t = malloc0(ALIGN(sizeof(sd_bus_message)) + sizeof(struct bus_header));
+ if (!t)
+ return -ENOMEM;
- return m;
+ t->n_ref = 1;
+ t->header = (struct bus_header*) ((uint8_t*) t + ALIGN(sizeof(struct sd_bus_message)));
+ t->header->endian = BUS_NATIVE_ENDIAN;
+ t->header->type = type;
+ t->header->version = bus->message_version;
+ t->allow_fds = bus->can_fds || !IN_SET(bus->state, BUS_HELLO, BUS_RUNNING);
+ t->root_container.need_offsets = BUS_MESSAGE_IS_GVARIANT(t);
+ t->bus = sd_bus_ref(bus);
+
+ if (bus->allow_interactive_authorization)
+ t->header->flags |= BUS_MESSAGE_ALLOW_INTERACTIVE_AUTHORIZATION;
+
+ *m = t;
+ return 0;
}
_public_ int sd_bus_message_new_signal(
assert_return(member_name_is_valid(member), -EINVAL);
assert_return(m, -EINVAL);
- t = message_new(bus, SD_BUS_MESSAGE_SIGNAL);
- if (!t)
+ r = sd_bus_message_new(bus, &t, SD_BUS_MESSAGE_SIGNAL);
+ if (r < 0)
return -ENOMEM;
+ assert(t);
+
t->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED;
r = message_append_field_string(t, BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path);
assert_return(member_name_is_valid(member), -EINVAL);
assert_return(m, -EINVAL);
- t = message_new(bus, SD_BUS_MESSAGE_METHOD_CALL);
- if (!t)
+ r = sd_bus_message_new(bus, &t, SD_BUS_MESSAGE_METHOD_CALL);
+ if (r < 0)
return -ENOMEM;
+ assert(t);
+
r = message_append_field_string(t, BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path);
if (r < 0)
goto fail;
assert_return(call->bus->state != BUS_UNSET, -ENOTCONN);
assert_return(m, -EINVAL);
- t = message_new(call->bus, type);
- if (!t)
+ r = sd_bus_message_new(call->bus, &t, type);
+ if (r < 0)
return -ENOMEM;
+ assert(t);
+
t->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED;
t->reply_cookie = BUS_MESSAGE_COOKIE(call);
+ if (t->reply_cookie == 0)
+ return -EOPNOTSUPP;
- r = message_append_field_uint32(t, BUS_MESSAGE_HEADER_REPLY_SERIAL, (uint32_t) t->reply_cookie);
+ r = message_append_reply_cookie(t, t->reply_cookie);
if (r < 0)
goto fail;
const char *format,
...) {
- _cleanup_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
va_list ap;
assert_return(name, -EINVAL);
int error,
const sd_bus_error *p) {
- _cleanup_free_ sd_bus_error berror = SD_BUS_ERROR_NULL;
+ _cleanup_(sd_bus_error_free) sd_bus_error berror = SD_BUS_ERROR_NULL;
if (sd_bus_error_is_set(p))
return sd_bus_message_new_method_error(call, m, p);
const char *format,
...) {
- _cleanup_free_ sd_bus_error berror = SD_BUS_ERROR_NULL;
+ _cleanup_(sd_bus_error_free) sd_bus_error berror = SD_BUS_ERROR_NULL;
va_list ap;
va_start(ap, format);
- bus_error_set_errnofv(&berror, error, format, ap);
+ sd_bus_error_set_errnofv(&berror, error, format, ap);
va_end(ap);
return sd_bus_message_new_method_error(call, m, &berror);
}
+void bus_message_set_sender_local(sd_bus *bus, sd_bus_message *m) {
+ assert(bus);
+ assert(m);
+
+ m->sender = m->creds.unique_name = (char*) "org.freedesktop.DBus.Local";
+ m->creds.well_known_names_local = true;
+ m->creds.mask |= (SD_BUS_CREDS_UNIQUE_NAME|SD_BUS_CREDS_WELL_KNOWN_NAMES) & bus->creds_mask;
+}
+
+void bus_message_set_sender_driver(sd_bus *bus, sd_bus_message *m) {
+ assert(bus);
+ assert(m);
+
+ m->sender = m->creds.unique_name = (char*) "org.freedesktop.DBus";
+ m->creds.well_known_names_driver = true;
+ m->creds.mask |= (SD_BUS_CREDS_UNIQUE_NAME|SD_BUS_CREDS_WELL_KNOWN_NAMES) & bus->creds_mask;
+}
+
int bus_message_new_synthetic_error(
sd_bus *bus,
uint64_t cookie,
assert(sd_bus_error_is_set(e));
assert(m);
- t = message_new(bus, SD_BUS_MESSAGE_METHOD_ERROR);
- if (!t)
+ r = sd_bus_message_new(bus, &t, SD_BUS_MESSAGE_METHOD_ERROR);
+ if (r < 0)
return -ENOMEM;
+ assert(t);
+
t->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED;
t->reply_cookie = cookie;
- r = message_append_field_uint32(t, BUS_MESSAGE_HEADER_REPLY_SERIAL, (uint32_t) t->reply_cookie);
+ r = message_append_reply_cookie(t, t->reply_cookie);
if (r < 0)
goto fail;
t->error._need_free = -1;
+ bus_message_set_sender_driver(bus, t);
+
*m = t;
return 0;
}
_public_ sd_bus_message* sd_bus_message_ref(sd_bus_message *m) {
- assert_return(m, NULL);
+
+ if (!m)
+ return NULL;
assert(m->n_ref > 0);
m->n_ref++;
}
_public_ int sd_bus_message_get_cookie(sd_bus_message *m, uint64_t *cookie) {
+ uint64_t c;
+
assert_return(m, -EINVAL);
assert_return(cookie, -EINVAL);
- assert_return(m->header->serial != 0, -ENODATA);
+
+ c = BUS_MESSAGE_COOKIE(m);
+ if (c == 0)
+ return -ENODATA;
*cookie = BUS_MESSAGE_COOKIE(m);
return 0;
_public_ int sd_bus_message_get_reply_cookie(sd_bus_message *m, uint64_t *cookie) {
assert_return(m, -EINVAL);
assert_return(cookie, -EINVAL);
- assert_return(m->reply_cookie != 0, -ENODATA);
+
+ if (m->reply_cookie == 0)
+ return -ENODATA;
*cookie = m->reply_cookie;
return 0;
_public_ const sd_bus_error *sd_bus_message_get_error(sd_bus_message *m) {
assert_return(m, NULL);
- assert_return(sd_bus_error_is_set(&m->error), NULL);
+
+ if (!sd_bus_error_is_set(&m->error))
+ return NULL;
return &m->error;
}
return &m->creds;
}
-_public_ int sd_bus_message_is_signal(sd_bus_message *m,
- const char *interface,
- const char *member) {
+_public_ int sd_bus_message_is_signal(
+ sd_bus_message *m,
+ const char *interface,
+ const char *member) {
+
assert_return(m, -EINVAL);
if (m->header->type != SD_BUS_MESSAGE_SIGNAL)
return 1;
}
-_public_ int sd_bus_message_is_method_call(sd_bus_message *m,
- const char *interface,
- const char *member) {
+_public_ int sd_bus_message_is_method_call(
+ sd_bus_message *m,
+ const char *interface,
+ const char *member) {
+
assert_return(m, -EINVAL);
if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
assert_return(!m->sealed, -EPERM);
assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EPERM);
- if (b)
- m->header->flags &= ~BUS_MESSAGE_NO_REPLY_EXPECTED;
- else
- m->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED;
+ SET_FLAG(m->header->flags, BUS_MESSAGE_NO_REPLY_EXPECTED, !b);
return 0;
}
assert_return(m, -EINVAL);
assert_return(!m->sealed, -EPERM);
- if (b)
- m->header->flags &= ~BUS_MESSAGE_NO_AUTO_START;
- else
- m->header->flags |= BUS_MESSAGE_NO_AUTO_START;
+ SET_FLAG(m->header->flags, BUS_MESSAGE_NO_AUTO_START, !b);
return 0;
}
assert_return(m, -EINVAL);
assert_return(!m->sealed, -EPERM);
- if (b)
- m->header->flags |= BUS_MESSAGE_ALLOW_INTERACTIVE_AUTHORIZATION;
- else
- m->header->flags &= ~BUS_MESSAGE_ALLOW_INTERACTIVE_AUTHORIZATION;
+ SET_FLAG(m->header->flags, BUS_MESSAGE_ALLOW_INTERACTIVE_AUTHORIZATION, b);
return 0;
}
part->memfd = -1;
m->body_end = part;
- m->n_body_parts ++;
+ m->n_body_parts++;
return part;
}
void **q) {
void *n;
- int r;
assert(m);
assert(part);
if (m->poisoned)
return -ENOMEM;
- if (!part->data && part->memfd < 0)
- part->memfd = bus_kernel_pop_memfd(m->bus, &part->data, &part->mapped, &part->allocated);
-
- if (part->memfd >= 0) {
-
- if (part->allocated == 0 || sz > part->allocated) {
- uint64_t new_allocated;
-
- new_allocated = PAGE_ALIGN(sz > 0 ? 2 * sz : 1);
- r = memfd_set_size(part->memfd, new_allocated);
- if (r < 0) {
- m->poisoned = true;
- return r;
- }
-
- part->allocated = new_allocated;
- }
-
- if (!part->data || sz > part->mapped) {
- size_t psz;
+ if (part->allocated == 0 || sz > part->allocated) {
+ size_t new_allocated;
- 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
- n = mremap(part->data, part->mapped, psz, MREMAP_MAYMOVE);
-
- if (n == MAP_FAILED) {
- m->poisoned = true;
- return -errno;
- }
-
- part->mapped = psz;
- part->data = n;
+ new_allocated = sz > 0 ? 2 * sz : 64;
+ n = realloc(part->data, new_allocated);
+ if (!n) {
+ m->poisoned = true;
+ return -ENOMEM;
}
- part->munmap_this = true;
- } else {
- if (part->allocated == 0 || sz > part->allocated) {
- size_t new_allocated;
-
- 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;
- }
+ part->data = n;
+ part->allocated = new_allocated;
+ part->free_this = true;
}
if (q)
}
}
-static void *message_extend_body(sd_bus_message *m, size_t align, size_t sz, bool add_offset) {
+static void *message_extend_body(
+ sd_bus_message *m,
+ size_t align,
+ size_t sz,
+ bool add_offset,
+ bool force_inline) {
+
size_t start_body, end_body, padding, added;
void *p;
int r;
if (m->poisoned)
return NULL;
- start_body = ALIGN_TO((size_t) m->header->body_size, align);
+ start_body = ALIGN_TO((size_t) m->body_size, align);
end_body = start_body + sz;
- padding = start_body - m->header->body_size;
+ padding = start_body - m->body_size;
added = padding + sz;
/* Check for 32bit overflows */
- if (end_body > (size_t) ((uint32_t) -1)) {
+ if (end_body > (size_t) ((uint32_t) -1) ||
+ end_body < start_body) {
m->poisoned = true;
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;
+ (padding != ALIGN_TO(m->body_end->size, align) - m->body_end->size) ||
+ (force_inline && m->body_end->size > MEMFD_MIN_SIZE); /* if this must be an inlined extension, let's create a new part if the previous part is large enough to be inlined */
if (add_new_part) {
if (padding > 0) {
/* Return something that is not NULL and is aligned */
p = (uint8_t *) NULL + align;
- m->header->body_size = end_body;
+ m->body_size = end_body;
message_extend_containers(m, added);
if (add_offset) {
return -EINVAL;
if (!m->allow_fds)
- return -ENOTSUP;
+ return -EOPNOTSUPP;
copy = fcntl(fd, F_DUPFD_CLOEXEC, 3);
if (copy < 0)
} else {
char *e;
- /* Maybe we can append to the signature? But only if this is the top-level container*/
+ /* Maybe we can append to the signature? But only if this is the top-level container */
if (c->enclosing != 0)
return -ENXIO;
assert(align > 0);
assert(sz > 0);
- a = message_extend_body(m, align, sz, true);
+ a = message_extend_body(m, align, sz, true, false);
if (!a)
return -ENOMEM;
assert(align > 0);
assert(sz > 0);
- a = message_extend_body(m, align, sz, false);
+ a = message_extend_body(m, align, sz, false, false);
if (!a)
return -ENOMEM;
- if (type == SD_BUS_TYPE_STRING || type == SD_BUS_TYPE_OBJECT_PATH) {
+ if (IN_SET(type, SD_BUS_TYPE_STRING, SD_BUS_TYPE_OBJECT_PATH)) {
*(uint32_t*) a = sz - 5;
memcpy((uint8_t*) a + 4, p, sz - 4);
}
if (type == SD_BUS_TYPE_UNIX_FD)
- m->n_fds ++;
+ m->n_fds++;
if (c->enclosing != SD_BUS_TYPE_ARRAY)
c->index++;
} else {
char *e;
- /* Maybe we can append to the signature? But only if this is the top-level container*/
+ /* Maybe we can append to the signature? But only if this is the top-level container */
if (c->enclosing != 0)
return -ENXIO;
}
if (BUS_MESSAGE_IS_GVARIANT(m)) {
- a = message_extend_body(m, 1, size + 1, true);
+ a = message_extend_body(m, 1, size + 1, true, false);
if (!a)
return -ENOMEM;
*s = a;
} else {
- a = message_extend_body(m, 4, 4 + size + 1, false);
+ a = message_extend_body(m, 4, 4 + size + 1, false, false);
if (!a)
return -ENOMEM;
return alignment;
/* Add alignment padding and add to offset list */
- if (!message_extend_body(m, alignment, 0, false))
+ if (!message_extend_body(m, alignment, 0, false, false))
return -ENOMEM;
r = bus_gvariant_is_fixed_size(contents);
if (r < 0)
return r;
- *begin = m->header->body_size;
+ *begin = m->body_size;
*need_offsets = r == 0;
} else {
void *a, *op;
if (alignment < 0)
return alignment;
- a = message_extend_body(m, 4, 4, false);
+ a = message_extend_body(m, 4, 4, false, false);
if (!a)
return -ENOMEM;
os = m->body_end->size;
/* Add alignment between size and first element */
- if (!message_extend_body(m, alignment, 0, false))
+ if (!message_extend_body(m, alignment, 0, false, false))
return -ENOMEM;
/* location of array size might have changed so let's readjust a */
if (BUS_MESSAGE_IS_GVARIANT(m)) {
/* Variants are always aligned to 8 */
- if (!message_extend_body(m, 8, 0, false))
+ if (!message_extend_body(m, 8, 0, false, false))
return -ENOMEM;
} else {
void *a;
l = strlen(contents);
- a = message_extend_body(m, 1, 1 + l + 1, false);
+ a = message_extend_body(m, 1, 1 + l + 1, false, false);
if (!a)
return -ENOMEM;
if (alignment < 0)
return alignment;
- if (!message_extend_body(m, alignment, 0, false))
+ if (!message_extend_body(m, alignment, 0, false, false))
return -ENOMEM;
r = bus_gvariant_is_fixed_size(contents);
if (r < 0)
return r;
- *begin = m->header->body_size;
+ *begin = m->body_size;
*need_offsets = r == 0;
} else {
/* Align contents to 8 byte boundary */
- if (!message_extend_body(m, 8, 0, false))
+ if (!message_extend_body(m, 8, 0, false, false))
return -ENOMEM;
}
if (alignment < 0)
return alignment;
- if (!message_extend_body(m, alignment, 0, false))
+ if (!message_extend_body(m, alignment, 0, false, false))
return -ENOMEM;
r = bus_gvariant_is_fixed_size(contents);
if (r < 0)
return r;
- *begin = m->header->body_size;
+ *begin = m->body_size;
*need_offsets = r == 0;
} else {
/* Align contents to 8 byte boundary */
- if (!message_extend_body(m, 8, 0, false))
+ if (!message_extend_body(m, 8, 0, false, false))
return -ENOMEM;
}
/* Save old index in the parent container, in case we have to
* abort this container */
c->saved_index = c->index;
- before = m->header->body_size;
+ before = m->body_size;
if (type == SD_BUS_TYPE_ARRAY)
r = bus_message_open_array(m, c, contents, &array_size, &begin, &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 == 8)
- 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);
/* Variable-width arrays */
payload = c->n_offsets > 0 ? c->offsets[c->n_offsets-1] - c->begin : 0;
- sz = determine_word_size(payload, c->n_offsets);
+ sz = bus_gvariant_determine_word_size(payload, c->n_offsets);
- a = message_extend_body(m, 1, sz * c->n_offsets, true);
+ a = message_extend_body(m, 1, sz * c->n_offsets, true, false);
if (!a)
return -ENOMEM;
for (i = 0; i < c->n_offsets; i++)
- write_word_le(a + sz*i, sz, c->offsets[i] - c->begin);
+ bus_gvariant_write_word_le(a + sz*i, sz, c->offsets[i] - c->begin);
} else {
void *a;
/* Fixed-width or empty arrays */
- a = message_extend_body(m, 1, 0, true); /* let's add offset to parent */
+ a = message_extend_body(m, 1, 0, true, false); /* let's add offset to parent */
if (!a)
return -ENOMEM;
}
assert(m);
assert(c);
+ assert(c->signature);
if (!BUS_MESSAGE_IS_GVARIANT(m))
return 0;
l = strlen(c->signature);
- a = message_extend_body(m, 1, 1 + l, true);
+ a = message_extend_body(m, 1, 1 + l, true, false);
if (!a)
return -ENOMEM;
}
static int bus_message_close_struct(sd_bus_message *m, struct bus_container *c, bool add_offset) {
+ bool fixed_size = true;
size_t n_variable = 0;
unsigned i = 0;
const char *p;
/* 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)
+ fixed_size = false;
if (r == 0 && p[n] != 0)
n_variable++;
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, add_offset);
+ if (isempty(c->signature)) {
+ /* The unary type is encoded as fixed 1 byte padding */
+ a = message_extend_body(m, 1, 1, add_offset, false);
+ if (!a)
+ return -ENOMEM;
+
+ *a = 0;
+ } else if (n_variable <= 0) {
+ int alignment = 1;
+
+ /* Structures with fixed-size members only have to be
+ * fixed-size themselves. But gvariant requires all fixed-size
+ * elements to be sized a multiple of their alignment. Hence,
+ * we must *always* add final padding after the last member so
+ * the overall size of the structure is properly aligned. */
+ if (fixed_size)
+ alignment = bus_gvariant_get_alignment(strempty(c->signature));
+
+ assert(alignment > 0);
+
+ a = message_extend_body(m, alignment, 0, add_offset, false);
if (!a)
return -ENOMEM;
} else {
size_t sz;
unsigned j;
- assert(c->offsets[c->n_offsets-1] == m->header->body_size);
+ assert(c->offsets[c->n_offsets-1] == m->body_size);
- sz = determine_word_size(m->header->body_size - c->begin, n_variable);
+ sz = bus_gvariant_determine_word_size(m->body_size - c->begin, n_variable);
- a = message_extend_body(m, 1, sz * n_variable, add_offset);
+ a = message_extend_body(m, 1, sz * n_variable, add_offset, false);
if (!a)
return -ENOMEM;
k = n_variable - 1 - j;
- write_word_le(a + k * sz, sz, c->offsets[i] - c->begin);
+ bus_gvariant_write_word_le(a + k * sz, sz, c->offsets[i] - c->begin);
j++;
}
r = bus_message_close_array(m, c);
else if (c->enclosing == SD_BUS_TYPE_VARIANT)
r = bus_message_close_variant(m, c);
- else if (c->enclosing == SD_BUS_TYPE_STRUCT || c->enclosing == SD_BUS_TYPE_DICT_ENTRY)
+ else if (IN_SET(c->enclosing, SD_BUS_TYPE_STRUCT, SD_BUS_TYPE_DICT_ENTRY))
r = bus_message_close_struct(m, c, true);
else
assert_not_reached("Unknown container type");
return 1;
}
-int bus_message_append_ap(
+_public_ int sd_bus_message_appendv(
sd_bus_message *m,
const char *types,
va_list ap) {
unsigned stack_ptr = 0;
int r;
- assert(m);
-
- if (!types)
- return 0;
+ assert_return(m, -EINVAL);
+ assert_return(types, -EINVAL);
+ assert_return(!m->sealed, -EPERM);
+ assert_return(!m->poisoned, -ESTALE);
n_array = (unsigned) -1;
n_struct = strlen(types);
t = types;
if (n_array != (unsigned) -1)
- n_array --;
+ n_array--;
else {
- types ++;
+ types++;
n_struct--;
}
}
case SD_BUS_TYPE_INT64:
- case SD_BUS_TYPE_UINT64:
- case SD_BUS_TYPE_DOUBLE: {
+ case SD_BUS_TYPE_UINT64: {
uint64_t x;
x = va_arg(ap, uint64_t);
break;
}
+ case SD_BUS_TYPE_DOUBLE: {
+ double x;
+
+ x = va_arg(ap, double);
+ r = sd_bus_message_append_basic(m, *t, &x);
+ break;
+ }
+
case SD_BUS_TYPE_STRING:
case SD_BUS_TYPE_OBJECT_PATH:
case SD_BUS_TYPE_SIGNATURE: {
assert_return(!m->poisoned, -ESTALE);
va_start(ap, types);
- r = bus_message_append_ap(m, types, ap);
+ r = sd_bus_message_appendv(m, types, ap);
va_end(ap);
return r;
if (r < 0)
return r;
- a = message_extend_body(m, align, size, false);
+ a = message_extend_body(m, align, size, false, false);
if (!a)
return -ENOMEM;
return 0;
}
-_public_ int sd_bus_message_append_array(sd_bus_message *m,
- char type,
- const void *ptr,
- size_t size) {
+_public_ int sd_bus_message_append_array(
+ sd_bus_message *m,
+ char type,
+ const void *ptr,
+ size_t size) {
int r;
void *p;
if (r < 0)
return r;
- if (size > 0)
- memcpy(p, ptr, size);
+ memcpy_safe(p, ptr, size);
return 0;
}
return 0;
}
-_public_ int sd_bus_message_append_array_memfd(sd_bus_message *m,
- char type,
- int memfd) {
+_public_ int sd_bus_message_append_array_memfd(
+ sd_bus_message *m,
+ char type,
+ int memfd,
+ uint64_t offset,
+ uint64_t size) {
+
_cleanup_close_ int copy_fd = -1;
struct bus_body_part *part;
ssize_t align, sz;
- uint64_t size;
+ uint64_t real_size;
void *a;
int r;
- if (!m)
- return -EINVAL;
- if (memfd < 0)
- return -EINVAL;
- if (m->sealed)
- return -EPERM;
- if (!bus_type_is_trivial(type))
- return -EINVAL;
- if (m->poisoned)
- return -ESTALE;
+ assert_return(m, -EINVAL);
+ assert_return(memfd >= 0, -EBADF);
+ assert_return(bus_type_is_trivial(type), -EINVAL);
+ assert_return(size > 0, -EINVAL);
+ assert_return(!m->sealed, -EPERM);
+ assert_return(!m->poisoned, -ESTALE);
r = memfd_set_sealed(memfd);
if (r < 0)
if (copy_fd < 0)
return copy_fd;
- r = memfd_get_size(memfd, &size);
+ r = memfd_get_size(memfd, &real_size);
if (r < 0)
return r;
+ if (offset == 0 && size == (uint64_t) -1)
+ size = real_size;
+ else if (offset + size > real_size)
+ return -EMSGSIZE;
+
align = bus_type_get_alignment(type);
sz = bus_type_get_size(type);
assert_se(align > 0);
assert_se(sz > 0);
+ if (offset % align != 0)
+ return -EINVAL;
+
if (size % sz != 0)
return -EINVAL;
if (r < 0)
return r;
- a = message_extend_body(m, align, 0, false);
+ a = message_extend_body(m, align, 0, false, false);
if (!a)
return -ENOMEM;
return -ENOMEM;
part->memfd = copy_fd;
+ part->memfd_offset = offset;
part->sealed = true;
part->size = size;
copy_fd = -1;
- m->header->body_size += size;
+ m->body_size += size;
message_extend_containers(m, size);
return sd_bus_message_close_container(m);
}
-_public_ int sd_bus_message_append_string_memfd(sd_bus_message *m, int memfd) {
+_public_ int sd_bus_message_append_string_memfd(
+ sd_bus_message *m,
+ int memfd,
+ uint64_t offset,
+ uint64_t size) {
+
_cleanup_close_ int copy_fd = -1;
struct bus_body_part *part;
struct bus_container *c;
- uint64_t size;
+ uint64_t real_size;
void *a;
int r;
assert_return(m, -EINVAL);
- assert_return(memfd >= 0, -EINVAL);
+ assert_return(memfd >= 0, -EBADF);
+ assert_return(size > 0, -EINVAL);
assert_return(!m->sealed, -EPERM);
assert_return(!m->poisoned, -ESTALE);
if (copy_fd < 0)
return copy_fd;
- r = memfd_get_size(memfd, &size);
+ r = memfd_get_size(memfd, &real_size);
if (r < 0)
return r;
+ if (offset == 0 && size == (uint64_t) -1)
+ size = real_size;
+ else if (offset + size > real_size)
+ return -EMSGSIZE;
+
/* We require this to be NUL terminated */
if (size == 0)
return -EINVAL;
} else {
char *e;
- /* Maybe we can append to the signature? But only if this is the top-level container*/
+ /* Maybe we can append to the signature? But only if this is the top-level container */
if (c->enclosing != 0)
return -ENXIO;
}
if (!BUS_MESSAGE_IS_GVARIANT(m)) {
- a = message_extend_body(m, 4, 4, false);
+ a = message_extend_body(m, 4, 4, false, false);
if (!a)
return -ENOMEM;
return -ENOMEM;
part->memfd = copy_fd;
+ part->memfd_offset = offset;
part->sealed = true;
part->size = size;
copy_fd = -1;
- m->header->body_size += size;
+ m->body_size += size;
message_extend_containers(m, size);
if (BUS_MESSAGE_IS_GVARIANT(m)) {
- r = message_add_offset(m, m->header->body_size);
+ r = message_add_offset(m, m->body_size);
if (r < 0) {
m->poisoned = true;
return -ENOMEM;
}
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;
+ /* The actual user data is finished now, we just complete the
+ variant and struct now (at least on gvariant). Remember
+ this position, so that during parsing we know where to
+ put the outer container end. */
+ m->user_body_size = m->body_size;
- if (m->n_header_offsets < 1)
- return 0;
+ if (BUS_MESSAGE_IS_GVARIANT(m)) {
+ const char *signature;
+ size_t sz, l;
+ void *d;
- assert(m->header->fields_size == m->header_offsets[m->n_header_offsets-1]);
+ /* Add offset table to end of fields array */
+ if (m->n_header_offsets >= 1) {
+ uint8_t *a;
+ unsigned i;
- sz = determine_word_size(m->header->fields_size, m->n_header_offsets);
+ assert(m->fields_size == m->header_offsets[m->n_header_offsets-1]);
- a = message_extend_fields(m, 1, sz * m->n_header_offsets, false);
- if (!a)
- return -ENOMEM;
+ sz = bus_gvariant_determine_word_size(m->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++)
+ bus_gvariant_write_word_le(a + sz*i, sz, m->header_offsets[i]);
+ }
+
+ /* Add gvariant NUL byte plus signature to the end of
+ * the body, followed by the final offset pointing to
+ * the end of the fields array */
+
+ signature = strempty(m->root_container.signature);
+ l = strlen(signature);
+
+ sz = bus_gvariant_determine_word_size(sizeof(struct bus_header) + ALIGN8(m->fields_size) + m->body_size + 1 + l + 2, 1);
+ d = message_extend_body(m, 1, 1 + l + 2 + sz, false, true);
+ if (!d)
+ return -ENOMEM;
- for (i = 0; i < m->n_header_offsets; i++)
- write_word_le(a + sz*i, sz, m->header_offsets[i]);
+ *(uint8_t*) d = 0;
+ *((uint8_t*) d + 1) = SD_BUS_TYPE_STRUCT_BEGIN;
+ memcpy((uint8_t*) d + 2, signature, l);
+ *((uint8_t*) d + 1 + l + 1) = SD_BUS_TYPE_STRUCT_END;
+
+ bus_gvariant_write_word_le((uint8_t*) d + 1 + l + 2, sz, sizeof(struct bus_header) + m->fields_size);
+
+ m->footer = d;
+ m->footer_accessible = 1 + l + 2 + sz;
+ } else {
+ m->header->dbus1.fields_size = m->fields_size;
+ m->header->dbus1.body_size = m->body_size;
+ }
return 0;
}
-int bus_message_seal(sd_bus_message *m, uint64_t cookie, usec_t timeout) {
+_public_ int sd_bus_message_seal(sd_bus_message *m, uint64_t cookie, uint64_t timeout_usec) {
struct bus_body_part *part;
- size_t l, a;
+ size_t a;
unsigned i;
int r;
- assert(m);
+ assert_return(m, -EINVAL);
if (m->sealed)
return -EPERM;
if (m->poisoned)
return -ESTALE;
+ if (cookie > 0xffffffffULL &&
+ !BUS_MESSAGE_IS_GVARIANT(m))
+ return -EOPNOTSUPP;
+
/* 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 &&
if (r < 0)
return r;
- /* If there's a non-trivial signature set, then add it in here */
- if (!isempty(m->root_container.signature)) {
+ /* If there's a non-trivial signature set, then add it in
+ * here, but only on dbus1 */
+ if (!isempty(m->root_container.signature) && !BUS_MESSAGE_IS_GVARIANT(m)) {
r = message_append_field_signature(m, BUS_MESSAGE_HEADER_SIGNATURE, m->root_container.signature, NULL);
if (r < 0)
return r;
if (r < 0)
return r;
- m->header->serial = (uint32_t) cookie;
- m->timeout = m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED ? 0 : timeout;
+ if (BUS_MESSAGE_IS_GVARIANT(m))
+ m->header->dbus2.cookie = cookie;
+ else
+ m->header->dbus1.serial = (uint32_t) cookie;
+
+ m->timeout = m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED ? 0 : timeout_usec;
/* 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;
+ a = ALIGN8(m->fields_size) - m->fields_size;
if (a > 0)
- memzero((uint8_t*) BUS_MESSAGE_FIELDS(m) + l, a);
+ memzero((uint8_t*) BUS_MESSAGE_FIELDS(m) + m->fields_size, 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->use_memfd) {
MESSAGE_FOREACH_PART(part, i, m)
- if (part->memfd >= 0 && !part->sealed && (part->size > MEMFD_MIN_SIZE || m->bus->use_memfd < 0)) {
+ if (part->memfd >= 0 &&
+ !part->sealed &&
+ (part->size > MEMFD_MIN_SIZE || m->bus->use_memfd < 0) &&
+ part != m->body_end) { /* The last part may never be sent as memfd */
uint64_t sz;
/* Try to seal it if that makes
}
}
- m->root_container.end = BUS_MESSAGE_BODY_SIZE(m);
+ m->root_container.end = m->user_body_size;
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;
int bus_body_part_map(struct bus_body_part *part) {
void *p;
- size_t psz;
+ size_t psz, shift;
assert_se(part);
return 0;
}
- psz = PAGE_ALIGN(part->size);
+ shift = part->memfd_offset - ((part->memfd_offset / page_size()) * page_size());
+ psz = PAGE_ALIGN(part->size + shift);
if (part->memfd >= 0)
- p = mmap(NULL, psz, PROT_READ, MAP_PRIVATE, part->memfd, 0);
+ p = mmap(NULL, psz, PROT_READ, MAP_PRIVATE, part->memfd, part->memfd_offset - shift);
else if (part->is_zero)
p = mmap(NULL, psz, PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
else
return -errno;
part->mapped = psz;
- part->data = p;
+ part->mmap_begin = p;
+ part->data = (uint8_t*) p + shift;
part->munmap_this = true;
return 0;
if (part->memfd < 0)
return;
- if (!part->data)
+ if (!part->mmap_begin)
return;
if (!part->munmap_this)
return;
- assert_se(munmap(part->data, part->mapped) == 0);
+ assert_se(munmap(part->mmap_begin, part->mapped) == 0);
+ part->mmap_begin = NULL;
part->data = NULL;
part->mapped = 0;
part->munmap_this = false;
c->offset_index++;
- } else if (c->enclosing == 0 ||
- c->enclosing == SD_BUS_TYPE_STRUCT ||
- c->enclosing == SD_BUS_TYPE_DICT_ENTRY) {
+ } else if (IN_SET(c->enclosing, 0, SD_BUS_TYPE_STRUCT, SD_BUS_TYPE_DICT_ENTRY)) {
int alignment;
size_t n, j;
padding = start - *rindex;
end = start + nbytes;
- if (end > BUS_MESSAGE_BODY_SIZE(m))
+ if (end > m->user_body_size)
return -EBADMSG;
part = find_part(m, *rindex, padding, (void**) &q);
return r;
} else {
- rindex = m->rindex;
-
if (IN_SET(type, SD_BUS_TYPE_STRING, SD_BUS_TYPE_OBJECT_PATH)) {
uint32_t l;
bool ok;
unsigned i;
/* gvariant: variable length array */
- sz = determine_word_size(c->item_size, 0);
+ sz = bus_gvariant_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);
+ framing = bus_gvariant_read_word_le(q, sz);
if (framing > c->item_size - sz)
return -EBADMSG;
if ((c->item_size - framing) % sz != 0)
for (i = 0; i < *n_offsets; i++) {
size_t x;
- x = read_word_le((uint8_t*) q + i * sz, sz);
+ x = bus_gvariant_read_word_le((uint8_t*) q + i * sz, sz);
if (x > c->item_size - sz)
return -EBADMSG;
if (x < p)
assert(n_offsets);
if (isempty(signature)) {
+ /* Unary type is encoded as *fixed* 1 byte padding */
+ r = message_peek_body(m, &m->rindex, 1, 1, &q);
+ if (r < 0)
+ return r;
+
+ if (*(uint8_t *) q != 0)
+ return -EBADMSG;
+
*item_size = 0;
*offsets = NULL;
*n_offsets = 0;
return 0;
}
- sz = determine_word_size(size, 0);
+ sz = bus_gvariant_determine_word_size(size, 0);
if (sz <= 0)
return -EBADMSG;
if (r < 0)
return r;
if (r == 0 && p[n] != 0) /* except the last item */
- n_variable ++;
+ n_variable++;
n_total++;
p += n;
if (v > 0) {
v--;
- x = read_word_le((uint8_t*) q + v*sz, sz);
+ x = bus_gvariant_read_word_le((uint8_t*) q + v*sz, sz);
if (x >= size)
return -EBADMSG;
if (m->rindex + x < previous)
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);
w->before = before;
w->begin = m->rindex;
- w->end = m->rindex + c->item_size;
+
+ /* Unary type has fixed size of 1, but virtual size of 0 */
+ if (BUS_MESSAGE_IS_GVARIANT(m) &&
+ type == SD_BUS_TYPE_STRUCT &&
+ isempty(signature))
+ w->end = m->rindex + 0;
+ else
+ w->end = m->rindex + c->item_size;
w->array_size = array_size;
w->item_size = item_size;
return 1;
}
- if (c->signature[c->index] == SD_BUS_TYPE_STRUCT_BEGIN ||
- c->signature[c->index] == SD_BUS_TYPE_DICT_ENTRY_BEGIN) {
+ if (IN_SET(c->signature[c->index], SD_BUS_TYPE_STRUCT_BEGIN, SD_BUS_TYPE_DICT_ENTRY_BEGIN)) {
if (contents) {
size_t l;
t = types;
if (n_array != (unsigned) -1)
- n_array --;
+ n_array--;
else {
- types ++;
+ types++;
n_struct--;
}
r = sd_bus_message_skip(m, s);
if (r < 0)
return r;
- assert(r != 0);
r = sd_bus_message_exit_container(m);
if (r < 0)
}
}
-_public_ int sd_bus_message_read_array(sd_bus_message *m,
- char type,
- const void **ptr,
- size_t *size) {
+_public_ int sd_bus_message_read_array(
+ sd_bus_message *m,
+ char type,
+ const void **ptr,
+ size_t *size) {
+
struct bus_container *c;
void *p;
size_t sz;
assert_return(bus_type_is_trivial(type), -EINVAL);
assert_return(ptr, -EINVAL);
assert_return(size, -EINVAL);
- assert_return(!BUS_MESSAGE_NEED_BSWAP(m), -ENOTSUP);
+ assert_return(!BUS_MESSAGE_NEED_BSWAP(m), -EOPNOTSUPP);
r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, CHAR_TO_STR(type));
if (r <= 0)
assert(rindex);
assert(align > 0);
- return buffer_peek(BUS_MESSAGE_FIELDS(m), BUS_MESSAGE_FIELDS_SIZE(m), rindex, align, nbytes, ret);
+ return buffer_peek(BUS_MESSAGE_FIELDS(m), m->fields_size, rindex, align, nbytes, ret);
}
static int message_peek_field_uint32(
return 0;
}
+static int message_peek_field_uint64(
+ sd_bus_message *m,
+ size_t *ri,
+ size_t item_size,
+ uint64_t *ret) {
+
+ int r;
+ void *q;
+
+ assert(m);
+ assert(ri);
+
+ if (BUS_MESSAGE_IS_GVARIANT(m) && item_size != 8)
+ return -EBADMSG;
+
+ /* identical for gvariant and dbus1 */
+
+ r = message_peek_fields(m, ri, 8, 8, &q);
+ if (r < 0)
+ return r;
+
+ if (ret)
+ *ret = BUS_MESSAGE_BSWAP64(m, *(uint64_t*) q);
+
+ return 0;
+}
+
static int message_peek_field_string(
sd_bus_message *m,
bool (*validate)(const char *p),
(*signature)++;
- } else if (t == SD_BUS_TYPE_STRUCT ||
- t == SD_BUS_TYPE_DICT_ENTRY) {
+ } else if (IN_SET(t, SD_BUS_TYPE_STRUCT, SD_BUS_TYPE_DICT_ENTRY)) {
r = signature_element_length(*signature, &l);
if (r < 0)
assert(m);
if (BUS_MESSAGE_IS_GVARIANT(m)) {
- void *q;
+ char *p;
+
+ /* Read the signature from the end of the body variant first */
+ sz = bus_gvariant_determine_word_size(BUS_MESSAGE_SIZE(m), 0);
+ if (m->footer_accessible < 1 + sz)
+ return -EBADMSG;
+
+ p = (char*) m->footer + m->footer_accessible - (1 + sz);
+ for (;;) {
+ if (p < (char*) m->footer)
+ return -EBADMSG;
+
+ if (*p == 0) {
+ size_t l;
+ char *c;
+
+ /* We found the beginning of the signature
+ * string, yay! We require the body to be a
+ * structure, so verify it and then strip the
+ * opening/closing brackets. */
+
+ l = ((char*) m->footer + m->footer_accessible) - p - (1 + sz);
+ if (l < 2 ||
+ p[1] != SD_BUS_TYPE_STRUCT_BEGIN ||
+ p[1 + l - 1] != SD_BUS_TYPE_STRUCT_END)
+ return -EBADMSG;
+
+ c = strndup(p + 1 + 1, l - 2);
+ if (!c)
+ return -ENOMEM;
+
+ free(m->root_container.signature);
+ m->root_container.signature = c;
+ break;
+ }
+
+ p--;
+ }
+
+ /* Calculate the actual user body size, by removing
+ * the trailing variant signature and struct offset
+ * table */
+ m->user_body_size = m->body_size - ((char*) m->footer + m->footer_accessible - p);
- sz = determine_word_size(BUS_MESSAGE_FIELDS_SIZE(m), 0);
+ /* Pull out the offset table for the fields array */
+ sz = bus_gvariant_determine_word_size(m->fields_size, 0);
if (sz > 0) {
size_t framing;
+ void *q;
- ri = BUS_MESSAGE_FIELDS_SIZE(m) - sz;
+ ri = m->fields_size - 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)
+ framing = bus_gvariant_read_word_le(q, sz);
+ if (framing >= m->fields_size - sz)
return -EBADMSG;
- if ((BUS_MESSAGE_FIELDS_SIZE(m) - framing) % sz != 0)
+ if ((m->fields_size - framing) % sz != 0)
return -EBADMSG;
ri = framing;
- r = message_peek_fields(m, &ri, 1, BUS_MESSAGE_FIELDS_SIZE(m) - framing, &offsets);
+ r = message_peek_fields(m, &ri, 1, m->fields_size - framing, &offsets);
if (r < 0)
return r;
- n_offsets = (BUS_MESSAGE_FIELDS_SIZE(m) - framing) / sz;
+ n_offsets = (m->fields_size - framing) / sz;
}
- }
+ } else
+ m->user_body_size = m->body_size;
ri = 0;
- while (ri < BUS_MESSAGE_FIELDS_SIZE(m)) {
+ while (ri < m->fields_size) {
_cleanup_free_ char *sig = NULL;
const char *signature;
- uint8_t *header;
+ uint64_t field_type;
size_t item_size = (size_t) -1;
if (BUS_MESSAGE_IS_GVARIANT(m)) {
+ uint64_t *u64;
+
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);
- }
+ ri = ALIGN_TO(bus_gvariant_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_fields(m, &ri, 8, 8, (void**) &u64);
+ if (r < 0)
+ return r;
+
+ field_type = BUS_MESSAGE_BSWAP64(m, *u64);
+ } else {
+ uint8_t *u8;
+
+ r = message_peek_fields(m, &ri, 8, 1, (void**) &u8);
+ if (r < 0)
+ return r;
+
+ field_type = *u8;
+ }
if (BUS_MESSAGE_IS_GVARIANT(m)) {
size_t where, end;
char *b;
void *q;
- end = read_word_le((uint8_t*) offsets + i*sz, sz);
+ end = bus_gvariant_read_word_le((uint8_t*) offsets + i*sz, sz);
if (end < ri)
return -EBADMSG;
return r;
}
- switch (*header) {
+ switch (field_type) {
+
case _BUS_MESSAGE_HEADER_INVALID:
return -EBADMSG;
r = message_peek_field_string(m, service_name_is_valid, &ri, item_size, &m->sender);
- if (r >= 0 && m->sender[0] == ':' && m->bus->bus_client && !m->bus->is_kernel) {
+ if (r >= 0 && m->sender[0] == ':' && m->bus->bus_client) {
m->creds.unique_name = (char*) m->sender;
m->creds.mask |= SD_BUS_CREDS_UNIQUE_NAME & m->bus->creds_mask;
}
const char *s;
char *c;
+ if (BUS_MESSAGE_IS_GVARIANT(m)) /* only applies to dbus1 */
+ return -EBADMSG;
+
if (m->root_container.signature)
return -EBADMSG;
break;
}
- case BUS_MESSAGE_HEADER_REPLY_SERIAL: {
- uint32_t serial;
+ case BUS_MESSAGE_HEADER_REPLY_SERIAL:
if (m->reply_cookie != 0)
return -EBADMSG;
- if (!streq(signature, "u"))
- return -EBADMSG;
+ if (BUS_MESSAGE_IS_GVARIANT(m)) {
+ /* 64bit on dbus2 */
- r = message_peek_field_uint32(m, &ri, item_size, &serial);
- if (r < 0)
- return r;
+ if (!streq(signature, "t"))
+ return -EBADMSG;
+
+ r = message_peek_field_uint64(m, &ri, item_size, &m->reply_cookie);
+ if (r < 0)
+ return r;
+ } else {
+ /* 32bit on dbus1 */
+ uint32_t serial;
+
+ if (!streq(signature, "u"))
+ return -EBADMSG;
+
+ r = message_peek_field_uint32(m, &ri, item_size, &serial);
+ if (r < 0)
+ return r;
- m->reply_cookie = serial;
+ m->reply_cookie = serial;
+ }
if (m->reply_cookie == 0)
return -EBADMSG;
break;
- }
case BUS_MESSAGE_HEADER_UNIX_FDS:
if (unix_fds_set)
if (streq_ptr(m->sender, "org.freedesktop.DBus.Local"))
return -EBADMSG;
- m->root_container.end = BUS_MESSAGE_BODY_SIZE(m);
+ m->root_container.end = m->user_body_size;
if (BUS_MESSAGE_IS_GVARIANT(m)) {
r = build_struct_offsets(
m,
m->root_container.signature,
- BUS_MESSAGE_BODY_SIZE(m),
+ m->user_body_size,
&m->root_container.item_size,
&m->root_container.offsets,
&m->root_container.n_offsets);
/* 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);
+ (void) sd_bus_message_read(m, "s", &m->error.message);
return 0;
}
}
int bus_message_read_strv_extend(sd_bus_message *m, char ***l) {
+ const char *s;
int r;
assert(m);
if (r <= 0)
return r;
- for (;;) {
- const char *s;
-
- r = sd_bus_message_read_basic(m, 's', &s);
- if (r < 0)
- return r;
- if (r == 0)
- break;
-
+ while ((r = sd_bus_message_read_basic(m, 's', &s)) > 0) {
r = strv_extend(l, s);
if (r < 0)
return r;
}
+ if (r < 0)
+ return r;
r = sd_bus_message_exit_container(m);
if (r < 0)
return 1;
}
-const char* bus_message_get_arg(sd_bus_message *m, unsigned i) {
- int r;
- const char *t = NULL;
- unsigned j;
+static int bus_message_get_arg_skip(
+ sd_bus_message *m,
+ unsigned i,
+ char *_type,
+ const char **_contents) {
- assert(m);
+ unsigned j;
+ int r;
r = sd_bus_message_rewind(m, true);
if (r < 0)
- return NULL;
+ return r;
- for (j = 0; j <= i; j++) {
+ for (j = 0;; j++) {
+ const char *contents;
char type;
- r = sd_bus_message_peek_type(m, &type, NULL);
+ r = sd_bus_message_peek_type(m, &type, &contents);
if (r < 0)
- return NULL;
+ return r;
+ if (r == 0)
+ return -ENXIO;
- if (type != SD_BUS_TYPE_STRING &&
- type != SD_BUS_TYPE_OBJECT_PATH &&
- type != SD_BUS_TYPE_SIGNATURE)
- return NULL;
+ /* Don't match against arguments after the first one we don't understand */
+ if (!IN_SET(type, SD_BUS_TYPE_STRING, SD_BUS_TYPE_OBJECT_PATH, SD_BUS_TYPE_SIGNATURE) &&
+ !(type == SD_BUS_TYPE_ARRAY && STR_IN_SET(contents, "s", "o", "g")))
+ return -ENXIO;
- r = sd_bus_message_read_basic(m, type, &t);
+ if (j >= i) {
+ if (_contents)
+ *_contents = contents;
+ if (_type)
+ *_type = type;
+ return 0;
+ }
+
+ r = sd_bus_message_skip(m, NULL);
if (r < 0)
- return NULL;
+ return r;
}
- return t;
}
-bool bus_header_is_complete(struct bus_header *h, size_t size) {
- size_t full;
+int bus_message_get_arg(sd_bus_message *m, unsigned i, const char **str) {
+ char type;
+ int r;
- assert(h);
- assert(size);
+ assert(m);
+ assert(str);
- if (size < sizeof(struct bus_header))
- return false;
+ r = bus_message_get_arg_skip(m, i, &type, NULL);
+ if (r < 0)
+ return r;
- full = sizeof(struct bus_header) +
- (h->endian == BUS_NATIVE_ENDIAN ? h->fields_size : bswap_32(h->fields_size));
+ if (!IN_SET(type, SD_BUS_TYPE_STRING, SD_BUS_TYPE_OBJECT_PATH, SD_BUS_TYPE_SIGNATURE))
+ return -ENXIO;
- return size >= full;
+ return sd_bus_message_read_basic(m, type, str);
}
-int bus_header_message_size(struct bus_header *h, size_t *sum) {
- size_t fs, bs;
+int bus_message_get_arg_strv(sd_bus_message *m, unsigned i, char ***strv) {
+ const char *contents;
+ char type;
+ int r;
- assert(h);
- assert(sum);
+ assert(m);
+ assert(strv);
- if (h->endian == BUS_NATIVE_ENDIAN) {
- fs = h->fields_size;
- bs = h->body_size;
- } else if (h->endian == BUS_REVERSE_ENDIAN) {
- fs = bswap_32(h->fields_size);
- bs = bswap_32(h->body_size);
- } else
- return -EBADMSG;
+ r = bus_message_get_arg_skip(m, i, &type, &contents);
+ if (r < 0)
+ return r;
- *sum = sizeof(struct bus_header) + ALIGN8(fs) + bs;
- return 0;
+ if (type != SD_BUS_TYPE_ARRAY)
+ return -ENXIO;
+ if (!STR_IN_SET(contents, "s", "o", "g"))
+ return -ENXIO;
+
+ return sd_bus_message_read_strv(m, strv);
}
_public_ int sd_bus_message_get_errno(sd_bus_message *m) {
assert(r > 0);
- if (type == SD_BUS_TYPE_OBJECT_PATH ||
- type == SD_BUS_TYPE_SIGNATURE ||
- type == SD_BUS_TYPE_STRING)
+ if (IN_SET(type, SD_BUS_TYPE_OBJECT_PATH, SD_BUS_TYPE_SIGNATURE, SD_BUS_TYPE_STRING))
r = sd_bus_message_append_basic(m, type, basic.string);
else
r = sd_bus_message_append_basic(m, type, &basic);
}
int bus_message_remarshal(sd_bus *bus, sd_bus_message **m) {
- _cleanup_bus_message_unref_ sd_bus_message *n = NULL;
+ _cleanup_(sd_bus_message_unrefp) sd_bus_message *n = NULL;
usec_t timeout;
int r;
case SD_BUS_MESSAGE_METHOD_RETURN:
case SD_BUS_MESSAGE_METHOD_ERROR:
- n = message_new(bus, (*m)->header->type);
- if (!n)
+ r = sd_bus_message_new(bus, &n, (*m)->header->type);
+ if (r < 0)
return -ENOMEM;
+ assert(n);
+
n->reply_cookie = (*m)->reply_cookie;
- r = message_append_field_uint32(n, BUS_MESSAGE_HEADER_REPLY_SERIAL, (uint32_t) n->reply_cookie);
+
+ r = message_append_reply_cookie(n, n->reply_cookie);
if (r < 0)
return r;
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);
+ r = sd_bus_message_seal(n, BUS_MESSAGE_COOKIE(*m), timeout);
if (r < 0)
return r;