Patchwork [16/18] qemu-sockets: add error propagation to inet_dgram_opts

login
register
mail settings
Submitter Paolo Bonzini
Date Oct. 3, 2012, 2:37 p.m.
Message ID <1349275025-5093-17-git-send-email-pbonzini@redhat.com>
Download mbox | patch
Permalink /patch/188816/
State New
Headers show

Comments

Paolo Bonzini - Oct. 3, 2012, 2:37 p.m.
Before:

    $ qemu-system-x86_64 -monitor udp:localhost:631@localhost:631
    inet_dgram_opts: bind(ipv4,127.0.0.1,631): OK
    inet_dgram_opts failed
    chardev: opening backend "udp" failed

After:

    $ x86_64-softmmu/qemu-system-x86_64 -monitor udp:localhost:631@localhost:631
    qemu-system-x86_64: -monitor udp:localhost:631@localhost:631: Failed to bind socket: Address already in use
    chardev: opening backend "udp" failed

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 qemu-char.c    |  1 -
 qemu-sockets.c | 34 ++++++++--------------------------
 2 file modificati, 8 inserzioni(+), 27 rimozioni(-)
Luiz Capitulino - Oct. 9, 2012, 5:30 p.m.
On Wed,  3 Oct 2012 16:37:03 +0200
Paolo Bonzini <pbonzini@redhat.com> wrote:

> Before:
> 
>     $ qemu-system-x86_64 -monitor udp:localhost:631@localhost:631
>     inet_dgram_opts: bind(ipv4,127.0.0.1,631): OK
>     inet_dgram_opts failed
>     chardev: opening backend "udp" failed
> 
> After:
> 
>     $ x86_64-softmmu/qemu-system-x86_64 -monitor udp:localhost:631@localhost:631
>     qemu-system-x86_64: -monitor udp:localhost:631@localhost:631: Failed to bind socket: Address already in use
>     chardev: opening backend "udp" failed
> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

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

> ---
>  qemu-char.c    |  1 -
>  qemu-sockets.c | 34 ++++++++--------------------------
>  2 file modificati, 8 inserzioni(+), 27 rimozioni(-)
> 
> diff --git a/qemu-char.c b/qemu-char.c
> index 04b5c23..afe2bfb 100644
> --- a/qemu-char.c
> +++ b/qemu-char.c
> @@ -2105,7 +2105,6 @@ static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
>  
>      fd = inet_dgram_opts(opts, &local_err);
>      if (fd < 0) {
> -        fprintf(stderr, "inet_dgram_opts failed\n");
>          goto return_err;
>      }
>  
> diff --git a/qemu-sockets.c b/qemu-sockets.c
> index b096f49..6f13121 100644
> --- a/qemu-sockets.c
> +++ b/qemu-sockets.c
> @@ -397,8 +397,6 @@ int inet_dgram_opts(QemuOpts *opts, Error **errp)
>      struct addrinfo ai, *peer = NULL, *local = NULL;
>      const char *addr;
>      const char *port;
> -    char uaddr[INET6_ADDRSTRLEN+1];
> -    char uport[33];
>      int sock = -1, rc;
>  
>      /* lookup peer addr */
> @@ -413,7 +411,7 @@ int inet_dgram_opts(QemuOpts *opts, Error **errp)
>          addr = "localhost";
>      }
>      if (port == NULL || strlen(port) == 0) {
> -        fprintf(stderr, "inet_dgram: port not specified\n");
> +        error_setg(errp, "remote port not specified");
>          return -1;
>      }
>  
> @@ -423,8 +421,8 @@ int inet_dgram_opts(QemuOpts *opts, Error **errp)
>          ai.ai_family = PF_INET6;
>  
>      if (0 != (rc = getaddrinfo(addr, port, &ai, &peer))) {
> -        fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
> -                gai_strerror(rc));
> +        error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
> +                   gai_strerror(rc));
>  	return -1;
>      }
>  
> @@ -443,44 +441,28 @@ int inet_dgram_opts(QemuOpts *opts, Error **errp)
>          port = "0";
>  
>      if (0 != (rc = getaddrinfo(addr, port, &ai, &local))) {
> -        fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
> -                gai_strerror(rc));
> +        error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
> +                   gai_strerror(rc));
>          goto err;
>      }
>  
>      /* create socket */
>      sock = qemu_socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol);
>      if (sock < 0) {
> -        fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
> -                inet_strfamily(peer->ai_family), strerror(errno));
> +        error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED);
>          goto err;
>      }
>      setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
>  
>      /* bind socket */
> -    if (getnameinfo((struct sockaddr*)local->ai_addr,local->ai_addrlen,
> -                    uaddr,INET6_ADDRSTRLEN,uport,32,
> -                    NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
> -        fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
> -        goto err;
> -    }
>      if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) {
> -        fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,
> -                inet_strfamily(local->ai_family), uaddr, inet_getport(local));
> +        error_set_errno(errp, errno, QERR_SOCKET_BIND_FAILED);
>          goto err;
>      }
>  
>      /* connect to peer */
> -    if (getnameinfo((struct sockaddr*)peer->ai_addr, peer->ai_addrlen,
> -                    uaddr, INET6_ADDRSTRLEN, uport, 32,
> -                    NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
> -        fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
> -        goto err;
> -    }
>      if (connect(sock,peer->ai_addr,peer->ai_addrlen) < 0) {
> -        fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
> -                inet_strfamily(peer->ai_family),
> -                peer->ai_canonname, uaddr, uport, strerror(errno));
> +        error_set_errno(errp, errno, QERR_SOCKET_CONNECT_FAILED);
>          goto err;
>      }
>

