Subject: Re: bug#8846: coreutils-8.12 on HP-UX 11.31: 3 of 365 tests failed

From: Eric Blake <[email-masked]>
Date: Mon, 13 Jun 2011 09:47:17 -0600

On 06/13/2011 09:37 AM, Jim Meyering wrote:
>> But I think the reason that it invokes a problem is not because of the
>> 1>&$e construct, but because HP-UX opens secondary file descriptors as
>> cloexec or otherwise closing them at some point, so the '/bin/sh k'
>> child process is not inheriting fd 9 from the parent process.
> 
> Ah ha!
> That sounds like a syscall that needs to be gnulib-replaced in bash/zsh.
> Of course, first we'd need to gnulib-enable a shell.

Yep, I've confirmed that both HP-UX and Solaris 10 ksh use CLOEXEC fds
for intermediate fd values created by exec; that POSIX permits (but
fortunately does not require) this behavior, and that autoconf already
documents the pitfall of expecting fds > 2 to be preserved into child
processes:

http://www.gnu.org/software/autoconf/manual/autoconf.html#File-Descriptors

> Don't rely on open file descriptors being open in child processes. In ksh, file descriptors above 2 which are opened using ‘exec n>file’ are closed by a subsequent ‘exec’ (such as that involved in the fork-and-exec which runs a program or script). Thus, using sh, we have:
> 
>      $ cat ./descrips
>      #!/bin/sh -
>      echo hello >&5
>      $ exec 5>t
>      $ ./descrips
>      $ cat t
>      hello
>      $
> 
> But using ksh:
> 
>      $ exec 5>t
>      $ ./descrips
>      hello
>      $ cat t
>      $

On Solaris 10, truss shows this for 'exec 9>&2':

dup(2)                                          = 3
close(9)                                        Err#9 EBADF
fcntl(3, F_DUPFD, 0x00000009)                   = 9
close(3)                                        = 0
fcntl(9, F_SETFD, 0x00000001)                   = 0

>   tusc (11+, freeware)

Likewise on HP-UX 11.31, tusc shows:

dup(2) ................................................... = 3
close(9) ................................................. ERR#9 EBADF
fcntl(3, F_DUPFD, 9) ..................................... = 9
close(3) ................................................. = 0
fcntl(9, F_SETFD, 1) ..................................... = 0

About the only thing I can think of is to pass the details about the
desired redirection as a command-line or env-var setting, to be done by
the script itself, rather than relying on the parent shell (the one in
make) being able to do a redirection that persists into the child (the
one sourcing init.sh).

-- 
Eric Blake   [email-masked]    +1-801-349-2682
Libvirt virtualization library http://libvirt.org