bus-message.c 95.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/

/***
  This file is part of systemd.

  Copyright 2013 Lennart Poettering

  systemd is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 2.1 of the License, or
  (at your option) any later version.

  systemd is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

#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 34 35 36
#include "bus-internal.h"
#include "bus-type.h"
#include "bus-signature.h"

37
static int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored);
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
static void reset_containers(sd_bus_message *m) {
        unsigned i;

        assert(m);

        for (i = 0; i < m->n_containers; i++)
                free(m->containers[i].signature);

        free(m->containers);
        m->containers = NULL;

        m->n_containers = 0;
        m->root_container.index = 0;
}

54 55 56 57 58 59 60 61 62 63 64 65
static void message_free(sd_bus_message *m) {
        assert(m);

        if (m->free_header)
                free(m->header);

        if (m->free_fields)
                free(m->fields);

        if (m->free_body)
                free(m->body);

66 67 68
        if (m->free_kdbus)
                free(m->kdbus);

69 70 71 72
        if (m->free_fds) {
                close_many(m->fds, m->n_fds);
                free(m->fds);
        }
73

74 75
        free(m->cmdline_array);

76 77
        reset_containers(m);
        free(m->root_container.signature);
78

79
        free(m->peeked_signature);
80 81 82 83

        free(m->unit);
        free(m->user_unit);
        free(m->session);
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
        free(m);
}

static void* buffer_extend(void **p, uint32_t *sz, size_t align, size_t extend) {
        size_t start, n;
        void *k;

        assert(p);
        assert(sz);
        assert(align > 0);

        start = ALIGN_TO((size_t) *sz, align);
        n = start + extend;

        if (n == *sz)
                return (uint8_t*) *p + start;

        if (n > (size_t) ((uint32_t) -1))
                return NULL;

        k = realloc(*p, n);
        if (!k)
                return NULL;

        /* Zero out padding */
        if (start > *sz)
                memset((uint8_t*) k + *sz, 0, start - *sz);

        *p = k;
        *sz = n;

        return (uint8_t*) k + start;
}

static void *message_extend_fields(sd_bus_message *m, size_t align, size_t sz) {
        void *p, *o;

        assert(m);

        o = m->fields;
        p = buffer_extend(&m->fields, &m->header->fields_size, align, sz);
        if (!p)
                return NULL;

        if (o != m->fields) {
                /* Adjust quick access pointers */

                if (m->path)
                        m->path = (const char*) m->fields + (m->path - (const char*) o);
                if (m->interface)
                        m->interface = (const char*) m->fields + (m->interface - (const char*) o);
                if (m->member)
                        m->member = (const char*) m->fields + (m->member - (const char*) o);
                if (m->destination)
                        m->destination = (const char*) m->fields + (m->destination - (const char*) o);
                if (m->sender)
                        m->sender = (const char*) m->fields + (m->sender - (const char*) o);
                if (m->error.name)
                        m->error.name = (const char*) m->fields + (m->error.name - (const char*) o);
        }

        m->free_fields = true;

        return p;
}

static int message_append_field_string(
                sd_bus_message *m,
                uint8_t h,
                char type,
                const char *s,
                const char **ret) {

        size_t l;
        uint8_t *p;

        assert(m);

        l = strlen(s);
        if (l > (size_t) (uint32_t) -1)
                return -EINVAL;

        /* field id byte + signature length + signature 's' + NUL + string length + string + NUL */
        p = message_extend_fields(m, 8, 4 + 4 + l + 1);
        if (!p)
                return -ENOMEM;

        p[0] = h;
        p[1] = 1;
        p[2] = type;
        p[3] = 0;

        ((uint32_t*) p)[1] = l;
        memcpy(p + 8, s, l + 1);

        if (ret)
                *ret = (const char*) p + 8;

        return 0;
}

static int message_append_field_signature(
                sd_bus_message *m,
                uint8_t h,
                const char *s,
                const char **ret) {

        size_t l;
        uint8_t *p;

        assert(m);

        l = strlen(s);
        if (l > 255)
                return -EINVAL;

        /* field id byte + signature length + signature 'g' + NUL + string length + string + NUL */
        p = message_extend_fields(m, 8, 4 + 1 + l + 1);
        if (!p)
                return -ENOMEM;

        p[0] = h;
        p[1] = 1;
        p[2] = SD_BUS_TYPE_SIGNATURE;
        p[3] = 0;
        p[4] = l;
        memcpy(p + 5, s, l + 1);

        if (ret)
                *ret = (const char*) p + 5;

        return 0;
}

static int message_append_field_uint32(sd_bus_message *m, uint8_t h, uint32_t x) {
        uint8_t *p;

        assert(m);

        /* field id byte + signature length + signature 'u' + NUL + value */
        p = message_extend_fields(m, 8, 4 + 4);
        if (!p)
                return -ENOMEM;

        p[0] = h;
        p[1] = 1;
        p[2] = SD_BUS_TYPE_UINT32;
        p[3] = 0;

        ((uint32_t*) p)[1] = x;

        return 0;
}

