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) {
41 const name_error_mapping *m;
46 p = startswith(name, "System.Error.");
48 r = errno_from_name(p);
55 m = bus_error_mapping_lookup(name, strlen(name));
62 static sd_bus_error errno_to_bus_error_const(int error) {
74 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_ACCESS_DENIED, "Access denied");
77 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INVALID_ARGS, "Invalid argument");
80 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_UNIX_PROCESS_ID_UNKNOWN, "No such process");
83 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_FILE_NOT_FOUND, "File not found");
86 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_FILE_EXISTS, "File exists");
90 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_TIMEOUT, "Timed out");
93 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_IO_ERROR, "Input/output error");
98 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_DISCONNECTED, "Disconnected");
101 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NOT_SUPPORTED, "Not supported");
104 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_BAD_ADDRESS, "Address not available");
107 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_LIMITS_EXCEEDED, "Limits exceeded");
110 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_ADDRESS_IN_USE, "Address in use");
113 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Inconsistent message");
116 return SD_BUS_ERROR_NULL;
119 static int errno_to_bus_error_name_new(int error, char **ret) {
126 name = errno_to_name(error);
130 n = strappend("System.Error.", name);
138 bool bus_error_is_dirty(sd_bus_error *e) {
142 return e->name || e->message || e->_need_free != 0;
145 _public_ void sd_bus_error_free(sd_bus_error *e) {
149 if (e->_need_free > 0) {
150 free((void*) e->name);
151 free((void*) e->message);
154 e->name = e->message = NULL;
158 _public_ int sd_bus_error_set(sd_bus_error *e, const char *name, const char *message) {
165 assert_return(!bus_error_is_dirty(e), -EINVAL);
167 e->name = strdup(name);
174 e->message = strdup(message);
179 return -bus_error_name_to_errno(name);
182 int bus_error_setfv(sd_bus_error *e, const char *name, const char *format, va_list ap) {
189 assert_return(!bus_error_is_dirty(e), -EINVAL);
191 e->name = strdup(name);
200 r = vasprintf((char**) &e->message, format, ap);
208 return -bus_error_name_to_errno(name);
211 _public_ int sd_bus_error_setf(sd_bus_error *e, const char *name, const char *format, ...) {
217 va_start(ap, format);
218 r = bus_error_setfv(e, name, format, ap);
224 return sd_bus_error_set(e, name, NULL);
227 _public_ int sd_bus_error_copy(sd_bus_error *dest, const sd_bus_error *e) {
229 if (!sd_bus_error_is_set(e))
234 assert_return(!bus_error_is_dirty(dest), -EINVAL);
237 * _need_free < 0 indicates that the error is temporarily const, needs deep copying
238 * _need_free == 0 indicates that the error is perpetually const, needs no deep copying
239 * _need_free > 0 indicates that the error is fully dynamic, needs deep copying
242 if (e->_need_free == 0)
245 dest->name = strdup(e->name);
247 *dest = BUS_ERROR_OOM;
252 dest->message = strdup(e->message);
254 dest->_need_free = 1;
258 return -bus_error_name_to_errno(e->name);
261 _public_ int sd_bus_error_set_const(sd_bus_error *e, const char *name, const char *message) {
267 assert_return(!bus_error_is_dirty(e), -EINVAL);
269 *e = SD_BUS_ERROR_MAKE_CONST(name, message);
272 return -bus_error_name_to_errno(name);
275 _public_ int sd_bus_error_is_set(const sd_bus_error *e) {
282 _public_ int sd_bus_error_has_name(const sd_bus_error *e, const char *name) {
286 return streq_ptr(e->name, name);
289 _public_ int sd_bus_error_get_errno(const sd_bus_error* e) {
296 return bus_error_name_to_errno(e->name);
299 static void bus_error_strerror(sd_bus_error *e, int error) {
313 x = strerror_r(error, m, k);
314 if (errno == ERANGE || strlen(x) >= k - 1) {
326 if (e->_need_free > 0) {
327 /* Error is already dynamic, let's just update the message */
328 free((char*) e->message);
333 /* Error was const so far, let's make it dynamic, if we can */
348 if (e->_need_free > 0) {
351 /* Error is dynamic, let's hence make the message also dynamic */
356 free((char*) e->message);
359 /* Error is const, hence we can just override */
368 _public_ int sd_bus_error_set_errno(sd_bus_error *e, int error) {
378 assert_return(!bus_error_is_dirty(e), -EINVAL);
380 /* First, try a const translation */
381 *e = errno_to_bus_error_const(error);
383 if (!sd_bus_error_is_set(e)) {
386 /* If that didn't work, try a dynamic one. */
388 k = errno_to_bus_error_name_new(error, (char**) &e->name);
395 *e = BUS_ERROR_FAILED;
398 /* Now, fill in the message from strerror() if we can */
399 bus_error_strerror(e, error);
403 int bus_error_set_errnofv(sd_bus_error *e, int error, const char *format, va_list ap) {
414 assert_return(!bus_error_is_dirty(e), -EINVAL);
416 /* First, try a const translation */
417 *e = errno_to_bus_error_const(error);
419 if (!sd_bus_error_is_set(e)) {
422 /* If that didn't work, try a dynamic one */
424 k = errno_to_bus_error_name_new(error, (char**) &e->name);
431 *e = BUS_ERROR_FAILED;
437 /* First, let's try to fill in the supplied message */
439 r = vasprintf(&m, format, ap);
442 if (e->_need_free <= 0) {
455 free((char*) e->message);
462 /* If that didn't work, use strerror() for the message */
463 bus_error_strerror(e, error);
467 _public_ int sd_bus_error_set_errnof(sd_bus_error *e, int error, const char *format, ...) {
478 assert_return(!bus_error_is_dirty(e), -EINVAL);
483 va_start(ap, format);
484 r = bus_error_set_errnofv(e, error, format, ap);
490 return sd_bus_error_set_errno(e, error);
493 const char *bus_error_message(const sd_bus_error *e, int error) {
496 /* Sometimes the D-Bus server is a little bit too verbose with
497 * its error messages, so let's override them here */
498 if (sd_bus_error_has_name(e, SD_BUS_ERROR_ACCESS_DENIED))
499 return "Access denied";
508 return strerror(error);