Blame 0233-Separate-inet_connect-into-inet_connect-blocking-and.patch

5544c1
From 36f7af3f8bfd2c16f2e6c4b61d9564d5cfdd2394 Mon Sep 17 00:00:00 2001
5544c1
From: Orit Wasserman <owasserm@redhat.com>
5544c1
Date: Mon, 24 Sep 2012 13:11:08 +0200
5544c1
Subject: [PATCH] Separate inet_connect into inet_connect (blocking) and
5544c1
 inet_nonblocking_connect
5544c1
5544c1
No need to add non blocking parameters to the blocking inet_connect
5544c1
add block parameter for inet_connect_opts instead of using QemuOpt "block".
5544c1
5544c1
Signed-off-by: Orit Wasserman <owasserm@redhat.com>
5544c1
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
5544c1
(cherry picked from commit 5db5f44cb4b7f24b9e0efdefc9015e36b7c34881)
5544c1
5544c1
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
5544c1
---
5544c1
 migration-tcp.c |  2 +-
5544c1
 nbd.c           |  2 +-
5544c1
 qemu-char.c     |  2 +-
5544c1
 qemu-sockets.c  | 58 +++++++++++++++++++++++++++++++++++++++++++++++----------
5544c1
 qemu_socket.h   |  7 +++++--
5544c1
 ui/vnc.c        |  2 +-
5544c1
 6 files changed, 57 insertions(+), 16 deletions(-)
5544c1
5544c1
diff --git a/migration-tcp.c b/migration-tcp.c
5544c1
index ac891c3..7f6ad98 100644
5544c1
--- a/migration-tcp.c
5544c1
+++ b/migration-tcp.c
5544c1
@@ -88,7 +88,7 @@ int tcp_start_outgoing_migration(MigrationState *s, const char *host_port,
5544c1
     s->write = socket_write;
5544c1
     s->close = tcp_close;
5544c1
 
5544c1
-    s->fd = inet_connect(host_port, false, &in_progress, errp);
5544c1
+    s->fd = inet_nonblocking_connect(host_port, &in_progress, errp);
5544c1
     if (error_is_set(errp)) {
5544c1
         migrate_fd_error(s);
5544c1
         return -1;
5544c1
diff --git a/nbd.c b/nbd.c
5544c1
index 0dd60c5..206f75c 100644
5544c1
--- a/nbd.c
5544c1
+++ b/nbd.c
5544c1
@@ -162,7 +162,7 @@ int tcp_socket_outgoing(const char *address, uint16_t port)
5544c1
 
5544c1
 int tcp_socket_outgoing_spec(const char *address_and_port)
5544c1
 {
5544c1
-    return inet_connect(address_and_port, true, NULL, NULL);
5544c1
+    return inet_connect(address_and_port, NULL);
5544c1
 }
5544c1
 
5544c1
 int tcp_socket_incoming(const char *address, uint16_t port)
5544c1
diff --git a/qemu-char.c b/qemu-char.c
5544c1
index 7f0f895..13b87b5 100644
5544c1
--- a/qemu-char.c
5544c1
+++ b/qemu-char.c
5544c1
@@ -2456,7 +2456,7 @@ static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
5544c1
         if (is_listen) {
5544c1
             fd = inet_listen_opts(opts, 0, NULL);
5544c1
         } else {
5544c1
-            fd = inet_connect_opts(opts, NULL, NULL);
5544c1
+            fd = inet_connect_opts(opts, true, NULL, NULL);
5544c1
         }
5544c1
     }
5544c1
     if (fd < 0) {
5544c1
diff --git a/qemu-sockets.c b/qemu-sockets.c
5544c1
index 22797bf..0883a66 100644
5544c1
--- a/qemu-sockets.c
5544c1
+++ b/qemu-sockets.c
5544c1
@@ -54,9 +54,6 @@ static QemuOptsList dummy_opts = {
5544c1
         },{
5544c1
             .name = "ipv6",
5544c1
             .type = QEMU_OPT_BOOL,
5544c1
-        },{
5544c1
-            .name = "block",
5544c1
-            .type = QEMU_OPT_BOOL,
5544c1
         },
5544c1
         { /* end if list */ }
5544c1
     },
5544c1
@@ -294,11 +291,22 @@ static struct addrinfo *inet_parse_connect_opts(QemuOpts *opts, Error **errp)
5544c1
     return res;
5544c1
 }
5544c1
 
