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);
254 int bus_error_setfv(sd_bus_error *e, const char *name, const char *format, va_list ap) {
261 assert_return(!bus_error_is_dirty(e), -EINVAL);
263 e->name = strdup(name);
270 vasprintf((char**) &e->message, format, ap);
275 return -bus_error_name_to_errno(name);
278 _public_ int sd_bus_error_setf(sd_bus_error *e, const char *name, const char *format, ...) {
284 va_start(ap, format);
285 r = bus_error_setfv(e, name, format, ap);
291 return sd_bus_error_set(e, name, NULL);
294 _public_ int sd_bus_error_copy(sd_bus_error *dest, const sd_bus_error *e) {
296 if (!sd_bus_error_is_set(e))
301 assert_return(!bus_error_is_dirty(dest), -EINVAL);
304 * _need_free < 0 indicates that the error is temporarily const, needs deep copying
305 * _need_free == 0 indicates that the error is perpetually const, needs no deep copying
306 * _need_free > 0 indicates that the error is fully dynamic, needs deep copying
309 if (e->_need_free == 0)
312 dest->name = strdup(e->name);
314 *dest = BUS_ERROR_OOM;
319 dest->message = strdup(e->message);
321 dest->_need_free = 1;
325 return -bus_error_name_to_errno(e->name);
328 _public_ int sd_bus_error_set_const(sd_bus_error *e, const char *name, const char *message) {
334 assert_return(!bus_error_is_dirty(e), -EINVAL);
336 *e = SD_BUS_ERROR_MAKE_CONST(name, message);
339 return -bus_error_name_to_errno(name);
342 _public_ int sd_bus_error_is_set(const sd_bus_error *e) {
349 _public_ int sd_bus_error_has_name(const sd_bus_error *e, const char *name) {
353 return streq_ptr(e->name, name);
356 _public_ int sd_bus_error_get_errno(const sd_bus_error* e) {
360 return bus_error_name_to_errno(e->name);
363 static void bus_error_strerror(sd_bus_error *e, int error) {
377 x = strerror_r(error, m, k);
378 if (errno == ERANGE || strlen(x) >= k - 1) {
390 if (e->_need_free > 0) {
391 /* Error is already dynamic, let's just update the message */
392 free((char*) e->message);
397 /* Error was const so far, let's make it dynamic, if we can */
412 if (e->_need_free > 0) {
415 /* Error is dynamic, let's hence make the message also dynamic */
420 free((char*) e->message);
423 /* Error is const, hence we can just override */
432 _public_ int sd_bus_error_set_errno(sd_bus_error *e, int error) {
442 assert_return(!bus_error_is_dirty(e), -EINVAL);
444 /* First, try a const translation */
445 *e = errno_to_bus_error_const(error);
447 if (!sd_bus_error_is_set(e)) {
450 /* If that didn't work, try a dynamic one. */
452 k = errno_to_bus_error_name_new(error, (char**) &e->name);
459 *e = BUS_ERROR_FAILED;
462 /* Now, fill in the message from strerror() if we can */
463 bus_error_strerror(e, error);
468 int bus_error_set_errnofv(sd_bus_error *e, int error, const char *format, va_list ap) {
479 assert_return(!bus_error_is_dirty(e), -EINVAL);
481 /* First, try a const translation */
482 *e = errno_to_bus_error_const(error);
484 if (!sd_bus_error_is_set(e)) {
487 /* If that didn't work, try a dynamic one */
489 k = errno_to_bus_error_name_new(error, (char**) &e->name);
496 *e = BUS_ERROR_FAILED;
502 /* First, let's try to fill in the supplied message */
504 r = vasprintf(&m, format, ap);
507 if (e->_need_free <= 0) {
520 free((char*) e->message);
527 /* If that didn't work, use strerror() for the message */
528 bus_error_strerror(e, error);
532 _public_ int sd_bus_error_set_errnof(sd_bus_error *e, int error, const char *format, ...) {
543 assert_return(!bus_error_is_dirty(e), -EINVAL);
548 va_start(ap, format);
549 r = bus_error_set_errnofv(e, error, format, ap);
555 return sd_bus_error_set_errno(e, error);
558 const char *bus_error_message(const sd_bus_error *e, int error) {
561 /* Sometimes the D-Bus server is a little bit too verbose with
562 * its error messages, so let's override them here */
563 if (sd_bus_error_has_name(e, SD_BUS_ERROR_ACCESS_DENIED))
564 return "Access denied";
573 return strerror(error);