summaryrefslogtreecommitdiffstats
path: root/spawn.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2019-07-05 19:13:59 -0400
committerTavian Barnes <tavianator@tavianator.com>2019-07-05 19:13:59 -0400
commit1330ca3b575de9fc9877c1867897ebfcf7c43386 (patch)
treefbacb07828025d760ea4069ac1be30fa6fe68aa0 /spawn.c
parent71a1b65ba926ff9c6db3cb671580b5491db0bd97 (diff)
downloadbfs-1330ca3b575de9fc9877c1867897ebfcf7c43386.tar.xz
spawn: Fix moving the pipe out of the way
The old code could dup() the pipe from in_fd to out_fd, for example, and neglected to keep it CLOEXEC.
Diffstat (limited to 'spawn.c')
-rw-r--r--spawn.c12
1 files changed, 9 insertions, 3 deletions
diff --git a/spawn.c b/spawn.c
index 18ddd72..bcd2c08 100644
--- a/spawn.c
+++ b/spawn.c
@@ -147,9 +147,15 @@ static void bfs_spawn_exec(const char *exe, const struct bfs_spawn *ctx, char **
close(pipefd[0]);
for (const struct bfs_spawn_action *action = actions; action; action = action->next) {
- // Move the error-reporting pipe out of the way if necessary
- if (action->in_fd == pipefd[1] || action->out_fd == pipefd[1]) {
- int fd = dup(pipefd[1]);
+ // Pretend the error-reporting pipe doesn't exist
+ if (action->in_fd == pipefd[1]) {
+ errno = EBADF;
+ goto fail;
+ }
+
+ // Move the pipe out of the way if necessary
+ if (action->out_fd == pipefd[1]) {
+ int fd = dup_cloexec(pipefd[1]);
if (fd < 0) {
goto fail;
}