Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions ptrace/debugger/child.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from os import (
fork, execvp, execvpe, waitpid,
close, dup2, pipe,
read, write, devnull, sysconf)
read, write, devnull, sysconf, listdir)
from os.path import isdir
from sys import exc_info
from traceback import format_exception
from ptrace.os_tools import RUNNING_WINDOWS
Expand Down Expand Up @@ -87,6 +88,27 @@ def _createParent(pid, errpipe_read):
raise child_exception


def _closeFdsExcept(fds, ignore_fds):
for fd in fds:
if fd not in ignore_fds:
try:
close(fd)
except OSError:
pass


def _closeFds(ignore_fds):
for path in ['/proc/self/fd', '/dev/fd']:
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/dev/fd is a symlink to /proc/self/fd. Is it really useful to test it?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/dev/fd is symlink in Linux.
Other systems like BSD and Mac have /dev/fd.
On FreeBSD until fdescfs is mounted it contains only static descriptors 0, 1, 2 so there will be needed another test, otherwise it wont close any descriptor.
I will look at python subprocess.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try:
if isdir(path):
_closeFdsExcept([int(i) for i in listdir(path)], ignore_fds)
return
except OSError:
pass

_closeFdsExcept(range(0, MAXFD), ignore_fds)


def _createChild(arguments, no_stdout, env, errpipe_write):
# Child code
try:
Expand All @@ -95,13 +117,8 @@ def _createChild(arguments, no_stdout, env, errpipe_write):
raise ChildError(str(err))

# Close all files except 0, 1, 2 and errpipe_write
for fd in range(3, MAXFD):
if fd == errpipe_write:
continue
try:
close(fd)
except OSError:
pass
_closeFds([0, 1, 2, errpipe_write])

try:
_execChild(arguments, no_stdout, env)
except:
Expand Down