Skip to contentSkip to conversation

use different fd variables for device and socket, unbreak qemu-nbd -c

PATCH In review Michael Tokarev 2 participants 3 messages Jan 14, 2012 – Jan 15, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 use different fd variables for device and socket, unbreak qemu-nbd -c

commit a61c67828dea7c64edaf226cadb45b4ffcc1d411

Author: Paolo Bonzini <[email protected]>
Date:   Mon Sep 12 17:28:11 2011 +0200

qemu-nbd: use common main loop

Using a single main loop for sockets will help yielding from the socket coroutine back to the main loop, and later reentering it.

changed code to use local variable "fd" in qemu-nbd.c:main() in two places: for /dev/nbd device and for control socket. The result is that qemu-nbd -c $device does not work anymore.

Use two variables - devfs and sockfd - for the two purposes, instead of one fd.

---
 qemu-nbd.c |   26 +++++++++++++-------------
 1 files changed, 13 insertions(+), 13 deletions(-)
qemu-nbd.c +13 -13
index eb61c33..e76c782 100644
@@ -301,7 +301,7 @@ int main(int argc, char **argv)
     int flags = BDRV_O_RDWR;
     int partition = -1;
     int ret;
-    int fd;
+    int sockfd, devfd;
     int persistent = 0;
     pthread_t client_thread;
 
