sd-bus.c 63.8 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 23 24 25 26 27 28 29 30 31
/*-*- 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 <endian.h>
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/poll.h>
#include <byteswap.h>

#include "util.h"
#include "macro.h"
32
#include "strv.h"
33
#include "set.h"
34 35 36 37 38

#include "sd-bus.h"
#include "bus-internal.h"
#include "bus-message.h"
#include "bus-type.h"
39
#include "bus-socket.h"
40
#include "bus-kernel.h"
41
#include "bus-control.h"
42

43 44
static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec);

45 46
static void bus_free(sd_bus *b) {
        struct filter_callback *f;
47
        struct object_callback *c;
48
        unsigned i;
49 50 51

        assert(b);

52
        sd_bus_close(b);
53 54

        free(b->rbuffer);
55
        free(b->unique_name);
56
        free(b->auth_buffer);
57
        free(b->address);
58
        free(b->kernel);
59

60 61 62
        free(b->exec_path);
        strv_free(b->exec_argv);

63 64 65
        close_many(b->fds, b->n_fds);
        free(b->fds);

66 67
        for (i = 0; i < b->rqueue_size; i++)
                sd_bus_message_unref(b->rqueue[i]);
68
        free(b->rqueue);
69 70 71

        for (i = 0; i < b->wqueue_size; i++)
                sd_bus_message_unref(b->wqueue[i]);
72 73 74
        free(b->wqueue);

        hashmap_free_free(b->reply_callbacks);
75
        prioq_free(b->reply_callbacks_prioq);
76 77 78 79 80 81

        while ((f = b->filter_callbacks)) {
                LIST_REMOVE(struct filter_callback, callbacks, b->filter_callbacks, f);
                free(f);
        }

82 83 84 85 86 87 88
        while ((c = hashmap_steal_first(b->object_callbacks))) {
                free(c->path);
                free(c);
        }

        hashmap_free(b->object_callbacks);

89 90
        bus_match_free(&b->match_callbacks);

91 92 93
        free(b);
}

94
int sd_bus_new(sd_bus **ret) {
95 96
        sd_bus *r;

97 98 99
        if (!ret)
                return -EINVAL;

100 101
        r = new0(sd_bus, 1);
        if (!r)
102
                return -ENOMEM;
103 104

        r->n_ref = 1;
105
        r->input_fd = r->output_fd = -1;
106
        r->message_version = 1;
107
        r->negotiate_fds = true;
108 109 110 111 112 113

        /* We guarantee that wqueue always has space for at least one
         * entry */
        r->wqueue = new(sd_bus_message*, 1);
        if (!r->wqueue) {
                free(r);
114
                return -ENOMEM;
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
        *ret = r;
        return 0;
}

int sd_bus_set_address(sd_bus *bus, const char *address) {
        char *a;

        if (!bus)
                return -EINVAL;
        if (bus->state != BUS_UNSET)
                return -EPERM;
        if (!address)
                return -EINVAL;

        a = strdup(address);
        if (!a)
                return -ENOMEM;

        free(bus->address);
        bus->address = a;

        return 0;
}

141
int sd_bus_set_fd(sd_bus *bus, int input_fd, int output_fd) {
142 143 144 145
        if (!bus)
                return -EINVAL;
        if (bus->state != BUS_UNSET)
                return -EPERM;
146 147 148
        if (input_fd < 0)
                return -EINVAL;
        if (output_fd < 0)
149 150
                return -EINVAL;

151 152
        bus->input_fd = input_fd;
        bus->output_fd = output_fd;
153 154 155
        return 0;
}

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
int sd_bus_set_exec(sd_bus *bus, const char *path, char *const argv[]) {
        char *p, **a;

        if (!bus)
                return -EINVAL;
        if (bus->state != BUS_UNSET)
                return -EPERM;
        if (!path)
                return -EINVAL;
        if (strv_isempty(argv))
                return -EINVAL;

        p = strdup(path);
        if (!p)
                return -ENOMEM;

        a = strv_copy(argv);
        if (!a) {
                free(p);
                return -ENOMEM;
        }

        free(bus->exec_path);
        strv_free(bus->exec_argv);

        bus->exec_path = p;
        bus->exec_argv = a;

        return 0;
}

