chiark / gitweb /
bus: add new sd_bus_creds object to encapsulate process credentials
[elogind.git] / src / libsystemd-bus / bus-message.c
index c1e1c468b81a46016d19359d0488148ae8294f11..dd058e72c426b3c10d8ddd2b41b23688e7b22c31 100644 (file)
@@ -59,17 +59,21 @@ static void message_free_part(sd_bus_message *m, struct bus_body_part *part) {
         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);
                 else {
-                        if (part->size > 0)
-                                assert_se(munmap(part->data, PAGE_ALIGN(part->size)) == 0);
+                        if (part->mapped > 0)
+                                assert_se(munmap(part->data, part->mapped) == 0);
 
                         close_nointr_nofail(part->memfd);
                 }
 
-        } else if (part->free_this)
+        } else if (part->munmap_this)
+                munmap(part->data, part->mapped);
+        else if (part->free_this)
                 free(part->data);
 
         if (part != &m->body)
@@ -121,30 +125,30 @@ static void message_free(sd_bus_message *m) {
         if (m->free_kdbus)
                 free(m->kdbus);
 
-        if (m->release_kdbus)
-                ioctl(m->bus->input_fd, KDBUS_CMD_MSG_RELEASE, m->kdbus);
+        if (m->release_kdbus) {
+                uint64_t off;
 
-        if (m->free_fds) {
-                close_many(m->fds, m->n_fds);
-                free(m->fds);
+                off = (uint8_t *)m->kdbus - (uint8_t *)m->bus->kdbus_buffer;
+                ioctl(m->bus->input_fd, KDBUS_CMD_MSG_RELEASE, &off);
         }
 
         if (m->bus)
                 sd_bus_unref(m->bus);
 
+        if (m->free_fds) {
+                close_many(m->fds, m->n_fds);
+                free(m->fds);
+        }
+
         if (m->iovec != m->iovec_fixed)
                 free(m->iovec);
 
-        free(m->cmdline_array);
-
         message_reset_containers(m);
         free(m->root_container.signature);
 
         free(m->peeked_signature);
 
-        free(m->unit);
-        free(m->user_unit);
-        free(m->session);
+        bus_creds_done(&m->creds);
         free(m);
 }
 
@@ -297,6 +301,7 @@ static int message_append_field_uint32(sd_bus_message *m, uint8_t h, uint32_t x)
 }
 
 int bus_message_from_header(
+                sd_bus *bus,
                 void *buffer,
                 size_t length,
                 int *fds,
@@ -349,22 +354,28 @@ int bus_message_from_header(
         m->n_fds = n_fds;
 
         if (ucred) {
-                m->uid = ucred->uid;
-                m->pid = ucred->pid;
-                m->gid = ucred->gid;
-                m->uid_valid = m->gid_valid = true;
+                m->creds.uid = ucred->uid;
+                m->creds.pid = ucred->pid;
+                m->creds.gid = ucred->gid;
+                m->creds.mask |= SD_BUS_CREDS_UID | SD_BUS_CREDS_PID | SD_BUS_CREDS_GID;
         }
 
         if (label) {
-                m->label = (char*) m + ALIGN(sizeof(sd_bus_message)) + ALIGN(extra);
-                memcpy(m->label, label, label_sz + 1);
+                m->creds.label = (char*) m + ALIGN(sizeof(sd_bus_message)) + ALIGN(extra);
+                memcpy(m->creds.label, label, label_sz + 1);
+
+                m->creds.mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
         }
 
+        if (bus)
+                m->bus = sd_bus_ref(bus);
+
         *ret = m;
         return 0;
 }
 
 int bus_message_from_malloc(
+                sd_bus *bus,
                 void *buffer,
                 size_t length,
                 int *fds,
@@ -376,7 +387,7 @@ int bus_message_from_malloc(
         sd_bus_message *m;
         int r;
 
-        r = bus_message_from_header(buffer, length, fds, n_fds, ucred, label, 0, &m);
+        r = bus_message_from_header(bus, buffer, length, fds, n_fds, ucred, label, 0, &m);
         if (r < 0)
                 return r;
 
@@ -432,7 +443,7 @@ static sd_bus_message *message_new(sd_bus *bus, uint8_t type) {
         return m;
 }
 
-int sd_bus_message_new_signal(
+_public_ int sd_bus_message_new_signal(
                 sd_bus *bus,
                 const char *path,
                 const char *interface,
@@ -442,18 +453,13 @@ int sd_bus_message_new_signal(
         sd_bus_message *t;
         int r;
 
-        if (!path)
-                return -EINVAL;
-        if (!interface)
-                return -EINVAL;
-        if (!member)
-                return -EINVAL;
-        if (!m)
-                return -EINVAL;
-        if (bus && bus->state == BUS_UNSET)
-                return -ENOTCONN;
+        assert_return(!bus || bus->state != BUS_UNSET, -ENOTCONN);
+        assert_return(object_path_is_valid(path), -EINVAL);
+        assert_return(interface_name_is_valid(interface), -EINVAL);
+        assert_return(member_name_is_valid(member), -EINVAL);
+        assert_return(m, -EINVAL);
 
-        t = message_new(bus, SD_BUS_MESSAGE_TYPE_SIGNAL);
+        t = message_new(bus, SD_BUS_MESSAGE_SIGNAL);
         if (!t)
                 return -ENOMEM;
 
@@ -477,7 +483,7 @@ fail:
         return r;
 }
 
-int sd_bus_message_new_method_call(
+_public_ int sd_bus_message_new_method_call(
                 sd_bus *bus,
                 const char *destination,
                 const char *path,
@@ -488,16 +494,14 @@ int sd_bus_message_new_method_call(
         sd_bus_message *t;
         int r;
 
-        if (!path)
-                return -EINVAL;
-        if (!member)
-                return -EINVAL;
-        if (!m)
-                return -EINVAL;
-        if (bus && bus->state == BUS_UNSET)
-                return -ENOTCONN;
+        assert_return(!bus || bus->state != BUS_UNSET, -ENOTCONN);
+        assert_return(!destination || service_name_is_valid(destination), -EINVAL);
+        assert_return(object_path_is_valid(path), -EINVAL);
+        assert_return(!interface || interface_name_is_valid(interface), -EINVAL);
+        assert_return(member_name_is_valid(member), -EINVAL);
+        assert_return(m, -EINVAL);
 
-        t = message_new(bus, SD_BUS_MESSAGE_TYPE_METHOD_CALL);
+        t = message_new(bus, SD_BUS_MESSAGE_METHOD_CALL);
         if (!t)
                 return -ENOMEM;
 
@@ -529,7 +533,6 @@ fail:
 }
 
 static int message_new_reply(
-                sd_bus *bus,
                 sd_bus_message *call,
                 uint8_t type,
                 sd_bus_message **m) {
@@ -537,18 +540,13 @@ static int message_new_reply(
         sd_bus_message *t;
         int r;
 
-        if (!call)
-                return -EINVAL;
-        if (!call->sealed)
-                return -EPERM;
-        if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
-                return -EINVAL;
-        if (!m)
-                return -EINVAL;
-        if (bus && bus->state == BUS_UNSET)
-                return -ENOTCONN;
+        assert_return(call, -EINVAL);
+        assert_return(call->sealed, -EPERM);
+        assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
+        assert_return(!call->bus || call->bus->state != BUS_UNSET, -ENOTCONN);
+        assert_return(m, -EINVAL);
 
-        t = message_new(bus, type);
+        t = message_new(call->bus, type);
         if (!t)
                 return -ENOMEM;
 
@@ -560,7 +558,7 @@ static int message_new_reply(
                 goto fail;
 
         if (call->sender) {
-                r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, call->sender, &t->sender);
+                r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, call->sender, &t->destination);
                 if (r < 0)
                         goto fail;
         }
@@ -575,16 +573,14 @@ fail:
         return r;
 }
 
-int sd_bus_message_new_method_return(
-                sd_bus *bus,
+_public_ int sd_bus_message_new_method_return(
                 sd_bus_message *call,
                 sd_bus_message **m) {
 
-        return message_new_reply(bus, call, SD_BUS_MESSAGE_TYPE_METHOD_RETURN, m);
+        return message_new_reply(call, SD_BUS_MESSAGE_METHOD_RETURN, m);
 }
 
-int sd_bus_message_new_method_error(
-                sd_bus *bus,
+_public_ int sd_bus_message_new_method_error(
                 sd_bus_message *call,
                 const sd_bus_error *e,
                 sd_bus_message **m) {
@@ -592,12 +588,10 @@ int sd_bus_message_new_method_error(
         sd_bus_message *t;
         int r;
 
-        if (!sd_bus_error_is_set(e))
-                return -EINVAL;
-        if (!m)
-                return -EINVAL;
+        assert_return(sd_bus_error_is_set(e), -EINVAL);
+        assert_return(m, -EINVAL);
 
-        r = message_new_reply(bus, call, SD_BUS_MESSAGE_TYPE_METHOD_ERROR, &t);
+        r = message_new_reply(call, SD_BUS_MESSAGE_METHOD_ERROR, &t);
         if (r < 0)
                 return r;
 
@@ -619,405 +613,235 @@ fail:
         return r;
 }
 
-sd_bus_message* sd_bus_message_ref(sd_bus_message *m) {
-        if (!m)
-                return NULL;
-
-        assert(m->n_ref > 0);
-        m->n_ref++;
-
-        return m;
-}
-
-sd_bus_message* sd_bus_message_unref(sd_bus_message *m) {
-        if (!m)
-                return NULL;
-
-        assert(m->n_ref > 0);
-        m->n_ref--;
+_public_ int sd_bus_message_new_method_errorf(
+                sd_bus_message *call,
+                sd_bus_message **m,
+                const char *name,
+                const char *format,
+                ...) {
 
-        if (m->n_ref <= 0)
-                message_free(m);
+        _cleanup_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+        va_list ap;
 
-        return NULL;
-}
+        assert_return(name, -EINVAL);
+        assert_return(m, -EINVAL);
 
-int sd_bus_message_get_type(sd_bus_message *m, uint8_t *type) {
-        if (!m)
-                return -EINVAL;
-        if (!type)
-                return -EINVAL;
+        va_start(ap, format);
+        bus_error_setfv(&error, name, format, ap);
+        va_end(ap);
 
-        *type = m->header->type;
-        return 0;
+        return sd_bus_message_new_method_error(call, &error, m);
 }
 
-int sd_bus_message_get_serial(sd_bus_message *m, uint64_t *serial) {
-        if (!m)
-                return -EINVAL;
-        if (!serial)
-                return -EINVAL;
-        if (m->header->serial == 0)
-                return -ENOENT;
-
-        *serial = BUS_MESSAGE_SERIAL(m);
-        return 0;
-}
+_public_ int sd_bus_message_new_method_errno(
+                sd_bus_message *call,
+                int error,
+                const sd_bus_error *p,
+                sd_bus_message **m) {
 
-int sd_bus_message_get_reply_serial(sd_bus_message *m, uint64_t *serial) {
-        if (!m)
-                return -EINVAL;
-        if (!serial)
-                return -EINVAL;
-        if (m->reply_serial == 0)
-                return -ENOENT;
+        _cleanup_free_ sd_bus_error berror = SD_BUS_ERROR_NULL;
 
-        *serial = m->reply_serial;
-        return 0;
-}
+        if (sd_bus_error_is_set(p))
+                return sd_bus_message_new_method_error(call, p, m);
 
-int sd_bus_message_get_no_reply(sd_bus_message *m) {
-        if (!m)
-                return -EINVAL;
+        sd_bus_error_set_errno(&berror, error);
 
-        return m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_CALL ? !!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED) : 0;
+        return sd_bus_message_new_method_error(call, &berror, m);
 }
 
-const char *sd_bus_message_get_path(sd_bus_message *m) {
-        if (!m)
-                return NULL;
+_public_ int sd_bus_message_new_method_errnof(
+                sd_bus_message *call,
+                sd_bus_message **m,
+                int error,
+                const char *format,
+                ...) {
 
-        return m->path;
-}
+        _cleanup_free_ sd_bus_error berror = SD_BUS_ERROR_NULL;
+        va_list ap;
 
-const char *sd_bus_message_get_interface(sd_bus_message *m) {
-        if (!m)
-                return NULL;
+        va_start(ap, format);
+        bus_error_set_errnofv(&berror, error, format, ap);
+        va_end(ap);
 
-        return m->interface;
+        return sd_bus_message_new_method_error(call, &berror, m);
 }
 
-const char *sd_bus_message_get_member(sd_bus_message *m) {
-        if (!m)
-                return NULL;
+int bus_message_new_synthetic_error(
+                sd_bus *bus,
+                uint64_t serial,
+                const sd_bus_error *e,
+                sd_bus_message **m) {
 
-        return m->member;
-}
-const char *sd_bus_message_get_destination(sd_bus_message *m) {
-        if (!m)
-                return NULL;
+        sd_bus_message *t;
+        int r;
 
-        return m->destination;
-}
+        assert(sd_bus_error_is_set(e));
+        assert(m);
 
-const char *sd_bus_message_get_sender(sd_bus_message *m) {
-        if (!m)
-                return NULL;
+        t = message_new(bus, SD_BUS_MESSAGE_METHOD_ERROR);
+        if (!t)
+                return -ENOMEM;
 
-        return m->sender;
-}
+        t->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
+        t->reply_serial = serial;
 
-const sd_bus_error *sd_bus_message_get_error(sd_bus_message *m) {
-        if (!m)
-                return NULL;
+        r = message_append_field_uint32(t, SD_BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_serial);
+        if (r < 0)
+                goto fail;
 
-        if (!sd_bus_error_is_set(&m->error))
-                return NULL;
+        if (bus && bus->unique_name) {
+                r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, bus->unique_name, &t->destination);
+                if (r < 0)
+                        goto fail;
+        }
 
-        return &m->error;
-}
+        r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, e->name, &t->error.name);
+        if (r < 0)
+                goto fail;
 
-int sd_bus_message_get_uid(sd_bus_message *m, uid_t *uid) {
-        if (!m)
-                return -EINVAL;
-        if (!uid)
-                return -EINVAL;
-        if (!m->uid_valid)
-                return -ESRCH;
+        if (e->message) {
+                r = message_append_basic(t, SD_BUS_TYPE_STRING, e->message, (const void**) &t->error.message);
+                if (r < 0)
+                        goto fail;
+        }
 
-        *uid = m->uid;
+        *m = t;
         return 0;
-}
 
-int sd_bus_message_get_gid(sd_bus_message *m, gid_t *gid) {
-        if (!m)
-                return -EINVAL;
-        if (!gid)
-                return -EINVAL;
-        if (!m->gid_valid)
-                return -ESRCH;
-
-        *gid = m->gid;
-        return 0;
+fail:
+        message_free(t);
+        return r;
 }
 
-int sd_bus_message_get_pid(sd_bus_message *m, pid_t *pid) {
-        if (!m)
-                return -EINVAL;
-        if (!pid)
-                return -EINVAL;
-        if (m->pid <= 0)
-                return -ESRCH;
+_public_ sd_bus_message* sd_bus_message_ref(sd_bus_message *m) {
+        assert_return(m, NULL);
 
-        *pid = m->pid;
-        return 0;
-}
-
-int sd_bus_message_get_tid(sd_bus_message *m, pid_t *tid) {
-        if (!m)
-                return -EINVAL;
-        if (!tid)
-                return -EINVAL;
-        if (m->tid <= 0)
-                return -ESRCH;
+        assert(m->n_ref > 0);
+        m->n_ref++;
 
-        *tid = m->tid;
-        return 0;
+        return m;
 }
 
-int sd_bus_message_get_pid_starttime(sd_bus_message *m, uint64_t *usec) {
-        if (!m)
-                return -EINVAL;
-        if (!usec)
-                return -EINVAL;
-        if (m->pid_starttime <= 0)
-                return -ESRCH;
+_public_ sd_bus_message* sd_bus_message_unref(sd_bus_message *m) {
+        assert_return(m, NULL);
 
-        *usec = m->pid_starttime;
-        return 0;
-}
+        assert(m->n_ref > 0);
+        m->n_ref--;
 
-int sd_bus_message_get_selinux_context(sd_bus_message *m, const char **ret) {
-        if (!m)
-                return -EINVAL;
-        if (!m->label)
-                return -ESRCH;
+        if (m->n_ref <= 0)
+                message_free(m);
 
-        *ret = m->label;
-        return 0;
+        return NULL;
 }
 
-int sd_bus_message_get_monotonic_timestamp(sd_bus_message *m, uint64_t *usec) {
-        if (!m)
-                return -EINVAL;
-        if (!usec)
-                return -EINVAL;
-        if (m->monotonic <= 0)
-                return -ESRCH;
+_public_ int sd_bus_message_get_type(sd_bus_message *m, uint8_t *type) {
+        assert_return(m, -EINVAL);
+        assert_return(type, -EINVAL);
 
-        *usec = m->monotonic;
+        *type = m->header->type;
         return 0;
 }
 
-int sd_bus_message_get_realtime_timestamp(sd_bus_message *m, uint64_t *usec) {
-        if (!m)
-                return -EINVAL;
-        if (!usec)
-                return -EINVAL;
-        if (m->realtime <= 0)
-                return -ESRCH;
+_public_ int sd_bus_message_get_serial(sd_bus_message *m, uint64_t *serial) {
+        assert_return(m, -EINVAL);
+        assert_return(serial, -EINVAL);
+        assert_return(m->header->serial != 0, -ENOENT);
 
-        *usec = m->realtime;
+        *serial = BUS_MESSAGE_SERIAL(m);
         return 0;
 }
 
-int sd_bus_message_get_comm(sd_bus_message *m, const char **ret) {
-        if (!m)
-                return -EINVAL;
-        if (!ret)
-                return -EINVAL;
-        if (!m->comm)
-                return -ESRCH;
+_public_ int sd_bus_message_get_reply_serial(sd_bus_message *m, uint64_t *serial) {
+        assert_return(m, -EINVAL);
+        assert_return(serial, -EINVAL);
+        assert_return(m->reply_serial != 0, -ENOENT);
 
-        *ret = m->comm;
+        *serial = m->reply_serial;
         return 0;
 }
 
-int sd_bus_message_get_tid_comm(sd_bus_message *m, const char **ret) {
-        if (!m)
-                return -EINVAL;
-        if (!ret)
-                return -EINVAL;
-        if (!m->tid_comm)
-                return -ESRCH;
+_public_ int sd_bus_message_get_no_reply(sd_bus_message *m) {
+        assert_return(m, -EINVAL);
 
-        *ret = m->tid_comm;
-        return 0;
+        return m->header->type == SD_BUS_MESSAGE_METHOD_CALL ? !!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED) : 0;
 }
 
-int sd_bus_message_get_exe(sd_bus_message *m, const char **ret) {
-        if (!m)
-                return -EINVAL;
-        if (!ret)
-                return -EINVAL;
-        if (!m->exe)
-                return -ESRCH;
+_public_ int sd_bus_message_get_no_auto_start(sd_bus_message *m) {
+        assert_return(m, -EINVAL);
 
-        *ret = m->exe;
-        return 0;
+        return !!(m->header->flags & SD_BUS_MESSAGE_NO_AUTO_START);
 }
 
-int sd_bus_message_get_cgroup(sd_bus_message *m, const char **ret) {
-        if (!m)
-                return -EINVAL;
-        if (!ret)
-                return -EINVAL;
-        if (!m->cgroup)
-                return -ESRCH;
+_public_ const char *sd_bus_message_get_path(sd_bus_message *m) {
+        assert_return(m, NULL);
 
-        *ret = m->cgroup;
-        return 0;
+        return m->path;
 }
 
-int sd_bus_message_get_unit(sd_bus_message *m, const char **ret) {
-        int r;
-
-        if (!m)
-                return -EINVAL;
-        if (!ret)
-                return -EINVAL;
-        if (!m->cgroup)
-                return -ESRCH;
+_public_ const char *sd_bus_message_get_interface(sd_bus_message *m) {
+        assert_return(m, NULL);
 
-        if (!m->unit) {
-                r = cg_path_get_unit(m->cgroup, &m->unit);
-                if (r < 0)
-                        return r;
-        }
-
-        *ret = m->unit;
-        return 0;
+        return m->interface;
 }
 
-int sd_bus_message_get_user_unit(sd_bus_message *m, const char **ret) {
-        int r;
-
-        if (!m)
-                return -EINVAL;
-        if (!ret)
-                return -EINVAL;
-        if (!m->cgroup)
-                return -ESRCH;
-
-        if (!m->user_unit) {
-                r = cg_path_get_user_unit(m->cgroup, &m->user_unit);
-                if (r < 0)
-                        return r;
-        }
+_public_ const char *sd_bus_message_get_member(sd_bus_message *m) {
+        assert_return(m, NULL);
 
-        *ret = m->user_unit;
-        return 0;
+        return m->member;
 }
 
-int sd_bus_message_get_session(sd_bus_message *m, const char **ret) {
-        int r;
+_public_ const char *sd_bus_message_get_destination(sd_bus_message *m) {
+        assert_return(m, NULL);
 
-        if (!m)
-                return -EINVAL;
-        if (!ret)
-                return -EINVAL;
-        if (!m->cgroup)
-                return -ESRCH;
-
-        if (!m->session) {
-                r = cg_path_get_session(m->cgroup, &m->session);
-                if (r < 0)
-                        return r;
-        }
-
-        *ret = m->session;
-        return 0;
+        return m->destination;
 }
 
-int sd_bus_message_get_owner_uid(sd_bus_message *m, uid_t *uid) {
-        if (!m)
-                return -EINVAL;
-        if (!uid)
-                return -EINVAL;
-        if (!m->cgroup)
-                return -ESRCH;
+_public_ const char *sd_bus_message_get_sender(sd_bus_message *m) {
+        assert_return(m, NULL);
 
-        return cg_path_get_owner_uid(m->cgroup, uid);
+        return m->sender;
 }
 
-int sd_bus_message_get_cmdline(sd_bus_message *m, char ***cmdline) {
-        size_t n, i;
-        const char *p;
-        bool first;
-
-        if (!m)
-                return -EINVAL;
-
-        if (!m->cmdline)
-                return -ENOENT;
-
-        for (p = m->cmdline, n = 0; p < m->cmdline + m->cmdline_length; p++)
-                if (*p == 0)
-                        n++;
-
-        m->cmdline_array = new(char*, n + 1);
-        if (!m->cmdline_array)
-                return -ENOMEM;
+_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);
 
-        for (p = m->cmdline, i = 0, first = true; p < m->cmdline + m->cmdline_length; p++) {
-                if (first)
-                        m->cmdline_array[i++] = (char*) p;
-
-                first = *p == 0;
-        }
-
-        m->cmdline_array[i] = NULL;
-        *cmdline = m->cmdline_array;
-
-        return 0;
+        return &m->error;
 }
 
-int sd_bus_message_get_audit_sessionid(sd_bus_message *m, uint32_t *sessionid) {
-        if (!m)
-                return -EINVAL;
-        if (!sessionid)
-                return -EINVAL;
-        if (!m->audit)
-                return -ESRCH;
+_public_ int sd_bus_message_get_monotonic_timestamp(sd_bus_message *m, uint64_t *usec) {
+        assert_return(m, -EINVAL);
+        assert_return(usec, -EINVAL);
+        assert_return(m->monotonic > 0, -ENODATA);
 
-        *sessionid = m->audit->sessionid;
+        *usec = m->monotonic;
         return 0;
 }
 
-int sd_bus_message_get_audit_loginuid(sd_bus_message *m, uid_t *uid) {
-        if (!m)
-                return -EINVAL;
-        if (!uid)
-                return -EINVAL;
-        if (!m->audit)
-                return -ESRCH;
+_public_ int sd_bus_message_get_realtime_timestamp(sd_bus_message *m, uint64_t *usec) {
+        assert_return(m, -EINVAL);
+        assert_return(usec, -EINVAL);
+        assert_return(m->realtime > 0, -ENODATA);
 
-        *uid = m->audit->loginuid;
+        *usec = m->realtime;
         return 0;
 }
 
-int sd_bus_message_has_effective_cap(sd_bus_message *m, int capability) {
-        unsigned sz;
+_public_ sd_bus_creds *sd_bus_message_get_creds(sd_bus_message *m) {
+        assert_return(m, NULL);
 
-        if (!m)
-                return -EINVAL;
-        if (capability < 0)
-                return -EINVAL;
-        if (!m->capability)
-                return -ESRCH;
-
-        sz = m->capability_size / 4;
-        if ((unsigned) capability >= sz*8)
-                return 0;
+        if (m->creds.mask == 0)
+                return NULL;
 
-        return !!(m->capability[2 * sz + (capability / 8)] & (1 << (capability % 8)));
+        return &m->creds;
 }
 
-int sd_bus_message_is_signal(sd_bus_message *m, const char *interface, const char *member) {
-        if (!m)
-                return -EINVAL;
+_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_TYPE_SIGNAL)
+        if (m->header->type != SD_BUS_MESSAGE_SIGNAL)
                 return 0;
 
         if (interface && (!m->interface || !streq(m->interface, interface)))
@@ -1029,11 +853,12 @@ int sd_bus_message_is_signal(sd_bus_message *m, const char *interface, const cha
         return 1;
 }
 
-int sd_bus_message_is_method_call(sd_bus_message *m, const char *interface, const char *member) {
-        if (!m)
-                return -EINVAL;
+_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_TYPE_METHOD_CALL)
+        if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
                 return 0;
 
         if (interface && (!m->interface || !streq(m->interface, interface)))
@@ -1045,11 +870,10 @@ int sd_bus_message_is_method_call(sd_bus_message *m, const char *interface, cons
         return 1;
 }
 
-int sd_bus_message_is_method_error(sd_bus_message *m, const char *name) {
-        if (!m)
-                return -EINVAL;
+_public_ int sd_bus_message_is_method_error(sd_bus_message *m, const char *name) {
+        assert_return(m, -EINVAL);
 
-        if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_ERROR)
+        if (m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
                 return 0;
 
         if (name && (!m->error.name || !streq(m->error.name, name)))
@@ -1058,13 +882,10 @@ int sd_bus_message_is_method_error(sd_bus_message *m, const char *name) {
         return 1;
 }
 
-int sd_bus_message_set_no_reply(sd_bus_message *m, int b) {
-        if (!m)
-                return -EINVAL;
-        if (m->sealed)
-                return -EPERM;
-        if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
-                return -EPERM;
+_public_ int sd_bus_message_set_no_reply(sd_bus_message *m, int b) {
+        assert_return(m, -EINVAL);
+        assert_return(!m->sealed, -EPERM);
+        assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EPERM);
 
         if (b)
                 m->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
@@ -1074,6 +895,18 @@ int sd_bus_message_set_no_reply(sd_bus_message *m, int b) {
         return 0;
 }
 
+_public_ int sd_bus_message_set_no_auto_start(sd_bus_message *m, int b) {
+        assert_return(m, -EINVAL);
+        assert_return(!m->sealed, -EPERM);
+
+        if (b)
+                m->header->flags |= SD_BUS_MESSAGE_NO_AUTO_START;
+        else
+                m->header->flags &= ~SD_BUS_MESSAGE_NO_AUTO_START;
+
+        return 0;
+}
+
 static struct bus_container *message_get_container(sd_bus_message *m) {
         assert(m);
 
@@ -1119,8 +952,13 @@ static void part_zero(struct bus_body_part *part, size_t sz) {
         assert(sz > 0);
         assert(sz < 8);
 
-        part->data = NULL;
+        /* All other fields can be left in their defaults */
+        assert(!part->data);
+        assert(part->memfd < 0);
+
         part->size = sz;
+        part->is_zero = true;
+        part->sealed = true;
 }
 
 static int part_make_space(
@@ -1151,8 +989,8 @@ static int part_make_space(
                         return -errno;
                 }
 
-                if (sz > part->mapped) {
-                        size_t psz = PAGE_ALIGN(sz);
+                if (!part->data || sz > part->mapped) {
+                        size_t psz = PAGE_ALIGN(sz > 0 ? sz : 1);
 
                         if (part->mapped <= 0)
                                 n = mmap(NULL, psz, PROT_READ|PROT_WRITE, MAP_SHARED, part->memfd, 0);
@@ -1167,8 +1005,10 @@ static int part_make_space(
                         part->mapped = psz;
                         part->data = n;
                 }
+
+                part->munmap_this = true;
         } else {
-                n = realloc(part->data, sz);
+                n = realloc(part->data, MAX(sz, 1u));
                 if (!n) {
                         m->poisoned = true;
                         return -ENOMEM;
@@ -1185,8 +1025,21 @@ static int part_make_space(
         return 0;
 }
 
-static void *message_extend_body(sd_bus_message *m, size_t align, size_t sz) {
+static void message_extend_containers(sd_bus_message *m, size_t expand) {
         struct bus_container *c;
+
+        assert(m);
+
+        if (expand <= 0)
+                return;
+
+        /* Update counters */
+        for (c = m->containers; c < m->containers + m->n_containers; c++)
+                if (c->array_size)
+                        *c->array_size += expand;
+}
+
+static void *message_extend_body(sd_bus_message *m, size_t align, size_t sz) {
         struct bus_body_part *part = NULL;
         size_t start_body, end_body, padding, start_part, end_part, added;
         bool add_new_part;
@@ -1234,6 +1087,7 @@ static void *message_extend_body(sd_bus_message *m, size_t align, size_t sz) {
                 if (r < 0)
                         return NULL;
         } else {
+                struct bus_container *c;
                 void *op;
                 size_t os;
 
@@ -1260,12 +1114,8 @@ static void *message_extend_body(sd_bus_message *m, size_t align, size_t sz) {
                 m->error.message = (const char*) adjust_pointer(m->error.message, op, os, part->data);
         }
 
-        /* Update counters */
-        for (c = m->containers; c < m->containers + m->n_containers; c++)
-                if (c->array_size)
-                        *c->array_size += added;
-
         m->header->body_size = end_body;
+        message_extend_containers(m, added);
 
         return p;
 }
@@ -1275,21 +1125,14 @@ int message_append_basic(sd_bus_message *m, char type, const void *p, const void
         ssize_t align, sz;
         uint32_t k;
         void *a;
-        char *e = NULL;
         int fd = -1;
         uint32_t fdi = 0;
         int r;
 
-        if (!m)
-                return -EINVAL;
-        if (!p)
-                return -EINVAL;
-        if (m->sealed)
-                return -EPERM;
-        if (!bus_type_is_basic(type))
-                return -EINVAL;
-        if (m->poisoned)
-                return -ESTALE;
+        assert_return(m, -EINVAL);
+        assert_return(!m->sealed, -EPERM);
+        assert_return(bus_type_is_basic(type), -EINVAL);
+        assert_return(!m->poisoned, -ESTALE);
 
         c = message_get_container(m);
 
@@ -1299,6 +1142,8 @@ int message_append_basic(sd_bus_message *m, char type, const void *p, const void
                 if (c->signature[c->index] != type)
                         return -ENXIO;
         } else {
+                char *e;
+
                 /* Maybe we can append to the signature? But only if this is the top-level container*/
                 if (c->enclosing != 0)
                         return -ENXIO;
@@ -1313,19 +1158,40 @@ int message_append_basic(sd_bus_message *m, char type, const void *p, const void
         switch (type) {
 
         case SD_BUS_TYPE_STRING:
+                /* To make things easy we'll serialize a NULL string
+                 * into the empty string */
+                p = strempty(p);
+
+                /* Fall through... */
         case SD_BUS_TYPE_OBJECT_PATH:
 
+                if (!p) {
+                        r = -EINVAL;
+                        goto fail;
+                }
+
                 align = 4;
                 sz = 4 + strlen(p) + 1;
                 break;
 
         case SD_BUS_TYPE_SIGNATURE:
 
+                if (!p) {
+                        r = -EINVAL;
+                        goto fail;
+                }
+
                 align = 1;
                 sz = 1 + strlen(p) + 1;
                 break;
 
         case SD_BUS_TYPE_BOOLEAN:
+
+                if (!p) {
+                        r = -EINVAL;
+                        goto fail;
+                }
+
                 align = sz = 4;
 
                 assert_cc(sizeof(int) == sizeof(uint32_t));
@@ -1337,6 +1203,11 @@ int message_append_basic(sd_bus_message *m, char type, const void *p, const void
         case SD_BUS_TYPE_UNIX_FD: {
                 int z, *f;
 
+                if (!p) {
+                        r = -EINVAL;
+                        goto fail;
+                }
+
                 if (!m->allow_fds) {
                         r = -ENOTSUP;
                         goto fail;
@@ -1371,6 +1242,11 @@ int message_append_basic(sd_bus_message *m, char type, const void *p, const void
         }
 
         default:
+                if (!p) {
+                        r = -EINVAL;
+                        goto fail;
+                }
+
                 align = bus_type_get_alignment(type);
                 sz = bus_type_get_size(type);
                 break;
@@ -1425,23 +1301,22 @@ fail:
         return r;
 }
 
-int sd_bus_message_append_basic(sd_bus_message *m, char type, const void *p) {
+_public_ int sd_bus_message_append_basic(sd_bus_message *m, char type, const void *p) {
         return message_append_basic(m, type, p, NULL);
 }
 
-int sd_bus_message_append_string_space(sd_bus_message *m, size_t size, char **s) {
+_public_ int sd_bus_message_append_string_space(
+                sd_bus_message *m,
+                size_t size,
+                char **s) {
+
         struct bus_container *c;
-        char *e;
         void *a;
 
-        if (!m)
-                return -EINVAL;
-        if (!s)
-                return -EINVAL;
-        if (m->sealed)
-                return -EPERM;
-        if (m->poisoned)
-                return -ESTALE;
+        assert_return(m, -EINVAL);
+        assert_return(s, -EINVAL);
+        assert_return(!m->sealed, -EPERM);
+        assert_return(!m->poisoned, -ESTALE);
 
         c = message_get_container(m);
 
@@ -1451,6 +1326,8 @@ int sd_bus_message_append_string_space(sd_bus_message *m, size_t size, char **s)
                 if (c->signature[c->index] != SD_BUS_TYPE_STRING)
                         return -ENXIO;
         } else {
+                char *e;
+
                 /* Maybe we can append to the signature? But only if this is the top-level container*/
                 if (c->enclosing != 0)
                         return -ENXIO;
@@ -1462,7 +1339,6 @@ int sd_bus_message_append_string_space(sd_bus_message *m, size_t size, char **s)
                 }
         }
 
-
         a = message_extend_body(m, 4, 4 + size + 1);
         if (!a)
                 return -ENOMEM;
@@ -1478,6 +1354,40 @@ int sd_bus_message_append_string_space(sd_bus_message *m, size_t size, char **s)
         return 0;
 }
 
+_public_ int sd_bus_message_append_string_iovec(
+                sd_bus_message *m,
+                const struct iovec *iov,
+                unsigned n) {
+
+        size_t size;
+        unsigned i;
+        char *p;
+        int r;
+
+        assert_return(m, -EINVAL);
+        assert_return(!m->sealed, -EPERM);
+        assert_return(iov || n == 0, -EINVAL);
+        assert_return(!m->poisoned, -ESTALE);
+
+        size = IOVEC_TOTAL_SIZE(iov, n);
+
+        r = sd_bus_message_append_string_space(m, size, &p);
+        if (r < 0)
+                return r;
+
+        for (i = 0; i < n; i++) {
+
+                if (iov[i].iov_base)
+                        memcpy(p, iov[i].iov_base, iov[i].iov_len);
+                else
+                        memset(p, ' ', iov[i].iov_len);
+
+                p += iov[i].iov_len;
+        }
+
+        return 0;
+}
+
 static int bus_message_open_array(
                 sd_bus_message *m,
                 struct bus_container *c,
@@ -1485,7 +1395,6 @@ static int bus_message_open_array(
                 uint32_t **array_size) {
 
         unsigned nindex;
-        char *e = NULL;
         void *a, *op;
         int alignment;
         size_t os;
@@ -1496,7 +1405,7 @@ static int bus_message_open_array(
         assert(contents);
         assert(array_size);
 
-        if (!signature_is_single(contents))
+        if (!signature_is_single(contents, true))
                 return -EINVAL;
 
         alignment = bus_type_get_alignment(contents[0]);
@@ -1515,6 +1424,8 @@ static int bus_message_open_array(
 
                 nindex = c->index + 1 + strlen(contents);
         } else {
+                char *e;
+
                 if (c->enclosing != 0)
                         return -ENXIO;
 
@@ -1558,7 +1469,6 @@ static int bus_message_open_variant(
                 struct bus_container *c,
                 const char *contents) {
 
-        char *e = NULL;
         size_t l;
         void *a;
 
@@ -1566,7 +1476,7 @@ static int bus_message_open_variant(
         assert(c);
         assert(contents);
 
-        if (!signature_is_single(contents))
+        if (!signature_is_single(contents, false))
                 return -EINVAL;
 
         if (*contents == SD_BUS_TYPE_DICT_ENTRY_BEGIN)
@@ -1578,6 +1488,8 @@ static int bus_message_open_variant(
                         return -ENXIO;
 
         } else {
+                char *e;
+
                 if (c->enclosing != 0)
                         return -ENXIO;
 
@@ -1608,7 +1520,6 @@ static int bus_message_open_struct(
                 const char *contents) {
 
         size_t nindex;
-        char *e = NULL;
 
         assert(m);
         assert(c);
@@ -1629,6 +1540,8 @@ static int bus_message_open_struct(
 
                 nindex = c->index + 1 + l + 1;
         } else {
+                char *e;
+
                 if (c->enclosing != 0)
                         return -ENXIO;
 
@@ -1692,7 +1605,7 @@ static int bus_message_open_dict_entry(
         return 0;
 }
 
-int sd_bus_message_open_container(
+_public_ int sd_bus_message_open_container(
                 sd_bus_message *m,
                 char type,
                 const char *contents) {
@@ -1703,14 +1616,10 @@ int sd_bus_message_open_container(
         size_t before;
         int r;
 
-        if (!m)
-                return -EINVAL;
-        if (m->sealed)
-                return -EPERM;
-        if (!contents)
-                return -EINVAL;
-        if (m->poisoned)
-                return -ESTALE;
+        assert_return(m, -EINVAL);
+        assert_return(!m->sealed, -EPERM);
+        assert_return(contents, -EINVAL);
+        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));
@@ -1762,17 +1671,13 @@ int sd_bus_message_open_container(
         return 0;
 }
 
-int sd_bus_message_close_container(sd_bus_message *m) {
+_public_ int sd_bus_message_close_container(sd_bus_message *m) {
         struct bus_container *c;
 
-        if (!m)
-                return -EINVAL;
-        if (m->sealed)
-                return -EPERM;
-        if (m->n_containers <= 0)
-                return -EINVAL;
-        if (m->poisoned)
-                return -ESTALE;
+        assert_return(m, -EINVAL);
+        assert_return(!m->sealed, -EPERM);
+        assert_return(m->n_containers > 0, -EINVAL);
+        assert_return(!m->poisoned, -ESTALE);
 
         c = message_get_container(m);
         if (c->enclosing != SD_BUS_TYPE_ARRAY)
@@ -2007,96 +1912,357 @@ int bus_message_append_ap(
                         n_struct = k - 2;
                         n_array = (unsigned) -1;
 
-                        break;
-                }
+                        break;
+                }
+
+                default:
+                        r = -EINVAL;
+                }
+
+                if (r < 0)
+                        return r;
+        }
+
+        return 1;
+}
+
+_public_ int sd_bus_message_append(sd_bus_message *m, const char *types, ...) {
+        va_list ap;
+        int r;
+
+        assert_return(m, -EINVAL);
+        assert_return(types, -EINVAL);
+        assert_return(!m->sealed, -EPERM);
+        assert_return(!m->poisoned, -ESTALE);
+
+        va_start(ap, types);
+        r = bus_message_append_ap(m, types, ap);
+        va_end(ap);
+
+        return r;
+}
+
+_public_ int sd_bus_message_append_array_space(sd_bus_message *m,
+                                               char type,
+                                               size_t size,
+                                               void **ptr) {
+        ssize_t align, sz;
+        void *a;
+        int r;
+
+        assert_return(m, -EINVAL);
+        assert_return(!m->sealed, -EPERM);
+        assert_return(bus_type_is_trivial(type), -EINVAL);
+        assert_return(ptr || size == 0, -EINVAL);
+        assert_return(!m->poisoned, -ESTALE);
+
+        align = bus_type_get_alignment(type);
+        sz = bus_type_get_size(type);
+
+        assert_se(align > 0);
+        assert_se(sz > 0);
+
+        if (size % sz != 0)
+                return -EINVAL;
+
+        r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, CHAR_TO_STR(type));
+        if (r < 0)
+                return r;
+
+        a = message_extend_body(m, align, size);
+        if (!a)
+                return -ENOMEM;
+
+        r = sd_bus_message_close_container(m);
+        if (r < 0)
+                return r;
+
+        *ptr = a;
+        return 0;
+}
+
+_public_ int sd_bus_message_append_array(sd_bus_message *m,
+                                         char type,
+                                         const void *ptr,
+                                         size_t size) {
+        int r;
+        void *p;
+
+        assert_return(m, -EINVAL);
+        assert_return(!m->sealed, -EPERM);
+        assert_return(bus_type_is_trivial(type), -EINVAL);
+        assert_return(ptr || size == 0, -EINVAL);
+        assert_return(!m->poisoned, -ESTALE);
+
+        r = sd_bus_message_append_array_space(m, type, size, &p);
+        if (r < 0)
+                return r;
+
+        if (size > 0)
+                memcpy(p, ptr, size);
+
+        return 0;
+}
+
+_public_ int sd_bus_message_append_array_iovec(
+                sd_bus_message *m,
+                char type,
+                const struct iovec *iov,
+                unsigned n) {
+
+        size_t size;
+        unsigned i;
+        void *p;
+        int r;
+
+        assert_return(m, -EINVAL);
+        assert_return(!m->sealed, -EPERM);
+        assert_return(bus_type_is_trivial(type), -EINVAL);
+        assert_return(iov || n == 0, -EINVAL);
+        assert_return(!m->poisoned, -ESTALE);
+
+        size = IOVEC_TOTAL_SIZE(iov, n);
+
+        r = sd_bus_message_append_array_space(m, type, size, &p);
+        if (r < 0)
+                return r;
+
+        for (i = 0; i < n; i++) {
+
+                if (iov[i].iov_base)
+                        memcpy(p, iov[i].iov_base, iov[i].iov_len);
+                else
+                        memset(p, 0, iov[i].iov_len);
+
+                p = (uint8_t*) p + iov[i].iov_len;
+        }
+
+        return 0;
+}
+
+_public_ int sd_bus_message_append_array_memfd(sd_bus_message *m,
+                                               char type,
+                                               sd_memfd *memfd) {
+        _cleanup_close_ int copy_fd = -1;
+        struct bus_body_part *part;
+        ssize_t align, sz;
+        uint64_t size;
+        void *a;
+        int r;
+
+        if (!m)
+                return -EINVAL;
+        if (!memfd)
+                return -EINVAL;
+        if (m->sealed)
+                return -EPERM;
+        if (!bus_type_is_trivial(type))
+                return -EINVAL;
+        if (m->poisoned)
+                return -ESTALE;
+
+        r = sd_memfd_set_sealed(memfd, true);
+        if (r < 0)
+                return r;
+
+        copy_fd = sd_memfd_dup_fd(memfd);
+        if (copy_fd < 0)
+                return copy_fd;
+
+        r = sd_memfd_get_size(memfd, &size);
+        if (r < 0)
+                return r;
+
+        align = bus_type_get_alignment(type);
+        sz = bus_type_get_size(type);
+
+        assert_se(align > 0);
+        assert_se(sz > 0);
+
+        if (size % sz != 0)
+                return -EINVAL;
+
+        if (size > (uint64_t) (uint32_t) -1)
+                return -EINVAL;
+
+        r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, CHAR_TO_STR(type));
+        if (r < 0)
+                return r;
+
+        a = message_extend_body(m, align, 0);
+        if (!a)
+                return -ENOMEM;
+
+        part = message_append_part(m);
+        if (!part)
+                return -ENOMEM;
+
+        part->memfd = copy_fd;
+        part->sealed = true;
+        part->size = size;
+        copy_fd = -1;
+
+        message_extend_containers(m, size);
+        m->header->body_size += size;
+
+        return sd_bus_message_close_container(m);
+}
+
+_public_ int sd_bus_message_append_string_memfd(sd_bus_message *m, sd_memfd *memfd) {
+        _cleanup_close_ int copy_fd = -1;
+        struct bus_body_part *part;
+        struct bus_container *c;
+        uint64_t size;
+        void *a;
+        int r;
+
+        assert_return(m, -EINVAL);
+        assert_return(memfd, -EINVAL);
+        assert_return(!m->sealed, -EPERM);
+        assert_return(!m->poisoned, -ESTALE);
+
+        r = sd_memfd_set_sealed(memfd, true);
+        if (r < 0)
+                return r;
+
+        copy_fd = sd_memfd_dup_fd(memfd);
+        if (copy_fd < 0)
+                return copy_fd;
+
+        r = sd_memfd_get_size(memfd, &size);
+        if (r < 0)
+                return r;
+
+        /* We require this to be NUL terminated */
+        if (size == 0)
+                return -EINVAL;
+
+        if (size > (uint64_t) (uint32_t) -1)
+                return -EINVAL;
+
+        c = message_get_container(m);
+        if (c->signature && c->signature[c->index]) {
+                /* Container signature is already set */
+
+                if (c->signature[c->index] != SD_BUS_TYPE_STRING)
+                        return -ENXIO;
+        } else {
+                char *e;
+
+                /* Maybe we can append to the signature? But only if this is the top-level container*/
+                if (c->enclosing != 0)
+                        return -ENXIO;
+
+                e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRING), NULL);
+                if (!e) {
+                        m->poisoned = true;
+                        return -ENOMEM;
+                }
+        }
+
+        a = message_extend_body(m, 4, 4);
+        if (!a)
+                return -ENOMEM;
+
+        *(uint32_t*) a = size - 1;
+
+        part = message_append_part(m);
+        if (!part)
+                return -ENOMEM;
+
+        part->memfd = copy_fd;
+        part->sealed = true;
+        part->size = size;
+        copy_fd = -1;
+
+        message_extend_containers(m, size);
+        m->header->body_size += size;
+
+        if (c->enclosing != SD_BUS_TYPE_ARRAY)
+                c->index++;
+
+        return 0;
+}
+
+_public_ int sd_bus_message_append_strv(sd_bus_message *m, char **l) {
+        char **i;
+        int r;
 
-                default:
-                        r = -EINVAL;
-                }
+        assert_return(m, -EINVAL);
+        assert_return(!m->sealed, -EPERM);
+        assert_return(!m->poisoned, -ESTALE);
+
+        r = sd_bus_message_open_container(m, 'a', "s");
+        if (r < 0)
+                return r;
 
+        STRV_FOREACH(i, l) {
+                r = sd_bus_message_append_basic(m, 's', *i);
                 if (r < 0)
                         return r;
         }
 
-        return 0;
+        return sd_bus_message_close_container(m);
 }
 
-int sd_bus_message_append(sd_bus_message *m, const char *types, ...) {
-        va_list ap;
-        int r;
+int bus_body_part_map(struct bus_body_part *part) {
+        void *p;
+        size_t psz;
 
-        if (!m)
-                return -EINVAL;
-        if (m->sealed)
-                return -EPERM;
-        if (m->poisoned)
-                return -ESTALE;
-        if (!types)
+        assert_se(part);
+
+        if (part->data)
                 return 0;
 
-        va_start(ap, types);
-        r = bus_message_append_ap(m, types, ap);
-        va_end(ap);
+        if (part->size <= 0)
+                return 0;
 
-        return r;
-}
+        /* For smaller zero parts (as used for padding) we don't need to map anything... */
+        if (part->memfd < 0 && part->is_zero && part->size < 8) {
+                static const uint8_t zeroes[7] = { };
+                part->data = (void*) zeroes;
+                return 0;
+        }
 
-int sd_bus_message_append_array_space(sd_bus_message *m, char type, size_t size, void **ptr) {
-        ssize_t align, sz;
-        void *a;
-        int r;
+        psz = PAGE_ALIGN(part->size);
 
-        if (!m)
-                return -EINVAL;
-        if (m->sealed)
-                return -EPERM;
-        if (!bus_type_is_trivial(type))
-                return -EINVAL;
-        if (!ptr && size > 0)
+        if (part->memfd >= 0)
+                p = mmap(NULL, psz, PROT_READ, MAP_SHARED, part->memfd, 0);
+        else if (part->is_zero)
+                p = mmap(NULL, psz, PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+        else
                 return -EINVAL;
-        if (m->poisoned)
-                return -ESTALE;
-
-        align = bus_type_get_alignment(type);
-        sz = bus_type_get_size(type);
 
-        assert_se(align > 0);
-        assert_se(sz > 0);
+        if (p == MAP_FAILED)
+                return -errno;
 
-        if (size % sz != 0)
-                return -EINVAL;
+        part->mapped = psz;
+        part->data = p;
+        part->munmap_this = true;
 
-        r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, CHAR_TO_STR(type));
-        if (r < 0)
-                return r;
+        return 0;
+}
 
-        a = message_extend_body(m, align, size);
-        if (!a)
-                return -ENOMEM;
+void bus_body_part_unmap(struct bus_body_part *part) {
 
-        r = sd_bus_message_close_container(m);
-        if (r < 0)
-                return r;
+        assert_se(part);
 
-        *ptr = a;
-        return 0;
-}
+        if (part->memfd < 0)
+                return;
 
-int sd_bus_message_append_array(sd_bus_message *m, char type, const void *ptr, size_t size) {
-        int r;
-        void *p;
+        if (!part->data)
+                return;
 
-        if (!ptr && size > 0)
-                return -EINVAL;
+        if (!part->munmap_this)
+                return;
 
-        r = sd_bus_message_append_array_space(m, type, size, &p);
-        if (r < 0)
-                return r;
+        assert_se(munmap(part->data, part->mapped) == 0);
 
-        if (size > 0)
-                memcpy(p, ptr, size);
+        part->data = NULL;
+        part->mapped = 0;
+        part->munmap_this = false;
 
-        return 0;
+        return;
 }
 
 static int buffer_peek(const void *p, uint32_t sz, size_t *rindex, size_t align, size_t nbytes, void **r) {
@@ -2124,6 +2290,15 @@ static int buffer_peek(const void *p, uint32_t sz, size_t *rindex, size_t align,
         return 1;
 }
 
+static bool message_end_of_signature(sd_bus_message *m) {
+        struct bus_container *c;
+
+        assert(m);
+
+        c = message_get_container(m);
+        return !c->signature || c->signature[c->index] == 0;
+}
+
 static bool message_end_of_array(sd_bus_message *m, size_t index) {
         struct bus_container *c;
 
@@ -2136,9 +2311,27 @@ static bool message_end_of_array(sd_bus_message *m, size_t index) {
         return index >= c->begin + BUS_MESSAGE_BSWAP32(m, *c->array_size);
 }
 
+_public_ int sd_bus_message_at_end(sd_bus_message *m, int complete) {
+        assert_return(m, -EINVAL);
+        assert_return(m->sealed, -EPERM);
+
+        if (complete && m->n_containers > 0)
+                return false;
+
+        if (message_end_of_signature(m))
+                return true;
+
+        if (message_end_of_array(m, m->rindex))
+                return true;
+
+        return false;
+}
+
 static struct bus_body_part* find_part(sd_bus_message *m, size_t index, size_t sz, void **p) {
         struct bus_body_part *part;
         size_t begin;
+        int r;
+
         assert(m);
 
         if (m->cached_rindex_part && index >= m->cached_rindex_part_begin) {
@@ -2154,8 +2347,13 @@ static struct bus_body_part* find_part(sd_bus_message *m, size_t index, size_t s
                         return NULL;
 
                 if (index + sz <= begin + part->size) {
+
+                        r = bus_body_part_map(part);
+                        if (r < 0)
+                                return NULL;
+
                         if (p)
-                                *p = part->data ? (uint8_t*) part->data + index - begin : NULL;
+                                *p = (uint8_t*) part->data + index - begin;
 
                         m->cached_rindex_part = part;
                         m->cached_rindex_part_begin = begin;
@@ -2163,6 +2361,7 @@ static struct bus_body_part* find_part(sd_bus_message *m, size_t index, size_t s
                         return part;
                 }
 
+                begin += part->size;
                 part = part->next;
         }
 
@@ -2265,25 +2464,22 @@ static bool validate_object_path(const char *s, size_t l) {
         return true;
 }
 
-int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p) {
+_public_ int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p) {
         struct bus_container *c;
-        int r;
         void *q;
+        int r;
 
-        if (!m)
-                return -EINVAL;
-        if (!m->sealed)
-                return -EPERM;
-        if (!bus_type_is_basic(type))
-                return -EINVAL;
-        if (!p)
-                return -EINVAL;
+        assert_return(m, -EINVAL);
+        assert_return(m->sealed, -EPERM);
+        assert_return(bus_type_is_basic(type), -EINVAL);
 
-        c = message_get_container(m);
+        if (message_end_of_signature(m))
+                return -ENXIO;
 
-        if (!c->signature || c->signature[c->index] == 0)
+        if (message_end_of_array(m, m->rindex))
                 return 0;
 
+        c = message_get_container(m);
         if (c->signature[c->index] != type)
                 return -ENXIO;
 
@@ -2315,7 +2511,9 @@ int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p) {
                 }
 
                 m->rindex = rindex;
-                *(const char**) p = q;
+                if (p)
+                        *(const char**) p = q;
+
                 break;
         }
 
@@ -2339,7 +2537,9 @@ int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p) {
                         return -EBADMSG;
 
                 m->rindex = rindex;
-                *(const char**) p = q;
+
+                if (p)
+                        *(const char**) p = q;
                 break;
         }
 
@@ -2359,27 +2559,32 @@ int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p) {
                 switch (type) {
 
                 case SD_BUS_TYPE_BYTE:
-                        *(uint8_t*) p = *(uint8_t*) q;
+                        if (p)
+                                *(uint8_t*) p = *(uint8_t*) q;
                         break;
 
                 case SD_BUS_TYPE_BOOLEAN:
-                        *(int*) p = !!*(uint32_t*) q;
+                        if (p)
+                                *(int*) p = !!*(uint32_t*) q;
                         break;
 
                 case SD_BUS_TYPE_INT16:
                 case SD_BUS_TYPE_UINT16:
-                        *(uint16_t*) p = BUS_MESSAGE_BSWAP16(m, *(uint16_t*) q);
+                        if (p)
+                                *(uint16_t*) p = BUS_MESSAGE_BSWAP16(m, *(uint16_t*) q);
                         break;
 
                 case SD_BUS_TYPE_INT32:
                 case SD_BUS_TYPE_UINT32:
-                        *(uint32_t*) p = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
+                        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:
-                        *(uint64_t*) p = BUS_MESSAGE_BSWAP64(m, *(uint64_t*) q);
+                        if (p)
+                                *(uint64_t*) p = BUS_MESSAGE_BSWAP64(m, *(uint64_t*) q);
                         break;
 
                 case SD_BUS_TYPE_UNIX_FD: {
@@ -2389,7 +2594,8 @@ int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p) {
                         if (j >= m->n_fds)
                                 return -EBADMSG;
 
-                        *(int*) p = m->fds[j];
+                        if (p)
+                                *(int*) p = m->fds[j];
                         break;
                 }
 
@@ -2424,7 +2630,7 @@ static int bus_message_enter_array(
         assert(contents);
         assert(array_size);
 
-        if (!signature_is_single(contents))
+        if (!signature_is_single(contents, true))
                 return -EINVAL;
 
         alignment = bus_type_get_alignment(contents[0]);
@@ -2478,7 +2684,7 @@ static int bus_message_enter_variant(
         assert(c);
         assert(contents);
 
-        if (!signature_is_single(contents))
+        if (!signature_is_single(contents, false))
                 return -EINVAL;
 
         if (*contents == SD_BUS_TYPE_DICT_ENTRY_BEGIN)
@@ -2589,19 +2795,37 @@ static int bus_message_enter_dict_entry(
         return 1;
 }
 
-int sd_bus_message_enter_container(sd_bus_message *m, char type, const char *contents) {
+_public_ int sd_bus_message_enter_container(sd_bus_message *m,
+                                            char type,
+                                            const char *contents) {
         struct bus_container *c, *w;
         uint32_t *array_size = NULL;
         char *signature;
         size_t before;
         int r;
 
-        if (!m)
-                return -EINVAL;
-        if (!m->sealed)
-                return -EPERM;
-        if (!contents)
-                return -EINVAL;
+        assert_return(m, -EINVAL);
+        assert_return(m->sealed, -EPERM);
+        assert_return(type != 0 || !contents, -EINVAL);
+
+        if (type == 0 || !contents) {
+                const char *cc;
+                char tt;
+
+                /* Allow entering into anonymous containers */
+                r = sd_bus_message_peek_type(m, &tt, &cc);
+                if (r <= 0)
+                        return r;
+
+                if (type != 0 && type != tt)
+                        return -ENXIO;
+
+                if (contents && !streq(contents, cc))
+                        return -ENXIO;
+
+                type = tt;
+                contents = cc;
+        }
 
         /*
          * We enforce a global limit on container depth, that is much
@@ -2627,11 +2851,14 @@ int sd_bus_message_enter_container(sd_bus_message *m, char type, const char *con
                 return -ENOMEM;
         m->containers = w;
 
-        c = message_get_container(m);
+        if (message_end_of_signature(m))
+                return -ENXIO;
 
-        if (!c->signature || c->signature[c->index] == 0)
+        if (message_end_of_array(m, m->rindex))
                 return 0;
 
+        c = message_get_container(m);
+
         signature = strdup(contents);
         if (!signature)
                 return -ENOMEM;
@@ -2667,15 +2894,12 @@ int sd_bus_message_enter_container(sd_bus_message *m, char type, const char *con
         return 1;
 }
 
-int sd_bus_message_exit_container(sd_bus_message *m) {
+_public_ int sd_bus_message_exit_container(sd_bus_message *m) {
         struct bus_container *c;
 
-        if (!m)
-                return -EINVAL;
-        if (!m->sealed)
-                return -EPERM;
-        if (m->n_containers <= 0)
-                return -EINVAL;
+        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) {
@@ -2687,7 +2911,7 @@ int sd_bus_message_exit_container(sd_bus_message *m) {
 
         } else {
                 if (c->signature && c->signature[c->index] != 0)
-                        return -EINVAL;
+                        return -EBUSY;
         }
 
         free(c->signature);
@@ -2718,23 +2942,21 @@ static void message_quit_container(sd_bus_message *m) {
         c->index = c->saved_index;
 }
 
-int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char **contents) {
+_public_ int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char **contents) {
         struct bus_container *c;
         int r;
 
-        if (!m)
-                return -EINVAL;
-        if (!m->sealed)
-                return -EPERM;
-
-        c = message_get_container(m);
+        assert_return(m, -EINVAL);
+        assert_return(m->sealed, -EPERM);
 
-        if (!c->signature || c->signature[c->index] == 0)
+        if (message_end_of_signature(m))
                 goto eof;
 
         if (message_end_of_array(m, m->rindex))
                 goto eof;
 
+        c = message_get_container(m);
+
         if (bus_type_is_basic(c->signature[c->index])) {
                 if (contents)
                         *contents = NULL;
@@ -2834,19 +3056,17 @@ int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char **content
 
 eof:
         if (type)
-                *type = c->enclosing;
+                *type = 0;
         if (contents)
                 *contents = NULL;
         return 0;
 }
 
-int sd_bus_message_rewind(sd_bus_message *m, int complete) {
+_public_ int sd_bus_message_rewind(sd_bus_message *m, int complete) {
         struct bus_container *c;
 
-        if (!m)
-                return -EINVAL;
-        if (!m->sealed)
-                return -EPERM;
+        assert_return(m, -EINVAL);
+        assert_return(m->sealed, -EPERM);
 
         if (complete) {
                 message_reset_containers(m);
@@ -2863,6 +3083,7 @@ int sd_bus_message_rewind(sd_bus_message *m, int complete) {
 
         return !isempty(c->signature);
 }
+
 static int message_read_ap(
                 sd_bus_message *m,
                 const char *types,
@@ -2871,11 +3092,12 @@ static int message_read_ap(
         unsigned n_array, n_struct;
         TypeStack stack[BUS_CONTAINER_DEPTH];
         unsigned stack_ptr = 0;
+        unsigned n_loop = 0;
         int r;
 
         assert(m);
 
-        if (!types)
+        if (isempty(types))
                 return 0;
 
         /* Ideally, we'd just call ourselves recursively on every
@@ -2885,12 +3107,14 @@ static int message_read_ap(
          * in a single stackframe. We hence implement our own
          * home-grown stack in an array. */
 
-        n_array = (unsigned) -1;
-        n_struct = strlen(types);
+        n_array = (unsigned) -1; /* lenght of current array entries */
+        n_struct = strlen(types); /* length of current struct contents signature */
 
         for (;;) {
                 const char *t;
 
+                n_loop++;
+
                 if (n_array == 0 || (n_array == (unsigned) -1 && n_struct == 0)) {
                         r = type_stack_pop(stack, ELEMENTSOF(stack), &stack_ptr, &types, &n_struct, &n_array);
                         if (r < 0)
@@ -2934,8 +3158,12 @@ static int message_read_ap(
                         r = sd_bus_message_read_basic(m, *t, p);
                         if (r < 0)
                                 return r;
-                        if (r == 0)
+                        if (r == 0) {
+                                if (n_loop <= 1)
+                                        return 0;
+
                                 return -ENXIO;
+                        }
 
                         break;
                 }
@@ -2955,8 +3183,12 @@ static int message_read_ap(
                                 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, s);
                                 if (r < 0)
                                         return r;
-                                if (r == 0)
+                                if (r == 0) {
+                                        if (n_loop <= 1)
+                                                return 0;
+
                                         return -ENXIO;
+                                }
                         }
 
                         if (n_array == (unsigned) -1) {
@@ -2985,8 +3217,12 @@ static int message_read_ap(
                         r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, s);
                         if (r < 0)
                                 return r;
-                        if (r == 0)
+                        if (r == 0) {
+                                if (n_loop <= 1)
+                                        return 0;
+
                                 return -ENXIO;
+                        }
 
                         r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
                         if (r < 0)
@@ -3015,8 +3251,11 @@ static int message_read_ap(
                                 r = sd_bus_message_enter_container(m, *t == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s);
                                 if (r < 0)
                                         return r;
-                                if (r == 0)
+                                if (r == 0) {
+                                        if (n_loop <= 1)
+                                                return 0;
                                         return -ENXIO;
+                                }
                         }
 
                         if (n_array == (unsigned) -1) {
@@ -3035,69 +3274,211 @@ static int message_read_ap(
                         break;
                 }
 
-                default:
-                        return -EINVAL;
+                default:
+                        return -EINVAL;
+                }
+        }
+
+        return 1;
+}
+
+_public_ int sd_bus_message_read(sd_bus_message *m, const char *types, ...) {
+        va_list ap;
+        int r;
+
+        assert_return(m, -EINVAL);
+        assert_return(m->sealed, -EPERM);
+        assert_return(types, -EINVAL);
+
+        va_start(ap, types);
+        r = message_read_ap(m, types, ap);
+        va_end(ap);
+
+        return r;
+}
+
+_public_ int sd_bus_message_skip(sd_bus_message *m, const char *types) {
+        int r;
+
+        assert_return(m, -EINVAL);
+        assert_return(m->sealed, -EPERM);
+        assert_return(types, -EINVAL);
+
+        if (isempty(types))
+                return 0;
+
+        switch (*types) {
+
+        case SD_BUS_TYPE_BYTE:
+        case SD_BUS_TYPE_BOOLEAN:
+        case SD_BUS_TYPE_INT16:
+        case SD_BUS_TYPE_UINT16:
+        case SD_BUS_TYPE_INT32:
+        case SD_BUS_TYPE_UINT32:
+        case SD_BUS_TYPE_INT64:
+        case SD_BUS_TYPE_UINT64:
+        case SD_BUS_TYPE_DOUBLE:
+        case SD_BUS_TYPE_STRING:
+        case SD_BUS_TYPE_OBJECT_PATH:
+        case SD_BUS_TYPE_SIGNATURE:
+        case SD_BUS_TYPE_UNIX_FD:
+
+                r = sd_bus_message_read_basic(m, *types, NULL);
+                if (r <= 0)
+                        return r;
+
+                r = sd_bus_message_skip(m, types + 1);
+                if (r < 0)
+                        return r;
+
+                return 1;
+
+        case SD_BUS_TYPE_ARRAY: {
+                size_t k;
+
+                r = signature_element_length(types + 1, &k);
+                if (r < 0)
+                        return r;
+
+                {
+                        char s[k+1];
+                        memcpy(s, types+1, k);
+                        s[k] = 0;
+
+                        r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, s);
+                        if (r <= 0)
+                                return r;
+
+                        for (;;) {
+                                r = sd_bus_message_skip(m, s);
+                                if (r < 0)
+                                        return r;
+                                if (r == 0)
+                                        break;
+                        }
+
+                        r = sd_bus_message_exit_container(m);
+                        if (r < 0)
+                                return r;
+                }
+
+                r = sd_bus_message_skip(m, types + 1 + k);
+                if (r < 0)
+                        return r;
+
+                return 1;
+        }
+
+        case SD_BUS_TYPE_VARIANT: {
+                const char *contents;
+                char x;
+
+                r = sd_bus_message_peek_type(m, &x, &contents);
+                if (r <= 0)
+                        return r;
+
+                if (x != SD_BUS_TYPE_VARIANT)
+                        return -ENXIO;
+
+                r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents);
+                if (r <= 0)
+                        return r;
+
+                r = sd_bus_message_skip(m, contents);
+                if (r < 0)
+                        return r;
+                assert(r != 0);
+
+                r = sd_bus_message_exit_container(m);
+                if (r < 0)
+                        return r;
+
+                r = sd_bus_message_skip(m, types + 1);
+                if (r < 0)
+                        return r;
+
+                return 1;
+        }
+
+        case SD_BUS_TYPE_STRUCT_BEGIN:
+        case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
+                size_t k;
+
+                r = signature_element_length(types, &k);
+                if (r < 0)
+                        return r;
+
+                {
+                        char s[k-1];
+                        memcpy(s, types+1, k-2);
+                        s[k-2] = 0;
+
+                        r = sd_bus_message_enter_container(m, *types == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s);
+                        if (r <= 0)
+                                return r;
+
+                        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)
+                                return r;
                 }
-        }
 
-        return 1;
-}
+                r = sd_bus_message_skip(m, types + k);
+                if (r < 0)
+                        return r;
 
-int sd_bus_message_read(sd_bus_message *m, const char *types, ...) {
-        va_list ap;
-        int r;
+                return 1;
+        }
 
-        if (!m)
-                return -EINVAL;
-        if (!m->sealed)
-                return -EPERM;
-        if (!types)
+        default:
                 return -EINVAL;
-
-        va_start(ap, types);
-        r = message_read_ap(m, types, ap);
-        va_end(ap);
-
-        return r;
+        }
 }
 
-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;
         ssize_t align;
         int r;
 
-        if (!m)
-                return -EINVAL;
-        if (!m->sealed)
-                return -EPERM;
-        if (!bus_type_is_trivial(type))
-                return -EINVAL;
-        if (!ptr)
-                return -EINVAL;
-        if (!size)
-                return -EINVAL;
-        if (BUS_MESSAGE_NEED_BSWAP(m))
-                return -ENOTSUP;
+        assert_return(m, -EINVAL);
+        assert_return(m->sealed, -EPERM);
+        assert_return(bus_type_is_trivial(type), -EINVAL);
+        assert_return(ptr, -EINVAL);
+        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)
+        if (r <= 0)
                 return r;
 
         c = message_get_container(m);
         sz = BUS_MESSAGE_BSWAP32(m, *c->array_size);
 
-        r = message_peek_body(m, &m->rindex, align, sz, &p);
-        if (r < 0)
-                goto fail;
-        if (r == 0) {
-                r = -EBADMSG;
-                goto fail;
+        if (sz == 0)
+                /* Zero length array, let's return some aligned
+                 * pointer that is not NULL */
+                p = (uint8_t*) NULL + align;
+        else {
+                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);
@@ -3515,25 +3896,25 @@ int bus_message_parse_fields(sd_bus_message *m) {
 
         switch (m->header->type) {
 
-        case SD_BUS_MESSAGE_TYPE_SIGNAL:
+        case SD_BUS_MESSAGE_SIGNAL:
                 if (!m->path || !m->interface || !m->member)
                         return -EBADMSG;
                 break;
 
-        case SD_BUS_MESSAGE_TYPE_METHOD_CALL:
+        case SD_BUS_MESSAGE_METHOD_CALL:
 
                 if (!m->path || !m->member)
                         return -EBADMSG;
 
                 break;
 
-        case SD_BUS_MESSAGE_TYPE_METHOD_RETURN:
+        case SD_BUS_MESSAGE_METHOD_RETURN:
 
                 if (m->reply_serial == 0)
                         return -EBADMSG;
                 break;
 
-        case SD_BUS_MESSAGE_TYPE_METHOD_ERROR:
+        case SD_BUS_MESSAGE_METHOD_ERROR:
 
                 if (m->reply_serial == 0 || !m->error.name)
                         return -EBADMSG;
@@ -3541,7 +3922,7 @@ int bus_message_parse_fields(sd_bus_message *m) {
         }
 
         /* Try to read the error message, but if we can't it's a non-issue */
-        if (m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_ERROR)
+        if (m->header->type == SD_BUS_MESSAGE_METHOD_ERROR)
                 sd_bus_message_read(m, "s", &m->error.message);
 
         return 0;
@@ -3577,19 +3958,27 @@ int bus_message_seal(sd_bus_message *m, uint64_t serial) {
                         return r;
         }
 
-        /* Add padding at the end, since we know the body
-         * needs to start at an 8 byte alignment. */
-
+        /* 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);
 
-        MESSAGE_FOREACH_PART(part, i, m)
-                if (part->memfd >= 0 && !part->sealed) {
-                        ioctl(part->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 1);
-                        part->sealed = true;
-                }
+        /* 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;
@@ -3597,277 +3986,15 @@ int bus_message_seal(sd_bus_message *m, uint64_t serial) {
         return 0;
 }
 
-int sd_bus_message_set_destination(sd_bus_message *m, const char *destination) {
-        if (!m)
-                return -EINVAL;
-        if (!destination)
-                return -EINVAL;
-        if (m->sealed)
-                return -EPERM;
-        if (m->destination)
-                return -EEXIST;
+_public_ int sd_bus_message_set_destination(sd_bus_message *m, const char *destination) {
+        assert_return(m, -EINVAL);
+        assert_return(destination, -EINVAL);
+        assert_return(!m->sealed, -EPERM);
+        assert_return(!m->destination, -EEXIST);
 
         return message_append_field_string(m, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &m->destination);
 }
 
-int bus_message_dump(sd_bus_message *m) {
-        const char *u = NULL, *uu = NULL, *s = NULL;
-        char **cmdline = NULL;
-        unsigned level = 1;
-        int r;
-        uid_t owner, audit_loginuid;
-        uint32_t audit_sessionid;
-
-        assert(m);
-
-        printf("Message %p\n"
-               "\tn_ref=%u\n"
-               "\tendian=%c\n"
-               "\ttype=%i\n"
-               "\tflags=%u\n"
-               "\tversion=%u\n"
-               "\tserial=%u\n"
-               "\tfields_size=%u\n"
-               "\tbody_size=%u\n"
-               "\tpath=%s\n"
-               "\tinterface=%s\n"
-               "\tmember=%s\n"
-               "\tdestination=%s\n"
-               "\tsender=%s\n"
-               "\tsignature=%s\n"
-               "\treply_serial=%u\n"
-               "\terror.name=%s\n"
-               "\terror.message=%s\n"
-               "\tsealed=%s\n",
-               m,
-               m->n_ref,
-               m->header->endian,
-               m->header->type,
-               m->header->flags,
-               m->header->version,
-               BUS_MESSAGE_SERIAL(m),
-               BUS_MESSAGE_FIELDS_SIZE(m),
-               BUS_MESSAGE_BODY_SIZE(m),
-               strna(m->path),
-               strna(m->interface),
-               strna(m->member),
-               strna(m->destination),
-               strna(m->sender),
-               strna(m->root_container.signature),
-               m->reply_serial,
-               strna(m->error.name),
-               strna(m->error.message),
-               yes_no(m->sealed));
-
-        if (m->pid != 0)
-                printf("\tpid=%lu\n", (unsigned long) m->pid);
-        if (m->tid != 0)
-                printf("\ttid=%lu\n", (unsigned long) m->tid);
-        if (m->uid_valid)
-                printf("\tuid=%lu\n", (unsigned long) m->uid);
-        if (m->gid_valid)
-                printf("\tgid=%lu\n", (unsigned long) m->gid);
-        if (m->pid_starttime != 0)
-                printf("\tpid_starttime=%llu\n", (unsigned long long) m->pid_starttime);
-        if (m->monotonic != 0)
-                printf("\tmonotonic=%llu\n", (unsigned long long) m->monotonic);
-        if (m->realtime != 0)
-                printf("\trealtime=%llu\n", (unsigned long long) m->realtime);
-        if (m->exe)
-                printf("\texe=[%s]\n", m->exe);
-        if (m->comm)
-                printf("\tcomm=[%s]\n", m->comm);
-        if (m->tid_comm)
-                printf("\ttid_comm=[%s]\n", m->tid_comm);
-        if (m->label)
-                printf("\tlabel=[%s]\n", m->label);
-        if (m->cgroup)
-                printf("\tcgroup=[%s]\n", m->cgroup);
-
-        sd_bus_message_get_unit(m, &u);
-        if (u)
-                printf("\tunit=[%s]\n", u);
-        sd_bus_message_get_user_unit(m, &uu);
-        if (uu)
-                printf("\tuser_unit=[%s]\n", uu);
-        sd_bus_message_get_session(m, &s);
-        if (s)
-                printf("\tsession=[%s]\n", s);
-        if (sd_bus_message_get_owner_uid(m, &owner) >= 0)
-                printf("\towner_uid=%lu\n", (unsigned long) owner);
-        if (sd_bus_message_get_audit_loginuid(m, &audit_loginuid) >= 0)
-                printf("\taudit_loginuid=%lu\n", (unsigned long) audit_loginuid);
-        if (sd_bus_message_get_audit_sessionid(m, &audit_sessionid) >= 0)
-                printf("\taudit_sessionid=%lu\n", (unsigned long) audit_sessionid);
-
-        printf("\tCAP_KILL=%i\n", sd_bus_message_has_effective_cap(m, 5));
-
-        if (sd_bus_message_get_cmdline(m, &cmdline) >= 0) {
-                char **c;
-
-                fputs("\tcmdline=[", stdout);
-                STRV_FOREACH(c, cmdline) {
-                        if (c != cmdline)
-                                putchar(' ');
-
-                        fputs(*c, stdout);
-                }
-
-                fputs("]\n", stdout);
-        }
-
-        r = sd_bus_message_rewind(m, true);
-        if (r < 0) {
-                log_error("Failed to rewind: %s", strerror(-r));
-                return r;
-        }
-
-        printf("BEGIN_MESSAGE \"%s\" {\n", strempty(m->root_container.signature));
-
-        for(;;) {
-                _cleanup_free_ char *prefix = NULL;
-                const char *contents = NULL;
-                char type;
-                union {
-                        uint8_t u8;
-                        uint16_t u16;
-                        int16_t s16;
-                        uint32_t u32;
-                        int32_t s32;
-                        uint64_t u64;
-                        int64_t s64;
-                        double d64;
-                        const char *string;
-                        int i;
-                } basic;
-
-                r = sd_bus_message_peek_type(m, &type, &contents);
-                if (r < 0) {
-                        log_error("Failed to peek type: %s", strerror(-r));
-                        return r;
-                }
-                if (r == 0) {
-                        if (level <= 1)
-                                break;
-
-                        r = sd_bus_message_exit_container(m);
-                        if (r < 0) {
-                                log_error("Failed to exit container: %s", strerror(-r));
-                                return r;
-                        }
-
-                        level--;
-
-                        prefix = strrep("\t", level);
-                        if (!prefix)
-                                return log_oom();
-
-                        if (type == SD_BUS_TYPE_ARRAY)
-                                printf("%s} END_ARRAY \n", prefix);
-                        else if (type == SD_BUS_TYPE_VARIANT)
-                                printf("%s} END_VARIANT\n", prefix);
-                        else if (type == SD_BUS_TYPE_STRUCT)
-                                printf("%s} END_STRUCT\n", prefix);
-                        else if (type == SD_BUS_TYPE_DICT_ENTRY)
-                                printf("%s} END_DICT_ENTRY\n", prefix);
-
-                        continue;
-                }
-
-                prefix = strrep("\t", level);
-                if (!prefix)
-                        return log_oom();
-
-                if (bus_type_is_container(type) > 0) {
-                        r = sd_bus_message_enter_container(m, type, contents);
-                        if (r < 0) {
-                                log_error("Failed to enter container: %s", strerror(-r));
-                                return r;
-                        }
-
-                        if (type == SD_BUS_TYPE_ARRAY)
-                                printf("%sBEGIN_ARRAY \"%s\" {\n", prefix, contents);
-                        else if (type == SD_BUS_TYPE_VARIANT)
-                                printf("%sBEGIN_VARIANT \"%s\" {\n", prefix, contents);
-                        else if (type == SD_BUS_TYPE_STRUCT)
-                                printf("%sBEGIN_STRUCT \"%s\" {\n", prefix, contents);
-                        else if (type == SD_BUS_TYPE_DICT_ENTRY)
-                                printf("%sBEGIN_DICT_ENTRY \"%s\" {\n", prefix, contents);
-
-                        level ++;
-
-                        continue;
-                }
-
-                r = sd_bus_message_read_basic(m, type, &basic);
-                if (r < 0) {
-                        log_error("Failed to get basic: %s", strerror(-r));
-                        return r;
-                }
-
-                switch (type) {
-
-                case SD_BUS_TYPE_BYTE:
-                        printf("%sBYTE: %u\n", prefix, basic.u8);
-                        break;
-
-                case SD_BUS_TYPE_BOOLEAN:
-                        printf("%sBOOLEAN: %s\n", prefix, yes_no(basic.i));
-                        break;
-
-                case SD_BUS_TYPE_INT16:
-                        printf("%sINT16: %i\n", prefix, basic.s16);
-                        break;
-
-                case SD_BUS_TYPE_UINT16:
-                        printf("%sUINT16: %u\n", prefix, basic.u16);
-                        break;
-
-                case SD_BUS_TYPE_INT32:
-                        printf("%sINT32: %i\n", prefix, basic.s32);
-                        break;
-
-                case SD_BUS_TYPE_UINT32:
-                        printf("%sUINT32: %u\n", prefix, basic.u32);
-                        break;
-
-                case SD_BUS_TYPE_INT64:
-                        printf("%sINT64: %lli\n", prefix, (long long) basic.s64);
-                        break;
-
-                case SD_BUS_TYPE_UINT64:
-                        printf("%sUINT64: %llu\n", prefix, (unsigned long long) basic.u64);
-                        break;
-
-                case SD_BUS_TYPE_DOUBLE:
-                        printf("%sDOUBLE: %g\n", prefix, basic.d64);
-                        break;
-
-                case SD_BUS_TYPE_STRING:
-                        printf("%sSTRING: \"%s\"\n", prefix, basic.string);
-                        break;
-
-                case SD_BUS_TYPE_OBJECT_PATH:
-                        printf("%sOBJECT_PATH: \"%s\"\n", prefix, basic.string);
-                        break;
-
-                case SD_BUS_TYPE_SIGNATURE:
-                        printf("%sSIGNATURE: \"%s\"\n", prefix, basic.string);
-                        break;
-
-                case SD_BUS_TYPE_UNIX_FD:
-                        printf("%sUNIX_FD: %i\n", prefix, basic.i);
-                        break;
-
-                default:
-                        assert_not_reached("Unknown basic type.");
-                }
-        }
-
-        printf("} END_MESSAGE\n");
-        return 0;
-}
-
 int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz) {
         size_t total;
         void *p, *e;
@@ -3903,7 +4030,7 @@ int bus_message_read_strv_extend(sd_bus_message *m, char ***l) {
         assert(l);
 
         r = sd_bus_message_enter_container(m, 'a', "s");
-        if (r < 0)
+        if (r <= 0)
                 return r;
 
         for (;;) {
@@ -3924,7 +4051,25 @@ int bus_message_read_strv_extend(sd_bus_message *m, char ***l) {
         if (r < 0)
                 return r;
 
-        return 0;
+        return 1;
+}
+
+_public_ int sd_bus_message_read_strv(sd_bus_message *m, char ***l) {
+        char **strv = NULL;
+        int r;
+
+        assert_return(m, -EINVAL);
+        assert_return(m->sealed, -EPERM);
+        assert_return(l, -EINVAL);
+
+        r = bus_message_read_strv_extend(m, &strv);
+        if (r <= 0) {
+                strv_free(strv);
+                return r;
+        }
+
+        *l = strv;
+        return 1;
 }
 
 const char* bus_message_get_arg(sd_bus_message *m, unsigned i) {
@@ -3991,3 +4136,131 @@ int bus_header_message_size(struct bus_header *h, size_t *sum) {
         *sum = sizeof(struct bus_header) + ALIGN8(fs) + bs;
         return 0;
 }
+
+_public_ int sd_bus_message_get_errno(sd_bus_message *m) {
+        assert_return(m, -EINVAL);
+
+        if (m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
+                return 0;
+
+        return sd_bus_error_get_errno(&m->error);
+}
+
+_public_ const char* sd_bus_message_get_signature(sd_bus_message *m, int complete) {
+        struct bus_container *c;
+
+        assert_return(m, NULL);
+
+        c = complete ? &m->root_container : message_get_container(m);
+        return c->signature ?: "";
+}
+
+_public_ int sd_bus_message_copy(sd_bus_message *m, sd_bus_message *source, int all) {
+        bool done_something = false;
+        int r;
+
+        assert_return(m, -EINVAL);
+        assert_return(source, -EINVAL);
+        assert_return(!m->sealed, -EPERM);
+        assert_return(source->sealed, -EPERM);
+
+        do {
+                const char *contents;
+                char type;
+                union {
+                        uint8_t u8;
+                        uint16_t u16;
+                        int16_t s16;
+                        uint32_t u32;
+                        int32_t s32;
+                        uint64_t u64;
+                        int64_t s64;
+                        double d64;
+                        const char *string;
+                        int i;
+                } basic;
+
+                r = sd_bus_message_peek_type(source, &type, &contents);
+                if (r < 0)
+                        return r;
+                if (r == 0)
+                        break;
+
+                done_something = true;
+
+                if (bus_type_is_container(type) > 0) {
+
+                        r = sd_bus_message_enter_container(source, type, contents);
+                        if (r < 0)
+                                return r;
+
+                        r = sd_bus_message_open_container(m, type, contents);
+                        if (r < 0)
+                                return r;
+
+                        r = sd_bus_message_copy(m, source, true);
+                        if (r < 0)
+                                return r;
+
+                        r = sd_bus_message_close_container(m);
+                        if (r < 0)
+                                return r;
+
+                        r = sd_bus_message_exit_container(source);
+                        if (r < 0)
+                                return r;
+
+                        continue;
+                }
+
+                r = sd_bus_message_read_basic(source, type, &basic);
+                if (r < 0)
+                        return r;
+
+                assert(r > 0);
+
+                if (type == SD_BUS_TYPE_OBJECT_PATH ||
+                    type == SD_BUS_TYPE_SIGNATURE ||
+                    type == SD_BUS_TYPE_STRING)
+                        r = sd_bus_message_append_basic(m, type, basic.string);
+                else
+                        r = sd_bus_message_append_basic(m, type, &basic);
+
+                if (r < 0)
+                        return r;
+
+        } while (all);
+
+        return done_something;
+}
+
+_public_ int sd_bus_message_verify_type(sd_bus_message *m, char type, const char *contents) {
+        const char *c;
+        char t;
+        int r;
+
+        assert_return(m, -EINVAL);
+        assert_return(m->sealed, -EPERM);
+        assert_return(!type || bus_type_is_valid(type), -EINVAL);
+        assert_return(!contents || signature_is_valid(contents, true), -EINVAL);
+        assert_return(type || contents, -EINVAL);
+        assert_return(!contents || !type || bus_type_is_container(type), -EINVAL);
+
+        r = sd_bus_message_peek_type(m, &t, &c);
+        if (r <= 0)
+                return r;
+
+        if (type != 0 && type != t)
+                return 0;
+
+        if (contents && !streq_ptr(contents, c))
+                return 0;
+
+        return 1;
+}
+
+_public_ sd_bus *sd_bus_message_get_bus(sd_bus_message *m) {
+        assert_return(m, NULL);
+
+        return m->bus;
+}