238
int bus_message_from_header(
239 240
                void *buffer,
                size_t length,
241 242 243
                int *fds,
                unsigned n_fds,
                const struct ucred *ucred,
244
                const char *label,
245
                size_t extra,
246 247
                sd_bus_message **ret) {

248 249
        sd_bus_message *m;
        struct bus_header *h;
250
        size_t a, label_sz;
251 252

        assert(buffer || length <= 0);
253
        assert(fds || n_fds <= 0);
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
        assert(ret);

        if (length < sizeof(struct bus_header))
                return -EBADMSG;

        h = buffer;
        if (h->version != 1)
                return -EBADMSG;

        if (h->serial == 0)
                return -EBADMSG;

        if (h->type == _SD_BUS_MESSAGE_TYPE_INVALID)
                return -EBADMSG;

269 270
        if (h->endian != SD_BUS_LITTLE_ENDIAN &&
            h->endian != SD_BUS_BIG_ENDIAN)
271 272
                return -EBADMSG;

273
        a = ALIGN(sizeof(sd_bus_message)) + ALIGN(extra);
274

275 276
        if (label) {
                label_sz = strlen(label);
277 278
                a += label_sz + 1;
        }
279 280

        m = malloc0(a);
281 282 283 284
        if (!m)
                return -ENOMEM;

        m->n_ref = 1;
285
        m->sealed = true;
286
        m->header = h;
287 288
        m->fds = fds;
        m->n_fds = n_fds;
289

290 291 292 293 294 295 296 297
        if (ucred) {
                m->uid = ucred->uid;
                m->pid = ucred->pid;
                m->gid = ucred->gid;
                m->uid_valid = m->gid_valid = true;
        }

        if (label) {
298
                m->label = (char*) m + ALIGN(sizeof(sd_bus_message)) + ALIGN(extra);
299 300 301
                memcpy(m->label, label, label_sz + 1);
        }

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
        *ret = m;
        return 0;
}

int bus_message_from_malloc(
                void *buffer,
                size_t length,
                int *fds,
                unsigned n_fds,
                const struct ucred *ucred,
                const char *label,
                sd_bus_message **ret) {

        sd_bus_message *m;
        int r;

        r = bus_message_from_header(buffer, length, fds, n_fds, ucred, label, 0, &m);
        if (r < 0)
                return r;

        if (length != BUS_MESSAGE_SIZE(m)) {
                r = -EBADMSG;
                goto fail;
        }

        m->fields = (uint8_t*) buffer + sizeof(struct bus_header);
        m->body = (uint8_t*) buffer + sizeof(struct bus_header) + ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m));

330 331 332 333
        m->n_iovec = 1;
        m->iovec[0].iov_base = buffer;
        m->iovec[0].iov_len = length;

334
        r = bus_message_parse_fields(m);
335 336 337 338 339 340
        if (r < 0)
                goto fail;

        /* We take possession of the memory and fds now */
        m->free_header = true;
        m->free_fds = true;
341 342 343

        *ret = m;
        return 0;
344 345 346 347

fail:
        message_free(m);
        return r;
348 349
}

350 351 352
static sd_bus_message *message_new(sd_bus *bus, uint8_t type) {
        sd_bus_message *m;

353
        m = malloc0(ALIGN(sizeof(sd_bus_message)) + sizeof(struct bus_header));
354 355 356 357 358
        if (!m)
                return NULL;

        m->n_ref = 1;
        m->header = (struct bus_header*) ((uint8_t*) m + ALIGN(sizeof(struct sd_bus_message)));
359
        m->header->endian = SD_BUS_NATIVE_ENDIAN;
360 361
        m->header->type = type;
        m->header->version = bus ? bus->message_version : 1;
362
        m->allow_fds = !bus || bus->can_fds || (bus->state != BUS_HELLO && bus->state != BUS_RUNNING);
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382

        return m;
}

int sd_bus_message_new_signal(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *member,
                sd_bus_message **m) {

        sd_bus_message *t;
        int r;

        if (!path)
                return -EINVAL;
        if (!interface)
                return -EINVAL;
        if (!member)
                return -EINVAL;
383 384
        if (!m)
                return -EINVAL;
385 386
        if (bus && bus->state == BUS_UNSET)
                return -ENOTCONN;
387 388 389 390 391

        t = message_new(bus, SD_BUS_MESSAGE_TYPE_SIGNAL);
        if (!t)
                return -ENOMEM;

392 393
        t->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;

394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
        r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path);
        if (r < 0)
                goto fail;
        r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_INTERFACE, SD_BUS_TYPE_STRING, interface, &t->interface);
        if (r < 0)
                goto fail;
        r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_MEMBER, SD_BUS_TYPE_STRING, member, &t->member);
        if (r < 0)
                goto fail;

        *m = t;
        return 0;

