[Spice-commits] 3 commits - server/Makefile.am server/reds-stream.c server/reds-stream.h server/tests

Frediano Ziglio fziglio at kemper.freedesktop.org
Thu Jan 14 03:06:40 PST 2016


 server/Makefile.am         |   10 ++-
 server/reds-stream.c       |   40 +++++++++++++++
 server/reds-stream.h       |    1 
 server/tests/Makefile.am   |   10 +++
 server/tests/stream-test.c |  118 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 175 insertions(+), 4 deletions(-)

New commits:
commit f438bd6ae979bb0e2c2536a05ee9fbf125d18b3b
Author: Marc-Andre Lureau <marcandre.lureau at gmail.com>
Date:   Wed Dec 16 00:49:21 2015 +0100

    tests: add fdpass stream test
    
    Signed-off-by: Marc-André Lureau <marcandre.lureau at gmail.com>
    Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
    Acked-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/server/tests/Makefile.am b/server/tests/Makefile.am
index 981c570..71e29c7 100644
--- a/server/tests/Makefile.am
+++ b/server/tests/Makefile.am
@@ -42,12 +42,14 @@ noinst_PROGRAMS =				\
 	test_vdagent				\
 	test_display_width_stride		\
 	spice-server-replay			\
+	stream-test				\
 	stat_test \
 	$(NULL)
 
-TESTS =	\
+TESTS =						\
 	stat_test				\
 	test-qxl-parsing			\
+	stream-test				\
 	$(NULL)
 
 check_PROGRAMS = $(TESTS)
@@ -125,6 +127,12 @@ spice_server_replay_SOURCES = 			\
 	test_display_base.h			\
 	$(NULL)
 
+stream_test_SOURCES =				\
+	$(COMMON_BASE)				\
+	stream-test.c				\
+	$(NULL)
+stream_test_LDADD = ../libserver.la
+
 stat_test_SOURCES = stat-main.c
 stat_test_LDADD = \
 	libstat_test1.a \
diff --git a/server/tests/stream-test.c b/server/tests/stream-test.c
new file mode 100644
index 0000000..7dc9fbf
--- /dev/null
+++ b/server/tests/stream-test.c
@@ -0,0 +1,118 @@
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include "common/log.h"
+#include "reds-stream.h"
+#include "basic_event_loop.h"
+
+static int server_init(void)
+{
+    SpiceCoreInterface *core = basic_event_loop_init();
+    SpiceServer *server = spice_server_new();
+
+    return spice_server_init(server, core);
+}
+
+
+/*
+ * Based on code from Keith Packard:
+ * http://keithp.com/blogs/fd-passing/
+ */
+static ssize_t
+sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd)
+{
+    ssize_t size;
+
+    if (fd) {
+        struct msghdr msg;
+        struct iovec iov;
+        union {
+            struct cmsghdr cmsghdr;
+            char control[CMSG_SPACE(sizeof (int))];
+        } cmsgu;
+        struct cmsghdr *cmsg;
+
+        iov.iov_base = buf;
+        iov.iov_len = bufsize;
+
+        msg.msg_name = NULL;
+        msg.msg_namelen = 0;
+        msg.msg_iov = &iov;
+        msg.msg_iovlen = 1;
+        msg.msg_control = cmsgu.control;
+        msg.msg_controllen = sizeof(cmsgu.control);
+        size = recvmsg(sock, &msg, 0);
+        if (size < 0) {
+            perror ("recvmsg");
+            exit(1);
+        }
+        cmsg = CMSG_FIRSTHDR(&msg);
+        if (cmsg && cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
+            if (cmsg->cmsg_level != SOL_SOCKET) {
+                fprintf(stderr, "invalid cmsg_level %d\n",
+                        cmsg->cmsg_level);
+                exit(1);
+            }
+            if (cmsg->cmsg_type != SCM_RIGHTS) {
+                fprintf(stderr, "invalid cmsg_type %d\n",
+                        cmsg->cmsg_type);
+                exit(1);
+            }
+
+            *fd = *((int *) CMSG_DATA(cmsg));
+        } else
+            *fd = -1;
+    } else {
+        size = read(sock, buf, bufsize);
+        if (size < 0) {
+            perror("read");
+            exit(1);
+        }
+    }
+
+    return size;
+}
+
+int main(int argc, char *argv[])
+{
+    RedsStream *st[2];
+    int sv[2];
+    int ret, fd = -1;
+    char c;
+
+    spice_return_val_if_fail(server_init() == 0, -1);
+
+    if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv) == -1) {
+        spice_error("socketpair failed %s", strerror(errno));
+        return -1;
+    }
+
+    st[0] = reds_stream_new(sv[0]);
+    spice_assert(reds_stream_is_plain_unix(st[0]));
+    st[1] = reds_stream_new(sv[1]);
+    spice_assert(reds_stream_is_plain_unix(st[1]));
+
+    /* send stdin, for the fun of it */
+    ret = reds_stream_send_msgfd(st[0], 0);
+    spice_assert(ret == 1);
+    ret = sock_fd_read(sv[1], &c, 1, &fd);
+    spice_assert(c == '@');
+    spice_assert(ret == 1);
+    spice_assert(fd != -1);
+    close(fd);
+
+    /* send invalid fd behaviour */
+    ret = reds_stream_send_msgfd(st[0], -1);
+    spice_assert(ret == 1);
+    ret = sock_fd_read(sv[1], &c, 1, &fd);
+    spice_assert(c == '@');
+    spice_assert(ret == 1);
+    spice_assert(fd == -1);
+
+    reds_stream_free(st[0]);
+    reds_stream_free(st[1]);
+
+    return 0;
+}
commit 5c1073266e4a8672d461fbf4bcd9e95cc67421a1
Author: Marc-André Lureau <mlureau at redhat.com>
Date:   Wed Dec 16 00:49:19 2015 +0100

    reds-stream: add send_msgfd()
    
    A new function to send fd with unix socket anciliary data.
    
    Signed-off-by: Marc-André Lureau <marcandre.lureau at gmail.com>
    Acked-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/server/reds-stream.c b/server/reds-stream.c
