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