sd-bus.c revision 8333c77edf8fd1654cd96f3f6ee0f078dd64b58b
/*-*- 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 "util.h"
#include "macro.h"
#include "strv.h"
#include "set.h"
#include "sd-bus.h"
#include "bus-internal.h"
#include "bus-message.h"
#include "bus-type.h"
#include "bus-socket.h"
struct filter_callback *f;
struct object_callback *c;
unsigned i;
assert(b);
if (b->fd >= 0)
close_nointr_nofail(b->fd);
free(b->unique_name);
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 ((c = hashmap_steal_first(b->object_callbacks))) {
free(c);
}
free(b);
}
sd_bus *r;
if (!ret)
return -EINVAL;
if (!r)
return -ENOMEM;
r->n_ref = 1;
r->fd = -1;
r->message_version = 1;
r->negotiate_fds = true;
/* 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 (!a)
return -ENOMEM;
return 0;
}
if (!bus)
return -EINVAL;
return -EPERM;
if (fd < 0)
return -EINVAL;
return 0;
}
char *p, **a;
if (!bus)
return -EINVAL;
return -EPERM;
if (!path)
return -EINVAL;
if (strv_isempty(argv))
return -EINVAL;
if (!p)
return -ENOMEM;
if (!a) {
free(p);
return -ENOMEM;
}
return 0;
}
if (!bus)
return -EINVAL;
return -EPERM;
bus->bus_client = !!b;
return 0;
}
if (!bus)
return -EINVAL;
return -EPERM;
bus->negotiate_fds = !!b;
return 0;
}
const char *s;
int r;
if (error != 0)
return -error;
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;
if (!bus->bus_client)
return 0;
bus,
"org.freedesktop.DBus",
"/",
"org.freedesktop.DBus",
"Hello",
&m);
if (r < 0)
return r;
if (r < 0)
return r;
return r;
}
if (bus->bus_client) {
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;
}
static void bus_reset_parsed_address(sd_bus *b) {
assert(b);
b->sockaddr_size = 0;
b->peer = 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;
}
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 (;;) {
if (b->fd >= 0) {
close_nointr_nofail(b->fd);
b->fd = -1;
}
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;
}
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->fd, true);
if (r < 0)
return r;
r = fd_cloexec(b->fd, true);
if (r < 0)
return r;
return bus_socket_take_fd(b);
}
int r;
if (!bus)
return -EINVAL;
return -EPERM;
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 = 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 = getenv("DBUS_SESSION_BUS_ADDRESS");
if (e) {
r = sd_bus_set_address(b, e);
if (r < 0)
goto fail;
} else {
e = 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)
return NULL;
return bus;
}
if (!bus)
return NULL;
return NULL;
}
if (!bus)
return -EINVAL;
}
int r;
if (!bus)
return -EINVAL;
return -ENOTCONN;
if (type == SD_BUS_TYPE_UNIX_FD) {
if (!bus->negotiate_fds)
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 (!peer)
return -EINVAL;
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;
return -ENOTCONN;
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;
}
sd_bus_message *z = NULL;
int r, ret = 0;
assert(m);
return -ENOTCONN;
if (bus->rqueue_size > 0) {
/* Dispatch a queued message */
bus->rqueue_size --;
return 1;
}
/* Try to read a new message */
do {
r = bus_socket_read_message(bus, &z);
if (r < 0) {
return r;
}
if (r == 0)
return ret;
r = 1;
} while (!z);
*m = z;
return 1;
}
int r;
if (!bus)
return -EINVAL;
return -ENOTCONN;
return -ENOTCONN;
if (!m)
return -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);
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;
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;
return -ENOTCONN;
if (!m)
return -EINVAL;
if (!callback)
return -EINVAL;
return -EINVAL;
return -EINVAL;
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 (!c)
return 0;
if (c->timeout != 0)
free(c);
return 1;
}
int r;
return -ENOTCONN;
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;
return -ENOTCONN;
if (!m)
return -EINVAL;
return -EINVAL;
return -EINVAL;
if (bus_error_is_dirty(error))
return -EINVAL;
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;
}
if (r < 0)
return r;
if (incoming) {
/* Found a match! */
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;
}
int flags = 0;
if (!bus)
return -EINVAL;
return -ENOTCONN;
return -ENOTCONN;
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;
return -ENOTCONN;
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;
free(c);
return r < 0 ? r : 1;
}
struct reply_callback *c;
int r;
assert(m);
return 0;
if (!c)
return 0;
if (c->timeout != 0)
free(c);
return r;
}
struct filter_callback *l;
int r;
if (r != 0)
return r;
}
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;
}
struct object_callback *c;
char *p;
int r;
bool found = false;
assert(m);
return 0;
return 0;
if (c) {
if (r != 0)
return r;
found = true;
}
/* Look for fallback prefixes */
for (;;) {
char *e;
e = strrchr(p, '/');
if (e == p || !e)
break;
*e = 0;
if (c && c->is_fallback) {
if (r != 0)
return r;
found = true;
}
}
/* We found some handlers but none wanted to take this, then
* return this -- with one exception, we can handle
* introspection minimally ourselves */
if (!found || sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect"))
return 0;
"org.freedesktop.DBus.Error.UnknownMethod",
if (r < 0)
return r;
if (r < 0)
return r;
return 1;
}
struct object_callback *c;
Iterator i;
char *node;
int r;
assert(m);
return 0;
if (!m->path)
return 0;
if (!s)
return -ENOMEM;
const char *e;
char *a, *p;
continue;
e = c->path;
else {
if (!e || *e != '/')
continue;
}
a = strdup(e+1);
if (!a)
return -ENOMEM;
p = strchr(a, '/');
if (p)
*p = 0;
r = set_put(s, a);
if (r < 0) {
free(a);
if (r != -EEXIST)
return r;
}
}
if (!f)
return -ENOMEM;
fputs("<node>\n", f);
while ((node = set_steal_first(s))) {
}
fputs("</node>\n", f);
fflush(f);
if (ferror(f))
return -ENOMEM;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
return 1;
}
int r;
assert(m);
r = process_reply(bus, m);
if (r != 0)
return r;
r = process_filter(bus, m);
if (r != 0)
return r;
r = process_builtin(bus, m);
if (r != 0)
return r;
r = process_object(bus, m);
if (r != 0)
return r;
return process_introspect(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) {
*ret = m;
m = NULL;
return 1;
}
sd_bus_error_set(&error, "org.freedesktop.DBus.Error.UnknownObject", "Unknown object '%s'.", m->path);
if (r < 0)
return r;
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;
return -ENOTCONN;
case BUS_UNSET:
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:
}
assert_not_reached("Unknown state");
}
struct pollfd p;
int r, e;
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 {
usec_t n;
n = now(CLOCK_MONOTONIC);
}
m = timeout_usec;
zero(p);
p.events = e;
if (r < 0)
return -errno;
return r > 0 ? 1 : 0;
}
if (!bus)
return -EINVAL;
return -ENOTCONN;
return -ENOTCONN;
if (bus->rqueue_size > 0)
return 0;
}
int r;
if (!bus)
return -EINVAL;
return -ENOTCONN;
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 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 (!f)
return -ENOMEM;
return 0;
}
struct filter_callback *f;
if (!bus)
return -EINVAL;
if (!callback)
return -EINVAL;
free(f);
return 1;
}
}
return 0;
}
static int bus_add_object(
bool fallback,
const char *path,
void *userdata) {
struct object_callback *c;
int r;
if (!bus)
return -EINVAL;
if (!path)
return -EINVAL;
if (!callback)
return -EINVAL;
if (r < 0)
return r;
if (!c)
return -ENOMEM;
if (!c->path) {
free(c);
return -ENOMEM;
}
c->is_fallback = fallback;
if (r < 0) {
free(c);
return r;
}
return 0;
}
static int bus_remove_object(
bool fallback,
const char *path,
void *userdata) {
struct object_callback *c;
if (!bus)
return -EINVAL;
if (!path)
return -EINVAL;
if (!callback)
return -EINVAL;
if (!c)
return 0;
return 0;
free(c);
return 1;
}
int sd_bus_add_object(sd_bus *bus, const char *path, sd_message_handler_t callback, void *userdata) {
}
int sd_bus_remove_object(sd_bus *bus, const char *path, sd_message_handler_t callback, void *userdata) {
}
int sd_bus_add_fallback(sd_bus *bus, const char *prefix, sd_message_handler_t callback, void *userdata) {
}
int sd_bus_remove_fallback(sd_bus *bus, const char *prefix, sd_message_handler_t callback, void *userdata) {
}