8651382
From d2d3940a65dab60a2caeaf824eaff12fcc85e1f0 Mon Sep 17 00:00:00 2001
8651382
From: "Richard W.M. Jones" <rjones@redhat.com>
8651382
Date: Thu, 12 Sep 2019 10:28:19 +0100
8651382
Subject: [PATCH 2/3] nbd_connect_tcp: Try to return errno from underlying
8651382
 connect(2) call.
8651382
8651382
When we make a TCP connection we have to make multiple underlying
8651382
connect(2) calls, once for each address returned by getaddrinfo.
8651382
Unfortunately this meant that we lost the errno from any of these
8651382
calls:
8651382
8651382
$ nbdsh -c 'h.connect_tcp ("localhost", "nbd")'
8651382
nbd.Error: nbd_connect_tcp: connect: localhost:nbd: could not connect to remote host
8651382
8651382
This commit saves the errno from the first failed connect(2):
8651382
8651382
$ ./run nbdsh -c 'h.connect_tcp ("localhost", "nbd")'
8651382
nbd.Error: nbd_connect_tcp: connect: localhost:nbd: could not connect to remote host: Connection refused (ECONNREFUSED)
8651382
---
8651382
 generator/states-connect.c | 12 ++++++++++--
8651382
 lib/internal.h             |  1 +
8651382
 2 files changed, 11 insertions(+), 2 deletions(-)
8651382
8651382
diff --git a/generator/states-connect.c b/generator/states-connect.c
8651382
index 9e2e1d4..e9b3582 100644
8651382
--- a/generator/states-connect.c
8651382
+++ b/generator/states-connect.c
8651382
@@ -128,6 +128,8 @@ disable_nagle (int sock)
8651382
     h->result = NULL;
8651382
   }
8651382
 
8651382
+  h->connect_errno = 0;
8651382
+
8651382
   memset (&h->hints, 0, sizeof h->hints);
8651382
   h->hints.ai_family = AF_UNSPEC;
8651382
   h->hints.ai_socktype = SOCK_STREAM;
8651382
@@ -160,7 +162,8 @@ disable_nagle (int sock)
8651382
      * Save errno from most recent connect(2) call. XXX
8651382
      */
8651382
     SET_NEXT_STATE (%^START);
8651382
-    set_error (0, "connect: %s:%s: could not connect to remote host",
8651382
+    set_error (h->connect_errno,
8651382
+               "connect: %s:%s: could not connect to remote host",
8651382
                h->hostname, h->port);
8651382
     return -1;
8651382
   }
8651382
@@ -182,6 +185,8 @@ disable_nagle (int sock)
8651382
 
8651382
   if (connect (fd, h->rp->ai_addr, h->rp->ai_addrlen) == -1) {
8651382
     if (errno != EINPROGRESS) {
8651382
+      if (h->connect_errno == 0)
8651382
+        h->connect_errno = errno;
8651382
       SET_NEXT_STATE (%NEXT_ADDRESS);
8651382
       return 0;
8651382
     }
8651382
@@ -203,8 +208,11 @@ disable_nagle (int sock)
8651382
   /* This checks the status of the original connect call. */
8651382
   if (status == 0)
8651382
     SET_NEXT_STATE (%^MAGIC.START);
8651382
-  else
8651382
+  else {
8651382
+    if (h->connect_errno == 0)
8651382
+      h->connect_errno = status;
8651382
     SET_NEXT_STATE (%NEXT_ADDRESS);
8651382
+  }
8651382
   return 0;
8651382
 
8651382
  CONNECT_TCP.NEXT_ADDRESS:
8651382
diff --git a/lib/internal.h b/lib/internal.h
8651382
index a48edff..ccaca32 100644
8651382
--- a/lib/internal.h
8651382
+++ b/lib/internal.h
8651382
@@ -188,6 +188,7 @@ struct nbd_handle {
8651382
   char *hostname, *port;
8651382
   struct addrinfo hints;
8651382
   struct addrinfo *result, *rp;
8651382
+  int connect_errno;
8651382
 
8651382
   /* When sending metadata contexts, this is used. */
8651382
   size_t querynum;
8651382
-- 
8651382
2.23.0
8651382