| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-bus.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "bus-internal.h" |
| 7 |
#include "bus-message.h" |
| 8 |
#include "bus-signature.h" |
| 9 |
#include "bus-type.h" |
| 10 |
#include "fd-util.h" |
| 11 |
#include "iovec-util.h" |
| 12 |
#include "log.h" |
| 13 |
#include "memfd-util.h" |
| 14 |
#include "memory-util.h" |
| 15 |
#include "string-util.h" |
| 16 |
#include "strv.h" |
| 17 |
#include "utf8.h" |
| 18 |
|
| 19 |
static int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored); |
| 20 |
static int message_parse_fields(sd_bus_message *m, bool got_ctrunc); |
| 21 |
|
| 22 |
static void* adjust_pointer(const void *p, void *old_base, size_t sz, void *new_base) { |
| 23 |
|
| 24 |
if (!p) |
| 25 |
return NULL; |
| 26 |
|
| 27 |
if (old_base == new_base) |
| 28 |
return (void*) p; |
| 29 |
|
| 30 |
if ((uint8_t*) p < (uint8_t*) old_base) |
| 31 |
return (void*) p; |
| 32 |
|
| 33 |
if ((uint8_t*) p >= (uint8_t*) old_base + sz) |
| 34 |
return (void*) p; |
| 35 |
|
| 36 |
return (uint8_t*) new_base + ((uint8_t*) p - (uint8_t*) old_base); |
| 37 |
} |
| 38 |
|
| 39 |
static void message_free_part(sd_bus_message *m, BusMessageBodyPart *part) { |
| 40 |
assert(m); |
| 41 |
assert(part); |
| 42 |
|
| 43 |
if (part->memfd >= 0) { |
| 44 |
/* erase if requested, but only if the memfd is not sealed yet, i.e. is writable */ |
| 45 |
if (m->sensitive && !m->sealed) |
| 46 |
explicit_bzero_safe(part->data, part->size); |
| 47 |
|
| 48 |
close_and_munmap(part->memfd, part->mmap_begin, part->mapped); |
| 49 |
} else if (part->munmap_this) |
| 50 |
/* We don't erase sensitive data here, since the data is memory mapped from someone else, and |
| 51 |
* we just don't know if it's OK to write to it */ |
| 52 |
munmap(part->mmap_begin, part->mapped); |
| 53 |
else { |
| 54 |
/* Erase this if that is requested. Since this is regular memory we know we can write it. */ |
| 55 |
if (m->sensitive) |
| 56 |
explicit_bzero_safe(part->data, part->size); |
| 57 |
|
| 58 |
if (part->free_this) |
| 59 |
free(part->data); |
| 60 |
} |
| 61 |
|
| 62 |
if (part != &m->body) |
| 63 |
free(part); |
| 64 |
} |
| 65 |
|
| 66 |
static void message_reset_parts(sd_bus_message *m) { |
| 67 |
BusMessageBodyPart *part; |
| 68 |
|
| 69 |
assert(m); |
| 70 |
|
| 71 |
part = &m->body; |
| 72 |
while (m->n_body_parts > 0) { |
| 73 |
BusMessageBodyPart *next = part->next; |
| 74 |
message_free_part(m, part); |
| 75 |
part = next; |
| 76 |
m->n_body_parts--; |
| 77 |
} |
| 78 |
|
| 79 |
m->body_end = NULL; |
| 80 |
|
| 81 |
m->cached_rindex_part = NULL; |
| 82 |
m->cached_rindex_part_begin = 0; |
| 83 |
} |
| 84 |
|
| 85 |
static BusMessageContainer* message_get_last_container(sd_bus_message *m) { |
| 86 |
assert(m); |
| 87 |
|
| 88 |
if (m->n_containers == 0) |
| 89 |
return &m->root_container; |
| 90 |
|
| 91 |
assert(m->containers); |
| 92 |
return m->containers + m->n_containers - 1; |
| 93 |
} |
| 94 |
|
| 95 |
static void message_free_last_container(sd_bus_message *m) { |
| 96 |
BusMessageContainer *c; |
| 97 |
|
| 98 |
c = message_get_last_container(m); |
| 99 |
|
| 100 |
free(c->signature); |
| 101 |
free(c->peeked_signature); |
| 102 |
|
| 103 |
/* Move to previous container, but not if we are on root container */ |
| 104 |
if (m->n_containers > 0) |
| 105 |
m->n_containers--; |
| 106 |
} |
| 107 |
|
| 108 |
static void message_reset_containers(sd_bus_message *m) { |
| 109 |
assert(m); |
| 110 |
|
| 111 |
while (m->n_containers > 0) |
| 112 |
message_free_last_container(m); |
| 113 |
|
| 114 |
m->containers = mfree(m->containers); |
| 115 |
m->root_container.index = 0; |
| 116 |
} |
| 117 |
|
| 118 |
static sd_bus_message* message_free(sd_bus_message *m) { |
| 119 |
assert(m); |
| 120 |
|
| 121 |
message_reset_parts(m); |
| 122 |
|
| 123 |
if (m->free_header) |
| 124 |
free(m->header); |
| 125 |
|
| 126 |
/* Note that we don't unref m->bus here. That's already done by sd_bus_message_unref() as each user |
| 127 |
* reference to the bus message also is considered a reference to the bus connection itself. */ |
| 128 |
|
| 129 |
if (m->free_fds) { |
| 130 |
close_many(m->fds, m->n_fds); |
| 131 |
free(m->fds); |
| 132 |
} |
| 133 |
|
| 134 |
if (m->iovec != m->iovec_fixed) |
| 135 |
free(m->iovec); |
| 136 |
|
| 137 |
message_reset_containers(m); |
| 138 |
assert(m->n_containers == 0); |
| 139 |
message_free_last_container(m); |
| 140 |
|
| 141 |
bus_creds_done(&m->creds); |
| 142 |
return mfree(m); |
| 143 |
} |
| 144 |
|
| 145 |
static void* message_extend_fields(sd_bus_message *m, size_t sz, bool add_offset) { |
| 146 |
void *op, *np; |
| 147 |
size_t old_size, new_size, start; |
| 148 |
|
| 149 |
assert(m); |
| 150 |
|
| 151 |
if (m->poisoned) |
| 152 |
return NULL; |
| 153 |
|
| 154 |
old_size = sizeof(BusMessageHeader) + m->fields_size; |
| 155 |
start = ALIGN8(old_size); |
| 156 |
new_size = start + sz; |
| 157 |
|
| 158 |
if (new_size < start || new_size > UINT32_MAX) |
| 159 |
goto poison; |
| 160 |
|
| 161 |
if (old_size == new_size) |
| 162 |
return (uint8_t*) m->header + old_size; |
| 163 |
|
| 164 |
if (m->free_header) { |
| 165 |
np = realloc(m->header, ALIGN8(new_size)); |
| 166 |
if (!np) |
| 167 |
goto poison; |
| 168 |
} else { |
| 169 |
/* Initially, the header is allocated as part of |
| 170 |
* the sd_bus_message itself, let's replace it by |
| 171 |
* dynamic data */ |
| 172 |
|
| 173 |
np = malloc(ALIGN8(new_size)); |
| 174 |
if (!np) |
| 175 |
goto poison; |
| 176 |
|
| 177 |
memcpy(np, m->header, sizeof(BusMessageHeader)); |
| 178 |
} |
| 179 |
|
| 180 |
/* Zero out padding */ |
| 181 |
if (start > old_size) |
| 182 |
memzero((uint8_t*) np + old_size, start - old_size); |
| 183 |
|
| 184 |
op = m->header; |
| 185 |
m->header = np; |
| 186 |
m->fields_size = new_size - sizeof(BusMessageHeader); |
| 187 |
|
| 188 |
/* Adjust quick access pointers */ |
| 189 |
m->path = adjust_pointer(m->path, op, old_size, m->header); |
| 190 |
m->interface = adjust_pointer(m->interface, op, old_size, m->header); |
| 191 |
m->member = adjust_pointer(m->member, op, old_size, m->header); |
| 192 |
m->destination = adjust_pointer(m->destination, op, old_size, m->header); |
| 193 |
m->sender = adjust_pointer(m->sender, op, old_size, m->header); |
| 194 |
m->error.name = adjust_pointer(m->error.name, op, old_size, m->header); |
| 195 |
|
| 196 |
m->free_header = true; |
| 197 |
|
| 198 |
if (add_offset) { |
| 199 |
if (m->n_header_offsets >= ELEMENTSOF(m->header_offsets)) |
| 200 |
goto poison; |
| 201 |
|
| 202 |
m->header_offsets[m->n_header_offsets++] = new_size - sizeof(BusMessageHeader); |
| 203 |
} |
| 204 |
|
| 205 |
return (uint8_t*) np + start; |
| 206 |
|
| 207 |
poison: |
| 208 |
m->poisoned = true; |
| 209 |
return NULL; |
| 210 |
} |
| 211 |
|
| 212 |
static int message_append_field_string( |
| 213 |
sd_bus_message *m, |
| 214 |
uint64_t h, |
| 215 |
char type, |
| 216 |
const char *s, |
| 217 |
const char **ret) { |
| 218 |
|
| 219 |
size_t l; |
| 220 |
uint8_t *p; |
| 221 |
|
| 222 |
assert(m); |
| 223 |
|
| 224 |
/* dbus only allows 8-bit header field ids */ |
| 225 |
if (h > 0xFF) |
| 226 |
return -EINVAL; |
| 227 |
|
| 228 |
/* dbus doesn't allow strings over 32-bit */ |
| 229 |
l = strlen(s); |
| 230 |
if (l > UINT32_MAX) |
| 231 |
return -EINVAL; |
| 232 |
|
| 233 |
/* Signature "(yv)" where the variant contains "s" */ |
| 234 |
|
| 235 |
/* (field id byte + (signature length + signature 's' + NUL) + (string length + string + NUL)) */ |
| 236 |
p = message_extend_fields(m, 4 + 4 + l + 1, false); |
| 237 |
if (!p) |
| 238 |
return -ENOMEM; |
| 239 |
|
| 240 |
p[0] = (uint8_t) h; |
| 241 |
p[1] = 1; |
| 242 |
p[2] = type; |
| 243 |
p[3] = 0; |
| 244 |
|
| 245 |
((uint32_t*) p)[1] = l; |
| 246 |
memcpy(p + 8, s, l + 1); |
| 247 |
|
| 248 |
if (ret) |
| 249 |
*ret = (char*) p + 8; |
| 250 |
|
| 251 |
return 0; |
| 252 |
} |
| 253 |
|
| 254 |
static int message_append_field_signature( |
| 255 |
sd_bus_message *m, |
| 256 |
uint64_t h, |
| 257 |
const char *s, |
| 258 |
const char **ret) { |
| 259 |
|
| 260 |
size_t l; |
| 261 |
uint8_t *p; |
| 262 |
|
| 263 |
assert(m); |
| 264 |
|
| 265 |
/* dbus only allows 8-bit header field ids */ |
| 266 |
if (h > 0xFF) |
| 267 |
return -EINVAL; |
| 268 |
|
| 269 |
/* dbus doesn't allow signatures over 8-bit */ |
| 270 |
l = strlen(s); |
| 271 |
if (l > SD_BUS_MAXIMUM_SIGNATURE_LENGTH) |
| 272 |
return -EINVAL; |
| 273 |
|
| 274 |
/* Signature "(yv)" where the variant contains "g" */ |
| 275 |
|
| 276 |
/* (field id byte + (signature length + signature 'g' + NUL) + (string length + string + NUL)) */ |
| 277 |
p = message_extend_fields(m, 4 + 1 + l + 1, false); |
| 278 |
if (!p) |
| 279 |
return -ENOMEM; |
| 280 |
|
| 281 |
p[0] = (uint8_t) h; |
| 282 |
p[1] = 1; |
| 283 |
p[2] = SD_BUS_TYPE_SIGNATURE; |
| 284 |
p[3] = 0; |
| 285 |
p[4] = l; |
| 286 |
memcpy(p + 5, s, l + 1); |
| 287 |
|
| 288 |
if (ret) |
| 289 |
*ret = (const char*) p + 5; |
| 290 |
|
| 291 |
return 0; |
| 292 |
} |
| 293 |
|
| 294 |
static int message_append_field_uint32(sd_bus_message *m, uint64_t h, uint32_t x) { |
| 295 |
uint8_t *p; |
| 296 |
|
| 297 |
assert(m); |
| 298 |
|
| 299 |
/* dbus only allows 8-bit header field ids */ |
| 300 |
if (h > 0xFF) |
| 301 |
return -EINVAL; |
| 302 |
|
| 303 |
/* (field id byte + (signature length + signature 'u' + NUL) + value) */ |
| 304 |
p = message_extend_fields(m, 4 + 4, false); |
| 305 |
if (!p) |
| 306 |
return -ENOMEM; |
| 307 |
|
| 308 |
p[0] = (uint8_t) h; |
| 309 |
p[1] = 1; |
| 310 |
p[2] = 'u'; |
| 311 |
p[3] = 0; |
| 312 |
|
| 313 |
((uint32_t*) p)[1] = x; |
| 314 |
|
| 315 |
return 0; |
| 316 |
} |
| 317 |
|
| 318 |
static int message_append_reply_cookie(sd_bus_message *m, uint64_t cookie) { |
| 319 |
assert(m); |
| 320 |
|
| 321 |
/* 64-bit cookies are not supported */ |
| 322 |
if (cookie > UINT32_MAX) |
| 323 |
return -EOPNOTSUPP; |
| 324 |
|
| 325 |
return message_append_field_uint32(m, BUS_MESSAGE_HEADER_REPLY_SERIAL, (uint32_t) cookie); |
| 326 |
} |
| 327 |
|
| 328 |
static int message_from_header( |
| 329 |
sd_bus *bus, |
| 330 |
void *buffer, |
| 331 |
size_t message_size, |
| 332 |
int *fds, |
| 333 |
size_t n_fds, |
| 334 |
const char *label, |
| 335 |
sd_bus_message **ret) { |
| 336 |
|
| 337 |
_cleanup_free_ sd_bus_message *m = NULL; |
| 338 |
BusMessageHeader *h; |
| 339 |
size_t a, label_sz = 0; /* avoid false maybe-uninitialized warning */ |
| 340 |
|
| 341 |
assert(bus); |
| 342 |
assert(buffer || message_size <= 0); |
| 343 |
assert(fds || n_fds <= 0); |
| 344 |
assert(ret); |
| 345 |
|
| 346 |
if (message_size < sizeof(BusMessageHeader)) |
| 347 |
return -EBADMSG; |
| 348 |
|
| 349 |
h = buffer; |
| 350 |
if (!IN_SET(h->version, 1, 2)) |
| 351 |
return -EBADMSG; |
| 352 |
|
| 353 |
if (h->type == _SD_BUS_MESSAGE_TYPE_INVALID) |
| 354 |
return -EBADMSG; |
| 355 |
|
| 356 |
if (!IN_SET(h->endian, BUS_LITTLE_ENDIAN, BUS_BIG_ENDIAN)) |
| 357 |
return -EBADMSG; |
| 358 |
|
| 359 |
/* Note that we are happy with unknown flags in the flags header! */ |
| 360 |
|
| 361 |
a = ALIGN(sizeof(sd_bus_message)); |
| 362 |
/* Silence static analyzers, ALIGN cannot overflow for sizeof() */ |
| 363 |
assert(a != SIZE_MAX); |
| 364 |
|
| 365 |
if (label) { |
| 366 |
label_sz = strlen(label); |
| 367 |
assert_se(INC_SAFE(&a, label_sz + 1)); |
| 368 |
} |
| 369 |
|
| 370 |
m = malloc0(a); |
| 371 |
if (!m) |
| 372 |
return -ENOMEM; |
| 373 |
|
| 374 |
m->creds = (sd_bus_creds) { SD_BUS_CREDS_INIT_FIELDS }; |
| 375 |
m->sealed = true; |
| 376 |
m->header = buffer; |
| 377 |
|
| 378 |
if (h->serial == 0) |
| 379 |
return -EBADMSG; |
| 380 |
|
| 381 |
m->fields_size = BUS_MESSAGE_BSWAP32(m, h->fields_size); |
| 382 |
m->body_size = BUS_MESSAGE_BSWAP32(m, h->body_size); |
| 383 |
|
| 384 |
assert(message_size >= sizeof(BusMessageHeader)); |
| 385 |
if (ALIGN8(m->fields_size) > message_size - sizeof(BusMessageHeader) || |
| 386 |
m->body_size != message_size - sizeof(BusMessageHeader) - ALIGN8(m->fields_size)) |
| 387 |
return -EBADMSG; |
| 388 |
|
| 389 |
m->fds = fds; |
| 390 |
m->n_fds = n_fds; |
| 391 |
|
| 392 |
if (label) { |
| 393 |
m->creds.label = (char*) m + ALIGN(sizeof(sd_bus_message)); |
| 394 |
memcpy(m->creds.label, label, label_sz + 1); |
| 395 |
|
| 396 |
m->creds.mask |= SD_BUS_CREDS_SELINUX_CONTEXT; |
| 397 |
} |
| 398 |
|
| 399 |
m->n_ref = 1; |
| 400 |
m->bus = sd_bus_ref(bus); |
| 401 |
|
| 402 |
*ret = TAKE_PTR(m); |
| 403 |
|
| 404 |
return 0; |
| 405 |
} |
| 406 |
|
| 407 |
int bus_message_from_malloc( |
| 408 |
sd_bus *bus, |
| 409 |
void *buffer, |
| 410 |
size_t length, |
| 411 |
int *fds, |
| 412 |
size_t n_fds, |
| 413 |
bool got_ctrunc, |
| 414 |
const char *label, |
| 415 |
sd_bus_message **ret) { |
| 416 |
|
| 417 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 418 |
size_t sz; |
| 419 |
int r; |
| 420 |
|
| 421 |
assert(ret); |
| 422 |
|
| 423 |
r = message_from_header( |
| 424 |
bus, |
| 425 |
buffer, length, |
| 426 |
fds, n_fds, |
| 427 |
label, |
| 428 |
&m); |
| 429 |
if (r < 0) |
| 430 |
return r; |
| 431 |
|
| 432 |
sz = length - sizeof(BusMessageHeader) - ALIGN8(m->fields_size); |
| 433 |
if (sz > 0) { |
| 434 |
m->n_body_parts = 1; |
| 435 |
m->body.data = (uint8_t*) buffer + sizeof(BusMessageHeader) + ALIGN8(m->fields_size); |
| 436 |
m->body.size = sz; |
| 437 |
m->body.sealed = true; |
| 438 |
m->body.memfd = -EBADF; |
| 439 |
} |
| 440 |
|
| 441 |
m->n_iovec = 1; |
| 442 |
m->iovec = m->iovec_fixed; |
| 443 |
m->iovec[0] = IOVEC_MAKE(buffer, length); |
| 444 |
|
| 445 |
r = message_parse_fields(m, got_ctrunc); |
| 446 |
if (r < 0) |
| 447 |
return r; |
| 448 |
|
| 449 |
/* We take possession of the memory and fds now */ |
| 450 |
m->free_header = true; |
| 451 |
m->free_fds = true; |
| 452 |
|
| 453 |
*ret = TAKE_PTR(m); |
| 454 |
return 0; |
| 455 |
} |
| 456 |
|
| 457 |
_public_ int sd_bus_message_new( |
| 458 |
sd_bus *bus, |
| 459 |
sd_bus_message **ret, |
| 460 |
uint8_t type) { |
| 461 |
|
| 462 |
assert_return(bus, -ENOTCONN); |
| 463 |
assert_return(bus = bus_resolve(bus), -ENOPKG); |
| 464 |
assert_return(bus->state != BUS_UNSET, -ENOTCONN); |
| 465 |
assert_return(ret, -EINVAL); |
| 466 |
/* Creation of messages with _SD_BUS_MESSAGE_TYPE_INVALID is allowed. */ |
| 467 |
assert_return(type < _SD_BUS_MESSAGE_TYPE_MAX, -EINVAL); |
| 468 |
|
| 469 |
/* Silence static analyzers, ALIGN cannot overflow for sizeof() */ |
| 470 |
assert(ALIGN(sizeof(sd_bus_message)) != SIZE_MAX); |
| 471 |
sd_bus_message *t = malloc0(ALIGN(sizeof(sd_bus_message)) + sizeof(BusMessageHeader)); |
| 472 |
if (!t) |
| 473 |
return -ENOMEM; |
| 474 |
|
| 475 |
t->n_ref = 1; |
| 476 |
t->creds = (sd_bus_creds) { SD_BUS_CREDS_INIT_FIELDS }; |
| 477 |
t->bus = sd_bus_ref(bus); |
| 478 |
t->header = (BusMessageHeader*) ((uint8_t*) t + ALIGN(sizeof(struct sd_bus_message))); |
| 479 |
t->header->endian = BUS_NATIVE_ENDIAN; |
| 480 |
t->header->type = type; |
| 481 |
t->header->version = bus->message_version; |
| 482 |
t->allow_fds = bus->can_fds || !IN_SET(bus->state, BUS_HELLO, BUS_RUNNING); |
| 483 |
|
| 484 |
if (bus->allow_interactive_authorization) |
| 485 |
t->header->flags |= BUS_MESSAGE_ALLOW_INTERACTIVE_AUTHORIZATION; |
| 486 |
|
| 487 |
*ret = t; |
| 488 |
return 0; |
| 489 |
} |
| 490 |
|
| 491 |
_public_ int sd_bus_message_new_signal_to( |
| 492 |
sd_bus *bus, |
| 493 |
sd_bus_message **ret, |
| 494 |
const char *destination, |
| 495 |
const char *path, |
| 496 |
const char *interface, |
| 497 |
const char *member) { |
| 498 |
|
| 499 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *t = NULL; |
| 500 |
int r; |
| 501 |
|
| 502 |
assert_return(bus, -ENOTCONN); |
| 503 |
assert_return(bus = bus_resolve(bus), -ENOPKG); |
| 504 |
assert_return(bus->state != BUS_UNSET, -ENOTCONN); |
| 505 |
assert_return(!destination || service_name_is_valid(destination), -EINVAL); |
| 506 |
assert_return(object_path_is_valid(path), -EINVAL); |
| 507 |
assert_return(interface_name_is_valid(interface), -EINVAL); |
| 508 |
assert_return(member_name_is_valid(member), -EINVAL); |
| 509 |
assert_return(ret, -EINVAL); |
| 510 |
|
| 511 |
r = sd_bus_message_new(bus, &t, SD_BUS_MESSAGE_SIGNAL); |
| 512 |
if (r < 0) |
| 513 |
return -ENOMEM; |
| 514 |
|
| 515 |
assert(t); |
| 516 |
|
| 517 |
t->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED; |
| 518 |
|
| 519 |
r = message_append_field_string(t, BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path); |
| 520 |
if (r < 0) |
| 521 |
return r; |
| 522 |
r = message_append_field_string(t, BUS_MESSAGE_HEADER_INTERFACE, SD_BUS_TYPE_STRING, interface, &t->interface); |
| 523 |
if (r < 0) |
| 524 |
return r; |
| 525 |
r = message_append_field_string(t, BUS_MESSAGE_HEADER_MEMBER, SD_BUS_TYPE_STRING, member, &t->member); |
| 526 |
if (r < 0) |
| 527 |
return r; |
| 528 |
|
| 529 |
if (destination) { |
| 530 |
r = message_append_field_string(t, BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &t->destination); |
| 531 |
if (r < 0) |
| 532 |
return r; |
| 533 |
} |
| 534 |
|
| 535 |
*ret = TAKE_PTR(t); |
| 536 |
return 0; |
| 537 |
} |
| 538 |
|
| 539 |
_public_ int sd_bus_message_new_signal( |
| 540 |
sd_bus *bus, |
| 541 |
sd_bus_message **ret, |
| 542 |
const char *path, |
| 543 |
const char *interface, |
| 544 |
const char *member) { |
| 545 |
|
| 546 |
return sd_bus_message_new_signal_to(bus, ret, NULL, path, interface, member); |
| 547 |
} |
| 548 |
|
| 549 |
_public_ int sd_bus_message_new_method_call( |
| 550 |
sd_bus *bus, |
| 551 |
sd_bus_message **ret, |
| 552 |
const char *destination, |
| 553 |
const char *path, |
| 554 |
const char *interface, |
| 555 |
const char *member) { |
| 556 |
|
| 557 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *t = NULL; |
| 558 |
int r; |
| 559 |
|
| 560 |
assert_return(bus, -ENOTCONN); |
| 561 |
assert_return(bus = bus_resolve(bus), -ENOPKG); |
| 562 |
assert_return(bus->state != BUS_UNSET, -ENOTCONN); |
| 563 |
assert_return(!destination || service_name_is_valid(destination), -EINVAL); |
| 564 |
assert_return(object_path_is_valid(path), -EINVAL); |
| 565 |
assert_return(!interface || interface_name_is_valid(interface), -EINVAL); |
| 566 |
assert_return(member_name_is_valid(member), -EINVAL); |
| 567 |
assert_return(ret, -EINVAL); |
| 568 |
|
| 569 |
r = sd_bus_message_new(bus, &t, SD_BUS_MESSAGE_METHOD_CALL); |
| 570 |
if (r < 0) |
| 571 |
return -ENOMEM; |
| 572 |
|
| 573 |
assert(t); |
| 574 |
|
| 575 |
r = message_append_field_string(t, BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path); |
| 576 |
if (r < 0) |
| 577 |
return r; |
| 578 |
r = message_append_field_string(t, BUS_MESSAGE_HEADER_MEMBER, SD_BUS_TYPE_STRING, member, &t->member); |
| 579 |
if (r < 0) |
| 580 |
return r; |
| 581 |
|
| 582 |
if (interface) { |
| 583 |
r = message_append_field_string(t, BUS_MESSAGE_HEADER_INTERFACE, SD_BUS_TYPE_STRING, interface, &t->interface); |
| 584 |
if (r < 0) |
| 585 |
return r; |
| 586 |
} |
| 587 |
|
| 588 |
if (destination) { |
| 589 |
r = message_append_field_string(t, BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &t->destination); |
| 590 |
if (r < 0) |
| 591 |
return r; |
| 592 |
} |
| 593 |
|
| 594 |
*ret = TAKE_PTR(t); |
| 595 |
return 0; |
| 596 |
} |
| 597 |
|
| 598 |
static int message_new_reply( |
| 599 |
sd_bus_message *call, |
| 600 |
uint8_t type, |
| 601 |
sd_bus_message **ret) { |
| 602 |
|
| 603 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *t = NULL; |
| 604 |
uint64_t cookie; |
| 605 |
int r; |
| 606 |
|
| 607 |
assert_return(call, -EINVAL); |
| 608 |
assert_return(call->sealed, -EPERM); |
| 609 |
assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL); |
| 610 |
assert_return(call->bus->state != BUS_UNSET, -ENOTCONN); |
| 611 |
assert_return(ret, -EINVAL); |
| 612 |
|
| 613 |
cookie = BUS_MESSAGE_COOKIE(call); |
| 614 |
if (cookie == 0) |
| 615 |
return -EOPNOTSUPP; |
| 616 |
|
| 617 |
r = sd_bus_message_new(call->bus, &t, type); |
| 618 |
if (r < 0) |
| 619 |
return -ENOMEM; |
| 620 |
|
| 621 |
assert(t); |
| 622 |
|
| 623 |
t->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED; |
| 624 |
t->reply_cookie = cookie; |
| 625 |
r = message_append_reply_cookie(t, t->reply_cookie); |
| 626 |
if (r < 0) |
| 627 |
return r; |
| 628 |
|
| 629 |
if (call->sender) { |
| 630 |
r = message_append_field_string(t, BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, call->sender, &t->destination); |
| 631 |
if (r < 0) |
| 632 |
return r; |
| 633 |
} |
| 634 |
|
| 635 |
t->dont_send = FLAGS_SET(call->header->flags, BUS_MESSAGE_NO_REPLY_EXPECTED); |
| 636 |
t->enforced_reply_signature = call->enforced_reply_signature; |
| 637 |
|
| 638 |
/* let's copy the sensitive flag over. Let's do that as a safety precaution to keep a transaction |
| 639 |
* wholly sensitive if already the incoming message was sensitive. This is particularly useful when a |
| 640 |
* vtable record sets the SD_BUS_VTABLE_SENSITIVE flag on a method call, since this means it applies |
| 641 |
* to both the message call and the reply. */ |
| 642 |
t->sensitive = call->sensitive; |
| 643 |
|
| 644 |
*ret = TAKE_PTR(t); |
| 645 |
return 0; |
| 646 |
} |
| 647 |
|
| 648 |
_public_ int sd_bus_message_new_method_return( |
| 649 |
sd_bus_message *call, |
| 650 |
sd_bus_message **ret) { |
| 651 |
|
| 652 |
return message_new_reply(call, SD_BUS_MESSAGE_METHOD_RETURN, ret); |
| 653 |
} |
| 654 |
|
| 655 |
_public_ int sd_bus_message_new_method_error( |
| 656 |
sd_bus_message *call, |
| 657 |
sd_bus_message **ret, |
| 658 |
const sd_bus_error *e) { |
| 659 |
|
| 660 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *t = NULL; |
| 661 |
int r; |
| 662 |
|
| 663 |
assert_return(sd_bus_error_is_set(e), -EINVAL); |
| 664 |
assert_return(ret, -EINVAL); |
| 665 |
|
| 666 |
r = message_new_reply(call, SD_BUS_MESSAGE_METHOD_ERROR, &t); |
| 667 |
if (r < 0) |
| 668 |
return r; |
| 669 |
|
| 670 |
r = message_append_field_string(t, BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, e->name, &t->error.name); |
| 671 |
if (r < 0) |
| 672 |
return r; |
| 673 |
|
| 674 |
if (e->message) { |
| 675 |
r = message_append_basic(t, SD_BUS_TYPE_STRING, e->message, (const void**) &t->error.message); |
| 676 |
if (r < 0) |
| 677 |
return r; |
| 678 |
} |
| 679 |
|
| 680 |
t->error._need_free = -1; |
| 681 |
|
| 682 |
*ret = TAKE_PTR(t); |
| 683 |
return 0; |
| 684 |
} |
| 685 |
|
| 686 |
_public_ int sd_bus_message_new_method_errorf( |
| 687 |
sd_bus_message *call, |
| 688 |
sd_bus_message **ret, |
| 689 |
const char *name, |
| 690 |
const char *format, |
| 691 |
...) { |
| 692 |
|
| 693 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 694 |
va_list ap; |
| 695 |
|
| 696 |
assert_return(name, -EINVAL); |
| 697 |
assert_return(ret, -EINVAL); |
| 698 |
|
| 699 |
va_start(ap, format); |
| 700 |
sd_bus_error_setfv(&error, name, format, ap); |
| 701 |
va_end(ap); |
| 702 |
|
| 703 |
return sd_bus_message_new_method_error(call, ret, &error); |
| 704 |
} |
| 705 |
|
| 706 |
_public_ int sd_bus_message_new_method_errno( |
| 707 |
sd_bus_message *call, |
| 708 |
sd_bus_message **ret, |
| 709 |
int error, |
| 710 |
const sd_bus_error *e) { |
| 711 |
|
| 712 |
_cleanup_(sd_bus_error_free) sd_bus_error berror = SD_BUS_ERROR_NULL; |
| 713 |
|
| 714 |
if (sd_bus_error_is_set(e)) |
| 715 |
return sd_bus_message_new_method_error(call, ret, e); |
| 716 |
|
| 717 |
sd_bus_error_set_errno(&berror, error); |
| 718 |
|
| 719 |
return sd_bus_message_new_method_error(call, ret, &berror); |
| 720 |
} |
| 721 |
|
| 722 |
_public_ int sd_bus_message_new_method_errnof( |
| 723 |
sd_bus_message *call, |
| 724 |
sd_bus_message **ret, |
| 725 |
int error, |
| 726 |
const char *format, |
| 727 |
...) { |
| 728 |
|
| 729 |
_cleanup_(sd_bus_error_free) sd_bus_error berror = SD_BUS_ERROR_NULL; |
| 730 |
va_list ap; |
| 731 |
|
| 732 |
va_start(ap, format); |
| 733 |
sd_bus_error_set_errnofv(&berror, error, format, ap); |
| 734 |
va_end(ap); |
| 735 |
|
| 736 |
return sd_bus_message_new_method_error(call, ret, &berror); |
| 737 |
} |
| 738 |
|
| 739 |
void bus_message_set_sender_local(sd_bus *bus, sd_bus_message *m) { |
| 740 |
assert(bus); |
| 741 |
assert(m); |
| 742 |
|
| 743 |
m->sender = m->creds.unique_name = (char*) "org.freedesktop.DBus.Local"; |
| 744 |
m->creds.well_known_names_local = true; |
| 745 |
m->creds.mask |= (SD_BUS_CREDS_UNIQUE_NAME|SD_BUS_CREDS_WELL_KNOWN_NAMES) & bus->creds_mask; |
| 746 |
} |
| 747 |
|
| 748 |
void bus_message_set_sender_driver(sd_bus *bus, sd_bus_message *m) { |
| 749 |
assert(bus); |
| 750 |
assert(m); |
| 751 |
|
| 752 |
m->sender = m->creds.unique_name = (char*) "org.freedesktop.DBus"; |
| 753 |
m->creds.well_known_names_driver = true; |
| 754 |
m->creds.mask |= (SD_BUS_CREDS_UNIQUE_NAME|SD_BUS_CREDS_WELL_KNOWN_NAMES) & bus->creds_mask; |
| 755 |
} |
| 756 |
|
| 757 |
int bus_message_new_synthetic_error( |
| 758 |
sd_bus *bus, |
| 759 |
uint64_t cookie, |
| 760 |
const sd_bus_error *e, |
| 761 |
sd_bus_message **m) { |
| 762 |
|
| 763 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *t = NULL; |
| 764 |
int r; |
| 765 |
|
| 766 |
assert(bus); |
| 767 |
assert(sd_bus_error_is_set(e)); |
| 768 |
assert(m); |
| 769 |
|
| 770 |
r = sd_bus_message_new(bus, &t, SD_BUS_MESSAGE_METHOD_ERROR); |
| 771 |
if (r < 0) |
| 772 |
return -ENOMEM; |
| 773 |
|
| 774 |
assert(t); |
| 775 |
|
| 776 |
t->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED; |
| 777 |
t->reply_cookie = cookie; |
| 778 |
|
| 779 |
r = message_append_reply_cookie(t, t->reply_cookie); |
| 780 |
if (r < 0) |
| 781 |
return r; |
| 782 |
|
| 783 |
if (bus && bus->unique_name) { |
| 784 |
r = message_append_field_string(t, BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, bus->unique_name, &t->destination); |
| 785 |
if (r < 0) |
| 786 |
return r; |
| 787 |
} |
| 788 |
|
| 789 |
r = message_append_field_string(t, BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, e->name, &t->error.name); |
| 790 |
if (r < 0) |
| 791 |
return r; |
| 792 |
|
| 793 |
if (e->message) { |
| 794 |
r = message_append_basic(t, SD_BUS_TYPE_STRING, e->message, (const void**) &t->error.message); |
| 795 |
if (r < 0) |
| 796 |
return r; |
| 797 |
} |
| 798 |
|
| 799 |
t->error._need_free = -1; |
| 800 |
|
| 801 |
bus_message_set_sender_driver(bus, t); |
| 802 |
|
| 803 |
*m = TAKE_PTR(t); |
| 804 |
return 0; |
| 805 |
} |
| 806 |
|
| 807 |
_public_ sd_bus_message* sd_bus_message_ref(sd_bus_message *m) { |
| 808 |
if (!m) |
| 809 |
return NULL; |
| 810 |
|
| 811 |
/* We are fine if this message so far was either explicitly reffed or not reffed but queued into at |
| 812 |
* least one bus connection object. */ |
| 813 |
assert(m->n_ref > 0 || m->n_queued > 0); |
| 814 |
|
| 815 |
m->n_ref++; |
| 816 |
|
| 817 |
/* Each user reference to a bus message shall also be considered a ref on the bus */ |
| 818 |
sd_bus_ref(m->bus); |
| 819 |
return m; |
| 820 |
} |
| 821 |
|
| 822 |
_public_ sd_bus_message* sd_bus_message_unref(sd_bus_message *m) { |
| 823 |
if (!m) |
| 824 |
return NULL; |
| 825 |
|
| 826 |
assert(m->n_ref > 0); |
| 827 |
|
| 828 |
sd_bus_unref(m->bus); /* Each regular ref is also a ref on the bus connection. Let's hence drop it |
| 829 |
* here. Note we have to do this before decrementing our own n_ref here, since |
| 830 |
* otherwise, if this message is currently queued sd_bus_unref() might call |
| 831 |
* bus_message_unref_queued() for this which might then destroy the message |
| 832 |
* while we are still processing it. */ |
| 833 |
m->n_ref--; |
| 834 |
|
| 835 |
if (m->n_ref > 0 || m->n_queued > 0) |
| 836 |
return NULL; |
| 837 |
|
| 838 |
/* Unset the bus field if neither the user has a reference nor this message is queued. We are careful |
| 839 |
* to reset the field only after the last reference to the bus is dropped, after all we might keep |
| 840 |
* multiple references to the bus, once for each reference kept on ourselves. */ |
| 841 |
m->bus = NULL; |
| 842 |
|
| 843 |
return message_free(m); |
| 844 |
} |
| 845 |
|
| 846 |
sd_bus_message* bus_message_ref_queued(sd_bus_message *m, sd_bus *bus) { |
| 847 |
if (!m) |
| 848 |
return NULL; |
| 849 |
|
| 850 |
/* If this is a different bus than the message is associated with, then implicitly turn this into a |
| 851 |
* regular reference. This means that you can create a memory leak by enqueuing a message generated |
| 852 |
* on one bus onto another at the same time as enqueueing a message from the second one on the first, |
| 853 |
* as we'll not detect the cyclic references there. */ |
| 854 |
if (bus != m->bus) |
| 855 |
return sd_bus_message_ref(m); |
| 856 |
|
| 857 |
assert(m->n_ref > 0 || m->n_queued > 0); |
| 858 |
m->n_queued++; |
| 859 |
|
| 860 |
return m; |
| 861 |
} |
| 862 |
|
| 863 |
sd_bus_message* bus_message_unref_queued(sd_bus_message *m, sd_bus *bus) { |
| 864 |
if (!m) |
| 865 |
return NULL; |
| 866 |
|
| 867 |
if (bus != m->bus) |
| 868 |
return sd_bus_message_unref(m); |
| 869 |
|
| 870 |
assert(m->n_queued > 0); |
| 871 |
m->n_queued--; |
| 872 |
|
| 873 |
if (m->n_ref > 0 || m->n_queued > 0) |
| 874 |
return NULL; |
| 875 |
|
| 876 |
m->bus = NULL; |
| 877 |
|
| 878 |
return message_free(m); |
| 879 |
} |
| 880 |
|
| 881 |
_public_ int sd_bus_message_get_type(sd_bus_message *m, uint8_t *ret) { |
| 882 |
assert_return(m, -EINVAL); |
| 883 |
assert_return(ret, -EINVAL); |
| 884 |
|
| 885 |
*ret = m->header->type; |
| 886 |
return 0; |
| 887 |
} |
| 888 |
|
| 889 |
_public_ int sd_bus_message_get_cookie(sd_bus_message *m, uint64_t *ret) { |
| 890 |
uint64_t c; |
| 891 |
|
| 892 |
assert_return(m, -EINVAL); |
| 893 |
assert_return(ret, -EINVAL); |
| 894 |
|
| 895 |
c = BUS_MESSAGE_COOKIE(m); |
| 896 |
if (c == 0) |
| 897 |
return -ENODATA; |
| 898 |
|
| 899 |
*ret = BUS_MESSAGE_COOKIE(m); |
| 900 |
return 0; |
| 901 |
} |
| 902 |
|
| 903 |
_public_ int sd_bus_message_get_reply_cookie(sd_bus_message *m, uint64_t *ret) { |
| 904 |
assert_return(m, -EINVAL); |
| 905 |
assert_return(ret, -EINVAL); |
| 906 |
|
| 907 |
if (m->reply_cookie == 0) |
| 908 |
return -ENODATA; |
| 909 |
|
| 910 |
*ret = m->reply_cookie; |
| 911 |
return 0; |
| 912 |
} |
| 913 |
|
| 914 |
_public_ int sd_bus_message_get_expect_reply(sd_bus_message *m) { |
| 915 |
assert_return(m, -EINVAL); |
| 916 |
|
| 917 |
return m->header->type == SD_BUS_MESSAGE_METHOD_CALL && |
| 918 |
!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED); |
| 919 |
} |
| 920 |
|
| 921 |
_public_ int sd_bus_message_get_auto_start(sd_bus_message *m) { |
| 922 |
assert_return(m, -EINVAL); |
| 923 |
|
| 924 |
return !(m->header->flags & BUS_MESSAGE_NO_AUTO_START); |
| 925 |
} |
| 926 |
|
| 927 |
_public_ int sd_bus_message_get_allow_interactive_authorization(sd_bus_message *m) { |
| 928 |
assert_return(m, -EINVAL); |
| 929 |
|
| 930 |
return m->header->type == SD_BUS_MESSAGE_METHOD_CALL && |
| 931 |
(m->header->flags & BUS_MESSAGE_ALLOW_INTERACTIVE_AUTHORIZATION); |
| 932 |
} |
| 933 |
|
| 934 |
_public_ const char* sd_bus_message_get_path(sd_bus_message *m) { |
| 935 |
assert_return(m, NULL); |
| 936 |
|
| 937 |
return m->path; |
| 938 |
} |
| 939 |
|
| 940 |
_public_ const char* sd_bus_message_get_interface(sd_bus_message *m) { |
| 941 |
assert_return(m, NULL); |
| 942 |
|
| 943 |
return m->interface; |
| 944 |
} |
| 945 |
|
| 946 |
_public_ const char* sd_bus_message_get_member(sd_bus_message *m) { |
| 947 |
assert_return(m, NULL); |
| 948 |
|
| 949 |
return m->member; |
| 950 |
} |
| 951 |
|
| 952 |
_public_ const char* sd_bus_message_get_destination(sd_bus_message *m) { |
| 953 |
assert_return(m, NULL); |
| 954 |
|
| 955 |
return m->destination; |
| 956 |
} |
| 957 |
|
| 958 |
_public_ const char* sd_bus_message_get_sender(sd_bus_message *m) { |
| 959 |
assert_return(m, NULL); |
| 960 |
|
| 961 |
return m->sender; |
| 962 |
} |
| 963 |
|
| 964 |
_public_ const sd_bus_error* sd_bus_message_get_error(sd_bus_message *m) { |
| 965 |
assert_return(m, NULL); |
| 966 |
|
| 967 |
if (!sd_bus_error_is_set(&m->error)) |
| 968 |
return NULL; |
| 969 |
|
| 970 |
return &m->error; |
| 971 |
} |
| 972 |
|
| 973 |
_public_ int sd_bus_message_get_monotonic_usec(sd_bus_message *m, uint64_t *ret) { |
| 974 |
assert_return(m, -EINVAL); |
| 975 |
assert_return(ret, -EINVAL); |
| 976 |
|
| 977 |
if (m->monotonic <= 0) |
| 978 |
return -ENODATA; |
| 979 |
|
| 980 |
*ret = m->monotonic; |
| 981 |
return 0; |
| 982 |
} |
| 983 |
|
| 984 |
_public_ int sd_bus_message_get_realtime_usec(sd_bus_message *m, uint64_t *ret) { |
| 985 |
assert_return(m, -EINVAL); |
| 986 |
assert_return(ret, -EINVAL); |
| 987 |
|
| 988 |
if (m->realtime <= 0) |
| 989 |
return -ENODATA; |
| 990 |
|
| 991 |
*ret = m->realtime; |
| 992 |
return 0; |
| 993 |
} |
| 994 |
|
| 995 |
_public_ int sd_bus_message_get_seqnum(sd_bus_message *m, uint64_t *ret) { |
| 996 |
assert_return(m, -EINVAL); |
| 997 |
assert_return(ret, -EINVAL); |
| 998 |
|
| 999 |
if (m->seqnum <= 0) |
| 1000 |
return -ENODATA; |
| 1001 |
|
| 1002 |
*ret = m->seqnum; |
| 1003 |
return 0; |
| 1004 |
} |
| 1005 |
|
| 1006 |
_public_ sd_bus_creds* sd_bus_message_get_creds(sd_bus_message *m) { |
| 1007 |
assert_return(m, NULL); |
| 1008 |
|
| 1009 |
if (m->creds.mask == 0) |
| 1010 |
return NULL; |
| 1011 |
|
| 1012 |
return &m->creds; |
| 1013 |
} |
| 1014 |
|
| 1015 |
_public_ int sd_bus_message_is_signal( |
| 1016 |
sd_bus_message *m, |
| 1017 |
const char *interface, |
| 1018 |
const char *member) { |
| 1019 |
|
| 1020 |
assert_return(m, -EINVAL); |
| 1021 |
|
| 1022 |
if (m->header->type != SD_BUS_MESSAGE_SIGNAL) |
| 1023 |
return 0; |
| 1024 |
|
| 1025 |
if (interface && !streq_ptr(m->interface, interface)) |
| 1026 |
return 0; |
| 1027 |
|
| 1028 |
if (member && !streq_ptr(m->member, member)) |
| 1029 |
return 0; |
| 1030 |
|
| 1031 |
return 1; |
| 1032 |
} |
| 1033 |
|
| 1034 |
_public_ int sd_bus_message_is_method_call( |
| 1035 |
sd_bus_message *m, |
| 1036 |
const char *interface, |
| 1037 |
const char *member) { |
| 1038 |
|
| 1039 |
assert_return(m, -EINVAL); |
| 1040 |
|
| 1041 |
if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL) |
| 1042 |
return 0; |
| 1043 |
|
| 1044 |
if (interface && !streq_ptr(m->interface, interface)) |
| 1045 |
return 0; |
| 1046 |
|
| 1047 |
if (member && !streq_ptr(m->member, member)) |
| 1048 |
return 0; |
| 1049 |
|
| 1050 |
return 1; |
| 1051 |
} |
| 1052 |
|
| 1053 |
_public_ int sd_bus_message_is_method_error(sd_bus_message *m, const char *name) { |
| 1054 |
assert_return(m, -EINVAL); |
| 1055 |
|
| 1056 |
if (m->header->type != SD_BUS_MESSAGE_METHOD_ERROR) |
| 1057 |
return 0; |
| 1058 |
|
| 1059 |
if (name && !streq_ptr(m->error.name, name)) |
| 1060 |
return 0; |
| 1061 |
|
| 1062 |
return 1; |
| 1063 |
} |
| 1064 |
|
| 1065 |
_public_ int sd_bus_message_set_expect_reply(sd_bus_message *m, int b) { |
| 1066 |
assert_return(m, -EINVAL); |
| 1067 |
assert_return(!m->sealed, -EPERM); |
| 1068 |
assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EPERM); |
| 1069 |
|
| 1070 |
SET_FLAG(m->header->flags, BUS_MESSAGE_NO_REPLY_EXPECTED, !b); |
| 1071 |
|
| 1072 |
return 0; |
| 1073 |
} |
| 1074 |
|
| 1075 |
_public_ int sd_bus_message_set_auto_start(sd_bus_message *m, int b) { |
| 1076 |
assert_return(m, -EINVAL); |
| 1077 |
assert_return(!m->sealed, -EPERM); |
| 1078 |
|
| 1079 |
SET_FLAG(m->header->flags, BUS_MESSAGE_NO_AUTO_START, !b); |
| 1080 |
|
| 1081 |
return 0; |
| 1082 |
} |
| 1083 |
|
| 1084 |
_public_ int sd_bus_message_set_allow_interactive_authorization(sd_bus_message *m, int b) { |
| 1085 |
assert_return(m, -EINVAL); |
| 1086 |
assert_return(!m->sealed, -EPERM); |
| 1087 |
|
| 1088 |
SET_FLAG(m->header->flags, BUS_MESSAGE_ALLOW_INTERACTIVE_AUTHORIZATION, b); |
| 1089 |
|
| 1090 |
return 0; |
| 1091 |
} |
| 1092 |
|
| 1093 |
static BusMessageBodyPart* message_append_part(sd_bus_message *m) { |
| 1094 |
BusMessageBodyPart *part; |
| 1095 |
|
| 1096 |
assert(m); |
| 1097 |
|
| 1098 |
if (m->poisoned) |
| 1099 |
return NULL; |
| 1100 |
|
| 1101 |
if (m->n_body_parts <= 0) { |
| 1102 |
part = &m->body; |
| 1103 |
zero(*part); |
| 1104 |
} else { |
| 1105 |
assert(m->body_end); |
| 1106 |
|
| 1107 |
part = new0(BusMessageBodyPart, 1); |
| 1108 |
if (!part) { |
| 1109 |
m->poisoned = true; |
| 1110 |
return NULL; |
| 1111 |
} |
| 1112 |
|
| 1113 |
m->body_end->next = part; |
| 1114 |
} |
| 1115 |
|
| 1116 |
part->memfd = -EBADF; |
| 1117 |
m->body_end = part; |
| 1118 |
m->n_body_parts++; |
| 1119 |
|
| 1120 |
return part; |
| 1121 |
} |
| 1122 |
|
| 1123 |
static void part_zero(BusMessageBodyPart *part, size_t sz) { |
| 1124 |
assert(part); |
| 1125 |
assert(sz > 0); |
| 1126 |
assert(sz < 8); |
| 1127 |
|
| 1128 |
/* All other fields can be left in their defaults */ |
| 1129 |
assert(!part->data); |
| 1130 |
assert(part->memfd < 0); |
| 1131 |
|
| 1132 |
part->size = sz; |
| 1133 |
part->is_zero = true; |
| 1134 |
part->sealed = true; |
| 1135 |
} |
| 1136 |
|
| 1137 |
static int part_make_space( |
| 1138 |
struct sd_bus_message *m, |
| 1139 |
BusMessageBodyPart *part, |
| 1140 |
size_t sz, |
| 1141 |
void **q) { |
| 1142 |
|
| 1143 |
void *n; |
| 1144 |
|
| 1145 |
assert(m); |
| 1146 |
assert(part); |
| 1147 |
assert(!part->sealed); |
| 1148 |
|
| 1149 |
if (m->poisoned) |
| 1150 |
return -ENOMEM; |
| 1151 |
|
| 1152 |
if (part->allocated == 0 || sz > part->allocated) { |
| 1153 |
size_t new_allocated; |
| 1154 |
|
| 1155 |
new_allocated = sz > 0 ? 2 * sz : 64; |
| 1156 |
n = realloc(part->data, new_allocated); |
| 1157 |
if (!n) { |
| 1158 |
m->poisoned = true; |
| 1159 |
return -ENOMEM; |
| 1160 |
} |
| 1161 |
|
| 1162 |
part->data = n; |
| 1163 |
part->allocated = new_allocated; |
| 1164 |
part->free_this = true; |
| 1165 |
} |
| 1166 |
|
| 1167 |
if (q) |
| 1168 |
*q = part->data ? (uint8_t*) part->data + part->size : NULL; |
| 1169 |
|
| 1170 |
part->size = sz; |
| 1171 |
return 0; |
| 1172 |
} |
| 1173 |
|
| 1174 |
static void message_extend_containers(sd_bus_message *m, size_t expand) { |
| 1175 |
assert(m); |
| 1176 |
|
| 1177 |
if (expand <= 0) |
| 1178 |
return; |
| 1179 |
|
| 1180 |
if (m->n_containers <= 0) |
| 1181 |
return; |
| 1182 |
|
| 1183 |
/* Update counters */ |
| 1184 |
for (BusMessageContainer *c = m->containers; c < m->containers + m->n_containers; c++) |
| 1185 |
if (c->array_size) |
| 1186 |
*c->array_size += expand; |
| 1187 |
} |
| 1188 |
|
| 1189 |
static void* message_extend_body( |
| 1190 |
sd_bus_message *m, |
| 1191 |
size_t align, |
| 1192 |
size_t sz) { |
| 1193 |
|
| 1194 |
size_t start_body, end_body, padding, added; |
| 1195 |
void *p; |
| 1196 |
int r; |
| 1197 |
|
| 1198 |
assert(m); |
| 1199 |
assert(align > 0); |
| 1200 |
assert(!m->sealed); |
| 1201 |
|
| 1202 |
if (m->poisoned) |
| 1203 |
return NULL; |
| 1204 |
|
| 1205 |
start_body = ALIGN_TO(m->body_size, align); |
| 1206 |
end_body = start_body + sz; |
| 1207 |
|
| 1208 |
padding = start_body - m->body_size; |
| 1209 |
added = padding + sz; |
| 1210 |
|
| 1211 |
/* Check for 32-bit overflows */ |
| 1212 |
if (end_body < start_body || end_body > UINT32_MAX) { |
| 1213 |
m->poisoned = true; |
| 1214 |
return NULL; |
| 1215 |
} |
| 1216 |
|
| 1217 |
if (added > 0) { |
| 1218 |
BusMessageBodyPart *part = NULL; |
| 1219 |
bool add_new_part; |
| 1220 |
|
| 1221 |
add_new_part = |
| 1222 |
m->n_body_parts <= 0 || |
| 1223 |
m->body_end->sealed || |
| 1224 |
(padding != ALIGN_TO(m->body_end->size, align) - m->body_end->size); |
| 1225 |
/* If this must be an inlined extension, let's create a new part if |
| 1226 |
* the previous part is large enough to be inlined. */ |
| 1227 |
|
| 1228 |
if (add_new_part) { |
| 1229 |
if (padding > 0) { |
| 1230 |
part = message_append_part(m); |
| 1231 |
if (!part) |
| 1232 |
return NULL; |
| 1233 |
|
| 1234 |
part_zero(part, padding); |
| 1235 |
} |
| 1236 |
|
| 1237 |
part = message_append_part(m); |
| 1238 |
if (!part) |
| 1239 |
return NULL; |
| 1240 |
|
| 1241 |
r = part_make_space(m, part, sz, &p); |
| 1242 |
if (r < 0) |
| 1243 |
return NULL; |
| 1244 |
} else { |
| 1245 |
void *op; |
| 1246 |
size_t os, start_part, end_part; |
| 1247 |
|
| 1248 |
part = m->body_end; |
| 1249 |
op = part->data; |
| 1250 |
os = part->size; |
| 1251 |
|
| 1252 |
start_part = ALIGN_TO(part->size, align); |
| 1253 |
end_part = start_part + sz; |
| 1254 |
|
| 1255 |
r = part_make_space(m, part, end_part, &p); |
| 1256 |
if (r < 0) |
| 1257 |
return NULL; |
| 1258 |
|
| 1259 |
if (padding > 0) { |
| 1260 |
memzero(p, padding); |
| 1261 |
p = (uint8_t*) p + padding; |
| 1262 |
} |
| 1263 |
|
| 1264 |
/* Readjust pointers */ |
| 1265 |
if (m->n_containers > 0) |
| 1266 |
for (BusMessageContainer *c = m->containers; c < m->containers + m->n_containers; c++) |
| 1267 |
c->array_size = adjust_pointer(c->array_size, op, os, part->data); |
| 1268 |
|
| 1269 |
m->error.message = (const char*) adjust_pointer(m->error.message, op, os, part->data); |
| 1270 |
} |
| 1271 |
} else |
| 1272 |
/* Return something that is not NULL and is aligned */ |
| 1273 |
p = (uint8_t*) align; |
| 1274 |
|
| 1275 |
m->body_size = end_body; |
| 1276 |
message_extend_containers(m, added); |
| 1277 |
|
| 1278 |
return p; |
| 1279 |
} |
| 1280 |
|
| 1281 |
static int message_push_fd(sd_bus_message *m, int fd) { |
| 1282 |
int copy; |
| 1283 |
|
| 1284 |
assert(m); |
| 1285 |
|
| 1286 |
if (fd < 0) |
| 1287 |
return -EINVAL; |
| 1288 |
|
| 1289 |
if (!m->allow_fds) |
| 1290 |
return -EOPNOTSUPP; |
| 1291 |
|
| 1292 |
copy = fcntl(fd, F_DUPFD_CLOEXEC, 3); |
| 1293 |
if (copy < 0) |
| 1294 |
return -errno; |
| 1295 |
|
| 1296 |
if (!GREEDY_REALLOC(m->fds, m->n_fds + 1)) { |
| 1297 |
m->poisoned = true; |
| 1298 |
safe_close(copy); |
| 1299 |
return -ENOMEM; |
| 1300 |
} |
| 1301 |
|
| 1302 |
m->fds[m->n_fds] = copy; /* m->n_fds will be incremented by the caller later */ |
| 1303 |
m->free_fds = true; |
| 1304 |
|
| 1305 |
return copy; |
| 1306 |
} |
| 1307 |
|
| 1308 |
int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored) { |
| 1309 |
_cleanup_close_ int fd = -EBADF; |
| 1310 |
BusMessageContainer *c; |
| 1311 |
ssize_t align, sz; |
| 1312 |
uint32_t u32; |
| 1313 |
void *a; |
| 1314 |
|
| 1315 |
assert_return(m, -EINVAL); |
| 1316 |
assert_return(!m->sealed, -EPERM); |
| 1317 |
assert_return(bus_type_is_basic(type), -EINVAL); |
| 1318 |
assert_return(!m->poisoned, -ESTALE); |
| 1319 |
|
| 1320 |
c = message_get_last_container(m); |
| 1321 |
|
| 1322 |
if (c->signature && c->signature[c->index]) { |
| 1323 |
/* Container signature is already set */ |
| 1324 |
|
| 1325 |
if (c->signature[c->index] != type) |
| 1326 |
return -ENXIO; |
| 1327 |
} else { |
| 1328 |
char *e; |
| 1329 |
|
| 1330 |
/* Maybe we can append to the signature? But only if this is the top-level container */ |
| 1331 |
if (c->enclosing != 0) |
| 1332 |
return -ENXIO; |
| 1333 |
|
| 1334 |
e = strextend(&c->signature, CHAR_TO_STR(type)); |
| 1335 |
if (!e) { |
| 1336 |
m->poisoned = true; |
| 1337 |
return -ENOMEM; |
| 1338 |
} |
| 1339 |
} |
| 1340 |
|
| 1341 |
switch (type) { |
| 1342 |
|
| 1343 |
case SD_BUS_TYPE_STRING: |
| 1344 |
/* To make things easy we'll serialize a NULL string |
| 1345 |
* into the empty string */ |
| 1346 |
p = strempty(p); |
| 1347 |
|
| 1348 |
if (!utf8_is_valid(p)) |
| 1349 |
return -EINVAL; |
| 1350 |
|
| 1351 |
align = 4; |
| 1352 |
sz = 4 + strlen(p) + 1; |
| 1353 |
break; |
| 1354 |
|
| 1355 |
case SD_BUS_TYPE_OBJECT_PATH: |
| 1356 |
|
| 1357 |
if (!p) |
| 1358 |
return -EINVAL; |
| 1359 |
|
| 1360 |
if (!object_path_is_valid(p)) |
| 1361 |
return -EINVAL; |
| 1362 |
|
| 1363 |
align = 4; |
| 1364 |
sz = 4 + strlen(p) + 1; |
| 1365 |
break; |
| 1366 |
|
| 1367 |
case SD_BUS_TYPE_SIGNATURE: |
| 1368 |
|
| 1369 |
p = strempty(p); |
| 1370 |
|
| 1371 |
if (!signature_is_valid(p, /* allow_dict_entry= */ true)) |
| 1372 |
return -EINVAL; |
| 1373 |
|
| 1374 |
align = 1; |
| 1375 |
sz = 1 + strlen(p) + 1; |
| 1376 |
break; |
| 1377 |
|
| 1378 |
case SD_BUS_TYPE_BOOLEAN: |
| 1379 |
|
| 1380 |
u32 = p && *(int*) p; |
| 1381 |
p = &u32; |
| 1382 |
|
| 1383 |
align = sz = 4; |
| 1384 |
break; |
| 1385 |
|
| 1386 |
case SD_BUS_TYPE_UNIX_FD: |
| 1387 |
|
| 1388 |
if (!p) |
| 1389 |
return -EINVAL; |
| 1390 |
|
| 1391 |
fd = message_push_fd(m, *(int*) p); |
| 1392 |
if (fd < 0) |
| 1393 |
return fd; |
| 1394 |
|
| 1395 |
u32 = m->n_fds; |
| 1396 |
p = &u32; |
| 1397 |
|
| 1398 |
align = sz = 4; |
| 1399 |
break; |
| 1400 |
|
| 1401 |
default: |
| 1402 |
align = bus_type_get_alignment(type); |
| 1403 |
sz = bus_type_get_size(type); |
| 1404 |
} |
| 1405 |
|
| 1406 |
assert(align > 0); |
| 1407 |
assert(sz > 0); |
| 1408 |
|
| 1409 |
a = message_extend_body(m, align, sz); |
| 1410 |
if (!a) |
| 1411 |
return -ENOMEM; |
| 1412 |
|
| 1413 |
if (IN_SET(type, SD_BUS_TYPE_STRING, SD_BUS_TYPE_OBJECT_PATH)) { |
| 1414 |
*(uint32_t*) a = sz - 5; |
| 1415 |
memcpy((uint8_t*) a + 4, p, sz - 4); |
| 1416 |
|
| 1417 |
if (stored) |
| 1418 |
*stored = (const uint8_t*) a + 4; |
| 1419 |
|
| 1420 |
} else if (type == SD_BUS_TYPE_SIGNATURE) { |
| 1421 |
*(uint8_t*) a = sz - 2; |
| 1422 |
memcpy((uint8_t*) a + 1, p, sz - 1); |
| 1423 |
|
| 1424 |
if (stored) |
| 1425 |
*stored = (const uint8_t*) a + 1; |
| 1426 |
} else { |
| 1427 |
memcpy(a, p, sz); |
| 1428 |
|
| 1429 |
if (stored) |
| 1430 |
*stored = a; |
| 1431 |
} |
| 1432 |
|
| 1433 |
if (type == SD_BUS_TYPE_UNIX_FD) |
| 1434 |
m->n_fds++; |
| 1435 |
|
| 1436 |
if (c->enclosing != SD_BUS_TYPE_ARRAY) |
| 1437 |
c->index++; |
| 1438 |
|
| 1439 |
fd = -EBADF; |
| 1440 |
return 0; |
| 1441 |
} |
| 1442 |
|
| 1443 |
_public_ int sd_bus_message_append_basic(sd_bus_message *m, char type, const void *p) { |
| 1444 |
return message_append_basic(m, type, p, NULL); |
| 1445 |
} |
| 1446 |
|
| 1447 |
_public_ int sd_bus_message_append_string_space( |
| 1448 |
sd_bus_message *m, |
| 1449 |
size_t size, |
| 1450 |
char **ret) { |
| 1451 |
|
| 1452 |
BusMessageContainer *c; |
| 1453 |
void *a; |
| 1454 |
|
| 1455 |
assert_return(m, -EINVAL); |
| 1456 |
assert_return(!m->sealed, -EPERM); |
| 1457 |
assert_return(!m->poisoned, -ESTALE); |
| 1458 |
assert_return(ret, -EINVAL); |
| 1459 |
|
| 1460 |
c = message_get_last_container(m); |
| 1461 |
|
| 1462 |
if (c->signature && c->signature[c->index]) { |
| 1463 |
/* Container signature is already set */ |
| 1464 |
|
| 1465 |
if (c->signature[c->index] != SD_BUS_TYPE_STRING) |
| 1466 |
return -ENXIO; |
| 1467 |
} else { |
| 1468 |
char *e; |
| 1469 |
|
| 1470 |
/* Maybe we can append to the signature? But only if this is the top-level container */ |
| 1471 |
if (c->enclosing != 0) |
| 1472 |
return -ENXIO; |
| 1473 |
|
| 1474 |
e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRING)); |
| 1475 |
if (!e) { |
| 1476 |
m->poisoned = true; |
| 1477 |
return -ENOMEM; |
| 1478 |
} |
| 1479 |
} |
| 1480 |
|
| 1481 |
a = message_extend_body(m, 4, 4 + size + 1); |
| 1482 |
if (!a) |
| 1483 |
return -ENOMEM; |
| 1484 |
|
| 1485 |
*(uint32_t*) a = size; |
| 1486 |
*ret = (char*) a + 4; |
| 1487 |
|
| 1488 |
(*ret)[size] = 0; |
| 1489 |
|
| 1490 |
if (c->enclosing != SD_BUS_TYPE_ARRAY) |
| 1491 |
c->index++; |
| 1492 |
|
| 1493 |
return 0; |
| 1494 |
} |
| 1495 |
|
| 1496 |
_public_ int sd_bus_message_append_string_iovec( |
| 1497 |
sd_bus_message *m, |
| 1498 |
const struct iovec *iov, |
| 1499 |
unsigned n /* should be size_t, but is API now… 😞 */) { |
| 1500 |
|
| 1501 |
int r; |
| 1502 |
|
| 1503 |
assert_return(m, -EINVAL); |
| 1504 |
assert_return(!m->sealed, -EPERM); |
| 1505 |
assert_return(iov || n == 0, -EINVAL); |
| 1506 |
assert_return(!m->poisoned, -ESTALE); |
| 1507 |
|
| 1508 |
size_t size = iovec_total_size(iov, n); |
| 1509 |
if (size == SIZE_MAX) |
| 1510 |
return -ENOBUFS; |
| 1511 |
|
| 1512 |
char *p; |
| 1513 |
r = sd_bus_message_append_string_space(m, size, &p); |
| 1514 |
if (r < 0) |
| 1515 |
return r; |
| 1516 |
|
| 1517 |
for (unsigned i = 0; i < n; i++) { |
| 1518 |
|
| 1519 |
if (iov[i].iov_base) |
| 1520 |
memcpy(p, iov[i].iov_base, iov[i].iov_len); |
| 1521 |
else |
| 1522 |
memset(p, ' ', iov[i].iov_len); |
| 1523 |
|
| 1524 |
p += iov[i].iov_len; |
| 1525 |
} |
| 1526 |
|
| 1527 |
return 0; |
| 1528 |
} |
| 1529 |
|
| 1530 |
static int bus_message_open_array( |
| 1531 |
sd_bus_message *m, |
| 1532 |
BusMessageContainer *c, |
| 1533 |
const char *contents, |
| 1534 |
uint32_t **ret_array_size) { |
| 1535 |
|
| 1536 |
unsigned nindex; |
| 1537 |
int alignment; |
| 1538 |
void *a, *op; |
| 1539 |
size_t os; |
| 1540 |
BusMessageBodyPart *o; |
| 1541 |
|
| 1542 |
assert(m); |
| 1543 |
assert(c); |
| 1544 |
assert(contents); |
| 1545 |
assert(ret_array_size); |
| 1546 |
|
| 1547 |
if (!signature_is_single(contents, true)) |
| 1548 |
return -EINVAL; |
| 1549 |
|
| 1550 |
if (c->signature && c->signature[c->index]) { |
| 1551 |
|
| 1552 |
/* Verify the existing signature */ |
| 1553 |
|
| 1554 |
if (c->signature[c->index] != SD_BUS_TYPE_ARRAY) |
| 1555 |
return -ENXIO; |
| 1556 |
|
| 1557 |
if (!startswith(c->signature + c->index + 1, contents)) |
| 1558 |
return -ENXIO; |
| 1559 |
|
| 1560 |
nindex = c->index + 1 + strlen(contents); |
| 1561 |
} else { |
| 1562 |
char *e; |
| 1563 |
|
| 1564 |
if (c->enclosing != 0) |
| 1565 |
return -ENXIO; |
| 1566 |
|
| 1567 |
/* Extend the existing signature */ |
| 1568 |
|
| 1569 |
e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_ARRAY), contents); |
| 1570 |
if (!e) { |
| 1571 |
m->poisoned = true; |
| 1572 |
return -ENOMEM; |
| 1573 |
} |
| 1574 |
|
| 1575 |
nindex = e - c->signature; |
| 1576 |
} |
| 1577 |
|
| 1578 |
alignment = bus_type_get_alignment(contents[0]); |
| 1579 |
if (alignment < 0) |
| 1580 |
return alignment; |
| 1581 |
|
| 1582 |
a = message_extend_body(m, 4, 4); |
| 1583 |
if (!a) |
| 1584 |
return -ENOMEM; |
| 1585 |
|
| 1586 |
o = m->body_end; |
| 1587 |
op = m->body_end->data; |
| 1588 |
os = m->body_end->size; |
| 1589 |
|
| 1590 |
/* Add alignment between size and first element */ |
| 1591 |
if (!message_extend_body(m, alignment, 0)) |
| 1592 |
return -ENOMEM; |
| 1593 |
|
| 1594 |
/* location of array size might have changed so let's readjust a */ |
| 1595 |
if (o == m->body_end) |
| 1596 |
a = adjust_pointer(a, op, os, m->body_end->data); |
| 1597 |
|
| 1598 |
*(uint32_t*) a = 0; |
| 1599 |
*ret_array_size = a; |
| 1600 |
|
| 1601 |
if (c->enclosing != SD_BUS_TYPE_ARRAY) |
| 1602 |
c->index = nindex; |
| 1603 |
|
| 1604 |
return 0; |
| 1605 |
} |
| 1606 |
|
| 1607 |
static int bus_message_open_variant( |
| 1608 |
sd_bus_message *m, |
| 1609 |
BusMessageContainer *c, |
| 1610 |
const char *contents) { |
| 1611 |
|
| 1612 |
size_t l; |
| 1613 |
void *a; |
| 1614 |
|
| 1615 |
assert(m); |
| 1616 |
assert(c); |
| 1617 |
assert(contents); |
| 1618 |
|
| 1619 |
if (!signature_is_single(contents, false)) |
| 1620 |
return -EINVAL; |
| 1621 |
|
| 1622 |
if (*contents == SD_BUS_TYPE_DICT_ENTRY_BEGIN) |
| 1623 |
return -EINVAL; |
| 1624 |
|
| 1625 |
if (c->signature && c->signature[c->index]) { |
| 1626 |
|
| 1627 |
if (c->signature[c->index] != SD_BUS_TYPE_VARIANT) |
| 1628 |
return -ENXIO; |
| 1629 |
|
| 1630 |
} else { |
| 1631 |
char *e; |
| 1632 |
|
| 1633 |
if (c->enclosing != 0) |
| 1634 |
return -ENXIO; |
| 1635 |
|
| 1636 |
e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_VARIANT)); |
| 1637 |
if (!e) { |
| 1638 |
m->poisoned = true; |
| 1639 |
return -ENOMEM; |
| 1640 |
} |
| 1641 |
} |
| 1642 |
|
| 1643 |
l = strlen(contents); |
| 1644 |
a = message_extend_body(m, 1, 1 + l + 1); |
| 1645 |
if (!a) |
| 1646 |
return -ENOMEM; |
| 1647 |
|
| 1648 |
*(uint8_t*) a = l; |
| 1649 |
memcpy((uint8_t*) a + 1, contents, l + 1); |
| 1650 |
|
| 1651 |
if (c->enclosing != SD_BUS_TYPE_ARRAY) |
| 1652 |
c->index++; |
| 1653 |
|
| 1654 |
return 0; |
| 1655 |
} |
| 1656 |
|
| 1657 |
static int bus_message_open_struct( |
| 1658 |
sd_bus_message *m, |
| 1659 |
BusMessageContainer *c, |
| 1660 |
const char *contents) { |
| 1661 |
|
| 1662 |
size_t nindex; |
| 1663 |
|
| 1664 |
assert(m); |
| 1665 |
assert(c); |
| 1666 |
assert(contents); |
| 1667 |
|
| 1668 |
if (!signature_is_valid(contents, false)) |
| 1669 |
return -EINVAL; |
| 1670 |
|
| 1671 |
if (c->signature && c->signature[c->index]) { |
| 1672 |
size_t l; |
| 1673 |
|
| 1674 |
l = strlen(contents); |
| 1675 |
|
| 1676 |
if (c->signature[c->index] != SD_BUS_TYPE_STRUCT_BEGIN || |
| 1677 |
!startswith(c->signature + c->index + 1, contents) || |
| 1678 |
c->signature[c->index + 1 + l] != SD_BUS_TYPE_STRUCT_END) |
| 1679 |
return -ENXIO; |
| 1680 |
|
| 1681 |
nindex = c->index + 1 + l + 1; |
| 1682 |
} else { |
| 1683 |
char *e; |
| 1684 |
|
| 1685 |
if (c->enclosing != 0) |
| 1686 |
return -ENXIO; |
| 1687 |
|
| 1688 |
e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_BEGIN), contents, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_END)); |
| 1689 |
if (!e) { |
| 1690 |
m->poisoned = true; |
| 1691 |
return -ENOMEM; |
| 1692 |
} |
| 1693 |
|
| 1694 |
nindex = e - c->signature; |
| 1695 |
} |
| 1696 |
|
| 1697 |
/* Align contents to 8 byte boundary */ |
| 1698 |
if (!message_extend_body(m, 8, 0)) |
| 1699 |
return -ENOMEM; |
| 1700 |
|
| 1701 |
if (c->enclosing != SD_BUS_TYPE_ARRAY) |
| 1702 |
c->index = nindex; |
| 1703 |
|
| 1704 |
return 0; |
| 1705 |
} |
| 1706 |
|
| 1707 |
static int bus_message_open_dict_entry( |
| 1708 |
sd_bus_message *m, |
| 1709 |
BusMessageContainer *c, |
| 1710 |
const char *contents) { |
| 1711 |
|
| 1712 |
assert(m); |
| 1713 |
assert(c); |
| 1714 |
assert(contents); |
| 1715 |
|
| 1716 |
if (!signature_is_pair(contents)) |
| 1717 |
return -EINVAL; |
| 1718 |
|
| 1719 |
if (c->enclosing != SD_BUS_TYPE_ARRAY) |
| 1720 |
return -ENXIO; |
| 1721 |
|
| 1722 |
if (c->signature && c->signature[c->index]) { |
| 1723 |
size_t l; |
| 1724 |
|
| 1725 |
l = strlen(contents); |
| 1726 |
|
| 1727 |
if (c->signature[c->index] != SD_BUS_TYPE_DICT_ENTRY_BEGIN || |
| 1728 |
!startswith(c->signature + c->index + 1, contents) || |
| 1729 |
c->signature[c->index + 1 + l] != SD_BUS_TYPE_DICT_ENTRY_END) |
| 1730 |
return -ENXIO; |
| 1731 |
} else |
| 1732 |
return -ENXIO; |
| 1733 |
|
| 1734 |
/* Align contents to 8 byte boundary */ |
| 1735 |
if (!message_extend_body(m, 8, 0)) |
| 1736 |
return -ENOMEM; |
| 1737 |
|
| 1738 |
return 0; |
| 1739 |
} |
| 1740 |
|
| 1741 |
_public_ int sd_bus_message_open_container( |
| 1742 |
sd_bus_message *m, |
| 1743 |
char type, |
| 1744 |
const char *contents) { |
| 1745 |
|
| 1746 |
BusMessageContainer *c; |
| 1747 |
uint32_t *array_size = NULL; |
| 1748 |
_cleanup_free_ char *signature = NULL; |
| 1749 |
size_t before; |
| 1750 |
int r; |
| 1751 |
|
| 1752 |
assert_return(m, -EINVAL); |
| 1753 |
assert_return(!m->sealed, -EPERM); |
| 1754 |
assert_return(contents, -EINVAL); |
| 1755 |
assert_return(!m->poisoned, -ESTALE); |
| 1756 |
|
| 1757 |
/* Make sure we have space for one more container */ |
| 1758 |
if (!GREEDY_REALLOC(m->containers, m->n_containers + 1)) { |
| 1759 |
m->poisoned = true; |
| 1760 |
return -ENOMEM; |
| 1761 |
} |
| 1762 |
|
| 1763 |
c = message_get_last_container(m); |
| 1764 |
|
| 1765 |
signature = strdup(contents); |
| 1766 |
if (!signature) { |
| 1767 |
m->poisoned = true; |
| 1768 |
return -ENOMEM; |
| 1769 |
} |
| 1770 |
|
| 1771 |
/* Save old index in the parent container, in case we have to |
| 1772 |
* abort this container */ |
| 1773 |
c->saved_index = c->index; |
| 1774 |
before = m->body_size; |
| 1775 |
|
| 1776 |
if (type == SD_BUS_TYPE_ARRAY) |
| 1777 |
r = bus_message_open_array(m, c, contents, &array_size); |
| 1778 |
else if (type == SD_BUS_TYPE_VARIANT) |
| 1779 |
r = bus_message_open_variant(m, c, contents); |
| 1780 |
else if (type == SD_BUS_TYPE_STRUCT) |
| 1781 |
r = bus_message_open_struct(m, c, contents); |
| 1782 |
else if (type == SD_BUS_TYPE_DICT_ENTRY) |
| 1783 |
r = bus_message_open_dict_entry(m, c, contents); |
| 1784 |
else |
| 1785 |
r = -EINVAL; |
| 1786 |
if (r < 0) |
| 1787 |
return r; |
| 1788 |
|
| 1789 |
/* OK, let's fill it in */ |
| 1790 |
m->containers[m->n_containers++] = (BusMessageContainer) { |
| 1791 |
.enclosing = type, |
| 1792 |
.signature = TAKE_PTR(signature), |
| 1793 |
.array_size = array_size, |
| 1794 |
.before = before, |
| 1795 |
}; |
| 1796 |
|
| 1797 |
return 0; |
| 1798 |
} |
| 1799 |
|
| 1800 |
_public_ int sd_bus_message_close_container(sd_bus_message *m) { |
| 1801 |
BusMessageContainer *c; |
| 1802 |
|
| 1803 |
assert_return(m, -EINVAL); |
| 1804 |
assert_return(!m->sealed, -EPERM); |
| 1805 |
assert_return(m->n_containers > 0, -EINVAL); |
| 1806 |
assert_return(!m->poisoned, -ESTALE); |
| 1807 |
|
| 1808 |
c = message_get_last_container(m); |
| 1809 |
|
| 1810 |
if (c->enclosing != SD_BUS_TYPE_ARRAY) |
| 1811 |
if (c->signature && c->signature[c->index] != 0) |
| 1812 |
return -EINVAL; |
| 1813 |
|
| 1814 |
m->n_containers--; |
| 1815 |
|
| 1816 |
free(c->signature); |
| 1817 |
|
| 1818 |
return 0; |
| 1819 |
} |
| 1820 |
|
| 1821 |
typedef struct BusTypeStack { |
| 1822 |
const char *types; |
| 1823 |
unsigned n_struct; |
| 1824 |
unsigned n_array; |
| 1825 |
} BusTypeStack; |
| 1826 |
|
| 1827 |
static int type_stack_push( |
| 1828 |
BusTypeStack *stack, |
| 1829 |
unsigned max, |
| 1830 |
unsigned *i, |
| 1831 |
const char *types, |
| 1832 |
unsigned n_struct, |
| 1833 |
unsigned n_array) { |
| 1834 |
|
| 1835 |
assert(stack); |
| 1836 |
assert(max > 0); |
| 1837 |
assert(i); |
| 1838 |
|
| 1839 |
if (*i >= max) |
| 1840 |
return -EINVAL; |
| 1841 |
|
| 1842 |
stack[*i].types = types; |
| 1843 |
stack[*i].n_struct = n_struct; |
| 1844 |
stack[*i].n_array = n_array; |
| 1845 |
(*i)++; |
| 1846 |
|
| 1847 |
return 0; |
| 1848 |
} |
| 1849 |
|
| 1850 |
static int type_stack_pop( |
| 1851 |
BusTypeStack *stack, |
| 1852 |
unsigned max, |
| 1853 |
unsigned *i, |
| 1854 |
const char **ret_types, |
| 1855 |
unsigned *ret_n_struct, |
| 1856 |
unsigned *ret_n_array) { |
| 1857 |
|
| 1858 |
assert(stack); |
| 1859 |
assert(max > 0); |
| 1860 |
assert(i); |
| 1861 |
assert(ret_types); |
| 1862 |
assert(ret_n_struct); |
| 1863 |
assert(ret_n_array); |
| 1864 |
|
| 1865 |
if (*i <= 0) |
| 1866 |
return 0; |
| 1867 |
|
| 1868 |
(*i)--; |
| 1869 |
*ret_types = stack[*i].types; |
| 1870 |
*ret_n_struct = stack[*i].n_struct; |
| 1871 |
*ret_n_array = stack[*i].n_array; |
| 1872 |
|
| 1873 |
return 1; |
| 1874 |
} |
| 1875 |
|
| 1876 |
_public_ int sd_bus_message_appendv( |
| 1877 |
sd_bus_message *m, |
| 1878 |
const char *types, |
| 1879 |
va_list ap) { |
| 1880 |
|
| 1881 |
unsigned n_array, n_struct; |
| 1882 |
BusTypeStack stack[BUS_CONTAINER_DEPTH]; |
| 1883 |
unsigned stack_ptr = 0; |
| 1884 |
int r; |
| 1885 |
|
| 1886 |
assert_return(m, -EINVAL); |
| 1887 |
assert_return(types, -EINVAL); |
| 1888 |
assert_return(!m->sealed, -EPERM); |
| 1889 |
assert_return(!m->poisoned, -ESTALE); |
| 1890 |
|
| 1891 |
n_array = UINT_MAX; |
| 1892 |
n_struct = strlen(types); |
| 1893 |
|
| 1894 |
for (;;) { |
| 1895 |
const char *t; |
| 1896 |
|
| 1897 |
if (n_array == 0 || (n_array == UINT_MAX && n_struct == 0)) { |
| 1898 |
r = type_stack_pop(stack, ELEMENTSOF(stack), &stack_ptr, &types, &n_struct, &n_array); |
| 1899 |
if (r < 0) |
| 1900 |
return r; |
| 1901 |
if (r == 0) |
| 1902 |
break; |
| 1903 |
|
| 1904 |
r = sd_bus_message_close_container(m); |
| 1905 |
if (r < 0) |
| 1906 |
return r; |
| 1907 |
|
| 1908 |
continue; |
| 1909 |
} |
| 1910 |
|
| 1911 |
t = types; |
| 1912 |
if (n_array != UINT_MAX) |
| 1913 |
n_array--; |
| 1914 |
else { |
| 1915 |
types++; |
| 1916 |
n_struct--; |
| 1917 |
} |
| 1918 |
|
| 1919 |
switch (*t) { |
| 1920 |
|
| 1921 |
case SD_BUS_TYPE_BYTE: { |
| 1922 |
uint8_t x; |
| 1923 |
|
| 1924 |
x = (uint8_t) va_arg(ap, int); |
| 1925 |
r = sd_bus_message_append_basic(m, *t, &x); |
| 1926 |
break; |
| 1927 |
} |
| 1928 |
|
| 1929 |
case SD_BUS_TYPE_BOOLEAN: |
| 1930 |
case SD_BUS_TYPE_INT32: |
| 1931 |
case SD_BUS_TYPE_UINT32: |
| 1932 |
case SD_BUS_TYPE_UNIX_FD: { |
| 1933 |
uint32_t x; |
| 1934 |
|
| 1935 |
/* We assume a boolean is the same as int32_t */ |
| 1936 |
assert_cc(sizeof(int32_t) == sizeof(int)); |
| 1937 |
|
| 1938 |
x = va_arg(ap, uint32_t); |
| 1939 |
r = sd_bus_message_append_basic(m, *t, &x); |
| 1940 |
break; |
| 1941 |
} |
| 1942 |
|
| 1943 |
case SD_BUS_TYPE_INT16: |
| 1944 |
case SD_BUS_TYPE_UINT16: { |
| 1945 |
uint16_t x; |
| 1946 |
|
| 1947 |
x = (uint16_t) va_arg(ap, int); |
| 1948 |
r = sd_bus_message_append_basic(m, *t, &x); |
| 1949 |
break; |
| 1950 |
} |
| 1951 |
|
| 1952 |
case SD_BUS_TYPE_INT64: |
| 1953 |
case SD_BUS_TYPE_UINT64: { |
| 1954 |
uint64_t x; |
| 1955 |
|
| 1956 |
x = va_arg(ap, uint64_t); |
| 1957 |
r = sd_bus_message_append_basic(m, *t, &x); |
| 1958 |
break; |
| 1959 |
} |
| 1960 |
|
| 1961 |
case SD_BUS_TYPE_DOUBLE: { |
| 1962 |
double x; |
| 1963 |
|
| 1964 |
x = va_arg(ap, double); |
| 1965 |
r = sd_bus_message_append_basic(m, *t, &x); |
| 1966 |
break; |
| 1967 |
} |
| 1968 |
|
| 1969 |
case SD_BUS_TYPE_STRING: |
| 1970 |
case SD_BUS_TYPE_OBJECT_PATH: |
| 1971 |
case SD_BUS_TYPE_SIGNATURE: { |
| 1972 |
const char *x; |
| 1973 |
|
| 1974 |
x = va_arg(ap, const char*); |
| 1975 |
r = sd_bus_message_append_basic(m, *t, x); |
| 1976 |
break; |
| 1977 |
} |
| 1978 |
|
| 1979 |
case SD_BUS_TYPE_ARRAY: { |
| 1980 |
size_t k; |
| 1981 |
|
| 1982 |
r = signature_element_length(t + 1, &k); |
| 1983 |
if (r < 0) |
| 1984 |
return r; |
| 1985 |
|
| 1986 |
{ |
| 1987 |
char s[k + 1]; |
| 1988 |
memcpy(s, t + 1, k); |
| 1989 |
s[k] = 0; |
| 1990 |
|
| 1991 |
r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, s); |
| 1992 |
if (r < 0) |
| 1993 |
return r; |
| 1994 |
} |
| 1995 |
|
| 1996 |
if (n_array == UINT_MAX) { |
| 1997 |
types += k; |
| 1998 |
n_struct -= k; |
| 1999 |
} |
| 2000 |
|
| 2001 |
r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array); |
| 2002 |
if (r < 0) |
| 2003 |
return r; |
| 2004 |
|
| 2005 |
types = t + 1; |
| 2006 |
n_struct = k; |
| 2007 |
n_array = va_arg(ap, unsigned); |
| 2008 |
|
| 2009 |
break; |
| 2010 |
} |
| 2011 |
|
| 2012 |
case SD_BUS_TYPE_VARIANT: { |
| 2013 |
const char *s; |
| 2014 |
|
| 2015 |
s = va_arg(ap, const char*); |
| 2016 |
if (!s) |
| 2017 |
return -EINVAL; |
| 2018 |
|
| 2019 |
r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT, s); |
| 2020 |
if (r < 0) |
| 2021 |
return r; |
| 2022 |
|
| 2023 |
r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array); |
| 2024 |
if (r < 0) |
| 2025 |
return r; |
| 2026 |
|
| 2027 |
types = s; |
| 2028 |
n_struct = strlen(s); |
| 2029 |
n_array = UINT_MAX; |
| 2030 |
|
| 2031 |
break; |
| 2032 |
} |
| 2033 |
|
| 2034 |
case SD_BUS_TYPE_STRUCT_BEGIN: |
| 2035 |
case SD_BUS_TYPE_DICT_ENTRY_BEGIN: { |
| 2036 |
size_t k; |
| 2037 |
|
| 2038 |
r = signature_element_length(t, &k); |
| 2039 |
if (r < 0) |
| 2040 |
return r; |
| 2041 |
if (k < 2) |
| 2042 |
return -ERANGE; |
| 2043 |
|
| 2044 |
{ |
| 2045 |
char s[k - 1]; |
| 2046 |
|
| 2047 |
memcpy(s, t + 1, k - 2); |
| 2048 |
s[k - 2] = 0; |
| 2049 |
|
| 2050 |
r = sd_bus_message_open_container(m, *t == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s); |
| 2051 |
if (r < 0) |
| 2052 |
return r; |
| 2053 |
} |
| 2054 |
|
| 2055 |
if (n_array == UINT_MAX) { |
| 2056 |
types += k - 1; |
| 2057 |
n_struct -= k - 1; |
| 2058 |
} |
| 2059 |
|
| 2060 |
r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array); |
| 2061 |
if (r < 0) |
| 2062 |
return r; |
| 2063 |
|
| 2064 |
types = t + 1; |
| 2065 |
n_struct = k - 2; |
| 2066 |
n_array = UINT_MAX; |
| 2067 |
|
| 2068 |
break; |
| 2069 |
} |
| 2070 |
|
| 2071 |
default: |
| 2072 |
r = -EINVAL; |
| 2073 |
} |
| 2074 |
|
| 2075 |
if (r < 0) |
| 2076 |
return r; |
| 2077 |
} |
| 2078 |
|
| 2079 |
return 1; |
| 2080 |
} |
| 2081 |
|
| 2082 |
_public_ int sd_bus_message_append(sd_bus_message *m, const char *types, ...) { |
| 2083 |
va_list ap; |
| 2084 |
int r; |
| 2085 |
|
| 2086 |
va_start(ap, types); |
| 2087 |
r = sd_bus_message_appendv(m, types, ap); |
| 2088 |
va_end(ap); |
| 2089 |
|
| 2090 |
return r; |
| 2091 |
} |
| 2092 |
|
| 2093 |
_public_ int sd_bus_message_append_array_space( |
| 2094 |
sd_bus_message *m, |
| 2095 |
char type, |
| 2096 |
size_t size, |
| 2097 |
void **ret) { |
| 2098 |
|
| 2099 |
ssize_t align, sz; |
| 2100 |
void *a; |
| 2101 |
int r; |
| 2102 |
|
| 2103 |
assert_return(m, -EINVAL); |
| 2104 |
assert_return(!m->sealed, -EPERM); |
| 2105 |
assert_return(bus_type_is_trivial(type) && type != SD_BUS_TYPE_BOOLEAN, -EINVAL); |
| 2106 |
assert_return(ret || size == 0, -EINVAL); |
| 2107 |
assert_return(!m->poisoned, -ESTALE); |
| 2108 |
|
| 2109 |
align = bus_type_get_alignment(type); |
| 2110 |
sz = bus_type_get_size(type); |
| 2111 |
|
| 2112 |
assert_se(align > 0); |
| 2113 |
assert_se(sz > 0); |
| 2114 |
|
| 2115 |
if (size % sz != 0) |
| 2116 |
return -EINVAL; |
| 2117 |
|
| 2118 |
r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, CHAR_TO_STR(type)); |
| 2119 |
if (r < 0) |
| 2120 |
return r; |
| 2121 |
|
| 2122 |
a = message_extend_body(m, align, size); |
| 2123 |
if (!a) |
| 2124 |
return -ENOMEM; |
| 2125 |
|
| 2126 |
r = sd_bus_message_close_container(m); |
| 2127 |
if (r < 0) |
| 2128 |
return r; |
| 2129 |
|
| 2130 |
*ret = a; |
| 2131 |
return 0; |
| 2132 |
} |
| 2133 |
|
| 2134 |
_public_ int sd_bus_message_append_array( |
| 2135 |
sd_bus_message *m, |
| 2136 |
char type, |
| 2137 |
const void *ptr, |
| 2138 |
size_t size) { |
| 2139 |
int r; |
| 2140 |
void *p; |
| 2141 |
|
| 2142 |
assert_return(m, -EINVAL); |
| 2143 |
assert_return(!m->sealed, -EPERM); |
| 2144 |
assert_return(bus_type_is_trivial(type), -EINVAL); |
| 2145 |
assert_return(ptr || size == 0, -EINVAL); |
| 2146 |
assert_return(!m->poisoned, -ESTALE); |
| 2147 |
|
| 2148 |
r = sd_bus_message_append_array_space(m, type, size, &p); |
| 2149 |
if (r < 0) |
| 2150 |
return r; |
| 2151 |
|
| 2152 |
memcpy_safe(p, ptr, size); |
| 2153 |
|
| 2154 |
return 0; |
| 2155 |
} |
| 2156 |
|
| 2157 |
_public_ int sd_bus_message_append_array_iovec( |
| 2158 |
sd_bus_message *m, |
| 2159 |
char type, |
| 2160 |
const struct iovec *iov, |
| 2161 |
unsigned n /* should be size_t, but is API now… 😞 */) { |
| 2162 |
|
| 2163 |
int r; |
| 2164 |
|
| 2165 |
assert_return(m, -EINVAL); |
| 2166 |
assert_return(!m->sealed, -EPERM); |
| 2167 |
assert_return(bus_type_is_trivial(type), -EINVAL); |
| 2168 |
assert_return(iov || n == 0, -EINVAL); |
| 2169 |
assert_return(!m->poisoned, -ESTALE); |
| 2170 |
|
| 2171 |
size_t size = iovec_total_size(iov, n); |
| 2172 |
if (size == SIZE_MAX) |
| 2173 |
return -ENOBUFS; |
| 2174 |
|
| 2175 |
void *p; |
| 2176 |
r = sd_bus_message_append_array_space(m, type, size, &p); |
| 2177 |
if (r < 0) |
| 2178 |
return r; |
| 2179 |
|
| 2180 |
for (unsigned i = 0; i < n; i++) { |
| 2181 |
|
| 2182 |
if (iov[i].iov_base) |
| 2183 |
memcpy(p, iov[i].iov_base, iov[i].iov_len); |
| 2184 |
else |
| 2185 |
memzero(p, iov[i].iov_len); |
| 2186 |
|
| 2187 |
p = (uint8_t*) p + iov[i].iov_len; |
| 2188 |
} |
| 2189 |
|
| 2190 |
return 0; |
| 2191 |
} |
| 2192 |
|
| 2193 |
_public_ int sd_bus_message_append_array_memfd( |
| 2194 |
sd_bus_message *m, |
| 2195 |
char type, |
| 2196 |
int memfd, |
| 2197 |
uint64_t offset, |
| 2198 |
uint64_t size) { |
| 2199 |
|
| 2200 |
_cleanup_close_ int copy_fd = -EBADF; |
| 2201 |
BusMessageBodyPart *part; |
| 2202 |
ssize_t align, sz; |
| 2203 |
uint64_t real_size; |
| 2204 |
void *a; |
| 2205 |
int r; |
| 2206 |
|
| 2207 |
assert_return(m, -EINVAL); |
| 2208 |
assert_return(memfd >= 0, -EBADF); |
| 2209 |
assert_return(bus_type_is_trivial(type), -EINVAL); |
| 2210 |
assert_return(size > 0, -EINVAL); |
| 2211 |
assert_return(!m->sealed, -EPERM); |
| 2212 |
assert_return(!m->poisoned, -ESTALE); |
| 2213 |
|
| 2214 |
r = memfd_set_sealed(memfd); |
| 2215 |
if (r < 0) |
| 2216 |
return r; |
| 2217 |
|
| 2218 |
copy_fd = fcntl(memfd, F_DUPFD_CLOEXEC, 3); |
| 2219 |
if (copy_fd < 0) |
| 2220 |
return copy_fd; |
| 2221 |
|
| 2222 |
r = memfd_get_size(memfd, &real_size); |
| 2223 |
if (r < 0) |
| 2224 |
return r; |
| 2225 |
|
| 2226 |
if (offset == 0 && size == UINT64_MAX) |
| 2227 |
size = real_size; |
| 2228 |
else if (offset + size > real_size) |
| 2229 |
return -EMSGSIZE; |
| 2230 |
|
| 2231 |
align = bus_type_get_alignment(type); |
| 2232 |
sz = bus_type_get_size(type); |
| 2233 |
|
| 2234 |
assert_se(align > 0); |
| 2235 |
assert_se(sz > 0); |
| 2236 |
|
| 2237 |
if (offset % align != 0) |
| 2238 |
return -EINVAL; |
| 2239 |
|
| 2240 |
if (size % sz != 0) |
| 2241 |
return -EINVAL; |
| 2242 |
|
| 2243 |
if (size > (uint64_t) UINT32_MAX) |
| 2244 |
return -EINVAL; |
| 2245 |
|
| 2246 |
r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, CHAR_TO_STR(type)); |
| 2247 |
if (r < 0) |
| 2248 |
return r; |
| 2249 |
|
| 2250 |
a = message_extend_body(m, align, 0); |
| 2251 |
if (!a) |
| 2252 |
return -ENOMEM; |
| 2253 |
|
| 2254 |
part = message_append_part(m); |
| 2255 |
if (!part) |
| 2256 |
return -ENOMEM; |
| 2257 |
|
| 2258 |
part->memfd = copy_fd; |
| 2259 |
part->memfd_offset = offset; |
| 2260 |
part->sealed = true; |
| 2261 |
part->size = size; |
| 2262 |
copy_fd = -EBADF; |
| 2263 |
|
| 2264 |
m->body_size += size; |
| 2265 |
message_extend_containers(m, size); |
| 2266 |
|
| 2267 |
return sd_bus_message_close_container(m); |
| 2268 |
} |
| 2269 |
|
| 2270 |
_public_ int sd_bus_message_append_string_memfd( |
| 2271 |
sd_bus_message *m, |
| 2272 |
int memfd, |
| 2273 |
uint64_t offset, |
| 2274 |
uint64_t size) { |
| 2275 |
|
| 2276 |
_cleanup_close_ int copy_fd = -EBADF; |
| 2277 |
BusMessageBodyPart *part; |
| 2278 |
BusMessageContainer *c; |
| 2279 |
uint64_t real_size; |
| 2280 |
void *a; |
| 2281 |
int r; |
| 2282 |
|
| 2283 |
assert_return(m, -EINVAL); |
| 2284 |
assert_return(memfd >= 0, -EBADF); |
| 2285 |
assert_return(size > 0, -EINVAL); |
| 2286 |
assert_return(!m->sealed, -EPERM); |
| 2287 |
assert_return(!m->poisoned, -ESTALE); |
| 2288 |
|
| 2289 |
r = memfd_set_sealed(memfd); |
| 2290 |
if (r < 0) |
| 2291 |
return r; |
| 2292 |
|
| 2293 |
copy_fd = fcntl(memfd, FD_CLOEXEC, 3); |
| 2294 |
if (copy_fd < 0) |
| 2295 |
return copy_fd; |
| 2296 |
|
| 2297 |
r = memfd_get_size(memfd, &real_size); |
| 2298 |
if (r < 0) |
| 2299 |
return r; |
| 2300 |
|
| 2301 |
if (offset == 0 && size == UINT64_MAX) |
| 2302 |
size = real_size; |
| 2303 |
else if (offset + size > real_size) |
| 2304 |
return -EMSGSIZE; |
| 2305 |
|
| 2306 |
/* We require this to be NUL terminated */ |
| 2307 |
if (size == 0) |
| 2308 |
return -EINVAL; |
| 2309 |
|
| 2310 |
if (size > (uint64_t) UINT32_MAX) |
| 2311 |
return -EINVAL; |
| 2312 |
|
| 2313 |
c = message_get_last_container(m); |
| 2314 |
if (c->signature && c->signature[c->index]) { |
| 2315 |
/* Container signature is already set */ |
| 2316 |
|
| 2317 |
if (c->signature[c->index] != SD_BUS_TYPE_STRING) |
| 2318 |
return -ENXIO; |
| 2319 |
} else { |
| 2320 |
char *e; |
| 2321 |
|
| 2322 |
/* Maybe we can append to the signature? But only if this is the top-level container */ |
| 2323 |
if (c->enclosing != 0) |
| 2324 |
return -ENXIO; |
| 2325 |
|
| 2326 |
e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRING)); |
| 2327 |
if (!e) { |
| 2328 |
m->poisoned = true; |
| 2329 |
return -ENOMEM; |
| 2330 |
} |
| 2331 |
} |
| 2332 |
|
| 2333 |
a = message_extend_body(m, 4, 4); |
| 2334 |
if (!a) |
| 2335 |
return -ENOMEM; |
| 2336 |
|
| 2337 |
*(uint32_t*) a = size - 1; |
| 2338 |
|
| 2339 |
part = message_append_part(m); |
| 2340 |
if (!part) |
| 2341 |
return -ENOMEM; |
| 2342 |
|
| 2343 |
part->memfd = copy_fd; |
| 2344 |
part->memfd_offset = offset; |
| 2345 |
part->sealed = true; |
| 2346 |
part->size = size; |
| 2347 |
copy_fd = -EBADF; |
| 2348 |
|
| 2349 |
m->body_size += size; |
| 2350 |
message_extend_containers(m, size); |
| 2351 |
|
| 2352 |
if (c->enclosing != SD_BUS_TYPE_ARRAY) |
| 2353 |
c->index++; |
| 2354 |
|
| 2355 |
return 0; |
| 2356 |
} |
| 2357 |
|
| 2358 |
_public_ int sd_bus_message_append_strv(sd_bus_message *m, char **l) { |
| 2359 |
int r; |
| 2360 |
|
| 2361 |
assert_return(m, -EINVAL); |
| 2362 |
assert_return(!m->sealed, -EPERM); |
| 2363 |
assert_return(!m->poisoned, -ESTALE); |
| 2364 |
|
| 2365 |
r = sd_bus_message_open_container(m, 'a', "s"); |
| 2366 |
if (r < 0) |
| 2367 |
return r; |
| 2368 |
|
| 2369 |
STRV_FOREACH(i, l) { |
| 2370 |
r = sd_bus_message_append_basic(m, 's', *i); |
| 2371 |
if (r < 0) |
| 2372 |
return r; |
| 2373 |
} |
| 2374 |
|
| 2375 |
return sd_bus_message_close_container(m); |
| 2376 |
} |
| 2377 |
|
| 2378 |
static int bus_message_close_header(sd_bus_message *m) { |
| 2379 |
assert(m); |
| 2380 |
|
| 2381 |
/* The actual user data is finished now, we just complete the variant and struct now. Remember |
| 2382 |
* this position, so that during parsing we know where to put the outer container end. */ |
| 2383 |
m->user_body_size = m->body_size; |
| 2384 |
|
| 2385 |
m->header->fields_size = m->fields_size; |
| 2386 |
m->header->body_size = m->body_size; |
| 2387 |
|
| 2388 |
return 0; |
| 2389 |
} |
| 2390 |
|
| 2391 |
_public_ int sd_bus_message_seal(sd_bus_message *m, uint64_t cookie, uint64_t timeout_usec) { |
| 2392 |
BusMessageBodyPart *part; |
| 2393 |
size_t a; |
| 2394 |
unsigned i; |
| 2395 |
int r; |
| 2396 |
|
| 2397 |
assert_return(m, -EINVAL); |
| 2398 |
|
| 2399 |
if (m->sealed) |
| 2400 |
return -EPERM; |
| 2401 |
|
| 2402 |
if (m->n_containers > 0) |
| 2403 |
return -EBADMSG; |
| 2404 |
|
| 2405 |
if (m->poisoned) |
| 2406 |
return -ESTALE; |
| 2407 |
|
| 2408 |
if (cookie > UINT32_MAX) |
| 2409 |
return -EOPNOTSUPP; |
| 2410 |
|
| 2411 |
/* In vtables the return signature of method calls is listed, |
| 2412 |
* let's check if they match if this is a response */ |
| 2413 |
if (m->header->type == SD_BUS_MESSAGE_METHOD_RETURN && |
| 2414 |
m->enforced_reply_signature && |
| 2415 |
!streq(strempty(m->root_container.signature), m->enforced_reply_signature)) |
| 2416 |
return -ENOMSG; |
| 2417 |
|
| 2418 |
/* If there's a non-trivial signature set, then add it in here */ |
| 2419 |
if (!isempty(m->root_container.signature)) { |
| 2420 |
r = message_append_field_signature(m, BUS_MESSAGE_HEADER_SIGNATURE, m->root_container.signature, NULL); |
| 2421 |
if (r < 0) |
| 2422 |
return r; |
| 2423 |
} |
| 2424 |
|
| 2425 |
if (m->n_fds > 0) { |
| 2426 |
r = message_append_field_uint32(m, BUS_MESSAGE_HEADER_UNIX_FDS, m->n_fds); |
| 2427 |
if (r < 0) |
| 2428 |
return r; |
| 2429 |
} |
| 2430 |
|
| 2431 |
r = bus_message_close_header(m); |
| 2432 |
if (r < 0) |
| 2433 |
return r; |
| 2434 |
|
| 2435 |
m->header->serial = (uint32_t) cookie; |
| 2436 |
|
| 2437 |
m->timeout = m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED ? 0 : timeout_usec; |
| 2438 |
|
| 2439 |
/* Add padding at the end of the fields part, since we know |
| 2440 |
* the body needs to start at an 8 byte alignment. We made |
| 2441 |
* sure we allocated enough space for this, so all we need to |
| 2442 |
* do here is to zero it out. */ |
| 2443 |
a = ALIGN8(m->fields_size) - m->fields_size; |
| 2444 |
if (a > 0) |
| 2445 |
memzero((uint8_t*) BUS_MESSAGE_FIELDS(m) + m->fields_size, a); |
| 2446 |
|
| 2447 |
/* If this is something we can send as memfd, then let's seal |
| 2448 |
the memfd now. Note that we can send memfds as payload only |
| 2449 |
for directed messages, and not for broadcasts. */ |
| 2450 |
if (m->destination && m->bus->use_memfd) { |
| 2451 |
MESSAGE_FOREACH_PART(part, i, m) |
| 2452 |
if (part->memfd >= 0 && |
| 2453 |
!part->sealed && |
| 2454 |
(part->size > MEMFD_MIN_SIZE || m->bus->use_memfd < 0) && |
| 2455 |
part != m->body_end) { /* The last part may never be sent as memfd */ |
| 2456 |
uint64_t sz; |
| 2457 |
|
| 2458 |
/* Try to seal it if that makes |
| 2459 |
* sense. First, unmap our own map to |
| 2460 |
* make sure we don't keep it busy. */ |
| 2461 |
bus_body_part_unmap(part); |
| 2462 |
|
| 2463 |
/* Then, sync up real memfd size */ |
| 2464 |
sz = part->size; |
| 2465 |
r = memfd_set_size(part->memfd, sz); |
| 2466 |
if (r < 0) |
| 2467 |
return r; |
| 2468 |
|
| 2469 |
/* Finally, try to seal */ |
| 2470 |
if (memfd_set_sealed(part->memfd) >= 0) |
| 2471 |
part->sealed = true; |
| 2472 |
} |
| 2473 |
} |
| 2474 |
|
| 2475 |
m->root_container.end = m->user_body_size; |
| 2476 |
m->root_container.index = 0; |
| 2477 |
|
| 2478 |
m->sealed = true; |
| 2479 |
|
| 2480 |
return 0; |
| 2481 |
} |
| 2482 |
|
| 2483 |
int bus_body_part_map(BusMessageBodyPart *part) { |
| 2484 |
void *p; |
| 2485 |
size_t psz, shift; |
| 2486 |
|
| 2487 |
assert_se(part); |
| 2488 |
|
| 2489 |
if (part->data) |
| 2490 |
return 0; |
| 2491 |
|
| 2492 |
if (part->size <= 0) |
| 2493 |
return 0; |
| 2494 |
|
| 2495 |
/* For smaller zero parts (as used for padding) we don't need to map anything... */ |
| 2496 |
if (part->memfd < 0 && part->is_zero && part->size < 8) { |
| 2497 |
static const uint8_t zeroes[7] = { }; |
| 2498 |
part->data = (void*) zeroes; |
| 2499 |
return 0; |
| 2500 |
} |
| 2501 |
|
| 2502 |
shift = PAGE_OFFSET(part->memfd_offset); |
| 2503 |
psz = PAGE_ALIGN(part->size + shift); |
| 2504 |
if (psz >= SIZE_MAX) |
| 2505 |
return -EFBIG; |
| 2506 |
|
| 2507 |
if (part->memfd >= 0) |
| 2508 |
p = mmap(NULL, psz, PROT_READ, MAP_PRIVATE, part->memfd, part->memfd_offset - shift); |
| 2509 |
else if (part->is_zero) |
| 2510 |
p = mmap(NULL, psz, PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 2511 |
else |
| 2512 |
return -EINVAL; |
| 2513 |
|
| 2514 |
if (p == MAP_FAILED) |
| 2515 |
return -errno; |
| 2516 |
|
| 2517 |
part->mapped = psz; |
| 2518 |
part->mmap_begin = p; |
| 2519 |
part->data = (uint8_t*) p + shift; |
| 2520 |
part->munmap_this = true; |
| 2521 |
|
| 2522 |
return 0; |
| 2523 |
} |
| 2524 |
|
| 2525 |
void bus_body_part_unmap(BusMessageBodyPart *part) { |
| 2526 |
|
| 2527 |
assert_se(part); |
| 2528 |
|
| 2529 |
if (part->memfd < 0) |
| 2530 |
return; |
| 2531 |
|
| 2532 |
if (!part->mmap_begin) |
| 2533 |
return; |
| 2534 |
|
| 2535 |
if (!part->munmap_this) |
| 2536 |
return; |
| 2537 |
|
| 2538 |
assert_se(munmap(part->mmap_begin, part->mapped) == 0); |
| 2539 |
|
| 2540 |
part->mmap_begin = NULL; |
| 2541 |
part->data = NULL; |
| 2542 |
part->mapped = 0; |
| 2543 |
part->munmap_this = false; |
| 2544 |
|
| 2545 |
return; |
| 2546 |
} |
| 2547 |
|
| 2548 |
static bool message_end_of_signature(sd_bus_message *m) { |
| 2549 |
BusMessageContainer *c; |
| 2550 |
|
| 2551 |
assert(m); |
| 2552 |
|
| 2553 |
c = message_get_last_container(m); |
| 2554 |
return !c->signature || c->signature[c->index] == 0; |
| 2555 |
} |
| 2556 |
|
| 2557 |
static bool message_end_of_array(sd_bus_message *m, size_t index) { |
| 2558 |
BusMessageContainer *c; |
| 2559 |
|
| 2560 |
assert(m); |
| 2561 |
|
| 2562 |
c = message_get_last_container(m); |
| 2563 |
if (c->enclosing != SD_BUS_TYPE_ARRAY) |
| 2564 |
return false; |
| 2565 |
|
| 2566 |
assert(c->array_size); |
| 2567 |
return index >= c->begin + BUS_MESSAGE_BSWAP32(m, *c->array_size); |
| 2568 |
} |
| 2569 |
|
| 2570 |
_public_ int sd_bus_message_at_end(sd_bus_message *m, int complete) { |
| 2571 |
assert_return(m, -EINVAL); |
| 2572 |
assert_return(m->sealed, -EPERM); |
| 2573 |
|
| 2574 |
if (complete && m->n_containers > 0) |
| 2575 |
return false; |
| 2576 |
|
| 2577 |
if (message_end_of_signature(m)) |
| 2578 |
return true; |
| 2579 |
|
| 2580 |
if (message_end_of_array(m, m->rindex)) |
| 2581 |
return true; |
| 2582 |
|
| 2583 |
return false; |
| 2584 |
} |
| 2585 |
|
| 2586 |
static BusMessageBodyPart* find_part(sd_bus_message *m, size_t index, size_t sz, void **ret) { |
| 2587 |
BusMessageBodyPart *part; |
| 2588 |
size_t begin; |
| 2589 |
int r; |
| 2590 |
|
| 2591 |
assert(m); |
| 2592 |
|
| 2593 |
if (m->cached_rindex_part && index >= m->cached_rindex_part_begin) { |
| 2594 |
part = m->cached_rindex_part; |
| 2595 |
begin = m->cached_rindex_part_begin; |
| 2596 |
} else { |
| 2597 |
part = &m->body; |
| 2598 |
begin = 0; |
| 2599 |
} |
| 2600 |
|
| 2601 |
while (part) { |
| 2602 |
if (index < begin) |
| 2603 |
return NULL; |
| 2604 |
|
| 2605 |
if (index + sz <= begin + part->size) { |
| 2606 |
|
| 2607 |
r = bus_body_part_map(part); |
| 2608 |
if (r < 0) |
| 2609 |
return NULL; |
| 2610 |
|
| 2611 |
if (ret) |
| 2612 |
*ret = part->data ? (uint8_t*) part->data + index - begin |
| 2613 |
: NULL; /* Avoid dereferencing a NULL pointer. */ |
| 2614 |
|
| 2615 |
m->cached_rindex_part = part; |
| 2616 |
m->cached_rindex_part_begin = begin; |
| 2617 |
|
| 2618 |
return part; |
| 2619 |
} |
| 2620 |
|
| 2621 |
begin += part->size; |
| 2622 |
part = part->next; |
| 2623 |
} |
| 2624 |
|
| 2625 |
return NULL; |
| 2626 |
} |
| 2627 |
|
| 2628 |
static int message_peek_body( |
| 2629 |
sd_bus_message *m, |
| 2630 |
size_t *rindex, |
| 2631 |
size_t align, |
| 2632 |
size_t nbytes, |
| 2633 |
void **ret) { |
| 2634 |
|
| 2635 |
size_t k, start, end, padding; |
| 2636 |
BusMessageBodyPart *part; |
| 2637 |
uint8_t *q; |
| 2638 |
|
| 2639 |
assert(m); |
| 2640 |
assert(rindex); |
| 2641 |
assert(align > 0); |
| 2642 |
|
| 2643 |
start = ALIGN_TO(*rindex, align); |
| 2644 |
if (start > m->user_body_size) |
| 2645 |
return -EBADMSG; |
| 2646 |
|
| 2647 |
padding = start - *rindex; |
| 2648 |
|
| 2649 |
/* Avoid overflow below */ |
| 2650 |
if (nbytes > SIZE_MAX - start) |
| 2651 |
return -EBADMSG; |
| 2652 |
|
| 2653 |
end = start + nbytes; |
| 2654 |
if (end > m->user_body_size) |
| 2655 |
return -EBADMSG; |
| 2656 |
|
| 2657 |
part = find_part(m, *rindex, padding, (void**) &q); |
| 2658 |
if (!part) |
| 2659 |
return -EBADMSG; |
| 2660 |
|
| 2661 |
if (q) { |
| 2662 |
/* Verify padding */ |
| 2663 |
for (k = 0; k < padding; k++) |
| 2664 |
if (q[k] != 0) |
| 2665 |
return -EBADMSG; |
| 2666 |
} |
| 2667 |
|
| 2668 |
part = find_part(m, start, nbytes, (void**) &q); |
| 2669 |
if (!part || (nbytes > 0 && !q)) |
| 2670 |
return -EBADMSG; |
| 2671 |
|
| 2672 |
*rindex = end; |
| 2673 |
|
| 2674 |
if (ret) |
| 2675 |
*ret = q; |
| 2676 |
|
| 2677 |
return 0; |
| 2678 |
} |
| 2679 |
|
| 2680 |
static bool validate_nul(const char *s, size_t l) { |
| 2681 |
|
| 2682 |
/* Check for NUL chars in the string */ |
| 2683 |
if (memchr(s, 0, l)) |
| 2684 |
return false; |
| 2685 |
|
| 2686 |
/* Check for NUL termination */ |
| 2687 |
if (s[l] != 0) |
| 2688 |
return false; |
| 2689 |
|
| 2690 |
return true; |
| 2691 |
} |
| 2692 |
|
| 2693 |
static bool validate_string(const char *s, size_t l) { |
| 2694 |
|
| 2695 |
if (!validate_nul(s, l)) |
| 2696 |
return false; |
| 2697 |
|
| 2698 |
/* Check if valid UTF8 */ |
| 2699 |
if (!utf8_is_valid(s)) |
| 2700 |
return false; |
| 2701 |
|
| 2702 |
return true; |
| 2703 |
} |
| 2704 |
|
| 2705 |
static bool validate_signature(const char *s, size_t l) { |
| 2706 |
|
| 2707 |
if (!validate_nul(s, l)) |
| 2708 |
return false; |
| 2709 |
|
| 2710 |
/* Check if valid signature */ |
| 2711 |
if (!signature_is_valid(s, true)) |
| 2712 |
return false; |
| 2713 |
|
| 2714 |
return true; |
| 2715 |
} |
| 2716 |
|
| 2717 |
static bool validate_object_path(const char *s, size_t l) { |
| 2718 |
|
| 2719 |
if (!validate_nul(s, l)) |
| 2720 |
return false; |
| 2721 |
|
| 2722 |
if (!object_path_is_valid(s)) |
| 2723 |
return false; |
| 2724 |
|
| 2725 |
return true; |
| 2726 |
} |
| 2727 |
|
| 2728 |
_public_ int sd_bus_message_read_basic(sd_bus_message *m, char type, void *ret) { |
| 2729 |
BusMessageContainer *c; |
| 2730 |
size_t rindex; |
| 2731 |
void *q; |
| 2732 |
int r; |
| 2733 |
|
| 2734 |
assert_return(m, -EINVAL); |
| 2735 |
assert_return(m->sealed, -EPERM); |
| 2736 |
assert_return(bus_type_is_basic(type), -EINVAL); |
| 2737 |
|
| 2738 |
if (message_end_of_signature(m)) |
| 2739 |
return -ENXIO; |
| 2740 |
|
| 2741 |
if (message_end_of_array(m, m->rindex)) |
| 2742 |
return 0; |
| 2743 |
|
| 2744 |
c = message_get_last_container(m); |
| 2745 |
if (c->signature[c->index] != type) |
| 2746 |
return -ENXIO; |
| 2747 |
|
| 2748 |
rindex = m->rindex; |
| 2749 |
|
| 2750 |
if (IN_SET(type, SD_BUS_TYPE_STRING, SD_BUS_TYPE_OBJECT_PATH)) { |
| 2751 |
uint32_t l; |
| 2752 |
bool ok; |
| 2753 |
|
| 2754 |
r = message_peek_body(m, &rindex, 4, 4, &q); |
| 2755 |
if (r < 0) |
| 2756 |
return r; |
| 2757 |
|
| 2758 |
l = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q); |
| 2759 |
if (l == UINT32_MAX) |
| 2760 |
/* avoid overflow right below */ |
| 2761 |
return -EBADMSG; |
| 2762 |
|
| 2763 |
r = message_peek_body(m, &rindex, 1, l+1, &q); |
| 2764 |
if (r < 0) |
| 2765 |
return r; |
| 2766 |
|
| 2767 |
if (type == SD_BUS_TYPE_OBJECT_PATH) |
| 2768 |
ok = validate_object_path(q, l); |
| 2769 |
else |
| 2770 |
ok = validate_string(q, l); |
| 2771 |
if (!ok) |
| 2772 |
return -EBADMSG; |
| 2773 |
|
| 2774 |
if (ret) |
| 2775 |
*(const char**) ret = q; |
| 2776 |
|
| 2777 |
} else if (type == SD_BUS_TYPE_SIGNATURE) { |
| 2778 |
uint8_t l; |
| 2779 |
|
| 2780 |
r = message_peek_body(m, &rindex, 1, 1, &q); |
| 2781 |
if (r < 0) |
| 2782 |
return r; |
| 2783 |
|
| 2784 |
l = *(uint8_t*) q; |
| 2785 |
if (l == UINT8_MAX) |
| 2786 |
/* avoid overflow right below */ |
| 2787 |
return -EBADMSG; |
| 2788 |
|
| 2789 |
r = message_peek_body(m, &rindex, 1, l+1, &q); |
| 2790 |
if (r < 0) |
| 2791 |
return r; |
| 2792 |
|
| 2793 |
if (!validate_signature(q, l)) |
| 2794 |
return -EBADMSG; |
| 2795 |
|
| 2796 |
if (ret) |
| 2797 |
*(const char**) ret = q; |
| 2798 |
|
| 2799 |
} else { |
| 2800 |
ssize_t sz, align; |
| 2801 |
|
| 2802 |
align = bus_type_get_alignment(type); |
| 2803 |
assert(align > 0); |
| 2804 |
|
| 2805 |
sz = bus_type_get_size(type); |
| 2806 |
assert(sz > 0); |
| 2807 |
|
| 2808 |
r = message_peek_body(m, &rindex, align, sz, &q); |
| 2809 |
if (r < 0) |
| 2810 |
return r; |
| 2811 |
|
| 2812 |
switch (type) { |
| 2813 |
|
| 2814 |
case SD_BUS_TYPE_BYTE: |
| 2815 |
if (ret) |
| 2816 |
*(uint8_t*) ret = *(uint8_t*) q; |
| 2817 |
break; |
| 2818 |
|
| 2819 |
case SD_BUS_TYPE_BOOLEAN: |
| 2820 |
if (ret) |
| 2821 |
*(int*) ret = !!*(uint32_t*) q; |
| 2822 |
break; |
| 2823 |
|
| 2824 |
case SD_BUS_TYPE_INT16: |
| 2825 |
case SD_BUS_TYPE_UINT16: |
| 2826 |
if (ret) |
| 2827 |
*(uint16_t*) ret = BUS_MESSAGE_BSWAP16(m, *(uint16_t*) q); |
| 2828 |
break; |
| 2829 |
|
| 2830 |
case SD_BUS_TYPE_INT32: |
| 2831 |
case SD_BUS_TYPE_UINT32: |
| 2832 |
if (ret) |
| 2833 |
*(uint32_t*) ret = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q); |
| 2834 |
break; |
| 2835 |
|
| 2836 |
case SD_BUS_TYPE_INT64: |
| 2837 |
case SD_BUS_TYPE_UINT64: |
| 2838 |
case SD_BUS_TYPE_DOUBLE: |
| 2839 |
if (ret) |
| 2840 |
*(uint64_t*) ret = BUS_MESSAGE_BSWAP64(m, *(uint64_t*) q); |
| 2841 |
break; |
| 2842 |
|
| 2843 |
case SD_BUS_TYPE_UNIX_FD: { |
| 2844 |
uint32_t j; |
| 2845 |
|
| 2846 |
j = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q); |
| 2847 |
if (j >= m->n_fds) |
| 2848 |
return -EBADMSG; |
| 2849 |
|
| 2850 |
if (ret) |
| 2851 |
*(int*) ret = m->fds[j]; |
| 2852 |
break; |
| 2853 |
} |
| 2854 |
|
| 2855 |
default: |
| 2856 |
assert_not_reached(); |
| 2857 |
} |
| 2858 |
} |
| 2859 |
|
| 2860 |
m->rindex = rindex; |
| 2861 |
|
| 2862 |
if (c->enclosing != SD_BUS_TYPE_ARRAY) |
| 2863 |
c->index++; |
| 2864 |
|
| 2865 |
return 1; |
| 2866 |
} |
| 2867 |
|
| 2868 |
static int bus_message_enter_array( |
| 2869 |
sd_bus_message *m, |
| 2870 |
BusMessageContainer *c, |
| 2871 |
const char *contents, |
| 2872 |
uint32_t **ret_array_size) { |
| 2873 |
|
| 2874 |
size_t rindex; |
| 2875 |
void *q; |
| 2876 |
int alignment, r; |
| 2877 |
|
| 2878 |
assert(m); |
| 2879 |
assert(c); |
| 2880 |
assert(contents); |
| 2881 |
assert(ret_array_size); |
| 2882 |
|
| 2883 |
if (!signature_is_single(contents, true)) |
| 2884 |
return -EINVAL; |
| 2885 |
|
| 2886 |
if (!c->signature || c->signature[c->index] == 0) |
| 2887 |
return -ENXIO; |
| 2888 |
|
| 2889 |
if (c->signature[c->index] != SD_BUS_TYPE_ARRAY) |
| 2890 |
return -ENXIO; |
| 2891 |
|
| 2892 |
if (!startswith(c->signature + c->index + 1, contents)) |
| 2893 |
return -ENXIO; |
| 2894 |
|
| 2895 |
rindex = m->rindex; |
| 2896 |
|
| 2897 |
r = message_peek_body(m, &rindex, 4, 4, &q); |
| 2898 |
if (r < 0) |
| 2899 |
return r; |
| 2900 |
|
| 2901 |
if (BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q) > BUS_ARRAY_MAX_SIZE) |
| 2902 |
return -EBADMSG; |
| 2903 |
|
| 2904 |
alignment = bus_type_get_alignment(contents[0]); |
| 2905 |
if (alignment < 0) |
| 2906 |
return alignment; |
| 2907 |
|
| 2908 |
r = message_peek_body(m, &rindex, alignment, 0, NULL); |
| 2909 |
if (r < 0) |
| 2910 |
return r; |
| 2911 |
|
| 2912 |
*ret_array_size = (uint32_t*) q; |
| 2913 |
|
| 2914 |
m->rindex = rindex; |
| 2915 |
|
| 2916 |
if (c->enclosing != SD_BUS_TYPE_ARRAY) |
| 2917 |
c->index += 1 + strlen(contents); |
| 2918 |
|
| 2919 |
return 1; |
| 2920 |
} |
| 2921 |
|
| 2922 |
static int bus_message_enter_variant( |
| 2923 |
sd_bus_message *m, |
| 2924 |
BusMessageContainer *c, |
| 2925 |
const char *contents) { |
| 2926 |
|
| 2927 |
size_t rindex; |
| 2928 |
uint8_t l; |
| 2929 |
void *q; |
| 2930 |
int r; |
| 2931 |
|
| 2932 |
assert(m); |
| 2933 |
assert(c); |
| 2934 |
assert(contents); |
| 2935 |
|
| 2936 |
if (!signature_is_single(contents, false)) |
| 2937 |
return -EINVAL; |
| 2938 |
|
| 2939 |
if (*contents == SD_BUS_TYPE_DICT_ENTRY_BEGIN) |
| 2940 |
return -EINVAL; |
| 2941 |
|
| 2942 |
if (!c->signature || c->signature[c->index] == 0) |
| 2943 |
return -ENXIO; |
| 2944 |
|
| 2945 |
if (c->signature[c->index] != SD_BUS_TYPE_VARIANT) |
| 2946 |
return -ENXIO; |
| 2947 |
|
| 2948 |
rindex = m->rindex; |
| 2949 |
|
| 2950 |
r = message_peek_body(m, &rindex, 1, 1, &q); |
| 2951 |
if (r < 0) |
| 2952 |
return r; |
| 2953 |
|
| 2954 |
l = *(uint8_t*) q; |
| 2955 |
if (l == UINT8_MAX) |
| 2956 |
/* avoid overflow right below */ |
| 2957 |
return -EBADMSG; |
| 2958 |
|
| 2959 |
r = message_peek_body(m, &rindex, 1, l+1, &q); |
| 2960 |
if (r < 0) |
| 2961 |
return r; |
| 2962 |
|
| 2963 |
if (!validate_signature(q, l)) |
| 2964 |
return -EBADMSG; |
| 2965 |
|
| 2966 |
if (!streq(q, contents)) |
| 2967 |
return -ENXIO; |
| 2968 |
|
| 2969 |
m->rindex = rindex; |
| 2970 |
|
| 2971 |
if (c->enclosing != SD_BUS_TYPE_ARRAY) |
| 2972 |
c->index++; |
| 2973 |
|
| 2974 |
return 1; |
| 2975 |
} |
| 2976 |
|
| 2977 |
static int bus_message_enter_struct( |
| 2978 |
sd_bus_message *m, |
| 2979 |
BusMessageContainer *c, |
| 2980 |
const char *contents) { |
| 2981 |
|
| 2982 |
size_t l; |
| 2983 |
int r; |
| 2984 |
|
| 2985 |
assert(m); |
| 2986 |
assert(c); |
| 2987 |
assert(contents); |
| 2988 |
|
| 2989 |
if (!signature_is_valid(contents, false)) |
| 2990 |
return -EINVAL; |
| 2991 |
|
| 2992 |
if (!c->signature || c->signature[c->index] == 0) |
| 2993 |
return -ENXIO; |
| 2994 |
|
| 2995 |
l = strlen(contents); |
| 2996 |
|
| 2997 |
if (c->signature[c->index] != SD_BUS_TYPE_STRUCT_BEGIN || |
| 2998 |
!startswith(c->signature + c->index + 1, contents) || |
| 2999 |
c->signature[c->index + 1 + l] != SD_BUS_TYPE_STRUCT_END) |
| 3000 |
return -ENXIO; |
| 3001 |
|
| 3002 |
r = message_peek_body(m, &m->rindex, 8, 0, NULL); |
| 3003 |
if (r < 0) |
| 3004 |
return r; |
| 3005 |
|
| 3006 |
if (c->enclosing != SD_BUS_TYPE_ARRAY) |
| 3007 |
c->index += 1 + l + 1; |
| 3008 |
|
| 3009 |
return 1; |
| 3010 |
} |
| 3011 |
|
| 3012 |
static int bus_message_enter_dict_entry( |
| 3013 |
sd_bus_message *m, |
| 3014 |
BusMessageContainer *c, |
| 3015 |
const char *contents) { |
| 3016 |
|
| 3017 |
size_t l; |
| 3018 |
int r; |
| 3019 |
|
| 3020 |
assert(m); |
| 3021 |
assert(c); |
| 3022 |
assert(contents); |
| 3023 |
|
| 3024 |
if (!signature_is_pair(contents)) |
| 3025 |
return -EINVAL; |
| 3026 |
|
| 3027 |
if (c->enclosing != SD_BUS_TYPE_ARRAY) |
| 3028 |
return -ENXIO; |
| 3029 |
|
| 3030 |
if (!c->signature || c->signature[c->index] == 0) |
| 3031 |
return 0; |
| 3032 |
|
| 3033 |
l = strlen(contents); |
| 3034 |
|
| 3035 |
if (c->signature[c->index] != SD_BUS_TYPE_DICT_ENTRY_BEGIN || |
| 3036 |
!startswith(c->signature + c->index + 1, contents) || |
| 3037 |
c->signature[c->index + 1 + l] != SD_BUS_TYPE_DICT_ENTRY_END) |
| 3038 |
return -ENXIO; |
| 3039 |
|
| 3040 |
r = message_peek_body(m, &m->rindex, 8, 0, NULL); |
| 3041 |
if (r < 0) |
| 3042 |
return r; |
| 3043 |
|
| 3044 |
if (c->enclosing != SD_BUS_TYPE_ARRAY) |
| 3045 |
c->index += 1 + l + 1; |
| 3046 |
|
| 3047 |
return 1; |
| 3048 |
} |
| 3049 |
|
| 3050 |
_public_ int sd_bus_message_enter_container( |
| 3051 |
sd_bus_message *m, |
| 3052 |
char type, |
| 3053 |
const char *contents) { |
| 3054 |
|
| 3055 |
BusMessageContainer *c; |
| 3056 |
uint32_t *array_size = NULL; |
| 3057 |
_cleanup_free_ char *signature = NULL; |
| 3058 |
size_t before; |
| 3059 |
int r; |
| 3060 |
|
| 3061 |
assert_return(m, -EINVAL); |
| 3062 |
assert_return(m->sealed, -EPERM); |
| 3063 |
assert_return(type != 0 || !contents, -EINVAL); |
| 3064 |
|
| 3065 |
if (type == 0 || !contents) { |
| 3066 |
const char *cc; |
| 3067 |
char tt; |
| 3068 |
|
| 3069 |
/* Allow entering into anonymous containers */ |
| 3070 |
r = sd_bus_message_peek_type(m, &tt, &cc); |
| 3071 |
if (r < 0) |
| 3072 |
return r; |
| 3073 |
|
| 3074 |
if (type != 0 && type != tt) |
| 3075 |
return -ENXIO; |
| 3076 |
|
| 3077 |
if (contents && !streq(contents, cc)) |
| 3078 |
return -ENXIO; |
| 3079 |
|
| 3080 |
type = tt; |
| 3081 |
contents = cc; |
| 3082 |
} |
| 3083 |
|
| 3084 |
/* |
| 3085 |
* We enforce a global limit on container depth, that is much |
| 3086 |
* higher than the 32 structs and 32 arrays the specification |
| 3087 |
* mandates. This is simpler to implement for us, and we need |
| 3088 |
* this only to ensure our container array doesn't grow |
| 3089 |
* without bounds. We are happy to return any data from a |
| 3090 |
* message as long as the data itself is valid, even if the |
| 3091 |
* overall message might be not. |
| 3092 |
* |
| 3093 |
* Note that the message signature is validated when |
| 3094 |
* parsing the headers, and that validation does check the |
| 3095 |
* 32/32 limit. |
| 3096 |
* |
| 3097 |
* Note that the specification defines no limits on the depth |
| 3098 |
* of stacked variants, but we do. |
| 3099 |
*/ |
| 3100 |
if (m->n_containers >= BUS_CONTAINER_DEPTH) |
| 3101 |
return -EBADMSG; |
| 3102 |
|
| 3103 |
if (!GREEDY_REALLOC(m->containers, m->n_containers + 1)) |
| 3104 |
return -ENOMEM; |
| 3105 |
|
| 3106 |
if (message_end_of_signature(m)) |
| 3107 |
return -ENXIO; |
| 3108 |
|
| 3109 |
if (message_end_of_array(m, m->rindex)) |
| 3110 |
return 0; |
| 3111 |
|
| 3112 |
c = message_get_last_container(m); |
| 3113 |
|
| 3114 |
signature = strdup(contents); |
| 3115 |
if (!signature) |
| 3116 |
return -ENOMEM; |
| 3117 |
|
| 3118 |
c->saved_index = c->index; |
| 3119 |
before = m->rindex; |
| 3120 |
|
| 3121 |
if (type == SD_BUS_TYPE_ARRAY) |
| 3122 |
r = bus_message_enter_array(m, c, contents, &array_size); |
| 3123 |
else if (type == SD_BUS_TYPE_VARIANT) |
| 3124 |
r = bus_message_enter_variant(m, c, contents); |
| 3125 |
else if (type == SD_BUS_TYPE_STRUCT) |
| 3126 |
r = bus_message_enter_struct(m, c, contents); |
| 3127 |
else if (type == SD_BUS_TYPE_DICT_ENTRY) |
| 3128 |
r = bus_message_enter_dict_entry(m, c, contents); |
| 3129 |
else |
| 3130 |
r = -EINVAL; |
| 3131 |
if (r <= 0) |
| 3132 |
return r; |
| 3133 |
|
| 3134 |
/* OK, let's fill it in */ |
| 3135 |
m->containers[m->n_containers++] = (BusMessageContainer) { |
| 3136 |
.enclosing = type, |
| 3137 |
.signature = TAKE_PTR(signature), |
| 3138 |
|
| 3139 |
.before = before, |
| 3140 |
.begin = m->rindex, |
| 3141 |
/* Unary type has fixed size of 1, but virtual size of 0 */ |
| 3142 |
.end = m->rindex, |
| 3143 |
.array_size = array_size, |
| 3144 |
}; |
| 3145 |
|
| 3146 |
return 1; |
| 3147 |
} |
| 3148 |
|
| 3149 |
_public_ int sd_bus_message_exit_container(sd_bus_message *m) { |
| 3150 |
BusMessageContainer *c; |
| 3151 |
|
| 3152 |
assert_return(m, -EINVAL); |
| 3153 |
assert_return(m->sealed, -EPERM); |
| 3154 |
assert_return(m->n_containers > 0, -ENXIO); |
| 3155 |
|
| 3156 |
c = message_get_last_container(m); |
| 3157 |
|
| 3158 |
if (c->enclosing != SD_BUS_TYPE_ARRAY) { |
| 3159 |
if (c->signature && c->signature[c->index] != 0) |
| 3160 |
return -EBUSY; |
| 3161 |
} |
| 3162 |
|
| 3163 |
if (c->enclosing == SD_BUS_TYPE_ARRAY) { |
| 3164 |
uint32_t l; |
| 3165 |
|
| 3166 |
l = BUS_MESSAGE_BSWAP32(m, *c->array_size); |
| 3167 |
if (c->begin + l != m->rindex) |
| 3168 |
return -EBUSY; |
| 3169 |
} |
| 3170 |
|
| 3171 |
message_free_last_container(m); |
| 3172 |
|
| 3173 |
return 1; |
| 3174 |
} |
| 3175 |
|
| 3176 |
static void message_quit_container(sd_bus_message *m) { |
| 3177 |
BusMessageContainer *c; |
| 3178 |
|
| 3179 |
assert(m); |
| 3180 |
assert(m->sealed); |
| 3181 |
assert(m->n_containers > 0); |
| 3182 |
|
| 3183 |
/* Undo seeks */ |
| 3184 |
c = message_get_last_container(m); |
| 3185 |
assert(m->rindex >= c->before); |
| 3186 |
m->rindex = c->before; |
| 3187 |
|
| 3188 |
/* Free container */ |
| 3189 |
message_free_last_container(m); |
| 3190 |
|
| 3191 |
/* Correct index of new top-level container */ |
| 3192 |
c = message_get_last_container(m); |
| 3193 |
c->index = c->saved_index; |
| 3194 |
} |
| 3195 |
|
| 3196 |
_public_ int sd_bus_message_peek_type(sd_bus_message *m, char *ret_type, const char **ret_contents) { |
| 3197 |
BusMessageContainer *c; |
| 3198 |
int r; |
| 3199 |
|
| 3200 |
assert_return(m, -EINVAL); |
| 3201 |
assert_return(m->sealed, -EPERM); |
| 3202 |
|
| 3203 |
if (message_end_of_signature(m)) |
| 3204 |
goto eof; |
| 3205 |
|
| 3206 |
if (message_end_of_array(m, m->rindex)) |
| 3207 |
goto eof; |
| 3208 |
|
| 3209 |
c = message_get_last_container(m); |
| 3210 |
|
| 3211 |
if (bus_type_is_basic(c->signature[c->index])) { |
| 3212 |
if (ret_contents) |
| 3213 |
*ret_contents = NULL; |
| 3214 |
if (ret_type) |
| 3215 |
*ret_type = c->signature[c->index]; |
| 3216 |
return 1; |
| 3217 |
} |
| 3218 |
|
| 3219 |
if (c->signature[c->index] == SD_BUS_TYPE_ARRAY) { |
| 3220 |
|
| 3221 |
if (ret_contents) { |
| 3222 |
size_t l; |
| 3223 |
|
| 3224 |
r = signature_element_length(c->signature+c->index+1, &l); |
| 3225 |
if (r < 0) |
| 3226 |
return r; |
| 3227 |
|
| 3228 |
/* signature_element_length does verification internally */ |
| 3229 |
|
| 3230 |
/* The array element must not be empty */ |
| 3231 |
assert(l >= 1); |
| 3232 |
if (free_and_strndup(&c->peeked_signature, |
| 3233 |
c->signature + c->index + 1, l) < 0) |
| 3234 |
return -ENOMEM; |
| 3235 |
|
| 3236 |
*ret_contents = c->peeked_signature; |
| 3237 |
} |
| 3238 |
|
| 3239 |
if (ret_type) |
| 3240 |
*ret_type = SD_BUS_TYPE_ARRAY; |
| 3241 |
|
| 3242 |
return 1; |
| 3243 |
} |
| 3244 |
|
| 3245 |
if (IN_SET(c->signature[c->index], SD_BUS_TYPE_STRUCT_BEGIN, SD_BUS_TYPE_DICT_ENTRY_BEGIN)) { |
| 3246 |
|
| 3247 |
if (ret_contents) { |
| 3248 |
size_t l; |
| 3249 |
|
| 3250 |
r = signature_element_length(c->signature+c->index, &l); |
| 3251 |
if (r < 0) |
| 3252 |
return r; |
| 3253 |
|
| 3254 |
assert(l >= 3); |
| 3255 |
if (free_and_strndup(&c->peeked_signature, |
| 3256 |
c->signature + c->index + 1, l - 2) < 0) |
| 3257 |
return -ENOMEM; |
| 3258 |
|
| 3259 |
*ret_contents = c->peeked_signature; |
| 3260 |
} |
| 3261 |
|
| 3262 |
if (ret_type) |
| 3263 |
*ret_type = c->signature[c->index] == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY; |
| 3264 |
|
| 3265 |
return 1; |
| 3266 |
} |
| 3267 |
|
| 3268 |
if (c->signature[c->index] == SD_BUS_TYPE_VARIANT) { |
| 3269 |
if (ret_contents) { |
| 3270 |
size_t rindex, l; |
| 3271 |
void *q; |
| 3272 |
|
| 3273 |
rindex = m->rindex; |
| 3274 |
r = message_peek_body(m, &rindex, 1, 1, &q); |
| 3275 |
if (r < 0) |
| 3276 |
return r; |
| 3277 |
|
| 3278 |
l = *(uint8_t*) q; |
| 3279 |
if (l == UINT8_MAX) |
| 3280 |
/* avoid overflow right below */ |
| 3281 |
return -EBADMSG; |
| 3282 |
|
| 3283 |
r = message_peek_body(m, &rindex, 1, l+1, &q); |
| 3284 |
if (r < 0) |
| 3285 |
return r; |
| 3286 |
|
| 3287 |
if (!validate_signature(q, l)) |
| 3288 |
return -EBADMSG; |
| 3289 |
|
| 3290 |
*ret_contents = q; |
| 3291 |
} |
| 3292 |
|
| 3293 |
if (ret_type) |
| 3294 |
*ret_type = SD_BUS_TYPE_VARIANT; |
| 3295 |
|
| 3296 |
return 1; |
| 3297 |
} |
| 3298 |
|
| 3299 |
return -EINVAL; |
| 3300 |
|
| 3301 |
eof: |
| 3302 |
if (ret_type) |
| 3303 |
*ret_type = 0; |
| 3304 |
if (ret_contents) |
| 3305 |
*ret_contents = NULL; |
| 3306 |
return 0; |
| 3307 |
} |
| 3308 |
|
| 3309 |
_public_ int sd_bus_message_rewind(sd_bus_message *m, int complete) { |
| 3310 |
BusMessageContainer *c; |
| 3311 |
|
| 3312 |
assert_return(m, -EINVAL); |
| 3313 |
assert_return(m->sealed, -EPERM); |
| 3314 |
|
| 3315 |
if (complete) { |
| 3316 |
message_reset_containers(m); |
| 3317 |
m->rindex = 0; |
| 3318 |
|
| 3319 |
c = message_get_last_container(m); |
| 3320 |
} else { |
| 3321 |
c = message_get_last_container(m); |
| 3322 |
|
| 3323 |
c->index = 0; |
| 3324 |
m->rindex = c->begin; |
| 3325 |
} |
| 3326 |
|
| 3327 |
return !isempty(c->signature); |
| 3328 |
} |
| 3329 |
|
| 3330 |
_public_ int sd_bus_message_readv( |
| 3331 |
sd_bus_message *m, |
| 3332 |
const char *types, |
| 3333 |
va_list ap) { |
| 3334 |
|
| 3335 |
unsigned n_array, n_struct; |
| 3336 |
BusTypeStack stack[BUS_CONTAINER_DEPTH]; |
| 3337 |
unsigned stack_ptr = 0; |
| 3338 |
unsigned n_loop = 0; |
| 3339 |
int r; |
| 3340 |
|
| 3341 |
assert_return(m, -EINVAL); |
| 3342 |
assert_return(m->sealed, -EPERM); |
| 3343 |
assert_return(types, -EINVAL); |
| 3344 |
|
| 3345 |
if (isempty(types)) |
| 3346 |
return 0; |
| 3347 |
|
| 3348 |
/* Ideally, we'd just call ourselves recursively on every |
| 3349 |
* complex type. However, the state of a va_list that is |
| 3350 |
* passed to a function is undefined after that function |
| 3351 |
* returns. This means we need to decode the va_list linearly |
| 3352 |
* in a single stackframe. We hence implement our own |
| 3353 |
* home-grown stack in an array. */ |
| 3354 |
|
| 3355 |
n_array = UINT_MAX; /* length of current array entries */ |
| 3356 |
n_struct = strlen(types); /* length of current struct contents signature */ |
| 3357 |
|
| 3358 |
for (;;) { |
| 3359 |
const char *t; |
| 3360 |
|
| 3361 |
n_loop++; |
| 3362 |
|
| 3363 |
if (n_array == 0 || (n_array == UINT_MAX && n_struct == 0)) { |
| 3364 |
r = type_stack_pop(stack, ELEMENTSOF(stack), &stack_ptr, &types, &n_struct, &n_array); |
| 3365 |
if (r < 0) |
| 3366 |
return r; |
| 3367 |
if (r == 0) |
| 3368 |
break; |
| 3369 |
|
| 3370 |
r = sd_bus_message_exit_container(m); |
| 3371 |
if (r < 0) |
| 3372 |
return r; |
| 3373 |
|
| 3374 |
continue; |
| 3375 |
} |
| 3376 |
|
| 3377 |
t = types; |
| 3378 |
if (n_array != UINT_MAX) |
| 3379 |
n_array--; |
| 3380 |
else { |
| 3381 |
types++; |
| 3382 |
n_struct--; |
| 3383 |
} |
| 3384 |
|
| 3385 |
switch (*t) { |
| 3386 |
|
| 3387 |
case SD_BUS_TYPE_BYTE: |
| 3388 |
case SD_BUS_TYPE_BOOLEAN: |
| 3389 |
case SD_BUS_TYPE_INT16: |
| 3390 |
case SD_BUS_TYPE_UINT16: |
| 3391 |
case SD_BUS_TYPE_INT32: |
| 3392 |
case SD_BUS_TYPE_UINT32: |
| 3393 |
case SD_BUS_TYPE_INT64: |
| 3394 |
case SD_BUS_TYPE_UINT64: |
| 3395 |
case SD_BUS_TYPE_DOUBLE: |
| 3396 |
case SD_BUS_TYPE_STRING: |
| 3397 |
case SD_BUS_TYPE_OBJECT_PATH: |
| 3398 |
case SD_BUS_TYPE_SIGNATURE: |
| 3399 |
case SD_BUS_TYPE_UNIX_FD: { |
| 3400 |
void *p; |
| 3401 |
|
| 3402 |
p = va_arg(ap, void*); |
| 3403 |
r = sd_bus_message_read_basic(m, *t, p); |
| 3404 |
if (r < 0) |
| 3405 |
return r; |
| 3406 |
if (r == 0) { |
| 3407 |
if (n_loop <= 1) |
| 3408 |
return 0; |
| 3409 |
|
| 3410 |
return -ENXIO; |
| 3411 |
} |
| 3412 |
|
| 3413 |
break; |
| 3414 |
} |
| 3415 |
|
| 3416 |
case SD_BUS_TYPE_ARRAY: { |
| 3417 |
size_t k; |
| 3418 |
|
| 3419 |
r = signature_element_length(t + 1, &k); |
| 3420 |
if (r < 0) |
| 3421 |
return r; |
| 3422 |
|
| 3423 |
{ |
| 3424 |
char s[k + 1]; |
| 3425 |
memcpy(s, t + 1, k); |
| 3426 |
s[k] = 0; |
| 3427 |
|
| 3428 |
r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, s); |
| 3429 |
if (r < 0) |
| 3430 |
return r; |
| 3431 |
if (r == 0) { |
| 3432 |
if (n_loop <= 1) |
| 3433 |
return 0; |
| 3434 |
|
| 3435 |
return -ENXIO; |
| 3436 |
} |
| 3437 |
} |
| 3438 |
|
| 3439 |
if (n_array == UINT_MAX) { |
| 3440 |
types += k; |
| 3441 |
n_struct -= k; |
| 3442 |
} |
| 3443 |
|
| 3444 |
r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array); |
| 3445 |
if (r < 0) |
| 3446 |
return r; |
| 3447 |
|
| 3448 |
types = t + 1; |
| 3449 |
n_struct = k; |
| 3450 |
n_array = va_arg(ap, unsigned); |
| 3451 |
|
| 3452 |
break; |
| 3453 |
} |
| 3454 |
|
| 3455 |
case SD_BUS_TYPE_VARIANT: { |
| 3456 |
const char *s; |
| 3457 |
|
| 3458 |
s = va_arg(ap, const char *); |
| 3459 |
if (!s) |
| 3460 |
return -EINVAL; |
| 3461 |
|
| 3462 |
r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, s); |
| 3463 |
if (r < 0) |
| 3464 |
return r; |
| 3465 |
if (r == 0) { |
| 3466 |
if (n_loop <= 1) |
| 3467 |
return 0; |
| 3468 |
|
| 3469 |
return -ENXIO; |
| 3470 |
} |
| 3471 |
|
| 3472 |
r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array); |
| 3473 |
if (r < 0) |
| 3474 |
return r; |
| 3475 |
|
| 3476 |
types = s; |
| 3477 |
n_struct = strlen(s); |
| 3478 |
n_array = UINT_MAX; |
| 3479 |
|
| 3480 |
break; |
| 3481 |
} |
| 3482 |
|
| 3483 |
case SD_BUS_TYPE_STRUCT_BEGIN: |
| 3484 |
case SD_BUS_TYPE_DICT_ENTRY_BEGIN: { |
| 3485 |
size_t k; |
| 3486 |
|
| 3487 |
r = signature_element_length(t, &k); |
| 3488 |
if (r < 0) |
| 3489 |
return r; |
| 3490 |
if (k < 2) |
| 3491 |
return -ERANGE; |
| 3492 |
|
| 3493 |
{ |
| 3494 |
char s[k - 1]; |
| 3495 |
memcpy(s, t + 1, k - 2); |
| 3496 |
s[k - 2] = 0; |
| 3497 |
|
| 3498 |
r = sd_bus_message_enter_container(m, *t == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s); |
| 3499 |
if (r < 0) |
| 3500 |
return r; |
| 3501 |
if (r == 0) { |
| 3502 |
if (n_loop <= 1) |
| 3503 |
return 0; |
| 3504 |
return -ENXIO; |
| 3505 |
} |
| 3506 |
} |
| 3507 |
|
| 3508 |
if (n_array == UINT_MAX) { |
| 3509 |
types += k - 1; |
| 3510 |
n_struct -= k - 1; |
| 3511 |
} |
| 3512 |
|
| 3513 |
r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array); |
| 3514 |
if (r < 0) |
| 3515 |
return r; |
| 3516 |
|
| 3517 |
types = t + 1; |
| 3518 |
n_struct = k - 2; |
| 3519 |
n_array = UINT_MAX; |
| 3520 |
|
| 3521 |
break; |
| 3522 |
} |
| 3523 |
|
| 3524 |
default: |
| 3525 |
return -EINVAL; |
| 3526 |
} |
| 3527 |
} |
| 3528 |
|
| 3529 |
return 1; |
| 3530 |
} |
| 3531 |
|
| 3532 |
_public_ int sd_bus_message_read(sd_bus_message *m, const char *types, ...) { |
| 3533 |
va_list ap; |
| 3534 |
int r; |
| 3535 |
|
| 3536 |
va_start(ap, types); |
| 3537 |
r = sd_bus_message_readv(m, types, ap); |
| 3538 |
va_end(ap); |
| 3539 |
|
| 3540 |
return r; |
| 3541 |
} |
| 3542 |
|
| 3543 |
_public_ int sd_bus_message_skip(sd_bus_message *m, const char *types) { |
| 3544 |
int r; |
| 3545 |
|
| 3546 |
assert_return(m, -EINVAL); |
| 3547 |
assert_return(m->sealed, -EPERM); |
| 3548 |
|
| 3549 |
/* If types is NULL, read exactly one element */ |
| 3550 |
if (!types) { |
| 3551 |
BusMessageContainer *c; |
| 3552 |
size_t l; |
| 3553 |
|
| 3554 |
if (message_end_of_signature(m)) |
| 3555 |
return -ENXIO; |
| 3556 |
|
| 3557 |
if (message_end_of_array(m, m->rindex)) |
| 3558 |
return 0; |
| 3559 |
|
| 3560 |
c = message_get_last_container(m); |
| 3561 |
|
| 3562 |
r = signature_element_length(c->signature + c->index, &l); |
| 3563 |
if (r < 0) |
| 3564 |
return r; |
| 3565 |
|
| 3566 |
types = strndupa_safe(c->signature + c->index, l); |
| 3567 |
} |
| 3568 |
|
| 3569 |
switch (*types) { |
| 3570 |
|
| 3571 |
case 0: /* Nothing to drop */ |
| 3572 |
return 0; |
| 3573 |
|
| 3574 |
case SD_BUS_TYPE_BYTE: |
| 3575 |
case SD_BUS_TYPE_BOOLEAN: |
| 3576 |
case SD_BUS_TYPE_INT16: |
| 3577 |
case SD_BUS_TYPE_UINT16: |
| 3578 |
case SD_BUS_TYPE_INT32: |
| 3579 |
case SD_BUS_TYPE_UINT32: |
| 3580 |
case SD_BUS_TYPE_INT64: |
| 3581 |
case SD_BUS_TYPE_UINT64: |
| 3582 |
case SD_BUS_TYPE_DOUBLE: |
| 3583 |
case SD_BUS_TYPE_STRING: |
| 3584 |
case SD_BUS_TYPE_OBJECT_PATH: |
| 3585 |
case SD_BUS_TYPE_SIGNATURE: |
| 3586 |
case SD_BUS_TYPE_UNIX_FD: |
| 3587 |
|
| 3588 |
r = sd_bus_message_read_basic(m, *types, NULL); |
| 3589 |
if (r <= 0) |
| 3590 |
return r; |
| 3591 |
|
| 3592 |
r = sd_bus_message_skip(m, types + 1); |
| 3593 |
if (r < 0) |
| 3594 |
return r; |
| 3595 |
|
| 3596 |
return 1; |
| 3597 |
|
| 3598 |
case SD_BUS_TYPE_ARRAY: { |
| 3599 |
size_t k; |
| 3600 |
|
| 3601 |
r = signature_element_length(types + 1, &k); |
| 3602 |
if (r < 0) |
| 3603 |
return r; |
| 3604 |
|
| 3605 |
{ |
| 3606 |
char s[k+1]; |
| 3607 |
memcpy(s, types+1, k); |
| 3608 |
s[k] = 0; |
| 3609 |
|
| 3610 |
r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, s); |
| 3611 |
if (r <= 0) |
| 3612 |
return r; |
| 3613 |
|
| 3614 |
for (;;) { |
| 3615 |
r = sd_bus_message_skip(m, s); |
| 3616 |
if (r < 0) |
| 3617 |
return r; |
| 3618 |
if (r == 0) |
| 3619 |
break; |
| 3620 |
} |
| 3621 |
|
| 3622 |
r = sd_bus_message_exit_container(m); |
| 3623 |
if (r < 0) |
| 3624 |
return r; |
| 3625 |
} |
| 3626 |
|
| 3627 |
r = sd_bus_message_skip(m, types + 1 + k); |
| 3628 |
if (r < 0) |
| 3629 |
return r; |
| 3630 |
|
| 3631 |
return 1; |
| 3632 |
} |
| 3633 |
|
| 3634 |
case SD_BUS_TYPE_VARIANT: { |
| 3635 |
const char *contents; |
| 3636 |
char x; |
| 3637 |
|
| 3638 |
r = sd_bus_message_peek_type(m, &x, &contents); |
| 3639 |
if (r <= 0) |
| 3640 |
return r; |
| 3641 |
|
| 3642 |
if (x != SD_BUS_TYPE_VARIANT) |
| 3643 |
return -ENXIO; |
| 3644 |
|
| 3645 |
r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents); |
| 3646 |
if (r <= 0) |
| 3647 |
return r; |
| 3648 |
|
| 3649 |
r = sd_bus_message_skip(m, contents); |
| 3650 |
if (r < 0) |
| 3651 |
return r; |
| 3652 |
assert(r != 0); |
| 3653 |
|
| 3654 |
r = sd_bus_message_exit_container(m); |
| 3655 |
if (r < 0) |
| 3656 |
return r; |
| 3657 |
|
| 3658 |
r = sd_bus_message_skip(m, types + 1); |
| 3659 |
if (r < 0) |
| 3660 |
return r; |
| 3661 |
|
| 3662 |
return 1; |
| 3663 |
} |
| 3664 |
|
| 3665 |
case SD_BUS_TYPE_STRUCT_BEGIN: |
| 3666 |
case SD_BUS_TYPE_DICT_ENTRY_BEGIN: { |
| 3667 |
size_t k; |
| 3668 |
|
| 3669 |
r = signature_element_length(types, &k); |
| 3670 |
if (r < 0) |
| 3671 |
return r; |
| 3672 |
if (k < 2) |
| 3673 |
return -ERANGE; |
| 3674 |
|
| 3675 |
{ |
| 3676 |
char s[k-1]; |
| 3677 |
memcpy(s, types+1, k-2); |
| 3678 |
s[k-2] = 0; |
| 3679 |
|
| 3680 |
r = sd_bus_message_enter_container(m, *types == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s); |
| 3681 |
if (r <= 0) |
| 3682 |
return r; |
| 3683 |
|
| 3684 |
r = sd_bus_message_skip(m, s); |
| 3685 |
if (r < 0) |
| 3686 |
return r; |
| 3687 |
|
| 3688 |
r = sd_bus_message_exit_container(m); |
| 3689 |
if (r < 0) |
| 3690 |
return r; |
| 3691 |
} |
| 3692 |
|
| 3693 |
r = sd_bus_message_skip(m, types + k); |
| 3694 |
if (r < 0) |
| 3695 |
return r; |
| 3696 |
|
| 3697 |
return 1; |
| 3698 |
} |
| 3699 |
|
| 3700 |
default: |
| 3701 |
return -EINVAL; |
| 3702 |
} |
| 3703 |
} |
| 3704 |
|
| 3705 |
_public_ int sd_bus_message_read_array( |
| 3706 |
sd_bus_message *m, |
| 3707 |
char type, |
| 3708 |
const void **ret_ptr, |
| 3709 |
size_t *ret_size) { |
| 3710 |
|
| 3711 |
BusMessageContainer *c; |
| 3712 |
void *p; |
| 3713 |
size_t sz; |
| 3714 |
ssize_t align; |
| 3715 |
int r; |
| 3716 |
|
| 3717 |
assert_return(m, -EINVAL); |
| 3718 |
assert_return(m->sealed, -EPERM); |
| 3719 |
assert_return(bus_type_is_trivial(type), -EINVAL); |
| 3720 |
assert_return(ret_ptr, -EINVAL); |
| 3721 |
assert_return(ret_size, -EINVAL); |
| 3722 |
assert_return(!BUS_MESSAGE_NEED_BSWAP(m), -EOPNOTSUPP); |
| 3723 |
|
| 3724 |
r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, CHAR_TO_STR(type)); |
| 3725 |
if (r < 0) |
| 3726 |
return r; |
| 3727 |
if (r == 0) { |
| 3728 |
*ret_ptr = NULL; |
| 3729 |
*ret_size = 0; |
| 3730 |
return 0; |
| 3731 |
} |
| 3732 |
|
| 3733 |
c = message_get_last_container(m); |
| 3734 |
|
| 3735 |
align = bus_type_get_alignment(type); |
| 3736 |
if (align < 0) |
| 3737 |
return align; |
| 3738 |
|
| 3739 |
sz = BUS_MESSAGE_BSWAP32(m, *c->array_size); |
| 3740 |
|
| 3741 |
if (sz == 0) |
| 3742 |
/* Zero length array, let's return some aligned |
| 3743 |
* pointer that is not NULL */ |
| 3744 |
p = (uint8_t*) align; |
| 3745 |
else { |
| 3746 |
r = message_peek_body(m, &m->rindex, align, sz, &p); |
| 3747 |
if (r < 0) |
| 3748 |
goto fail; |
| 3749 |
} |
| 3750 |
|
| 3751 |
r = sd_bus_message_exit_container(m); |
| 3752 |
if (r < 0) |
| 3753 |
goto fail; |
| 3754 |
|
| 3755 |
*ret_ptr = (const void*) p; |
| 3756 |
*ret_size = sz; |
| 3757 |
|
| 3758 |
return 1; |
| 3759 |
|
| 3760 |
fail: |
| 3761 |
message_quit_container(m); |
| 3762 |
return r; |
| 3763 |
} |
| 3764 |
|
| 3765 |
static int message_peek_fields( |
| 3766 |
sd_bus_message *m, |
| 3767 |
size_t *rindex, |
| 3768 |
size_t align, |
| 3769 |
size_t nbytes, |
| 3770 |
void **ret) { |
| 3771 |
|
| 3772 |
size_t start, end; |
| 3773 |
|
| 3774 |
assert(m); |
| 3775 |
assert(rindex); |
| 3776 |
assert(align > 0); |
| 3777 |
|
| 3778 |
start = ALIGN_TO(*rindex, align); |
| 3779 |
if (start > m->fields_size) |
| 3780 |
return -EBADMSG; |
| 3781 |
|
| 3782 |
/* Avoid overflow below */ |
| 3783 |
if (nbytes > SIZE_MAX - start) |
| 3784 |
return -EBADMSG; |
| 3785 |
|
| 3786 |
end = start + nbytes; |
| 3787 |
if (end > m->fields_size) |
| 3788 |
return -EBADMSG; |
| 3789 |
|
| 3790 |
/* Verify that padding is 0 */ |
| 3791 |
uint8_t *p = BUS_MESSAGE_FIELDS(m); |
| 3792 |
for (size_t k = *rindex; k < start; k++) |
| 3793 |
if (p[k] != 0) |
| 3794 |
return -EBADMSG; |
| 3795 |
|
| 3796 |
if (ret) |
| 3797 |
*ret = p + start; |
| 3798 |
|
| 3799 |
*rindex = end; |
| 3800 |
return 1; |
| 3801 |
} |
| 3802 |
|
| 3803 |
static int message_peek_field_uint32( |
| 3804 |
sd_bus_message *m, |
| 3805 |
size_t *ri, |
| 3806 |
size_t item_size, |
| 3807 |
uint32_t *ret) { |
| 3808 |
|
| 3809 |
int r; |
| 3810 |
void *q; |
| 3811 |
|
| 3812 |
assert(m); |
| 3813 |
assert(ri); |
| 3814 |
|
| 3815 |
r = message_peek_fields(m, ri, 4, 4, &q); |
| 3816 |
if (r < 0) |
| 3817 |
return r; |
| 3818 |
|
| 3819 |
if (ret) |
| 3820 |
*ret = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q); |
| 3821 |
|
| 3822 |
return 0; |
| 3823 |
} |
| 3824 |
|
| 3825 |
static int message_peek_field_string( |
| 3826 |
sd_bus_message *m, |
| 3827 |
bool (*validate)(const char *p), |
| 3828 |
size_t *ri, |
| 3829 |
size_t item_size, |
| 3830 |
const char **ret) { |
| 3831 |
|
| 3832 |
uint32_t l; |
| 3833 |
int r; |
| 3834 |
void *q; |
| 3835 |
|
| 3836 |
assert(m); |
| 3837 |
assert(ri); |
| 3838 |
|
| 3839 |
r = message_peek_field_uint32(m, ri, 4, &l); |
| 3840 |
if (r < 0) |
| 3841 |
return r; |
| 3842 |
|
| 3843 |
if (l == UINT32_MAX) |
| 3844 |
/* avoid overflow right below */ |
| 3845 |
return -EBADMSG; |
| 3846 |
|
| 3847 |
r = message_peek_fields(m, ri, 1, l+1, &q); |
| 3848 |
if (r < 0) |
| 3849 |
return r; |
| 3850 |
|
| 3851 |
if (validate) { |
| 3852 |
if (!validate_nul(q, l)) |
| 3853 |
return -EBADMSG; |
| 3854 |
|
| 3855 |
if (!validate(q)) |
| 3856 |
return -EBADMSG; |
| 3857 |
} else { |
| 3858 |
if (!validate_string(q, l)) |
| 3859 |
return -EBADMSG; |
| 3860 |
} |
| 3861 |
|
| 3862 |
if (ret) |
| 3863 |
*ret = q; |
| 3864 |
|
| 3865 |
return 0; |
| 3866 |
} |
| 3867 |
|
| 3868 |
static int message_peek_field_signature( |
| 3869 |
sd_bus_message *m, |
| 3870 |
size_t *ri, |
| 3871 |
size_t item_size, |
| 3872 |
const char **ret) { |
| 3873 |
|
| 3874 |
size_t l; |
| 3875 |
int r; |
| 3876 |
void *q; |
| 3877 |
|
| 3878 |
assert(m); |
| 3879 |
assert(ri); |
| 3880 |
|
| 3881 |
r = message_peek_fields(m, ri, 1, 1, &q); |
| 3882 |
if (r < 0) |
| 3883 |
return r; |
| 3884 |
|
| 3885 |
l = *(uint8_t*) q; |
| 3886 |
if (l == UINT8_MAX) |
| 3887 |
/* avoid overflow right below */ |
| 3888 |
return -EBADMSG; |
| 3889 |
|
| 3890 |
r = message_peek_fields(m, ri, 1, l+1, &q); |
| 3891 |
if (r < 0) |
| 3892 |
return r; |
| 3893 |
|
| 3894 |
if (!validate_signature(q, l)) |
| 3895 |
return -EBADMSG; |
| 3896 |
|
| 3897 |
if (ret) |
| 3898 |
*ret = q; |
| 3899 |
|
| 3900 |
return 0; |
| 3901 |
} |
| 3902 |
|
| 3903 |
static int message_skip_fields( |
| 3904 |
sd_bus_message *m, |
| 3905 |
size_t *ri, |
| 3906 |
uint32_t array_size, |
| 3907 |
const char **signature, |
| 3908 |
unsigned depth) { |
| 3909 |
|
| 3910 |
size_t original_index; |
| 3911 |
int r; |
| 3912 |
|
| 3913 |
assert(m); |
| 3914 |
assert(ri); |
| 3915 |
assert(signature); |
| 3916 |
|
| 3917 |
if (depth >= BUS_CONTAINER_DEPTH) |
| 3918 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "Maximum container nesting depth reached, refusing."); |
| 3919 |
|
| 3920 |
original_index = *ri; |
| 3921 |
|
| 3922 |
for (;;) { |
| 3923 |
char t; |
| 3924 |
size_t l; |
| 3925 |
|
| 3926 |
if (array_size != UINT32_MAX && |
| 3927 |
array_size <= *ri - original_index) |
| 3928 |
return 0; |
| 3929 |
|
| 3930 |
t = **signature; |
| 3931 |
if (!t) |
| 3932 |
return 0; |
| 3933 |
|
| 3934 |
if (t == SD_BUS_TYPE_STRING) { |
| 3935 |
|
| 3936 |
r = message_peek_field_string(m, NULL, ri, 0, NULL); |
| 3937 |
if (r < 0) |
| 3938 |
return r; |
| 3939 |
|
| 3940 |
(*signature)++; |
| 3941 |
|
| 3942 |
} else if (t == SD_BUS_TYPE_OBJECT_PATH) { |
| 3943 |
|
| 3944 |
r = message_peek_field_string(m, object_path_is_valid, ri, 0, NULL); |
| 3945 |
if (r < 0) |
| 3946 |
return r; |
| 3947 |
|
| 3948 |
(*signature)++; |
| 3949 |
|
| 3950 |
} else if (t == SD_BUS_TYPE_SIGNATURE) { |
| 3951 |
|
| 3952 |
r = message_peek_field_signature(m, ri, 0, NULL); |
| 3953 |
if (r < 0) |
| 3954 |
return r; |
| 3955 |
|
| 3956 |
(*signature)++; |
| 3957 |
|
| 3958 |
} else if (bus_type_is_basic(t)) { |
| 3959 |
ssize_t align, k; |
| 3960 |
|
| 3961 |
align = bus_type_get_alignment(t); |
| 3962 |
k = bus_type_get_size(t); |
| 3963 |
assert(align > 0 && k > 0); |
| 3964 |
|
| 3965 |
r = message_peek_fields(m, ri, align, k, NULL); |
| 3966 |
if (r < 0) |
| 3967 |
return r; |
| 3968 |
|
| 3969 |
(*signature)++; |
| 3970 |
|
| 3971 |
} else if (t == SD_BUS_TYPE_ARRAY) { |
| 3972 |
|
| 3973 |
r = signature_element_length(*signature + 1, &l); |
| 3974 |
if (r < 0) |
| 3975 |
return r; |
| 3976 |
|
| 3977 |
assert(l >= 1); |
| 3978 |
{ |
| 3979 |
char sig[l + 1], *s = sig; |
| 3980 |
uint32_t nas; |
| 3981 |
int alignment; |
| 3982 |
|
| 3983 |
strncpy(sig, *signature + 1, l); |
| 3984 |
sig[l] = '\0'; |
| 3985 |
|
| 3986 |
alignment = bus_type_get_alignment(sig[0]); |
| 3987 |
if (alignment < 0) |
| 3988 |
return alignment; |
| 3989 |
|
| 3990 |
r = message_peek_field_uint32(m, ri, 0, &nas); |
| 3991 |
if (r < 0) |
| 3992 |
return r; |
| 3993 |
if (nas > BUS_ARRAY_MAX_SIZE) |
| 3994 |
return -EBADMSG; |
| 3995 |
|
| 3996 |
r = message_peek_fields(m, ri, alignment, 0, NULL); |
| 3997 |
if (r < 0) |
| 3998 |
return r; |
| 3999 |
|
| 4000 |
r = message_skip_fields(m, ri, nas, (const char**) &s, depth + 1); |
| 4001 |
if (r < 0) |
| 4002 |
return r; |
| 4003 |
} |
| 4004 |
|
| 4005 |
(*signature) += 1 + l; |
| 4006 |
|
| 4007 |
} else if (t == SD_BUS_TYPE_VARIANT) { |
| 4008 |
const char *s; |
| 4009 |
|
| 4010 |
r = message_peek_field_signature(m, ri, 0, &s); |
| 4011 |
if (r < 0) |
| 4012 |
return r; |
| 4013 |
|
| 4014 |
r = message_skip_fields(m, ri, UINT32_MAX, &s, depth + 1); |
| 4015 |
if (r < 0) |
| 4016 |
return r; |
| 4017 |
|
| 4018 |
(*signature)++; |
| 4019 |
|
| 4020 |
} else if (IN_SET(t, SD_BUS_TYPE_STRUCT, SD_BUS_TYPE_DICT_ENTRY)) { |
| 4021 |
|
| 4022 |
r = signature_element_length(*signature, &l); |
| 4023 |
if (r < 0) |
| 4024 |
return r; |
| 4025 |
|
| 4026 |
assert(l >= 2); |
| 4027 |
{ |
| 4028 |
char sig[l + 1], *s = sig; |
| 4029 |
strncpy(sig, *signature + 1, l); |
| 4030 |
sig[l] = '\0'; |
| 4031 |
|
| 4032 |
r = message_skip_fields(m, ri, UINT32_MAX, (const char**) &s, depth + 1); |
| 4033 |
if (r < 0) |
| 4034 |
return r; |
| 4035 |
} |
| 4036 |
|
| 4037 |
*signature += l; |
| 4038 |
} else |
| 4039 |
return -EBADMSG; |
| 4040 |
} |
| 4041 |
} |
| 4042 |
|
| 4043 |
static int message_parse_fields(sd_bus_message *m, bool got_ctrunc) { |
| 4044 |
uint32_t n_unix_fds_declared = 0; |
| 4045 |
bool unix_fds_set = false; |
| 4046 |
int r; |
| 4047 |
|
| 4048 |
assert(m); |
| 4049 |
|
| 4050 |
m->user_body_size = m->body_size; |
| 4051 |
|
| 4052 |
for (size_t ri = 0; ri < m->fields_size; ) { |
| 4053 |
const char *signature; |
| 4054 |
uint64_t field_type; |
| 4055 |
size_t item_size = SIZE_MAX; |
| 4056 |
uint8_t *u8; |
| 4057 |
|
| 4058 |
r = message_peek_fields(m, &ri, 8, 1, (void**) &u8); |
| 4059 |
if (r < 0) |
| 4060 |
return r; |
| 4061 |
|
| 4062 |
field_type = *u8; |
| 4063 |
|
| 4064 |
r = message_peek_field_signature(m, &ri, 0, &signature); |
| 4065 |
if (r < 0) |
| 4066 |
return r; |
| 4067 |
|
| 4068 |
switch (field_type) { |
| 4069 |
|
| 4070 |
case _BUS_MESSAGE_HEADER_INVALID: |
| 4071 |
return -EBADMSG; |
| 4072 |
|
| 4073 |
case BUS_MESSAGE_HEADER_PATH: |
| 4074 |
|
| 4075 |
if (m->path) |
| 4076 |
return -EBADMSG; |
| 4077 |
|
| 4078 |
if (!streq(signature, "o")) |
| 4079 |
return -EBADMSG; |
| 4080 |
|
| 4081 |
r = message_peek_field_string(m, object_path_is_valid, &ri, item_size, &m->path); |
| 4082 |
break; |
| 4083 |
|
| 4084 |
case BUS_MESSAGE_HEADER_INTERFACE: |
| 4085 |
|
| 4086 |
if (m->interface) |
| 4087 |
return -EBADMSG; |
| 4088 |
|
| 4089 |
if (!streq(signature, "s")) |
| 4090 |
return -EBADMSG; |
| 4091 |
|
| 4092 |
r = message_peek_field_string(m, interface_name_is_valid, &ri, item_size, &m->interface); |
| 4093 |
break; |
| 4094 |
|
| 4095 |
case BUS_MESSAGE_HEADER_MEMBER: |
| 4096 |
|
| 4097 |
if (m->member) |
| 4098 |
return -EBADMSG; |
| 4099 |
|
| 4100 |
if (!streq(signature, "s")) |
| 4101 |
return -EBADMSG; |
| 4102 |
|
| 4103 |
r = message_peek_field_string(m, member_name_is_valid, &ri, item_size, &m->member); |
| 4104 |
break; |
| 4105 |
|
| 4106 |
case BUS_MESSAGE_HEADER_ERROR_NAME: |
| 4107 |
|
| 4108 |
if (m->error.name) |
| 4109 |
return -EBADMSG; |
| 4110 |
|
| 4111 |
if (!streq(signature, "s")) |
| 4112 |
return -EBADMSG; |
| 4113 |
|
| 4114 |
r = message_peek_field_string(m, error_name_is_valid, &ri, item_size, &m->error.name); |
| 4115 |
if (r >= 0) |
| 4116 |
m->error._need_free = -1; |
| 4117 |
|
| 4118 |
break; |
| 4119 |
|
| 4120 |
case BUS_MESSAGE_HEADER_DESTINATION: |
| 4121 |
|
| 4122 |
if (m->destination) |
| 4123 |
return -EBADMSG; |
| 4124 |
|
| 4125 |
if (!streq(signature, "s")) |
| 4126 |
return -EBADMSG; |
| 4127 |
|
| 4128 |
r = message_peek_field_string(m, service_name_is_valid, &ri, item_size, &m->destination); |
| 4129 |
break; |
| 4130 |
|
| 4131 |
case BUS_MESSAGE_HEADER_SENDER: |
| 4132 |
|
| 4133 |
if (m->sender) |
| 4134 |
return -EBADMSG; |
| 4135 |
|
| 4136 |
if (!streq(signature, "s")) |
| 4137 |
return -EBADMSG; |
| 4138 |
|
| 4139 |
r = message_peek_field_string(m, service_name_is_valid, &ri, item_size, &m->sender); |
| 4140 |
|
| 4141 |
if (r >= 0 && m->sender[0] == ':' && m->bus->bus_client) { |
| 4142 |
m->creds.unique_name = (char*) m->sender; |
| 4143 |
m->creds.mask |= SD_BUS_CREDS_UNIQUE_NAME & m->bus->creds_mask; |
| 4144 |
} |
| 4145 |
|
| 4146 |
break; |
| 4147 |
|
| 4148 |
case BUS_MESSAGE_HEADER_SIGNATURE: { |
| 4149 |
const char *s; |
| 4150 |
char *c; |
| 4151 |
|
| 4152 |
if (m->root_container.signature) |
| 4153 |
return -EBADMSG; |
| 4154 |
|
| 4155 |
if (!streq(signature, "g")) |
| 4156 |
return -EBADMSG; |
| 4157 |
|
| 4158 |
r = message_peek_field_signature(m, &ri, item_size, &s); |
| 4159 |
if (r < 0) |
| 4160 |
return r; |
| 4161 |
|
| 4162 |
c = strdup(s); |
| 4163 |
if (!c) |
| 4164 |
return -ENOMEM; |
| 4165 |
|
| 4166 |
free_and_replace(m->root_container.signature, c); |
| 4167 |
break; |
| 4168 |
} |
| 4169 |
|
| 4170 |
case BUS_MESSAGE_HEADER_REPLY_SERIAL: { |
| 4171 |
uint32_t serial; |
| 4172 |
|
| 4173 |
if (m->reply_cookie != 0) |
| 4174 |
return -EBADMSG; |
| 4175 |
|
| 4176 |
if (!streq(signature, "u")) |
| 4177 |
return -EBADMSG; |
| 4178 |
|
| 4179 |
r = message_peek_field_uint32(m, &ri, item_size, &serial); |
| 4180 |
if (r < 0) |
| 4181 |
return r; |
| 4182 |
|
| 4183 |
m->reply_cookie = serial; |
| 4184 |
|
| 4185 |
if (m->reply_cookie == 0) |
| 4186 |
return -EBADMSG; |
| 4187 |
|
| 4188 |
break; |
| 4189 |
} |
| 4190 |
case BUS_MESSAGE_HEADER_UNIX_FDS: |
| 4191 |
if (unix_fds_set) |
| 4192 |
return -EBADMSG; |
| 4193 |
|
| 4194 |
if (!streq(signature, "u")) |
| 4195 |
return -EBADMSG; |
| 4196 |
|
| 4197 |
r = message_peek_field_uint32(m, &ri, item_size, &n_unix_fds_declared); |
| 4198 |
if (r < 0) |
| 4199 |
return -EBADMSG; |
| 4200 |
|
| 4201 |
unix_fds_set = true; |
| 4202 |
break; |
| 4203 |
|
| 4204 |
default: |
| 4205 |
r = message_skip_fields(m, &ri, UINT32_MAX, &signature, 0); |
| 4206 |
} |
| 4207 |
if (r < 0) |
| 4208 |
return r; |
| 4209 |
} |
| 4210 |
|
| 4211 |
/* Validate that the number of fds we actually received via SCM_RIGHTS matches (or is compatible |
| 4212 |
* with) the number declared in the message header. |
| 4213 |
* |
| 4214 |
* Normally these must match exactly. However, when MSG_CTRUNC was set during recvmsg(), the kernel |
| 4215 |
* might have truncated the fd array (e.g., due to LSM denials blocking fd passing). In that case, |
| 4216 |
* we also accept fewer fds than declared. Any attempt to actually use a truncated fd will fail later |
| 4217 |
* when sd_bus_message_read_basic() finds the fd index out of range. Too many fds is always wrong. */ |
| 4218 |
if (m->n_fds > n_unix_fds_declared) |
| 4219 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 4220 |
"Received a bus message with too many fds: %" PRIu32 " received vs. %" PRIu32 " declared.", |
| 4221 |
m->n_fds, n_unix_fds_declared); |
| 4222 |
|
| 4223 |
if (m->n_fds < n_unix_fds_declared) { |
| 4224 |
if (!got_ctrunc) |
| 4225 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 4226 |
"Received a bus message with too few fds: %" PRIu32 " received vs. %" PRIu32 " declared.", |
| 4227 |
m->n_fds, n_unix_fds_declared); |
| 4228 |
|
| 4229 |
log_debug("Received a bus message with MSG_CTRUNC set with %" PRIu32 " fds received vs. %" PRIu32 " declared.", |
| 4230 |
m->n_fds, n_unix_fds_declared); |
| 4231 |
|
| 4232 |
} else if (got_ctrunc) |
| 4233 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 4234 |
"Received a bus message with truncated control data, refusing."); |
| 4235 |
|
| 4236 |
switch (m->header->type) { |
| 4237 |
|
| 4238 |
case SD_BUS_MESSAGE_SIGNAL: |
| 4239 |
if (!m->path || !m->interface || !m->member) |
| 4240 |
return -EBADMSG; |
| 4241 |
|
| 4242 |
if (m->reply_cookie != 0) |
| 4243 |
return -EBADMSG; |
| 4244 |
|
| 4245 |
break; |
| 4246 |
|
| 4247 |
case SD_BUS_MESSAGE_METHOD_CALL: |
| 4248 |
|
| 4249 |
if (!m->path || !m->member) |
| 4250 |
return -EBADMSG; |
| 4251 |
|
| 4252 |
if (m->reply_cookie != 0) |
| 4253 |
return -EBADMSG; |
| 4254 |
|
| 4255 |
break; |
| 4256 |
|
| 4257 |
case SD_BUS_MESSAGE_METHOD_RETURN: |
| 4258 |
|
| 4259 |
if (m->reply_cookie == 0) |
| 4260 |
return -EBADMSG; |
| 4261 |
break; |
| 4262 |
|
| 4263 |
case SD_BUS_MESSAGE_METHOD_ERROR: |
| 4264 |
|
| 4265 |
if (m->reply_cookie == 0 || !m->error.name) |
| 4266 |
return -EBADMSG; |
| 4267 |
break; |
| 4268 |
} |
| 4269 |
|
| 4270 |
/* Refuse non-local messages that claim they are local */ |
| 4271 |
if (streq_ptr(m->path, "/org/freedesktop/DBus/Local")) |
| 4272 |
return -EBADMSG; |
| 4273 |
if (streq_ptr(m->interface, "org.freedesktop.DBus.Local")) |
| 4274 |
return -EBADMSG; |
| 4275 |
if (streq_ptr(m->sender, "org.freedesktop.DBus.Local")) |
| 4276 |
return -EBADMSG; |
| 4277 |
|
| 4278 |
m->root_container.end = m->user_body_size; |
| 4279 |
|
| 4280 |
/* Try to read the error message, but if we can't it's a non-issue */ |
| 4281 |
if (m->header->type == SD_BUS_MESSAGE_METHOD_ERROR) |
| 4282 |
(void) sd_bus_message_read(m, "s", &m->error.message); |
| 4283 |
|
| 4284 |
return 0; |
| 4285 |
} |
| 4286 |
|
| 4287 |
_public_ int sd_bus_message_set_destination(sd_bus_message *m, const char *destination) { |
| 4288 |
assert_return(m, -EINVAL); |
| 4289 |
assert_return(destination, -EINVAL); |
| 4290 |
assert_return(service_name_is_valid(destination), -EINVAL); |
| 4291 |
assert_return(!m->sealed, -EPERM); |
| 4292 |
assert_return(!m->destination, -EEXIST); |
| 4293 |
|
| 4294 |
return message_append_field_string(m, BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &m->destination); |
| 4295 |
} |
| 4296 |
|
| 4297 |
_public_ int sd_bus_message_set_sender(sd_bus_message *m, const char *sender) { |
| 4298 |
assert_return(m, -EINVAL); |
| 4299 |
assert_return(sender, -EINVAL); |
| 4300 |
assert_return(service_name_is_valid(sender), -EINVAL); |
| 4301 |
assert_return(!m->sealed, -EPERM); |
| 4302 |
assert_return(!m->sender, -EEXIST); |
| 4303 |
|
| 4304 |
return message_append_field_string(m, BUS_MESSAGE_HEADER_SENDER, SD_BUS_TYPE_STRING, sender, &m->sender); |
| 4305 |
} |
| 4306 |
|
| 4307 |
int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz) { |
| 4308 |
size_t total; |
| 4309 |
void *p, *e; |
| 4310 |
size_t i; |
| 4311 |
BusMessageBodyPart *part; |
| 4312 |
|
| 4313 |
assert(m); |
| 4314 |
assert(buffer); |
| 4315 |
assert(sz); |
| 4316 |
|
| 4317 |
total = BUS_MESSAGE_SIZE(m); |
| 4318 |
|
| 4319 |
p = malloc(total); |
| 4320 |
if (!p) |
| 4321 |
return -ENOMEM; |
| 4322 |
|
| 4323 |
e = mempcpy(p, m->header, BUS_MESSAGE_BODY_BEGIN(m)); |
| 4324 |
MESSAGE_FOREACH_PART(part, i, m) |
| 4325 |
e = mempcpy(e, part->data, part->size); |
| 4326 |
|
| 4327 |
assert(total == (size_t) ((uint8_t*) e - (uint8_t*) p)); |
| 4328 |
|
| 4329 |
*buffer = p; |
| 4330 |
*sz = total; |
| 4331 |
|
| 4332 |
return 0; |
| 4333 |
} |
| 4334 |
|
| 4335 |
_public_ int sd_bus_message_read_strv_extend(sd_bus_message *m, char ***l) { |
| 4336 |
char type; |
| 4337 |
const char *contents, *s; |
| 4338 |
size_t n; |
| 4339 |
int r; |
| 4340 |
|
| 4341 |
assert(m); |
| 4342 |
assert(l); |
| 4343 |
|
| 4344 |
r = sd_bus_message_peek_type(m, &type, &contents); |
| 4345 |
if (r < 0) |
| 4346 |
return r; |
| 4347 |
|
| 4348 |
if (type != SD_BUS_TYPE_ARRAY || !STR_IN_SET(contents, "s", "o", "g")) |
| 4349 |
return -ENXIO; |
| 4350 |
|
| 4351 |
r = sd_bus_message_enter_container(m, 'a', NULL); |
| 4352 |
if (r <= 0) |
| 4353 |
return r; |
| 4354 |
|
| 4355 |
n = strv_length(*l); |
| 4356 |
/* sd_bus_message_read_basic() does content validation for us. */ |
| 4357 |
while ((r = sd_bus_message_read_basic(m, *contents, &s)) > 0) { |
| 4358 |
r = strv_extend_with_size(l, &n, s); |
| 4359 |
if (r < 0) |
| 4360 |
return r; |
| 4361 |
} |
| 4362 |
if (r < 0) |
| 4363 |
return r; |
| 4364 |
|
| 4365 |
r = sd_bus_message_exit_container(m); |
| 4366 |
if (r < 0) |
| 4367 |
return r; |
| 4368 |
|
| 4369 |
return 1; |
| 4370 |
} |
| 4371 |
|
| 4372 |
_public_ int sd_bus_message_read_strv(sd_bus_message *m, char ***ret) { |
| 4373 |
_cleanup_strv_free_ char **strv = NULL; |
| 4374 |
int r; |
| 4375 |
|
| 4376 |
assert_return(m, -EINVAL); |
| 4377 |
assert_return(m->sealed, -EPERM); |
| 4378 |
assert_return(ret, -EINVAL); |
| 4379 |
|
| 4380 |
r = sd_bus_message_read_strv_extend(m, &strv); |
| 4381 |
if (r <= 0) |
| 4382 |
return r; |
| 4383 |
|
| 4384 |
*ret = TAKE_PTR(strv); |
| 4385 |
return 1; |
| 4386 |
} |
| 4387 |
|
| 4388 |
static int bus_message_get_arg_skip( |
| 4389 |
sd_bus_message *m, |
| 4390 |
unsigned i, |
| 4391 |
char *_type, |
| 4392 |
const char **_contents) { |
| 4393 |
|
| 4394 |
unsigned j; |
| 4395 |
int r; |
| 4396 |
|
| 4397 |
r = sd_bus_message_rewind(m, true); |
| 4398 |
if (r < 0) |
| 4399 |
return r; |
| 4400 |
|
| 4401 |
for (j = 0;; j++) { |
| 4402 |
const char *contents; |
| 4403 |
char type; |
| 4404 |
|
| 4405 |
r = sd_bus_message_peek_type(m, &type, &contents); |
| 4406 |
if (r < 0) |
| 4407 |
return r; |
| 4408 |
if (r == 0) |
| 4409 |
return -ENXIO; |
| 4410 |
|
| 4411 |
/* Don't match against arguments after the first one we don't understand */ |
| 4412 |
if (!IN_SET(type, SD_BUS_TYPE_STRING, SD_BUS_TYPE_OBJECT_PATH, SD_BUS_TYPE_SIGNATURE) && |
| 4413 |
!(type == SD_BUS_TYPE_ARRAY && STR_IN_SET(contents, "s", "o", "g"))) |
| 4414 |
return -ENXIO; |
| 4415 |
|
| 4416 |
if (j >= i) { |
| 4417 |
if (_contents) |
| 4418 |
*_contents = contents; |
| 4419 |
if (_type) |
| 4420 |
*_type = type; |
| 4421 |
return 0; |
| 4422 |
} |
| 4423 |
|
| 4424 |
r = sd_bus_message_skip(m, NULL); |
| 4425 |
if (r < 0) |
| 4426 |
return r; |
| 4427 |
} |
| 4428 |
} |
| 4429 |
|
| 4430 |
int bus_message_get_arg(sd_bus_message *m, unsigned i, const char **str) { |
| 4431 |
char type; |
| 4432 |
int r; |
| 4433 |
|
| 4434 |
assert(m); |
| 4435 |
assert(str); |
| 4436 |
|
| 4437 |
r = bus_message_get_arg_skip(m, i, &type, NULL); |
| 4438 |
if (r < 0) |
| 4439 |
return r; |
| 4440 |
|
| 4441 |
if (!IN_SET(type, SD_BUS_TYPE_STRING, SD_BUS_TYPE_OBJECT_PATH, SD_BUS_TYPE_SIGNATURE)) |
| 4442 |
return -ENXIO; |
| 4443 |
|
| 4444 |
return sd_bus_message_read_basic(m, type, str); |
| 4445 |
} |
| 4446 |
|
| 4447 |
int bus_message_get_arg_strv(sd_bus_message *m, unsigned i, char ***strv) { |
| 4448 |
const char *contents; |
| 4449 |
char type; |
| 4450 |
int r; |
| 4451 |
|
| 4452 |
assert(m); |
| 4453 |
assert(strv); |
| 4454 |
|
| 4455 |
r = bus_message_get_arg_skip(m, i, &type, &contents); |
| 4456 |
if (r < 0) |
| 4457 |
return r; |
| 4458 |
|
| 4459 |
if (type != SD_BUS_TYPE_ARRAY) |
| 4460 |
return -ENXIO; |
| 4461 |
if (!STR_IN_SET(contents, "s", "o", "g")) |
| 4462 |
return -ENXIO; |
| 4463 |
|
| 4464 |
return sd_bus_message_read_strv(m, strv); |
| 4465 |
} |
| 4466 |
|
| 4467 |
_public_ int sd_bus_message_get_errno(sd_bus_message *m) { |
| 4468 |
assert_return(m, EINVAL); |
| 4469 |
|
| 4470 |
if (m->header->type != SD_BUS_MESSAGE_METHOD_ERROR) |
| 4471 |
return 0; |
| 4472 |
|
| 4473 |
return sd_bus_error_get_errno(&m->error); |
| 4474 |
} |
| 4475 |
|
| 4476 |
_public_ const char* sd_bus_message_get_signature(sd_bus_message *m, int complete) { |
| 4477 |
BusMessageContainer *c; |
| 4478 |
|
| 4479 |
assert_return(m, NULL); |
| 4480 |
|
| 4481 |
c = complete ? &m->root_container : message_get_last_container(m); |
| 4482 |
return strempty(c->signature); |
| 4483 |
} |
| 4484 |
|
| 4485 |
_public_ int sd_bus_message_is_empty(sd_bus_message *m) { |
| 4486 |
assert_return(m, -EINVAL); |
| 4487 |
|
| 4488 |
return isempty(m->root_container.signature); |
| 4489 |
} |
| 4490 |
|
| 4491 |
_public_ int sd_bus_message_has_signature(sd_bus_message *m, const char *signature) { |
| 4492 |
assert_return(m, -EINVAL); |
| 4493 |
|
| 4494 |
return streq(strempty(m->root_container.signature), strempty(signature)); |
| 4495 |
} |
| 4496 |
|
| 4497 |
_public_ int sd_bus_message_copy(sd_bus_message *m, sd_bus_message *source, int all) { |
| 4498 |
bool done_something = false; |
| 4499 |
int r; |
| 4500 |
|
| 4501 |
assert_return(m, -EINVAL); |
| 4502 |
assert_return(source, -EINVAL); |
| 4503 |
assert_return(!m->sealed, -EPERM); |
| 4504 |
assert_return(source->sealed, -EPERM); |
| 4505 |
|
| 4506 |
do { |
| 4507 |
const char *contents; |
| 4508 |
char type; |
| 4509 |
union { |
| 4510 |
uint8_t u8; |
| 4511 |
uint16_t u16; |
| 4512 |
int16_t s16; |
| 4513 |
uint32_t u32; |
| 4514 |
int32_t s32; |
| 4515 |
uint64_t u64; |
| 4516 |
int64_t s64; |
| 4517 |
double d64; |
| 4518 |
const char *string; |
| 4519 |
int i; |
| 4520 |
} basic; |
| 4521 |
|
| 4522 |
r = sd_bus_message_peek_type(source, &type, &contents); |
| 4523 |
if (r < 0) |
| 4524 |
return r; |
| 4525 |
if (r == 0) |
| 4526 |
break; |
| 4527 |
|
| 4528 |
done_something = true; |
| 4529 |
|
| 4530 |
if (bus_type_is_container(type) > 0) { |
| 4531 |
|
| 4532 |
r = sd_bus_message_enter_container(source, type, contents); |
| 4533 |
if (r < 0) |
| 4534 |
return r; |
| 4535 |
|
| 4536 |
r = sd_bus_message_open_container(m, type, contents); |
| 4537 |
if (r < 0) |
| 4538 |
return r; |
| 4539 |
|
| 4540 |
r = sd_bus_message_copy(m, source, true); |
| 4541 |
if (r < 0) |
| 4542 |
return r; |
| 4543 |
|
| 4544 |
r = sd_bus_message_close_container(m); |
| 4545 |
if (r < 0) |
| 4546 |
return r; |
| 4547 |
|
| 4548 |
r = sd_bus_message_exit_container(source); |
| 4549 |
if (r < 0) |
| 4550 |
return r; |
| 4551 |
|
| 4552 |
continue; |
| 4553 |
} |
| 4554 |
|
| 4555 |
r = sd_bus_message_read_basic(source, type, &basic); |
| 4556 |
if (r < 0) |
| 4557 |
return r; |
| 4558 |
|
| 4559 |
assert(r > 0); |
| 4560 |
|
| 4561 |
if (IN_SET(type, SD_BUS_TYPE_OBJECT_PATH, SD_BUS_TYPE_SIGNATURE, SD_BUS_TYPE_STRING)) |
| 4562 |
r = sd_bus_message_append_basic(m, type, basic.string); |
| 4563 |
else |
| 4564 |
r = sd_bus_message_append_basic(m, type, &basic); |
| 4565 |
|
| 4566 |
if (r < 0) |
| 4567 |
return r; |
| 4568 |
|
| 4569 |
} while (all); |
| 4570 |
|
| 4571 |
return done_something; |
| 4572 |
} |
| 4573 |
|
| 4574 |
_public_ int sd_bus_message_verify_type(sd_bus_message *m, char type, const char *contents) { |
| 4575 |
const char *c; |
| 4576 |
char t; |
| 4577 |
int r; |
| 4578 |
|
| 4579 |
assert_return(m, -EINVAL); |
| 4580 |
assert_return(m->sealed, -EPERM); |
| 4581 |
assert_return(!type || bus_type_is_valid(type), -EINVAL); |
| 4582 |
assert_return(!contents || signature_is_valid(contents, true), -EINVAL); |
| 4583 |
assert_return(type || contents, -EINVAL); |
| 4584 |
assert_return(!contents || !type || bus_type_is_container(type), -EINVAL); |
| 4585 |
|
| 4586 |
r = sd_bus_message_peek_type(m, &t, &c); |
| 4587 |
if (r <= 0) |
| 4588 |
return r; |
| 4589 |
|
| 4590 |
if (type != 0 && type != t) |
| 4591 |
return 0; |
| 4592 |
|
| 4593 |
if (contents && !streq_ptr(contents, c)) |
| 4594 |
return 0; |
| 4595 |
|
| 4596 |
return 1; |
| 4597 |
} |
| 4598 |
|
| 4599 |
_public_ sd_bus* sd_bus_message_get_bus(sd_bus_message *m) { |
| 4600 |
assert_return(m, NULL); |
| 4601 |
|
| 4602 |
return m->bus; |
| 4603 |
} |
| 4604 |
|
| 4605 |
int bus_message_remarshal(sd_bus *bus, sd_bus_message **m) { |
| 4606 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *n = NULL; |
| 4607 |
usec_t timeout; |
| 4608 |
int r; |
| 4609 |
|
| 4610 |
assert(bus); |
| 4611 |
assert(m); |
| 4612 |
assert(*m); |
| 4613 |
|
| 4614 |
switch ((*m)->header->type) { |
| 4615 |
|
| 4616 |
case SD_BUS_MESSAGE_SIGNAL: |
| 4617 |
r = sd_bus_message_new_signal(bus, &n, (*m)->path, (*m)->interface, (*m)->member); |
| 4618 |
if (r < 0) |
| 4619 |
return r; |
| 4620 |
|
| 4621 |
break; |
| 4622 |
|
| 4623 |
case SD_BUS_MESSAGE_METHOD_CALL: |
| 4624 |
r = sd_bus_message_new_method_call(bus, &n, (*m)->destination, (*m)->path, (*m)->interface, (*m)->member); |
| 4625 |
if (r < 0) |
| 4626 |
return r; |
| 4627 |
|
| 4628 |
break; |
| 4629 |
|
| 4630 |
case SD_BUS_MESSAGE_METHOD_RETURN: |
| 4631 |
case SD_BUS_MESSAGE_METHOD_ERROR: |
| 4632 |
|
| 4633 |
r = sd_bus_message_new(bus, &n, (*m)->header->type); |
| 4634 |
if (r < 0) |
| 4635 |
return -ENOMEM; |
| 4636 |
|
| 4637 |
assert(n); |
| 4638 |
|
| 4639 |
n->reply_cookie = (*m)->reply_cookie; |
| 4640 |
|
| 4641 |
r = message_append_reply_cookie(n, n->reply_cookie); |
| 4642 |
if (r < 0) |
| 4643 |
return r; |
| 4644 |
|
| 4645 |
if ((*m)->header->type == SD_BUS_MESSAGE_METHOD_ERROR && (*m)->error.name) { |
| 4646 |
r = message_append_field_string(n, BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, (*m)->error.name, &n->error.message); |
| 4647 |
if (r < 0) |
| 4648 |
return r; |
| 4649 |
|
| 4650 |
n->error._need_free = -1; |
| 4651 |
} |
| 4652 |
|
| 4653 |
break; |
| 4654 |
|
| 4655 |
default: |
| 4656 |
return -EINVAL; |
| 4657 |
} |
| 4658 |
|
| 4659 |
if ((*m)->destination && !n->destination) { |
| 4660 |
r = message_append_field_string(n, BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, (*m)->destination, &n->destination); |
| 4661 |
if (r < 0) |
| 4662 |
return r; |
| 4663 |
} |
| 4664 |
|
| 4665 |
if ((*m)->sender && !n->sender) { |
| 4666 |
r = message_append_field_string(n, BUS_MESSAGE_HEADER_SENDER, SD_BUS_TYPE_STRING, (*m)->sender, &n->sender); |
| 4667 |
if (r < 0) |
| 4668 |
return r; |
| 4669 |
} |
| 4670 |
|
| 4671 |
n->header->flags |= (*m)->header->flags & (BUS_MESSAGE_NO_REPLY_EXPECTED|BUS_MESSAGE_NO_AUTO_START); |
| 4672 |
|
| 4673 |
r = sd_bus_message_copy(n, *m, true); |
| 4674 |
if (r < 0) |
| 4675 |
return r; |
| 4676 |
|
| 4677 |
timeout = (*m)->timeout; |
| 4678 |
if (timeout == 0 && !((*m)->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)) { |
| 4679 |
r = sd_bus_get_method_call_timeout(bus, &timeout); |
| 4680 |
if (r < 0) |
| 4681 |
return r; |
| 4682 |
} |
| 4683 |
|
| 4684 |
r = sd_bus_message_seal(n, BUS_MESSAGE_COOKIE(*m), timeout); |
| 4685 |
if (r < 0) |
| 4686 |
return r; |
| 4687 |
|
| 4688 |
sd_bus_message_unref(*m); |
| 4689 |
*m = TAKE_PTR(n); |
| 4690 |
|
| 4691 |
return 0; |
| 4692 |
} |
| 4693 |
|
| 4694 |
_public_ int sd_bus_message_get_priority(sd_bus_message *m, int64_t *ret) { |
| 4695 |
static bool warned = false; |
| 4696 |
|
| 4697 |
assert_return(m, -EINVAL); |
| 4698 |
assert_return(ret, -EINVAL); |
| 4699 |
|
| 4700 |
if (!warned) { |
| 4701 |
log_debug("sd_bus_message_get_priority() is deprecated and always returns 0."); |
| 4702 |
warned = true; |
| 4703 |
} |
| 4704 |
|
| 4705 |
*ret = 0; |
| 4706 |
return 0; |
| 4707 |
} |
| 4708 |
|
| 4709 |
_public_ int sd_bus_message_set_priority(sd_bus_message *m, int64_t priority) { |
| 4710 |
static bool warned = false; |
| 4711 |
|
| 4712 |
assert_return(m, -EINVAL); |
| 4713 |
assert_return(!m->sealed, -EPERM); |
| 4714 |
|
| 4715 |
if (!warned) { |
| 4716 |
log_debug("sd_bus_message_set_priority() is deprecated and does nothing."); |
| 4717 |
warned = true; |
| 4718 |
} |
| 4719 |
|
| 4720 |
return 0; |
| 4721 |
} |
| 4722 |
|
| 4723 |
_public_ int sd_bus_message_sensitive(sd_bus_message *m) { |
| 4724 |
assert_return(m, -EINVAL); |
| 4725 |
|
| 4726 |
m->sensitive = true; |
| 4727 |
return 0; |
| 4728 |
} |
| 4729 |
|
| 4730 |
char** bus_message_make_log_fields(sd_bus_message *m) { |
| 4731 |
_cleanup_strv_free_ char **strv = NULL; |
| 4732 |
|
| 4733 |
assert(m); |
| 4734 |
|
| 4735 |
(void) strv_extend_assignment(&strv, "DBUS_MESSAGE_TYPE", bus_message_type_to_string(m->header->type)); |
| 4736 |
(void) strv_extend_assignment(&strv, "DBUS_SENDER", sd_bus_message_get_sender(m)); |
| 4737 |
(void) strv_extend_assignment(&strv, "DBUS_DESTINATION", sd_bus_message_get_destination(m)); |
| 4738 |
(void) strv_extend_assignment(&strv, "DBUS_PATH", sd_bus_message_get_path(m)); |
| 4739 |
(void) strv_extend_assignment(&strv, "DBUS_INTERFACE", sd_bus_message_get_interface(m)); |
| 4740 |
(void) strv_extend_assignment(&strv, "DBUS_MEMBER", sd_bus_message_get_member(m)); |
| 4741 |
|
| 4742 |
(void) strv_extendf(&strv, "DBUS_MESSAGE_COOKIE=%" PRIu64, BUS_MESSAGE_COOKIE(m)); |
| 4743 |
if (m->reply_cookie != 0) |
| 4744 |
(void) strv_extendf(&strv, "DBUS_MESSAGE_REPLY_COOKIE=%" PRIu64, m->reply_cookie); |
| 4745 |
|
| 4746 |
(void) strv_extend_assignment(&strv, "DBUS_SIGNATURE", m->root_container.signature); |
| 4747 |
(void) strv_extend_assignment(&strv, "DBUS_ERROR_NAME", m->error.name); |
| 4748 |
(void) strv_extend_assignment(&strv, "DBUS_ERROR_MESSAGE", m->error.message); |
| 4749 |
|
| 4750 |
return TAKE_PTR(strv); |
| 4751 |
} |