2 This file is part of systemd.
4 Copyright 2013 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "alloc-util.h"
30 #include "bus-error.h"
31 #include "errno-list.h"
32 #include "string-util.h"
35 BUS_ERROR_MAP_ELF_REGISTER const sd_bus_error_map bus_standard_errors[] = {
36 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.Failed", EACCES),
37 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NoMemory", ENOMEM),
38 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.ServiceUnknown", EHOSTUNREACH),
39 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NameHasNoOwner", ENXIO),
40 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NoReply", ETIMEDOUT),
41 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.IOError", EIO),
42 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.BadAddress", EADDRNOTAVAIL),
43 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NotSupported", EOPNOTSUPP),
44 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.LimitsExceeded", ENOBUFS),
45 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.AccessDenied", EACCES),
46 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.AuthFailed", EACCES),
47 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.InteractiveAuthorizationRequired", EACCES),
48 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NoServer", EHOSTDOWN),
49 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.Timeout", ETIMEDOUT),
50 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NoNetwork", ENONET),
51 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.AddressInUse", EADDRINUSE),
52 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.Disconnected", ECONNRESET),
53 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.InvalidArgs", EINVAL),
54 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.FileNotFound", ENOENT),
55 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.FileExists", EEXIST),
56 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.UnknownMethod", EBADR),
57 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.UnknownObject", EBADR),
58 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.UnknownInterface", EBADR),
59 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.UnknownProperty", EBADR),
60 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.PropertyReadOnly", EROFS),
61 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.UnixProcessIdUnknown", ESRCH),
62 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.InvalidSignature", EINVAL),
63 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.InconsistentMessage", EBADMSG),
64 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.TimedOut", ETIMEDOUT),
65 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.MatchRuleInvalid", EINVAL),
66 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.InvalidFileContent", EINVAL),
67 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.MatchRuleNotFound", ENOENT),
68 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.SELinuxSecurityContextUnknown", ESRCH),
69 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.ObjectPathInUse", EBUSY),
73 /* GCC maps this magically to the beginning and end of the BUS_ERROR_MAP section */
74 extern const sd_bus_error_map __start_BUS_ERROR_MAP[];
75 extern const sd_bus_error_map __stop_BUS_ERROR_MAP[];
77 /* Additional maps registered with sd_bus_error_add_map() are in this
78 * NULL terminated array */
79 static const sd_bus_error_map **additional_error_maps = NULL;
81 static int bus_error_name_to_errno(const char *name) {
82 const sd_bus_error_map **map, *m;
89 p = startswith(name, "System.Error.");
91 r = errno_from_name(p);
98 if (additional_error_maps)
99 for (map = additional_error_maps; *map; map++)
100 for (m = *map;; m++) {
101 /* For additional error maps the end marker is actually the end marker */
102 if (m->code == BUS_ERROR_MAP_END_MARKER)
105 if (streq(m->name, name))
109 m = __start_BUS_ERROR_MAP;
110 while (m < __stop_BUS_ERROR_MAP) {
111 /* For magic ELF error maps, the end marker might
112 * appear in the middle of things, since multiple maps
113 * might appear in the same section. Hence, let's skip
114 * over it, but realign the pointer to the next 8 byte
115 * boundary, which is the selected alignment for the
117 if (m->code == BUS_ERROR_MAP_END_MARKER) {
122 if (streq(m->name, name))
131 static sd_bus_error errno_to_bus_error_const(int error) {
139 return BUS_ERROR_OOM;
143 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_ACCESS_DENIED, "Access denied");
146 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INVALID_ARGS, "Invalid argument");
149 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_UNIX_PROCESS_ID_UNKNOWN, "No such process");
152 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_FILE_NOT_FOUND, "File not found");
155 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_FILE_EXISTS, "File exists");
159 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_TIMEOUT, "Timed out");
162 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_IO_ERROR, "Input/output error");
167 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_DISCONNECTED, "Disconnected");
170 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NOT_SUPPORTED, "Not supported");
173 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_BAD_ADDRESS, "Address not available");
176 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_LIMITS_EXCEEDED, "Limits exceeded");
179 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_ADDRESS_IN_USE, "Address in use");
182 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Inconsistent message");
185 return SD_BUS_ERROR_NULL;
188 static int errno_to_bus_error_name_new(int error, char **ret) {
195 name = errno_to_name(error);
199 n = strappend("System.Error.", name);
207 bool bus_error_is_dirty(sd_bus_error *e) {
211 return e->name || e->message || e->_need_free != 0;
214 _public_ void sd_bus_error_free(sd_bus_error *e) {
218 if (e->_need_free > 0) {
219 free((void*) e->name);
220 free((void*) e->message);
223 e->name = e->message = NULL;
227 _public_ int sd_bus_error_set(sd_bus_error *e, const char *name, const char *message) {
234 assert_return(!bus_error_is_dirty(e), -EINVAL);
236 e->name = strdup(name);
243 e->message = strdup(message);
248 return -bus_error_name_to_errno(name);
251 int bus_error_setfv(sd_bus_error *e, const char *name, const char *format, va_list ap) {
257 assert_return(!bus_error_is_dirty(e), -EINVAL);
259 e->name = strdup(name);
265 /* If we hit OOM on formatting the pretty message, we ignore
266 * this, since we at least managed to write the error name */
268 (void) vasprintf((char**) &e->message, format, ap);
273 return -bus_error_name_to_errno(name);
276 _public_ int sd_bus_error_setf(sd_bus_error *e, const char *name, const char *format, ...) {
282 va_start(ap, format);
283 r = bus_error_setfv(e, name, format, ap);
289 return sd_bus_error_set(e, name, NULL);
292 _public_ int sd_bus_error_copy(sd_bus_error *dest, const sd_bus_error *e) {
294 if (!sd_bus_error_is_set(e))
299 assert_return(!bus_error_is_dirty(dest), -EINVAL);
302 * _need_free < 0 indicates that the error is temporarily const, needs deep copying
303 * _need_free == 0 indicates that the error is perpetually const, needs no deep copying
304 * _need_free > 0 indicates that the error is fully dynamic, needs deep copying
307 if (e->_need_free == 0)
310 dest->name = strdup(e->name);
312 *dest = BUS_ERROR_OOM;
317 dest->message = strdup(e->message);
319 dest->_need_free = 1;
323 return -bus_error_name_to_errno(e->name);
326 _public_ int sd_bus_error_set_const(sd_bus_error *e, const char *name, const char *message) {
332 assert_return(!bus_error_is_dirty(e), -EINVAL);
334 *e = SD_BUS_ERROR_MAKE_CONST(name, message);
337 return -bus_error_name_to_errno(name);
340 _public_ int sd_bus_error_is_set(const sd_bus_error *e) {
347 _public_ int sd_bus_error_has_name(const sd_bus_error *e, const char *name) {
351 return streq_ptr(e->name, name);
354 _public_ int sd_bus_error_get_errno(const sd_bus_error* e) {
361 return bus_error_name_to_errno(e->name);
364 static void bus_error_strerror(sd_bus_error *e, int error) {
378 x = strerror_r(error, m, k);
379 if (errno == ERANGE || strlen(x) >= k - 1) {
391 if (e->_need_free > 0) {
392 /* Error is already dynamic, let's just update the message */
393 free((char*) e->message);
398 /* Error was const so far, let's make it dynamic, if we can */
413 if (e->_need_free > 0) {
416 /* Error is dynamic, let's hence make the message also dynamic */
421 free((char*) e->message);
424 /* Error is const, hence we can just override */
433 _public_ int sd_bus_error_set_errno(sd_bus_error *e, int error) {
443 assert_return(!bus_error_is_dirty(e), -EINVAL);
445 /* First, try a const translation */
446 *e = errno_to_bus_error_const(error);
448 if (!sd_bus_error_is_set(e)) {
451 /* If that didn't work, try a dynamic one. */
453 k = errno_to_bus_error_name_new(error, (char**) &e->name);
460 *e = BUS_ERROR_FAILED;
463 /* Now, fill in the message from strerror() if we can */
464 bus_error_strerror(e, error);
468 _public_ int sd_bus_error_set_errnofv(sd_bus_error *e, int error, const char *format, va_list ap) {
480 assert_return(!bus_error_is_dirty(e), -EINVAL);
482 /* First, try a const translation */
483 *e = errno_to_bus_error_const(error);
485 if (!sd_bus_error_is_set(e)) {
488 /* If that didn't work, try a dynamic one */
490 k = errno_to_bus_error_name_new(error, (char**) &e->name);
497 *e = BUS_ERROR_FAILED;
503 /* Then, let's try to fill in the supplied message */
505 errno = error; /* Make sure that %m resolves to the specified error */
506 r = vasprintf(&m, format, ap);
509 if (e->_need_free <= 0) {
522 free((char*) e->message);
529 /* If that didn't work, use strerror() for the message */
530 bus_error_strerror(e, error);
534 _public_ int sd_bus_error_set_errnof(sd_bus_error *e, int error, const char *format, ...) {
545 assert_return(!bus_error_is_dirty(e), -EINVAL);
550 va_start(ap, format);
551 r = sd_bus_error_set_errnofv(e, error, format, ap);
557 return sd_bus_error_set_errno(e, error);
560 const char *bus_error_message(const sd_bus_error *e, int error) {
563 /* Sometimes, the D-Bus server is a little bit too verbose with
564 * its error messages, so let's override them here */
565 if (sd_bus_error_has_name(e, SD_BUS_ERROR_ACCESS_DENIED))
566 return "Access denied";
575 return strerror(error);
578 static bool map_ok(const sd_bus_error_map *map) {
579 for (; map->code != BUS_ERROR_MAP_END_MARKER; map++)
580 if (!map->name || map->code <=0)
585 _public_ int sd_bus_error_add_map(const sd_bus_error_map *map) {
586 const sd_bus_error_map **maps = NULL;
589 assert_return(map, -EINVAL);
590 assert_return(map_ok(map), -EINVAL);
592 if (additional_error_maps)
593 for (; additional_error_maps[n] != NULL; n++)
594 if (additional_error_maps[n] == map)
597 maps = realloc_multiply(additional_error_maps, sizeof(struct sd_bus_error_map*), n + 2);
604 additional_error_maps = maps;