daemon: new option --detach to run git-daemon in background

Signed-off-by: Matthias Lederhofer <matled@gmx.net>
Signed-off-by: Junio C Hamano <junkio@cox.net>
This commit is contained in:
Matthias Lederhofer 2006-07-13 18:47:13 +02:00 committed by Junio C Hamano
parent 45ed5d7f4c
commit a5262768e1

View File

@ -674,6 +674,24 @@ static void sanitize_stdfds(void)
close(fd); close(fd);
} }
static void daemonize(void)
{
switch (fork()) {
case 0:
break;
case -1:
die("fork failed: %s", strerror(errno));
default:
exit(0);
}
if (setsid() == -1)
die("setsid failed: %s", strerror(errno));
close(0);
close(1);
close(2);
sanitize_stdfds();
}
static void store_pid(const char *path) static void store_pid(const char *path)
{ {
FILE *f = fopen(path, "w"); FILE *f = fopen(path, "w");
@ -699,6 +717,7 @@ int main(int argc, char **argv)
int port = DEFAULT_GIT_PORT; int port = DEFAULT_GIT_PORT;
int inetd_mode = 0; int inetd_mode = 0;
const char *pid_file = NULL; const char *pid_file = NULL;
int detach = 0;
int i; int i;
/* Without this we cannot rely on waitpid() to tell /* Without this we cannot rely on waitpid() to tell
@ -767,6 +786,11 @@ int main(int argc, char **argv)
pid_file = arg + 11; pid_file = arg + 11;
continue; continue;
} }
if (!strcmp(arg, "--detach")) {
detach = 1;
log_syslog = 1;
continue;
}
if (!strcmp(arg, "--")) { if (!strcmp(arg, "--")) {
ok_paths = &argv[i+1]; ok_paths = &argv[i+1];
break; break;
@ -799,6 +823,9 @@ int main(int argc, char **argv)
return execute(peer); return execute(peer);
} }
if (detach)
daemonize();
else
sanitize_stdfds(); sanitize_stdfds();
if (pid_file) if (pid_file)