diff mbox

[11/25] nbd: ask and print error information from qemu-sockets

Message ID 1349877786-23514-12-git-send-email-pbonzini@redhat.com
State New
Headers show

Commit Message

Paolo Bonzini Oct. 10, 2012, 2:02 p.m. UTC
Before:

    $ qemu-system-x86_64 nbd:localhost:12345
    inet_connect_opts: connect(ipv4,yakj.usersys.redhat.com,127.0.0.1,12345): Connection refused
    qemu-system-x86_64: could not open disk image nbd:localhost:12345: Connection refused

After:

    $ x86_64-softmmu/qemu-system-x86_64 nbd:localhost:12345
    qemu-system-x86_64: Failed to connect to socket: Connection refused
    qemu-system-x86_64: could not open disk image nbd:localhost:12345: Connection refused

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 nbd.c | 39 +++++++++++++++++++++++++++++++--------
 1 file modificato, 31 inserzioni(+), 8 rimozioni(-)

Comments

Markus Armbruster Oct. 17, 2012, 2:51 p.m. UTC | #1
Paolo Bonzini <pbonzini@redhat.com> writes:

> Before:
>
>     $ qemu-system-x86_64 nbd:localhost:12345
>     inet_connect_opts: connect(ipv4,yakj.usersys.redhat.com,127.0.0.1,12345): Connection refused
>     qemu-system-x86_64: could not open disk image nbd:localhost:12345: Connection refused
>
> After:
>
>     $ x86_64-softmmu/qemu-system-x86_64 nbd:localhost:12345
>     qemu-system-x86_64: Failed to connect to socket: Connection refused
>     qemu-system-x86_64: could not open disk image nbd:localhost:12345: Connection refused
>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  nbd.c | 39 +++++++++++++++++++++++++++++++--------
>  1 file modificato, 31 inserzioni(+), 8 rimozioni(-)
>
> diff --git a/nbd.c b/nbd.c
> index f61a288..cec5a94 100644
> --- a/nbd.c
> +++ b/nbd.c
> @@ -208,7 +208,14 @@ int tcp_socket_outgoing(const char *address, uint16_t port)
>  
>  int tcp_socket_outgoing_spec(const char *address_and_port)
>  {
> -    return inet_connect(address_and_port, NULL);
> +    Error *local_err = NULL;
> +    int fd = inet_connect(address_and_port, &local_err);
> +
> +    if (local_err != NULL) {
> +        qerror_report_err(local_err);
> +        error_free(local_err);
> +    }
> +    return fd;
>  }
>  
>  int tcp_socket_incoming(const char *address, uint16_t port)
> @@ -220,22 +227,38 @@ int tcp_socket_incoming(const char *address, uint16_t port)
>  
>  int tcp_socket_incoming_spec(const char *address_and_port)
>  {
> -    char *ostr  = NULL;
> -    int olen = 0;
> -    return inet_listen(address_and_port, ostr, olen, SOCK_STREAM, 0, NULL);
> +    Error *local_err = NULL;
> +    int fd = inet_listen(address_and_port, NULL, 0, SOCK_STREAM, 0, &local_err);
> +
> +    if (local_err != NULL) {
> +        qerror_report_err(local_err);
> +        error_free(local_err);
> +    }
> +    return fd;
>  }
>  
>  int unix_socket_incoming(const char *path)
>  {
> -    char *ostr = NULL;
> -    int olen = 0;
> +    Error *local_err = NULL;
> +    int fd = unix_listen(path, NULL, 0, &local_err);
>  
> -    return unix_listen(path, ostr, olen, NULL);
> +    if (local_err != NULL) {
> +        qerror_report_err(local_err);
> +        error_free(local_err);
> +    }
> +    return fd;
>  }
>  
>  int unix_socket_outgoing(const char *path)
>  {
> -    return unix_connect(path, NULL);
> +    Error *local_err = NULL;
> +    int fd = unix_connect(path, &local_err);
> +
> +    if (local_err != NULL) {
> +        qerror_report_err(local_err);
> +        error_free(local_err);
> +    }
> +    return fd;
>  }
>  
>  /* Basic flow for negotiation

The boilerplate pattern

    Error *local_err = NULL;
    FOO(..., *local_err);
    if (local_err) {
        qerror_report_err(local_err);
        error_free(local_err);
    }

is spreading.  Not quite sure it's worth a macro.
Paolo Bonzini Oct. 17, 2012, 2:53 p.m. UTC | #2
Il 17/10/2012 16:51, Markus Armbruster ha scritto:
>> >  /* Basic flow for negotiation
> The boilerplate pattern
> 
>     Error *local_err = NULL;
>     FOO(..., *local_err);
>     if (local_err) {
>         qerror_report_err(local_err);
>         error_free(local_err);
>     }
> 
> is spreading.  Not quite sure it's worth a macro.

Actually this should not spread, but this one should:

    Error *local_err = NULL;
    FOO(..., *local_err);
    if (local_err) {
        error_propagate(errp, local_err);
        return;
    }

Not quite sure how to macroize it though, at least without making the
code too ugly to see.

Paolo
diff mbox

Patch

diff --git a/nbd.c b/nbd.c
index f61a288..cec5a94 100644
--- a/nbd.c
+++ b/nbd.c
@@ -208,7 +208,14 @@  int tcp_socket_outgoing(const char *address, uint16_t port)
 
 int tcp_socket_outgoing_spec(const char *address_and_port)
 {
-    return inet_connect(address_and_port, NULL);
+    Error *local_err = NULL;
+    int fd = inet_connect(address_and_port, &local_err);
+
+    if (local_err != NULL) {
+        qerror_report_err(local_err);
+        error_free(local_err);
+    }
+    return fd;
 }
 
 int tcp_socket_incoming(const char *address, uint16_t port)
@@ -220,22 +227,38 @@  int tcp_socket_incoming(const char *address, uint16_t port)
 
 int tcp_socket_incoming_spec(const char *address_and_port)
 {
-    char *ostr  = NULL;
-    int olen = 0;
-    return inet_listen(address_and_port, ostr, olen, SOCK_STREAM, 0, NULL);
+    Error *local_err = NULL;
+    int fd = inet_listen(address_and_port, NULL, 0, SOCK_STREAM, 0, &local_err);
+
+    if (local_err != NULL) {
+        qerror_report_err(local_err);
+        error_free(local_err);
+    }
+    return fd;
 }
 
 int unix_socket_incoming(const char *path)
 {
-    char *ostr = NULL;
-    int olen = 0;
+    Error *local_err = NULL;
+    int fd = unix_listen(path, NULL, 0, &local_err);
 
-    return unix_listen(path, ostr, olen, NULL);
+    if (local_err != NULL) {
+        qerror_report_err(local_err);
+        error_free(local_err);
+    }
+    return fd;
 }
 
 int unix_socket_outgoing(const char *path)
 {
-    return unix_connect(path, NULL);
+    Error *local_err = NULL;
+    int fd = unix_connect(path, &local_err);
+
+    if (local_err != NULL) {
+        qerror_report_err(local_err);
+        error_free(local_err);
+    }
+    return fd;
 }
 
 /* Basic flow for negotiation