Commit f08838da authored by Lennart Poettering's avatar Lennart Poettering

bus: implement basic name registration with kdbus

parent 8c11aac1
......@@ -54,26 +54,46 @@ int sd_bus_request_name(sd_bus *bus, const char *name, int flags) {
return -EINVAL;
if (!name)
return -EINVAL;
if (!bus->bus_client)
return -EINVAL;
r = sd_bus_call_method(
bus,
"org.freedesktop.DBus",
"/",
"org.freedesktop.DBus",
"RequestName",
NULL,
&reply,
"su",
name,
flags);
if (r < 0)
return r;
r = sd_bus_message_read(reply, "u", &ret);
if (r < 0)
return r;
return ret;
if (bus->is_kernel) {
struct kdbus_cmd_name *n;
size_t l;
l = strlen(name);
n = alloca(offsetof(struct kdbus_cmd_name, name) + l + 1);
n->size = offsetof(struct kdbus_cmd_name, name) + l + 1;
n->flags = flags;
n->id = 0;
memcpy(n->name, name, l+1);
r = ioctl(bus->input_fd, KDBUS_CMD_NAME_ACQUIRE, n);
if (r < 0)
return -errno;
return n->flags;
} else {
r = sd_bus_call_method(
bus,
"org.freedesktop.DBus",
"/",
"org.freedesktop.DBus",
"RequestName",
NULL,
&reply,
"su",
name,
flags);
if (r < 0)
return r;
r = sd_bus_message_read(reply, "u", &ret);
if (r < 0)
return r;
return ret;
}
}
int sd_bus_release_name(sd_bus *bus, const char *name) {
......@@ -85,23 +105,43 @@ int sd_bus_release_name(sd_bus *bus, const char *name) {
return -EINVAL;
if (!name)
return -EINVAL;
if (!bus->bus_client)
return -EINVAL;
r = sd_bus_call_method(
bus,
"org.freedesktop.DBus",
"/",
"org.freedesktop.DBus",
"ReleaseName",
NULL,
&reply,
"s",
name);
if (r < 0)
return r;
r = sd_bus_message_read(reply, "u", &ret);
if (r < 0)
return r;
if (bus->is_kernel) {
struct kdbus_cmd_name *n;
size_t l;
l = strlen(name);
n = alloca(offsetof(struct kdbus_cmd_name, name) + l + 1);
n->size = offsetof(struct kdbus_cmd_name, name) + l + 1;
n->flags = 0;
n->id = 0;
memcpy(n->name, name, l+1);
r = ioctl(bus->input_fd, KDBUS_CMD_NAME_RELEASE, n);
if (r < 0)
return -errno;
return n->flags;
} else {
r = sd_bus_call_method(
bus,
"org.freedesktop.DBus",
"/",
"org.freedesktop.DBus",
"ReleaseName",
NULL,
&reply,
"s",
name);
if (r < 0)
return r;
r = sd_bus_message_read(reply, "u", &ret);
if (r < 0)
return r;
}
return ret;
}
......
......@@ -158,6 +158,9 @@ int bus_kernel_take_fd(sd_bus *b) {
assert(b);
if (b->is_server)
return -EINVAL;
r = ioctl(b->input_fd, KDBUS_CMD_HELLO, &hello);
if (r < 0)
return -errno;
......@@ -166,6 +169,7 @@ int bus_kernel_take_fd(sd_bus *b) {
return -ENOMEM;
b->is_kernel = true;
b->bus_client = true;
r = bus_start_running(b);
if (r < 0)
......@@ -180,6 +184,9 @@ int bus_kernel_connect(sd_bus *b) {
assert(b->output_fd < 0);
assert(b->kernel);
if (b->is_server)
return -EINVAL;
b->input_fd = open(b->kernel, O_RDWR|O_NOCTTY|O_CLOEXEC);
if (b->input_fd < 0)
return -errno;
......@@ -339,7 +346,7 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess
int bus_kernel_read_message(sd_bus *bus, sd_bus_message **m) {
struct kdbus_msg *k;
size_t sz = 128;
size_t sz = 1024;
int r;
assert(bus);
......
......@@ -280,7 +280,7 @@ static int bus_send_hello(sd_bus *bus) {
int bus_start_running(sd_bus *bus) {
assert(bus);
if (bus->bus_client) {
if (bus->bus_client && !bus->is_kernel) {
bus->state = BUS_HELLO;
return 1;
}
......
......@@ -85,6 +85,15 @@ int main(int argc, char *argv[]) {
assert_se(r >= 0);
assert_se(streq(the_string, "I am a string"));
r = sd_bus_request_name(a, "net.0pointer.foobar", 0);
assert_se(r >= 0);
r = sd_bus_release_name(a, "net.0pointer.foobar");
assert_se(r >= 0);
r = sd_bus_release_name(a, "net.0pointer.foobar");
assert_se(r < 0);
sd_bus_unref(a);
sd_bus_unref(b);
......
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