[PATCH 5/5] daemon: new option --detach to run git-daemon in background
From: Matthias Lederhofer <hidden>
Date: 2016-06-15 22:42:33
Subsystem:
the rest · Maintainer:
Linus Torvalds
Signed-off-by: Matthias Lederhofer <redacted> --- daemon.c | 32 +++++++++++++++++++++++++++++++- 1 files changed, 31 insertions(+), 1 deletions(-)
diff --git a/daemon.c b/daemon.c
index 4b85930..9f4bc20 100644
--- a/daemon.c
+++ b/daemon.c@@ -662,6 +662,27 @@ static int service_loop(int socknum, int } } +static void daemonize(void) +{ + int devnull = -1; + 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)); + if ((devnull = open("/dev/null", O_RDWR, 0)) == -1) + die("open /dev/null failed: %s", strerror(errno)); + if (dup2(devnull, 0) != 0 || + dup2(devnull, 1) != 1 || + dup2(devnull, 2) != 2) + die("dup2 failed: %s", strerror(errno)); +} + /* if any standard file descriptor is missing open it to /dev/null */ static void sanitize_stdfds(void) {
@@ -705,6 +726,7 @@ int main(int argc, char **argv) int port = DEFAULT_GIT_PORT; int inetd_mode = 0; const char *pid_file = NULL; + int detach = 0; int i; /* Without this we cannot rely on waitpid() to tell
@@ -773,6 +795,11 @@ int main(int argc, char **argv) pid_file = arg + 11; continue; } + if (!strcmp(arg, "--detach")) { + detach = 1; + log_syslog = 1; + continue; + } if (!strcmp(arg, "--")) { ok_paths = &argv[i+1]; break;
@@ -805,7 +832,10 @@ int main(int argc, char **argv) return execute(peer); } - sanitize_stdfds(); + if (detach) + daemonize(); + else + sanitize_stdfds(); if (pid_file) store_pid(pid_file);
--
1.4.1.gb16f