From a18bed136f866584daa2a03fe4cf12cc91d295bb Mon Sep 17 00:00:00 2001 From: Marc-Andre Lureau Date: Wed, 16 Dec 2015 00:27:22 +0100 Subject: [PATCH] marshaller: learn to describe fd passing in messages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The marshaller can't serialize fd in memory stream. Instead, append the fd to the marshaller structure. The marshaller user is responsible for sending the fd when the message is sent. The fd to send can be retrieved with spice_marshaller_get_fd(). Note: only a single fd is supported with this API, supporting multiple fd is left for the future if it becomes necessary. Signed-off-by: Marc-André Lureau Acked-by: Frediano Ziglio --- common/marshaller.c | 24 ++++++++++++++++++++++++ common/marshaller.h | 3 +++ 2 files changed, 27 insertions(+) diff --git a/common/marshaller.c b/common/marshaller.c index cedb321..c967371 100644 --- a/common/marshaller.c +++ b/common/marshaller.c @@ -19,11 +19,14 @@ #include #endif +#include "log.h" #include "marshaller.h" #include "mem.h" #include #include #include +#include +#include #ifdef WORDS_BIGENDIAN #define write_int8(ptr,v) (*((int8_t *)(ptr)) = v) @@ -84,6 +87,7 @@ struct SpiceMarshaller { MarshallerItem *items; MarshallerItem static_items[N_STATIC_ITEMS]; + int fd; }; struct SpiceMarshallerData { @@ -111,6 +115,7 @@ static void spice_marshaller_init(SpiceMarshaller *m, m->n_items = 0; m->items_size = N_STATIC_ITEMS; m->items = m->static_items; + m->fd = -1; } SpiceMarshaller *spice_marshaller_new(void) @@ -613,3 +618,22 @@ void *spice_marshaller_add_int8(SpiceMarshaller *m, int8_t v) write_int8(ptr, v); return (void *)ptr; } + +void spice_marshaller_add_fd(SpiceMarshaller *m, int fd) +{ + spice_assert(m->fd == -1); + + m->fd = dup(fd); + if (m->fd == -1) { + perror("dup"); + } +} + +int spice_marshaller_get_fd(SpiceMarshaller *m) +{ + int fd = m->fd; + + m->fd = -1; + + return fd; +} diff --git a/common/marshaller.h b/common/marshaller.h index e19c0f6..b698b69 100644 --- a/common/marshaller.h +++ b/common/marshaller.h @@ -66,6 +66,9 @@ void *spice_marshaller_add_int8(SpiceMarshaller *m, int8_t v); void spice_marshaller_set_uint32(SpiceMarshaller *m, void *ref, uint32_t v); +void spice_marshaller_add_fd(SpiceMarshaller *m, int fd); +int spice_marshaller_get_fd(SpiceMarshaller *m); + SPICE_END_DECLS #endif