sd-bus.c revision 9db76355212de5eb7985829d352183d3bdfb56d5
/*-*- 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"
static void bus_close_fds(sd_bus *b) {
assert(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);
}
struct filter_callback *f;
struct node *n;
unsigned i;
assert(b);
bus_close_fds(b);
if (b->kdbus_buffer)
free(b->unique_name);
free(b->auth_buffer);
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]);
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 (!ret)
return -EINVAL;
if (!r)
return -ENOMEM;
r->n_ref = REFCNT_INIT;
r->message_version = 1;
r->hello_flags |= KDBUS_HELLO_ACCEPT_FD;
r->original_pid = getpid();
/* We guarantee that wqueue always has space for at least one
* entry */
if (!r->wqueue) {
free(r);
return -ENOMEM;
}
*ret = r;
return 0;
}
char *a;
if (!bus)
return -EINVAL;
return -EPERM;
if (!address)
return -EINVAL;
if (bus_pid_changed(bus))
return -ECHILD;
if (!a)
return -ENOMEM;
return 0;
}
if (!bus)
return -EINVAL;
return -EPERM;
if (input_fd < 0)
return -EINVAL;
if (output_fd < 0)
return -EINVAL;
if (bus_pid_changed(bus))
return -ECHILD;
return 0;
}
char *p, **a;
if (!bus)
return -EINVAL;
return -EPERM;
if (!path)
return -EINVAL;
if (strv_isempty(argv))
return -EINVAL;
if (bus_pid_changed(bus))
return -ECHILD;
if (!p)
return -ENOMEM;
if (!a) {
free(p);
return -ENOMEM;
}
return 0;
}
if (!bus)
return -EINVAL;
return -EPERM;
if (bus_pid_changed(bus))
return -ECHILD;
bus->bus_client = !!b;
return 0;
}
if (!bus)
return -EINVAL;
return -EPERM;
if (bus_pid_changed(bus))
return -ECHILD;
return 0;
}
if (!bus)
return -EINVAL;
return -EPERM;
if (bus_pid_changed(bus))
return -ECHILD;
return 0;
}
if (!bus)
return -EINVAL;
return -EPERM;
if (bus_pid_changed(bus))
return -ECHILD;
return 0;
}
if (!bus)
return -EINVAL;
return -EPERM;
if (bus_pid_changed(bus))
return -ECHILD;
return 0;
}
if (!bus)
return -EINVAL;
return -EPERM;
if (bus_pid_changed(bus))
return -ECHILD;
return 0;
}
if (!bus)
return -EINVAL;
return -EPERM;
if (bus_pid_changed(bus))
return -ECHILD;
return 0;
}
if (!bus)
return -EINVAL;
return -EPERM;
if (bus_pid_changed(bus))
return -ECHILD;
return 0;
}
if (!bus)
return -EINVAL;
return -EPERM;
if (bus_pid_changed(bus))
return -ECHILD;
return 0;
}
if (!bus)
return -EINVAL;
return -EINVAL;
return -EPERM;
if (bus_pid_changed(bus))
return -ECHILD;
return 0;
}
if (!bus)
return -EINVAL;
return -EPERM;
if (bus_pid_changed(bus))
return -ECHILD;
bus->anonymous_auth = !!b;
return 0;
}
const char *s;
int r;
r = bus_message_to_errno(reply);
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",
"Hello",
&m);
if (r < 0)
return r;
}
return 1;
}
return 1;
}
size_t l, n = 0;
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, *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);
a += 3;
} else {
c = *a;
a++;
}
t = realloc(r, n + 2);
if (!t) {
free(r);
return -ENOMEM;
}
r = t;
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) ++;
char **x;
if (!x) {
r = -ENOMEM;
goto fail;
}
argv = x;
}
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;
}
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;
}
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 (;;) {
sd_bus_close(b);
r = bus_socket_connect(b);
if (r >= 0)
return r;
b->last_connect_error = -r;
} else if (b->exec_path) {
r = bus_socket_exec(b);
if (r >= 0)
return r;
b->last_connect_error = -r;
} else if (b->kernel) {
r = bus_kernel_connect(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;
if (!bus)
return -EINVAL;
return -EPERM;
if (bus_pid_changed(bus))
return -ECHILD;
return -EINVAL;
r = bus_start_fd(bus);
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;
if (!ret)
return -EINVAL;
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);
if (r < 0)
goto fail;
} else {
b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + sizeof("/run/dbus/system_bus_socket") - 1;
}
b->bus_client = true;
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;
size_t l;
int r;
if (!ret)
return -EINVAL;
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) {
r = -ENOENT;
goto fail;
}
l = strlen(e);
r = -E2BIG;
goto fail;
}
}
b->bus_client = true;
r = sd_bus_start(b);
if (r < 0)
goto fail;
*ret = b;
return 0;
fail:
bus_free(b);
return r;
}
if (!bus)
return;
return;
if (bus_pid_changed(bus))
return;
/* 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_MSG_RELEASE ioctl on the fd when they are
* freed. */
}
if (!bus)
return NULL;
return bus;
}
if (!bus)
return NULL;
return NULL;
}
if (!bus)
return -EINVAL;
if (bus_pid_changed(bus))
return -ECHILD;
}
int r;
if (!bus)
return -EINVAL;
return -ENOTCONN;
if (bus_pid_changed(bus))
return -ECHILD;
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;
if (!bus)
return -EINVAL;
if (!server_id)
return -EINVAL;
if (bus_pid_changed(bus))
return -ECHILD;
r = bus_ensure_running(bus);
if (r < 0)
return r;
return 0;
}
assert(m);
return -EPERM;
if (m->sealed)
return 0;
return bus_message_seal(m, ++b->serial);
}
int r, ret = 0;
while (bus->wqueue_size > 0) {
else
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;
}
sd_bus_message *z = NULL;
int r, ret = 0;
assert(m);
if (bus->rqueue_size > 0) {
/* Dispatch a queued message */
bus->rqueue_size --;
return 1;
}
/* Try to read a new message */
do {
r = bus_kernel_read_message(bus, &z);
else
r = bus_socket_read_message(bus, &z);
if (r < 0) {
return r;
}
if (r == 0)
return ret;
ret = 1;
} while (!z);
*m = z;
return ret;
}
int r;
if (!bus)
return -EINVAL;
return -ENOTCONN;
if (!m)
return -EINVAL;
if (bus_pid_changed(bus))
return -ECHILD;
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);
if (r < 0)
return r;
/* If this is a reply and no reply was requested, then let's
* suppress this, if we can */
return 0;
r = bus_kernel_write_message(bus, m);
else
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 {
sd_bus_message **q;
/* Just append it to the queue. */
return -ENOBUFS;
if (!q)
return -ENOMEM;
}
if (serial)
*serial = BUS_MESSAGE_SERIAL(m);
return 0;
}
return 0;
if (usec == 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_message *m,
void *userdata,
struct reply_callback *c;
int r;
if (!bus)
return -EINVAL;
return -ENOTCONN;
if (!m)
return -EINVAL;
if (!callback)
return -EINVAL;
return -EINVAL;
return -EINVAL;
if (bus_pid_changed(bus))
return -ECHILD;
if (r < 0)
return r;
if (r < 0)
return r;
}
r = bus_seal_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 (!bus)
return -EINVAL;
if (serial == 0)
return -EINVAL;
if (bus_pid_changed(bus))
return -ECHILD;
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;
}
}
sd_bus_message *m,
sd_bus_message **reply) {
int r;
bool room = false;
if (!bus)
return -EINVAL;
return -ENOTCONN;
if (!m)
return -EINVAL;
return -EINVAL;
return -EINVAL;
if (bus_error_is_dirty(error))
return -EINVAL;
if (bus_pid_changed(bus))
return -ECHILD;
r = bus_ensure_running(bus);
if (r < 0)
return r;
if (r < 0)
return r;
for (;;) {
if (!room) {
sd_bus_message **q;
return -ENOBUFS;
/* Make sure there's room for queuing this
* locally, before we read the message */
if (!q)
return -ENOMEM;
room = true;
}
else
if (r < 0)
return r;
if (incoming) {
/* Found a match! */
if (reply)
else
return 0;
}
int k;
if (r < 0) {
return r;
}
return k;
}
return -EIO;
}
/* There's already guaranteed to be room for
* this, so need to resize things here */
room = false;
/* Try to read more, right-away */
continue;
}
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;
r = dispatch_wqueue(bus);
if (r < 0)
return r;
}
}
if (!bus)
return -EINVAL;
return -ENOTCONN;
return -EPERM;
if (bus_pid_changed(bus))
return -ECHILD;
}
int flags = 0;
if (!bus)
return -EINVAL;
return -ENOTCONN;
if (bus_pid_changed(bus))
return -ECHILD;
if (bus->rqueue_size <= 0)
if (bus->wqueue_size > 0)
}
return flags;
}
struct reply_callback *c;
if (!bus)
return -EINVAL;
if (!timeout_usec)
return -EINVAL;
return -ENOTCONN;
if (bus_pid_changed(bus))
return -ECHILD;
return 1;
}
return 0;
}
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;
free(c);
return r < 0 ? r : 1;
}
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;
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;
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;
char sid[33];
r = sd_id128_get_machine(&id);
if (r < 0)
return r;
if (r < 0)
return r;
} else {
"org.freedesktop.DBus.Error.UnknownMethod",
}
if (r < 0)
return r;
if (r < 0)
return r;
return 1;
}
static int node_vtable_get_userdata(
const char *path,
struct node_vtable *c,
void **userdata) {
void *u;
int r;
assert(c);
u = c->userdata;
if (c->find) {
if (r <= 0)
return r;
}
if (userdata)
*userdata = u;
return 1;
}
static void *vtable_property_convert_userdata(const sd_bus_vtable *p, void *u) {
assert(p);
}
static int vtable_property_get_userdata(
const char *path,
struct vtable_member *p,
void **userdata) {
void *u;
int r;
assert(p);
if (r <= 0)
return r;
return 1;
}
static int add_enumerated_to_set(sd_bus *bus, const char *prefix, struct node_enumerator *first, Set *s) {
struct node_enumerator *c;
int r;
assert(s);
if (r < 0)
return r;
STRV_FOREACH(k, children) {
if (r < 0) {
free(*k);
continue;
}
free(*k);
r = -EINVAL;
continue;
}
r = set_consume(s, *k);
}
if (r < 0)
return r;
}
return 0;
}
struct node *i;
int r;
assert(n);
assert(s);
if (r < 0)
return r;
char *t;
if (!t)
return -ENOMEM;
r = set_consume(s, t);
if (r < 0 && r != -EEXIST)
return r;
if (r < 0)
return r;
}
return 0;
}
int r;
assert(n);
if (!s)
return -ENOMEM;
if (r < 0) {
set_free_free(s);
return r;
}
*_s = s;
return 0;
}
static int node_callbacks_run(
sd_bus_message *m,
struct node_callback *first,
bool require_fallback,
bool *found_object) {
struct node_callback *c;
int r;
assert(m);
if (require_fallback && !c->is_fallback)
continue;
*found_object = true;
continue;
r = sd_bus_message_rewind(m, true);
if (r < 0)
return r;
if (r != 0)
return r;
}
return 0;
}
static int method_callbacks_run(
sd_bus_message *m,
struct vtable_member *c,
bool require_fallback,
bool *found_object) {
const char *signature;
void *u;
int r;
assert(m);
assert(c);
return 0;
if (r <= 0)
return r;
*found_object = true;
r = sd_bus_message_rewind(m, true);
if (r < 0)
return r;
r = sd_bus_message_get_signature(m, true, &signature);
if (r < 0)
return r;
r = sd_bus_reply_method_errorf(bus, m,
"org.freedesktop.DBus.Error.InvalidArgs",
"Invalid arguments '%s' to call %s:%s, expecting '%s'.",
if (r < 0)
return r;
return 1;
}
/* If the method callback is NULL, make this a successful NOP */
if (r < 0)
return r;
return 1;
}
static int invoke_property_get(
const sd_bus_vtable *v,
const char *path,
const char *interface,
const char *property,
sd_bus_message *m,
void *userdata) {
int r;
void *p;
assert(v);
/* Automatic handling if no callback is defined. */
case SD_BUS_TYPE_STRING:
case SD_BUS_TYPE_OBJECT_PATH:
case SD_BUS_TYPE_SIGNATURE:
p = *(char**) userdata;
break;
default:
p = userdata;
break;
}
if (r < 0)
return r;
return 1;
}
static int invoke_property_set(
const sd_bus_vtable *v,
const char *path,
const char *interface,
const char *property,
void *userdata) {
int r;
assert(v);
/* Automatic handling if no callback is defined. */
case SD_BUS_TYPE_STRING:
case SD_BUS_TYPE_OBJECT_PATH:
case SD_BUS_TYPE_SIGNATURE: {
const char *p;
char *n;
if (r < 0)
return r;
n = strdup(p);
if (!n)
return -ENOMEM;
*(char**) userdata = n;
break;
}
default:
if (r < 0)
return r;
break;
}
return 1;
}
static int property_get_set_callbacks_run(
sd_bus_message *m,
struct vtable_member *c,
bool require_fallback,
bool is_get,
bool *found_object) {
void *u;
int r;
assert(m);
return 0;
if (r <= 0)
return r;
*found_object = true;
if (r < 0)
return r;
if (is_get) {
if (r < 0)
return r;
if (r < 0)
return r;
if (sd_bus_error_is_set(&error)) {
if (r < 0)
return r;
return 1;
}
if (r < 0)
return r;
} else {
sd_bus_error_setf(&error, "org.freedesktop.DBus.Error.PropertyReadOnly", "Property '%s' is not writable.", c->member);
else {
if (r < 0)
return r;
if (r < 0)
return r;
}
if (sd_bus_error_is_set(&error)) {
if (r < 0)
return r;
return 1;
}
r = sd_bus_message_exit_container(m);
if (r < 0)
return r;
}
if (r < 0)
return r;
return 1;
}
static int vtable_append_all_properties(
const char *path,
struct node_vtable *c,
void *userdata,
sd_bus_error *error) {
const sd_bus_vtable *v;
int r;
assert(c);
continue;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
r = invoke_property_get(bus, v, path, c->interface, v->property.member, reply, error, vtable_property_convert_userdata(v, userdata));
if (r < 0)
return r;
if (sd_bus_error_is_set(error))
return 0;
if (r < 0)
return r;
if (r < 0)
return r;
}
return 1;
}
static int property_get_all_callbacks_run(
sd_bus_message *m,
struct node_vtable *first,
bool require_fallback,
const char *iface,
bool *found_object) {
struct node_vtable *c;
bool found_interface = false;
int r;
assert(m);
if (r < 0)
return r;
if (r < 0)
return r;
void *u;
if (require_fallback && !c->is_fallback)
continue;
if (r < 0)
return r;
if (r == 0)
continue;
*found_object = true;
continue;
found_interface = true;
if (r < 0)
return r;
if (sd_bus_error_is_set(&error)) {
if (r < 0)
return r;
return 1;
}
}
if (!found_interface) {
bus, m,
"org.freedesktop.DBus.Error.UnknownInterface",
"Unknown interface '%s'.", iface);
if (r < 0)
return r;
return 1;
}
if (r < 0)
return r;
if (r < 0)
return r;
return 1;
}
if (n->object_manager)
return true;
if (n->parent)
return false;
}
struct node_vtable *c;
struct node_callback *k;
assert(n);
/* Tests if there's anything attached directly to this node
* for the specified path */
if (require_fallback && !k->is_fallback)
continue;
return true;
}
if (require_fallback && !c->is_fallback)
continue;
return true;
}
}
static int process_introspect(
sd_bus_message *m,
struct node *n,
bool require_fallback,
bool *found_object) {
struct introspect intro;
struct node_vtable *c;
bool empty;
int r;
assert(m);
assert(n);
if (r < 0)
return r;
r = introspect_begin(&intro);
if (r < 0)
return r;
if (r < 0)
return r;
empty = set_isempty(s);
if (require_fallback && !c->is_fallback)
continue;
if (r < 0)
return r;
if (r == 0)
continue;
empty = false;
if (r < 0)
goto finish;
}
if (empty) {
/* Nothing?, let's see if we exist at all, and if not
* refuse to do anything */
if (r < 0)
return r;
if (r == 0)
goto finish;
}
*found_object = true;
if (r < 0)
goto finish;
if (r < 0)
goto finish;
if (r < 0)
goto finish;
r = 1;
return r;
}
static int object_manager_serialize_vtable(
const char *path,
struct node_vtable *c,
sd_bus_error *error) {
void *u;
int r;
assert(c);
if (r <= 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
return 0;
}
static int object_manager_serialize_path(
const char *prefix,
const char *path,
bool require_fallback,
sd_bus_error *error) {
struct node_vtable *i;
struct node *n;
int r;
if (!n)
return 0;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (require_fallback && !i->is_fallback)
continue;
if (r < 0)
return r;
if (sd_bus_error_is_set(error))
return 0;
}
if (r < 0)
return r;
if (r < 0)
return r;
return 1;
}
static int object_manager_serialize_path_and_fallbacks(
const char *path,
sd_bus_error *error) {
int r;
/* First, add all vtables registered for this path */
if (r < 0)
return r;
if (sd_bus_error_is_set(error))
return 0;
/* Second, add fallback vtables registered for any of the prefixes */
if (pl > 1) {
char p[pl + 1];
for (;;) {
char *e;
e = strrchr(p, '/');
if (e == p || !e)
break;
*e = 0;
if (r < 0)
return r;
if (sd_bus_error_is_set(error))
return 0;
}
}
return 0;
}
static int process_get_managed_objects(
sd_bus_message *m,
struct node *n,
bool require_fallback,
bool *found_object) {
bool empty;
int r;
assert(m);
assert(n);
if (!bus_node_with_object_manager(bus, n))
return 0;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
empty = set_isempty(s);
if (empty) {
struct node_vtable *c;
/* Hmm, so we have no children? Then let's check
* whether we exist at all, i.e. whether at least one
* vtable exists. */
if (require_fallback && !c->is_fallback)
continue;
if (r < 0)
return r;
if (r == 0)
continue;
empty = false;
break;
}
if (empty)
return 0;
} else {
Iterator i;
char *path;
SET_FOREACH(path, s, i) {
if (r < 0)
return -ENOMEM;
if (sd_bus_error_is_set(&error)) {
if (r < 0)
return r;
return 1;
}
}
}
if (r < 0)
return r;
if (r < 0)
return r;
return 1;
}
static int object_find_and_run(
sd_bus_message *m,
const char *p,
bool require_fallback,
bool *found_object) {
struct node *n;
struct vtable_member vtable_key, *v;
int r;
assert(m);
assert(p);
if (!n)
return 0;
/* First, try object callbacks */
if (r != 0)
return r;
return 0;
/* Then, look for a known method */
vtable_key.path = (char*) p;
if (v) {
if (r != 0)
return r;
}
/* Then, look for a known property */
bool get = false;
r = sd_bus_message_rewind(m, true);
if (r < 0)
return r;
vtable_key.path = (char*) p;
if (r < 0)
return r;
if (v) {
if (r != 0)
return r;
}
const char *iface;
r = sd_bus_message_rewind(m, true);
if (r < 0)
return r;
if (r < 0)
return r;
if (iface[0] == 0)
if (r != 0)
return r;
}
if (r != 0)
return r;
} else if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus.ObjectManager", "GetManagedObjects")) {
if (r != 0)
return r;
}
if (!*found_object) {
if (r < 0)
return r;
if (r > 0)
*found_object = true;
}
return 0;
}
int r;
bool found_object = false;
assert(m);
return 0;
if (!m->path)
return 0;
return 0;
do {
char p[pl+1];
bus->nodes_modified = false;
if (r != 0)
return r;
/* Look for fallback prefixes */
for (;;) {
char *e;
if (streq(p, "/"))
break;
if (bus->nodes_modified)
break;
e = strrchr(p, '/');
assert(e);
if (e == p)
*(e+1) = 0;
else
*e = 0;
if (r != 0)
return r;
}
} while (bus->nodes_modified);
if (!found_object)
return 0;
bus, m,
"org.freedesktop.DBus.Error.UnknownProperty",
"Unknown property or interface.");
else
bus, m,
"org.freedesktop.DBus.Error.UnknownMethod",
if (r < 0)
return r;
return 1;
}
int r;
assert(m);
bus->iteration_counter++;
r = process_hello(bus, m);
if (r != 0)
return r;
r = process_reply(bus, m);
if (r != 0)
return r;
r = process_filter(bus, m);
if (r != 0)
return r;
r = process_match(bus, m);
if (r != 0)
return r;
r = process_builtin(bus, m);
if (r != 0)
return r;
return process_object(bus, m);
}
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;
}
bus, m,
"org.freedesktop.DBus.Error.UnknownObject",
"Unknown object '%s'.", m->path);
if (r < 0)
return r;
}
return 1;
if (r >= 0 && ret)
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. */
if (!bus)
return -EINVAL;
if (bus_pid_changed(bus))
return -ECHILD;
/* We don't allow recursively invoking sd_bus_process(). */
if (bus->processing)
return -EBUSY;
case BUS_UNSET:
case BUS_CLOSED:
return -ENOTCONN;
case BUS_OPENING:
if (r < 0)
return r;
if (ret)
return r;
case BUS_AUTHENTICATING:
if (r < 0)
return r;
if (ret)
return r;
case BUS_RUNNING:
case BUS_HELLO:
bus->processing = true;
bus->processing = false;
return r;
}
assert_not_reached("Unknown state");
}
struct pollfd p[2] = {};
int r, e, n;
return -ENOTCONN;
e = sd_bus_get_events(bus);
if (e < 0)
return e;
if (need_more)
e |= POLLIN;
if (r < 0)
return r;
if (r == 0)
m = (uint64_t) -1;
else {
}
m = timeout_usec;
p[0].events = e;
n = 1;
} else {
n = 2;
}
if (r < 0)
return -errno;
return r > 0 ? 1 : 0;
}
if (!bus)
return -EINVAL;
return -ENOTCONN;
if (bus_pid_changed(bus))
return -ECHILD;
if (bus->rqueue_size > 0)
return 0;
}
int r;
if (!bus)
return -EINVAL;
return -ENOTCONN;
if (bus_pid_changed(bus))
return -ECHILD;
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;
}
}
struct filter_callback *f;
if (!bus)
return -EINVAL;
if (!callback)
return -EINVAL;
if (bus_pid_changed(bus))
return -ECHILD;
if (!f)
return -ENOMEM;
bus->filter_callbacks_modified = true;
return 0;
}
struct filter_callback *f;
if (!bus)
return -EINVAL;
if (!callback)
return -EINVAL;
if (bus_pid_changed(bus))
return -ECHILD;
bus->filter_callbacks_modified = true;
free(f);
return 1;
}
}
return 0;
}
const char *e;
char *s, *p;
int r;
if (n)
return n;
if (r < 0)
return NULL;
if (!s)
return NULL;
else {
assert(e);
if (!parent) {
free(s);
return NULL;
}
}
if (!n)
return NULL;
n->path = s;
if (r < 0) {
free(s);
free(n);
return NULL;
}
if (parent)
return n;
}
assert(b);
if (!n)
return;
if (n->child ||
n->callbacks ||
n->vtables ||
n->enumerators ||
n->object_manager)
return;
if (n->parent)
bus_node_gc(b, n->parent);
free(n);
}
static int bus_add_object(
sd_bus *b,
bool fallback,
const char *path,
void *userdata) {
struct node_callback *c;
struct node *n;
int r;
if (!b)
return -EINVAL;
if (!object_path_is_valid(path))
return -EINVAL;
if (!callback)
return -EINVAL;
if (bus_pid_changed(b))
return -ECHILD;
n = bus_node_allocate(b, path);
if (!n)
return -ENOMEM;
if (!c) {
r = -ENOMEM;
goto fail;
}
c->node = n;
c->is_fallback = fallback;
return 0;
fail:
free(c);
bus_node_gc(b, n);
return r;
}
static int bus_remove_object(
bool fallback,
const char *path,
void *userdata) {
struct node_callback *c;
struct node *n;
if (!bus)
return -EINVAL;
if (!object_path_is_valid(path))
return -EINVAL;
if (!callback)
return -EINVAL;
if (bus_pid_changed(bus))
return -ECHILD;
if (!n)
return 0;
break;
if (!c)
return 0;
free(c);
bus_node_gc(bus, n);
return 1;
}
int sd_bus_add_object(sd_bus *bus, const char *path, sd_bus_message_handler_t callback, void *userdata) {
}
int sd_bus_remove_object(sd_bus *bus, const char *path, sd_bus_message_handler_t callback, void *userdata) {
}
int sd_bus_add_fallback(sd_bus *bus, const char *prefix, sd_bus_message_handler_t callback, void *userdata) {
}
int sd_bus_remove_fallback(sd_bus *bus, const char *prefix, sd_bus_message_handler_t callback, void *userdata) {
}
int sd_bus_add_match(sd_bus *bus, const char *match, sd_bus_message_handler_t callback, void *userdata) {
unsigned n_components = 0;
int r = 0;
if (!bus)
return -EINVAL;
if (!match)
return -EINVAL;
if (bus_pid_changed(bus))
return -ECHILD;
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;
}
int sd_bus_remove_match(sd_bus *bus, const char *match, sd_bus_message_handler_t callback, void *userdata) {
unsigned n_components = 0;
int r = 0, q = 0;
if (!bus)
return -EINVAL;
if (!match)
return -EINVAL;
if (bus_pid_changed(bus))
return -ECHILD;
if (r < 0)
return r;
bus->match_callbacks_modified = true;
if (bus->bus_client)
return r < 0 ? r : q;
}
int sd_bus_emit_signal(
const char *path,
const char *interface,
const char *member,
const char *types, ...) {
int r;
if (!bus)
return -EINVAL;
return -ENOTCONN;
if (bus_pid_changed(bus))
return -ECHILD;
if (r < 0)
return r;
if (r < 0)
return r;
}
}
int sd_bus_call_method(
const char *destination,
const char *path,
const char *interface,
const char *member,
const char *types, ...) {
int r;
if (!bus)
return -EINVAL;
return -ENOTCONN;
if (bus_pid_changed(bus))
return -ECHILD;
if (r < 0)
return r;
if (r < 0)
return r;
}
}
const char *types, ...) {
int r;
if (!bus)
return -EINVAL;
if (!call)
return -EINVAL;
return -EPERM;
return -EINVAL;
return -ENOTCONN;
if (bus_pid_changed(bus))
return -ECHILD;
return 0;
if (r < 0)
return r;
if (r < 0)
return r;
}
}
const sd_bus_error *e) {
int r;
if (!bus)
return -EINVAL;
if (!call)
return -EINVAL;
return -EPERM;
return -EINVAL;
if (!sd_bus_error_is_set(e))
return -EINVAL;
return -ENOTCONN;
if (bus_pid_changed(bus))
return -ECHILD;
return 0;
if (r < 0)
return r;
}
const char *name,
const char *format,
...) {
char *n, *m;
int r;
if (!n)
return -ENOMEM;
if (format) {
if (r < 0) {
free(n);
return -ENOMEM;
}
}
}
/* We don't support people creating a bus connection and
* keeping it around over a fork(). Let's complain. */
}
if (!w)
return;
const sd_bus_vtable *v;
struct vtable_member *x = NULL;
switch (v->type) {
case _SD_BUS_VTABLE_METHOD: {
struct vtable_member key;
break;
}
case _SD_BUS_VTABLE_PROPERTY:
case _SD_BUS_VTABLE_WRITABLE_PROPERTY: {
struct vtable_member key;
break;
}}
free(x);
}
}
free(w);
}
static unsigned vtable_member_hash_func(const void *a) {
const struct vtable_member *m = a;
return
string_hash_func(m->path) ^
string_hash_func(m->interface) ^
string_hash_func(m->member);
}
static int vtable_member_compare_func(const void *a, const void *b) {
const struct vtable_member *x = a, *y = b;
int r;
if (r != 0)
return r;
if (r != 0)
return r;
}
static int add_object_vtable_internal(
const char *path,
const char *interface,
const sd_bus_vtable *vtable,
bool fallback,
void *userdata) {
struct node_vtable *c = NULL, *i;
const sd_bus_vtable *v;
struct node *n;
int r;
if (!bus)
return -EINVAL;
if (!object_path_is_valid(path))
return -EINVAL;
if (!interface_name_is_valid(interface))
return -EINVAL;
if (!vtable || vtable[0].type != _SD_BUS_VTABLE_START || vtable[0].start.element_size != sizeof(struct sd_bus_vtable))
return -EINVAL;
if (bus_pid_changed(bus))
return -ECHILD;
r = hashmap_ensure_allocated(&bus->vtable_methods, vtable_member_hash_func, vtable_member_compare_func);
if (r < 0)
return r;
r = hashmap_ensure_allocated(&bus->vtable_properties, vtable_member_hash_func, vtable_member_compare_func);
if (r < 0)
return r;
if (!n)
return -ENOMEM;
r = -EEXIST;
goto fail;
}
if (i->is_fallback != fallback) {
r = -EPROTOTYPE;
goto fail;
}
}
if (!c) {
r = -ENOMEM;
goto fail;
}
c->node = n;
c->is_fallback = fallback;
if (!c->interface) {
r = -ENOMEM;
goto fail;
}
switch (v->type) {
case _SD_BUS_VTABLE_METHOD: {
struct vtable_member *m;
r = -EINVAL;
goto fail;
}
if (!m) {
r = -ENOMEM;
goto fail;
}
m->parent = c;
m->vtable = v;
if (r < 0) {
free(m);
goto fail;
}
break;
}
r = -EINVAL;
goto fail;
}
/* Fall through */
case _SD_BUS_VTABLE_PROPERTY: {
struct vtable_member *m;
v->flags & SD_BUS_VTABLE_METHOD_NO_REPLY ||
(v->flags & SD_BUS_VTABLE_PROPERTY_INVALIDATE_ONLY && !(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE))) {
r = -EINVAL;
goto fail;
}
if (!m) {
r = -ENOMEM;
goto fail;
}
m->parent = c;
m->vtable = v;
if (r < 0) {
free(m);
goto fail;
}
break;
}
case _SD_BUS_VTABLE_SIGNAL:
r = -EINVAL;
goto fail;
}
break;
default:
r = -EINVAL;
goto fail;
}
}
return 0;
fail:
if (c)
free_node_vtable(bus, c);
bus_node_gc(bus, n);
return r;
}
static int remove_object_vtable_internal(
const char *path,
const char *interface,
bool fallback) {
struct node_vtable *c;
struct node *n;
if (!bus)
return -EINVAL;
if (!object_path_is_valid(path))
return -EINVAL;
if (!interface_name_is_valid(interface))
return -EINVAL;
if (bus_pid_changed(bus))
return -ECHILD;
if (!n)
return 0;
break;
if (!c)
return 0;
free_node_vtable(bus, c);
return 1;
}
const char *path,
const char *interface,
const sd_bus_vtable *vtable,
void *userdata) {
}
const char *path,
const char *interface) {
}
const char *path,
const char *interface,
const sd_bus_vtable *vtable,
void *userdata) {
}
const char *path,
const char *interface) {
}
const char *path,
void *userdata) {
struct node_enumerator *c;
struct node *n;
int r;
if (!bus)
return -EINVAL;
if (!object_path_is_valid(path))
return -EINVAL;
if (!callback)
return -EINVAL;
if (bus_pid_changed(bus))
return -ECHILD;
if (!n)
return -ENOMEM;
if (!c) {
r = -ENOMEM;
goto fail;
}
c->node = n;
return 0;
fail:
free(c);
bus_node_gc(bus, n);
return r;
}
const char *path,
void *userdata) {
struct node_enumerator *c;
struct node *n;
if (!bus)
return -EINVAL;
if (!object_path_is_valid(path))
return -EINVAL;
if (!callback)
return -EINVAL;
if (bus_pid_changed(bus))
return -ECHILD;
if (!n)
return 0;
break;
if (!c)
return 0;
free(c);
bus_node_gc(bus, n);
return 1;
}
static int emit_properties_changed_on_interface(
const char *prefix,
const char *path,
const char *interface,
bool require_fallback,
char **names) {
bool has_invalidating = false;
struct vtable_member key;
struct node_vtable *c;
struct node *n;
char **property;
void *u = NULL;
int r;
if (!n)
return 0;
if (require_fallback && !c->is_fallback)
continue;
break;
}
if (!c)
return 0;
if (r <= 0)
return r;
r = sd_bus_message_new_signal(bus, path, "org.freedesktop.DBus.Properties", "PropertiesChanged", &m);
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
struct vtable_member *v;
if (!v)
return -ENOENT;
return -EDOM;
has_invalidating = true;
continue;
}
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
r = invoke_property_get(bus, v->vtable, m->path, interface, *property, m, &error, vtable_property_convert_userdata(v->vtable, u));
if (r < 0)
return r;
if (sd_bus_error_is_set(&error))
return bus_error_to_errno(&error);
r = sd_bus_message_close_container(m);
if (r < 0)
return r;
r = sd_bus_message_close_container(m);
if (r < 0)
return r;
}
r = sd_bus_message_close_container(m);
if (r < 0)
return r;
if (r < 0)
return r;
if (has_invalidating) {
struct vtable_member *v;
continue;
if (r < 0)
return r;
}
}
r = sd_bus_message_close_container(m);
if (r < 0)
return r;
if (r < 0)
return r;
return 1;
}
int sd_bus_emit_properties_changed_strv(sd_bus *bus, const char *path, const char *interface, char **names) {
int r;
if (!bus)
return -EINVAL;
if (!object_path_is_valid(path))
return -EINVAL;
if (!interface_name_is_valid(interface))
return -EINVAL;
if (r != 0)
return r;
if (pl > 1 ) {
char p[pl+1];
for (;;) {
char *e;
if (streq(p, "/"))
break;
e = strrchr(p, '/');
assert(e);
if (e == p)
*(e+1) = 0;
else
*e = 0;
if (r != 0)
return r;
}
}
return -ENOENT;
}
int sd_bus_emit_properties_changed(sd_bus *bus, const char *path, const char *interface, const char *name, ...) {
if (!names)
return -ENOMEM;
}
return -ENOSYS;
}
return -ENOSYS;
}
int sd_bus_get_property(
const char *destination,
const char *path,
const char *interface,
const char *member,
const char *type) {
int r;
return -EINVAL;
if (!member_name_is_valid(member))
return -EINVAL;
if (!signature_is_single(type, false))
return -EINVAL;
if (!reply)
return -EINVAL;
r = sd_bus_call_method(bus, destination, path, "org.freedesktop.DBus.Properties", "Get", error, &rep, "ss", strempty(interface), member);
if (r < 0)
return r;
if (r < 0) {
return r;
}
return 0;
}
int sd_bus_set_property(
const char *destination,
const char *path,
const char *interface,
const char *member,
const char *type, ...) {
int r;
return -EINVAL;
if (!member_name_is_valid(member))
return -EINVAL;
if (!signature_is_single(type, false))
return -EINVAL;
r = sd_bus_message_new_method_call(bus, destination, path, "org.freedesktop.DBus.Properties", "Set", &m);
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
r = sd_bus_message_close_container(m);
if (r < 0)
return r;
}
struct node *n;
if (!bus)
return -EINVAL;
if (!object_path_is_valid(path))
return -EINVAL;
if (bus_pid_changed(bus))
return -ECHILD;
if (!n)
return -ENOMEM;
n->object_manager = true;
return 0;
}
struct node *n;
if (!bus)
return -EINVAL;
if (!object_path_is_valid(path))
return -EINVAL;
if (bus_pid_changed(bus))
return -ECHILD;
if (!n)
return 0;
if (!n->object_manager)
return 0;
n->object_manager = false;
bus_node_gc(bus, n);
return 1;
}