In perl.git, the branch blead has been updated

<http://perl5.git.perl.org/perl.git/commitdiff/131d45a96c910d0fe46597ab156a35837879bf9c?hp=679f623f366bc81022b7c77de4bc4302828303c6>

- Log -----------------------------------------------------------------
commit 131d45a96c910d0fe46597ab156a35837879bf9c
Author: Jarkko Hietaniemi <[email protected]>
Date:   Wed Aug 26 08:55:37 2015 -0400

    Explicitly use and check for FD_CLOEXEC.
    
    This may break places which have the FD_CLOEXEC functionality
    but do not have the FD_CLOEXEC define.
    
    In any case, using a boolean for the F_SETFD flag is icky.
    Using an explicit 1 is also dubious.

M       perl.c
M       pp_sys.c
M       toke.c
M       util.c

commit 5798d63101f7b13bd5aaf5a5bf429a8e08991016
Author: Jarkko Hietaniemi <[email protected]>
Date:   Wed Aug 26 08:53:45 2015 -0400

    Note that the all the fd flags are saved/restored.
    
    FD_CLOEXEC is currently usually the only defined fd flag
    for F_GETFD/F_SETFD, but let's not assume that.

M       doio.c
-----------------------------------------------------------------------

Summary of changes:
 doio.c   | 12 +++++++-----
 perl.c   |  4 ++--
 pp_sys.c | 24 +++++++++++++-----------
 toke.c   |  6 ++++--
 util.c   |  2 +-
 5 files changed, 27 insertions(+), 21 deletions(-)

diff --git a/doio.c b/doio.c
index 39e5ce7..1b1e951 100644
--- a/doio.c
+++ b/doio.c
@@ -741,9 +741,10 @@ S_openn_cleanup(pTHX_ GV *gv, IO *io, PerlIO *fp, char 
*mode, const char *oname,
                 int ofd = PerlIO_fileno(fp);
                 int dupfd = ofd >= 0 ? PerlLIO_dup(ofd) : -1;
 #if defined(HAS_FCNTL) && defined(F_SETFD)
-               /* Assume if we have F_SETFD we have F_GETFD */
-                int coe = ofd >= 0 ? fcntl(ofd, F_GETFD) : -1;
-                if (coe < 0) {
+               /* Assume if we have F_SETFD we have F_GETFD. */
+                /* Get a copy of all the fd flags. */
+                int fd_flags = ofd >= 0 ? fcntl(ofd, F_GETFD) : -1;
+                if (fd_flags < 0) {
                     if (dupfd >= 0)
                         PerlLIO_close(dupfd);
                     goto say_false;
@@ -757,8 +758,9 @@ S_openn_cleanup(pTHX_ GV *gv, IO *io, PerlIO *fp, char 
*mode, const char *oname,
                 PerlIO_close(fp);
                 PerlLIO_dup2(dupfd, ofd);
 #if defined(HAS_FCNTL) && defined(F_SETFD)
-               /* The dup trick has lost close-on-exec on ofd */
-               fcntl(ofd,F_SETFD, coe);
+               /* The dup trick has lost close-on-exec on ofd,
+                 * and possibly any other flags, so restore them. */
+               fcntl(ofd,F_SETFD, fd_flags);
 #endif
                 PerlLIO_close(dupfd);
            }
diff --git a/perl.c b/perl.c
index 303e1f2..64cc395 100644
--- a/perl.c
+++ b/perl.c
@@ -3796,10 +3796,10 @@ S_open_script(pTHX_ const char *scriptname, bool 
dosearch, bool *suidscript)
                    CopFILE(PL_curcop), Strerror(errno));
     }
     fd = PerlIO_fileno(rsfp);
-#if defined(HAS_FCNTL) && defined(F_SETFD)
+#if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC)
     if (fd >= 0) {
         /* ensure close-on-exec */
-        if (fcntl(fd, F_SETFD, 1) < 0) {
+        if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) {
             Perl_croak(aTHX_ "Can't open perl script \"%s\": %s\n",
                        CopFILE(PL_curcop), Strerror(errno));
         }
diff --git a/pp_sys.c b/pp_sys.c
index dc1b3ce..658dee1 100644
--- a/pp_sys.c
+++ b/pp_sys.c
@@ -708,10 +708,10 @@ PP(pp_pipe_op)
            PerlLIO_close(fd[1]);
        goto badexit;
     }
-#if defined(HAS_FCNTL) && defined(F_SETFD)
+#if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC)
     /* ensure close-on-exec */
-    if ((fcntl(fd[0], F_SETFD,fd[0] > PL_maxsysfd) < 0) ||
-        (fcntl(fd[1], F_SETFD,fd[1] > PL_maxsysfd) < 0))
+    if ((fd[0] > PL_maxsysfd && fcntl(fd[0], F_SETFD, FD_CLOEXEC) < 0) ||
+        (fd[1] > PL_maxsysfd && fcntl(fd[1], F_SETFD, FD_CLOEXEC) < 0))
         goto badexit;
 #endif
     RETPUSHYES;
@@ -2496,8 +2496,9 @@ PP(pp_socket)
        if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
        RETPUSHUNDEF;
     }
