1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
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.
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.
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/>.
29 #include "time-util.h"
30 #include "cgroup-util.h"
33 #include "bus-message.h"
34 #include "bus-internal.h"
36 #include "bus-signature.h"
37 #include "bus-gvariant.h"
39 static int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored);
41 static void *adjust_pointer(const void *p, void *old_base, size_t sz, void *new_base) {
46 if (old_base == new_base)
49 if ((uint8_t*) p < (uint8_t*) old_base)
52 if ((uint8_t*) p >= (uint8_t*) old_base + sz)
55 return (uint8_t*) new_base + ((uint8_t*) p - (uint8_t*) old_base);
58 static void message_free_part(sd_bus_message *m, struct bus_body_part *part) {
62 if (part->memfd >= 0) {
63 /* If we can reuse the memfd, try that. For that it
64 * can't be sealed yet. */
67 bus_kernel_push_memfd(m->bus, part->memfd, part->data, part->mapped);
70 assert_se(munmap(part->data, part->mapped) == 0);
72 close_nointr_nofail(part->memfd);
75 } else if (part->munmap_this)
76 munmap(part->data, part->mapped);
77 else if (part->free_this)
84 static void message_reset_parts(sd_bus_message *m) {
85 struct bus_body_part *part;
90 while (m->n_body_parts > 0) {
91 struct bus_body_part *next = part->next;
92 message_free_part(m, part);
99 m->cached_rindex_part = NULL;
100 m->cached_rindex_part_begin = 0;
103 static void message_reset_containers(sd_bus_message *m) {
108 for (i = 0; i < m->n_containers; i++) {
109 free(m->containers[i].signature);
110 free(m->containers[i].offsets);
114 m->containers = NULL;
117 m->root_container.index = 0;
120 static void message_free(sd_bus_message *m) {
126 message_reset_parts(m);
131 if (m->release_kdbus) {
134 off = (uint8_t *)m->kdbus - (uint8_t *)m->bus->kdbus_buffer;
135 ioctl(m->bus->input_fd, KDBUS_CMD_FREE, &off);
139 sd_bus_unref(m->bus);
142 close_many(m->fds, m->n_fds);
146 if (m->iovec != m->iovec_fixed)
149 message_reset_containers(m);
150 free(m->root_container.signature);
151 free(m->root_container.offsets);
153 free(m->peeked_signature);
155 bus_creds_done(&m->creds);
159 static void *message_extend_fields(sd_bus_message *m, size_t align, size_t sz, bool add_offset) {
161 size_t old_size, new_size, start;
168 old_size = sizeof(struct bus_header) + m->header->fields_size;
169 start = ALIGN_TO(old_size, align);
170 new_size = start + sz;
172 if (old_size == new_size)
173 return (uint8_t*) m->header + old_size;
175 if (new_size > (size_t) ((uint32_t) -1))
178 if (m->free_header) {
179 np = realloc(m->header, ALIGN8(new_size));
183 /* Initially, the header is allocated as part of of
184 * the sd_bus_message itself, let's replace it by
187 np = malloc(ALIGN8(new_size));
191 memcpy(np, m->header, sizeof(struct bus_header));
194 /* Zero out padding */
195 if (start > old_size)
196 memset((uint8_t*) np + old_size, 0, start - old_size);
200 m->header->fields_size = new_size - sizeof(struct bus_header);
202 /* Adjust quick access pointers */
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);
210 m->free_header = true;
213 if (m->n_header_offsets >= ELEMENTSOF(m->header_offsets))
216 m->header_offsets[m->n_header_offsets++] = new_size - sizeof(struct bus_header);
219 return (uint8_t*) np + start;
226 static int message_append_field_string(
238 /* dbus1 doesn't allow strings over 32bit, let's enforce this
239 * globally, to not risk convertability */
241 if (l > (size_t) (uint32_t) -1)
244 /* Signature "(yv)" where the variant contains "s" */
246 if (BUS_MESSAGE_IS_GVARIANT(m)) {
248 /* (field id byte + 7x padding, ((string + NUL) + NUL + signature string 's') */
249 p = message_extend_fields(m, 8, 1 + 7 + l + 1 + 1 + 1, true);
261 *ret = (char*) p + 8;
264 /* (field id byte + (signature length + signature 's' + NUL) + (string length + string + NUL)) */
265 p = message_extend_fields(m, 8, 4 + 4 + l + 1, false);
274 ((uint32_t*) p)[1] = l;
275 memcpy(p + 8, s, l + 1);
278 *ret = (char*) p + 8;
284 static int message_append_field_signature(
295 /* dbus1 doesn't allow signatures over 32bit, let's enforce
296 * this globally, to not risk convertability */
301 /* Signature "(yv)" where the variant contains "g" */
303 if (BUS_MESSAGE_IS_GVARIANT(m))
304 /* For gvariant the serialization is the same as for normal strings */
305 return message_append_field_string(m, h, 'g', s, ret);
307 /* (field id byte + (signature length + signature 'g' + NUL) + (string length + string + NUL)) */
308 p = message_extend_fields(m, 8, 4 + 1 + l + 1, false);
314 p[2] = SD_BUS_TYPE_SIGNATURE;
317 memcpy(p + 5, s, l + 1);
320 *ret = (const char*) p + 5;
326 static int message_append_field_uint32(sd_bus_message *m, uint8_t h, uint32_t x) {
331 if (BUS_MESSAGE_IS_GVARIANT(m)) {
332 /* (field id byte + 7x padding + ((value + NUL + signature string 'u') */
334 p = message_extend_fields(m, 8, 1 + 7 + 4 + 1 + 1, true);
340 *((uint32_t*) (p + 8)) = x;
344 /* (field id byte + (signature length + signature 'u' + NUL) + value) */
345 p = message_extend_fields(m, 8, 4 + 4, false);
351 p[2] = SD_BUS_TYPE_UINT32;
354 ((uint32_t*) p)[1] = x;
360 int bus_message_from_header(
366 const struct ucred *ucred,
369 sd_bus_message **ret) {
372 struct bus_header *h;
375 assert(buffer || length <= 0);
376 assert(fds || n_fds <= 0);
379 if (length < sizeof(struct bus_header))
383 if (h->version != 1 &&
390 if (h->type == _SD_BUS_MESSAGE_TYPE_INVALID)
393 if (h->endian != BUS_LITTLE_ENDIAN &&
394 h->endian != BUS_BIG_ENDIAN)
397 a = ALIGN(sizeof(sd_bus_message)) + ALIGN(extra);
400 label_sz = strlen(label);
415 m->creds.uid = ucred->uid;
416 m->creds.pid = ucred->pid;
417 m->creds.gid = ucred->gid;
418 m->creds.mask |= SD_BUS_CREDS_UID | SD_BUS_CREDS_PID | SD_BUS_CREDS_GID;
422 m->creds.label = (char*) m + ALIGN(sizeof(sd_bus_message)) + ALIGN(extra);
423 memcpy(m->creds.label, label, label_sz + 1);
425 m->creds.mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
429 m->bus = sd_bus_ref(bus);
435 int bus_message_from_malloc(
441 const struct ucred *ucred,
443 sd_bus_message **ret) {
449 r = bus_message_from_header(bus, buffer, length, fds, n_fds, ucred, label, 0, &m);
453 if (length != BUS_MESSAGE_SIZE(m)) {
458 sz = length - sizeof(struct bus_header) - ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m));
461 m->body.data = (uint8_t*) buffer + sizeof(struct bus_header) + ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m));
463 m->body.sealed = true;
468 m->iovec = m->iovec_fixed;
469 m->iovec[0].iov_base = buffer;
470 m->iovec[0].iov_len = length;
472 r = bus_message_parse_fields(m);
476 /* We take possession of the memory and fds now */
477 m->free_header = true;
488 static sd_bus_message *message_new(sd_bus *bus, uint8_t type) {
491 m = malloc0(ALIGN(sizeof(sd_bus_message)) + sizeof(struct bus_header));
496 m->header = (struct bus_header*) ((uint8_t*) m + ALIGN(sizeof(struct sd_bus_message)));
497 m->header->endian = BUS_NATIVE_ENDIAN;
498 m->header->type = type;
499 m->header->version = bus ? bus->message_version : 1;
500 m->allow_fds = !bus || bus->can_fds || (bus->state != BUS_HELLO && bus->state != BUS_RUNNING);
501 m->root_container.need_offsets = BUS_MESSAGE_IS_GVARIANT(m);
504 m->bus = sd_bus_ref(bus);
509 _public_ int sd_bus_message_new_signal(
512 const char *interface,
514 sd_bus_message **m) {
519 assert_return(!bus || bus->state != BUS_UNSET, -ENOTCONN);
520 assert_return(object_path_is_valid(path), -EINVAL);
521 assert_return(interface_name_is_valid(interface), -EINVAL);
522 assert_return(member_name_is_valid(member), -EINVAL);
523 assert_return(m, -EINVAL);
525 t = message_new(bus, SD_BUS_MESSAGE_SIGNAL);
529 t->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED;
531 r = message_append_field_string(t, BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path);
534 r = message_append_field_string(t, BUS_MESSAGE_HEADER_INTERFACE, SD_BUS_TYPE_STRING, interface, &t->interface);
537 r = message_append_field_string(t, BUS_MESSAGE_HEADER_MEMBER, SD_BUS_TYPE_STRING, member, &t->member);
545 sd_bus_message_unref(t);
549 _public_ int sd_bus_message_new_method_call(
551 const char *destination,
553 const char *interface,
555 sd_bus_message **m) {
560 assert_return(!bus || bus->state != BUS_UNSET, -ENOTCONN);
561 assert_return(!destination || service_name_is_valid(destination), -EINVAL);
562 assert_return(object_path_is_valid(path), -EINVAL);
563 assert_return(!interface || interface_name_is_valid(interface), -EINVAL);
564 assert_return(member_name_is_valid(member), -EINVAL);
565 assert_return(m, -EINVAL);
567 t = message_new(bus, SD_BUS_MESSAGE_METHOD_CALL);
571 r = message_append_field_string(t, BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path);
574 r = message_append_field_string(t, BUS_MESSAGE_HEADER_MEMBER, SD_BUS_TYPE_STRING, member, &t->member);
579 r = message_append_field_string(t, BUS_MESSAGE_HEADER_INTERFACE, SD_BUS_TYPE_STRING, interface, &t->interface);
585 r = message_append_field_string(t, BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &t->destination);
598 static int message_new_reply(
599 sd_bus_message *call,
601 sd_bus_message **m) {
606 assert_return(call, -EINVAL);
607 assert_return(call->sealed, -EPERM);
608 assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
609 assert_return(!call->bus || call->bus->state != BUS_UNSET, -ENOTCONN);
610 assert_return(m, -EINVAL);
612 t = message_new(call->bus, type);
616 t->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED;
617 t->reply_serial = BUS_MESSAGE_SERIAL(call);
619 r = message_append_field_uint32(t, BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_serial);
624 r = message_append_field_string(t, BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, call->sender, &t->destination);
629 t->dont_send = !!(call->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED);
630 t->enforced_reply_signature = call->enforced_reply_signature;
640 _public_ int sd_bus_message_new_method_return(
641 sd_bus_message *call,
642 sd_bus_message **m) {
644 return message_new_reply(call, SD_BUS_MESSAGE_METHOD_RETURN, m);
647 _public_ int sd_bus_message_new_method_error(
648 sd_bus_message *call,
649 const sd_bus_error *e,
650 sd_bus_message **m) {
655 assert_return(sd_bus_error_is_set(e), -EINVAL);
656 assert_return(m, -EINVAL);
658 r = message_new_reply(call, SD_BUS_MESSAGE_METHOD_ERROR, &t);
662 r = message_append_field_string(t, BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, e->name, &t->error.name);
667 r = message_append_basic(t, SD_BUS_TYPE_STRING, e->message, (const void**) &t->error.message);
672 t->error._need_free = -1;
682 _public_ int sd_bus_message_new_method_errorf(
683 sd_bus_message *call,
689 _cleanup_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
692 assert_return(name, -EINVAL);
693 assert_return(m, -EINVAL);
695 va_start(ap, format);
696 bus_error_setfv(&error, name, format, ap);
699 return sd_bus_message_new_method_error(call, &error, m);
702 _public_ int sd_bus_message_new_method_errno(
703 sd_bus_message *call,
705 const sd_bus_error *p,
706 sd_bus_message **m) {
708 _cleanup_free_ sd_bus_error berror = SD_BUS_ERROR_NULL;
710 if (sd_bus_error_is_set(p))
711 return sd_bus_message_new_method_error(call, p, m);
713 sd_bus_error_set_errno(&berror, error);
715 return sd_bus_message_new_method_error(call, &berror, m);
718 _public_ int sd_bus_message_new_method_errnof(
719 sd_bus_message *call,
725 _cleanup_free_ sd_bus_error berror = SD_BUS_ERROR_NULL;
728 va_start(ap, format);
729 bus_error_set_errnofv(&berror, error, format, ap);
732 return sd_bus_message_new_method_error(call, &berror, m);
735 int bus_message_new_synthetic_error(
738 const sd_bus_error *e,
739 sd_bus_message **m) {
744 assert(sd_bus_error_is_set(e));
747 t = message_new(bus, SD_BUS_MESSAGE_METHOD_ERROR);
751 t->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED;
752 t->reply_serial = serial;
754 r = message_append_field_uint32(t, BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_serial);
758 if (bus && bus->unique_name) {
759 r = message_append_field_string(t, BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, bus->unique_name, &t->destination);
764 r = message_append_field_string(t, BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, e->name, &t->error.name);
769 r = message_append_basic(t, SD_BUS_TYPE_STRING, e->message, (const void**) &t->error.message);
774 t->error._need_free = -1;
784 _public_ sd_bus_message* sd_bus_message_ref(sd_bus_message *m) {
785 assert_return(m, NULL);
787 assert(m->n_ref > 0);
793 _public_ sd_bus_message* sd_bus_message_unref(sd_bus_message *m) {
794 assert_return(m, NULL);
796 assert(m->n_ref > 0);
805 _public_ int sd_bus_message_get_type(sd_bus_message *m, uint8_t *type) {
806 assert_return(m, -EINVAL);
807 assert_return(type, -EINVAL);
809 *type = m->header->type;
813 _public_ int sd_bus_message_get_serial(sd_bus_message *m, uint64_t *serial) {
814 assert_return(m, -EINVAL);
815 assert_return(serial, -EINVAL);
816 assert_return(m->header->serial != 0, -ENOENT);
818 *serial = BUS_MESSAGE_SERIAL(m);
822 _public_ int sd_bus_message_get_reply_serial(sd_bus_message *m, uint64_t *serial) {
823 assert_return(m, -EINVAL);
824 assert_return(serial, -EINVAL);
825 assert_return(m->reply_serial != 0, -ENOENT);
827 *serial = m->reply_serial;
831 _public_ int sd_bus_message_get_no_reply(sd_bus_message *m) {
832 assert_return(m, -EINVAL);
834 return m->header->type == SD_BUS_MESSAGE_METHOD_CALL ? !!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED) : 0;
837 _public_ int sd_bus_message_get_no_auto_start(sd_bus_message *m) {
838 assert_return(m, -EINVAL);
840 return !!(m->header->flags & BUS_MESSAGE_NO_AUTO_START);
843 _public_ const char *sd_bus_message_get_path(sd_bus_message *m) {
844 assert_return(m, NULL);
849 _public_ const char *sd_bus_message_get_interface(sd_bus_message *m) {
850 assert_return(m, NULL);
855 _public_ const char *sd_bus_message_get_member(sd_bus_message *m) {
856 assert_return(m, NULL);
861 _public_ const char *sd_bus_message_get_destination(sd_bus_message *m) {
862 assert_return(m, NULL);
864 return m->destination;
867 _public_ const char *sd_bus_message_get_sender(sd_bus_message *m) {
868 assert_return(m, NULL);
873 _public_ const sd_bus_error *sd_bus_message_get_error(sd_bus_message *m) {
874 assert_return(m, NULL);
875 assert_return(sd_bus_error_is_set(&m->error), NULL);
880 _public_ int sd_bus_message_get_monotonic_timestamp(sd_bus_message *m, uint64_t *usec) {
881 assert_return(m, -EINVAL);
882 assert_return(usec, -EINVAL);
883 assert_return(m->monotonic > 0, -ENODATA);
885 *usec = m->monotonic;
889 _public_ int sd_bus_message_get_realtime_timestamp(sd_bus_message *m, uint64_t *usec) {
890 assert_return(m, -EINVAL);
891 assert_return(usec, -EINVAL);
892 assert_return(m->realtime > 0, -ENODATA);
898 _public_ sd_bus_creds *sd_bus_message_get_creds(sd_bus_message *m) {
899 assert_return(m, NULL);
901 if (m->creds.mask == 0)
907 _public_ int sd_bus_message_is_signal(sd_bus_message *m,
908 const char *interface,
909 const char *member) {
910 assert_return(m, -EINVAL);
912 if (m->header->type != SD_BUS_MESSAGE_SIGNAL)
915 if (interface && (!m->interface || !streq(m->interface, interface)))
918 if (member && (!m->member || !streq(m->member, member)))
924 _public_ int sd_bus_message_is_method_call(sd_bus_message *m,
925 const char *interface,
926 const char *member) {
927 assert_return(m, -EINVAL);
929 if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
932 if (interface && (!m->interface || !streq(m->interface, interface)))
935 if (member && (!m->member || !streq(m->member, member)))
941 _public_ int sd_bus_message_is_method_error(sd_bus_message *m, const char *name) {
942 assert_return(m, -EINVAL);
944 if (m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
947 if (name && (!m->error.name || !streq(m->error.name, name)))
953 _public_ int sd_bus_message_set_no_reply(sd_bus_message *m, int b) {
954 assert_return(m, -EINVAL);
955 assert_return(!m->sealed, -EPERM);
956 assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EPERM);
959 m->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED;
961 m->header->flags &= ~BUS_MESSAGE_NO_REPLY_EXPECTED;
966 _public_ int sd_bus_message_set_no_auto_start(sd_bus_message *m, int b) {
967 assert_return(m, -EINVAL);
968 assert_return(!m->sealed, -EPERM);
971 m->header->flags |= BUS_MESSAGE_NO_AUTO_START;
973 m->header->flags &= ~BUS_MESSAGE_NO_AUTO_START;
978 static struct bus_container *message_get_container(sd_bus_message *m) {
981 if (m->n_containers == 0)
982 return &m->root_container;
984 assert(m->containers);
985 return m->containers + m->n_containers - 1;
988 struct bus_body_part *message_append_part(sd_bus_message *m) {
989 struct bus_body_part *part;
996 if (m->n_body_parts <= 0) {
1000 assert(m->body_end);
1002 part = new0(struct bus_body_part, 1);
1008 m->body_end->next = part;
1018 static void part_zero(struct bus_body_part *part, size_t sz) {
1023 /* All other fields can be left in their defaults */
1024 assert(!part->data);
1025 assert(part->memfd < 0);
1028 part->is_zero = true;
1029 part->sealed = true;
1032 static int part_make_space(
1033 struct sd_bus_message *m,
1034 struct bus_body_part *part,
1043 assert(!part->sealed);
1048 if (!part->data && part->memfd < 0)
1049 part->memfd = bus_kernel_pop_memfd(m->bus, &part->data, &part->mapped);
1051 if (part->memfd >= 0) {
1054 r = ioctl(part->memfd, KDBUS_CMD_MEMFD_SIZE_SET, &u);
1060 if (!part->data || sz > part->mapped) {
1061 size_t psz = PAGE_ALIGN(sz > 0 ? sz : 1);
1063 if (part->mapped <= 0)
1064 n = mmap(NULL, psz, PROT_READ|PROT_WRITE, MAP_SHARED, part->memfd, 0);
1066 n = mremap(part->data, part->mapped, psz, MREMAP_MAYMOVE);
1068 if (n == MAP_FAILED) {
1077 part->munmap_this = true;
1079 n = realloc(part->data, MAX(sz, 1u));
1086 part->free_this = true;
1090 *q = part->data ? (uint8_t*) part->data + part->size : NULL;
1096 static int message_add_offset(sd_bus_message *m, size_t offset) {
1097 struct bus_container *c;
1100 assert(BUS_MESSAGE_IS_GVARIANT(m));
1102 /* Add offset to current container, unless this is the first
1103 * item in it, which will have the 0 offset, which we can
1105 c = message_get_container(m);
1107 if (!c->need_offsets)
1110 if (!GREEDY_REALLOC(c->offsets, c->n_offsets_allocated, c->n_offsets + 1))
1113 c->offsets[c->n_offsets++] = offset;
1117 static void message_extend_containers(sd_bus_message *m, size_t expand) {
1118 struct bus_container *c;
1125 /* Update counters */
1126 for (c = m->containers; c < m->containers + m->n_containers; c++) {
1129 *c->array_size += expand;
1133 static void *message_extend_body(sd_bus_message *m, size_t align, size_t sz, bool add_offset) {
1134 struct bus_body_part *part = NULL;
1135 size_t start_body, end_body, padding, start_part, end_part, added;
1147 start_body = ALIGN_TO((size_t) m->header->body_size, align);
1148 end_body = start_body + sz;
1150 padding = start_body - m->header->body_size;
1151 added = padding + sz;
1153 /* Check for 32bit overflows */
1154 if (end_body > (size_t) ((uint32_t) -1)) {
1160 m->n_body_parts <= 0 ||
1161 m->body_end->sealed ||
1162 padding != ALIGN_TO(m->body_end->size, align) - m->body_end->size;
1166 part = message_append_part(m);
1170 part_zero(part, padding);
1173 part = message_append_part(m);
1177 r = part_make_space(m, part, sz, &p);
1181 struct bus_container *c;
1189 start_part = ALIGN_TO(part->size, align);
1190 end_part = start_part + sz;
1192 r = part_make_space(m, part, end_part, &p);
1197 memset(p, 0, padding);
1198 p = (uint8_t*) p + padding;
1201 /* Readjust pointers */
1202 for (c = m->containers; c < m->containers + m->n_containers; c++)
1203 c->array_size = adjust_pointer(c->array_size, op, os, part->data);
1205 m->error.message = (const char*) adjust_pointer(m->error.message, op, os, part->data);
1208 m->header->body_size = end_body;
1209 message_extend_containers(m, added);
1212 r = message_add_offset(m, end_body);
1222 static int message_push_fd(sd_bus_message *m, int fd) {
1233 copy = fcntl(fd, F_DUPFD_CLOEXEC, 3);
1237 f = realloc(m->fds, sizeof(int) * (m->n_fds + 1));
1240 close_nointr_nofail(copy);
1245 m->fds[m->n_fds] = copy;
1251 int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored) {
1252 _cleanup_close_ int fd = -1;
1253 struct bus_container *c;
1257 assert_return(m, -EINVAL);
1258 assert_return(!m->sealed, -EPERM);
1259 assert_return(bus_type_is_basic(type), -EINVAL);
1260 assert_return(!m->poisoned, -ESTALE);
1262 c = message_get_container(m);
1264 if (c->signature && c->signature[c->index]) {
1265 /* Container signature is already set */
1267 if (c->signature[c->index] != type)
1272 /* Maybe we can append to the signature? But only if this is the top-level container*/
1273 if (c->enclosing != 0)
1276 e = strextend(&c->signature, CHAR_TO_STR(type), NULL);
1283 if (BUS_MESSAGE_IS_GVARIANT(m)) {
1289 case SD_BUS_TYPE_SIGNATURE:
1290 case SD_BUS_TYPE_STRING:
1293 /* Fall through... */
1294 case SD_BUS_TYPE_OBJECT_PATH:
1302 case SD_BUS_TYPE_BOOLEAN:
1304 u8 = p && *(int*) p;
1310 case SD_BUS_TYPE_UNIX_FD:
1315 fd = message_push_fd(m, *(int*) p);
1326 align = bus_gvariant_get_alignment(CHAR_TO_STR(type));
1327 sz = bus_gvariant_get_size(CHAR_TO_STR(type));
1334 a = message_extend_body(m, align, sz, true);
1341 *stored = (const uint8_t*) a;
1348 case SD_BUS_TYPE_STRING:
1349 /* To make things easy we'll serialize a NULL string
1350 * into the empty string */
1353 /* Fall through... */
1354 case SD_BUS_TYPE_OBJECT_PATH:
1360 sz = 4 + strlen(p) + 1;
1363 case SD_BUS_TYPE_SIGNATURE:
1368 sz = 1 + strlen(p) + 1;
1371 case SD_BUS_TYPE_BOOLEAN:
1373 u32 = p && *(int*) p;
1379 case SD_BUS_TYPE_UNIX_FD:
1384 fd = message_push_fd(m, *(int*) p);
1395 align = bus_type_get_alignment(type);
1396 sz = bus_type_get_size(type);
1403 a = message_extend_body(m, align, sz, false);
1407 if (type == SD_BUS_TYPE_STRING || type == SD_BUS_TYPE_OBJECT_PATH) {
1408 *(uint32_t*) a = sz - 5;
1409 memcpy((uint8_t*) a + 4, p, sz - 4);
1412 *stored = (const uint8_t*) a + 4;
1414 } else if (type == SD_BUS_TYPE_SIGNATURE) {
1415 *(uint8_t*) a = sz - 1;
1416 memcpy((uint8_t*) a + 1, p, sz - 1);
1419 *stored = (const uint8_t*) a + 1;
1428 if (type == SD_BUS_TYPE_UNIX_FD)
1431 if (c->enclosing != SD_BUS_TYPE_ARRAY)
1438 _public_ int sd_bus_message_append_basic(sd_bus_message *m, char type, const void *p) {
1439 return message_append_basic(m, type, p, NULL);
1442 _public_ int sd_bus_message_append_string_space(
1447 struct bus_container *c;
1450 assert_return(m, -EINVAL);
1451 assert_return(s, -EINVAL);
1452 assert_return(!m->sealed, -EPERM);
1453 assert_return(!m->poisoned, -ESTALE);
1455 c = message_get_container(m);
1457 if (c->signature && c->signature[c->index]) {
1458 /* Container signature is already set */
1460 if (c->signature[c->index] != SD_BUS_TYPE_STRING)
1465 /* Maybe we can append to the signature? But only if this is the top-level container*/
1466 if (c->enclosing != 0)
1469 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRING), NULL);
1476 if (BUS_MESSAGE_IS_GVARIANT(m)) {
1477 a = message_extend_body(m, 1, size + 1, true);
1483 a = message_extend_body(m, 4, 4 + size + 1, false);
1487 *(uint32_t*) a = size;
1493 if (c->enclosing != SD_BUS_TYPE_ARRAY)
1499 _public_ int sd_bus_message_append_string_iovec(
1501 const struct iovec *iov,
1509 assert_return(m, -EINVAL);
1510 assert_return(!m->sealed, -EPERM);
1511 assert_return(iov || n == 0, -EINVAL);
1512 assert_return(!m->poisoned, -ESTALE);
1514 size = IOVEC_TOTAL_SIZE(iov, n);
1516 r = sd_bus_message_append_string_space(m, size, &p);
1520 for (i = 0; i < n; i++) {
1522 if (iov[i].iov_base)
1523 memcpy(p, iov[i].iov_base, iov[i].iov_len);
1525 memset(p, ' ', iov[i].iov_len);
1527 p += iov[i].iov_len;
1533 static int bus_message_open_array(
1535 struct bus_container *c,
1536 const char *contents,
1537 uint32_t **array_size,
1539 bool *need_offsets) {
1549 assert(need_offsets);
1551 if (!signature_is_single(contents, true))
1554 if (c->signature && c->signature[c->index]) {
1556 /* Verify the existing signature */
1558 if (c->signature[c->index] != SD_BUS_TYPE_ARRAY)
1561 if (!startswith(c->signature + c->index + 1, contents))
1564 nindex = c->index + 1 + strlen(contents);
1568 if (c->enclosing != 0)
1571 /* Extend the existing signature */
1573 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_ARRAY), contents, NULL);
1579 nindex = e - c->signature;
1582 if (BUS_MESSAGE_IS_GVARIANT(m)) {
1583 alignment = bus_gvariant_get_alignment(contents);
1587 /* Add alignment padding and add to offset list */
1588 if (!message_extend_body(m, alignment, 0, false))
1591 r = bus_gvariant_is_fixed_size(contents);
1595 *begin = m->header->body_size;
1596 *need_offsets = r == 0;
1600 struct bus_body_part *o;
1602 alignment = bus_type_get_alignment(contents[0]);
1606 a = message_extend_body(m, 4, 4, false);
1611 op = m->body_end->data;
1612 os = m->body_end->size;
1614 /* Add alignment between size and first element */
1615 if (!message_extend_body(m, alignment, 0, false))
1618 /* location of array size might have changed so let's readjust a */
1619 if (o == m->body_end)
1620 a = adjust_pointer(a, op, os, m->body_end->data);
1626 if (c->enclosing != SD_BUS_TYPE_ARRAY)
1632 static int bus_message_open_variant(
1634 struct bus_container *c,
1635 const char *contents) {
1641 if (!signature_is_single(contents, false))
1644 if (*contents == SD_BUS_TYPE_DICT_ENTRY_BEGIN)
1647 if (c->signature && c->signature[c->index]) {
1649 if (c->signature[c->index] != SD_BUS_TYPE_VARIANT)
1655 if (c->enclosing != 0)
1658 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_VARIANT), NULL);
1665 if (BUS_MESSAGE_IS_GVARIANT(m)) {
1666 /* Variants are always aligned to 8 */
1668 if (!message_extend_body(m, 8, 0, false))
1675 l = strlen(contents);
1676 a = message_extend_body(m, 1, 1 + l + 1, false);
1681 memcpy((uint8_t*) a + 1, contents, l + 1);
1684 if (c->enclosing != SD_BUS_TYPE_ARRAY)
1690 static int bus_message_open_struct(
1692 struct bus_container *c,
1693 const char *contents,
1695 bool *need_offsets) {
1704 assert(need_offsets);
1706 if (!signature_is_valid(contents, false))
1709 if (c->signature && c->signature[c->index]) {
1712 l = strlen(contents);
1714 if (c->signature[c->index] != SD_BUS_TYPE_STRUCT_BEGIN ||
1715 !startswith(c->signature + c->index + 1, contents) ||
1716 c->signature[c->index + 1 + l] != SD_BUS_TYPE_STRUCT_END)
1719 nindex = c->index + 1 + l + 1;
1723 if (c->enclosing != 0)
1726 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_BEGIN), contents, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_END), NULL);
1732 nindex = e - c->signature;
1735 if (BUS_MESSAGE_IS_GVARIANT(m)) {
1738 alignment = bus_gvariant_get_alignment(contents);
1742 if (!message_extend_body(m, alignment, 0, false))
1745 r = bus_gvariant_is_fixed_size(contents);
1749 *begin = m->header->body_size;
1750 *need_offsets = r == 0;
1752 /* Align contents to 8 byte boundary */
1753 if (!message_extend_body(m, 8, 0, false))
1757 if (c->enclosing != SD_BUS_TYPE_ARRAY)
1763 static int bus_message_open_dict_entry(
1765 struct bus_container *c,
1766 const char *contents,
1768 bool *need_offsets) {
1776 assert(need_offsets);
1778 if (!signature_is_pair(contents))
1781 if (c->enclosing != SD_BUS_TYPE_ARRAY)
1784 if (c->signature && c->signature[c->index]) {
1787 l = strlen(contents);
1789 if (c->signature[c->index] != SD_BUS_TYPE_DICT_ENTRY_BEGIN ||
1790 !startswith(c->signature + c->index + 1, contents) ||
1791 c->signature[c->index + 1 + l] != SD_BUS_TYPE_DICT_ENTRY_END)
1796 if (BUS_MESSAGE_IS_GVARIANT(m)) {
1799 alignment = bus_gvariant_get_alignment(contents);
1803 if (!message_extend_body(m, alignment, 0, false))
1806 r = bus_gvariant_is_fixed_size(contents);
1810 *begin = m->header->body_size;
1811 *need_offsets = r == 0;
1813 /* Align contents to 8 byte boundary */
1814 if (!message_extend_body(m, 8, 0, false))
1821 _public_ int sd_bus_message_open_container(
1824 const char *contents) {
1826 struct bus_container *c, *w;
1827 uint32_t *array_size = NULL;
1829 size_t before, begin;
1830 bool need_offsets = false;
1833 assert_return(m, -EINVAL);
1834 assert_return(!m->sealed, -EPERM);
1835 assert_return(contents, -EINVAL);
1836 assert_return(!m->poisoned, -ESTALE);
1838 /* Make sure we have space for one more container */
1839 w = realloc(m->containers, sizeof(struct bus_container) * (m->n_containers + 1));
1847 c = message_get_container(m);
1849 signature = strdup(contents);
1855 /* Save old index in the parent container, in case we have to
1856 * abort this container */
1857 c->saved_index = c->index;
1858 before = m->header->body_size;
1860 if (type == SD_BUS_TYPE_ARRAY)
1861 r = bus_message_open_array(m, c, contents, &array_size, &begin, &need_offsets);
1862 else if (type == SD_BUS_TYPE_VARIANT)
1863 r = bus_message_open_variant(m, c, contents);
1864 else if (type == SD_BUS_TYPE_STRUCT)
1865 r = bus_message_open_struct(m, c, contents, &begin, &need_offsets);
1866 else if (type == SD_BUS_TYPE_DICT_ENTRY)
1867 r = bus_message_open_dict_entry(m, c, contents, &begin, &need_offsets);
1876 /* OK, let's fill it in */
1877 w += m->n_containers++;
1878 w->enclosing = type;
1879 w->signature = signature;
1881 w->array_size = array_size;
1884 w->n_offsets = w->n_offsets_allocated = 0;
1886 w->need_offsets = need_offsets;
1891 static size_t determine_word_size(size_t sz, size_t extra) {
1892 if (sz <= 0 && extra == 0)
1894 else if (sz + extra <= 0xFF)
1896 else if (sz + extra*2 <= 0xFFFF)
1898 else if (sz + extra*4 <= 0xFFFFFFFF)
1904 static size_t read_word_le(void *p, size_t sz) {
1914 return *(uint8_t*) p;
1919 return le16toh(x.u16);
1921 return le32toh(x.u32);
1923 return le64toh(x.u64);
1925 assert_not_reached("unknown word width");
1928 static void write_word_le(void *p, size_t sz, size_t value) {
1936 assert(sz == 8 || (value < (1ULL << (sz*8))));
1939 *(uint8_t*) p = value;
1942 x.u16 = htole16((uint16_t) value);
1944 x.u32 = htole32((uint32_t) value);
1946 x.u64 = htole64((uint64_t) value);
1948 assert_not_reached("unknown word width");
1953 static int bus_message_close_array(sd_bus_message *m, struct bus_container *c) {
1958 if (!BUS_MESSAGE_IS_GVARIANT(m))
1961 if (c->need_offsets) {
1962 size_t payload, sz, i;
1965 /* Variable-width arrays */
1967 payload = c->n_offsets > 0 ? c->offsets[c->n_offsets-1] - c->begin : 0;
1968 sz = determine_word_size(payload, c->n_offsets);
1970 a = message_extend_body(m, 1, sz * c->n_offsets, true);
1974 for (i = 0; i < c->n_offsets; i++)
1975 write_word_le(a + sz*i, sz, c->offsets[i] - c->begin);
1979 /* Fixed-width or empty arrays */
1981 a = message_extend_body(m, 1, 0, true); /* let's add offset to parent */
1989 static int bus_message_close_variant(sd_bus_message *m, struct bus_container *c) {
1996 if (!BUS_MESSAGE_IS_GVARIANT(m))
1999 l = strlen(c->signature);
2001 a = message_extend_body(m, 1, 1 + l, true);
2006 memcpy(a+1, c->signature, l);
2011 static int bus_message_close_struct(sd_bus_message *m, struct bus_container *c, bool add_offset) {
2012 size_t n_variable = 0;
2021 if (!BUS_MESSAGE_IS_GVARIANT(m))
2028 r = signature_element_length(p, &n);
2037 r = bus_gvariant_is_fixed_size(t);
2042 assert(i <= c->n_offsets);
2044 /* We need to add an offset for each item that has a
2045 * variable size and that is not the last one in the
2047 if (r == 0 && p[n] != 0)
2054 assert(i == c->n_offsets);
2056 if (n_variable <= 0) {
2057 a = message_extend_body(m, 1, 0, add_offset);
2064 assert(c->offsets[c->n_offsets-1] == m->header->body_size);
2066 sz = determine_word_size(m->header->body_size - c->begin, n_variable);
2068 a = message_extend_body(m, 1, sz * n_variable, add_offset);
2073 for (i = 0, j = 0; i < c->n_offsets; i++) {
2077 r = signature_element_length(p, &n);
2088 r = bus_gvariant_is_fixed_size(t);
2091 if (r > 0 || p[0] == 0)
2095 k = n_variable - 1 - j;
2097 write_word_le(a + k * sz, sz, c->offsets[i] - c->begin);
2106 _public_ int sd_bus_message_close_container(sd_bus_message *m) {
2107 struct bus_container *c;
2110 assert_return(m, -EINVAL);
2111 assert_return(!m->sealed, -EPERM);
2112 assert_return(m->n_containers > 0, -EINVAL);
2113 assert_return(!m->poisoned, -ESTALE);
2115 c = message_get_container(m);
2117 if (c->enclosing != SD_BUS_TYPE_ARRAY)
2118 if (c->signature && c->signature[c->index] != 0)
2123 if (c->enclosing == SD_BUS_TYPE_ARRAY)
2124 r = bus_message_close_array(m, c);
2125 else if (c->enclosing == SD_BUS_TYPE_VARIANT)
2126 r = bus_message_close_variant(m, c);
2127 else if (c->enclosing == SD_BUS_TYPE_STRUCT || c->enclosing == SD_BUS_TYPE_DICT_ENTRY)
2128 r = bus_message_close_struct(m, c, true);
2130 assert_not_reached("Unknown container type");
2144 static int type_stack_push(TypeStack *stack, unsigned max, unsigned *i, const char *types, unsigned n_struct, unsigned n_array) {
2151 stack[*i].types = types;
2152 stack[*i].n_struct = n_struct;
2153 stack[*i].n_array = n_array;
2159 static int type_stack_pop(TypeStack *stack, unsigned max, unsigned *i, const char **types, unsigned *n_struct, unsigned *n_array) {
2170 *types = stack[*i].types;
2171 *n_struct = stack[*i].n_struct;
2172 *n_array = stack[*i].n_array;
2177 int bus_message_append_ap(
2182 unsigned n_array, n_struct;
2183 TypeStack stack[BUS_CONTAINER_DEPTH];
2184 unsigned stack_ptr = 0;
2192 n_array = (unsigned) -1;
2193 n_struct = strlen(types);
2198 if (n_array == 0 || (n_array == (unsigned) -1 && n_struct == 0)) {
2199 r = type_stack_pop(stack, ELEMENTSOF(stack), &stack_ptr, &types, &n_struct, &n_array);
2205 r = sd_bus_message_close_container(m);
2213 if (n_array != (unsigned) -1)
2222 case SD_BUS_TYPE_BYTE: {
2225 x = (uint8_t) va_arg(ap, int);
2226 r = sd_bus_message_append_basic(m, *t, &x);
2230 case SD_BUS_TYPE_BOOLEAN:
2231 case SD_BUS_TYPE_INT32:
2232 case SD_BUS_TYPE_UINT32:
2233 case SD_BUS_TYPE_UNIX_FD: {
2236 /* We assume a boolean is the same as int32_t */
2237 assert_cc(sizeof(int32_t) == sizeof(int));
2239 x = va_arg(ap, uint32_t);
2240 r = sd_bus_message_append_basic(m, *t, &x);
2244 case SD_BUS_TYPE_INT16:
2245 case SD_BUS_TYPE_UINT16: {
2248 x = (uint16_t) va_arg(ap, int);
2249 r = sd_bus_message_append_basic(m, *t, &x);
2253 case SD_BUS_TYPE_INT64:
2254 case SD_BUS_TYPE_UINT64:
2255 case SD_BUS_TYPE_DOUBLE: {
2258 x = va_arg(ap, uint64_t);
2259 r = sd_bus_message_append_basic(m, *t, &x);
2263 case SD_BUS_TYPE_STRING:
2264 case SD_BUS_TYPE_OBJECT_PATH:
2265 case SD_BUS_TYPE_SIGNATURE: {
2268 x = va_arg(ap, const char*);
2269 r = sd_bus_message_append_basic(m, *t, x);
2273 case SD_BUS_TYPE_ARRAY: {
2276 r = signature_element_length(t + 1, &k);
2282 memcpy(s, t + 1, k);
2285 r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, s);
2290 if (n_array == (unsigned) -1) {
2295 r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
2301 n_array = va_arg(ap, unsigned);
2306 case SD_BUS_TYPE_VARIANT: {
2309 s = va_arg(ap, const char*);
2313 r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT, s);
2317 r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
2322 n_struct = strlen(s);
2323 n_array = (unsigned) -1;
2328 case SD_BUS_TYPE_STRUCT_BEGIN:
2329 case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
2332 r = signature_element_length(t, &k);
2339 memcpy(s, t + 1, k - 2);
2342 r = sd_bus_message_open_container(m, *t == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s);
2347 if (n_array == (unsigned) -1) {
2352 r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
2358 n_array = (unsigned) -1;
2374 _public_ int sd_bus_message_append(sd_bus_message *m, const char *types, ...) {
2378 assert_return(m, -EINVAL);
2379 assert_return(types, -EINVAL);
2380 assert_return(!m->sealed, -EPERM);
2381 assert_return(!m->poisoned, -ESTALE);
2383 va_start(ap, types);
2384 r = bus_message_append_ap(m, types, ap);
2390 _public_ int sd_bus_message_append_array_space(
2400 assert_return(m, -EINVAL);
2401 assert_return(!m->sealed, -EPERM);
2402 assert_return(bus_type_is_trivial(type), -EINVAL);
2403 assert_return(ptr || size == 0, -EINVAL);
2404 assert_return(!m->poisoned, -ESTALE);
2406 align = bus_type_get_alignment(type);
2407 sz = bus_type_get_size(type);
2409 assert_se(align > 0);
2415 r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, CHAR_TO_STR(type));
2419 a = message_extend_body(m, align, size, false);
2423 r = sd_bus_message_close_container(m);
2431 _public_ int sd_bus_message_append_array(sd_bus_message *m,
2438 assert_return(m, -EINVAL);
2439 assert_return(!m->sealed, -EPERM);
2440 assert_return(bus_type_is_trivial(type), -EINVAL);
2441 assert_return(ptr || size == 0, -EINVAL);
2442 assert_return(!m->poisoned, -ESTALE);
2444 r = sd_bus_message_append_array_space(m, type, size, &p);
2449 memcpy(p, ptr, size);
2454 _public_ int sd_bus_message_append_array_iovec(
2457 const struct iovec *iov,
2465 assert_return(m, -EINVAL);
2466 assert_return(!m->sealed, -EPERM);
2467 assert_return(bus_type_is_trivial(type), -EINVAL);
2468 assert_return(iov || n == 0, -EINVAL);
2469 assert_return(!m->poisoned, -ESTALE);
2471 size = IOVEC_TOTAL_SIZE(iov, n);
2473 r = sd_bus_message_append_array_space(m, type, size, &p);
2477 for (i = 0; i < n; i++) {
2479 if (iov[i].iov_base)
2480 memcpy(p, iov[i].iov_base, iov[i].iov_len);
2482 memset(p, 0, iov[i].iov_len);
2484 p = (uint8_t*) p + iov[i].iov_len;
2490 _public_ int sd_bus_message_append_array_memfd(sd_bus_message *m,
2493 _cleanup_close_ int copy_fd = -1;
2494 struct bus_body_part *part;
2506 if (!bus_type_is_trivial(type))
2511 r = sd_memfd_set_sealed(memfd, true);
2515 copy_fd = sd_memfd_dup_fd(memfd);
2519 r = sd_memfd_get_size(memfd, &size);
2523 align = bus_type_get_alignment(type);
2524 sz = bus_type_get_size(type);
2526 assert_se(align > 0);
2532 if (size > (uint64_t) (uint32_t) -1)
2535 r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, CHAR_TO_STR(type));
2539 a = message_extend_body(m, align, 0, false);
2543 part = message_append_part(m);
2547 part->memfd = copy_fd;
2548 part->sealed = true;
2552 message_extend_containers(m, size);
2553 m->header->body_size += size;
2555 return sd_bus_message_close_container(m);
2558 _public_ int sd_bus_message_append_string_memfd(sd_bus_message *m, sd_memfd *memfd) {
2559 _cleanup_close_ int copy_fd = -1;
2560 struct bus_body_part *part;
2561 struct bus_container *c;
2566 assert_return(m, -EINVAL);
2567 assert_return(memfd, -EINVAL);
2568 assert_return(!m->sealed, -EPERM);
2569 assert_return(!m->poisoned, -ESTALE);
2571 r = sd_memfd_set_sealed(memfd, true);
2575 copy_fd = sd_memfd_dup_fd(memfd);
2579 r = sd_memfd_get_size(memfd, &size);
2583 /* We require this to be NUL terminated */
2587 if (size > (uint64_t) (uint32_t) -1)
2590 c = message_get_container(m);
2591 if (c->signature && c->signature[c->index]) {
2592 /* Container signature is already set */
2594 if (c->signature[c->index] != SD_BUS_TYPE_STRING)
2599 /* Maybe we can append to the signature? But only if this is the top-level container*/
2600 if (c->enclosing != 0)
2603 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRING), NULL);
2610 if (!BUS_MESSAGE_IS_GVARIANT(m)) {
2611 a = message_extend_body(m, 4, 4, false);
2615 *(uint32_t*) a = size - 1;
2618 part = message_append_part(m);
2622 part->memfd = copy_fd;
2623 part->sealed = true;
2627 m->header->body_size += size;
2628 message_extend_containers(m, size);
2630 if (BUS_MESSAGE_IS_GVARIANT(m)) {
2631 r = message_add_offset(m, m->header->body_size);
2638 if (c->enclosing != SD_BUS_TYPE_ARRAY)
2644 _public_ int sd_bus_message_append_strv(sd_bus_message *m, char **l) {
2648 assert_return(m, -EINVAL);
2649 assert_return(!m->sealed, -EPERM);
2650 assert_return(!m->poisoned, -ESTALE);
2652 r = sd_bus_message_open_container(m, 'a', "s");
2656 STRV_FOREACH(i, l) {
2657 r = sd_bus_message_append_basic(m, 's', *i);
2662 return sd_bus_message_close_container(m);
2665 int bus_body_part_map(struct bus_body_part *part) {
2674 if (part->size <= 0)
2677 /* For smaller zero parts (as used for padding) we don't need to map anything... */
2678 if (part->memfd < 0 && part->is_zero && part->size < 8) {
2679 static const uint8_t zeroes[7] = { };
2680 part->data = (void*) zeroes;
2684 psz = PAGE_ALIGN(part->size);
2686 if (part->memfd >= 0)
2687 p = mmap(NULL, psz, PROT_READ, MAP_SHARED, part->memfd, 0);
2688 else if (part->is_zero)
2689 p = mmap(NULL, psz, PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
2693 if (p == MAP_FAILED)
2698 part->munmap_this = true;
2703 void bus_body_part_unmap(struct bus_body_part *part) {
2707 if (part->memfd < 0)
2713 if (!part->munmap_this)
2716 assert_se(munmap(part->data, part->mapped) == 0);
2720 part->munmap_this = false;
2725 static int buffer_peek(const void *p, uint32_t sz, size_t *rindex, size_t align, size_t nbytes, void **r) {
2726 size_t k, start, end;
2731 start = ALIGN_TO((size_t) *rindex, align);
2732 end = start + nbytes;
2737 /* Verify that padding is 0 */
2738 for (k = *rindex; k < start; k++)
2739 if (((const uint8_t*) p)[k] != 0)
2743 *r = (uint8_t*) p + start;
2750 static bool message_end_of_signature(sd_bus_message *m) {
2751 struct bus_container *c;
2755 c = message_get_container(m);
2756 return !c->signature || c->signature[c->index] == 0;
2759 static bool message_end_of_array(sd_bus_message *m, size_t index) {
2760 struct bus_container *c;
2764 c = message_get_container(m);
2765 if (c->enclosing != SD_BUS_TYPE_ARRAY)
2768 if (BUS_MESSAGE_IS_GVARIANT(m))
2769 return index >= c->end;
2771 assert(c->array_size);
2772 return index >= c->begin + BUS_MESSAGE_BSWAP32(m, *c->array_size);
2776 _public_ int sd_bus_message_at_end(sd_bus_message *m, int complete) {
2777 assert_return(m, -EINVAL);
2778 assert_return(m->sealed, -EPERM);
2780 if (complete && m->n_containers > 0)
2783 if (message_end_of_signature(m))
2786 if (message_end_of_array(m, m->rindex))
2792 static struct bus_body_part* find_part(sd_bus_message *m, size_t index, size_t sz, void **p) {
2793 struct bus_body_part *part;
2799 if (m->cached_rindex_part && index >= m->cached_rindex_part_begin) {
2800 part = m->cached_rindex_part;
2801 begin = m->cached_rindex_part_begin;
2811 if (index + sz <= begin + part->size) {
2813 r = bus_body_part_map(part);
2818 *p = (uint8_t*) part->data + index - begin;
2820 m->cached_rindex_part = part;
2821 m->cached_rindex_part_begin = begin;
2826 begin += part->size;
2833 static int container_next_item(sd_bus_message *m, struct bus_container *c, size_t *rindex) {
2840 if (!BUS_MESSAGE_IS_GVARIANT(m))
2843 if (c->enclosing == SD_BUS_TYPE_ARRAY) {
2846 sz = bus_gvariant_get_size(c->signature);
2850 if (c->offset_index+1 >= c->n_offsets)
2853 /* Variable-size array */
2855 alignment = bus_gvariant_get_alignment(c->signature);
2856 assert(alignment > 0);
2858 *rindex = ALIGN_TO(c->offsets[c->offset_index], alignment);
2859 c->item_size = c->offsets[c->offset_index+1] - *rindex;
2861 /* Fixed-size array */
2868 } else if (c->enclosing == 0 ||
2869 c->enclosing == SD_BUS_TYPE_STRUCT ||
2870 c->enclosing == SD_BUS_TYPE_DICT_ENTRY) {
2875 if (c->offset_index+1 >= c->n_offsets)
2878 r = signature_element_length(c->signature + c->index, &n);
2882 r = signature_element_length(c->signature + c->index + n, &j);
2887 memcpy(t, c->signature + c->index + n, j);
2890 alignment = bus_gvariant_get_alignment(t);
2893 assert(alignment > 0);
2895 *rindex = ALIGN_TO(c->offsets[c->offset_index], alignment);
2896 c->item_size = c->offsets[c->offset_index+1] - *rindex;
2900 } else if (c->enclosing == SD_BUS_TYPE_VARIANT)
2903 assert_not_reached("Unknown container type");
2908 /* Reached the end */
2915 static int message_peek_body(
2922 size_t k, start, end, padding;
2923 struct bus_body_part *part;
2930 if (message_end_of_array(m, *rindex))
2933 start = ALIGN_TO((size_t) *rindex, align);
2934 padding = start - *rindex;
2935 end = start + nbytes;
2937 if (end > BUS_MESSAGE_BODY_SIZE(m))
2940 part = find_part(m, *rindex, padding, (void**) &q);
2945 /* Verify padding */
2946 for (k = 0; k < padding; k++)
2951 part = find_part(m, start, nbytes, (void**) &q);
2963 static bool validate_nul(const char *s, size_t l) {
2965 /* Check for NUL chars in the string */
2966 if (memchr(s, 0, l))
2969 /* Check for NUL termination */
2976 static bool validate_string(const char *s, size_t l) {
2978 if (!validate_nul(s, l))
2981 /* Check if valid UTF8 */
2982 if (!utf8_is_valid(s))
2988 static bool validate_signature(const char *s, size_t l) {
2990 if (!validate_nul(s, l))
2993 /* Check if valid signature */
2994 if (!signature_is_valid(s, true))
3000 static bool validate_object_path(const char *s, size_t l) {
3002 if (!validate_nul(s, l))
3005 if (!object_path_is_valid(s))
3011 _public_ int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p) {
3012 struct bus_container *c;
3017 assert_return(m, -EINVAL);
3018 assert_return(m->sealed, -EPERM);
3019 assert_return(bus_type_is_basic(type), -EINVAL);
3021 if (message_end_of_signature(m))
3024 if (message_end_of_array(m, m->rindex))
3027 c = message_get_container(m);
3028 if (c->signature[c->index] != type)
3033 if (BUS_MESSAGE_IS_GVARIANT(m)) {
3035 if (IN_SET(type, SD_BUS_TYPE_STRING, SD_BUS_TYPE_OBJECT_PATH, SD_BUS_TYPE_SIGNATURE)) {
3038 r = message_peek_body(m, &rindex, 1, c->item_size, &q);
3042 if (type == SD_BUS_TYPE_STRING)
3043 ok = validate_string(q, c->item_size-1);
3044 else if (type == SD_BUS_TYPE_OBJECT_PATH)
3045 ok = validate_object_path(q, c->item_size-1);
3047 ok = validate_signature(q, c->item_size-1);
3053 *(const char**) p = q;
3057 sz = bus_gvariant_get_size(CHAR_TO_STR(type));
3059 if ((size_t) sz != c->item_size)
3062 align = bus_gvariant_get_alignment(CHAR_TO_STR(type));
3065 r = message_peek_body(m, &rindex, align, c->item_size, &q);
3071 case SD_BUS_TYPE_BYTE:
3073 *(uint8_t*) p = *(uint8_t*) q;
3076 case SD_BUS_TYPE_BOOLEAN:
3078 *(int*) p = !!*(uint8_t*) q;
3081 case SD_BUS_TYPE_INT16:
3082 case SD_BUS_TYPE_UINT16:
3084 *(uint16_t*) p = BUS_MESSAGE_BSWAP16(m, *(uint16_t*) q);
3087 case SD_BUS_TYPE_INT32:
3088 case SD_BUS_TYPE_UINT32:
3090 *(uint32_t*) p = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
3093 case SD_BUS_TYPE_INT64:
3094 case SD_BUS_TYPE_UINT64:
3095 case SD_BUS_TYPE_DOUBLE:
3097 *(uint64_t*) p = BUS_MESSAGE_BSWAP64(m, *(uint64_t*) q);
3100 case SD_BUS_TYPE_UNIX_FD: {
3103 j = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
3108 *(int*) p = m->fds[j];
3114 assert_not_reached("unexpected type");
3118 r = container_next_item(m, c, &rindex);
3125 if (IN_SET(type, SD_BUS_TYPE_STRING, SD_BUS_TYPE_OBJECT_PATH)) {
3129 r = message_peek_body(m, &rindex, 4, 4, &q);
3133 l = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
3134 r = message_peek_body(m, &rindex, 1, l+1, &q);
3140 if (type == SD_BUS_TYPE_OBJECT_PATH)
3141 ok = validate_object_path(q, l);
3143 ok = validate_string(q, l);
3148 *(const char**) p = q;
3150 } else if (type == SD_BUS_TYPE_SIGNATURE) {
3153 r = message_peek_body(m, &rindex, 1, 1, &q);
3158 r = message_peek_body(m, &rindex, 1, l+1, &q);
3164 if (!validate_signature(q, l))
3168 *(const char**) p = q;
3173 align = bus_type_get_alignment(type);
3176 sz = bus_type_get_size(type);
3179 r = message_peek_body(m, &rindex, align, sz, &q);
3185 case SD_BUS_TYPE_BYTE:
3187 *(uint8_t*) p = *(uint8_t*) q;
3190 case SD_BUS_TYPE_BOOLEAN:
3192 *(int*) p = !!*(uint32_t*) q;
3195 case SD_BUS_TYPE_INT16:
3196 case SD_BUS_TYPE_UINT16:
3198 *(uint16_t*) p = BUS_MESSAGE_BSWAP16(m, *(uint16_t*) q);
3201 case SD_BUS_TYPE_INT32:
3202 case SD_BUS_TYPE_UINT32:
3204 *(uint32_t*) p = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
3207 case SD_BUS_TYPE_INT64:
3208 case SD_BUS_TYPE_UINT64:
3209 case SD_BUS_TYPE_DOUBLE:
3211 *(uint64_t*) p = BUS_MESSAGE_BSWAP64(m, *(uint64_t*) q);
3214 case SD_BUS_TYPE_UNIX_FD: {
3217 j = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
3222 *(int*) p = m->fds[j];
3227 assert_not_reached("Unknown basic type...");
3234 if (c->enclosing != SD_BUS_TYPE_ARRAY)
3240 static int bus_message_enter_array(
3242 struct bus_container *c,
3243 const char *contents,
3244 uint32_t **array_size,
3247 size_t *n_offsets) {
3261 if (!signature_is_single(contents, true))
3264 if (!c->signature || c->signature[c->index] == 0)
3267 if (c->signature[c->index] != SD_BUS_TYPE_ARRAY)
3270 if (!startswith(c->signature + c->index + 1, contents))
3275 if (!BUS_MESSAGE_IS_GVARIANT(m)) {
3278 r = message_peek_body(m, &rindex, 4, 4, &q);
3282 if (BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q) > BUS_ARRAY_MAX_SIZE)
3285 alignment = bus_type_get_alignment(contents[0]);
3289 r = message_peek_body(m, &rindex, alignment, 0, NULL);
3295 *array_size = (uint32_t*) q;
3297 } else if (c->item_size <= 0) {
3299 /* gvariant: empty array */
3304 } else if (bus_gvariant_is_fixed_size(contents)) {
3306 /* gvariant: fixed length array */
3307 *item_size = bus_gvariant_get_size(contents);
3312 size_t where, p = 0, framing, sz;
3315 /* gvariant: variable length array */
3316 sz = determine_word_size(c->item_size, 0);
3318 where = rindex + c->item_size - sz;
3319 r = message_peek_body(m, &where, 1, sz, &q);
3325 framing = read_word_le(q, sz);
3326 if (framing > c->item_size - sz)
3328 if ((c->item_size - framing) % sz != 0)
3331 *n_offsets = (c->item_size - framing) / sz;
3333 where = rindex + framing;
3334 r = message_peek_body(m, &where, 1, *n_offsets * sz, &q);
3340 *offsets = new(size_t, *n_offsets);
3344 for (i = 0; i < *n_offsets; i++) {
3347 x = read_word_le((uint8_t*) q + i * sz, sz);
3348 if (x > c->item_size - sz)
3353 (*offsets)[i] = rindex + x;
3357 *item_size = (*offsets)[0] - rindex;
3362 if (c->enclosing != SD_BUS_TYPE_ARRAY)
3363 c->index += 1 + strlen(contents);
3368 static int bus_message_enter_variant(
3370 struct bus_container *c,
3371 const char *contents,
3372 size_t *item_size) {
3384 if (!signature_is_single(contents, false))
3387 if (*contents == SD_BUS_TYPE_DICT_ENTRY_BEGIN)
3390 if (!c->signature || c->signature[c->index] == 0)
3393 if (c->signature[c->index] != SD_BUS_TYPE_VARIANT)
3398 if (BUS_MESSAGE_IS_GVARIANT(m)) {
3401 k = strlen(contents);
3402 if (1+k > c->item_size)
3405 where = rindex + c->item_size - (1+k);
3406 r = message_peek_body(m, &where, 1, 1+k, &q);
3412 if (*(char*) q != 0)
3415 if (memcmp((uint8_t*) q+1, contents, k))
3418 *item_size = c->item_size - (1+k);
3421 r = message_peek_body(m, &rindex, 1, 1, &q);
3428 r = message_peek_body(m, &rindex, 1, l+1, &q);
3434 if (!validate_signature(q, l))
3437 if (!streq(q, contents))
3443 if (c->enclosing != SD_BUS_TYPE_ARRAY)
3449 static int build_struct_offsets(
3451 const char *signature,
3455 size_t *n_offsets) {
3457 unsigned n_variable = 0, n_total = 0, v;
3458 size_t previous = 0, where;
3470 sz = determine_word_size(size, 0);
3472 /* First, loop over signature and count variable elements and
3473 * elements in general. We use this to know how large the
3474 * offset array is at the end of the structure. Note that
3475 * GVariant only stores offsets for all variable size elements
3476 * that are not the last item. */
3482 r = signature_element_length(p, &n);
3491 r = bus_gvariant_is_fixed_size(t);
3496 if (r == 0 && p[n] != 0) /* except the last item */
3503 if (size < n_variable * sz)
3506 where = m->rindex + size - (n_variable * sz);
3507 r = message_peek_body(m, &where, 1, n_variable * sz, &q);
3515 *offsets = new(size_t, n_total);
3521 /* Second, loop again and build an offset table */
3527 r = signature_element_length(p, &n);
3536 k = bus_gvariant_get_size(t);
3544 x = read_word_le((uint8_t*) q + v*sz, sz);
3547 if (m->rindex + x < previous)
3550 /* The last item's end
3551 * is determined from
3554 x = size - (n_variable * sz);
3556 offset = m->rindex + x;
3562 align = bus_gvariant_get_alignment(t);
3565 offset = (*n_offsets == 0 ? m->rindex : ALIGN_TO((*offsets)[*n_offsets-1], align)) + k;
3569 previous = (*offsets)[(*n_offsets)++] = offset;
3574 assert(*n_offsets == n_total);
3576 *item_size = (*offsets)[0] - m->rindex;
3580 static int enter_struct_or_dict_entry(
3582 struct bus_container *c,
3583 const char *contents,
3586 size_t *n_offsets) {
3597 if (!BUS_MESSAGE_IS_GVARIANT(m)) {
3600 r = message_peek_body(m, &m->rindex, 8, 0, NULL);
3604 } else if (c->item_size <= 0) {
3606 /* gvariant empty struct */
3611 /* gvariant with contents */
3612 return build_struct_offsets(m, contents, c->item_size, item_size, offsets, n_offsets);
3617 static int bus_message_enter_struct(
3619 struct bus_container *c,
3620 const char *contents,
3623 size_t *n_offsets) {
3635 if (!signature_is_valid(contents, false))
3638 if (!c->signature || c->signature[c->index] == 0)
3641 l = strlen(contents);
3643 if (c->signature[c->index] != SD_BUS_TYPE_STRUCT_BEGIN ||
3644 !startswith(c->signature + c->index + 1, contents) ||
3645 c->signature[c->index + 1 + l] != SD_BUS_TYPE_STRUCT_END)
3648 r = enter_struct_or_dict_entry(m, c, contents, item_size, offsets, n_offsets);
3652 if (c->enclosing != SD_BUS_TYPE_ARRAY)
3653 c->index += 1 + l + 1;
3658 static int bus_message_enter_dict_entry(
3660 struct bus_container *c,
3661 const char *contents,
3664 size_t *n_offsets) {
3673 if (!signature_is_pair(contents))
3676 if (c->enclosing != SD_BUS_TYPE_ARRAY)
3679 if (!c->signature || c->signature[c->index] == 0)
3682 l = strlen(contents);
3684 if (c->signature[c->index] != SD_BUS_TYPE_DICT_ENTRY_BEGIN ||
3685 !startswith(c->signature + c->index + 1, contents) ||
3686 c->signature[c->index + 1 + l] != SD_BUS_TYPE_DICT_ENTRY_END)
3689 r = enter_struct_or_dict_entry(m, c, contents, item_size, offsets, n_offsets);
3693 if (c->enclosing != SD_BUS_TYPE_ARRAY)
3694 c->index += 1 + l + 1;
3699 _public_ int sd_bus_message_enter_container(sd_bus_message *m,
3701 const char *contents) {
3702 struct bus_container *c, *w;
3703 uint32_t *array_size = NULL;
3706 size_t *offsets = NULL;
3707 size_t n_offsets = 0, item_size = 0;
3710 assert_return(m, -EINVAL);
3711 assert_return(m->sealed, -EPERM);
3712 assert_return(type != 0 || !contents, -EINVAL);
3714 if (type == 0 || !contents) {
3718 /* Allow entering into anonymous containers */
3719 r = sd_bus_message_peek_type(m, &tt, &cc);
3723 if (type != 0 && type != tt)
3726 if (contents && !streq(contents, cc))
3734 * We enforce a global limit on container depth, that is much
3735 * higher than the 32 structs and 32 arrays the specification
3736 * mandates. This is simpler to implement for us, and we need
3737 * this only to ensure our container array doesn't grow
3738 * without bounds. We are happy to return any data from a
3739 * message as long as the data itself is valid, even if the
3740 * overall message might be not.
3742 * Note that the message signature is validated when
3743 * parsing the headers, and that validation does check the
3746 * Note that the specification defines no limits on the depth
3747 * of stacked variants, but we do.
3749 if (m->n_containers >= BUS_CONTAINER_DEPTH)
3752 w = realloc(m->containers, sizeof(struct bus_container) * (m->n_containers + 1));
3757 if (message_end_of_signature(m))
3760 if (message_end_of_array(m, m->rindex))
3763 c = message_get_container(m);
3765 signature = strdup(contents);
3769 c->saved_index = c->index;
3772 if (type == SD_BUS_TYPE_ARRAY)
3773 r = bus_message_enter_array(m, c, contents, &array_size, &item_size, &offsets, &n_offsets);
3774 else if (type == SD_BUS_TYPE_VARIANT)
3775 r = bus_message_enter_variant(m, c, contents, &item_size);
3776 else if (type == SD_BUS_TYPE_STRUCT)
3777 r = bus_message_enter_struct(m, c, contents, &item_size, &offsets, &n_offsets);
3778 else if (type == SD_BUS_TYPE_DICT_ENTRY)
3779 r = bus_message_enter_dict_entry(m, c, contents, &item_size, &offsets, &n_offsets);
3789 /* OK, let's fill it in */
3790 w += m->n_containers++;
3791 w->enclosing = type;
3792 w->signature = signature;
3796 w->begin = m->rindex;
3797 w->end = m->rindex + c->item_size;
3799 w->array_size = array_size;
3800 w->item_size = item_size;
3801 w->offsets = offsets;
3802 w->n_offsets = n_offsets;
3803 w->offset_index = 0;
3808 _public_ int sd_bus_message_exit_container(sd_bus_message *m) {
3809 struct bus_container *c;
3812 assert_return(m, -EINVAL);
3813 assert_return(m->sealed, -EPERM);
3814 assert_return(m->n_containers > 0, -ENXIO);
3816 c = message_get_container(m);
3818 if (c->enclosing != SD_BUS_TYPE_ARRAY) {
3819 if (c->signature && c->signature[c->index] != 0)
3823 if (BUS_MESSAGE_IS_GVARIANT(m)) {
3824 if (m->rindex < c->end)
3827 } else if (c->enclosing == SD_BUS_TYPE_ARRAY) {
3830 l = BUS_MESSAGE_BSWAP32(m, *c->array_size);
3831 if (c->begin + l != m->rindex)
3839 c = message_get_container(m);
3841 r = container_next_item(m, c, &m->rindex);
3848 static void message_quit_container(sd_bus_message *m) {
3849 struct bus_container *c;
3853 assert(m->n_containers > 0);
3855 c = message_get_container(m);
3858 assert(m->rindex >= c->before);
3859 m->rindex = c->before;
3861 /* Free container */
3866 /* Correct index of new top-level container */
3867 c = message_get_container(m);
3868 c->index = c->saved_index;
3871 _public_ int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char **contents) {
3872 struct bus_container *c;
3875 assert_return(m, -EINVAL);
3876 assert_return(m->sealed, -EPERM);
3878 if (message_end_of_signature(m))
3881 if (message_end_of_array(m, m->rindex))
3884 c = message_get_container(m);
3886 if (bus_type_is_basic(c->signature[c->index])) {
3890 *type = c->signature[c->index];
3894 if (c->signature[c->index] == SD_BUS_TYPE_ARRAY) {
3900 r = signature_element_length(c->signature+c->index+1, &l);
3906 sig = strndup(c->signature + c->index + 1, l);
3910 free(m->peeked_signature);
3911 m->peeked_signature = sig;
3917 *type = SD_BUS_TYPE_ARRAY;
3922 if (c->signature[c->index] == SD_BUS_TYPE_STRUCT_BEGIN ||
3923 c->signature[c->index] == SD_BUS_TYPE_DICT_ENTRY_BEGIN) {
3929 r = signature_element_length(c->signature+c->index, &l);
3934 sig = strndup(c->signature + c->index + 1, l - 2);
3938 free(m->peeked_signature);
3939 m->peeked_signature = sig;
3945 *type = c->signature[c->index] == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY;
3950 if (c->signature[c->index] == SD_BUS_TYPE_VARIANT) {
3954 if (BUS_MESSAGE_IS_GVARIANT(m)) {
3957 if (c->item_size < 2)
3960 /* Look for the NUL delimiter that
3961 separates the payload from the
3962 signature. Since the body might be
3963 in a different part that then the
3964 signature we map byte by byte. */
3966 for (k = 2; k <= c->item_size; k++) {
3969 where = m->rindex + c->item_size - k;
3970 r = message_peek_body(m, &where, 1, k, &q);
3976 if (*(char*) q == 0)
3980 if (k > c->item_size)
3983 free(m->peeked_signature);
3984 m->peeked_signature = strndup((char*) q + 1, k - 1);
3985 if (!m->peeked_signature)
3988 if (!signature_is_valid(m->peeked_signature, true))
3991 *contents = m->peeked_signature;
3996 r = message_peek_body(m, &rindex, 1, 1, &q);
4003 r = message_peek_body(m, &rindex, 1, l+1, &q);
4009 if (!validate_signature(q, l))
4017 *type = SD_BUS_TYPE_VARIANT;
4032 _public_ int sd_bus_message_rewind(sd_bus_message *m, int complete) {
4033 struct bus_container *c;
4035 assert_return(m, -EINVAL);
4036 assert_return(m->sealed, -EPERM);
4039 message_reset_containers(m);
4042 c = message_get_container(m);
4044 c = message_get_container(m);
4046 c->offset_index = 0;
4048 m->rindex = c->begin;
4051 return !isempty(c->signature);
4054 static int message_read_ap(
4059 unsigned n_array, n_struct;
4060 TypeStack stack[BUS_CONTAINER_DEPTH];
4061 unsigned stack_ptr = 0;
4062 unsigned n_loop = 0;
4070 /* Ideally, we'd just call ourselves recursively on every
4071 * complex type. However, the state of a va_list that is
4072 * passed to a function is undefined after that function
4073 * returns. This means we need to docode the va_list linearly
4074 * in a single stackframe. We hence implement our own
4075 * home-grown stack in an array. */
4077 n_array = (unsigned) -1; /* lenght of current array entries */
4078 n_struct = strlen(types); /* length of current struct contents signature */
4085 if (n_array == 0 || (n_array == (unsigned) -1 && n_struct == 0)) {
4086 r = type_stack_pop(stack, ELEMENTSOF(stack), &stack_ptr, &types, &n_struct, &n_array);
4092 r = sd_bus_message_exit_container(m);
4100 if (n_array != (unsigned) -1)
4109 case SD_BUS_TYPE_BYTE:
4110 case SD_BUS_TYPE_BOOLEAN:
4111 case SD_BUS_TYPE_INT16:
4112 case SD_BUS_TYPE_UINT16:
4113 case SD_BUS_TYPE_INT32:
4114 case SD_BUS_TYPE_UINT32:
4115 case SD_BUS_TYPE_INT64:
4116 case SD_BUS_TYPE_UINT64:
4117 case SD_BUS_TYPE_DOUBLE:
4118 case SD_BUS_TYPE_STRING:
4119 case SD_BUS_TYPE_OBJECT_PATH:
4120 case SD_BUS_TYPE_SIGNATURE:
4121 case SD_BUS_TYPE_UNIX_FD: {
4124 p = va_arg(ap, void*);
4125 r = sd_bus_message_read_basic(m, *t, p);
4138 case SD_BUS_TYPE_ARRAY: {
4141 r = signature_element_length(t + 1, &k);
4147 memcpy(s, t + 1, k);
4150 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, s);
4161 if (n_array == (unsigned) -1) {
4166 r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
4172 n_array = va_arg(ap, unsigned);
4177 case SD_BUS_TYPE_VARIANT: {
4180 s = va_arg(ap, const char *);
4184 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, s);
4194 r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
4199 n_struct = strlen(s);
4200 n_array = (unsigned) -1;
4205 case SD_BUS_TYPE_STRUCT_BEGIN:
4206 case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
4209 r = signature_element_length(t, &k);
4215 memcpy(s, t + 1, k - 2);
4218 r = sd_bus_message_enter_container(m, *t == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s);
4228 if (n_array == (unsigned) -1) {
4233 r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
4239 n_array = (unsigned) -1;
4252 _public_ int sd_bus_message_read(sd_bus_message *m, const char *types, ...) {
4256 assert_return(m, -EINVAL);
4257 assert_return(m->sealed, -EPERM);
4258 assert_return(types, -EINVAL);
4260 va_start(ap, types);
4261 r = message_read_ap(m, types, ap);
4267 _public_ int sd_bus_message_skip(sd_bus_message *m, const char *types) {
4270 assert_return(m, -EINVAL);
4271 assert_return(m->sealed, -EPERM);
4272 assert_return(types, -EINVAL);
4279 case SD_BUS_TYPE_BYTE:
4280 case SD_BUS_TYPE_BOOLEAN:
4281 case SD_BUS_TYPE_INT16:
4282 case SD_BUS_TYPE_UINT16:
4283 case SD_BUS_TYPE_INT32:
4284 case SD_BUS_TYPE_UINT32:
4285 case SD_BUS_TYPE_INT64:
4286 case SD_BUS_TYPE_UINT64:
4287 case SD_BUS_TYPE_DOUBLE:
4288 case SD_BUS_TYPE_STRING:
4289 case SD_BUS_TYPE_OBJECT_PATH:
4290 case SD_BUS_TYPE_SIGNATURE:
4291 case SD_BUS_TYPE_UNIX_FD:
4293 r = sd_bus_message_read_basic(m, *types, NULL);
4297 r = sd_bus_message_skip(m, types + 1);
4303 case SD_BUS_TYPE_ARRAY: {
4306 r = signature_element_length(types + 1, &k);
4312 memcpy(s, types+1, k);
4315 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, s);
4320 r = sd_bus_message_skip(m, s);
4327 r = sd_bus_message_exit_container(m);
4332 r = sd_bus_message_skip(m, types + 1 + k);
4339 case SD_BUS_TYPE_VARIANT: {
4340 const char *contents;
4343 r = sd_bus_message_peek_type(m, &x, &contents);
4347 if (x != SD_BUS_TYPE_VARIANT)
4350 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents);
4354 r = sd_bus_message_skip(m, contents);
4359 r = sd_bus_message_exit_container(m);
4363 r = sd_bus_message_skip(m, types + 1);
4370 case SD_BUS_TYPE_STRUCT_BEGIN:
4371 case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
4374 r = signature_element_length(types, &k);
4380 memcpy(s, types+1, k-2);
4383 r = sd_bus_message_enter_container(m, *types == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s);
4387 r = sd_bus_message_skip(m, s);
4392 r = sd_bus_message_exit_container(m);
4397 r = sd_bus_message_skip(m, types + k);
4409 _public_ int sd_bus_message_read_array(sd_bus_message *m,
4413 struct bus_container *c;
4419 assert_return(m, -EINVAL);
4420 assert_return(m->sealed, -EPERM);
4421 assert_return(bus_type_is_trivial(type), -EINVAL);
4422 assert_return(ptr, -EINVAL);
4423 assert_return(size, -EINVAL);
4424 assert_return(!BUS_MESSAGE_NEED_BSWAP(m), -ENOTSUP);
4426 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, CHAR_TO_STR(type));
4430 c = message_get_container(m);
4432 if (BUS_MESSAGE_IS_GVARIANT(m)) {
4433 align = bus_gvariant_get_alignment(CHAR_TO_STR(type));
4439 align = bus_type_get_alignment(type);
4443 sz = BUS_MESSAGE_BSWAP32(m, *c->array_size);
4447 /* Zero length array, let's return some aligned
4448 * pointer that is not NULL */
4449 p = (uint8_t*) NULL + align;
4451 r = message_peek_body(m, &m->rindex, align, sz, &p);
4460 r = sd_bus_message_exit_container(m);
4464 *ptr = (const void*) p;
4470 message_quit_container(m);
4474 static int message_peek_fields(
4485 return buffer_peek(BUS_MESSAGE_FIELDS(m), BUS_MESSAGE_FIELDS_SIZE(m), rindex, align, nbytes, ret);
4488 static int message_peek_field_uint32(
4500 if (BUS_MESSAGE_IS_GVARIANT(m) && item_size != 4)
4503 /* identical for gvariant and dbus1 */
4505 r = message_peek_fields(m, ri, 4, 4, &q);
4510 *ret = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
4515 static int message_peek_field_string(
4517 bool (*validate)(const char *p),
4529 if (BUS_MESSAGE_IS_GVARIANT(m)) {
4534 r = message_peek_fields(m, ri, 1, item_size, &q);
4540 r = message_peek_field_uint32(m, ri, 4, &l);
4544 r = message_peek_fields(m, ri, 1, l+1, &q);
4550 if (!validate_nul(q, l))
4556 if (!validate_string(q, l))
4566 static int message_peek_field_signature(
4579 if (BUS_MESSAGE_IS_GVARIANT(m)) {
4584 r = message_peek_fields(m, ri, 1, item_size, &q);
4590 r = message_peek_fields(m, ri, 1, 1, &q);
4595 r = message_peek_fields(m, ri, 1, l+1, &q);
4600 if (!validate_signature(q, l))
4609 static int message_skip_fields(
4612 uint32_t array_size,
4613 const char **signature) {
4615 size_t original_index;
4621 assert(!BUS_MESSAGE_IS_GVARIANT(m));
4623 original_index = *ri;
4629 if (array_size != (uint32_t) -1 &&
4630 array_size <= *ri - original_index)
4637 if (t == SD_BUS_TYPE_STRING) {
4639 r = message_peek_field_string(m, NULL, ri, 0, NULL);
4645 } else if (t == SD_BUS_TYPE_OBJECT_PATH) {
4647 r = message_peek_field_string(m, object_path_is_valid, ri, 0, NULL);
4653 } else if (t == SD_BUS_TYPE_SIGNATURE) {
4655 r = message_peek_field_signature(m, ri, 0, NULL);
4661 } else if (bus_type_is_basic(t)) {
4664 align = bus_type_get_alignment(t);
4665 k = bus_type_get_size(t);
4666 assert(align > 0 && k > 0);
4668 r = message_peek_fields(m, ri, align, k, NULL);
4674 } else if (t == SD_BUS_TYPE_ARRAY) {
4676 r = signature_element_length(*signature+1, &l);
4686 strncpy(sig, *signature + 1, l-1);
4689 alignment = bus_type_get_alignment(sig[0]);
4693 r = message_peek_field_uint32(m, ri, 0, &nas);
4696 if (nas > BUS_ARRAY_MAX_SIZE)
4699 r = message_peek_fields(m, ri, alignment, 0, NULL);
4703 r = message_skip_fields(m, ri, nas, (const char**) &s);
4708 (*signature) += 1 + l;
4710 } else if (t == SD_BUS_TYPE_VARIANT) {
4713 r = message_peek_field_signature(m, ri, 0, &s);
4717 r = message_skip_fields(m, ri, (uint32_t) -1, (const char**) &s);
4723 } else if (t == SD_BUS_TYPE_STRUCT ||
4724 t == SD_BUS_TYPE_DICT_ENTRY) {
4726 r = signature_element_length(*signature, &l);
4733 strncpy(sig, *signature + 1, l-1);
4736 r = message_skip_fields(m, ri, (uint32_t) -1, (const char**) &s);
4747 int bus_message_parse_fields(sd_bus_message *m) {
4750 uint32_t unix_fds = 0;
4751 void *offsets = NULL;
4752 unsigned n_offsets = 0;
4758 if (BUS_MESSAGE_IS_GVARIANT(m)) {
4761 sz = determine_word_size(BUS_MESSAGE_FIELDS_SIZE(m), 0);
4765 ri = BUS_MESSAGE_FIELDS_SIZE(m) - sz;
4766 r = message_peek_fields(m, &ri, 1, sz, &q);
4770 framing = read_word_le(q, sz);
4771 if (framing >= BUS_MESSAGE_FIELDS_SIZE(m) - sz)
4773 if ((BUS_MESSAGE_FIELDS_SIZE(m) - framing) % sz != 0)
4777 r = message_peek_fields(m, &ri, 1, BUS_MESSAGE_FIELDS_SIZE(m) - framing, &offsets);
4781 n_offsets = (BUS_MESSAGE_FIELDS_SIZE(m) - framing) / sz;
4786 while (ri < BUS_MESSAGE_FIELDS_SIZE(m)) {
4787 _cleanup_free_ char *sig = NULL;
4788 const char *signature;
4790 size_t item_size = (size_t) -1;
4792 if (BUS_MESSAGE_IS_GVARIANT(m)) {
4799 ri = ALIGN_TO(read_word_le((uint8_t*) offsets + (i-1)*sz, sz), 8);
4802 r = message_peek_fields(m, &ri, 8, 1, (void**) &header);
4806 if (BUS_MESSAGE_IS_GVARIANT(m)) {
4811 end = read_word_le((uint8_t*) offsets + i*sz, sz);
4816 where = ri = ALIGN_TO(ri, 8);
4817 item_size = end - ri;
4818 r = message_peek_fields(m, &where, 1, item_size, &q);
4822 b = memrchr(q, 0, item_size);
4826 sig = strndup(b+1, item_size - (b+1-(char*) q));
4831 item_size = b - (char*) q;
4833 r = message_peek_field_signature(m, &ri, 0, &signature);
4839 case _BUS_MESSAGE_HEADER_INVALID:
4842 case BUS_MESSAGE_HEADER_PATH:
4847 if (!streq(signature, "o"))
4850 r = message_peek_field_string(m, object_path_is_valid, &ri, item_size, &m->path);
4853 case BUS_MESSAGE_HEADER_INTERFACE:
4858 if (!streq(signature, "s"))
4861 r = message_peek_field_string(m, interface_name_is_valid, &ri, item_size, &m->interface);
4864 case BUS_MESSAGE_HEADER_MEMBER:
4869 if (!streq(signature, "s"))
4872 r = message_peek_field_string(m, member_name_is_valid, &ri, item_size, &m->member);
4875 case BUS_MESSAGE_HEADER_ERROR_NAME:
4880 if (!streq(signature, "s"))
4883 r = message_peek_field_string(m, error_name_is_valid, &ri, item_size, &m->error.name);
4885 m->error._need_free = -1;
4889 case BUS_MESSAGE_HEADER_DESTINATION:
4894 if (!streq(signature, "s"))
4897 r = message_peek_field_string(m, service_name_is_valid, &ri, item_size, &m->destination);
4900 case BUS_MESSAGE_HEADER_SENDER:
4905 if (!streq(signature, "s"))
4908 r = message_peek_field_string(m, service_name_is_valid, &ri, item_size, &m->sender);
4910 if (r >= 0 && m->sender[0] == ':' && m->bus && m->bus->bus_client && !m->bus->is_kernel) {
4911 m->creds.unique_name = (char*) m->sender;
4912 m->creds.mask |= SD_BUS_CREDS_UNIQUE_NAME & m->bus->creds_mask;
4918 case BUS_MESSAGE_HEADER_SIGNATURE: {
4922 if (m->root_container.signature)
4925 if (!streq(signature, "g"))
4928 r = message_peek_field_signature(m, &ri, item_size, &s);
4936 free(m->root_container.signature);
4937 m->root_container.signature = c;
4941 case BUS_MESSAGE_HEADER_REPLY_SERIAL:
4942 if (m->reply_serial != 0)
4945 if (!streq(signature, "u"))
4948 r = message_peek_field_uint32(m, &ri, item_size, &m->reply_serial);
4952 if (m->reply_serial == 0)
4957 case BUS_MESSAGE_HEADER_UNIX_FDS:
4961 if (!streq(signature, "u"))
4964 r = message_peek_field_uint32(m, &ri, item_size, &unix_fds);
4974 if (!BUS_MESSAGE_IS_GVARIANT(m))
4975 r = message_skip_fields(m, &ri, (uint32_t) -1, (const char **) &signature);
4984 if (m->n_fds != unix_fds)
4987 if (isempty(m->root_container.signature) != (BUS_MESSAGE_BODY_SIZE(m) == 0))
4990 switch (m->header->type) {
4992 case SD_BUS_MESSAGE_SIGNAL:
4993 if (!m->path || !m->interface || !m->member)
4997 case SD_BUS_MESSAGE_METHOD_CALL:
4999 if (!m->path || !m->member)
5004 case SD_BUS_MESSAGE_METHOD_RETURN:
5006 if (m->reply_serial == 0)
5010 case SD_BUS_MESSAGE_METHOD_ERROR:
5012 if (m->reply_serial == 0 || !m->error.name)
5017 m->root_container.end = BUS_MESSAGE_BODY_SIZE(m);
5019 if (BUS_MESSAGE_IS_GVARIANT(m)) {
5020 r = build_struct_offsets(
5022 m->root_container.signature,
5023 BUS_MESSAGE_BODY_SIZE(m),
5024 &m->root_container.item_size,
5025 &m->root_container.offsets,
5026 &m->root_container.n_offsets);
5031 /* Try to read the error message, but if we can't it's a non-issue */
5032 if (m->header->type == SD_BUS_MESSAGE_METHOD_ERROR)
5033 sd_bus_message_read(m, "s", &m->error.message);
5038 static int bus_message_close_header(sd_bus_message *m) {
5044 if (!BUS_MESSAGE_IS_GVARIANT(m))
5047 if (m->n_header_offsets < 1)
5050 assert(m->header->fields_size == m->header_offsets[m->n_header_offsets-1]);
5052 sz = determine_word_size(m->header->fields_size, m->n_header_offsets);
5054 a = message_extend_fields(m, 1, sz * m->n_header_offsets, false);
5058 for (i = 0; i < m->n_header_offsets; i++)
5059 write_word_le(a + sz*i, sz, m->header_offsets[i]);
5064 int bus_message_seal(sd_bus_message *m, uint64_t serial) {
5065 struct bus_body_part *part;
5075 if (m->n_containers > 0)
5081 /* In vtables the return signature of method calls is listed,
5082 * let's check if they match if this is a response */
5083 if (m->header->type == SD_BUS_MESSAGE_METHOD_RETURN &&
5084 m->enforced_reply_signature &&
5085 !streq(strempty(m->root_container.signature), m->enforced_reply_signature))
5088 /* If gvariant marshalling is used we need to close the body structure */
5089 r = bus_message_close_struct(m, &m->root_container, false);
5093 /* If there's a non-trivial signature set, then add it in here */
5094 if (!isempty(m->root_container.signature)) {
5095 r = message_append_field_signature(m, BUS_MESSAGE_HEADER_SIGNATURE, m->root_container.signature, NULL);
5101 r = message_append_field_uint32(m, BUS_MESSAGE_HEADER_UNIX_FDS, m->n_fds);
5106 r = bus_message_close_header(m);
5110 m->header->serial = serial;
5112 /* Add padding at the end of the fields part, since we know
5113 * the body needs to start at an 8 byte alignment. We made
5114 * sure we allocated enough space for this, so all we need to
5115 * do here is to zero it out. */
5116 l = BUS_MESSAGE_FIELDS_SIZE(m);
5119 memset((uint8_t*) BUS_MESSAGE_FIELDS(m) + l, 0, a);
5121 /* If this is something we can send as memfd, then let's seal
5122 the memfd now. Note that we can send memfds as payload only
5123 for directed messages, and not for broadcasts. */
5124 if (m->destination && m->bus && m->bus->use_memfd) {
5125 MESSAGE_FOREACH_PART(part, i, m)
5126 if (part->memfd >= 0 && !part->sealed && (part->size > MEMFD_MIN_SIZE || m->bus->use_memfd < 0)) {
5127 bus_body_part_unmap(part);
5129 if (ioctl(part->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 1) >= 0)
5130 part->sealed = true;
5134 m->root_container.end = BUS_MESSAGE_BODY_SIZE(m);
5135 m->root_container.index = 0;
5136 m->root_container.offset_index = 0;
5137 m->root_container.item_size = m->root_container.n_offsets > 0 ? m->root_container.offsets[0] : 0;
5144 _public_ int sd_bus_message_set_destination(sd_bus_message *m, const char *destination) {
5145 assert_return(m, -EINVAL);
5146 assert_return(destination, -EINVAL);
5147 assert_return(!m->sealed, -EPERM);
5148 assert_return(!m->destination, -EEXIST);
5150 return message_append_field_string(m, BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &m->destination);
5153 int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz) {
5157 struct bus_body_part *part;
5163 total = BUS_MESSAGE_SIZE(m);
5169 e = mempcpy(p, m->header, BUS_MESSAGE_BODY_BEGIN(m));
5170 MESSAGE_FOREACH_PART(part, i, m)
5171 e = mempcpy(e, part->data, part->size);
5173 assert(total == (size_t) ((uint8_t*) e - (uint8_t*) p));
5181 int bus_message_read_strv_extend(sd_bus_message *m, char ***l) {
5187 r = sd_bus_message_enter_container(m, 'a', "s");
5194 r = sd_bus_message_read_basic(m, 's', &s);
5200 r = strv_extend(l, s);
5205 r = sd_bus_message_exit_container(m);
5212 _public_ int sd_bus_message_read_strv(sd_bus_message *m, char ***l) {
5216 assert_return(m, -EINVAL);
5217 assert_return(m->sealed, -EPERM);
5218 assert_return(l, -EINVAL);
5220 r = bus_message_read_strv_extend(m, &strv);
5230 const char* bus_message_get_arg(sd_bus_message *m, unsigned i) {
5232 const char *t = NULL;
5237 r = sd_bus_message_rewind(m, true);
5241 for (j = 0; j <= i; j++) {
5244 r = sd_bus_message_peek_type(m, &type, NULL);
5248 if (type != SD_BUS_TYPE_STRING &&
5249 type != SD_BUS_TYPE_OBJECT_PATH &&
5250 type != SD_BUS_TYPE_SIGNATURE)
5253 r = sd_bus_message_read_basic(m, type, &t);
5261 bool bus_header_is_complete(struct bus_header *h, size_t size) {
5267 if (size < sizeof(struct bus_header))
5270 full = sizeof(struct bus_header) +
5271 (h->endian == BUS_NATIVE_ENDIAN ? h->fields_size : bswap_32(h->fields_size));
5273 return size >= full;
5276 int bus_header_message_size(struct bus_header *h, size_t *sum) {
5282 if (h->endian == BUS_NATIVE_ENDIAN) {
5283 fs = h->fields_size;
5285 } else if (h->endian == BUS_REVERSE_ENDIAN) {
5286 fs = bswap_32(h->fields_size);
5287 bs = bswap_32(h->body_size);
5291 *sum = sizeof(struct bus_header) + ALIGN8(fs) + bs;
5295 _public_ int sd_bus_message_get_errno(sd_bus_message *m) {
5296 assert_return(m, -EINVAL);
5298 if (m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
5301 return sd_bus_error_get_errno(&m->error);
5304 _public_ const char* sd_bus_message_get_signature(sd_bus_message *m, int complete) {
5305 struct bus_container *c;
5307 assert_return(m, NULL);
5309 c = complete ? &m->root_container : message_get_container(m);
5310 return c->signature ?: "";
5313 _public_ int sd_bus_message_copy(sd_bus_message *m, sd_bus_message *source, int all) {
5314 bool done_something = false;
5317 assert_return(m, -EINVAL);
5318 assert_return(source, -EINVAL);
5319 assert_return(!m->sealed, -EPERM);
5320 assert_return(source->sealed, -EPERM);
5323 const char *contents;
5338 r = sd_bus_message_peek_type(source, &type, &contents);
5344 done_something = true;
5346 if (bus_type_is_container(type) > 0) {
5348 r = sd_bus_message_enter_container(source, type, contents);
5352 r = sd_bus_message_open_container(m, type, contents);
5356 r = sd_bus_message_copy(m, source, true);
5360 r = sd_bus_message_close_container(m);
5364 r = sd_bus_message_exit_container(source);
5371 r = sd_bus_message_read_basic(source, type, &basic);
5377 if (type == SD_BUS_TYPE_OBJECT_PATH ||
5378 type == SD_BUS_TYPE_SIGNATURE ||
5379 type == SD_BUS_TYPE_STRING)
5380 r = sd_bus_message_append_basic(m, type, basic.string);
5382 r = sd_bus_message_append_basic(m, type, &basic);
5389 return done_something;
5392 _public_ int sd_bus_message_verify_type(sd_bus_message *m, char type, const char *contents) {
5397 assert_return(m, -EINVAL);
5398 assert_return(m->sealed, -EPERM);
5399 assert_return(!type || bus_type_is_valid(type), -EINVAL);
5400 assert_return(!contents || signature_is_valid(contents, true), -EINVAL);
5401 assert_return(type || contents, -EINVAL);
5402 assert_return(!contents || !type || bus_type_is_container(type), -EINVAL);
5404 r = sd_bus_message_peek_type(m, &t, &c);
5408 if (type != 0 && type != t)
5411 if (contents && !streq_ptr(contents, c))
5417 _public_ sd_bus *sd_bus_message_get_bus(sd_bus_message *m) {
5418 assert_return(m, NULL);