index edc25f7..2b1c5b9 100644
--- a/server/reds-stream.c
+++ b/server/reds-stream.c
@@ -28,6 +28,7 @@
 #include <netdb.h>
 #include <unistd.h>
 #include <sys/socket.h>
+#include <fcntl.h>
 
 #include <glib.h>
 
@@ -254,6 +255,45 @@ int reds_stream_is_plain_unix(const RedsStream *s)
 
 }
 
+int reds_stream_send_msgfd(RedsStream *stream, int fd)
+{
+    struct msghdr msgh = { 0, };
+    struct iovec iov;
+    int r;
+
+    const size_t fd_size = 1 * sizeof(int);
+    struct cmsghdr *cmsg;
+    union {
+        struct cmsghdr hdr;
+        char data[CMSG_SPACE(fd_size)];
+    } control;
+
+    spice_return_val_if_fail(reds_stream_is_plain_unix(stream), -1);
+
+    /* set the payload */
+    iov.iov_base = (char*)"@";
+    iov.iov_len = 1;
+    msgh.msg_iovlen = 1;
+    msgh.msg_iov = &iov;
+
+    if (fd != -1) {
+        msgh.msg_control = control.data;
+        msgh.msg_controllen = sizeof(control.data);
+
+        cmsg = CMSG_FIRSTHDR(&msgh);
+        cmsg->cmsg_len = CMSG_LEN(fd_size);
+        cmsg->cmsg_level = SOL_SOCKET;
+        cmsg->cmsg_type = SCM_RIGHTS;
+        memcpy(CMSG_DATA(cmsg), &fd, fd_size);
+    }
+
+    do {
+        r = sendmsg(stream->socket, &msgh, MSG_NOSIGNAL);
+    } while (r < 0 && (errno == EINTR || errno == EAGAIN));
+
+    return r;
+}
+
 ssize_t reds_stream_writev(RedsStream *s, const struct iovec *iov, int iovcnt)
 {
     int i;
diff --git a/server/reds-stream.h b/server/reds-stream.h
index 9e53b22..72e5dd1 100644
--- a/server/reds-stream.h
+++ b/server/reds-stream.h
@@ -74,6 +74,7 @@ int reds_stream_enable_ssl(RedsStream *stream, SSL_CTX *ctx);
 void reds_stream_set_info_flag(RedsStream *stream, unsigned int flag);
 int reds_stream_get_family(const RedsStream *stream);
 int reds_stream_is_plain_unix(const RedsStream *stream);
+int reds_stream_send_msgfd(RedsStream *stream, int fd);
 
 typedef enum {
     REDS_SASL_ERROR_OK,
commit 2628197b9910f269a85ff7f7a549ef8fb846a442
Author: Marc-Andre Lureau <marcandre.lureau at gmail.com>
Date:   Wed Dec 16 00:49:16 2015 +0100

    build-sys: build a utility libserver.la
    
    This allow tests programs to link with statically built library to access all symbols
    
    Signed-off-by: Marc-André Lureau <marcandre.lureau at gmail.com>
    Acked-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/server/Makefile.am b/server/Makefile.am
index 32ab8eb..b20beec 100644
--- a/server/Makefile.am
+++ b/server/Makefile.am
@@ -17,6 +17,7 @@ AM_CPPFLAGS =					\
 	$(WARN_CFLAGS)				\
 	$(NULL)
 
+noinst_LTLIBRARIES = libserver.la
 lib_LTLIBRARIES = libspice-server.la
 
 libspice_server_la_LDFLAGS =			\
@@ -31,7 +32,7 @@ libspice_server_la_LDFLAGS += \
 	-Wl,--version-script=$(top_srcdir)/server/spice-server.syms
 endif
 
-libspice_server_la_LIBADD =						\
+libserver_la_LIBADD =							\
 	$(top_builddir)/spice-common/common/libspice-common.la		\
 	$(top_builddir)/spice-common/common/libspice-common-server.la	\
 	$(GL_LIBS)							\
@@ -61,7 +62,7 @@ libspice_serverinclude_HEADERS =		\
 	spice.h					\
 	$(NULL)
 
-libspice_server_la_SOURCES =			\
+libserver_la_SOURCES =				\
 	$(libspice_serverinclude_HEADERS)	\
 	agent-msg-filter.c			\
 	agent-msg-filter.h			\
@@ -144,12 +145,15 @@ libspice_server_la_SOURCES =			\
 	$(NULL)
 
 if HAVE_SMARTCARD
-libspice_server_la_SOURCES +=	\
+libserver_la_SOURCES +=	\
 	smartcard.c		\
 	smartcard.h		\
 	$(NULL)
 endif
 
+libspice_server_la_LIBADD = libserver.la
+libspice_server_la_SOURCES =
+
 EXTRA_DIST =					\
 	spice-bitmap-utils.tmpl.c			\
 	cache-item.tmpl.c			\


More information about the Spice-commits mailing list