daemon: log errors if we could not use some sockets
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
f696543dad
commit
089d82e8a6
37
daemon.c
37
daemon.c
@ -734,6 +734,29 @@ struct socketlist {
|
||||
size_t alloc;
|
||||
};
|
||||
|
||||
static const char *ip2str(int family, struct sockaddr *sin, socklen_t len)
|
||||
{
|
||||
#ifdef NO_IPV6
|
||||
static char ip[INET_ADDRSTRLEN];
|
||||
#else
|
||||
static char ip[INET6_ADDRSTRLEN];
|
||||
#endif
|
||||
|
||||
switch (family) {
|
||||
#ifndef NO_IPV6
|
||||
case AF_INET6:
|
||||
inet_ntop(family, &((struct sockaddr_in6*)sin)->sin6_addr, ip, len);
|
||||
break;
|
||||
#endif
|
||||
case AF_INET:
|
||||
inet_ntop(family, &((struct sockaddr_in*)sin)->sin_addr, ip, len);
|
||||
break;
|
||||
default:
|
||||
strcpy(ip, "<unknown>");
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
|
||||
#ifndef NO_IPV6
|
||||
|
||||
static int setup_named_sock(char *listen_addr, int listen_port, struct socketlist *socklist)
|
||||
@ -780,15 +803,22 @@ static int setup_named_sock(char *listen_addr, int listen_port, struct socketlis
|
||||
#endif
|
||||
|
||||
if (set_reuse_addr(sockfd)) {
|
||||
logerror("Could not set SO_REUSEADDR: %s", strerror(errno));
|
||||
close(sockfd);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
|
||||
logerror("Could not bind to %s: %s",
|
||||
ip2str(ai->ai_family, ai->ai_addr, ai->ai_addrlen),
|
||||
strerror(errno));
|
||||
close(sockfd);
|
||||
continue; /* not fatal */
|
||||
}
|
||||
if (listen(sockfd, 5) < 0) {
|
||||
logerror("Could not listen to %s: %s",
|
||||
ip2str(ai->ai_family, ai->ai_addr, ai->ai_addrlen),
|
||||
strerror(errno));
|
||||
close(sockfd);
|
||||
continue; /* not fatal */
|
||||
}
|
||||
@ -835,16 +865,23 @@ static int setup_named_sock(char *listen_addr, int listen_port, struct socketlis
|
||||
return 0;
|
||||
|
||||
if (set_reuse_addr(sockfd)) {
|
||||
logerror("Could not set SO_REUSEADDR: %s", strerror(errno));
|
||||
close(sockfd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ( bind(sockfd, (struct sockaddr *)&sin, sizeof sin) < 0 ) {
|
||||
logerror("Could not listen to %s: %s",
|
||||
ip2str(AF_INET, (struct sockaddr *)&sin, sizeof(sin)),
|
||||
strerror(errno));
|
||||
close(sockfd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (listen(sockfd, 5) < 0) {
|
||||
logerror("Could not listen to %s: %s",
|
||||
ip2str(AF_INET, (struct sockaddr *)&sin, sizeof(sin)),
|
||||
strerror(errno));
|
||||
close(sockfd);
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user