[Python-Dev] PEP 433: Add cloexec argument to functions creating file descriptors

Victor Stinner victor.stinner at gmail.com
Fri Jan 18 23:16:17 CET 2013


2013/1/13 Charles-François Natali <cf.natali at gmail.com>:
>> .. note::
>>    OpenBSD older 5.2 does not close the file descriptor with
>>    close-on-exec flag set if ``fork()`` is used before ``exec()``, but
>>    it works correctly if ``exec()`` is called without ``fork()``.
>
> That would be *really* surprising, are your sure your test case is correct?
> Otherwise it could be a compilation issue, because I simply can't
> believe OpenBSD would ignore the close-on-exec flag.

I didn't write a C program yet, but you can test the folllowing Python
script. On OpenBSD 4.9 it writes "OS BUG !!!".

--
USE_FORK = True

import fcntl, os, sys

fd = os.open("/etc/passwd", os.O_RDONLY)
flags = fcntl.fcntl(fd, fcntl.F_GETFD)
flags |= fcntl.FD_CLOEXEC
fcntl.fcntl(fd, fcntl.F_SETFD, flags)

code = """
import os, sys
fd = int(sys.argv[1])
try:
    os.fstat(fd)
except OSError:
    print("fd %s closed by exec (FD_CLOEXEC works)" % fd)
else:
    print("fd %s not closed by exec: FD_CLOEXEC doesn't work, OS BUG!!!" % fd)
"""

args = [sys.executable, '-c', code, str(fd)]
if USE_FORK:
    pid = os.fork()
    if pid:
        os.waitpid(pid, 0)
        sys.exit(0)

os.execv(args[0], args)
--

It works with USE_FORKS = False.

Victor


More information about the Python-Dev mailing list