daemon/shell: refactor redirection of 0/1/2 from /dev/null
authorThomas Rast <trast@inf.ethz.ch>
Tue, 16 Jul 2013 09:27:36 +0000 (11:27 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 17 Jul 2013 19:50:34 +0000 (12:50 -0700)
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
cache.h
daemon.c
setup.c
shell.c
diff --git a/cache.h b/cache.h
index 3622e18415ee407d59e6270424d635b5f1df126c..91974710c3c105c054b7e591fb44742ccc0e594b 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -400,6 +400,8 @@ extern int path_inside_repo(const char *prefix, const char *path);
 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)
 
 /*
index df8c0ab0588e70ad6e6f56195535030055782d85..89aad6c4a39f384fc5218b7cc4b7ce2d9c519caa 100644 (file)
--- 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 1b120175f2184b782f389d2fc99f3e37710f35d3..12a85d16138f1500d8e86c4c5f80273b615e657a 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -872,3 +872,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 84b237fef352e2f94f853897dbb5f2364d50962d..d6fb9f05045bc11582259a6e1d6c1db0e198a66c 100644 (file)
--- a/shell.c
+++ b/shell.c
@@ -134,7 +134,6 @@ int main(int argc, char **argv)
        char *prog;
        const char **user_argv;
        struct commands *cmd;
-       int devnull_fd;
        int count;
 
        git_setup_gettext();
@@ -143,15 +142,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