fail:
        sd_bus_message_unref(t);
        return r;
}

int sd_bus_message_new_method_call(
                sd_bus *bus,
                const char *destination,
                const char *path,
                const char *interface,
                const char *member,
                sd_bus_message **m) {

        sd_bus_message *t;
        int r;

        if (!path)
                return -EINVAL;
        if (!member)
                return -EINVAL;
427 428
        if (!m)
                return -EINVAL;
429 430
        if (bus && bus->state == BUS_UNSET)
                return -ENOTCONN;
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462

        t = message_new(bus, SD_BUS_MESSAGE_TYPE_METHOD_CALL);
        if (!t)
                return -ENOMEM;

        r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path);
        if (r < 0)
                goto fail;
        r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_MEMBER, SD_BUS_TYPE_STRING, member, &t->member);
        if (r < 0)
                goto fail;

        if (interface) {
                r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_INTERFACE, SD_BUS_TYPE_STRING, interface, &t->interface);
                if (r < 0)
                        goto fail;
        }

        if (destination) {
                r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &t->destination);
                if (r < 0)
                        goto fail;
        }

        *m = t;
        return 0;

fail:
        message_free(t);
        return r;
}

463
static int message_new_reply(
464 465
                sd_bus *bus,
                sd_bus_message *call,
466
                uint8_t type,
467 468 469 470 471 472 473
                sd_bus_message **m) {

        sd_bus_message *t;
        int r;

        if (!call)
                return -EINVAL;
474 475
        if (!call->sealed)
                return -EPERM;
476 477
        if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
                return -EINVAL;
478 479
        if (!m)
                return -EINVAL;
480 481
        if (bus && bus->state == BUS_UNSET)
                return -ENOTCONN;
482

483
        t = message_new(bus, type);
484 485 486
        if (!t)
                return -ENOMEM;

487
        t->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
488
        t->reply_serial = BUS_MESSAGE_SERIAL(call);
489

490 491 492 493 494 495 496 497 498 499
        r = message_append_field_uint32(t, SD_BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_serial);
        if (r < 0)
                goto fail;

        if (call->sender) {
                r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, call->sender, &t->sender);
                if (r < 0)
                        goto fail;
        }

500 501
        t->dont_send = !!(call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED);

502
        *m = t;
503
        return 0;
504 505 506 507 508 509

fail:
        message_free(t);
        return r;
}

510 511 512 513 514 515 516 517
int sd_bus_message_new_method_return(
                sd_bus *bus,
                sd_bus_message *call,
                sd_bus_message **m) {

        return message_new_reply(bus, call, SD_BUS_MESSAGE_TYPE_METHOD_RETURN, m);
}

518 519 520 521 522 523 524 525 526
int sd_bus_message_new_method_error(
                sd_bus *bus,
                sd_bus_message *call,
                const sd_bus_error *e,
                sd_bus_message **m) {

        sd_bus_message *t;
        int r;

527
        if (!sd_bus_error_is_set(e))
528
                return -EINVAL;
529 530
        if (!m)
                return -EINVAL;
531

532
        r = message_new_reply(bus, call, SD_BUS_MESSAGE_TYPE_METHOD_ERROR, &t);
533
        if (r < 0)
534
                return r;
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664

        r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, e->name, &t->error.name);
        if (r < 0)
                goto fail;

        if (e->message) {
                r = message_append_basic(t, SD_BUS_TYPE_STRING, e->message, (const void**) &t->error.message);
                if (r < 0)
                        goto fail;
        }

        *m = t;
        return 0;

fail:
        message_free(t);
        return r;
}

sd_bus_message* sd_bus_message_ref(sd_bus_message *m) {
        if (!m)
                return NULL;

        assert(m->n_ref > 0);
        m->n_ref++;

        return m;
}

sd_bus_message* sd_bus_message_unref(sd_bus_message *m) {
        if (!m)
                return NULL;

        assert(m->n_ref > 0);
        m->n_ref--;

        if (m->n_ref <= 0)
                message_free(m);

        return NULL;
}

int sd_bus_message_get_type(sd_bus_message *m, uint8_t *type) {
        if (!m)
                return -EINVAL;
        if (!type)
                return -EINVAL;

        *type = m->header->type;
        return 0;
}

int sd_bus_message_get_serial(sd_bus_message *m, uint64_t *serial) {
        if (!m)
                return -EINVAL;
        if (!serial)
                return -EINVAL;
        if (m->header->serial == 0)
                return -ENOENT;

        *serial = BUS_MESSAGE_SERIAL(m);
        return 0;
}

int sd_bus_message_get_reply_serial(sd_bus_message *m, uint64_t *serial) {
        if (!m)
                return -EINVAL;
        if (!serial)
                return -EINVAL;
        if (m->reply_serial == 0)
                return -ENOENT;

        *serial = m->reply_serial;
        return 0;
}

