summaryrefslogtreecommitdiffstats
path: root/spawn.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2019-07-08 17:08:22 -0400
committerTavian Barnes <tavianator@tavianator.com>2019-07-08 17:08:22 -0400
commit9086b192fb4eaa10bae1bc1a19b8a4559c7f65fa (patch)
treeb44e6cbc7cc1263b55f8c7abf70492944128e3fb /spawn.c
parent1330ca3b575de9fc9877c1867897ebfcf7c43386 (diff)
downloadbfs-9086b192fb4eaa10bae1bc1a19b8a4559c7f65fa.tar.xz
spawn: Actually fix moving the pipe out of the way
We have to check in_fd after out_fd, otherwise the dup() may move it to in_fd.
Diffstat (limited to 'spawn.c')
-rw-r--r--spawn.c14
1 files changed, 7 insertions, 7 deletions
diff --git a/spawn.c b/spawn.c
index bcd2c08..b356c41 100644
--- a/spawn.c
+++ b/spawn.c
@@ -147,13 +147,7 @@ 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) {
- // 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
+ // Move the error-reporting pipe out of the way if necessary...
if (action->out_fd == pipefd[1]) {
int fd = dup_cloexec(pipefd[1]);
if (fd < 0) {
@@ -163,6 +157,12 @@ static void bfs_spawn_exec(const char *exe, const struct bfs_spawn *ctx, char **
pipefd[1] = fd;
}
+ // ... and pretend the pipe doesn't exist
+ if (action->in_fd == pipefd[1]) {
+ errno = EBADF;
+ goto fail;
+ }
+
switch (action->op) {
case BFS_SPAWN_CLOSE:
if (close(action->out_fd) != 0) {