diff mbox

[4/3] qemu-socket: Clean up inet_connect_opts()

Message ID 1398752922-20320-1-git-send-email-armbru@redhat.com
State New
Headers show

Commit Message

Markus Armbruster April 29, 2014, 6:28 a.m. UTC
Separate the search for a working addrinfo from the code that does
something with it.  Makes for a clearer search loop.

Use a local Error * to simplify resetting the error in the search
loop.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
Forgot to include this one.  inet_connect_opts() admittedly isn't in
the character backend subsystem, just used by it.  Hope it's close
enough for review and commit.

 util/qemu-sockets.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

Comments

Markus Armbruster April 29, 2014, 6:31 a.m. UTC | #1
In-Reply-To missing, sorry!

Markus Armbruster <armbru@redhat.com> writes:

> Separate the search for a working addrinfo from the code that does
> something with it.  Makes for a clearer search loop.
>
> Use a local Error * to simplify resetting the error in the search
> loop.
>
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
> Forgot to include this one.  inet_connect_opts() admittedly isn't in
> the character backend subsystem, just used by it.  Hope it's close
> enough for review and commit.
>
>  util/qemu-sockets.c | 28 ++++++++++++++++------------
>  1 file changed, 16 insertions(+), 12 deletions(-)
Paolo Bonzini April 29, 2014, 9:13 a.m. UTC | #2
Il 29/04/2014 08:28, Markus Armbruster ha scritto:
> Separate the search for a working addrinfo from the code that does
> something with it.  Makes for a clearer search loop.
>
> Use a local Error * to simplify resetting the error in the search
> loop.
>
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
> Forgot to include this one.  inet_connect_opts() admittedly isn't in
> the character backend subsystem, just used by it.  Hope it's close
> enough for review and commit.
>
>  util/qemu-sockets.c | 28 ++++++++++++++++------------
>  1 file changed, 16 insertions(+), 12 deletions(-)
>
> diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c
> index 8818d7c..627e609 100644
> --- a/util/qemu-sockets.c
> +++ b/util/qemu-sockets.c
> @@ -354,6 +354,7 @@ static struct addrinfo *inet_parse_connect_opts(QemuOpts *opts, Error **errp)
>  int inet_connect_opts(QemuOpts *opts, Error **errp,
>                        NonBlockingConnectHandler *callback, void *opaque)
>  {
> +    Error *local_err = NULL;
>      struct addrinfo *res, *e;
>      int sock = -1;
>      bool in_progress;
> @@ -372,24 +373,27 @@ int inet_connect_opts(QemuOpts *opts, Error **errp,
>      }
>
>      for (e = res; e != NULL; e = e->ai_next) {
> -        if (error_is_set(errp)) {
> -            error_free(*errp);
> -            *errp = NULL;
> -        }
> +        error_free(local_err);
> +        local_err = NULL;
>          if (connect_state != NULL) {
>              connect_state->current_addr = e;
>          }
> -        sock = inet_connect_addr(e, &in_progress, connect_state, errp);
> -        if (in_progress) {
> -            return sock;
> -        } else if (sock >= 0) {
> -            /* non blocking socket immediate success, call callback */
> -            if (callback != NULL) {
> -                callback(sock, opaque);
> -            }
> +        sock = inet_connect_addr(e, &in_progress, connect_state, &local_err);
> +        if (sock >= 0) {
>              break;
>          }
>      }
> +
> +    if (sock < 0) {
> +        error_propagate(errp, local_err);
> +    } else if (in_progress) {
> +        /* wait_for_connect() will do the rest */
> +        return sock;
> +    } else {
> +        if (callback) {
> +            callback(sock, opaque);
> +        }
> +    }
>      g_free(connect_state);
>      freeaddrinfo(res);
>      return sock;
>

Nicer indeed.

Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
diff mbox

Patch

diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c
index 8818d7c..627e609 100644
--- a/util/qemu-sockets.c
+++ b/util/qemu-sockets.c
@@ -354,6 +354,7 @@  static struct addrinfo *inet_parse_connect_opts(QemuOpts *opts, Error **errp)
 int inet_connect_opts(QemuOpts *opts, Error **errp,
                       NonBlockingConnectHandler *callback, void *opaque)
 {
+    Error *local_err = NULL;
     struct addrinfo *res, *e;
     int sock = -1;
     bool in_progress;
@@ -372,24 +373,27 @@  int inet_connect_opts(QemuOpts *opts, Error **errp,
     }
 
     for (e = res; e != NULL; e = e->ai_next) {
-        if (error_is_set(errp)) {
-            error_free(*errp);
-            *errp = NULL;
-        }
+        error_free(local_err);
+        local_err = NULL;
         if (connect_state != NULL) {
             connect_state->current_addr = e;
         }
-        sock = inet_connect_addr(e, &in_progress, connect_state, errp);
-        if (in_progress) {
-            return sock;
-        } else if (sock >= 0) {
-            /* non blocking socket immediate success, call callback */
-            if (callback != NULL) {
-                callback(sock, opaque);
-            }
+        sock = inet_connect_addr(e, &in_progress, connect_state, &local_err);
+        if (sock >= 0) {
             break;
         }
     }
+
+    if (sock < 0) {
+        error_propagate(errp, local_err);
+    } else if (in_progress) {
+        /* wait_for_connect() will do the rest */
+        return sock;
+    } else {
+        if (callback) {
+            callback(sock, opaque);
+        }
+    }
     g_free(connect_state);
     freeaddrinfo(res);
     return sock;