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