# With errno fix: [PATCH] Do not log unless all connect() attempts fail

4 messages from 2011-07-13 to 2011-07-13. Participants: Dave Zarzycki, Erik Faye-Lund, Junio C Hamano.
Thread: https://gitlist.dev/t/27807

## Dave Zarzycki, 2011-07-13 16:28

Subject: With errno fix: [PATCH] Do not log unless all connect() attempts fail
Message-ID: <7276ACEE-EF52-49DF-83EA-642DE504B3EA@apple.com>
URL: https://gitlist.dev/e/7276ACEE-EF52-49DF-83EA-642DE504B3EA%40apple.com

```
IPv6 hosts are often unreachable on the primarily IPv4 Internet and
therefore we shouldn't print an error if there are still other hosts we
can try to connect() to. This helps "git fetch --quiet" stay quiet.

Signed-off-by: Dave Zarzycki <zarzycki@apple.com>
---
 connect.c |   15 +++++++++------
 1 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/connect.c b/connect.c
index 2119c3f..87b2e3f 100644
--- a/connect.c
+++ b/connect.c
@@ -192,6 +192,7 @@ static const char *ai_name(const struct addrinfo *ai)
  */
 static int git_tcp_connect_sock(char *host, int flags)
 {
+	struct strbuf error_message = STRBUF_INIT;
 	int sockfd = -1, saved_errno = 0;
 	const char *port = STR(DEFAULT_GIT_PORT);
 	struct addrinfo hints, *ai0, *ai;
@@ -217,6 +218,11 @@ static int git_tcp_connect_sock(char *host, int flags)
 		fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
 
 	for (ai0 = ai; ai; ai = ai->ai_next) {
+		if (saved_errno) {
+			strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
+				host, cnt, ai_name(ai), strerror(saved_errno));
+			saved_errno = 0;
+		}
 		sockfd = socket(ai->ai_family,
 				ai->ai_socktype, ai->ai_protocol);
 		if (sockfd < 0) {
@@ -225,11 +231,6 @@ static int git_tcp_connect_sock(char *host, int flags)
 		}
 		if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
 			saved_errno = errno;
-			fprintf(stderr, "%s[%d: %s]: errno=%s\n",
-				host,
-				cnt,
-				ai_name(ai),
-				strerror(saved_errno));
 			close(sockfd);
 			sockfd = -1;
 			continue;
@@ -242,11 +243,13 @@ static int git_tcp_connect_sock(char *host, int flags)
 	freeaddrinfo(ai0);
 
 	if (sockfd < 0)
-		die("unable to connect a socket (%s)", strerror(saved_errno));
+		die("unable to connect to %s:\n%s", host, error_message.buf);
 
 	if (flags & CONNECT_VERBOSE)
 		fprintf(stderr, "done.\n");
 
+	strbuf_release(&error_message);
+
 	return sockfd;
 }
 
-- 
1.7.6.135.g8cdba

```

## Erik Faye-Lund, 2011-07-13 18:47

Subject: Re: With errno fix: [PATCH] Do not log unless all connect() attempts fail
Message-ID: <CABPQNSYWKVngfoj4AqTCbWixENSmgnbdDBGBt+EpWnBgbqpofg@mail.gmail.com>
URL: https://gitlist.dev/e/CABPQNSYWKVngfoj4AqTCbWixENSmgnbdDBGBt%2BEpWnBgbqpofg%40mail.gmail.com
In-Reply-To: <7276ACEE-EF52-49DF-83EA-642DE504B3EA@apple.com>

```
On Wed, Jul 13, 2011 at 6:28 PM, Dave Zarzycki <zarzycki@apple.com> wrote:
> IPv6 hosts are often unreachable on the primarily IPv4 Internet and
> therefore we shouldn't print an error if there are still other hosts we
> can try to connect() to. This helps "git fetch --quiet" stay quiet.
>
> Signed-off-by: Dave Zarzycki <zarzycki@apple.com>
> ---
>  connect.c |   15 +++++++++------
>  1 files changed, 9 insertions(+), 6 deletions(-)
>
> diff --git a/connect.c b/connect.c
> index 2119c3f..87b2e3f 100644
> --- a/connect.c
> +++ b/connect.c
> @@ -192,6 +192,7 @@ static const char *ai_name(const struct addrinfo *ai)
>  */
>  static int git_tcp_connect_sock(char *host, int flags)
>  {
> +       struct strbuf error_message = STRBUF_INIT;
>        int sockfd = -1, saved_errno = 0;
>        const char *port = STR(DEFAULT_GIT_PORT);
>        struct addrinfo hints, *ai0, *ai;
> @@ -217,6 +218,11 @@ static int git_tcp_connect_sock(char *host, int flags)
>                fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
>
>        for (ai0 = ai; ai; ai = ai->ai_next) {
> +               if (saved_errno) {
> +                       strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
> +                               host, cnt, ai_name(ai), strerror(saved_errno));
> +                       saved_errno = 0;
> +               }

Uhm, this will still fail to report errors for the very last entry,
no? When socket returns -1 in the last iteration (and errno gets
saved), there's no code that reports it...

```