int sd_bus_message_get_no_reply(sd_bus_message *m) {
        if (!m)
                return -EINVAL;

        return m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_CALL ? !!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED) : 0;
}

const char *sd_bus_message_get_path(sd_bus_message *m) {
        if (!m)
                return NULL;

        return m->path;
}

const char *sd_bus_message_get_interface(sd_bus_message *m) {
        if (!m)
                return NULL;

        return m->interface;
}

const char *sd_bus_message_get_member(sd_bus_message *m) {
        if (!m)
                return NULL;

        return m->member;
}
const char *sd_bus_message_get_destination(sd_bus_message *m) {
        if (!m)
                return NULL;

        return m->destination;
}

const char *sd_bus_message_get_sender(sd_bus_message *m) {
        if (!m)
                return NULL;

        return m->sender;
}

const sd_bus_error *sd_bus_message_get_error(sd_bus_message *m) {
        if (!m)
                return NULL;

        if (!sd_bus_error_is_set(&m->error))
                return NULL;

        return &m->error;
}

int sd_bus_message_get_uid(sd_bus_message *m, uid_t *uid) {
        if (!m)
                return -EINVAL;
665 666
        if (!uid)
                return -EINVAL;
667
        if (!m->uid_valid)
668
                return -ESRCH;
669 670 671 672 673 674 675 676

        *uid = m->uid;
        return 0;
}

int sd_bus_message_get_gid(sd_bus_message *m, gid_t *gid) {
        if (!m)
                return -EINVAL;
677 678
        if (!gid)
                return -EINVAL;
679
        if (!m->gid_valid)
680
                return -ESRCH;
681 682 683 684 685 686 687 688

        *gid = m->gid;
        return 0;
}

int sd_bus_message_get_pid(sd_bus_message *m, pid_t *pid) {
        if (!m)
                return -EINVAL;
689 690
        if (!pid)
                return -EINVAL;
691
        if (m->pid <= 0)
692
                return -ESRCH;
693 694 695 696 697 698 699 700

        *pid = m->pid;
        return 0;
}

int sd_bus_message_get_tid(sd_bus_message *m, pid_t *tid) {
        if (!m)
                return -EINVAL;
701 702
        if (!tid)
                return -EINVAL;
703
        if (m->tid <= 0)
704
                return -ESRCH;
705 706 707 708 709

        *tid = m->tid;
        return 0;
}

710 711 712
int sd_bus_message_get_pid_starttime(sd_bus_message *m, uint64_t *usec) {
        if (!m)
                return -EINVAL;
713 714
        if (!usec)
                return -EINVAL;
715
        if (m->pid_starttime <= 0)
716
                return -ESRCH;
717 718 719 720 721

        *usec = m->pid_starttime;
        return 0;
}

722
int sd_bus_message_get_selinux_context(sd_bus_message *m, const char **ret) {
723
        if (!m)
724 725 726
                return -EINVAL;
        if (!m->label)
                return -ESRCH;
727

728 729
        *ret = m->label;
        return 0;
730 731
}

732
int sd_bus_message_get_monotonic_timestamp(sd_bus_message *m, uint64_t *usec) {
733 734
        if (!m)
                return -EINVAL;
735 736
        if (!usec)
                return -EINVAL;
737
        if (m->monotonic <= 0)
738
                return -ESRCH;
739

740
        *usec = m->monotonic;
741 742 743
        return 0;
}

744 745 746
int sd_bus_message_get_realtime_timestamp(sd_bus_message *m, uint64_t *usec) {
        if (!m)
                return -EINVAL;
747 748
        if (!usec)
                return -EINVAL;
749
        if (m->realtime <= 0)
750
                return -ESRCH;
751 752 753 754 755

        *usec = m->realtime;
        return 0;
}

756
int sd_bus_message_get_comm(sd_bus_message *m, const char **ret) {
757
        if (!m)
758 759 760 761 762
                return -EINVAL;
        if (!ret)
                return -EINVAL;
        if (!m->comm)
                return -ESRCH;
763

764 765
        *ret = m->comm;
        return 0;
766 767
}

768
int sd_bus_message_get_tid_comm(sd_bus_message *m, const char **ret) {
769
        if (!m)
770 771 772 773 774
                return -EINVAL;
        if (!ret)
                return -EINVAL;
        if (!m->tid_comm)
                return -ESRCH;
775

776 777
        *ret = m->tid_comm;
        return 0;
778 779
}

780
int sd_bus_message_get_exe(sd_bus_message *m, const char **ret) {
781
        if (!m)
782 783 784 785 786
                return -EINVAL;
        if (!ret)
                return -EINVAL;
        if (!m->exe)
                return -ESRCH;
787

788 789 790 791 792 793 794 795 796 797 798 799 800 801
        *ret = m->exe;
        return 0;
}

