@matheusbsbs / matheus-bagliano / commits / 1955fabf41
Fork 0

daemon: Set SO_REUSEADDR on listening sockets.

Without this, you can silently lose the ability to receive IPv4 connections if you stop and restart the daemon. [jc: tweaked code organization a bit and made this controllable from a command line option.] Signed-off-by: Mark Wooding <mdw@distorted.org.uk> Signed-off-by: Junio C Hamano <junkio@cox.net>

Mark Wooding committed Feb 3, 2006 at 20:27 UTC 1955fabf4194f3629e028778d0081bb2aa16c06c
1 file changed +26 -1
daemon.c
+26 -1
@@ -13,11 +13,12 @@
13
14 static int log_syslog;
15 static int verbose;
16 +static int reuseaddr;
17
18 static const char daemon_usage[] =
19 "git-daemon [--verbose] [--syslog] [--inetd | --port=n] [--export-all]\n"
20 " [--timeout=n] [--init-timeout=n] [--strict-paths]\n"
20 -" [--base-path=path] [directory...]";
21 +" [--base-path=path] [--reuseaddr] [directory...]";
22
23 /* List of acceptable pathname prefixes */
24 static char **ok_paths = NULL;
@@ -451,6 +452,16 @@ static void child_handler(int signo)
452 }
453 }
454
455 +static int set_reuse_addr(int sockfd)
456 +{
457 + int on = 1;
458 +
459 + if (!reuseaddr)
460 + return 0;
461 + return setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
462 + &on, sizeof(on));
463 +}
464 +
465 #ifndef NO_IPV6
466
467 static int socksetup(int port, int **socklist_p)
@@ -495,6 +506,11 @@ static int socksetup(int port, int **socklist_p)
506 }
507 #endif
508
509 + if (set_reuse_addr(sockfd)) {
510 + close(sockfd);
511 + return 0; /* not fatal */
512 + }
513 +
514 if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
515 close(sockfd);
516 continue; /* not fatal */
@@ -537,6 +553,11 @@ static int socksetup(int port, int **socklist_p)
553 sin.sin_addr.s_addr = htonl(INADDR_ANY);
554 sin.sin_port = htons(port);
555
556 + if (set_reuse_addr(sockfd)) {
557 + close(sockfd);
558 + return 0;
559 + }
560 +
561 if ( bind(sockfd, (struct sockaddr *)&sin, sizeof sin) < 0 ) {
562 close(sockfd);
563 return 0;
@@ -663,6 +684,10 @@ int main(int argc, char **argv)
684 base_path = arg+12;
685 continue;
686 }
687 + if (!strcmp(arg, "--reuseaddr")) {
688 + reuseaddr = 1;
689 + continue;
690 + }
691 if (!strcmp(arg, "--")) {
692 ok_paths = &argv[i+1];
693 break;