1SD_BUS_ERROR(3)                  sd_bus_error                  SD_BUS_ERROR(3)
2
3
4

NAME

6       sd_bus_error, SD_BUS_ERROR_MAKE_CONST, SD_BUS_ERROR_NULL,
7       sd_bus_error_free, sd_bus_error_set, sd_bus_error_setf,
8       sd_bus_error_set_const, sd_bus_error_set_errno,
9       sd_bus_error_set_errnof, sd_bus_error_set_errnofv,
10       sd_bus_error_get_errno, sd_bus_error_copy, sd_bus_error_move,
11       sd_bus_error_is_set, sd_bus_error_has_name,
12       sd_bus_error_has_names_sentinel, sd_bus_error_has_names - sd-bus error
13       handling
14

SYNOPSIS

16       #include <systemd/sd-bus.h>
17
18       typedef struct {
19               const char *name;
20               const char *message;
21               ...
22       } sd_bus_error;
23
24       SD_BUS_ERROR_MAKE_CONST(name, message)
25
26       SD_BUS_ERROR_NULL
27
28       void sd_bus_error_free(sd_bus_error *e);
29
30       int sd_bus_error_set(sd_bus_error *e, const char *name,
31                            const char *message);
32
33       int sd_bus_error_setf(sd_bus_error *e, const char *name,
34                             const char *format, ...);
35
36       int sd_bus_error_set_const(sd_bus_error *e, const char *name,
37                                  const char *message);
38
39       int sd_bus_error_set_errno(sd_bus_error *e, int error);
40
41       int sd_bus_error_set_errnof(sd_bus_error *e, int error,
42                                   const char *format, ...);
43
44       int sd_bus_error_set_errnofv(sd_bus_error *e, int error,
45                                    const char *format, va_list ap);
46
47       int sd_bus_error_get_errno(const sd_bus_error *e);
48
49       int sd_bus_error_copy(sd_bus_error *dst, const sd_bus_error *e);
50
51       int sd_bus_error_move(sd_bus_error *dst, sd_bus_error *e);
52
53       int sd_bus_error_is_set(const sd_bus_error *e);
54
55       int sd_bus_error_has_name(const sd_bus_error *e, const char *name);
56
57       int sd_bus_error_has_names_sentinel(const sd_bus_error *e, ...);
58
59       #define sd_bus_error_has_names(e, ...)
60       sd_bus_error_has_names_sentinel(e, ..., NULL)
61

DESCRIPTION

