]> git.proxmox.com Git - systemd.git/blame - src/libsystemd/sd-bus/bus-error.c
Imported Upstream version 229
[systemd.git] / src / libsystemd / sd-bus / bus-error.c
CommitLineData
60f067b4
JS
1/***
2 This file is part of systemd.
3
4 Copyright 2013 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
20#include <errno.h>
60f067b4
JS
21#include <stdarg.h>
22#include <stdbool.h>
60f067b4 23#include <stdio.h>
db2df898
MP
24#include <stdlib.h>
25#include <string.h>
60f067b4
JS
26
27#include "sd-bus.h"
db2df898
MP
28
29#include "alloc-util.h"
60f067b4 30#include "bus-error.h"
db2df898
MP
31#include "errno-list.h"
32#include "string-util.h"
33#include "util.h"
60f067b4 34
f47781d8
MP
35BUS_ERROR_MAP_ELF_REGISTER const sd_bus_error_map bus_standard_errors[] = {
36 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.Failed", EACCES),
37 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NoMemory", ENOMEM),
38 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.ServiceUnknown", EHOSTUNREACH),
39 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NameHasNoOwner", ENXIO),
40 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NoReply", ETIMEDOUT),
41 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.IOError", EIO),
42 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.BadAddress", EADDRNOTAVAIL),
e3bff60a 43 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NotSupported", EOPNOTSUPP),
f47781d8
MP
44 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.LimitsExceeded", ENOBUFS),
45 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.AccessDenied", EACCES),
46 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.AuthFailed", EACCES),
47 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.InteractiveAuthorizationRequired", EACCES),
48 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NoServer", EHOSTDOWN),
49 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.Timeout", ETIMEDOUT),
50 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.NoNetwork", ENONET),
51 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.AddressInUse", EADDRINUSE),
52 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.Disconnected", ECONNRESET),
53 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.InvalidArgs", EINVAL),
54 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.FileNotFound", ENOENT),
55 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.FileExists", EEXIST),
56 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.UnknownMethod", EBADR),
57 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.UnknownObject", EBADR),
58 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.UnknownInterface", EBADR),
59 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.UnknownProperty", EBADR),
60 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.PropertyReadOnly", EROFS),
61 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.UnixProcessIdUnknown", ESRCH),
62 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.InvalidSignature", EINVAL),
63 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.InconsistentMessage", EBADMSG),
64 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.TimedOut", ETIMEDOUT),
65 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.MatchRuleInvalid", EINVAL),
66 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.InvalidFileContent", EINVAL),
67 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.MatchRuleNotFound", ENOENT),
68 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.SELinuxSecurityContextUnknown", ESRCH),
69 SD_BUS_ERROR_MAP("org.freedesktop.DBus.Error.ObjectPathInUse", EBUSY),
70 SD_BUS_ERROR_MAP_END
71};
72
86f210e9
MP
73/* GCC maps this magically to the beginning and end of the BUS_ERROR_MAP section.
74 * Hide them; for currently unknown reasons they get exported to the shared libries
75 * even without being listed in the sym file. */
76extern const sd_bus_error_map __start_BUS_ERROR_MAP[] _hidden_;
77extern const sd_bus_error_map __stop_BUS_ERROR_MAP[] _hidden_;
f47781d8
MP
78
79/* Additional maps registered with sd_bus_error_add_map() are in this
80 * NULL terminated array */
81static const sd_bus_error_map **additional_error_maps = NULL;
60f067b4
JS
82
83static int bus_error_name_to_errno(const char *name) {
f47781d8 84 const sd_bus_error_map **map, *m;
60f067b4
JS
85 const char *p;
86 int r;
60f067b4
JS
87
88 if (!name)
89 return EINVAL;
90
91 p = startswith(name, "System.Error.");
92 if (p) {
93 r = errno_from_name(p);
4c89c718 94 if (r < 0)
60f067b4
JS
95 return EIO;
96
97 return r;
98 }
99
4c89c718
MP
100 if (additional_error_maps)
101 for (map = additional_error_maps; *map; map++)
f47781d8
MP
102 for (m = *map;; m++) {
103 /* For additional error maps the end marker is actually the end marker */
104 if (m->code == BUS_ERROR_MAP_END_MARKER)
105 break;
106
107 if (streq(m->name, name))
108 return m->code;
109 }
f47781d8
MP
110
111 m = __start_BUS_ERROR_MAP;
112 while (m < __stop_BUS_ERROR_MAP) {
113 /* For magic ELF error maps, the end marker might
114 * appear in the middle of things, since multiple maps
115 * might appear in the same section. Hence, let's skip
4c89c718 116 * over it, but realign the pointer to the next 8 byte
f47781d8
MP
117 * boundary, which is the selected alignment for the
118 * arrays. */
119 if (m->code == BUS_ERROR_MAP_END_MARKER) {
120 m = ALIGN8_PTR(m+1);
121 continue;
122 }
123
124 if (streq(m->name, name))
125 return m->code;
126
127 m++;
128 }
60f067b4
JS
129
130 return EIO;
131}
132
133static sd_bus_error errno_to_bus_error_const(int error) {
134
135 if (error < 0)
136 error = -error;
137
138 switch (error) {
139
140 case ENOMEM:
141 return BUS_ERROR_OOM;
142
143 case EPERM:
144 case EACCES:
145 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_ACCESS_DENIED, "Access denied");
146
147 case EINVAL:
148 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INVALID_ARGS, "Invalid argument");
149
150 case ESRCH:
151 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_UNIX_PROCESS_ID_UNKNOWN, "No such process");
152
153 case ENOENT:
154 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_FILE_NOT_FOUND, "File not found");
155
156 case EEXIST:
157 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_FILE_EXISTS, "File exists");
158
159 case ETIMEDOUT:
160 case ETIME:
161 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_TIMEOUT, "Timed out");
162
163 case EIO:
164 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_IO_ERROR, "Input/output error");
165
166 case ENETRESET:
167 case ECONNABORTED:
168 case ECONNRESET:
169 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_DISCONNECTED, "Disconnected");
170
e3bff60a 171 case EOPNOTSUPP:
60f067b4
JS
172 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NOT_SUPPORTED, "Not supported");
173
174 case EADDRNOTAVAIL:
175 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_BAD_ADDRESS, "Address not available");
176
177 case ENOBUFS:
178 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_LIMITS_EXCEEDED, "Limits exceeded");
179
180 case EADDRINUSE:
181 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_ADDRESS_IN_USE, "Address in use");
182
183 case EBADMSG:
184 return SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Inconsistent message");
185 }
186
187 return SD_BUS_ERROR_NULL;
188}
189
190static int errno_to_bus_error_name_new(int error, char **ret) {
191 const char *name;
192 char *n;
193
194 if (error < 0)
195 error = -error;
196
197 name = errno_to_name(error);
198 if (!name)
199 return 0;
200
201 n = strappend("System.Error.", name);
202 if (!n)
203 return -ENOMEM;
204
205 *ret = n;
206 return 1;
207}
208
209bool bus_error_is_dirty(sd_bus_error *e) {
210 if (!e)
211 return false;
212
213 return e->name || e->message || e->_need_free != 0;
214}
215
216_public_ void sd_bus_error_free(sd_bus_error *e) {
217 if (!e)
218 return;
219
220 if (e->_need_free > 0) {
221 free((void*) e->name);
222 free((void*) e->message);
223 }
224
225 e->name = e->message = NULL;
226 e->_need_free = 0;
227}
228
229_public_ int sd_bus_error_set(sd_bus_error *e, const char *name, const char *message) {
230
231 if (!name)
232 return 0;
233 if (!e)
234 goto finish;
235
236 assert_return(!bus_error_is_dirty(e), -EINVAL);
237
238 e->name = strdup(name);
239 if (!e->name) {
240 *e = BUS_ERROR_OOM;
241 return -ENOMEM;
242 }
243
244 if (message)
245 e->message = strdup(message);
246
247 e->_need_free = 1;
248
249finish:
250 return -bus_error_name_to_errno(name);
251}
252
253int bus_error_setfv(sd_bus_error *e, const char *name, const char *format, va_list ap) {
254
255 if (!name)
256 return 0;
60f067b4 257
4c89c718
MP
258 if (e) {
259 assert_return(!bus_error_is_dirty(e), -EINVAL);
60f067b4 260
4c89c718
MP
261 e->name = strdup(name);
262 if (!e->name) {
263 *e = BUS_ERROR_OOM;
264 return -ENOMEM;
265 }
60f067b4 266
4c89c718
MP
267 /* If we hit OOM on formatting the pretty message, we ignore
268 * this, since we at least managed to write the error name */
269 if (format)
270 (void) vasprintf((char**) &e->message, format, ap);
60f067b4 271
4c89c718
MP
272 e->_need_free = 1;
273 }
60f067b4 274
60f067b4
JS
275 return -bus_error_name_to_errno(name);
276}
277
278_public_ int sd_bus_error_setf(sd_bus_error *e, const char *name, const char *format, ...) {
279
280 if (format) {
281 int r;
282 va_list ap;
283
284 va_start(ap, format);
285 r = bus_error_setfv(e, name, format, ap);
286 va_end(ap);
287
288 return r;
289 }
290
291 return sd_bus_error_set(e, name, NULL);
292}
293
294_public_ int sd_bus_error_copy(sd_bus_error *dest, const sd_bus_error *e) {
295
296 if (!sd_bus_error_is_set(e))
297 return 0;
298 if (!dest)
299 goto finish;
300
301 assert_return(!bus_error_is_dirty(dest), -EINVAL);
302
303 /*
304 * _need_free < 0 indicates that the error is temporarily const, needs deep copying
305 * _need_free == 0 indicates that the error is perpetually const, needs no deep copying
306 * _need_free > 0 indicates that the error is fully dynamic, needs deep copying
307 */
308
309 if (e->_need_free == 0)
310 *dest = *e;
311 else {
312 dest->name = strdup(e->name);
313 if (!dest->name) {
314 *dest = BUS_ERROR_OOM;
315 return -ENOMEM;
316 }
317
318 if (e->message)
319 dest->message = strdup(e->message);
320
321 dest->_need_free = 1;
322 }
323
324finish:
325 return -bus_error_name_to_errno(e->name);
326}
327
328_public_ int sd_bus_error_set_const(sd_bus_error *e, const char *name, const char *message) {
329 if (!name)
330 return 0;
331 if (!e)
332 goto finish;
333
334 assert_return(!bus_error_is_dirty(e), -EINVAL);
335
336 *e = SD_BUS_ERROR_MAKE_CONST(name, message);
337
338finish:
339 return -bus_error_name_to_errno(name);
340}
341
342_public_ int sd_bus_error_is_set(const sd_bus_error *e) {
343 if (!e)
344 return 0;
345
346 return !!e->name;
347}
348
349_public_ int sd_bus_error_has_name(const sd_bus_error *e, const char *name) {
350 if (!e)
351 return 0;
352
353 return streq_ptr(e->name, name);
354}
355
356_public_ int sd_bus_error_get_errno(const sd_bus_error* e) {
357 if (!e)
358 return 0;
359
360 if (!e->name)
361 return 0;
362
363 return bus_error_name_to_errno(e->name);
364}
365
366static void bus_error_strerror(sd_bus_error *e, int error) {
367 size_t k = 64;
368 char *m;
369
370 assert(e);
371
372 for (;;) {
373 char *x;
374
375 m = new(char, k);
376 if (!m)
377 return;
378
379 errno = 0;
380 x = strerror_r(error, m, k);
381 if (errno == ERANGE || strlen(x) >= k - 1) {
382 free(m);
383 k *= 2;
384 continue;
385 }
386
5eef597e 387 if (errno) {
60f067b4
JS
388 free(m);
389 return;
390 }
391
392 if (x == m) {
393 if (e->_need_free > 0) {
394 /* Error is already dynamic, let's just update the message */
395 free((char*) e->message);
396 e->message = x;
397
398 } else {
399 char *t;
400 /* Error was const so far, let's make it dynamic, if we can */
401
402 t = strdup(e->name);
403 if (!t) {
404 free(m);
405 return;
406 }
407
408 e->_need_free = 1;
409 e->name = t;
410 e->message = x;
411 }
412 } else {
413 free(m);
414
415 if (e->_need_free > 0) {
416 char *t;
417
418 /* Error is dynamic, let's hence make the message also dynamic */
419 t = strdup(x);
420 if (!t)
421 return;
422
423 free((char*) e->message);
424 e->message = t;
425 } else {
426 /* Error is const, hence we can just override */
427 e->message = x;
428 }
429 }
430
431 return;
432 }
433}
434
435_public_ int sd_bus_error_set_errno(sd_bus_error *e, int error) {
436
437 if (error < 0)
438 error = -error;
439
440 if (!e)
441 return -error;
442 if (error == 0)
443 return -error;
444
445 assert_return(!bus_error_is_dirty(e), -EINVAL);
446
447 /* First, try a const translation */
448 *e = errno_to_bus_error_const(error);
449
450 if (!sd_bus_error_is_set(e)) {
451 int k;
452
453 /* If that didn't work, try a dynamic one. */
454
455 k = errno_to_bus_error_name_new(error, (char**) &e->name);
456 if (k > 0)
457 e->_need_free = 1;
458 else if (k < 0) {
459 *e = BUS_ERROR_OOM;
460 return -error;
461 } else
462 *e = BUS_ERROR_FAILED;
463 }
464
465 /* Now, fill in the message from strerror() if we can */
466 bus_error_strerror(e, error);
467 return -error;
468}
469
e735f4d4 470_public_ int sd_bus_error_set_errnofv(sd_bus_error *e, int error, const char *format, va_list ap) {
f47781d8 471 PROTECT_ERRNO;
60f067b4
JS
472 int r;
473
474 if (error < 0)
475 error = -error;
476
477 if (!e)
478 return -error;
479 if (error == 0)
480 return 0;
481
482 assert_return(!bus_error_is_dirty(e), -EINVAL);
483
484 /* First, try a const translation */
485 *e = errno_to_bus_error_const(error);
486
487 if (!sd_bus_error_is_set(e)) {
488 int k;
489
490 /* If that didn't work, try a dynamic one */
491
492 k = errno_to_bus_error_name_new(error, (char**) &e->name);
493 if (k > 0)
494 e->_need_free = 1;
495 else if (k < 0) {
496 *e = BUS_ERROR_OOM;
497 return -ENOMEM;
498 } else
499 *e = BUS_ERROR_FAILED;
500 }
501
502 if (format) {
503 char *m;
504
f47781d8 505 /* Then, let's try to fill in the supplied message */
60f067b4 506
f47781d8 507 errno = error; /* Make sure that %m resolves to the specified error */
60f067b4
JS
508 r = vasprintf(&m, format, ap);
509 if (r >= 0) {
510
511 if (e->_need_free <= 0) {
512 char *t;
513
514 t = strdup(e->name);
515 if (t) {
516 e->_need_free = 1;
517 e->name = t;
518 e->message = m;
519 return -error;
520 }
521
522 free(m);
523 } else {
524 free((char*) e->message);
525 e->message = m;
526 return -error;
527 }
528 }
529 }
530
531 /* If that didn't work, use strerror() for the message */
532 bus_error_strerror(e, error);
533 return -error;
534}
535
536_public_ int sd_bus_error_set_errnof(sd_bus_error *e, int error, const char *format, ...) {
537 int r;
538
539 if (error < 0)
540 error = -error;
541
542 if (!e)
543 return -error;
544 if (error == 0)
545 return 0;
546
547 assert_return(!bus_error_is_dirty(e), -EINVAL);
548
549 if (format) {
550 va_list ap;
551
552 va_start(ap, format);
e735f4d4 553 r = sd_bus_error_set_errnofv(e, error, format, ap);
60f067b4
JS
554 va_end(ap);
555
556 return r;
557 }
558
559 return sd_bus_error_set_errno(e, error);
560}
561
562const char *bus_error_message(const sd_bus_error *e, int error) {
563
564 if (e) {
db2df898 565 /* Sometimes, the D-Bus server is a little bit too verbose with
60f067b4
JS
566 * its error messages, so let's override them here */
567 if (sd_bus_error_has_name(e, SD_BUS_ERROR_ACCESS_DENIED))
568 return "Access denied";
569
570 if (e->message)
571 return e->message;
572 }
573
574 if (error < 0)
575 error = -error;
576
577 return strerror(error);
578}
f47781d8 579
4c89c718
MP
580static bool map_ok(const sd_bus_error_map *map) {
581 for (; map->code != BUS_ERROR_MAP_END_MARKER; map++)
582 if (!map->name || map->code <=0)
583 return false;
584 return true;
585}
586
f47781d8
MP
587_public_ int sd_bus_error_add_map(const sd_bus_error_map *map) {
588 const sd_bus_error_map **maps = NULL;
589 unsigned n = 0;
590
591 assert_return(map, -EINVAL);
4c89c718 592 assert_return(map_ok(map), -EINVAL);
f47781d8 593
4c89c718
MP
594 if (additional_error_maps)
595 for (; additional_error_maps[n] != NULL; n++)
f47781d8
MP
596 if (additional_error_maps[n] == map)
597 return 0;
f47781d8
MP
598
599 maps = realloc_multiply(additional_error_maps, sizeof(struct sd_bus_error_map*), n + 2);
600 if (!maps)
601 return -ENOMEM;
602
f47781d8
MP
603 maps[n] = map;
604 maps[n+1] = NULL;
605
606 additional_error_maps = maps;
607 return 1;
608}