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