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/>.
30 #include "errno-list.h"
33 #include "bus-error.h"
35 #define BUS_ERROR_OOM SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_MEMORY, "Out of memory")
36 #define BUS_ERROR_FAILED SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_FAILED, "Operation failed")
38 static int bus_error_name_to_errno(const char *name) {
45 p = startswith(name, "System.Error.");
47 r = errno_from_name(p);
54 /* Better replace this with a gperf table */
56 if (streq(name, SD_BUS_ERROR_NO_MEMORY))
59 if (streq(name, SD_BUS_ERROR_SERVICE_UNKNOWN))
62 if (streq(name, SD_BUS_ERROR_NAME_HAS_NO_OWNER))
65 if (streq(name, SD_BUS_ERROR_NO_REPLY) ||
66 streq(name, SD_BUS_ERROR_TIMEOUT) ||
67 streq(name, "org.freedesktop.DBus.Error.TimedOut"))
70 if (streq(name, SD_BUS_ERROR_IO_ERROR))
73 if (streq(name, SD_BUS_ERROR_BAD_ADDRESS))
76 if (streq(name, SD_BUS_ERROR_NOT_SUPPORTED))
79 if (streq(name, SD_BUS_ERROR_LIMITS_EXCEEDED))
82 if (streq(name, SD_BUS_ERROR_ACCESS_DENIED) ||
83 streq(name, SD_BUS_ERROR_AUTH_FAILED))
86 if (streq(name, SD_BUS_ERROR_NO_SERVER))
89 if (streq(name, SD_BUS_ERROR_NO_NETWORK))
92 if (streq(name, SD_BUS_ERROR_ADDRESS_IN_USE))
95 if (streq(name, SD_BUS_ERROR_DISCONNECTED))
98 if (streq(name, SD_BUS_ERROR_INVALID_ARGS) ||
99 streq(name, SD_BUS_ERROR_INVALID_SIGNATURE) ||
100 streq(name, "org.freedesktop.DBus.Error.MatchRuleInvalid") ||
101 streq(name, "org.freedesktop.DBus.Error.InvalidFileContent"))
104 if (streq(name, SD_BUS_ERROR_FILE_NOT_FOUND) ||
105 streq(name, "org.freedesktop.DBus.Error.MatchRuleNotFound"))
108 if (streq(name, SD_BUS_ERROR_FILE_EXISTS))
111 if (streq(name, SD_BUS_ERROR_UNKNOWN_METHOD) ||
112 streq(name, SD_BUS_ERROR_UNKNOWN_OBJECT) ||
113 streq(name, SD_BUS_ERROR_UNKNOWN_INTERFACE) ||
114 streq(name, SD_BUS_ERROR_UNKNOWN_PROPERTY))
117 if (streq(name, SD_BUS_ERROR_PROPERTY_READ_ONLY))
120 if (streq(name, SD_BUS_ERROR_UNIX_PROCESS_ID_UNKNOWN) ||
121 streq(name, "org.freedesktop.DBus.Error.SELinuxSecurityContextUnknown"))
124 if (streq(name, SD_BUS_ERROR_INCONSISTENT_MESSAGE))
127 if (streq(name, "org.freedesktop.DBus.Error.ObjectPathInUse"))
133 static sd_bus_error errno_to_bus_error_const(int error) {
141 return BUS_ERROR_OOM;
145 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_ACCESS_DENIED, "Access denied");
148 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INVALID_ARGS, "Invalid argument");
151 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_UNIX_PROCESS_ID_UNKNOWN, "No such process");
154 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_FILE_NOT_FOUND, "File not found");
157 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_FILE_EXISTS, "File exists");
161 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_TIMEOUT, "Timed out");
164 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_IO_ERROR, "Input/output error");
169 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_DISCONNECTED, "Disconnected");
172 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NOT_SUPPORTED, "Not supported");
175 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_BAD_ADDRESS, "Address not available");
178 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_LIMITS_EXCEEDED, "Limits exceeded");
181 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_ADDRESS_IN_USE, "Address in use");
184 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Inconsistent message");
187 return SD_BUS_ERROR_NULL;
190 static int errno_to_bus_error_name_new(int error, char **ret) {
197 name = errno_to_name(error);
201 n = strappend("System.Error.", name);
209 bool bus_error_is_dirty(sd_bus_error *e) {
213 return e->name || e->message || e->_need_free != 0;
216 _public_ void sd_bus_error_free(sd_bus_error *e) {
220 if (e->_need_free > 0) {
221 free((void*) e->name);
222 free((void*) e->message);
225 e->name = e->message = NULL;
229 _public_ int sd_bus_error_set(sd_bus_error *e, const char *name, const char *message) {
236 assert_return(!bus_error_is_dirty(e), -EINVAL);
238 e->name = strdup(name);
245 e->message = strdup(message);
250 return -bus_error_name_to_errno(name);
253 int bus_error_setfv(sd_bus_error *e, const char *name, const char *format, va_list ap) {
260 assert_return(!bus_error_is_dirty(e), -EINVAL);
262 e->name = strdup(name);
269 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) {
359 return bus_error_name_to_errno(e->name);
362 static void bus_error_strerror(sd_bus_error *e, int error) {
376 x = strerror_r(error, m, k);
377 if (errno == ERANGE || strlen(x) >= k - 1) {
389 if (e->_need_free > 0) {
390 /* Error is already dynamic, let's just update the message */
391 free((char*) e->message);
396 /* Error was const so far, let's make it dynamic, if we can */
411 if (e->_need_free > 0) {
414 /* Error is dynamic, let's hence make the message also dynamic */
419 free((char*) e->message);
422 /* Error is const, hence we can just override */
431 _public_ int sd_bus_error_set_errno(sd_bus_error *e, int error) {
441 assert_return(!bus_error_is_dirty(e), -EINVAL);
443 /* First, try a const translation */
444 *e = errno_to_bus_error_const(error);
446 if (!sd_bus_error_is_set(e)) {
449 /* If that didn't work, try a dynamic one. */
451 k = errno_to_bus_error_name_new(error, (char**) &e->name);
458 *e = BUS_ERROR_FAILED;
461 /* Now, fill in the message from strerror() if we can */
462 bus_error_strerror(e, error);
466 int bus_error_set_errnofv(sd_bus_error *e, int error, const char *format, va_list ap) {
477 assert_return(!bus_error_is_dirty(e), -EINVAL);
479 /* First, try a const translation */
480 *e = errno_to_bus_error_const(error);
482 if (!sd_bus_error_is_set(e)) {
485 /* If that didn't work, try a dynamic one */
487 k = errno_to_bus_error_name_new(error, (char**) &e->name);
494 *e = BUS_ERROR_FAILED;
500 /* First, let's try to fill in the supplied message */
502 r = vasprintf(&m, format, ap);
505 if (e->_need_free <= 0) {
518 free((char*) e->message);
525 /* If that didn't work, use strerror() for the message */
526 bus_error_strerror(e, error);
530 _public_ int sd_bus_error_set_errnof(sd_bus_error *e, int error, const char *format, ...) {
541 assert_return(!bus_error_is_dirty(e), -EINVAL);
546 va_start(ap, format);
547 r = bus_error_set_errnofv(e, error, format, ap);
553 return sd_bus_error_set_errno(e, error);
556 const char *bus_error_message(const sd_bus_error *e, int error) {
559 /* Sometimes the D-Bus server is a little bit too verbose with
560 * its error messages, so let's override them here */
561 if (sd_bus_error_has_name(e, SD_BUS_ERROR_ACCESS_DENIED))
562 return "Access denied";
571 return strerror(error);