From: Jarkko Hietaniemi Date: Wed, 26 Aug 2015 12:53:45 +0000 (-0400) Subject: Note that the all the fd flags are saved/restored. X-Git-Tag: if-0.0605~145 X-Git-Url: https://perl5.git.perl.org/perl5.git/commitdiff_plain/5798d63101f7b13bd5aaf5a5bf429a8e08991016?ds=sidebyside 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. --- 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); }