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