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>

Nguyễn Thái Ngọc Duy committed Oct 4, 2011 at 06:13 UTC 089d82e8a6d6443e10052f04c469e9e70784eb93
1 file changed +37
daemon.c
+37
@@ -734,6 +734,29 @@ struct socketlist {
734 size_t alloc;
735 };
736
737 +static const char *ip2str(int family, struct sockaddr *sin, socklen_t len)
738 +{
739 +#ifdef NO_IPV6
740 + static char ip[INET_ADDRSTRLEN];
741 +#else
742 + static char ip[INET6_ADDRSTRLEN];
743 +#endif
744 +
745 + switch (family) {
746 +#ifndef NO_IPV6
747 + case AF_INET6:
748 + inet_ntop(family, &((struct sockaddr_in6*)sin)->sin6_addr, ip, len);
749 + break;
750 +#endif
751 + case AF_INET:
752 + inet_ntop(family, &((struct sockaddr_in*)sin)->sin_addr, ip, len);
753 + break;
754 + default:
755 + strcpy(ip, "<unknown>");
756 + }
757 + return ip;
758 +}
759 +
760 #ifndef NO_IPV6
761
762 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
803 #endif
804
805 if (set_reuse_addr(sockfd)) {
806 + logerror("Could not set SO_REUSEADDR: %s", strerror(errno));
807 close(sockfd);
808 continue;
809 }
810
811 if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
812 + logerror("Could not bind to %s: %s",
813 + ip2str(ai->ai_family, ai->ai_addr, ai->ai_addrlen),
814 + strerror(errno));
815 close(sockfd);
816 continue; /* not fatal */
817 }
818 if (listen(sockfd, 5) < 0) {
819 + logerror("Could not listen to %s: %s",
820 + ip2str(ai->ai_family, ai->ai_addr, ai->ai_addrlen),
821 + strerror(errno));
822 close(sockfd);
823 continue; /* not fatal */
824 }
@@ -835,16 +865,23 @@ static int setup_named_sock(char *listen_addr, int listen_port, struct socketlis
865 return 0;
866
867 if (set_reuse_addr(sockfd)) {
868 + logerror("Could not set SO_REUSEADDR: %s", strerror(errno));
869 close(sockfd);
870 return 0;
871 }
872
873 if ( bind(sockfd, (struct sockaddr *)&sin, sizeof sin) < 0 ) {
874 + logerror("Could not listen to %s: %s",
875 + ip2str(AF_INET, (struct sockaddr *)&sin, sizeof(sin)),
876 + strerror(errno));
877 close(sockfd);
878 return 0;
879 }
880
881 if (listen(sockfd, 5) < 0) {
882 + logerror("Could not listen to %s: %s",
883 + ip2str(AF_INET, (struct sockaddr *)&sin, sizeof(sin)),
884 + strerror(errno));
885 close(sockfd);
886 return 0;
887 }