diff mbox

[1/2] nbd: Close socket on negotiation failure.

Message ID 1399937716-6086-1-git-send-email-kroosec@gmail.com
State New
Headers show

Commit Message

Hani Benhabiles May 12, 2014, 11:35 p.m. UTC
Otherwise, the nbd client may hang waiting for the server response.

Signed-off-by: Hani Benhabiles <hani@linux.com>
---

Quick method to trigger such behaviour:

(qemu) nbd_server_start localhost:10809
(qemu) nbd_server_add sd0
$ nbd-client localhost 10809 -name AAAA /dev/nbd0
Negotiation: ..

(Client will hang indefinitely.)

 blockdev-nbd.c | 4 ++--
 qemu-nbd.c     | 4 +++-
 2 files changed, 5 insertions(+), 3 deletions(-)

Comments

Paolo Bonzini May 13, 2014, 9:07 a.m. UTC | #1
Il 13/05/2014 01:35, Hani Benhabiles ha scritto:
> Otherwise, the nbd client may hang waiting for the server response.
>
> Signed-off-by: Hani Benhabiles <hani@linux.com>
> ---
>
> Quick method to trigger such behaviour:
>
> (qemu) nbd_server_start localhost:10809
> (qemu) nbd_server_add sd0
> $ nbd-client localhost 10809 -name AAAA /dev/nbd0
> Negotiation: ..
>
> (Client will hang indefinitely.)
>
>  blockdev-nbd.c | 4 ++--
>  qemu-nbd.c     | 4 +++-
>  2 files changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/blockdev-nbd.c b/blockdev-nbd.c
> index 922cf56..b60b66d 100644
> --- a/blockdev-nbd.c
> +++ b/blockdev-nbd.c
> @@ -27,8 +27,8 @@ static void nbd_accept(void *opaque)
>      socklen_t addr_len = sizeof(addr);
>
>      int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
> -    if (fd >= 0) {
> -        nbd_client_new(NULL, fd, nbd_client_put);
> +    if (fd >= 0 && !nbd_client_new(NULL, fd, nbd_client_put)) {
> +        close(fd);
>      }
>  }
>
> diff --git a/qemu-nbd.c b/qemu-nbd.c
> index eed79fa..f70e4b0 100644
> --- a/qemu-nbd.c
> +++ b/qemu-nbd.c
> @@ -369,8 +369,10 @@ static void nbd_accept(void *opaque)
>          return;
>      }
>
> -    if (fd >= 0 && nbd_client_new(exp, fd, nbd_client_closed)) {
> +    if (nbd_client_new(exp, fd, nbd_client_closed)) {
>          nb_fds++;
> +    } else {
> +        close(fd);
>      }
>  }
>
>

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

Michael, can you queue this yourself?

Paolo
Michael Tokarev May 17, 2014, 8:02 a.m. UTC | #2
13.05.2014 03:35, Hani Benhabiles wrote:
> Otherwise, the nbd client may hang waiting for the server response.

Applied to -trivial, thanks!

/mjt
diff mbox

Patch

diff --git a/blockdev-nbd.c b/blockdev-nbd.c
index 922cf56..b60b66d 100644
--- a/blockdev-nbd.c
+++ b/blockdev-nbd.c
@@ -27,8 +27,8 @@  static void nbd_accept(void *opaque)
     socklen_t addr_len = sizeof(addr);
 
     int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
-    if (fd >= 0) {
-        nbd_client_new(NULL, fd, nbd_client_put);
+    if (fd >= 0 && !nbd_client_new(NULL, fd, nbd_client_put)) {
+        close(fd);
     }
 }
 
diff --git a/qemu-nbd.c b/qemu-nbd.c
index eed79fa..f70e4b0 100644
--- a/qemu-nbd.c
+++ b/qemu-nbd.c
@@ -369,8 +369,10 @@  static void nbd_accept(void *opaque)
         return;
     }
 
-    if (fd >= 0 && nbd_client_new(exp, fd, nbd_client_closed)) {
+    if (nbd_client_new(exp, fd, nbd_client_closed)) {
         nb_fds++;
+    } else {
+        close(fd);
     }
 }