bus-message.c revision 102d8f8169427cb68cdebf5ee0f0e07788e9c2b2
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/***
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek This file is part of systemd.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Copyright 2013 Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (at your option) any later version.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek***/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include <errno.h>
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include <fcntl.h>
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include <sys/mman.h>
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "util.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "utf8.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "strv.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "time-util.h"
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include "cgroup-util.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "sd-bus.h"
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering#include "bus-message.h"
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering#include "bus-internal.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "bus-type.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "bus-signature.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored);
0dec689b098cf62b948d43dc78bde859665056ecTom Gundersen
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poetteringstatic void *adjust_pointer(const void *p, void *old_base, size_t sz, void *new_base) {
958b66ea16deddd794b3a52643bd44633e165eadLennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (p == NULL)
958b66ea16deddd794b3a52643bd44633e165eadLennart Poettering return NULL;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (old_base == new_base)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return (void*) p;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if ((uint8_t*) p < (uint8_t*) old_base)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return (void*) p;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if ((uint8_t*) p >= (uint8_t*) old_base + sz)
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering return (void*) p;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return (uint8_t*) new_base + ((uint8_t*) p - (uint8_t*) old_base);
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering}
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poetteringstatic void message_free_part(sd_bus_message *m, struct bus_body_part *part) {
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering assert(m);
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering assert(part);
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering if (part->memfd >= 0) {
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering /* If we can reuse the memfd, try that. For that it
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering * can't be sealed yet. */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!part->sealed)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek bus_kernel_push_memfd(m->bus, part->memfd, part->data, part->mapped);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk else {
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poettering if (part->mapped > 0)
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poettering assert_se(munmap(part->data, part->mapped) == 0);
e150e82097211f09b911c7784a89ef9efed713caMichał Bartoszkiewicz
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek close_nointr_nofail(part->memfd);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering } else if (part->munmap_this)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering munmap(part->data, part->mapped);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering else if (part->free_this)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering free(part->data);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (part != &m->body)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering free(part);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic void message_reset_parts(sd_bus_message *m) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering struct bus_body_part *part;
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(m);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering part = &m->body;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek while (m->n_body_parts > 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct bus_body_part *next = part->next;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering message_free_part(m, part);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering part = next;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering m->n_body_parts--;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering m->body_end = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering m->cached_rindex_part = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->cached_rindex_part_begin = 0;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic void message_reset_containers(sd_bus_message *m) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering unsigned i;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek for (i = 0; i < m->n_containers; i++)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering free(m->containers[i].signature);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek free(m->containers);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering m->containers = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->n_containers = 0;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering m->root_container.index = 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void message_free(sd_bus_message *m) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (m->free_header)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek free(m->header);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering message_reset_parts(m);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (m->free_kdbus)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering free(m->kdbus);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (m->release_kdbus) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek uint64_t off;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek off = (uint8_t *)m->kdbus - (uint8_t *)m->bus->kdbus_buffer;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ioctl(m->bus->input_fd, KDBUS_CMD_MSG_RELEASE, &off);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (m->bus)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering sd_bus_unref(m->bus);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (m->free_fds) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering close_many(m->fds, m->n_fds);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek free(m->fds);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek if (m->iovec != m->iovec_fixed)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering free(m->iovec);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek free(m->cmdline_array);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering message_reset_containers(m);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering free(m->root_container.signature);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek free(m->peeked_signature);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering free(m->unit);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek free(m->user_unit);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek free(m->session);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering free(m);
da2e288bbc4d8cebaa1d38a80f6eec8cde3e9cceLennart Poettering}
da2e288bbc4d8cebaa1d38a80f6eec8cde3e9cceLennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic void *message_extend_fields(sd_bus_message *m, size_t align, size_t sz) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering void *op, *np;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek size_t old_size, new_size, start;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(m);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (m->poisoned)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return NULL;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering old_size = sizeof(struct bus_header) + m->header->fields_size;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering start = ALIGN_TO(old_size, align);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering new_size = start + sz;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (old_size == new_size)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return (uint8_t*) m->header + old_size;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (new_size > (size_t) ((uint32_t) -1))
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering goto poison;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (m->free_header) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering np = realloc(m->header, ALIGN8(new_size));
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (!np)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering goto poison;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering } else {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering /* Initially, the header is allocated as part of of
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * the sd_bus_message itself, let's replace it by
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * dynamic data */
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering np = malloc(ALIGN8(new_size));
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (!np)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering goto poison;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek memcpy(np, m->header, sizeof(struct bus_header));
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek }
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Zero out padding */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (start > old_size)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek memset((uint8_t*) np + old_size, 0, start - old_size);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek op = m->header;
0fb398316c6705dfc168733361650fdb6824896dLennart Poettering m->header = np;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->header->fields_size = new_size - sizeof(struct bus_header);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Adjust quick access pointers */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->path = adjust_pointer(m->path, op, old_size, m->header);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->interface = adjust_pointer(m->interface, op, old_size, m->header);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->member = adjust_pointer(m->member, op, old_size, m->header);
4608af4333d0f7f5f8e3bc632801b04ef07d246dLennart Poettering m->destination = adjust_pointer(m->destination, op, old_size, m->header);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->sender = adjust_pointer(m->sender, op, old_size, m->header);
65089b82401cd395786a7987c470056ff3f01151Lennart Poettering m->error.name = adjust_pointer(m->error.name, op, old_size, m->header);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->free_header = true;
34c10968cbe3b5591b3c0ce225b8694edd9709d0Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return (uint8_t*) np + start;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekpoison:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->poisoned = true;
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int message_append_field_string(
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_bus_message *m,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek uint8_t h,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek char type,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *s,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char **ret) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt size_t l;
0fb398316c6705dfc168733361650fdb6824896dLennart Poettering uint8_t *p;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) l = strlen(s);
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) if (l > (size_t) (uint32_t) -1)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EINVAL;
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig)
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) /* field id byte + signature length + signature 's' + NUL + string length + string + NUL */
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt p = message_extend_fields(m, 8, 4 + 4 + l + 1);
0fb398316c6705dfc168733361650fdb6824896dLennart Poettering if (!p)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -ENOMEM;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek p[0] = h;
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt p[1] = 1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek p[2] = type;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek p[3] = 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ((uint32_t*) p)[1] = l;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek memcpy(p + 8, s, l + 1);
ed375bebf46c1251f4baa170b39ee93761dbdb19Zbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (ret)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *ret = (char*) p + 8;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int message_append_field_signature(
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_bus_message *m,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek uint8_t h,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *s,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char **ret) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek size_t l;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek uint8_t *p;
f7dc3ab9f43b67abcbd34062b9352ab42debec49Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek l = strlen(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (l > 255)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EINVAL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt /* field id byte + signature length + signature 'g' + NUL + string length + string + NUL */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek p = message_extend_fields(m, 8, 4 + 1 + l + 1);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!p)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -ENOMEM;
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek p[0] = h;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek p[1] = 1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek p[2] = SD_BUS_TYPE_SIGNATURE;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt p[3] = 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek p[4] = l;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt memcpy(p + 5, s, l + 1);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (ret)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *ret = (const char*) p + 5;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
cbd671772c9ce053a7050ddd29de170eb9efac7eZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int message_append_field_uint32(sd_bus_message *m, uint8_t h, uint32_t x) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek uint8_t *p;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt assert(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* field id byte + signature length + signature 'u' + NUL + value */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek p = message_extend_fields(m, 8, 4 + 4);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!p)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -ENOMEM;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek p[0] = h;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek p[1] = 1;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering p[2] = SD_BUS_TYPE_UINT32;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering p[3] = 0;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering ((uint32_t*) p)[1] = x;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering return 0;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering}
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmekint bus_message_from_header(
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek void *buffer,
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek size_t length,
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek int *fds,
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek unsigned n_fds,
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek const struct ucred *ucred,
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek const char *label,
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek size_t extra,
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering sd_bus_message **ret) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering sd_bus_message *m;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek struct bus_header *h;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek size_t a, label_sz;
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek assert(buffer || length <= 0);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek assert(fds || n_fds <= 0);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek assert(ret);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (length < sizeof(struct bus_header))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EBADMSG;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek h = buffer;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (h->version != 1)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EBADMSG;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (h->serial == 0)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return -EBADMSG;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt if (h->type == _SD_BUS_MESSAGE_TYPE_INVALID)
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek return -EBADMSG;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt if (h->endian != SD_BUS_LITTLE_ENDIAN &&
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek h->endian != SD_BUS_BIG_ENDIAN)
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek return -EBADMSG;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek a = ALIGN(sizeof(sd_bus_message)) + ALIGN(extra);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (label) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk label_sz = strlen(label);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk a += label_sz + 1;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk }
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk m = malloc0(a);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (!m)
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk return -ENOMEM;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk m->n_ref = 1;
65089b82401cd395786a7987c470056ff3f01151Lennart Poettering m->sealed = true;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk m->header = h;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk m->fds = fds;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt m->n_fds = n_fds;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (ucred) {
65089b82401cd395786a7987c470056ff3f01151Lennart Poettering m->uid = ucred->uid;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk m->pid = ucred->pid;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk m->gid = ucred->gid;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering m->uid_valid = m->gid_valid = true;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering }
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt if (label) {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering m->label = (char*) m + ALIGN(sizeof(sd_bus_message)) + ALIGN(extra);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk memcpy(m->label, label, label_sz + 1);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk }
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk *ret = m;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering return 0;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering}
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringint bus_message_from_malloc(
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering void *buffer,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering size_t length,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering int *fds,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering unsigned n_fds,
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering const struct ucred *ucred,
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering const char *label,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering sd_bus_message **ret) {
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek sd_bus_message *m;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering int r;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering r = bus_message_from_header(buffer, length, fds, n_fds, ucred, label, 0, &m);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (r < 0)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return r;
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek if (length != BUS_MESSAGE_SIZE(m)) {
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek r = -EBADMSG;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering goto fail;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering m->n_body_parts = 1;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering m->body.data = (uint8_t*) buffer + sizeof(struct bus_header) + ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m));
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering m->body.size = length - sizeof(struct bus_header) - ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m));
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek m->body.sealed = true;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering m->body.memfd = -1;
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek m->n_iovec = 1;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering m->iovec = m->iovec_fixed;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering m->iovec[0].iov_base = buffer;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->iovec[0].iov_len = length;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = bus_message_parse_fields(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto fail;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering /* We take possession of the memory and fds now */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->free_header = true;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering m->free_fds = true;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering *ret = m;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekfail:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek message_free(m);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return r;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering}
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poetteringstatic sd_bus_message *message_new(sd_bus *bus, uint8_t type) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering sd_bus_message *m;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering m = malloc0(ALIGN(sizeof(sd_bus_message)) + sizeof(struct bus_header));
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (!m)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return NULL;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering m->n_ref = 1;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering m->header = (struct bus_header*) ((uint8_t*) m + ALIGN(sizeof(struct sd_bus_message)));
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering m->header->endian = SD_BUS_NATIVE_ENDIAN;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering m->header->type = type;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering m->header->version = bus ? bus->message_version : 1;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering m->allow_fds = !bus || bus->can_fds || (bus->state != BUS_HELLO && bus->state != BUS_RUNNING);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (bus)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering m->bus = sd_bus_ref(bus);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return m;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering}
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering_public_ int sd_bus_message_new_signal(
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering sd_bus *bus,
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering const char *path,
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering const char *interface,
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering const char *member,
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering sd_bus_message **m) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering sd_bus_message *t;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering int r;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering assert_return(!bus || bus->state != BUS_UNSET, -ENOTCONN);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering assert_return(object_path_is_valid(path), -EINVAL);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering assert_return(interface_name_is_valid(interface), -EINVAL);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering assert_return(member_name_is_valid(member), -EINVAL);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering assert_return(m, -EINVAL);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering t = message_new(bus, SD_BUS_MESSAGE_SIGNAL);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (!t)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return -ENOMEM;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering t->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
8531ae707d4d0203e83304d4af948b8169a5fce1Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto fail;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_INTERFACE, SD_BUS_TYPE_STRING, interface, &t->interface);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering goto fail;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_MEMBER, SD_BUS_TYPE_STRING, member, &t->member);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto fail;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *m = t;
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering return 0;
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekfail:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_bus_message_unref(t);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ int sd_bus_message_new_method_call(
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_bus *bus,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *destination,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *path,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *interface,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *member,
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering sd_bus_message **m) {
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering sd_bus_message *t;
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(!bus || bus->state != BUS_UNSET, -ENOTCONN);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(!destination || service_name_is_valid(destination), -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(object_path_is_valid(path), -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(!interface || interface_name_is_valid(interface), -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(member_name_is_valid(member), -EINVAL);
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering assert_return(m, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek t = message_new(bus, SD_BUS_MESSAGE_METHOD_CALL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!t)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -ENOMEM;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto fail;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_MEMBER, SD_BUS_TYPE_STRING, member, &t->member);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto fail;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (interface) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_INTERFACE, SD_BUS_TYPE_STRING, interface, &t->interface);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering goto fail;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (destination) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &t->destination);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto fail;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk *m = t;
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchukfail:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek message_free(t);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int message_new_reply(
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_bus *bus,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_bus_message *call,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering uint8_t type,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_bus_message **m) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_bus_message *t;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(!bus || bus->state != BUS_UNSET, -ENOTCONN);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(call, -EINVAL);
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering assert_return(call->sealed, -EPERM);
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering assert_return(m, -EINVAL);
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek t = message_new(bus, type);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!t)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -ENOMEM;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek t->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering t->reply_serial = BUS_MESSAGE_SERIAL(call);
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = message_append_field_uint32(t, SD_BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_serial);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (r < 0)
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering goto fail;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (call->sender) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, call->sender, &t->destination);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering if (r < 0)
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering goto fail;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering }
d378991747d67fff1d4dc39e7fb2bc8f49f1b561Mirco Tischler
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek t->dont_send = !!(call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek *m = t;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return 0;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmekfail:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek message_free(t);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering return r;
82499507b369fea3033a74c22813bf423301aef4Lennart Poettering}
82499507b369fea3033a74c22813bf423301aef4Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering_public_ int sd_bus_message_new_method_return(
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek sd_bus *bus,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek sd_bus_message *call,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek sd_bus_message **m) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering return message_new_reply(bus, call, SD_BUS_MESSAGE_METHOD_RETURN, m);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering}
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering_public_ int sd_bus_message_new_method_error(
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_bus *bus,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_bus_message *call,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const sd_bus_error *e,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_bus_message **m) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_bus_message *t;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(sd_bus_error_is_set(e), -EINVAL);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek assert_return(m, -EINVAL);
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = message_new_reply(bus, call, SD_BUS_MESSAGE_METHOD_ERROR, &t);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek if (r < 0)
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, e->name, &t->error.name);
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto fail;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (e->message) {
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering r = message_append_basic(t, SD_BUS_TYPE_STRING, e->message, (const void**) &t->error.message);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek goto fail;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *m = t;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekfail:
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek message_free(t);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
9bdbc2e2ec523dbefe1c1c7e164b5544aff0b185Lukas Nykryn
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ int sd_bus_message_new_method_errorf(
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering sd_bus *bus,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_bus_message *call,
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden sd_bus_message **m,
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden const char *name,
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden const char *format,
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden ...) {
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering _cleanup_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden va_list ap;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(name, -EINVAL);
0a20e3c10761378869af7bbef2733e3ae879d0f1Holger Hans Peter Freyther assert_return(m, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering va_start(ap, format);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek r = bus_error_setfv(&error, name, format, ap);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering va_end(ap);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek return sd_bus_message_new_method_error(bus, call, &error, m);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering_public_ int sd_bus_message_new_method_errno(
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_bus *bus,
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering sd_bus_message *call,
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering int error,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek const sd_bus_error *p,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek sd_bus_message **m) {
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek _cleanup_free_ sd_bus_error berror = SD_BUS_ERROR_NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering if (sd_bus_error_is_set(p))
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering return sd_bus_message_new_method_error(bus, call, p, m);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering sd_bus_error_set_errno(&berror, error);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering return sd_bus_message_new_method_error(bus, call, &berror, m);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering}
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering_public_ int sd_bus_message_new_method_errnof(
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_bus *bus,
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek sd_bus_message *call,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_bus_message **m,
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering int error,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *format,
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering ...) {
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering _cleanup_free_ sd_bus_error berror = SD_BUS_ERROR_NULL;
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering va_list ap;
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering int r;
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering va_start(ap, format);
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering r = bus_error_set_errnofv(&berror, error, format, ap);
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering va_end(ap);
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering if (r < 0)
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering return r;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering return sd_bus_message_new_method_error(bus, call, &berror, m);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering}
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poetteringint bus_message_new_synthetic_error(
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering sd_bus *bus,
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering uint64_t serial,
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering const sd_bus_error *e,
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering sd_bus_message **m) {
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering sd_bus_message *t;
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering int r;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
2d43b190901902dbd98ccea77c1d1ddc9e2a9955Dan McGee assert(sd_bus_error_is_set(e));
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering assert(m);
2d43b190901902dbd98ccea77c1d1ddc9e2a9955Dan McGee
ef1673d16907726d83bdff2e57b5261997a85020Mirco Tischler t = message_new(bus, SD_BUS_MESSAGE_METHOD_ERROR);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!t)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -ENOMEM;
6baa7db00812437bbc87e73faa1a11b6cf631958Lennart Poettering
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering t->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
f8294e4175918117ca6c131720bcf287eadcd029Josh Triplett t->reply_serial = serial;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering r = message_append_field_uint32(t, SD_BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_serial);
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering if (r < 0)
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering goto fail;
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (bus && bus->unique_name) {
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, bus->unique_name, &t->destination);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering if (r < 0)
e7ff4e7fe9f3abd2297e4ef7b95dcb2804e051c3Greg Kroah-Hartman goto fail;
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering }
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, e->name, &t->error.name);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto fail;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (e->message) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = message_append_basic(t, SD_BUS_TYPE_STRING, e->message, (const void**) &t->error.message);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (r < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek goto fail;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek *m = t;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return 0;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmekfail:
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek message_free(t);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return r;
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek}
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek_public_ sd_bus_message* sd_bus_message_ref(sd_bus_message *m) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert_return(m, NULL);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert(m->n_ref > 0);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering m->n_ref++;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return m;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek}
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek_public_ sd_bus_message* sd_bus_message_unref(sd_bus_message *m) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert_return(m, NULL);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert(m->n_ref > 0);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek m->n_ref--;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (m->n_ref <= 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek message_free(m);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering return NULL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek}
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek_public_ int sd_bus_message_get_type(sd_bus_message *m, uint8_t *type) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert_return(m, -EINVAL);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert_return(type, -EINVAL);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek *type = m->header->type;
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek return 0;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek}
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek_public_ int sd_bus_message_get_serial(sd_bus_message *m, uint64_t *serial) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert_return(m, -EINVAL);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert_return(serial, -EINVAL);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek assert_return(m->header->serial != 0, -ENOENT);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek *serial = BUS_MESSAGE_SERIAL(m);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return 0;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek}
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek_public_ int sd_bus_message_get_reply_serial(sd_bus_message *m, uint64_t *serial) {
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering assert_return(m, -EINVAL);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert_return(serial, -EINVAL);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert_return(m->reply_serial != 0, -ENOENT);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek *serial = m->reply_serial;
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering return 0;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek}
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek_public_ int sd_bus_message_get_no_reply(sd_bus_message *m) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert_return(m, -EINVAL);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek return m->header->type == SD_BUS_MESSAGE_METHOD_CALL ? !!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED) : 0;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek}
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek_public_ const char *sd_bus_message_get_path(sd_bus_message *m) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert_return(m, NULL);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return m->path;
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering}
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering_public_ const char *sd_bus_message_get_interface(sd_bus_message *m) {
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering assert_return(m, NULL);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return m->interface;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek}
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek_public_ const char *sd_bus_message_get_member(sd_bus_message *m) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert_return(m, NULL);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return m->member;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ const char *sd_bus_message_get_destination(sd_bus_message *m) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering assert_return(m, NULL);
a569398925430de1f8479262e8ab39502054f2e9Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return m->destination;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ const char *sd_bus_message_get_sender(sd_bus_message *m) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m, NULL);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return m->sender;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering_public_ const sd_bus_error *sd_bus_message_get_error(sd_bus_message *m) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m, NULL);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering assert_return(sd_bus_error_is_set(&m->error), NULL);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return &m->error;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
da4993920cdf5527b8528f0a483b54ab3cbc1971Kay Sievers_public_ int sd_bus_message_get_uid(sd_bus_message *m, uid_t *uid) {
40adcda869bda55f44b57fd3a2bd71d006dfb51bLennart Poettering assert_return(m, -EINVAL);
759c945a43577d56e85a927f15e7d9aaa94a4e4aColin Walters assert_return(uid, -EINVAL);
82499507b369fea3033a74c22813bf423301aef4Lennart Poettering assert_return(m->uid_valid, -ESRCH);
edc3797f7cd9e37c24e5241cac3263e7c918f732Lennart Poettering
edc3797f7cd9e37c24e5241cac3263e7c918f732Lennart Poettering *uid = m->uid;
edc3797f7cd9e37c24e5241cac3263e7c918f732Lennart Poettering return 0;
edc3797f7cd9e37c24e5241cac3263e7c918f732Lennart Poettering}
7517e17443225fafea86b21e42a36af69feb1dbcTorstein Husebø
8a0889dfdafa3054c894e54852d8a9e3a7e8390bLennart Poettering_public_ int sd_bus_message_get_gid(sd_bus_message *m, gid_t *gid) {
da4993920cdf5527b8528f0a483b54ab3cbc1971Kay Sievers assert_return(m, -EINVAL);
da4993920cdf5527b8528f0a483b54ab3cbc1971Kay Sievers assert_return(gid, -EINVAL);
759c945a43577d56e85a927f15e7d9aaa94a4e4aColin Walters assert_return(m->gid_valid, -ESRCH);
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *gid = m->gid;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ int sd_bus_message_get_pid(sd_bus_message *m, pid_t *pid) {
b6fa25552e538eca207072e12d223e3523b21a19Evgeny Vereshchagin assert_return(m, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(pid, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m->pid > 0, -ESRCH);
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *pid = m->pid;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
b6fa25552e538eca207072e12d223e3523b21a19Evgeny Vereshchagin_public_ int sd_bus_message_get_tid(sd_bus_message *m, pid_t *tid) {
b6fa25552e538eca207072e12d223e3523b21a19Evgeny Vereshchagin assert_return(m, -EINVAL);
b6fa25552e538eca207072e12d223e3523b21a19Evgeny Vereshchagin assert_return(tid, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m->tid > 0, -ESRCH);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *tid = m->tid;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ int sd_bus_message_get_pid_starttime(sd_bus_message *m, uint64_t *usec) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(usec, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m->pid_starttime > 0, -ESRCH);
e2cc6eca73cd1df8be552d7c23f9ff3d69c06f1eLennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *usec = m->pid_starttime;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ int sd_bus_message_get_selinux_context(sd_bus_message *m, const char **ret) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m->label, -ESRCH);
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *ret = m->label;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ int sd_bus_message_get_monotonic_timestamp(sd_bus_message *m, uint64_t *usec) {
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering assert_return(m, -EINVAL);
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering assert_return(usec, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m->monotonic > 0, -ESRCH);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek *usec = m->monotonic;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer_public_ int sd_bus_message_get_realtime_timestamp(sd_bus_message *m, uint64_t *usec) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_return(m, -EINVAL);
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek assert_return(usec, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m->realtime > 0, -ESRCH);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *usec = m->realtime;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ int sd_bus_message_get_comm(sd_bus_message *m, const char **ret) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(ret, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m->comm, -ESRCH);
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther *ret = m->comm;
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther return 0;
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther}
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ int sd_bus_message_get_tid_comm(sd_bus_message *m, const char **ret) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(ret, -EINVAL);
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering assert_return(m->tid_comm, -ESRCH);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *ret = m->tid_comm;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ int sd_bus_message_get_exe(sd_bus_message *m, const char **ret) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(ret, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m->exe, -ESRCH);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *ret = m->exe;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ int sd_bus_message_get_cgroup(sd_bus_message *m, const char **ret) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(ret, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m->cgroup, -ESRCH);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering *ret = m->cgroup;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return 0;
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ int sd_bus_message_get_unit(sd_bus_message *m, const char **ret) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek assert_return(m, -EINVAL);
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek assert_return(ret, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m->cgroup, -ESRCH);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering if (!m->unit) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = cg_path_get_unit(m->cgroup, &m->unit);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
caa2f4c0c9613b2e02aafa308c8fb092576014a9Zbigniew Jędrzejewski-Szmek }
84267e4043cf88bf540b5bf9cd65e194670a4ffaLennart Poettering
09eba4d46b1119c758fba38a520a38c8d19af739Lennart Poettering *ret = m->unit;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
caa2f4c0c9613b2e02aafa308c8fb092576014a9Zbigniew Jędrzejewski-Szmek_public_ int sd_bus_message_get_user_unit(sd_bus_message *m, const char **ret) {
caa2f4c0c9613b2e02aafa308c8fb092576014a9Zbigniew Jędrzejewski-Szmek int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(ret, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m->cgroup, -ESRCH);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!m->user_unit) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = cg_path_get_user_unit(m->cgroup, &m->user_unit);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
ac892057c2ddd8f06323c73ebd80423cc3ec7190Dimitri John Ledkov return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *ret = m->user_unit;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering_public_ int sd_bus_message_get_session(sd_bus_message *m, const char **ret) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_return(m, -EINVAL);
433dd100442e8197868def975c6fd38b48dc6439Lukas Nykryn assert_return(ret, -EINVAL);
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt assert_return(m->cgroup, -ESRCH);
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek
433dd100442e8197868def975c6fd38b48dc6439Lukas Nykryn if (!m->session) {
433dd100442e8197868def975c6fd38b48dc6439Lukas Nykryn r = cg_path_get_session(m->cgroup, &m->session);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *ret = m->session;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ int sd_bus_message_get_owner_uid(sd_bus_message *m, uid_t *uid) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(uid, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m->cgroup, -ESRCH);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return cg_path_get_owner_uid(m->cgroup, uid);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt_public_ int sd_bus_message_get_cmdline(sd_bus_message *m, char ***cmdline) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek size_t n, i;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *p;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek bool first;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m->cmdline, -ESRCH);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek for (p = m->cmdline, n = 0; p < m->cmdline + m->cmdline_length; p++)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (*p == 0)
fc1d70af2101e16c9e6f3c5bfd5ab315ee9e6daeLennart Poettering n++;
fc1d70af2101e16c9e6f3c5bfd5ab315ee9e6daeLennart Poettering
fc1d70af2101e16c9e6f3c5bfd5ab315ee9e6daeLennart Poettering m->cmdline_array = new(char*, n + 1);
fc1d70af2101e16c9e6f3c5bfd5ab315ee9e6daeLennart Poettering if (!m->cmdline_array)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -ENOMEM;
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt for (p = m->cmdline, i = 0, first = true; p < m->cmdline + m->cmdline_length; p++) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (first)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->cmdline_array[i++] = (char*) p;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek first = *p == 0;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->cmdline_array[i] = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *cmdline = m->cmdline_array;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ int sd_bus_message_get_audit_sessionid(sd_bus_message *m, uint32_t *sessionid) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m, -EINVAL);
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering assert_return(sessionid, -EINVAL);
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering assert_return(m->audit, -ESRCH);
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering *sessionid = m->audit->sessionid;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ int sd_bus_message_get_audit_loginuid(sd_bus_message *m, uid_t *uid) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(uid, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m->audit, -ESRCH);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *uid = m->audit->loginuid;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ int sd_bus_message_has_effective_cap(sd_bus_message *m, int capability) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unsigned sz;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(capability < 0, -EINVAL);
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering assert_return(!m->capability, -ESRCH);
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sz = m->capability_size / 4;
00a168618906bea43c3c57e20b9152582c324bf8Olivier Brunel if ((unsigned) capability >= sz*8)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return !!(m->capability[2 * sz + (capability / 8)] & (1 << (capability % 8)));
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt}
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ int sd_bus_message_is_signal(sd_bus_message *m,
93b73b064c663d6248bebfbbbd82989b5ca10fc5Lennart Poettering const char *interface,
93b73b064c663d6248bebfbbbd82989b5ca10fc5Lennart Poettering const char *member) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (m->header->type != SD_BUS_MESSAGE_SIGNAL)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (interface && (!m->interface || !streq(m->interface, interface)))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering if (member && (!m->member || !streq(m->member, member)))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt return 1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ int sd_bus_message_is_method_call(sd_bus_message *m,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *interface,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *member) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (interface && (!m->interface || !streq(m->interface, interface)))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (member && (!m->member || !streq(m->member, member)))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poettering
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poettering return 1;
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poettering}
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poettering
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poettering_public_ int sd_bus_message_is_method_error(sd_bus_message *m, const char *name) {
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poettering assert_return(m, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (name && (!m->error.name || !streq(m->error.name, name)))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 1;
804ae586d475d77946debb22c1bc9ee049d4750cLennart Poettering}
804ae586d475d77946debb22c1bc9ee049d4750cLennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ int sd_bus_message_set_no_reply(sd_bus_message *m, int b) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(!m->sealed, -EPERM);
804ae586d475d77946debb22c1bc9ee049d4750cLennart Poettering assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EPERM);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (b)
c6878637502b1717a110a9a7e8bba32a8583fcdfLennart Poettering m->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek else
763c7aa288485cf5ab627fe1d25ff58e76f9dacbZbigniew Jędrzejewski-Szmek m->header->flags &= ~SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering return 0;
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic struct bus_container *message_get_container(sd_bus_message *m) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m);
8531ae707d4d0203e83304d4af948b8169a5fce1Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering if (m->n_containers == 0)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return &m->root_container;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering assert(m->containers);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return m->containers + m->n_containers - 1;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering}
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poetteringstruct bus_body_part *message_append_part(sd_bus_message *m) {
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering struct bus_body_part *part;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering assert(m);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (m->poisoned)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return NULL;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (m->n_body_parts <= 0) {
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering part = &m->body;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering zero(*part);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering } else {
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering assert(m->body_end);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering part = new0(struct bus_body_part, 1);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (!part) {
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering m->poisoned = true;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return NULL;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering }
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering m->body_end->next = part;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering }
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering part->memfd = -1;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering m->body_end = part;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering m->n_body_parts ++;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return part;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering}
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poetteringstatic void part_zero(struct bus_body_part *part, size_t sz) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(part);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering assert(sz > 0);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering assert(sz < 8);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering /* All other fields can be left in their defaults */
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering assert(!part->data);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering assert(part->memfd < 0);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering part->size = sz;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering part->is_zero = true;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering part->sealed = true;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poetteringstatic int part_make_space(
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering struct sd_bus_message *m,
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering struct bus_body_part *part,
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering size_t sz,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek void **q) {
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering void *n;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering int r;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering assert(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(part);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering assert(!part->sealed);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (m->poisoned)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return -ENOMEM;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (!part->data && part->memfd < 0)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering part->memfd = bus_kernel_pop_memfd(m->bus, &part->data, &part->mapped);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (part->memfd >= 0) {
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering uint64_t u = sz;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering r = ioctl(part->memfd, KDBUS_CMD_MEMFD_SIZE_SET, &u);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (r < 0) {
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering m->poisoned = true;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return -errno;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering }
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (!part->data || sz > part->mapped) {
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering size_t psz = PAGE_ALIGN(sz > 0 ? sz : 1);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (part->mapped <= 0)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering n = mmap(NULL, psz, PROT_READ|PROT_WRITE, MAP_SHARED, part->memfd, 0);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering else
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering n = mremap(part->data, part->mapped, psz, MREMAP_MAYMOVE);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (n == MAP_FAILED) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->poisoned = true;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return -errno;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering part->mapped = psz;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering part->data = n;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering }
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering part->munmap_this = true;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering } else {
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering n = realloc(part->data, MAX(sz, 1u));
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (!n) {
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering m->poisoned = true;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return -ENOMEM;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering }
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering part->data = n;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering part->free_this = true;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering }
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (q)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering *q = part->data ? (uint8_t*) part->data + part->size : NULL;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering part->size = sz;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return 0;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering}
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poetteringstatic void message_extend_containers(sd_bus_message *m, size_t expand) {
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering struct bus_container *c;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering assert(m);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (expand <= 0)
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering return;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Update counters */
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering for (c = m->containers; c < m->containers + m->n_containers; c++)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (c->array_size)
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering *c->array_size += expand;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poetteringstatic void *message_extend_body(sd_bus_message *m, size_t align, size_t sz) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering struct bus_body_part *part = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek size_t start_body, end_body, padding, start_part, end_part, added;
ac5b0c13d8023745c0fbdaaa95dcec5b7b21d1e2Lennart Poettering bool add_new_part;
74055aa76278232ff05574fc47c4e6b3560554a7Lennart Poettering void *p;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering int r;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering assert(align > 0);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering assert(!m->sealed);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering if (m->poisoned)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return NULL;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering start_body = ALIGN_TO((size_t) m->header->body_size, align);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering end_body = start_body + sz;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering padding = start_body - m->header->body_size;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering added = padding + sz;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering /* Check for 32bit overflows */
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering if (end_body > (size_t) ((uint32_t) -1)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->poisoned = true;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
4daf54a851e4fb7ed1a13c3117bba12528fd2c7fZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek add_new_part =
6203e07a83214a55bb1f88508fcda2005c601deaLennart Poettering m->n_body_parts <= 0 ||
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->body_end->sealed ||
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek padding != ALIGN_TO(m->body_end->size, align) - m->body_end->size;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering if (add_new_part) {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering if (padding > 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek part = message_append_part(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!part)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return NULL;
72c0a2c255b172ebbb2a2b7dab7c9aec4c9582d9Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek part_zero(part, padding);
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering }
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering part = message_append_part(m);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering if (!part)
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering return NULL;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering r = part_make_space(m, part, sz, &p);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering return NULL;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering } else {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering struct bus_container *c;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek void *op;
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering size_t os;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering part = m->body_end;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek op = part->data;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek os = part->size;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek start_part = ALIGN_TO(part->size, align);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek end_part = start_part + sz;
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer
d581d9d91fa5c42ce7828a7d0b1334d370cf1670Susant Sahani r = part_make_space(m, part, end_part, &p);
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return NULL;
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering
b5884878a2874447b2a9f07f324a7cd909d96d48Lennart Poettering if (padding > 0) {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt memset(p, 0, padding);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek p = (uint8_t*) p + padding;
b5884878a2874447b2a9f07f324a7cd909d96d48Lennart Poettering }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d581d9d91fa5c42ce7828a7d0b1334d370cf1670Susant Sahani /* Readjust pointers */
d581d9d91fa5c42ce7828a7d0b1334d370cf1670Susant Sahani for (c = m->containers; c < m->containers + m->n_containers; c++)
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer c->array_size = adjust_pointer(c->array_size, op, os, part->data);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d581d9d91fa5c42ce7828a7d0b1334d370cf1670Susant Sahani m->error.message = (const char*) adjust_pointer(m->error.message, op, os, part->data);
d581d9d91fa5c42ce7828a7d0b1334d370cf1670Susant Sahani }
d581d9d91fa5c42ce7828a7d0b1334d370cf1670Susant Sahani
d581d9d91fa5c42ce7828a7d0b1334d370cf1670Susant Sahani m->header->body_size = end_body;
d581d9d91fa5c42ce7828a7d0b1334d370cf1670Susant Sahani message_extend_containers(m, added);
d581d9d91fa5c42ce7828a7d0b1334d370cf1670Susant Sahani
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return p;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekint message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct bus_container *c;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ssize_t align, sz;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek uint32_t k;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek void *a;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int fd = -1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek uint32_t fdi = 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(m, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(p, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(!m->sealed, -EPERM);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(bus_type_is_basic(type), -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(!m->poisoned, -ESTALE);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek c = message_get_container(m);
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen if (c->signature && c->signature[c->index]) {
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen /* Container signature is already set */
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen if (c->signature[c->index] != type)
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen return -ENXIO;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek char *e;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Maybe we can append to the signature? But only if this is the top-level container*/
804ae586d475d77946debb22c1bc9ee049d4750cLennart Poettering if (c->enclosing != 0)
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek return -ENXIO;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek e = strextend(&c->signature, CHAR_TO_STR(type), NULL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!e) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->poisoned = true;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -ENOMEM;
a9edaeff8481573764288ccf7e433a95b6dc7c03Josh Triplett }
a9edaeff8481573764288ccf7e433a95b6dc7c03Josh Triplett }
a9edaeff8481573764288ccf7e433a95b6dc7c03Josh Triplett
a9edaeff8481573764288ccf7e433a95b6dc7c03Josh Triplett switch (type) {
a9edaeff8481573764288ccf7e433a95b6dc7c03Josh Triplett
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek case SD_BUS_TYPE_STRING:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* To make things easy we'll serialize a NULL string
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering * into the empty string */
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering p = strempty(p);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk /* Fall through... */
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk case SD_BUS_TYPE_OBJECT_PATH:
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (!p) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk r = -EINVAL;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk goto fail;
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering }
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk align = 4;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk sz = 4 + strlen(p) + 1;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk break;
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering case SD_BUS_TYPE_SIGNATURE:
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering if (!p) {
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering r = -EINVAL;
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering goto fail;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk }
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk align = 1;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering sz = 1 + strlen(p) + 1;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering break;
ca2670162464b98f44d3f30a1d8b47b02609784cMichał Bartoszkiewicz
6a0f1f6d5af7c7300d3db7a0ba2b068f8abd222bLennart Poettering case SD_BUS_TYPE_BOOLEAN:
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering align = sz = 4;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk assert_cc(sizeof(int) == sizeof(uint32_t));
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering memcpy(&k, p, 4);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering k = !!k;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering p = &k;
6a0f1f6d5af7c7300d3db7a0ba2b068f8abd222bLennart Poettering break;
6a0f1f6d5af7c7300d3db7a0ba2b068f8abd222bLennart Poettering
6a0f1f6d5af7c7300d3db7a0ba2b068f8abd222bLennart Poettering case SD_BUS_TYPE_UNIX_FD: {
6a0f1f6d5af7c7300d3db7a0ba2b068f8abd222bLennart Poettering int z, *f;
6a0f1f6d5af7c7300d3db7a0ba2b068f8abd222bLennart Poettering
6a0f1f6d5af7c7300d3db7a0ba2b068f8abd222bLennart Poettering if (!m->allow_fds) {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering r = -ENOTSUP;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering goto fail;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering }
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering align = sz = 4;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering z = *(int*) p;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering if (z < 0) {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering r = -EINVAL;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering goto fail;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering }
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering fd = fcntl(z, F_DUPFD_CLOEXEC, 3);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (fd < 0) {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering r = -errno;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering goto fail;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk }
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk f = realloc(m->fds, sizeof(int) * (m->n_fds + 1));
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (!f) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering m->poisoned = true;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering r = -ENOMEM;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering goto fail;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering }
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering fdi = m->n_fds;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering f[fdi] = fd;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering m->fds = f;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering m->free_fds = true;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering break;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering }
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering default:
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering align = bus_type_get_alignment(type);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering sz = bus_type_get_size(type);
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt break;
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt }
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering assert(align > 0);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering assert(sz > 0);
28def94cc8fd4394b20e2155d7130166662343c4Dave Reisner
28def94cc8fd4394b20e2155d7130166662343c4Dave Reisner a = message_extend_body(m, align, sz);
28def94cc8fd4394b20e2155d7130166662343c4Dave Reisner if (!a) {
e53fc357a9bb9d0a5362ccc4246d598cb0febd5eLennart Poettering r = -ENOMEM;
03e334a1c7dc8c20c38902aa039440763acc9b17Lennart Poettering goto fail;
28def94cc8fd4394b20e2155d7130166662343c4Dave Reisner }
28def94cc8fd4394b20e2155d7130166662343c4Dave Reisner
28def94cc8fd4394b20e2155d7130166662343c4Dave Reisner if (type == SD_BUS_TYPE_STRING || type == SD_BUS_TYPE_OBJECT_PATH) {
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt *(uint32_t*) a = sz - 5;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering memcpy((uint8_t*) a + 4, p, sz - 4);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (stored)
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt *stored = (const uint8_t*) a + 4;
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering } else if (type == SD_BUS_TYPE_SIGNATURE) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering *(uint8_t*) a = sz - 1;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering memcpy((uint8_t*) a + 1, p, sz - 1);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (stored)
13790add4bf648fed816361794d8277a75253410Lennart Poettering *stored = (const uint8_t*) a + 1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else if (type == SD_BUS_TYPE_UNIX_FD) {
7d18d348da26fdbb392c76b0f5edb7f06282afbbZbigniew Jędrzejewski-Szmek *(uint32_t*) a = fdi;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (stored)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *stored = a;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering m->n_fds ++;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek memcpy(a, p, sz);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (stored)
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk *stored = a;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (c->enclosing != SD_BUS_TYPE_ARRAY)
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen c->index++;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
e150e82097211f09b911c7784a89ef9efed713caMichał Bartoszkiewicz return 0;
e150e82097211f09b911c7784a89ef9efed713caMichał Bartoszkiewicz
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekfail:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (fd >= 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek close_nointr_nofail(fd);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering_public_ int sd_bus_message_append_basic(sd_bus_message *m, char type, const void *p) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return message_append_basic(m, type, p, NULL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering_public_ int sd_bus_message_append_string_space(sd_bus_message *m, size_t size, char **s) {
d288f79fb4a2fe4a93cf99f74dacd2cebd3f2440Zbigniew Jędrzejewski-Szmek struct bus_container *c;
b1389b0d0805392570085acc7cb10eafcf885405Zbigniew Jędrzejewski-Szmek void *a;
b1389b0d0805392570085acc7cb10eafcf885405Zbigniew Jędrzejewski-Szmek
d288f79fb4a2fe4a93cf99f74dacd2cebd3f2440Zbigniew Jędrzejewski-Szmek assert_return(m, -EINVAL);
d288f79fb4a2fe4a93cf99f74dacd2cebd3f2440Zbigniew Jędrzejewski-Szmek assert_return(s, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_return(!m->sealed, -EPERM);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_return(!m->poisoned, -ESTALE);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt c = message_get_container(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (c->signature && c->signature[c->index]) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Container signature is already set */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (c->signature[c->index] != SD_BUS_TYPE_STRING)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -ENXIO;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering char *e;
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt /* Maybe we can append to the signature? But only if this is the top-level container*/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (c->enclosing != 0)
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering return -ENXIO;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRING), NULL);
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt if (!e) {
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt m->poisoned = true;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -ENOMEM;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek a = message_extend_body(m, 4, 4 + size + 1);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!a)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -ENOMEM;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *(uint32_t*) a = size;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *s = (char*) a + 4;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (*s)[size] = 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (c->enclosing != SD_BUS_TYPE_ARRAY)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek c->index++;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int bus_message_open_array(
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_bus_message *m,
03ee5c38cb0da193dd08733fb4c0c2809cee6a99Lennart Poettering struct bus_container *c,
03ee5c38cb0da193dd08733fb4c0c2809cee6a99Lennart Poettering const char *contents,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek uint32_t **array_size) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unsigned nindex;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek void *a, *op;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int alignment;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek size_t os;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct bus_body_part *o;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering assert(m);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering assert(c);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering assert(contents);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering assert(array_size);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (!signature_is_single(contents, true))
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return -EINVAL;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering alignment = bus_type_get_alignment(contents[0]);
4ec3cd7391e119b597375c547cf4ed50fce9f115Lennart Poettering if (alignment < 0)
4ec3cd7391e119b597375c547cf4ed50fce9f115Lennart Poettering return alignment;
13790add4bf648fed816361794d8277a75253410Lennart Poettering
13790add4bf648fed816361794d8277a75253410Lennart Poettering if (c->signature && c->signature[c->index]) {
13790add4bf648fed816361794d8277a75253410Lennart Poettering
13790add4bf648fed816361794d8277a75253410Lennart Poettering /* Verify the existing signature */
13790add4bf648fed816361794d8277a75253410Lennart Poettering
4ec3cd7391e119b597375c547cf4ed50fce9f115Lennart Poettering if (c->signature[c->index] != SD_BUS_TYPE_ARRAY)
13790add4bf648fed816361794d8277a75253410Lennart Poettering return -ENXIO;
13790add4bf648fed816361794d8277a75253410Lennart Poettering
13790add4bf648fed816361794d8277a75253410Lennart Poettering if (!startswith(c->signature + c->index + 1, contents))
4ec3cd7391e119b597375c547cf4ed50fce9f115Lennart Poettering return -ENXIO;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek nindex = c->index + 1 + strlen(contents);
15d91bff36c61d38df8edff258d1702a017a0e66Zbigniew Jędrzejewski-Szmek } else {
15d91bff36c61d38df8edff258d1702a017a0e66Zbigniew Jędrzejewski-Szmek char *e;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
13790add4bf648fed816361794d8277a75253410Lennart Poettering if (c->enclosing != 0)
13790add4bf648fed816361794d8277a75253410Lennart Poettering return -ENXIO;
13790add4bf648fed816361794d8277a75253410Lennart Poettering
13790add4bf648fed816361794d8277a75253410Lennart Poettering /* Extend the existing signature */
13790add4bf648fed816361794d8277a75253410Lennart Poettering
7d18d348da26fdbb392c76b0f5edb7f06282afbbZbigniew Jędrzejewski-Szmek e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_ARRAY), contents, NULL);
7d18d348da26fdbb392c76b0f5edb7f06282afbbZbigniew Jędrzejewski-Szmek if (!e) {
7d18d348da26fdbb392c76b0f5edb7f06282afbbZbigniew Jędrzejewski-Szmek m->poisoned = true;
37b7affefde5443680d73642a990ce86776e28afZbigniew Jędrzejewski-Szmek return -ENOMEM;
37b7affefde5443680d73642a990ce86776e28afZbigniew Jędrzejewski-Szmek }
15d91bff36c61d38df8edff258d1702a017a0e66Zbigniew Jędrzejewski-Szmek
15d91bff36c61d38df8edff258d1702a017a0e66Zbigniew Jędrzejewski-Szmek nindex = e - c->signature;
15d91bff36c61d38df8edff258d1702a017a0e66Zbigniew Jędrzejewski-Szmek }
15d91bff36c61d38df8edff258d1702a017a0e66Zbigniew Jędrzejewski-Szmek
37b7affefde5443680d73642a990ce86776e28afZbigniew Jędrzejewski-Szmek a = message_extend_body(m, 4, 4);
13790add4bf648fed816361794d8277a75253410Lennart Poettering if (!a)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -ENOMEM;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek o = m->body_end;
37b7affefde5443680d73642a990ce86776e28afZbigniew Jędrzejewski-Szmek op = m->body_end->data;
13790add4bf648fed816361794d8277a75253410Lennart Poettering os = m->body_end->size;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Add alignment between size and first element */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!message_extend_body(m, alignment, 0))
37b7affefde5443680d73642a990ce86776e28afZbigniew Jędrzejewski-Szmek return -ENOMEM;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (c->enclosing != SD_BUS_TYPE_ARRAY)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek c->index = nindex;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
7d18d348da26fdbb392c76b0f5edb7f06282afbbZbigniew Jędrzejewski-Szmek /* location of array size might have changed so let's readjust a */
7d18d348da26fdbb392c76b0f5edb7f06282afbbZbigniew Jędrzejewski-Szmek if (o == m->body_end)
7d18d348da26fdbb392c76b0f5edb7f06282afbbZbigniew Jędrzejewski-Szmek a = adjust_pointer(a, op, os, m->body_end->data);
7d18d348da26fdbb392c76b0f5edb7f06282afbbZbigniew Jędrzejewski-Szmek
7d18d348da26fdbb392c76b0f5edb7f06282afbbZbigniew Jędrzejewski-Szmek *(uint32_t*) a = 0;
7d18d348da26fdbb392c76b0f5edb7f06282afbbZbigniew Jędrzejewski-Szmek *array_size = a;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int bus_message_open_variant(
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_bus_message *m,
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering struct bus_container *c,
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering const char *contents) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering size_t l;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering void *a;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(c);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(contents);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!signature_is_single(contents, false))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EINVAL;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (*contents == SD_BUS_TYPE_DICT_ENTRY_BEGIN)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EINVAL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering if (c->signature && c->signature[c->index]) {
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering if (c->signature[c->index] != SD_BUS_TYPE_VARIANT)
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering return -ENXIO;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering } else {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering char *e;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
804ae586d475d77946debb22c1bc9ee049d4750cLennart Poettering if (c->enclosing != 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -ENXIO;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_VARIANT), NULL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!e) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->poisoned = true;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -ENOMEM;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek l = strlen(contents);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek a = message_extend_body(m, 1, 1 + l + 1);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!a)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -ENOMEM;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *(uint8_t*) a = l;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek memcpy((uint8_t*) a + 1, contents, l + 1);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (c->enclosing != SD_BUS_TYPE_ARRAY)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek c->index++;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int bus_message_open_struct(
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_bus_message *m,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct bus_container *c,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *contents) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek size_t nindex;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(m);
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt assert(c);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(contents);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt if (!signature_is_valid(contents, false))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EINVAL;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering if (c->signature && c->signature[c->index]) {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering size_t l;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering l = strlen(contents);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering if (c->signature[c->index] != SD_BUS_TYPE_STRUCT_BEGIN ||
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering !startswith(c->signature + c->index + 1, contents) ||
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering c->signature[c->index + 1 + l] != SD_BUS_TYPE_STRUCT_END)
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering return -ENXIO;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering nindex = c->index + 1 + l + 1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else {
03e334a1c7dc8c20c38902aa039440763acc9b17Lennart Poettering char *e;
03e334a1c7dc8c20c38902aa039440763acc9b17Lennart Poettering
03e334a1c7dc8c20c38902aa039440763acc9b17Lennart Poettering if (c->enclosing != 0)
03e334a1c7dc8c20c38902aa039440763acc9b17Lennart Poettering return -ENXIO;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
03e334a1c7dc8c20c38902aa039440763acc9b17Lennart Poettering e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_BEGIN), contents, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_END), NULL);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (!e) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m->poisoned = true;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -ENOMEM;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek nindex = e - c->signature;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Align contents to 8 byte boundary */
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering if (!message_extend_body(m, 8, 0))
99d0966e75a984bed4f117c888ecc93e16e7b7b6Lennart Poettering return -ENOMEM;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (c->enclosing != SD_BUS_TYPE_ARRAY)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek c->index = nindex;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
3e044c492e3ebe64f4e3175c94f9db8a62557b82Markus Elfring return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic int bus_message_open_dict_entry(
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering sd_bus_message *m,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering struct bus_container *c,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering const char *contents) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering size_t nindex;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(m);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(c);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(contents);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (!signature_is_pair(contents))
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return -EINVAL;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (c->enclosing != SD_BUS_TYPE_ARRAY)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return -ENXIO;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (c->signature && c->signature[c->index]) {
size_t l;
l = strlen(contents);
if (c->signature[c->index] != SD_BUS_TYPE_DICT_ENTRY_BEGIN ||
!startswith(c->signature + c->index + 1, contents) ||
c->signature[c->index + 1 + l] != SD_BUS_TYPE_DICT_ENTRY_END)
return -ENXIO;
nindex = c->index + 1 + l + 1;
} else
return -ENXIO;
/* Align contents to 8 byte boundary */
if (!message_extend_body(m, 8, 0))
return -ENOMEM;
if (c->enclosing != SD_BUS_TYPE_ARRAY)
c->index = nindex;
return 0;
}
_public_ int sd_bus_message_open_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;
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));
if (!w) {
m->poisoned = true;
return -ENOMEM;
}
m->containers = w;
c = message_get_container(m);
signature = strdup(contents);
if (!signature) {
m->poisoned = true;
return -ENOMEM;
}
/* Save old index in the parent container, in case we have to
* abort this container */
c->saved_index = c->index;
before = m->header->body_size;
if (type == SD_BUS_TYPE_ARRAY)
r = bus_message_open_array(m, c, contents, &array_size);
else if (type == SD_BUS_TYPE_VARIANT)
r = bus_message_open_variant(m, c, contents);
else if (type == SD_BUS_TYPE_STRUCT)
r = bus_message_open_struct(m, c, contents);
else if (type == SD_BUS_TYPE_DICT_ENTRY)
r = bus_message_open_dict_entry(m, c, contents);
else
r = -EINVAL;
if (r < 0) {
free(signature);
return r;
}
/* OK, let's fill it in */
w += m->n_containers++;
w->enclosing = type;
w->signature = signature;
w->index = 0;
w->array_size = array_size;
w->before = before;
w->begin = m->rindex;
return 0;
}
_public_ int sd_bus_message_close_container(sd_bus_message *m) {
struct bus_container *c;
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)
if (c->signature && c->signature[c->index] != 0)
return -EINVAL;
free(c->signature);
m->n_containers--;
return 0;
}
typedef struct {
const char *types;
unsigned n_struct;
unsigned n_array;
} TypeStack;
static int type_stack_push(TypeStack *stack, unsigned max, unsigned *i, const char *types, unsigned n_struct, unsigned n_array) {
assert(stack);
assert(max > 0);
if (*i >= max)
return -EINVAL;
stack[*i].types = types;
stack[*i].n_struct = n_struct;
stack[*i].n_array = n_array;
(*i)++;
return 0;
}
static int type_stack_pop(TypeStack *stack, unsigned max, unsigned *i, const char **types, unsigned *n_struct, unsigned *n_array) {
assert(stack);
assert(max > 0);
assert(types);
assert(n_struct);
assert(n_array);
if (*i <= 0)
return 0;
(*i)--;
*types = stack[*i].types;
*n_struct = stack[*i].n_struct;
*n_array = stack[*i].n_array;
return 1;
}
int bus_message_append_ap(
sd_bus_message *m,
const char *types,
va_list ap) {
unsigned n_array, n_struct;
TypeStack stack[BUS_CONTAINER_DEPTH];
unsigned stack_ptr = 0;
int r;
assert(m);
if (!types)
return 0;
n_array = (unsigned) -1;
n_struct = strlen(types);
for (;;) {
const char *t;
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)
return r;
if (r == 0)
break;
r = sd_bus_message_close_container(m);
if (r < 0)
return r;
continue;
}
t = types;
if (n_array != (unsigned) -1)
n_array --;
else {
types ++;
n_struct--;
}
switch (*t) {
case SD_BUS_TYPE_BYTE: {
uint8_t x;
x = (uint8_t) va_arg(ap, int);
r = sd_bus_message_append_basic(m, *t, &x);
break;
}
case SD_BUS_TYPE_BOOLEAN:
case SD_BUS_TYPE_INT32:
case SD_BUS_TYPE_UINT32:
case SD_BUS_TYPE_UNIX_FD: {
uint32_t x;
/* We assume a boolean is the same as int32_t */
assert_cc(sizeof(int32_t) == sizeof(int));
x = va_arg(ap, uint32_t);
r = sd_bus_message_append_basic(m, *t, &x);
break;
}
case SD_BUS_TYPE_INT16:
case SD_BUS_TYPE_UINT16: {
uint16_t x;
x = (uint16_t) va_arg(ap, int);
r = sd_bus_message_append_basic(m, *t, &x);
break;
}
case SD_BUS_TYPE_INT64:
case SD_BUS_TYPE_UINT64:
case SD_BUS_TYPE_DOUBLE: {
uint64_t x;
x = va_arg(ap, uint64_t);
r = sd_bus_message_append_basic(m, *t, &x);
break;
}
case SD_BUS_TYPE_STRING:
case SD_BUS_TYPE_OBJECT_PATH:
case SD_BUS_TYPE_SIGNATURE: {
const char *x;
x = va_arg(ap, const char*);
r = sd_bus_message_append_basic(m, *t, x);
break;
}
case SD_BUS_TYPE_ARRAY: {
size_t k;
r = signature_element_length(t + 1, &k);
if (r < 0)
return r;
{
char s[k + 1];
memcpy(s, t + 1, k);
s[k] = 0;
r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, s);
if (r < 0)
return r;
}
if (n_array == (unsigned) -1) {
types += k;
n_struct -= k;
}
r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
if (r < 0)
return r;
types = t + 1;
n_struct = k;
n_array = va_arg(ap, unsigned);
break;
}
case SD_BUS_TYPE_VARIANT: {
const char *s;
s = va_arg(ap, const char*);
if (!s)
return -EINVAL;
r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT, s);
if (r < 0)
return r;
r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
if (r < 0)
return r;
types = s;
n_struct = strlen(s);
n_array = (unsigned) -1;
break;
}
case SD_BUS_TYPE_STRUCT_BEGIN:
case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
size_t k;
r = signature_element_length(t, &k);
if (r < 0)
return r;
{
char s[k - 1];
memcpy(s, t + 1, k - 2);
s[k - 2] = 0;
r = sd_bus_message_open_container(m, *t == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s);
if (r < 0)
return r;
}
if (n_array == (unsigned) -1) {
types += k - 1;
n_struct -= k - 1;
}
r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
if (r < 0)
return r;
types = t + 1;
n_struct = k - 2;
n_array = (unsigned) -1;
break;
}
default:
r = -EINVAL;
}
if (r < 0)
return r;
}
return 0;
}
_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_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;
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 sd_bus_message_close_container(m);
}
int bus_body_part_map(struct bus_body_part *part) {
void *p;
size_t psz;
assert_se(part);
if (part->data)
return 0;
if (part->size <= 0)
return 0;
/* 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;
}
psz = PAGE_ALIGN(part->size);
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 (p == MAP_FAILED)
return -errno;
part->mapped = psz;
part->data = p;
part->munmap_this = true;
return 0;
}
void bus_body_part_unmap(struct bus_body_part *part) {
assert_se(part);
if (part->memfd < 0)
return;
if (!part->data)
return;
if (!part->munmap_this)
return;
assert_se(munmap(part->data, part->mapped) == 0);
part->data = NULL;
part->mapped = 0;
part->munmap_this = false;
return;
}
static int buffer_peek(const void *p, uint32_t sz, size_t *rindex, size_t align, size_t nbytes, void **r) {
size_t k, start, end;
assert(rindex);
assert(align > 0);
start = ALIGN_TO((size_t) *rindex, align);
end = start + nbytes;
if (end > sz)
return -EBADMSG;
/* Verify that padding is 0 */
for (k = *rindex; k < start; k++)
if (((const uint8_t*) p)[k] != 0)
return -EBADMSG;
if (r)
*r = (uint8_t*) p + start;
*rindex = end;
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;
assert(m);
c = message_get_container(m);
if (!c->array_size)
return false;
return index >= c->begin + BUS_MESSAGE_BSWAP32(m, *c->array_size);
}
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) {
part = m->cached_rindex_part;
begin = m->cached_rindex_part_begin;
} else {
part = &m->body;
begin = 0;
}
while (part) {
if (index < begin)
return NULL;
if (index + sz <= begin + part->size) {
r = bus_body_part_map(part);
if (r < 0)
return NULL;
if (p)
*p = (uint8_t*) part->data + index - begin;
m->cached_rindex_part = part;
m->cached_rindex_part_begin = begin;
return part;
}
begin += part->size;
part = part->next;
}
return NULL;
}
static int message_peek_body(
sd_bus_message *m,
size_t *rindex,
size_t align,
size_t nbytes,
void **ret) {
size_t k, start, end, padding;
struct bus_body_part *part;
uint8_t *q;
assert(m);
assert(rindex);
assert(align > 0);
if (message_end_of_array(m, *rindex))
return 0;
start = ALIGN_TO((size_t) *rindex, align);
padding = start - *rindex;
end = start + nbytes;
if (end > BUS_MESSAGE_BODY_SIZE(m))
return -EBADMSG;
part = find_part(m, *rindex, padding, (void**) &q);
if (!part)
return -EBADMSG;
if (q) {
/* Verify padding */
for (k = 0; k < padding; k++)
if (q[k] != 0)
return -EBADMSG;
}
part = find_part(m, start, nbytes, (void**) &q);
if (!part || !q)
return -EBADMSG;
*rindex = end;
if (ret)
*ret = q;
return 1;
}
static bool validate_nul(const char *s, size_t l) {
/* Check for NUL chars in the string */
if (memchr(s, 0, l))
return false;
/* Check for NUL termination */
if (s[l] != 0)
return false;
return true;
}
static bool validate_string(const char *s, size_t l) {
if (!validate_nul(s, l))
return false;
/* Check if valid UTF8 */
if (!utf8_is_valid(s))
return false;
return true;
}
static bool validate_signature(const char *s, size_t l) {
if (!validate_nul(s, l))
return false;
/* Check if valid signature */
if (!signature_is_valid(s, true))
return false;
return true;
}
static bool validate_object_path(const char *s, size_t l) {
if (!validate_nul(s, l))
return false;
if (!object_path_is_valid(s))
return false;
return true;
}
_public_ int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p) {
struct bus_container *c;
void *q;
int r;
assert_return(m, -EINVAL);
assert_return(m->sealed, -EPERM);
assert_return(bus_type_is_basic(type), -EINVAL);
if (message_end_of_signature(m))
return -ENXIO;
if (message_end_of_array(m, m->rindex))
return 0;
c = message_get_container(m);
if (c->signature[c->index] != type)
return -ENXIO;
switch (type) {
case SD_BUS_TYPE_STRING:
case SD_BUS_TYPE_OBJECT_PATH: {
uint32_t l;
size_t rindex;
rindex = m->rindex;
r = message_peek_body(m, &rindex, 4, 4, &q);
if (r <= 0)
return r;
l = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
r = message_peek_body(m, &rindex, 1, l+1, &q);
if (r < 0)
return r;
if (r == 0)
return -EBADMSG;
if (type == SD_BUS_TYPE_OBJECT_PATH) {
if (!validate_object_path(q, l))
return -EBADMSG;
} else {
if (!validate_string(q, l))
return -EBADMSG;
}
m->rindex = rindex;
if (p)
*(const char**) p = q;
break;
}
case SD_BUS_TYPE_SIGNATURE: {
uint8_t l;
size_t rindex;
rindex = m->rindex;
r = message_peek_body(m, &rindex, 1, 1, &q);
if (r <= 0)
return r;
l = *(uint8_t*) q;
r = message_peek_body(m, &rindex, 1, l+1, &q);
if (r < 0)
return r;
if (r == 0)
return -EBADMSG;
if (!validate_signature(q, l))
return -EBADMSG;
m->rindex = rindex;
if (p)
*(const char**) p = q;
break;
}
default: {
ssize_t sz, align;
size_t rindex;
align = bus_type_get_alignment(type);
sz = bus_type_get_size(type);
assert(align > 0 && sz > 0);
rindex = m->rindex;
r = message_peek_body(m, &rindex, align, sz, &q);
if (r <= 0)
return r;
switch (type) {
case SD_BUS_TYPE_BYTE:
if (p)
*(uint8_t*) p = *(uint8_t*) q;
break;
case SD_BUS_TYPE_BOOLEAN:
if (p)
*(int*) p = !!*(uint32_t*) q;
break;
case SD_BUS_TYPE_INT16:
case SD_BUS_TYPE_UINT16:
if (p)
*(uint16_t*) p = BUS_MESSAGE_BSWAP16(m, *(uint16_t*) q);
break;
case SD_BUS_TYPE_INT32:
case SD_BUS_TYPE_UINT32:
if (p)
*(uint32_t*) p = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
break;
case SD_BUS_TYPE_INT64:
case SD_BUS_TYPE_UINT64:
case SD_BUS_TYPE_DOUBLE:
if (p)
*(uint64_t*) p = BUS_MESSAGE_BSWAP64(m, *(uint64_t*) q);
break;
case SD_BUS_TYPE_UNIX_FD: {
uint32_t j;
j = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
if (j >= m->n_fds)
return -EBADMSG;
if (p)
*(int*) p = m->fds[j];
break;
}
default:
assert_not_reached("Unknown basic type...");
}
m->rindex = rindex;
break;
}
}
if (c->enclosing != SD_BUS_TYPE_ARRAY)
c->index++;
return 1;
}
static int bus_message_enter_array(
sd_bus_message *m,
struct bus_container *c,
const char *contents,
uint32_t **array_size) {
size_t rindex;
void *q;
int r, alignment;
assert(m);
assert(c);
assert(contents);
assert(array_size);
if (!signature_is_single(contents, true))
return -EINVAL;
alignment = bus_type_get_alignment(contents[0]);
if (alignment < 0)
return alignment;
if (!c->signature || c->signature[c->index] == 0)
return 0;
if (c->signature[c->index] != SD_BUS_TYPE_ARRAY)
return -ENXIO;
if (!startswith(c->signature + c->index + 1, contents))
return -ENXIO;
rindex = m->rindex;
r = message_peek_body(m, &rindex, 4, 4, &q);
if (r <= 0)
return r;
if (BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q) > BUS_ARRAY_MAX_SIZE)
return -EBADMSG;
r = message_peek_body(m, &rindex, alignment, 0, NULL);
if (r < 0)
return r;
if (r == 0)
return -EBADMSG;
if (c->enclosing != SD_BUS_TYPE_ARRAY)
c->index += 1 + strlen(contents);
m->rindex = rindex;
*array_size = (uint32_t*) q;
return 1;
}
static int bus_message_enter_variant(
sd_bus_message *m,
struct bus_container *c,
const char *contents) {
size_t rindex;
uint8_t l;
void *q;
int r;
assert(m);
assert(c);
assert(contents);
if (!signature_is_single(contents, false))
return -EINVAL;
if (*contents == SD_BUS_TYPE_DICT_ENTRY_BEGIN)
return -EINVAL;
if (!c->signature || c->signature[c->index] == 0)
return 0;
if (c->signature[c->index] != SD_BUS_TYPE_VARIANT)
return -ENXIO;
rindex = m->rindex;
r = message_peek_body(m, &rindex, 1, 1, &q);
if (r <= 0)
return r;
l = *(uint8_t*) q;
r = message_peek_body(m, &rindex, 1, l+1, &q);
if (r < 0)
return r;
if (r == 0)
return -EBADMSG;
if (!validate_signature(q, l))
return -EBADMSG;
if (!streq(q, contents))
return -ENXIO;
if (c->enclosing != SD_BUS_TYPE_ARRAY)
c->index++;
m->rindex = rindex;
return 1;
}
static int bus_message_enter_struct(
sd_bus_message *m,
struct bus_container *c,
const char *contents) {
size_t l;
int r;
assert(m);
assert(c);
assert(contents);
if (!signature_is_valid(contents, false))
return -EINVAL;
if (!c->signature || c->signature[c->index] == 0)
return 0;
l = strlen(contents);
if (c->signature[c->index] != SD_BUS_TYPE_STRUCT_BEGIN ||
!startswith(c->signature + c->index + 1, contents) ||
c->signature[c->index + 1 + l] != SD_BUS_TYPE_STRUCT_END)
return -ENXIO;
r = message_peek_body(m, &m->rindex, 8, 0, NULL);
if (r <= 0)
return r;
if (c->enclosing != SD_BUS_TYPE_ARRAY)
c->index += 1 + l + 1;
return 1;
}
static int bus_message_enter_dict_entry(
sd_bus_message *m,
struct bus_container *c,
const char *contents) {
size_t l;
int r;
assert(m);
assert(c);
assert(contents);
if (!signature_is_pair(contents))
return -EINVAL;
if (c->enclosing != SD_BUS_TYPE_ARRAY)
return -ENXIO;
if (!c->signature || c->signature[c->index] == 0)
return 0;
l = strlen(contents);
if (c->signature[c->index] != SD_BUS_TYPE_DICT_ENTRY_BEGIN ||
!startswith(c->signature + c->index + 1, contents) ||
c->signature[c->index + 1 + l] != SD_BUS_TYPE_DICT_ENTRY_END)
return -ENXIO;
r = message_peek_body(m, &m->rindex, 8, 0, NULL);
if (r <= 0)
return r;
if (c->enclosing != SD_BUS_TYPE_ARRAY)
c->index += 1 + l + 1;
return 1;
}
_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;
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
* higher than the 32 structs and 32 arrays the specification
* mandates. This is simpler to implement for us, and we need
* this only to ensure our container array doesn't grow
* without bounds. We are happy to return any data from a
* message as long as the data itself is valid, even if the
* overall message might be not.
*
* Note that the message signature is validated when
* parsing the headers, and that validation does check the
* 32/32 limit.
*
* Note that the specification defines no limits on the depth
* of stacked variants, but we do.
*/
if (m->n_containers >= BUS_CONTAINER_DEPTH)
return -EBADMSG;
w = realloc(m->containers, sizeof(struct bus_container) * (m->n_containers + 1));
if (!w)
return -ENOMEM;
m->containers = w;
if (message_end_of_signature(m))
return -ENXIO;
if (message_end_of_array(m, m->rindex))
return 0;
c = message_get_container(m);
signature = strdup(contents);
if (!signature)
return -ENOMEM;
c->saved_index = c->index;
before = m->rindex;
if (type == SD_BUS_TYPE_ARRAY)
r = bus_message_enter_array(m, c, contents, &array_size);
else if (type == SD_BUS_TYPE_VARIANT)
r = bus_message_enter_variant(m, c, contents);
else if (type == SD_BUS_TYPE_STRUCT)
r = bus_message_enter_struct(m, c, contents);
else if (type == SD_BUS_TYPE_DICT_ENTRY)
r = bus_message_enter_dict_entry(m, c, contents);
else
r = -EINVAL;
if (r <= 0) {
free(signature);
return r;
}
/* OK, let's fill it in */
w += m->n_containers++;
w->enclosing = type;
w->signature = signature;
w->index = 0;
w->array_size = array_size;
w->before = before;
w->begin = m->rindex;
return 1;
}
_public_ int sd_bus_message_exit_container(sd_bus_message *m) {
struct bus_container *c;
assert_return(m, -EINVAL);
assert_return(m->sealed, -EPERM);
assert_return(m->n_containers > 0, -EINVAL);
c = message_get_container(m);
if (c->enclosing == SD_BUS_TYPE_ARRAY) {
uint32_t l;
l = BUS_MESSAGE_BSWAP32(m, *c->array_size);
if (c->begin + l != m->rindex)
return -EBUSY;
} else {
if (c->signature && c->signature[c->index] != 0)
return -EINVAL;
}
free(c->signature);
m->n_containers--;
return 1;
}
static void message_quit_container(sd_bus_message *m) {
struct bus_container *c;
assert(m);
assert(m->sealed);
assert(m->n_containers > 0);
c = message_get_container(m);
/* Undo seeks */
assert(m->rindex >= c->before);
m->rindex = c->before;
/* Free container */
free(c->signature);
m->n_containers--;
/* Correct index of new top-level container */
c = message_get_container(m);
c->index = c->saved_index;
}
_public_ int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char **contents) {
struct bus_container *c;
int r;
assert_return(m, -EINVAL);
assert_return(m->sealed, -EPERM);
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;
if (type)
*type = c->signature[c->index];
return 1;
}
if (c->signature[c->index] == SD_BUS_TYPE_ARRAY) {
if (contents) {
size_t l;
char *sig;
r = signature_element_length(c->signature+c->index+1, &l);
if (r < 0)
return r;
assert(l >= 1);
sig = strndup(c->signature + c->index + 1, l);
if (!sig)
return -ENOMEM;
free(m->peeked_signature);
m->peeked_signature = sig;
*contents = sig;
}
if (type)
*type = SD_BUS_TYPE_ARRAY;
return 1;
}
if (c->signature[c->index] == SD_BUS_TYPE_STRUCT_BEGIN ||
c->signature[c->index] == SD_BUS_TYPE_DICT_ENTRY_BEGIN) {
if (contents) {
size_t l;
char *sig;
r = signature_element_length(c->signature+c->index, &l);
if (r < 0)
return r;
assert(l >= 2);
sig = strndup(c->signature + c->index + 1, l - 2);
if (!sig)
return -ENOMEM;
free(m->peeked_signature);
m->peeked_signature = sig;
*contents = sig;
}
if (type)
*type = c->signature[c->index] == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY;
return 1;
}
if (c->signature[c->index] == SD_BUS_TYPE_VARIANT) {
if (contents) {
size_t rindex, l;
void *q;
rindex = m->rindex;
r = message_peek_body(m, &rindex, 1, 1, &q);
if (r < 0)
return r;
if (r == 0)
goto eof;
l = *(uint8_t*) q;
r = message_peek_body(m, &rindex, 1, l+1, &q);
if (r < 0)
return r;
if (r == 0)
return -EBADMSG;
if (!validate_signature(q, l))
return -EBADMSG;
*contents = q;
}
if (type)
*type = SD_BUS_TYPE_VARIANT;
return 1;
}
return -EINVAL;
eof:
if (type)
*type = 0;
if (contents)
*contents = NULL;
return 0;
}
_public_ int sd_bus_message_rewind(sd_bus_message *m, int complete) {
struct bus_container *c;
assert_return(m, -EINVAL);
assert_return(m->sealed, -EPERM);
if (complete) {
message_reset_containers(m);
m->rindex = 0;
m->root_container.index = 0;
c = message_get_container(m);
} else {
c = message_get_container(m);
c->index = 0;
m->rindex = c->begin;
}
return !isempty(c->signature);
}
static int message_read_ap(
sd_bus_message *m,
const char *types,
va_list ap) {
unsigned n_array, n_struct;
TypeStack stack[BUS_CONTAINER_DEPTH];
unsigned stack_ptr = 0;
unsigned n_loop = 0;
int r;
assert(m);
if (isempty(types))
return 0;
/* Ideally, we'd just call ourselves recursively on every
* complex type. However, the state of a va_list that is
* passed to a function is undefined after that function
* returns. This means we need to docode the va_list linearly
* in a single stackframe. We hence implement our own
* home-grown stack in an array. */
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)
return r;
if (r == 0)
break;
r = sd_bus_message_exit_container(m);
if (r < 0)
return r;
continue;
}
t = types;
if (n_array != (unsigned) -1)
n_array --;
else {
types ++;
n_struct--;
}
switch (*t) {
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: {
void *p;
p = va_arg(ap, void*);
r = sd_bus_message_read_basic(m, *t, p);
if (r < 0)
return r;
if (r == 0) {
if (n_loop <= 1)
return 0;
return -ENXIO;
}
break;
}
case SD_BUS_TYPE_ARRAY: {
size_t k;
r = signature_element_length(t + 1, &k);
if (r < 0)
return r;
{
char s[k + 1];
memcpy(s, t + 1, k);
s[k] = 0;
r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, s);
if (r < 0)
return r;
if (r == 0) {
if (n_loop <= 1)
return 0;
return -ENXIO;
}
}
if (n_array == (unsigned) -1) {
types += k;
n_struct -= k;
}
r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
if (r < 0)
return r;
types = t + 1;
n_struct = k;
n_array = va_arg(ap, unsigned);
break;
}
case SD_BUS_TYPE_VARIANT: {
const char *s;
s = va_arg(ap, const char *);
if (!s)
return -EINVAL;
r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, s);
if (r < 0)
return r;
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)
return r;
types = s;
n_struct = strlen(s);
n_array = (unsigned) -1;
break;
}
case SD_BUS_TYPE_STRUCT_BEGIN:
case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
size_t k;
r = signature_element_length(t, &k);
if (r < 0)
return r;
{
char s[k - 1];
memcpy(s, t + 1, k - 2);
s[k - 2] = 0;
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 (n_loop <= 1)
return 0;
return -ENXIO;
}
}
if (n_array == (unsigned) -1) {
types += k - 1;
n_struct -= k - 1;
}
r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
if (r < 0)
return r;
types = t + 1;
n_struct = k - 2;
n_array = (unsigned) -1;
break;
}
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;
}
r = sd_bus_message_skip(m, types + k);
if (r < 0)
return r;
return 1;
}
default:
return -EINVAL;
}
}
_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;
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)
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;
}
r = sd_bus_message_exit_container(m);
if (r < 0)
goto fail;
*ptr = (const void*) p;
*size = sz;
return 1;
fail:
message_quit_container(m);
return r;
}
static int message_peek_fields(
sd_bus_message *m,
size_t *rindex,
size_t align,
size_t nbytes,
void **ret) {
assert(m);
assert(rindex);
assert(align > 0);
return buffer_peek(BUS_MESSAGE_FIELDS(m), BUS_MESSAGE_FIELDS_SIZE(m), rindex, align, nbytes, ret);
}
static int message_peek_field_uint32(
sd_bus_message *m,
size_t *ri,
uint32_t *ret) {
int r;
void *q;
assert(m);
assert(ri);
r = message_peek_fields(m, ri, 4, 4, &q);
if (r < 0)
return r;
if (ret)
*ret = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
return 0;
}
static int message_peek_field_string(
sd_bus_message *m,
bool (*validate)(const char *p),
size_t *ri,
const char **ret) {
uint32_t l;
int r;
void *q;
assert(m);
assert(ri);
r = message_peek_field_uint32(m, ri, &l);
if (r < 0)
return r;
r = message_peek_fields(m, ri, 1, l+1, &q);
if (r < 0)
return r;
if (validate) {
if (!validate_nul(q, l))
return -EBADMSG;
if (!validate(q))
return -EBADMSG;
} else {
if (!validate_string(q, l))
return -EBADMSG;
}
if (ret)
*ret = q;
return 0;
}
static int message_peek_field_signature(
sd_bus_message *m,
size_t *ri,
const char **ret) {
size_t l;
int r;
void *q;
assert(m);
assert(ri);
r = message_peek_fields(m, ri, 1, 1, &q);
if (r < 0)
return r;
l = *(uint8_t*) q;
r = message_peek_fields(m, ri, 1, l+1, &q);
if (r < 0)
return r;
if (!validate_signature(q, l))
return -EBADMSG;
if (ret)
*ret = q;
return 0;
}
static int message_skip_fields(
sd_bus_message *m,
size_t *ri,
uint32_t array_size,
const char **signature) {
size_t original_index;
int r;
assert(m);
assert(ri);
assert(signature);
original_index = *ri;
for (;;) {
char t;
size_t l;
if (array_size != (uint32_t) -1 &&
array_size <= *ri - original_index)
return 0;
t = **signature;
if (!t)
return 0;
if (t == SD_BUS_TYPE_STRING) {
r = message_peek_field_string(m, NULL, ri, NULL);
if (r < 0)
return r;
(*signature)++;
} else if (t == SD_BUS_TYPE_OBJECT_PATH) {
r = message_peek_field_string(m, object_path_is_valid, ri, NULL);
if (r < 0)
return r;
(*signature)++;
} else if (t == SD_BUS_TYPE_SIGNATURE) {
r = message_peek_field_signature(m, ri, NULL);
if (r < 0)
return r;
(*signature)++;
} else if (bus_type_is_basic(t)) {
ssize_t align, k;
align = bus_type_get_alignment(t);
k = bus_type_get_size(t);
assert(align > 0 && k > 0);
r = message_peek_fields(m, ri, align, k, NULL);
if (r < 0)
return r;
(*signature)++;
} else if (t == SD_BUS_TYPE_ARRAY) {
r = signature_element_length(*signature+1, &l);
if (r < 0)
return r;
assert(l >= 1);
{
char sig[l-1], *s;
uint32_t nas;
int alignment;
strncpy(sig, *signature + 1, l-1);
s = sig;
alignment = bus_type_get_alignment(sig[0]);
if (alignment < 0)
return alignment;
r = message_peek_field_uint32(m, ri, &nas);
if (r < 0)
return r;
if (nas > BUS_ARRAY_MAX_SIZE)
return -EBADMSG;
r = message_peek_fields(m, ri, alignment, 0, NULL);
if (r < 0)
return r;
r = message_skip_fields(m, ri, nas, (const char**) &s);
if (r < 0)
return r;
}
(*signature) += 1 + l;
} else if (t == SD_BUS_TYPE_VARIANT) {
const char *s;
r = message_peek_field_signature(m, ri, &s);
if (r < 0)
return r;
r = message_skip_fields(m, ri, (uint32_t) -1, (const char**) &s);
if (r < 0)
return r;
(*signature)++;
} else if (t == SD_BUS_TYPE_STRUCT ||
t == SD_BUS_TYPE_DICT_ENTRY) {
r = signature_element_length(*signature, &l);
if (r < 0)
return r;
assert(l >= 2);
{
char sig[l-1], *s;
strncpy(sig, *signature + 1, l-1);
s = sig;
r = message_skip_fields(m, ri, (uint32_t) -1, (const char**) &s);
if (r < 0)
return r;
}
*signature += l;
} else
return -EINVAL;
}
}
int bus_message_parse_fields(sd_bus_message *m) {
size_t ri;
int r;
uint32_t unix_fds = 0;
assert(m);
for (ri = 0; ri < BUS_MESSAGE_FIELDS_SIZE(m); ) {
const char *signature;
uint8_t *header;
r = message_peek_fields(m, &ri, 8, 1, (void**) &header);
if (r < 0)
return r;
r = message_peek_field_signature(m, &ri, &signature);
if (r < 0)
return r;
switch (*header) {
case _SD_BUS_MESSAGE_HEADER_INVALID:
return -EBADMSG;
case SD_BUS_MESSAGE_HEADER_PATH:
if (m->path)
return -EBADMSG;
if (!streq(signature, "o"))
return -EBADMSG;
r = message_peek_field_string(m, object_path_is_valid, &ri, &m->path);
break;
case SD_BUS_MESSAGE_HEADER_INTERFACE:
if (m->interface)
return -EBADMSG;
if (!streq(signature, "s"))
return -EBADMSG;
r = message_peek_field_string(m, interface_name_is_valid, &ri, &m->interface);
break;
case SD_BUS_MESSAGE_HEADER_MEMBER:
if (m->member)
return -EBADMSG;
if (!streq(signature, "s"))
return -EBADMSG;
r = message_peek_field_string(m, member_name_is_valid, &ri, &m->member);
break;
case SD_BUS_MESSAGE_HEADER_ERROR_NAME:
if (m->error.name)
return -EBADMSG;
if (!streq(signature, "s"))
return -EBADMSG;
r = message_peek_field_string(m, error_name_is_valid, &ri, &m->error.name);
break;
case SD_BUS_MESSAGE_HEADER_DESTINATION:
if (m->destination)
return -EBADMSG;
if (!streq(signature, "s"))
return -EBADMSG;
r = message_peek_field_string(m, service_name_is_valid, &ri, &m->destination);
break;
case SD_BUS_MESSAGE_HEADER_SENDER:
if (m->sender)
return -EBADMSG;
if (!streq(signature, "s"))
return -EBADMSG;
r = message_peek_field_string(m, service_name_is_valid, &ri, &m->sender);
break;
case SD_BUS_MESSAGE_HEADER_SIGNATURE: {
const char *s;
char *c;
if (m->root_container.signature)
return -EBADMSG;
if (!streq(signature, "g"))
return -EBADMSG;
r = message_peek_field_signature(m, &ri, &s);
if (r < 0)
return r;
c = strdup(s);
if (!c)
return -ENOMEM;
free(m->root_container.signature);
m->root_container.signature = c;
break;
}
case SD_BUS_MESSAGE_HEADER_REPLY_SERIAL:
if (m->reply_serial != 0)
return -EBADMSG;
if (!streq(signature, "u"))
return -EBADMSG;
r = message_peek_field_uint32(m, &ri, &m->reply_serial);
if (r < 0)
return r;
if (m->reply_serial == 0)
return -EBADMSG;
break;
case SD_BUS_MESSAGE_HEADER_UNIX_FDS:
if (unix_fds != 0)
return -EBADMSG;
if (!streq(signature, "u"))
return -EBADMSG;
r = message_peek_field_uint32(m, &ri, &unix_fds);
if (r < 0)
return -EBADMSG;
if (unix_fds == 0)
return -EBADMSG;
break;
default:
r = message_skip_fields(m, &ri, (uint32_t) -1, (const char **) &signature);
}
if (r < 0)
return r;
}
if (m->n_fds != unix_fds)
return -EBADMSG;
if (isempty(m->root_container.signature) != (BUS_MESSAGE_BODY_SIZE(m) == 0))
return -EBADMSG;
switch (m->header->type) {
case SD_BUS_MESSAGE_SIGNAL:
if (!m->path || !m->interface || !m->member)
return -EBADMSG;
break;
case SD_BUS_MESSAGE_METHOD_CALL:
if (!m->path || !m->member)
return -EBADMSG;
break;
case SD_BUS_MESSAGE_METHOD_RETURN:
if (m->reply_serial == 0)
return -EBADMSG;
break;
case SD_BUS_MESSAGE_METHOD_ERROR:
if (m->reply_serial == 0 || !m->error.name)
return -EBADMSG;
break;
}
/* Try to read the error message, but if we can't it's a non-issue */
if (m->header->type == SD_BUS_MESSAGE_METHOD_ERROR)
sd_bus_message_read(m, "s", &m->error.message);
return 0;
}
int bus_message_seal(sd_bus_message *m, uint64_t serial) {
struct bus_body_part *part;
size_t l, a;
unsigned i;
int r;
assert(m);
if (m->sealed)
return -EPERM;
if (m->n_containers > 0)
return -EBADMSG;
if (m->poisoned)
return -ESTALE;
/* If there's a non-trivial signature set, then add it in here */
if (!isempty(m->root_container.signature)) {
r = message_append_field_signature(m, SD_BUS_MESSAGE_HEADER_SIGNATURE, m->root_container.signature, NULL);
if (r < 0)
return r;
}
if (m->n_fds > 0) {
r = message_append_field_uint32(m, SD_BUS_MESSAGE_HEADER_UNIX_FDS, m->n_fds);
if (r < 0)
return r;
}
/* Add padding at the end of the fields part, since we know
* the body needs to start at an 8 byte alignment. We made
* sure we allocated enough space for this, so all we need to
* do here is to zero it out. */
l = BUS_MESSAGE_FIELDS_SIZE(m);
a = ALIGN8(l) - l;
if (a > 0)
memset((uint8_t*) BUS_MESSAGE_FIELDS(m) + l, 0, a);
/* If this is something we can send as memfd, then let's seal
the memfd now. Note that we can send memfds as payload only
for directed messages, and not for broadcasts. */
if (m->destination && m->bus && m->bus->use_memfd) {
MESSAGE_FOREACH_PART(part, i, m)
if (part->memfd >= 0 && !part->sealed && (part->size > MEMFD_MIN_SIZE || m->bus->use_memfd < 0)) {
bus_body_part_unmap(part);
if (ioctl(part->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 1) >= 0)
part->sealed = true;
}
}
m->header->serial = serial;
m->sealed = true;
return 0;
}
_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, FILE *f, bool with_header) {
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);
if (!f)
f = stdout;
if (with_header) {
fprintf(f,
"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"
"\tsealed=%s\n"
"\tn_body_parts=%u\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,
yes_no(m->sealed),
m->n_body_parts);
if (sd_bus_error_is_set(&m->error))
fprintf(f,
"\terror.name=%s\n"
"\terror.message=%s\n",
strna(m->error.name),
strna(m->error.message));
if (m->pid != 0)
fprintf(f, "\tpid=%lu\n", (unsigned long) m->pid);
if (m->tid != 0)
fprintf(f, "\ttid=%lu\n", (unsigned long) m->tid);
if (m->uid_valid)
fprintf(f, "\tuid=%lu\n", (unsigned long) m->uid);
if (m->gid_valid)
fprintf(f, "\tgid=%lu\n", (unsigned long) m->gid);
if (m->pid_starttime != 0)
fprintf(f, "\tpid_starttime=%llu\n", (unsigned long long) m->pid_starttime);
if (m->monotonic != 0)
fprintf(f, "\tmonotonic=%llu\n", (unsigned long long) m->monotonic);
if (m->realtime != 0)
fprintf(f, "\trealtime=%llu\n", (unsigned long long) m->realtime);
if (m->exe)
fprintf(f, "\texe=[%s]\n", m->exe);
if (m->comm)
fprintf(f, "\tcomm=[%s]\n", m->comm);
if (m->tid_comm)
fprintf(f, "\ttid_comm=[%s]\n", m->tid_comm);
if (m->label)
fprintf(f, "\tlabel=[%s]\n", m->label);
if (m->cgroup)
fprintf(f, "\tcgroup=[%s]\n", m->cgroup);
sd_bus_message_get_unit(m, &u);
if (u)
fprintf(f, "\tunit=[%s]\n", u);
sd_bus_message_get_user_unit(m, &uu);
if (uu)
fprintf(f, "\tuser_unit=[%s]\n", uu);
sd_bus_message_get_session(m, &s);
if (s)
fprintf(f, "\tsession=[%s]\n", s);
if (sd_bus_message_get_owner_uid(m, &owner) >= 0)
fprintf(f, "\towner_uid=%lu\n", (unsigned long) owner);
if (sd_bus_message_get_audit_loginuid(m, &audit_loginuid) >= 0)
fprintf(f, "\taudit_loginuid=%lu\n", (unsigned long) audit_loginuid);
if (sd_bus_message_get_audit_sessionid(m, &audit_sessionid) >= 0)
fprintf(f, "\taudit_sessionid=%lu\n", (unsigned long) audit_sessionid);
r = sd_bus_message_has_effective_cap(m, 5);
if (r >= 0)
fprintf(f, "\tCAP_KILL=%s\n", yes_no(r));
if (sd_bus_message_get_cmdline(m, &cmdline) >= 0) {
char **c;
fputs("\tcmdline=[", f);
STRV_FOREACH(c, cmdline) {
if (c != cmdline)
fputc(' ', f);
fputs(*c, f);
}
fputs("]\n", f);
}
}
r = sd_bus_message_rewind(m, true);
if (r < 0) {
log_error("Failed to rewind: %s", strerror(-r));
return r;
}
fprintf(f, "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)
fprintf(f, "%s} END_ARRAY \n", prefix);
else if (type == SD_BUS_TYPE_VARIANT)
fprintf(f, "%s} END_VARIANT\n", prefix);
else if (type == SD_BUS_TYPE_STRUCT)
fprintf(f, "%s} END_STRUCT\n", prefix);
else if (type == SD_BUS_TYPE_DICT_ENTRY)
fprintf(f, "%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)
fprintf(f, "%sBEGIN_ARRAY \"%s\" {\n", prefix, contents);
else if (type == SD_BUS_TYPE_VARIANT)
fprintf(f, "%sBEGIN_VARIANT \"%s\" {\n", prefix, contents);
else if (type == SD_BUS_TYPE_STRUCT)
fprintf(f, "%sBEGIN_STRUCT \"%s\" {\n", prefix, contents);
else if (type == SD_BUS_TYPE_DICT_ENTRY)
fprintf(f, "%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;
}
assert(r > 0);
switch (type) {
case SD_BUS_TYPE_BYTE:
fprintf(f, "%sBYTE: %u\n", prefix, basic.u8);
break;
case SD_BUS_TYPE_BOOLEAN:
fprintf(f, "%sBOOLEAN: %s\n", prefix, yes_no(basic.i));
break;
case SD_BUS_TYPE_INT16:
fprintf(f, "%sINT16: %i\n", prefix, basic.s16);
break;
case SD_BUS_TYPE_UINT16:
fprintf(f, "%sUINT16: %u\n", prefix, basic.u16);
break;
case SD_BUS_TYPE_INT32:
fprintf(f, "%sINT32: %i\n", prefix, basic.s32);
break;
case SD_BUS_TYPE_UINT32:
fprintf(f, "%sUINT32: %u\n", prefix, basic.u32);
break;
case SD_BUS_TYPE_INT64:
fprintf(f, "%sINT64: %lli\n", prefix, (long long) basic.s64);
break;
case SD_BUS_TYPE_UINT64:
fprintf(f, "%sUINT64: %llu\n", prefix, (unsigned long long) basic.u64);
break;
case SD_BUS_TYPE_DOUBLE:
fprintf(f, "%sDOUBLE: %g\n", prefix, basic.d64);
break;
case SD_BUS_TYPE_STRING:
fprintf(f, "%sSTRING: \"%s\"\n", prefix, basic.string);
break;
case SD_BUS_TYPE_OBJECT_PATH:
fprintf(f, "%sOBJECT_PATH: \"%s\"\n", prefix, basic.string);
break;
case SD_BUS_TYPE_SIGNATURE:
fprintf(f, "%sSIGNATURE: \"%s\"\n", prefix, basic.string);
break;
case SD_BUS_TYPE_UNIX_FD:
fprintf(f, "%sUNIX_FD: %i\n", prefix, basic.i);
break;
default:
assert_not_reached("Unknown basic type.");
}
}
fprintf(f, "} 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;
unsigned i;
struct bus_body_part *part;
assert(m);
assert(buffer);
assert(sz);
total = BUS_MESSAGE_SIZE(m);
p = malloc(total);
if (!p)
return -ENOMEM;
e = mempcpy(p, m->header, BUS_MESSAGE_BODY_BEGIN(m));
MESSAGE_FOREACH_PART(part, i, m)
e = mempcpy(e, part->data, part->size);
assert(total == (size_t) ((uint8_t*) e - (uint8_t*) p));
*buffer = p;
*sz = total;
return 0;
}
int bus_message_read_strv_extend(sd_bus_message *m, char ***l) {
int r;
assert(m);
assert(l);
r = sd_bus_message_enter_container(m, 'a', "s");
if (r <= 0)
return r;
for (;;) {
const char *s;
r = sd_bus_message_read_basic(m, 's', &s);
if (r < 0)
return r;
if (r == 0)
break;
r = strv_extend(l, s);
if (r < 0)
return r;
}
r = sd_bus_message_exit_container(m);
if (r < 0)
return r;
return 0;
}
_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) {
int r;
const char *t = NULL;
unsigned j;
assert(m);
r = sd_bus_message_rewind(m, true);
if (r < 0)
return NULL;
for (j = 0; j <= i; j++) {
char type;
r = sd_bus_message_peek_type(m, &type, NULL);
if (r < 0)
return NULL;
if (type != SD_BUS_TYPE_STRING &&
type != SD_BUS_TYPE_OBJECT_PATH &&
type != SD_BUS_TYPE_SIGNATURE)
return NULL;
r = sd_bus_message_read_basic(m, type, &t);
if (r < 0)
return NULL;
}
return t;
}
bool bus_header_is_complete(struct bus_header *h, size_t size) {
size_t full;
assert(h);
assert(size);
if (size < sizeof(struct bus_header))
return false;
full = sizeof(struct bus_header) +
(h->endian == SD_BUS_NATIVE_ENDIAN ? h->fields_size : bswap_32(h->fields_size));
return size >= full;
}
int bus_header_message_size(struct bus_header *h, size_t *sum) {
size_t fs, bs;
assert(h);
assert(sum);
if (h->endian == SD_BUS_NATIVE_ENDIAN) {
fs = h->fields_size;
bs = h->body_size;
} else if (h->endian == SD_BUS_REVERSE_ENDIAN) {
fs = bswap_32(h->fields_size);
bs = bswap_32(h->body_size);
} else
return -EBADMSG;
*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;
}