Patch

diff --git a/qemu-char.c b/qemu-char.c
index 04b5c23..afe2bfb 100644
--- a/qemu-char.c
+++ b/qemu-char.c
@@ -2105,7 +2105,6 @@  static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
 
     fd = inet_dgram_opts(opts, &local_err);
     if (fd < 0) {
-        fprintf(stderr, "inet_dgram_opts failed\n");
         goto return_err;
     }
 
diff --git a/qemu-sockets.c b/qemu-sockets.c
index b096f49..6f13121 100644
--- a/qemu-sockets.c
+++ b/qemu-sockets.c
@@ -397,8 +397,6 @@  int inet_dgram_opts(QemuOpts *opts, Error **errp)
     struct addrinfo ai, *peer = NULL, *local = NULL;
     const char *addr;
     const char *port;
-    char uaddr[INET6_ADDRSTRLEN+1];
-    char uport[33];
     int sock = -1, rc;
 
     /* lookup peer addr */
@@ -413,7 +411,7 @@  int inet_dgram_opts(QemuOpts *opts, Error **errp)
         addr = "localhost";
     }
     if (port == NULL || strlen(port) == 0) {
-        fprintf(stderr, "inet_dgram: port not specified\n");
+        error_setg(errp, "remote port not specified");
         return -1;
     }
 
@@ -423,8 +421,8 @@  int inet_dgram_opts(QemuOpts *opts, Error **errp)
         ai.ai_family = PF_INET6;
 
     if (0 != (rc = getaddrinfo(addr, port, &ai, &peer))) {
-        fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
-                gai_strerror(rc));
+        error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
+                   gai_strerror(rc));
 	return -1;
     }
 
@@ -443,44 +441,28 @@  int inet_dgram_opts(QemuOpts *opts, Error **errp)
         port = "0";
 
     if (0 != (rc = getaddrinfo(addr, port, &ai, &local))) {
-        fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
-                gai_strerror(rc));
+        error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
+                   gai_strerror(rc));
         goto err;
     }
 
     /* create socket */
     sock = qemu_socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol);
     if (sock < 0) {
-        fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
-                inet_strfamily(peer->ai_family), strerror(errno));
+        error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED);
         goto err;
     }
     setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
 
     /* bind socket */
-    if (getnameinfo((struct sockaddr*)local->ai_addr,local->ai_addrlen,
-                    uaddr,INET6_ADDRSTRLEN,uport,32,
-                    NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
-        fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
-        goto err;
-    }
     if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) {
-        fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,
-                inet_strfamily(local->ai_family), uaddr, inet_getport(local));
+        error_set_errno(errp, errno, QERR_SOCKET_BIND_FAILED);
         goto err;
     }
 
     /* connect to peer */
-    if (getnameinfo((struct sockaddr*)peer->ai_addr, peer->ai_addrlen,
-                    uaddr, INET6_ADDRSTRLEN, uport, 32,
-                    NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
-        fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
-        goto err;
-    }
     if (connect(sock,peer->ai_addr,peer->ai_addrlen) < 0) {
-        fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
-                inet_strfamily(peer->ai_family),
-                peer->ai_canonname, uaddr, uport, strerror(errno));
+        error_set_errno(errp, errno, QERR_SOCKET_CONNECT_FAILED);
         goto err;
     }