chiark / gitweb /
bus: add new sd_bus_creds object to encapsulate process credentials
[elogind.git] / src / libsystemd-bus / bus-message.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <sys/mman.h>
25
26 #include "util.h"
27 #include "utf8.h"
28 #include "strv.h"
29 #include "time-util.h"
30 #include "cgroup-util.h"
31
32 #include "sd-bus.h"
33 #include "bus-message.h"
34 #include "bus-internal.h"
35 #include "bus-type.h"
36 #include "bus-signature.h"
37
38 static int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored);
39
40 static void *adjust_pointer(const void *p, void *old_base, size_t sz, void *new_base) {
41
42         if (p == NULL)
43                 return NULL;
44
45         if (old_base == new_base)
46                 return (void*) p;
47
48         if ((uint8_t*) p < (uint8_t*) old_base)
49                 return (void*) p;
50
51         if ((uint8_t*) p >= (uint8_t*) old_base + sz)
52                 return (void*) p;
53
54         return (uint8_t*) new_base + ((uint8_t*) p - (uint8_t*) old_base);
55 }
56
57 static void message_free_part(sd_bus_message *m, struct bus_body_part *part) {
58         assert(m);
59         assert(part);
60
61         if (part->memfd >= 0) {
62                 /* If we can reuse the memfd, try that. For that it
63                  * can't be sealed yet. */
64
65                 if (!part->sealed)
66                         bus_kernel_push_memfd(m->bus, part->memfd, part->data, part->mapped);
67                 else {
68                         if (part->mapped > 0)
69                                 assert_se(munmap(part->data, part->mapped) == 0);
70
71                         close_nointr_nofail(part->memfd);
72                 }
73
74         } else if (part->munmap_this)
75                 munmap(part->data, part->mapped);
76         else if (part->free_this)
77                 free(part->data);
78
79         if (part != &m->body)
80                 free(part);
81 }
82
83 static void message_reset_parts(sd_bus_message *m) {
84         struct bus_body_part *part;
85
86         assert(m);
87
88         part = &m->body;
89         while (m->n_body_parts > 0) {
90                 struct bus_body_part *next = part->next;
91                 message_free_part(m, part);
92                 part = next;
93                 m->n_body_parts--;
94         }
95
96         m->body_end = NULL;
97
98         m->cached_rindex_part = NULL;
99         m->cached_rindex_part_begin = 0;
100 }
101
102 static void message_reset_containers(sd_bus_message *m) {
103         unsigned i;
104
105         assert(m);
106
107         for (i = 0; i < m->n_containers; i++)
108                 free(m->containers[i].signature);
109
110         free(m->containers);
111         m->containers = NULL;
112
113         m->n_containers = 0;
114         m->root_container.index = 0;
115 }
116
117 static void message_free(sd_bus_message *m) {
118         assert(m);
119
120         if (m->free_header)
121                 free(m->header);
122
123         message_reset_parts(m);
124
125         if (m->free_kdbus)
126                 free(m->kdbus);
127
128         if (m->release_kdbus) {
129                 uint64_t off;
130
131                 off = (uint8_t *)m->kdbus - (uint8_t *)m->bus->kdbus_buffer;
132                 ioctl(m->bus->input_fd, KDBUS_CMD_MSG_RELEASE, &off);
133         }
134
135         if (m->bus)
136                 sd_bus_unref(m->bus);
137
138         if (m->free_fds) {
139                 close_many(m->fds, m->n_fds);
140                 free(m->fds);
141         }
142
143         if (m->iovec != m->iovec_fixed)
144                 free(m->iovec);
145
146         message_reset_containers(m);
147         free(m->root_container.signature);
148
149         free(m->peeked_signature);
150
151         bus_creds_done(&m->creds);
152         free(m);
153 }
154
155 static void *message_extend_fields(sd_bus_message *m, size_t align, size_t sz) {
156         void *op, *np;
157         size_t old_size, new_size, start;
158
159         assert(m);
160
161         if (m->poisoned)
162                 return NULL;
163
164         old_size = sizeof(struct bus_header) + m->header->fields_size;
165         start = ALIGN_TO(old_size, align);
166         new_size = start + sz;
167
168         if (old_size == new_size)
169                 return (uint8_t*) m->header + old_size;
170
171         if (new_size > (size_t) ((uint32_t) -1))
172                 goto poison;
173
174         if (m->free_header) {
175                 np = realloc(m->header, ALIGN8(new_size));
176                 if (!np)
177                         goto poison;
178         } else {
179                 /* Initially, the header is allocated as part of of
180                  * the sd_bus_message itself, let's replace it by
181                  * dynamic data */
182
183                 np = malloc(ALIGN8(new_size));
184                 if (!np)
185                         goto poison;
186
187                 memcpy(np, m->header, sizeof(struct bus_header));
188         }
189
190         /* Zero out padding */
191         if (start > old_size)
192                 memset((uint8_t*) np + old_size, 0, start - old_size);
193
194         op = m->header;
195         m->header = np;
196         m->header->fields_size = new_size - sizeof(struct bus_header);
197
198         /* Adjust quick access pointers */
199         m->path = adjust_pointer(m->path, op, old_size, m->header);
200         m->interface = adjust_pointer(m->interface, op, old_size, m->header);
201         m->member = adjust_pointer(m->member, op, old_size, m->header);
202         m->destination = adjust_pointer(m->destination, op, old_size, m->header);
203         m->sender = adjust_pointer(m->sender, op, old_size, m->header);
204         m->error.name = adjust_pointer(m->error.name, op, old_size, m->header);
205
206         m->free_header = true;
207
208         return (uint8_t*) np + start;
209
210 poison:
211         m->poisoned = true;
212         return NULL;
213 }
214
215 static int message_append_field_string(
216                 sd_bus_message *m,
217                 uint8_t h,
218                 char type,
219                 const char *s,
220                 const char **ret) {
221
222         size_t l;
223         uint8_t *p;
224
225         assert(m);
226
227         l = strlen(s);
228         if (l > (size_t) (uint32_t) -1)
229                 return -EINVAL;
230
231         /* field id byte + signature length + signature 's' + NUL + string length + string + NUL */
232         p = message_extend_fields(m, 8, 4 + 4 + l + 1);
233         if (!p)
234                 return -ENOMEM;
235
236         p[0] = h;
237         p[1] = 1;
238         p[2] = type;
239         p[3] = 0;
240
241         ((uint32_t*) p)[1] = l;
242         memcpy(p + 8, s, l + 1);
243
244         if (ret)
245                 *ret = (char*) p + 8;
246
247         return 0;
248 }
249
250 static int message_append_field_signature(
251                 sd_bus_message *m,
252                 uint8_t h,
253                 const char *s,
254                 const char **ret) {
255
256         size_t l;
257         uint8_t *p;
258
259         assert(m);
260
261         l = strlen(s);
262         if (l > 255)
263                 return -EINVAL;
264
265         /* field id byte + signature length + signature 'g' + NUL + string length + string + NUL */
266         p = message_extend_fields(m, 8, 4 + 1 + l + 1);
267         if (!p)
268                 return -ENOMEM;
269
270         p[0] = h;
271         p[1] = 1;
272         p[2] = SD_BUS_TYPE_SIGNATURE;
273         p[3] = 0;
274         p[4] = l;
275         memcpy(p + 5, s, l + 1);
276
277         if (ret)
278                 *ret = (const char*) p + 5;
279
280         return 0;
281 }
282
283 static int message_append_field_uint32(sd_bus_message *m, uint8_t h, uint32_t x) {
284         uint8_t *p;
285
286         assert(m);
287
288         /* field id byte + signature length + signature 'u' + NUL + value */
289         p = message_extend_fields(m, 8, 4 + 4);
290         if (!p)
291                 return -ENOMEM;
292
293         p[0] = h;
294         p[1] = 1;
295         p[2] = SD_BUS_TYPE_UINT32;
296         p[3] = 0;
297
298         ((uint32_t*) p)[1] = x;
299
300         return 0;
301 }
302
303 int bus_message_from_header(
304                 sd_bus *bus,
305                 void *buffer,
306                 size_t length,
307                 int *fds,
308                 unsigned n_fds,
309                 const struct ucred *ucred,
310                 const char *label,
311                 size_t extra,
312                 sd_bus_message **ret) {
313
314         sd_bus_message *m;
315         struct bus_header *h;
316         size_t a, label_sz;
317
318         assert(buffer || length <= 0);
319         assert(fds || n_fds <= 0);
320         assert(ret);
321
322         if (length < sizeof(struct bus_header))
323                 return -EBADMSG;
324
325         h = buffer;
326         if (h->version != 1)
327                 return -EBADMSG;
328
329         if (h->serial == 0)
330                 return -EBADMSG;
331
332         if (h->type == _SD_BUS_MESSAGE_TYPE_INVALID)
333                 return -EBADMSG;
334
335         if (h->endian != SD_BUS_LITTLE_ENDIAN &&
336             h->endian != SD_BUS_BIG_ENDIAN)
337                 return -EBADMSG;
338
339         a = ALIGN(sizeof(sd_bus_message)) + ALIGN(extra);
340
341         if (label) {
342                 label_sz = strlen(label);
343                 a += label_sz + 1;
344         }
345
346         m = malloc0(a);
347         if (!m)
348                 return -ENOMEM;
349
350         m->n_ref = 1;
351         m->sealed = true;
352         m->header = h;
353         m->fds = fds;
354         m->n_fds = n_fds;
355
356         if (ucred) {
357                 m->creds.uid = ucred->uid;
358                 m->creds.pid = ucred->pid;
359                 m->creds.gid = ucred->gid;
360                 m->creds.mask |= SD_BUS_CREDS_UID | SD_BUS_CREDS_PID | SD_BUS_CREDS_GID;
361         }
362
363         if (label) {
364                 m->creds.label = (char*) m + ALIGN(sizeof(sd_bus_message)) + ALIGN(extra);
365                 memcpy(m->creds.label, label, label_sz + 1);
366
367                 m->creds.mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
368         }
369
370         if (bus)
371                 m->bus = sd_bus_ref(bus);
372
373         *ret = m;
374         return 0;
375 }
376
377 int bus_message_from_malloc(
378                 sd_bus *bus,
379                 void *buffer,
380                 size_t length,
381                 int *fds,
382                 unsigned n_fds,
383                 const struct ucred *ucred,
384                 const char *label,
385                 sd_bus_message **ret) {
386
387         sd_bus_message *m;
388         int r;
389
390         r = bus_message_from_header(bus, buffer, length, fds, n_fds, ucred, label, 0, &m);
391         if (r < 0)
392                 return r;
393
394         if (length != BUS_MESSAGE_SIZE(m)) {
395                 r = -EBADMSG;
396                 goto fail;
397         }
398
399         m->n_body_parts = 1;
400         m->body.data = (uint8_t*) buffer + sizeof(struct bus_header) + ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m));
401         m->body.size = length - sizeof(struct bus_header) - ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m));
402         m->body.sealed = true;
403         m->body.memfd = -1;
404
405         m->n_iovec = 1;
406         m->iovec = m->iovec_fixed;
407         m->iovec[0].iov_base = buffer;
408         m->iovec[0].iov_len = length;
409
410         r = bus_message_parse_fields(m);
411         if (r < 0)
412                 goto fail;
413
414         /* We take possession of the memory and fds now */
415         m->free_header = true;
416         m->free_fds = true;
417
418         *ret = m;
419         return 0;
420
421 fail:
422         message_free(m);
423         return r;
424 }
425
426 static sd_bus_message *message_new(sd_bus *bus, uint8_t type) {
427         sd_bus_message *m;
428
429         m = malloc0(ALIGN(sizeof(sd_bus_message)) + sizeof(struct bus_header));
430         if (!m)
431                 return NULL;
432
433         m->n_ref = 1;
434         m->header = (struct bus_header*) ((uint8_t*) m + ALIGN(sizeof(struct sd_bus_message)));
435         m->header->endian = SD_BUS_NATIVE_ENDIAN;
436         m->header->type = type;
437         m->header->version = bus ? bus->message_version : 1;
438         m->allow_fds = !bus || bus->can_fds || (bus->state != BUS_HELLO && bus->state != BUS_RUNNING);
439
440         if (bus)
441                 m->bus = sd_bus_ref(bus);
442
443         return m;
444 }
445
446 _public_ int sd_bus_message_new_signal(
447                 sd_bus *bus,
448                 const char *path,
449                 const char *interface,
450                 const char *member,
451                 sd_bus_message **m) {
452
453         sd_bus_message *t;
454         int r;
455
456         assert_return(!bus || bus->state != BUS_UNSET, -ENOTCONN);
457         assert_return(object_path_is_valid(path), -EINVAL);
458         assert_return(interface_name_is_valid(interface), -EINVAL);
459         assert_return(member_name_is_valid(member), -EINVAL);
460         assert_return(m, -EINVAL);
461
462         t = message_new(bus, SD_BUS_MESSAGE_SIGNAL);
463         if (!t)
464                 return -ENOMEM;
465
466         t->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
467
468         r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path);
469         if (r < 0)
470                 goto fail;
471         r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_INTERFACE, SD_BUS_TYPE_STRING, interface, &t->interface);
472         if (r < 0)
473                 goto fail;
474         r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_MEMBER, SD_BUS_TYPE_STRING, member, &t->member);
475         if (r < 0)
476                 goto fail;
477
478         *m = t;
479         return 0;
480
481 fail:
482         sd_bus_message_unref(t);
483         return r;
484 }
485
486 _public_ int sd_bus_message_new_method_call(
487                 sd_bus *bus,
488                 const char *destination,
489                 const char *path,
490                 const char *interface,
491                 const char *member,
492                 sd_bus_message **m) {
493
494         sd_bus_message *t;
495         int r;
496
497         assert_return(!bus || bus->state != BUS_UNSET, -ENOTCONN);
498         assert_return(!destination || service_name_is_valid(destination), -EINVAL);
499         assert_return(object_path_is_valid(path), -EINVAL);
500         assert_return(!interface || interface_name_is_valid(interface), -EINVAL);
501         assert_return(member_name_is_valid(member), -EINVAL);
502         assert_return(m, -EINVAL);
503
504         t = message_new(bus, SD_BUS_MESSAGE_METHOD_CALL);
505         if (!t)
506                 return -ENOMEM;
507
508         r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path);
509         if (r < 0)
510                 goto fail;
511         r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_MEMBER, SD_BUS_TYPE_STRING, member, &t->member);
512         if (r < 0)
513                 goto fail;
514
515         if (interface) {
516                 r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_INTERFACE, SD_BUS_TYPE_STRING, interface, &t->interface);
517                 if (r < 0)
518                         goto fail;
519         }
520
521         if (destination) {
522                 r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &t->destination);
523                 if (r < 0)
524                         goto fail;
525         }
526
527         *m = t;
528         return 0;
529
530 fail:
531         message_free(t);
532         return r;
533 }
534
535 static int message_new_reply(
536                 sd_bus_message *call,
537                 uint8_t type,
538                 sd_bus_message **m) {
539
540         sd_bus_message *t;
541         int r;
542
543         assert_return(call, -EINVAL);
544         assert_return(call->sealed, -EPERM);
545         assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
546         assert_return(!call->bus || call->bus->state != BUS_UNSET, -ENOTCONN);
547         assert_return(m, -EINVAL);
548
549         t = message_new(call->bus, type);
550         if (!t)
551                 return -ENOMEM;
552
553         t->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
554         t->reply_serial = BUS_MESSAGE_SERIAL(call);
555
556         r = message_append_field_uint32(t, SD_BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_serial);
557         if (r < 0)
558                 goto fail;
559
560         if (call->sender) {
561                 r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, call->sender, &t->destination);
562                 if (r < 0)
563                         goto fail;
564         }
565
566         t->dont_send = !!(call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED);
567
568         *m = t;
569         return 0;
570
571 fail:
572         message_free(t);
573         return r;
574 }
575
576 _public_ int sd_bus_message_new_method_return(
577                 sd_bus_message *call,
578                 sd_bus_message **m) {
579
580         return message_new_reply(call, SD_BUS_MESSAGE_METHOD_RETURN, m);
581 }
582
583 _public_ int sd_bus_message_new_method_error(
584                 sd_bus_message *call,
585                 const sd_bus_error *e,
586                 sd_bus_message **m) {
587
588         sd_bus_message *t;
589         int r;
590
591         assert_return(sd_bus_error_is_set(e), -EINVAL);
592         assert_return(m, -EINVAL);
593
594         r = message_new_reply(call, SD_BUS_MESSAGE_METHOD_ERROR, &t);
595         if (r < 0)
596                 return r;
597
598         r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, e->name, &t->error.name);
599         if (r < 0)
600                 goto fail;
601
602         if (e->message) {
603                 r = message_append_basic(t, SD_BUS_TYPE_STRING, e->message, (const void**) &t->error.message);
604                 if (r < 0)
605                         goto fail;
606         }
607
608         *m = t;
609         return 0;
610
611 fail:
612         message_free(t);
613         return r;
614 }
615
616 _public_ int sd_bus_message_new_method_errorf(
617                 sd_bus_message *call,
618                 sd_bus_message **m,
619                 const char *name,
620                 const char *format,
621                 ...) {
622
623         _cleanup_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
624         va_list ap;
625
626         assert_return(name, -EINVAL);
627         assert_return(m, -EINVAL);
628
629         va_start(ap, format);
630         bus_error_setfv(&error, name, format, ap);
631         va_end(ap);
632
633         return sd_bus_message_new_method_error(call, &error, m);
634 }
635
636 _public_ int sd_bus_message_new_method_errno(
637                 sd_bus_message *call,
638                 int error,
639                 const sd_bus_error *p,
640                 sd_bus_message **m) {
641
642         _cleanup_free_ sd_bus_error berror = SD_BUS_ERROR_NULL;
643
644         if (sd_bus_error_is_set(p))
645                 return sd_bus_message_new_method_error(call, p, m);
646
647         sd_bus_error_set_errno(&berror, error);
648
649         return sd_bus_message_new_method_error(call, &berror, m);
650 }
651
652 _public_ int sd_bus_message_new_method_errnof(
653                 sd_bus_message *call,
654                 sd_bus_message **m,
655                 int error,
656                 const char *format,
657                 ...) {
658
659         _cleanup_free_ sd_bus_error berror = SD_BUS_ERROR_NULL;
660         va_list ap;
661
662         va_start(ap, format);
663         bus_error_set_errnofv(&berror, error, format, ap);
664         va_end(ap);
665
666         return sd_bus_message_new_method_error(call, &berror, m);
667 }
668
669 int bus_message_new_synthetic_error(
670                 sd_bus *bus,
671                 uint64_t serial,
672                 const sd_bus_error *e,
673                 sd_bus_message **m) {
674
675         sd_bus_message *t;
676         int r;
677
678         assert(sd_bus_error_is_set(e));
679         assert(m);
680
681         t = message_new(bus, SD_BUS_MESSAGE_METHOD_ERROR);
682         if (!t)
683                 return -ENOMEM;
684
685         t->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
686         t->reply_serial = serial;
687
688         r = message_append_field_uint32(t, SD_BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_serial);
689         if (r < 0)
690                 goto fail;
691
692         if (bus && bus->unique_name) {
693                 r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, bus->unique_name, &t->destination);
694                 if (r < 0)
695                         goto fail;
696         }
697
698         r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, e->name, &t->error.name);
699         if (r < 0)
700                 goto fail;
701
702         if (e->message) {
703                 r = message_append_basic(t, SD_BUS_TYPE_STRING, e->message, (const void**) &t->error.message);
704                 if (r < 0)
705                         goto fail;
706         }
707
708         *m = t;
709         return 0;
710
711 fail:
712         message_free(t);
713         return r;
714 }
715
716 _public_ sd_bus_message* sd_bus_message_ref(sd_bus_message *m) {
717         assert_return(m, NULL);
718
719         assert(m->n_ref > 0);
720         m->n_ref++;
721
722         return m;
723 }
724
725 _public_ sd_bus_message* sd_bus_message_unref(sd_bus_message *m) {
726         assert_return(m, NULL);
727
728         assert(m->n_ref > 0);
729         m->n_ref--;
730
731         if (m->n_ref <= 0)
732                 message_free(m);
733
734         return NULL;
735 }
736
737 _public_ int sd_bus_message_get_type(sd_bus_message *m, uint8_t *type) {
738         assert_return(m, -EINVAL);
739         assert_return(type, -EINVAL);
740
741         *type = m->header->type;
742         return 0;
743 }
744
745 _public_ int sd_bus_message_get_serial(sd_bus_message *m, uint64_t *serial) {
746         assert_return(m, -EINVAL);
747         assert_return(serial, -EINVAL);
748         assert_return(m->header->serial != 0, -ENOENT);
749
750         *serial = BUS_MESSAGE_SERIAL(m);
751         return 0;
752 }
753
754 _public_ int sd_bus_message_get_reply_serial(sd_bus_message *m, uint64_t *serial) {
755         assert_return(m, -EINVAL);
756         assert_return(serial, -EINVAL);
757         assert_return(m->reply_serial != 0, -ENOENT);
758
759         *serial = m->reply_serial;
760         return 0;
761 }
762
763 _public_ int sd_bus_message_get_no_reply(sd_bus_message *m) {
764         assert_return(m, -EINVAL);
765
766         return m->header->type == SD_BUS_MESSAGE_METHOD_CALL ? !!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED) : 0;
767 }
768
769 _public_ int sd_bus_message_get_no_auto_start(sd_bus_message *m) {
770         assert_return(m, -EINVAL);
771
772         return !!(m->header->flags & SD_BUS_MESSAGE_NO_AUTO_START);
773 }
774
775 _public_ const char *sd_bus_message_get_path(sd_bus_message *m) {
776         assert_return(m, NULL);
777
778         return m->path;
779 }
780
781 _public_ const char *sd_bus_message_get_interface(sd_bus_message *m) {
782         assert_return(m, NULL);
783
784         return m->interface;
785 }
786
787 _public_ const char *sd_bus_message_get_member(sd_bus_message *m) {
788         assert_return(m, NULL);
789
790         return m->member;
791 }
792
793 _public_ const char *sd_bus_message_get_destination(sd_bus_message *m) {
794         assert_return(m, NULL);
795
796         return m->destination;
797 }
798
799 _public_ const char *sd_bus_message_get_sender(sd_bus_message *m) {
800         assert_return(m, NULL);
801
802         return m->sender;
803 }
804
805 _public_ const sd_bus_error *sd_bus_message_get_error(sd_bus_message *m) {
806         assert_return(m, NULL);
807         assert_return(sd_bus_error_is_set(&m->error), NULL);
808
809         return &m->error;
810 }
811
812 _public_ int sd_bus_message_get_monotonic_timestamp(sd_bus_message *m, uint64_t *usec) {
813         assert_return(m, -EINVAL);
814         assert_return(usec, -EINVAL);
815         assert_return(m->monotonic > 0, -ENODATA);
816
817         *usec = m->monotonic;
818         return 0;
819 }
820
821 _public_ int sd_bus_message_get_realtime_timestamp(sd_bus_message *m, uint64_t *usec) {
822         assert_return(m, -EINVAL);
823         assert_return(usec, -EINVAL);
824         assert_return(m->realtime > 0, -ENODATA);
825
826         *usec = m->realtime;
827         return 0;
828 }
829
830 _public_ sd_bus_creds *sd_bus_message_get_creds(sd_bus_message *m) {
831         assert_return(m, NULL);
832
833         if (m->creds.mask == 0)
834                 return NULL;
835
836         return &m->creds;
837 }
838
839 _public_ int sd_bus_message_is_signal(sd_bus_message *m,
840                                       const char *interface,
841                                       const char *member) {
842         assert_return(m, -EINVAL);
843
844         if (m->header->type != SD_BUS_MESSAGE_SIGNAL)
845                 return 0;
846
847         if (interface && (!m->interface || !streq(m->interface, interface)))
848                 return 0;
849
850         if (member &&  (!m->member || !streq(m->member, member)))
851                 return 0;
852
853         return 1;
854 }
855
856 _public_ int sd_bus_message_is_method_call(sd_bus_message *m,
857                                            const char *interface,
858                                            const char *member) {
859         assert_return(m, -EINVAL);
860
861         if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
862                 return 0;
863
864         if (interface && (!m->interface || !streq(m->interface, interface)))
865                 return 0;
866
867         if (member &&  (!m->member || !streq(m->member, member)))
868                 return 0;
869
870         return 1;
871 }
872
873 _public_ int sd_bus_message_is_method_error(sd_bus_message *m, const char *name) {
874         assert_return(m, -EINVAL);
875
876         if (m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
877                 return 0;
878
879         if (name && (!m->error.name || !streq(m->error.name, name)))
880                 return 0;
881
882         return 1;
883 }
884
885 _public_ int sd_bus_message_set_no_reply(sd_bus_message *m, int b) {
886         assert_return(m, -EINVAL);
887         assert_return(!m->sealed, -EPERM);
888         assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EPERM);
889
890         if (b)
891                 m->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
892         else
893                 m->header->flags &= ~SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
894
895         return 0;
896 }
897
898 _public_ int sd_bus_message_set_no_auto_start(sd_bus_message *m, int b) {
899         assert_return(m, -EINVAL);
900         assert_return(!m->sealed, -EPERM);
901
902         if (b)
903                 m->header->flags |= SD_BUS_MESSAGE_NO_AUTO_START;
904         else
905                 m->header->flags &= ~SD_BUS_MESSAGE_NO_AUTO_START;
906
907         return 0;
908 }
909
910 static struct bus_container *message_get_container(sd_bus_message *m) {
911         assert(m);
912
913         if (m->n_containers == 0)
914                 return &m->root_container;
915
916         assert(m->containers);
917         return m->containers + m->n_containers - 1;
918 }
919
920 struct bus_body_part *message_append_part(sd_bus_message *m) {
921         struct bus_body_part *part;
922
923         assert(m);
924
925         if (m->poisoned)
926                 return NULL;
927
928         if (m->n_body_parts <= 0) {
929                 part = &m->body;
930                 zero(*part);
931         } else {
932                 assert(m->body_end);
933
934                 part = new0(struct bus_body_part, 1);
935                 if (!part) {
936                         m->poisoned = true;
937                         return NULL;
938                 }
939
940                 m->body_end->next = part;
941         }
942
943         part->memfd = -1;
944         m->body_end = part;
945         m->n_body_parts ++;
946
947         return part;
948 }
949
950 static void part_zero(struct bus_body_part *part, size_t sz) {
951         assert(part);
952         assert(sz > 0);
953         assert(sz < 8);
954
955         /* All other fields can be left in their defaults */
956         assert(!part->data);
957         assert(part->memfd < 0);
958
959         part->size = sz;
960         part->is_zero = true;
961         part->sealed = true;
962 }
963
964 static int part_make_space(
965                 struct sd_bus_message *m,
966                 struct bus_body_part *part,
967                 size_t sz,
968                 void **q) {
969
970         void *n;
971         int r;
972
973         assert(m);
974         assert(part);
975         assert(!part->sealed);
976
977         if (m->poisoned)
978                 return -ENOMEM;
979
980         if (!part->data && part->memfd < 0)
981                 part->memfd = bus_kernel_pop_memfd(m->bus, &part->data, &part->mapped);
982
983         if (part->memfd >= 0) {
984                 uint64_t u = sz;
985
986                 r = ioctl(part->memfd, KDBUS_CMD_MEMFD_SIZE_SET, &u);
987                 if (r < 0) {
988                         m->poisoned = true;
989                         return -errno;
990                 }
991
992                 if (!part->data || sz > part->mapped) {
993                         size_t psz = PAGE_ALIGN(sz > 0 ? sz : 1);
994
995                         if (part->mapped <= 0)
996                                 n = mmap(NULL, psz, PROT_READ|PROT_WRITE, MAP_SHARED, part->memfd, 0);
997                         else
998                                 n = mremap(part->data, part->mapped, psz, MREMAP_MAYMOVE);
999
1000                         if (n == MAP_FAILED) {
1001                                 m->poisoned = true;
1002                                 return -errno;
1003                         }
1004
1005                         part->mapped = psz;
1006                         part->data = n;
1007                 }
1008
1009                 part->munmap_this = true;
1010         } else {
1011                 n = realloc(part->data, MAX(sz, 1u));
1012                 if (!n) {
1013                         m->poisoned = true;
1014                         return -ENOMEM;
1015                 }
1016
1017                 part->data = n;
1018                 part->free_this = true;
1019         }
1020
1021         if (q)
1022                 *q = part->data ? (uint8_t*) part->data + part->size : NULL;
1023
1024         part->size = sz;
1025         return 0;
1026 }
1027
1028 static void message_extend_containers(sd_bus_message *m, size_t expand) {
1029         struct bus_container *c;
1030
1031         assert(m);
1032
1033         if (expand <= 0)
1034                 return;
1035
1036         /* Update counters */
1037         for (c = m->containers; c < m->containers + m->n_containers; c++)
1038                 if (c->array_size)
1039                         *c->array_size += expand;
1040 }
1041
1042 static void *message_extend_body(sd_bus_message *m, size_t align, size_t sz) {
1043         struct bus_body_part *part = NULL;
1044         size_t start_body, end_body, padding, start_part, end_part, added;
1045         bool add_new_part;
1046         void *p;
1047         int r;
1048
1049         assert(m);
1050         assert(align > 0);
1051         assert(!m->sealed);
1052
1053         if (m->poisoned)
1054                 return NULL;
1055
1056         start_body = ALIGN_TO((size_t) m->header->body_size, align);
1057         end_body = start_body + sz;
1058
1059         padding = start_body - m->header->body_size;
1060         added = padding + sz;
1061
1062         /* Check for 32bit overflows */
1063         if (end_body > (size_t) ((uint32_t) -1)) {
1064                 m->poisoned = true;
1065                 return NULL;
1066         }
1067
1068         add_new_part =
1069                 m->n_body_parts <= 0 ||
1070                 m->body_end->sealed ||
1071                 padding != ALIGN_TO(m->body_end->size, align) - m->body_end->size;
1072
1073         if (add_new_part) {
1074                 if (padding > 0) {
1075                         part = message_append_part(m);
1076                         if (!part)
1077                                 return NULL;
1078
1079                         part_zero(part, padding);
1080                 }
1081
1082                 part = message_append_part(m);
1083                 if (!part)
1084                         return NULL;
1085
1086                 r = part_make_space(m, part, sz, &p);
1087                 if (r < 0)
1088                         return NULL;
1089         } else {
1090                 struct bus_container *c;
1091                 void *op;
1092                 size_t os;
1093
1094                 part = m->body_end;
1095                 op = part->data;
1096                 os = part->size;
1097
1098                 start_part = ALIGN_TO(part->size, align);
1099                 end_part = start_part + sz;
1100
1101                 r = part_make_space(m, part, end_part, &p);
1102                 if (r < 0)
1103                         return NULL;
1104
1105                 if (padding > 0) {
1106                         memset(p, 0, padding);
1107                         p = (uint8_t*) p + padding;
1108                 }
1109
1110                 /* Readjust pointers */
1111                 for (c = m->containers; c < m->containers + m->n_containers; c++)
1112                         c->array_size = adjust_pointer(c->array_size, op, os, part->data);
1113
1114                 m->error.message = (const char*) adjust_pointer(m->error.message, op, os, part->data);
1115         }
1116
1117         m->header->body_size = end_body;
1118         message_extend_containers(m, added);
1119
1120         return p;
1121 }
1122
1123 int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored) {
1124         struct bus_container *c;
1125         ssize_t align, sz;
1126         uint32_t k;
1127         void *a;
1128         int fd = -1;
1129         uint32_t fdi = 0;
1130         int r;
1131
1132         assert_return(m, -EINVAL);
1133         assert_return(!m->sealed, -EPERM);
1134         assert_return(bus_type_is_basic(type), -EINVAL);
1135         assert_return(!m->poisoned, -ESTALE);
1136
1137         c = message_get_container(m);
1138
1139         if (c->signature && c->signature[c->index]) {
1140                 /* Container signature is already set */
1141
1142                 if (c->signature[c->index] != type)
1143                         return -ENXIO;
1144         } else {
1145                 char *e;
1146
1147                 /* Maybe we can append to the signature? But only if this is the top-level container*/
1148                 if (c->enclosing != 0)
1149                         return -ENXIO;
1150
1151                 e = strextend(&c->signature, CHAR_TO_STR(type), NULL);
1152                 if (!e) {
1153                         m->poisoned = true;
1154                         return -ENOMEM;
1155                 }
1156         }
1157
1158         switch (type) {
1159
1160         case SD_BUS_TYPE_STRING:
1161                 /* To make things easy we'll serialize a NULL string
1162                  * into the empty string */
1163                 p = strempty(p);
1164
1165                 /* Fall through... */
1166         case SD_BUS_TYPE_OBJECT_PATH:
1167
1168                 if (!p) {
1169                         r = -EINVAL;
1170                         goto fail;
1171                 }
1172
1173                 align = 4;
1174                 sz = 4 + strlen(p) + 1;
1175                 break;
1176
1177         case SD_BUS_TYPE_SIGNATURE:
1178
1179                 if (!p) {
1180                         r = -EINVAL;
1181                         goto fail;
1182                 }
1183
1184                 align = 1;
1185                 sz = 1 + strlen(p) + 1;
1186                 break;
1187
1188         case SD_BUS_TYPE_BOOLEAN:
1189
1190                 if (!p) {
1191                         r = -EINVAL;
1192                         goto fail;
1193                 }
1194
1195                 align = sz = 4;
1196
1197                 assert_cc(sizeof(int) == sizeof(uint32_t));
1198                 memcpy(&k, p, 4);
1199                 k = !!k;
1200                 p = &k;
1201                 break;
1202
1203         case SD_BUS_TYPE_UNIX_FD: {
1204                 int z, *f;
1205
1206                 if (!p) {
1207                         r = -EINVAL;
1208                         goto fail;
1209                 }
1210
1211                 if (!m->allow_fds) {
1212                         r = -ENOTSUP;
1213                         goto fail;
1214                 }
1215
1216                 align = sz = 4;
1217
1218                 z = *(int*) p;
1219                 if (z < 0) {
1220                         r = -EINVAL;
1221                         goto fail;
1222                 }
1223
1224                 fd = fcntl(z, F_DUPFD_CLOEXEC, 3);
1225                 if (fd < 0) {
1226                         r = -errno;
1227                         goto fail;
1228                 }
1229
1230                 f = realloc(m->fds, sizeof(int) * (m->n_fds + 1));
1231                 if (!f) {
1232                         m->poisoned = true;
1233                         r = -ENOMEM;
1234                         goto fail;
1235                 }
1236
1237                 fdi = m->n_fds;
1238                 f[fdi] = fd;
1239                 m->fds = f;
1240                 m->free_fds = true;
1241                 break;
1242         }
1243
1244         default:
1245                 if (!p) {
1246                         r = -EINVAL;
1247                         goto fail;
1248                 }
1249
1250                 align = bus_type_get_alignment(type);
1251                 sz = bus_type_get_size(type);
1252                 break;
1253         }
1254
1255         assert(align > 0);
1256         assert(sz > 0);
1257
1258         a = message_extend_body(m, align, sz);
1259         if (!a) {
1260                 r = -ENOMEM;
1261                 goto fail;
1262         }
1263
1264         if (type == SD_BUS_TYPE_STRING || type == SD_BUS_TYPE_OBJECT_PATH) {
1265                 *(uint32_t*) a = sz - 5;
1266                 memcpy((uint8_t*) a + 4, p, sz - 4);
1267
1268                 if (stored)
1269                         *stored = (const uint8_t*) a + 4;
1270
1271         } else if (type == SD_BUS_TYPE_SIGNATURE) {
1272                 *(uint8_t*) a = sz - 1;
1273                 memcpy((uint8_t*) a + 1, p, sz - 1);
1274
1275                 if (stored)
1276                         *stored = (const uint8_t*) a + 1;
1277         } else if (type == SD_BUS_TYPE_UNIX_FD) {
1278                 *(uint32_t*) a = fdi;
1279
1280                 if (stored)
1281                         *stored = a;
1282
1283                 m->n_fds ++;
1284
1285         } else {
1286                 memcpy(a, p, sz);
1287
1288                 if (stored)
1289                         *stored = a;
1290         }
1291
1292         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1293                 c->index++;
1294
1295         return 0;
1296
1297 fail:
1298         if (fd >= 0)
1299                 close_nointr_nofail(fd);
1300
1301         return r;
1302 }
1303
1304 _public_ int sd_bus_message_append_basic(sd_bus_message *m, char type, const void *p) {
1305         return message_append_basic(m, type, p, NULL);
1306 }
1307
1308 _public_ int sd_bus_message_append_string_space(
1309                 sd_bus_message *m,
1310                 size_t size,
1311                 char **s) {
1312
1313         struct bus_container *c;
1314         void *a;
1315
1316         assert_return(m, -EINVAL);
1317         assert_return(s, -EINVAL);
1318         assert_return(!m->sealed, -EPERM);
1319         assert_return(!m->poisoned, -ESTALE);
1320
1321         c = message_get_container(m);
1322
1323         if (c->signature && c->signature[c->index]) {
1324                 /* Container signature is already set */
1325
1326                 if (c->signature[c->index] != SD_BUS_TYPE_STRING)
1327                         return -ENXIO;
1328         } else {
1329                 char *e;
1330
1331                 /* Maybe we can append to the signature? But only if this is the top-level container*/
1332                 if (c->enclosing != 0)
1333                         return -ENXIO;
1334
1335                 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRING), NULL);
1336                 if (!e) {
1337                         m->poisoned = true;
1338                         return -ENOMEM;
1339                 }
1340         }
1341
1342         a = message_extend_body(m, 4, 4 + size + 1);
1343         if (!a)
1344                 return -ENOMEM;
1345
1346         *(uint32_t*) a = size;
1347         *s = (char*) a + 4;
1348
1349         (*s)[size] = 0;
1350
1351         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1352                 c->index++;
1353
1354         return 0;
1355 }
1356
1357 _public_ int sd_bus_message_append_string_iovec(
1358                 sd_bus_message *m,
1359                 const struct iovec *iov,
1360                 unsigned n) {
1361
1362         size_t size;
1363         unsigned i;
1364         char *p;
1365         int r;
1366
1367         assert_return(m, -EINVAL);
1368         assert_return(!m->sealed, -EPERM);
1369         assert_return(iov || n == 0, -EINVAL);
1370         assert_return(!m->poisoned, -ESTALE);
1371
1372         size = IOVEC_TOTAL_SIZE(iov, n);
1373
1374         r = sd_bus_message_append_string_space(m, size, &p);
1375         if (r < 0)
1376                 return r;
1377
1378         for (i = 0; i < n; i++) {
1379
1380                 if (iov[i].iov_base)
1381                         memcpy(p, iov[i].iov_base, iov[i].iov_len);
1382                 else
1383                         memset(p, ' ', iov[i].iov_len);
1384
1385                 p += iov[i].iov_len;
1386         }
1387
1388         return 0;
1389 }
1390
1391 static int bus_message_open_array(
1392                 sd_bus_message *m,
1393                 struct bus_container *c,
1394                 const char *contents,
1395                 uint32_t **array_size) {
1396
1397         unsigned nindex;
1398         void *a, *op;
1399         int alignment;
1400         size_t os;
1401         struct bus_body_part *o;
1402
1403         assert(m);
1404         assert(c);
1405         assert(contents);
1406         assert(array_size);
1407
1408         if (!signature_is_single(contents, true))
1409                 return -EINVAL;
1410
1411         alignment = bus_type_get_alignment(contents[0]);
1412         if (alignment < 0)
1413                 return alignment;
1414
1415         if (c->signature && c->signature[c->index]) {
1416
1417                 /* Verify the existing signature */
1418
1419                 if (c->signature[c->index] != SD_BUS_TYPE_ARRAY)
1420                         return -ENXIO;
1421
1422                 if (!startswith(c->signature + c->index + 1, contents))
1423                         return -ENXIO;
1424
1425                 nindex = c->index + 1 + strlen(contents);
1426         } else {
1427                 char *e;
1428
1429                 if (c->enclosing != 0)
1430                         return -ENXIO;
1431
1432                 /* Extend the existing signature */
1433
1434                 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_ARRAY), contents, NULL);
1435                 if (!e) {
1436                         m->poisoned = true;
1437                         return -ENOMEM;
1438                 }
1439
1440                 nindex = e - c->signature;
1441         }
1442
1443         a = message_extend_body(m, 4, 4);
1444         if (!a)
1445                 return -ENOMEM;
1446
1447         o = m->body_end;
1448         op = m->body_end->data;
1449         os = m->body_end->size;
1450
1451         /* Add alignment between size and first element */
1452         if (!message_extend_body(m, alignment, 0))
1453                 return -ENOMEM;
1454
1455         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1456                 c->index = nindex;
1457
1458         /* location of array size might have changed so let's readjust a */
1459         if (o == m->body_end)
1460                 a = adjust_pointer(a, op, os, m->body_end->data);
1461
1462         *(uint32_t*) a = 0;
1463         *array_size = a;
1464         return 0;
1465 }
1466
1467 static int bus_message_open_variant(
1468                 sd_bus_message *m,
1469                 struct bus_container *c,
1470                 const char *contents) {
1471
1472         size_t l;
1473         void *a;
1474
1475         assert(m);
1476         assert(c);
1477         assert(contents);
1478
1479         if (!signature_is_single(contents, false))
1480                 return -EINVAL;
1481
1482         if (*contents == SD_BUS_TYPE_DICT_ENTRY_BEGIN)
1483                 return -EINVAL;
1484
1485         if (c->signature && c->signature[c->index]) {
1486
1487                 if (c->signature[c->index] != SD_BUS_TYPE_VARIANT)
1488                         return -ENXIO;
1489
1490         } else {
1491                 char *e;
1492
1493                 if (c->enclosing != 0)
1494                         return -ENXIO;
1495
1496                 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_VARIANT), NULL);
1497                 if (!e) {
1498                         m->poisoned = true;
1499                         return -ENOMEM;
1500                 }
1501         }
1502
1503         l = strlen(contents);
1504         a = message_extend_body(m, 1, 1 + l + 1);
1505         if (!a)
1506                 return -ENOMEM;
1507
1508         *(uint8_t*) a = l;
1509         memcpy((uint8_t*) a + 1, contents, l + 1);
1510
1511         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1512                 c->index++;
1513
1514         return 0;
1515 }
1516
1517 static int bus_message_open_struct(
1518                 sd_bus_message *m,
1519                 struct bus_container *c,
1520                 const char *contents) {
1521
1522         size_t nindex;
1523
1524         assert(m);
1525         assert(c);
1526         assert(contents);
1527
1528         if (!signature_is_valid(contents, false))
1529                 return -EINVAL;
1530
1531         if (c->signature && c->signature[c->index]) {
1532                 size_t l;
1533
1534                 l = strlen(contents);
1535
1536                 if (c->signature[c->index] != SD_BUS_TYPE_STRUCT_BEGIN ||
1537                     !startswith(c->signature + c->index + 1, contents) ||
1538                     c->signature[c->index + 1 + l] != SD_BUS_TYPE_STRUCT_END)
1539                         return -ENXIO;
1540
1541                 nindex = c->index + 1 + l + 1;
1542         } else {
1543                 char *e;
1544
1545                 if (c->enclosing != 0)
1546                         return -ENXIO;
1547
1548                 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_BEGIN), contents, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_END), NULL);
1549                 if (!e) {
1550                         m->poisoned = true;
1551                         return -ENOMEM;
1552                 }
1553
1554                 nindex = e - c->signature;
1555         }
1556
1557         /* Align contents to 8 byte boundary */
1558         if (!message_extend_body(m, 8, 0))
1559                 return -ENOMEM;
1560
1561         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1562                 c->index = nindex;
1563
1564         return 0;
1565 }
1566
1567 static int bus_message_open_dict_entry(
1568                 sd_bus_message *m,
1569                 struct bus_container *c,
1570                 const char *contents) {
1571
1572         size_t nindex;
1573
1574         assert(m);
1575         assert(c);
1576         assert(contents);
1577
1578         if (!signature_is_pair(contents))
1579                 return -EINVAL;
1580
1581         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1582                 return -ENXIO;
1583
1584         if (c->signature && c->signature[c->index]) {
1585                 size_t l;
1586
1587                 l = strlen(contents);
1588
1589                 if (c->signature[c->index] != SD_BUS_TYPE_DICT_ENTRY_BEGIN ||
1590                     !startswith(c->signature + c->index + 1, contents) ||
1591                     c->signature[c->index + 1 + l] != SD_BUS_TYPE_DICT_ENTRY_END)
1592                         return -ENXIO;
1593
1594                 nindex = c->index + 1 + l + 1;
1595         } else
1596                 return -ENXIO;
1597
1598         /* Align contents to 8 byte boundary */
1599         if (!message_extend_body(m, 8, 0))
1600                 return -ENOMEM;
1601
1602         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1603                 c->index = nindex;
1604
1605         return 0;
1606 }
1607
1608 _public_ int sd_bus_message_open_container(
1609                 sd_bus_message *m,
1610                 char type,
1611                 const char *contents) {
1612
1613         struct bus_container *c, *w;
1614         uint32_t *array_size = NULL;
1615         char *signature;
1616         size_t before;
1617         int r;
1618
1619         assert_return(m, -EINVAL);
1620         assert_return(!m->sealed, -EPERM);
1621         assert_return(contents, -EINVAL);
1622         assert_return(!m->poisoned, -ESTALE);
1623
1624         /* Make sure we have space for one more container */
1625         w = realloc(m->containers, sizeof(struct bus_container) * (m->n_containers + 1));
1626         if (!w) {
1627                 m->poisoned = true;
1628                 return -ENOMEM;
1629         }
1630
1631         m->containers = w;
1632
1633         c = message_get_container(m);
1634
1635         signature = strdup(contents);
1636         if (!signature) {
1637                 m->poisoned = true;
1638                 return -ENOMEM;
1639         }
1640
1641         /* Save old index in the parent container, in case we have to
1642          * abort this container */
1643         c->saved_index = c->index;
1644         before = m->header->body_size;
1645
1646         if (type == SD_BUS_TYPE_ARRAY)
1647                 r = bus_message_open_array(m, c, contents, &array_size);
1648         else if (type == SD_BUS_TYPE_VARIANT)
1649                 r = bus_message_open_variant(m, c, contents);
1650         else if (type == SD_BUS_TYPE_STRUCT)
1651                 r = bus_message_open_struct(m, c, contents);
1652         else if (type == SD_BUS_TYPE_DICT_ENTRY)
1653                 r = bus_message_open_dict_entry(m, c, contents);
1654         else
1655                 r = -EINVAL;
1656
1657         if (r < 0) {
1658                 free(signature);
1659                 return r;
1660         }
1661
1662         /* OK, let's fill it in */
1663         w += m->n_containers++;
1664         w->enclosing = type;
1665         w->signature = signature;
1666         w->index = 0;
1667         w->array_size = array_size;
1668         w->before = before;
1669         w->begin = m->rindex;
1670
1671         return 0;
1672 }
1673
1674 _public_ int sd_bus_message_close_container(sd_bus_message *m) {
1675         struct bus_container *c;
1676
1677         assert_return(m, -EINVAL);
1678         assert_return(!m->sealed, -EPERM);
1679         assert_return(m->n_containers > 0, -EINVAL);
1680         assert_return(!m->poisoned, -ESTALE);
1681
1682         c = message_get_container(m);
1683         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1684                 if (c->signature && c->signature[c->index] != 0)
1685                         return -EINVAL;
1686
1687         free(c->signature);
1688         m->n_containers--;
1689
1690         return 0;
1691 }
1692
1693 typedef struct {
1694         const char *types;
1695         unsigned n_struct;
1696         unsigned n_array;
1697 } TypeStack;
1698
1699 static int type_stack_push(TypeStack *stack, unsigned max, unsigned *i, const char *types, unsigned n_struct, unsigned n_array) {
1700         assert(stack);
1701         assert(max > 0);
1702
1703         if (*i >= max)
1704                 return -EINVAL;
1705
1706         stack[*i].types = types;
1707         stack[*i].n_struct = n_struct;
1708         stack[*i].n_array = n_array;
1709         (*i)++;
1710
1711         return 0;
1712 }
1713
1714 static int type_stack_pop(TypeStack *stack, unsigned max, unsigned *i, const char **types, unsigned *n_struct, unsigned *n_array) {
1715         assert(stack);
1716         assert(max > 0);
1717         assert(types);
1718         assert(n_struct);
1719         assert(n_array);
1720
1721         if (*i <= 0)
1722                 return 0;
1723
1724         (*i)--;
1725         *types = stack[*i].types;
1726         *n_struct = stack[*i].n_struct;
1727         *n_array = stack[*i].n_array;
1728
1729         return 1;
1730 }
1731
1732 int bus_message_append_ap(
1733                 sd_bus_message *m,
1734                 const char *types,
1735                 va_list ap) {
1736
1737         unsigned n_array, n_struct;
1738         TypeStack stack[BUS_CONTAINER_DEPTH];
1739         unsigned stack_ptr = 0;
1740         int r;
1741
1742         assert(m);
1743
1744         if (!types)
1745                 return 0;
1746
1747         n_array = (unsigned) -1;
1748         n_struct = strlen(types);
1749
1750         for (;;) {
1751                 const char *t;
1752
1753                 if (n_array == 0 || (n_array == (unsigned) -1 && n_struct == 0)) {
1754                         r = type_stack_pop(stack, ELEMENTSOF(stack), &stack_ptr, &types, &n_struct, &n_array);
1755                         if (r < 0)
1756                                 return r;
1757                         if (r == 0)
1758                                 break;
1759
1760                         r = sd_bus_message_close_container(m);
1761                         if (r < 0)
1762                                 return r;
1763
1764                         continue;
1765                 }
1766
1767                 t = types;
1768                 if (n_array != (unsigned) -1)
1769                         n_array --;
1770                 else {
1771                         types ++;
1772                         n_struct--;
1773                 }
1774
1775                 switch (*t) {
1776
1777                 case SD_BUS_TYPE_BYTE: {
1778                         uint8_t x;
1779
1780                         x = (uint8_t) va_arg(ap, int);
1781                         r = sd_bus_message_append_basic(m, *t, &x);
1782                         break;
1783                 }
1784
1785                 case SD_BUS_TYPE_BOOLEAN:
1786                 case SD_BUS_TYPE_INT32:
1787                 case SD_BUS_TYPE_UINT32:
1788                 case SD_BUS_TYPE_UNIX_FD: {
1789                         uint32_t x;
1790
1791                         /* We assume a boolean is the same as int32_t */
1792                         assert_cc(sizeof(int32_t) == sizeof(int));
1793
1794                         x = va_arg(ap, uint32_t);
1795                         r = sd_bus_message_append_basic(m, *t, &x);
1796                         break;
1797                 }
1798
1799                 case SD_BUS_TYPE_INT16:
1800                 case SD_BUS_TYPE_UINT16: {
1801                         uint16_t x;
1802
1803                         x = (uint16_t) va_arg(ap, int);
1804                         r = sd_bus_message_append_basic(m, *t, &x);
1805                         break;
1806                 }
1807
1808                 case SD_BUS_TYPE_INT64:
1809                 case SD_BUS_TYPE_UINT64:
1810                 case SD_BUS_TYPE_DOUBLE: {
1811                         uint64_t x;
1812
1813                         x = va_arg(ap, uint64_t);
1814                         r = sd_bus_message_append_basic(m, *t, &x);
1815                         break;
1816                 }
1817
1818                 case SD_BUS_TYPE_STRING:
1819                 case SD_BUS_TYPE_OBJECT_PATH:
1820                 case SD_BUS_TYPE_SIGNATURE: {
1821                         const char *x;
1822
1823                         x = va_arg(ap, const char*);
1824                         r = sd_bus_message_append_basic(m, *t, x);
1825                         break;
1826                 }
1827
1828                 case SD_BUS_TYPE_ARRAY: {
1829                         size_t k;
1830
1831                         r = signature_element_length(t + 1, &k);
1832                         if (r < 0)
1833                                 return r;
1834
1835                         {
1836                                 char s[k + 1];
1837                                 memcpy(s, t + 1, k);
1838                                 s[k] = 0;
1839
1840                                 r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, s);
1841                                 if (r < 0)
1842                                         return r;
1843                         }
1844
1845                         if (n_array == (unsigned) -1) {
1846                                 types += k;
1847                                 n_struct -= k;
1848                         }
1849
1850                         r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
1851                         if (r < 0)
1852                                 return r;
1853
1854                         types = t + 1;
1855                         n_struct = k;
1856                         n_array = va_arg(ap, unsigned);
1857
1858                         break;
1859                 }
1860
1861                 case SD_BUS_TYPE_VARIANT: {
1862                         const char *s;
1863
1864                         s = va_arg(ap, const char*);
1865                         if (!s)
1866                                 return -EINVAL;
1867
1868                         r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT, s);
1869                         if (r < 0)
1870                                 return r;
1871
1872                         r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
1873                         if (r < 0)
1874                                 return r;
1875
1876                         types = s;
1877                         n_struct = strlen(s);
1878                         n_array = (unsigned) -1;
1879
1880                         break;
1881                 }
1882
1883                 case SD_BUS_TYPE_STRUCT_BEGIN:
1884                 case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
1885                         size_t k;
1886
1887                         r = signature_element_length(t, &k);
1888                         if (r < 0)
1889                                 return r;
1890
1891                         {
1892                                 char s[k - 1];
1893
1894                                 memcpy(s, t + 1, k - 2);
1895                                 s[k - 2] = 0;
1896
1897                                 r = sd_bus_message_open_container(m, *t == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s);
1898                                 if (r < 0)
1899                                         return r;
1900                         }
1901
1902                         if (n_array == (unsigned) -1) {
1903                                 types += k - 1;
1904                                 n_struct -= k - 1;
1905                         }
1906
1907                         r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
1908                         if (r < 0)
1909                                 return r;
1910
1911                         types = t + 1;
1912                         n_struct = k - 2;
1913                         n_array = (unsigned) -1;
1914
1915                         break;
1916                 }
1917
1918                 default:
1919                         r = -EINVAL;
1920                 }
1921
1922                 if (r < 0)
1923                         return r;
1924         }
1925
1926         return 1;
1927 }
1928
1929 _public_ int sd_bus_message_append(sd_bus_message *m, const char *types, ...) {
1930         va_list ap;
1931         int r;
1932
1933         assert_return(m, -EINVAL);
1934         assert_return(types, -EINVAL);
1935         assert_return(!m->sealed, -EPERM);
1936         assert_return(!m->poisoned, -ESTALE);
1937
1938         va_start(ap, types);
1939         r = bus_message_append_ap(m, types, ap);
1940         va_end(ap);
1941
1942         return r;
1943 }
1944
1945 _public_ int sd_bus_message_append_array_space(sd_bus_message *m,
1946                                                char type,
1947                                                size_t size,
1948                                                void **ptr) {
1949         ssize_t align, sz;
1950         void *a;
1951         int r;
1952
1953         assert_return(m, -EINVAL);
1954         assert_return(!m->sealed, -EPERM);
1955         assert_return(bus_type_is_trivial(type), -EINVAL);
1956         assert_return(ptr || size == 0, -EINVAL);
1957         assert_return(!m->poisoned, -ESTALE);
1958
1959         align = bus_type_get_alignment(type);
1960         sz = bus_type_get_size(type);
1961
1962         assert_se(align > 0);
1963         assert_se(sz > 0);
1964
1965         if (size % sz != 0)
1966                 return -EINVAL;
1967
1968         r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, CHAR_TO_STR(type));
1969         if (r < 0)
1970                 return r;
1971
1972         a = message_extend_body(m, align, size);
1973         if (!a)
1974                 return -ENOMEM;
1975
1976         r = sd_bus_message_close_container(m);
1977         if (r < 0)
1978                 return r;
1979
1980         *ptr = a;
1981         return 0;
1982 }
1983
1984 _public_ int sd_bus_message_append_array(sd_bus_message *m,
1985                                          char type,
1986                                          const void *ptr,
1987                                          size_t size) {
1988         int r;
1989         void *p;
1990
1991         assert_return(m, -EINVAL);
1992         assert_return(!m->sealed, -EPERM);
1993         assert_return(bus_type_is_trivial(type), -EINVAL);
1994         assert_return(ptr || size == 0, -EINVAL);
1995         assert_return(!m->poisoned, -ESTALE);
1996
1997         r = sd_bus_message_append_array_space(m, type, size, &p);
1998         if (r < 0)
1999                 return r;
2000
2001         if (size > 0)
2002                 memcpy(p, ptr, size);
2003
2004         return 0;
2005 }
2006
2007 _public_ int sd_bus_message_append_array_iovec(
2008                 sd_bus_message *m,
2009                 char type,
2010                 const struct iovec *iov,
2011                 unsigned n) {
2012
2013         size_t size;
2014         unsigned i;
2015         void *p;
2016         int r;
2017
2018         assert_return(m, -EINVAL);
2019         assert_return(!m->sealed, -EPERM);
2020         assert_return(bus_type_is_trivial(type), -EINVAL);
2021         assert_return(iov || n == 0, -EINVAL);
2022         assert_return(!m->poisoned, -ESTALE);
2023
2024         size = IOVEC_TOTAL_SIZE(iov, n);
2025
2026         r = sd_bus_message_append_array_space(m, type, size, &p);
2027         if (r < 0)
2028                 return r;
2029
2030         for (i = 0; i < n; i++) {
2031
2032                 if (iov[i].iov_base)
2033                         memcpy(p, iov[i].iov_base, iov[i].iov_len);
2034                 else
2035                         memset(p, 0, iov[i].iov_len);
2036
2037                 p = (uint8_t*) p + iov[i].iov_len;
2038         }
2039
2040         return 0;
2041 }
2042
2043 _public_ int sd_bus_message_append_array_memfd(sd_bus_message *m,
2044                                                char type,
2045                                                sd_memfd *memfd) {
2046         _cleanup_close_ int copy_fd = -1;
2047         struct bus_body_part *part;
2048         ssize_t align, sz;
2049         uint64_t size;
2050         void *a;
2051         int r;
2052
2053         if (!m)
2054                 return -EINVAL;
2055         if (!memfd)
2056                 return -EINVAL;
2057         if (m->sealed)
2058                 return -EPERM;
2059         if (!bus_type_is_trivial(type))
2060                 return -EINVAL;
2061         if (m->poisoned)
2062                 return -ESTALE;
2063
2064         r = sd_memfd_set_sealed(memfd, true);
2065         if (r < 0)
2066                 return r;
2067
2068         copy_fd = sd_memfd_dup_fd(memfd);
2069         if (copy_fd < 0)
2070                 return copy_fd;
2071
2072         r = sd_memfd_get_size(memfd, &size);
2073         if (r < 0)
2074                 return r;
2075
2076         align = bus_type_get_alignment(type);
2077         sz = bus_type_get_size(type);
2078
2079         assert_se(align > 0);
2080         assert_se(sz > 0);
2081
2082         if (size % sz != 0)
2083                 return -EINVAL;
2084
2085         if (size > (uint64_t) (uint32_t) -1)
2086                 return -EINVAL;
2087
2088         r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, CHAR_TO_STR(type));
2089         if (r < 0)
2090                 return r;
2091
2092         a = message_extend_body(m, align, 0);
2093         if (!a)
2094                 return -ENOMEM;
2095
2096         part = message_append_part(m);
2097         if (!part)
2098                 return -ENOMEM;
2099
2100         part->memfd = copy_fd;
2101         part->sealed = true;
2102         part->size = size;
2103         copy_fd = -1;
2104
2105         message_extend_containers(m, size);
2106         m->header->body_size += size;
2107
2108         return sd_bus_message_close_container(m);
2109 }
2110
2111 _public_ int sd_bus_message_append_string_memfd(sd_bus_message *m, sd_memfd *memfd) {
2112         _cleanup_close_ int copy_fd = -1;
2113         struct bus_body_part *part;
2114         struct bus_container *c;
2115         uint64_t size;
2116         void *a;
2117         int r;
2118
2119         assert_return(m, -EINVAL);
2120         assert_return(memfd, -EINVAL);
2121         assert_return(!m->sealed, -EPERM);
2122         assert_return(!m->poisoned, -ESTALE);
2123
2124         r = sd_memfd_set_sealed(memfd, true);
2125         if (r < 0)
2126                 return r;
2127
2128         copy_fd = sd_memfd_dup_fd(memfd);
2129         if (copy_fd < 0)
2130                 return copy_fd;
2131
2132         r = sd_memfd_get_size(memfd, &size);
2133         if (r < 0)
2134                 return r;
2135
2136         /* We require this to be NUL terminated */
2137         if (size == 0)
2138                 return -EINVAL;
2139
2140         if (size > (uint64_t) (uint32_t) -1)
2141                 return -EINVAL;
2142
2143         c = message_get_container(m);
2144         if (c->signature && c->signature[c->index]) {
2145                 /* Container signature is already set */
2146
2147                 if (c->signature[c->index] != SD_BUS_TYPE_STRING)
2148                         return -ENXIO;
2149         } else {
2150                 char *e;
2151
2152                 /* Maybe we can append to the signature? But only if this is the top-level container*/
2153                 if (c->enclosing != 0)
2154                         return -ENXIO;
2155
2156                 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRING), NULL);
2157                 if (!e) {
2158                         m->poisoned = true;
2159                         return -ENOMEM;
2160                 }
2161         }
2162
2163         a = message_extend_body(m, 4, 4);
2164         if (!a)
2165                 return -ENOMEM;
2166
2167         *(uint32_t*) a = size - 1;
2168
2169         part = message_append_part(m);
2170         if (!part)
2171                 return -ENOMEM;
2172
2173         part->memfd = copy_fd;
2174         part->sealed = true;
2175         part->size = size;
2176         copy_fd = -1;
2177
2178         message_extend_containers(m, size);
2179         m->header->body_size += size;
2180
2181         if (c->enclosing != SD_BUS_TYPE_ARRAY)
2182                 c->index++;
2183
2184         return 0;
2185 }
2186
2187 _public_ int sd_bus_message_append_strv(sd_bus_message *m, char **l) {
2188         char **i;
2189         int r;
2190
2191         assert_return(m, -EINVAL);
2192         assert_return(!m->sealed, -EPERM);
2193         assert_return(!m->poisoned, -ESTALE);
2194
2195         r = sd_bus_message_open_container(m, 'a', "s");
2196         if (r < 0)
2197                 return r;
2198
2199         STRV_FOREACH(i, l) {
2200                 r = sd_bus_message_append_basic(m, 's', *i);
2201                 if (r < 0)
2202                         return r;
2203         }
2204
2205         return sd_bus_message_close_container(m);
2206 }
2207
2208 int bus_body_part_map(struct bus_body_part *part) {
2209         void *p;
2210         size_t psz;
2211
2212         assert_se(part);
2213
2214         if (part->data)
2215                 return 0;
2216
2217         if (part->size <= 0)
2218                 return 0;
2219
2220         /* For smaller zero parts (as used for padding) we don't need to map anything... */
2221         if (part->memfd < 0 && part->is_zero && part->size < 8) {
2222                 static const uint8_t zeroes[7] = { };
2223                 part->data = (void*) zeroes;
2224                 return 0;
2225         }
2226
2227         psz = PAGE_ALIGN(part->size);
2228
2229         if (part->memfd >= 0)
2230                 p = mmap(NULL, psz, PROT_READ, MAP_SHARED, part->memfd, 0);
2231         else if (part->is_zero)
2232                 p = mmap(NULL, psz, PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
2233         else
2234                 return -EINVAL;
2235
2236         if (p == MAP_FAILED)
2237                 return -errno;
2238
2239         part->mapped = psz;
2240         part->data = p;
2241         part->munmap_this = true;
2242
2243         return 0;
2244 }
2245
2246 void bus_body_part_unmap(struct bus_body_part *part) {
2247
2248         assert_se(part);
2249
2250         if (part->memfd < 0)
2251                 return;
2252
2253         if (!part->data)
2254                 return;
2255
2256         if (!part->munmap_this)
2257                 return;
2258
2259         assert_se(munmap(part->data, part->mapped) == 0);
2260
2261         part->data = NULL;
2262         part->mapped = 0;
2263         part->munmap_this = false;
2264
2265         return;
2266 }
2267
2268 static int buffer_peek(const void *p, uint32_t sz, size_t *rindex, size_t align, size_t nbytes, void **r) {
2269         size_t k, start, end;
2270
2271         assert(rindex);
2272         assert(align > 0);
2273
2274         start = ALIGN_TO((size_t) *rindex, align);
2275         end = start + nbytes;
2276
2277         if (end > sz)
2278                 return -EBADMSG;
2279
2280         /* Verify that padding is 0 */
2281         for (k = *rindex; k < start; k++)
2282                 if (((const uint8_t*) p)[k] != 0)
2283                         return -EBADMSG;
2284
2285         if (r)
2286                 *r = (uint8_t*) p + start;
2287
2288         *rindex = end;
2289
2290         return 1;
2291 }
2292
2293 static bool message_end_of_signature(sd_bus_message *m) {
2294         struct bus_container *c;
2295
2296         assert(m);
2297
2298         c = message_get_container(m);
2299         return !c->signature || c->signature[c->index] == 0;
2300 }
2301
2302 static bool message_end_of_array(sd_bus_message *m, size_t index) {
2303         struct bus_container *c;
2304
2305         assert(m);
2306
2307         c = message_get_container(m);
2308         if (!c->array_size)
2309                 return false;
2310
2311         return index >= c->begin + BUS_MESSAGE_BSWAP32(m, *c->array_size);
2312 }
2313
2314 _public_ int sd_bus_message_at_end(sd_bus_message *m, int complete) {
2315         assert_return(m, -EINVAL);
2316         assert_return(m->sealed, -EPERM);
2317
2318         if (complete && m->n_containers > 0)
2319                 return false;
2320
2321         if (message_end_of_signature(m))
2322                 return true;
2323
2324         if (message_end_of_array(m, m->rindex))
2325                 return true;
2326
2327         return false;
2328 }
2329
2330 static struct bus_body_part* find_part(sd_bus_message *m, size_t index, size_t sz, void **p) {
2331         struct bus_body_part *part;
2332         size_t begin;
2333         int r;
2334
2335         assert(m);
2336
2337         if (m->cached_rindex_part && index >= m->cached_rindex_part_begin) {
2338                 part = m->cached_rindex_part;
2339                 begin = m->cached_rindex_part_begin;
2340         } else {
2341                 part = &m->body;
2342                 begin = 0;
2343         }
2344
2345         while (part) {
2346                 if (index < begin)
2347                         return NULL;
2348
2349                 if (index + sz <= begin + part->size) {
2350
2351                         r = bus_body_part_map(part);
2352                         if (r < 0)
2353                                 return NULL;
2354
2355                         if (p)
2356                                 *p = (uint8_t*) part->data + index - begin;
2357
2358                         m->cached_rindex_part = part;
2359                         m->cached_rindex_part_begin = begin;
2360
2361                         return part;
2362                 }
2363
2364                 begin += part->size;
2365                 part = part->next;
2366         }
2367
2368         return NULL;
2369 }
2370
2371 static int message_peek_body(
2372                 sd_bus_message *m,
2373                 size_t *rindex,
2374                 size_t align,
2375                 size_t nbytes,
2376                 void **ret) {
2377
2378         size_t k, start, end, padding;
2379         struct bus_body_part *part;
2380         uint8_t *q;
2381
2382         assert(m);
2383         assert(rindex);
2384         assert(align > 0);
2385
2386         if (message_end_of_array(m, *rindex))
2387                 return 0;
2388
2389         start = ALIGN_TO((size_t) *rindex, align);
2390         padding = start - *rindex;
2391         end = start + nbytes;
2392
2393         if (end > BUS_MESSAGE_BODY_SIZE(m))
2394                 return -EBADMSG;
2395
2396         part = find_part(m, *rindex, padding, (void**) &q);
2397         if (!part)
2398                 return -EBADMSG;
2399
2400         if (q) {
2401                 /* Verify padding */
2402                 for (k = 0; k < padding; k++)
2403                         if (q[k] != 0)
2404                                 return -EBADMSG;
2405         }
2406
2407         part = find_part(m, start, nbytes, (void**) &q);
2408         if (!part || !q)
2409                 return -EBADMSG;
2410
2411         *rindex = end;
2412
2413         if (ret)
2414                 *ret = q;
2415
2416         return 1;
2417 }
2418
2419 static bool validate_nul(const char *s, size_t l) {
2420
2421         /* Check for NUL chars in the string */
2422         if (memchr(s, 0, l))
2423                 return false;
2424
2425         /* Check for NUL termination */
2426         if (s[l] != 0)
2427                 return false;
2428
2429         return true;
2430 }
2431
2432 static bool validate_string(const char *s, size_t l) {
2433
2434         if (!validate_nul(s, l))
2435                 return false;
2436
2437         /* Check if valid UTF8 */
2438         if (!utf8_is_valid(s))
2439                 return false;
2440
2441         return true;
2442 }
2443
2444 static bool validate_signature(const char *s, size_t l) {
2445
2446         if (!validate_nul(s, l))
2447                 return false;
2448
2449         /* Check if valid signature */
2450         if (!signature_is_valid(s, true))
2451                 return false;
2452
2453         return true;
2454 }
2455
2456 static bool validate_object_path(const char *s, size_t l) {
2457
2458         if (!validate_nul(s, l))
2459                 return false;
2460
2461         if (!object_path_is_valid(s))
2462                 return false;
2463
2464         return true;
2465 }
2466
2467 _public_ int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p) {
2468         struct bus_container *c;
2469         void *q;
2470         int r;
2471
2472         assert_return(m, -EINVAL);
2473         assert_return(m->sealed, -EPERM);
2474         assert_return(bus_type_is_basic(type), -EINVAL);
2475
2476         if (message_end_of_signature(m))
2477                 return -ENXIO;
2478
2479         if (message_end_of_array(m, m->rindex))
2480                 return 0;
2481
2482         c = message_get_container(m);
2483         if (c->signature[c->index] != type)
2484                 return -ENXIO;
2485
2486         switch (type) {
2487
2488         case SD_BUS_TYPE_STRING:
2489         case SD_BUS_TYPE_OBJECT_PATH: {
2490                 uint32_t l;
2491                 size_t rindex;
2492
2493                 rindex = m->rindex;
2494                 r = message_peek_body(m, &rindex, 4, 4, &q);
2495                 if (r <= 0)
2496                         return r;
2497
2498                 l = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
2499                 r = message_peek_body(m, &rindex, 1, l+1, &q);
2500                 if (r < 0)
2501                         return r;
2502                 if (r == 0)
2503                         return -EBADMSG;
2504
2505                 if (type == SD_BUS_TYPE_OBJECT_PATH) {
2506                         if (!validate_object_path(q, l))
2507                                 return -EBADMSG;
2508                 } else {
2509                         if (!validate_string(q, l))
2510                                 return -EBADMSG;
2511                 }
2512
2513                 m->rindex = rindex;
2514                 if (p)
2515                         *(const char**) p = q;
2516
2517                 break;
2518         }
2519
2520         case SD_BUS_TYPE_SIGNATURE: {
2521                 uint8_t l;
2522                 size_t rindex;
2523
2524                 rindex = m->rindex;
2525                 r = message_peek_body(m, &rindex, 1, 1, &q);
2526                 if (r <= 0)
2527                         return r;
2528
2529                 l = *(uint8_t*) q;
2530                 r = message_peek_body(m, &rindex, 1, l+1, &q);
2531                 if (r < 0)
2532                         return r;
2533                 if (r == 0)
2534                         return -EBADMSG;
2535
2536                 if (!validate_signature(q, l))
2537                         return -EBADMSG;
2538
2539                 m->rindex = rindex;
2540
2541                 if (p)
2542                         *(const char**) p = q;
2543                 break;
2544         }
2545
2546         default: {
2547                 ssize_t sz, align;
2548                 size_t rindex;
2549
2550                 align = bus_type_get_alignment(type);
2551                 sz = bus_type_get_size(type);
2552                 assert(align > 0 && sz > 0);
2553
2554                 rindex = m->rindex;
2555                 r = message_peek_body(m, &rindex, align, sz, &q);
2556                 if (r <= 0)
2557                         return r;
2558
2559                 switch (type) {
2560
2561                 case SD_BUS_TYPE_BYTE:
2562                         if (p)
2563                                 *(uint8_t*) p = *(uint8_t*) q;
2564                         break;
2565
2566                 case SD_BUS_TYPE_BOOLEAN:
2567                         if (p)
2568                                 *(int*) p = !!*(uint32_t*) q;
2569                         break;
2570
2571                 case SD_BUS_TYPE_INT16:
2572                 case SD_BUS_TYPE_UINT16:
2573                         if (p)
2574                                 *(uint16_t*) p = BUS_MESSAGE_BSWAP16(m, *(uint16_t*) q);
2575                         break;
2576
2577                 case SD_BUS_TYPE_INT32:
2578                 case SD_BUS_TYPE_UINT32:
2579                         if (p)
2580                                 *(uint32_t*) p = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
2581                         break;
2582
2583                 case SD_BUS_TYPE_INT64:
2584                 case SD_BUS_TYPE_UINT64:
2585                 case SD_BUS_TYPE_DOUBLE:
2586                         if (p)
2587                                 *(uint64_t*) p = BUS_MESSAGE_BSWAP64(m, *(uint64_t*) q);
2588                         break;
2589
2590                 case SD_BUS_TYPE_UNIX_FD: {
2591                         uint32_t j;
2592
2593                         j = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
2594                         if (j >= m->n_fds)
2595                                 return -EBADMSG;
2596
2597                         if (p)
2598                                 *(int*) p = m->fds[j];
2599                         break;
2600                 }
2601
2602                 default:
2603                         assert_not_reached("Unknown basic type...");
2604                 }
2605
2606                 m->rindex = rindex;
2607
2608                 break;
2609         }
2610         }
2611
2612         if (c->enclosing != SD_BUS_TYPE_ARRAY)
2613                 c->index++;
2614
2615         return 1;
2616 }
2617
2618 static int bus_message_enter_array(
2619                 sd_bus_message *m,
2620                 struct bus_container *c,
2621                 const char *contents,
2622                 uint32_t **array_size) {
2623
2624         size_t rindex;
2625         void *q;
2626         int r, alignment;
2627
2628         assert(m);
2629         assert(c);
2630         assert(contents);
2631         assert(array_size);
2632
2633         if (!signature_is_single(contents, true))
2634                 return -EINVAL;
2635
2636         alignment = bus_type_get_alignment(contents[0]);
2637         if (alignment < 0)
2638                 return alignment;
2639
2640         if (!c->signature || c->signature[c->index] == 0)
2641                 return 0;
2642
2643         if (c->signature[c->index] != SD_BUS_TYPE_ARRAY)
2644                 return -ENXIO;
2645
2646         if (!startswith(c->signature + c->index + 1, contents))
2647                 return -ENXIO;
2648
2649         rindex = m->rindex;
2650         r = message_peek_body(m, &rindex, 4, 4, &q);
2651         if (r <= 0)
2652                 return r;
2653
2654         if (BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q) > BUS_ARRAY_MAX_SIZE)
2655                 return -EBADMSG;
2656
2657         r = message_peek_body(m, &rindex, alignment, 0, NULL);
2658         if (r < 0)
2659                 return r;
2660         if (r == 0)
2661                 return -EBADMSG;
2662
2663         if (c->enclosing != SD_BUS_TYPE_ARRAY)
2664                 c->index += 1 + strlen(contents);
2665
2666         m->rindex = rindex;
2667
2668         *array_size = (uint32_t*) q;
2669
2670         return 1;
2671 }
2672
2673 static int bus_message_enter_variant(
2674                 sd_bus_message *m,
2675                 struct bus_container *c,
2676                 const char *contents) {
2677
2678         size_t rindex;
2679         uint8_t l;
2680         void *q;
2681         int r;
2682
2683         assert(m);
2684         assert(c);
2685         assert(contents);
2686
2687         if (!signature_is_single(contents, false))
2688                 return -EINVAL;
2689
2690         if (*contents == SD_BUS_TYPE_DICT_ENTRY_BEGIN)
2691                 return -EINVAL;
2692
2693         if (!c->signature || c->signature[c->index] == 0)
2694                 return 0;
2695
2696         if (c->signature[c->index] != SD_BUS_TYPE_VARIANT)
2697                 return -ENXIO;
2698
2699         rindex = m->rindex;
2700         r = message_peek_body(m, &rindex, 1, 1, &q);
2701         if (r <= 0)
2702                 return r;
2703
2704         l = *(uint8_t*) q;
2705         r = message_peek_body(m, &rindex, 1, l+1, &q);
2706         if (r < 0)
2707                 return r;
2708         if (r == 0)
2709                 return -EBADMSG;
2710
2711         if (!validate_signature(q, l))
2712                 return -EBADMSG;
2713
2714         if (!streq(q, contents))
2715                 return -ENXIO;
2716
2717         if (c->enclosing != SD_BUS_TYPE_ARRAY)
2718                 c->index++;
2719
2720         m->rindex = rindex;
2721
2722         return 1;
2723 }
2724
2725 static int bus_message_enter_struct(
2726                 sd_bus_message *m,
2727                 struct bus_container *c,
2728                 const char *contents) {
2729
2730         size_t l;
2731         int r;
2732
2733         assert(m);
2734         assert(c);
2735         assert(contents);
2736
2737         if (!signature_is_valid(contents, false))
2738                 return -EINVAL;
2739
2740         if (!c->signature || c->signature[c->index] == 0)
2741                 return 0;
2742
2743         l = strlen(contents);
2744
2745         if (c->signature[c->index] != SD_BUS_TYPE_STRUCT_BEGIN ||
2746             !startswith(c->signature + c->index + 1, contents) ||
2747             c->signature[c->index + 1 + l] != SD_BUS_TYPE_STRUCT_END)
2748                 return -ENXIO;
2749
2750         r = message_peek_body(m, &m->rindex, 8, 0, NULL);
2751         if (r <= 0)
2752                 return r;
2753
2754         if (c->enclosing != SD_BUS_TYPE_ARRAY)
2755                 c->index += 1 + l + 1;
2756
2757         return 1;
2758 }
2759
2760 static int bus_message_enter_dict_entry(
2761                 sd_bus_message *m,
2762                 struct bus_container *c,
2763                 const char *contents) {
2764
2765         size_t l;
2766         int r;
2767
2768         assert(m);
2769         assert(c);
2770         assert(contents);
2771
2772         if (!signature_is_pair(contents))
2773                 return -EINVAL;
2774
2775         if (c->enclosing != SD_BUS_TYPE_ARRAY)
2776                 return -ENXIO;
2777
2778         if (!c->signature || c->signature[c->index] == 0)
2779                 return 0;
2780
2781         l = strlen(contents);
2782
2783         if (c->signature[c->index] != SD_BUS_TYPE_DICT_ENTRY_BEGIN ||
2784             !startswith(c->signature + c->index + 1, contents) ||
2785             c->signature[c->index + 1 + l] != SD_BUS_TYPE_DICT_ENTRY_END)
2786                 return -ENXIO;
2787
2788         r = message_peek_body(m, &m->rindex, 8, 0, NULL);
2789         if (r <= 0)
2790                 return r;
2791
2792         if (c->enclosing != SD_BUS_TYPE_ARRAY)
2793                 c->index += 1 + l + 1;
2794
2795         return 1;
2796 }
2797
2798 _public_ int sd_bus_message_enter_container(sd_bus_message *m,
2799                                             char type,
2800                                             const char *contents) {
2801         struct bus_container *c, *w;
2802         uint32_t *array_size = NULL;
2803         char *signature;
2804         size_t before;
2805         int r;
2806
2807         assert_return(m, -EINVAL);
2808         assert_return(m->sealed, -EPERM);
2809         assert_return(type != 0 || !contents, -EINVAL);
2810
2811         if (type == 0 || !contents) {
2812                 const char *cc;
2813                 char tt;
2814
2815                 /* Allow entering into anonymous containers */
2816                 r = sd_bus_message_peek_type(m, &tt, &cc);
2817                 if (r <= 0)
2818                         return r;
2819
2820                 if (type != 0 && type != tt)
2821                         return -ENXIO;
2822
2823                 if (contents && !streq(contents, cc))
2824                         return -ENXIO;
2825
2826                 type = tt;
2827                 contents = cc;
2828         }
2829
2830         /*
2831          * We enforce a global limit on container depth, that is much
2832          * higher than the 32 structs and 32 arrays the specification
2833          * mandates. This is simpler to implement for us, and we need
2834          * this only to ensure our container array doesn't grow
2835          * without bounds. We are happy to return any data from a
2836          * message as long as the data itself is valid, even if the
2837          * overall message might be not.
2838          *
2839          * Note that the message signature is validated when
2840          * parsing the headers, and that validation does check the
2841          * 32/32 limit.
2842          *
2843          * Note that the specification defines no limits on the depth
2844          * of stacked variants, but we do.
2845          */
2846         if (m->n_containers >= BUS_CONTAINER_DEPTH)
2847                 return -EBADMSG;
2848
2849         w = realloc(m->containers, sizeof(struct bus_container) * (m->n_containers + 1));
2850         if (!w)
2851                 return -ENOMEM;
2852         m->containers = w;
2853
2854         if (message_end_of_signature(m))
2855                 return -ENXIO;
2856
2857         if (message_end_of_array(m, m->rindex))
2858                 return 0;
2859
2860         c = message_get_container(m);
2861
2862         signature = strdup(contents);
2863         if (!signature)
2864                 return -ENOMEM;
2865
2866         c->saved_index = c->index;
2867         before = m->rindex;
2868
2869         if (type == SD_BUS_TYPE_ARRAY)
2870                 r = bus_message_enter_array(m, c, contents, &array_size);
2871         else if (type == SD_BUS_TYPE_VARIANT)
2872                 r = bus_message_enter_variant(m, c, contents);
2873         else if (type == SD_BUS_TYPE_STRUCT)
2874                 r = bus_message_enter_struct(m, c, contents);
2875         else if (type == SD_BUS_TYPE_DICT_ENTRY)
2876                 r = bus_message_enter_dict_entry(m, c, contents);
2877         else
2878                 r = -EINVAL;
2879
2880         if (r <= 0) {
2881                 free(signature);
2882                 return r;
2883         }
2884
2885         /* OK, let's fill it in */
2886         w += m->n_containers++;
2887         w->enclosing = type;
2888         w->signature = signature;
2889         w->index = 0;
2890         w->array_size = array_size;
2891         w->before = before;
2892         w->begin = m->rindex;
2893
2894         return 1;
2895 }
2896
2897 _public_ int sd_bus_message_exit_container(sd_bus_message *m) {
2898         struct bus_container *c;
2899
2900         assert_return(m, -EINVAL);
2901         assert_return(m->sealed, -EPERM);
2902         assert_return(m->n_containers > 0, -ENXIO);
2903
2904         c = message_get_container(m);
2905         if (c->enclosing == SD_BUS_TYPE_ARRAY) {
2906                 uint32_t l;
2907
2908                 l = BUS_MESSAGE_BSWAP32(m, *c->array_size);
2909                 if (c->begin + l != m->rindex)
2910                         return -EBUSY;
2911
2912         } else {
2913                 if (c->signature && c->signature[c->index] != 0)
2914                         return -EBUSY;
2915         }
2916
2917         free(c->signature);
2918         m->n_containers--;
2919
2920         return 1;
2921 }
2922
2923 static void message_quit_container(sd_bus_message *m) {
2924         struct bus_container *c;
2925
2926         assert(m);
2927         assert(m->sealed);
2928         assert(m->n_containers > 0);
2929
2930         c = message_get_container(m);
2931
2932         /* Undo seeks */
2933         assert(m->rindex >= c->before);
2934         m->rindex = c->before;
2935
2936         /* Free container */
2937         free(c->signature);
2938         m->n_containers--;
2939
2940         /* Correct index of new top-level container */
2941         c = message_get_container(m);
2942         c->index = c->saved_index;
2943 }
2944
2945 _public_ int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char **contents) {
2946         struct bus_container *c;
2947         int r;
2948
2949         assert_return(m, -EINVAL);
2950         assert_return(m->sealed, -EPERM);
2951
2952         if (message_end_of_signature(m))
2953                 goto eof;
2954
2955         if (message_end_of_array(m, m->rindex))
2956                 goto eof;
2957
2958         c = message_get_container(m);
2959
2960         if (bus_type_is_basic(c->signature[c->index])) {
2961                 if (contents)
2962                         *contents = NULL;
2963                 if (type)
2964                         *type = c->signature[c->index];
2965                 return 1;
2966         }
2967
2968         if (c->signature[c->index] == SD_BUS_TYPE_ARRAY) {
2969
2970                 if (contents) {
2971                         size_t l;
2972                         char *sig;
2973
2974                         r = signature_element_length(c->signature+c->index+1, &l);
2975                         if (r < 0)
2976                                 return r;
2977
2978                         assert(l >= 1);
2979
2980                         sig = strndup(c->signature + c->index + 1, l);
2981                         if (!sig)
2982                                 return -ENOMEM;
2983
2984                         free(m->peeked_signature);
2985                         m->peeked_signature = sig;
2986
2987                         *contents = sig;
2988                 }
2989
2990                 if (type)
2991                         *type = SD_BUS_TYPE_ARRAY;
2992
2993                 return 1;
2994         }
2995
2996         if (c->signature[c->index] == SD_BUS_TYPE_STRUCT_BEGIN ||
2997             c->signature[c->index] == SD_BUS_TYPE_DICT_ENTRY_BEGIN) {
2998
2999                 if (contents) {
3000                         size_t l;
3001                         char *sig;
3002
3003                         r = signature_element_length(c->signature+c->index, &l);
3004                         if (r < 0)
3005                                 return r;
3006
3007                         assert(l >= 2);
3008                         sig = strndup(c->signature + c->index + 1, l - 2);
3009                         if (!sig)
3010                                 return -ENOMEM;
3011
3012                         free(m->peeked_signature);
3013                         m->peeked_signature = sig;
3014
3015                         *contents = sig;
3016                 }
3017
3018                 if (type)
3019                         *type = c->signature[c->index] == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY;
3020
3021                 return 1;
3022         }
3023
3024         if (c->signature[c->index] == SD_BUS_TYPE_VARIANT) {
3025                 if (contents) {
3026                         size_t rindex, l;
3027                         void *q;
3028
3029                         rindex = m->rindex;
3030                         r = message_peek_body(m, &rindex, 1, 1, &q);
3031                         if (r < 0)
3032                                 return r;
3033                         if (r == 0)
3034                                 goto eof;
3035
3036                         l = *(uint8_t*) q;
3037                         r = message_peek_body(m, &rindex, 1, l+1, &q);
3038                         if (r < 0)
3039                                 return r;
3040                         if (r == 0)
3041                                 return -EBADMSG;
3042
3043                         if (!validate_signature(q, l))
3044                                 return -EBADMSG;
3045
3046                         *contents = q;
3047                 }
3048
3049                 if (type)
3050                         *type = SD_BUS_TYPE_VARIANT;
3051
3052                 return 1;
3053         }
3054
3055         return -EINVAL;
3056
3057 eof:
3058         if (type)
3059                 *type = 0;
3060         if (contents)
3061                 *contents = NULL;
3062         return 0;
3063 }
3064
3065 _public_ int sd_bus_message_rewind(sd_bus_message *m, int complete) {
3066         struct bus_container *c;
3067
3068         assert_return(m, -EINVAL);
3069         assert_return(m->sealed, -EPERM);
3070
3071         if (complete) {
3072                 message_reset_containers(m);
3073                 m->rindex = 0;
3074                 m->root_container.index = 0;
3075
3076                 c = message_get_container(m);
3077         } else {
3078                 c = message_get_container(m);
3079
3080                 c->index = 0;
3081                 m->rindex = c->begin;
3082         }
3083
3084         return !isempty(c->signature);
3085 }
3086
3087 static int message_read_ap(
3088                 sd_bus_message *m,
3089                 const char *types,
3090                 va_list ap) {
3091
3092         unsigned n_array, n_struct;
3093         TypeStack stack[BUS_CONTAINER_DEPTH];
3094         unsigned stack_ptr = 0;
3095         unsigned n_loop = 0;
3096         int r;
3097
3098         assert(m);
3099
3100         if (isempty(types))
3101                 return 0;
3102
3103         /* Ideally, we'd just call ourselves recursively on every
3104          * complex type. However, the state of a va_list that is
3105          * passed to a function is undefined after that function
3106          * returns. This means we need to docode the va_list linearly
3107          * in a single stackframe. We hence implement our own
3108          * home-grown stack in an array. */
3109
3110         n_array = (unsigned) -1; /* lenght of current array entries */
3111         n_struct = strlen(types); /* length of current struct contents signature */
3112
3113         for (;;) {
3114                 const char *t;
3115
3116                 n_loop++;
3117
3118                 if (n_array == 0 || (n_array == (unsigned) -1 && n_struct == 0)) {
3119                         r = type_stack_pop(stack, ELEMENTSOF(stack), &stack_ptr, &types, &n_struct, &n_array);
3120                         if (r < 0)
3121                                 return r;
3122                         if (r == 0)
3123                                 break;
3124
3125                         r = sd_bus_message_exit_container(m);
3126                         if (r < 0)
3127                                 return r;
3128
3129                         continue;
3130                 }
3131
3132                 t = types;
3133                 if (n_array != (unsigned) -1)
3134                         n_array --;
3135                 else {
3136                         types ++;
3137                         n_struct--;
3138                 }
3139
3140                 switch (*t) {
3141
3142                 case SD_BUS_TYPE_BYTE:
3143                 case SD_BUS_TYPE_BOOLEAN:
3144                 case SD_BUS_TYPE_INT16:
3145                 case SD_BUS_TYPE_UINT16:
3146                 case SD_BUS_TYPE_INT32:
3147                 case SD_BUS_TYPE_UINT32:
3148                 case SD_BUS_TYPE_INT64:
3149                 case SD_BUS_TYPE_UINT64:
3150                 case SD_BUS_TYPE_DOUBLE:
3151                 case SD_BUS_TYPE_STRING:
3152                 case SD_BUS_TYPE_OBJECT_PATH:
3153                 case SD_BUS_TYPE_SIGNATURE:
3154                 case SD_BUS_TYPE_UNIX_FD: {
3155                         void *p;
3156
3157                         p = va_arg(ap, void*);
3158                         r = sd_bus_message_read_basic(m, *t, p);
3159                         if (r < 0)
3160                                 return r;
3161                         if (r == 0) {
3162                                 if (n_loop <= 1)
3163                                         return 0;
3164
3165                                 return -ENXIO;
3166                         }
3167
3168                         break;
3169                 }
3170
3171                 case SD_BUS_TYPE_ARRAY: {
3172                         size_t k;
3173
3174                         r = signature_element_length(t + 1, &k);
3175                         if (r < 0)
3176                                 return r;
3177
3178                         {
3179                                 char s[k + 1];
3180                                 memcpy(s, t + 1, k);
3181                                 s[k] = 0;
3182
3183                                 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, s);
3184                                 if (r < 0)
3185                                         return r;
3186                                 if (r == 0) {
3187                                         if (n_loop <= 1)
3188                                                 return 0;
3189
3190                                         return -ENXIO;
3191                                 }
3192                         }
3193
3194                         if (n_array == (unsigned) -1) {
3195                                 types += k;
3196                                 n_struct -= k;
3197                         }
3198
3199                         r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
3200                         if (r < 0)
3201                                 return r;
3202
3203                         types = t + 1;
3204                         n_struct = k;
3205                         n_array = va_arg(ap, unsigned);
3206
3207                         break;
3208                 }
3209
3210                 case SD_BUS_TYPE_VARIANT: {
3211                         const char *s;
3212
3213                         s = va_arg(ap, const char *);
3214                         if (!s)
3215                                 return -EINVAL;
3216
3217                         r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, s);
3218                         if (r < 0)
3219                                 return r;
3220                         if (r == 0) {
3221                                 if (n_loop <= 1)
3222                                         return 0;
3223
3224                                 return -ENXIO;
3225                         }
3226
3227                         r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
3228                         if (r < 0)
3229                                 return r;
3230
3231                         types = s;
3232                         n_struct = strlen(s);
3233                         n_array = (unsigned) -1;
3234
3235                         break;
3236                 }
3237
3238                 case SD_BUS_TYPE_STRUCT_BEGIN:
3239                 case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
3240                         size_t k;
3241
3242                         r = signature_element_length(t, &k);
3243                         if (r < 0)
3244                                 return r;
3245
3246                         {
3247                                 char s[k - 1];
3248                                 memcpy(s, t + 1, k - 2);
3249                                 s[k - 2] = 0;
3250
3251                                 r = sd_bus_message_enter_container(m, *t == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s);
3252                                 if (r < 0)