sd-bus.c revision de1c301ed165eb4d04a0c9d4babe97912b5233bb
/*-*- 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 "sd-bus.h"
#include "bus-internal.h"
#include "bus-message.h"
#include "bus-type.h"
#define WQUEUE_MAX 128
struct filter_callback *f;
assert(b);
if (b->fd >= 0)
close_nointr_nofail(b->fd);
free(b->unique_name);
while ((f = b->filter_callbacks)) {
free(f);
}
free(b);
}
sd_bus *r;
if (!r)
return NULL;
r->n_ref = 1;
r->fd = -1;
r->message_version = 1;
/* We guarantee that wqueue always has space for at least one
* entry */
if (!r->wqueue) {
free(r);
return NULL;
}
return r;
};
const char *s;
int r;
if (r < 0)
return r;
if (!bus->unique_name)
return -ENOMEM;
return 1;
}
int r;
bus,
"org.freedesktop.DBus",
"/",
"org.freedesktop.DBus",
"Hello",
&m);
if (r < 0)
return r;
if (r < 0)
return r;
return 0;
}
int r;
if (bus->send_hello) {
r = bus_send_hello(bus);
if (r < 0)
return r;
}
return 0;
}
size_t l, n = 0;
const char *a;
char *r = NULL;
assert(p);
assert(*p);
return 0;
if ((*p)[l] != '=')
return 0;
if (*value)
return -EINVAL;
a = *p + l + 1;
while (*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;
}
a += 3;
c = (char) ((x << 4) | y);
} else
c = *a;
t = realloc(r, n + 1);
if (!t) {
free(r);
return -ENOMEM;
}
r = t;
r[n++] = c;
}
*p = a;
*value = r;
return 1;
}
static void skip_address_key(const char **p) {
assert(p);
assert(*p);
*p += strcspn(*p, ";");
}
static int bus_parse_next_address(sd_bus *b) {
const char *a, *p;
int r;
assert(b);
if (!b->address)
return 0;
if (b->address[b->address_index] == 0)
return 0;
a = b->address + b->address_index;
b->sockaddr_size = 0;
b->peer = SD_ID128_NULL;
if (startswith(a, "unix:")) {
p = a + 5;
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) {
size_t l;
return -E2BIG;
} else if (abstract) {
size_t l;
return -E2BIG;
}
} else if (startswith(a, "tcp:")) {
p = a + 4;
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;
}
if (guid) {
if (r < 0)
return r;
}
b->address_index = p - b->address;
return 1;
}
while (size > 0) {
return;
}
i->iov_len = 0;
(*idx) ++;
}
}
static int bus_write_auth(sd_bus *b) {
ssize_t k;
assert(b);
return 0;
if (k < 0)
return 1;
}
static int bus_auth_verify(sd_bus *b) {
char *e, *f;
unsigned i;
int r;
/* We expect two response lines: "OK", "AGREE_UNIX_FD", and
* that's it */
if (!e)
return 0;
if (!f)
return 0;
return -EPERM;
return -EPERM;
for (i = 0; i < 32; i += 2) {
int x, y;
if (x < 0 || y < 0)
return -EINVAL;
}
return -EPERM;
b->can_fds =
(f - e == sizeof("\r\nAGREE_UNIX_FD") - 1) &&
b->rbuffer_size -= (f - (char*) b->rbuffer);
}
r = bus_start_running(b);
if (r < 0)
return r;
return 1;
}
static int bus_read_auth(sd_bus *b) {
size_t n;
ssize_t k;
int r;
assert(b);
r = bus_auth_verify(b);
if (r != 0)
return r;
if (k < 0)
b->rbuffer_size += k;
r = bus_auth_verify(b);
if (r != 0)
return r;
return 0;
}
static int bus_start_auth(sd_bus *b) {
static const char auth_prefix[] = "\0AUTH_EXTERNAL ";
static const char auth_suffix[] = "\r\nNEGOTIATE_UNIX_FD\r\nBEGIN\r\n";
size_t l;
assert(b);
b->state = BUS_AUTHENTICATING;
if (!b->auth_uid)
return -ENOMEM;
return bus_write_auth(b);
}
static int bus_start_connect(sd_bus *b) {
int r;
assert(b);
for (;;) {
r = bus_parse_next_address(b);
if (r < 0)
return r;
if (r == 0)
}
if (b->fd < 0) {
b->last_connect_error = -errno;
continue;
}
if (r < 0) {
if (errno == EINPROGRESS)
return 0;
b->last_connect_error = -errno;
close_nointr_nofail(b->fd);
b->fd = -1;
continue;
}
return bus_start_auth(b);
}
}
const char *e;
sd_bus *b;
int r;
if (!ret)
return -EINVAL;
e = getenv("DBUS_SYSTEM_BUS_ADDRESS");
if (e) {
r = sd_bus_open_address(e, &b);
if (r < 0)
return r;
b->send_hello = true;
*ret = b;
return r;
}
b = bus_new();
if (!b)
return -ENOMEM;
b->send_hello = true;
b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + sizeof("/run/dbus/system_bus_socket") - 1;
r = bus_start_connect(b);
if (r < 0) {
bus_free(b);
return r;
}
*ret = b;
return 0;
}
const char *e;
sd_bus *b;
size_t l;
int r;
if (!ret)
return -EINVAL;
e = getenv("DBUS_SESSION_BUS_ADDRESS");
if (e) {
r = sd_bus_open_address(e, &b);
if (r < 0)
return r;
b->send_hello = true;
*ret = b;
return r;
}
e = getenv("XDG_RUNTIME_DIR");
if (!e)
return -ENOENT;
l = strlen(e);
return -E2BIG;
b = bus_new();
if (!b)
return -ENOMEM;
b->send_hello = true;
r = bus_start_connect(b);
if (r < 0) {
bus_free(b);
return r;
}
*ret = b;
return 0;
}
sd_bus *b;
int r;
if (!address)
return -EINVAL;
if (!ret)
return -EINVAL;
b = bus_new();
if (!b)
return -ENOMEM;
if (!b->address) {
bus_free(b);
return -ENOMEM;
}
r = bus_start_connect(b);
if (r < 0) {
bus_free(b);
return r;
}
*ret = b;
return 0;
}
sd_bus *b;
int r;
if (fd < 0)
return -EINVAL;
if (!ret)
return -EINVAL;
b = bus_new();
if (!b)
return -ENOMEM;
fd_nonblock(b->fd, true);
fd_cloexec(b->fd, true);
r = bus_start_auth(b);
if (r < 0) {
bus_free(b);
return r;
}
*ret = b;
return 0;
}
if (!bus)
return;
return;
}
if (!bus)
return NULL;
return bus;
}
if (!bus)
return NULL;
return NULL;
}
if (!bus)
return -EINVAL;
return -ENOTCONN;
}
if (!bus)
return -EINVAL;
if (type == SD_BUS_TYPE_UNIX_FD)
return bus_type_is_valid(type);
}
assert(m);
if (m->sealed)
return 0;
return message_seal(m, ++b->serial);
}
ssize_t k;
size_t n;
unsigned j;
assert(m);
j = 0;
if (k < 0)
return -errno;
return j > m->n_iovec;
}
uint32_t a, b;
uint8_t e;
*need = sizeof(struct bus_header);
return 0;
}
if (e == SD_BUS_LITTLE_ENDIAN) {
a = le32toh(a);
b = le32toh(b);
} else if (e == SD_BUS_BIG_ENDIAN) {
a = be32toh(a);
b = be32toh(b);
} else
return -EIO;
return 0;
}
sd_bus_message *t;
void *b = NULL;
int r;
assert(m);
if (!t)
return -ENOMEM;
if (!b) {
free(t);
return -ENOMEM;
}
}
t->n_ref = 1;
t->free_header = true;
t->body = (uint8_t*) bus->rbuffer + sizeof(struct bus_header) + ALIGN_TO(BUS_MESSAGE_BODY_SIZE(t), 8);
r = message_parse(t);
if (r < 0) {
return r;
}
*m = t;
return 1;
}
ssize_t k;
int r;
void *b;
assert(m);
if (r < 0)
return r;
if (!b)
return -ENOMEM;
if (k < 0)
bus->rbuffer_size += k;
if (r < 0)
return r;
return 0;
}
int r, c = 0;
return -ENOTCONN;
while (bus->wqueue_size > 0) {
if (r < 0) {
return r;
} else if (r == 0)
/* Wasn't fully written yet... */
break;
else {
/* 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 --;
c++;
}
}
return c;
}
int r;
assert(m);
return -ENOTCONN;
if (bus->rqueue_size > 0) {
/* Dispatch a queued message */
bus->rqueue_size --;
return 1;
}
/* Try to read a new message */
r = message_read(bus, m);
if (r < 0) {
return r;
}
return r;
}
int r;
if (!bus)
return -EINVAL;
return -ENOTCONN;
if (!m)
return -EINVAL;
return -EPERM;
r = bus_seal_message(bus, m);
if (r < 0)
return r;
if (bus->wqueue_size <= 0) {
if (r < 0) {
return r;
} else if (r == 0) {
/* 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)
}
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;
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 (r < 0) {
free(c);
return r;
}
return r;
}
struct reply_callbacks *c;
if (!bus)
return -EINVAL;
if (serial == 0)
return -EINVAL;
if (!c)
return 0;
free(c);
return 1;
}
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;
if (sd_bus_error_is_set(error))
return -EINVAL;
if (r < 0)
return r;
for (;;) {
if (!room) {
sd_bus_message **q;
/* 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 (r > 0) {
/* 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 (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 -EINVAL;
}
int flags = 0;
if (!bus)
return -EINVAL;
return -EINVAL;
if (bus->rqueue_size <= 0)
if (bus->wqueue_size > 0)
}
return flags;
}
sd_bus_message *m;
int r;
if (!bus)
return -EINVAL;
return -ENOTCONN;
struct pollfd p;
zero(p);
r = poll(&p, 1, 0);
if (r < 0)
return -errno;
int error;
if (r < 0)
return -errno;
if (error != 0)
else
return bus_start_auth(bus);
/* Try next address */
return bus_start_connect(bus);
}
return 0;
r = bus_write_auth(bus);
if (r < 0)
return r;
r = bus_read_auth(bus);
if (r <= 0)
return r;
return bus_start_running(bus);
struct filter_callback *l;
r = dispatch_wqueue(bus);
if (r < 0)
return r;
r = dispatch_rqueue(bus, &m);
if (r <= 0)
return r;
if (m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_CALL || m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_RETURN) {
struct reply_callback *c;
if (c) {
free(c);
if (r != 0) {
return r < 0 ? r : 0;
}
}
}
if (r != 0) {
return r < 0 ? r : 0;
}
}
if (ret) {
*ret = m;
return 1;
}
return 0;
}
return -ENOTSUP;
}
struct pollfd p;
int r, e;
if (!bus)
return -EINVAL;
return -ECONNREFUSED;
e = sd_bus_get_events(bus);
if (e < 0)
return e;
zero(p);
p.events = e;
if (r < 0)
return -EINVAL;
return r;
}
int r;
if (!bus)
return -EINVAL;
return -ENOTCONN;
return 0;
for (;;) {
r = dispatch_wqueue(bus);
if (r < 0)
return r;
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;
}