int sd_bus_message_get_cgroup(sd_bus_message *m, const char **ret) {
        if (!m)
                return -EINVAL;
        if (!ret)
                return -EINVAL;
        if (!m->cgroup)
                return -ESRCH;

        *ret = m->cgroup;
        return 0;
802 803
}

804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
int sd_bus_message_get_unit(sd_bus_message *m, const char **ret) {
        int r;

        if (!m)
                return -EINVAL;
        if (!ret)
                return -EINVAL;
        if (!m->cgroup)
                return -ESRCH;

        if (!m->unit) {
                r = cg_path_get_unit(m->cgroup, &m->unit);
                if (r < 0)
                        return r;
        }

        *ret = m->unit;
        return 0;
}

int sd_bus_message_get_user_unit(sd_bus_message *m, const char **ret) {
        int r;

        if (!m)
                return -EINVAL;
        if (!ret)
                return -EINVAL;
        if (!m->cgroup)
                return -ESRCH;

        if (!m->user_unit) {
                r = cg_path_get_user_unit(m->cgroup, &m->user_unit);
                if (r < 0)
                        return r;
        }

        *ret = m->user_unit;
        return 0;
}

int sd_bus_message_get_session(sd_bus_message *m, const char **ret) {
        int r;

        if (!m)
                return -EINVAL;
        if (!ret)
                return -EINVAL;
        if (!m->cgroup)
                return -ESRCH;

        if (!m->session) {
                r = cg_path_get_session(m->cgroup, &m->session);
                if (r < 0)
                        return r;
        }

        *ret = m->session;
        return 0;
}

864 865 866 867 868 869 870 871 872 873 874
int sd_bus_message_get_owner_uid(sd_bus_message *m, uid_t *uid) {
        if (!m)
                return -EINVAL;
        if (!uid)
                return -EINVAL;
        if (!m->cgroup)
                return -ESRCH;

        return cg_path_get_owner_uid(m->cgroup, uid);
}

875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
int sd_bus_message_get_cmdline(sd_bus_message *m, char ***cmdline) {
        size_t n, i;
        const char *p;
        bool first;

        if (!m)
                return -EINVAL;

        if (!m->cmdline)
                return -ENOENT;

        for (p = m->cmdline, n = 0; p < m->cmdline + m->cmdline_length; p++)
                if (*p == 0)
                        n++;

        m->cmdline_array = new(char*, n + 1);
        if (!m->cmdline_array)
                return -ENOMEM;

        for (p = m->cmdline, i = 0, first = true; p < m->cmdline + m->cmdline_length; p++) {
                if (first)
                        m->cmdline_array[i++] = (char*) p;

                first = *p == 0;
        }

        m->cmdline_array[i] = NULL;
        *cmdline = m->cmdline_array;

        return 0;
}

907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
int sd_bus_message_get_audit_sessionid(sd_bus_message *m, uint32_t *sessionid) {
        if (!m)
                return -EINVAL;
        if (!sessionid)
                return -EINVAL;
        if (!m->audit)
                return -ESRCH;

        *sessionid = m->audit->sessionid;
        return 0;
}

int sd_bus_message_get_audit_loginuid(sd_bus_message *m, uid_t *uid) {
        if (!m)
                return -EINVAL;
        if (!uid)
                return -EINVAL;
        if (!m->audit)
                return -ESRCH;

        *uid = m->audit->loginuid;
        return 0;
}

931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
int sd_bus_message_is_signal(sd_bus_message *m, const char *interface, const char *member) {
        if (!m)
                return -EINVAL;

        if (m->header->type != SD_BUS_MESSAGE_TYPE_SIGNAL)
                return 0;

        if (interface && (!m->interface || !streq(m->interface, interface)))
                return 0;

        if (member &&  (!m->member || !streq(m->member, member)))
                return 0;

        return 1;
}

int sd_bus_message_is_method_call(sd_bus_message *m, const char *interface, const char *member) {
        if (!m)
                return -EINVAL;

        if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
                return 0;

        if (interface && (!m->interface || !streq(m->interface, interface)))
                return 0;

        if (member &&  (!m->member || !streq(m->member, member)))
                return 0;

        return 1;
}

int sd_bus_message_is_method_error(sd_bus_message *m, const char *name) {
        if (!m)
                return -EINVAL;

        if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_ERROR)
                return 0;

        if (name && (!m->error.name || !streq(m->error.name, name)))
                return 0;

        return 1;
}

int sd_bus_message_set_no_reply(sd_bus_message *m, int b) {
        if (!m)
                return -EINVAL;
        if (m->sealed)
                return -EPERM;
        if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
                return -EPERM;

        if (b)
                m->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
        else
                m->header->flags &= ~SD_BUS_MESSAGE_NO_REPLY_EXPECTED;

        return 0;
}

