sd-bus.c revision 751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9
/*-*- 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 "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"
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)
}
struct node_callback *c;
struct node_vtable *v;
struct node_enumerator *e;
assert(b);
if (!n)
return;
while (n->child)
bus_node_destroy(b, n->child);
while ((c = n->callbacks)) {
free(c);
}
while ((v = n->vtables)) {
free(v);
}
while ((e = n->enumerators)) {
free(e);
}
if (n->parent)
free(n);
}
static void bus_reset_queues(sd_bus *b) {
unsigned i;
assert(b);
for (i = 0; i < b->rqueue_size; i++)
sd_bus_message_unref(b->rqueue[i]);
for (i = 0; i < b->wqueue_size; i++)
sd_bus_message_unref(b->wqueue[i]);
b->rqueue_allocated = b->wqueue_allocated = 0;
b->rqueue_size = b->wqueue_size = 0;
}
struct filter_callback *f;
struct node *n;
assert(b);
bus_close_fds(b);
if (b->kdbus_buffer)
free(b->unique_name);
free(b->auth_buffer);
free(b->fake_label);
free(b->cgroup_root);
bus_reset_queues(b);
while ((f = b->filter_callbacks)) {
free(f);
}
while ((n = hashmap_first(b->nodes)))
bus_node_destroy(b, n);
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;
}
/* 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 (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,
"org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus",
"Hello",
&m);
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 (!filename_is_safe(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) + sizeof("/var/run/dbus/system_bus_socket") - 1;
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 (!filename_is_safe(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;
r = sd_bus_new(&b);
if (r < 0)
return r;
e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
if (e)
r = sd_bus_set_address(b, e);
else
#ifdef ENABLE_KDBUS
r = sd_bus_set_address(b, "kernel:path=/dev/kdbus/0-system/bus;unix:path=/run/dbus/system_bus_socket");
#else
r = sd_bus_set_address(b, "unix:path=/run/dbus/system_bus_socket");
#endif
if (r < 0)
goto fail;
b->bus_client = 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;
}
const char *e;
sd_bus *b;
int r;
r = sd_bus_new(&b);
if (r < 0)
return r;
e = secure_getenv("DBUS_SESSION_BUS_ADDRESS");
if (e) {
r = sd_bus_set_address(b, e);
if (r < 0)
goto fail;
} else {
e = secure_getenv("XDG_RUNTIME_DIR");
if (e) {
ee = bus_address_escape(e);
if (!ee) {
r = -ENOMEM;
goto fail;
}
#ifdef ENABLE_KDBUS
asprintf(&b->address, "kernel:path=/dev/kdbus/%lu-user/bus;unix:path=%s/bus", (unsigned long) getuid(), ee);
#else
#endif
} else {
#ifdef ENABLE_KDBUS
#else
return -ECONNREFUSED;
#endif
}
if (!b->address) {
r = -ENOMEM;
goto fail;
}
}
b->bus_client = 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;
char *p = NULL;
int r;
e = bus_address_escape(host);
if (!e)
return -ENOMEM;
if (!p)
return -ENOMEM;
r = sd_bus_new(&bus);
if (r < 0) {
free(p);
return r;
}
bus->bus_client = true;
r = sd_bus_start(bus);
if (r < 0) {
return r;
}
return 0;
}
_cleanup_free_ char *e = NULL;
char *p;
int r;
e = bus_address_escape(machine);
if (!e)
return -ENOMEM;
#ifdef ENABLE_KDBUS
#else
#endif
if (!p)
return -ENOMEM;
r = sd_bus_new(&bus);
if (r < 0) {
free(p);
return r;
}
bus->bus_client = true;
r = sd_bus_start(bus);
if (r < 0) {
return r;
}
return 0;
}
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;
}
if (!bus)
return NULL;
return NULL;
}
}
int r;
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 serial
* numbers. */
return 0;
}
if (timeout == 0)
}
assert(b);
/* Do packet version and endianess 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, artifical 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);
}
else
}
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;
}
return bus_kernel_read_message(bus);
else
return bus_socket_read_message(bus);
}
return -ENOBUFS;
return -ENOMEM;
return 0;
}
int r, ret = 0;
assert(m);
for (;;) {
if (bus->rqueue_size > 0) {
/* Dispatch a queued message */
bus->rqueue_size --;
return 1;
}
/* Try to read a new message */
r = bus_read_message(bus);
if (r < 0)
return r;
if (r == 0)
return ret;
ret = 1;
}
}
int r;
assert_return(m, -EINVAL);
if (m->n_fds > 0) {
if (r < 0)
return r;
if (r == 0)
return -ENOTSUP;
}
/* If the serial 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 possible 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 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 (serial)
*serial = BUS_MESSAGE_SERIAL(m);
return 1;
}
_public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destination, uint64_t *serial) {
int r;
assert_return(m, -EINVAL);
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;
}
void *userdata,
struct reply_callback *c;
int r;
assert_return(m, -EINVAL);
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 (!c)
return -ENOMEM;
c->serial = BUS_MESSAGE_SERIAL(m);
if (r < 0) {
free(c);
return r;
}
if (c->timeout != 0) {
if (r < 0) {
c->timeout = 0;
return r;
}
}
if (r < 0) {
return r;
}
return r;
}
struct reply_callback *c;
if (!c)
return 0;
if (c->timeout != 0)
free(c);
return 1;
}
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);
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;
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);
if (r < 0) {
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 r;
}
}
}
}
int flags = 0;
if (bus->rqueue_size <= 0)
if (bus->wqueue_size > 0)
}
return flags;
}
struct reply_callback *c;
*timeout_usec = 0;
return 1;
}
return 1;
}
return 0;
}
if (bus->rqueue_size > 0) {
*timeout_usec = 0;
return 1;
}
if (!c) {
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->serial,
&m);
if (r < 0)
return r;
m->sender = "org.freedesktop.DBus";
r = bus_seal_synthetic_message(bus, m);
if (r < 0)
return r;
bus->iteration_counter ++;
r = bus_maybe_reply_error(m, r, &error_buffer);
free(c);
return r;
}
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;
return -EIO;
return 0;
}
struct reply_callback *c;
int r;
assert(m);
return 0;
if (!c)
return 0;
if (c->timeout != 0)
r = sd_bus_message_rewind(m, true);
if (r < 0)
return r;
r = bus_maybe_reply_error(m, r, &error_buffer);
free(c);
return r;
}
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;
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;
}
int r;
assert(m);
bus->iteration_counter++;
log_debug("Got message sender=%s object=%s interface=%s member=%s",
r = process_hello(bus, m);
if (r != 0)
goto finish;
r = process_reply(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;
}
int r;
r = process_timeout(bus);
if (r != 0)
goto null_message;
r = dispatch_wqueue(bus);
if (r != 0)
goto null_message;
r = dispatch_rqueue(bus, &m);
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->serial,
&m);
if (r < 0)
return r;
r = bus_seal_synthetic_message(bus, m);
if (r < 0)
return r;
if (c->timeout != 0)
bus->iteration_counter++;
r = bus_maybe_reply_error(m, r, &error_buffer);
free(c);
goto finish;
}
/* Then, synthesize a Disconnected message */
bus,
"/org/freedesktop/DBus/Local",
"org.freedesktop.DBus.Local",
"Disconnected",
&m);
if (r < 0)
return r;
m->sender = "org.freedesktop.DBus.Local";
r = bus_seal_synthetic_message(bus, m);
if (r < 0)
return r;
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;
}
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;
return 1;
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;
if (bus->rqueue_size > 0)
return 0;
}
int r;
return 0;
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 r;
}
if (bus->wqueue_size <= 0)
return 0;
if (r < 0)
return r;
}
}
void *userdata) {
struct filter_callback *f;
if (!f)
return -ENOMEM;
bus->filter_callbacks_modified = true;
return 0;
}
void *userdata) {
struct filter_callback *f;
bus->filter_callbacks_modified = true;
free(f);
return 1;
}
}
return 0;
}
const char *match,
void *userdata) {
unsigned n_components = 0;
int r = 0;
if (r < 0)
goto finish;
if (bus->bus_client) {
if (r < 0)
goto finish;
}
bus->match_callbacks_modified = true;
r = bus_match_add(&bus->match_callbacks, components, n_components, callback, userdata, cookie, NULL);
if (r < 0) {
if (bus->bus_client)
}
return r;
}
const char *match,
void *userdata) {
unsigned n_components = 0;
int r = 0, q = 0;
if (r < 0)
return r;
bus->match_callbacks_modified = true;
if (bus->bus_client)
return r < 0 ? r : q;
}
/* 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;
} else
if (r < 0)
return r;
if (!bus->output_io_event_source) {
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;
}
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;
}
}
}
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;
}
}
}
assert_return(b, -EINVAL);
if (b->tid != 0) {
return 0;
}
if (b->event)
return -ENXIO;
}
_public_ char *sd_bus_label_escape(const char *s) {
char *r, *t;
const char *f;
assert_return(s, NULL);
/* Escapes all chars that D-Bus' object path cannot deal
* with. Can be reversed with bus_path_unescape(). We special
* case the empty string. */
if (*s == 0)
return strdup("_");
if (!r)
return NULL;
for (f = s, t = r; *f; f++) {
/* Escape everything that is not a-zA-Z0-9. We also
* escape 0-9 if it's the first character */
if (!(*f >= 'A' && *f <= 'Z') &&
!(*f >= 'a' && *f <= 'z') &&
!(f > s && *f >= '0' && *f <= '9')) {
*(t++) = '_';
*(t++) = hexchar(*f >> 4);
*(t++) = hexchar(*f);
} else
*(t++) = *f;
}
*t = 0;
return r;
}
_public_ char *sd_bus_label_unescape(const char *f) {
char *r, *t;
assert_return(f, NULL);
/* Special case for the empty string */
if (streq(f, "_"))
return strdup("");
if (!r)
return NULL;
for (t = r; *f; f++) {
if (*f == '_') {
int a, b;
if ((a = unhexchar(f[1])) < 0 ||
(b = unhexchar(f[2])) < 0) {
/* Invalid escape code, let's take it literal then */
*(t++) = '_';
} else {
*(t++) = (char) ((a << 4) | b);
f += 2;
}
} else
*(t++) = *f;
}
*t = 0;
return r;
}
sd_bus_creds *c;
int r;
return -ENODATA;
c = bus_creds_new();
if (!c)
return -ENOMEM;
if (bus->ucred_valid) {
}
if (!c->label) {
return -ENOMEM;
}
c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
}
if (r < 0)
return r;
*ret = c;
return 0;
}
int r;
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;
}