From fff990f5ce37b4402f4af389914ef90a509fff7d Mon Sep 17 00:00:00 2001
Message-Id: <fff990f5ce37b4402f4af389914ef90a509fff7d.1349175436.git.minovotn@redhat.com>
In-Reply-To: <94968b7fa9b14e71f004474d7ce77e189e6a2bf3.1349175436.git.minovotn@redhat.com>
References: <94968b7fa9b14e71f004474d7ce77e189e6a2bf3.1349175436.git.minovotn@redhat.com>
From: Amos Kong <akong@redhat.com>
Date: Mon, 1 Oct 2012 14:12:45 +0200
Subject: [PATCH 32/34] Separate inet_connect into inet_connect (blocking) and
 inet_nonblocking_connect

RH-Author: Amos Kong <akong@redhat.com>
Message-id: <1349100767-9066-13-git-send-email-akong@redhat.com>
Patchwork-id: 42571
O-Subject: [RHEL-6.4 qemu-kvm PATCH v7 12/14] Separate inet_connect into inet_connect (blocking) and inet_nonblocking_connect
Bugzilla: 680356
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
RH-Acked-by: Markus Armbruster <armbru@redhat.com>
RH-Acked-by: Orit Wasserman <owasserm@redhat.com>

From: Orit Wasserman <owasserm@redhat.com>

No need to add non blocking parameters to the blocking inet_connect
add block parameter for inet_connect_opts instead of using QemuOpt "block".

(Cherry-picked from commit 5db5f44cb4b7f24b9e0efdefc9015e36b7c34881)

Conflicts:
    nbd.c
    ui/vnc.c

Signed-off-by: Orit Wasserman <owasserm@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Amos Kong <akong@redhat.com>
---
 migration-tcp.c |    2 +-
 qemu-char.c     |    2 +-
 qemu-sockets.c  |   58 +++++++++++++++++++++++++++++++++++++++++++++---------
 qemu_socket.h   |    7 ++++-
 vnc.c           |    2 +-
 5 files changed, 56 insertions(+), 15 deletions(-)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 migration-tcp.c |  2 +-
 qemu-char.c     |  2 +-
 qemu-sockets.c  | 58 +++++++++++++++++++++++++++++++++++++++++++++++----------
 qemu_socket.h   |  7 +++++--
 vnc.c           |  2 +-
 5 files changed, 56 insertions(+), 15 deletions(-)

diff --git a/migration-tcp.c b/migration-tcp.c
index cf00ec7..dc218c8 100644
--- a/migration-tcp.c
+++ b/migration-tcp.c
@@ -107,7 +107,7 @@ MigrationState *tcp_start_outgoing_migration(Monitor *mon,
         migrate_fd_monitor_suspend(s, mon);
     }
 
-    s->fd = inet_connect(host_port, false, &in_progress, errp);
+    s->fd = inet_nonblocking_connect(host_port, &in_progress, errp);
     if (error_is_set(errp)) {
         migrate_fd_error(s);
         qemu_free(s);
diff --git a/qemu-char.c b/qemu-char.c
index 5622eb2..8dc1cda 100644
--- a/qemu-char.c
+++ b/qemu-char.c
@@ -2350,7 +2350,7 @@ static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
         if (is_listen) {
             fd = inet_listen_opts(opts, 0, NULL);
         } else {
-            fd = inet_connect_opts(opts, NULL, NULL);
+            fd = inet_connect_opts(opts, true, NULL, NULL);
         }
     }
     if (fd < 0)
diff --git a/qemu-sockets.c b/qemu-sockets.c
index a410a51..4a28e9e 100644
--- a/qemu-sockets.c
+++ b/qemu-sockets.c
@@ -51,9 +51,6 @@ static QemuOptsList dummy_opts = {
         },{
             .name = "ipv6",
             .type = QEMU_OPT_BOOL,
-        },{
-            .name = "block",
-            .type = QEMU_OPT_BOOL,
         },
         { /* end if list */ }
     },
@@ -291,11 +288,22 @@ static struct addrinfo *inet_parse_connect_opts(QemuOpts *opts, Error **errp)
     return res;
 }
 
