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 void sd_bus_error_free(sd_bus_error *e) {
39 free((void*) e->name);
40 free((void*) e->message);
43 e->name = e->message = NULL;
47 int sd_bus_error_set(sd_bus_error *e, const char *name, const char *format, ...) {
54 if (sd_bus_error_is_set(e))
65 r = vasprintf(&m, format, ap);
81 int sd_bus_error_copy(sd_bus_error *dest, const sd_bus_error *e) {
84 if (sd_bus_error_is_set(dest))
86 if (!sd_bus_error_is_set(e))
97 y = strdup(e->message);
106 dest->need_free = true;
113 void sd_bus_error_set_const(sd_bus_error *e, const char *name, const char *message) {
116 if (sd_bus_error_is_set(e))
120 e->message = message;
123 int sd_bus_error_is_set(const sd_bus_error *e) {
127 return e->name || e->message || e->need_free;
130 int sd_bus_error_has_name(const sd_bus_error *e, const char *name) {
134 return streq_ptr(e->name, name);
137 int bus_error_to_errno(const sd_bus_error* e) {
139 /* Better replce this with a gperf table */
144 if (streq(e->name, "org.freedesktop.DBus.Error.NoMemory"))
147 if (streq(e->name, "org.freedesktop.DBus.Error.AuthFailed") ||
148 streq(e->name, "org.freedesktop.DBus.Error.AccessDenied"))
154 int bus_error_from_errno(sd_bus_error *e, int error) {
158 if (error == -ENOMEM)
159 sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.NoMemory", strerror(-error));
160 else if (error == -EPERM || error == EACCES)
161 sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.AccessDenied", strerror(-error));
163 sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.Failed", "Operation failed");