git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Thomas Rast <trast@inf.ethz.ch>
To: <git@vger.kernel.org>
Cc: Junio C Hamano <gitster@pobox.com>,
	"Dale R. Worley" <worley@alum.mit.edu>
Subject: [PATCH 1/2] daemon/shell: refactor redirection of 0/1/2 from /dev/null
Date: Tue, 16 Jul 2013 11:27:36 +0200	[thread overview]
Message-ID: <1fe4ff3df32aa1836ed3672e749f582047745bc9.1373966758.git.trast@inf.ethz.ch> (raw)
In-Reply-To: <7vtxjzlmaf.fsf@alter.siamese.dyndns.org>

Both daemon.c and shell.c contain logic to open FDs 0/1/2 from
/dev/null if they are not already open.  Move the function in daemon.c
to setup.c and use it in shell.c, too.

While there, remove a 'not' that inverted the meaning of the comment.
The point is indeed to *avoid* messing up.

Signed-off-by: Thomas Rast <trast@inf.ethz.ch>
---
 cache.h  |  2 ++
 daemon.c | 12 ------------
 setup.c  | 12 ++++++++++++
 shell.c  | 12 +++---------
 4 files changed, 17 insertions(+), 21 deletions(-)

diff --git a/cache.h b/cache.h
index dd0fb33..f007724 100644
--- a/cache.h
+++ b/cache.h
@@ -425,6 +425,8 @@ extern void verify_filename(const char *prefix,
 extern int set_git_dir_init(const char *git_dir, const char *real_git_dir, int);
 extern int init_db(const char *template_dir, unsigned int flags);
 
+extern void sanitize_stdfds(void);
+
 #define alloc_nr(x) (((x)+16)*3/2)
 
 /*
diff --git a/daemon.c b/daemon.c
index 6aeddcb..973ec38 100644
--- a/daemon.c
+++ b/daemon.c
@@ -1047,18 +1047,6 @@ static int service_loop(struct socketlist *socklist)
 	}
 }
 
-/* if any standard file descriptor is missing open it to /dev/null */
-static void sanitize_stdfds(void)
-{
-	int fd = open("/dev/null", O_RDWR, 0);
-	while (fd != -1 && fd < 2)
-		fd = dup(fd);
-	if (fd == -1)
-		die_errno("open /dev/null or dup failed");
-	if (fd > 2)
-		close(fd);
-}
-
 #ifdef NO_POSIX_GOODIES
 
 struct credentials;
diff --git a/setup.c b/setup.c
index 94c1e61..88aab94 100644
--- a/setup.c
+++ b/setup.c
@@ -908,3 +908,15 @@ const char *resolve_gitdir(const char *suspect)
 		return suspect;
 	return read_gitfile(suspect);
 }
+
+/* if any standard file descriptor is missing open it to /dev/null */
+void sanitize_stdfds(void)
+{
+	int fd = open("/dev/null", O_RDWR, 0);
+	while (fd != -1 && fd < 2)
+		fd = dup(fd);
+	if (fd == -1)
+		die_errno("open /dev/null or dup failed");
+	if (fd > 2)
+		close(fd);
+}
diff --git a/shell.c b/shell.c
index 1429870..66350b2 100644
--- a/shell.c
+++ b/shell.c
@@ -147,7 +147,6 @@ int main(int argc, char **argv)
 	char *prog;
 	const char **user_argv;
 	struct commands *cmd;
-	int devnull_fd;
 	int count;
 
 	git_setup_gettext();
@@ -156,15 +155,10 @@ int main(int argc, char **argv)
 
 	/*
 	 * Always open file descriptors 0/1/2 to avoid clobbering files
-	 * in die().  It also avoids not messing up when the pipes are
-	 * dup'ed onto stdin/stdout/stderr in the child processes we spawn.
+	 * in die().  It also avoids messing up when the pipes are dup'ed
+	 * onto stdin/stdout/stderr in the child processes we spawn.
 	 */
-	devnull_fd = open("/dev/null", O_RDWR);
-	while (devnull_fd >= 0 && devnull_fd <= 2)
-		devnull_fd = dup(devnull_fd);
-	if (devnull_fd == -1)
-		die_errno("opening /dev/null failed");
-	close (devnull_fd);
+	sanitize_stdfds();
 
 	/*
 	 * Special hack to pretend to be a CVS server
-- 
1.8.3.2.998.g1d087bc

  parent reply	other threads:[~2013-07-16  9:27 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-12  8:58 [PATCH 0/2] open() error checking Thomas Rast
2013-07-12  8:58 ` [PATCH 1/2] git_mkstemps: correctly test return value of open() Thomas Rast
2013-07-16  9:37   ` Thomas Rast
2013-07-17 19:29     ` Junio C Hamano
2013-07-18 12:32       ` Drew Northup
2013-07-18 17:46         ` Junio C Hamano
2013-07-18 17:47           ` Junio C Hamano
2013-07-18 20:32           ` Dale R. Worley
2013-07-18 20:49             ` Eric Sunshine
2013-07-18 20:54             ` Junio C Hamano
2013-07-18 22:46               ` Dale R. Worley
2013-07-18 23:23                 ` Junio C Hamano
2013-07-18 23:29                   ` Dale R. Worley
2013-07-12  8:58 ` [PATCH 2/2] run-command: dup_devnull(): guard against syscalls failing Thomas Rast
2013-07-12 17:29 ` [PATCH 0/2] open() error checking Junio C Hamano
2013-07-16  9:25   ` Thomas Rast
2013-07-16  9:27   ` Thomas Rast [this message]
2013-07-16  9:27     ` [PATCH 2/2] git: ensure 0/1/2 are open in main() Thomas Rast

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=1fe4ff3df32aa1836ed3672e749f582047745bc9.1373966758.git.trast@inf.ethz.ch \
    --to=trast@inf.ethz.ch \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=worley@alum.mit.edu \
    /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).