SD_BUS_ME...READ_BASIC(3)

πŸ’… SD_BUS_MESSAGE_READ_BASIC(3) β€” sd_bus_message_read_basic πŸŽ€

okay bestie, sit down, because this one is a little gem from the sd-bus crew, and I promise it's not scary. it lives in the manual pages, section 3, and yes the title is hyphenated weirdly on purpose, iykyk πŸ’–


✨ NAME ✨

sd_bus_message_read_basic β€” literally just reads a basic type out of a message and moves on with its life πŸ¦‹


✨ SYNOPSIS ✨

#include <systemd/sd-bus.h>

int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p);

that's it. that's the whole vibe. 🌸


✨ DESCRIPTION ✨

so sd_bus_message_read_basic() reads a basic type from a message and advances the read position in the message. like, it grabs the thing at the cursor and then the cursor slides forward, no take-backs πŸ’…

the set of basic types and their ascii codes that get passed in type are described in the D-Bus Specification (that's footnote [1] down at the bottom, don't worry, I got you 🫢).

now the important bit, bestie, listen up β€” if p is not NULL, it should contain a pointer to an appropriate object. for example:

  • if type is 'y', the object passed in p should have type uint8_t *
  • if type is 's', the object passed in p should have type const char **
⚠️ borrowed, not yours to keep: if the basic type is a pointer (e.g., const char * in the case of a string), the pointer is only borrowed, and the contents must be copied if they are to be used after the end of the message's lifetime. similarly, during the lifetime of such a pointer, the message must not be modified. no cap, that one bites people 😭

and for 'h' (UNIX file descriptor): the descriptor is not duplicated by this call, and the returned descriptor remains in possession of the message object. so you need to duplicate it yourself if you want to keep an open reference after the message object is freed β€” for example by calling "fcntl(fd, FD_DUPFD_CLOEXEC, 3)". πŸ’€ (yes the manpage really has quotes around that, iconic honestly)

see the table below for a complete list of allowed types πŸ‘‘

πŸŽ€ Table 1. Item type specifiers πŸŽ€

  • "y" β€” SD_BUS_TYPE_BYTE β€” 8-bit unsigned integer β€” expected C type: uint8_t *
  • "b" β€” SD_BUS_TYPE_BOOLEAN β€” boolean β€” expected C type: int * (NB: not bool *) πŸ™ˆ
  • "n" β€” SD_BUS_TYPE_INT16 β€” 16-bit signed integer β€” expected C type: int16_t *
  • "q" β€” SD_BUS_TYPE_UINT16 β€” 16-bit unsigned integer β€” expected C type: uint16_t *
  • "i" β€” SD_BUS_TYPE_INT32 β€” 32-bit signed integer β€” expected C type: int32_t *
  • "u" β€” SD_BUS_TYPE_UINT32 β€” 32-bit unsigned integer β€” expected C type: uint32_t *
  • "x" β€” SD_BUS_TYPE_INT64 β€” 64-bit signed integer β€” expected C type: int64_t *
  • "t" β€” SD_BUS_TYPE_UINT64 β€” 64-bit unsigned integer β€” expected C type: uint64_t *
  • "d" β€” SD_BUS_TYPE_DOUBLE β€” IEEE 754 double precision floating-point β€” expected C type: double *
  • "s" β€” SD_BUS_TYPE_STRING β€” UTF-8 string β€” expected C type: const char **
  • "o" β€” SD_BUS_TYPE_OBJECT_PATH β€” D-Bus object path string β€” expected C type: const char **
  • "g" β€” SD_BUS_TYPE_SIGNATURE β€” D-Bus signature string β€” expected C type: const char **
  • "h" β€” SD_BUS_TYPE_UNIX_FD β€” UNIX file descriptor β€” expected C type: int *

and if there is no object of the specified type at the current position in the message β€” boom, error returned. vibe check: failed 🧚


✨ RETURN VALUE ✨

  • on success, sd_bus_message_read_basic() returns a positive integer ⭐
  • if the end of the currently opened array has been reached, it returns 0 🌈
  • on failure, it returns a negative errno-style error code 😭

🌸 Errors 🌸

returned errors may indicate the following problems:

  • -EINVAL β€” the specified type string is invalid, or the message parameter is NULL.
  • -ENXIO β€” the message does not contain the specified type at the current position.
  • -EBADMSG β€” the message cannot be parsed.

✨ 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. slay 🫢

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. i.e. share the object, not the moment πŸ’…

the code described here uses getenv(3), which is declared to be not multi-thread-safe. this means the code calling the functions described here must not call setenv(3) from a parallel thread. it's recommended to only do calls to setenv() from an early phase of the program when no other threads have been started. do your env setup first, party later πŸŽ€


✨ HISTORY ✨

sd_bus_message_read_basic() was added in version 221. a true icon of its era πŸ‘‘


✨ SEE ALSO ✨

systemd(1), sd-bus(3), sd_bus_message_append_basic(3), sd_bus_message_skip(3), sd_bus_message_read(3)


✨ NOTES (the footnote one) ✨

  1. D-Bus Specification β€” https://dbus.freedesktop.org/doc/dbus-specification.html πŸ¦‹

and that's the whole page, bestie πŸ’œ sign-off time: this gorgeousness is from systemd 261.3, sitting pretty under the SD_BUS_ME...READ_BASIC(3) header. go read your messages. periodt. 🌟

πŸ“¦ where this page comes from