1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "time-util.h"
30 #include "cgroup-util.h"
33 #include "bus-message.h"
34 #include "bus-internal.h"
36 #include "bus-signature.h"
38 static int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored);
40 static void *adjust_pointer(const void *p, void *old_base, size_t sz, void *new_base) {
45 if (old_base == new_base)
48 if ((uint8_t*) p < (uint8_t*) old_base)
51 if ((uint8_t*) p >= (uint8_t*) old_base + sz)
54 return (uint8_t*) new_base + ((uint8_t*) p - (uint8_t*) old_base);
57 static void message_free_part(sd_bus_message *m, struct bus_body_part *part) {
61 if (part->memfd >= 0) {
62 /* If we can reuse the memfd, try that. For that it
63 * can't be sealed yet. */
66 bus_kernel_push_memfd(m->bus, part->memfd, part->data, part->mapped);
69 assert_se(munmap(part->data, part->mapped) == 0);
71 close_nointr_nofail(part->memfd);
74 } else if (part->munmap_this)
75 munmap(part->data, part->mapped);
76 else if (part->free_this)
83 static void message_reset_parts(sd_bus_message *m) {
84 struct bus_body_part *part;
89 while (m->n_body_parts > 0) {
90 struct bus_body_part *next = part->next;
91 message_free_part(m, part);
98 m->cached_rindex_part = NULL;
99 m->cached_rindex_part_begin = 0;
102 static void message_reset_containers(sd_bus_message *m) {
107 for (i = 0; i < m->n_containers; i++)
108 free(m->containers[i].signature);
111 m->containers = NULL;
114 m->root_container.index = 0;
117 static void message_free(sd_bus_message *m) {
123 message_reset_parts(m);
128 if (m->release_kdbus) {
131 off = (uint8_t *)m->kdbus - (uint8_t *)m->bus->kdbus_buffer;
132 ioctl(m->bus->input_fd, KDBUS_CMD_MSG_RELEASE, &off);
136 sd_bus_unref(m->bus);
139 close_many(m->fds, m->n_fds);
143 if (m->iovec != m->iovec_fixed)
146 free(m->cmdline_array);
148 message_reset_containers(m);
149 free(m->root_container.signature);
151 free(m->peeked_signature);
159 static void *message_extend_fields(sd_bus_message *m, size_t align, size_t sz) {
161 size_t old_size, new_size, start;
168 old_size = sizeof(struct bus_header) + m->header->fields_size;
169 start = ALIGN_TO(old_size, align);
170 new_size = start + sz;
172 if (old_size == new_size)
173 return (uint8_t*) m->header + old_size;
175 if (new_size > (size_t) ((uint32_t) -1))
178 if (m->free_header) {
179 np = realloc(m->header, ALIGN8(new_size));
183 /* Initially, the header is allocated as part of of
184 * the sd_bus_message itself, let's replace it by
187 np = malloc(ALIGN8(new_size));
191 memcpy(np, m->header, sizeof(struct bus_header));
194 /* Zero out padding */
195 if (start > old_size)
196 memset((uint8_t*) np + old_size, 0, start - old_size);
200 m->header->fields_size = new_size - sizeof(struct bus_header);
202 /* Adjust quick access pointers */
203 m->path = adjust_pointer(m->path, op, old_size, m->header);
204 m->interface = adjust_pointer(m->interface, op, old_size, m->header);
205 m->member = adjust_pointer(m->member, op, old_size, m->header);
206 m->destination = adjust_pointer(m->destination, op, old_size, m->header);
207 m->sender = adjust_pointer(m->sender, op, old_size, m->header);
208 m->error.name = adjust_pointer(m->error.name, op, old_size, m->header);
210 m->free_header = true;
212 return (uint8_t*) np + start;
219 static int message_append_field_string(
232 if (l > (size_t) (uint32_t) -1)
235 /* field id byte + signature length + signature 's' + NUL + string length + string + NUL */
236 p = message_extend_fields(m, 8, 4 + 4 + l + 1);
245 ((uint32_t*) p)[1] = l;
246 memcpy(p + 8, s, l + 1);
249 *ret = (char*) p + 8;
254 static int message_append_field_signature(
269 /* field id byte + signature length + signature 'g' + NUL + string length + string + NUL */
270 p = message_extend_fields(m, 8, 4 + 1 + l + 1);
276 p[2] = SD_BUS_TYPE_SIGNATURE;
279 memcpy(p + 5, s, l + 1);
282 *ret = (const char*) p + 5;
287 static int message_append_field_uint32(sd_bus_message *m, uint8_t h, uint32_t x) {
292 /* field id byte + signature length + signature 'u' + NUL + value */
293 p = message_extend_fields(m, 8, 4 + 4);
299 p[2] = SD_BUS_TYPE_UINT32;
302 ((uint32_t*) p)[1] = x;
307 int bus_message_from_header(
312 const struct ucred *ucred,
315 sd_bus_message **ret) {
318 struct bus_header *h;
321 assert(buffer || length <= 0);
322 assert(fds || n_fds <= 0);
325 if (length < sizeof(struct bus_header))
335 if (h->type == _SD_BUS_MESSAGE_TYPE_INVALID)
338 if (h->endian != SD_BUS_LITTLE_ENDIAN &&
339 h->endian != SD_BUS_BIG_ENDIAN)
342 a = ALIGN(sizeof(sd_bus_message)) + ALIGN(extra);
345 label_sz = strlen(label);
363 m->uid_valid = m->gid_valid = true;
367 m->label = (char*) m + ALIGN(sizeof(sd_bus_message)) + ALIGN(extra);
368 memcpy(m->label, label, label_sz + 1);
375 int bus_message_from_malloc(
380 const struct ucred *ucred,
382 sd_bus_message **ret) {
387 r = bus_message_from_header(buffer, length, fds, n_fds, ucred, label, 0, &m);
391 if (length != BUS_MESSAGE_SIZE(m)) {
397 m->body.data = (uint8_t*) buffer + sizeof(struct bus_header) + ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m));
398 m->body.size = length - sizeof(struct bus_header) - ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m));
399 m->body.sealed = true;
403 m->iovec = m->iovec_fixed;
404 m->iovec[0].iov_base = buffer;
405 m->iovec[0].iov_len = length;
407 r = bus_message_parse_fields(m);
411 /* We take possession of the memory and fds now */
412 m->free_header = true;
423 static sd_bus_message *message_new(sd_bus *bus, uint8_t type) {
426 m = malloc0(ALIGN(sizeof(sd_bus_message)) + sizeof(struct bus_header));
431 m->header = (struct bus_header*) ((uint8_t*) m + ALIGN(sizeof(struct sd_bus_message)));
432 m->header->endian = SD_BUS_NATIVE_ENDIAN;
433 m->header->type = type;
434 m->header->version = bus ? bus->message_version : 1;
435 m->allow_fds = !bus || bus->can_fds || (bus->state != BUS_HELLO && bus->state != BUS_RUNNING);
438 m->bus = sd_bus_ref(bus);
443 int sd_bus_message_new_signal(
446 const char *interface,
448 sd_bus_message **m) {
461 if (bus && bus->state == BUS_UNSET)
464 t = message_new(bus, SD_BUS_MESSAGE_TYPE_SIGNAL);
468 t->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
470 r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path);
473 r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_INTERFACE, SD_BUS_TYPE_STRING, interface, &t->interface);
476 r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_MEMBER, SD_BUS_TYPE_STRING, member, &t->member);
484 sd_bus_message_unref(t);
488 int sd_bus_message_new_method_call(
490 const char *destination,
492 const char *interface,
494 sd_bus_message **m) {
499 if (destination && !service_name_is_valid(destination))
501 if (!object_path_is_valid(path))
503 if (interface && !interface_name_is_valid(interface))
505 if (!member_name_is_valid(member))
509 if (bus && bus->state == BUS_UNSET)
512 t = message_new(bus, SD_BUS_MESSAGE_TYPE_METHOD_CALL);
516 r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path);
519 r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_MEMBER, SD_BUS_TYPE_STRING, member, &t->member);
524 r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_INTERFACE, SD_BUS_TYPE_STRING, interface, &t->interface);
530 r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &t->destination);
543 static int message_new_reply(
545 sd_bus_message *call,
547 sd_bus_message **m) {
556 if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
560 if (bus && bus->state == BUS_UNSET)
563 t = message_new(bus, type);
567 t->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
568 t->reply_serial = BUS_MESSAGE_SERIAL(call);
570 r = message_append_field_uint32(t, SD_BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_serial);
575 r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, call->sender, &t->destination);
580 t->dont_send = !!(call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED);
590 int sd_bus_message_new_method_return(
592 sd_bus_message *call,
593 sd_bus_message **m) {
595 return message_new_reply(bus, call, SD_BUS_MESSAGE_TYPE_METHOD_RETURN, m);
598 int sd_bus_message_new_method_error(
600 sd_bus_message *call,
601 const sd_bus_error *e,
602 sd_bus_message **m) {
607 if (!sd_bus_error_is_set(e))
612 r = message_new_reply(bus, call, SD_BUS_MESSAGE_TYPE_METHOD_ERROR, &t);
616 r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, e->name, &t->error.name);
621 r = message_append_basic(t, SD_BUS_TYPE_STRING, e->message, (const void**) &t->error.message);
634 int sd_bus_message_new_method_errorf(
636 sd_bus_message *call,
642 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
652 r = message_new_reply(bus, call, SD_BUS_MESSAGE_TYPE_METHOD_ERROR, &t);
656 r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, name, &t->error.name);
661 _cleanup_free_ char *message = NULL;
663 va_start(ap, format);
664 r = vasprintf(&message, format, ap);
672 r = message_append_basic(t, SD_BUS_TYPE_STRING, message, (const void**) &t->error.message);
685 int bus_message_new_synthetic_error(
688 const sd_bus_error *e,
689 sd_bus_message **m) {
694 assert(sd_bus_error_is_set(e));
697 t = message_new(bus, SD_BUS_MESSAGE_TYPE_METHOD_ERROR);
701 t->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
702 t->reply_serial = serial;
704 r = message_append_field_uint32(t, SD_BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_serial);
708 if (bus && bus->unique_name) {
709 r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, bus->unique_name, &t->destination);
714 r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, e->name, &t->error.name);
719 r = message_append_basic(t, SD_BUS_TYPE_STRING, e->message, (const void**) &t->error.message);
732 sd_bus_message* sd_bus_message_ref(sd_bus_message *m) {
736 assert(m->n_ref > 0);
742 sd_bus_message* sd_bus_message_unref(sd_bus_message *m) {
746 assert(m->n_ref > 0);
755 int sd_bus_message_get_type(sd_bus_message *m, uint8_t *type) {
761 *type = m->header->type;
765 int sd_bus_message_get_serial(sd_bus_message *m, uint64_t *serial) {
770 if (m->header->serial == 0)
773 *serial = BUS_MESSAGE_SERIAL(m);
777 int sd_bus_message_get_reply_serial(sd_bus_message *m, uint64_t *serial) {
782 if (m->reply_serial == 0)
785 *serial = m->reply_serial;
789 int sd_bus_message_get_no_reply(sd_bus_message *m) {
793 return m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_CALL ? !!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED) : 0;
796 const char *sd_bus_message_get_path(sd_bus_message *m) {
803 const char *sd_bus_message_get_interface(sd_bus_message *m) {
810 const char *sd_bus_message_get_member(sd_bus_message *m) {
816 const char *sd_bus_message_get_destination(sd_bus_message *m) {
820 return m->destination;
823 const char *sd_bus_message_get_sender(sd_bus_message *m) {
830 const sd_bus_error *sd_bus_message_get_error(sd_bus_message *m) {
834 if (!sd_bus_error_is_set(&m->error))
840 int sd_bus_message_get_uid(sd_bus_message *m, uid_t *uid) {
852 int sd_bus_message_get_gid(sd_bus_message *m, gid_t *gid) {
864 int sd_bus_message_get_pid(sd_bus_message *m, pid_t *pid) {
876 int sd_bus_message_get_tid(sd_bus_message *m, pid_t *tid) {
888 int sd_bus_message_get_pid_starttime(sd_bus_message *m, uint64_t *usec) {
893 if (m->pid_starttime <= 0)
896 *usec = m->pid_starttime;
900 int sd_bus_message_get_selinux_context(sd_bus_message *m, const char **ret) {
910 int sd_bus_message_get_monotonic_timestamp(sd_bus_message *m, uint64_t *usec) {
915 if (m->monotonic <= 0)
918 *usec = m->monotonic;
922 int sd_bus_message_get_realtime_timestamp(sd_bus_message *m, uint64_t *usec) {
927 if (m->realtime <= 0)
934 int sd_bus_message_get_comm(sd_bus_message *m, const char **ret) {
946 int sd_bus_message_get_tid_comm(sd_bus_message *m, const char **ret) {
958 int sd_bus_message_get_exe(sd_bus_message *m, const char **ret) {
970 int sd_bus_message_get_cgroup(sd_bus_message *m, const char **ret) {
982 int sd_bus_message_get_unit(sd_bus_message *m, const char **ret) {
993 r = cg_path_get_unit(m->cgroup, &m->unit);
1002 int sd_bus_message_get_user_unit(sd_bus_message *m, const char **ret) {
1012 if (!m->user_unit) {
1013 r = cg_path_get_user_unit(m->cgroup, &m->user_unit);
1018 *ret = m->user_unit;
1022 int sd_bus_message_get_session(sd_bus_message *m, const char **ret) {
1033 r = cg_path_get_session(m->cgroup, &m->session);
1042 int sd_bus_message_get_owner_uid(sd_bus_message *m, uid_t *uid) {
1050 return cg_path_get_owner_uid(m->cgroup, uid);
1053 int sd_bus_message_get_cmdline(sd_bus_message *m, char ***cmdline) {
1064 for (p = m->cmdline, n = 0; p < m->cmdline + m->cmdline_length; p++)
1068 m->cmdline_array = new(char*, n + 1);
1069 if (!m->cmdline_array)
1072 for (p = m->cmdline, i = 0, first = true; p < m->cmdline + m->cmdline_length; p++) {
1074 m->cmdline_array[i++] = (char*) p;
1079 m->cmdline_array[i] = NULL;
1080 *cmdline = m->cmdline_array;
1085 int sd_bus_message_get_audit_sessionid(sd_bus_message *m, uint32_t *sessionid) {
1093 *sessionid = m->audit->sessionid;
1097 int sd_bus_message_get_audit_loginuid(sd_bus_message *m, uid_t *uid) {
1105 *uid = m->audit->loginuid;
1109 int sd_bus_message_has_effective_cap(sd_bus_message *m, int capability) {
1119 sz = m->capability_size / 4;
1120 if ((unsigned) capability >= sz*8)
1123 return !!(m->capability[2 * sz + (capability / 8)] & (1 << (capability % 8)));
1126 int sd_bus_message_is_signal(sd_bus_message *m, const char *interface, const char *member) {
1130 if (m->header->type != SD_BUS_MESSAGE_TYPE_SIGNAL)
1133 if (interface && (!m->interface || !streq(m->interface, interface)))
1136 if (member && (!m->member || !streq(m->member, member)))
1142 int sd_bus_message_is_method_call(sd_bus_message *m, const char *interface, const char *member) {
1146 if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
1149 if (interface && (!m->interface || !streq(m->interface, interface)))
1152 if (member && (!m->member || !streq(m->member, member)))
1158 int sd_bus_message_is_method_error(sd_bus_message *m, const char *name) {
1162 if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_ERROR)
1165 if (name && (!m->error.name || !streq(m->error.name, name)))
1171 int sd_bus_message_set_no_reply(sd_bus_message *m, int b) {
1176 if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
1180 m->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
1182 m->header->flags &= ~SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
1187 static struct bus_container *message_get_container(sd_bus_message *m) {
1190 if (m->n_containers == 0)
1191 return &m->root_container;
1193 assert(m->containers);
1194 return m->containers + m->n_containers - 1;
1197 struct bus_body_part *message_append_part(sd_bus_message *m) {
1198 struct bus_body_part *part;
1205 if (m->n_body_parts <= 0) {
1209 assert(m->body_end);
1211 part = new0(struct bus_body_part, 1);
1217 m->body_end->next = part;
1227 static void part_zero(struct bus_body_part *part, size_t sz) {
1232 /* All other fields can be left in their defaults */
1233 assert(!part->data);
1234 assert(part->memfd < 0);
1237 part->is_zero = true;
1238 part->sealed = true;
1241 static int part_make_space(
1242 struct sd_bus_message *m,
1243 struct bus_body_part *part,
1252 assert(!part->sealed);
1257 if (!part->data && part->memfd < 0)
1258 part->memfd = bus_kernel_pop_memfd(m->bus, &part->data, &part->mapped);
1260 if (part->memfd >= 0) {
1263 r = ioctl(part->memfd, KDBUS_CMD_MEMFD_SIZE_SET, &u);
1269 if (!part->data || sz > part->mapped) {
1270 size_t psz = PAGE_ALIGN(sz > 0 ? sz : 1);
1272 if (part->mapped <= 0)
1273 n = mmap(NULL, psz, PROT_READ|PROT_WRITE, MAP_SHARED, part->memfd, 0);
1275 n = mremap(part->data, part->mapped, psz, MREMAP_MAYMOVE);
1277 if (n == MAP_FAILED) {
1286 part->munmap_this = true;
1288 n = realloc(part->data, sz);
1295 part->free_this = true;
1299 *q = part->data ? (uint8_t*) part->data + part->size : NULL;
1305 static void message_extend_containers(sd_bus_message *m, size_t expand) {
1306 struct bus_container *c;
1313 /* Update counters */
1314 for (c = m->containers; c < m->containers + m->n_containers; c++)
1316 *c->array_size += expand;
1319 static void *message_extend_body(sd_bus_message *m, size_t align, size_t sz) {
1320 struct bus_body_part *part = NULL;
1321 size_t start_body, end_body, padding, start_part, end_part, added;
1333 start_body = ALIGN_TO((size_t) m->header->body_size, align);
1334 end_body = start_body + sz;
1336 padding = start_body - m->header->body_size;
1337 added = padding + sz;
1339 /* Check for 32bit overflows */
1340 if (end_body > (size_t) ((uint32_t) -1)) {
1346 m->n_body_parts <= 0 ||
1347 m->body_end->sealed ||
1348 padding != ALIGN_TO(m->body_end->size, align) - m->body_end->size;
1352 part = message_append_part(m);
1356 part_zero(part, padding);
1359 part = message_append_part(m);
1363 r = part_make_space(m, part, sz, &p);
1367 struct bus_container *c;
1375 start_part = ALIGN_TO(part->size, align);
1376 end_part = start_part + sz;
1378 r = part_make_space(m, part, end_part, &p);
1383 memset(p, 0, padding);
1384 p = (uint8_t*) p + padding;
1387 /* Readjust pointers */
1388 for (c = m->containers; c < m->containers + m->n_containers; c++)
1389 c->array_size = adjust_pointer(c->array_size, op, os, part->data);
1391 m->error.message = (const char*) adjust_pointer(m->error.message, op, os, part->data);
1394 m->header->body_size = end_body;
1395 message_extend_containers(m, added);
1400 int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored) {
1401 struct bus_container *c;
1415 if (!bus_type_is_basic(type))
1420 c = message_get_container(m);
1422 if (c->signature && c->signature[c->index]) {
1423 /* Container signature is already set */
1425 if (c->signature[c->index] != type)
1430 /* Maybe we can append to the signature? But only if this is the top-level container*/
1431 if (c->enclosing != 0)
1434 e = strextend(&c->signature, CHAR_TO_STR(type), NULL);
1443 case SD_BUS_TYPE_STRING:
1444 case SD_BUS_TYPE_OBJECT_PATH:
1447 sz = 4 + strlen(p) + 1;
1450 case SD_BUS_TYPE_SIGNATURE:
1453 sz = 1 + strlen(p) + 1;
1456 case SD_BUS_TYPE_BOOLEAN:
1459 assert_cc(sizeof(int) == sizeof(uint32_t));
1465 case SD_BUS_TYPE_UNIX_FD: {
1468 if (!m->allow_fds) {
1481 fd = fcntl(z, F_DUPFD_CLOEXEC, 3);
1487 f = realloc(m->fds, sizeof(int) * (m->n_fds + 1));
1502 align = bus_type_get_alignment(type);
1503 sz = bus_type_get_size(type);
1510 a = message_extend_body(m, align, sz);
1516 if (type == SD_BUS_TYPE_STRING || type == SD_BUS_TYPE_OBJECT_PATH) {
1517 *(uint32_t*) a = sz - 5;
1518 memcpy((uint8_t*) a + 4, p, sz - 4);
1521 *stored = (const uint8_t*) a + 4;
1523 } else if (type == SD_BUS_TYPE_SIGNATURE) {
1524 *(uint8_t*) a = sz - 1;
1525 memcpy((uint8_t*) a + 1, p, sz - 1);
1528 *stored = (const uint8_t*) a + 1;
1529 } else if (type == SD_BUS_TYPE_UNIX_FD) {
1530 *(uint32_t*) a = fdi;
1544 if (c->enclosing != SD_BUS_TYPE_ARRAY)
1551 close_nointr_nofail(fd);
1556 int sd_bus_message_append_basic(sd_bus_message *m, char type, const void *p) {
1557 return message_append_basic(m, type, p, NULL);
1560 int sd_bus_message_append_string_space(sd_bus_message *m, size_t size, char **s) {
1561 struct bus_container *c;
1573 c = message_get_container(m);
1575 if (c->signature && c->signature[c->index]) {
1576 /* Container signature is already set */
1578 if (c->signature[c->index] != SD_BUS_TYPE_STRING)
1583 /* Maybe we can append to the signature? But only if this is the top-level container*/
1584 if (c->enclosing != 0)
1587 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRING), NULL);
1594 a = message_extend_body(m, 4, 4 + size + 1);
1598 *(uint32_t*) a = size;
1603 if (c->enclosing != SD_BUS_TYPE_ARRAY)
1609 static int bus_message_open_array(
1611 struct bus_container *c,
1612 const char *contents,
1613 uint32_t **array_size) {
1619 struct bus_body_part *o;
1626 if (!signature_is_single(contents, true))
1629 alignment = bus_type_get_alignment(contents[0]);
1633 if (c->signature && c->signature[c->index]) {
1635 /* Verify the existing signature */
1637 if (c->signature[c->index] != SD_BUS_TYPE_ARRAY)
1640 if (!startswith(c->signature + c->index + 1, contents))
1643 nindex = c->index + 1 + strlen(contents);
1647 if (c->enclosing != 0)
1650 /* Extend the existing signature */
1652 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_ARRAY), contents, NULL);
1658 nindex = e - c->signature;
1661 a = message_extend_body(m, 4, 4);
1666 op = m->body_end->data;
1667 os = m->body_end->size;
1669 /* Add alignment between size and first element */
1670 if (!message_extend_body(m, alignment, 0))
1673 if (c->enclosing != SD_BUS_TYPE_ARRAY)
1676 /* location of array size might have changed so let's readjust a */
1677 if (o == m->body_end)
1678 a = adjust_pointer(a, op, os, m->body_end->data);
1685 static int bus_message_open_variant(
1687 struct bus_container *c,
1688 const char *contents) {
1697 if (!signature_is_single(contents, false))
1700 if (*contents == SD_BUS_TYPE_DICT_ENTRY_BEGIN)
1703 if (c->signature && c->signature[c->index]) {
1705 if (c->signature[c->index] != SD_BUS_TYPE_VARIANT)
1711 if (c->enclosing != 0)
1714 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_VARIANT), NULL);
1721 l = strlen(contents);
1722 a = message_extend_body(m, 1, 1 + l + 1);
1727 memcpy((uint8_t*) a + 1, contents, l + 1);
1729 if (c->enclosing != SD_BUS_TYPE_ARRAY)
1735 static int bus_message_open_struct(
1737 struct bus_container *c,
1738 const char *contents) {
1746 if (!signature_is_valid(contents, false))
1749 if (c->signature && c->signature[c->index]) {
1752 l = strlen(contents);
1754 if (c->signature[c->index] != SD_BUS_TYPE_STRUCT_BEGIN ||
1755 !startswith(c->signature + c->index + 1, contents) ||
1756 c->signature[c->index + 1 + l] != SD_BUS_TYPE_STRUCT_END)
1759 nindex = c->index + 1 + l + 1;
1763 if (c->enclosing != 0)
1766 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_BEGIN), contents, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_END), NULL);
1772 nindex = e - c->signature;
1775 /* Align contents to 8 byte boundary */
1776 if (!message_extend_body(m, 8, 0))
1779 if (c->enclosing != SD_BUS_TYPE_ARRAY)
1785 static int bus_message_open_dict_entry(
1787 struct bus_container *c,
1788 const char *contents) {
1796 if (!signature_is_pair(contents))
1799 if (c->enclosing != SD_BUS_TYPE_ARRAY)
1802 if (c->signature && c->signature[c->index]) {
1805 l = strlen(contents);
1807 if (c->signature[c->index] != SD_BUS_TYPE_DICT_ENTRY_BEGIN ||
1808 !startswith(c->signature + c->index + 1, contents) ||
1809 c->signature[c->index + 1 + l] != SD_BUS_TYPE_DICT_ENTRY_END)
1812 nindex = c->index + 1 + l + 1;
1816 /* Align contents to 8 byte boundary */
1817 if (!message_extend_body(m, 8, 0))
1820 if (c->enclosing != SD_BUS_TYPE_ARRAY)
1826 int sd_bus_message_open_container(
1829 const char *contents) {
1831 struct bus_container *c, *w;
1832 uint32_t *array_size = NULL;
1846 /* Make sure we have space for one more container */
1847 w = realloc(m->containers, sizeof(struct bus_container) * (m->n_containers + 1));
1855 c = message_get_container(m);
1857 signature = strdup(contents);
1863 /* Save old index in the parent container, in case we have to
1864 * abort this container */
1865 c->saved_index = c->index;
1866 before = m->header->body_size;
1868 if (type == SD_BUS_TYPE_ARRAY)
1869 r = bus_message_open_array(m, c, contents, &array_size);
1870 else if (type == SD_BUS_TYPE_VARIANT)
1871 r = bus_message_open_variant(m, c, contents);
1872 else if (type == SD_BUS_TYPE_STRUCT)
1873 r = bus_message_open_struct(m, c, contents);
1874 else if (type == SD_BUS_TYPE_DICT_ENTRY)
1875 r = bus_message_open_dict_entry(m, c, contents);
1884 /* OK, let's fill it in */
1885 w += m->n_containers++;
1886 w->enclosing = type;
1887 w->signature = signature;
1889 w->array_size = array_size;
1891 w->begin = m->rindex;
1896 int sd_bus_message_close_container(sd_bus_message *m) {
1897 struct bus_container *c;
1903 if (m->n_containers <= 0)
1908 c = message_get_container(m);
1909 if (c->enclosing != SD_BUS_TYPE_ARRAY)
1910 if (c->signature && c->signature[c->index] != 0)
1925 static int type_stack_push(TypeStack *stack, unsigned max, unsigned *i, const char *types, unsigned n_struct, unsigned n_array) {
1932 stack[*i].types = types;
1933 stack[*i].n_struct = n_struct;
1934 stack[*i].n_array = n_array;
1940 static int type_stack_pop(TypeStack *stack, unsigned max, unsigned *i, const char **types, unsigned *n_struct, unsigned *n_array) {
1951 *types = stack[*i].types;
1952 *n_struct = stack[*i].n_struct;
1953 *n_array = stack[*i].n_array;
1958 int bus_message_append_ap(
1963 unsigned n_array, n_struct;
1964 TypeStack stack[BUS_CONTAINER_DEPTH];
1965 unsigned stack_ptr = 0;
1973 n_array = (unsigned) -1;
1974 n_struct = strlen(types);
1979 if (n_array == 0 || (n_array == (unsigned) -1 && n_struct == 0)) {
1980 r = type_stack_pop(stack, ELEMENTSOF(stack), &stack_ptr, &types, &n_struct, &n_array);
1986 r = sd_bus_message_close_container(m);
1994 if (n_array != (unsigned) -1)
2003 case SD_BUS_TYPE_BYTE: {
2006 x = (uint8_t) va_arg(ap, int);
2007 r = sd_bus_message_append_basic(m, *t, &x);
2011 case SD_BUS_TYPE_BOOLEAN:
2012 case SD_BUS_TYPE_INT32:
2013 case SD_BUS_TYPE_UINT32:
2014 case SD_BUS_TYPE_UNIX_FD: {
2017 /* We assume a boolean is the same as int32_t */
2018 assert_cc(sizeof(int32_t) == sizeof(int));
2020 x = va_arg(ap, uint32_t);
2021 r = sd_bus_message_append_basic(m, *t, &x);
2025 case SD_BUS_TYPE_INT16:
2026 case SD_BUS_TYPE_UINT16: {
2029 x = (uint16_t) va_arg(ap, int);
2030 r = sd_bus_message_append_basic(m, *t, &x);
2034 case SD_BUS_TYPE_INT64:
2035 case SD_BUS_TYPE_UINT64:
2036 case SD_BUS_TYPE_DOUBLE: {
2039 x = va_arg(ap, uint64_t);
2040 r = sd_bus_message_append_basic(m, *t, &x);
2044 case SD_BUS_TYPE_STRING:
2045 case SD_BUS_TYPE_OBJECT_PATH:
2046 case SD_BUS_TYPE_SIGNATURE: {
2049 x = va_arg(ap, const char*);
2050 r = sd_bus_message_append_basic(m, *t, x);
2054 case SD_BUS_TYPE_ARRAY: {
2057 r = signature_element_length(t + 1, &k);
2063 memcpy(s, t + 1, k);
2066 r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, s);
2071 if (n_array == (unsigned) -1) {
2076 r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
2082 n_array = va_arg(ap, unsigned);
2087 case SD_BUS_TYPE_VARIANT: {
2090 s = va_arg(ap, const char*);
2094 r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT, s);
2098 r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
2103 n_struct = strlen(s);
2104 n_array = (unsigned) -1;
2109 case SD_BUS_TYPE_STRUCT_BEGIN:
2110 case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
2113 r = signature_element_length(t, &k);
2120 memcpy(s, t + 1, k - 2);
2123 r = sd_bus_message_open_container(m, *t == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s);
2128 if (n_array == (unsigned) -1) {
2133 r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
2139 n_array = (unsigned) -1;
2155 int sd_bus_message_append(sd_bus_message *m, const char *types, ...) {
2168 va_start(ap, types);
2169 r = bus_message_append_ap(m, types, ap);
2175 int sd_bus_message_append_array_space(sd_bus_message *m, char type, size_t size, void **ptr) {
2184 if (!bus_type_is_trivial(type))
2186 if (!ptr && size > 0)
2191 align = bus_type_get_alignment(type);
2192 sz = bus_type_get_size(type);
2194 assert_se(align > 0);
2200 r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, CHAR_TO_STR(type));
2204 a = message_extend_body(m, align, size);
2208 r = sd_bus_message_close_container(m);
2216 int sd_bus_message_append_array(sd_bus_message *m, char type, const void *ptr, size_t size) {
2220 if (!ptr && size > 0)
2223 r = sd_bus_message_append_array_space(m, type, size, &p);
2228 memcpy(p, ptr, size);
2233 int sd_bus_message_append_array_memfd(sd_bus_message *m, char type, sd_memfd *memfd) {
2234 _cleanup_close_ int copy_fd = -1;
2235 struct bus_body_part *part;
2247 if (!bus_type_is_trivial(type))
2252 r = sd_memfd_set_sealed(memfd, true);
2256 copy_fd = sd_memfd_dup_fd(memfd);
2260 r = sd_memfd_get_size(memfd, &size);
2264 align = bus_type_get_alignment(type);
2265 sz = bus_type_get_size(type);
2267 assert_se(align > 0);
2273 if (size > (uint64_t) (uint32_t) -1)
2276 r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, CHAR_TO_STR(type));
2280 a = message_extend_body(m, align, 0);
2284 part = message_append_part(m);
2288 part->memfd = copy_fd;
2289 part->sealed = true;
2293 message_extend_containers(m, size);
2294 m->header->body_size += size;
2296 return sd_bus_message_close_container(m);
2299 int sd_bus_message_append_string_memfd(sd_bus_message *m, sd_memfd *memfd) {
2300 _cleanup_close_ int copy_fd = -1;
2301 struct bus_body_part *part;
2302 struct bus_container *c;
2307 assert_return(m, -EINVAL);
2308 assert_return(memfd, -EINVAL);
2309 assert_return(!m->sealed, -EPERM);
2310 assert_return(!m->poisoned, -ESTALE);
2312 r = sd_memfd_set_sealed(memfd, true);
2316 copy_fd = sd_memfd_dup_fd(memfd);
2320 r = sd_memfd_get_size(memfd, &size);
2324 /* We require this to be NUL terminated */
2328 if (size > (uint64_t) (uint32_t) -1)
2331 c = message_get_container(m);
2332 if (c->signature && c->signature[c->index]) {
2333 /* Container signature is already set */
2335 if (c->signature[c->index] != SD_BUS_TYPE_STRING)
2340 /* Maybe we can append to the signature? But only if this is the top-level container*/
2341 if (c->enclosing != 0)
2344 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRING), NULL);
2351 a = message_extend_body(m, 4, 4);
2355 *(uint32_t*) a = size - 1;
2357 part = message_append_part(m);
2361 part->memfd = copy_fd;
2362 part->sealed = true;
2366 message_extend_containers(m, size);
2367 m->header->body_size += size;
2369 if (c->enclosing != SD_BUS_TYPE_ARRAY)
2375 int sd_bus_message_append_strv(sd_bus_message *m, char **l) {
2379 assert_return(m, -EINVAL);
2380 assert_return(!m->sealed, -EPERM);
2381 assert_return(!m->poisoned, -ESTALE);
2383 r = sd_bus_message_open_container(m, 'a', "s");
2387 STRV_FOREACH(i, l) {
2388 r = sd_bus_message_append_basic(m, 's', *i);
2393 return sd_bus_message_close_container(m);
2396 int bus_body_part_map(struct bus_body_part *part) {
2405 if (part->size <= 0)
2408 /* For smaller zero parts (as used for padding) we don't need to map anything... */
2409 if (part->memfd < 0 && part->is_zero && part->size < 8) {
2410 static const uint8_t zeroes[7] = { };
2411 part->data = (void*) zeroes;
2415 psz = PAGE_ALIGN(part->size);
2417 if (part->memfd >= 0)
2418 p = mmap(NULL, psz, PROT_READ, MAP_SHARED, part->memfd, 0);
2419 else if (part->is_zero)
2420 p = mmap(NULL, psz, PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
2424 if (p == MAP_FAILED)
2429 part->munmap_this = true;
2434 void bus_body_part_unmap(struct bus_body_part *part) {
2438 if (part->memfd < 0)
2444 if (!part->munmap_this)
2447 assert_se(munmap(part->data, part->mapped) == 0);
2451 part->munmap_this = false;
2456 static int buffer_peek(const void *p, uint32_t sz, size_t *rindex, size_t align, size_t nbytes, void **r) {
2457 size_t k, start, end;
2462 start = ALIGN_TO((size_t) *rindex, align);
2463 end = start + nbytes;
2468 /* Verify that padding is 0 */
2469 for (k = *rindex; k < start; k++)
2470 if (((const uint8_t*) p)[k] != 0)
2474 *r = (uint8_t*) p + start;
2481 static bool message_end_of_array(sd_bus_message *m, size_t index) {
2482 struct bus_container *c;
2486 c = message_get_container(m);
2490 return index >= c->begin + BUS_MESSAGE_BSWAP32(m, *c->array_size);
2493 static struct bus_body_part* find_part(sd_bus_message *m, size_t index, size_t sz, void **p) {
2494 struct bus_body_part *part;
2500 if (m->cached_rindex_part && index >= m->cached_rindex_part_begin) {
2501 part = m->cached_rindex_part;
2502 begin = m->cached_rindex_part_begin;
2512 if (index + sz <= begin + part->size) {
2514 r = bus_body_part_map(part);
2519 *p = (uint8_t*) part->data + index - begin;
2521 m->cached_rindex_part = part;
2522 m->cached_rindex_part_begin = begin;
2527 begin += part->size;
2534 static int message_peek_body(
2541 size_t k, start, end, padding;
2542 struct bus_body_part *part;
2549 if (message_end_of_array(m, *rindex))
2552 start = ALIGN_TO((size_t) *rindex, align);
2553 padding = start - *rindex;
2554 end = start + nbytes;
2556 if (end > BUS_MESSAGE_BODY_SIZE(m))
2559 part = find_part(m, *rindex, padding, (void**) &q);
2564 /* Verify padding */
2565 for (k = 0; k < padding; k++)
2570 part = find_part(m, start, nbytes, (void**) &q);
2582 static bool validate_nul(const char *s, size_t l) {
2584 /* Check for NUL chars in the string */
2585 if (memchr(s, 0, l))
2588 /* Check for NUL termination */
2595 static bool validate_string(const char *s, size_t l) {
2597 if (!validate_nul(s, l))
2600 /* Check if valid UTF8 */
2601 if (!utf8_is_valid(s))
2607 static bool validate_signature(const char *s, size_t l) {
2609 if (!validate_nul(s, l))
2612 /* Check if valid signature */
2613 if (!signature_is_valid(s, true))
2619 static bool validate_object_path(const char *s, size_t l) {
2621 if (!validate_nul(s, l))
2624 if (!object_path_is_valid(s))
2630 int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p) {
2631 struct bus_container *c;
2639 if (!bus_type_is_basic(type))
2644 c = message_get_container(m);
2646 if (!c->signature || c->signature[c->index] == 0)
2649 if (c->signature[c->index] != type)
2654 case SD_BUS_TYPE_STRING:
2655 case SD_BUS_TYPE_OBJECT_PATH: {
2660 r = message_peek_body(m, &rindex, 4, 4, &q);
2664 l = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
2665 r = message_peek_body(m, &rindex, 1, l+1, &q);
2671 if (type == SD_BUS_TYPE_OBJECT_PATH) {
2672 if (!validate_object_path(q, l))
2675 if (!validate_string(q, l))
2680 *(const char**) p = q;
2684 case SD_BUS_TYPE_SIGNATURE: {
2689 r = message_peek_body(m, &rindex, 1, 1, &q);
2694 r = message_peek_body(m, &rindex, 1, l+1, &q);
2700 if (!validate_signature(q, l))
2704 *(const char**) p = q;
2712 align = bus_type_get_alignment(type);
2713 sz = bus_type_get_size(type);
2714 assert(align > 0 && sz > 0);
2717 r = message_peek_body(m, &rindex, align, sz, &q);
2723 case SD_BUS_TYPE_BYTE:
2724 *(uint8_t*) p = *(uint8_t*) q;
2727 case SD_BUS_TYPE_BOOLEAN:
2728 *(int*) p = !!*(uint32_t*) q;
2731 case SD_BUS_TYPE_INT16:
2732 case SD_BUS_TYPE_UINT16:
2733 *(uint16_t*) p = BUS_MESSAGE_BSWAP16(m, *(uint16_t*) q);
2736 case SD_BUS_TYPE_INT32:
2737 case SD_BUS_TYPE_UINT32:
2738 *(uint32_t*) p = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
2741 case SD_BUS_TYPE_INT64:
2742 case SD_BUS_TYPE_UINT64:
2743 case SD_BUS_TYPE_DOUBLE:
2744 *(uint64_t*) p = BUS_MESSAGE_BSWAP64(m, *(uint64_t*) q);
2747 case SD_BUS_TYPE_UNIX_FD: {
2750 j = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
2754 *(int*) p = m->fds[j];
2759 assert_not_reached("Unknown basic type...");
2768 if (c->enclosing != SD_BUS_TYPE_ARRAY)
2774 static int bus_message_enter_array(
2776 struct bus_container *c,
2777 const char *contents,
2778 uint32_t **array_size) {
2789 if (!signature_is_single(contents, true))
2792 alignment = bus_type_get_alignment(contents[0]);
2796 if (!c->signature || c->signature[c->index] == 0)
2799 if (c->signature[c->index] != SD_BUS_TYPE_ARRAY)
2802 if (!startswith(c->signature + c->index + 1, contents))
2806 r = message_peek_body(m, &rindex, 4, 4, &q);
2810 if (BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q) > BUS_ARRAY_MAX_SIZE)
2813 r = message_peek_body(m, &rindex, alignment, 0, NULL);
2819 if (c->enclosing != SD_BUS_TYPE_ARRAY)
2820 c->index += 1 + strlen(contents);
2824 *array_size = (uint32_t*) q;
2829 static int bus_message_enter_variant(
2831 struct bus_container *c,
2832 const char *contents) {
2843 if (!signature_is_single(contents, false))
2846 if (*contents == SD_BUS_TYPE_DICT_ENTRY_BEGIN)
2849 if (!c->signature || c->signature[c->index] == 0)
2852 if (c->signature[c->index] != SD_BUS_TYPE_VARIANT)
2856 r = message_peek_body(m, &rindex, 1, 1, &q);
2861 r = message_peek_body(m, &rindex, 1, l+1, &q);
2867 if (!validate_signature(q, l))
2870 if (!streq(q, contents))
2873 if (c->enclosing != SD_BUS_TYPE_ARRAY)
2881 static int bus_message_enter_struct(
2883 struct bus_container *c,
2884 const char *contents) {
2893 if (!signature_is_valid(contents, false))
2896 if (!c->signature || c->signature[c->index] == 0)
2899 l = strlen(contents);
2901 if (c->signature[c->index] != SD_BUS_TYPE_STRUCT_BEGIN ||
2902 !startswith(c->signature + c->index + 1, contents) ||
2903 c->signature[c->index + 1 + l] != SD_BUS_TYPE_STRUCT_END)
2906 r = message_peek_body(m, &m->rindex, 8, 0, NULL);
2910 if (c->enclosing != SD_BUS_TYPE_ARRAY)
2911 c->index += 1 + l + 1;
2916 static int bus_message_enter_dict_entry(
2918 struct bus_container *c,
2919 const char *contents) {
2928 if (!signature_is_pair(contents))
2931 if (c->enclosing != SD_BUS_TYPE_ARRAY)
2934 if (!c->signature || c->signature[c->index] == 0)
2937 l = strlen(contents);
2939 if (c->signature[c->index] != SD_BUS_TYPE_DICT_ENTRY_BEGIN ||
2940 !startswith(c->signature + c->index + 1, contents) ||
2941 c->signature[c->index + 1 + l] != SD_BUS_TYPE_DICT_ENTRY_END)
2944 r = message_peek_body(m, &m->rindex, 8, 0, NULL);
2948 if (c->enclosing != SD_BUS_TYPE_ARRAY)
2949 c->index += 1 + l + 1;
2954 int sd_bus_message_enter_container(sd_bus_message *m, char type, const char *contents) {
2955 struct bus_container *c, *w;
2956 uint32_t *array_size = NULL;
2969 * We enforce a global limit on container depth, that is much
2970 * higher than the 32 structs and 32 arrays the specification
2971 * mandates. This is simpler to implement for us, and we need
2972 * this only to ensure our container array doesn't grow
2973 * without bounds. We are happy to return any data from a
2974 * message as long as the data itself is valid, even if the
2975 * overall message might be not.
2977 * Note that the message signature is validated when
2978 * parsing the headers, and that validation does check the
2981 * Note that the specification defines no limits on the depth
2982 * of stacked variants, but we do.
2984 if (m->n_containers >= BUS_CONTAINER_DEPTH)
2987 w = realloc(m->containers, sizeof(struct bus_container) * (m->n_containers + 1));
2992 c = message_get_container(m);
2994 if (!c->signature || c->signature[c->index] == 0)
2997 signature = strdup(contents);
3001 c->saved_index = c->index;
3004 if (type == SD_BUS_TYPE_ARRAY)
3005 r = bus_message_enter_array(m, c, contents, &array_size);
3006 else if (type == SD_BUS_TYPE_VARIANT)
3007 r = bus_message_enter_variant(m, c, contents);
3008 else if (type == SD_BUS_TYPE_STRUCT)
3009 r = bus_message_enter_struct(m, c, contents);
3010 else if (type == SD_BUS_TYPE_DICT_ENTRY)
3011 r = bus_message_enter_dict_entry(m, c, contents);
3020 /* OK, let's fill it in */
3021 w += m->n_containers++;
3022 w->enclosing = type;
3023 w->signature = signature;
3025 w->array_size = array_size;
3027 w->begin = m->rindex;
3032 int sd_bus_message_exit_container(sd_bus_message *m) {
3033 struct bus_container *c;
3039 if (m->n_containers <= 0)
3042 c = message_get_container(m);
3043 if (c->enclosing == SD_BUS_TYPE_ARRAY) {
3046 l = BUS_MESSAGE_BSWAP32(m, *c->array_size);
3047 if (c->begin + l != m->rindex)
3051 if (c->signature && c->signature[c->index] != 0)
3061 static void message_quit_container(sd_bus_message *m) {
3062 struct bus_container *c;
3066 assert(m->n_containers > 0);
3068 c = message_get_container(m);
3071 assert(m->rindex >= c->before);
3072 m->rindex = c->before;
3074 /* Free container */
3078 /* Correct index of new top-level container */
3079 c = message_get_container(m);
3080 c->index = c->saved_index;
3083 int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char **contents) {
3084 struct bus_container *c;
3092 c = message_get_container(m);
3094 if (!c->signature || c->signature[c->index] == 0)
3097 if (message_end_of_array(m, m->rindex))
3100 if (bus_type_is_basic(c->signature[c->index])) {
3104 *type = c->signature[c->index];
3108 if (c->signature[c->index] == SD_BUS_TYPE_ARRAY) {
3114 r = signature_element_length(c->signature+c->index+1, &l);
3120 sig = strndup(c->signature + c->index + 1, l);
3124 free(m->peeked_signature);
3125 m->peeked_signature = sig;
3131 *type = SD_BUS_TYPE_ARRAY;
3136 if (c->signature[c->index] == SD_BUS_TYPE_STRUCT_BEGIN ||
3137 c->signature[c->index] == SD_BUS_TYPE_DICT_ENTRY_BEGIN) {
3143 r = signature_element_length(c->signature+c->index, &l);
3148 sig = strndup(c->signature + c->index + 1, l - 2);
3152 free(m->peeked_signature);
3153 m->peeked_signature = sig;
3159 *type = c->signature[c->index] == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY;
3164 if (c->signature[c->index] == SD_BUS_TYPE_VARIANT) {
3170 r = message_peek_body(m, &rindex, 1, 1, &q);
3177 r = message_peek_body(m, &rindex, 1, l+1, &q);
3183 if (!validate_signature(q, l))
3190 *type = SD_BUS_TYPE_VARIANT;
3199 *type = c->enclosing;
3205 int sd_bus_message_rewind(sd_bus_message *m, int complete) {
3206 struct bus_container *c;
3214 message_reset_containers(m);
3216 m->root_container.index = 0;
3218 c = message_get_container(m);
3220 c = message_get_container(m);
3223 m->rindex = c->begin;
3226 return !isempty(c->signature);
3228 static int message_read_ap(
3233 unsigned n_array, n_struct;
3234 TypeStack stack[BUS_CONTAINER_DEPTH];
3235 unsigned stack_ptr = 0;
3243 /* Ideally, we'd just call ourselves recursively on every
3244 * complex type. However, the state of a va_list that is
3245 * passed to a function is undefined after that function
3246 * returns. This means we need to docode the va_list linearly
3247 * in a single stackframe. We hence implement our own
3248 * home-grown stack in an array. */
3250 n_array = (unsigned) -1;
3251 n_struct = strlen(types);
3256 if (n_array == 0 || (n_array == (unsigned) -1 && n_struct == 0)) {
3257 r = type_stack_pop(stack, ELEMENTSOF(stack), &stack_ptr, &types, &n_struct, &n_array);
3263 r = sd_bus_message_exit_container(m);
3271 if (n_array != (unsigned) -1)
3280 case SD_BUS_TYPE_BYTE:
3281 case SD_BUS_TYPE_BOOLEAN:
3282 case SD_BUS_TYPE_INT16:
3283 case SD_BUS_TYPE_UINT16:
3284 case SD_BUS_TYPE_INT32:
3285 case SD_BUS_TYPE_UINT32:
3286 case SD_BUS_TYPE_INT64:
3287 case SD_BUS_TYPE_UINT64:
3288 case SD_BUS_TYPE_DOUBLE:
3289 case SD_BUS_TYPE_STRING:
3290 case SD_BUS_TYPE_OBJECT_PATH:
3291 case SD_BUS_TYPE_SIGNATURE:
3292 case SD_BUS_TYPE_UNIX_FD: {
3295 p = va_arg(ap, void*);
3296 r = sd_bus_message_read_basic(m, *t, p);
3305 case SD_BUS_TYPE_ARRAY: {
3308 r = signature_element_length(t + 1, &k);
3314 memcpy(s, t + 1, k);
3317 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, s);
3324 if (n_array == (unsigned) -1) {
3329 r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
3335 n_array = va_arg(ap, unsigned);
3340 case SD_BUS_TYPE_VARIANT: {
3343 s = va_arg(ap, const char *);
3347 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, s);
3353 r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
3358 n_struct = strlen(s);
3359 n_array = (unsigned) -1;
3364 case SD_BUS_TYPE_STRUCT_BEGIN:
3365 case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
3368 r = signature_element_length(t, &k);
3374 memcpy(s, t + 1, k - 2);
3377 r = sd_bus_message_enter_container(m, *t == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s);
3384 if (n_array == (unsigned) -1) {
3389 r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
3395 n_array = (unsigned) -1;
3408 int sd_bus_message_read(sd_bus_message *m, const char *types, ...) {
3419 va_start(ap, types);
3420 r = message_read_ap(m, types, ap);
3426 int sd_bus_message_read_array(sd_bus_message *m, char type, const void **ptr, size_t *size) {
3427 struct bus_container *c;
3437 if (!bus_type_is_trivial(type))
3443 if (BUS_MESSAGE_NEED_BSWAP(m))
3446 align = bus_type_get_alignment(type);
3450 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, CHAR_TO_STR(type));
3454 c = message_get_container(m);
3455 sz = BUS_MESSAGE_BSWAP32(m, *c->array_size);
3457 r = message_peek_body(m, &m->rindex, align, sz, &p);
3465 r = sd_bus_message_exit_container(m);
3469 *ptr = (const void*) p;
3475 message_quit_container(m);
3479 static int message_peek_fields(
3490 return buffer_peek(BUS_MESSAGE_FIELDS(m), BUS_MESSAGE_FIELDS_SIZE(m), rindex, align, nbytes, ret);
3493 static int message_peek_field_uint32(
3504 r = message_peek_fields(m, ri, 4, 4, &q);
3509 *ret = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
3514 static int message_peek_field_string(
3516 bool (*validate)(const char *p),
3527 r = message_peek_field_uint32(m, ri, &l);
3531 r = message_peek_fields(m, ri, 1, l+1, &q);
3536 if (!validate_nul(q, l))
3542 if (!validate_string(q, l))
3552 static int message_peek_field_signature(
3564 r = message_peek_fields(m, ri, 1, 1, &q);
3569 r = message_peek_fields(m, ri, 1, l+1, &q);
3573 if (!validate_signature(q, l))
3582 static int message_skip_fields(
3585 uint32_t array_size,
3586 const char **signature) {
3588 size_t original_index;
3595 original_index = *ri;
3601 if (array_size != (uint32_t) -1 &&
3602 array_size <= *ri - original_index)
3609 if (t == SD_BUS_TYPE_STRING) {
3611 r = message_peek_field_string(m, NULL, ri, NULL);
3617 } else if (t == SD_BUS_TYPE_OBJECT_PATH) {
3619 r = message_peek_field_string(m, object_path_is_valid, ri, NULL);
3625 } else if (t == SD_BUS_TYPE_SIGNATURE) {
3627 r = message_peek_field_signature(m, ri, NULL);
3633 } else if (bus_type_is_basic(t)) {
3636 align = bus_type_get_alignment(t);
3637 k = bus_type_get_size(t);
3638 assert(align > 0 && k > 0);
3640 r = message_peek_fields(m, ri, align, k, NULL);
3646 } else if (t == SD_BUS_TYPE_ARRAY) {
3648 r = signature_element_length(*signature+1, &l);
3658 strncpy(sig, *signature + 1, l-1);
3661 alignment = bus_type_get_alignment(sig[0]);
3665 r = message_peek_field_uint32(m, ri, &nas);
3668 if (nas > BUS_ARRAY_MAX_SIZE)
3671 r = message_peek_fields(m, ri, alignment, 0, NULL);
3675 r = message_skip_fields(m, ri, nas, (const char**) &s);
3680 (*signature) += 1 + l;
3682 } else if (t == SD_BUS_TYPE_VARIANT) {
3685 r = message_peek_field_signature(m, ri, &s);
3689 r = message_skip_fields(m, ri, (uint32_t) -1, (const char**) &s);
3695 } else if (t == SD_BUS_TYPE_STRUCT ||
3696 t == SD_BUS_TYPE_DICT_ENTRY) {
3698 r = signature_element_length(*signature, &l);
3705 strncpy(sig, *signature + 1, l-1);
3708 r = message_skip_fields(m, ri, (uint32_t) -1, (const char**) &s);
3719 int bus_message_parse_fields(sd_bus_message *m) {
3722 uint32_t unix_fds = 0;
3726 for (ri = 0; ri < BUS_MESSAGE_FIELDS_SIZE(m); ) {
3727 const char *signature;
3730 r = message_peek_fields(m, &ri, 8, 1, (void**) &header);
3734 r = message_peek_field_signature(m, &ri, &signature);
3739 case _SD_BUS_MESSAGE_HEADER_INVALID:
3742 case SD_BUS_MESSAGE_HEADER_PATH:
3747 if (!streq(signature, "o"))
3750 r = message_peek_field_string(m, object_path_is_valid, &ri, &m->path);
3753 case SD_BUS_MESSAGE_HEADER_INTERFACE:
3758 if (!streq(signature, "s"))
3761 r = message_peek_field_string(m, interface_name_is_valid, &ri, &m->interface);
3764 case SD_BUS_MESSAGE_HEADER_MEMBER:
3769 if (!streq(signature, "s"))
3772 r = message_peek_field_string(m, member_name_is_valid, &ri, &m->member);
3775 case SD_BUS_MESSAGE_HEADER_ERROR_NAME:
3780 if (!streq(signature, "s"))
3783 r = message_peek_field_string(m, error_name_is_valid, &ri, &m->error.name);
3786 case SD_BUS_MESSAGE_HEADER_DESTINATION:
3791 if (!streq(signature, "s"))
3794 r = message_peek_field_string(m, service_name_is_valid, &ri, &m->destination);
3797 case SD_BUS_MESSAGE_HEADER_SENDER:
3802 if (!streq(signature, "s"))
3805 r = message_peek_field_string(m, service_name_is_valid, &ri, &m->sender);
3809 case SD_BUS_MESSAGE_HEADER_SIGNATURE: {
3813 if (m->root_container.signature)
3816 if (!streq(signature, "g"))
3819 r = message_peek_field_signature(m, &ri, &s);
3827 free(m->root_container.signature);
3828 m->root_container.signature = c;
3832 case SD_BUS_MESSAGE_HEADER_REPLY_SERIAL:
3833 if (m->reply_serial != 0)
3836 if (!streq(signature, "u"))
3839 r = message_peek_field_uint32(m, &ri, &m->reply_serial);
3843 if (m->reply_serial == 0)
3848 case SD_BUS_MESSAGE_HEADER_UNIX_FDS:
3852 if (!streq(signature, "u"))
3855 r = message_peek_field_uint32(m, &ri, &unix_fds);
3865 r = message_skip_fields(m, &ri, (uint32_t) -1, (const char **) &signature);
3872 if (m->n_fds != unix_fds)
3875 if (isempty(m->root_container.signature) != (BUS_MESSAGE_BODY_SIZE(m) == 0))
3878 switch (m->header->type) {
3880 case SD_BUS_MESSAGE_TYPE_SIGNAL:
3881 if (!m->path || !m->interface || !m->member)
3885 case SD_BUS_MESSAGE_TYPE_METHOD_CALL:
3887 if (!m->path || !m->member)
3892 case SD_BUS_MESSAGE_TYPE_METHOD_RETURN:
3894 if (m->reply_serial == 0)
3898 case SD_BUS_MESSAGE_TYPE_METHOD_ERROR:
3900 if (m->reply_serial == 0 || !m->error.name)
3905 /* Try to read the error message, but if we can't it's a non-issue */
3906 if (m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_ERROR)
3907 sd_bus_message_read(m, "s", &m->error.message);
3912 int bus_message_seal(sd_bus_message *m, uint64_t serial) {
3913 struct bus_body_part *part;
3923 if (m->n_containers > 0)
3929 /* If there's a non-trivial signature set, then add it in here */
3930 if (!isempty(m->root_container.signature)) {
3931 r = message_append_field_signature(m, SD_BUS_MESSAGE_HEADER_SIGNATURE, m->root_container.signature, NULL);
3937 r = message_append_field_uint32(m, SD_BUS_MESSAGE_HEADER_UNIX_FDS, m->n_fds);
3942 /* Add padding at the end of the fields part, since we know
3943 * the body needs to start at an 8 byte alignment. We made
3944 * sure we allocated enough space for this, so all we need to
3945 * do here is to zero it out. */
3946 l = BUS_MESSAGE_FIELDS_SIZE(m);
3949 memset((uint8_t*) BUS_MESSAGE_FIELDS(m) + l, 0, a);
3951 /* If this is something we can send as memfd, then let's seal
3952 the memfd now. Note that we can send memfds as payload only
3953 for directed messages, and not for broadcasts. */
3954 if (m->destination && m->bus && m->bus->use_memfd) {
3955 MESSAGE_FOREACH_PART(part, i, m)
3956 if (part->memfd >= 0 && !part->sealed && (part->size > MEMFD_MIN_SIZE || m->bus->use_memfd < 0)) {
3957 bus_body_part_unmap(part);
3959 if (ioctl(part->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 1) >= 0)
3960 part->sealed = true;
3964 m->header->serial = serial;
3970 int sd_bus_message_set_destination(sd_bus_message *m, const char *destination) {
3980 return message_append_field_string(m, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &m->destination);
3983 int bus_message_dump(sd_bus_message *m) {
3984 const char *u = NULL, *uu = NULL, *s = NULL;
3985 char **cmdline = NULL;
3988 uid_t owner, audit_loginuid;
3989 uint32_t audit_sessionid;
3993 printf("Message %p\n"
4000 "\tfields_size=%u\n"
4005 "\tdestination=%s\n"
4008 "\treply_serial=%u\n"
4010 "\terror.message=%s\n"
4012 "\tn_body_parts=%u\n",
4019 BUS_MESSAGE_SERIAL(m),
4020 BUS_MESSAGE_FIELDS_SIZE(m),
4021 BUS_MESSAGE_BODY_SIZE(m),
4023 strna(m->interface),
4025 strna(m->destination),
4027 strna(m->root_container.signature),
4029 strna(m->error.name),
4030 strna(m->error.message),
4035 printf("\tpid=%lu\n", (unsigned long) m->pid);
4037 printf("\ttid=%lu\n", (unsigned long) m->tid);
4039 printf("\tuid=%lu\n", (unsigned long) m->uid);
4041 printf("\tgid=%lu\n", (unsigned long) m->gid);
4042 if (m->pid_starttime != 0)
4043 printf("\tpid_starttime=%llu\n", (unsigned long long) m->pid_starttime);
4044 if (m->monotonic != 0)
4045 printf("\tmonotonic=%llu\n", (unsigned long long) m->monotonic);
4046 if (m->realtime != 0)
4047 printf("\trealtime=%llu\n", (unsigned long long) m->realtime);
4049 printf("\texe=[%s]\n", m->exe);
4051 printf("\tcomm=[%s]\n", m->comm);
4053 printf("\ttid_comm=[%s]\n", m->tid_comm);
4055 printf("\tlabel=[%s]\n", m->label);
4057 printf("\tcgroup=[%s]\n", m->cgroup);
4059 sd_bus_message_get_unit(m, &u);
4061 printf("\tunit=[%s]\n", u);
4062 sd_bus_message_get_user_unit(m, &uu);
4064 printf("\tuser_unit=[%s]\n", uu);
4065 sd_bus_message_get_session(m, &s);
4067 printf("\tsession=[%s]\n", s);
4068 if (sd_bus_message_get_owner_uid(m, &owner) >= 0)
4069 printf("\towner_uid=%lu\n", (unsigned long) owner);
4070 if (sd_bus_message_get_audit_loginuid(m, &audit_loginuid) >= 0)
4071 printf("\taudit_loginuid=%lu\n", (unsigned long) audit_loginuid);
4072 if (sd_bus_message_get_audit_sessionid(m, &audit_sessionid) >= 0)
4073 printf("\taudit_sessionid=%lu\n", (unsigned long) audit_sessionid);
4075 printf("\tCAP_KILL=%i\n", sd_bus_message_has_effective_cap(m, 5));
4077 if (sd_bus_message_get_cmdline(m, &cmdline) >= 0) {
4080 fputs("\tcmdline=[", stdout);
4081 STRV_FOREACH(c, cmdline) {
4088 fputs("]\n", stdout);
4091 r = sd_bus_message_rewind(m, true);
4093 log_error("Failed to rewind: %s", strerror(-r));
4097 printf("BEGIN_MESSAGE \"%s\" {\n", strempty(m->root_container.signature));
4100 _cleanup_free_ char *prefix = NULL;
4101 const char *contents = NULL;
4116 r = sd_bus_message_peek_type(m, &type, &contents);
4118 log_error("Failed to peek type: %s", strerror(-r));
4125 r = sd_bus_message_exit_container(m);
4127 log_error("Failed to exit container: %s", strerror(-r));
4133 prefix = strrep("\t", level);
4137 if (type == SD_BUS_TYPE_ARRAY)
4138 printf("%s} END_ARRAY \n", prefix);
4139 else if (type == SD_BUS_TYPE_VARIANT)
4140 printf("%s} END_VARIANT\n", prefix);
4141 else if (type == SD_BUS_TYPE_STRUCT)
4142 printf("%s} END_STRUCT\n", prefix);
4143 else if (type == SD_BUS_TYPE_DICT_ENTRY)
4144 printf("%s} END_DICT_ENTRY\n", prefix);
4149 prefix = strrep("\t", level);
4153 if (bus_type_is_container(type) > 0) {
4154 r = sd_bus_message_enter_container(m, type, contents);
4156 log_error("Failed to enter container: %s", strerror(-r));
4160 if (type == SD_BUS_TYPE_ARRAY)
4161 printf("%sBEGIN_ARRAY \"%s\" {\n", prefix, contents);
4162 else if (type == SD_BUS_TYPE_VARIANT)
4163 printf("%sBEGIN_VARIANT \"%s\" {\n", prefix, contents);
4164 else if (type == SD_BUS_TYPE_STRUCT)
4165 printf("%sBEGIN_STRUCT \"%s\" {\n", prefix, contents);
4166 else if (type == SD_BUS_TYPE_DICT_ENTRY)
4167 printf("%sBEGIN_DICT_ENTRY \"%s\" {\n", prefix, contents);
4174 r = sd_bus_message_read_basic(m, type, &basic);
4176 log_error("Failed to get basic: %s", strerror(-r));
4182 case SD_BUS_TYPE_BYTE:
4183 printf("%sBYTE: %u\n", prefix, basic.u8);
4186 case SD_BUS_TYPE_BOOLEAN:
4187 printf("%sBOOLEAN: %s\n", prefix, yes_no(basic.i));
4190 case SD_BUS_TYPE_INT16:
4191 printf("%sINT16: %i\n", prefix, basic.s16);
4194 case SD_BUS_TYPE_UINT16:
4195 printf("%sUINT16: %u\n", prefix, basic.u16);
4198 case SD_BUS_TYPE_INT32:
4199 printf("%sINT32: %i\n", prefix, basic.s32);
4202 case SD_BUS_TYPE_UINT32:
4203 printf("%sUINT32: %u\n", prefix, basic.u32);
4206 case SD_BUS_TYPE_INT64:
4207 printf("%sINT64: %lli\n", prefix, (long long) basic.s64);
4210 case SD_BUS_TYPE_UINT64:
4211 printf("%sUINT64: %llu\n", prefix, (unsigned long long) basic.u64);
4214 case SD_BUS_TYPE_DOUBLE:
4215 printf("%sDOUBLE: %g\n", prefix, basic.d64);
4218 case SD_BUS_TYPE_STRING:
4219 printf("%sSTRING: \"%s\"\n", prefix, basic.string);
4222 case SD_BUS_TYPE_OBJECT_PATH:
4223 printf("%sOBJECT_PATH: \"%s\"\n", prefix, basic.string);
4226 case SD_BUS_TYPE_SIGNATURE:
4227 printf("%sSIGNATURE: \"%s\"\n", prefix, basic.string);
4230 case SD_BUS_TYPE_UNIX_FD:
4231 printf("%sUNIX_FD: %i\n", prefix, basic.i);
4235 assert_not_reached("Unknown basic type.");
4239 printf("} END_MESSAGE\n");
4243 int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz) {
4247 struct bus_body_part *part;
4253 total = BUS_MESSAGE_SIZE(m);
4259 e = mempcpy(p, m->header, BUS_MESSAGE_BODY_BEGIN(m));
4260 MESSAGE_FOREACH_PART(part, i, m)
4261 e = mempcpy(e, part->data, part->size);
4263 assert(total == (size_t) ((uint8_t*) e - (uint8_t*) p));
4271 int bus_message_read_strv_extend(sd_bus_message *m, char ***l) {
4277 r = sd_bus_message_enter_container(m, 'a', "s");
4284 r = sd_bus_message_read_basic(m, 's', &s);
4290 r = strv_extend(l, s);
4295 r = sd_bus_message_exit_container(m);
4302 const char* bus_message_get_arg(sd_bus_message *m, unsigned i) {
4304 const char *t = NULL;
4309 r = sd_bus_message_rewind(m, true);
4313 for (j = 0; j <= i; j++) {
4316 r = sd_bus_message_peek_type(m, &type, NULL);
4320 if (type != SD_BUS_TYPE_STRING &&
4321 type != SD_BUS_TYPE_OBJECT_PATH &&
4322 type != SD_BUS_TYPE_SIGNATURE)
4325 r = sd_bus_message_read_basic(m, type, &t);
4333 bool bus_header_is_complete(struct bus_header *h, size_t size) {
4339 if (size < sizeof(struct bus_header))
4342 full = sizeof(struct bus_header) +
4343 (h->endian == SD_BUS_NATIVE_ENDIAN ? h->fields_size : bswap_32(h->fields_size));
4345 return size >= full;
4348 int bus_header_message_size(struct bus_header *h, size_t *sum) {
4354 if (h->endian == SD_BUS_NATIVE_ENDIAN) {
4355 fs = h->fields_size;
4357 } else if (h->endian == SD_BUS_REVERSE_ENDIAN) {
4358 fs = bswap_32(h->fields_size);
4359 bs = bswap_32(h->body_size);
4363 *sum = sizeof(struct bus_header) + ALIGN8(fs) + bs;
4367 int bus_message_to_errno(sd_bus_message *m) {
4370 if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_ERROR)
4373 return bus_error_to_errno(&m->error);
4376 int sd_bus_message_get_signature(sd_bus_message *m, int complete, const char **signature) {
4377 struct bus_container *c;
4384 c = complete ? &m->root_container : message_get_container(m);
4385 *signature = c->signature ?: "";