]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-bus/bus-error.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / libsystemd / sd-bus / bus-error.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <stdarg.h>
5 #include <stdbool.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 #include "sd-bus.h"
11
12 #include "alloc-util.h"
13 #include "bus-error.h"
14 #include "errno-list.h"
15 #include "string-util.h"
16 #include "util.h"
17
18 BUS_ERROR_MAP_ELF_REGISTER const sd_bus_error_map bus_standard_errors[] = {
19 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.Failed", EACCES),
20 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NoMemory", ENOMEM),
21 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.ServiceUnknown", EHOSTUNREACH),
22 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NameHasNoOwner", ENXIO),
23 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NoReply", ETIMEDOUT),
24 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.IOError", EIO),
25 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.BadAddress", EADDRNOTAVAIL),
26 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NotSupported", EOPNOTSUPP),
27 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.LimitsExceeded", ENOBUFS),
28 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.AccessDenied", EACCES),
29 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.AuthFailed", EACCES),
30 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.InteractiveAuthorizationRequired", EACCES),
31 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NoServer", EHOSTDOWN),
32 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.Timeout", ETIMEDOUT),
33 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NoNetwork", ENONET),
34 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.AddressInUse", EADDRINUSE),
35 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.Disconnected", ECONNRESET),
36 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.InvalidArgs", EINVAL),
37 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.FileNotFound", ENOENT),
38 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.FileExists", EEXIST),
39 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.UnknownMethod", EBADR),
40 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.UnknownObject", EBADR),
41 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.UnknownInterface", EBADR),
42 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.UnknownProperty", EBADR),
43 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.PropertyReadOnly", EROFS),
44 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.UnixProcessIdUnknown", ESRCH),
45 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.InvalidSignature", EINVAL),
46 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.InconsistentMessage", EBADMSG),
47 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.TimedOut", ETIMEDOUT),
48 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.MatchRuleInvalid", EINVAL),
49 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.InvalidFileContent", EINVAL),
50 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.MatchRuleNotFound", ENOENT),
51 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.SELinuxSecurityContextUnknown", ESRCH),
52 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.ObjectPathInUse", EBUSY),
53 SD_BUS_ERROR_MAP_END
54 };
55
56 /* GCC maps this magically to the beginning and end of the BUS_ERROR_MAP section */
57 extern const sd_bus_error_map __start_BUS_ERROR_MAP[];
58 extern const sd_bus_error_map __stop_BUS_ERROR_MAP[];
59
60 /* Additional maps registered with sd_bus_error_add_map() are in this
61 * NULL terminated array */
62 static const sd_bus_error_map **additional_error_maps = NULL;
63
64 static int bus_error_name_to_errno(const char *name) {
65 const sd_bus_error_map **map, *m;
66 const char *p;
67 int r;
68
69 if (!name)
70 return EINVAL;
71
72 p = startswith(name, "System.Error.");
73 if (p) {
74 r = errno_from_name(p);
75 if (r < 0)
76 return EIO;
77
78 return r;
79 }
80
81 if (additional_error_maps)
82 for (map = additional_error_maps; *map; map++)
83 for (m = *map;; m++) {
84 /* For additional error maps the end marker is actually the end marker */
85 if (m->code == BUS_ERROR_MAP_END_MARKER)
86 break;
87
88 if (streq(m->name, name))
89 return m->code;
90 }
91
92 m = __start_BUS_ERROR_MAP;
93 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
94 while (m < __stop_BUS_ERROR_MAP) {
95 /* For magic ELF error maps, the end marker might
96 * appear in the middle of things, since multiple maps
97 * might appear in the same section. Hence, let's skip
98 * over it, but realign the pointer to the next 8 byte
99 * boundary, which is the selected alignment for the
100 * arrays. */
101 if (m->code == BUS_ERROR_MAP_END_MARKER) {
102 m = ALIGN8_PTR(m+1);
103 continue;
104 }
105
106 if (streq(m->name, name))
107 return m->code;
108
109 m++;
110 }
111 #endif
112
113 return EIO;
114 }
115
116 static sd_bus_error errno_to_bus_error_const(int error) {
117
118 if (error < 0)
119 error = -error;
120
121 switch (error) {
122
123 case ENOMEM:
124 return BUS_ERROR_OOM;
125
126 case EPERM:
127 case EACCES:
128 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_ACCESS_DENIED, "Access denied");
129
130 case EINVAL:
131 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INVALID_ARGS, "Invalid argument");
132
133 case ESRCH:
134 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_UNIX_PROCESS_ID_UNKNOWN, "No such process");
135
136 case ENOENT:
137 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_FILE_NOT_FOUND, "File not found");
138
139 case EEXIST:
140 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_FILE_EXISTS, "File exists");
141
142 case ETIMEDOUT:
143 case ETIME:
144 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_TIMEOUT, "Timed out");
145
146 case EIO:
147 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_IO_ERROR, "Input/output error");
148
149 case ENETRESET:
150 case ECONNABORTED:
151 case ECONNRESET:
152 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_DISCONNECTED, "Disconnected");
153
154 case EOPNOTSUPP:
155 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NOT_SUPPORTED, "Not supported");
156
157 case EADDRNOTAVAIL:
158 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_BAD_ADDRESS, "Address not available");
159
160 case ENOBUFS:
161 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_LIMITS_EXCEEDED, "Limits exceeded");
162
163 case EADDRINUSE:
164 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_ADDRESS_IN_USE, "Address in use");
165
166 case EBADMSG:
167 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Inconsistent message");
168 }
169
170 return SD_BUS_ERROR_NULL;
171 }
172
173 static int errno_to_bus_error_name_new(int error, char **ret) {
174 const char *name;
175 char *n;
176
177 if (error < 0)
178 error = -error;
179
180 name = errno_to_name(error);
181 if (!name)
182 return 0;
183
184 n = strappend("System.Error.", name);
185 if (!n)
186 return -ENOMEM;
187
188 *ret = n;
189 return 1;
190 }
191
192 bool bus_error_is_dirty(sd_bus_error *e) {
193 if (!e)
194 return false;
195
196 return e->name || e->message || e->_need_free != 0;
197 }
198
199 _public_ void sd_bus_error_free(sd_bus_error *e) {
200 if (!e)
201 return;
202
203 if (e->_need_free > 0) {
204 free((void*) e->name);
205 free((void*) e->message);
206 }
207
208 *e = SD_BUS_ERROR_NULL;
209 }
210
211 _public_ int sd_bus_error_set(sd_bus_error *e, const char *name, const char *message) {
212
213 if (!name)
214 return 0;
215 if (!e)
216 goto finish;
217
218 assert_return(!bus_error_is_dirty(e), -EINVAL);
219
220 e->name = strdup(name);
221 if (!e->name) {
222 *e = BUS_ERROR_OOM;
223 return -ENOMEM;
224 }
225
226 if (message)
227 e->message = strdup(message);
228
229 e->_need_free = 1;
230
231 finish:
232 return -bus_error_name_to_errno(name);
233 }
234
235 int bus_error_setfv(sd_bus_error *e, const char *name, const char *format, va_list ap) {
236
237 if (!name)
238 return 0;
239
240 if (e) {
241 assert_return(!bus_error_is_dirty(e), -EINVAL);
242
243 e->name = strdup(name);
244 if (!e->name) {
245 *e = BUS_ERROR_OOM;
246 return -ENOMEM;
247 }
248
249 /* If we hit OOM on formatting the pretty message, we ignore
250 * this, since we at least managed to write the error name */
251 if (format)
252 (void) vasprintf((char**) &e->message, format, ap);
253
254 e->_need_free = 1;
255 }
256
257 return -bus_error_name_to_errno(name);
258 }
259
260 _public_ int sd_bus_error_setf(sd_bus_error *e, const char *name, const char *format, ...) {
261
262 if (format) {
263 int r;
264 va_list ap;
265
266 va_start(ap, format);
267 r = bus_error_setfv(e, name, format, ap);
268 va_end(ap);
269
270 return r;
271 }
272
273 return sd_bus_error_set(e, name, NULL);
274 }
275
276 _public_ int sd_bus_error_copy(sd_bus_error *dest, const sd_bus_error *e) {
277
278 if (!sd_bus_error_is_set(e))
279 return 0;
280 if (!dest)
281 goto finish;
282
283 assert_return(!bus_error_is_dirty(dest), -EINVAL);
284
285 /*
286 * _need_free < 0 indicates that the error is temporarily const, needs deep copying
287 * _need_free == 0 indicates that the error is perpetually const, needs no deep copying
288 * _need_free > 0 indicates that the error is fully dynamic, needs deep copying
289 */
290
291 if (e->_need_free == 0)
292 *dest = *e;
293 else {
294 dest->name = strdup(e->name);
295 if (!dest->name) {
296 *dest = BUS_ERROR_OOM;
297 return -ENOMEM;
298 }
299
300 if (e->message)
301 dest->message = strdup(e->message);
302
303 dest->_need_free = 1;
304 }
305
306 finish:
307 return -bus_error_name_to_errno(e->name);
308 }
309
310 _public_ int sd_bus_error_move(sd_bus_error *dest, sd_bus_error *e) {
311 int r;
312
313 if (!sd_bus_error_is_set(e)) {
314
315 if (dest)
316 *dest = SD_BUS_ERROR_NULL;
317
318 return 0;
319 }
320
321 r = -bus_error_name_to_errno(e->name);
322
323 if (dest) {
324 *dest = *e;
325 *e = SD_BUS_ERROR_NULL;
326 } else
327 sd_bus_error_free(e);
328
329 return r;
330 }
331
332 _public_ int sd_bus_error_set_const(sd_bus_error *e, const char *name, const char *message) {
333 if (!name)
334 return 0;
335 if (!e)
336 goto finish;
337
338 assert_return(!bus_error_is_dirty(e), -EINVAL);
339
340 *e = SD_BUS_ERROR_MAKE_CONST(name, message);
341
342 finish:
343 return -bus_error_name_to_errno(name);
344 }
345
346 _public_ int sd_bus_error_is_set(const sd_bus_error *e) {
347 if (!e)
348 return 0;
349
350 return !!e->name;
351 }
352
353 _public_ int sd_bus_error_has_name(const sd_bus_error *e, const char *name) {
354 if (!e)
355 return 0;
356
357 return streq_ptr(e->name, name);
358 }
359
360 _public_ int sd_bus_error_get_errno(const sd_bus_error* e) {
361 if (!e)
362 return 0;
363
364 if (!e->name)
365 return 0;
366
367 return bus_error_name_to_errno(e->name);
368 }
369
370 static void bus_error_strerror(sd_bus_error *e, int error) {
371 size_t k = 64;
372 char *m;
373
374 assert(e);
375
376 for (;;) {
377 char *x;
378
379 m = new(char, k);
380 if (!m)
381 return;
382
383 errno = 0;
384 x = strerror_r(error, m, k);
385 if (errno == ERANGE || strlen(x) >= k - 1) {
386 free(m);
387 k *= 2;
388 continue;
389 }
390
391 if (errno) {
392 free(m);
393 return;
394 }
395
396 if (x == m) {
397 if (e->_need_free > 0) {
398 /* Error is already dynamic, let's just update the message */
399 free((char*) e->message);
400 e->message = x;
401
402 } else {
403 char *t;
404 /* Error was const so far, let's make it dynamic, if we can */
405
406 t = strdup(e->name);
407 if (!t) {
408 free(m);
409 return;
410 }
411
412 e->_need_free = 1;
413 e->name = t;
414 e->message = x;
415 }
416 } else {
417 free(m);
418
419 if (e->_need_free > 0) {
420 char *t;
421
422 /* Error is dynamic, let's hence make the message also dynamic */
423 t = strdup(x);
424 if (!t)
425 return;
426
427 free((char*) e->message);
428 e->message = t;
429 } else {
430 /* Error is const, hence we can just override */
431 e->message = x;
432 }
433 }
434
435 return;
436 }
437 }
438
439 _public_ int sd_bus_error_set_errno(sd_bus_error *e, int error) {
440
441 if (error < 0)
442 error = -error;
443
444 if (!e)
445 return -error;
446 if (error == 0)
447 return -error;
448
449 assert_return(!bus_error_is_dirty(e), -EINVAL);
450
451 /* First, try a const translation */
452 *e = errno_to_bus_error_const(error);
453
454 if (!sd_bus_error_is_set(e)) {
455 int k;
456
457 /* If that didn't work, try a dynamic one. */
458
459 k = errno_to_bus_error_name_new(error, (char**) &e->name);
460 if (k > 0)
461 e->_need_free = 1;
462 else if (k < 0) {
463 *e = BUS_ERROR_OOM;
464 return -error;
465 } else
466 *e = BUS_ERROR_FAILED;
467 }
468
469 /* Now, fill in the message from strerror() if we can */
470 bus_error_strerror(e, error);
471 return -error;
472 }
473
474 _public_ int sd_bus_error_set_errnofv(sd_bus_error *e, int error, const char *format, va_list ap) {
475 PROTECT_ERRNO;
476 int r;
477
478 if (error < 0)
479 error = -error;
480
481 if (!e)
482 return -error;
483 if (error == 0)
484 return 0;
485
486 assert_return(!bus_error_is_dirty(e), -EINVAL);
487
488 /* First, try a const translation */
489 *e = errno_to_bus_error_const(error);
490
491 if (!sd_bus_error_is_set(e)) {
492 int k;
493
494 /* If that didn't work, try a dynamic one */
495
496 k = errno_to_bus_error_name_new(error, (char**) &e->name);
497 if (k > 0)
498 e->_need_free = 1;
499 else if (k < 0) {
500 *e = BUS_ERROR_OOM;
501 return -ENOMEM;
502 } else
503 *e = BUS_ERROR_FAILED;
504 }
505
506 if (format) {
507 char *m;
508
509 /* Then, let's try to fill in the supplied message */
510
511 errno = error; /* Make sure that %m resolves to the specified error */
512 r = vasprintf(&m, format, ap);
513 if (r >= 0) {
514
515 if (e->_need_free <= 0) {
516 char *t;
517
518 t = strdup(e->name);
519 if (t) {
520 e->_need_free = 1;
521 e->name = t;
522 e->message = m;
523 return -error;
524 }
525
526 free(m);
527 } else {
528 free((char*) e->message);
529 e->message = m;
530 return -error;
531 }
532 }
533 }
534
535 /* If that didn't work, use strerror() for the message */
536 bus_error_strerror(e, error);
537 return -error;
538 }
539
540 _public_ int sd_bus_error_set_errnof(sd_bus_error *e, int error, const char *format, ...) {
541 int r;
542
543 if (error < 0)
544 error = -error;
545
546 if (!e)
547 return -error;
548 if (error == 0)
549 return 0;
550
551 assert_return(!bus_error_is_dirty(e), -EINVAL);
552
553 if (format) {
554 va_list ap;
555
556 va_start(ap, format);
557 r = sd_bus_error_set_errnofv(e, error, format, ap);
558 va_end(ap);
559
560 return r;
561 }
562
563 return sd_bus_error_set_errno(e, error);
564 }
565
566 const char *bus_error_message(const sd_bus_error *e, int error) {
567
568 if (e) {
569 /* Sometimes, the D-Bus server is a little bit too verbose with
570 * its error messages, so let's override them here */
571 if (sd_bus_error_has_name(e, SD_BUS_ERROR_ACCESS_DENIED))
572 return "Access denied";
573
574 if (e->message)
575 return e->message;
576 }
577
578 if (error < 0)
579 error = -error;
580
581 return strerror(error);
582 }
583
584 static bool map_ok(const sd_bus_error_map *map) {
585 for (; map->code != BUS_ERROR_MAP_END_MARKER; map++)
586 if (!map->name || map->code <=0)
587 return false;
588 return true;
589 }
590
591 _public_ int sd_bus_error_add_map(const sd_bus_error_map *map) {
592 const sd_bus_error_map **maps = NULL;
593 unsigned n = 0;
594
595 assert_return(map, -EINVAL);
596 assert_return(map_ok(map), -EINVAL);
597
598 if (additional_error_maps)
599 for (; additional_error_maps[n] != NULL; n++)
600 if (additional_error_maps[n] == map)
601 return 0;
602
603 maps = reallocarray(additional_error_maps, n + 2, sizeof(struct sd_bus_error_map*));
604 if (!maps)
605 return -ENOMEM;
606
607 maps[n] = map;
608 maps[n+1] = NULL;
609
610 additional_error_maps = maps;
611 return 1;
612 }