63       The sd_bus_error structure carries information about a D-Bus error
64       condition, or lack thereof. The functions described below may be used
65       to set and query fields in this structure.
66
67       •   The name field contains a short identifier of an error. It should
68           follow the rules for error names described in the D-Bus
69           specification, subsection Valid Names[1]. A number of common,
70           standardized error names are described in sd-bus-errors(3), but
71           additional domain-specific errors may be defined by applications.
72
73       •   The message field usually contains a human-readable string
74           describing the details, but might be NULL.
75
76       An unset sd_bus_error structure should have both fields initialized to
77       NULL, and signifies lack of an error, i.e. success. Assign
78       SD_BUS_ERROR_NULL to the structure in order to initialize both fields
79       to NULL. When no longer necessary, resources held by the sd_bus_error
80       structure should be destroyed with sd_bus_error_free().
81
82       sd_bus_error_set() sets an error structure to the specified name and
83       message strings. The strings will be copied into internal, newly
84       allocated memory. It is essential to free the contents again when they
85       are not required anymore (see above). Do not use this call on error
86       structures that have already been set. If you intend to reuse an error
87       structure, free the old data stored in it with sd_bus_error_free()
88       first.
89
90       sd_bus_error_set() will return an errno-like value (see errno(3))
91       determined from the specified error name name. If name is NULL, it is
92       assumed that no error occurred, and 0 is returned. If name is nonnull,
93       a negative value is always returned. If e is NULL, no error structure
94       is initialized, but name is still converted into an errno-style value.
95
96       Various well-known D-Bus errors are converted to well-known errno
97       counterparts, and the other ones to -EIO. See sd-bus-errors(3) for a
98       list of well-known error names. Additional error mappings may be
99       defined with sd_bus_error_add_map(3).
100
101       sd_bus_error_set() is designed to be conveniently used in a return
102       statement. If message is NULL, no message is set. This call can fail if
103       no memory may be allocated for the name and message strings, in which
104       case an SD_BUS_ERROR_NO_MEMORY error will be set instead and -ENOMEM
105       returned.
106
107       sd_bus_error_setf() is similar to sd_bus_error_set(), but takes a
108       printf(3) format string and corresponding arguments to generate the
109       message field.
110
111       sd_bus_error_set_const() is similar to sd_bus_error_set(), but the
112       string parameters are not copied internally, and must hence remain
113       constant and valid for the lifetime of e. Use this call to avoid memory
114       allocations when setting error structures. Since this call does not
115       allocate memory, it will not fail with an out-of-memory condition as
116       sd_bus_error_set() may, as described above. Alternatively, the
117       SD_BUS_ERROR_MAKE_CONST() macro may be used to generate a literal,
118       constant bus error structure on-the-fly.
119
120       sd_bus_error_set_errno() will immediately return 0 if the specified
121       error parameter error is 0. Otherwise, it will set name from an
122       errno-like value that is converted to a D-Bus error.  strerror_r(3)
123       will be used to set message. Well-known D-Bus error names will be used
124       for name if applicable, otherwise a name in the "System.Error."
125       namespace will be generated. The sign of the specified error number is
126       ignored and the absolute value is used implicitly. If the specified
127       error error is non-zero, the call always returns a negative value, for
128       convenient usage in return statements. This call might fail due to lack
129       of memory, in which case an SD_BUS_ERROR_NO_MEMORY error is set
130       instead, and -ENOMEM is returned.
131
132       sd_bus_error_set_errnof() is similar to sd_bus_error_set_errno(), but
133       in addition to error, takes a printf(3) format string and corresponding
134       arguments. The message field will be generated from format and the
135       arguments.
136
137       sd_bus_error_set_errnofv() is similar to sd_bus_error_set_errnof(), but
138       takes the format string parameters as va_arg(3) parameter list.
139
140       sd_bus_error_get_errno() converts the name field of an error structure
141       to an errno-like (positive) value using the same rules as
142       sd_bus_error_set(). If e is NULL, 0 will be returned.
143
144       sd_bus_error_copy() will initialize dst using the values in e, if e has
145       been set with an error value before. Otherwise, it will return
146       immediately. If the strings in e were set using
147       sd_bus_error_set_const(), they will be shared. Otherwise, they will be
148       copied. Before this call, dst must be unset, i.e. either freshly
149       initialized with NULL or reset using sd_bus_error_free().
150
151       sd_bus_error_copy() generally returns 0 or a negative errno-like value
152       based on the input parameter e: 0 if it was unset and a negative
153       integer if it was set to some error, similarly to sd_bus_error_set().
154       It may however also return an error generated internally, for example
155       -ENOMEM if a memory allocation fails.
156
157       sd_bus_error_move() is similar to sd_bus_error_copy(), but will move
158       any error information from e into dst, resetting the former. This
159       function cannot fail, as no new memory is allocated. Note that if e is
160       not set, dst is initialized to SD_BUS_ERROR_NULL. Moreover, if dst is
161       NULL no operation is executed on it and resources held by e are freed
162       and reset. Returns a converted errno-like, non-positive error value.
163
164       sd_bus_error_is_set() will return a non-zero value if e is non-NULL and
165       an error has been set, false otherwise.
166
167       sd_bus_error_has_name() will return a non-zero value if e is non-NULL
168       and an error with the same name has been set, false otherwise.
169
170       sd_bus_error_has_names_sentinel() is similar to
171       sd_bus_error_has_name(), but takes multiple names to check against. The
172       list must be terminated with NULL.  sd_bus_error_has_names() is a macro
173       wrapper around sd_bus_error_has_names_sentinel() that adds the NULL
174       sentinel automatically.
175
176       sd_bus_error_free() will destroy resources held by e. The parameter
177       itself will not be deallocated, and must be free(3)d by the caller if
178       necessary. The function may also be called safely on unset errors
179       (error structures with both fields set to NULL), in which case it
180       performs no operation. This call will reset the error structure after
181       freeing the data, so that all fields are set to NULL. The structure may
182       be reused afterwards.
183

