sd-bus.c revision f4b2933ee7890e5d414ab266d8586f19027a2bd9
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Lennart Poettering
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 <byteswap.h>
#include <pthread.h>
#include "util.h"
#include "macro.h"
#include "strv.h"
#include "set.h"
#include "missing.h"
#include "def.h"
#include "cgroup-util.h"
#include "bus-label.h"
#include "sd-bus.h"
#include "bus-internal.h"
#include "bus-message.h"
#include "bus-type.h"
#include "bus-socket.h"
#include "bus-kernel.h"
#include "bus-control.h"
#include "bus-introspect.h"
#include "bus-signature.h"
#include "bus-objects.h"
#include "bus-util.h"
#include "bus-container.h"
#include "bus-protocol.h"
#include "bus-track.h"
#include "bus-slot.h"
static int attach_io_events(sd_bus *b);
static void detach_io_events(sd_bus *b);
static void bus_close_fds(sd_bus *b) {
assert(b);
detach_io_events(b);
if (b->input_fd >= 0)
safe_close(b->input_fd);
safe_close(b->output_fd);
}
static void bus_reset_queues(sd_bus *b) {
assert(b);
while (b->rqueue_size > 0)
b->rqueue_allocated = 0;
while (b->wqueue_size > 0)
b->wqueue_allocated = 0;
}
sd_bus_slot *s;
assert(b);
assert(!b->track_queue);
b->state = BUS_CLOSED;
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. */
}
if (b->default_bus_ptr)
*b->default_bus_ptr = NULL;
bus_close_fds(b);
if (b->kdbus_buffer)
free(b->unique_name);
free(b->auth_buffer);
free(b->fake_label);
free(b->cgroup_root);
free(b->description);
bus_reset_queues(b);
hashmap_free(b->nodes);
free(b);
}
sd_bus *r;
if (!r)
return -ENOMEM;
r->n_ref = REFCNT_INIT;
r->message_version = 1;
r->hello_flags |= KDBUS_HELLO_ACCEPT_FD;
r->attach_flags |= KDBUS_ATTACH_NAMES;
r->original_pid = getpid();
/* We guarantee that wqueue always has space for at least one
* entry */
free(r);
return -ENOMEM;
}
*ret = r;
return 0;
}
char *a;
if (!a)
return -ENOMEM;
return 0;
}
return 0;
}
char *p, **a;
if (!p)
return -ENOMEM;
if (!a) {
free(p);
return -ENOMEM;
}
return 0;
}
bus->bus_client = !!b;
return 0;
}
return 0;
}
return 0;
}
return 0;
}
/* The well knowns we need unconditionally, so that matches can work */
}
return 0;
}
bus->anonymous_auth = !!b;
return 0;
}
return 0;
}
}
static int hello_callback(sd_bus *bus, sd_bus_message *reply, void *userdata, sd_bus_error *error) {
const char *s;
int r;
if (r > 0)
return -r;
if (r < 0)
return r;
if (!service_name_is_valid(s) || s[0] != ':')
return -EBADMSG;
if (!bus->unique_name)
return -ENOMEM;
return 1;
}
int r;
return 0;
bus,
&m,
"org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus",
"Hello");
if (r < 0)
return r;
}
return 1;
}
return 1;
}
const char *a;
char *r = NULL;
assert(p);
assert(*p);
if (key) {
return 0;
if ((*p)[l] != '=')
return 0;
if (*value)
return -EINVAL;
a = *p + l + 1;
} else
a = *p;
while (*a != ';' && *a != ',' && *a != 0) {
char c;
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);
a += 3;
} else {
c = *a;
a++;
}
return -ENOMEM;
r[n++] = c;
}
if (!r) {
r = strdup("");
if (!r)
return -ENOMEM;
} else
r[n] = 0;
if (*a == ',')
a++;
*p = a;
*value = r;
return 1;
}
static void skip_address_key(const char **p) {
assert(p);
assert(*p);
*p += strcspn(*p, ",");
if (**p == ',')
(*p) ++;
}
size_t l;
int r;
assert(b);
assert(p);
assert(*p);
while (**p != 0 && **p != ';') {
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
skip_address_key(p);
}
return -EINVAL;
return -EINVAL;
if (path) {
return -E2BIG;
} else if (abstract) {
return -E2BIG;
}
return 0;
}
int r;
};
assert(b);
assert(p);
assert(*p);
while (**p != 0 && **p != ';') {
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
skip_address_key(p);
}
return -EINVAL;
if (family) {
else
return -EINVAL;
}
if (r == EAI_SYSTEM)
return -errno;
else if (r != 0)
return -EADDRNOTAVAIL;
return 0;
}
unsigned n_argv = 0, j;
int r;
assert(b);
assert(p);
assert(*p);
while (**p != 0 && **p != ';') {
if (r < 0)
goto fail;
else if (r > 0)
continue;
if (r < 0)
goto fail;
else if (r > 0)
continue;
if (startswith(*p, "argv")) {
unsigned ul;
errno = 0;
r = -EINVAL;
goto fail;
}
(*p) ++;
r = -ENOMEM;
goto fail;
}
}
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[0]) {
r = -ENOMEM;
goto fail;
}
}
return 0;
fail:
for (j = 0; j < n_argv; j++)
return r;
}
int r;
assert(b);
assert(p);
assert(*p);
while (**p != 0 && **p != ';') {
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
skip_address_key(p);
}
if (!path)
return -EINVAL;
return 0;
}
int r;
assert(b);
assert(p);
assert(*p);
while (**p != 0 && **p != ';') {
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
skip_address_key(p);
}
if (!machine)
return -EINVAL;
if (!machine_name_is_valid(machine))
return -EINVAL;
strncpy(b->sockaddr.un.sun_path, "/var/run/dbus/system_bus_socket", sizeof(b->sockaddr.un.sun_path));
b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + strlen("/var/run/dbus/system_bus_socket");
return 0;
}
int r;
assert(b);
assert(p);
assert(*p);
while (**p != 0 && **p != ';') {
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
skip_address_key(p);
}
if (!machine)
return -EINVAL;
if (!machine_name_is_valid(machine))
return -EINVAL;
if (!b->kernel)
return -ENOMEM;
return 0;
}
static void bus_reset_parsed_address(sd_bus *b) {
assert(b);
b->sockaddr_size = 0;
b->server_id = SD_ID128_NULL;
}
static int bus_parse_next_address(sd_bus *b) {
const char *a;
int r;
assert(b);
if (!b->address)
return 0;
if (b->address[b->address_index] == 0)
return 0;
a = b->address + b->address_index;
while (*a != 0) {
if (*a == ';') {
a++;
continue;
}
if (startswith(a, "unix:")) {
a += 5;
r = parse_unix_address(b, &a, &guid);
if (r < 0)
return r;
break;
} else if (startswith(a, "tcp:")) {
a += 4;
r = parse_tcp_address(b, &a, &guid);
if (r < 0)
return r;
break;
} else if (startswith(a, "unixexec:")) {
a += 9;
r = parse_exec_address(b, &a, &guid);
if (r < 0)
return r;
break;
} else if (startswith(a, "kernel:")) {
a += 7;
r = parse_kernel_address(b, &a, &guid);
if (r < 0)
return r;
break;
} else if (startswith(a, "x-container-unix:")) {
a += 17;
r = parse_container_unix_address(b, &a, &guid);
if (r < 0)
return r;
break;
} else if (startswith(a, "x-container-kernel:")) {
a += 19;
r = parse_container_kernel_address(b, &a, &guid);
if (r < 0)
return r;
break;
}
a = strchr(a, ';');
if (!a)
return 0;
}
if (guid) {
if (r < 0)
return r;
}
b->address_index = a - b->address;
return 1;
}
static int bus_start_address(sd_bus *b) {
int r;
assert(b);
for (;;) {
bool skipped = false;
bus_close_fds(b);
if (b->exec_path)
r = bus_socket_exec(b);
r = bus_container_connect_kernel(b);
r = bus_container_connect_socket(b);
else if (b->kernel)
r = bus_kernel_connect(b);
r = bus_socket_connect(b);
else
skipped = true;
if (!skipped) {
if (r >= 0) {
r = attach_io_events(b);
if (r >= 0)
return r;
}
b->last_connect_error = -r;
}
r = bus_parse_next_address(b);
if (r < 0)
return r;
if (r == 0)
}
}
int bus_next_address(sd_bus *b) {
assert(b);
return bus_start_address(b);
}
static int bus_start_fd(sd_bus *b) {
int r;
assert(b);
r = fd_nonblock(b->input_fd, true);
if (r < 0)
return r;
r = fd_cloexec(b->input_fd, true);
if (r < 0)
return r;
r = fd_nonblock(b->output_fd, true);
if (r < 0)
return r;
r = fd_cloexec(b->output_fd, true);
if (r < 0)
return r;
}
return -errno;
return bus_kernel_take_fd(b);
else
return bus_socket_take_fd(b);
}
int r;
return -EINVAL;
r = bus_start_fd(bus);
else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path || bus->kernel || bus->machine)
r = bus_start_address(bus);
else
return -EINVAL;
if (r < 0) {
return r;
}
return bus_send_hello(bus);
}
const char *e;
sd_bus *b;
int r;
/* Let's connect to the starter bus if it is set, and
* otherwise to the bus that is appropropriate for the scope
* we are running in */
e = secure_getenv("DBUS_STARTER_BUS_TYPE");
if (e) {
if (streq(e, "system"))
return sd_bus_open_system(ret);
return sd_bus_open_user(ret);
}
e = secure_getenv("DBUS_STARTER_ADDRESS");
if (!e) {
if (cg_pid_get_owner_uid(0, NULL) >= 0)
return sd_bus_open_user(ret);
else
return sd_bus_open_system(ret);
}
r = sd_bus_new(&b);
if (r < 0)
return r;
r = sd_bus_set_address(b, e);
if (r < 0)
goto fail;
b->bus_client = true;
/* We don't know whether the bus is trusted or not, so better
* be safe, and authenticate everything */
b->trusted = false;
r = sd_bus_start(b);
if (r < 0)
goto fail;
*ret = b;
return 0;
fail:
bus_free(b);
return r;
}
int bus_set_address_system(sd_bus *b) {
const char *e;
assert(b);
e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
if (e)
return sd_bus_set_address(b, e);
return sd_bus_set_address(b, DEFAULT_SYSTEM_BUS_PATH);
}
sd_bus *b;
int r;
r = sd_bus_new(&b);
if (r < 0)
return r;
r = bus_set_address_system(b);
if (r < 0)
goto fail;
b->bus_client = true;
b->is_system = true;
/* Let's do per-method access control on the system bus. We
* need the caller's UID and capability set for that. */
b->trusted = false;
r = sd_bus_start(b);
if (r < 0)
goto fail;
*ret = b;
return 0;
fail:
bus_free(b);
return r;
}
int bus_set_address_user(sd_bus *b) {
const char *e;
assert(b);
e = secure_getenv("DBUS_SESSION_BUS_ADDRESS");
if (e)
return sd_bus_set_address(b, e);
e = secure_getenv("XDG_RUNTIME_DIR");
if (e) {
ee = bus_address_escape(e);
if (!ee)
return -ENOMEM;
#ifdef ENABLE_KDBUS
#else
#endif
} else {
#ifdef ENABLE_KDBUS
#else
return -ECONNREFUSED;
#endif
}
if (!b->address)
return -ENOMEM;
return 0;
}
sd_bus *b;
int r;
r = sd_bus_new(&b);
if (r < 0)
return r;
r = bus_set_address_user(b);
if (r < 0)
return r;
b->bus_client = true;
b->is_user = true;
/* We don't do any per-method access control on the user
* bus. */
b->trusted = true;
r = sd_bus_start(b);
if (r < 0)
goto fail;
*ret = b;
return 0;
fail:
bus_free(b);
return r;
}
_cleanup_free_ char *e = NULL;
assert(b);
/* Let's see if we shall enter some container */
if (m) {
m++;
/* Let's make sure this is not a port of some kind,
* and is a valid machine name. */
char *t;
/* Cut out the host part */
e = bus_address_escape(t);
if (!e)
return -ENOMEM;
c = strappenda(",argv4=--machine=", m);
}
}
if (!e) {
e = bus_address_escape(host);
if (!e)
return -ENOMEM;
}
b->address = strjoin("unixexec:path=ssh,argv1=-xT,argv2=", e, ",argv3=systemd-stdio-bridge", c, NULL);
if (!b->address)
return -ENOMEM;
return 0;
}
int r;
r = sd_bus_new(&bus);
if (r < 0)
return r;
if (r < 0)
goto fail;
bus->bus_client = true;
r = sd_bus_start(bus);
if (r < 0)
goto fail;
return 0;
fail:
return r;
}
_cleanup_free_ char *e = NULL;
assert(b);
e = bus_address_escape(machine);
if (!e)
return -ENOMEM;
#ifdef ENABLE_KDBUS
#else
#endif
if (!b->address)
return -ENOMEM;
return 0;
}
int r;
r = sd_bus_new(&bus);
if (r < 0)
return r;
if (r < 0)
goto fail;
bus->bus_client = true;
r = sd_bus_start(bus);
if (r < 0)
goto fail;
return 0;
fail:
return r;
}
if (!bus)
return;
return;
if (bus_pid_changed(bus))
return;
/* Drop all queued messages so that they drop references to
* the bus object and the bus may be freed */
/* We'll leave the fd open in case this is a kernel bus, since
* there might still be memblocks around that reference this
* bus, and they might need to invoke the KDBUS_CMD_FREE
* ioctl on the fd when they are freed. */
}
return;
}
return bus;
}
unsigned i;
if (!bus)
return NULL;
if (i > 0)
return NULL;
return NULL;
}
}
int r;
return 0;
if (type == SD_BUS_TYPE_UNIX_FD) {
return 0;
r = bus_ensure_running(bus);
if (r < 0)
return r;
}
return bus_type_is_valid(type);
}
int r;
r = bus_ensure_running(bus);
if (r < 0)
return r;
return 0;
}
assert(b);
assert(m);
if (m->sealed) {
/* If we copy the same message to multiple
* destinations, avoid using the same cookie
* numbers. */
return 0;
}
if (timeout == 0)
}
assert(b);
/* Do packet version and endianness already match? */
return 0;
/* No? Then remarshal! */
return bus_message_remarshal(b, m);
}
assert(b);
assert(m);
/* The bus specification says the serial number cannot be 0,
* hence let's fill something in for synthetic messages. Since
* synthetic messages might have a fake sender and we don't
* want to interfere with the real sender's serial numbers we
* pick a fixed, artificial one. We use (uint32_t) -1 rather
* than (uint64_t) -1 since dbus1 only had 32bit identifiers,
* even though kdbus can do 64bit. */
return bus_message_seal(m, 0xFFFFFFFFULL, 0);
}
int r;
assert(m);
else
if (r <= 0)
return r;
log_debug("Sent message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%" PRIu64 " reply_cookie=%" PRIu64 " error=%s",
m->reply_cookie,
return r;
}
int r, ret = 0;
while (bus->wqueue_size > 0) {
if (r < 0)
return r;
else if (r == 0)
/* Didn't do anything this time */
return ret;
/* 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. */
bus->wqueue_size --;
ret = 1;
}
}
return ret;
}
else
return bus_socket_read_message(bus);
}
return -ENOBUFS;
return -ENOMEM;
return 0;
}
int r, ret = 0;
assert(m);
/* Note that the priority logic is only available on kdbus,
* where the rqueue is unused. We check the rqueue here
* anyway, because it's simple... */
for (;;) {
if (bus->rqueue_size > 0) {
/* Dispatch a queued message */
bus->rqueue_size --;
return 1;
}
/* Try to read a new message */
if (r < 0)
return r;
if (r == 0)
return ret;
ret = 1;
}
}
static int bus_send_internal(sd_bus *bus, sd_bus_message *_m, uint64_t *cookie, bool hint_sync_call) {
int r;
assert_return(m, -EINVAL);
return -ENOTCONN;
if (m->n_fds > 0) {
if (r < 0)
return r;
if (r == 0)
return -ENOTSUP;
}
/* If the cookie number isn't kept, then we know that no reply
* is expected */
r = bus_seal_message(bus, m, 0);
if (r < 0)
return r;
/* Remarshall if we have to. This will possibly unref the
* message and place a replacement in m */
r = bus_remarshal_message(bus, &m);
if (r < 0)
return r;
/* If this is a reply and no reply was requested, then let's
* suppress this, if we can */
return 1;
if (r < 0) {
return -ECONNRESET;
}
return r;
/* Wasn't fully written. So let's remember how
* much was written. Note that the first entry
* of the wqueue array is always allocated so
* that we always can remember how much was
* written. */
}
} else {
/* Just append it to the queue. */
return -ENOBUFS;
return -ENOMEM;
}
if (cookie)
*cookie = BUS_MESSAGE_COOKIE(m);
return 1;
}
}
_public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destination, uint64_t *cookie) {
int r;
assert_return(m, -EINVAL);
return -ENOTCONN;
if (!destination)
return -EEXIST;
if (r < 0)
return r;
}
}
return 0;
}
static int timeout_compare(const void *a, const void *b) {
const struct reply_callback *x = a, *y = b;
return -1;
return 1;
return -1;
return 1;
return 0;
}
sd_bus_slot **slot,
void *userdata,
int r;
assert_return(m, -EINVAL);
return -ENOTCONN;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
r = bus_remarshal_message(bus, &m);
if (r < 0)
return r;
if (!s)
return -ENOMEM;
if (r < 0) {
s->reply_callback.cookie = 0;
return r;
}
if (s->reply_callback.timeout != 0) {
if (r < 0) {
s->reply_callback.timeout = 0;
return r;
}
}
if (r < 0)
return r;
if (slot)
*slot = s;
s = NULL;
return r;
}
int r;
return -ENOTCONN;
return 1;
for (;;) {
if (r < 0)
return r;
return 1;
if (r > 0)
continue;
if (r < 0)
return r;
}
}
_public_ int sd_bus_call(
sd_bus_message **reply) {
unsigned i;
int r;
assert_return(m, -EINVAL);
return -ENOTCONN;
r = bus_ensure_running(bus);
if (r < 0)
return r;
i = bus->rqueue_size;
if (r < 0)
return r;
r = bus_remarshal_message(bus, &m);
if (r < 0)
return r;
if (r < 0)
return r;
for (;;) {
while (i < bus->rqueue_size) {
/* Found a match! */
memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
bus->rqueue_size--;
if (reply)
else
return 1;
}
r = sd_bus_error_setf(error, SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Reply message contained file descriptors which I couldn't accept. Sorry.");
else
r = -EIO;
return r;
bus->unique_name &&
memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
bus->rqueue_size--;
/* Our own message? Somebody is trying
* to send its own client a message,
* let's not dead-lock, let's fail
* immediately. */
return -ELOOP;
}
/* Try to read more, right-away */
i++;
}
r = bus_read_message(bus, false, 0);
if (r < 0) {
return -ECONNRESET;
}
return r;
}
if (r > 0)
continue;
if (timeout > 0) {
usec_t n;
n = now(CLOCK_MONOTONIC);
if (n >= timeout)
return -ETIMEDOUT;
} else
if (r < 0)
return r;
if (r == 0)
return -ETIMEDOUT;
r = dispatch_wqueue(bus);
if (r < 0) {
return -ECONNRESET;
}
return r;
}
}
}
}
int flags = 0;
return -ENOTCONN;
if (bus->rqueue_size <= 0)
if (bus->wqueue_size > 0)
}
return flags;
}
struct reply_callback *c;
return -ENOTCONN;
if (bus->track_queue) {
*timeout_usec = 0;
return 1;
}
*timeout_usec = 0;
return 1;
}
return 1;
}
return 0;
}
if (bus->rqueue_size > 0) {
*timeout_usec = 0;
return 1;
}
if (!c) {
return 0;
}
if (c->timeout == 0) {
return 0;
}
*timeout_usec = c->timeout;
return 1;
}
struct reply_callback *c;
usec_t n;
int r;
if (!c)
return 0;
n = now(CLOCK_MONOTONIC);
if (c->timeout > n)
return 0;
bus,
c->cookie,
&m);
if (r < 0)
return r;
m->sender = "org.freedesktop.DBus";
r = bus_seal_synthetic_message(bus, m);
if (r < 0)
return r;
c->timeout = 0;
c->cookie = 0;
bus->iteration_counter ++;
bus->current_message = m;
}
return bus_maybe_reply_error(m, r, &error_buffer);
}
assert(m);
return 0;
/* Let's make sure the first message on the bus is the HELLO
* reply. But note that we don't actually parse the message
* here (we leave that to the usual handling), we just verify
* we don't let any earlier msg through. */
return -EIO;
if (m->reply_cookie != 1)
return -EIO;
return 0;
}
struct reply_callback *c;
int r;
assert(m);
return 0;
return 0;
return 0;
if (!c)
return 0;
c->cookie = 0;
/* If the reply contained a file descriptor which we
* didn't want we pass an error instead. */
bus,
m->reply_cookie,
&SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Reply message contained file descriptor"),
if (r < 0)
return r;
if (r < 0)
return r;
m = synthetic_reply;
} else {
r = sd_bus_message_rewind(m, true);
if (r < 0)
return r;
}
if (c->timeout != 0) {
c->timeout = 0;
}
}
return bus_maybe_reply_error(m, r, &error_buffer);
}
struct filter_callback *l;
int r;
assert(m);
do {
bus->filter_callbacks_modified = false;
if (bus->filter_callbacks_modified)
break;
/* Don't run this more than once per iteration */
continue;
r = sd_bus_message_rewind(m, true);
if (r < 0)
return r;
r = bus_maybe_reply_error(m, r, &error_buffer);
if (r != 0)
return r;
}
} while (bus->filter_callbacks_modified);
return 0;
}
int r;
assert(m);
do {
bus->match_callbacks_modified = false;
if (r != 0)
return r;
} while (bus->match_callbacks_modified);
return 0;
}
int r;
assert(m);
return 0;
if (bus->manual_peer_interface)
return 0;
return 0;
return 0;
return 1;
r = sd_bus_message_new_method_return(m, &reply);
char sid[33];
r = sd_id128_get_machine(&id);
if (r < 0)
return r;
r = sd_bus_message_new_method_return(m, &reply);
if (r < 0)
return r;
} else {
m, &reply,
}
if (r < 0)
return r;
if (r < 0)
return r;
return 1;
}
assert(m);
/* If we got a message with a file descriptor which we didn't
* want to accept, then let's drop it. How can this even
* happen? For example, when the kernel queues a message into
* an activatable names's queue which allows fds, and then is
* delivered to us later even though we ourselves did not
* negotiate it. */
return 0;
if (m->n_fds <= 0)
return 0;
return 0;
return 1; /* just eat it up */
return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Message contains file descriptors, which I cannot accept. Sorry.");
}
int r;
assert(m);
bus->current_message = m;
bus->iteration_counter++;
log_debug("Got message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%" PRIu64 " reply_cookie=%" PRIu64 " error=%s",
m->reply_cookie,
r = process_hello(bus, m);
if (r != 0)
goto finish;
r = process_reply(bus, m);
if (r != 0)
goto finish;
r = process_fd_check(bus, m);
if (r != 0)
goto finish;
r = process_filter(bus, m);
if (r != 0)
goto finish;
r = process_match(bus, m);
if (r != 0)
goto finish;
r = process_builtin(bus, m);
if (r != 0)
goto finish;
r = bus_process_object(bus, m);
return r;
}
if (!bus->track_queue)
return 0;
return 1;
}
static int process_running(sd_bus *bus, bool hint_priority, int64_t priority, sd_bus_message **ret) {
int r;
r = process_timeout(bus);
if (r != 0)
goto null_message;
r = dispatch_wqueue(bus);
if (r != 0)
goto null_message;
r = dispatch_track(bus);
if (r != 0)
goto null_message;
if (r < 0)
return r;
if (!m)
goto null_message;
r = process_message(bus, m);
if (r != 0)
goto null_message;
if (ret) {
r = sd_bus_message_rewind(m, true);
if (r < 0)
return r;
*ret = m;
m = NULL;
return 1;
}
log_debug("Unprocessed message call sender=%s object=%s interface=%s member=%s",
m,
"Unknown object '%s'.", m->path);
if (r < 0)
return r;
}
return 1;
if (r >= 0 && ret)
return r;
}
struct reply_callback *c;
int r;
if (c) {
/* First, fail all outstanding method calls */
bus,
c->cookie,
&m);
if (r < 0)
return r;
r = bus_seal_synthetic_message(bus, m);
if (r < 0)
return r;
if (c->timeout != 0) {
c->timeout = 0;
}
c->cookie = 0;
bus->iteration_counter++;
bus->current_message = m;
}
return bus_maybe_reply_error(m, r, &error_buffer);
}
/* Then, synthesize a Disconnected message */
bus,
&m,
"/org/freedesktop/DBus/Local",
"org.freedesktop.DBus.Local",
"Disconnected");
if (r < 0)
return r;
m->sender = "org.freedesktop.DBus.Local";
r = bus_seal_synthetic_message(bus, m);
if (r < 0)
return r;
bus->current_message = m;
bus->iteration_counter++;
r = process_filter(bus, m);
if (r != 0)
goto finish;
r = process_match(bus, m);
if (r != 0)
goto finish;
if (ret) {
*ret = m;
m = NULL;
}
r = 1;
return r;
}
static int bus_process_internal(sd_bus *bus, bool hint_priority, int64_t priority, sd_bus_message **ret) {
int r;
/* Returns 0 when we didn't do anything. This should cause the
* caller to invoke sd_bus_wait() before returning the next
* time. Returns > 0 when we did something, which possibly
* means *ret is filled in with an unprocessed message. */
/* We don't allow recursively invoking sd_bus_process(). */
case BUS_UNSET:
return -ENOTCONN;
case BUS_CLOSED:
return -ECONNRESET;
case BUS_OPENING:
r = 1;
} else if (r < 0)
return r;
if (ret)
return r;
case BUS_AUTHENTICATING:
r = 1;
} else if (r < 0)
return r;
if (ret)
return r;
case BUS_RUNNING:
case BUS_HELLO:
r = 1;
if (ret)
}
return r;
case BUS_CLOSING:
}
assert_not_reached("Unknown state");
}
}
}
struct pollfd p[2] = {};
int r, e, n;
usec_t m = USEC_INFINITY;
return 1;
return -ENOTCONN;
e = sd_bus_get_events(bus);
if (e < 0)
return e;
if (need_more)
/* The caller really needs some more data, he doesn't
* care about what's already read, or any timeouts
* except its own.*/
e |= POLLIN;
else {
/* The caller wants to process if there's something to
* process, but doesn't care otherwise */
if (r < 0)
return r;
if (r > 0) {
}
}
m = timeout_usec;
p[0].events = e;
n = 1;
} else {
n = 2;
}
if (r < 0)
return -errno;
return r > 0 ? 1 : 0;
}
return 0;
return -ENOTCONN;
if (bus->rqueue_size > 0)
return 0;
}
int r;
return 0;
return -ENOTCONN;
r = bus_ensure_running(bus);
if (r < 0)
return r;
if (bus->wqueue_size <= 0)
return 0;
for (;;) {
r = dispatch_wqueue(bus);
if (r < 0) {
return -ECONNRESET;
}
return r;
}
if (bus->wqueue_size <= 0)
return 0;
if (r < 0)
return r;
}
}
sd_bus_slot **slot,
void *userdata) {
sd_bus_slot *s;
if (!s)
return -ENOMEM;
bus->filter_callbacks_modified = true;
if (slot)
*slot = s;
return 0;
}
_public_ int sd_bus_add_match(
sd_bus_slot **slot,
const char *match,
void *userdata) {
unsigned n_components = 0;
sd_bus_slot *s = NULL;
int r = 0;
if (r < 0)
goto finish;
if (!s) {
r = -ENOMEM;
goto finish;
}
if (bus->bus_client) {
/* When this is not a kernel transport, we
* store the original match string, so that we
* can use it to remove the match again */
if (!s->match_callback.match_string) {
r = -ENOMEM;
goto finish;
}
}
r = bus_add_match_internal(bus, s->match_callback.match_string, components, n_components, s->match_callback.cookie);
if (r < 0)
goto finish;
}
bus->match_callbacks_modified = true;
if (r < 0)
goto finish;
if (slot)
*slot = s;
s = NULL;
return r;
}
const char *match,
void *userdata) {
unsigned n_components = 0;
struct match_callback *c;
int r = 0;
if (r < 0)
goto finish;
if (r <= 0)
goto finish;
return r;
}
/* We don't support people creating a bus connection and
* keeping it around over a fork(). Let's complain. */
}
int r;
if (r < 0)
return r;
return 1;
}
int r;
if (r < 0)
return r;
return 1;
}
int r, e;
assert(s);
e = sd_bus_get_events(bus);
if (e < 0)
return e;
if (r < 0)
return r;
if (r < 0)
return r;
} else {
if (r < 0)
return r;
}
if (r < 0)
return r;
if (r > 0) {
int j;
if (j < 0)
return j;
}
if (r < 0)
return r;
return 1;
}
return 1;
}
int r;
return 0;
return 0;
if (!bus->input_io_event_source) {
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
} else
if (r < 0)
return r;
if (!bus->output_io_event_source) {
if (r < 0)
return r;
if (r < 0)
return r;
} else
if (r < 0)
return r;
}
return 0;
}
if (bus->input_io_event_source) {
}
if (bus->output_io_event_source) {
}
}
int r;
if (event)
else {
if (r < 0)
return r;
}
r = sd_event_add_time(bus->event, &bus->time_event_source, CLOCK_MONOTONIC, 0, 0, time_callback, bus);
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
r = attach_io_events(bus);
if (r < 0)
goto fail;
return 0;
fail:
return r;
}
return 0;
if (bus->time_event_source) {
}
if (bus->quit_event_source) {
}
return 1;
}
}
return bus->current_message;
}
return bus->current_slot;
}
return bus->current_handler;
}
return bus->current_userdata;
}
int r;
if (!ret)
return !!*default_bus;
if (*default_bus) {
return 0;
}
r = bus_open(&b);
if (r < 0)
return r;
b->default_bus_ptr = default_bus;
*default_bus = b;
*ret = b;
return 1;
}
}
}
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
* bus. */
e = secure_getenv("DBUS_STARTER_BUS_TYPE");
if (e) {
if (streq(e, "system"))
return sd_bus_default_system(ret);
return sd_bus_default_user(ret);
}
/* 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) {
}
/* Finally, if nothing is set use the cached connection for
* the right scope */
if (cg_pid_get_owner_uid(0, NULL) >= 0)
return sd_bus_default_user(ret);
else
return sd_bus_default_system(ret);
}
assert_return(b, -EINVAL);
if (b->tid != 0) {
return 0;
}
if (b->event)
return -ENXIO;
}
_cleanup_free_ char *e = NULL;
char *ret;
e = bus_label_escape(external_id);
if (!e)
return -ENOMEM;
if (!ret)
return -ENOMEM;
return 0;
}
const char *e;
char *ret;
if (!e) {
*external_id = NULL;
return 0;
}
ret = bus_label_unescape(e);
if (!ret)
return -ENOMEM;
*external_id = ret;
return 1;
}
int r;
return -ENOTSUP;
return -ENOTCONN;
if (bus->rqueue_size > 0)
return -EBUSY;
if (bus->wqueue_size > 0)
return -EBUSY;
r = bus_kernel_try_close(bus);
if (r < 0)
return r;
return 0;
}
return 0;
}
int r;
if (bus->cgroup_root)
return 0;
if (r == -ENOENT) {
if (!bus->cgroup_root)
return -ENOMEM;
r = 0;
}
return r;
}