1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
31 #include "alloc-util.h"
32 #include "bus-error.h"
33 #include "errno-list.h"
34 #include "string-util.h"
37 BUS_ERROR_MAP_ELF_REGISTER
const sd_bus_error_map bus_standard_errors
[] = {
38 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.Failed", EACCES
),
39 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NoMemory", ENOMEM
),
40 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.ServiceUnknown", EHOSTUNREACH
),
41 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NameHasNoOwner", ENXIO
),
42 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NoReply", ETIMEDOUT
),
43 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.IOError", EIO
),
44 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.BadAddress", EADDRNOTAVAIL
),
45 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NotSupported", EOPNOTSUPP
),
46 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.LimitsExceeded", ENOBUFS
),
47 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.AccessDenied", EACCES
),
48 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.AuthFailed", EACCES
),
49 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.InteractiveAuthorizationRequired", EACCES
),
50 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NoServer", EHOSTDOWN
),
51 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.Timeout", ETIMEDOUT
),
52 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NoNetwork", ENONET
),
53 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.AddressInUse", EADDRINUSE
),
54 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.Disconnected", ECONNRESET
),
55 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.InvalidArgs", EINVAL
),
56 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.FileNotFound", ENOENT
),
57 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.FileExists", EEXIST
),
58 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.UnknownMethod", EBADR
),
59 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.UnknownObject", EBADR
),
60 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.UnknownInterface", EBADR
),
61 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.UnknownProperty", EBADR
),
62 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.PropertyReadOnly", EROFS
),
63 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.UnixProcessIdUnknown", ESRCH
),
64 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.InvalidSignature", EINVAL
),
65 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.InconsistentMessage", EBADMSG
),
66 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.TimedOut", ETIMEDOUT
),
67 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.MatchRuleInvalid", EINVAL
),
68 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.InvalidFileContent", EINVAL
),
69 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.MatchRuleNotFound", ENOENT
),
70 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.SELinuxSecurityContextUnknown", ESRCH
),
71 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.ObjectPathInUse", EBUSY
),
75 /* GCC maps this magically to the beginning and end of the BUS_ERROR_MAP section.
76 * Hide them; for currently unknown reasons they get exported to the shared libries
77 * even without being listed in the sym file. */
78 extern const sd_bus_error_map __start_BUS_ERROR_MAP
[] _hidden_
;
79 extern const sd_bus_error_map __stop_BUS_ERROR_MAP
[] _hidden_
;
81 /* Additional maps registered with sd_bus_error_add_map() are in this
82 * NULL terminated array */
83 static const sd_bus_error_map
**additional_error_maps
= NULL
;
85 static int bus_error_name_to_errno(const char *name
) {
86 const sd_bus_error_map
**map
, *m
;
93 p
= startswith(name
, "System.Error.");
95 r
= errno_from_name(p
);
102 if (additional_error_maps
) {
103 for (map
= additional_error_maps
; *map
; map
++) {
104 for (m
= *map
;; m
++) {
105 /* For additional error maps the end marker is actually the end marker */
106 if (m
->code
== BUS_ERROR_MAP_END_MARKER
)
109 if (streq(m
->name
, name
))
115 m
= __start_BUS_ERROR_MAP
;
116 while (m
< __stop_BUS_ERROR_MAP
) {
117 /* For magic ELF error maps, the end marker might
118 * appear in the middle of things, since multiple maps
119 * might appear in the same section. Hence, let's skip
120 * over it, but realign the pointer to the netx 8byte
121 * boundary, which is the selected alignment for the
123 if (m
->code
== BUS_ERROR_MAP_END_MARKER
) {
128 if (streq(m
->name
, name
))
137 static sd_bus_error
errno_to_bus_error_const(int error
) {
145 return BUS_ERROR_OOM
;
149 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_ACCESS_DENIED
, "Access denied");
152 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INVALID_ARGS
, "Invalid argument");
155 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_UNIX_PROCESS_ID_UNKNOWN
, "No such process");
158 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_FILE_NOT_FOUND
, "File not found");
161 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_FILE_EXISTS
, "File exists");
165 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_TIMEOUT
, "Timed out");
168 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_IO_ERROR
, "Input/output error");
173 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_DISCONNECTED
, "Disconnected");
176 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NOT_SUPPORTED
, "Not supported");
179 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_BAD_ADDRESS
, "Address not available");
182 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_LIMITS_EXCEEDED
, "Limits exceeded");
185 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_ADDRESS_IN_USE
, "Address in use");
188 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INCONSISTENT_MESSAGE
, "Inconsistent message");
191 return SD_BUS_ERROR_NULL
;
194 static int errno_to_bus_error_name_new(int error
, char **ret
) {
201 name
= errno_to_name(error
);
205 n
= strappend("System.Error.", name
);
213 bool bus_error_is_dirty(sd_bus_error
*e
) {
217 return e
->name
|| e
->message
|| e
->_need_free
!= 0;
220 _public_
void sd_bus_error_free(sd_bus_error
*e
) {
224 if (e
->_need_free
> 0) {
225 free((void*) e
->name
);
226 free((void*) e
->message
);
229 e
->name
= e
->message
= NULL
;
233 _public_
int sd_bus_error_set(sd_bus_error
*e
, const char *name
, const char *message
) {
240 assert_return(!bus_error_is_dirty(e
), -EINVAL
);
242 e
->name
= strdup(name
);
249 e
->message
= strdup(message
);
254 return -bus_error_name_to_errno(name
);
257 int bus_error_setfv(sd_bus_error
*e
, const char *name
, const char *format
, va_list ap
) {
264 assert_return(!bus_error_is_dirty(e
), -EINVAL
);
266 e
->name
= strdup(name
);
272 /* If we hit OOM on formatting the pretty message, we ignore
273 * this, since we at least managed to write the error name */
275 (void) vasprintf((char**) &e
->message
, format
, ap
);
280 return -bus_error_name_to_errno(name
);
283 _public_
int sd_bus_error_setf(sd_bus_error
*e
, const char *name
, const char *format
, ...) {
289 va_start(ap
, format
);
290 r
= bus_error_setfv(e
, name
, format
, ap
);
296 return sd_bus_error_set(e
, name
, NULL
);
299 _public_
int sd_bus_error_copy(sd_bus_error
*dest
, const sd_bus_error
*e
) {
301 if (!sd_bus_error_is_set(e
))
306 assert_return(!bus_error_is_dirty(dest
), -EINVAL
);
309 * _need_free < 0 indicates that the error is temporarily const, needs deep copying
310 * _need_free == 0 indicates that the error is perpetually const, needs no deep copying
311 * _need_free > 0 indicates that the error is fully dynamic, needs deep copying
314 if (e
->_need_free
== 0)
317 dest
->name
= strdup(e
->name
);
319 *dest
= BUS_ERROR_OOM
;
324 dest
->message
= strdup(e
->message
);
326 dest
->_need_free
= 1;
330 return -bus_error_name_to_errno(e
->name
);
333 _public_
int sd_bus_error_set_const(sd_bus_error
*e
, const char *name
, const char *message
) {
339 assert_return(!bus_error_is_dirty(e
), -EINVAL
);
341 *e
= SD_BUS_ERROR_MAKE_CONST(name
, message
);
344 return -bus_error_name_to_errno(name
);
347 _public_
int sd_bus_error_is_set(const sd_bus_error
*e
) {
354 _public_
int sd_bus_error_has_name(const sd_bus_error
*e
, const char *name
) {
358 return streq_ptr(e
->name
, name
);
361 _public_
int sd_bus_error_get_errno(const sd_bus_error
* e
) {
368 return bus_error_name_to_errno(e
->name
);
371 static void bus_error_strerror(sd_bus_error
*e
, int error
) {
385 x
= strerror_r(error
, m
, k
);
386 if (errno
== ERANGE
|| strlen(x
) >= k
- 1) {
398 if (e
->_need_free
> 0) {
399 /* Error is already dynamic, let's just update the message */
400 free((char*) e
->message
);
405 /* Error was const so far, let's make it dynamic, if we can */
420 if (e
->_need_free
> 0) {
423 /* Error is dynamic, let's hence make the message also dynamic */
428 free((char*) e
->message
);
431 /* Error is const, hence we can just override */
440 _public_
int sd_bus_error_set_errno(sd_bus_error
*e
, int error
) {
450 assert_return(!bus_error_is_dirty(e
), -EINVAL
);
452 /* First, try a const translation */
453 *e
= errno_to_bus_error_const(error
);
455 if (!sd_bus_error_is_set(e
)) {
458 /* If that didn't work, try a dynamic one. */
460 k
= errno_to_bus_error_name_new(error
, (char**) &e
->name
);
467 *e
= BUS_ERROR_FAILED
;
470 /* Now, fill in the message from strerror() if we can */
471 bus_error_strerror(e
, error
);
475 _public_
int sd_bus_error_set_errnofv(sd_bus_error
*e
, int error
, const char *format
, va_list ap
) {
487 assert_return(!bus_error_is_dirty(e
), -EINVAL
);
489 /* First, try a const translation */
490 *e
= errno_to_bus_error_const(error
);
492 if (!sd_bus_error_is_set(e
)) {
495 /* If that didn't work, try a dynamic one */
497 k
= errno_to_bus_error_name_new(error
, (char**) &e
->name
);
504 *e
= BUS_ERROR_FAILED
;
510 /* Then, let's try to fill in the supplied message */
512 errno
= error
; /* Make sure that %m resolves to the specified error */
513 r
= vasprintf(&m
, format
, ap
);
516 if (e
->_need_free
<= 0) {
529 free((char*) e
->message
);
536 /* If that didn't work, use strerror() for the message */
537 bus_error_strerror(e
, error
);
541 _public_
int sd_bus_error_set_errnof(sd_bus_error
*e
, int error
, const char *format
, ...) {
552 assert_return(!bus_error_is_dirty(e
), -EINVAL
);
557 va_start(ap
, format
);
558 r
= sd_bus_error_set_errnofv(e
, error
, format
, ap
);
564 return sd_bus_error_set_errno(e
, error
);
567 const char *bus_error_message(const sd_bus_error
*e
, int error
) {
570 /* Sometimes, the D-Bus server is a little bit too verbose with
571 * its error messages, so let's override them here */
572 if (sd_bus_error_has_name(e
, SD_BUS_ERROR_ACCESS_DENIED
))
573 return "Access denied";
582 return strerror(error
);
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
);
591 if (additional_error_maps
) {
593 if (additional_error_maps
[n
] == NULL
)
596 if (additional_error_maps
[n
] == map
)
601 maps
= realloc_multiply(additional_error_maps
, sizeof(struct sd_bus_error_map
*), n
+ 2);
609 additional_error_maps
= maps
;