-int inet_connect_opts(QemuOpts *opts, bool *in_progress, Error **errp)
+/**
+ * Create a socket and connect it to an address.
+ *
+ * @opts: QEMU options, recognized parameters strings "host" and "port",
+ *        bools "ipv4" and "ipv6".
+ * @block: set true for blocking socket
+ * @in_progress: set to true in case of ongoing connect
+ * @errp: set on error
+ *
+ * Returns: -1 on error, file descriptor on success.
+ */
+int inet_connect_opts(QemuOpts *opts, bool block, bool *in_progress,
+                      Error **errp)
 {
     struct addrinfo *res, *e;
     int sock = -1;
-    bool block = qemu_opt_get_bool(opts, "block", 0);
 
     res = inet_parse_connect_opts(opts, errp);
     if (!res) {
@@ -512,17 +520,47 @@ int inet_listen(const char *str, char *ostr, int olen,
     return sock;
 }
 
-int inet_connect(const char *str, bool block, bool *in_progress, Error **errp)
+/**
+ * Create a blocking socket and connect it to an address.
+ *
+ * @str: address string
+ * @errp: set in case of an error
+ *
+ * Returns -1 in case of error, file descriptor on success
+ **/
+int inet_connect(const char *str, Error **errp)
 {
     QemuOpts *opts;
     int sock = -1;
 
     opts = qemu_opts_create(&dummy_opts, NULL, 0);
     if (inet_parse(opts, str) == 0) {
-        if (block) {
-            qemu_opt_set(opts, "block", "on");
-        }
-        sock = inet_connect_opts(opts, in_progress, errp);
+        sock = inet_connect_opts(opts, true, NULL, errp);
+    } else {
+        error_set(errp, QERR_SOCKET_CREATE_FAILED);
+    }
+    qemu_opts_del(opts);
+    return sock;
+}
+
+/**
+ * Create a non-blocking socket and connect it to an address.
+ *
+ * @str: address string
+ * @in_progress: set to true in case of ongoing connect
+ * @errp: set in case of an error
+ *
+ * Returns: -1 on error, file descriptor on success.
+ **/
+int inet_nonblocking_connect(const char *str, bool *in_progress,
+                             Error **errp)
+{
+    QemuOpts *opts;
+    int sock = -1;
+
+    opts = qemu_opts_create(&dummy_opts, NULL, 0);
+    if (inet_parse(opts, str) == 0) {
+        sock = inet_connect_opts(opts, false, in_progress, errp);
     } else {
         error_set(errp, QERR_SOCKET_CREATE_FAILED);
     }
diff --git a/qemu_socket.h b/qemu_socket.h
index d3076cd..898b2a5 100644
--- a/qemu_socket.h
+++ b/qemu_socket.h
@@ -43,8 +43,11 @@ int send_all(CharDriverState *chr, int fd, const void *buf, int len1);
 int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp);
 int inet_listen(const char *str, char *ostr, int olen,
                 int socktype, int port_offset, Error **errp);
-int inet_connect_opts(QemuOpts *opts, bool *in_progress, Error **errp);
-int inet_connect(const char *str, bool block, bool *in_progress, Error **errp);
+int inet_connect_opts(QemuOpts *opts, bool block, bool *in_progress,
+                      Error **errp);
+int inet_connect(const char *str, Error **errp);
+int inet_nonblocking_connect(const char *str, bool *in_progress,
+                             Error **errp);
 int inet_dgram_opts(QemuOpts *opts);
 const char *inet_strfamily(int family);
 
diff --git a/vnc.c b/vnc.c
index a38fa33..0a2ee28 100644
--- a/vnc.c
+++ b/vnc.c
@@ -2844,7 +2844,7 @@ int vnc_display_open(DisplayState *ds, const char *display)
         if (strncmp(display, "unix:", 5) == 0)
             vs->lsock = unix_connect(display+5);
         else
-            vs->lsock = inet_connect(display, true, NULL, NULL);
+            vs->lsock = inet_connect(display, NULL);
         if (-1 == vs->lsock) {
             free(vs->display);
             vs->display = NULL;
-- 
1.7.11.4

