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/>.
32 #include "bus-error.h"
34 bool bus_error_is_dirty(sd_bus_error *e) {
38 return e->name || e->message || e->need_free;
41 void sd_bus_error_free(sd_bus_error *e) {
46 free((void*) e->name);
47 free((void*) e->message);
50 e->name = e->message = NULL;
54 int sd_bus_error_set(sd_bus_error *e, const char *name, const char *format, ...) {
61 if (bus_error_is_dirty(e))
72 r = vasprintf(&m, format, ap);
88 int sd_bus_error_copy(sd_bus_error *dest, const sd_bus_error *e) {
93 if (bus_error_is_dirty(dest))
95 if (!sd_bus_error_is_set(e))
103 y = strdup(e->message);
112 dest->need_free = true;
116 void sd_bus_error_set_const(sd_bus_error *e, const char *name, const char *message) {
119 if (bus_error_is_dirty(e))
123 e->message = message;
124 e->need_free = false;
127 int sd_bus_error_is_set(const sd_bus_error *e) {
134 int sd_bus_error_has_name(const sd_bus_error *e, const char *name) {
138 return streq_ptr(e->name, name);
141 int bus_error_to_errno(const sd_bus_error* e) {
143 /* Better replce this with a gperf table */
148 if (streq(e->name, "org.freedesktop.DBus.Error.NoMemory"))
151 if (streq(e->name, "org.freedesktop.DBus.Error.AuthFailed") ||
152 streq(e->name, "org.freedesktop.DBus.Error.AccessDenied"))
158 int bus_error_from_errno(sd_bus_error *e, int error) {
162 if (error == -ENOMEM)
163 sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.NoMemory", strerror(-error));
164 else if (error == -EPERM || error == -EACCES)
165 sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.AccessDenied", strerror(-error));
167 sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.Failed", "Operation failed");
172 const char *bus_error_message(const sd_bus_error *e, int error) {
176 return strerror(error);