in reply to Leaking a file descriptor into a child to use with /proc/self/fd/3

Might also want to look at $^F (see perlvar) and/or explicitly clearing CLOEXEC on the descriptor with fcntl.

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^2: Leaking a file descriptor into a child to use with /proc/self/fd/3
by ewheeler (Novice) on Feb 26, 2020 at 23:46 UTC

    Perfect, that fixed the problem. I did not realize Perl closed FDs on exec (or maybe that is an OS thing).

      The Perl process essentially goes away by replacing itself when you exec. So, it might feel surprising but it seems sensible/intuitive that any FDs opened in Perl would close.

      This worked:

      open(my $in, '/etc/passwd'); my $flags = fcntl($in, F_GETFD, 0); fcntl($in, F_SETFD, $flags & (~FD_CLOEXEC)) or die "Can't set flags: $ +!\n"; if (!fork()) { exec("grep", "root", "/proc/self/fd/" . fileno($in)); } close($in);