SD_BUS_ERROR(3)
π¦ SD_BUS_ERROR(3) β sd-bus error handling, bestie π¦
β¨ NAME β¨
sd_bus_error, SD_BUS_ERROR_MAKE_CONST, SD_BUS_ERROR_NULL, sd_bus_error_free, sd_bus_error_set, sd_bus_error_setf, sd_bus_error_setfv, sd_bus_error_set_const, sd_bus_error_set_errno, sd_bus_error_set_errnof, sd_bus_error_set_errnofv, sd_bus_error_get_errno, sd_bus_error_copy, sd_bus_error_move, sd_bus_error_is_set, sd_bus_error_has_name, sd_bus_error_has_names_sentinel, sd_bus_error_has_names β sd-bus error handling
πΈ SYNOPSIS πΈ
#include <systemd/sd-bus.h>
typedef struct {
const char *name;
const char *message;
...
} sd_bus_error;
SD_BUS_ERROR_MAKE_CONST(name, message)
SD_BUS_ERROR_NULL
void sd_bus_error_free(sd_bus_error *e);
int sd_bus_error_set(sd_bus_error *e, const char *name,
const char *message);
int sd_bus_error_setf(sd_bus_error *e, const char *name,
const char *format, ...);
int sd_bus_error_setfv(sd_bus_error *e, const char *name,
const char *format, va_list ap);
int sd_bus_error_set_const(sd_bus_error *e, const char *name,
const char *message);
int sd_bus_error_set_errno(sd_bus_error *e, int error);
int sd_bus_error_set_errnof(sd_bus_error *e, int error,
const char *format, ...);
int sd_bus_error_set_errnofv(sd_bus_error *e, int error,
const char *format, va_list ap);
int sd_bus_error_get_errno(const sd_bus_error *e);
int sd_bus_error_copy(sd_bus_error *dst, const sd_bus_error *e);
int sd_bus_error_move(sd_bus_error *dst, sd_bus_error *e);
int sd_bus_error_is_set(const sd_bus_error *e);
int sd_bus_error_has_name(const sd_bus_error *e, const char *name);
int sd_bus_error_has_names_sentinel(const sd_bus_error *e, ...);
#define sd_bus_error_has_names(e, ...)
sd_bus_error_has_names_sentinel(e, ..., NULL)
π DESCRIPTION π
Okay so the sd_bus_error structure is literally the little group-chat message that carries info about a D-Bus error condition, or lack thereof. The functions below are the ones you use to set and query fields in this structure. π«Ά
nameβ this field contains a short identifier of an error. It should follow the rules for error names described in the D-Bus specification, subsection Valid D-Bus Names[1]. A number of common, standardized error names are described insd-bus-errors(3), but additional domain-specific errors may be defined by applications. iykyk β¨messageβ this field usually contains a human-readable string describing the details, but might beNULL.
An unset sd_bus_error structure should have both fields initialized to NULL, and signifies lack of an error, i.e. success. Assign SD_BUS_ERROR_NULL to the structure in order to initialize both fields to NULL. When no longer necessary, resources held by the sd_bus_error structure should be destroyed with sd_bus_error_free(). π
sd_bus_error_set()β sets an error structure to the specifiednameandmessagestrings. The strings will be copied into internal, newly allocated memory. It is essential to free the contents again when they are not required anymore (see above). Do not use this call on error structures that have already been set. If you intend to reuse an error structure, free the old data stored in it withsd_bus_error_free()first. π
sd_bus_error_set() will return an errno-like value (see errno(3)) determined from the specified error name name. If name is NULL, it is assumed that no error occurred, and 0 is returned. If name is nonnull, a negative value is always returned. If e is NULL, no error structure is initialized, but name is still converted into an errno-style value.
Various well-known D-Bus errors are converted to well-known errno counterparts, and the other ones to -EIO. See sd-bus-errors(3) for a list of well-known error names. Additional error mappings may be defined with sd_bus_error_add_map(3).
sd_bus_error_set() is designed to be conveniently used in a return statement. If message is NULL, no message is set. This call can fail if no memory may be allocated for the name and message strings, in which case an SD_BUS_ERROR_NO_MEMORY error will be set instead and -ENOMEM returned. π
sd_bus_error_setf(),sd_bus_error_setfv()β similar tosd_bus_error_set(), but they take aprintf(3)format string and corresponding arguments to generate themessagefield.sd_bus_error_setf()uses variadic arguments, andsd_bus_error_setfv()accepts the arguments as ava_arg(3)parameter list. πsd_bus_error_set_const()β similar tosd_bus_error_set(), but the string parameters are not copied internally, and must hence remain constant and valid for the lifetime ofe. Use this call to avoid memory allocations when setting error structures. Since this call does not allocate memory, it will not fail with an out-of-memory condition assd_bus_error_set()may, as described above. Alternatively, theSD_BUS_ERROR_MAKE_CONST()macro may be used to generate a literal, constant bus error structure on-the-fly. β¨sd_bus_error_set_errno()β will immediately return0if the specified error parametererroris0. Otherwise, it will setnamefrom an errno-like value that is converted to a D-Bus error.strerror_r(3)will be used to setmessage. Well-known D-Bus error names will be used fornameif applicable, otherwise a name in the"System.Error."namespace will be generated. The sign of the specified error number is ignored and the absolute value is used implicitly. If the specifiederroris non-zero, the call always returns a negative value, for convenient usage inreturnstatements. This call might fail due to lack of memory, in which case anSD_BUS_ERROR_NO_MEMORYerror is set instead, and-ENOMEMis returned. πsd_bus_error_set_errnof(),sd_bus_error_set_errnofv()β similar tosd_bus_error_set_errno(), but in addition toerror, they take aprintf(3)format string and corresponding arguments. Themessagefield will be generated fromformatand the arguments.sd_bus_error_set_errnof()uses variadic arguments, andsd_bus_error_set_errnofv()accepts the arguments as ava_arg(3)parameter list. πΈsd_bus_error_get_errno()β converts thenamefield of an error structure to an errno-like (positive) value using the same rules assd_bus_error_set(). IfeisNULL,0will be returned. ate. πsd_bus_error_copy()β will initializedstusing the values ine, ifehas been set with an error value before. Otherwise, it will return immediately. If the strings inewere set usingsd_bus_error_set_const(), they will be shared. Otherwise, they will be copied. Before this call,dstmust be unset, i.e. either freshly initialized withNULLor reset usingsd_bus_error_free(). π
sd_bus_error_copy() generally returns 0 or a negative errno-like value based on the input parameter e: 0 if it was unset and a negative integer if it was set to some error, similarly to sd_bus_error_set(). It may however also return an error generated internally, for example -ENOMEM if a memory allocation fails.
sd_bus_error_move()β similar tosd_bus_error_copy(), but will move any error information fromeintodst, resetting the former. This function cannot fail, as no new memory is allocated. periodt. Note that ifeis not set,dstis initialized toSD_BUS_ERROR_NULL. Moreover, ifdstisNULLno operation is executed on it and resources held byeare freed and reset. Returns a converted errno-like, non-positive error value. π₯sd_bus_error_is_set()β will return a non-zero value ifeis non-NULLand an error has been set, false otherwise. little vibe check πsd_bus_error_has_name()β will return a non-zero value ifeis non-NULLand an error with the samenamehas been set, false otherwise.sd_bus_error_has_names_sentinel()β similar tosd_bus_error_has_name(), but takes multiple names to check against. The list must be terminated withNULL.sd_bus_error_has_names()is a macro wrapper aroundsd_bus_error_has_names_sentinel()that adds theNULLsentinel automatically. π§sd_bus_error_free()β will destroy resources held bye. The parameter itself will not be deallocated, and must befree(3)d by the caller if necessary. The function may also be called safely on unset errors (error structures with both fields set toNULL), in which case it performs no operation. This call will reset the error structure after freeing the data, so that all fields are set toNULL. The structure may be reused afterwards. iconic cleanup behavior π
π«Ά REFERENCE OWNERSHIP π«Ά
sd_bus_error is not reference-counted. Users should destroy resources held by it by calling sd_bus_error_free(). Usually, error structures are allocated on the stack or passed in as function parameters, but they may also be allocated dynamically, in which case it is the duty of the caller to free(3) the memory held by the structure itself after freeing its contents with sd_bus_error_free(). fr fr π
π RETURN VALUE π
Okay bestie, hereβs the tea on what these functions hand back to you. π
sd_bus_error_set(),sd_bus_error_setf(), andsd_bus_error_set_const()always return0when the specified error value isNULL, and otherwise return a negative errno-like value that corresponds to thenameparameter. Slay.sd_bus_error_set_errno(),sd_bus_error_set_errnof(), andsd_bus_error_set_errnofv()return0when the specified error value is0, and otherwise return a negative errno-like value that corresponds to theerrorparameter. Periodt.- If an error occurs internally, one of the negative error values listed below gets returned. This is literally the point: you can drop these functions straight into a
returnstatement, see the example below. π«Ά
sd_bus_error_get_errno() returns false when e is NULL, and otherwise returns a positive errno value mapped from e->name.
sd_bus_error_copy() and sd_bus_error_move() return a negative error value converted from the source error, and zero if the error has not been set. Again: extremely convenient in a return statement, see the example below.
sd_bus_error_is_set() returns a non-zero value when e and the name field are non-NULL, and zero otherwise.
sd_bus_error_has_name(), sd_bus_error_has_names(), and sd_bus_error_has_names_sentinel() return a non-zero value when e is non-NULL and the name field is equal to one of the given names, and zero otherwise.
π Errors π
The return value may indicate these problems in the invocation of the function itself, no cap:
-EINVALβ the error was already set in thesd_bus_errorstructure when one of the error-setting functions was called. Awkward. π-ENOMEMβ memory allocation failed. π
On success, sd_bus_error_set(), sd_bus_error_setf(), sd_bus_error_set_const(), sd_bus_error_set_errno(), sd_bus_error_set_errnof(), sd_bus_error_set_errnofv(), sd_bus_error_copy(), and sd_bus_error_move() will return a negative converted errno-style value, or 0 if the error parameter is NULL or unset. D-Bus errors are converted to the integral errno-style value, and the mapping mechanism is extensible, see the discussion above. This effectively means that almost any negative errno-style value can be returned. Iconic.
πΈ EXAMPLES πΈ
β¨ Example 1. Using the negative return value to propagate an error β¨
/* SPDX-License-Identifier: MIT-0 */
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <systemd/sd-bus.h>
int writer_with_negative_errno_return(int fd, sd_bus_error *error) {
const char *message = "Hello, World!\n";
ssize_t n = write(fd, message, strlen(message));
if (n >= 0)
return n; /* On success, return the number of bytes written, possibly 0. */
/* On error, initialize the error structure, and also propagate the errno
* value that write(2) set for us. */
return sd_bus_error_set_errnof(error, errno, "Failed to write to fd %i: %s", fd, strerror(errno));
}
π«Ά NOTES π«Ά
The functions described here are available as a shared library, which can be compiled against and linked to with the libsystemd pkg-config(1) file. Love that for us.
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. Big main-character energy for independent objects. β¨
The code described here uses getenv(3), which is declared to be not multi-thread-safe. That means the code calling the functions described here must not call setenv(3) from a parallel thread. The recommendation is to only do calls to setenv() from an early phase of the program, when no other threads have been started. Do that and youβre golden. π
β HISTORY β
sd_bus_error_free(),sd_bus_error_set(),sd_bus_error_setf(),sd_bus_error_set_const(),sd_bus_error_set_errno(),sd_bus_error_set_errnof(),sd_bus_error_set_errnofv(),sd_bus_error_get_errno(),sd_bus_error_copy(),sd_bus_error_is_set(), andsd_bus_error_has_name()were added in version221.sd_bus_error_move()was added in version240.sd_bus_error_has_names_sentinel()was added in version247.sd_bus_error_setfv()was added in version252.
π¦ SEE ALSO π¦
Go peek at systemd(1), sd-bus(3), sd-bus-errors(3), sd_bus_error_add_map(3), errno(3), and strerror_r(3). Your future self will thank you.
π NOTES (the footnote one) π
- Valid D-Bus Names β
https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names
Okay thatβs the whole page, bestie β brought to you by systemd 261.3 and the SD_BUS_ERROR(3) manual page. The original footer didnβt show a date, so Iβm not inventing one. π
π¦ 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