187
int sd_bus_set_bus_client(sd_bus *bus, int b) {
188 189 190 191 192
        if (!bus)
                return -EINVAL;
        if (bus->state != BUS_UNSET)
                return -EPERM;

193
        bus->bus_client = !!b;
194 195 196 197 198 199 200 201 202 203 204 205
        return 0;
}

int sd_bus_set_negotiate_fds(sd_bus *bus, int b) {
        if (!bus)
                return -EINVAL;
        if (bus->state != BUS_UNSET)
                return -EPERM;

        bus->negotiate_fds = !!b;
        return 0;
}
206

207
int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t server_id) {
208 209
        if (!bus)
                return -EINVAL;
210
        if (!b && !sd_id128_equal(server_id, SD_ID128_NULL))
211 212 213 214 215
                return -EINVAL;
        if (bus->state != BUS_UNSET)
                return -EPERM;

        bus->is_server = !!b;
216
        bus->server_id = server_id;
217 218 219 220 221 222 223 224 225 226 227 228 229
        return 0;
}

int sd_bus_set_anonymous(sd_bus *bus, int b) {
        if (!bus)
                return -EINVAL;
        if (bus->state != BUS_UNSET)
                return -EPERM;

        bus->anonymous_auth = !!b;
        return 0;
}

230
static int hello_callback(sd_bus *bus, int error, sd_bus_message *reply, void *userdata) {
231 232 233 234
        const char *s;
        int r;

        assert(bus);
235
        assert(bus->state == BUS_HELLO);
236 237 238 239

        if (error != 0)
                return -error;

240 241 242 243 244 245
        assert(reply);

        r = sd_bus_message_read(reply, "s", &s);
        if (r < 0)
                return r;

246 247 248
        if (!service_name_is_valid(s) || s[0] != ':')
                return -EBADMSG;

249 250 251 252
        bus->unique_name = strdup(s);
        if (!bus->unique_name)
                return -ENOMEM;

253 254
        bus->state = BUS_RUNNING;

255 256 257 258 259 260 261 262 263
        return 1;
}

static int bus_send_hello(sd_bus *bus) {
        _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
        int r;

        assert(bus);

264
        if (!bus->bus_client || bus->is_kernel)
265 266
                return 0;

267 268 269 270 271 272 273 274 275 276
        r = sd_bus_message_new_method_call(
                        bus,
                        "org.freedesktop.DBus",
                        "/",
                        "org.freedesktop.DBus",
                        "Hello",
                        &m);
        if (r < 0)
                return r;

277
        return sd_bus_send_with_reply(bus, m, hello_callback, NULL, 0, &bus->hello_serial);
278 279
}

280
int bus_start_running(sd_bus *bus) {
281 282
        assert(bus);

283
        if (bus->bus_client && !bus->is_kernel) {
284
                bus->state = BUS_HELLO;
285
                return 1;
286 287 288
        }

        bus->state = BUS_RUNNING;
289
        return 1;
290 291 292 293 294 295 296 297 298 299 300
}

static int parse_address_key(const char **p, const char *key, char **value) {
        size_t l, n = 0;
        const char *a;
        char *r = NULL;

        assert(p);
        assert(*p);
        assert(value);

301 302 303 304
        if (key) {
                l = strlen(key);
                if (strncmp(*p, key, l) != 0)
                        return 0;
305

306 307
                if ((*p)[l] != '=')
                        return 0;
308

309 310
                if (*value)
                        return -EINVAL;
311

312 313 314 315 316
                a = *p + l + 1;
        } else
                a = *p;

        while (*a != ';' && *a != ',' && *a != 0) {
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
                char c, *t;

                if (*a == '%') {
                        int x, y;

                        x = unhexchar(a[1]);
                        if (x < 0) {
                                free(r);
                                return x;
                        }

                        y = unhexchar(a[2]);
                        if (y < 0) {
                                free(r);
                                return y;
                        }

                        c = (char) ((x << 4) | y);
335 336
                        a += 3;
                } else {
337
                        c = *a;
338 339
                        a++;
                }
340

341
                t = realloc(r, n + 2);
342 343 344 345 346 347 348 349 350
                if (!t) {
                        free(r);
                        return -ENOMEM;
                }

                r = t;
                r[n++] = c;
        }

351 352 353 354 355 356 357 358 359 360
        if (!r) {
                r = strdup("");
                if (!r)
                        return -ENOMEM;
        } else
                r[n] = 0;

        if (*a == ',')
                a++;

361
        *p = a;
362 363

        free(*value);
364
        *value = r;
365

366 367 368 369 370 371 372
        return 1;
}