static struct bus_container *message_get_container(sd_bus_message *m) {
        assert(m);

        if (m->n_containers == 0)
                return &m->root_container;

998 999
        assert(m->containers);
        return m->containers + m->n_containers - 1;
1000 1001 1002 1003 1004 1005 1006 1007
}

static void *message_extend_body(sd_bus_message *m, size_t align, size_t sz) {
        void *p, *o;
        size_t added;
        struct bus_container *c;

        assert(m);
1008
        assert(align > 0);
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018

        o = m->body;
        added = m->header->body_size;

        p = buffer_extend(&m->body, &m->header->body_size, align, sz);
        if (!p)
                return NULL;

        added = m->header->body_size - added;

1019
        for (c = m->containers; c < m->containers + m->n_containers; c++)
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
                if (c->array_size) {
                        c->array_size = (uint32_t*) ((uint8_t*) m->body + ((uint8_t*) c->array_size - (uint8_t*) o));
                        *c->array_size += added;
                }

        if (o != m->body) {
                if (m->error.message)
                        m->error.message = (const char*) m->body + (m->error.message - (const char*) o);
        }

        m->free_body = true;

        return p;
}

int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored) {
        struct bus_container *c;
1037
        ssize_t align, sz;
1038 1039 1040
        uint32_t k;
        void *a;
        char *e = NULL;
1041
        int fd = -1;
1042
        uint32_t fdi = 0;
1043
        int r;
1044 1045 1046

        if (!m)
                return -EINVAL;
1047 1048
        if (!p)
                return -EINVAL;
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
        if (m->sealed)
                return -EPERM;
        if (!bus_type_is_basic(type))
                return -EINVAL;

        c = message_get_container(m);

        if (c->signature && c->signature[c->index]) {
                /* Container signature is already set */

                if (c->signature[c->index] != type)
1060
                        return -ENXIO;
1061 1062 1063
        } else {
                /* Maybe we can append to the signature? But only if this is the top-level container*/
                if (c->enclosing != 0)
1064
                        return -ENXIO;
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074

                e = strextend(&c->signature, CHAR_TO_STR(type), NULL);
                if (!e)
                        return -ENOMEM;
        }

        switch (type) {

        case SD_BUS_TYPE_STRING:
        case SD_BUS_TYPE_OBJECT_PATH:
1075

1076 1077 1078 1079 1080
                align = 4;
                sz = 4 + strlen(p) + 1;
                break;

        case SD_BUS_TYPE_SIGNATURE:
1081

1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
                align = 1;
                sz = 1 + strlen(p) + 1;
                break;

        case SD_BUS_TYPE_BOOLEAN:
                align = sz = 4;

                assert_cc(sizeof(int) == sizeof(uint32_t));
                memcpy(&k, p, 4);
                k = !!k;
                p = &k;
                break;

1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
        case SD_BUS_TYPE_UNIX_FD: {
                int z, *f;

                if (!m->allow_fds) {
                        r = -ENOTSUP;
                        goto fail;
                }

                align = sz = 4;

                z = *(int*) p;
                if (z < 0) {
                        r = -EINVAL;
                        goto fail;
                }

                fd = fcntl(z, F_DUPFD_CLOEXEC, 3);
                if (fd < 0) {
                        r = -errno;
                        goto fail;
                }

                f = realloc(m->fds, sizeof(int) * (m->n_fds + 1));
                if (!f) {
                        r = -ENOMEM;
                        goto fail;
                }

                fdi = m->n_fds;
                f[fdi] = fd;
                m->fds = f;
                m->free_fds = true;
                break;
        }

1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
        default:
                align = bus_type_get_alignment(type);
                sz = bus_type_get_size(type);
                break;
        }

        assert(align > 0);
        assert(sz > 0);

        a = message_extend_body(m, align, sz);
        if (!a) {
1141 1142
                r = -ENOMEM;
                goto fail;
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
        }

        if (type == SD_BUS_TYPE_STRING || type == SD_BUS_TYPE_OBJECT_PATH) {
                *(uint32_t*) a = sz - 5;
                memcpy((uint8_t*) a + 4, p, sz - 4);

                if (stored)
                        *stored = (const uint8_t*) a + 4;

        } else if (type == SD_BUS_TYPE_SIGNATURE) {
                *(uint8_t*) a = sz - 1;
                memcpy((uint8_t*) a + 1, p, sz - 1);

                if (stored)
                        *stored = (const uint8_t*) a + 1;
1158 1159 1160 1161 1162 1163 1164
        } else if (type == SD_BUS_TYPE_UNIX_FD) {
                *(uint32_t*) a = fdi;

                if (stored)
                        *stored = a;

                m->n_fds ++;
1165 1166 1167 1168 1169 1170 1171 1172 1173

        } else {
                memcpy(a, p, sz);

                if (stored)
                        *stored = a;
        }

        if (c->enclosing != SD_BUS_TYPE_ARRAY)
1174
                c->index++;
1175 1176

        return 0;
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186

fail:
        /* Truncate extended signature again */
        if (e)
                c->signature[c->index] = 0;

        if (fd >= 0)
                close_nointr_nofail(fd);

        return r;
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
}

