bus-message.c revision de1c301ed165eb4d04a0c9d4babe97912b5233bb
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include "util.h"
#include "bus-message.h"
#include "sd-bus.h"
#include "bus-internal.h"
#include "bus-type.h"
#include "bus-signature.h"
static void message_free(sd_bus_message *m) {
unsigned i;
assert(m);
if (m->free_header)
if (m->free_fields)
if (m->free_body)
for (i = 0; i < m->n_fds; i++)
close_nointr_nofail(m->fds[i]);
for (i = 0; i < m->n_containers; i++)
free(m->sub_containers);
free(m);
}
void *k;
assert(p);
if (n == *sz)
return NULL;
k = realloc(*p, n);
if (!k)
return NULL;
/* Zero out padding */
*p = k;
*sz = n;
}
void *p, *o;
assert(m);
o = m->fields;
if (!p)
return NULL;
if (o != m->fields) {
/* Adjust quick access pointers */
if (m->path)
if (m->interface)
if (m->member)
if (m->destination)
if (m->sender)
if (m->signature)
}
m->free_fields = true;
return p;
}
static int message_append_field_string(
sd_bus_message *m,
uint8_t h,
char type,
const char *s,
const char **ret) {
size_t l;
uint8_t *p;
assert(m);
l = strlen(s);
return -EINVAL;
/* field id byte + signature length + signature 's' + NUL + string length + string + NUL */
if (!p)
return -ENOMEM;
p[0] = h;
p[1] = 1;
p[2] = type;
p[3] = 0;
((uint32_t*) p)[1] = l;
if (ret)
*ret = (const char*) p + 8;
return 0;
}
static int message_append_field_signature(
sd_bus_message *m,
uint8_t h,
const char *s,
const char **ret) {
size_t l;
uint8_t *p;
assert(m);
l = strlen(s);
if (l > 255)
return -EINVAL;
/* field id byte + signature length + signature 'g' + NUL + string length + string + NUL */
if (!p)
return -ENOMEM;
p[0] = h;
p[1] = 1;
p[2] = SD_BUS_TYPE_SIGNATURE;
p[3] = 0;
p[4] = l;
if (ret)
*ret = (const char*) p + 5;
return 0;
}
uint8_t *p;
assert(m);
/* field id byte + signature length + signature 'u' + NUL + value */
if (!p)
return -ENOMEM;
p[0] = h;
p[1] = 1;
p[2] = SD_BUS_TYPE_UINT32;
p[3] = 0;
((uint32_t*) p)[1] = x;
return 0;
}
sd_bus_message *m;
if (!m)
return NULL;
m->n_ref = 1;
#if __BYTE_ORDER == __BIG_ENDIAN
#else
#endif
return m;
}
const char *path,
const char *interface,
const char *member,
sd_bus_message **m) {
sd_bus_message *t;
int r;
if (!path)
return -EINVAL;
if (!interface)
return -EINVAL;
if (!member)
return -EINVAL;
if (!t)
return -ENOMEM;
r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path);
if (r < 0)
goto fail;
r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_INTERFACE, SD_BUS_TYPE_STRING, interface, &t->interface);
if (r < 0)
goto fail;
r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_MEMBER, SD_BUS_TYPE_STRING, member, &t->member);
if (r < 0)
goto fail;
*m = t;
return 0;
fail:
return r;
}
const char *destination,
const char *path,
const char *interface,
const char *member,
sd_bus_message **m) {
sd_bus_message *t;
int r;
if (!path)
return -EINVAL;
if (!member)
return -EINVAL;
if (!t)
return -ENOMEM;
r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path);
if (r < 0)
goto fail;
r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_MEMBER, SD_BUS_TYPE_STRING, member, &t->member);
if (r < 0)
goto fail;
if (interface) {
r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_INTERFACE, SD_BUS_TYPE_STRING, interface, &t->interface);
if (r < 0)
goto fail;
}
if (destination) {
r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &t->destination);
if (r < 0)
goto fail;
}
*m = t;
return 0;
fail:
message_free(t);
return r;
}
sd_bus_message **m) {
sd_bus_message *t;
int r;
if (!call)
return -EINVAL;
return -EINVAL;
if (!t)
return -ENOMEM;
if (r < 0)
goto fail;
r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, call->sender, &t->sender);
if (r < 0)
goto fail;
}
*m = t;
return 0;
fail:
message_free(t);
return r;
}
const sd_bus_error *e,
sd_bus_message **m) {
sd_bus_message *t;
int r;
if (!call)
return -EINVAL;
return -EINVAL;
if (!e)
return -EINVAL;
if (!e->name)
return -EINVAL;
if (!t)
return -ENOMEM;
if (r < 0)
goto fail;
r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, call->sender, &t->sender);
if (r < 0)
goto fail;
}
r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, e->name, &t->error.name);
if (r < 0)
goto fail;
if (e->message) {
if (r < 0)
goto fail;
}
*m = t;
return 0;
fail:
message_free(t);
return r;
}
if (!m)
return NULL;
m->n_ref++;
return m;
}
if (!m)
return NULL;
m->n_ref--;
if (m->n_ref <= 0)
message_free(m);
return NULL;
}
if (!m)
return -EINVAL;
if (!type)
return -EINVAL;
return 0;
}
if (!m)
return -EINVAL;
if (!serial)
return -EINVAL;
return -ENOENT;
*serial = BUS_MESSAGE_SERIAL(m);
return 0;
}
if (!m)
return -EINVAL;
if (!serial)
return -EINVAL;
if (m->reply_serial == 0)
return -ENOENT;
*serial = m->reply_serial;
return 0;
}
int sd_bus_message_get_no_reply(sd_bus_message *m) {
if (!m)
return -EINVAL;
return m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_CALL ? !!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED) : 0;
}
const char *sd_bus_message_get_path(sd_bus_message *m) {
if (!m)
return NULL;
return m->path;
}
const char *sd_bus_message_get_interface(sd_bus_message *m) {
if (!m)
return NULL;
return m->interface;
}
const char *sd_bus_message_get_member(sd_bus_message *m) {
if (!m)
return NULL;
return m->member;
}
const char *sd_bus_message_get_destination(sd_bus_message *m) {
if (!m)
return NULL;
return m->destination;
}
const char *sd_bus_message_get_sender(sd_bus_message *m) {
if (!m)
return NULL;
return m->sender;
}
if (!m)
return NULL;
if (!sd_bus_error_is_set(&m->error))
return NULL;
return &m->error;
}
if (!m)
return -EINVAL;
if (!m->uid_valid)
return -ENOENT;
return 0;
}
if (!m)
return -EINVAL;
if (!m->gid_valid)
return -ENOENT;
return 0;
}
if (!m)
return -EINVAL;
if (m->pid <= 0)
return -ENOENT;
return 0;
}
if (!m)
return -EINVAL;
if (m->tid <= 0)
return -ENOENT;
return 0;
}
if (!m)
return -EINVAL;
return 0;
return 0;
return 0;
return 1;
}
if (!m)
return -EINVAL;
return 0;
return 0;
return 0;
return 1;
}
if (!m)
return -EINVAL;
return 0;
return 0;
return 1;
}
int sd_bus_message_set_no_reply(sd_bus_message *m, int b) {
if (!m)
return -EINVAL;
if (m->sealed)
return -EPERM;
return -EPERM;
if (b)
else
return 0;
}
assert(m);
if (m->n_containers == 0)
return &m->root_container;
assert(m->sub_containers);
}
void *p, *o;
struct bus_container *c;
assert(m);
o = m->body;
if (!p)
return NULL;
if (c->array_size) {
*c->array_size += added;
}
if (o != m->body) {
}
m->free_body = true;
return p;
}
struct bus_container *c;
uint32_t k;
void *a;
char *e = NULL;
if (!m)
return -EINVAL;
if (m->sealed)
return -EPERM;
if (!bus_type_is_basic(type))
return -EINVAL;
c = message_get_container(m);
/* Container signature is already set */
return -EINVAL;
} else {
/* Maybe we can append to the signature? But only if this is the top-level container*/
if (c->enclosing != 0)
return -EINVAL;
if (!e)
return -ENOMEM;
}
switch (type) {
case SD_BUS_TYPE_STRING:
case SD_BUS_TYPE_OBJECT_PATH:
align = 4;
break;
case SD_BUS_TYPE_SIGNATURE:
align = 1;
break;
case SD_BUS_TYPE_BOOLEAN:
memcpy(&k, p, 4);
k = !!k;
p = &k;
break;
default:
break;
}
if (!a) {
/* Truncate extended signature again */
if (e)
return -ENOMEM;
}
if (stored)
} else if (type == SD_BUS_TYPE_SIGNATURE) {
if (stored)
} else {
if (stored)
*stored = a;
}
if (c->enclosing != SD_BUS_TYPE_ARRAY)
return 0;
}
}
static int bus_message_open_array(
sd_bus_message *m,
struct bus_container *c,
const char *contents,
uint32_t **array_size) {
char *e = NULL;
void *a, *b;
int alignment;
assert(m);
assert(c);
if (!signature_is_single(contents))
return -EINVAL;
if (alignment < 0)
return alignment;
/* Verify the existing signature */
return -EINVAL;
return -EINVAL;
} else {
if (c->enclosing != 0)
return -EINVAL;
/* Extend the existing signature */
if (!e)
return -ENOMEM;
}
if (!a) {
/* Truncate extended signature again */
if (e)
return -ENOMEM;
}
b = m->body;
if (!message_extend_body(m, alignment, 0)) {
/* Add alignment between size and first element */
if (e)
return -ENOMEM;
}
if (c->enclosing != SD_BUS_TYPE_ARRAY)
/* m->body might have changed so let's readjust a */
*(uint32_t*) a = 0;
*array_size = a;
return 0;
}
static int bus_message_open_variant(
sd_bus_message *m,
struct bus_container *c,
const char *contents) {
char *e = NULL;
void *a;
assert(m);
assert(c);
if (!signature_is_single(contents))
return -EINVAL;
if (*contents == SD_BUS_TYPE_DICT_ENTRY_BEGIN)
return -EINVAL;
return -EINVAL;
} else {
if (c->enclosing != 0)
return -EINVAL;
if (!e)
return -ENOMEM;
}
if (!a) {
/* Truncate extended signature again */
if (e)
return -ENOMEM;
}
*(uint8_t*) a = l;
if (c->enclosing != SD_BUS_TYPE_ARRAY)
return 0;
}
static int bus_message_open_struct(
sd_bus_message *m,
struct bus_container *c,
const char *contents) {
char *e = NULL;
assert(m);
assert(c);
if (!signature_is_valid(contents, false))
return -EINVAL;
size_t l;
return -EINVAL;
} else {
if (c->enclosing != 0)
return -EINVAL;
e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_BEGIN), contents, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_END), NULL);
if (!e)
return -ENOMEM;
}
/* Align contents to 8 byte boundary */
if (!message_extend_body(m, 8, 0)) {
if (e)
return -ENOMEM;
}
if (c->enclosing != SD_BUS_TYPE_ARRAY)
return 0;
}
static int bus_message_open_dict_entry(
sd_bus_message *m,
struct bus_container *c,
const char *contents) {
assert(m);
assert(c);
if (!signature_is_pair(contents))
return -EINVAL;
if (c->enclosing != SD_BUS_TYPE_ARRAY)
return -EINVAL;
size_t l;
return -EINVAL;
} else
return -EINVAL;
/* Align contents to 8 byte boundary */
if (!message_extend_body(m, 8, 0))
return -ENOMEM;
if (c->enclosing != SD_BUS_TYPE_ARRAY)
return 0;
}
sd_bus_message *m,
char type,
const char *contents) {
struct bus_container *c, *sub;
char *signature;
int r;
if (!m)
return -EINVAL;
if (m->sealed)
return -EPERM;
if (!contents)
return -EINVAL;
/* Make sure we have space for one more container */
if (!sub)
return -ENOMEM;
m->sub_containers = sub;
c = message_get_container(m);
if (!signature)
return -ENOMEM;
if (type == SD_BUS_TYPE_ARRAY)
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) {
return r;
}
/* OK, let's fill it in */
sub += m->n_containers++;
return 0;
}
int sd_bus_message_close_container(sd_bus_message *m) {
struct bus_container *c;
if (!m)
return -EINVAL;
if (m->sealed)
return -EPERM;
if (m->n_containers <= 0)
return -EINVAL;
c = message_get_container(m);
if (!c->signature)
return -EINVAL;
if (c->enclosing != SD_BUS_TYPE_ARRAY)
return -EINVAL;
m->n_containers--;
return 0;
}
static int message_append_ap(
sd_bus_message *m,
const char *types,
const char *t;
int r;
assert(m);
for (t = types; *t; t++) {
switch (*t) {
case SD_BUS_TYPE_BYTE: {
uint8_t x;
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: {
uint32_t x;
r = sd_bus_message_append_basic(m, *t, &x);
break;
}
case SD_BUS_TYPE_INT16:
case SD_BUS_TYPE_UINT16: {
uint16_t x;
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;
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;
r = sd_bus_message_append_basic(m, *t, x);
break;
}
case SD_BUS_TYPE_UNIX_FD: {
int x;
r = sd_bus_message_append_basic(m, *t, &x);
break;
}
case SD_BUS_TYPE_ARRAY: {
unsigned i, n;
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;
t += k;
r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, s);
if (r < 0)
return r;
for (i = 0; i < n; i++) {
r = message_append_ap(m, s, ap);
if (r < 0)
return r;
}
r = sd_bus_message_close_container(m);
}
break;
}
case SD_BUS_TYPE_VARIANT: {
const char *s;
if (!s)
return -EINVAL;
r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT, s);
if (r < 0)
return r;
r = message_append_ap(m, s, ap);
if (r < 0)
return r;
r = sd_bus_message_close_container(m);
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];
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;
t += k - 1;
r = message_append_ap(m, s, ap);
if (r < 0)
return r;
r = sd_bus_message_close_container(m);
}
break;
}
default:
r = -EINVAL;
}
if (r < 0)
return r;
}
return 0;
}
int r;
if (!m)
return -EINVAL;
if (m->sealed)
return -EPERM;
if (!types)
return -EINVAL;
return r;
}
return -ENOTSUP;
}
int sd_bus_message_read_basic(sd_bus_message *m, char type, char element, const void **p, size_t *length) {
return -ENOTSUP;
}
return -ENOTSUP;
}
int message_parse(sd_bus_message *m) {
assert(m);
return -EIO;
return -EIO;
return 0;
}
static void setup_iovec(sd_bus_message *m) {
assert(m);
m->n_iovec = 0;
m->n_iovec++;
if (m->fields) {
m->n_iovec++;
m->n_iovec++;
}
}
if (m->body) {
m->n_iovec++;
}
}
int r;
assert(m);
if (m->sealed)
return -EPERM;
if (m->n_containers > 0)
return -EBADMSG;
/* If there's a non-trivial signature set, then add it in here */
r = message_append_field_signature(m, SD_BUS_MESSAGE_HEADER_SIGNATURE, m->root_container.signature, &m->signature);
if (r < 0)
return r;
}
if (m->n_fds > 0) {
if (r < 0)
return r;
}
m->sealed = true;
setup_iovec(m);
return 0;
}
if (!m)
return -EINVAL;
if (!destination)
return -EINVAL;
if (m->sealed)
return -EPERM;
if (m->destination)
return -EEXIST;
return message_append_field_string(m, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &m->destination);
}
void message_dump(sd_bus_message *m) {
log_info("Message %p\n"
"\tn_ref=%u\n"
"\tendian=%c\n"
"\ttype=%i\n"
"\tflags=%u\n"
"\tversion=%u\n"
"\tserial=%u\n"
"\tfields_size=%u\n"
"\tbody_size=%u\n"
"\tpath=%s\n"
"\tinterface=%s\n"
"\tmember=%s\n"
"\tdestination=%s\n"
"\tsender=%s\n"
"\tsignature=%s\n"
"\treply_serial=%u\n"
"\terror.name=%s\n"
"\terror.message=%s\n"
"\tsealed=%s\n",
m,
m->n_ref,
strna(m->destination),
m->reply_serial,
}
unsigned i;
void *p, *e;
assert(m);
if (!p)
return -ENOMEM;
for (i = 0, e = p; i < m->n_iovec; i++)
*buffer = p;
return 0;
}