bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gawk] gawk and inherited file descriptors


From: Stephane Chazelas
Subject: Re: [bug-gawk] gawk and inherited file descriptors
Date: Mon, 2 Nov 2015 15:33:12 +0000
User-agent: Mutt/1.5.21 (2010-09-15)

2015-11-02 06:53:18 -0700, address@hidden:
> Hi.
> 
> Thanks for your report.
> 
> Howard Bryden <address@hidden> wrote:
> 
> > Guys,
> >
> > I'm using gawk 4.1.3 under HP-UX 11.31 (POSIX shell) and I wonder if
> > I'm missing something.
> 
> Indeed, you are. :-)
> 
> > The shell fragment
> >
> > exec 9<&1
> 
> This only changes the shell's file descriptors, not those of commands
> it runs.

Note that that is only true of ksh (which do a fcntl(9, F_SETFD,
FD_CLOEXEC) for that fd), not of any other shell that I know
(bash, zsh, Bourne, dash, yash) which don't do that and pass the
fd along.

> Additionally, you should be using 9>&1 for output.

True from a cosmetic point of view though that makes not
difference as a dup2() or equivalent is done which doesn't care
about the mode the fd was open.

The test case the OP is showing probably works because HP/UX's
sh (based on ksh) doesn't reexecute itself when invoking
she-bang-less scripts (so no exec means FD_CLOEXEC is not
triggered).

> The following works for me on Linux:
> 
>       $ gawk 'BEGIN { print "hi" > "/dev/fd/9" }' 9>&1
>       hi

Yes, ksh doesn't set the FD_CLOEXEC flag on those, and actually,
you can work around those FD_CLOEXEC done upon exec, by doing:

exec 9>&1

cmd1 # 9 closed for cmd1
cmd2 9>&9 # 9 not closed for cmd1

~$ ksh -c 'exec 9>&1; ls /proc/self/fd/; :'
0  1  2  3
~$ ksh -c 'exec 9>&1; ls /proc/self/fd 9>&9; :'
0  1  2  3  9

-- 
Stephane
> 
> I suspect that it will also work for you on HP/UX.
> 
> Thanks,
> 
> Arnold
> 
> 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]