@@ -401,13 +401,13 @@ int main(int argc, char **argv)
     }
 
     if (disconnect) {
-        fd = open(argv[optind], O_RDWR);
-        if (fd == -1)
+        sockfd = open(argv[optind], O_RDWR);
+        if (sockfd == -1)
             err(EXIT_FAILURE, "Cannot open %s", argv[optind]);
 
-        nbd_disconnect(fd);
+        nbd_disconnect(sockfd);
 
-        close(fd);
+        close(sockfd);
 
         printf("%s disconnected\n", argv[optind]);
 
@@ -470,8 +470,8 @@ int main(int argc, char **argv)
         /* Open before spawning new threads.  In the future, we may
          * drop privileges after opening.
          */
-        fd = open(device, O_RDWR);
-        if (fd == -1) {
+        devfd = open(device, O_RDWR);
+        if (devfd == -1) {
             err(EXIT_FAILURE, "Failed to open %s", device);
         }
 
@@ -501,19 +501,19 @@ int main(int argc, char **argv)
     exp = nbd_export_new(bs, dev_offset, fd_size, nbdflags);
 
     if (sockpath) {
-        fd = unix_socket_incoming(sockpath);
+        sockfd = unix_socket_incoming(sockpath);
     } else {
-        fd = tcp_socket_incoming(bindto, port);
+        sockfd = tcp_socket_incoming(bindto, port);
     }
 
-    if (fd == -1) {
+    if (sockfd == -1) {
         return 1;
     }
 
     if (device) {
         int ret;
 
-        ret = pthread_create(&client_thread, NULL, nbd_client_thread, &fd);
+        ret = pthread_create(&client_thread, NULL, nbd_client_thread, &devfd);
         if (ret != 0) {
             errx(EXIT_FAILURE, "Failed to create client thread: %s",
                  strerror(ret));
@@ -524,8 +524,8 @@ int main(int argc, char **argv)
     }
 
     qemu_init_main_loop();
-    qemu_set_fd_handler2(fd, nbd_can_accept, nbd_accept, NULL,
-                         (void *)(uintptr_t)fd);
+    qemu_set_fd_handler2(sockfd, nbd_can_accept, nbd_accept, NULL,
+                         (void *)(uintptr_t)sockfd);
 
     do {
         main_loop_wait(false);
Signature · 1 line
1.7.2.5
#1 lore raw
Quoted 42 lines · Michael Tokarev

commit a61c67828dea7c64edaf226cadb45b4ffcc1d411

Author: Paolo Bonzini<[email protected]>
Date:   Mon Sep 12 17:28:11 2011 +0200

qemu-nbd: use common main loop

Using a single main loop for sockets will help yielding from the socket coroutine back to the main loop, and later reentering it.

changed code to use local variable "fd" in qemu-nbd.c:main() in two places: for /dev/nbd device and for control socket. The result is that qemu-nbd -c $device does not work anymore.

Use two variables - devfs and sockfd - for the two purposes, instead of one fd.

---
  qemu-nbd.c |   26 +++++++++++++-------------
  1 files changed, 13 insertions(+), 13 deletions(-)
qemu-nbd.c +3 -3
index eb61c33..e76c782 100644
@@ -301,7 +301,7 @@ int main(int argc, char **argv)
      int flags = BDRV_O_RDWR;
      int partition = -1;
      int ret;
-    int fd;
+    int sockfd, devfd;
      int persistent = 0;
      pthread_t client_thread;
 
@@ -401,13 +401,13 @@ int main(int argc, char **argv)
      }
 
      if (disconnect) {
-        fd = open(argv[optind], O_RDWR);
-        if (fd == -1)
+        sockfd = open(argv[optind], O_RDWR);
+        if (sockfd == -1)

qemu-nbd.c int main(int argc, char **argv) · line 413

Quoted 10 lines
qemu-nbd.c +2 -2
              err(EXIT_FAILURE, "Cannot open %s", argv[optind]);
 
-        nbd_disconnect(fd);
+        nbd_disconnect(sockfd);
 
-        close(fd);
+        close(sockfd);
 
          printf("%s disconnected\n", argv[optind]);
 

This should be devfd.

Quoted 36 lines
qemu-nbd.c +6 -6
@@ -470,8 +470,8 @@ int main(int argc, char **argv)
          /* Open before spawning new threads.  In the future, we may
           * drop privileges after opening.
           */
-        fd = open(device, O_RDWR);
-        if (fd == -1) {
+        devfd = open(device, O_RDWR);
+        if (devfd == -1) {
              err(EXIT_FAILURE, "Failed to open %s", device);
          }
 
@@ -501,19 +501,19 @@ int main(int argc, char **argv)
      exp = nbd_export_new(bs, dev_offset, fd_size, nbdflags);
 
      if (sockpath) {
-        fd = unix_socket_incoming(sockpath);
+        sockfd = unix_socket_incoming(sockpath);
      } else {
-        fd = tcp_socket_incoming(bindto, port);
+        sockfd = tcp_socket_incoming(bindto, port);
      }
 
-    if (fd == -1) {
+    if (sockfd == -1) {
          return 1;
      }
 
      if (device) {
          int ret;
 
-        ret = pthread_create(&client_thread, NULL, nbd_client_thread,&fd);
+        ret = pthread_create(&client_thread, NULL, nbd_client_thread,&devfd);
          if (ret != 0) {
              errx(EXIT_FAILURE, "Failed to create client thread: %s",
                   strerror(ret));

qemu-nbd.c int main(int argc, char **argv) · line 531

Quoted 10 lines
qemu-nbd.c +2 -2
@@ -524,8 +524,8 @@ int main(int argc, char **argv)
      }
 
      qemu_init_main_loop();
-    qemu_set_fd_handler2(fd, nbd_can_accept, nbd_accept, NULL,
-                         (void *)(uintptr_t)fd);
+    qemu_set_fd_handler2(sockfd, nbd_can_accept, nbd_accept, NULL,
+                         (void *)(uintptr_t)sockfd);
 
      do {
          main_loop_wait(false);

Otherwise looks good, I'll fix up and send for inclusion.

Paolo

#2 lore raw
Quoted 48 lines · Paolo Bonzini
Quoted 47 lines · Michael Tokarev

commit a61c67828dea7c64edaf226cadb45b4ffcc1d411

Author: Paolo Bonzini<[email protected]>
Date:   Mon Sep 12 17:28:11 2011 +0200

qemu-nbd: use common main loop

Using a single main loop for sockets will help yielding from the socket coroutine back to the main loop, and later reentering it.

changed code to use local variable "fd" in qemu-nbd.c:main() in two places: for /dev/nbd device and for control socket. The result is that qemu-nbd -c $device does not work anymore.

Use two variables - devfs and sockfd - for the two purposes, instead of one fd.

---
  qemu-nbd.c |   26 +++++++++++++-------------
  1 files changed, 13 insertions(+), 13 deletions(-)
qemu-nbd.c +4 -4
index eb61c33..e76c782 100644
@@ -301,7 +301,7 @@ int main(int argc, char **argv)
      int flags = BDRV_O_RDWR;
      int partition = -1;
      int ret;
-    int fd;
+    int sockfd, devfd;
      int persistent = 0;
      pthread_t client_thread;
 
@@ -401,13 +401,13 @@ int main(int argc, char **argv)
      }
 
      if (disconnect) {
-        fd = open(argv[optind], O_RDWR);
-        if (fd == -1)
+        sockfd = open(argv[optind], O_RDWR);
+        if (sockfd == -1)
              err(EXIT_FAILURE, "Cannot open %s", argv[optind]);
 
-        nbd_disconnect(fd);
+        nbd_disconnect(sockfd);
 
Quoted 6 lines

qemu-nbd.c int main(int argc, char **argv) · line 412

Quoted 4 lines
qemu-nbd.c +1 -1
-        close(fd);
+        close(sockfd);
 
          printf("%s disconnected\n", argv[optind]);

This should be devfd.

Yes indeed. In that case make it nbdfd not devfd - there are other variables prefixed "nbd" already.

[]

Quoted 1 line

Otherwise looks good, I'll fix up and send for inclusion.

Thanks!

FWIW, it is not -stable material, since 1.0 isn't broken yet ;)

/mjt