Skip to content

Commit 1d999dd

Browse files
Thomas Rastgitster
authored andcommitted
daemon/shell: refactor redirection of 0/1/2 from /dev/null
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 62e91ef commit 1d999dd

File tree

4 files changed

+17
-21
lines changed

4 files changed

+17
-21
lines changed

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@ extern int path_inside_repo(const char *prefix, const char *path);
400400
extern int set_git_dir_init(const char *git_dir, const char *real_git_dir, int);
401401
extern int init_db(const char *template_dir, unsigned int flags);
402402

403+
extern void sanitize_stdfds(void);
404+
403405
#define alloc_nr(x) (((x)+16)*3/2)
404406

405407
/*

daemon.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,18 +1047,6 @@ static int service_loop(struct socketlist *socklist)
10471047
}
10481048
}
10491049

1050-
/* if any standard file descriptor is missing open it to /dev/null */
1051-
static void sanitize_stdfds(void)
1052-
{
1053-
int fd = open("/dev/null", O_RDWR, 0);
1054-
while (fd != -1 && fd < 2)
1055-
fd = dup(fd);
1056-
if (fd == -1)
1057-
die_errno("open /dev/null or dup failed");
1058-
if (fd > 2)
1059-
close(fd);
1060-
}
1061-
10621050
#ifdef NO_POSIX_GOODIES
10631051

10641052
struct credentials;

setup.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,3 +872,15 @@ const char *resolve_gitdir(const char *suspect)
872872
return suspect;
873873
return read_gitfile(suspect);
874874
}
875+
876+
/* if any standard file descriptor is missing open it to /dev/null */
877+
void sanitize_stdfds(void)
878+
{
879+
int fd = open("/dev/null", O_RDWR, 0);
880+
while (fd != -1 && fd < 2)
881+
fd = dup(fd);
882+
if (fd == -1)
883+
die_errno("open /dev/null or dup failed");
884+
if (fd > 2)
885+
close(fd);
886+
}

shell.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ int main(int argc, char **argv)
134134
char *prog;
135135
const char **user_argv;
136136
struct commands *cmd;
137-
int devnull_fd;
138137
int count;
139138

140139
git_setup_gettext();
@@ -143,15 +142,10 @@ int main(int argc, char **argv)
143142

144143
/*
145144
* Always open file descriptors 0/1/2 to avoid clobbering files
146-
* in die(). It also avoids not messing up when the pipes are
147-
* dup'ed onto stdin/stdout/stderr in the child processes we spawn.
145+
* in die(). It also avoids messing up when the pipes are dup'ed
146+
* onto stdin/stdout/stderr in the child processes we spawn.
148147
*/
149-
devnull_fd = open("/dev/null", O_RDWR);
150-
while (devnull_fd >= 0 && devnull_fd <= 2)
151-
devnull_fd = dup(devnull_fd);
152-
if (devnull_fd == -1)
153-
die_errno("opening /dev/null failed");
154-
close (devnull_fd);
148+
sanitize_stdfds();
155149

156150
/*
157151
* Special hack to pretend to be a CVS server

0 commit comments

Comments
 (0)