Always mark pipe in pipe-open as inherit-on-exec
authorLeon Timmermans <fawaka@gmail.com>
Sat, 15 Dec 2018 18:08:41 +0000 (19:08 +0100)
committerLeon Timmermans <fawaka@gmail.com>
Sat, 15 Dec 2018 21:25:53 +0000 (22:25 +0100)
Since 2cdf406a a lot of file descriptors are opened close-on-exec,
including the pipe that is passed to the child process in a pipe-open.
This is usually fine because a dup2 follows to rename that handle to
stdin/stdout that will set the inherit-on-exec. However, if the pipe
descriptor already has the right value, for example because stdin was
closed, then no dup2 happens and hence it's still marked as
close-on-exec right when we want to perform an exec.

This patch explicitly marks such a handle as inherit-on-exec, to ensure
it will be open for the child process.

util.c

diff --git a/util.c b/util.c
index a9bf9b8..99bf4ae 100644 (file)
--- a/util.c
+++ b/util.c
@@ -2469,8 +2469,10 @@ Perl_my_popen(pTHX_ const char *cmd, const char *mode)
            if (p[THAT] != (*mode == 'r'))      /* if dup2() didn't close it */
                PerlLIO_close(p[THAT]);
        }
-       else
+       else {
+           setfd_cloexec_or_inhexec_by_sysfdness(p[THIS]);
            PerlLIO_close(p[THAT]);
+       }
 #ifndef OS2
        if (doexec) {
 #if !defined(HAS_FCNTL) || !defined(F_SETFD)