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