From: Junio C Hamano Date: Wed, 28 Dec 2011 19:32:36 +0000 (-0800) Subject: Merge branch 'ew/keepalive' into maint X-Git-Tag: v1.7.8.2~17 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/2cb1ff9ac35ff043e377f78e36a57422a6846485?hp=-c Merge branch 'ew/keepalive' into maint * ew/keepalive: enable SO_KEEPALIVE for connected TCP sockets --- 2cb1ff9ac35ff043e377f78e36a57422a6846485 diff --combined connect.c index 51990fa0cb,d725b1794f..d0f59ef0de --- a/connect.c +++ b/connect.c @@@ -22,7 -22,7 +22,7 @@@ static int check_ref(const char *name, len -= 5; /* REF_NORMAL means that we don't want the magic fake tag refs */ - if ((flags & REF_NORMAL) && check_ref_format(name) < 0) + if ((flags & REF_NORMAL) && check_refname_format(name, 0)) return 0; /* REF_HEADS means that we want regular branch heads */ @@@ -175,6 -175,15 +175,15 @@@ static void get_host_and_port(char **ho } } + static void enable_keepalive(int sockfd) + { + int ka = 1; + + if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0) + fprintf(stderr, "unable to set SO_KEEPALIVE on socket: %s\n", + strerror(errno)); + } + #ifndef NO_IPV6 static const char *ai_name(const struct addrinfo *ai) @@@ -239,6 -248,8 +248,8 @@@ static int git_tcp_connect_sock(char *h if (sockfd < 0) die("unable to connect to %s:\n%s", host, error_message.buf); + enable_keepalive(sockfd); + if (flags & CONNECT_VERBOSE) fprintf(stderr, "done.\n"); @@@ -254,8 -265,7 +265,8 @@@ */ static int git_tcp_connect_sock(char *host, int flags) { - int sockfd = -1, saved_errno = 0; + struct strbuf error_message = STRBUF_INIT; + int sockfd = -1; const char *port = STR(DEFAULT_GIT_PORT); char *ep; struct hostent *he; @@@ -285,21 -295,25 +296,21 @@@ fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port); for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) { - sockfd = socket(he->h_addrtype, SOCK_STREAM, 0); - if (sockfd < 0) { - saved_errno = errno; - continue; - } - memset(&sa, 0, sizeof sa); sa.sin_family = he->h_addrtype; sa.sin_port = htons(nport); memcpy(&sa.sin_addr, *ap, he->h_length); - if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) { - saved_errno = errno; - fprintf(stderr, "%s[%d: %s]: errno=%s\n", + sockfd = socket(he->h_addrtype, SOCK_STREAM, 0); + if ((sockfd < 0) || + connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) { + strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n", host, cnt, inet_ntoa(*(struct in_addr *)&sa.sin_addr), - strerror(saved_errno)); - close(sockfd); + strerror(errno)); + if (0 <= sockfd) + close(sockfd); sockfd = -1; continue; } @@@ -310,8 -324,10 +321,10 @@@ } 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); + enable_keepalive(sockfd); + if (flags & CONNECT_VERBOSE) fprintf(stderr, "done.\n");