Phobos: Posix hands down open files to sub processes.

Marco Leise Marco.Leise at gmx.de
Sun Mar 10 22:24:13 PDT 2013


I've taken a look around at how other cross-platform languages
go about this...

OpenJDK:

> All file descriptors that are opened in the JVM and not
> specifically destined for a subprocess should have the
> close-on-exec flag set.  If we don't set it, then careless 3rd
> party native code might fork and exec without closing all
> appropriate file descriptors (e.g. as we do in closeDescriptors in
> UNIXProcess.c), and this in turn might:
>
> - cause end-of-file to fail to be detected on some file
>   descriptors, resulting in mysterious hangs, or
>
> - might cause an fopen in the subprocess to fail on a system
>   suffering from bug 1085341.
>
> (Yes, the default setting of the close-on-exec flag is a Unix
> design flaw)

Python:

> def _mkstemp_inner(dir, pre, suf, flags):
>     """Code common to mkstemp, TemporaryFile, and NamedTemporaryFile."""
> 
>     names = _get_candidate_names()
> 
>     for seq in range(TMP_MAX):
>         name = next(names)
>         file = _os.path.join(dir, pre + name + suf)
>         try:
>             fd = _os.open(file, flags, 0o600)
>             _set_cloexec(fd)
>             return (fd, _os.path.abspath(file))
>         except FileExistsError:
>             continue    # try again
> 
>     raise FileExistsError("No usable temporary file name found")

Ruby:

> Ruby sets close-on-exec flags of all file descriptors by default since
> Ruby 2.0.0. So you don’t need to set by yourself. Also, unsetting a
> close-on-exec flag can cause file descriptor leak if another thread
> use fork() and exec() (via system() method for example).
> If you really needs file descriptor inheritance to child process,
> use spawn()‘s argument such as fd=>fd.

Haskell:

I found some discussion here in 2009: http://therning.org/magnus/archives/727
I think they just rely on the C library at the moment for their System.IO,
which in turn doesn't set FD_CLOEXEC. But I'm really bad at reading Haskell :p

Rust:

Uses C stdlib.

-----------------------------------------------------

From this small set of languages it looks like those that have their
own IO implementation and don't rely on the C lib, mostly set
FD_CLOEXEC by default.

Despite the increased maintenance cost I think we should adapt that
behavoir in D as well.

-- 
Marco



More information about the Digitalmars-d mailing list