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);
198 vasprintf((char**) &e->message, format, ap);
203 return -bus_error_name_to_errno(name);
206 _public_ int sd_bus_error_setf(sd_bus_error *e, const char *name, const char *format, ...) {
212 va_start(ap, format);
213 r = bus_error_setfv(e, name, format, ap);
219 return sd_bus_error_set(e, name, NULL);
222 _public_ int sd_bus_error_copy(sd_bus_error *dest, const sd_bus_error *e) {
224 if (!sd_bus_error_is_set(e))
229 assert_return(!bus_error_is_dirty(dest), -EINVAL);
232 * _need_free < 0 indicates that the error is temporarily const, needs deep copying
233 * _need_free == 0 indicates that the error is perpetually const, needs no deep copying
234 * _need_free > 0 indicates that the error is fully dynamic, needs deep copying
237 if (e->_need_free == 0)
240 dest->name = strdup(e->name);
242 *dest = BUS_ERROR_OOM;
247 dest->message = strdup(e->message);
249 dest->_need_free = 1;
253 return -bus_error_name_to_errno(e->name);
256 _public_ int sd_bus_error_set_const(sd_bus_error *e, const char *name, const char *message) {
262 assert_return(!bus_error_is_dirty(e), -EINVAL);
264 *e = SD_BUS_ERROR_MAKE_CONST(name, message);
267 return -bus_error_name_to_errno(name);
270 _public_ int sd_bus_error_is_set(const sd_bus_error *e) {
277 _public_ int sd_bus_error_has_name(const sd_bus_error *e, const char *name) {
281 return streq_ptr(e->name, name);
284 _public_ int sd_bus_error_get_errno(const sd_bus_error* e) {
291 return bus_error_name_to_errno(e->name);
294 static void bus_error_strerror(sd_bus_error *e, int error) {
308 x = strerror_r(error, m, k);
309 if (errno == ERANGE || strlen(x) >= k - 1) {
321 if (e->_need_free > 0) {
322 /* Error is already dynamic, let's just update the message */
323 free((char*) e->message);
328 /* Error was const so far, let's make it dynamic, if we can */
343 if (e->_need_free > 0) {
346 /* Error is dynamic, let's hence make the message also dynamic */
351 free((char*) e->message);
354 /* Error is const, hence we can just override */
363 _public_ int sd_bus_error_set_errno(sd_bus_error *e, int error) {
373 assert_return(!bus_error_is_dirty(e), -EINVAL);
375 /* First, try a const translation */
376 *e = errno_to_bus_error_const(error);
378 if (!sd_bus_error_is_set(e)) {
381 /* If that didn't work, try a dynamic one. */
383 k = errno_to_bus_error_name_new(error, (char**) &e->name);
390 *e = BUS_ERROR_FAILED;
393 /* Now, fill in the message from strerror() if we can */
394 bus_error_strerror(e, error);
398 int bus_error_set_errnofv(sd_bus_error *e, int error, const char *format, va_list ap) {
409 assert_return(!bus_error_is_dirty(e), -EINVAL);
411 /* First, try a const translation */
412 *e = errno_to_bus_error_const(error);
414 if (!sd_bus_error_is_set(e)) {
417 /* If that didn't work, try a dynamic one */
419 k = errno_to_bus_error_name_new(error, (char**) &e->name);
426 *e = BUS_ERROR_FAILED;
432 /* First, let's try to fill in the supplied message */
434 r = vasprintf(&m, format, ap);
437 if (e->_need_free <= 0) {
450 free((char*) e->message);
457 /* If that didn't work, use strerror() for the message */
458 bus_error_strerror(e, error);
462 _public_ int sd_bus_error_set_errnof(sd_bus_error *e, int error, const char *format, ...) {
473 assert_return(!bus_error_is_dirty(e), -EINVAL);
478 va_start(ap, format);
479 r = bus_error_set_errnofv(e, error, format, ap);
485 return sd_bus_error_set_errno(e, error);
488 const char *bus_error_message(const sd_bus_error *e, int error) {
491 /* Sometimes the D-Bus server is a little bit too verbose with
492 * its error messages, so let's override them here */
493 if (sd_bus_error_has_name(e, SD_BUS_ERROR_ACCESS_DENIED))
494 return "Access denied";
503 return strerror(error);