static void skip_address_key(const char **p) {
        assert(p);
        assert(*p);

373 374 375 376
        *p += strcspn(*p, ",");

        if (**p == ',')
                (*p) ++;
377 378
}

379 380 381
static int parse_unix_address(sd_bus *b, const char **p, char **guid) {
        _cleanup_free_ char *path = NULL, *abstract = NULL;
        size_t l;
382 383 384
        int r;

        assert(b);
385 386 387
        assert(p);
        assert(*p);
        assert(guid);
388

389 390 391 392 393 394
        while (**p != 0 && **p != ';') {
                r = parse_address_key(p, "guid", guid);
                if (r < 0)
                        return r;
                else if (r > 0)
                        continue;
395

396 397 398 399 400
                r = parse_address_key(p, "path", &path);
                if (r < 0)
                        return r;
                else if (r > 0)
                        continue;
401

402 403 404 405 406
                r = parse_address_key(p, "abstract", &abstract);
                if (r < 0)
                        return r;
                else if (r > 0)
                        continue;
407

408 409
                skip_address_key(p);
        }
410

411 412
        if (!path && !abstract)
                return -EINVAL;
413

414 415 416 417 418 419 420
        if (path && abstract)
                return -EINVAL;

        if (path) {
                l = strlen(path);
                if (l > sizeof(b->sockaddr.un.sun_path))
                        return -E2BIG;
421

422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
                b->sockaddr.un.sun_family = AF_UNIX;
                strncpy(b->sockaddr.un.sun_path, path, sizeof(b->sockaddr.un.sun_path));
                b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l;
        } else if (abstract) {
                l = strlen(abstract);
                if (l > sizeof(b->sockaddr.un.sun_path) - 1)
                        return -E2BIG;

                b->sockaddr.un.sun_family = AF_UNIX;
                b->sockaddr.un.sun_path[0] = 0;
                strncpy(b->sockaddr.un.sun_path+1, abstract, sizeof(b->sockaddr.un.sun_path)-1);
                b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + 1 + l;
        }

        return 0;
}

static int parse_tcp_address(sd_bus *b, const char **p, char **guid) {
        _cleanup_free_ char *host = NULL, *port = NULL, *family = NULL;
        int r;
442 443 444 445
        struct addrinfo *result, hints = {
                .ai_socktype = SOCK_STREAM,
                .ai_flags = AI_ADDRCONFIG,
        };
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534

        assert(b);
        assert(p);
        assert(*p);
        assert(guid);

        while (**p != 0 && **p != ';') {
                r = parse_address_key(p, "guid", guid);
                if (r < 0)
                        return r;
                else if (r > 0)
                        continue;

                r = parse_address_key(p, "host", &host);
                if (r < 0)
                        return r;
                else if (r > 0)
                        continue;

                r = parse_address_key(p, "port", &port);
                if (r < 0)
                        return r;
                else if (r > 0)
                        continue;

                r = parse_address_key(p, "family", &family);
                if (r < 0)
                        return r;
                else if (r > 0)
                        continue;

                skip_address_key(p);
        }

        if (!host || !port)
                return -EINVAL;

        if (family) {
                if (streq(family, "ipv4"))
                        hints.ai_family = AF_INET;
                else if (streq(family, "ipv6"))
                        hints.ai_family = AF_INET6;
                else
                        return -EINVAL;
        }

        r = getaddrinfo(host, port, &hints, &result);
        if (r == EAI_SYSTEM)
                return -errno;
        else if (r != 0)
                return -EADDRNOTAVAIL;

        memcpy(&b->sockaddr, result->ai_addr, result->ai_addrlen);
        b->sockaddr_size = result->ai_addrlen;

        freeaddrinfo(result);

        return 0;
}

static int parse_exec_address(sd_bus *b, const char **p, char **guid) {
        char *path = NULL;
        unsigned n_argv = 0, j;
        char **argv = NULL;
        int r;

        assert(b);
        assert(p);
        assert(*p);
        assert(guid);

        while (**p != 0 && **p != ';') {
                r = parse_address_key(p, "guid", guid);
                if (r < 0)
                        goto fail;
                else if (r > 0)
                        continue;

                r = parse_address_key(p, "path", &path);
                if (r < 0)
                        goto fail;
                else if (r > 0)
                        continue;

                if (startswith(*p, "argv")) {
                        unsigned ul;

                        errno = 0;
                        ul = strtoul(*p + 4, (char**) p, 10);
535
                        if (errno > 0 || **p != '=' || ul > 256) {
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
                                r = -EINVAL;
                                goto fail;
                        }

                        (*p) ++;

                        if (ul >= n_argv) {
                                char **x;

                                x = realloc(argv, sizeof(char*) * (ul + 2));
                                if (!x) {
                                        r = -ENOMEM;
                                        goto fail;
                                }

                                memset(x + n_argv, 0, sizeof(char*) * (ul - n_argv + 2));

                                argv = x;
                                n_argv = ul + 1;
                        }

                        r = parse_address_key(p, NULL, argv + ul);
558
                        if (r < 0)
559
                                goto fail;
560

561
                        continue;
562 563
                }

564 565
                skip_address_key(p);
        }
566

567 568
        if (!path) {
                r = -EINVAL;
569
                goto fail;
570
        }
571

572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
        /* Make sure there are no holes in the array, with the
         * exception of argv[0] */
        for (j = 1; j < n_argv; j++)
                if (!argv[j]) {
                        r = -EINVAL;
                        goto fail;
                }

        if (argv && argv[0] == NULL) {
                argv[0] = strdup(path);
                if (!argv[0]) {
                        r = -ENOMEM;
                        goto fail;
                }
        }
587

588 589 590
        b->exec_path = path;
        b->exec_argv = argv;
        return 0;
591

592 593 594 595 596 597 598 599 600
fail:
        for (j = 0; j < n_argv; j++)
                free(argv[j]);

        free(argv);
        free(path);
        return r;
}

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
static int parse_kernel_address(sd_bus *b, const char **p, char **guid) {
        _cleanup_free_ char *path = NULL;
        int r;

        assert(b);
        assert(p);
        assert(*p);
        assert(guid);

        while (**p != 0 && **p != ';') {
                r = parse_address_key(p, "guid", guid);
                if (r < 0)
                        return r;
                else if (r > 0)
                        continue;

                r = parse_address_key(p, "path", &path);
                if (r < 0)
                        return r;
                else if (r > 0)
                        continue;

                skip_address_key(p);
        }

        if (!path)
                return -EINVAL;

        free(b->kernel);
        b->kernel = path;
        path = NULL;

        return 0;
}

636 637 638 639 640 641 642 643 644
static void bus_reset_parsed_address(sd_bus *b) {
        assert(b);

        zero(b->sockaddr);
        b->sockaddr_size = 0;
        strv_free(b->exec_argv);
        free(b->exec_path);
        b->exec_path = NULL;
        b->exec_argv = NULL;
645
        b->server_id = SD_ID128_NULL;
646 647
        free(b->kernel);
        b->kernel = NULL;
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
}

static int bus_parse_next_address(sd_bus *b) {
        _cleanup_free_ char *guid = NULL;
        const char *a;
        int r;

        assert(b);

        if (!b->address)
                return 0;
        if (b->address[b->address_index] == 0)
                return 0;

        bus_reset_parsed_address(b);

        a = b->address + b->address_index;
665

666
        while (*a != 0) {
667

668 669 670
                if (*a == ';') {
                        a++;
                        continue;
671 672
                }

673 674
                if (startswith(a, "unix:")) {
                        a += 5;
675

676
                        r = parse_unix_address(b, &a, &guid);
677 678
                        if (r < 0)
                                return r;
679
                        break;
680

681
                } else if (startswith(a, "tcp:")) {
682

683 684
                        a += 4;
                        r = parse_tcp_address(b, &a, &guid);
685 686 687
                        if (r < 0)
                                return r;

688 689 690 691 692 693
                        break;

                } else if (startswith(a, "unixexec:")) {

                        a += 9;
                        r = parse_exec_address(b, &a, &guid);
694 695 696
                        if (r < 0)
                                return r;

697
                        break;
698

699 700 701 702 703 704 705 706
                } else if (startswith(a, "kernel:")) {

                        a += 7;
                        r = parse_kernel_address(b, &a, &guid);
                        if (r < 0)
                                return r;

                        break;
707 708
                }

709 710 711
                a = strchr(a, ';');
                if (!a)
                        return 0;
712 713 714
        }

        if (guid) {
715
                r = sd_id128_from_string(guid, &b->server_id);
716 717 718 719
                if (r < 0)
                        return r;
        }

720
        b->address_index = a - b->address;
721 722 723
        return 1;
}

724
static int bus_start_address(sd_bus *b) {
725 726 727 728 729
        int r;

        assert(b);

        for (;;) {
730
                sd_bus_close(b);
731 732

                if (b->sockaddr.sa.sa_family != AF_UNSPEC) {
733 734

                        r = bus_socket_connect(b);
735 736 737 738 739 740 741
                        if (r >= 0)
                                return r;

                        b->last_connect_error = -r;

                } else if (b->exec_path) {

742
                        r = bus_socket_exec(b);
743 744 745
                        if (r >= 0)
                                return r;

746 747 748 749 750 751 752
                        b->last_connect_error = -r;
                } else if (b->kernel) {

                        r = bus_kernel_connect(b);
                        if (r >= 0)
                                return r;

753 754 755 756 757 758 759 760
                        b->last_connect_error = -r;
                }

                r = bus_parse_next_address(b);
                if (r < 0)
                        return r;
                if (r == 0)
                        return b->last_connect_error ? -b->last_connect_error : -ECONNREFUSED;
761 762 763
        }
}

764 765 766 767 768 769 770
int bus_next_address(sd_bus *b) {
        assert(b);

        bus_reset_parsed_address(b);
        return bus_start_address(b);
}

771
static int bus_start_fd(sd_bus *b) {
772
        struct stat st;
773 774 775
        int r;

        assert(b);
776 777
        assert(b->input_fd >= 0);
        assert(b->output_fd >= 0);
778

779
        r = fd_nonblock(b->input_fd, true);
780 781 782
        if (r < 0)
                return r;

783
        r = fd_cloexec(b->input_fd, true);
784 785 786
        if (r < 0)
                return r;

787 788 789 790 791 792 793 794 795 796
        if (b->input_fd != b->output_fd) {
                r = fd_nonblock(b->output_fd, true);
                if (r < 0)
                        return r;

                r = fd_cloexec(b->output_fd, true);
                if (r < 0)
                        return r;
        }

797 798 799 800 801 802 803
        if (fstat(b->input_fd, &st) < 0)
                return -errno;

        if (S_ISCHR(b->input_fd))
                return bus_kernel_take_fd(b);
        else
                return bus_socket_take_fd(b);
804 805 806 807 808 809 810 811 812 813 814 815
}

int sd_bus_start(sd_bus *bus) {
        int r;

        if (!bus)
                return -EINVAL;
        if (bus->state != BUS_UNSET)
                return -EPERM;

        bus->state = BUS_OPENING;

816 817 818
        if (bus->is_server && bus->bus_client)
                return -EINVAL;

819
        if (bus->input_fd >= 0)
820
                r = bus_start_fd(bus);
821
        else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path || bus->kernel)
822
                r = bus_start_address(bus);
823 824 825 826 827 828 829 830 831
        else
                return -EINVAL;

        if (r < 0)
                return r;

        return bus_send_hello(bus);
}

832 833 834 835 836 837 838 839
int sd_bus_open_system(sd_bus **ret) {
        const char *e;
        sd_bus *b;
        int r;

        if (!ret)
                return -EINVAL;

840 841 842 843
        r = sd_bus_new(&b);
        if (r < 0)
                return r;

844 845
        e = getenv("DBUS_SYSTEM_BUS_ADDRESS");
        if (e) {
846
                r = sd_bus_set_address(b, e);
847
                if (r < 0)
848
                        goto fail;
849 850 851 852 853
        } else {
                b->sockaddr.un.sun_family = AF_UNIX;
                strncpy(b->sockaddr.un.sun_path, "/run/dbus/system_bus_socket", sizeof(b->sockaddr.un.sun_path));
                b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + sizeof("/run/dbus/system_bus_socket") - 1;
        }
854

855
        b->bus_client = true;
856 857 858 859

        r = sd_bus_start(b);
        if (r < 0)
                goto fail;
860 861 862

        *ret = b;
        return 0;
863 864 865 866

fail:
        bus_free(b);
        return r;
867 868 869 870 871 872 873 874 875 876 877
}

int sd_bus_open_user(sd_bus **ret) {
        const char *e;
        sd_bus *b;
        size_t l;
        int r;

        if (!ret)
                return -EINVAL;

878 879 880 881
        r = sd_bus_new(&b);
        if (r < 0)
                return r;

882 883
        e = getenv("DBUS_SESSION_BUS_ADDRESS");
        if (e) {
884
                r = sd_bus_set_address(b, e);
885
                if (r < 0)
886
                        goto fail;
887 888
        } else {
                e = getenv("XDG_RUNTIME_DIR");
889 890 891 892
                if (!e) {
                        r = -ENOENT;
                        goto fail;
                }
893

894
                l = strlen(e);
895 896 897 898
                if (l + 4 > sizeof(b->sockaddr.un.sun_path)) {
                        r = -E2BIG;
                        goto fail;
                }
899

900 901 902
                b->sockaddr.un.sun_family = AF_UNIX;
                memcpy(mempcpy(b->sockaddr.un.sun_path, e, l), "/bus", 4);
                b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l + 4;
903 904
        }

905
        b->bus_client = true;
906

907
        r = sd_bus_start(b);
908 909
        if (r < 0)
                goto fail;
910 911 912

        *ret = b;
        return 0;
913 914

fail:
915
        bus_free(b);
916
        return r;
917 918 919 920 921 922
}

void sd_bus_close(sd_bus *bus) {
        if (!bus)
                return;

923 924 925 926 927 928
        if (bus->input_fd >= 0)
                close_nointr_nofail(bus->input_fd);
        if (bus->output_fd >= 0 && bus->output_fd != bus->input_fd)
                close_nointr_nofail(bus->output_fd);

        bus->input_fd = bus->output_fd = -1;
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
}

sd_bus *sd_bus_ref(sd_bus *bus) {
        if (!bus)
                return NULL;

        assert(bus->n_ref > 0);

        bus->n_ref++;
        return bus;
}

sd_bus *sd_bus_unref(sd_bus *bus) {
        if (!bus)
                return NULL;

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

        if (bus->n_ref <= 0)
                bus_free(bus);

        return NULL;
}

954 955 956 957
int sd_bus_is_open(sd_bus *bus) {
        if (!bus)
                return -EINVAL;

958
        return bus->state != BUS_UNSET && bus->input_fd >= 0;
959 960
}

961 962 963
int sd_bus_can_send(sd_bus *bus, char type) {
        int r;

964 965
        if (!bus)
                return -EINVAL;
966
        if (bus->output_fd < 0)
967
                return -ENOTCONN;
968

969
        if (type == SD_BUS_TYPE_UNIX_FD) {
970 971 972
                if (!bus->negotiate_fds)
                        return 0;

973
                r = bus_ensure_running(bus);
974 975
                if (r < 0)
                        return r;
976

977 978 979 980
                return bus->can_fds;
        }

        return bus_type_is_valid(type);
981 982
}

983
int sd_bus_get_server_id(sd_bus *bus, sd_id128_t *server_id) {
984
        int r;
985 986 987

        if (!bus)
                return -EINVAL;
988
        if (!server_id)
989
                return -EINVAL;
990

991
        r = bus_ensure_running(bus);
992 993
        if (r < 0)
                return r;
994

995
        *server_id = bus->server_id;
996
        return 0;
997 998 999 1000 1001
}

static int bus_seal_message(sd_bus *b, sd_bus_message *m) {
        assert(m);

1002 1003 1004
        if (m->header->version > b->message_version)
                return -EPERM;

1005 1006 1007
        if (m->sealed)
                return 0;

1008
        return bus_message_seal(m, ++b->serial);
1009 1010 1011
}

static int dispatch_wqueue(sd_bus *bus) {
1012
        int r, ret = 0;
1013 1014

        assert(bus);
1015
        assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1016

1017
        if (bus->output_fd < 0)
1018 1019 1020 1021
                return -ENOTCONN;

        while (bus->wqueue_size > 0) {

1022 1023 1024 1025 1026
                if (bus->is_kernel)
                        r = bus_kernel_write_message(bus, bus->wqueue[0]);
                else
                        r = bus_socket_write_message(bus, bus->wqueue[0], &bus->windex);

1027 1028 1029 1030
                if (r < 0) {
                        sd_bus_close(bus);
                        return r;
                } else if (r == 0)
1031 1032
                        /* Didn't do anything this time */
                        return ret;
1033
                else if (bus->is_kernel || bus->windex >= BUS_MESSAGE_SIZE(bus->wqueue[0])) {
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
                        /* Fully written. Let's drop the entry from
                         * the queue.
                         *
                         * This isn't particularly optimized, but
                         * well, this is supposed to be our worst-case
                         * buffer only, and the socket buffer is
                         * supposed to be our primary buffer, and if
                         * it got full, then all bets are off
                         * anyway. */

                        sd_bus_message_unref(bus->wqueue[0]);
                        bus->wqueue_size --;
                        memmove(bus->wqueue, bus->wqueue + 1, sizeof(sd_bus_message*) * bus->wqueue_size);
                        bus->windex = 0;

1049
                        ret = 1;
1050 1051 1052
                }
        }

1053
        return ret;
1054 1055 1056
}

static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
1057
        sd_bus_message *z = NULL;
1058
        int r, ret = 0;
1059 1060 1061

        assert(bus);
        assert(m);
1062
        assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1063

1064
        if (bus->input_fd < 0)
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
                return -ENOTCONN;

        if (bus->rqueue_size > 0) {
                /* Dispatch a queued message */

                *m = bus->rqueue[0];
                bus->rqueue_size --;
                memmove(bus->rqueue, bus->rqueue + 1, sizeof(sd_bus_message*) * bus->rqueue_size);
                return 1;
        }

        /* Try to read a new message */
1077
        do {
1078 1079 1080 1081 1082
                if (bus->is_kernel)
                        r = bus_kernel_read_message(bus, &z);
                else
                        r = bus_socket_read_message(bus, &z);

1083 1084 1085 1086 1087 1088
                if (r < 0) {
                        sd_bus_close(bus);
                        return r;
                }
                if (r == 0)
                        return ret;
1089

1090 1091 1092 1093 1094
                r = 1;
        } while (!z);

        *m = z;
        return 1;
1095 1096 1097 1098 1099 1100 1101
}

int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
        int r;

        if (!bus)
                return -EINVAL;
1102 1103
        if (bus->state == BUS_UNSET)
                return -ENOTCONN;
1104
        if (bus->output_fd < 0)
1105 1106 1107
                return -ENOTCONN;
        if (!m)
                return -EINVAL;
1108 1109 1110 1111 1112 1113 1114 1115

        if (m->n_fds > 0) {
                r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD);
                if (r < 0)
                        return r;
                if (r == 0)
                        return -ENOTSUP;
        }
1116

1117 1118 1119 1120 1121
        /* If the serial number isn't kept, then we know that no reply
         * is expected */
        if (!serial && !m->sealed)
                m->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;