int sd_bus_message_append_basic(sd_bus_message *m, char type, const void *p) {
        return message_append_basic(m, type, p, NULL);
}

static int bus_message_open_array(
                sd_bus_message *m,
                struct bus_container *c,
                const char *contents,
                uint32_t **array_size) {

1199
        unsigned nindex;
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
        char *e = NULL;
        void *a, *b;
        int alignment;
        size_t saved;

        assert(m);
        assert(c);
        assert(contents);
        assert(array_size);

        if (!signature_is_single(contents))
                return -EINVAL;

        alignment = bus_type_get_alignment(contents[0]);
        if (alignment < 0)
                return alignment;

        if (c->signature && c->signature[c->index]) {

                /* Verify the existing signature */

                if (c->signature[c->index] != SD_BUS_TYPE_ARRAY)
1222
                        return -ENXIO;
1223 1224

                if (!startswith(c->signature + c->index + 1, contents))
1225
                        return -ENXIO;
1226 1227 1228 1229

                nindex = c->index + 1 + strlen(contents);
        } else {
                if (c->enclosing != 0)
1230
                        return -ENXIO;
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277

                /* Extend the existing signature */

                e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_ARRAY), contents, NULL);
                if (!e)
                        return -ENOMEM;

                nindex = e - c->signature;
        }

        saved = m->header->body_size;
        a = message_extend_body(m, 4, 4);
        if (!a) {
                /* Truncate extended signature again */
                if (e)
                        c->signature[c->index] = 0;

                return -ENOMEM;
        }
        b = m->body;

        if (!message_extend_body(m, alignment, 0)) {
                /* Add alignment between size and first element */
                if (e)
                        c->signature[c->index] = 0;

                m->header->body_size = saved;
                return -ENOMEM;
        }

        if (c->enclosing != SD_BUS_TYPE_ARRAY)
                c->index = nindex;

        /* m->body might have changed so let's readjust a */
        a = (uint8_t*) m->body + ((uint8_t*) a - (uint8_t*) b);
        *(uint32_t*) a = 0;

        *array_size = a;
        return 0;
}

static int bus_message_open_variant(
                sd_bus_message *m,
                struct bus_container *c,
                const char *contents) {

        char *e = NULL;
1278
        size_t l;
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
        void *a;

        assert(m);
        assert(c);
        assert(contents);

        if (!signature_is_single(contents))
                return -EINVAL;

        if (*contents == SD_BUS_TYPE_DICT_ENTRY_BEGIN)
                return -EINVAL;

        if (c->signature && c->signature[c->index]) {

                if (c->signature[c->index] != SD_BUS_TYPE_VARIANT)
1294
                        return -ENXIO;
1295 1296 1297

        } else {
                if (c->enclosing != 0)
1298
                        return -ENXIO;
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318

                e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_VARIANT), NULL);
                if (!e)
                        return -ENOMEM;
        }

        l = strlen(contents);
        a = message_extend_body(m, 1, 1 + l + 1);
        if (!a) {
                /* Truncate extended signature again */
                if (e)
                        c->signature[c->index] = 0;

                return -ENOMEM;
        }

        *(uint8_t*) a = l;
        memcpy((uint8_t*) a + 1, contents, l + 1);

        if (c->enclosing != SD_BUS_TYPE_ARRAY)
1319
                c->index++;
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346

        return 0;
}

static int bus_message_open_struct(
                sd_bus_message *m,
                struct bus_container *c,
                const char *contents) {

        size_t nindex;
        char *e = NULL;

        assert(m);
        assert(c);
        assert(contents);

        if (!signature_is_valid(contents, false))
                return -EINVAL;

        if (c->signature && c->signature[c->index]) {
                size_t l;

                l = strlen(contents);

                if (c->signature[c->index] != SD_BUS_TYPE_STRUCT_BEGIN ||
                    !startswith(c->signature + c->index + 1, contents) ||
                    c->signature[c->index + 1 + l] != SD_BUS_TYPE_STRUCT_END)
1347
                        return -ENXIO;
1348 1349 1350 1351

                nindex = c->index + 1 + l + 1;
        } else {
                if (c->enclosing != 0)
1352
                        return -ENXIO;
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389

                e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_BEGIN), contents, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_END), NULL);
                if (!e)
                        return -ENOMEM;

                nindex = e - c->signature;
        }

        /* Align contents to 8 byte boundary */
        if (!message_extend_body(m, 8, 0)) {
                if (e)
                        c->signature[c->index] = 0;

                return -ENOMEM;
        }

        if (c->enclosing != SD_BUS_TYPE_ARRAY)
                c->index = nindex;

        return 0;
}

