bus-message.c 92 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 30

#include "sd-bus.h"
31
#include "bus-message.h"
32 33 34 35
#include "bus-internal.h"
#include "bus-type.h"
#include "bus-signature.h"

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

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
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;
}

53 54 55 56 57 58 59 60 61 62 63 64
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);

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

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

73 74
        free(m->cmdline_array);

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

78
        free(m->peeked_signature);
79 80 81 82 83 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
        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;
}

233
int bus_message_from_header(
234 235
                void *buffer,
                size_t length,
236 237 238
                int *fds,
                unsigned n_fds,
                const struct ucred *ucred,
239
                const char *label,
240
                size_t extra,
241 242
                sd_bus_message **ret) {

243 244
        sd_bus_message *m;
        struct bus_header *h;
245
        size_t a, label_sz;
246 247

        assert(buffer || length <= 0);
248
        assert(fds || n_fds <= 0);
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
        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;

264 265
        if (h->endian != SD_BUS_LITTLE_ENDIAN &&
            h->endian != SD_BUS_BIG_ENDIAN)
266 267
                return -EBADMSG;

268
        a = ALIGN(sizeof(sd_bus_message)) + ALIGN(extra);
269

270 271
        if (label) {
                label_sz = strlen(label);
272 273
                a += label_sz + 1;
        }
274 275

        m = malloc0(a);
276 277 278 279
        if (!m)
                return -ENOMEM;

        m->n_ref = 1;
280
        m->sealed = true;
281
        m->header = h;
282 283
        m->fds = fds;
        m->n_fds = n_fds;
284

285 286 287 288 289 290 291 292
        if (ucred) {
                m->uid = ucred->uid;
                m->pid = ucred->pid;
                m->gid = ucred->gid;
                m->uid_valid = m->gid_valid = true;
        }

        if (label) {
293
                m->label = (char*) m + ALIGN(sizeof(sd_bus_message)) + ALIGN(extra);
294 295 296
                memcpy(m->label, label, label_sz + 1);
        }

297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
        *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));

325 326 327 328
        m->n_iovec = 1;
        m->iovec[0].iov_base = buffer;
        m->iovec[0].iov_len = length;

329
        r = bus_message_parse_fields(m);
330 331 332 333 334 335
        if (r < 0)
                goto fail;

        /* We take possession of the memory and fds now */
        m->free_header = true;
        m->free_fds = true;
336 337 338

        *ret = m;
        return 0;
339 340 341 342

fail:
        message_free(m);
        return r;
343 344
}

345 346 347
static sd_bus_message *message_new(sd_bus *bus, uint8_t type) {
        sd_bus_message *m;

348
        m = malloc0(ALIGN(sizeof(sd_bus_message)) + sizeof(struct bus_header));
349 350 351 352 353
        if (!m)
                return NULL;

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

        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;
378 379
        if (!m)
                return -EINVAL;
380 381
        if (bus && bus->state == BUS_UNSET)
                return -ENOTCONN;
382 383 384 385 386

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

387 388
        t->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;

389 390 391 392 393 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
        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;
422 423
        if (!m)
                return -EINVAL;
424 425
        if (bus && bus->state == BUS_UNSET)
                return -ENOTCONN;
426 427 428 429 430 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

        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;
}

458
static int message_new_reply(
459 460
                sd_bus *bus,
                sd_bus_message *call,
461
                uint8_t type,
462 463 464 465 466 467 468
                sd_bus_message **m) {

        sd_bus_message *t;
        int r;

        if (!call)
                return -EINVAL;
469 470
        if (!call->sealed)
                return -EPERM;
471 472
        if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
                return -EINVAL;
473 474
        if (!m)
                return -EINVAL;
475 476
        if (bus && bus->state == BUS_UNSET)
                return -ENOTCONN;
477

478
        t = message_new(bus, type);
479 480 481
        if (!t)
                return -ENOMEM;

482
        t->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
483
        t->reply_serial = BUS_MESSAGE_SERIAL(call);
484

485 486 487 488 489 490 491 492 493 494
        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;
        }

495 496
        t->dont_send = !!(call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED);

497
        *m = t;
498
        return 0;
499 500 501 502 503 504

fail:
        message_free(t);
        return r;
}

505 506 507 508 509 510 511 512
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);
}

513 514 515 516 517 518 519 520 521
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;

522
        if (!sd_bus_error_is_set(e))
523
                return -EINVAL;
524 525
        if (!m)
                return -EINVAL;
526

527
        r = message_new_reply(bus, call, SD_BUS_MESSAGE_TYPE_METHOD_ERROR, &t);
528
        if (r < 0)
529
                return r;
530 531 532 533 534 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

        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;
660 661
        if (!uid)
                return -EINVAL;
662
        if (!m->uid_valid)
663
                return -ESRCH;
664 665 666 667 668 669 670 671

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

