Skip to content

Commit 908bb1a

Browse files
committed
Merge branch 'dz/connect-error-report' into maint
* dz/connect-error-report: Do not log unless all connect() attempts fail
2 parents b3038a5 + 63a995b commit 908bb1a

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

connect.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ static const char *ai_name(const struct addrinfo *ai)
192192
*/
193193
static int git_tcp_connect_sock(char *host, int flags)
194194
{
195-
int sockfd = -1, saved_errno = 0;
195+
struct strbuf error_message = STRBUF_INIT;
196+
int sockfd = -1;
196197
const char *port = STR(DEFAULT_GIT_PORT);
197198
struct addrinfo hints, *ai0, *ai;
198199
int gai;
@@ -219,18 +220,12 @@ static int git_tcp_connect_sock(char *host, int flags)
219220
for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
220221
sockfd = socket(ai->ai_family,
221222
ai->ai_socktype, ai->ai_protocol);
222-
if (sockfd < 0) {
223-
saved_errno = errno;
224-
continue;
225-
}
226-
if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
227-
saved_errno = errno;
228-
fprintf(stderr, "%s[%d: %s]: errno=%s\n",
229-
host,
230-
cnt,
231-
ai_name(ai),
232-
strerror(saved_errno));
233-
close(sockfd);
223+
if ((sockfd < 0) ||
224+
(connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
225+
strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
226+
host, cnt, ai_name(ai), strerror(errno));
227+
if (0 <= sockfd)
228+
close(sockfd);
234229
sockfd = -1;
235230
continue;
236231
}
@@ -242,11 +237,13 @@ static int git_tcp_connect_sock(char *host, int flags)
242237
freeaddrinfo(ai0);
243238

244239
if (sockfd < 0)
245-
die("unable to connect a socket (%s)", strerror(saved_errno));
240+
die("unable to connect to %s:\n%s", host, error_message.buf);
246241

247242
if (flags & CONNECT_VERBOSE)
248243
fprintf(stderr, "done.\n");
249244

245+
strbuf_release(&error_message);
246+
250247
return sockfd;
251248
}
252249

0 commit comments

Comments
 (0)