git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Matthias Lederhofer <matled@gmx.net>
To: Edgar Toernig <froese@gmx.de>
Cc: git@vger.kernel.org
Subject: Re: [PATCH 5.1/5] daemon: new option --detach to run git-daemon in background
Date: Thu, 13 Jul 2006 18:47:13 +0200	[thread overview]
Message-ID: <E1G14LJ-0007q1-Fa@moooo.ath.cx> (raw)
In-Reply-To: <20060713153703.05f862ee.froese@gmx.de>

Signed-off-by: Matthias Lederhofer <matled@gmx.net>
---
Edgar Toernig <froese@gmx.de> wrote:
> Hmm... leaks devnull.  Why not simply close(0/1/2) and
> let sanitize_stdfds take care of the rest?
---
 daemon.c |   29 ++++++++++++++++++++++++++++-
 1 files changed, 28 insertions(+), 1 deletions(-)

diff --git a/daemon.c b/daemon.c
index cdc4266..e4ec676 100644
--- a/daemon.c
+++ b/daemon.c
@@ -674,6 +674,24 @@ static void sanitize_stdfds(void)
 		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)
 {
 	FILE *f = fopen(path, "w");
@@ -699,6 +717,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
@@ -767,6 +786,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;
@@ -799,7 +823,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.g8b4b

  reply	other threads:[~2006-07-13 16:47 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-07-11 22:24 git-daemon problem Matthias Lederhofer
2006-07-11 23:04 ` Junio C Hamano
2006-07-11 23:32 ` Junio C Hamano
2006-07-12  4:42 ` Junio C Hamano
2006-07-12 19:28   ` Matthias Lederhofer
2006-07-13  5:44     ` Junio C Hamano
2006-07-13  7:42       ` Matthias Lederhofer
2006-07-13 11:42       ` Andre Noll
2006-07-13 11:51 ` Matthias Lederhofer
2006-07-13 10:02   ` [PATCH 1/5] daemon: use a custom die routine with syslog Matthias Lederhofer
2006-07-13 10:10   ` [PATCH 2/5] daemon: if one of the standard fds is missing open it to /dev/null Matthias Lederhofer
2006-07-13 13:27     ` Edgar Toernig
2006-07-13 14:04       ` Matthias Lederhofer
2006-07-13 14:36         ` Uwe Zeisberger
2006-07-13 15:37     ` Morten Welinder
2006-07-13 16:03       ` Matthias Lederhofer
2006-07-13 16:32     ` [PATCH 2.1/5] " Matthias Lederhofer
2006-07-13 10:18   ` [PATCH 4/5] daemon: new option --pid-file=<path> to store the pid Matthias Lederhofer
2006-07-13 10:32   ` [PATCH 5/5] daemon: new option --detach to run git-daemon in background Matthias Lederhofer
2006-07-13 13:37     ` Edgar Toernig
2006-07-13 16:47       ` Matthias Lederhofer [this message]
2006-07-13 11:07   ` [PATCH 3/5] upload-pack: ignore write errors to stderr Matthias Lederhofer
2006-07-14 15:53   ` [PATCH] daemon: documentation for --reuseaddr, --detach and --pid-file Matthias Lederhofer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E1G14LJ-0007q1-Fa@moooo.ath.cx \
    --to=matled@gmx.net \
    --cc=froese@gmx.de \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).