Commit 6629161f authored by Lennart Poettering's avatar Lennart Poettering

bus: basic implementation of kdbus client side

parent 4cda0f21
......@@ -1700,6 +1700,8 @@ libsystemd_bus_la_SOURCES = \
src/libsystemd-bus/bus-internal.h \
src/libsystemd-bus/bus-socket.c \
src/libsystemd-bus/bus-socket.h \
src/libsystemd-bus/bus-kernel.c \
src/libsystemd-bus/bus-kernel.h \
src/libsystemd-bus/bus-message.c \
src/libsystemd-bus/bus-message.h \
src/libsystemd-bus/bus-signature.c \
......@@ -4105,6 +4107,10 @@ hwdb-update:
http://standards.ieee.org/develop/regauth/iab/iab.txt && \
./ids-update.pl )
kdbus-update:
( cd $(top_srcdir)/src/libsystemd-bus/ && \
wget -N https://d-bus.googlecode.com/git/kdbus.h )
upload: all distcheck
cp -v systemd-$(VERSION).tar.xz /home/lennart/git.fedora/systemd/
scp systemd-$(VERSION).tar.xz fdo:/srv/www.freedesktop.org/www/software/systemd/
......
......@@ -81,6 +81,7 @@ struct sd_bus {
int input_fd, output_fd;
int message_version;
bool is_kernel:1;
bool negotiate_fds:1;
bool can_fds:1;
bool bus_client:1;
......@@ -122,6 +123,8 @@ struct sd_bus {
} sockaddr;
socklen_t sockaddr_size;
char *kernel;
sd_id128_t server_id;
char *address;
......
This diff is collapsed.
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
#pragma once
/***
This file is part of systemd.
Copyright 2013 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
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 "sd-bus.h"
int bus_kernel_connect(sd_bus *b);
int bus_kernel_take_fd(sd_bus *b);
int bus_kernel_write_message(sd_bus *bus, sd_bus_message *m);
int bus_kernel_read_message(sd_bus *bus, sd_bus_message **m);
int bus_kernel_create(const char *name, char **s);
......@@ -25,6 +25,7 @@
#include "util.h"
#include "utf8.h"
#include "strv.h"
#include "time-util.h"
#include "sd-bus.h"
#include "bus-message.h"
......@@ -32,7 +33,6 @@
#include "bus-type.h"
#include "bus-signature.h"
static int message_parse_fields(sd_bus_message *m);
static int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored);
static void reset_containers(sd_bus_message *m) {
......@@ -62,6 +62,9 @@ static void message_free(sd_bus_message *m) {
if (m->free_body)
free(m->body);
if (m->free_kdbus)
free(m->kdbus);
if (m->free_fds) {
close_many(m->fds, m->n_fds);
free(m->fds);
......@@ -225,19 +228,19 @@ static int message_append_field_uint32(sd_bus_message *m, uint8_t h, uint32_t x)
return 0;
}
int bus_message_from_malloc(
int bus_message_from_header(
void *buffer,
size_t length,
int *fds,
unsigned n_fds,
const struct ucred *ucred,
const char *label,
size_t extra,
sd_bus_message **ret) {
sd_bus_message *m;
struct bus_header *h;
size_t total, fs, bs, label_sz, a;
int r;
size_t a, label_sz;
assert(buffer || length <= 0);
assert(fds || n_fds <= 0);
......@@ -256,24 +259,16 @@ int bus_message_from_malloc(
if (h->type == _SD_BUS_MESSAGE_TYPE_INVALID)
return -EBADMSG;
if (h->endian == SD_BUS_NATIVE_ENDIAN) {
fs = h->fields_size;
bs = h->body_size;
} else if (h->endian == SD_BUS_REVERSE_ENDIAN) {
fs = bswap_32(h->fields_size);
bs = bswap_32(h->body_size);
} else
if (h->endian != SD_BUS_LITTLE_ENDIAN &&
h->endian != SD_BUS_BIG_ENDIAN)
return -EBADMSG;
total = sizeof(struct bus_header) + ALIGN8(fs) + bs;
if (length != total)
return -EBADMSG;
a = ALIGN(sizeof(sd_bus_message)) + ALIGN(extra);
if (label) {
label_sz = strlen(label);
a = ALIGN(sizeof(sd_bus_message)) + label_sz + 1;
} else
a = sizeof(sd_bus_message);
a += label_sz + 1;
}
m = malloc0(a);
if (!m)
......@@ -282,8 +277,6 @@ int bus_message_from_malloc(
m->n_ref = 1;
m->sealed = true;
m->header = h;
m->fields = (uint8_t*) buffer + sizeof(struct bus_header);
m->body = (uint8_t*) buffer + sizeof(struct bus_header) + ALIGN8(fs);
m->fds = fds;
m->n_fds = n_fds;
......@@ -295,15 +288,43 @@ int bus_message_from_malloc(
}
if (label) {
m->label = (char*) m + ALIGN(sizeof(sd_bus_message));
m->label = (char*) m + ALIGN(sizeof(sd_bus_message)) + ALIGN(extra);
memcpy(m->label, label, label_sz + 1);
}
*ret = m;
return 0;
}
int bus_message_from_malloc(
void *buffer,
size_t length,
int *fds,
unsigned n_fds,
const struct ucred *ucred,
const char *label,
sd_bus_message **ret) {
sd_bus_message *m;
int r;
r = bus_message_from_header(buffer, length, fds, n_fds, ucred, label, 0, &m);
if (r < 0)
return r;
if (length != BUS_MESSAGE_SIZE(m)) {
r = -EBADMSG;
goto fail;
}
m->fields = (uint8_t*) buffer + sizeof(struct bus_header);
m->body = (uint8_t*) buffer + sizeof(struct bus_header) + ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m));
m->n_iovec = 1;
m->iovec[0].iov_base = buffer;
m->iovec[0].iov_len = length;
r = message_parse_fields(m);
r = bus_message_parse_fields(m);
if (r < 0)
goto fail;
......@@ -2600,7 +2621,7 @@ static int message_skip_fields(
}
}
static int message_parse_fields(sd_bus_message *m) {
int bus_message_parse_fields(sd_bus_message *m) {
size_t ri;
int r;
uint32_t unix_fds = 0;
......@@ -3041,7 +3062,7 @@ int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz) {
assert(buffer);
assert(sz);
total = bus_message_size(m);
total = BUS_MESSAGE_SIZE(m);
p = malloc(total);
if (!p)
......@@ -3133,12 +3154,21 @@ const char* bus_message_get_arg(sd_bus_message *m, unsigned i) {
return t;
}
size_t bus_message_size(sd_bus_message *m) {
assert(m);
assert(m->sealed);
int bus_header_size(struct bus_header *h, size_t *sum) {
size_t fs, bs;
assert(h);
assert(sum);
if (h->endian == SD_BUS_NATIVE_ENDIAN) {
fs = h->fields_size;
bs = h->body_size;
} else if (h->endian == SD_BUS_REVERSE_ENDIAN) {
fs = bswap_32(h->fields_size);
bs = bswap_32(h->body_size);
} else
return -EBADMSG;
return
sizeof(*m->header) +
ALIGN8(m->header->fields_size) +
m->header->body_size;
*sum = sizeof(struct bus_header) + ALIGN8(fs) + bs;
return 0;
}
......@@ -27,6 +27,7 @@
#include "macro.h"
#include "sd-bus.h"
#include "kdbus.h"
struct bus_container {
char enclosing;
......@@ -74,11 +75,13 @@ struct sd_bus_message {
bool free_header:1;
bool free_fields:1;
bool free_body:1;
bool free_kdbus:1;
bool free_fds:1;
struct bus_header *header;
void *fields;
void *body;
struct kdbus_msg *kdbus;
char *label;
......@@ -94,6 +97,8 @@ struct sd_bus_message {
unsigned n_iovec;
char *peeked_signature;
usec_t timeout;
};
#define BUS_MESSAGE_NEED_BSWAP(m) ((m)->header->endian != SD_BUS_NATIVE_ENDIAN)
......@@ -122,6 +127,13 @@ static inline uint32_t BUS_MESSAGE_FIELDS_SIZE(sd_bus_message *m) {
return BUS_MESSAGE_BSWAP32(m, m->header->fields_size);
}
static inline uint32_t BUS_MESSAGE_SIZE(sd_bus_message *m) {
return
sizeof(struct bus_header) +
ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m)) +
BUS_MESSAGE_BODY_SIZE(m);
}
static inline void bus_message_unrefp(sd_bus_message **m) {
sd_bus_message_unref(*m);
}
......@@ -133,6 +145,16 @@ int bus_message_dump(sd_bus_message *m);
int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz);
int bus_message_read_strv_extend(sd_bus_message *m, char ***l);
int bus_message_from_header(
void *header,
size_t length,
int *fds,
unsigned n_fds,
const struct ucred *ucred,
const char *label,
size_t extra,
sd_bus_message **ret);
int bus_message_from_malloc(
void *buffer,
size_t length,
......@@ -146,4 +168,6 @@ const char* bus_message_get_arg(sd_bus_message *m, unsigned i);
int bus_message_append_ap(sd_bus_message *m, const char *types, va_list ap);
size_t bus_message_size(sd_bus_message *m);
int bus_message_parse_fields(sd_bus_message *m);
int bus_header_size(struct bus_header *h, size_t *sum);
......@@ -762,7 +762,7 @@ int bus_socket_write_message(sd_bus *bus, sd_bus_message *m, size_t *idx) {
assert(idx);
assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
if (*idx >= bus_message_size(m))
if (*idx >= BUS_MESSAGE_SIZE(m))
return 0;
bus_message_setup_iovec(m);
......
This diff is collapsed.
......@@ -37,6 +37,7 @@
#include "bus-message.h"
#include "bus-type.h"
#include "bus-socket.h"
#include "bus-kernel.h"
#include "bus-control.h"
static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec);
......@@ -259,7 +260,7 @@ static int bus_send_hello(sd_bus *bus) {
assert(bus);
if (!bus->bus_client)
if (!bus->bus_client || bus->is_kernel)
return 0;
r = sd_bus_message_new_method_call(
......@@ -596,6 +597,41 @@ fail:
return r;
}
static int parse_kernel_address(sd_bus *b, const char **p, char **guid) {
_cleanup_free_ char *path = NULL;
int r;
assert(b);
assert(p);
assert(*p);
assert(guid);
while (**p != 0 && **p != ';') {
r = parse_address_key(p, "guid", guid);
if (r < 0)
return r;
else if (r > 0)
continue;
r = parse_address_key(p, "path", &path);
if (r < 0)
return r;
else if (r > 0)
continue;
skip_address_key(p);
}
if (!path)
return -EINVAL;
free(b->kernel);
b->kernel = path;
path = NULL;
return 0;
}
static void bus_reset_parsed_address(sd_bus *b) {
assert(b);
......@@ -606,6 +642,8 @@ static void bus_reset_parsed_address(sd_bus *b) {
b->exec_path = NULL;
b->exec_argv = NULL;
b->server_id = SD_ID128_NULL;
free(b->kernel);
b->kernel = NULL;
}
static int bus_parse_next_address(sd_bus *b) {
......@@ -657,6 +695,14 @@ static int bus_parse_next_address(sd_bus *b) {
break;
} else if (startswith(a, "kernel:")) {
a += 7;
r = parse_kernel_address(b, &a, &guid);
if (r < 0)
return r;
break;
}
a = strchr(a, ';');
......@@ -696,6 +742,13 @@ static int bus_start_address(sd_bus *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;
}
......@@ -715,6 +768,7 @@ int bus_next_address(sd_bus *b) {
}
static int bus_start_fd(sd_bus *b) {
struct stat st;
int r;
assert(b);
......@@ -739,7 +793,13 @@ static int bus_start_fd(sd_bus *b) {
return r;
}
return bus_socket_take_fd(b);
if (fstat(b->input_fd, &st) < 0)
return -errno;
if (S_ISCHR(b->input_fd))
return bus_kernel_take_fd(b);
else
return bus_socket_take_fd(b);
}
int sd_bus_start(sd_bus *bus) {
......@@ -757,7 +817,7 @@ int sd_bus_start(sd_bus *bus) {
if (bus->input_fd >= 0)
r = bus_start_fd(bus);
else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path)
else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path || bus->kernel)
r = bus_start_address(bus);
else
return -EINVAL;
......@@ -958,14 +1018,18 @@ static int dispatch_wqueue(sd_bus *bus) {
while (bus->wqueue_size > 0) {
r = bus_socket_write_message(bus, bus->wqueue[0], &bus->windex);
if (bus->is_kernel)
r = bus_kernel_write_message(bus, bus->wqueue[0]);
else
r = bus_socket_write_message(bus, bus->wqueue[0], &bus->windex);
if (r < 0) {
sd_bus_close(bus);
return r;
} else if (r == 0)
/* Didn't do anything this time */
return ret;
else if (bus->windex >= bus_message_size(bus->wqueue[0])) {
else if (bus->is_kernel || bus->windex >= BUS_MESSAGE_SIZE(bus->wqueue[0])) {
/* Fully written. Let's drop the entry from
* the queue.
*
......@@ -1010,7 +1074,11 @@ static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
/* Try to read a new message */
do {
r = bus_socket_read_message(bus, &z);
if (bus->is_kernel)
r = bus_kernel_read_message(bus, &z);
else
r = bus_socket_read_message(bus, &z);
if (r < 0) {
sd_bus_close(bus);
return r;
......@@ -1062,11 +1130,15 @@ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
if ((bus->state == BUS_RUNNING || bus->state == BUS_HELLO) && bus->wqueue_size <= 0) {
size_t idx = 0;
r = bus_socket_write_message(bus, m, &idx);
if (bus->is_kernel)
r = bus_kernel_write_message(bus, m);
else
r = bus_socket_write_message(bus, m, &idx);
if (r < 0) {
sd_bus_close(bus);
return r;
} else if (idx < bus_message_size(m)) {
} else if (!bus->is_kernel && idx < BUS_MESSAGE_SIZE(m)) {
/* 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
......@@ -1304,7 +1376,10 @@ int sd_bus_send_with_reply_and_block(
room = true;
}
r = bus_socket_read_message(bus, &incoming);
if (bus->is_kernel)
r = bus_kernel_read_message(bus, &incoming);
else
r = bus_socket_read_message(bus, &incoming);
if (r < 0)
return r;
if (incoming) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment