Skip to content

Commit a526276

Browse files
matledJunio C Hamano
authored andcommitted
daemon: new option --detach to run git-daemon in background
Signed-off-by: Matthias Lederhofer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 45ed5d7 commit a526276

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

daemon.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,24 @@ static void sanitize_stdfds(void)
674674
close(fd);
675675
}
676676

677+
static void daemonize(void)
678+
{
679+
switch (fork()) {
680+
case 0:
681+
break;
682+
case -1:
683+
die("fork failed: %s", strerror(errno));
684+
default:
685+
exit(0);
686+
}
687+
if (setsid() == -1)
688+
die("setsid failed: %s", strerror(errno));
689+
close(0);
690+
close(1);
691+
close(2);
692+
sanitize_stdfds();
693+
}
694+
677695
static void store_pid(const char *path)
678696
{
679697
FILE *f = fopen(path, "w");
@@ -699,6 +717,7 @@ int main(int argc, char **argv)
699717
int port = DEFAULT_GIT_PORT;
700718
int inetd_mode = 0;
701719
const char *pid_file = NULL;
720+
int detach = 0;
702721
int i;
703722

704723
/* Without this we cannot rely on waitpid() to tell
@@ -767,6 +786,11 @@ int main(int argc, char **argv)
767786
pid_file = arg + 11;
768787
continue;
769788
}
789+
if (!strcmp(arg, "--detach")) {
790+
detach = 1;
791+
log_syslog = 1;
792+
continue;
793+
}
770794
if (!strcmp(arg, "--")) {
771795
ok_paths = &argv[i+1];
772796
break;
@@ -799,7 +823,10 @@ int main(int argc, char **argv)
799823
return execute(peer);
800824
}
801825

802-
sanitize_stdfds();
826+
if (detach)
827+
daemonize();
828+
else
829+
sanitize_stdfds();
803830

804831
if (pid_file)
805832
store_pid(pid_file);

0 commit comments

Comments
 (0)