diff --git a/src/stdio/__stdio_close.c b/src/stdio/__stdio_close.c index 30291328..fd46b09f 100644 --- a/src/stdio/__stdio_close.c +++ b/src/stdio/__stdio_close.c @@ -10,5 +10,7 @@ weak_alias(dummy, __aio_close); int __stdio_close(FILE *f) { - return syscall(SYS_close, __aio_close(f->fd)); + int fd = f->fd; + f->fd = -1; + return syscall(SYS_close, __aio_close(fd)); } diff --git a/src/stdio/popen.c b/src/stdio/popen.c index 3ec83394..9a7388f3 100644 --- a/src/stdio/popen.c +++ b/src/stdio/popen.c @@ -34,9 +34,14 @@ FILE *popen(const char *cmd, const char *mode) e = ENOMEM; if (!posix_spawn_file_actions_init(&fa)) { - for (FILE *l = *__ofl_lock(); l; l=l->next) - if (l->pipe_pid && posix_spawn_file_actions_addclose(&fa, l->fd)) + for (FILE *l = *__ofl_lock(); l; l=l->next) { + if (!l->pipe_pid) continue; + FLOCK(l); + int t = l->fd; + FUNLOCK(l); + if (t>=0 && posix_spawn_file_actions_addclose(&fa, t)) goto fail; + } if (!posix_spawn_file_actions_adddup2(&fa, p[1-op], 1-op)) { if (!(e = posix_spawn(&pid, "/bin/sh", &fa, 0, (char *[]){ "sh", "-c", (char *)cmd, 0 }, __environ))) {