int sd_bus_message_get_gid(sd_bus_message *m, gid_t *gid) {
        if (!m)
                return -EINVAL;
672 673
        if (!gid)
                return -EINVAL;
674
        if (!m->gid_valid)
675
                return -ESRCH;
676 677 678 679 680 681 682 683

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

int sd_bus_message_get_pid(sd_bus_message *m, pid_t *pid) {
        if (!m)
                return -EINVAL;
684 685
        if (!pid)
                return -EINVAL;
686
        if (m->pid <= 0)
687
                return -ESRCH;
688 689 690 691 692 693 694 695

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

int sd_bus_message_get_tid(sd_bus_message *m, pid_t *tid) {
        if (!m)
                return -EINVAL;
696 697
        if (!tid)
                return -EINVAL;
698
        if (m->tid <= 0)
699
                return -ESRCH;
700 701 702 703 704

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

705 706 707
int sd_bus_message_get_pid_starttime(sd_bus_message *m, uint64_t *usec) {
        if (!m)
                return -EINVAL;
708 709
        if (!usec)
                return -EINVAL;
710
        if (m->pid_starttime <= 0)
711
                return -ESRCH;
712 713 714 715 716

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

717
int sd_bus_message_get_selinux_context(sd_bus_message *m, const char **ret) {
718
        if (!m)
719 720 721
                return -EINVAL;
        if (!m->label)
                return -ESRCH;
722

723 724
        *ret = m->label;
        return 0;
725 726
}

727
int sd_bus_message_get_monotonic_timestamp(sd_bus_message *m, uint64_t *usec) {
728 729
        if (!m)
                return -EINVAL;
730 731
        if (!usec)
                return -EINVAL;
732
        if (m->monotonic <= 0)
733
                return -ESRCH;
734

735
        *usec = m->monotonic;
736 737 738
        return 0;
}

739 740 741
int sd_bus_message_get_realtime_timestamp(sd_bus_message *m, uint64_t *usec) {
        if (!m)
                return -EINVAL;
742 743
        if (!usec)
                return -EINVAL;
744
        if (m->realtime <= 0)
745
                return -ESRCH;
746 747 748 749 750

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

751
int sd_bus_message_get_comm(sd_bus_message *m, const char **ret) {
752
        if (!m)
753 754 755 756 757
                return -EINVAL;
        if (!ret)
                return -EINVAL;
        if (!m->comm)
                return -ESRCH;
758

759 760
        *ret = m->comm;
        return 0;
761 762
}

763
int sd_bus_message_get_tid_comm(sd_bus_message *m, const char **ret) {
764
        if (!m)
765 766 767 768 769
                return -EINVAL;
        if (!ret)
                return -EINVAL;
        if (!m->tid_comm)
                return -ESRCH;
770

771 772
        *ret = m->tid_comm;
        return 0;
773 774
}

775
int sd_bus_message_get_exe(sd_bus_message *m, const char **ret) {
776
        if (!m)
777 778 779 780 781
                return -EINVAL;
        if (!ret)
                return -EINVAL;
        if (!m->exe)
                return -ESRCH;
782

783 784 785 786 787 788 789 790 791 792 793 794 795 796
        *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;
797 798
}

799 800 801 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
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;
}

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 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
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;

898 899
        assert(m->containers);
        return m->containers + m->n_containers - 1;
900 901 902 903 904 905 906 907
}

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);
908
        assert(align > 0);
909 910 911 912 913 914 915 916 917 918

        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;

919
        for (c = m->containers; c < m->containers + m->n_containers; c++)
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
                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;
937
        ssize_t align, sz;
938 939 940
        uint32_t k;
        void *a;
        char *e = NULL;
941
        int fd = -1;
942
        uint32_t fdi = 0;
943
        int r;
944 945 946

        if (!m)
                return -EINVAL;
947 948
        if (!p)
                return -EINVAL;
949 950 951 952 953 954 955 956 957 958 959
        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)
960
                        return -ENXIO;
961 962 963
        } else {
                /* Maybe we can append to the signature? But only if this is the top-level container*/
                if (c->enclosing != 0)
964
                        return -ENXIO;
965 966 967 968 969 970 971 972 973 974

                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:
975

976 977 978 979 980
                align = 4;
                sz = 4 + strlen(p) + 1;
                break;

        case SD_BUS_TYPE_SIGNATURE:
981

982 983 984 985 986 987 988 989 990 991 992 993 994
                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;

995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
        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;
        }

1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
        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) {
1041 1042
                r = -ENOMEM;
                goto fail;
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
        }

        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;
1058 1059 1060 1061 1062 1063 1064
        } else if (type == SD_BUS_TYPE_UNIX_FD) {
                *(uint32_t*) a = fdi;

                if (stored)
                        *stored = a;

                m->n_fds ++;
1065 1066 1067 1068 1069 1070 1071 1072 1073

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

                if (stored)
                        *stored = a;
        }

        if (c->enclosing != SD_BUS_TYPE_ARRAY)
1074
                c->index++;
1075 1076

        return 0;
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086

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

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

        return r;
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
}

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) {

1099
        unsigned nindex;
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
        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)
1122
                        return -ENXIO;
