1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "alloc-util.h"
10 #include "bus-gvariant.h"
11 #include "bus-internal.h"
12 #include "bus-message.h"
13 #include "bus-signature.h"
18 #include "memfd-util.h"
19 #include "memory-util.h"
20 #include "string-util.h"
22 #include "time-util.h"
25 static int message_append_basic(sd_bus_message
*m
, char type
, const void *p
, const void **stored
);
27 static void *adjust_pointer(const void *p
, void *old_base
, size_t sz
, void *new_base
) {
32 if (old_base
== new_base
)
35 if ((uint8_t*) p
< (uint8_t*) old_base
)
38 if ((uint8_t*) p
>= (uint8_t*) old_base
+ sz
)
41 return (uint8_t*) new_base
+ ((uint8_t*) p
- (uint8_t*) old_base
);
44 static void message_free_part(sd_bus_message
*m
, struct bus_body_part
*part
) {
48 if (part
->memfd
>= 0) {
49 /* erase if requested, but only if the memfd is not sealed yet, i.e. is writable */
50 if (m
->sensitive
&& !m
->sealed
)
51 explicit_bzero_safe(part
->data
, part
->size
);
53 close_and_munmap(part
->memfd
, part
->mmap_begin
, part
->mapped
);
54 } else if (part
->munmap_this
)
55 /* We don't erase sensitive data here, since the data is memory mapped from someone else, and
56 * we just don't know if it's OK to write to it */
57 munmap(part
->mmap_begin
, part
->mapped
);
59 /* Erase this if that is requested. Since this is regular memory we know we can write it. */
61 explicit_bzero_safe(part
->data
, part
->size
);
71 static void message_reset_parts(sd_bus_message
*m
) {
72 struct bus_body_part
*part
;
77 while (m
->n_body_parts
> 0) {
78 struct bus_body_part
*next
= part
->next
;
79 message_free_part(m
, part
);
86 m
->cached_rindex_part
= NULL
;
87 m
->cached_rindex_part_begin
= 0;
90 static struct bus_container
*message_get_last_container(sd_bus_message
*m
) {
93 if (m
->n_containers
== 0)
94 return &m
->root_container
;
96 assert(m
->containers
);
97 return m
->containers
+ m
->n_containers
- 1;
100 static void message_free_last_container(sd_bus_message
*m
) {
101 struct bus_container
*c
;
103 c
= message_get_last_container(m
);
106 free(c
->peeked_signature
);
109 /* Move to previous container, but not if we are on root container */
110 if (m
->n_containers
> 0)
114 static void message_reset_containers(sd_bus_message
*m
) {
117 while (m
->n_containers
> 0)
118 message_free_last_container(m
);
120 m
->containers
= mfree(m
->containers
);
121 m
->containers_allocated
= 0;
122 m
->root_container
.index
= 0;
125 static sd_bus_message
* message_free(sd_bus_message
*m
) {
128 message_reset_parts(m
);
133 /* Note that we don't unref m->bus here. That's already done by sd_bus_message_unref() as each user
134 * reference to the bus message also is considered a reference to the bus connection itself. */
137 close_many(m
->fds
, m
->n_fds
);
141 if (m
->iovec
!= m
->iovec_fixed
)
144 message_reset_containers(m
);
145 assert(m
->n_containers
== 0);
146 message_free_last_container(m
);
148 bus_creds_done(&m
->creds
);
152 static void *message_extend_fields(sd_bus_message
*m
, size_t align
, size_t sz
, bool add_offset
) {
154 size_t old_size
, new_size
, start
;
161 old_size
= sizeof(struct bus_header
) + m
->fields_size
;
162 start
= ALIGN_TO(old_size
, align
);
163 new_size
= start
+ sz
;
165 if (new_size
< start
||
166 new_size
> (size_t) ((uint32_t) -1))
169 if (old_size
== new_size
)
170 return (uint8_t*) m
->header
+ old_size
;
172 if (m
->free_header
) {
173 np
= realloc(m
->header
, ALIGN8(new_size
));
177 /* Initially, the header is allocated as part of
178 * the sd_bus_message itself, let's replace it by
181 np
= malloc(ALIGN8(new_size
));
185 memcpy(np
, m
->header
, sizeof(struct bus_header
));
188 /* Zero out padding */
189 if (start
> old_size
)
190 memzero((uint8_t*) np
+ old_size
, start
- old_size
);
194 m
->fields_size
= new_size
- sizeof(struct bus_header
);
196 /* Adjust quick access pointers */
197 m
->path
= adjust_pointer(m
->path
, op
, old_size
, m
->header
);
198 m
->interface
= adjust_pointer(m
->interface
, op
, old_size
, m
->header
);
199 m
->member
= adjust_pointer(m
->member
, op
, old_size
, m
->header
);
200 m
->destination
= adjust_pointer(m
->destination
, op
, old_size
, m
->header
);
201 m
->sender
= adjust_pointer(m
->sender
, op
, old_size
, m
->header
);
202 m
->error
.name
= adjust_pointer(m
->error
.name
, op
, old_size
, m
->header
);
204 m
->free_header
= true;
207 if (m
->n_header_offsets
>= ELEMENTSOF(m
->header_offsets
))
210 m
->header_offsets
[m
->n_header_offsets
++] = new_size
- sizeof(struct bus_header
);
213 return (uint8_t*) np
+ start
;
220 static int message_append_field_string(
232 /* dbus1 only allows 8bit header field ids */
236 /* dbus1 doesn't allow strings over 32bit, let's enforce this
237 * globally, to not risk convertability */
242 /* Signature "(yv)" where the variant contains "s" */
244 if (BUS_MESSAGE_IS_GVARIANT(m
)) {
246 /* (field id 64bit, ((string + NUL) + NUL + signature string 's') */
247 p
= message_extend_fields(m
, 8, 8 + l
+ 1 + 1 + 1, true);
251 *((uint64_t*) p
) = h
;
258 *ret
= (char*) p
+ 8;
261 /* (field id byte + (signature length + signature 's' + NUL) + (string length + string + NUL)) */
262 p
= message_extend_fields(m
, 8, 4 + 4 + l
+ 1, false);
271 ((uint32_t*) p
)[1] = l
;
272 memcpy(p
+ 8, s
, l
+ 1);
275 *ret
= (char*) p
+ 8;
281 static int message_append_field_signature(
292 /* dbus1 only allows 8bit header field ids */
296 /* dbus1 doesn't allow signatures over 8bit, let's enforce
297 * this globally, to not risk convertability */
299 if (l
> SD_BUS_MAXIMUM_SIGNATURE_LENGTH
)
302 /* Signature "(yv)" where the variant contains "g" */
304 if (BUS_MESSAGE_IS_GVARIANT(m
))
305 /* For gvariant the serialization is the same as for normal strings */
306 return message_append_field_string(m
, h
, 'g', s
, ret
);
308 /* (field id byte + (signature length + signature 'g' + NUL) + (string length + string + NUL)) */
309 p
= message_extend_fields(m
, 8, 4 + 1 + l
+ 1, false);
315 p
[2] = SD_BUS_TYPE_SIGNATURE
;
318 memcpy(p
+ 5, s
, l
+ 1);
321 *ret
= (const char*) p
+ 5;
327 static int message_append_field_uint32(sd_bus_message
*m
, uint64_t h
, uint32_t x
) {
332 /* dbus1 only allows 8bit header field ids */
336 if (BUS_MESSAGE_IS_GVARIANT(m
)) {
337 /* (field id 64bit + ((value + NUL + signature string 'u') */
339 p
= message_extend_fields(m
, 8, 8 + 4 + 1 + 1, true);
343 *((uint64_t*) p
) = h
;
344 *((uint32_t*) (p
+ 8)) = x
;
348 /* (field id byte + (signature length + signature 'u' + NUL) + value) */
349 p
= message_extend_fields(m
, 8, 4 + 4, false);
358 ((uint32_t*) p
)[1] = x
;
364 static int message_append_field_uint64(sd_bus_message
*m
, uint64_t h
, uint64_t x
) {
369 /* dbus1 only allows 8bit header field ids */
373 if (BUS_MESSAGE_IS_GVARIANT(m
)) {
374 /* (field id 64bit + ((value + NUL + signature string 't') */
376 p
= message_extend_fields(m
, 8, 8 + 8 + 1 + 1, true);
380 *((uint64_t*) p
) = h
;
381 *((uint64_t*) (p
+ 8)) = x
;
385 /* (field id byte + (signature length + signature 't' + NUL) + 4 byte padding + value) */
386 p
= message_extend_fields(m
, 8, 4 + 4 + 8, false);
399 ((uint64_t*) p
)[1] = x
;
405 static int message_append_reply_cookie(sd_bus_message
*m
, uint64_t cookie
) {
408 if (BUS_MESSAGE_IS_GVARIANT(m
))
409 return message_append_field_uint64(m
, BUS_MESSAGE_HEADER_REPLY_SERIAL
, cookie
);
411 /* 64bit cookies are not supported on dbus1 */
412 if (cookie
> 0xffffffffUL
)
415 return message_append_field_uint32(m
, BUS_MESSAGE_HEADER_REPLY_SERIAL
, (uint32_t) cookie
);
419 int bus_message_from_header(
422 size_t header_accessible
,
424 size_t footer_accessible
,
430 sd_bus_message
**ret
) {
432 _cleanup_free_ sd_bus_message
*m
= NULL
;
433 struct bus_header
*h
;
437 assert(header
|| header_accessible
<= 0);
438 assert(footer
|| footer_accessible
<= 0);
439 assert(fds
|| n_fds
<= 0);
442 if (header_accessible
< sizeof(struct bus_header
))
445 if (header_accessible
> message_size
)
447 if (footer_accessible
> message_size
)
451 if (!IN_SET(h
->version
, 1, 2))
454 if (h
->type
<= _SD_BUS_MESSAGE_TYPE_INVALID
|| h
->type
>= _SD_BUS_MESSAGE_TYPE_MAX
)
457 if (!IN_SET(h
->endian
, BUS_LITTLE_ENDIAN
, BUS_BIG_ENDIAN
))
460 /* Note that we are happy with unknown flags in the flags header! */
462 a
= ALIGN(sizeof(sd_bus_message
)) + ALIGN(extra
);
465 label_sz
= strlen(label
);
475 m
->header_accessible
= header_accessible
;
477 m
->footer_accessible
= footer_accessible
;
479 if (BUS_MESSAGE_IS_GVARIANT(m
)) {
482 if (h
->dbus2
.cookie
== 0)
485 /* dbus2 derives the sizes from the message size and
486 the offset table at the end, since it is formatted as
487 gvariant "yyyyuta{tv}v". Since the message itself is a
488 structure with precisely to variable sized entries,
489 there's only one offset in the table, which marks the
490 end of the fields array. */
492 ws
= bus_gvariant_determine_word_size(message_size
, 0);
493 if (footer_accessible
< ws
)
496 m
->fields_size
= bus_gvariant_read_word_le((uint8_t*) footer
+ footer_accessible
- ws
, ws
);
497 if (ALIGN8(m
->fields_size
) > message_size
- ws
)
499 if (m
->fields_size
< sizeof(struct bus_header
))
502 m
->fields_size
-= sizeof(struct bus_header
);
503 m
->body_size
= message_size
- (sizeof(struct bus_header
) + ALIGN8(m
->fields_size
));
505 if (h
->dbus1
.serial
== 0)
508 /* dbus1 has the sizes in the header */
509 m
->fields_size
= BUS_MESSAGE_BSWAP32(m
, h
->dbus1
.fields_size
);
510 m
->body_size
= BUS_MESSAGE_BSWAP32(m
, h
->dbus1
.body_size
);
512 if (sizeof(struct bus_header
) + ALIGN8(m
->fields_size
) + m
->body_size
!= message_size
)
520 m
->creds
.label
= (char*) m
+ ALIGN(sizeof(sd_bus_message
)) + ALIGN(extra
);
521 memcpy(m
->creds
.label
, label
, label_sz
+ 1);
523 m
->creds
.mask
|= SD_BUS_CREDS_SELINUX_CONTEXT
;
527 m
->bus
= sd_bus_ref(bus
);
534 int bus_message_from_malloc(
541 sd_bus_message
**ret
) {
543 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
547 r
= bus_message_from_header(
549 buffer
, length
, /* in this case the initial bytes and the final bytes are the same */
558 sz
= length
- sizeof(struct bus_header
) - ALIGN8(m
->fields_size
);
561 m
->body
.data
= (uint8_t*) buffer
+ sizeof(struct bus_header
) + ALIGN8(m
->fields_size
);
563 m
->body
.sealed
= true;
568 m
->iovec
= m
->iovec_fixed
;
569 m
->iovec
[0] = IOVEC_MAKE(buffer
, length
);
571 r
= bus_message_parse_fields(m
);
575 /* We take possession of the memory and fds now */
576 m
->free_header
= true;
583 _public_
int sd_bus_message_new(
588 assert_return(bus
, -ENOTCONN
);
589 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
590 assert_return(bus
->state
!= BUS_UNSET
, -ENOTCONN
);
591 assert_return(m
, -EINVAL
);
592 assert_return(type
> _SD_BUS_MESSAGE_TYPE_INVALID
&& type
< _SD_BUS_MESSAGE_TYPE_MAX
, -EINVAL
);
594 sd_bus_message
*t
= malloc0(ALIGN(sizeof(sd_bus_message
)) + sizeof(struct bus_header
));
599 t
->bus
= sd_bus_ref(bus
);
600 t
->header
= (struct bus_header
*) ((uint8_t*) t
+ ALIGN(sizeof(struct sd_bus_message
)));
601 t
->header
->endian
= BUS_NATIVE_ENDIAN
;
602 t
->header
->type
= type
;
603 t
->header
->version
= bus
->message_version
;
604 t
->allow_fds
= bus
->can_fds
|| !IN_SET(bus
->state
, BUS_HELLO
, BUS_RUNNING
);
605 t
->root_container
.need_offsets
= BUS_MESSAGE_IS_GVARIANT(t
);
607 if (bus
->allow_interactive_authorization
)
608 t
->header
->flags
|= BUS_MESSAGE_ALLOW_INTERACTIVE_AUTHORIZATION
;
614 _public_
int sd_bus_message_new_signal(
618 const char *interface
,
619 const char *member
) {
621 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*t
= NULL
;
624 assert_return(bus
, -ENOTCONN
);
625 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
626 assert_return(bus
->state
!= BUS_UNSET
, -ENOTCONN
);
627 assert_return(object_path_is_valid(path
), -EINVAL
);
628 assert_return(interface_name_is_valid(interface
), -EINVAL
);
629 assert_return(member_name_is_valid(member
), -EINVAL
);
630 assert_return(m
, -EINVAL
);
632 r
= sd_bus_message_new(bus
, &t
, SD_BUS_MESSAGE_SIGNAL
);
638 t
->header
->flags
|= BUS_MESSAGE_NO_REPLY_EXPECTED
;
640 r
= message_append_field_string(t
, BUS_MESSAGE_HEADER_PATH
, SD_BUS_TYPE_OBJECT_PATH
, path
, &t
->path
);
643 r
= message_append_field_string(t
, BUS_MESSAGE_HEADER_INTERFACE
, SD_BUS_TYPE_STRING
, interface
, &t
->interface
);
646 r
= message_append_field_string(t
, BUS_MESSAGE_HEADER_MEMBER
, SD_BUS_TYPE_STRING
, member
, &t
->member
);
654 _public_
int sd_bus_message_new_method_call(
657 const char *destination
,
659 const char *interface
,
660 const char *member
) {
662 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*t
= NULL
;
665 assert_return(bus
, -ENOTCONN
);
666 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
667 assert_return(bus
->state
!= BUS_UNSET
, -ENOTCONN
);
668 assert_return(!destination
|| service_name_is_valid(destination
), -EINVAL
);
669 assert_return(object_path_is_valid(path
), -EINVAL
);
670 assert_return(!interface
|| interface_name_is_valid(interface
), -EINVAL
);
671 assert_return(member_name_is_valid(member
), -EINVAL
);
672 assert_return(m
, -EINVAL
);
674 r
= sd_bus_message_new(bus
, &t
, SD_BUS_MESSAGE_METHOD_CALL
);
680 r
= message_append_field_string(t
, BUS_MESSAGE_HEADER_PATH
, SD_BUS_TYPE_OBJECT_PATH
, path
, &t
->path
);
683 r
= message_append_field_string(t
, BUS_MESSAGE_HEADER_MEMBER
, SD_BUS_TYPE_STRING
, member
, &t
->member
);
688 r
= message_append_field_string(t
, BUS_MESSAGE_HEADER_INTERFACE
, SD_BUS_TYPE_STRING
, interface
, &t
->interface
);
694 r
= message_append_field_string(t
, BUS_MESSAGE_HEADER_DESTINATION
, SD_BUS_TYPE_STRING
, destination
, &t
->destination
);
703 static int message_new_reply(
704 sd_bus_message
*call
,
706 sd_bus_message
**m
) {
708 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*t
= NULL
;
712 assert_return(call
, -EINVAL
);
713 assert_return(call
->sealed
, -EPERM
);
714 assert_return(call
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
, -EINVAL
);
715 assert_return(call
->bus
->state
!= BUS_UNSET
, -ENOTCONN
);
716 assert_return(m
, -EINVAL
);
718 cookie
= BUS_MESSAGE_COOKIE(call
);
722 r
= sd_bus_message_new(call
->bus
, &t
, type
);
728 t
->header
->flags
|= BUS_MESSAGE_NO_REPLY_EXPECTED
;
729 t
->reply_cookie
= cookie
;
730 r
= message_append_reply_cookie(t
, t
->reply_cookie
);
735 r
= message_append_field_string(t
, BUS_MESSAGE_HEADER_DESTINATION
, SD_BUS_TYPE_STRING
, call
->sender
, &t
->destination
);
740 t
->dont_send
= !!(call
->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
);
741 t
->enforced_reply_signature
= call
->enforced_reply_signature
;
743 /* let's copy the sensitive flag over. Let's do that as a safety precaution to keep a transaction
744 * wholly sensitive if already the incoming message was sensitive. This is particularly useful when a
745 * vtable record sets the SD_BUS_VTABLE_SENSITIVE flag on a method call, since this means it applies
746 * to both the message call and the reply. */
747 t
->sensitive
= call
->sensitive
;
753 _public_
int sd_bus_message_new_method_return(
754 sd_bus_message
*call
,
755 sd_bus_message
**m
) {
757 return message_new_reply(call
, SD_BUS_MESSAGE_METHOD_RETURN
, m
);
760 _public_
int sd_bus_message_new_method_error(
761 sd_bus_message
*call
,
763 const sd_bus_error
*e
) {
765 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*t
= NULL
;
768 assert_return(sd_bus_error_is_set(e
), -EINVAL
);
769 assert_return(m
, -EINVAL
);
771 r
= message_new_reply(call
, SD_BUS_MESSAGE_METHOD_ERROR
, &t
);
775 r
= message_append_field_string(t
, BUS_MESSAGE_HEADER_ERROR_NAME
, SD_BUS_TYPE_STRING
, e
->name
, &t
->error
.name
);
780 r
= message_append_basic(t
, SD_BUS_TYPE_STRING
, e
->message
, (const void**) &t
->error
.message
);
785 t
->error
._need_free
= -1;
791 _public_
int sd_bus_message_new_method_errorf(
792 sd_bus_message
*call
,
798 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
801 assert_return(name
, -EINVAL
);
802 assert_return(m
, -EINVAL
);
804 va_start(ap
, format
);
805 bus_error_setfv(&error
, name
, format
, ap
);
808 return sd_bus_message_new_method_error(call
, m
, &error
);
811 _public_
int sd_bus_message_new_method_errno(
812 sd_bus_message
*call
,
815 const sd_bus_error
*p
) {
817 _cleanup_(sd_bus_error_free
) sd_bus_error berror
= SD_BUS_ERROR_NULL
;
819 if (sd_bus_error_is_set(p
))
820 return sd_bus_message_new_method_error(call
, m
, p
);
822 sd_bus_error_set_errno(&berror
, error
);
824 return sd_bus_message_new_method_error(call
, m
, &berror
);
827 _public_
int sd_bus_message_new_method_errnof(
828 sd_bus_message
*call
,
834 _cleanup_(sd_bus_error_free
) sd_bus_error berror
= SD_BUS_ERROR_NULL
;
837 va_start(ap
, format
);
838 sd_bus_error_set_errnofv(&berror
, error
, format
, ap
);
841 return sd_bus_message_new_method_error(call
, m
, &berror
);
844 void bus_message_set_sender_local(sd_bus
*bus
, sd_bus_message
*m
) {
848 m
->sender
= m
->creds
.unique_name
= (char*) "org.freedesktop.DBus.Local";
849 m
->creds
.well_known_names_local
= true;
850 m
->creds
.mask
|= (SD_BUS_CREDS_UNIQUE_NAME
|SD_BUS_CREDS_WELL_KNOWN_NAMES
) & bus
->creds_mask
;
853 void bus_message_set_sender_driver(sd_bus
*bus
, sd_bus_message
*m
) {
857 m
->sender
= m
->creds
.unique_name
= (char*) "org.freedesktop.DBus";
858 m
->creds
.well_known_names_driver
= true;
859 m
->creds
.mask
|= (SD_BUS_CREDS_UNIQUE_NAME
|SD_BUS_CREDS_WELL_KNOWN_NAMES
) & bus
->creds_mask
;
862 int bus_message_new_synthetic_error(
865 const sd_bus_error
*e
,
866 sd_bus_message
**m
) {
868 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*t
= NULL
;
872 assert(sd_bus_error_is_set(e
));
875 r
= sd_bus_message_new(bus
, &t
, SD_BUS_MESSAGE_METHOD_ERROR
);
881 t
->header
->flags
|= BUS_MESSAGE_NO_REPLY_EXPECTED
;
882 t
->reply_cookie
= cookie
;
884 r
= message_append_reply_cookie(t
, t
->reply_cookie
);
888 if (bus
&& bus
->unique_name
) {
889 r
= message_append_field_string(t
, BUS_MESSAGE_HEADER_DESTINATION
, SD_BUS_TYPE_STRING
, bus
->unique_name
, &t
->destination
);
894 r
= message_append_field_string(t
, BUS_MESSAGE_HEADER_ERROR_NAME
, SD_BUS_TYPE_STRING
, e
->name
, &t
->error
.name
);
899 r
= message_append_basic(t
, SD_BUS_TYPE_STRING
, e
->message
, (const void**) &t
->error
.message
);
904 t
->error
._need_free
= -1;
906 bus_message_set_sender_driver(bus
, t
);
912 _public_ sd_bus_message
* sd_bus_message_ref(sd_bus_message
*m
) {
916 /* We are fine if this message so far was either explicitly reffed or not reffed but queued into at
917 * least one bus connection object. */
918 assert(m
->n_ref
> 0 || m
->n_queued
> 0);
922 /* Each user reference to a bus message shall also be considered a ref on the bus */
927 _public_ sd_bus_message
* sd_bus_message_unref(sd_bus_message
*m
) {
931 assert(m
->n_ref
> 0);
933 sd_bus_unref(m
->bus
); /* Each regular ref is also a ref on the bus connection. Let's hence drop it
934 * here. Note we have to do this before decrementing our own n_ref here, since
935 * otherwise, if this message is currently queued sd_bus_unref() might call
936 * bus_message_unref_queued() for this which might then destroy the message
937 * while we are still processing it. */
940 if (m
->n_ref
> 0 || m
->n_queued
> 0)
943 /* Unset the bus field if neither the user has a reference nor this message is queued. We are careful
944 * to reset the field only after the last reference to the bus is dropped, after all we might keep
945 * multiple references to the bus, once for each reference kept on ourselves. */
948 return message_free(m
);
951 sd_bus_message
* bus_message_ref_queued(sd_bus_message
*m
, sd_bus
*bus
) {
955 /* If this is a different bus than the message is associated with, then implicitly turn this into a
956 * regular reference. This means that you can create a memory leak by enqueuing a message generated
957 * on one bus onto another at the same time as enqueueing a message from the second one on the first,
958 * as we'll not detect the cyclic references there. */
960 return sd_bus_message_ref(m
);
962 assert(m
->n_ref
> 0 || m
->n_queued
> 0);
968 sd_bus_message
* bus_message_unref_queued(sd_bus_message
*m
, sd_bus
*bus
) {
973 return sd_bus_message_unref(m
);
975 assert(m
->n_queued
> 0);
978 if (m
->n_ref
> 0 || m
->n_queued
> 0)
983 return message_free(m
);
986 _public_
int sd_bus_message_get_type(sd_bus_message
*m
, uint8_t *type
) {
987 assert_return(m
, -EINVAL
);
988 assert_return(type
, -EINVAL
);
990 *type
= m
->header
->type
;
994 _public_
int sd_bus_message_get_cookie(sd_bus_message
*m
, uint64_t *cookie
) {
997 assert_return(m
, -EINVAL
);
998 assert_return(cookie
, -EINVAL
);
1000 c
= BUS_MESSAGE_COOKIE(m
);
1004 *cookie
= BUS_MESSAGE_COOKIE(m
);
1008 _public_
int sd_bus_message_get_reply_cookie(sd_bus_message
*m
, uint64_t *cookie
) {
1009 assert_return(m
, -EINVAL
);
1010 assert_return(cookie
, -EINVAL
);
1012 if (m
->reply_cookie
== 0)
1015 *cookie
= m
->reply_cookie
;
1019 _public_
int sd_bus_message_get_expect_reply(sd_bus_message
*m
) {
1020 assert_return(m
, -EINVAL
);
1022 return m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
&&
1023 !(m
->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
);
1026 _public_
int sd_bus_message_get_auto_start(sd_bus_message
*m
) {
1027 assert_return(m
, -EINVAL
);
1029 return !(m
->header
->flags
& BUS_MESSAGE_NO_AUTO_START
);
1032 _public_
int sd_bus_message_get_allow_interactive_authorization(sd_bus_message
*m
) {
1033 assert_return(m
, -EINVAL
);
1035 return m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
&&
1036 (m
->header
->flags
& BUS_MESSAGE_ALLOW_INTERACTIVE_AUTHORIZATION
);
1039 _public_
const char *sd_bus_message_get_path(sd_bus_message
*m
) {
1040 assert_return(m
, NULL
);
1045 _public_
const char *sd_bus_message_get_interface(sd_bus_message
*m
) {
1046 assert_return(m
, NULL
);
1048 return m
->interface
;
1051 _public_
const char *sd_bus_message_get_member(sd_bus_message
*m
) {
1052 assert_return(m
, NULL
);
1057 _public_
const char *sd_bus_message_get_destination(sd_bus_message
*m
) {
1058 assert_return(m
, NULL
);
1060 return m
->destination
;
1063 _public_
const char *sd_bus_message_get_sender(sd_bus_message
*m
) {
1064 assert_return(m
, NULL
);
1069 _public_
const sd_bus_error
*sd_bus_message_get_error(sd_bus_message
*m
) {
1070 assert_return(m
, NULL
);
1072 if (!sd_bus_error_is_set(&m
->error
))
1078 _public_
int sd_bus_message_get_monotonic_usec(sd_bus_message
*m
, uint64_t *usec
) {
1079 assert_return(m
, -EINVAL
);
1080 assert_return(usec
, -EINVAL
);
1082 if (m
->monotonic
<= 0)
1085 *usec
= m
->monotonic
;
1089 _public_
int sd_bus_message_get_realtime_usec(sd_bus_message
*m
, uint64_t *usec
) {
1090 assert_return(m
, -EINVAL
);
1091 assert_return(usec
, -EINVAL
);
1093 if (m
->realtime
<= 0)
1096 *usec
= m
->realtime
;
1100 _public_
int sd_bus_message_get_seqnum(sd_bus_message
*m
, uint64_t *seqnum
) {
1101 assert_return(m
, -EINVAL
);
1102 assert_return(seqnum
, -EINVAL
);
1107 *seqnum
= m
->seqnum
;
1111 _public_ sd_bus_creds
*sd_bus_message_get_creds(sd_bus_message
*m
) {
1112 assert_return(m
, NULL
);
1114 if (m
->creds
.mask
== 0)
1120 _public_
int sd_bus_message_is_signal(
1122 const char *interface
,
1123 const char *member
) {
1125 assert_return(m
, -EINVAL
);
1127 if (m
->header
->type
!= SD_BUS_MESSAGE_SIGNAL
)
1130 if (interface
&& !streq_ptr(m
->interface
, interface
))
1133 if (member
&& !streq_ptr(m
->member
, member
))
1139 _public_
int sd_bus_message_is_method_call(
1141 const char *interface
,
1142 const char *member
) {
1144 assert_return(m
, -EINVAL
);
1146 if (m
->header
->type
!= SD_BUS_MESSAGE_METHOD_CALL
)
1149 if (interface
&& !streq_ptr(m
->interface
, interface
))
1152 if (member
&& !streq_ptr(m
->member
, member
))
1158 _public_
int sd_bus_message_is_method_error(sd_bus_message
*m
, const char *name
) {
1159 assert_return(m
, -EINVAL
);
1161 if (m
->header
->type
!= SD_BUS_MESSAGE_METHOD_ERROR
)
1164 if (name
&& !streq_ptr(m
->error
.name
, name
))
1170 _public_
int sd_bus_message_set_expect_reply(sd_bus_message
*m
, int b
) {
1171 assert_return(m
, -EINVAL
);
1172 assert_return(!m
->sealed
, -EPERM
);
1173 assert_return(m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
, -EPERM
);
1175 SET_FLAG(m
->header
->flags
, BUS_MESSAGE_NO_REPLY_EXPECTED
, !b
);
1180 _public_
int sd_bus_message_set_auto_start(sd_bus_message
*m
, int b
) {
1181 assert_return(m
, -EINVAL
);
1182 assert_return(!m
->sealed
, -EPERM
);
1184 SET_FLAG(m
->header
->flags
, BUS_MESSAGE_NO_AUTO_START
, !b
);
1189 _public_
int sd_bus_message_set_allow_interactive_authorization(sd_bus_message
*m
, int b
) {
1190 assert_return(m
, -EINVAL
);
1191 assert_return(!m
->sealed
, -EPERM
);
1193 SET_FLAG(m
->header
->flags
, BUS_MESSAGE_ALLOW_INTERACTIVE_AUTHORIZATION
, b
);
1198 struct bus_body_part
*message_append_part(sd_bus_message
*m
) {
1199 struct bus_body_part
*part
;
1206 if (m
->n_body_parts
<= 0) {
1210 assert(m
->body_end
);
1212 part
= new0(struct bus_body_part
, 1);
1218 m
->body_end
->next
= part
;
1228 static void part_zero(struct bus_body_part
*part
, size_t sz
) {
1233 /* All other fields can be left in their defaults */
1234 assert(!part
->data
);
1235 assert(part
->memfd
< 0);
1238 part
->is_zero
= true;
1239 part
->sealed
= true;
1242 static int part_make_space(
1243 struct sd_bus_message
*m
,
1244 struct bus_body_part
*part
,
1252 assert(!part
->sealed
);
1257 if (part
->allocated
== 0 || sz
> part
->allocated
) {
1258 size_t new_allocated
;
1260 new_allocated
= sz
> 0 ? 2 * sz
: 64;
1261 n
= realloc(part
->data
, new_allocated
);
1268 part
->allocated
= new_allocated
;
1269 part
->free_this
= true;
1273 *q
= part
->data
? (uint8_t*) part
->data
+ part
->size
: NULL
;
1279 static int message_add_offset(sd_bus_message
*m
, size_t offset
) {
1280 struct bus_container
*c
;
1283 assert(BUS_MESSAGE_IS_GVARIANT(m
));
1285 /* Add offset to current container, unless this is the first
1286 * item in it, which will have the 0 offset, which we can
1288 c
= message_get_last_container(m
);
1290 if (!c
->need_offsets
)
1293 if (!GREEDY_REALLOC(c
->offsets
, c
->offsets_allocated
, c
->n_offsets
+ 1))
1296 c
->offsets
[c
->n_offsets
++] = offset
;
1300 static void message_extend_containers(sd_bus_message
*m
, size_t expand
) {
1306 if (m
->n_containers
<= 0)
1309 /* Update counters */
1310 for (struct bus_container
*c
= m
->containers
; c
< m
->containers
+ m
->n_containers
; c
++)
1312 *c
->array_size
+= expand
;
1315 static void *message_extend_body(
1320 bool force_inline
) {
1322 size_t start_body
, end_body
, padding
, added
;
1333 start_body
= ALIGN_TO((size_t) m
->body_size
, align
);
1334 end_body
= start_body
+ sz
;
1336 padding
= start_body
- m
->body_size
;
1337 added
= padding
+ sz
;
1339 /* Check for 32bit overflows */
1340 if (end_body
> (size_t) ((uint32_t) -1) ||
1341 end_body
< start_body
) {
1347 struct bus_body_part
*part
= NULL
;
1351 m
->n_body_parts
<= 0 ||
1352 m
->body_end
->sealed
||
1353 (padding
!= ALIGN_TO(m
->body_end
->size
, align
) - m
->body_end
->size
) ||
1354 (force_inline
&& m
->body_end
->size
> MEMFD_MIN_SIZE
);
1355 /* If this must be an inlined extension, let's create a new part if
1356 * the previous part is large enough to be inlined. */
1360 part
= message_append_part(m
);
1364 part_zero(part
, padding
);
1367 part
= message_append_part(m
);
1371 r
= part_make_space(m
, part
, sz
, &p
);
1376 size_t os
, start_part
, end_part
;
1382 start_part
= ALIGN_TO(part
->size
, align
);
1383 end_part
= start_part
+ sz
;
1385 r
= part_make_space(m
, part
, end_part
, &p
);
1390 memzero(p
, padding
);
1391 p
= (uint8_t*) p
+ padding
;
1394 /* Readjust pointers */
1395 if (m
->n_containers
> 0)
1396 for (struct bus_container
*c
= m
->containers
; c
< m
->containers
+ m
->n_containers
; c
++)
1397 c
->array_size
= adjust_pointer(c
->array_size
, op
, os
, part
->data
);
1399 m
->error
.message
= (const char*) adjust_pointer(m
->error
.message
, op
, os
, part
->data
);
1402 /* Return something that is not NULL and is aligned */
1403 p
= (uint8_t*) align
;
1405 m
->body_size
= end_body
;
1406 message_extend_containers(m
, added
);
1409 r
= message_add_offset(m
, end_body
);
1419 static int message_push_fd(sd_bus_message
*m
, int fd
) {
1430 copy
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
1434 f
= reallocarray(m
->fds
, sizeof(int), m
->n_fds
+ 1);
1442 m
->fds
[m
->n_fds
] = copy
;
1448 int message_append_basic(sd_bus_message
*m
, char type
, const void *p
, const void **stored
) {
1449 _cleanup_close_
int fd
= -1;
1450 struct bus_container
*c
;
1454 assert_return(m
, -EINVAL
);
1455 assert_return(!m
->sealed
, -EPERM
);
1456 assert_return(bus_type_is_basic(type
), -EINVAL
);
1457 assert_return(!m
->poisoned
, -ESTALE
);
1459 c
= message_get_last_container(m
);
1461 if (c
->signature
&& c
->signature
[c
->index
]) {
1462 /* Container signature is already set */
1464 if (c
->signature
[c
->index
] != type
)
1469 /* Maybe we can append to the signature? But only if this is the top-level container */
1470 if (c
->enclosing
!= 0)
1473 e
= strextend(&c
->signature
, CHAR_TO_STR(type
), NULL
);
1480 if (BUS_MESSAGE_IS_GVARIANT(m
)) {
1486 case SD_BUS_TYPE_SIGNATURE
:
1487 case SD_BUS_TYPE_STRING
:
1491 case SD_BUS_TYPE_OBJECT_PATH
:
1499 case SD_BUS_TYPE_BOOLEAN
:
1501 u8
= p
&& *(int*) p
;
1507 case SD_BUS_TYPE_UNIX_FD
:
1512 fd
= message_push_fd(m
, *(int*) p
);
1523 align
= bus_gvariant_get_alignment(CHAR_TO_STR(type
));
1524 sz
= bus_gvariant_get_size(CHAR_TO_STR(type
));
1531 a
= message_extend_body(m
, align
, sz
, true, false);
1538 *stored
= (const uint8_t*) a
;
1545 case SD_BUS_TYPE_STRING
:
1546 /* To make things easy we'll serialize a NULL string
1547 * into the empty string */
1551 case SD_BUS_TYPE_OBJECT_PATH
:
1557 sz
= 4 + strlen(p
) + 1;
1560 case SD_BUS_TYPE_SIGNATURE
:
1565 sz
= 1 + strlen(p
) + 1;
1568 case SD_BUS_TYPE_BOOLEAN
:
1570 u32
= p
&& *(int*) p
;
1576 case SD_BUS_TYPE_UNIX_FD
:
1581 fd
= message_push_fd(m
, *(int*) p
);
1592 align
= bus_type_get_alignment(type
);
1593 sz
= bus_type_get_size(type
);
1600 a
= message_extend_body(m
, align
, sz
, false, false);
1604 if (IN_SET(type
, SD_BUS_TYPE_STRING
, SD_BUS_TYPE_OBJECT_PATH
)) {
1605 *(uint32_t*) a
= sz
- 5;
1606 memcpy((uint8_t*) a
+ 4, p
, sz
- 4);
1609 *stored
= (const uint8_t*) a
+ 4;
1611 } else if (type
== SD_BUS_TYPE_SIGNATURE
) {
1612 *(uint8_t*) a
= sz
- 2;
1613 memcpy((uint8_t*) a
+ 1, p
, sz
- 1);
1616 *stored
= (const uint8_t*) a
+ 1;
1625 if (type
== SD_BUS_TYPE_UNIX_FD
)
1628 if (c
->enclosing
!= SD_BUS_TYPE_ARRAY
)
1635 _public_
int sd_bus_message_append_basic(sd_bus_message
*m
, char type
, const void *p
) {
1636 return message_append_basic(m
, type
, p
, NULL
);
1639 _public_
int sd_bus_message_append_string_space(
1644 struct bus_container
*c
;
1647 assert_return(m
, -EINVAL
);
1648 assert_return(s
, -EINVAL
);
1649 assert_return(!m
->sealed
, -EPERM
);
1650 assert_return(!m
->poisoned
, -ESTALE
);
1652 c
= message_get_last_container(m
);
1654 if (c
->signature
&& c
->signature
[c
->index
]) {
1655 /* Container signature is already set */
1657 if (c
->signature
[c
->index
] != SD_BUS_TYPE_STRING
)
1662 /* Maybe we can append to the signature? But only if this is the top-level container */
1663 if (c
->enclosing
!= 0)
1666 e
= strextend(&c
->signature
, CHAR_TO_STR(SD_BUS_TYPE_STRING
), NULL
);
1673 if (BUS_MESSAGE_IS_GVARIANT(m
)) {
1674 a
= message_extend_body(m
, 1, size
+ 1, true, false);
1680 a
= message_extend_body(m
, 4, 4 + size
+ 1, false, false);
1684 *(uint32_t*) a
= size
;
1690 if (c
->enclosing
!= SD_BUS_TYPE_ARRAY
)
1696 _public_
int sd_bus_message_append_string_iovec(
1698 const struct iovec
*iov
,
1699 unsigned n
/* should be size_t, but is API now… 😞 */) {
1706 assert_return(m
, -EINVAL
);
1707 assert_return(!m
->sealed
, -EPERM
);
1708 assert_return(iov
|| n
== 0, -EINVAL
);
1709 assert_return(!m
->poisoned
, -ESTALE
);
1711 size
= IOVEC_TOTAL_SIZE(iov
, n
);
1713 r
= sd_bus_message_append_string_space(m
, size
, &p
);
1717 for (i
= 0; i
< n
; i
++) {
1719 if (iov
[i
].iov_base
)
1720 memcpy(p
, iov
[i
].iov_base
, iov
[i
].iov_len
);
1722 memset(p
, ' ', iov
[i
].iov_len
);
1724 p
+= iov
[i
].iov_len
;
1730 static int bus_message_open_array(
1732 struct bus_container
*c
,
1733 const char *contents
,
1734 uint32_t **array_size
,
1736 bool *need_offsets
) {
1746 assert(need_offsets
);
1748 if (!signature_is_single(contents
, true))
1751 if (c
->signature
&& c
->signature
[c
->index
]) {
1753 /* Verify the existing signature */
1755 if (c
->signature
[c
->index
] != SD_BUS_TYPE_ARRAY
)
1758 if (!startswith(c
->signature
+ c
->index
+ 1, contents
))
1761 nindex
= c
->index
+ 1 + strlen(contents
);
1765 if (c
->enclosing
!= 0)
1768 /* Extend the existing signature */
1770 e
= strextend(&c
->signature
, CHAR_TO_STR(SD_BUS_TYPE_ARRAY
), contents
, NULL
);
1776 nindex
= e
- c
->signature
;
1779 if (BUS_MESSAGE_IS_GVARIANT(m
)) {
1780 alignment
= bus_gvariant_get_alignment(contents
);
1784 /* Add alignment padding and add to offset list */
1785 if (!message_extend_body(m
, alignment
, 0, false, false))
1788 r
= bus_gvariant_is_fixed_size(contents
);
1792 *begin
= m
->body_size
;
1793 *need_offsets
= r
== 0;
1797 struct bus_body_part
*o
;
1799 alignment
= bus_type_get_alignment(contents
[0]);
1803 a
= message_extend_body(m
, 4, 4, false, false);
1808 op
= m
->body_end
->data
;
1809 os
= m
->body_end
->size
;
1811 /* Add alignment between size and first element */
1812 if (!message_extend_body(m
, alignment
, 0, false, false))
1815 /* location of array size might have changed so let's readjust a */
1816 if (o
== m
->body_end
)
1817 a
= adjust_pointer(a
, op
, os
, m
->body_end
->data
);
1823 if (c
->enclosing
!= SD_BUS_TYPE_ARRAY
)
1829 static int bus_message_open_variant(
1831 struct bus_container
*c
,
1832 const char *contents
) {
1838 if (!signature_is_single(contents
, false))
1841 if (*contents
== SD_BUS_TYPE_DICT_ENTRY_BEGIN
)
1844 if (c
->signature
&& c
->signature
[c
->index
]) {
1846 if (c
->signature
[c
->index
] != SD_BUS_TYPE_VARIANT
)
1852 if (c
->enclosing
!= 0)
1855 e
= strextend(&c
->signature
, CHAR_TO_STR(SD_BUS_TYPE_VARIANT
), NULL
);
1862 if (BUS_MESSAGE_IS_GVARIANT(m
)) {
1863 /* Variants are always aligned to 8 */
1865 if (!message_extend_body(m
, 8, 0, false, false))
1872 l
= strlen(contents
);
1873 a
= message_extend_body(m
, 1, 1 + l
+ 1, false, false);
1878 memcpy((uint8_t*) a
+ 1, contents
, l
+ 1);
1881 if (c
->enclosing
!= SD_BUS_TYPE_ARRAY
)
1887 static int bus_message_open_struct(
1889 struct bus_container
*c
,
1890 const char *contents
,
1892 bool *need_offsets
) {
1901 assert(need_offsets
);
1903 if (!signature_is_valid(contents
, false))
1906 if (c
->signature
&& c
->signature
[c
->index
]) {
1909 l
= strlen(contents
);
1911 if (c
->signature
[c
->index
] != SD_BUS_TYPE_STRUCT_BEGIN
||
1912 !startswith(c
->signature
+ c
->index
+ 1, contents
) ||
1913 c
->signature
[c
->index
+ 1 + l
] != SD_BUS_TYPE_STRUCT_END
)
1916 nindex
= c
->index
+ 1 + l
+ 1;
1920 if (c
->enclosing
!= 0)
1923 e
= strextend(&c
->signature
, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_BEGIN
), contents
, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_END
), NULL
);
1929 nindex
= e
- c
->signature
;
1932 if (BUS_MESSAGE_IS_GVARIANT(m
)) {
1935 alignment
= bus_gvariant_get_alignment(contents
);
1939 if (!message_extend_body(m
, alignment
, 0, false, false))
1942 r
= bus_gvariant_is_fixed_size(contents
);
1946 *begin
= m
->body_size
;
1947 *need_offsets
= r
== 0;
1949 /* Align contents to 8 byte boundary */
1950 if (!message_extend_body(m
, 8, 0, false, false))
1954 if (c
->enclosing
!= SD_BUS_TYPE_ARRAY
)
1960 static int bus_message_open_dict_entry(
1962 struct bus_container
*c
,
1963 const char *contents
,
1965 bool *need_offsets
) {
1973 assert(need_offsets
);
1975 if (!signature_is_pair(contents
))
1978 if (c
->enclosing
!= SD_BUS_TYPE_ARRAY
)
1981 if (c
->signature
&& c
->signature
[c
->index
]) {
1984 l
= strlen(contents
);
1986 if (c
->signature
[c
->index
] != SD_BUS_TYPE_DICT_ENTRY_BEGIN
||
1987 !startswith(c
->signature
+ c
->index
+ 1, contents
) ||
1988 c
->signature
[c
->index
+ 1 + l
] != SD_BUS_TYPE_DICT_ENTRY_END
)
1993 if (BUS_MESSAGE_IS_GVARIANT(m
)) {
1996 alignment
= bus_gvariant_get_alignment(contents
);
2000 if (!message_extend_body(m
, alignment
, 0, false, false))
2003 r
= bus_gvariant_is_fixed_size(contents
);
2007 *begin
= m
->body_size
;
2008 *need_offsets
= r
== 0;
2010 /* Align contents to 8 byte boundary */
2011 if (!message_extend_body(m
, 8, 0, false, false))
2018 _public_
int sd_bus_message_open_container(
2021 const char *contents
) {
2023 struct bus_container
*c
;
2024 uint32_t *array_size
= NULL
;
2025 _cleanup_free_
char *signature
= NULL
;
2026 size_t before
, begin
= 0;
2027 bool need_offsets
= false;
2030 assert_return(m
, -EINVAL
);
2031 assert_return(!m
->sealed
, -EPERM
);
2032 assert_return(contents
, -EINVAL
);
2033 assert_return(!m
->poisoned
, -ESTALE
);
2035 /* Make sure we have space for one more container */
2036 if (!GREEDY_REALLOC(m
->containers
, m
->containers_allocated
, m
->n_containers
+ 1)) {
2041 c
= message_get_last_container(m
);
2043 signature
= strdup(contents
);
2049 /* Save old index in the parent container, in case we have to
2050 * abort this container */
2051 c
->saved_index
= c
->index
;
2052 before
= m
->body_size
;
2054 if (type
== SD_BUS_TYPE_ARRAY
)
2055 r
= bus_message_open_array(m
, c
, contents
, &array_size
, &begin
, &need_offsets
);
2056 else if (type
== SD_BUS_TYPE_VARIANT
)
2057 r
= bus_message_open_variant(m
, c
, contents
);
2058 else if (type
== SD_BUS_TYPE_STRUCT
)
2059 r
= bus_message_open_struct(m
, c
, contents
, &begin
, &need_offsets
);
2060 else if (type
== SD_BUS_TYPE_DICT_ENTRY
)
2061 r
= bus_message_open_dict_entry(m
, c
, contents
, &begin
, &need_offsets
);
2067 /* OK, let's fill it in */
2068 m
->containers
[m
->n_containers
++] = (struct bus_container
) {
2070 .signature
= TAKE_PTR(signature
),
2071 .array_size
= array_size
,
2074 .need_offsets
= need_offsets
,
2080 static int bus_message_close_array(sd_bus_message
*m
, struct bus_container
*c
) {
2085 if (!BUS_MESSAGE_IS_GVARIANT(m
))
2088 if (c
->need_offsets
) {
2089 size_t payload
, sz
, i
;
2092 /* Variable-width arrays */
2094 payload
= c
->n_offsets
> 0 ? c
->offsets
[c
->n_offsets
-1] - c
->begin
: 0;
2095 sz
= bus_gvariant_determine_word_size(payload
, c
->n_offsets
);
2097 a
= message_extend_body(m
, 1, sz
* c
->n_offsets
, true, false);
2101 for (i
= 0; i
< c
->n_offsets
; i
++)
2102 bus_gvariant_write_word_le(a
+ sz
*i
, sz
, c
->offsets
[i
] - c
->begin
);
2106 /* Fixed-width or empty arrays */
2108 a
= message_extend_body(m
, 1, 0, true, false); /* let's add offset to parent */
2116 static int bus_message_close_variant(sd_bus_message
*m
, struct bus_container
*c
) {
2122 assert(c
->signature
);
2124 if (!BUS_MESSAGE_IS_GVARIANT(m
))
2127 l
= strlen(c
->signature
);
2129 a
= message_extend_body(m
, 1, 1 + l
, true, false);
2134 memcpy(a
+1, c
->signature
, l
);
2139 static int bus_message_close_struct(sd_bus_message
*m
, struct bus_container
*c
, bool add_offset
) {
2140 bool fixed_size
= true;
2141 size_t n_variable
= 0;
2150 if (!BUS_MESSAGE_IS_GVARIANT(m
))
2153 p
= strempty(c
->signature
);
2157 r
= signature_element_length(p
, &n
);
2166 r
= bus_gvariant_is_fixed_size(t
);
2171 assert(!c
->need_offsets
|| i
<= c
->n_offsets
);
2173 /* We need to add an offset for each item that has a
2174 * variable size and that is not the last one in the
2178 if (r
== 0 && p
[n
] != 0)
2185 assert(!c
->need_offsets
|| i
== c
->n_offsets
);
2186 assert(c
->need_offsets
|| n_variable
== 0);
2188 if (isempty(c
->signature
)) {
2189 /* The unary type is encoded as fixed 1 byte padding */
2190 a
= message_extend_body(m
, 1, 1, add_offset
, false);
2195 } else if (n_variable
<= 0) {
2198 /* Structures with fixed-size members only have to be
2199 * fixed-size themselves. But gvariant requires all fixed-size
2200 * elements to be sized a multiple of their alignment. Hence,
2201 * we must *always* add final padding after the last member so
2202 * the overall size of the structure is properly aligned. */
2204 alignment
= bus_gvariant_get_alignment(strempty(c
->signature
));
2206 assert(alignment
> 0);
2208 a
= message_extend_body(m
, alignment
, 0, add_offset
, false);
2215 assert(c
->offsets
[c
->n_offsets
-1] == m
->body_size
);
2217 sz
= bus_gvariant_determine_word_size(m
->body_size
- c
->begin
, n_variable
);
2219 a
= message_extend_body(m
, 1, sz
* n_variable
, add_offset
, false);
2223 p
= strempty(c
->signature
);
2224 for (i
= 0, j
= 0; i
< c
->n_offsets
; i
++) {
2228 r
= signature_element_length(p
, &n
);
2239 r
= bus_gvariant_is_fixed_size(t
);
2242 if (r
> 0 || p
[0] == 0)
2246 k
= n_variable
- 1 - j
;
2248 bus_gvariant_write_word_le(a
+ k
* sz
, sz
, c
->offsets
[i
] - c
->begin
);
2257 _public_
int sd_bus_message_close_container(sd_bus_message
*m
) {
2258 struct bus_container
*c
;
2261 assert_return(m
, -EINVAL
);
2262 assert_return(!m
->sealed
, -EPERM
);
2263 assert_return(m
->n_containers
> 0, -EINVAL
);
2264 assert_return(!m
->poisoned
, -ESTALE
);
2266 c
= message_get_last_container(m
);
2268 if (c
->enclosing
!= SD_BUS_TYPE_ARRAY
)
2269 if (c
->signature
&& c
->signature
[c
->index
] != 0)
2274 if (c
->enclosing
== SD_BUS_TYPE_ARRAY
)
2275 r
= bus_message_close_array(m
, c
);
2276 else if (c
->enclosing
== SD_BUS_TYPE_VARIANT
)
2277 r
= bus_message_close_variant(m
, c
);
2278 else if (IN_SET(c
->enclosing
, SD_BUS_TYPE_STRUCT
, SD_BUS_TYPE_DICT_ENTRY
))
2279 r
= bus_message_close_struct(m
, c
, true);
2281 assert_not_reached("Unknown container type");
2295 static int type_stack_push(TypeStack
*stack
, unsigned max
, unsigned *i
, const char *types
, unsigned n_struct
, unsigned n_array
) {
2302 stack
[*i
].types
= types
;
2303 stack
[*i
].n_struct
= n_struct
;
2304 stack
[*i
].n_array
= n_array
;
2310 static int type_stack_pop(TypeStack
*stack
, unsigned max
, unsigned *i
, const char **types
, unsigned *n_struct
, unsigned *n_array
) {
2321 *types
= stack
[*i
].types
;
2322 *n_struct
= stack
[*i
].n_struct
;
2323 *n_array
= stack
[*i
].n_array
;
2328 _public_
int sd_bus_message_appendv(
2333 unsigned n_array
, n_struct
;
2334 TypeStack stack
[BUS_CONTAINER_DEPTH
];
2335 unsigned stack_ptr
= 0;
2338 assert_return(m
, -EINVAL
);
2339 assert_return(types
, -EINVAL
);
2340 assert_return(!m
->sealed
, -EPERM
);
2341 assert_return(!m
->poisoned
, -ESTALE
);
2343 n_array
= (unsigned) -1;
2344 n_struct
= strlen(types
);
2349 if (n_array
== 0 || (n_array
== (unsigned) -1 && n_struct
== 0)) {
2350 r
= type_stack_pop(stack
, ELEMENTSOF(stack
), &stack_ptr
, &types
, &n_struct
, &n_array
);
2356 r
= sd_bus_message_close_container(m
);
2364 if (n_array
!= (unsigned) -1)
2373 case SD_BUS_TYPE_BYTE
: {
2376 x
= (uint8_t) va_arg(ap
, int);
2377 r
= sd_bus_message_append_basic(m
, *t
, &x
);
2381 case SD_BUS_TYPE_BOOLEAN
:
2382 case SD_BUS_TYPE_INT32
:
2383 case SD_BUS_TYPE_UINT32
:
2384 case SD_BUS_TYPE_UNIX_FD
: {
2387 /* We assume a boolean is the same as int32_t */
2388 assert_cc(sizeof(int32_t) == sizeof(int));
2390 x
= va_arg(ap
, uint32_t);
2391 r
= sd_bus_message_append_basic(m
, *t
, &x
);
2395 case SD_BUS_TYPE_INT16
:
2396 case SD_BUS_TYPE_UINT16
: {
2399 x
= (uint16_t) va_arg(ap
, int);
2400 r
= sd_bus_message_append_basic(m
, *t
, &x
);
2404 case SD_BUS_TYPE_INT64
:
2405 case SD_BUS_TYPE_UINT64
: {
2408 x
= va_arg(ap
, uint64_t);
2409 r
= sd_bus_message_append_basic(m
, *t
, &x
);
2413 case SD_BUS_TYPE_DOUBLE
: {
2416 x
= va_arg(ap
, double);
2417 r
= sd_bus_message_append_basic(m
, *t
, &x
);
2421 case SD_BUS_TYPE_STRING
:
2422 case SD_BUS_TYPE_OBJECT_PATH
:
2423 case SD_BUS_TYPE_SIGNATURE
: {
2426 x
= va_arg(ap
, const char*);
2427 r
= sd_bus_message_append_basic(m
, *t
, x
);
2431 case SD_BUS_TYPE_ARRAY
: {
2434 r
= signature_element_length(t
+ 1, &k
);
2440 memcpy(s
, t
+ 1, k
);
2443 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_ARRAY
, s
);
2448 if (n_array
== (unsigned) -1) {
2453 r
= type_stack_push(stack
, ELEMENTSOF(stack
), &stack_ptr
, types
, n_struct
, n_array
);
2459 n_array
= va_arg(ap
, unsigned);
2464 case SD_BUS_TYPE_VARIANT
: {
2467 s
= va_arg(ap
, const char*);
2471 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_VARIANT
, s
);
2475 r
= type_stack_push(stack
, ELEMENTSOF(stack
), &stack_ptr
, types
, n_struct
, n_array
);
2480 n_struct
= strlen(s
);
2481 n_array
= (unsigned) -1;
2486 case SD_BUS_TYPE_STRUCT_BEGIN
:
2487 case SD_BUS_TYPE_DICT_ENTRY_BEGIN
: {
2490 r
= signature_element_length(t
, &k
);
2497 memcpy(s
, t
+ 1, k
- 2);
2500 r
= sd_bus_message_open_container(m
, *t
== SD_BUS_TYPE_STRUCT_BEGIN
? SD_BUS_TYPE_STRUCT
: SD_BUS_TYPE_DICT_ENTRY
, s
);
2505 if (n_array
== (unsigned) -1) {
2510 r
= type_stack_push(stack
, ELEMENTSOF(stack
), &stack_ptr
, types
, n_struct
, n_array
);
2516 n_array
= (unsigned) -1;
2532 _public_
int sd_bus_message_append(sd_bus_message
*m
, const char *types
, ...) {
2536 va_start(ap
, types
);
2537 r
= sd_bus_message_appendv(m
, types
, ap
);
2543 _public_
int sd_bus_message_append_array_space(
2553 assert_return(m
, -EINVAL
);
2554 assert_return(!m
->sealed
, -EPERM
);
2555 assert_return(bus_type_is_trivial(type
) && type
!= SD_BUS_TYPE_BOOLEAN
, -EINVAL
);
2556 assert_return(ptr
|| size
== 0, -EINVAL
);
2557 assert_return(!m
->poisoned
, -ESTALE
);
2559 /* alignment and size of the trivial types (except bool) is
2560 * identical for gvariant and dbus1 marshalling */
2561 align
= bus_type_get_alignment(type
);
2562 sz
= bus_type_get_size(type
);
2564 assert_se(align
> 0);
2570 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_ARRAY
, CHAR_TO_STR(type
));
2574 a
= message_extend_body(m
, align
, size
, false, false);
2578 r
= sd_bus_message_close_container(m
);
2586 _public_
int sd_bus_message_append_array(
2594 assert_return(m
, -EINVAL
);
2595 assert_return(!m
->sealed
, -EPERM
);
2596 assert_return(bus_type_is_trivial(type
), -EINVAL
);
2597 assert_return(ptr
|| size
== 0, -EINVAL
);
2598 assert_return(!m
->poisoned
, -ESTALE
);
2600 r
= sd_bus_message_append_array_space(m
, type
, size
, &p
);
2604 memcpy_safe(p
, ptr
, size
);
2609 _public_
int sd_bus_message_append_array_iovec(
2612 const struct iovec
*iov
,
2613 unsigned n
/* should be size_t, but is API now… 😞 */) {
2620 assert_return(m
, -EINVAL
);
2621 assert_return(!m
->sealed
, -EPERM
);
2622 assert_return(bus_type_is_trivial(type
), -EINVAL
);
2623 assert_return(iov
|| n
== 0, -EINVAL
);
2624 assert_return(!m
->poisoned
, -ESTALE
);
2626 size
= IOVEC_TOTAL_SIZE(iov
, n
);
2628 r
= sd_bus_message_append_array_space(m
, type
, size
, &p
);
2632 for (i
= 0; i
< n
; i
++) {
2634 if (iov
[i
].iov_base
)
2635 memcpy(p
, iov
[i
].iov_base
, iov
[i
].iov_len
);
2637 memzero(p
, iov
[i
].iov_len
);
2639 p
= (uint8_t*) p
+ iov
[i
].iov_len
;
2645 _public_
int sd_bus_message_append_array_memfd(
2652 _cleanup_close_
int copy_fd
= -1;
2653 struct bus_body_part
*part
;
2659 assert_return(m
, -EINVAL
);
2660 assert_return(memfd
>= 0, -EBADF
);
2661 assert_return(bus_type_is_trivial(type
), -EINVAL
);
2662 assert_return(size
> 0, -EINVAL
);
2663 assert_return(!m
->sealed
, -EPERM
);
2664 assert_return(!m
->poisoned
, -ESTALE
);
2666 r
= memfd_set_sealed(memfd
);
2670 copy_fd
= fcntl(memfd
, F_DUPFD_CLOEXEC
, 3);
2674 r
= memfd_get_size(memfd
, &real_size
);
2678 if (offset
== 0 && size
== (uint64_t) -1)
2680 else if (offset
+ size
> real_size
)
2683 align
= bus_type_get_alignment(type
);
2684 sz
= bus_type_get_size(type
);
2686 assert_se(align
> 0);
2689 if (offset
% align
!= 0)
2695 if (size
> (uint64_t) (uint32_t) -1)
2698 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_ARRAY
, CHAR_TO_STR(type
));
2702 a
= message_extend_body(m
, align
, 0, false, false);
2706 part
= message_append_part(m
);
2710 part
->memfd
= copy_fd
;
2711 part
->memfd_offset
= offset
;
2712 part
->sealed
= true;
2716 m
->body_size
+= size
;
2717 message_extend_containers(m
, size
);
2719 return sd_bus_message_close_container(m
);
2722 _public_
int sd_bus_message_append_string_memfd(
2728 _cleanup_close_
int copy_fd
= -1;
2729 struct bus_body_part
*part
;
2730 struct bus_container
*c
;
2735 assert_return(m
, -EINVAL
);
2736 assert_return(memfd
>= 0, -EBADF
);
2737 assert_return(size
> 0, -EINVAL
);
2738 assert_return(!m
->sealed
, -EPERM
);
2739 assert_return(!m
->poisoned
, -ESTALE
);
2741 r
= memfd_set_sealed(memfd
);
2745 copy_fd
= fcntl(memfd
, FD_CLOEXEC
, 3);
2749 r
= memfd_get_size(memfd
, &real_size
);
2753 if (offset
== 0 && size
== (uint64_t) -1)
2755 else if (offset
+ size
> real_size
)
2758 /* We require this to be NUL terminated */
2762 if (size
> (uint64_t) (uint32_t) -1)
2765 c
= message_get_last_container(m
);
2766 if (c
->signature
&& c
->signature
[c
->index
]) {
2767 /* Container signature is already set */
2769 if (c
->signature
[c
->index
] != SD_BUS_TYPE_STRING
)
2774 /* Maybe we can append to the signature? But only if this is the top-level container */
2775 if (c
->enclosing
!= 0)
2778 e
= strextend(&c
->signature
, CHAR_TO_STR(SD_BUS_TYPE_STRING
), NULL
);
2785 if (!BUS_MESSAGE_IS_GVARIANT(m
)) {
2786 a
= message_extend_body(m
, 4, 4, false, false);
2790 *(uint32_t*) a
= size
- 1;
2793 part
= message_append_part(m
);
2797 part
->memfd
= copy_fd
;
2798 part
->memfd_offset
= offset
;
2799 part
->sealed
= true;
2803 m
->body_size
+= size
;
2804 message_extend_containers(m
, size
);
2806 if (BUS_MESSAGE_IS_GVARIANT(m
)) {
2807 r
= message_add_offset(m
, m
->body_size
);
2814 if (c
->enclosing
!= SD_BUS_TYPE_ARRAY
)
2820 _public_
int sd_bus_message_append_strv(sd_bus_message
*m
, char **l
) {
2824 assert_return(m
, -EINVAL
);
2825 assert_return(!m
->sealed
, -EPERM
);
2826 assert_return(!m
->poisoned
, -ESTALE
);
2828 r
= sd_bus_message_open_container(m
, 'a', "s");
2832 STRV_FOREACH(i
, l
) {
2833 r
= sd_bus_message_append_basic(m
, 's', *i
);
2838 return sd_bus_message_close_container(m
);
2841 static int bus_message_close_header(sd_bus_message
*m
) {
2845 /* The actual user data is finished now, we just complete the
2846 variant and struct now (at least on gvariant). Remember
2847 this position, so that during parsing we know where to
2848 put the outer container end. */
2849 m
->user_body_size
= m
->body_size
;
2851 if (BUS_MESSAGE_IS_GVARIANT(m
)) {
2852 const char *signature
;
2856 /* Add offset table to end of fields array */
2857 if (m
->n_header_offsets
>= 1) {
2861 assert(m
->fields_size
== m
->header_offsets
[m
->n_header_offsets
-1]);
2863 sz
= bus_gvariant_determine_word_size(m
->fields_size
, m
->n_header_offsets
);
2864 a
= message_extend_fields(m
, 1, sz
* m
->n_header_offsets
, false);
2868 for (i
= 0; i
< m
->n_header_offsets
; i
++)
2869 bus_gvariant_write_word_le(a
+ sz
*i
, sz
, m
->header_offsets
[i
]);
2872 /* Add gvariant NUL byte plus signature to the end of
2873 * the body, followed by the final offset pointing to
2874 * the end of the fields array */
2876 signature
= strempty(m
->root_container
.signature
);
2877 l
= strlen(signature
);
2879 sz
= bus_gvariant_determine_word_size(sizeof(struct bus_header
) + ALIGN8(m
->fields_size
) + m
->body_size
+ 1 + l
+ 2, 1);
2880 d
= message_extend_body(m
, 1, 1 + l
+ 2 + sz
, false, true);
2885 *((uint8_t*) d
+ 1) = SD_BUS_TYPE_STRUCT_BEGIN
;
2886 memcpy((uint8_t*) d
+ 2, signature
, l
);
2887 *((uint8_t*) d
+ 1 + l
+ 1) = SD_BUS_TYPE_STRUCT_END
;
2889 bus_gvariant_write_word_le((uint8_t*) d
+ 1 + l
+ 2, sz
, sizeof(struct bus_header
) + m
->fields_size
);
2892 m
->footer_accessible
= 1 + l
+ 2 + sz
;
2894 m
->header
->dbus1
.fields_size
= m
->fields_size
;
2895 m
->header
->dbus1
.body_size
= m
->body_size
;
2901 _public_
int sd_bus_message_seal(sd_bus_message
*m
, uint64_t cookie
, uint64_t timeout_usec
) {
2902 struct bus_body_part
*part
;
2907 assert_return(m
, -EINVAL
);
2912 if (m
->n_containers
> 0)
2918 if (cookie
> 0xffffffffULL
&&
2919 !BUS_MESSAGE_IS_GVARIANT(m
))
2922 /* In vtables the return signature of method calls is listed,
2923 * let's check if they match if this is a response */
2924 if (m
->header
->type
== SD_BUS_MESSAGE_METHOD_RETURN
&&
2925 m
->enforced_reply_signature
&&
2926 !streq(strempty(m
->root_container
.signature
), m
->enforced_reply_signature
))
2929 /* If gvariant marshalling is used we need to close the body structure */
2930 r
= bus_message_close_struct(m
, &m
->root_container
, false);
2934 /* If there's a non-trivial signature set, then add it in
2935 * here, but only on dbus1 */
2936 if (!isempty(m
->root_container
.signature
) && !BUS_MESSAGE_IS_GVARIANT(m
)) {
2937 r
= message_append_field_signature(m
, BUS_MESSAGE_HEADER_SIGNATURE
, m
->root_container
.signature
, NULL
);
2943 r
= message_append_field_uint32(m
, BUS_MESSAGE_HEADER_UNIX_FDS
, m
->n_fds
);
2948 r
= bus_message_close_header(m
);
2952 if (BUS_MESSAGE_IS_GVARIANT(m
))
2953 m
->header
->dbus2
.cookie
= cookie
;
2955 m
->header
->dbus1
.serial
= (uint32_t) cookie
;
2957 m
->timeout
= m
->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
? 0 : timeout_usec
;
2959 /* Add padding at the end of the fields part, since we know
2960 * the body needs to start at an 8 byte alignment. We made
2961 * sure we allocated enough space for this, so all we need to
2962 * do here is to zero it out. */
2963 a
= ALIGN8(m
->fields_size
) - m
->fields_size
;
2965 memzero((uint8_t*) BUS_MESSAGE_FIELDS(m
) + m
->fields_size
, a
);
2967 /* If this is something we can send as memfd, then let's seal
2968 the memfd now. Note that we can send memfds as payload only
2969 for directed messages, and not for broadcasts. */
2970 if (m
->destination
&& m
->bus
->use_memfd
) {
2971 MESSAGE_FOREACH_PART(part
, i
, m
)
2972 if (part
->memfd
>= 0 &&
2974 (part
->size
> MEMFD_MIN_SIZE
|| m
->bus
->use_memfd
< 0) &&
2975 part
!= m
->body_end
) { /* The last part may never be sent as memfd */
2978 /* Try to seal it if that makes
2979 * sense. First, unmap our own map to
2980 * make sure we don't keep it busy. */
2981 bus_body_part_unmap(part
);
2983 /* Then, sync up real memfd size */
2985 r
= memfd_set_size(part
->memfd
, sz
);
2989 /* Finally, try to seal */
2990 if (memfd_set_sealed(part
->memfd
) >= 0)
2991 part
->sealed
= true;
2995 m
->root_container
.end
= m
->user_body_size
;
2996 m
->root_container
.index
= 0;
2997 m
->root_container
.offset_index
= 0;
2998 m
->root_container
.item_size
= m
->root_container
.n_offsets
> 0 ? m
->root_container
.offsets
[0] : 0;
3005 int bus_body_part_map(struct bus_body_part
*part
) {
3014 if (part
->size
<= 0)
3017 /* For smaller zero parts (as used for padding) we don't need to map anything... */
3018 if (part
->memfd
< 0 && part
->is_zero
&& part
->size
< 8) {
3019 static const uint8_t zeroes
[7] = { };
3020 part
->data
= (void*) zeroes
;
3024 shift
= part
->memfd_offset
- ((part
->memfd_offset
/ page_size()) * page_size());
3025 psz
= PAGE_ALIGN(part
->size
+ shift
);
3027 if (part
->memfd
>= 0)
3028 p
= mmap(NULL
, psz
, PROT_READ
, MAP_PRIVATE
, part
->memfd
, part
->memfd_offset
- shift
);
3029 else if (part
->is_zero
)
3030 p
= mmap(NULL
, psz
, PROT_READ
, MAP_PRIVATE
|MAP_ANONYMOUS
, -1, 0);
3034 if (p
== MAP_FAILED
)
3038 part
->mmap_begin
= p
;
3039 part
->data
= (uint8_t*) p
+ shift
;
3040 part
->munmap_this
= true;
3045 void bus_body_part_unmap(struct bus_body_part
*part
) {
3049 if (part
->memfd
< 0)
3052 if (!part
->mmap_begin
)
3055 if (!part
->munmap_this
)
3058 assert_se(munmap(part
->mmap_begin
, part
->mapped
) == 0);
3060 part
->mmap_begin
= NULL
;
3063 part
->munmap_this
= false;
3068 static int buffer_peek(const void *p
, uint32_t sz
, size_t *rindex
, size_t align
, size_t nbytes
, void **r
) {
3069 size_t k
, start
, end
;
3074 start
= ALIGN_TO((size_t) *rindex
, align
);
3075 end
= start
+ nbytes
;
3080 /* Verify that padding is 0 */
3081 for (k
= *rindex
; k
< start
; k
++)
3082 if (((const uint8_t*) p
)[k
] != 0)
3086 *r
= (uint8_t*) p
+ start
;
3093 static bool message_end_of_signature(sd_bus_message
*m
) {
3094 struct bus_container
*c
;
3098 c
= message_get_last_container(m
);
3099 return !c
->signature
|| c
->signature
[c
->index
] == 0;
3102 static bool message_end_of_array(sd_bus_message
*m
, size_t index
) {
3103 struct bus_container
*c
;
3107 c
= message_get_last_container(m
);
3108 if (c
->enclosing
!= SD_BUS_TYPE_ARRAY
)
3111 if (BUS_MESSAGE_IS_GVARIANT(m
))
3112 return index
>= c
->end
;
3114 assert(c
->array_size
);
3115 return index
>= c
->begin
+ BUS_MESSAGE_BSWAP32(m
, *c
->array_size
);
3119 _public_
int sd_bus_message_at_end(sd_bus_message
*m
, int complete
) {
3120 assert_return(m
, -EINVAL
);
3121 assert_return(m
->sealed
, -EPERM
);
3123 if (complete
&& m
->n_containers
> 0)
3126 if (message_end_of_signature(m
))
3129 if (message_end_of_array(m
, m
->rindex
))
3135 static struct bus_body_part
* find_part(sd_bus_message
*m
, size_t index
, size_t sz
, void **p
) {
3136 struct bus_body_part
*part
;
3142 if (m
->cached_rindex_part
&& index
>= m
->cached_rindex_part_begin
) {
3143 part
= m
->cached_rindex_part
;
3144 begin
= m
->cached_rindex_part_begin
;
3154 if (index
+ sz
<= begin
+ part
->size
) {
3156 r
= bus_body_part_map(part
);
3161 *p
= (uint8_t*) part
->data
+ index
- begin
;
3163 m
->cached_rindex_part
= part
;
3164 m
->cached_rindex_part_begin
= begin
;
3169 begin
+= part
->size
;
3176 static int container_next_item(sd_bus_message
*m
, struct bus_container
*c
, size_t *rindex
) {
3183 if (!BUS_MESSAGE_IS_GVARIANT(m
))
3186 if (c
->enclosing
== SD_BUS_TYPE_ARRAY
) {
3189 sz
= bus_gvariant_get_size(c
->signature
);
3193 if (c
->offset_index
+1 >= c
->n_offsets
)
3196 /* Variable-size array */
3198 alignment
= bus_gvariant_get_alignment(c
->signature
);
3199 assert(alignment
> 0);
3201 *rindex
= ALIGN_TO(c
->offsets
[c
->offset_index
], alignment
);
3202 assert(c
->offsets
[c
->offset_index
+1] >= *rindex
);
3203 c
->item_size
= c
->offsets
[c
->offset_index
+1] - *rindex
;
3206 if (c
->offset_index
+1 >= (c
->end
-c
->begin
)/sz
)
3209 /* Fixed-size array */
3210 *rindex
= c
->begin
+ (c
->offset_index
+1) * sz
;
3216 } else if (IN_SET(c
->enclosing
, 0, SD_BUS_TYPE_STRUCT
, SD_BUS_TYPE_DICT_ENTRY
)) {
3221 if (c
->offset_index
+1 >= c
->n_offsets
)
3224 r
= signature_element_length(c
->signature
+ c
->index
, &n
);
3228 r
= signature_element_length(c
->signature
+ c
->index
+ n
, &j
);
3233 memcpy(t
, c
->signature
+ c
->index
+ n
, j
);
3236 alignment
= bus_gvariant_get_alignment(t
);
3239 assert(alignment
> 0);
3241 *rindex
= ALIGN_TO(c
->offsets
[c
->offset_index
], alignment
);
3242 assert(c
->offsets
[c
->offset_index
+1] >= *rindex
);
3243 c
->item_size
= c
->offsets
[c
->offset_index
+1] - *rindex
;
3247 } else if (c
->enclosing
== SD_BUS_TYPE_VARIANT
)
3250 assert_not_reached("Unknown container type");
3255 /* Reached the end */
3261 static int message_peek_body(
3268 size_t k
, start
, end
, padding
;
3269 struct bus_body_part
*part
;
3276 start
= ALIGN_TO((size_t) *rindex
, align
);
3277 padding
= start
- *rindex
;
3278 end
= start
+ nbytes
;
3280 if (end
> m
->user_body_size
)
3283 part
= find_part(m
, *rindex
, padding
, (void**) &q
);
3288 /* Verify padding */
3289 for (k
= 0; k
< padding
; k
++)
3294 part
= find_part(m
, start
, nbytes
, (void**) &q
);
3295 if (!part
|| (nbytes
> 0 && !q
))
3306 static bool validate_nul(const char *s
, size_t l
) {
3308 /* Check for NUL chars in the string */
3309 if (memchr(s
, 0, l
))
3312 /* Check for NUL termination */
3319 static bool validate_string(const char *s
, size_t l
) {
3321 if (!validate_nul(s
, l
))
3324 /* Check if valid UTF8 */
3325 if (!utf8_is_valid(s
))
3331 static bool validate_signature(const char *s
, size_t l
) {
3333 if (!validate_nul(s
, l
))
3336 /* Check if valid signature */
3337 if (!signature_is_valid(s
, true))
3343 static bool validate_object_path(const char *s
, size_t l
) {
3345 if (!validate_nul(s
, l
))
3348 if (!object_path_is_valid(s
))
3354 _public_
int sd_bus_message_read_basic(sd_bus_message
*m
, char type
, void *p
) {
3355 struct bus_container
*c
;
3360 assert_return(m
, -EINVAL
);
3361 assert_return(m
->sealed
, -EPERM
);
3362 assert_return(bus_type_is_basic(type
), -EINVAL
);
3364 if (message_end_of_signature(m
))
3367 if (message_end_of_array(m
, m
->rindex
))
3370 c
= message_get_last_container(m
);
3371 if (c
->signature
[c
->index
] != type
)
3376 if (BUS_MESSAGE_IS_GVARIANT(m
)) {
3378 if (IN_SET(type
, SD_BUS_TYPE_STRING
, SD_BUS_TYPE_OBJECT_PATH
, SD_BUS_TYPE_SIGNATURE
)) {
3381 /* D-Bus spec: The marshalling formats for the string-like types all end
3382 * with a single zero (NUL) byte, but that byte is not considered to be part
3384 if (c
->item_size
== 0)
3387 r
= message_peek_body(m
, &rindex
, 1, c
->item_size
, &q
);
3391 if (type
== SD_BUS_TYPE_STRING
)
3392 ok
= validate_string(q
, c
->item_size
-1);
3393 else if (type
== SD_BUS_TYPE_OBJECT_PATH
)
3394 ok
= validate_object_path(q
, c
->item_size
-1);
3396 ok
= validate_signature(q
, c
->item_size
-1);
3402 *(const char**) p
= q
;
3406 sz
= bus_gvariant_get_size(CHAR_TO_STR(type
));
3408 if ((size_t) sz
!= c
->item_size
)
3411 align
= bus_gvariant_get_alignment(CHAR_TO_STR(type
));
3414 r
= message_peek_body(m
, &rindex
, align
, c
->item_size
, &q
);
3420 case SD_BUS_TYPE_BYTE
:
3422 *(uint8_t*) p
= *(uint8_t*) q
;
3425 case SD_BUS_TYPE_BOOLEAN
:
3427 *(int*) p
= !!*(uint8_t*) q
;
3430 case SD_BUS_TYPE_INT16
:
3431 case SD_BUS_TYPE_UINT16
:
3433 *(uint16_t*) p
= BUS_MESSAGE_BSWAP16(m
, *(uint16_t*) q
);
3436 case SD_BUS_TYPE_INT32
:
3437 case SD_BUS_TYPE_UINT32
:
3439 *(uint32_t*) p
= BUS_MESSAGE_BSWAP32(m
, *(uint32_t*) q
);
3442 case SD_BUS_TYPE_INT64
:
3443 case SD_BUS_TYPE_UINT64
:
3444 case SD_BUS_TYPE_DOUBLE
:
3446 *(uint64_t*) p
= BUS_MESSAGE_BSWAP64(m
, *(uint64_t*) q
);
3449 case SD_BUS_TYPE_UNIX_FD
: {
3452 j
= BUS_MESSAGE_BSWAP32(m
, *(uint32_t*) q
);
3457 *(int*) p
= m
->fds
[j
];
3463 assert_not_reached("unexpected type");
3467 r
= container_next_item(m
, c
, &rindex
);
3472 if (IN_SET(type
, SD_BUS_TYPE_STRING
, SD_BUS_TYPE_OBJECT_PATH
)) {
3476 r
= message_peek_body(m
, &rindex
, 4, 4, &q
);
3480 l
= BUS_MESSAGE_BSWAP32(m
, *(uint32_t*) q
);
3481 if (l
== UINT32_MAX
)
3482 /* avoid overflow right below */
3485 r
= message_peek_body(m
, &rindex
, 1, l
+1, &q
);
3489 if (type
== SD_BUS_TYPE_OBJECT_PATH
)
3490 ok
= validate_object_path(q
, l
);
3492 ok
= validate_string(q
, l
);
3497 *(const char**) p
= q
;
3499 } else if (type
== SD_BUS_TYPE_SIGNATURE
) {
3502 r
= message_peek_body(m
, &rindex
, 1, 1, &q
);
3508 /* avoid overflow right below */
3511 r
= message_peek_body(m
, &rindex
, 1, l
+1, &q
);
3515 if (!validate_signature(q
, l
))
3519 *(const char**) p
= q
;
3524 align
= bus_type_get_alignment(type
);
3527 sz
= bus_type_get_size(type
);
3530 r
= message_peek_body(m
, &rindex
, align
, sz
, &q
);
3536 case SD_BUS_TYPE_BYTE
:
3538 *(uint8_t*) p
= *(uint8_t*) q
;
3541 case SD_BUS_TYPE_BOOLEAN
:
3543 *(int*) p
= !!*(uint32_t*) q
;
3546 case SD_BUS_TYPE_INT16
:
3547 case SD_BUS_TYPE_UINT16
:
3549 *(uint16_t*) p
= BUS_MESSAGE_BSWAP16(m
, *(uint16_t*) q
);
3552 case SD_BUS_TYPE_INT32
:
3553 case SD_BUS_TYPE_UINT32
:
3555 *(uint32_t*) p
= BUS_MESSAGE_BSWAP32(m
, *(uint32_t*) q
);
3558 case SD_BUS_TYPE_INT64
:
3559 case SD_BUS_TYPE_UINT64
:
3560 case SD_BUS_TYPE_DOUBLE
:
3562 *(uint64_t*) p
= BUS_MESSAGE_BSWAP64(m
, *(uint64_t*) q
);
3565 case SD_BUS_TYPE_UNIX_FD
: {
3568 j
= BUS_MESSAGE_BSWAP32(m
, *(uint32_t*) q
);
3573 *(int*) p
= m
->fds
[j
];
3578 assert_not_reached("Unknown basic type...");
3585 if (c
->enclosing
!= SD_BUS_TYPE_ARRAY
)
3591 static int bus_message_enter_array(
3593 struct bus_container
*c
,
3594 const char *contents
,
3595 uint32_t **array_size
,
3598 size_t *n_offsets
) {
3612 if (!signature_is_single(contents
, true))
3615 if (!c
->signature
|| c
->signature
[c
->index
] == 0)
3618 if (c
->signature
[c
->index
] != SD_BUS_TYPE_ARRAY
)
3621 if (!startswith(c
->signature
+ c
->index
+ 1, contents
))
3626 if (!BUS_MESSAGE_IS_GVARIANT(m
)) {
3630 r
= message_peek_body(m
, &rindex
, 4, 4, &q
);
3634 if (BUS_MESSAGE_BSWAP32(m
, *(uint32_t*) q
) > BUS_ARRAY_MAX_SIZE
)
3637 alignment
= bus_type_get_alignment(contents
[0]);
3641 r
= message_peek_body(m
, &rindex
, alignment
, 0, NULL
);
3645 *array_size
= (uint32_t*) q
;
3647 } else if (c
->item_size
<= 0) {
3649 /* gvariant: empty array */
3654 } else if (bus_gvariant_is_fixed_size(contents
)) {
3656 /* gvariant: fixed length array */
3657 *item_size
= bus_gvariant_get_size(contents
);
3662 size_t where
, previous
= 0, framing
, sz
;
3666 /* gvariant: variable length array */
3667 sz
= bus_gvariant_determine_word_size(c
->item_size
, 0);
3669 where
= rindex
+ c
->item_size
- sz
;
3670 r
= message_peek_body(m
, &where
, 1, sz
, &q
);
3674 framing
= bus_gvariant_read_word_le(q
, sz
);
3675 if (framing
> c
->item_size
- sz
)
3677 if ((c
->item_size
- framing
) % sz
!= 0)
3680 *n_offsets
= (c
->item_size
- framing
) / sz
;
3682 where
= rindex
+ framing
;
3683 r
= message_peek_body(m
, &where
, 1, *n_offsets
* sz
, &q
);
3687 *offsets
= new(size_t, *n_offsets
);
3691 alignment
= bus_gvariant_get_alignment(c
->signature
);
3692 assert(alignment
> 0);
3694 for (i
= 0; i
< *n_offsets
; i
++) {
3697 start
= ALIGN_TO(previous
, alignment
);
3699 x
= bus_gvariant_read_word_le((uint8_t*) q
+ i
* sz
, sz
);
3700 if (x
> c
->item_size
- sz
)
3705 (*offsets
)[i
] = rindex
+ x
;
3709 *item_size
= (*offsets
)[0] - rindex
;
3714 if (c
->enclosing
!= SD_BUS_TYPE_ARRAY
)
3715 c
->index
+= 1 + strlen(contents
);
3720 static int bus_message_enter_variant(
3722 struct bus_container
*c
,
3723 const char *contents
,
3724 size_t *item_size
) {
3736 if (!signature_is_single(contents
, false))
3739 if (*contents
== SD_BUS_TYPE_DICT_ENTRY_BEGIN
)
3742 if (!c
->signature
|| c
->signature
[c
->index
] == 0)
3745 if (c
->signature
[c
->index
] != SD_BUS_TYPE_VARIANT
)
3750 if (BUS_MESSAGE_IS_GVARIANT(m
)) {
3753 k
= strlen(contents
);
3754 if (1+k
> c
->item_size
)
3757 where
= rindex
+ c
->item_size
- (1+k
);
3758 r
= message_peek_body(m
, &where
, 1, 1+k
, &q
);
3762 if (*(char*) q
!= 0)
3765 if (memcmp((uint8_t*) q
+1, contents
, k
))
3768 *item_size
= c
->item_size
- (1+k
);
3771 r
= message_peek_body(m
, &rindex
, 1, 1, &q
);
3777 /* avoid overflow right below */
3780 r
= message_peek_body(m
, &rindex
, 1, l
+1, &q
);
3784 if (!validate_signature(q
, l
))
3787 if (!streq(q
, contents
))
3793 if (c
->enclosing
!= SD_BUS_TYPE_ARRAY
)
3799 static int build_struct_offsets(
3801 const char *signature
,
3805 size_t *n_offsets
) {
3807 unsigned n_variable
= 0, n_total
= 0, v
;
3808 size_t previous
, where
;
3819 if (isempty(signature
)) {
3820 /* Unary type is encoded as *fixed* 1 byte padding */
3821 r
= message_peek_body(m
, &m
->rindex
, 1, 1, &q
);
3825 if (*(uint8_t *) q
!= 0)
3834 sz
= bus_gvariant_determine_word_size(size
, 0);
3838 /* First, loop over signature and count variable elements and
3839 * elements in general. We use this to know how large the
3840 * offset array is at the end of the structure. Note that
3841 * GVariant only stores offsets for all variable size elements
3842 * that are not the last item. */
3848 r
= signature_element_length(p
, &n
);
3857 r
= bus_gvariant_is_fixed_size(t
);
3862 if (r
== 0 && p
[n
] != 0) /* except the last item */
3869 if (size
< n_variable
* sz
)
3872 where
= m
->rindex
+ size
- (n_variable
* sz
);
3873 r
= message_peek_body(m
, &where
, 1, n_variable
* sz
, &q
);
3879 *offsets
= new(size_t, n_total
);
3885 /* Second, loop again and build an offset table */
3887 previous
= m
->rindex
;
3892 r
= signature_element_length(p
, &n
);
3901 size_t align
= bus_gvariant_get_alignment(t
);
3904 /* The possible start of this member after including alignment */
3905 size_t start
= ALIGN_TO(previous
, align
);
3907 k
= bus_gvariant_get_size(t
);
3915 x
= bus_gvariant_read_word_le((uint8_t*) q
+ v
*sz
, sz
);
3919 /* The last item's end is determined
3920 * from the start of the offset array */
3921 x
= size
- (n_variable
* sz
);
3923 offset
= m
->rindex
+ x
;
3925 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
3926 "For type %s with alignment %zu, message specifies offset %zu which is smaller than previous end %zu + alignment = %zu",
3936 previous
= (*offsets
)[(*n_offsets
)++] = offset
;
3941 assert(*n_offsets
== n_total
);
3943 *item_size
= (*offsets
)[0] - m
->rindex
;
3947 static int enter_struct_or_dict_entry(
3949 struct bus_container
*c
,
3950 const char *contents
,
3953 size_t *n_offsets
) {
3964 if (!BUS_MESSAGE_IS_GVARIANT(m
)) {
3967 r
= message_peek_body(m
, &m
->rindex
, 8, 0, NULL
);
3972 /* gvariant with contents */
3973 return build_struct_offsets(m
, contents
, c
->item_size
, item_size
, offsets
, n_offsets
);
3978 static int bus_message_enter_struct(
3980 struct bus_container
*c
,
3981 const char *contents
,
3984 size_t *n_offsets
) {
3996 if (!signature_is_valid(contents
, false))
3999 if (!c
->signature
|| c
->signature
[c
->index
] == 0)
4002 l
= strlen(contents
);
4004 if (c
->signature
[c
->index
] != SD_BUS_TYPE_STRUCT_BEGIN
||
4005 !startswith(c
->signature
+ c
->index
+ 1, contents
) ||
4006 c
->signature
[c
->index
+ 1 + l
] != SD_BUS_TYPE_STRUCT_END
)
4009 r
= enter_struct_or_dict_entry(m
, c
, contents
, item_size
, offsets
, n_offsets
);
4013 if (c
->enclosing
!= SD_BUS_TYPE_ARRAY
)
4014 c
->index
+= 1 + l
+ 1;
4019 static int bus_message_enter_dict_entry(
4021 struct bus_container
*c
,
4022 const char *contents
,
4025 size_t *n_offsets
) {
4034 if (!signature_is_pair(contents
))
4037 if (c
->enclosing
!= SD_BUS_TYPE_ARRAY
)
4040 if (!c
->signature
|| c
->signature
[c
->index
] == 0)
4043 l
= strlen(contents
);
4045 if (c
->signature
[c
->index
] != SD_BUS_TYPE_DICT_ENTRY_BEGIN
||
4046 !startswith(c
->signature
+ c
->index
+ 1, contents
) ||
4047 c
->signature
[c
->index
+ 1 + l
] != SD_BUS_TYPE_DICT_ENTRY_END
)
4050 r
= enter_struct_or_dict_entry(m
, c
, contents
, item_size
, offsets
, n_offsets
);
4054 if (c
->enclosing
!= SD_BUS_TYPE_ARRAY
)
4055 c
->index
+= 1 + l
+ 1;
4060 _public_
int sd_bus_message_enter_container(sd_bus_message
*m
,
4062 const char *contents
) {
4063 struct bus_container
*c
;
4064 uint32_t *array_size
= NULL
;
4065 _cleanup_free_
char *signature
= NULL
;
4067 _cleanup_free_
size_t *offsets
= NULL
;
4068 size_t n_offsets
= 0, item_size
= 0;
4071 assert_return(m
, -EINVAL
);
4072 assert_return(m
->sealed
, -EPERM
);
4073 assert_return(type
!= 0 || !contents
, -EINVAL
);
4075 if (type
== 0 || !contents
) {
4079 /* Allow entering into anonymous containers */
4080 r
= sd_bus_message_peek_type(m
, &tt
, &cc
);
4084 if (type
!= 0 && type
!= tt
)
4087 if (contents
&& !streq(contents
, cc
))
4095 * We enforce a global limit on container depth, that is much
4096 * higher than the 32 structs and 32 arrays the specification
4097 * mandates. This is simpler to implement for us, and we need
4098 * this only to ensure our container array doesn't grow
4099 * without bounds. We are happy to return any data from a
4100 * message as long as the data itself is valid, even if the
4101 * overall message might be not.
4103 * Note that the message signature is validated when
4104 * parsing the headers, and that validation does check the
4107 * Note that the specification defines no limits on the depth
4108 * of stacked variants, but we do.
4110 if (m
->n_containers
>= BUS_CONTAINER_DEPTH
)
4113 if (!GREEDY_REALLOC(m
->containers
, m
->containers_allocated
, m
->n_containers
+ 1))
4116 if (message_end_of_signature(m
))
4119 if (message_end_of_array(m
, m
->rindex
))
4122 c
= message_get_last_container(m
);
4124 signature
= strdup(contents
);
4128 c
->saved_index
= c
->index
;
4131 if (type
== SD_BUS_TYPE_ARRAY
)
4132 r
= bus_message_enter_array(m
, c
, contents
, &array_size
, &item_size
, &offsets
, &n_offsets
);
4133 else if (type
== SD_BUS_TYPE_VARIANT
)
4134 r
= bus_message_enter_variant(m
, c
, contents
, &item_size
);
4135 else if (type
== SD_BUS_TYPE_STRUCT
)
4136 r
= bus_message_enter_struct(m
, c
, contents
, &item_size
, &offsets
, &n_offsets
);
4137 else if (type
== SD_BUS_TYPE_DICT_ENTRY
)
4138 r
= bus_message_enter_dict_entry(m
, c
, contents
, &item_size
, &offsets
, &n_offsets
);
4144 /* OK, let's fill it in */
4145 if (BUS_MESSAGE_IS_GVARIANT(m
) &&
4146 type
== SD_BUS_TYPE_STRUCT
&&
4148 end
= m
->rindex
+ 0;
4150 end
= m
->rindex
+ c
->item_size
;
4152 m
->containers
[m
->n_containers
++] = (struct bus_container
) {
4154 .signature
= TAKE_PTR(signature
),
4158 /* Unary type has fixed size of 1, but virtual size of 0 */
4160 .array_size
= array_size
,
4161 .item_size
= item_size
,
4162 .offsets
= TAKE_PTR(offsets
),
4163 .n_offsets
= n_offsets
,
4169 _public_
int sd_bus_message_exit_container(sd_bus_message
*m
) {
4170 struct bus_container
*c
;
4174 assert_return(m
, -EINVAL
);
4175 assert_return(m
->sealed
, -EPERM
);
4176 assert_return(m
->n_containers
> 0, -ENXIO
);
4178 c
= message_get_last_container(m
);
4180 if (c
->enclosing
!= SD_BUS_TYPE_ARRAY
) {
4181 if (c
->signature
&& c
->signature
[c
->index
] != 0)
4185 if (BUS_MESSAGE_IS_GVARIANT(m
)) {
4186 if (m
->rindex
< c
->end
)
4189 } else if (c
->enclosing
== SD_BUS_TYPE_ARRAY
) {
4192 l
= BUS_MESSAGE_BSWAP32(m
, *c
->array_size
);
4193 if (c
->begin
+ l
!= m
->rindex
)
4197 message_free_last_container(m
);
4199 c
= message_get_last_container(m
);
4201 c
->index
= c
->saved_index
;
4202 r
= container_next_item(m
, c
, &m
->rindex
);
4210 static void message_quit_container(sd_bus_message
*m
) {
4211 struct bus_container
*c
;
4215 assert(m
->n_containers
> 0);
4218 c
= message_get_last_container(m
);
4219 assert(m
->rindex
>= c
->before
);
4220 m
->rindex
= c
->before
;
4222 /* Free container */
4223 message_free_last_container(m
);
4225 /* Correct index of new top-level container */
4226 c
= message_get_last_container(m
);
4227 c
->index
= c
->saved_index
;
4230 _public_
int sd_bus_message_peek_type(sd_bus_message
*m
, char *type
, const char **contents
) {
4231 struct bus_container
*c
;
4234 assert_return(m
, -EINVAL
);
4235 assert_return(m
->sealed
, -EPERM
);
4237 if (message_end_of_signature(m
))
4240 if (message_end_of_array(m
, m
->rindex
))
4243 c
= message_get_last_container(m
);
4245 if (bus_type_is_basic(c
->signature
[c
->index
])) {
4249 *type
= c
->signature
[c
->index
];
4253 if (c
->signature
[c
->index
] == SD_BUS_TYPE_ARRAY
) {
4258 r
= signature_element_length(c
->signature
+c
->index
+1, &l
);
4262 /* signature_element_length does verification internally */
4264 /* The array element must not be empty */
4266 if (free_and_strndup(&c
->peeked_signature
,
4267 c
->signature
+ c
->index
+ 1, l
) < 0)
4270 *contents
= c
->peeked_signature
;
4274 *type
= SD_BUS_TYPE_ARRAY
;
4279 if (IN_SET(c
->signature
[c
->index
], SD_BUS_TYPE_STRUCT_BEGIN
, SD_BUS_TYPE_DICT_ENTRY_BEGIN
)) {
4284 r
= signature_element_length(c
->signature
+c
->index
, &l
);
4289 if (free_and_strndup(&c
->peeked_signature
,
4290 c
->signature
+ c
->index
+ 1, l
- 2) < 0)
4293 *contents
= c
->peeked_signature
;
4297 *type
= c
->signature
[c
->index
] == SD_BUS_TYPE_STRUCT_BEGIN
? SD_BUS_TYPE_STRUCT
: SD_BUS_TYPE_DICT_ENTRY
;
4302 if (c
->signature
[c
->index
] == SD_BUS_TYPE_VARIANT
) {
4306 if (BUS_MESSAGE_IS_GVARIANT(m
)) {
4309 if (c
->item_size
< 2)
4312 /* Look for the NUL delimiter that
4313 separates the payload from the
4314 signature. Since the body might be
4315 in a different part that then the
4316 signature we map byte by byte. */
4318 for (k
= 2; k
<= c
->item_size
; k
++) {
4321 where
= m
->rindex
+ c
->item_size
- k
;
4322 r
= message_peek_body(m
, &where
, 1, k
, &q
);
4326 if (*(char*) q
== 0)
4330 if (k
> c
->item_size
)
4333 if (free_and_strndup(&c
->peeked_signature
,
4334 (char*) q
+ 1, k
- 1) < 0)
4337 if (!signature_is_valid(c
->peeked_signature
, true))
4340 *contents
= c
->peeked_signature
;
4345 r
= message_peek_body(m
, &rindex
, 1, 1, &q
);
4351 /* avoid overflow right below */
4354 r
= message_peek_body(m
, &rindex
, 1, l
+1, &q
);
4358 if (!validate_signature(q
, l
))
4366 *type
= SD_BUS_TYPE_VARIANT
;
4381 _public_
int sd_bus_message_rewind(sd_bus_message
*m
, int complete
) {
4382 struct bus_container
*c
;
4384 assert_return(m
, -EINVAL
);
4385 assert_return(m
->sealed
, -EPERM
);
4388 message_reset_containers(m
);
4391 c
= message_get_last_container(m
);
4393 c
= message_get_last_container(m
);
4396 m
->rindex
= c
->begin
;
4399 c
->offset_index
= 0;
4400 c
->item_size
= (c
->n_offsets
> 0 ? c
->offsets
[0] : c
->end
) - c
->begin
;
4402 return !isempty(c
->signature
);
4405 _public_
int sd_bus_message_readv(
4410 unsigned n_array
, n_struct
;
4411 TypeStack stack
[BUS_CONTAINER_DEPTH
];
4412 unsigned stack_ptr
= 0;
4413 unsigned n_loop
= 0;
4416 assert_return(m
, -EINVAL
);
4417 assert_return(m
->sealed
, -EPERM
);
4418 assert_return(types
, -EINVAL
);
4423 /* Ideally, we'd just call ourselves recursively on every
4424 * complex type. However, the state of a va_list that is
4425 * passed to a function is undefined after that function
4426 * returns. This means we need to decode the va_list linearly
4427 * in a single stackframe. We hence implement our own
4428 * home-grown stack in an array. */
4430 n_array
= (unsigned) -1; /* length of current array entries */
4431 n_struct
= strlen(types
); /* length of current struct contents signature */
4438 if (n_array
== 0 || (n_array
== (unsigned) -1 && n_struct
== 0)) {
4439 r
= type_stack_pop(stack
, ELEMENTSOF(stack
), &stack_ptr
, &types
, &n_struct
, &n_array
);
4445 r
= sd_bus_message_exit_container(m
);
4453 if (n_array
!= (unsigned) -1)
4462 case SD_BUS_TYPE_BYTE
:
4463 case SD_BUS_TYPE_BOOLEAN
:
4464 case SD_BUS_TYPE_INT16
:
4465 case SD_BUS_TYPE_UINT16
:
4466 case SD_BUS_TYPE_INT32
:
4467 case SD_BUS_TYPE_UINT32
:
4468 case SD_BUS_TYPE_INT64
:
4469 case SD_BUS_TYPE_UINT64
:
4470 case SD_BUS_TYPE_DOUBLE
:
4471 case SD_BUS_TYPE_STRING
:
4472 case SD_BUS_TYPE_OBJECT_PATH
:
4473 case SD_BUS_TYPE_SIGNATURE
:
4474 case SD_BUS_TYPE_UNIX_FD
: {
4477 p
= va_arg(ap
, void*);
4478 r
= sd_bus_message_read_basic(m
, *t
, p
);
4491 case SD_BUS_TYPE_ARRAY
: {
4494 r
= signature_element_length(t
+ 1, &k
);
4500 memcpy(s
, t
+ 1, k
);
4503 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, s
);
4514 if (n_array
== (unsigned) -1) {
4519 r
= type_stack_push(stack
, ELEMENTSOF(stack
), &stack_ptr
, types
, n_struct
, n_array
);
4525 n_array
= va_arg(ap
, unsigned);
4530 case SD_BUS_TYPE_VARIANT
: {
4533 s
= va_arg(ap
, const char *);
4537 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_VARIANT
, s
);
4547 r
= type_stack_push(stack
, ELEMENTSOF(stack
), &stack_ptr
, types
, n_struct
, n_array
);
4552 n_struct
= strlen(s
);
4553 n_array
= (unsigned) -1;
4558 case SD_BUS_TYPE_STRUCT_BEGIN
:
4559 case SD_BUS_TYPE_DICT_ENTRY_BEGIN
: {
4562 r
= signature_element_length(t
, &k
);
4568 memcpy(s
, t
+ 1, k
- 2);
4571 r
= sd_bus_message_enter_container(m
, *t
== SD_BUS_TYPE_STRUCT_BEGIN
? SD_BUS_TYPE_STRUCT
: SD_BUS_TYPE_DICT_ENTRY
, s
);
4581 if (n_array
== (unsigned) -1) {
4586 r
= type_stack_push(stack
, ELEMENTSOF(stack
), &stack_ptr
, types
, n_struct
, n_array
);
4592 n_array
= (unsigned) -1;
4605 _public_
int sd_bus_message_read(sd_bus_message
*m
, const char *types
, ...) {
4609 va_start(ap
, types
);
4610 r
= sd_bus_message_readv(m
, types
, ap
);
4616 _public_
int sd_bus_message_skip(sd_bus_message
*m
, const char *types
) {
4619 assert_return(m
, -EINVAL
);
4620 assert_return(m
->sealed
, -EPERM
);
4622 /* If types is NULL, read exactly one element */
4624 struct bus_container
*c
;
4627 if (message_end_of_signature(m
))
4630 if (message_end_of_array(m
, m
->rindex
))
4633 c
= message_get_last_container(m
);
4635 r
= signature_element_length(c
->signature
+ c
->index
, &l
);
4639 types
= strndupa(c
->signature
+ c
->index
, l
);
4644 case 0: /* Nothing to drop */
4647 case SD_BUS_TYPE_BYTE
:
4648 case SD_BUS_TYPE_BOOLEAN
:
4649 case SD_BUS_TYPE_INT16
:
4650 case SD_BUS_TYPE_UINT16
:
4651 case SD_BUS_TYPE_INT32
:
4652 case SD_BUS_TYPE_UINT32
:
4653 case SD_BUS_TYPE_INT64
:
4654 case SD_BUS_TYPE_UINT64
:
4655 case SD_BUS_TYPE_DOUBLE
:
4656 case SD_BUS_TYPE_STRING
:
4657 case SD_BUS_TYPE_OBJECT_PATH
:
4658 case SD_BUS_TYPE_SIGNATURE
:
4659 case SD_BUS_TYPE_UNIX_FD
:
4661 r
= sd_bus_message_read_basic(m
, *types
, NULL
);
4665 r
= sd_bus_message_skip(m
, types
+ 1);
4671 case SD_BUS_TYPE_ARRAY
: {
4674 r
= signature_element_length(types
+ 1, &k
);
4680 memcpy(s
, types
+1, k
);
4683 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, s
);
4688 r
= sd_bus_message_skip(m
, s
);
4695 r
= sd_bus_message_exit_container(m
);
4700 r
= sd_bus_message_skip(m
, types
+ 1 + k
);
4707 case SD_BUS_TYPE_VARIANT
: {
4708 const char *contents
;
4711 r
= sd_bus_message_peek_type(m
, &x
, &contents
);
4715 if (x
!= SD_BUS_TYPE_VARIANT
)
4718 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_VARIANT
, contents
);
4722 r
= sd_bus_message_skip(m
, contents
);
4727 r
= sd_bus_message_exit_container(m
);
4731 r
= sd_bus_message_skip(m
, types
+ 1);
4738 case SD_BUS_TYPE_STRUCT_BEGIN
:
4739 case SD_BUS_TYPE_DICT_ENTRY_BEGIN
: {
4742 r
= signature_element_length(types
, &k
);
4748 memcpy(s
, types
+1, k
-2);
4751 r
= sd_bus_message_enter_container(m
, *types
== SD_BUS_TYPE_STRUCT_BEGIN
? SD_BUS_TYPE_STRUCT
: SD_BUS_TYPE_DICT_ENTRY
, s
);
4755 r
= sd_bus_message_skip(m
, s
);
4759 r
= sd_bus_message_exit_container(m
);
4764 r
= sd_bus_message_skip(m
, types
+ k
);
4776 _public_
int sd_bus_message_read_array(
4782 struct bus_container
*c
;
4788 assert_return(m
, -EINVAL
);
4789 assert_return(m
->sealed
, -EPERM
);
4790 assert_return(bus_type_is_trivial(type
), -EINVAL
);
4791 assert_return(ptr
, -EINVAL
);
4792 assert_return(size
, -EINVAL
);
4793 assert_return(!BUS_MESSAGE_NEED_BSWAP(m
), -EOPNOTSUPP
);
4795 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, CHAR_TO_STR(type
));
4799 c
= message_get_last_container(m
);
4801 if (BUS_MESSAGE_IS_GVARIANT(m
)) {
4802 align
= bus_gvariant_get_alignment(CHAR_TO_STR(type
));
4806 sz
= c
->end
- c
->begin
;
4808 align
= bus_type_get_alignment(type
);
4812 sz
= BUS_MESSAGE_BSWAP32(m
, *c
->array_size
);
4816 /* Zero length array, let's return some aligned
4817 * pointer that is not NULL */
4818 p
= (uint8_t*) align
;
4820 r
= message_peek_body(m
, &m
->rindex
, align
, sz
, &p
);
4825 r
= sd_bus_message_exit_container(m
);
4829 *ptr
= (const void*) p
;
4835 message_quit_container(m
);
4839 static int message_peek_fields(
4850 return buffer_peek(BUS_MESSAGE_FIELDS(m
), m
->fields_size
, rindex
, align
, nbytes
, ret
);
4853 static int message_peek_field_uint32(
4865 if (BUS_MESSAGE_IS_GVARIANT(m
) && item_size
!= 4)
4868 /* identical for gvariant and dbus1 */
4870 r
= message_peek_fields(m
, ri
, 4, 4, &q
);
4875 *ret
= BUS_MESSAGE_BSWAP32(m
, *(uint32_t*) q
);
4880 static int message_peek_field_uint64(
4892 if (BUS_MESSAGE_IS_GVARIANT(m
) && item_size
!= 8)
4895 /* identical for gvariant and dbus1 */
4897 r
= message_peek_fields(m
, ri
, 8, 8, &q
);
4902 *ret
= BUS_MESSAGE_BSWAP64(m
, *(uint64_t*) q
);
4907 static int message_peek_field_string(
4909 bool (*validate
)(const char *p
),
4921 if (BUS_MESSAGE_IS_GVARIANT(m
)) {
4926 r
= message_peek_fields(m
, ri
, 1, item_size
, &q
);
4932 r
= message_peek_field_uint32(m
, ri
, 4, &l
);
4936 if (l
== UINT32_MAX
)
4937 /* avoid overflow right below */
4940 r
= message_peek_fields(m
, ri
, 1, l
+1, &q
);
4946 if (!validate_nul(q
, l
))
4952 if (!validate_string(q
, l
))
4962 static int message_peek_field_signature(
4975 if (BUS_MESSAGE_IS_GVARIANT(m
)) {
4980 r
= message_peek_fields(m
, ri
, 1, item_size
, &q
);
4986 r
= message_peek_fields(m
, ri
, 1, 1, &q
);
4992 /* avoid overflow right below */
4995 r
= message_peek_fields(m
, ri
, 1, l
+1, &q
);
5000 if (!validate_signature(q
, l
))
5009 static int message_skip_fields(
5012 uint32_t array_size
,
5013 const char **signature
) {
5015 size_t original_index
;
5021 assert(!BUS_MESSAGE_IS_GVARIANT(m
));
5023 original_index
= *ri
;
5029 if (array_size
!= (uint32_t) -1 &&
5030 array_size
<= *ri
- original_index
)
5037 if (t
== SD_BUS_TYPE_STRING
) {
5039 r
= message_peek_field_string(m
, NULL
, ri
, 0, NULL
);
5045 } else if (t
== SD_BUS_TYPE_OBJECT_PATH
) {
5047 r
= message_peek_field_string(m
, object_path_is_valid
, ri
, 0, NULL
);
5053 } else if (t
== SD_BUS_TYPE_SIGNATURE
) {
5055 r
= message_peek_field_signature(m
, ri
, 0, NULL
);
5061 } else if (bus_type_is_basic(t
)) {
5064 align
= bus_type_get_alignment(t
);
5065 k
= bus_type_get_size(t
);
5066 assert(align
> 0 && k
> 0);
5068 r
= message_peek_fields(m
, ri
, align
, k
, NULL
);
5074 } else if (t
== SD_BUS_TYPE_ARRAY
) {
5076 r
= signature_element_length(*signature
+ 1, &l
);
5082 char sig
[l
+ 1], *s
= sig
;
5086 strncpy(sig
, *signature
+ 1, l
);
5089 alignment
= bus_type_get_alignment(sig
[0]);
5093 r
= message_peek_field_uint32(m
, ri
, 0, &nas
);
5096 if (nas
> BUS_ARRAY_MAX_SIZE
)
5099 r
= message_peek_fields(m
, ri
, alignment
, 0, NULL
);
5103 r
= message_skip_fields(m
, ri
, nas
, (const char**) &s
);
5108 (*signature
) += 1 + l
;
5110 } else if (t
== SD_BUS_TYPE_VARIANT
) {
5113 r
= message_peek_field_signature(m
, ri
, 0, &s
);
5117 r
= message_skip_fields(m
, ri
, (uint32_t) -1, (const char**) &s
);
5123 } else if (IN_SET(t
, SD_BUS_TYPE_STRUCT
, SD_BUS_TYPE_DICT_ENTRY
)) {
5125 r
= signature_element_length(*signature
, &l
);
5131 char sig
[l
+ 1], *s
= sig
;
5132 strncpy(sig
, *signature
+ 1, l
);
5135 r
= message_skip_fields(m
, ri
, (uint32_t) -1, (const char**) &s
);
5146 int bus_message_parse_fields(sd_bus_message
*m
) {
5149 uint32_t unix_fds
= 0;
5150 bool unix_fds_set
= false;
5151 void *offsets
= NULL
;
5152 unsigned n_offsets
= 0;
5158 if (BUS_MESSAGE_IS_GVARIANT(m
)) {
5161 /* Read the signature from the end of the body variant first */
5162 sz
= bus_gvariant_determine_word_size(BUS_MESSAGE_SIZE(m
), 0);
5163 if (m
->footer_accessible
< 1 + sz
)
5166 p
= (char*) m
->footer
+ m
->footer_accessible
- (1 + sz
);
5168 if (p
< (char*) m
->footer
)
5172 _cleanup_free_
char *k
= NULL
;
5175 /* We found the beginning of the signature
5176 * string, yay! We require the body to be a
5177 * structure, so verify it and then strip the
5178 * opening/closing brackets. */
5180 l
= (char*) m
->footer
+ m
->footer_accessible
- p
- (1 + sz
);
5182 p
[1] != SD_BUS_TYPE_STRUCT_BEGIN
||
5183 p
[1 + l
- 1] != SD_BUS_TYPE_STRUCT_END
)
5186 k
= memdup_suffix0(p
+ 1 + 1, l
- 2);
5190 if (!signature_is_valid(k
, true))
5193 free_and_replace(m
->root_container
.signature
, k
);
5200 /* Calculate the actual user body size, by removing
5201 * the trailing variant signature and struct offset
5203 m
->user_body_size
= m
->body_size
- ((char*) m
->footer
+ m
->footer_accessible
- p
);
5205 /* Pull out the offset table for the fields array, if any */
5206 if (m
->fields_size
> 0) {
5207 sz
= bus_gvariant_determine_word_size(m
->fields_size
, 0);
5212 if (m
->fields_size
< sz
)
5215 ri
= m
->fields_size
- sz
;
5216 r
= message_peek_fields(m
, &ri
, 1, sz
, &q
);
5220 framing
= bus_gvariant_read_word_le(q
, sz
);
5221 if (framing
>= m
->fields_size
- sz
)
5223 if ((m
->fields_size
- framing
) % sz
!= 0)
5227 r
= message_peek_fields(m
, &ri
, 1, m
->fields_size
- framing
, &offsets
);
5231 n_offsets
= (m
->fields_size
- framing
) / sz
;
5235 m
->user_body_size
= m
->body_size
;
5238 while (ri
< m
->fields_size
) {
5239 _cleanup_free_
char *sig
= NULL
;
5240 const char *signature
;
5241 uint64_t field_type
;
5242 size_t item_size
= (size_t) -1;
5244 if (BUS_MESSAGE_IS_GVARIANT(m
)) {
5253 ri
= ALIGN_TO(bus_gvariant_read_word_le((uint8_t*) offsets
+ (i
-1)*sz
, sz
), 8);
5255 r
= message_peek_fields(m
, &ri
, 8, 8, (void**) &u64
);
5259 field_type
= BUS_MESSAGE_BSWAP64(m
, *u64
);
5263 r
= message_peek_fields(m
, &ri
, 8, 1, (void**) &u8
);
5270 if (BUS_MESSAGE_IS_GVARIANT(m
)) {
5275 end
= bus_gvariant_read_word_le((uint8_t*) offsets
+ i
*sz
, sz
);
5280 where
= ri
= ALIGN_TO(ri
, 8);
5281 item_size
= end
- ri
;
5282 r
= message_peek_fields(m
, &where
, 1, item_size
, &q
);
5286 b
= memrchr(q
, 0, item_size
);
5290 sig
= memdup_suffix0(b
+1, item_size
- (b
+1-(char*) q
));
5295 item_size
= b
- (char*) q
;
5297 r
= message_peek_field_signature(m
, &ri
, 0, &signature
);
5302 switch (field_type
) {
5304 case _BUS_MESSAGE_HEADER_INVALID
:
5307 case BUS_MESSAGE_HEADER_PATH
:
5312 if (!streq(signature
, "o"))
5315 r
= message_peek_field_string(m
, object_path_is_valid
, &ri
, item_size
, &m
->path
);
5318 case BUS_MESSAGE_HEADER_INTERFACE
:
5323 if (!streq(signature
, "s"))
5326 r
= message_peek_field_string(m
, interface_name_is_valid
, &ri
, item_size
, &m
->interface
);
5329 case BUS_MESSAGE_HEADER_MEMBER
:
5334 if (!streq(signature
, "s"))
5337 r
= message_peek_field_string(m
, member_name_is_valid
, &ri
, item_size
, &m
->member
);
5340 case BUS_MESSAGE_HEADER_ERROR_NAME
:
5345 if (!streq(signature
, "s"))
5348 r
= message_peek_field_string(m
, error_name_is_valid
, &ri
, item_size
, &m
->error
.name
);
5350 m
->error
._need_free
= -1;
5354 case BUS_MESSAGE_HEADER_DESTINATION
:
5359 if (!streq(signature
, "s"))
5362 r
= message_peek_field_string(m
, service_name_is_valid
, &ri
, item_size
, &m
->destination
);
5365 case BUS_MESSAGE_HEADER_SENDER
:
5370 if (!streq(signature
, "s"))
5373 r
= message_peek_field_string(m
, service_name_is_valid
, &ri
, item_size
, &m
->sender
);
5375 if (r
>= 0 && m
->sender
[0] == ':' && m
->bus
->bus_client
) {
5376 m
->creds
.unique_name
= (char*) m
->sender
;
5377 m
->creds
.mask
|= SD_BUS_CREDS_UNIQUE_NAME
& m
->bus
->creds_mask
;
5382 case BUS_MESSAGE_HEADER_SIGNATURE
: {
5386 if (BUS_MESSAGE_IS_GVARIANT(m
)) /* only applies to dbus1 */
5389 if (m
->root_container
.signature
)
5392 if (!streq(signature
, "g"))
5395 r
= message_peek_field_signature(m
, &ri
, item_size
, &s
);
5403 free_and_replace(m
->root_container
.signature
, c
);
5407 case BUS_MESSAGE_HEADER_REPLY_SERIAL
:
5409 if (m
->reply_cookie
!= 0)
5412 if (BUS_MESSAGE_IS_GVARIANT(m
)) {
5413 /* 64bit on dbus2 */
5415 if (!streq(signature
, "t"))
5418 r
= message_peek_field_uint64(m
, &ri
, item_size
, &m
->reply_cookie
);
5422 /* 32bit on dbus1 */
5425 if (!streq(signature
, "u"))
5428 r
= message_peek_field_uint32(m
, &ri
, item_size
, &serial
);
5432 m
->reply_cookie
= serial
;
5435 if (m
->reply_cookie
== 0)
5440 case BUS_MESSAGE_HEADER_UNIX_FDS
:
5444 if (!streq(signature
, "u"))
5447 r
= message_peek_field_uint32(m
, &ri
, item_size
, &unix_fds
);
5451 unix_fds_set
= true;
5455 if (!BUS_MESSAGE_IS_GVARIANT(m
))
5456 r
= message_skip_fields(m
, &ri
, (uint32_t) -1, (const char **) &signature
);
5465 if (m
->n_fds
!= unix_fds
)
5468 switch (m
->header
->type
) {
5470 case SD_BUS_MESSAGE_SIGNAL
:
5471 if (!m
->path
|| !m
->interface
|| !m
->member
)
5474 if (m
->reply_cookie
!= 0)
5479 case SD_BUS_MESSAGE_METHOD_CALL
:
5481 if (!m
->path
|| !m
->member
)
5484 if (m
->reply_cookie
!= 0)
5489 case SD_BUS_MESSAGE_METHOD_RETURN
:
5491 if (m
->reply_cookie
== 0)
5495 case SD_BUS_MESSAGE_METHOD_ERROR
:
5497 if (m
->reply_cookie
== 0 || !m
->error
.name
)
5502 assert_not_reached("Bad message type");
5505 /* Refuse non-local messages that claim they are local */
5506 if (streq_ptr(m
->path
, "/org/freedesktop/DBus/Local"))
5508 if (streq_ptr(m
->interface
, "org.freedesktop.DBus.Local"))
5510 if (streq_ptr(m
->sender
, "org.freedesktop.DBus.Local"))
5513 m
->root_container
.end
= m
->user_body_size
;
5515 if (BUS_MESSAGE_IS_GVARIANT(m
)) {
5516 r
= build_struct_offsets(
5518 m
->root_container
.signature
,
5520 &m
->root_container
.item_size
,
5521 &m
->root_container
.offsets
,
5522 &m
->root_container
.n_offsets
);
5529 /* Try to read the error message, but if we can't it's a non-issue */
5530 if (m
->header
->type
== SD_BUS_MESSAGE_METHOD_ERROR
)
5531 (void) sd_bus_message_read(m
, "s", &m
->error
.message
);
5536 _public_
int sd_bus_message_set_destination(sd_bus_message
*m
, const char *destination
) {
5537 assert_return(m
, -EINVAL
);
5538 assert_return(destination
, -EINVAL
);
5539 assert_return(service_name_is_valid(destination
), -EINVAL
);
5540 assert_return(!m
->sealed
, -EPERM
);
5541 assert_return(!m
->destination
, -EEXIST
);
5543 return message_append_field_string(m
, BUS_MESSAGE_HEADER_DESTINATION
, SD_BUS_TYPE_STRING
, destination
, &m
->destination
);
5546 _public_
int sd_bus_message_set_sender(sd_bus_message
*m
, const char *sender
) {
5547 assert_return(m
, -EINVAL
);
5548 assert_return(sender
, -EINVAL
);
5549 assert_return(service_name_is_valid(sender
), -EINVAL
);
5550 assert_return(!m
->sealed
, -EPERM
);
5551 assert_return(!m
->sender
, -EEXIST
);
5553 return message_append_field_string(m
, BUS_MESSAGE_HEADER_SENDER
, SD_BUS_TYPE_STRING
, sender
, &m
->sender
);
5556 int bus_message_get_blob(sd_bus_message
*m
, void **buffer
, size_t *sz
) {
5560 struct bus_body_part
*part
;
5566 total
= BUS_MESSAGE_SIZE(m
);
5572 e
= mempcpy(p
, m
->header
, BUS_MESSAGE_BODY_BEGIN(m
));
5573 MESSAGE_FOREACH_PART(part
, i
, m
)
5574 e
= mempcpy(e
, part
->data
, part
->size
);
5576 assert(total
== (size_t) ((uint8_t*) e
- (uint8_t*) p
));
5584 int bus_message_read_strv_extend(sd_bus_message
*m
, char ***l
) {
5591 r
= sd_bus_message_enter_container(m
, 'a', "s");
5595 while ((r
= sd_bus_message_read_basic(m
, 's', &s
)) > 0) {
5596 r
= strv_extend(l
, s
);
5603 r
= sd_bus_message_exit_container(m
);
5610 _public_
int sd_bus_message_read_strv(sd_bus_message
*m
, char ***l
) {
5611 _cleanup_strv_free_
char **strv
= NULL
;
5614 assert_return(m
, -EINVAL
);
5615 assert_return(m
->sealed
, -EPERM
);
5616 assert_return(l
, -EINVAL
);
5618 r
= bus_message_read_strv_extend(m
, &strv
);
5622 *l
= TAKE_PTR(strv
);
5626 static int bus_message_get_arg_skip(
5630 const char **_contents
) {
5635 r
= sd_bus_message_rewind(m
, true);
5640 const char *contents
;
5643 r
= sd_bus_message_peek_type(m
, &type
, &contents
);
5649 /* Don't match against arguments after the first one we don't understand */
5650 if (!IN_SET(type
, SD_BUS_TYPE_STRING
, SD_BUS_TYPE_OBJECT_PATH
, SD_BUS_TYPE_SIGNATURE
) &&
5651 !(type
== SD_BUS_TYPE_ARRAY
&& STR_IN_SET(contents
, "s", "o", "g")))
5656 *_contents
= contents
;
5662 r
= sd_bus_message_skip(m
, NULL
);
5669 int bus_message_get_arg(sd_bus_message
*m
, unsigned i
, const char **str
) {
5676 r
= bus_message_get_arg_skip(m
, i
, &type
, NULL
);
5680 if (!IN_SET(type
, SD_BUS_TYPE_STRING
, SD_BUS_TYPE_OBJECT_PATH
, SD_BUS_TYPE_SIGNATURE
))
5683 return sd_bus_message_read_basic(m
, type
, str
);
5686 int bus_message_get_arg_strv(sd_bus_message
*m
, unsigned i
, char ***strv
) {
5687 const char *contents
;
5694 r
= bus_message_get_arg_skip(m
, i
, &type
, &contents
);
5698 if (type
!= SD_BUS_TYPE_ARRAY
)
5700 if (!STR_IN_SET(contents
, "s", "o", "g"))
5703 return sd_bus_message_read_strv(m
, strv
);
5706 _public_
int sd_bus_message_get_errno(sd_bus_message
*m
) {
5707 assert_return(m
, EINVAL
);
5709 if (m
->header
->type
!= SD_BUS_MESSAGE_METHOD_ERROR
)
5712 return sd_bus_error_get_errno(&m
->error
);
5715 _public_
const char* sd_bus_message_get_signature(sd_bus_message
*m
, int complete
) {
5716 struct bus_container
*c
;
5718 assert_return(m
, NULL
);
5720 c
= complete
? &m
->root_container
: message_get_last_container(m
);
5721 return strempty(c
->signature
);
5724 _public_
int sd_bus_message_is_empty(sd_bus_message
*m
) {
5725 assert_return(m
, -EINVAL
);
5727 return isempty(m
->root_container
.signature
);
5730 _public_
int sd_bus_message_has_signature(sd_bus_message
*m
, const char *signature
) {
5731 assert_return(m
, -EINVAL
);
5733 return streq(strempty(m
->root_container
.signature
), strempty(signature
));
5736 _public_
int sd_bus_message_copy(sd_bus_message
*m
, sd_bus_message
*source
, int all
) {
5737 bool done_something
= false;
5740 assert_return(m
, -EINVAL
);
5741 assert_return(source
, -EINVAL
);
5742 assert_return(!m
->sealed
, -EPERM
);
5743 assert_return(source
->sealed
, -EPERM
);
5746 const char *contents
;
5761 r
= sd_bus_message_peek_type(source
, &type
, &contents
);
5767 done_something
= true;
5769 if (bus_type_is_container(type
) > 0) {
5771 r
= sd_bus_message_enter_container(source
, type
, contents
);
5775 r
= sd_bus_message_open_container(m
, type
, contents
);
5779 r
= sd_bus_message_copy(m
, source
, true);
5783 r
= sd_bus_message_close_container(m
);
5787 r
= sd_bus_message_exit_container(source
);
5794 r
= sd_bus_message_read_basic(source
, type
, &basic
);
5800 if (IN_SET(type
, SD_BUS_TYPE_OBJECT_PATH
, SD_BUS_TYPE_SIGNATURE
, SD_BUS_TYPE_STRING
))
5801 r
= sd_bus_message_append_basic(m
, type
, basic
.string
);
5803 r
= sd_bus_message_append_basic(m
, type
, &basic
);
5810 return done_something
;
5813 _public_
int sd_bus_message_verify_type(sd_bus_message
*m
, char type
, const char *contents
) {
5818 assert_return(m
, -EINVAL
);
5819 assert_return(m
->sealed
, -EPERM
);
5820 assert_return(!type
|| bus_type_is_valid(type
), -EINVAL
);
5821 assert_return(!contents
|| signature_is_valid(contents
, true), -EINVAL
);
5822 assert_return(type
|| contents
, -EINVAL
);
5823 assert_return(!contents
|| !type
|| bus_type_is_container(type
), -EINVAL
);
5825 r
= sd_bus_message_peek_type(m
, &t
, &c
);
5829 if (type
!= 0 && type
!= t
)
5832 if (contents
&& !streq_ptr(contents
, c
))
5838 _public_ sd_bus
*sd_bus_message_get_bus(sd_bus_message
*m
) {
5839 assert_return(m
, NULL
);
5844 int bus_message_remarshal(sd_bus
*bus
, sd_bus_message
**m
) {
5845 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*n
= NULL
;
5853 switch ((*m
)->header
->type
) {
5855 case SD_BUS_MESSAGE_SIGNAL
:
5856 r
= sd_bus_message_new_signal(bus
, &n
, (*m
)->path
, (*m
)->interface
, (*m
)->member
);
5862 case SD_BUS_MESSAGE_METHOD_CALL
:
5863 r
= sd_bus_message_new_method_call(bus
, &n
, (*m
)->destination
, (*m
)->path
, (*m
)->interface
, (*m
)->member
);
5869 case SD_BUS_MESSAGE_METHOD_RETURN
:
5870 case SD_BUS_MESSAGE_METHOD_ERROR
:
5872 r
= sd_bus_message_new(bus
, &n
, (*m
)->header
->type
);
5878 n
->reply_cookie
= (*m
)->reply_cookie
;
5880 r
= message_append_reply_cookie(n
, n
->reply_cookie
);
5884 if ((*m
)->header
->type
== SD_BUS_MESSAGE_METHOD_ERROR
&& (*m
)->error
.name
) {
5885 r
= message_append_field_string(n
, BUS_MESSAGE_HEADER_ERROR_NAME
, SD_BUS_TYPE_STRING
, (*m
)->error
.name
, &n
->error
.message
);
5889 n
->error
._need_free
= -1;
5898 if ((*m
)->destination
&& !n
->destination
) {
5899 r
= message_append_field_string(n
, BUS_MESSAGE_HEADER_DESTINATION
, SD_BUS_TYPE_STRING
, (*m
)->destination
, &n
->destination
);
5904 if ((*m
)->sender
&& !n
->sender
) {
5905 r
= message_append_field_string(n
, BUS_MESSAGE_HEADER_SENDER
, SD_BUS_TYPE_STRING
, (*m
)->sender
, &n
->sender
);
5910 n
->header
->flags
|= (*m
)->header
->flags
& (BUS_MESSAGE_NO_REPLY_EXPECTED
|BUS_MESSAGE_NO_AUTO_START
);
5912 r
= sd_bus_message_copy(n
, *m
, true);
5916 timeout
= (*m
)->timeout
;
5917 if (timeout
== 0 && !((*m
)->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
)) {
5918 r
= sd_bus_get_method_call_timeout(bus
, &timeout
);
5923 r
= sd_bus_message_seal(n
, BUS_MESSAGE_COOKIE(*m
), timeout
);
5927 sd_bus_message_unref(*m
);
5933 _public_
int sd_bus_message_get_priority(sd_bus_message
*m
, int64_t *priority
) {
5934 static bool warned
= false;
5936 assert_return(m
, -EINVAL
);
5937 assert_return(priority
, -EINVAL
);
5940 log_debug("sd_bus_message_get_priority() is deprecated and always returns 0.");
5948 _public_
int sd_bus_message_set_priority(sd_bus_message
*m
, int64_t priority
) {
5949 static bool warned
= false;
5951 assert_return(m
, -EINVAL
);
5952 assert_return(!m
->sealed
, -EPERM
);
5955 log_debug("sd_bus_message_set_priority() is deprecated and does nothing.");
5962 _public_
int sd_bus_message_sensitive(sd_bus_message
*m
) {
5963 assert_return(m
, -EINVAL
);
5965 m
->sensitive
= true;