[Spice-devel] [spice-server 2/3] reds-stream: Introduce reds_stream_set_non_blocking() helper

Christophe Fergeau cfergeau at redhat.com
Wed Mar 8 16:18:16 UTC 2017


This new function removes one place outside of RedsStream which needs to
access RedsStream::socket
---
 server/reds-stream.c | 17 +++++++++++++++++
 server/reds-stream.h |  1 +
 server/reds.c        | 21 +++++++--------------
 3 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/server/reds-stream.c b/server/reds-stream.c
index 8faa174..dd0eb8a 100644
--- a/server/reds-stream.c
+++ b/server/reds-stream.c
@@ -272,6 +272,23 @@ bool reds_stream_set_no_delay(RedsStream *stream, bool no_delay)
     return TRUE;
 }
 
+bool reds_stream_set_non_blocking(RedsStream *stream, bool non_blocking)
+{
+    int flags;
+
+    if ((flags = fcntl(stream->socket, F_GETFL)) == -1) {
+        spice_warning("fnctl(F_GETFL) failed, %s", strerror(errno));
+        return FALSE;
+    }
+
+    if (fcntl(stream->socket, F_SETFL, flags | O_NONBLOCK) == -1) {
+        spice_warning("fnctl(F_SETFL) failed, %s", strerror(errno));
+        return FALSE;
+    }
+
+    return TRUE;
+}
+
 int reds_stream_send_msgfd(RedsStream *stream, int fd)
 {
     struct msghdr msgh = { 0, };
diff --git a/server/reds-stream.h b/server/reds-stream.h
index 568ec49..97cfcd5 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);
 int reds_stream_get_family(const RedsStream *stream);
 int reds_stream_is_plain_unix(const RedsStream *stream);
 bool reds_stream_set_no_delay(RedsStream *stream, bool no_delay);
+bool reds_stream_set_non_blocking(RedsStream *stream, bool non_blocking);
 int reds_stream_send_msgfd(RedsStream *stream, int fd);
 
 typedef enum {
diff --git a/server/reds.c b/server/reds.c
index f1c3ef9..77976e6 100644
--- a/server/reds.c
+++ b/server/reds.c
@@ -31,8 +31,6 @@
 #include <limits.h>
 #include <pthread.h>
 #include <sys/mman.h>
-#include <fcntl.h>
-#include <errno.h>
 #include <ctype.h>
 #include <stdbool.h>
 
@@ -2405,24 +2403,17 @@ static bool reds_init_keepalive(int socket)
 
 static RedLinkInfo *reds_init_client_connection(RedsState *reds, int socket)
 {
-    RedLinkInfo *link;
-    int flags;
-
-    if ((flags = fcntl(socket, F_GETFL)) == -1) {
-        spice_warning("accept failed, %s", strerror(errno));
-        goto error;
-    }
-
-    if (fcntl(socket, F_SETFL, flags | O_NONBLOCK) == -1) {
-        spice_warning("accept failed, %s", strerror(errno));
-        goto error;
-    }
+    RedLinkInfo *link = NULL;
 
     reds_init_keepalive(socket);
 
     link = spice_new0(RedLinkInfo, 1);
     link->reds = reds;
     link->stream = reds_stream_new(reds, socket);
+    if (!reds_stream_set_non_blocking(link->stream, TRUE))
+    {
+       goto error;
+    }
     reds_stream_set_no_delay(link->stream, TRUE);
 
     /* gather info + send event */
@@ -2434,6 +2425,8 @@ static RedLinkInfo *reds_init_client_connection(RedsState *reds, int socket)
     return link;
 
 error:
+    free(link);
+    reds_stream_free(link->stream);
     return NULL;
 }
 
-- 
2.9.3



More information about the Spice-devel mailing list