1123 1124

                if (!startswith(c->signature + c->index + 1, contents))
1125
                        return -ENXIO;
1126 1127 1128 1129

                nindex = c->index + 1 + strlen(contents);
        } else {
                if (c->enclosing != 0)
1130
                        return -ENXIO;
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177

                /* 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;
1178
        size_t l;
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
        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)
1194
                        return -ENXIO;
1195 1196 1197

        } else {
                if (c->enclosing != 0)
1198
                        return -ENXIO;
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218

                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)
1219
                c->index++;
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246

        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)
1247
                        return -ENXIO;
1248 1249 1250 1251

                nindex = c->index + 1 + l + 1;
        } else {
                if (c->enclosing != 0)
1252
                        return -ENXIO;
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 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289

                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)
1290
                return -ENXIO;
1291 1292 1293 1294 1295 1296 1297 1298 1299

        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)
1300
                        return -ENXIO;
1301 1302 1303

                nindex = c->index + 1 + l + 1;
        } else
1304
                return -ENXIO;
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320

        /* 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) {

1321
        struct bus_container *c, *w;
1322
        uint32_t *array_size = NULL;
1323
        char *signature;
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
        int r;

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

        /* Make sure we have space for one more container */
1334 1335
        w = realloc(m->containers, sizeof(struct bus_container) * (m->n_containers + 1));
        if (!w)
1336
                return -ENOMEM;
1337
        m->containers = w;
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361

        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 */
1362 1363 1364 1365 1366 1367
        w += m->n_containers++;
        w->enclosing = type;
        w->signature = signature;
        w->index = 0;
        w->array_size = array_size;
        w->begin = 0;
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383

        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)
1384
                if (c->signature && c->signature[c->index] != 0)
1385 1386 1387 1388 1389 1390 1391 1392
                        return -EINVAL;

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

        return 0;
}

1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432

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;
}

1433
int bus_message_append_ap(
1434 1435 1436 1437
                sd_bus_message *m,
                const char *types,
                va_list ap) {

1438 1439 1440
        unsigned n_array, n_struct;
        TypeStack stack[BUS_CONTAINER_DEPTH];
        unsigned stack_ptr = 0;
1441 1442 1443
        int r;

        assert(m);
1444 1445 1446

        if (!types)
                return 0;
1447

1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
        n_array = (unsigned) -1;
        n_struct = strlen(types);

        for (;;) {
                const char *t;

                if (n_array == 0 || (n_array == (unsigned) -1 && n_struct == 0)) {
                        r = type_stack_pop(stack, ELEMENTSOF(stack), &stack_ptr, &types, &n_struct, &n_array);
                        if (r < 0)
                                return r;
                        if (r == 0)
                                break;

                        r = sd_bus_message_close_container(m);
                        if (r < 0)
                                return r;

                        continue;
                }

                t = types;
                if (n_array != (unsigned) -1)
                        n_array --;
                else {
                        types ++;
                        n_struct--;
                }

1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
                switch (*t) {

                case SD_BUS_TYPE_BYTE: {
                        uint8_t x;

                        x = (uint8_t) va_arg(ap, int);
                        r = sd_bus_message_append_basic(m, *t, &x);
                        break;
                }

                case SD_BUS_TYPE_BOOLEAN:
                case SD_BUS_TYPE_INT32:
1488 1489
                case SD_BUS_TYPE_UINT32:
                case SD_BUS_TYPE_UNIX_FD: {
1490 1491
                        uint32_t x;

1492 1493 1494
                        /* We assume a boolean is the same as int32_t */
                        assert_cc(sizeof(int32_t) == sizeof(int));

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 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
                        x = va_arg(ap, uint32_t);
                        r = sd_bus_message_append_basic(m, *t, &x);
                        break;
                }

                case SD_BUS_TYPE_INT16:
                case SD_BUS_TYPE_UINT16: {
                        uint16_t x;

                        x = (uint16_t) va_arg(ap, int);
                        r = sd_bus_message_append_basic(m, *t, &x);
                        break;
                }

                case SD_BUS_TYPE_INT64:
                case SD_BUS_TYPE_UINT64:
                case SD_BUS_TYPE_DOUBLE: {
                        uint64_t x;

                        x = va_arg(ap, uint64_t);
                        r = sd_bus_message_append_basic(m, *t, &x);
                        break;
                }

                case SD_BUS_TYPE_STRING:
                case SD_BUS_TYPE_OBJECT_PATH:
                case SD_BUS_TYPE_SIGNATURE: {
                        const char *x;

                        x = va_arg(ap, const char*);
                        r = sd_bus_message_append_basic(m, *t, x);
                        break;
                }

                case SD_BUS_TYPE_ARRAY: {
                        size_t k;

                        r = signature_element_length(t + 1, &k);
                        if (r < 0)
                                return r;

                        {
                                char s[k + 1];
                                memcpy(s, t + 1, k);
                                s[k] = 0;

                                r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, s);
                                if (r < 0)
                                        return r;
1544