summaryrefslogtreecommitdiff
path: root/src/libsystemd-bus/bus-error.c
blob: 4696a88f762b952f3c304f8ee710030b24779cd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/

/***
  This file is part of systemd.

  Copyright 2013 Lennart Poettering

  systemd is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 2.1 of the License, or
  (at your option) any later version.

  systemd is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

#include <errno.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>

#include "util.h"

#include "sd-bus.h"
#include "bus-error.h"

bool bus_error_is_dirty(sd_bus_error *e) {
        if (!e)
                return 0;

        return e->name || e->message || e->need_free;
}

void sd_bus_error_free(sd_bus_error *e) {
        if (!e)
                return;

        if (e->need_free) {
                free((void*) e->name);
                free((void*) e->message);
        }

        e->name = e->message = NULL;
        e->need_free = false;
}

int sd_bus_error_set(sd_bus_error *e, const char *name, const char *format, ...) {
        char *n, *m = NULL;
        va_list ap;
        int r;

        if (!e)
                return 0;
        if (bus_error_is_dirty(e))
                return -EINVAL;
        if (!name)
                return -EINVAL;

        n = strdup(name);
        if (!n)
                return -ENOMEM;

        if (format) {
                va_start(ap, format);
                r = vasprintf(&m, format, ap);
                va_end(ap);

                if (r < 0) {
                        free(n);
                        return -ENOMEM;
                }
        }

        e->name = n;
        e->message = m;
        e->need_free = true;

        return 0;
}

int sd_bus_error_copy(sd_bus_error *dest, const sd_bus_error *e) {
        char *x, *y = NULL;

        if (!dest)
                return 0;
        if (bus_error_is_dirty(dest))
                return -EINVAL;
        if (!sd_bus_error_is_set(e))
                return 0;

        x = strdup(e->name);
        if (!x)
                return -ENOMEM;

        if (e->message) {
                y = strdup(e->message);
                if (!y) {
                        free(x);
                        return -ENOMEM;
                }
        }

        dest->name = x;
        dest->message = y;
        dest->need_free = true;
        return 0;
}

void sd_bus_error_set_const(sd_bus_error *e, const char *name, const char *message) {
        if (!e)
                return;
        if (bus_error_is_dirty(e))
                return;

        e->name = name;
        e->message = message;
        e->need_free = false;
}

int sd_bus_error_is_set(const sd_bus_error *e) {
        if (!e)
                return 0;

        return !!e->name;
}

int sd_bus_error_has_name(const sd_bus_error *e, const char *name) {
        if (!e)
                return 0;

        return streq_ptr(e->name, name);
}

int bus_error_to_errno(const sd_bus_error* e) {

        /* Better replce this with a gperf table */

        if (!e)
                return -EIO;

        if (!e->name)
                return -EIO;

        if (streq(e->name, "org.freedesktop.DBus.Error.NoMemory"))
                return -ENOMEM;

        if (streq(e->name, "org.freedesktop.DBus.Error.AuthFailed") ||
            streq(e->name, "org.freedesktop.DBus.Error.AccessDenied"))
                return -EPERM;

        if (streq(e->name, "org.freedesktop.DBus.Error.InvalidArgs"))
                return -EINVAL;

        if (streq(e->name, "org.freedesktop.DBus.Error.UnixProcessIdUnknown"))
                return -ESRCH;

        if (streq(e->name, "org.freedesktop.DBus.Error.FileNotFound"))
                return -ENOENT;

        if (streq(e->name, "org.freedesktop.DBus.Error.FileExists"))
                return -EEXIST;

        if (streq(e->name, "org.freedesktop.DBus.Error.Timeout"))
                return -ETIMEDOUT;

        if (streq(e->name, "org.freedesktop.DBus.Error.IOError"))
                return -EIO;

        if (streq(e->name, "org.freedesktop.DBus.Error.Disconnected"))
                return -ECONNRESET;

        if (streq(e->name, "org.freedesktop.DBus.Error.NotSupported"))
                return -ENOTSUP;

        return -EIO;
}

int bus_error_from_errno(sd_bus_error *e, int error) {
        if (!e)
                return error;

        switch (error) {

        case -ENOMEM:
                sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.NoMemory", "Out of memory");
                break;

        case -EPERM:
        case -EACCES:
                sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.AccessDenied", "Access denied");
                break;

        case -EINVAL:
                sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.InvalidArgs", "Invalid argument");
                break;

        case -ESRCH:
                sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.UnixProcessIdUnknown", "No such process");
                break;

        case -ENOENT:
                sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.FileNotFound", "File not found");
                break;

        case -EEXIST:
                sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.FileExists", "File exists");
                break;

        case -ETIMEDOUT:
        case -ETIME:
                sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.Timeout", "Timed out");
                break;

        case -EIO:
                sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.IOError", "Input/output error");
                break;

        case -ENETRESET:
        case -ECONNABORTED:
        case -ECONNRESET:
                sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.Disconnected", "Disconnected");
                break;

        case -ENOTSUP:
                sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.NotSupported", "Not supported");
                break;
        }

        sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.Failed", "Operation failed");
        return error;
}

const char *bus_error_message(const sd_bus_error *e, int error) {
        if (e && e->message)
                return e->message;

        return strerror(error);
}