static int bus_message_open_dict_entry(
                sd_bus_message *m,
                struct bus_container *c,
                const char *contents) {

        size_t nindex;

        assert(m);
        assert(c);
        assert(contents);

        if (!signature_is_pair(contents))
                return -EINVAL;

        if (c->enclosing != SD_BUS_TYPE_ARRAY)
1390
                return -ENXIO;
1391 1392 1393 1394 1395 1396 1397 1398 1399

        if (c->signature && c->signature[c->index]) {
                size_t l;

                l = strlen(contents);

                if (c->signature[c->index] != SD_BUS_TYPE_DICT_ENTRY_BEGIN ||
                    !startswith(c->signature + c->index + 1, contents) ||
                    c->signature[c->index + 1 + l] != SD_BUS_TYPE_DICT_ENTRY_END)
1400
                        return -ENXIO;
1401 1402 1403

                nindex = c->index + 1 + l + 1;
        } else
1404
                return -ENXIO;
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420

        /* Align contents to 8 byte boundary */
        if (!message_extend_body(m, 8, 0))
                return -ENOMEM;

        if (c->enclosing != SD_BUS_TYPE_ARRAY)
                c->index = nindex;

        return 0;
}

int sd_bus_message_open_container(
                sd_bus_message *m,
                char type,
                const char *contents) {

1421
        struct bus_container *c, *w;
1422
        uint32_t *array_size = NULL;
1423
        char *signature;
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
        int r;

        if (!m)
                return -EINVAL;
        if (m->sealed)
                return -EPERM;
        if (!contents)
                return -EINVAL;

        /* Make sure we have space for one more container */
1434 1435
        w = realloc(m->containers, sizeof(struct bus_container) * (m->n_containers + 1));
        if (!w)
1436
                return -ENOMEM;
1437
        m->containers = w;
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461

        c = message_get_container(m);

        signature = strdup(contents);
        if (!signature)
                return -ENOMEM;

        if (type == SD_BUS_TYPE_ARRAY)
                r = bus_message_open_array(m, c, contents, &array_size);
        else if (type == SD_BUS_TYPE_VARIANT)
                r = bus_message_open_variant(m, c, contents);
        else if (type == SD_BUS_TYPE_STRUCT)
                r = bus_message_open_struct(m, c, contents);
        else if (type == SD_BUS_TYPE_DICT_ENTRY)
                r = bus_message_open_dict_entry(m, c, contents);
        else
                r = -EINVAL;

        if (r < 0) {
                free(signature);
                return r;
        }

        /* OK, let's fill it in */
1462 1463 1464 1465 1466 1467
        w += m->n_containers++;
        w->enclosing = type;
        w->signature = signature;
        w->index = 0;
        w->array_size = array_size;
        w->begin = 0;
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483

        return 0;
}

int sd_bus_message_close_container(sd_bus_message *m) {
        struct bus_container *c;

        if (!m)
                return -EINVAL;
        if (m->sealed)
                return -EPERM;
        if (m->n_containers <= 0)
                return -EINVAL;

        c = message_get_container(m);
        if (c->enclosing != SD_BUS_TYPE_ARRAY)
1484
                if (c->signature && c->signature[c->index] != 0)
1485 1486 1487 1488 1489 1490 1491 1492
                        return -EINVAL;

        free(c->signature);
        m->n_containers--;

        return 0;
}

1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532

typedef struct {
        const char *types;
        unsigned n_struct;
        unsigned n_array;
} TypeStack;

static int type_stack_push(TypeStack *stack, unsigned max, unsigned *i, const char *types, unsigned n_struct, unsigned n_array) {
        assert(stack);
        assert(max > 0);

        if (*i >= max)
                return -EINVAL;

        stack[*i].types = types;
        stack[*i].n_struct = n_struct;
        stack[*i].n_array = n_array;
        (*i)++;

        return 0;
}

static int type_stack_pop(TypeStack *stack, unsigned max, unsigned *i, const char **types, unsigned *n_struct, unsigned *n_array) {
        assert(stack);
        assert(max > 0);
        assert(types);
        assert(n_struct);
        assert(n_array);

        if (*i <= 0)
                return 0;

        (*i)--;
        *types = stack[*i].types;
        *n_struct = stack[*i].n_struct;
        *n_array = stack[*i].n_array;

        return 1;
}

1533
int bus_message_append_ap(
1534 1535 1536 1537
                sd_bus_message *m,
                const char *types,
                va_list ap) {

1538 1539 1540
        unsigned n_array, n_struct;
        TypeStack stack[BUS_CONTAINER_DEPTH];
        unsigned stack_ptr = 0;
1541 1542 1543
        int r;

        assert(m);
1544 1545 1546

        if (!types)
                return 0;
1547

1548 1549