NAME |SYNOPSIS |DESCRIPTION |REFERENCE OWNERSHIP |RETURN VALUE |EXAMPLES |NOTES |HISTORY |SEE ALSO |NOTES |COLOPHON | |
SD_BUS_ERROR(3) sd_bus_errorSD_BUS_ERROR(3)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
#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_NULLvoid 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_listap);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, interror);int sd_bus_error_set_errnof(sd_bus_error *e, interror,const char *format, ...);int sd_bus_error_set_errnofv(sd_bus_error *e, interror,const char *format, va_listap);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)The sd_bus_error structure carries information about a D-Bus error condition, or lack thereof. The functions described below may be used to set and query fields in this structure. • Thename field contains a short identifier of an error. It should follow the rules for error names described in the D-Bus specification, subsectionValid 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. • Themessage field usually contains a human-readable string describing the details, but might beNULL. An unset sd_bus_error structure should have both fields initialized toNULL, and signifies lack of an error, i.e. success. AssignSD_BUS_ERROR_NULLto the structure in order to initialize both fields toNULL. When no longer necessary, resources held by the sd_bus_error structure should be destroyed withsd_bus_error_free().sd_bus_error_set()sets an error structure to the specified name and message strings. 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 anerrno-like value (seeerrno(3)) determined from the specified error namename. Ifname isNULL, it is assumed that no error occurred, and0is returned. Ifname is nonnull, a negative value is always returned. Ife isNULL, no error structure is initialized, butname is still converted into anerrno-style value. Various well-known D-Bus errors are converted to well-knownerrno counterparts, and the other ones to-EIO. Seesd-bus-errors(3) for a list of well-known error names. Additional error mappings may be defined withsd_bus_error_add_map(3).sd_bus_error_set()is designed to be conveniently used in areturn statement. Ifmessage isNULL, no message is set. This call can fail if no memory may be allocated for the name and message strings, in which case anSD_BUS_ERROR_NO_MEMORYerror will be set instead and-ENOMEMreturned.sd_bus_error_setf()andsd_bus_error_setfv()are similar tosd_bus_error_set(), but take aprintf(3) format string and corresponding arguments to generate themessage field.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()is 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 parametererror is0. Otherwise, it will setname from anerrno-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 forname if 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 specified errorerror is non-zero, the call always returns a negative value, for convenient usage inreturn statements. This call might fail due to lack of memory, in which case anSD_BUS_ERROR_NO_MEMORYerror is set instead, and-ENOMEM is returned.sd_bus_error_set_errnof()andsd_bus_error_set_errnof()are similar tosd_bus_error_set_errno(), but in addition toerror, take aprintf(3) format string and corresponding arguments. Themessage field will be generated fromformat and 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 thename field of an error structure to anerrno-like (positive) value using the same rules assd_bus_error_set(). Ife isNULL,0will be returned.sd_bus_error_copy()will initializedst using the values ine, ife has been set with an error value before. Otherwise, it will return immediately. If the strings ine were set usingsd_bus_error_set_const(), they will be shared. Otherwise, they will be copied. Before this call,dst must be unset, i.e. either freshly initialized withNULLor reset usingsd_bus_error_free().sd_bus_error_copy()generally returns0or a negativeerrno-like value based on the input parametere:0if it was unset and a negative integer if it was set to some error, similarly tosd_bus_error_set(). It may however also return an error generated internally, for example-ENOMEMif a memory allocation fails.sd_bus_error_move()is similar tosd_bus_error_copy(), but will move any error information frome intodst, resetting the former. This function cannot fail, as no new memory is allocated. Note that ife is not set,dst is initialized toSD_BUS_ERROR_NULL. Moreover, ifdst isNULLno operation is executed on it and resources held bye are freed and reset. Returns a convertederrno-like, non-positive error value.sd_bus_error_is_set()will return a non-zero value ife is non-NULLand an error has been set,falseotherwise.sd_bus_error_has_name()will return a non-zero value ife is non-NULLand an error with the samename has been set,false otherwise.sd_bus_error_has_names_sentinel()is 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.
sd_bus_error is not reference-counted. Users should destroy resources held by it by callingsd_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 tofree(3) the memory held by the structure itself after freeing its contents withsd_bus_error_free().
The functionssd_bus_error_set(),sd_bus_error_setf(), andsd_bus_error_set_const()always return0when the specified error value isNULL, and a negative errno-like value corresponding to thename parameter otherwise. The functionssd_bus_error_set_errno(),sd_bus_error_set_errnof()andsd_bus_error_set_errnofv(), return0when the specified error value is0, and a negative errno-like value corresponding to theerror parameter otherwise. If an error occurs internally, one of the negative error values listed below will be returned. This allows those functions to be conveniently used in areturn statement, see the example below.sd_bus_error_get_errno()returnsfalsewhene isNULL, and a positive errno value mapped frome->name otherwise.sd_bus_error_copy()andsd_bus_error_move()return a negative error value converted from the source error, and zero if the error has not been set. This allows those functions to be conveniently used in areturnstatement, see the example below.sd_bus_error_is_set()returns a non-zero value whene and thename field are non-NULL, zero otherwise.sd_bus_error_has_name(),sd_bus_error_has_names(), andsd_bus_error_has_names_sentinel()return a non-zero value whene is non-NULLand thename field is equal to one of the given names, zero otherwise.Errors Return value may indicate the following problems in the invocation of the function itself:-EINVAL Error was already set in the sd_bus_error structure when one the error-setting functions was called.-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(), andsd_bus_error_move()will return a negative convertederrno-style value, or0if the error parameter isNULLor unset. D-Bus errors are converted to the integralerrno-style value, and the mapping mechanism is extensible, see the discussion above. This effectively means that almost any negativeerrno-style value can be returned.
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)); }Functions described here are available as a shared library, which can be compiled against and linked to with thelibsystemd pkg-config(1) file. 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.
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 version 221.sd_bus_error_move()was added in version 240.sd_bus_error_has_names_sentinel()was added in version 247.sd_bus_error_setfv()was added in version 252.
systemd(1),sd-bus(3),sd-bus-errors(3),sd_bus_error_add_map(3),errno(3),strerror_r(3)
1. Valid D-Bus Nameshttps://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names
This page is part of thesystemd (systemd system and service manager) project. Information about the project can be found at ⟨http://www.freedesktop.org/wiki/Software/systemd⟩. If you have a bug report for this manual page, see ⟨http://www.freedesktop.org/wiki/Software/systemd/#bugreports⟩. This page was obtained from the project's upstream Git repository ⟨https://github.com/systemd/systemd.git⟩ on 2025-08-11. (At that time, the date of the most recent commit that was found in the repository was 2025-08-11.) If you discover any rendering problems in this HTML version of the page, or you believe there is a better or more up-to-date source for the page, or you have corrections or improvements to the information in this COLOPHON (which isnot part of the original manual page), send a mail to man-pages@man7.orgsystemd 258~rc2SD_BUS_ERROR(3)Pages that refer to this page:sd-bus(3), sd_bus_add_object(3), sd_bus_call(3), sd_bus_error_add_map(3), sd-bus-errors(3), sd_bus_message_new_method_error(3), sd_bus_reply_method_error(3), systemd.directives(7), systemd.index(7)
HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |