HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/libsystemd/sd-bus/sd-bus.c

140,786 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 1a6eed2baad85e39cff35c47f1771ee148f511c6

Lines 1–1000 of 4,512

/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
#include <netdb.h>
#include <poll.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <threads.h>
#include <unistd.h>
 
#include "sd-bus.h"
#include "sd-event.h"
#include "sd-future.h"
 
#include "af-list.h"
#include "alloc-util.h"
#include "bus-container.h"
#include "bus-control.h"
#include "bus-error.h"
#include "bus-future.h"
#include "bus-internal.h"
#include "bus-kernel.h"
#include "bus-label.h"
#include "bus-message.h"
#include "bus-objects.h"
#include "bus-protocol.h"
#include "bus-slot.h"
#include "bus-socket.h"
#include "bus-track.h"
#include "bus-type.h"
#include "cgroup-util.h"
#include "errno-util.h"
#include "fd-util.h"
#include "format-util.h"
#include "glyph-util.h"
#include "hexdecoct.h"
#include "hostname-util.h"
#include "io-util.h"
#include "log.h"
#include "log-context.h"
#include "memory-util.h"
#include "origin-id.h"
#include "parse-util.h"
#include "path-util.h"
#include "prioq.h"
#include "set.h"
#include "string-util.h"
#include "strv.h"
#include "time-util.h"
#include "user-util.h"
 
#define log_debug_bus_message(m)                                         \
        do {                                                             \
                sd_bus_message *_mm = (m);                               \
                log_debug("Got message type=%s sender=%s destination=%s path=%s interface=%s member=%s " \
                          " cookie=%" PRIu64 " reply_cookie=%" PRIu64   \
                          " signature=%s error-name=%s error-message=%s", \
                          strna(bus_message_type_to_string(_mm->header->type)), \
                          strna(sd_bus_message_get_sender(_mm)),         \
                          strna(sd_bus_message_get_destination(_mm)),    \
                          strna(sd_bus_message_get_path(_mm)),           \
                          strna(sd_bus_message_get_interface(_mm)),      \
                          strna(sd_bus_message_get_member(_mm)),         \
                          BUS_MESSAGE_COOKIE(_mm),                       \
                          _mm->reply_cookie,                             \
                          strna(_mm->root_container.signature),          \
                          strna(_mm->error.name),                        \
                          strna(_mm->error.message));                    \
        } while (false)
 
static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec);
static void bus_detach_io_events(sd_bus *b);
 
static thread_local sd_bus *default_system_bus = NULL;
static thread_local sd_bus *default_user_bus = NULL;
static thread_local sd_bus *default_starter_bus = NULL;
 
static sd_bus** bus_choose_default(int (**bus_open)(sd_bus **)) {
        const char *e;
 
        /* Let's try our best to reuse another cached connection. If
         * the starter bus type is set, connect via our normal
         * connection logic, ignoring $DBUS_STARTER_ADDRESS, so that
         * we can share the connection with the user/system default
         * bus. */
 
        e = secure_getenv("DBUS_STARTER_BUS_TYPE");
        if (e) {
                if (streq(e, "system")) {
                        if (bus_open)
                                *bus_open = sd_bus_open_system;
                        return &default_system_bus;
                } else if (STR_IN_SET(e, "user", "session")) {
                        if (bus_open)
                                *bus_open = sd_bus_open_user;
                        return &default_user_bus;
                }
        }
 
        /* No type is specified, so we have not other option than to
         * use the starter address if it is set. */
        e = secure_getenv("DBUS_STARTER_ADDRESS");
        if (e) {
                if (bus_open)
                        *bus_open = sd_bus_open;
                return &default_starter_bus;
        }
 
        /* Finally, if nothing is set use the cached connection for
         * the right scope */
 
        if (cg_pid_get_owner_uid(0, NULL) >= 0) {
                if (bus_open)
                        *bus_open = sd_bus_open_user;
                return &default_user_bus;
        } else {
                if (bus_open)
                        *bus_open = sd_bus_open_system;
                return &default_system_bus;
        }
}
 
sd_bus *bus_resolve(sd_bus *bus) {
        switch ((uintptr_t) bus) {
        case (uintptr_t) SD_BUS_DEFAULT:
                return *(bus_choose_default(NULL));
        case (uintptr_t) SD_BUS_DEFAULT_USER:
                return default_user_bus;
        case (uintptr_t) SD_BUS_DEFAULT_SYSTEM:
                return default_system_bus;
        default:
                return bus;
        }
}
 
void bus_close_io_fds(sd_bus *b) {
        assert(b);
 
        bus_detach_io_events(b);
 
        if (b->input_fd != b->output_fd)
                safe_close(b->output_fd);
        b->output_fd = b->input_fd = safe_close(b->input_fd);
}
 
void bus_close_inotify_fd(sd_bus *b) {
        assert(b);
 
        b->inotify_event_source = sd_event_source_disable_unref(b->inotify_event_source);
 
        b->inotify_fd = safe_close(b->inotify_fd);
        b->inotify_watches = mfree(b->inotify_watches);
        b->n_inotify_watches = 0;
}
 
static void bus_close_fds(sd_bus *b) {
        assert(b);
 
        bus_close_io_fds(b);
        bus_close_inotify_fd(b);
        b->pidfd = safe_close(b->pidfd);
}
 
static void bus_reset_queues(sd_bus *b) {
        assert(b);
 
        while (b->rqueue_size > 0)
                bus_message_unref_queued(b->rqueue[--b->rqueue_size], b);
 
        b->rqueue = mfree(b->rqueue);
 
        while (b->wqueue_size > 0)
                bus_message_unref_queued(b->wqueue[--b->wqueue_size], b);
 
        b->wqueue = mfree(b->wqueue);
}
 
static sd_bus* bus_free(sd_bus *b) {
        sd_bus_slot *s;
 
        assert(b);
        assert(!b->track_queue);
        assert(!b->tracks);
 
        b->state = BUS_CLOSED;
 
        sd_bus_detach_event(b);
 
        while ((s = b->slots)) {
                /* At this point only floating slots can still be
                 * around, because the non-floating ones keep a
                 * reference to the bus, and we thus couldn't be
                 * destructing right now... We forcibly disconnect the
                 * slots here, so that they still can be referenced by
                 * apps, but are dead. */
 
                assert(s->floating);
                bus_slot_disconnect(s, true);
        }
 
        if (b->default_bus_ptr)
                *b->default_bus_ptr = NULL;
 
        bus_close_fds(b);
 
        free(b->label);
        free(b->groups);
        free(b->rbuffer);
        free(b->unique_name);
        free(b->auth_buffer);
        free(b->address);
        free(b->machine);
        free(b->description);
        free(b->patch_sender);
 
        free(b->exec_path);
        strv_free(b->exec_argv);
 
        close_many(b->fds, b->n_fds);
        free(b->fds);
 
        bus_reset_queues(b);
 
        ordered_hashmap_free(b->reply_callbacks);
        prioq_free(b->reply_callbacks_prioq);
 
        /* Outstanding fiber handlers pin the bus via their BusFiberData ref, so by the time refcount
         * reaches zero and bus_free() runs, every fiber has already resolved and removed itself from
         * this set. */
        assert(set_isempty(b->fiber_futures));
        set_free(b->fiber_futures);
 
        assert(b->match_callbacks.type == BUS_MATCH_ROOT);
        bus_match_free(&b->match_callbacks);
 
        set_free(b->vtable_methods);
        set_free(b->vtable_properties);
 
        assert(hashmap_isempty(b->nodes));
        hashmap_free(b->nodes);
 
        bus_flush_memfd(b);
 
        assert_se(pthread_mutex_destroy(&b->memfd_cache_mutex) == 0);
 
        return mfree(b);
}
 
DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus*, bus_free);
 
DEFINE_ORIGIN_ID_HELPERS(sd_bus, bus);
 
_public_ int sd_bus_new(sd_bus **ret) {
        _cleanup_free_ sd_bus *b = NULL;
 
        assert_return(ret, -EINVAL);
 
        b = new(sd_bus, 1);
        if (!b)
                return -ENOMEM;
 
        *b = (sd_bus) {
                .n_ref = 1,
                .input_fd = -EBADF,
                .output_fd = -EBADF,
                .inotify_fd = -EBADF,
                .exit_code = EXIT_FAILURE,
                .message_version = 1,
                .creds_mask = SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME,
                .accept_fd = true,
                .origin_id = origin_id_query(),
                .busexec_pidref = PIDREF_NULL,
                .n_groups = SIZE_MAX,
                .close_on_exit = true,
                .ucred = UCRED_INVALID,
                .pidfd = -EBADF,
                .runtime_scope = _RUNTIME_SCOPE_INVALID,
                .connect_as_uid = UID_INVALID,
                .connect_as_gid = GID_INVALID,
        };
 
        /* We guarantee that wqueue always has space for at least one entry */
        if (!GREEDY_REALLOC(b->wqueue, 1))
                return -ENOMEM;
 
        assert_se(pthread_mutex_init(&b->memfd_cache_mutex, NULL) == 0);
 
        *ret = TAKE_PTR(b);
        return 0;
}
 
_public_ int sd_bus_set_address(sd_bus *bus, const char *address) {
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(bus->state == BUS_UNSET, -EPERM);
        assert_return(address, -EINVAL);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        return free_and_strdup(&bus->address, address);
}
 
_public_ int sd_bus_set_fd(sd_bus *bus, int input_fd, int output_fd) {
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(bus->state == BUS_UNSET, -EPERM);
        assert_return(input_fd >= 0, -EBADF);
        assert_return(output_fd >= 0, -EBADF);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        bus->input_fd = input_fd;
        bus->output_fd = output_fd;
        return 0;
}
 
_public_ int sd_bus_set_exec(sd_bus *bus, const char *path, char *const *argv) {
        _cleanup_strv_free_ char **a = NULL;
        int r;
 
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(bus->state == BUS_UNSET, -EPERM);
        assert_return(path, -EINVAL);
        assert_return(!strv_isempty(argv), -EINVAL);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        a = strv_copy(argv);
        if (!a)
                return -ENOMEM;
 
        r = free_and_strdup(&bus->exec_path, path);
        if (r < 0)
                return r;
 
        return strv_free_and_replace(bus->exec_argv, a);
}
 
_public_ int sd_bus_set_bus_client(sd_bus *bus, int b) {
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(bus->state == BUS_UNSET, -EPERM);
        assert_return(!bus->patch_sender, -EPERM);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        bus->bus_client = b;
        return 0;
}
 
_public_ int sd_bus_set_monitor(sd_bus *bus, int b) {
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(bus->state == BUS_UNSET, -EPERM);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        bus->is_monitor = b;
        return 0;
}
 
_public_ int sd_bus_negotiate_fds(sd_bus *bus, int b) {
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(bus->state == BUS_UNSET, -EPERM);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        bus->accept_fd = b;
        return 0;
}
 
_public_ int sd_bus_negotiate_timestamp(sd_bus *bus, int b) {
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        if (IN_SET(bus->state, BUS_CLOSING, BUS_CLOSED))
                return -ENOTCONN;
 
        /* This is not actually supported by any of our transports these days, but we do honour it for synthetic
         * replies, and maybe one day classic D-Bus learns this too */
        bus->attach_timestamp = b;
 
        return 0;
}
 
_public_ int sd_bus_negotiate_creds(sd_bus *bus, int b, uint64_t mask) {
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(mask <= _SD_BUS_CREDS_ALL, -EINVAL);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        if (IN_SET(bus->state, BUS_CLOSING, BUS_CLOSED))
                return -ENOTCONN;
 
        SET_FLAG(bus->creds_mask, mask, b);
 
        /* The well knowns we need unconditionally, so that matches can work */
        bus->creds_mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME;
 
        return 0;
}
 
_public_ int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t server_id) {
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(b || sd_id128_equal(server_id, SD_ID128_NULL), -EINVAL);
        assert_return(bus->state == BUS_UNSET, -EPERM);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        bus->is_server = b;
        bus->server_id = server_id;
        return 0;
}
 
_public_ int sd_bus_set_anonymous(sd_bus *bus, int b) {
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(bus->state == BUS_UNSET, -EPERM);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        bus->anonymous_auth = b;
        return 0;
}
 
_public_ int sd_bus_set_trusted(sd_bus *bus, int b) {
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(bus->state == BUS_UNSET, -EPERM);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        bus->trusted = b;
        return 0;
}
 
_public_ int sd_bus_set_description(sd_bus *bus, const char *description) {
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(bus->state == BUS_UNSET, -EPERM);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        return free_and_strdup(&bus->description, description);
}
 
_public_ int sd_bus_set_allow_interactive_authorization(sd_bus *bus, int b) {
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        bus->allow_interactive_authorization = b;
        return 0;
}
 
_public_ int sd_bus_get_allow_interactive_authorization(sd_bus *bus) {
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        return bus->allow_interactive_authorization;
}
 
_public_ int sd_bus_set_watch_bind(sd_bus *bus, int b) {
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(bus->state == BUS_UNSET, -EPERM);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        bus->watch_bind = b;
        return 0;
}
 
_public_ int sd_bus_get_watch_bind(sd_bus *bus) {
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        return bus->watch_bind;
}
 
_public_ int sd_bus_set_connected_signal(sd_bus *bus, int b) {
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(bus->state == BUS_UNSET, -EPERM);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        bus->connected_signal = b;
        return 0;
}
 
_public_ int sd_bus_get_connected_signal(sd_bus *bus) {
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        return bus->connected_signal;
}
 
static int synthesize_connected_signal(sd_bus *bus) {
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
        int r;
 
        assert(bus);
 
        /* If enabled, synthesizes a local "Connected" signal mirroring the local "Disconnected" signal. This is called
         * whenever we fully established a connection, i.e. after the authorization phase, and after receiving the
         * Hello() reply. Or in other words, whenever we enter BUS_RUNNING state.
         *
         * This is useful so that clients can start doing stuff whenever the connection is fully established in a way
         * that works independently from whether we connected to a full bus or just a direct connection. */
 
        if (!bus->connected_signal)
                return 0;
 
        r = sd_bus_message_new_signal(
                        bus,
                        &m,
                        "/org/freedesktop/DBus/Local",
                        "org.freedesktop.DBus.Local",
                        "Connected");
        if (r < 0)
                return r;
 
        bus_message_set_sender_local(bus, m);
        m->read_counter = ++bus->read_counter;
 
        r = bus_seal_synthetic_message(bus, m);
        if (r < 0)
                return r;
 
        r = bus_rqueue_make_room(bus);
        if (r < 0)
                return r;
 
        /* Insert at the very front */
        memmove(bus->rqueue + 1, bus->rqueue, sizeof(sd_bus_message*) * bus->rqueue_size);
        bus->rqueue[0] = bus_message_ref_queued(m, bus);
        bus->rqueue_size++;
 
        return 0;
}
 
void bus_set_state(sd_bus *bus, BusState state) {
        static const char* const table[_BUS_STATE_MAX] = {
                [BUS_UNSET]          = "UNSET",
                [BUS_WATCH_BIND]     = "WATCH_BIND",
                [BUS_OPENING]        = "OPENING",
                [BUS_AUTHENTICATING] = "AUTHENTICATING",
                [BUS_HELLO]          = "HELLO",
                [BUS_RUNNING]        = "RUNNING",
                [BUS_CLOSING]        = "CLOSING",
                [BUS_CLOSED]         = "CLOSED",
        };
 
        assert(bus);
        assert(state < _BUS_STATE_MAX);
 
        if (state == bus->state)
                return;
 
        log_debug("Bus %s: changing state %s %s %s", strna(bus->description),
                  table[bus->state], glyph(GLYPH_ARROW_RIGHT), table[state]);
        bus->state = state;
}
 
static int hello_callback(sd_bus_message *reply, void *userdata, sd_bus_error *reterr_error) {
        const char *s;
        sd_bus *bus;
        int r;
 
        assert(reply);
        bus = reply->bus;
        assert(bus);
        assert(IN_SET(bus->state, BUS_HELLO, BUS_CLOSING));
 
        r = sd_bus_message_get_errno(reply);
        if (r > 0) {
                r = -r;
                goto fail;
        }
 
        r = sd_bus_message_read(reply, "s", &s);
        if (r < 0)
                goto fail;
 
        if (!service_name_is_valid(s) || s[0] != ':') {
                r = -EBADMSG;
                goto fail;
        }
 
        r = free_and_strdup(&bus->unique_name, s);
        if (r < 0)
                goto fail;
 
        if (bus->state == BUS_HELLO) {
                bus_set_state(bus, BUS_RUNNING);
 
                r = synthesize_connected_signal(bus);
                if (r < 0)
                        goto fail;
        }
 
        return 1;
 
fail:
        /* When Hello() failed, let's propagate this in two ways: first we return the error immediately here,
         * which is the propagated up towards the event loop. Let's also invalidate the connection, so that
         * if the user then calls back into us again we won't wait any longer. */
 
        bus_set_state(bus, BUS_CLOSING);
        return r;
}
 
static int bus_send_hello(sd_bus *bus) {
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
        int r;
 
        assert(bus);
 
        if (!bus->bus_client)
                return 0;
 
        r = sd_bus_message_new_method_call(
                        bus,
                        &m,
                        "org.freedesktop.DBus",
                        "/org/freedesktop/DBus",
                        "org.freedesktop.DBus",
                        "Hello");
        if (r < 0)
                return r;
 
        return sd_bus_call_async(bus, NULL, m, hello_callback, NULL, 0);
}
 
int bus_start_running(sd_bus *bus) {
        BusReplyCallback *c;
        usec_t n;
        int r;
 
        assert(bus);
        assert(bus->state < BUS_HELLO);
 
        /* We start all method call timeouts when we enter BUS_HELLO or BUS_RUNNING mode. At this point let's convert
         * all relative to absolute timestamps. Note that we do not reshuffle the reply callback priority queue since
         * adding a fixed value to all entries should not alter the internal order. */
 
        n = now(CLOCK_MONOTONIC);
        ORDERED_HASHMAP_FOREACH(c, bus->reply_callbacks) {
                if (c->timeout_usec == 0)
                        continue;
 
                c->timeout_usec = usec_add(n, c->timeout_usec);
        }
 
        if (bus->bus_client) {
                bus_set_state(bus, BUS_HELLO);
                return 1;
        }
 
        bus_set_state(bus, BUS_RUNNING);
 
        r = synthesize_connected_signal(bus);
        if (r < 0)
                return r;
 
        return 1;
}
 
static int parse_address_key(const char **p, const char *key, char **value) {
        _cleanup_free_ char *r = NULL;
        size_t n = 0;
        const char *a;
 
        assert(p);
        assert(*p);
        assert(value);
 
        if (key) {
                a = startswith(*p, key);
                if (!a || *a != '=')
                        return 0;
 
                if (*value)
                        return -EINVAL;
 
                a++;
        } else
                a = *p;
 
        while (!IN_SET(*a, ';', ',', 0)) {
                char c;
 
                if (*a == '%') {
                        int x, y;
 
                        x = unhexchar(a[1]);
                        if (x < 0)
                                return x;
 
                        y = unhexchar(a[2]);
                        if (y < 0)
                                return y;
 
                        c = (char) ((x << 4) | y);
                        a += 3;
                } else {
                        c = *a;
                        a++;
                }
 
                if (!GREEDY_REALLOC(r, n + 2))
                        return -ENOMEM;
 
                r[n++] = c;
        }
 
        if (!r) {
                r = strdup("");
                if (!r)
                        return -ENOMEM;
        } else
                r[n] = 0;
 
        if (*a == ',')
                a++;
 
        *p = a;
 
        free_and_replace(*value, r);
 
        return 1;
}
 
static void skip_address_key(const char **p) {
        assert(p);
        assert(*p);
 
        *p += strcspn(*p, ",");
 
        if (**p == ',')
                (*p)++;
}
 
static int parse_unix_address(sd_bus *b, const char **p, char **guid) {
        _cleanup_free_ char *path = NULL, *abstract = NULL, *uids = NULL, *gids = NULL;
        size_t l;
        int r;
 
        assert(b);
        assert(p);
        assert(*p);
        assert(guid);
 
        while (!IN_SET(**p, 0, ';')) {
                r = parse_address_key(p, "guid", guid);
                if (r < 0)
                        return r;
                if (r > 0)
                        continue;
 
                r = parse_address_key(p, "path", &path);
                if (r < 0)
                        return r;
                if (r > 0)
                        continue;
 
                r = parse_address_key(p, "abstract", &abstract);
                if (r < 0)
                        return r;
                if (r > 0)
                        continue;
 
                r = parse_address_key(p, "uid", &uids);
                if (r < 0)
                        return r;
                if (r > 0)
                        continue;
 
                r = parse_address_key(p, "gid", &gids);
                if (r < 0)
                        return r;
                if (r > 0)
                        continue;
 
                skip_address_key(p);
        }
 
        if (!path && !abstract)
                return -EINVAL;
 
        if (path && abstract)
                return -EINVAL;
 
        if (path) {
                l = strlen(path);
                if (l >= sizeof(b->sockaddr.un.sun_path)) /* We insist on NUL termination */
                        return -E2BIG;
 
                b->sockaddr.un = (struct sockaddr_un) {
                        .sun_family = AF_UNIX,
                };
 
                memcpy(b->sockaddr.un.sun_path, path, l);
                b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l + 1;
 
        } else {
                assert(abstract);
 
                l = strlen(abstract);
                if (l >= sizeof(b->sockaddr.un.sun_path) - 1) /* We insist on NUL termination */
                        return -E2BIG;
 
                b->sockaddr.un = (struct sockaddr_un) {
                        .sun_family = AF_UNIX,
                };
 
                memcpy(b->sockaddr.un.sun_path+1, abstract, l);
                b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + 1 + l;
        }
 
        if (uids) {
                r = parse_uid(uids, &b->connect_as_uid);
                if (r < 0)
                        return r;
        }
        if (gids) {
                r = parse_gid(gids, &b->connect_as_gid);
                if (r < 0)
                        return r;
        }
 
        b->is_local = true;
 
        return 0;
}
 
static int parse_tcp_address(sd_bus *b, const char **p, char **guid) {
#if BUILD_STATIC
        return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
                               "tcp:// connections are not supported in static builds");
#else
        _cleanup_free_ char *host = NULL, *port = NULL, *family = NULL;
        int r;
        struct addrinfo *result, hints = {
                .ai_socktype = SOCK_STREAM,
        };
 
        assert(b);
        assert(p);
        assert(*p);
        assert(guid);
 
        while (!IN_SET(**p, 0, ';')) {
                r = parse_address_key(p, "guid", guid);
                if (r < 0)
                        return r;
                if (r > 0)
                        continue;
 
                r = parse_address_key(p, "host", &host);
                if (r < 0)
                        return r;
                if (r > 0)
                        continue;
 
                r = parse_address_key(p, "port", &port);
                if (r < 0)
                        return r;
                if (r > 0)
                        continue;
 
                r = parse_address_key(p, "family", &family);
                if (r < 0)
                        return r;
                if (r > 0)
                        continue;
 
                skip_address_key(p);
        }
 
        if (!host || !port)
                return -EINVAL;
 
        if (family) {
                hints.ai_family = af_from_ipv4_ipv6(family);
                if (hints.ai_family == AF_UNSPEC)
                        return -EINVAL;
        }
 
        r = getaddrinfo(host, port, &hints, &result);
        if (r == EAI_SYSTEM)
                return -errno;
        if (r != 0)
                return -EADDRNOTAVAIL;
 
        memcpy(&b->sockaddr, result->ai_addr, result->ai_addrlen);
        b->sockaddr_size = result->ai_addrlen;
 
        freeaddrinfo(result);
 
        b->is_local = false;
 
        return 0;
#endif
}
 
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 (!IN_SET(**p, 0, ';')) {
                r = parse_address_key(p, "guid", guid);
                if (r < 0)
                        goto fail;
                if (r > 0)
                        continue;
 
                r = parse_address_key(p, "path", &path);
                if (r < 0)
                        goto fail;
                if (r > 0)
                        continue;
 
                if (startswith(*p, "argv")) {
                        unsigned ul;
 
                        errno = 0;
                        ul = strtoul(*p + 4, (char**) p, 10);
                        if (errno > 0 || **p != '=' || ul > 256) {
                                r = -EINVAL;
                                goto fail;
                        }
 
                        (*p)++;
 
                        if (ul >= n_argv) {
                                if (!GREEDY_REALLOC0(argv, ul + 2)) {
                                        r = -ENOMEM;
                                        goto fail;
                                }
 
                                n_argv = ul + 1;
                        }
 
                        r = parse_address_key(p, NULL, argv + ul);
                        if (r < 0)
                                goto fail;
 
                        continue;
                }
 
                skip_address_key(p);
        }
 
        if (!path) {
                r = -EINVAL;
                goto fail;
        }
 
        /* 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;
                }
        }
 
        b->exec_path = path;
        b->exec_argv = argv;
 
        b->is_local = false;
 
        return 0;
 
fail:
        for (j = 0; j < n_argv; j++)
                free(argv[j]);
 
        free(argv);
        free(path);
        return r;
}
 
static int parse_container_unix_address(sd_bus *b, const char **p, char **guid) {
        _cleanup_free_ char *machine = NULL, *pid = NULL;
        int r;
 
        assert(b);
        assert(p);