Skip to contentSkip to conversation

nbd: Close socket on negotiation failure.

PATCH Applied Hani Benhabiles 4 participants 7 messages May 12, 2014 – May 17, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 nbd: Close socket on negotiation failure.

Otherwise, the nbd client may hang waiting for the server response.

---

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(-)
blockdev-nbd.c +2 -2
index 922cf56..b60b66d 100644
@@ -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);
     }
 }
 
qemu-nbd.c +3 -1
index eed79fa..f70e4b0 100644
@@ -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);
     }
 }
 
Signature · 1 line
1.8.3.2
#1 lore raw

PATCH 2/2 nbd: Miscellaneous typo fixes.

---
 nbd.c         | 2 +-
 qemu-nbd.c    | 2 +-
 qemu-nbd.texi | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
nbd.c +1 -1
index e5084b6..e0d032c 100644
@@ -306,7 +306,7 @@ static int nbd_send_negotiate(NBDClient *client)
         [ 8 ..  15]   magic        (NBD_CLIENT_MAGIC)
         [16 ..  23]   size
         [24 ..  25]   server flags (0)
-        [24 ..  27]   export flags
+        [26 ..  27]   export flags
         [28 .. 151]   reserved     (0)
 
        Negotiation header with options, part 1:
qemu-nbd.c +1 -1
index f70e4b0..cd6bd50 100644
@@ -294,7 +294,7 @@ static void *nbd_client_thread(void *arg)
     fd = open(device, O_RDWR);
     if (fd < 0) {
         /* Linux-only, we can use %m in printf.  */
-        fprintf(stderr, "Failed to open %s: %m", device);
+        fprintf(stderr, "Failed to open %s: %m\n", device);
         goto out_socket;
     }
 
qemu-nbd.texi +1 -1
index 0a7e013..46fd483 100644
@@ -15,7 +15,7 @@ Export QEMU disk image using NBD protocol.
 @item @var{filename}
  is a disk image filename
 @item -p, --port=@var{port}
-  port to listen on (default @samp{1024})
+  port to listen on (default @samp{10809})
 @item -o, --offset=@var{offset}
   offset into the image
 @item -b, --bind=@var{iface}
Signature · 1 line
1.8.3.2
Acked-by on v1 1/2
#5 lore raw
In reply to Hani Benhabiles

Il 13/05/2014 01:35, Hani Benhabiles ha scritto:

Quoted 40 lines

Otherwise, the nbd client may hang waiting for the server response.

---

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(-)
blockdev-nbd.c +2 -2
index 922cf56..b60b66d 100644
@@ -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);
     }
 }
 
qemu-nbd.c +0 -0
index eed79fa..f70e4b0 100644
@@ -369,8 +369,10 @@ static void nbd_accept(void *opaque)
         return;

qemu-nbd.c static void nbd_accept(void *opaque) · line 378

Quoted 10 lines
qemu-nbd.c +3 -1
     }
 
-    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);
     }
 }
 

Michael, can you queue this yourself?

Paolo

Acked-by on v1 2/2
#2 lore raw

Re: [Qemu-devel] [PATCH 2/2] nbd: Miscellaneous typo fixes.

In reply to Hani Benhabiles

Il 13/05/2014 01:35, Hani Benhabiles ha scritto:

Quoted 39 lines
---
 nbd.c         | 2 +-
 qemu-nbd.c    | 2 +-
 qemu-nbd.texi | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
nbd.c +1 -1
index e5084b6..e0d032c 100644
@@ -306,7 +306,7 @@ static int nbd_send_negotiate(NBDClient *client)
         [ 8 ..  15]   magic        (NBD_CLIENT_MAGIC)
         [16 ..  23]   size
         [24 ..  25]   server flags (0)
-        [24 ..  27]   export flags
+        [26 ..  27]   export flags
         [28 .. 151]   reserved     (0)
 
        Negotiation header with options, part 1:
qemu-nbd.c +1 -1
index f70e4b0..cd6bd50 100644
@@ -294,7 +294,7 @@ static void *nbd_client_thread(void *arg)
     fd = open(device, O_RDWR);
     if (fd < 0) {
         /* Linux-only, we can use %m in printf.  */
-        fprintf(stderr, "Failed to open %s: %m", device);
+        fprintf(stderr, "Failed to open %s: %m\n", device);
         goto out_socket;
     }
 
qemu-nbd.texi +0 -0
index 0a7e013..46fd483 100644

qemu-nbd.texi Export QEMU disk image using NBD protocol. · line 21

Quoted 8 lines
qemu-nbd.texi +1 -1
@@ -15,7 +15,7 @@ Export QEMU disk image using NBD protocol.
 @item @var{filename}
  is a disk image filename
 @item -p, --port=@var{port}
-  port to listen on (default @samp{1024})
+  port to listen on (default @samp{10809})
 @item -o, --offset=@var{offset}
   offset into the image
 @item -b, --bind=@var{iface}