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