-#if defined(HAS_FCNTL) && defined(F_SETFD)
-    if (fcntl(fd, F_SETFD, fd > PL_maxsysfd) < 0)      /* ensure close-on-exec 
*/
+#if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC)
+    /* ensure close-on-exec */
+    if (fd > PL_maxsysfd && fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
        RETPUSHUNDEF;
 #endif
 
@@ -2542,10 +2543,10 @@ PP(pp_sockpair)
        if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
        RETPUSHUNDEF;
     }
-#if defined(HAS_FCNTL) && defined(F_SETFD)
+#if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC)
     /* ensure close-on-exec */
-    if ((fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd) < 0) ||
-        (fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd) < 0))
+    if ((fd[0] > PL_maxsysfd && fcntl(fd[0], F_SETFD, FD_CLOEXEC) < 0) ||
+        (fd[1] > PL_maxsysfd && fcntl(fd[1], F_SETFD, FD_CLOEXEC) < 0))
        RETPUSHUNDEF;
 #endif
 
@@ -2659,8 +2660,9 @@ PP(pp_accept)
        if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
        goto badexit;
     }
-#if defined(HAS_FCNTL) && defined(F_SETFD)
-    if (fcntl(fd, F_SETFD, fd > PL_maxsysfd) < 0)      /* ensure close-on-exec 
*/
+#if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC)
+    /* ensure close-on-exec */
+    if (fd > PL_maxsysfd && fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
         goto badexit;
 #endif
 
@@ -4386,7 +4388,7 @@ PP(pp_system)
 #endif
        if (did_pipes) {
            PerlLIO_close(pp[0]);
-#if defined(HAS_FCNTL) && defined(F_SETFD)
+#if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC)
            if (fcntl(pp[1], F_SETFD, FD_CLOEXEC) < 0)
                 RETPUSHUNDEF;
 #endif
diff --git a/toke.c b/toke.c
index 7a0f1b6..d30bfe6 100644
--- a/toke.c
+++ b/toke.c
@@ -7039,10 +7039,12 @@ Perl_yylex(pTHX)
                if (!GvIO(gv))
                    GvIOp(gv) = newIO();
                IoIFP(GvIOp(gv)) = PL_rsfp;
-#if defined(HAS_FCNTL) && defined(F_SETFD)
+#if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC)
                {
                    const int fd = PerlIO_fileno(PL_rsfp);
-                   fcntl(fd,F_SETFD,fd >= 3);
+                    if (fd >= 3) {
+                        fcntl(fd,F_SETFD, FD_CLOEXEC);
+                    }
                }
 #endif
                /* Mark this internal pseudo-handle as clean */
diff --git a/util.c b/util.c
index e357379..98c2cd9 100644
--- a/util.c
+++ b/util.c
@@ -2373,7 +2373,7 @@ Perl_my_popen_list(pTHX_ const char *mode, int n, SV 
**args)
        /* Close parent's end of error status pipe (if any) */
        if (did_pipes) {
            PerlLIO_close(pp[0]);
-#if defined(HAS_FCNTL) && defined(F_SETFD)
+#if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC)
            /* Close error pipe automatically if exec works */
            if (fcntl(pp[1], F_SETFD, FD_CLOEXEC) < 0)
                 return NULL;

--
Perl5 Master Repository

Reply via email to