[systemd-commits] 2 commits - src/libsystemd-bus
Lennart Poettering
lennart at kemper.freedesktop.org
Wed May 15 17:38:27 PDT 2013
src/libsystemd-bus/bus-kernel.c | 136 ++++++++++++++++++++++++--------
src/libsystemd-bus/bus-message.c | 41 ++++++++-
src/libsystemd-bus/bus-message.h | 3
src/libsystemd-bus/test-bus-zero-copy.c | 22 ++++-
4 files changed, 159 insertions(+), 43 deletions(-)
New commits:
commit 1307c3ff9aa9d96fff6f9f42bb760887fa9aa240
Author: Lennart Poettering <lennart at poettering.net>
Date: Thu May 16 02:37:42 2013 +0200
bus: implement receiving side of memfd hookup
diff --git a/src/libsystemd-bus/bus-kernel.c b/src/libsystemd-bus/bus-kernel.c
index a0e892a..f7759b6 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);
diff --git a/src/libsystemd-bus/bus-message.c b/src/libsystemd-bus/bus-message.c
index b5a3115..209fd71 100644
--- a/src/libsystemd-bus/bus-message.c
+++ b/src/libsystemd-bus/bus-message.c
@@ -2196,6 +2196,13 @@ int bus_body_part_map(struct bus_body_part *part) {
if (part->size <= 0)
return 0;
+ /* For smaller zero parts (as used for padding) we don't need to map anything... */
+ if (part->memfd < 0 && part->is_zero && part->size < 8) {
+ static const uint8_t zeroes[7] = { };
+ part->data = (void*) zeroes;
+ return 0;
+ }
+
psz = PAGE_ALIGN(part->size);
if (part->memfd >= 0)
@@ -2222,9 +2229,6 @@ void bus_body_part_unmap(struct bus_body_part *part) {
if (part->memfd < 0)
return;
- if (!part->sealed)
- return;
-
if (!part->data)
return;
diff --git a/src/libsystemd-bus/test-bus-zero-copy.c b/src/libsystemd-bus/test-bus-zero-copy.c
index 0d8435e..5a9f454 100644
--- a/src/libsystemd-bus/test-bus-zero-copy.c
+++ b/src/libsystemd-bus/test-bus-zero-copy.c
@@ -111,6 +111,12 @@ int main(int argc, char *argv[]) {
sd_bus_message_unref(m);
+ r = sd_bus_process(a, &m);
+ assert_se(r > 0);
+
+ bus_message_dump(m);
+ sd_bus_message_unref(m);
+
sd_bus_unref(a);
sd_bus_unref(b);
commit a392d36195f92eaa2d5b7c1d588ff8e52025a43a
Author: Lennart Poettering <lennart at poettering.net>
Date: Thu May 16 02:04:13 2013 +0200
bus: put together messages with memfd payload correctly
diff --git a/src/libsystemd-bus/bus-kernel.c b/src/libsystemd-bus/bus-kernel.c
index 54a5e16..a0e892a 100644
--- a/src/libsystemd-bus/bus-kernel.c
+++ b/src/libsystemd-bus/bus-kernel.c
@@ -65,19 +65,32 @@ static int parse_unique_name(const char *s, uint64_t *id) {
static void append_payload_vec(struct kdbus_item **d, const void *p, size_t sz) {
assert(d);
- assert(p);
assert(sz > 0);
*d = ALIGN8_PTR(*d);
(*d)->size = offsetof(struct kdbus_item, vec) + sizeof(struct kdbus_vec);
(*d)->type = KDBUS_MSG_PAYLOAD_VEC;
- (*d)->vec.address = (uint64_t) p;
+ (*d)->vec.address = PTR_TO_UINT64(p);
(*d)->vec.size = sz;
*d = (struct kdbus_item *) ((uint8_t*) *d + (*d)->size);
}
+static void append_payload_memfd(struct kdbus_item **d, int memfd, size_t sz) {
+ assert(d);
+ assert(memfd >= 0);
+ assert(sz > 0);
+
+ *d = ALIGN8_PTR(*d);
+ (*d)->size = offsetof(struct kdbus_item, memfd) + sizeof(struct kdbus_memfd);
+ (*d)->type = KDBUS_MSG_PAYLOAD_MEMFD;
+ (*d)->memfd.fd = memfd;
+ (*d)->memfd.size = sz;
+
+ *d = (struct kdbus_item *) ((uint8_t*) *d + (*d)->size);
+}
+
static void append_destination(struct kdbus_item **d, const char *s, size_t length) {
assert(d);
assert(s);
@@ -210,6 +223,9 @@ static int bus_message_setup_kmsg(sd_bus *b, sd_bus_message *m) {
sz = offsetof(struct kdbus_msg, items);
+ assert_cc(ALIGN8(offsetof(struct kdbus_item, vec) + sizeof(struct kdbus_vec)) ==
+ ALIGN8(offsetof(struct kdbus_item, memfd) + sizeof(struct kdbus_memfd)));
+
/* Add in fixed header, fields header and payload */
sz += (1 + m->n_body_parts) *
ALIGN8(offsetof(struct kdbus_item, vec) + sizeof(struct kdbus_vec));
@@ -250,19 +266,38 @@ static int bus_message_setup_kmsg(sd_bus *b, sd_bus_message *m) {
append_destination(&d, m->destination, dl);
append_payload_vec(&d, m->header, BUS_MESSAGE_BODY_BEGIN(m));
- MESSAGE_FOREACH_PART(part, i, m)
+
+ MESSAGE_FOREACH_PART(part, i, m) {
+ if (part->is_zero) {
+ append_payload_vec(&d, NULL, part->size);
+ continue;
+ }
+
+ if (part->memfd >= 0 && part->sealed) {
+ bus_body_part_unmap(part);
+
+ if (!part->data) {
+ append_payload_memfd(&d, part->memfd, part->size);
+ continue;
+ }
+ }
+
+ if (part->memfd >= 0) {
+ r = bus_body_part_map(part);
+ if (r < 0)
+ goto fail;
+ }
+
append_payload_vec(&d, part->data, part->size);
+ }
if (m->kdbus->dst_id == KDBUS_DST_ID_BROADCAST) {
void *p;
p = append_bloom(&d, BLOOM_SIZE);
r = bus_message_setup_bloom(m, p);
- if (r < 0) {
- free(m->kdbus);
- m->kdbus = NULL;
- return -r;
- }
+ if (r < 0)
+ goto fail;
}
if (m->n_fds > 0)
@@ -274,6 +309,11 @@ static int bus_message_setup_kmsg(sd_bus *b, sd_bus_message *m) {
m->free_kdbus = true;
return 0;
+
+fail:
+ free(m->kdbus);
+ m->kdbus = NULL;
+ return r;
}
int bus_kernel_take_fd(sd_bus *b) {
diff --git a/src/libsystemd-bus/bus-message.c b/src/libsystemd-bus/bus-message.c
index 747b44a..b5a3115 100644
--- a/src/libsystemd-bus/bus-message.c
+++ b/src/libsystemd-bus/bus-message.c
@@ -2184,7 +2184,7 @@ int sd_bus_message_append_array_memfd(sd_bus_message *m, char type, sd_memfd *me
return sd_bus_message_close_container(m);
}
-static int body_part_map_for_read(struct bus_body_part *part) {
+int bus_body_part_map(struct bus_body_part *part) {
void *p;
size_t psz;
@@ -2210,9 +2210,36 @@ static int body_part_map_for_read(struct bus_body_part *part) {
part->mapped = psz;
part->data = p;
+ part->munmap_this = true;
+
return 0;
}
+void bus_body_part_unmap(struct bus_body_part *part) {
+
+ assert_se(part);
+
+ if (part->memfd < 0)
+ return;
+
+ if (!part->sealed)
+ return;
+
+ if (!part->data)
+ return;
+
+ if (!part->munmap_this)
+ return;
+
+ assert_se(munmap(part->data, part->mapped) == 0);
+
+ part->data = NULL;
+ part->mapped = 0;
+ part->munmap_this = false;
+
+ return;
+}
+
static int buffer_peek(const void *p, uint32_t sz, size_t *rindex, size_t align, size_t nbytes, void **r) {
size_t k, start, end;
@@ -2271,7 +2298,7 @@ static struct bus_body_part* find_part(sd_bus_message *m, size_t index, size_t s
if (index + sz <= begin + part->size) {
- r = body_part_map_for_read(part);
+ r = bus_body_part_map(part);
if (r < 0)
return NULL;
@@ -3709,8 +3736,10 @@ int bus_message_seal(sd_bus_message *m, uint64_t serial) {
MESSAGE_FOREACH_PART(part, i, m)
if (part->memfd >= 0 && !part->sealed) {
- ioctl(part->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 1);
- part->sealed = true;
+ bus_body_part_unmap(part);
+
+ if (ioctl(part->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 1) >= 0)
+ part->sealed = true;
}
m->header->serial = serial;
diff --git a/src/libsystemd-bus/bus-message.h b/src/libsystemd-bus/bus-message.h
index f9c8fc9..44390c6 100644
--- a/src/libsystemd-bus/bus-message.h
+++ b/src/libsystemd-bus/bus-message.h
@@ -232,3 +232,6 @@ struct bus_body_part *message_append_part(sd_bus_message *m);
#define MESSAGE_FOREACH_PART(part, i, m) \
for ((i) = 0, (part) = &(m)->body; (i) < (m)->n_body_parts; (i)++, (part) = (part)->next)
+
+int bus_body_part_map(struct bus_body_part *part);
+void bus_body_part_unmap(struct bus_body_part *part);
diff --git a/src/libsystemd-bus/test-bus-zero-copy.c b/src/libsystemd-bus/test-bus-zero-copy.c
index 024a0bf..0d8435e 100644
--- a/src/libsystemd-bus/test-bus-zero-copy.c
+++ b/src/libsystemd-bus/test-bus-zero-copy.c
@@ -38,6 +38,7 @@ int main(int argc, char *argv[]) {
int r, bus_ref;
sd_bus_message *m;
sd_memfd *f;
+ uint64_t sz;
log_set_max_level(LOG_DEBUG);
@@ -79,21 +80,27 @@ int main(int argc, char *argv[]) {
memset(p, 'L', 32);
- r = sd_memfd_new_and_map(&f, 32, &p);
+ r = sd_memfd_new_and_map(&f, 17, &p);
assert_se(r >= 0);
- memset(p, 'P', 32);
- munmap(p, 32);
+ memset(p, 'P', 17);
+ munmap(p, 17);
- r = sd_memfd_set_size(f, 32);
+ r = sd_memfd_get_size(f, &sz);
assert_se(r >= 0);
+ assert_se(sz == 17);
r = sd_bus_message_append_array_memfd(m, 'y', f);
assert_se(r >= 0);
+ sd_memfd_free(f);
+
r = sd_bus_message_close_container(m);
assert_se(r >= 0);
+ r = sd_bus_message_append(m, "u", 4711);
+ assert_se(r >= 0);
+
r = bus_message_seal(m, 55);
assert_se(r >= 0);
@@ -106,7 +113,6 @@ int main(int argc, char *argv[]) {
sd_bus_unref(a);
sd_bus_unref(b);
- sd_memfd_free(f);
return 0;
}
More information about the systemd-commits
mailing list