[qmtest] [PATCH] Mark fd's close-on-exec in qm.executable.
Mark Mitchell
mark at codesourcery.com
Thu Aug 14 08:33:42 UTC 2003
> + def _MakeCloseOnExec(self, fd):
> + """Modifies 'fd' to not be inherited across 'exec'.
"""Prevent 'fd' from being inherited across 'exec'."""
> +
> + UNIX only."""
Needs documentation for 'fd'.
> + inheritable by child processes. On UNIX the fds will always be
> + >= 3 and in close-on-exec mode."""
I'd just say "and not inherited across 'exec'."
> + pipe = os.pipe()
> + for fd in pipe:
> + # Push the fd up above 2, to make sure it won't conflict
> + # with stdin/stdout/stderr.
> + closable = []
> + while fd <= 2:
> + closable.append(fd)
> + fd = os.dup(fd)
> + for old in closable:
> + os.close(old)
> + # And make it close-on-exec.
> + self._MakeCloseOnExec(fd)
> + return pipe
This logic has an inefficiency and a corner-case bug.
The former is that you might as well accumulate closable across both
iterations. In other words, move it outside the loop. That could save
some open/close calls.
The latter is that if there are no new available fds when you call dup,
we will (a) crash, and (b) leak the fds in closable.
--
Mark Mitchell
CodeSourcery, LLC
mark at codesourcery.com
More information about the qmtest
mailing list