Permalink
Browse files

When redirecting a file, close the descriptor it's opened on immediat…

…ely.

gold/open-fds.sh showed that we had two copies of it open, which doesn't
make sense and isn't the behavior other shells have.

This might fix some bugs running debootstrap, but I haven't verified it
(issue #62).

gold/open-fds: Show descriptor state inside both redirects and
pipelines.
  • Loading branch information...
Andy Chu
Andy Chu committed Jan 12, 2018
1 parent 6456db0 commit 09a8e1a452bc3be43277b465d2d321233dded366
Showing with 39 additions and 11 deletions.
  1. +6 −1 core/process.py
  2. +33 −10 gold/open-fds.sh
View
@@ -124,7 +124,10 @@ def _ApplyRedirect(self, r, waiter):
if not self._PushDup(target_fd, r.fd):
ok = False
self._PushClose(target_fd)
os.close(target_fd) # We already made a copy of it.
# I don't think we need to close(0) because it will be restored from its
# saved position (10), which closes it.
#self._PushClose(r.fd)
elif r.tag == redirect_e.DescRedirect:
if r.op_id == Id.Redir_GreatAnd: # 1>&2
@@ -139,7 +142,9 @@ def _ApplyRedirect(self, r, waiter):
raise NotImplementedError
elif r.tag == redirect_e.HereRedirect:
# NOTE: Do these descriptors have to be moved out of the range 0-9?
read_fd, write_fd = os.pipe()
if not self._PushDup(read_fd, r.fd): # stdin is now the pipe
ok = False
self._PushClose(read_fd)
View
@@ -9,22 +9,45 @@
# POSIX I guess.
# count FDs greater than 10. 0-9 are reserved for scripts.
count_fds() {
count_func() {
local count=0
local reserved=0
{
for path in /proc/$$/fd/*; do
echo $path
count=$((count + 1))
if test $(basename $path) -lt 10; then
reserved=$((reserved + 1))
fi
done
echo "$count FDs open; $reserved are RESERVED (0-9)"
local pid=$$
# Uncomment this to show the FD table of the pipeline process! The parent
# doesn't change!
#local pid=$BASHPID
for path in /proc/$pid/fd/*; do
echo $path
count=$((count + 1))
local fd=$(basename $path)
if test $fd -gt 2 && test $fd -lt 10; then
reserved=$((reserved + 1))
fi
done
ls -l /proc/$pid/fd
echo "$count FDs open; $reserved are RESERVED (3-9)"
}
# What does it look like inside a redirect? _tmp/err.txt must be open.
count_redir() {
{
count_func
} 2> _tmp/err.txt
}
count_pipeline() {
count_func | cat
}
# https://stackoverflow.com/questions/2493642/how-does-a-linux-unix-bash-script-know-its-own-pid
pid() {
echo $$ $BASHPID | cat
}
"$@"

0 comments on commit 09a8e1a

Please sign in to comment.