OpenSolaris

Printable Version Enter a New Search
Bug ID 6807216
Synopsis posix_spawn() should provide a 'closefrom' file action
State 10-Fix Delivered (Fix available in build)
Category:Subcategory library:libc
Keywords
Responsible Engineer Roger Faulkner
Reported Against s10_52
Duplicate Of
Introduced In
Commit to Fix snv_110
Fixed In snv_110
Release Fixed solaris_nevada(snv_110)
Related Bugs 4987112 , 6808260 , 6808312
Submit Date 18-February-2009
Last Update Date 11-March-2009
Description
The posix_spawn() interface has a problem with MT-safeness,
complained about on comp.unix.programmer and other places:

One thread of a multithreaded process may want to create
a child process/program using posix_spawn() and it wants
to supply a list of file descriptors to be closed before
the new program begins execution (by one or more calls
to posix_spawn_file_actions_addclose() prior to the call
to posix_spawn().

However, between the call to posix_spawn_file_actions_addclose()
and the call to posix_spawn(), some other thread in the process
may close the specified file descriptor.

The call to posix_spawn() will then fail with EBADF because the
file-to-be-closed is not currently open in the parent process.

This is neither useful nor rational.  The file is already
closed, so another close() (returning EBADF) should be
considered to be a no-operation.

The POSIX SUSv3 specification states:
    if posix_spawn() or posix_spawnp() fails for any
    of the reasons that would cause close(), ... to fail,
    an error value shall be returned as described by close() ...

It doesn't say that posix_spawn() *must* fail if it is
asked to close a file descriptor that is not open.
It says that *if* it fails due to a failing close(), it will
return the error value as described for close().

The implementation can legally choose for posix_spawn()
*not*  to fail in this case.

In addition, many if not most programs expect to be started
with only file descriptors 0, 1, and 2 open (STDIN, STDOUT,
STDERR).

It would be useful to provide a posix_spawn() file action
interface that provides the utility of the Solaris interface:

   void closefrom(int lowfd);

that closes all open file descriptors greater than or equal
to lowfd.  This is the purpose of the new interface:

  int posix_spawn_file_actions_addclosefrom_np(
       posix_spawn_file_actions_t *file_actions,
       int lowfiledes);
Work Around
N/A
Comments
See the test case in the attachments: spawn_test.c

This tests the new posix_spawn_file_actions_addclosefrom_np()
interface, as well as the new behavior of not failing if the file
to be closed is already closed.