## Junio C Hamano, 2011-07-13 20:58

Subject: Re: With errno fix: [PATCH] Do not log unless all connect() attempts fail
Message-ID: <7vtyap6gox.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vtyap6gox.fsf%40alter.siamese.dyndns.org
In-Reply-To: <CABPQNSYWKVngfoj4AqTCbWixENSmgnbdDBGBt+EpWnBgbqpofg@mail.gmail.com>

```
Erik Faye-Lund <kusmabite@gmail.com> writes:

> Uhm, this will still fail to report errors for the very last entry,
> no? When socket returns -1 in the last iteration (and errno gets
> saved), there's no code that reports it...

I guess the fix should look something like this.

By the way, is anybody interested in fixing the other side of the ifdef
that is compiled on IPv4-only installations? 

 connect.c |   15 ++++++---------
 1 files changed, 6 insertions(+), 9 deletions(-)

diff --git a/connect.c b/connect.c
index 8eb9f44..844107e 100644
--- a/connect.c
+++ b/connect.c
@@ -193,7 +193,7 @@ static const char *ai_name(const struct addrinfo *ai)
 static int git_tcp_connect_sock(char *host, int flags)
 {
 	struct strbuf error_message = STRBUF_INIT;
-	int sockfd = -1, saved_errno = 0;
+	int sockfd = -1;
 	const char *port = STR(DEFAULT_GIT_PORT);
 	struct addrinfo hints, *ai0, *ai;
 	int gai;
@@ -220,15 +220,12 @@ static int git_tcp_connect_sock(char *host, int flags)
 	for (ai0 = ai; ai; ai = ai->ai_next) {
 		sockfd = socket(ai->ai_family,
 				ai->ai_socktype, ai->ai_protocol);
-		if (sockfd < 0) {
-			saved_errno = errno;
-			continue;
-		}
-		if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
-			saved_errno = errno;
+		if ((sockfd < 0) ||
+		    (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
 			strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
-				host, cnt, ai_name(ai), strerror(saved_errno));
-			close(sockfd);
+				    host, cnt, ai_name(ai), strerror(errno));
+			if (0 <= sockfd)
+				close(sockfd);
 			sockfd = -1;
 			continue;
 		}

```

## Erik Faye-Lund, 2011-07-13 22:38

Subject: Re: With errno fix: [PATCH] Do not log unless all connect() attempts fail
Message-ID: <CABPQNSZj7P6XUMgChgZ6XYEZVtVNmXmVM27_Ms=mXV1c1an6KQ@mail.gmail.com>
URL: https://gitlist.dev/e/CABPQNSZj7P6XUMgChgZ6XYEZVtVNmXmVM27_Ms%3DmXV1c1an6KQ%40mail.gmail.com
In-Reply-To: <7vtyap6gox.fsf@alter.siamese.dyndns.org>

```
On Wed, Jul 13, 2011 at 10:58 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Erik Faye-Lund <kusmabite@gmail.com> writes:
>
>> Uhm, this will still fail to report errors for the very last entry,
>> no? When socket returns -1 in the last iteration (and errno gets
>> saved), there's no code that reports it...
>
> I guess the fix should look something like this.
>
> By the way, is anybody interested in fixing the other side of the ifdef
> that is compiled on IPv4-only installations?
>
>  connect.c |   15 ++++++---------
>  1 files changed, 6 insertions(+), 9 deletions(-)
>
> diff --git a/connect.c b/connect.c
> index 8eb9f44..844107e 100644
> --- a/connect.c
> +++ b/connect.c
> @@ -193,7 +193,7 @@ static const char *ai_name(const struct addrinfo *ai)
>  static int git_tcp_connect_sock(char *host, int flags)
>  {
>        struct strbuf error_message = STRBUF_INIT;
> -       int sockfd = -1, saved_errno = 0;
> +       int sockfd = -1;
>        const char *port = STR(DEFAULT_GIT_PORT);
>        struct addrinfo hints, *ai0, *ai;
>        int gai;
> @@ -220,15 +220,12 @@ static int git_tcp_connect_sock(char *host, int flags)
>        for (ai0 = ai; ai; ai = ai->ai_next) {
>                sockfd = socket(ai->ai_family,
>                                ai->ai_socktype, ai->ai_protocol);
> -               if (sockfd < 0) {
> -                       saved_errno = errno;
> -                       continue;
> -               }
> -               if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
> -                       saved_errno = errno;
> +               if ((sockfd < 0) ||
> +                   (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
>                        strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
> -                               host, cnt, ai_name(ai), strerror(saved_errno));
> -                       close(sockfd);
> +                                   host, cnt, ai_name(ai), strerror(errno));
> +                       if (0 <= sockfd)
> +                               close(sockfd);
>                        sockfd = -1;
>                        continue;
>                }
>

Yeah, this looks like sensible to me.

```
