marshaller: learn to describe fd passing in messages

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 <marcandre.lureau@gmail.com>
Acked-by: Frediano Ziglio <fziglio@redhat.com>
This commit is contained in:
Marc-Andre Lureau 2015-12-16 00:27:22 +01:00 committed by Frediano Ziglio
parent 80cc3f680a
commit a18bed136f
2 changed files with 27 additions and 0 deletions

View File

@ -19,11 +19,14 @@
#include <config.h>
#endif
#include "log.h"
#include "marshaller.h"
#include "mem.h"
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#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;
}

View File

@ -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