1SD_BUS_ERROR(3) sd_bus_error SD_BUS_ERROR(3)
2
3
4
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_setfv, 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
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_setfv(sd_bus_error *e, const char *name,
37 const char *format, va_list ap);
38
39 int sd_bus_error_set_const(sd_bus_error *e, const char *name,
40 const char *message);
41
42 int sd_bus_error_set_errno(sd_bus_error *e, int error);
43
44 int sd_bus_error_set_errnof(sd_bus_error *e, int error,
45 const char *format, ...);
46
47 int sd_bus_error_set_errnofv(sd_bus_error *e, int error,
48 const char *format, va_list ap);
49
50 int sd_bus_error_get_errno(const sd_bus_error *e);
51
52 int sd_bus_error_copy(sd_bus_error *dst, const sd_bus_error *e);
53
54 int sd_bus_error_move(sd_bus_error *dst, sd_bus_error *e);
55
56 int sd_bus_error_is_set(const sd_bus_error *e);
57
58 int sd_bus_error_has_name(const sd_bus_error *e, const char *name);
59
60 int sd_bus_error_has_names_sentinel(const sd_bus_error *e, ...);
61
62 #define sd_bus_error_has_names(e, ...)
63 sd_bus_error_has_names_sentinel(e, ..., NULL)
64
66 The sd_bus_error structure carries information about a D-Bus error
67 condition, or lack thereof. The functions described below may be used
68 to set and query fields in this structure.
69
70 • The name field contains a short identifier of an error. It should
71 follow the rules for error names described in the D-Bus
72 specification, subsection Valid Names[1]. A number of common,
73 standardized error names are described in sd-bus-errors(3), but
74 additional domain-specific errors may be defined by applications.
75
76 • The message field usually contains a human-readable string
77 describing the details, but might be NULL.
78
79 An unset sd_bus_error structure should have both fields initialized to
80 NULL, and signifies lack of an error, i.e. success. Assign
81 SD_BUS_ERROR_NULL to the structure in order to initialize both fields
82 to NULL. When no longer necessary, resources held by the sd_bus_error
83 structure should be destroyed with sd_bus_error_free().
84
85 sd_bus_error_set() sets an error structure to the specified name and
86 message strings. The strings will be copied into internal, newly
87 allocated memory. It is essential to free the contents again when they
88 are not required anymore (see above). Do not use this call on error
89 structures that have already been set. If you intend to reuse an error
90 structure, free the old data stored in it with sd_bus_error_free()
91 first.
92
93 sd_bus_error_set() will return an errno-like value (see errno(3))
94 determined from the specified error name name. If name is NULL, it is
95 assumed that no error occurred, and 0 is returned. If name is nonnull,
96 a negative value is always returned. If e is NULL, no error structure
97 is initialized, but name is still converted into an errno-style value.
98
99 Various well-known D-Bus errors are converted to well-known errno
100 counterparts, and the other ones to -EIO. See sd-bus-errors(3) for a
101 list of well-known error names. Additional error mappings may be
102 defined with sd_bus_error_add_map(3).
103
104 sd_bus_error_set() is designed to be conveniently used in a return
105 statement. If message is NULL, no message is set. This call can fail if
106 no memory may be allocated for the name and message strings, in which
107 case an SD_BUS_ERROR_NO_MEMORY error will be set instead and -ENOMEM
108 returned.
109
110 sd_bus_error_setf() and sd_bus_error_setfv() are similar to
111 sd_bus_error_set(), but take a printf(3) format string and
112 corresponding arguments to generate the message field.
113 sd_bus_error_setf() uses variadic arguments, and sd_bus_error_setfv()
114 accepts the arguments as a va_arg(3) parameter list.
115
116 sd_bus_error_set_const() is similar to sd_bus_error_set(), but the
117 string parameters are not copied internally, and must hence remain
118 constant and valid for the lifetime of e. Use this call to avoid memory
119 allocations when setting error structures. Since this call does not
120 allocate memory, it will not fail with an out-of-memory condition as
121 sd_bus_error_set() may, as described above. Alternatively, the
122 SD_BUS_ERROR_MAKE_CONST() macro may be used to generate a literal,
123 constant bus error structure on-the-fly.
124
125 sd_bus_error_set_errno() will immediately return 0 if the specified
126 error parameter error is 0. Otherwise, it will set name from an
127 errno-like value that is converted to a D-Bus error. strerror_r(3)
128 will be used to set message. Well-known D-Bus error names will be used
129 for name if applicable, otherwise a name in the "System.Error."
130 namespace will be generated. The sign of the specified error number is
131 ignored and the absolute value is used implicitly. If the specified
132 error error is non-zero, the call always returns a negative value, for
133 convenient usage in return statements. This call might fail due to lack
134 of memory, in which case an SD_BUS_ERROR_NO_MEMORY error is set
135 instead, and -ENOMEM is returned.
136
137 sd_bus_error_set_errnof() and sd_bus_error_set_errnof() are similar to
138 sd_bus_error_set_errno(), but in addition to error, take a printf(3)
139 format string and corresponding arguments. The message field will be
140 generated from format and the arguments. sd_bus_error_set_errnof()
141 uses variadic arguments, and sd_bus_error_set_errnofv() accepts the
142 arguments as a va_arg(3) parameter list.
143
144 sd_bus_error_get_errno() converts the name field of an error structure
145 to an errno-like (positive) value using the same rules as
146 sd_bus_error_set(). If e is NULL, 0 will be returned.
147
148 sd_bus_error_copy() will initialize dst using the values in e, if e has
149 been set with an error value before. Otherwise, it will return
150 immediately. If the strings in e were set using
151 sd_bus_error_set_const(), they will be shared. Otherwise, they will be
152 copied. Before this call, dst must be unset, i.e. either freshly
153 initialized with NULL or reset using sd_bus_error_free().
154
155 sd_bus_error_copy() generally returns 0 or a negative errno-like value
156 based on the input parameter e: 0 if it was unset and a negative
157 integer if it was set to some error, similarly to sd_bus_error_set().
158 It may however also return an error generated internally, for example
159 -ENOMEM if a memory allocation fails.
160
161 sd_bus_error_move() is similar to sd_bus_error_copy(), but will move
162 any error information from e into dst, resetting the former. This
163 function cannot fail, as no new memory is allocated. Note that if e is
164 not set, dst is initialized to SD_BUS_ERROR_NULL. Moreover, if dst is
165 NULL no operation is executed on it and resources held by e are freed
166 and reset. Returns a converted errno-like, non-positive error value.
167
168 sd_bus_error_is_set() will return a non-zero value if e is non-NULL and
169 an error has been set, false otherwise.
170
171 sd_bus_error_has_name() will return a non-zero value if e is non-NULL
172 and an error with the same name has been set, false otherwise.
173
174 sd_bus_error_has_names_sentinel() is similar to
175 sd_bus_error_has_name(), but takes multiple names to check against. The
176 list must be terminated with NULL. sd_bus_error_has_names() is a macro
177 wrapper around sd_bus_error_has_names_sentinel() that adds the NULL
178 sentinel automatically.
179
180 sd_bus_error_free() will destroy resources held by e. The parameter
181 itself will not be deallocated, and must be free(3)d by the caller if
182 necessary. The function may also be called safely on unset errors
183 (error structures with both fields set to NULL), in which case it
184 performs no operation. This call will reset the error structure after
185 freeing the data, so that all fields are set to NULL. The structure may
186 be reused afterwards.
187
189 sd_bus_error is not reference-counted. Users should destroy resources
190 held by it by calling sd_bus_error_free(). Usually, error structures
191 are allocated on the stack or passed in as function parameters, but
192 they may also be allocated dynamically, in which case it is the duty of
193 the caller to free(3) the memory held by the structure itself after
194 freeing its contents with sd_bus_error_free().
195
197 The functions sd_bus_error_set(), sd_bus_error_setf(), and
198 sd_bus_error_set_const() always return 0 when the specified error value
199 is NULL, and a negative errno-like value corresponding to the name
200 parameter otherwise. The functions sd_bus_error_set_errno(),
201 sd_bus_error_set_errnof() and sd_bus_error_set_errnofv(), return 0 when
202 the specified error value is 0, and a negative errno-like value
203 corresponding to the error parameter otherwise. If an error occurs
204 internally, one of the negative error values listed below will be
205 returned. This allows those functions to be conveniently used in a
206 return statement, see the example below.
207
208 sd_bus_error_get_errno() returns false when e is NULL, and a positive
209 errno value mapped from e->name otherwise.
210
211 sd_bus_error_copy() and sd_bus_error_move() return a negative error
212 value converted from the source error, and zero if the error has not
213 been set. This allows those functions to be conveniently used in a
214 return statement, see the example below.
215
216 sd_bus_error_is_set() returns a non-zero value when e and the name
217 field are non-NULL, zero otherwise.
218
219 sd_bus_error_has_name(), sd_bus_error_has_names(), and
220 sd_bus_error_has_names_sentinel() return a non-zero value when e is
221 non-NULL and the name field is equal to one of the given names, zero
222 otherwise.
223
224 Errors
225 Return value may indicate the following problems in the invocation of
226 the function itself:
227
228 -EINVAL
229 Error was already set in the sd_bus_error structure when one the
230 error-setting functions was called.
231
232 -ENOMEM
233 Memory allocation failed.
234
235 On success, sd_bus_error_set(), sd_bus_error_setf(),
236 sd_bus_error_set_const(), sd_bus_error_set_errno(),
237 sd_bus_error_set_errnof(), sd_bus_error_set_errnofv(),
238 sd_bus_error_copy(), and sd_bus_error_move() will return a negative
239 converted errno-style value, or 0 if the error parameter is NULL or
240 unset. D-Bus errors are converted to the integral errno-style value,
241 and the mapping mechanism is extensible, see the discussion above. This
242 effectively means that almost any negative errno-style value can be
243 returned.
244
246 Example 1. Using the negative return value to propagate an error
247
248 /* SPDX-License-Identifier: MIT-0 */
249
250 #include <errno.h>
251 #include <string.h>
252 #include <unistd.h>
253 #include <sd-bus.h>
254
255 int writer_with_negative_errno_return(int fd, sd_bus_error *error) {
256 const char *message = "Hello, World!\n";
257
258 ssize_t n = write(fd, message, strlen(message));
259 if (n >= 0)
260 return n; /* On success, return the number of bytes written, possibly 0. */
261
262 /* On error, initialize the error structure, and also propagate the errno
263 * value that write(2) set for us. */
264 return sd_bus_error_set_errnof(error, errno, "Failed to write to fd %i: %m", fd);
265 }
266
268 These APIs are implemented as a shared library, which can be compiled
269 and linked to with the libsystemd pkg-config(1) file.
270
272 systemd(1), sd-bus(3), sd-bus-errors(3), sd_bus_error_add_map(3),
273 errno(3), strerror_r(3)
274
276 1. Valid Names
277 https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names
278
279
280
281systemd 253 SD_BUS_ERROR(3)