sd_bus_message_read - Man Page

Read a sequence of values from a message


#include <systemd/sd-bus.h>

int sd_bus_message_read(sd_bus_message *m, char char *types, ...);

int sd_bus_message_readv(sd_bus_message *m, char char *types, va_list ap);


sd_bus_message_read() reads a sequence of fields from the D-Bus message object m and advances the read position in the message. The type string types describes the types of items expected in the message and the field arguments that follow. The type string may be NULL or empty, in which case nothing is read.

The type string is composed of the elements described in sd_bus_message_append(3), i.e. basic and container types. It must contain zero or more single "complete types". The type string is NUL-terminated.

For each type specified in the type string, one or more arguments need to be specified after the types parameter, in the same order. The arguments must be pointers to appropriate types (a pointer to int8_t for a "y" in the type string, a pointer to int32_t for an "i", a pointer to const char* for an "s", ...) which are set based on the values in the message. As an exception, in case or array and variant types, the first argument is an "input" argument that further specifies how the message should be read. See the table below for a complete list of allowed arguments and their types. Note that, if the basic type is a pointer (e.g., const char * in the case of a string), the argument is a pointer to a pointer, and also the pointer value that is written is only borrowed and the contents must be copied if they are to be used after the end of the messages lifetime.

Each argument may also be NULL, in which case the value is read and ignored.

Table 1. Item type specifiers

SpecifierConstantDescriptionType of the first argumentTypes of the subsequent arguments, if any
"y"SD_BUS_TYPE_BYTE8bit unsigned integeruint8_t * 
"b"SD_BUS_TYPE_BOOLEANbooleanint * (NB: not bool *) 
"n"SD_BUS_TYPE_INT1616bit signed integerint16_t * 
"q"SD_BUS_TYPE_UINT1616bit unsigned integeruint16_t * 
"i"SD_BUS_TYPE_INT3232bit signed integerint32_t * 
"u"SD_BUS_TYPE_UINT3232bit unsigned integeruint32_t * 
"x"SD_BUS_TYPE_INT6464bit signed integerint64_t * 
"t"SD_BUS_TYPE_UINT6464bit unsigned integeruint64_t * 
"d"SD_BUS_TYPE_DOUBLEIEEE 754 double precision floating-pointdouble * 
"s"SD_BUS_TYPE_STRINGUTF-8 stringconst char ** 
"o"SD_BUS_TYPE_OBJECT_PATHD-Bus object path stringconst char ** 
"g"SD_BUS_TYPE_SIGNATURED-Bus signature stringconst char ** 
"h"SD_BUS_TYPE_UNIX_FDUNIX file descriptorint * 
"a"SD_BUS_TYPE_ARRAYarrayint, which specifies the expected length n of the arrayn sets of arguments appropriate for the array element type
"v"SD_BUS_TYPE_VARIANTvariantsignature stringarguments appropriate for the types specified by the signature
"("SD_BUS_TYPE_STRUCT_BEGINarray startarguments appropriate for the structure elements
"{"SD_BUS_TYPE_DICT_ENTRY_BEGINdictionary entry startarguments appropriate for the first type in the pairarguments appropriate for the second type in the pair
"}"SD_BUS_TYPE_DICT_ENTRY_ENDdictionary entry end

If objects of the specified types are not present at the current position in the message, an error is returned.

The sd_bus_message_readv() is equivalent to the sd_bus_message_read(), except that it is called with a "va_list" instead of a variable number of arguments. This function does not call the va_end() macro. Because it invokes the va_arg() macro, the value of ap is undefined after the call.

Return Value

On success, sd_bus_message_read() and sd_bus_message_readv() return 0 or a positive integer. On failure, they return a negative errno-style error code.


Returned errors may indicate the following problems:


Specified type string is invalid or the message parameter is NULL.


The message does not contain the specified type at current position.


The message cannot be parsed.


These APIs are implemented as a shared library, which can be compiled and linked to with the libsystemd pkg-config(1) file.


Read a single basic type (a 64-bit integer):

sd_bus_message *m;
int64_t x;

sd_bus_message_read(m, "x", &x);

Read a boolean value:

sd_bus_message *m;
int x; /* Do not use C99 'bool' type here, it's typically smaller
          in memory and would cause memory corruption */

sd_bus_message_read(m, "b", &x);

Read all types of integers:

uint8_t y;
int16_t n;
uint16_t q;
int32_t i;
uint32_t u;
int32_t x;
uint32_t t;
double d;

sd_bus_message_read(m, "ynqiuxtd", &y, &n, &q, &i, &u, &x, &t, &d);

Read a structure composed of a string and a D-Bus path:

const char *s, *p;

sd_bus_message_read(m, "(so)", &s, &p);

Read a variant, with the real type "gt" (signature, unsigned integer):

const char *s;
uint64_t *v;

sd_bus_message_read(m, "v", "gt", &s, &v);

Read a dictionary containing three pairs of type {integer=>string}:

int i, j, k;
const char *s, *t, *u;

sd_bus_message_read(m, "a{is}", 3, &i, &s, &j, &t, &k, &u);

See Also

systemd(1), sd-bus(3), sd_bus_message_read_basic(3), sd_bus_message_skip(3), sd_bus_message_append(3)

Referenced By

sd-bus(3), sd_bus_message_read_array(3), sd_bus_message_read_basic(3), sd_bus_message_rewind(3), sd_bus_message_skip(3), systemd.directives(7), systemd.index(7).

The man page sd_bus_message_readv(3) is an alias of sd_bus_message_read(3).

systemd 245