Commit f08838da authored by Lennart Poettering's avatar Lennart Poettering
Browse files

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