SD_BUS_MESSAGE_APPEND(3)
π NAME π
Okay bestie, this is the manual page for sd_bus_message_append(3) β and its ride-or-die sd_bus_message_appendv is right here too. The whole point: Attach fields to a D-Bus message based on a type string. Periodt. π
π§ SYNOPSIS π§
#include <systemd/sd-bus.h>
int sd_bus_message_append(sd_bus_message *m, const char *types, ...);
int sd_bus_message_appendv(sd_bus_message *m, const char *types,
va_list ap);
π₯ DESCRIPTION π₯
So sd_bus_message_append() appends a sequence of fields to the D-Bus message object m. The type string types describes the types of the field arguments that follow. For each type specified in the type string, one or more arguments need to be specified, in the same order as declared in the type string. Literally match the order, bestie β no chaos. β¨
The type string is composed of the elements shown in the table below. It contains zero or more single "complete types". Each complete type may be one of the basic types or a fully described container type. A container type may be a structure with the contained types, a variant, an array with its element type, or a dictionary entry with the contained types. The type string is NUL-terminated. π
- Basic type β in case of a basic type, one argument of the corresponding type is expected. Simple. Iconic. π
- Structure β a structure is denoted by a sequence of complete types between
(and). This sequence cannot be empty β it must contain at least one type. Arguments corresponding to this nested sequence follow the same rules as if they were not nested. Nested but not messy, iykyk. π¦ - Variant β a variant is denoted by
v. Corresponding arguments must begin with a type string denoting a complete type, and following that, arguments corresponding to the specified type. It's giving "type reveal" energy. πΈ - Array β an array is denoted by
afollowed by a complete type. Corresponding arguments must begin with the number of entries in the array, followed by the entries themselves, matching the element type of the array. Count first, then the entries β no cap. π₯ - Dictionary β a dictionary is an array of dictionary entries, denoted by
afollowed by a pair of complete types between{and}. The first of those types must be a basic type. Corresponding arguments must begin with the number of dictionary entries, followed by a pair of values for each entry matching the element type of the dictionary entries. Pair by pair, slay. π
sd_bus_message_appendv() is equivalent to sd_bus_message_append(), except that it is called with a va_list instead of a variable number of arguments. π¨ Real talk: 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. Not me warning you dramatically, but also yes. π
For further details on the D-Bus type system, please consult the D-Bus Specification[1]. π
π Table 1. Item type specifiers π
okay bestie, this is the cheat sheet for every type-string character β specifier, constant, meaning, size, and the C type you should expect. no skips, this is the whole table:
"y"β constantSD_BUS_TYPE_BYTE; unsigned integer; size: 1 byte; expected C type:uint8_t."b"β constantSD_BUS_TYPE_BOOLEAN; boolean; size: 4 bytes; expected C type:int."n"β constantSD_BUS_TYPE_INT16; signed integer; size: 2 bytes; expected C type:int16_t."q"β constantSD_BUS_TYPE_UINT16; unsigned integer; size: 2 bytes; expected C type:uint16_t."i"β constantSD_BUS_TYPE_INT32; signed integer; size: 4 bytes; expected C type:int32_t."u"β constantSD_BUS_TYPE_UINT32; unsigned integer; size: 4 bytes; expected C type:uint32_t."x"β constantSD_BUS_TYPE_INT64; signed integer; size: 8 bytes; expected C type:int64_t."t"β constantSD_BUS_TYPE_UINT64; unsigned integer; size: 8 bytes; expected C type:uint64_t."d"β constantSD_BUS_TYPE_DOUBLE; floating-point; size: 8 bytes; expected C type:double."s"β constantSD_BUS_TYPE_STRING; Unicode string; size: variable; expected C type:char[]."o"β constantSD_BUS_TYPE_OBJECT_PATH; object path; size: variable; expected C type:char[]."g"β constantSD_BUS_TYPE_SIGNATURE; signature; size: variable; expected C type:char[]."h"β constantSD_BUS_TYPE_UNIX_FD; UNIX file descriptor; size: 4 bytes; expected C type:int."a"β constantSD_BUS_TYPE_ARRAY; array; size: determined by array type and size; expected C type:int, followed by array contents."v"β constantSD_BUS_TYPE_VARIANT; variant; size: determined by the type argument; expected C type: signature string, followed by variant contents."("β constantSD_BUS_TYPE_STRUCT_BEGIN; array start; size: determined by the nested types; expected C type: structure contents.")"β constantSD_BUS_TYPE_STRUCT_END; array end; size: determined by the nested types; expected C type: structure contents."{"β constantSD_BUS_TYPE_DICT_ENTRY_BEGIN; dictionary entry start; size: determined by the nested types; expected C type: dictionary contents."}"β constantSD_BUS_TYPE_DICT_ENTRY_END; dictionary entry end; size: determined by the nested types; expected C type: dictionary contents.
okay so for types "s" and "g" (unicode string or signature), the pointer may be NULL, which is equivalent to an empty string. for "h" (UNIX file descriptor), the descriptor is duplicated by this call and the passed descriptor stays in possession of the caller. see sd_bus_message_append_basic(3) for the precise interpretation of those and other types. π
π TYPES STRING GRAMMAR π
this is the literal grammar vibe check for type strings, so copy it exactly β every character counts:
types ::= complete_type*
complete_type ::= basic_type | variant | structure | array | dictionary
basic_type ::= "y" | "n" | "q" | "u" | "i" | "x" | "t" | "d" |
"b" | "h" |
"s" | "o" | "g"
variant ::= "v"
structure ::= "(" complete_type+ ")"
array ::= "a" complete_type
dictionary ::= "a" "{" basic_type complete_type "}"
π EXAMPLES π
okay bestie, this is the part where it all clicks. real code, real vibes β¨
Append a single basic type (the string "a string"):
sd_bus_message *m;
...
sd_bus_message_append(m, "s", "a string");
one string, one call, done. we love a low-effort icon π
Append all types of integers:
uint8_t y = 1;
int16_t n = 2;
uint16_t q = 3;
int32_t i = 4;
uint32_t u = 5;
int32_t x = 6;
uint32_t t = 7;
double d = 8.0;
sd_bus_message_append(m, "ynqiuxtd", y, n, q, i, u, x, t, d);
look at that type string just eating all those variables in order π like, ynqiuxtd is genuinely giving alphabet soup but she's correct, ok? each letter matches its variable, one for one, no skipping π«Ά
Append a structure composed of a string and a D-Bus path:
sd_bus_message_append(m, "(so)", "a string", "/a/path");
parentheses = struct, s = string, o = path. that's it, that's the whole meal π₯
Append an array of UNIX file descriptors:
sd_bus_message_append(m, "ah", 3, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO);
see how the array needs its length 3 first, and then the elements? yeah. that's the rule for arrays, no cap. the h letter is the UNIX file descriptor type btw π
Append a variant, with the real type "g" (signature), and value "sdbusisgood":
sd_bus_message_append(m, "v", "g", "sdbusisgood");
a variant is literally just "surprise, here's the actual type" β so you pass v, then the signature string "g", then the value. and the value is "sdbusisgood" because of course it is π iconic behavior honestly
Append a dictionary containing the mapping {1=>"a", 2=>"b", 3=>""}:
sd_bus_message_append(m, "a{is}", 3, 1, "a", 2, "b", 3, NULL);
so a{is} is an array of dict entries from int β string. count first (3), then key/value, key/value, key/value. and notice the last value is NULL β that empty string is being passed as a null pointer, not a "". she's built different π
π RETURN VALUE π
On success, these functions return a non-negative integer. on failure? they return a negative errno-style error code. simple as that, bestie π
π Errors π
Returned errors may indicate the following problems:
-EINVALβ Specified parameter is invalid. (something you handed over just doesn't check out)-EPERMβ Message has been sealed. (sealed = locked, and she is not letting you in π)-ESTALEβ Message is in invalid state.-ENXIOβ Message cannot be appended to.-ENOMEMβ Memory allocation failed. (the classic π)
β¨ NOTES β¨
Functions described here are available as a shared library, which can be compiled against and linked to with the libsystemd pkg-config(1) file. so yes, you do need to link her properly, don't just vibe and hope π«Ά
Thread tea β β All functions listed here are thread-agnostic and only a single thread may operate on a given object at any given time. Different threads may access the same object at different times. Multiple independent objects may be used from different threads in parallel. translation: one object = one thread at a time, but different objects can party in parallel, no drama π
β οΈ okay important one: the code described here usesgetenv(3), which is declared to be not multi-thread-safe. This means that the code calling the functions described here must not callsetenv(3)from a parallel thread. It is recommended to only do calls tosetenv()from an early phase of the program when no other threads have been started. so like... set your env vars early, before the threads wake up, or it's giving chaos π
π SEE ALSO π
systemd(1), sd-bus(3), sd_bus_message_append_basic(3), sd_bus_message_append_array(3), sd_bus_message_open_container(3)
go read the rest of the family, they all ate β
π NOTES π
(yeah there's a second one, it's the footnote section, stay with me π§)
- D-Bus Specification β
https://dbus.freedesktop.org/doc/dbus-specification.html#type-system
and that's the whole page, bestie! π this lil masterpiece is from systemd 261.3, and it's the SD_BUS_MESSAGE_APPEND(3) manual page β go forth and append responsibly, mwah ππ¦
π¦ where this page comes from
- ubuntu stonking: libsystemd-dev 261.3-0ubuntu3 Β· LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms