From 79d832988eaa51f564eca913dec112e4df33593e Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Tue, 15 Jun 2021 21:19:01 -0700 Subject: subprocesses: don't bother saving descriptors. * stream.c (fds_subst_nosave): New static function. (fds_clobber): New static function. Like fds_swizzle, without the saving. (open_subprocess, run): Use fds_clobber instead of fds_swizzle in the child process. It makes no sense to use fds_swizzle, which saves duplicates of the old descriptors, if fds_restore is not going to be called. --- stream.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/stream.c b/stream.c index 7316972e..5a303685 100644 --- a/stream.c +++ b/stream.c @@ -4279,6 +4279,13 @@ static int fds_subst(int fd_sub, int fd_std, val self) } } +static void fds_subst_nosave(int fd_sub, int fd_std) +{ + if (fd_sub == fd_std) + return; + dup2(fd_sub, fd_std); +} + static void fds_prepare(struct save_fds *fds, int flags, val self) { if ((flags & FDS_IN) != 0) @@ -4321,6 +4328,18 @@ static void fds_restore(struct save_fds *fds) } } +static void fds_clobber(struct save_fds *fds, int flags) +{ + if ((flags & FDS_IN) != 0) + fds_subst_nosave(fds->subin, STDIN_FILENO); + + if ((flags & FDS_OUT) != 0) + fds_subst_nosave(fds->subout, STDOUT_FILENO); + + if ((flags & FDS_ERR) != 0) + fds_subst_nosave(fds->suberr, STDERR_FILENO); +} + val open_command(val path, val mode_str) { val self = lit("open-command"); @@ -4418,7 +4437,7 @@ static val open_subprocess(val name, val mode_str, val args, val fun) } if (pid == 0) { - fds_swizzle(&sfds, fds_flags, self); + fds_clobber(&sfds, fds_flags); if (input) { dup2(fd[1], STDOUT_FILENO); @@ -4726,7 +4745,7 @@ static val run(val name, val args) } if (pid == 0) { - fds_swizzle(&sfds, FDS_IN | FDS_OUT | FDS_ERR, self); + fds_clobber(&sfds, FDS_IN | FDS_OUT | FDS_ERR); execvp(argv[0], argv); _exit(errno); } else { -- cgit v1.2.3