[systemd-devel] sdbus errors and their underlaying int value: unique?

Lennart Poettering lennart at poettering.net
Wed Mar 3 12:51:38 UTC 2021


On Di, 02.03.21 10:40, Carlo Wood (carlo at alinoe.com) wrote:

> Hello,
>
> thank you for you previous help; I made a lot of progress.
>
> I'm not writing my own C++ wrappers around sbus.
>
> I have the following question:
>
> A sd_bus_error has a name (and a message). The name is usually something
> like "org.freedesktop.DBus.Error.InvalidArgs" or
> "com.alinoe.DBus.Error.test_error" - in other words it contains a
> domain, "org.freedesktop.DBus.Error" and "com.alinoe.DBus.Error"
> respectively.
>
> In C++ we have std::error_code which stores both a (unique) domain
> and an int that is defined within that domain. The integer values
> do not have to be globally unique.
>
> For example, org.freedesktop.DBus.Error.InvalidArgs is mapped to
> EINVAL which is 22. But can I use 22 too for
> com.alinoe.DBus.Error.test_error? This would be fine with
> std::error_code; and I'm trying to support conversion from
> DBus errors to std::error_code.

D-Bus has no concept of error numbers, only of error names (in reverse
domain name notation) and human readable messages.

Since sd-bus is a C wrapper around D-Bus, and in C it is more common
to deal with 'errno' style error codes there's a bit of infrastructure
in place to map names to errors, since quite often it is useful to
propagate errno-style error codes on where one receives a D-Bus error,
and vice versa.

sd_bus_error_get_errno() wraps the error name → errno mapping, and
sd_bus_error_set_errno() can be used for the opposite direction. Note
however, that D-Bus names are a lot more precise, and thus multiple error
names might map to the same error numbers.

sd-bus has a bunch of mappings built-in, for well-known errors from
the D-Bus spec and from systemd. You can define more such mappings via
sd_bus_error_add_map(), to make sd-bus translate things with them too.

Generally: when doing D-Bus focus on the error names, because that's
the native thing. Use the error numbers only really if you really need
to provide C-style errnos instead, and ignore them, and never forget
that the mapping logic is not a D-Bus invention but an sd-bus one.

Lennart

--
Lennart Poettering, Berlin


More information about the systemd-devel mailing list