1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
30 #include "alloc-util.h"
31 #include "bus-error.h"
32 #include "errno-list.h"
33 #include "string-util.h"
36 BUS_ERROR_MAP_ELF_REGISTER const sd_bus_error_map bus_standard_errors[] = {
37 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.Failed", EACCES),
38 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NoMemory", ENOMEM),
39 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.ServiceUnknown", EHOSTUNREACH),
40 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NameHasNoOwner", ENXIO),
41 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NoReply", ETIMEDOUT),
42 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.IOError", EIO),
43 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.BadAddress", EADDRNOTAVAIL),
44 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NotSupported", EOPNOTSUPP),
45 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.LimitsExceeded", ENOBUFS),
46 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.AccessDenied", EACCES),
47 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.AuthFailed", EACCES),
48 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.InteractiveAuthorizationRequired", EACCES),
49 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NoServer", EHOSTDOWN),
50 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.Timeout", ETIMEDOUT),
51 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NoNetwork", ENONET),
52 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.AddressInUse", EADDRINUSE),
53 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.Disconnected", ECONNRESET),
54 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.InvalidArgs", EINVAL),
55 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.FileNotFound", ENOENT),
56 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.FileExists", EEXIST),
57 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.UnknownMethod", EBADR),
58 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.UnknownObject", EBADR),
59 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.UnknownInterface", EBADR),
60 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.UnknownProperty", EBADR),
61 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.PropertyReadOnly", EROFS),
62 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.UnixProcessIdUnknown", ESRCH),
63 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.InvalidSignature", EINVAL),
64 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.InconsistentMessage", EBADMSG),
65 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.TimedOut", ETIMEDOUT),
66 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.MatchRuleInvalid", EINVAL),
67 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.InvalidFileContent", EINVAL),
68 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.MatchRuleNotFound", ENOENT),
69 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.SELinuxSecurityContextUnknown", ESRCH),
70 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.ObjectPathInUse", EBUSY),
74 /* GCC maps this magically to the beginning and end of the BUS_ERROR_MAP section */
75 extern const sd_bus_error_map __start_BUS_ERROR_MAP[];
76 extern const sd_bus_error_map __stop_BUS_ERROR_MAP[];
78 /* Additional maps registered with sd_bus_error_add_map() are in this
79 * NULL terminated array */
80 static const sd_bus_error_map **additional_error_maps = NULL;
82 static int bus_error_name_to_errno(const char *name) {
83 const sd_bus_error_map **map, *m;
90 p = startswith(name, "System.Error.");
92 r = errno_from_name(p);
99 if (additional_error_maps)
100 for (map = additional_error_maps; *map; map++)
101 for (m = *map;; m++) {
102 /* For additional error maps the end marker is actually the end marker */
103 if (m->code == BUS_ERROR_MAP_END_MARKER)
106 if (streq(m->name, name))
110 m = __start_BUS_ERROR_MAP;
111 while (m < __stop_BUS_ERROR_MAP) {
112 /* For magic ELF error maps, the end marker might
113 * appear in the middle of things, since multiple maps
114 * might appear in the same section. Hence, let's skip
115 * over it, but realign the pointer to the next 8 byte
116 * boundary, which is the selected alignment for the
118 if (m->code == BUS_ERROR_MAP_END_MARKER) {
123 if (streq(m->name, name))
132 static sd_bus_error errno_to_bus_error_const(int error) {
140 return BUS_ERROR_OOM;
144 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_ACCESS_DENIED, "Access denied");
147 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INVALID_ARGS, "Invalid argument");
150 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_UNIX_PROCESS_ID_UNKNOWN, "No such process");
153 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_FILE_NOT_FOUND, "File not found");
156 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_FILE_EXISTS, "File exists");
160 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_TIMEOUT, "Timed out");
163 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_IO_ERROR, "Input/output error");
168 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_DISCONNECTED, "Disconnected");
171 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NOT_SUPPORTED, "Not supported");
174 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_BAD_ADDRESS, "Address not available");
177 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_LIMITS_EXCEEDED, "Limits exceeded");
180 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_ADDRESS_IN_USE, "Address in use");
183 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Inconsistent message");
186 return SD_BUS_ERROR_NULL;
189 static int errno_to_bus_error_name_new(int error, char **ret) {
196 name = errno_to_name(error);
200 n = strappend("System.Error.", name);
208 bool bus_error_is_dirty(sd_bus_error *e) {
212 return e->name || e->message || e->_need_free != 0;
215 _public_ void sd_bus_error_free(sd_bus_error *e) {
219 if (e->_need_free > 0) {
220 free((void*) e->name);
221 free((void*) e->message);
224 e->name = e->message = NULL;
228 _public_ int sd_bus_error_set(sd_bus_error *e, const char *name, const char *message) {
235 assert_return(!bus_error_is_dirty(e), -EINVAL);
237 e->name = strdup(name);
244 e->message = strdup(message);
249 return -bus_error_name_to_errno(name);
252 int bus_error_setfv(sd_bus_error *e, const char *name, const char *format, va_list ap) {
258 assert_return(!bus_error_is_dirty(e), -EINVAL);
260 e->name = strdup(name);
266 /* If we hit OOM on formatting the pretty message, we ignore
267 * this, since we at least managed to write the error name */
269 (void) vasprintf((char**) &e->message, format, ap);
274 return -bus_error_name_to_errno(name);
277 _public_ int sd_bus_error_setf(sd_bus_error *e, const char *name, const char *format, ...) {
283 va_start(ap, format);
284 r = bus_error_setfv(e, name, format, ap);
290 return sd_bus_error_set(e, name, NULL);
293 _public_ int sd_bus_error_copy(sd_bus_error *dest, const sd_bus_error *e) {
295 if (!sd_bus_error_is_set(e))
300 assert_return(!bus_error_is_dirty(dest), -EINVAL);
303 * _need_free < 0 indicates that the error is temporarily const, needs deep copying
304 * _need_free == 0 indicates that the error is perpetually const, needs no deep copying
305 * _need_free > 0 indicates that the error is fully dynamic, needs deep copying
308 if (e->_need_free == 0)
311 dest->name = strdup(e->name);
313 *dest = BUS_ERROR_OOM;
318 dest->message = strdup(e->message);
320 dest->_need_free = 1;
324 return -bus_error_name_to_errno(e->name);
327 _public_ int sd_bus_error_set_const(sd_bus_error *e, const char *name, const char *message) {
333 assert_return(!bus_error_is_dirty(e), -EINVAL);
335 *e = SD_BUS_ERROR_MAKE_CONST(name, message);
338 return -bus_error_name_to_errno(name);
341 _public_ int sd_bus_error_is_set(const sd_bus_error *e) {
348 _public_ int sd_bus_error_has_name(const sd_bus_error *e, const char *name) {
352 return streq_ptr(e->name, name);
355 _public_ int sd_bus_error_get_errno(const sd_bus_error* e) {
362 return bus_error_name_to_errno(e->name);
365 static void bus_error_strerror(sd_bus_error *e, int error) {
379 x = strerror_r(error, m, k);
380 if (errno == ERANGE || strlen(x) >= k - 1) {
392 if (e->_need_free > 0) {
393 /* Error is already dynamic, let's just update the message */
394 free((char*) e->message);
399 /* Error was const so far, let's make it dynamic, if we can */
414 if (e->_need_free > 0) {
417 /* Error is dynamic, let's hence make the message also dynamic */
422 free((char*) e->message);
425 /* Error is const, hence we can just override */
434 _public_ int sd_bus_error_set_errno(sd_bus_error *e, int error) {
444 assert_return(!bus_error_is_dirty(e), -EINVAL);
446 /* First, try a const translation */
447 *e = errno_to_bus_error_const(error);
449 if (!sd_bus_error_is_set(e)) {
452 /* If that didn't work, try a dynamic one. */
454 k = errno_to_bus_error_name_new(error, (char**) &e->name);
461 *e = BUS_ERROR_FAILED;
464 /* Now, fill in the message from strerror() if we can */
465 bus_error_strerror(e, error);
469 _public_ int sd_bus_error_set_errnofv(sd_bus_error *e, int error, const char *format, va_list ap) {
481 assert_return(!bus_error_is_dirty(e), -EINVAL);
483 /* First, try a const translation */
484 *e = errno_to_bus_error_const(error);
486 if (!sd_bus_error_is_set(e)) {
489 /* If that didn't work, try a dynamic one */
491 k = errno_to_bus_error_name_new(error, (char**) &e->name);
498 *e = BUS_ERROR_FAILED;
504 /* Then, let's try to fill in the supplied message */
506 errno = error; /* Make sure that %m resolves to the specified error */
507 r = vasprintf(&m, format, ap);
510 if (e->_need_free <= 0) {
523 free((char*) e->message);
530 /* If that didn't work, use strerror() for the message */
531 bus_error_strerror(e, error);
535 _public_ int sd_bus_error_set_errnof(sd_bus_error *e, int error, const char *format, ...) {
546 assert_return(!bus_error_is_dirty(e), -EINVAL);
551 va_start(ap, format);
552 r = sd_bus_error_set_errnofv(e, error, format, ap);
558 return sd_bus_error_set_errno(e, error);
561 const char *bus_error_message(const sd_bus_error *e, int error) {
564 /* Sometimes, the D-Bus server is a little bit too verbose with
565 * its error messages, so let's override them here */
566 if (sd_bus_error_has_name(e, SD_BUS_ERROR_ACCESS_DENIED))
567 return "Access denied";
576 return strerror(error);
579 static bool map_ok(const sd_bus_error_map *map) {
580 for (; map->code != BUS_ERROR_MAP_END_MARKER; map++)
581 if (!map->name || map->code <=0)
586 _public_ int sd_bus_error_add_map(const sd_bus_error_map *map) {
587 const sd_bus_error_map **maps = NULL;
590 assert_return(map, -EINVAL);
591 assert_return(map_ok(map), -EINVAL);
593 if (additional_error_maps)
594 for (; additional_error_maps[n] != NULL; n++)
595 if (additional_error_maps[n] == map)
598 maps = realloc_multiply(additional_error_maps, sizeof(struct sd_bus_error_map*), n + 2);
605 additional_error_maps = maps;