Skip to content

Commit db015a2

Browse files
bmwillgitster
authored andcommitted
run-command: don't die in child when duping /dev/null
Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent ae25394 commit db015a2

1 file changed

Lines changed: 13 additions & 15 deletions

File tree

run-command.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,6 @@ static inline void close_pair(int fd[2])
117117
close(fd[1]);
118118
}
119119

120-
#ifndef GIT_WINDOWS_NATIVE
121-
static inline void dup_devnull(int to)
122-
{
123-
int fd = open("/dev/null", O_RDWR);
124-
if (fd < 0)
125-
die_errno(_("open /dev/null failed"));
126-
if (dup2(fd, to) < 0)
127-
die_errno(_("dup2(%d,%d) failed"), fd, to);
128-
close(fd);
129-
}
130-
#endif
131-
132120
static char *locate_in_PATH(const char *file)
133121
{
134122
const char *p = getenv("PATH");
@@ -444,12 +432,20 @@ int start_command(struct child_process *cmd)
444432
#ifndef GIT_WINDOWS_NATIVE
445433
{
446434
int notify_pipe[2];
435+
int null_fd = -1;
447436
char **childenv;
448437
struct argv_array argv = ARGV_ARRAY_INIT;
449438

450439
if (pipe(notify_pipe))
451440
notify_pipe[0] = notify_pipe[1] = -1;
452441

442+
if (cmd->no_stdin || cmd->no_stdout || cmd->no_stderr) {
443+
null_fd = open("/dev/null", O_RDWR | O_CLOEXEC);
444+
if (null_fd < 0)
445+
die_errno(_("open /dev/null failed"));
446+
set_cloexec(null_fd);
447+
}
448+
453449
prepare_cmd(&argv, cmd);
454450
childenv = prep_childenv(cmd->env);
455451

@@ -473,7 +469,7 @@ int start_command(struct child_process *cmd)
473469
atexit(notify_parent);
474470

475471
if (cmd->no_stdin)
476-
dup_devnull(0);
472+
dup2(null_fd, 0);
477473
else if (need_in) {
478474
dup2(fdin[0], 0);
479475
close_pair(fdin);
@@ -483,7 +479,7 @@ int start_command(struct child_process *cmd)
483479
}
484480

485481
if (cmd->no_stderr)
486-
dup_devnull(2);
482+
dup2(null_fd, 2);
487483
else if (need_err) {
488484
dup2(fderr[1], 2);
489485
close_pair(fderr);
@@ -493,7 +489,7 @@ int start_command(struct child_process *cmd)
493489
}
494490

495491
if (cmd->no_stdout)
496-
dup_devnull(1);
492+
dup2(null_fd, 1);
497493
else if (cmd->stdout_to_stderr)
498494
dup2(2, 1);
499495
else if (need_out) {
@@ -553,6 +549,8 @@ int start_command(struct child_process *cmd)
553549
}
554550
close(notify_pipe[0]);
555551

552+
if (null_fd >= 0)
553+
close(null_fd);
556554
argv_array_clear(&argv);
557555
free(childenv);
558556
}

0 commit comments

Comments
 (0)