Patchwork [v2,3/9] qemu-sockets: add error propagation to inet_parse

login
register
mail settings
Submitter Paolo Bonzini
Date Oct. 1, 2012, 2:52 p.m.
Message ID <1349103144-6827-4-git-send-email-pbonzini@redhat.com>
Download mbox | patch
Permalink /patch/188307/
State New
Headers show

Comments

Paolo Bonzini - Oct. 1, 2012, 2:52 p.m.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 qemu-sockets.c | 41 +++++++++++++++++++++--------------------
 1 file modificato, 21 inserzioni(+), 20 rimozioni(-)
Luiz Capitulino - Oct. 2, 2012, 12:34 p.m.
On Mon,  1 Oct 2012 16:52:18 +0200
Paolo Bonzini <pbonzini@redhat.com> wrote:

> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

Reviewed-by: Luiz Capitulino <lcapitulino@redhat.com>

> ---
>  qemu-sockets.c | 41 +++++++++++++++++++++--------------------
>  1 file modificato, 21 inserzioni(+), 20 rimozioni(-)
> 
> diff --git a/qemu-sockets.c b/qemu-sockets.c
> index 1f14e8b..bf1f794 100644
> --- a/qemu-sockets.c
> +++ b/qemu-sockets.c
> @@ -511,7 +511,7 @@ err:
>  }
>  
>  /* compatibility wrapper */
> -static int inet_parse(QemuOpts *opts, const char *str)
> +static void inet_parse(QemuOpts *opts, const char *str, Error **errp)
>  {
>      const char *optstr, *h;
>      char addr[64];
> @@ -523,32 +523,28 @@ static int inet_parse(QemuOpts *opts, const char *str)
>          /* no host given */
>          addr[0] = '\0';
>          if (1 != sscanf(str,":%32[^,]%n",port,&pos)) {
> -            fprintf(stderr, "%s: portonly parse error (%s)\n",
> -                    __FUNCTION__, str);
> -            return -1;
> +            error_setg(errp, "error parsing port in address '%s'", str);
> +            return;
>          }
>      } else if (str[0] == '[') {
>          /* IPv6 addr */
>          if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) {
> -            fprintf(stderr, "%s: ipv6 parse error (%s)\n",
> -                    __FUNCTION__, str);
> -            return -1;
> +            error_setg(errp, "error parsing IPv6 address '%s'", str);
> +            return;
>          }
>          qemu_opt_set(opts, "ipv6", "on");
>      } else if (qemu_isdigit(str[0])) {
>          /* IPv4 addr */
>          if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) {
> -            fprintf(stderr, "%s: ipv4 parse error (%s)\n",
> -                    __FUNCTION__, str);
> -            return -1;
> +            error_setg(errp, "error parsing IPv4 address '%s'", str);
> +            return;
>          }
>          qemu_opt_set(opts, "ipv4", "on");
>      } else {
>          /* hostname */
>          if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) {
> -            fprintf(stderr, "%s: hostname parse error (%s)\n",
> -                    __FUNCTION__, str);
> -            return -1;
> +            error_setg(errp, "error parsing address '%s'", str);
> +            return;
>          }
>      }
>      qemu_opt_set(opts, "host", addr);
> @@ -563,7 +559,6 @@ static int inet_parse(QemuOpts *opts, const char *str)
>          qemu_opt_set(opts, "ipv4", "on");
>      if (strstr(optstr, ",ipv6"))
>          qemu_opt_set(opts, "ipv6", "on");
> -    return 0;
>  }
>  
>  int inet_listen(const char *str, char *ostr, int olen,
> @@ -572,9 +567,11 @@ int inet_listen(const char *str, char *ostr, int olen,
>      QemuOpts *opts;
>      char *optstr;
>      int sock = -1;
> +    Error *local_err = NULL;
>  
>      opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
> -    if (inet_parse(opts, str) == 0) {
> +    inet_parse(opts, str, &local_err);
> +    if (local_err == NULL) {
>          sock = inet_listen_opts(opts, port_offset, errp);
>          if (sock != -1 && ostr) {
>              optstr = strchr(str, ',');
> @@ -591,7 +588,7 @@ int inet_listen(const char *str, char *ostr, int olen,
>              }
>          }
>      } else {
> -        error_set(errp, QERR_SOCKET_CREATE_FAILED);
> +        error_propagate(errp, local_err);
>      }
>      qemu_opts_del(opts);
>      return sock;
> @@ -609,12 +606,14 @@ int inet_connect(const char *str, Error **errp)
>  {
>      QemuOpts *opts;
>      int sock = -1;
> +    Error *local_err = NULL;
>  
>      opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
> -    if (inet_parse(opts, str) == 0) {
> +    inet_parse(opts, str, &local_err);
> +    if (local_err == NULL) {
>          sock = inet_connect_opts(opts, errp, NULL, NULL);
>      } else {
> -        error_set(errp, QERR_SOCKET_CREATE_FAILED);
> +        error_propagate(errp, local_err);
>      }
>      qemu_opts_del(opts);
>      return sock;
> @@ -639,14 +638,16 @@ int inet_nonblocking_connect(const char *str,
>  {
>      QemuOpts *opts;
>      int sock = -1;
> +    Error *local_err = NULL;
>  
>      g_assert(callback != NULL);
>  
>      opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
> -    if (inet_parse(opts, str) == 0) {
> +    inet_parse(opts, str, &local_err);
> +    if (local_err == NULL) {
>          sock = inet_connect_opts(opts, errp, callback, opaque);
>      } else {
> -        error_set(errp, QERR_SOCKET_CREATE_FAILED);
> +        error_propagate(errp, local_err);
>      }
>      qemu_opts_del(opts);
>      return sock;

Patch

diff --git a/qemu-sockets.c b/qemu-sockets.c
index 1f14e8b..bf1f794 100644
--- a/qemu-sockets.c
+++ b/qemu-sockets.c
@@ -511,7 +511,7 @@  err:
 }
 
 /* compatibility wrapper */
-static int inet_parse(QemuOpts *opts, const char *str)
+static void inet_parse(QemuOpts *opts, const char *str, Error **errp)
 {
     const char *optstr, *h;
     char addr[64];
@@ -523,32 +523,28 @@  static int inet_parse(QemuOpts *opts, const char *str)
         /* no host given */
         addr[0] = '\0';
         if (1 != sscanf(str,":%32[^,]%n",port,&pos)) {
-            fprintf(stderr, "%s: portonly parse error (%s)\n",
-                    __FUNCTION__, str);
-            return -1;
+            error_setg(errp, "error parsing port in address '%s'", str);
+            return;
         }
     } else if (str[0] == '[') {
         /* IPv6 addr */
         if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) {
-            fprintf(stderr, "%s: ipv6 parse error (%s)\n",
-                    __FUNCTION__, str);
-            return -1;
+            error_setg(errp, "error parsing IPv6 address '%s'", str);
+            return;
         }
         qemu_opt_set(opts, "ipv6", "on");
     } else if (qemu_isdigit(str[0])) {
         /* IPv4 addr */
         if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) {
-            fprintf(stderr, "%s: ipv4 parse error (%s)\n",
-                    __FUNCTION__, str);
-            return -1;
+            error_setg(errp, "error parsing IPv4 address '%s'", str);
+            return;
         }
         qemu_opt_set(opts, "ipv4", "on");
     } else {
         /* hostname */
         if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) {
-            fprintf(stderr, "%s: hostname parse error (%s)\n",
-                    __FUNCTION__, str);
-            return -1;
+            error_setg(errp, "error parsing address '%s'", str);
+            return;
         }
     }
     qemu_opt_set(opts, "host", addr);
@@ -563,7 +559,6 @@  static int inet_parse(QemuOpts *opts, const char *str)
         qemu_opt_set(opts, "ipv4", "on");
     if (strstr(optstr, ",ipv6"))
         qemu_opt_set(opts, "ipv6", "on");
-    return 0;
 }
 
 int inet_listen(const char *str, char *ostr, int olen,
@@ -572,9 +567,11 @@  int inet_listen(const char *str, char *ostr, int olen,
     QemuOpts *opts;
     char *optstr;
     int sock = -1;
+    Error *local_err = NULL;
 
     opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
-    if (inet_parse(opts, str) == 0) {
+    inet_parse(opts, str, &local_err);
+    if (local_err == NULL) {
         sock = inet_listen_opts(opts, port_offset, errp);
         if (sock != -1 && ostr) {
             optstr = strchr(str, ',');
@@ -591,7 +588,7 @@  int inet_listen(const char *str, char *ostr, int olen,
             }
         }
     } else {
-        error_set(errp, QERR_SOCKET_CREATE_FAILED);
+        error_propagate(errp, local_err);
     }
     qemu_opts_del(opts);
     return sock;
@@ -609,12 +606,14 @@  int inet_connect(const char *str, Error **errp)
 {
     QemuOpts *opts;
     int sock = -1;
+    Error *local_err = NULL;
 
     opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
-    if (inet_parse(opts, str) == 0) {
+    inet_parse(opts, str, &local_err);
+    if (local_err == NULL) {
         sock = inet_connect_opts(opts, errp, NULL, NULL);
     } else {
-        error_set(errp, QERR_SOCKET_CREATE_FAILED);
+        error_propagate(errp, local_err);
     }
     qemu_opts_del(opts);
     return sock;
@@ -639,14 +638,16 @@  int inet_nonblocking_connect(const char *str,
 {
     QemuOpts *opts;
     int sock = -1;
+    Error *local_err = NULL;
 
     g_assert(callback != NULL);
 
     opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
-    if (inet_parse(opts, str) == 0) {
+    inet_parse(opts, str, &local_err);
+    if (local_err == NULL) {
         sock = inet_connect_opts(opts, errp, callback, opaque);
     } else {
-        error_set(errp, QERR_SOCKET_CREATE_FAILED);
+        error_propagate(errp, local_err);
     }
     qemu_opts_del(opts);
     return sock;