diff options
author | Lennart Poettering <lennart@poettering.net> | 2013-05-16 02:37:42 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2013-05-16 02:37:42 +0200 |
commit | 1307c3ff9aa9d96fff6f9f42bb760887fa9aa240 (patch) | |
tree | f8d937aa4b454af00628e1c6c008df996e0bb28f /src/libsystemd-bus/bus-kernel.c | |
parent | a392d36195f92eaa2d5b7c1d588ff8e52025a43a (diff) |
bus: implement receiving side of memfd hookup
Diffstat (limited to 'src/libsystemd-bus/bus-kernel.c')
-rw-r--r-- | src/libsystemd-bus/bus-kernel.c | 80 |
1 files changed, 54 insertions, 26 deletions
diff --git a/src/libsystemd-bus/bus-kernel.c b/src/libsystemd-bus/bus-kernel.c index a0e892a49f..f7759b6fb4 100644 --- a/src/libsystemd-bus/bus-kernel.c +++ b/src/libsystemd-bus/bus-kernel.c @@ -427,17 +427,17 @@ static void close_kdbus_msg(sd_bus *bus, struct kdbus_msg *k) { KDBUS_ITEM_FOREACH(d, k) { - if (d->type != KDBUS_MSG_FDS) - continue; - - close_many(d->fds, (d->size - offsetof(struct kdbus_item, fds)) / sizeof(int)); + if (d->type == KDBUS_MSG_FDS) + close_many(d->fds, (d->size - offsetof(struct kdbus_item, fds)) / sizeof(int)); + else if (d->type == KDBUS_MSG_PAYLOAD_MEMFD) + close_nointr_nofail(d->memfd.fd); } } static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_message **ret) { sd_bus_message *m = NULL; struct kdbus_item *d; - unsigned n_payload = 0, n_fds = 0; + unsigned n_fds = 0; _cleanup_free_ int *fds = NULL; struct bus_header *h = NULL; size_t total, n_bytes = 0, idx = 0; @@ -465,9 +465,15 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess return -EBADMSG; } - n_payload++; n_bytes += d->vec.size; + } else if (d->type == KDBUS_MSG_PAYLOAD_MEMFD) { + + if (!h) + return -EBADMSG; + + n_bytes += d->memfd.size; + } else if (d->type == KDBUS_MSG_FDS) { int *f; unsigned j; @@ -481,9 +487,7 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess memcpy(fds + n_fds, d->fds, sizeof(int) * j); n_fds += j; - } else if (d->type == KDBUS_MSG_DST_NAME) - destination = d->str; - else if (d->type == KDBUS_MSG_SRC_SECLABEL) + } else if (d->type == KDBUS_MSG_SRC_SECLABEL) seclabel = d->str; } @@ -518,8 +522,8 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess part = message_append_part(m); if (!part) { - sd_bus_message_unref(m); - return -ENOMEM; + r = -ENOMEM; + goto fail; } if (idx >= begin_body) { @@ -535,6 +539,25 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess } idx += d->vec.size; + } else if (d->type == KDBUS_MSG_PAYLOAD_MEMFD) { + struct bus_body_part *part; + + if (idx < BUS_MESSAGE_BODY_BEGIN(m)) { + r = -EBADMSG; + goto fail; + } + + part = message_append_part(m); + if (!part) { + r = -ENOMEM; + goto fail; + } + + part->memfd = d->memfd.fd; + part->size = d->memfd.size; + part->sealed = true; + + idx += d->memfd.size; } else if (d->type == KDBUS_MSG_SRC_CREDS) { m->pid_starttime = d->creds.starttime / NSEC_PER_USEC; @@ -562,17 +585,16 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess else if (d->type == KDBUS_MSG_SRC_CAPS) { m->capability = d->data; m->capability_size = l; - } else if (d->type != KDBUS_MSG_FDS && - d->type != KDBUS_MSG_DST_NAME && + } else if (d->type == KDBUS_MSG_DST_NAME) + destination = d->str; + else if (d->type != KDBUS_MSG_FDS && d->type != KDBUS_MSG_SRC_SECLABEL) log_debug("Got unknown field from kernel %llu", d->type); } r = bus_message_parse_fields(m); - if (r < 0) { - sd_bus_message_unref(m); - return r; - } + if (r < 0) + goto fail; if (k->src_id == KDBUS_SRC_ID_KERNEL) m->sender = "org.freedesktop.DBus"; @@ -601,6 +623,21 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess *ret = m; return 1; + +fail: + if (m) { + struct bus_body_part *part; + unsigned i; + + /* Make sure the memfds are not freed twice */ + MESSAGE_FOREACH_PART(part, i, m) + if (part->memfd >= 0) + part->memfd = -1; + + sd_bus_message_unref(m); + } + + return r; } int bus_kernel_read_message(sd_bus *bus, sd_bus_message **m) { @@ -618,15 +655,6 @@ int bus_kernel_read_message(sd_bus *bus, sd_bus_message **m) { return -errno; } - -/* /\* Let's tell valgrind that there's really no need to */ -/* * initialize this fully. This should be removed again */ -/* * when valgrind learned the kdbus ioctls natively. *\/ */ -/* #ifdef HAVE_VALGRIND_MEMCHECK_H */ -/* VALGRIND_MAKE_MEM_DEFINED(k, sz); */ -/* #endif */ - - r = bus_kernel_make_message(bus, k, m); if (r <= 0) close_kdbus_msg(bus, k); |