diff options
author | Lennart Poettering <lennart@poettering.net> | 2013-04-21 22:24:50 -0300 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2013-04-21 22:24:50 -0300 |
commit | 9097fe299f22c46316979356d945ebb494e85814 (patch) | |
tree | 9573511bd9cdc16c7ce0c586fa9ac57ff9e42210 /src/libsystemd-bus | |
parent | fcdfc159248fa484f443e17ab2bde8e354ecc560 (diff) |
bus: implement client logic for fd passing
Diffstat (limited to 'src/libsystemd-bus')
-rw-r--r-- | src/libsystemd-bus/bus-kernel.c | 25 | ||||
-rw-r--r-- | src/libsystemd-bus/test-bus-kernel.c | 28 |
2 files changed, 49 insertions, 4 deletions
diff --git a/src/libsystemd-bus/bus-kernel.c b/src/libsystemd-bus/bus-kernel.c index 5172b32113..115297dca4 100644 --- a/src/libsystemd-bus/bus-kernel.c +++ b/src/libsystemd-bus/bus-kernel.c @@ -35,6 +35,7 @@ #define KDBUS_ITEM_NEXT(item) \ (typeof(item))(((uint8_t *)item) + ALIGN8((item)->size)) + #define KDBUS_ITEM_FOREACH(item, head) \ for (item = (head)->items; \ (uint8_t *)(item) < (uint8_t *)(head) + (head)->size; \ @@ -100,6 +101,19 @@ static void* append_bloom(struct kdbus_msg_item **d, size_t length) { return r; } +static void append_fds(struct kdbus_msg_item **d, const int fds[], unsigned n_fds) { + assert(d); + assert(fds); + assert(n_fds > 0); + + *d = ALIGN8_PTR(*d); + (*d)->size = offsetof(struct kdbus_msg_item, fds) + sizeof(int) * n_fds; + (*d)->type = KDBUS_MSG_UNIX_FDS; + memcpy((*d)->fds, fds, sizeof(int) * n_fds); + + *d = (struct kdbus_msg_item *) ((uint8_t*) *d + (*d)->size); +} + static int bus_message_setup_bloom(sd_bus_message *m, void *bloom) { unsigned i; int r; @@ -200,6 +214,10 @@ static int bus_message_setup_kmsg(sd_bus *b, sd_bus_message *m) { sz += ALIGN8(offsetof(struct kdbus_msg_item, str) + dl + 1); } + /* Add space for unix fds */ + if (m->n_fds > 0) + sz += ALIGN8(offsetof(struct kdbus_msg_item, fds) + sizeof(int)*m->n_fds); + m->kdbus = memalign(8, sz); if (!m->kdbus) return -ENOMEM; @@ -233,7 +251,6 @@ static int bus_message_setup_kmsg(sd_bus *b, sd_bus_message *m) { if (m->kdbus->dst_id == KDBUS_DST_ID_BROADCAST) { void *p; - /* For now, let's add a mask all bloom filter */ p = append_bloom(&d, BLOOM_SIZE); r = bus_message_setup_bloom(m, p); if (r < 0) { @@ -243,6 +260,9 @@ static int bus_message_setup_kmsg(sd_bus *b, sd_bus_message *m) { } } + if (m->n_fds > 0) + append_fds(&d, m->fds, m->n_fds); + m->kdbus->size = (uint8_t*) d - (uint8_t*) m->kdbus; assert(m->kdbus->size <= sz); @@ -289,6 +309,7 @@ int bus_kernel_take_fd(sd_bus *b) { b->is_kernel = true; b->bus_client = true; + b->can_fds = true; r = bus_start_running(b); if (r < 0) @@ -389,7 +410,7 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess return -ENOMEM; fds = f; - memcpy(fds + n_fds, d->fds, j); + memcpy(fds + n_fds, d->fds, sizeof(int) * j); n_fds += j; } else if (d->type == KDBUS_MSG_DST_NAME) diff --git a/src/libsystemd-bus/test-bus-kernel.c b/src/libsystemd-bus/test-bus-kernel.c index ffabdcbd14..1095e57e42 100644 --- a/src/libsystemd-bus/test-bus-kernel.c +++ b/src/libsystemd-bus/test-bus-kernel.c @@ -19,6 +19,8 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>. ***/ +#include <fcntl.h> + #include "util.h" #include "sd-bus.h" @@ -32,7 +34,7 @@ int main(int argc, char *argv[]) { _cleanup_bus_message_unref_ sd_bus_message *m = NULL; const char *ua = NULL, *ub = NULL, *the_string = NULL; sd_bus *a, *b; - int r; + int r, pipe_fds[2]; bus_ref = bus_kernel_create("deine-mutter", &bus_name); if (bus_ref == -ENOENT) @@ -108,6 +110,19 @@ int main(int argc, char *argv[]) { r = sd_bus_message_new_method_call(b, "net.x0pointer.foobar", "/a/path", "an.inter.face", "AMethod", &m); assert_se(r >= 0); + assert_se(pipe2(pipe_fds, O_CLOEXEC) >= 0); + + assert_se(write(pipe_fds[1], "x", 1) == 1); + + close_nointr_nofail(pipe_fds[1]); + pipe_fds[1] = -1; + + r = sd_bus_message_append(m, "h", pipe_fds[0]); + assert_se(r >= 0); + + close_nointr_nofail(pipe_fds[0]); + pipe_fds[0] = -1; + r = sd_bus_send(b, m, NULL); assert_se(r >= 0); @@ -121,8 +136,17 @@ int main(int argc, char *argv[]) { bus_message_dump(m); assert_se(sd_bus_message_rewind(m, true) >= 0); - if (sd_bus_message_is_method_call(m, "an.inter.face", "AMethod")) + if (sd_bus_message_is_method_call(m, "an.inter.face", "AMethod")) { + int fd; + char x; + + r = sd_bus_message_read(m, "h", &fd); + assert_se(r >= 0); + + assert_se(read(fd, &x, 1) == 1); + assert_se(x == 'x'); break; + } } r = sd_bus_release_name(a, "net.x0pointer.foobar"); |