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) {
91 if (bus_error_is_dirty(dest))
93 if (!sd_bus_error_is_set(e))
104 y = strdup(e->message);
113 dest->need_free = true;
120 void sd_bus_error_set_const(sd_bus_error *e, const char *name, const char *message) {
123 if (bus_error_is_dirty(e))
127 e->message = message;
128 e->need_free = false;
131 int sd_bus_error_is_set(const sd_bus_error *e) {
138 int sd_bus_error_has_name(const sd_bus_error *e, const char *name) {
142 return streq_ptr(e->name, name);
145 int bus_error_to_errno(const sd_bus_error* e) {
147 /* Better replce this with a gperf table */
152 if (streq(e->name, "org.freedesktop.DBus.Error.NoMemory"))
155 if (streq(e->name, "org.freedesktop.DBus.Error.AuthFailed") ||
156 streq(e->name, "org.freedesktop.DBus.Error.AccessDenied"))
162 int bus_error_from_errno(sd_bus_error *e, int error) {
166 if (error == -ENOMEM)
167 sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.NoMemory", strerror(-error));
168 else if (error == -EPERM || error == EACCES)
169 sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.AccessDenied", strerror(-error));
171 sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.Failed", "Operation failed");