REFERENCE OWNERSHIP

185       sd_bus_error is not reference-counted. Users should destroy resources
186       held by it by calling sd_bus_error_free(). Usually, error structures
187       are allocated on the stack or passed in as function parameters, but
188       they may also be allocated dynamically, in which case it is the duty of
189       the caller to free(3) the memory held by the structure itself after
190       freeing its contents with sd_bus_error_free().
191

RETURN VALUE

193       The functions sd_bus_error_set(), sd_bus_error_setf(), and
194       sd_bus_error_set_const() always return 0 when the specified error value
195       is NULL, and a negative errno-like value corresponding to the name
196       parameter otherwise. The functions sd_bus_error_set_errno(),
197       sd_bus_error_set_errnof() and sd_bus_error_set_errnofv(), return 0 when
198       the specified error value is 0, and a negative errno-like value
199       corresponding to the error parameter otherwise. If an error occurs
200       internally, one of the negative error values listed below will be
201       returned. This allows those functions to be conveniently used in a
202       return statement, see the example below.
203
204       sd_bus_error_get_errno() returns false when e is NULL, and a positive
205       errno value mapped from e->name otherwise.
206
207       sd_bus_error_copy() and sd_bus_error_move() return a negative error
208       value converted from the source error, and zero if the error has not
209       been set. This allows those functions to be conveniently used in a
210       return statement, see the example below.
211
212       sd_bus_error_is_set() returns a non-zero value when e and the name
213       field are non-NULL, zero otherwise.
214
215       sd_bus_error_has_name(), sd_bus_error_has_names(), and
216       sd_bus_error_has_names_sentinel() return a non-zero value when e is
217       non-NULL and the name field is equal to one of the given names, zero
218       otherwise.
219
220   Errors
221       Return value may indicate the following problems in the invocation of
222       the function itself:
223
224       -EINVAL
225           Error was already set in the sd_bus_error structure when one the
226           error-setting functions was called.
227
228       -ENOMEM
229           Memory allocation failed.
230
231       On success, sd_bus_error_set(), sd_bus_error_setf(),
232       sd_bus_error_set_const(), sd_bus_error_set_errno(),
233       sd_bus_error_set_errnof(), sd_bus_error_set_errnofv(),
234       sd_bus_error_copy(), and sd_bus_error_move() will return a negative
235       converted errno-style value, or 0 if the error parameter is NULL or
236       unset. D-Bus errors are converted to the integral errno-style value,
237       and the mapping mechanism is extensible, see the discussion above. This
238       effectively means that almost any negative errno-style value can be
239       returned.
240

EXAMPLES

242       Example 1. Using the negative return value to propagate an error
243
244           /* SPDX-License-Identifier: CC0-1.0 */
245
246           #include <errno.h>
247           #include <string.h>
248           #include <unistd.h>
249           #include <sd-bus.h>
250
251           int writer_with_negative_errno_return(int fd, sd_bus_error *error) {
252             const char *message = "Hello, World!\n";
253
254             ssize_t n = write(fd, message, strlen(message));
255             if (n >= 0)
256               return n; /* On success, return the number of bytes written, possibly 0. */
257
258             /* On error, initialize the error structure, and also propagate the errno
259              * value that write(2) set for us. */
260             return sd_bus_error_set_errnof(error, errno, "Failed to write to fd %i: %m", fd);
261           }
262

NOTES

264       These APIs are implemented as a shared library, which can be compiled
265       and linked to with the libsystemd pkg-config(1) file.
266

SEE ALSO

268       systemd(1), sd-bus(3), sd-bus-errors(3), sd_bus_error_add_map(3),
269       errno(3), strerror_r(3)
270

NOTES

272        1. Valid Names
273           http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names
274
275
276
277systemd 251                                                    SD_BUS_ERROR(3)
Impressum