5544c1
-int inet_connect_opts(QemuOpts *opts, bool *in_progress, Error **errp)
5544c1
+/**
5544c1
+ * Create a socket and connect it to an address.
5544c1
+ *
5544c1
+ * @opts: QEMU options, recognized parameters strings "host" and "port",
5544c1
+ *        bools "ipv4" and "ipv6".
5544c1
+ * @block: set true for blocking socket
5544c1
+ * @in_progress: set to true in case of ongoing connect
5544c1
+ * @errp: set on error
5544c1
+ *
5544c1
+ * Returns: -1 on error, file descriptor on success.
5544c1
+ */
5544c1
+int inet_connect_opts(QemuOpts *opts, bool block, bool *in_progress,
5544c1
+                      Error **errp)
5544c1
 {
5544c1
     struct addrinfo *res, *e;
5544c1
     int sock = -1;
5544c1
-    bool block = qemu_opt_get_bool(opts, "block", 0);
5544c1
 
5544c1
     res = inet_parse_connect_opts(opts, errp);
5544c1
     if (!res) {
5544c1
@@ -515,17 +523,47 @@ int inet_listen(const char *str, char *ostr, int olen,
5544c1
     return sock;
5544c1
 }
5544c1
 
5544c1
-int inet_connect(const char *str, bool block, bool *in_progress, Error **errp)
5544c1
+/**
5544c1
+ * Create a blocking socket and connect it to an address.
5544c1
+ *
5544c1
+ * @str: address string
5544c1
+ * @errp: set in case of an error
5544c1
+ *
5544c1
+ * Returns -1 in case of error, file descriptor on success
5544c1
+ **/
5544c1
+int inet_connect(const char *str, Error **errp)
5544c1
 {
5544c1
     QemuOpts *opts;
5544c1
     int sock = -1;
5544c1
 
5544c1
     opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
5544c1
     if (inet_parse(opts, str) == 0) {
5544c1
-        if (block) {
5544c1
-            qemu_opt_set(opts, "block", "on");
5544c1
-        }
5544c1
-        sock = inet_connect_opts(opts, in_progress, errp);
5544c1
+        sock = inet_connect_opts(opts, true, NULL, errp);
5544c1
+    } else {
5544c1
+        error_set(errp, QERR_SOCKET_CREATE_FAILED);
5544c1
+    }
5544c1
+    qemu_opts_del(opts);
5544c1
+    return sock;
5544c1
+}
5544c1
+
5544c1
+/**
5544c1
+ * Create a non-blocking socket and connect it to an address.
5544c1
+ *
5544c1
+ * @str: address string
5544c1
+ * @in_progress: set to true in case of ongoing connect
5544c1
+ * @errp: set in case of an error
5544c1
+ *
5544c1
+ * Returns: -1 on error, file descriptor on success.
5544c1
+ **/
5544c1
+int inet_nonblocking_connect(const char *str, bool *in_progress,
5544c1
+                             Error **errp)
5544c1
+{
5544c1
+    QemuOpts *opts;
5544c1
+    int sock = -1;
5544c1
+
5544c1
+    opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
5544c1
+    if (inet_parse(opts, str) == 0) {
5544c1
+        sock = inet_connect_opts(opts, false, in_progress, errp);
5544c1
     } else {
5544c1
         error_set(errp, QERR_SOCKET_CREATE_FAILED);
5544c1
     }
5544c1
diff --git a/qemu_socket.h b/qemu_socket.h
5544c1
index 30ae6af..80696aa 100644
5544c1
--- a/qemu_socket.h
5544c1
+++ b/qemu_socket.h
5544c1
@@ -42,8 +42,11 @@ int send_all(int fd, const void *buf, int len1);
5544c1
 int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp);
5544c1
 int inet_listen(const char *str, char *ostr, int olen,
5544c1
                 int socktype, int port_offset, Error **errp);
5544c1
-int inet_connect_opts(QemuOpts *opts, bool *in_progress, Error **errp);
5544c1
-int inet_connect(const char *str, bool block, bool *in_progress, Error **errp);
5544c1
+int inet_connect_opts(QemuOpts *opts, bool block, bool *in_progress,
5544c1
+                      Error **errp);
5544c1
+int inet_connect(const char *str, Error **errp);
5544c1
+int inet_nonblocking_connect(const char *str, bool *in_progress,
5544c1
+                             Error **errp);
5544c1
 int inet_dgram_opts(QemuOpts *opts);
5544c1
 const char *inet_strfamily(int family);
5544c1
 
5544c1
diff --git a/ui/vnc.c b/ui/vnc.c
5544c1
index 385e345..01b2daf 100644
5544c1
--- a/ui/vnc.c
5544c1
+++ b/ui/vnc.c
5544c1
@@ -3061,7 +3061,7 @@ int vnc_display_open(DisplayState *ds, const char *display)
5544c1
         if (strncmp(display, "unix:", 5) == 0)
5544c1
             vs->lsock = unix_connect(display+5);
5544c1
         else
5544c1
-            vs->lsock = inet_connect(display, true, NULL, NULL);
5544c1
+            vs->lsock = inet_connect(display, NULL);
5544c1
         if (-1 == vs->lsock) {
5544c1
             g_free(vs->display);
5544c1
             vs->display = NULL;
5544c1
-- 
5544c1
1.7.12.1
5544c1