/***
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 <netdb.h>
#include <poll.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include "sd-bus.h"
#include "alloc-util.h"
#include "bus-container.h"
#include "bus-control.h"
#include "bus-internal.h"
#include "bus-kernel.h"
#include "bus-label.h"
#include "bus-message.h"
#include "bus-objects.h"
#include "bus-protocol.h"
#include "bus-slot.h"
#include "bus-socket.h"
#include "bus-track.h"
#include "bus-type.h"
#include "bus-util.h"
#include "cgroup-util.h"
#include "def.h"
#include "fd-util.h"
#include "hexdecoct.h"
#include "hostname-util.h"
#include "macro.h"
#include "missing.h"
#include "parse-util.h"
#include "string-util.h"
#include "strv.h"
#include "util.h"
#define log_debug_bus_message(m) \
do { \
sd_bus_message *_mm = (m); \
log_debug("Got message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%" PRIu64 " reply_cookie=%" PRIu64 " error=%s", \
_mm->reply_cookie, \
} while (false)
static int attach_io_events(sd_bus *b);
static void detach_io_events(sd_bus *b);
assert(b);
detach_io_events(b);
safe_close(b->output_fd);
}
assert(b);
while (b->rqueue_size > 0)
b->rqueue_allocated = 0;
while (b->wqueue_size > 0)
b->wqueue_allocated = 0;
}
sd_bus_slot *s;
assert(b);
assert(!b->track_queue);
b->state = BUS_CLOSED;
while ((s = b->slots)) {
/* At this point only floating slots can still be
* around, because the non-floating ones keep a
* reference to the bus, and we thus couldn't be
* destructing right now... We forcibly disconnect the
* slots here, so that they still can be referenced by
* apps, but are dead. */
}
if (b->default_bus_ptr)
*b->default_bus_ptr = NULL;
bus_close_fds(b);
if (b->kdbus_buffer)
free(b->unique_name);
free(b->auth_buffer);
free(b->fake_label);
free(b->cgroup_root);
free(b->description);
bus_reset_queues(b);
hashmap_free(b->nodes);
free(b);
}
sd_bus *r;
if (!r)
return -ENOMEM;
r->n_ref = REFCNT_INIT;
r->message_version = 1;
r->hello_flags |= KDBUS_HELLO_ACCEPT_FD;
r->attach_flags |= KDBUS_ATTACH_NAMES;
r->original_pid = getpid();
/* We guarantee that wqueue always has space for at least one
* entry */
free(r);
return -ENOMEM;
}
*ret = r;
return 0;
}
char *a;
if (!a)
return -ENOMEM;
return 0;
}
return 0;
}
char *p, **a;
if (!p)
return -ENOMEM;
if (!a) {
free(p);
return -ENOMEM;
}
return 0;
}
bus->bus_client = !!b;
return 0;
}
return 0;
}
return 0;
}
return 0;
return 0;
}
if (b)
else
/* The well knowns we need unconditionally, so that matches can work */
/* Make sure we don't lose the timestamp flag */
return 0;
return 0;
}
return 0;
}
bus->anonymous_auth = !!b;
return 0;
}
return 0;
}
}
bus->allow_interactive_authorization = !!b;
return 0;
}
return bus->allow_interactive_authorization;
}
const char *s;
int r;
if (r > 0)
return -r;
if (r < 0)
return r;
if (!service_name_is_valid(s) || s[0] != ':')
return -EBADMSG;
if (!bus->unique_name)
return -ENOMEM;
return 1;
}
int r;
return 0;
bus,
&m,
"org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus",
"Hello");
if (r < 0)
return r;
}
return 1;
}
return 1;
}
const char *a;
char *r = NULL;
assert(p);
assert(*p);
if (key) {
return 0;
if ((*p)[l] != '=')
return 0;
if (*value)
return -EINVAL;
a = *p + l + 1;
} else
a = *p;
while (*a != ';' && *a != ',' && *a != 0) {
char c;
if (*a == '%') {
int x, y;
x = unhexchar(a[1]);
if (x < 0) {
free(r);
return x;
}
y = unhexchar(a[2]);
if (y < 0) {
free(r);
return y;
}
c = (char) ((x << 4) | y);
a += 3;
} else {
c = *a;
a++;
}
return -ENOMEM;
r[n++] = c;
}
if (!r) {
r = strdup("");
if (!r)
return -ENOMEM;
} else
r[n] = 0;
if (*a == ',')
a++;
*p = a;
*value = r;
return 1;
}
static void skip_address_key(const char **p) {
assert(p);
assert(*p);
*p += strcspn(*p, ",");
if (**p == ',')
(*p) ++;
}
size_t l;
int r;
assert(b);
assert(p);
assert(*p);
while (**p != 0 && **p != ';') {
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
skip_address_key(p);
}
return -EINVAL;
return -EINVAL;
if (path) {
return -E2BIG;
} else if (abstract) {
return -E2BIG;
}
return 0;
}
int r;
};
assert(b);
assert(p);
assert(*p);
while (**p != 0 && **p != ';') {
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
skip_address_key(p);
}
return -EINVAL;
if (family) {
else
return -EINVAL;
}
if (r == EAI_SYSTEM)
return -errno;
else if (r != 0)
return -EADDRNOTAVAIL;
return 0;
}
unsigned n_argv = 0, j;
int r;
assert(b);
assert(p);
assert(*p);
while (**p != 0 && **p != ';') {
if (r < 0)
goto fail;
else if (r > 0)
continue;
if (r < 0)
goto fail;
else if (r > 0)
continue;
if (startswith(*p, "argv")) {
unsigned ul;
errno = 0;
r = -EINVAL;
goto fail;
}
(*p) ++;
r = -ENOMEM;
goto fail;
}
}
if (r < 0)
goto fail;
continue;
}
skip_address_key(p);
}
if (!path) {
r = -EINVAL;
goto fail;
}
/* Make sure there are no holes in the array, with the
* exception of argv[0] */
for (j = 1; j < n_argv; j++)
if (!argv[j]) {
r = -EINVAL;
goto fail;
}
if (!argv[0]) {
r = -ENOMEM;
goto fail;
}
}
return 0;
fail:
for (j = 0; j < n_argv; j++)
return r;
}
int r;
assert(b);
assert(p);
assert(*p);
while (**p != 0 && **p != ';') {
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
skip_address_key(p);
}
if (!path)
return -EINVAL;
return 0;
}
int r;
assert(b);
assert(p);
assert(*p);
while (**p != 0 && **p != ';') {
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
skip_address_key(p);
}
return -EINVAL;
if (machine) {
if (!machine_name_is_valid(machine))
return -EINVAL;
} else {
}
if (pid) {
if (r < 0)
return r;
} else
b->nspid = 0;
strncpy(b->sockaddr.un.sun_path, "/var/run/dbus/system_bus_socket", sizeof(b->sockaddr.un.sun_path));
b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + strlen("/var/run/dbus/system_bus_socket");
return 0;
}
int r;
assert(b);
assert(p);
assert(*p);
while (**p != 0 && **p != ';') {
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
skip_address_key(p);
}
return -EINVAL;
if (machine) {
if (!machine_name_is_valid(machine))
return -EINVAL;
} else {
}
if (pid) {
if (r < 0)
return r;
} else
b->nspid = 0;
if (r < 0)
return r;
return 0;
}
assert(b);
b->sockaddr_size = 0;
b->server_id = SD_ID128_NULL;
b->nspid = 0;
}
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-machine-unix:")) {
a += 15;
r = parse_container_unix_address(b, &a, &guid);
if (r < 0)
return r;
break;
} else if (startswith(a, "x-machine-kernel:")) {
a += 17;
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;
}
bool container_kdbus_available = false;
bool kdbus_available = false;
int r;
assert(b);
for (;;) {
bool skipped = false;
bus_close_fds(b);
/*
* Usually, if you provide multiple different bus-addresses, we
* try all of them in order. We use the first one that
* succeeds. However, if you mix kernel and unix addresses, we
* never try unix-addresses if a previous kernel address was
* tried and kdbus was available. This is required to prevent
* clients to fallback to the bus-proxy if kdbus is available
* but failed (eg., too many connections).
*/
if (b->exec_path)
r = bus_socket_exec(b);
r = bus_container_connect_kernel(b);
container_kdbus_available = true;
r = bus_container_connect_socket(b);
else
skipped = true;
} else if (b->kernel) {
r = bus_kernel_connect(b);
kdbus_available = true;
if (!kdbus_available)
r = bus_socket_connect(b);
else
skipped = true;
} 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)
}
}
assert(b);
return bus_start_address(b);
}
int r;
assert(b);
r = fd_nonblock(b->input_fd, true);
if (r < 0)
return r;
r = fd_cloexec(b->input_fd, true);
if (r < 0)
return r;
r = fd_nonblock(b->output_fd, true);
if (r < 0)
return r;
r = fd_cloexec(b->output_fd, true);
if (r < 0)
return r;
}
return -errno;
return bus_kernel_take_fd(b);
else
return bus_socket_take_fd(b);
}
int r;
return -EINVAL;
r = bus_start_fd(bus);
else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path || bus->kernel || bus->machine)
r = bus_start_address(bus);
else
return -EINVAL;
if (r < 0) {
return r;
}
return bus_send_hello(bus);
}
const char *e;
sd_bus *b;
int r;
/* Let's connect to the starter bus if it is set, and
* otherwise to the bus that is appropropriate for the scope
* we are running in */
e = secure_getenv("DBUS_STARTER_BUS_TYPE");
if (e) {
if (streq(e, "system"))
return sd_bus_open_system(ret);
return sd_bus_open_user(ret);
}
e = secure_getenv("DBUS_STARTER_ADDRESS");
if (!e) {
if (cg_pid_get_owner_uid(0, NULL) >= 0)
return sd_bus_open_user(ret);
else
return sd_bus_open_system(ret);
}
r = sd_bus_new(&b);
if (r < 0)
return r;
r = sd_bus_set_address(b, e);
if (r < 0)
goto fail;
b->bus_client = true;
/* We don't know whether the bus is trusted or not, so better
* be safe, and authenticate everything */
b->trusted = false;
r = sd_bus_start(b);
if (r < 0)
goto fail;
*ret = b;
return 0;
fail:
bus_free(b);
return r;
}
const char *e;
assert(b);
e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
if (e)
return sd_bus_set_address(b, e);
return sd_bus_set_address(b, DEFAULT_SYSTEM_BUS_ADDRESS);
}
sd_bus *b;
int r;
r = sd_bus_new(&b);
if (r < 0)
return r;
r = bus_set_address_system(b);
if (r < 0)
goto fail;
b->bus_client = true;
b->is_system = true;
/* Let's do per-method access control on the system bus. We
* need the caller's UID and capability set for that. */
b->trusted = false;
r = sd_bus_start(b);
if (r < 0)
goto fail;
*ret = b;
return 0;
fail:
bus_free(b);
return r;
}
const char *e;
int r;
assert(b);
e = secure_getenv("DBUS_SESSION_BUS_ADDRESS");
if (e)
return sd_bus_set_address(b, e);
r = cg_pid_get_owner_uid(0, &uid);
if (r < 0)
e = secure_getenv("XDG_RUNTIME_DIR");
if (e) {
ee = bus_address_escape(e);
if (!ee)
return -ENOMEM;
} else
if (!b->address)
return -ENOMEM;
return 0;
}
sd_bus *b;
int r;
r = sd_bus_new(&b);
if (r < 0)
return r;
r = bus_set_address_user(b);
if (r < 0)
return r;
b->bus_client = true;
b->is_user = true;
/* We don't do any per-method access control on the user
* bus. */
b->trusted = true;
r = sd_bus_start(b);
if (r < 0)
goto fail;
*ret = b;
return 0;
fail:
bus_free(b);
return r;
}
_cleanup_free_ char *e = NULL;
assert(b);
/* Let's see if we shall enter some container */
if (m) {
m++;
/* Let's make sure this is not a port of some kind,
* and is a valid machine name. */
char *t;
/* Cut out the host part */
e = bus_address_escape(t);
if (!e)
return -ENOMEM;
c = strjoina(",argv4=--machine=", m);
}
}
if (!e) {
e = bus_address_escape(host);
if (!e)
return -ENOMEM;
}
b->address = strjoin("unixexec:path=ssh,argv1=-xT,argv2=", e, ",argv3=systemd-stdio-bridge", c, NULL);
if (!b->address)
return -ENOMEM;
return 0;
}
int r;
r = sd_bus_new(&bus);
if (r < 0)
return r;
if (r < 0)
goto fail;
bus->bus_client = true;
r = sd_bus_start(bus);
if (r < 0)
goto fail;
return 0;
fail:
return r;
}
_cleanup_free_ char *e = NULL;
assert(b);
e = bus_address_escape(machine);
if (!e)
return -ENOMEM;
if (!b->address)
return -ENOMEM;
return 0;
}
int r;
r = sd_bus_new(&bus);
if (r < 0)
return r;
if (r < 0)
goto fail;
bus->bus_client = true;
r = sd_bus_start(bus);
if (r < 0)
goto fail;
return 0;
fail:
return r;
}
if (!bus)
return;
return;
if (bus_pid_changed(bus))
return;
/* Drop all queued messages so that they drop references to
* the bus object and the bus may be freed */
/* We'll leave the fd open in case this is a kernel bus, since
* there might still be memblocks around that reference this
* bus, and they might need to invoke the KDBUS_CMD_FREE
* ioctl on the fd when they are freed. */
}
if (!bus)
return NULL;
return sd_bus_unref(bus);
}
return;
}
if (!bus)
return NULL;
return bus;
}
unsigned i;
if (!bus)
return NULL;
if (i > 0)
return NULL;
return NULL;
}
}
int r;
return 0;
if (type == SD_BUS_TYPE_UNIX_FD) {
return 0;
r = bus_ensure_running(bus);
if (r < 0)
return r;
}
return bus_type_is_valid(type);
}
int r;
r = bus_ensure_running(bus);
if (r < 0)
return r;
return 0;
}
assert(b);
assert(m);
if (m->sealed) {
/* If we copy the same message to multiple
* destinations, avoid using the same cookie
* numbers. */
return 0;
}
if (timeout == 0)
}
bool remarshal = false;
assert(b);
/* wrong packet version */
remarshal = true;
/* wrong packet endianness */
remarshal = true;
/* TODO: kdbus-messages received from the kernel contain data which is
* not allowed to be passed to KDBUS_CMD_SEND. Therefore, we have to
* force remarshaling of the message. Technically, we could just
* recreate the kdbus message, but that is non-trivial as other parts of
* the message refer to m->kdbus already. This should be fixed! */
if ((*m)->kdbus && (*m)->release_kdbus)
remarshal = true;
return remarshal ? bus_message_remarshal(b, m) : 0;
}
assert(b);
assert(m);
/* Fake some timestamps, if they were requested, and not
* already initialized */
if (b->attach_flags & KDBUS_ATTACH_TIMESTAMP) {
if (m->realtime <= 0)
if (m->monotonic <= 0)
}
/* The bus specification says the serial number cannot be 0,
* hence let's fill something in for synthetic messages. Since
* synthetic messages might have a fake sender and we don't
* want to interfere with the real sender's serial numbers we
* pick a fixed, artificial one. We use (uint32_t) -1 rather
* than (uint64_t) -1 since dbus1 only had 32bit identifiers,
* even though kdbus can do 64bit. */
return bus_message_seal(m, 0xFFFFFFFFULL, 0);
}
int r;
assert(m);
else
if (r <= 0)
return r;
log_debug("Sent message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%" PRIu64 " reply_cookie=%" PRIu64 " error=%s",
m->reply_cookie,
return r;
}
int r, ret = 0;
while (bus->wqueue_size > 0) {
if (r < 0)
return r;
else if (r == 0)
/* Didn't do anything this time */
return ret;
/* Fully written. Let's drop the entry from
* the queue.
*
* This isn't particularly optimized, but
* well, this is supposed to be our worst-case
* buffer only, and the socket buffer is
* supposed to be our primary buffer, and if
* it got full, then all bets are off
* anyway. */
bus->wqueue_size --;
ret = 1;
}
}
return ret;
}
else
return bus_socket_read_message(bus);
}
return -ENOBUFS;
return -ENOMEM;
return 0;
}
int r, ret = 0;
assert(m);
/* Note that the priority logic is only available on kdbus,
* where the rqueue is unused. We check the rqueue here
* anyway, because it's simple... */
for (;;) {
if (bus->rqueue_size > 0) {
/* Dispatch a queued message */
bus->rqueue_size --;
return 1;
}
/* Try to read a new message */
if (r < 0)
return r;
if (r == 0)
return ret;
ret = 1;
}
}
static int bus_send_internal(sd_bus *bus, sd_bus_message *_m, uint64_t *cookie, bool hint_sync_call) {
int r;
assert_return(m, -EINVAL);
if (!bus)
return -ENOTCONN;
if (m->n_fds > 0) {
if (r < 0)
return r;
if (r == 0)
return -EOPNOTSUPP;
}
/* If the cookie number isn't kept, then we know that no reply
* is expected */
r = bus_seal_message(bus, m, 0);
if (r < 0)
return r;
/* Remarshall if we have to. This will possibly unref the
* message and place a replacement in m */
r = bus_remarshal_message(bus, &m);
if (r < 0)
return r;
/* If this is a reply and no reply was requested, then let's
* suppress this, if we can */
if (m->dont_send)
goto finish;
if (r < 0) {
return -ECONNRESET;
}
return r;
}
/* Wasn't fully written. So let's remember how
* much was written. Note that the first entry
* of the wqueue array is always allocated so
* that we always can remember how much was
* written. */
}
} else {
/* Just append it to the queue. */
return -ENOBUFS;
return -ENOMEM;
}
if (cookie)
*cookie = BUS_MESSAGE_COOKIE(m);
return 1;
}
}
_public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destination, uint64_t *cookie) {
int r;
assert_return(m, -EINVAL);
if (!bus)
return -ENOTCONN;
if (!destination)
return -EEXIST;
if (r < 0)
return r;
}
}
return 0;
}
static int timeout_compare(const void *a, const void *b) {
const struct reply_callback *x = a, *y = b;
return -1;
return 1;
return -1;
return 1;
return 0;
}
sd_bus_slot **slot,
void *userdata,
int r;
assert_return(m, -EINVAL);
if (!bus)
return -ENOTCONN;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
r = bus_remarshal_message(bus, &m);
if (r < 0)
return r;
if (!s)
return -ENOMEM;
if (r < 0) {
s->reply_callback.cookie = 0;
return r;
}
if (s->reply_callback.timeout != 0) {
if (r < 0) {
s->reply_callback.timeout = 0;
return r;
}
}
if (r < 0)
return r;
if (slot)
*slot = s;
s = NULL;
return r;
}
int r;
return -ENOTCONN;
return 1;
for (;;) {
if (r < 0)
return r;
return 1;
if (r > 0)
continue;
if (r < 0)
return r;
}
}
sd_bus_message **reply) {
unsigned i;
int r;
if (!bus)
r = -ENOTCONN;
goto fail;
}
r = bus_ensure_running(bus);
if (r < 0)
goto fail;
i = bus->rqueue_size;
if (r < 0)
goto fail;
r = bus_remarshal_message(bus, &m);
if (r < 0)
goto fail;
if (r < 0)
goto fail;
for (;;) {
while (i < bus->rqueue_size) {
/* Found a match! */
memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
bus->rqueue_size--;
if (reply)
else
return 1;
}
r = sd_bus_error_setf(error, SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Reply message contained file descriptors which I couldn't accept. Sorry.");
return r;
return r;
} else {
r = -EIO;
goto fail;
}
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. */
r = -ELOOP;
goto fail;
}
/* Try to read more, right-away */
i++;
}
r = bus_read_message(bus, false, 0);
if (r < 0) {
r = -ECONNRESET;
}
goto fail;
}
if (r > 0)
continue;
if (timeout > 0) {
usec_t n;
n = now(CLOCK_MONOTONIC);
if (n >= timeout) {
r = -ETIMEDOUT;
goto fail;
}
} else
if (r < 0)
goto fail;
if (r == 0) {
r = -ETIMEDOUT;
goto fail;
}
r = dispatch_wqueue(bus);
if (r < 0) {
r = -ECONNRESET;
}
goto fail;
}
}
fail:
return sd_bus_error_set_errno(error, r);
}
}
int flags = 0;
return -ENOTCONN;
if (bus->rqueue_size <= 0)
if (bus->wqueue_size > 0)
}
return flags;
}
struct reply_callback *c;
return -ENOTCONN;
if (bus->track_queue) {
*timeout_usec = 0;
return 1;
}
*timeout_usec = 0;
return 1;
}
return 1;
}
return 0;
}
if (bus->rqueue_size > 0) {
*timeout_usec = 0;
return 1;
}
if (!c) {
return 0;
}
if (c->timeout == 0) {
return 0;
}
*timeout_usec = c->timeout;
return 1;
}
struct reply_callback *c;
usec_t n;
int r;
if (!c)
return 0;
n = now(CLOCK_MONOTONIC);
if (c->timeout > n)
return 0;
bus,
c->cookie,
&m);
if (r < 0)
return r;
r = bus_seal_synthetic_message(bus, m);
if (r < 0)
return r;
c->timeout = 0;
c->cookie = 0;
bus->iteration_counter ++;
bus->current_message = m;
}
return bus_maybe_reply_error(m, r, &error_buffer);
}
assert(m);
return 0;
/* Let's make sure the first message on the bus is the HELLO
* reply. But note that we don't actually parse the message
* here (we leave that to the usual handling), we just verify
* we don't let any earlier msg through. */
return -EIO;
if (m->reply_cookie != 1)
return -EIO;
return 0;
}
struct reply_callback *c;
int r;
assert(m);
return 0;
return 0;
return 0;
if (!c)
return 0;
c->cookie = 0;
/* If the reply contained a file descriptor which we
* didn't want we pass an error instead. */
bus,
m->reply_cookie,
&SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Reply message contained file descriptor"),
if (r < 0)
return r;
/* Copy over original timestamp */
if (r < 0)
return r;
m = synthetic_reply;
} else {
r = sd_bus_message_rewind(m, true);
if (r < 0)
return r;
}
if (c->timeout != 0) {
c->timeout = 0;
}
}
return bus_maybe_reply_error(m, r, &error_buffer);
}
struct filter_callback *l;
int r;
assert(m);
do {
bus->filter_callbacks_modified = false;
if (bus->filter_callbacks_modified)
break;
/* Don't run this more than once per iteration */
continue;
r = sd_bus_message_rewind(m, true);
if (r < 0)
return r;
r = bus_maybe_reply_error(m, r, &error_buffer);
if (r != 0)
return r;
}
} while (bus->filter_callbacks_modified);
return 0;
}
int r;
assert(m);
do {
bus->match_callbacks_modified = false;
if (r != 0)
return r;
} while (bus->match_callbacks_modified);
return 0;
}
int r;
assert(m);
return 0;
if (bus->manual_peer_interface)
return 0;
return 0;
return 0;
return 1;
r = sd_bus_message_new_method_return(m, &reply);
r = sd_id128_get_machine(&id);
if (r < 0)
return r;
r = sd_bus_message_new_method_return(m, &reply);
if (r < 0)
return r;
} else {
m, &reply,
}
if (r < 0)
return r;
if (r < 0)
return r;
return 1;
}
assert(m);
/* If we got a message with a file descriptor which we didn't
* want to accept, then let's drop it. How can this even
* happen? For example, when the kernel queues a message into
* an activatable names's queue which allows fds, and then is
* delivered to us later even though we ourselves did not
* negotiate it. */
return 0;
if (m->n_fds <= 0)
return 0;
return 0;
return 1; /* just eat it up */
return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Message contains file descriptors, which I cannot accept. Sorry.");
}
int r;
assert(m);
bus->current_message = m;
bus->iteration_counter++;
r = process_hello(bus, m);
if (r != 0)
goto finish;
r = process_reply(bus, m);
if (r != 0)
goto finish;
r = process_fd_check(bus, m);
if (r != 0)
goto finish;
r = process_filter(bus, m);
if (r != 0)
goto finish;
r = process_match(bus, m);
if (r != 0)
goto finish;
r = process_builtin(bus, m);
if (r != 0)
goto finish;
r = bus_process_object(bus, m);
return r;
}
if (!bus->track_queue)
return 0;
return 1;
}
static int process_running(sd_bus *bus, bool hint_priority, int64_t priority, sd_bus_message **ret) {
int r;
r = process_timeout(bus);
if (r != 0)
goto null_message;
r = dispatch_wqueue(bus);
if (r != 0)
goto null_message;
r = dispatch_track(bus);
if (r != 0)
goto null_message;
if (r < 0)
return r;
if (!m)
goto null_message;
r = process_message(bus, m);
if (r != 0)
goto null_message;
if (ret) {
r = sd_bus_message_rewind(m, true);
if (r < 0)
return r;
*ret = m;
m = NULL;
return 1;
}
log_debug("Unprocessed message call sender=%s object=%s interface=%s member=%s",
m,
"Unknown object '%s'.", m->path);
if (r < 0)
return r;
}
return 1;
if (r >= 0 && ret)
return r;
}
struct reply_callback *c;
int r;
if (c) {
/* First, fail all outstanding method calls */
bus,
c->cookie,
&m);
if (r < 0)
return r;
r = bus_seal_synthetic_message(bus, m);
if (r < 0)
return r;
if (c->timeout != 0) {
c->timeout = 0;
}
c->cookie = 0;
bus->iteration_counter++;
bus->current_message = m;
}
return bus_maybe_reply_error(m, r, &error_buffer);
}
/* Then, synthesize a Disconnected message */
bus,
&m,
"/org/freedesktop/DBus/Local",
"org.freedesktop.DBus.Local",
"Disconnected");
if (r < 0)
return r;
r = bus_seal_synthetic_message(bus, m);
if (r < 0)
return r;
bus->current_message = m;
bus->iteration_counter++;
r = process_filter(bus, m);
if (r != 0)
goto finish;
r = process_match(bus, m);
if (r != 0)
goto finish;
if (ret) {
*ret = m;
m = NULL;
}
r = 1;
return r;
}
static int bus_process_internal(sd_bus *bus, bool hint_priority, int64_t priority, sd_bus_message **ret) {
int r;
/* Returns 0 when we didn't do anything. This should cause the
* caller to invoke sd_bus_wait() before returning the next
* time. Returns > 0 when we did something, which possibly
* means *ret is filled in with an unprocessed message. */
/* We don't allow recursively invoking sd_bus_process(). */
case BUS_UNSET:
return -ENOTCONN;
case BUS_CLOSED:
return -ECONNRESET;
case BUS_OPENING:
r = 1;
} else if (r < 0)
return r;
if (ret)
return r;
case BUS_AUTHENTICATING:
r = 1;
} else if (r < 0)
return r;
if (ret)
return r;
case BUS_RUNNING:
case BUS_HELLO:
r = 1;
if (ret)
}
return r;
case BUS_CLOSING:
}
assert_not_reached("Unknown state");
}
}
}
struct pollfd p[2] = {};
int r, e, n;
usec_t m = USEC_INFINITY;
return 1;
return -ENOTCONN;
e = sd_bus_get_events(bus);
if (e < 0)
return e;
if (need_more)
/* The caller really needs some more data, he doesn't
* care about what's already read, or any timeouts
* except its own. */
e |= POLLIN;
else {
/* The caller wants to process if there's something to
* process, but doesn't care otherwise */
if (r < 0)
return r;
if (r > 0) {
}
}
m = timeout_usec;
p[0].events = e;
n = 1;
} else {
n = 2;
}
if (r < 0)
return -errno;
return r > 0 ? 1 : 0;
}
return 0;
return -ENOTCONN;
if (bus->rqueue_size > 0)
return 0;
}
int r;
return 0;
return -ENOTCONN;
r = bus_ensure_running(bus);
if (r < 0)
return r;
if (bus->wqueue_size <= 0)
return 0;
for (;;) {
r = dispatch_wqueue(bus);
if (r < 0) {
return -ECONNRESET;
}
return r;
}
if (bus->wqueue_size <= 0)
return 0;
if (r < 0)
return r;
}
}
sd_bus_slot **slot,
void *userdata) {
sd_bus_slot *s;
if (!s)
return -ENOMEM;
bus->filter_callbacks_modified = true;
if (slot)
*slot = s;
return 0;
}
sd_bus_slot **slot,
const char *match,
void *userdata) {
unsigned n_components = 0;
sd_bus_slot *s = NULL;
int r = 0;
if (r < 0)
goto finish;
if (!s) {
r = -ENOMEM;
goto finish;
}
if (bus->bus_client) {
/* Do not install server-side matches for matches
* against the local service, interface or bus
* path. */
if (scope != BUS_MATCH_LOCAL) {
/* When this is not a kernel transport, we
* store the original match string, so that we
* can use it to remove the match again */
if (!s->match_callback.match_string) {
r = -ENOMEM;
goto finish;
}
}
r = bus_add_match_internal(bus, s->match_callback.match_string, components, n_components, s->match_callback.cookie);
if (r < 0)
goto finish;
s->match_added = true;
}
}
bus->match_callbacks_modified = true;
if (r < 0)
goto finish;
if (slot)
*slot = s;
s = NULL;
return r;
}
const char *match,
void *userdata) {
unsigned n_components = 0;
struct match_callback *c;
int r = 0;
if (r < 0)
goto finish;
if (r <= 0)
goto finish;
return r;
}
/* We don't support people creating a bus connection and
* keeping it around over a fork(). Let's complain. */
}
int r;
if (r < 0)
return r;
return 1;
}
int r;
if (r < 0)
return r;
return 1;
}
int r, e;
assert(s);
e = sd_bus_get_events(bus);
if (e < 0)
return e;
if (r < 0)
return r;
if (r < 0)
return r;
} else {
if (r < 0)
return r;
}
if (r < 0)
return r;
if (r > 0) {
int j;
if (j < 0)
return j;
}
if (r < 0)
return r;
return 1;
}
return 1;
}
int r;
return 0;
return 0;
if (!bus->input_io_event_source) {
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
} else
if (r < 0)
return r;
if (!bus->output_io_event_source) {
if (r < 0)
return r;
if (r < 0)
return r;
} else
if (r < 0)
return r;
}
return 0;
}
if (bus->input_io_event_source) {
}
if (bus->output_io_event_source) {
}
}
int r;
if (event)
else {
if (r < 0)
return r;
}
r = sd_event_add_time(bus->event, &bus->time_event_source, CLOCK_MONOTONIC, 0, 0, time_callback, bus);
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
r = attach_io_events(bus);
if (r < 0)
goto fail;
return 0;
fail:
return r;
}
return 0;
if (bus->time_event_source) {
}
if (bus->quit_event_source) {
}
return 1;
}
}
return bus->current_message;
}
return bus->current_slot;
}
return bus->current_handler;
}
return bus->current_userdata;
}
int r;
if (!ret)
return !!*default_bus;
if (*default_bus) {
return 0;
}
r = bus_open(&b);
if (r < 0)
return r;
b->default_bus_ptr = default_bus;
*default_bus = b;
*ret = b;
return 1;
}
}
}
const char *e;
/* Let's try our best to reuse another cached connection. If
* the starter bus type is set, connect via our normal
* connection logic, ignoring $DBUS_STARTER_ADDRESS, so that
* bus. */
e = secure_getenv("DBUS_STARTER_BUS_TYPE");
if (e) {
if (streq(e, "system"))
return sd_bus_default_system(ret);
return sd_bus_default_user(ret);
}
/* No type is specified, so we have not other option than to
* use the starter address if it is set. */
e = secure_getenv("DBUS_STARTER_ADDRESS");
if (e) {
}
/* Finally, if nothing is set use the cached connection for
* the right scope */
if (cg_pid_get_owner_uid(0, NULL) >= 0)
return sd_bus_default_user(ret);
else
return sd_bus_default_system(ret);
}
assert_return(b, -EINVAL);
if (b->tid != 0) {
return 0;
}
if (b->event)
return -ENXIO;
}
_cleanup_free_ char *e = NULL;
char *ret;
e = bus_label_escape(external_id);
if (!e)
return -ENOMEM;
if (!ret)
return -ENOMEM;
return 0;
}
const char *e;
char *ret;
if (!e) {
*external_id = NULL;
return 0;
}
ret = bus_label_unescape(e);
if (!ret)
return -ENOMEM;
*external_id = ret;
return 1;
}
int r;
const char *arg;
char *label;
if (!arg) {
return -EINVAL;
}
if (!label) {
return -ENOMEM;
}
if (r < 0) {
return r;
}
/* add label length, but account for the format character */
}
if (!path)
return -ENOMEM;
if (!*sep)
break;
}
*path_pos = 0;
return 0;
}
char **label_pos;
int r;
/*
* This decodes an object-path based on a template argument. The
* template consists of a verbatim path, optionally including special
* directives:
*
* - Each occurrence of '%' in the template matches an arbitrary
* substring of a label in the given path. At most one such
* directive is allowed per label. For each such directive, the
* caller must provide an output parameter (char **) via va_arg. If
* NULL is passed, the given label is verified, but not returned.
* For each matched label, the *decoded* label is stored in the
* passed output argument, and the caller is responsible to free
* it. Note that the output arguments are only modified if the
* actualy path matched the template. Otherwise, they're left
* untouched.
*
* This function returns <0 on error, 0 if the path does not match the
* template, 1 if it matched.
*/
const char *sep;
char *label;
/* verify everything until the next '%' matches verbatim */
return 0;
template_pos += length;
if (!*template_pos)
break;
/* We found the next '%' character. Everything up until here
* matched. We now skip ahead to the end of this label and make
* sure it matches the tail of the label in the path. Then we
* decode the string in-between and save it for later use. */
++template_pos; /* skip over '%' */
/* verify the suffixes match */
return 0;
/* store unescaped label for later use */
if (!label)
return -ENOMEM;
if (r < 0)
return r;
}
/* end of template must match end of path */
if (*path_pos)
return 0;
/* copy the labels over to the caller */
char **arg;
if (arg)
else
}
return 1;
}
int r;
return -EOPNOTSUPP;
return -ENOTCONN;
if (bus->rqueue_size > 0)
return -EBUSY;
if (bus->wqueue_size > 0)
return -EBUSY;
r = bus_kernel_try_close(bus);
if (r < 0)
return r;
return 0;
}
return 0;
}
int r;
if (bus->cgroup_root)
return 0;
if (r == -ENOENT) {
if (!bus->cgroup_root)
return -ENOMEM;
r = 0;
}
return r;
}
int r;
_cleanup_free_ char *n = NULL;
const char *dash;
r = bus_kernel_get_bus_name(bus, &n);
if (r < 0)
return r;
if (streq(n, "0-system")) {
*scope = "system";
return 0;
}
*scope = "user";
return 0;
}
}
*scope = "user";
return 0;
}
*scope = "system";
return 0;
}
return -ENODATA;
}
return 0;
}
return -ENODATA;
}
return 0;
}
return bus->bus_client;
}
}
return bus->anonymous_auth;
}
}
}
if (!bus)
return;
/* Flushes and closes the specified bus. We take a ref before,
* to ensure the flushing does not cause the bus to be
* unreferenced. */
}
}