From: Jason Stubbs <jstubbs@gentoo.org>
To: gentoo-portage-dev@lists.gentoo.org
Subject: Re: [gentoo-portage-dev] [3/7] portage_exec cleanups
Date: Sun, 30 Oct 2005 00:06:10 +0900 [thread overview]
Message-ID: <200510300006.11024.jstubbs@gentoo.org> (raw)
In-Reply-To: <20051029142207.GL24883@nightcrawler>
[-- Attachment #1: Type: text/plain, Size: 2534 bytes --]
On Saturday 29 October 2005 23:22, Brian Harring wrote:
> On Sat, Oct 29, 2005 at 07:49:49PM +0900, Jason Stubbs wrote:
> > This patch simplifies the moving around and closing of file descriptors
> > when setting up the pipe. The addition of fd_unused to the parameter list
> > is in preparation of a later patch.
> >
> > +++ portage_exec.py 2005-10-29 19:07:21.000000000 +0900
> > - # this may look ugly, but basically it moves file descriptors around
> > to ensure no - # handles that are needed are accidentally closed during
> > the final dup2 calls. - trg_fd=[]
> > - if type(fd_pipes)==types.DictType:
> > - src_fd=[]
> > - k=fd_pipes.keys()
> > - k.sort()
> > - for x in k:
> > - trg_fd.append(x)
> > - src_fd.append(fd_pipes[x])
> > - for x in range(0,len(trg_fd)):
> > - if trg_fd[x] == src_fd[x]:
> > - continue
> > - if trg_fd[x] in src_fd[x+1:]:
> > - new=os.dup2(trg_fd[x],max(src_fd) + 1)
> > - os.close(trg_fd[x])
> > - try:
> > - while True:
> > - src_fd[s.index(trg_fd[x])]=new
"s" doesn't exist. Whatever this code was intending to do, it's not doing it.
> > - except SystemExit, e:
> > - raise
> > - except:
> > - pass
> > - for x in range(0,len(trg_fd)):
> > - if trg_fd[x] != src_fd[x]:
> > - os.dup2(src_fd[x], trg_fd[x])
> > - else:
> > - trg_fd=[0,1,2]
>
> Above *does* need reworking, knew that when I wrote it- but the reason
> it's so damn ugly is to cover against dup2 calls stomping handles that
> are needed. Must be a collect, then re-arrange algo, not single pass
>
> fd_pipes = [1:2, 2:1]
>
> > + for x in fd_pipes:
> > + if x != fd_pipes[x]:
> > + os.dup2(fd_pipes[x], x)
> > + if fd_pipes[x] not in fd_pipes and fd_pipes[x] not in fd_unused:
> > + fd_unused.append(fd_pipes[x])
> > + for x in fd_unused:
> > + os.close(x)
>
> Replacement code will fail with the simple stdout <-> stderr switch.
> dup2 will set the actual fd 2 handle to fd 1, but *prior* to
> duplicating fd 2 to a throw away handle so it remains open, end result
> is that fd 2 is now fd 1, so the 2 -> 1 move does nothing.
>
> Code above will result in 1 == 1, 2 == 1, rather then 1 == 2, 2 == 1
>
> Which is a regression, although existing code _probably_ won't choke
> on it- the protection I added made it robust so that it handles the
> corner case without tripping up any dev writing the code, rather then
> silently tieing the fd's together.
Yep, missed that case. Respun and left the fd_unused for later.
--
Jason Stubbs
[-- Attachment #2: 03-fd_pipes-switching-take2.patch --]
[-- Type: text/x-diff, Size: 1721 bytes --]
--- portage_exec.py.orig 2005-10-30 00:04:12.000000000 +0900
+++ portage_exec.py 2005-10-30 00:03:49.000000000 +0900
@@ -76,7 +76,10 @@
myc = find_binary(myc)
if myc == None:
return None
-
+
+ if fd_pipes is None:
+ fd_pipes = {0:0, 1:1, 2:2}
+
mypid=[]
if logfile:
pr,pw=os.pipe()
@@ -88,9 +91,6 @@
return (retval >> 8) # exit code
else:
return ((retval & 0xff) << 8) # signal
- if not fd_pipes:
- fd_pipes={}
- fd_pipes[0] = 0
fd_pipes[1]=pw
fd_pipes[2]=pw
@@ -100,34 +100,16 @@
myargs.extend(mycommand[1:])
mypid.append(os.fork())
if mypid[-1] == 0:
- # this may look ugly, but basically it moves file descriptors around to ensure no
- # handles that are needed are accidentally closed during the final dup2 calls.
- trg_fd=[]
- if type(fd_pipes)==types.DictType:
- src_fd=[]
- k=fd_pipes.keys()
- k.sort()
- for x in k:
- trg_fd.append(x)
- src_fd.append(fd_pipes[x])
- for x in range(0,len(trg_fd)):
- if trg_fd[x] == src_fd[x]:
- continue
- if trg_fd[x] in src_fd[x+1:]:
- new=os.dup2(trg_fd[x],max(src_fd) + 1)
- os.close(trg_fd[x])
- try:
- while True:
- src_fd[s.index(trg_fd[x])]=new
- except SystemExit, e:
- raise
- except:
- pass
- for x in range(0,len(trg_fd)):
- if trg_fd[x] != src_fd[x]:
- os.dup2(src_fd[x], trg_fd[x])
- else:
- trg_fd=[0,1,2]
+ my_pipes = {}
+ close_pipes = {}
+ for x in fd_pipes:
+ my_pipes[x] = os.dup(fd_pipes[x])
+ close_pipes[fd_pipes[x]] = True
+ for x in close_pipes:
+ os.close(x)
+ for x in my_pipes:
+ os.dup2(my_pipes[x], x)
+ os.close(my_pipes[x])
for x in range(0,max_fd_limit):
if x not in trg_fd:
try:
next prev parent reply other threads:[~2005-10-29 15:06 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-10-23 6:45 [gentoo-portage-dev] [Bug 104705] emerge doesn't print complete error message Jason Stubbs
2005-10-29 10:34 ` [gentoo-portage-dev] [0/7] portage_exec cleanups (WAS: [Bug 104705] emerge doesn't print complete error message) Jason Stubbs
2005-10-29 10:41 ` [gentoo-portage-dev] [1/7] portage_exec cleanups Jason Stubbs
2005-10-29 10:44 ` [gentoo-portage-dev] [2/7] " Jason Stubbs
2005-10-29 10:49 ` [gentoo-portage-dev] [3/7] " Jason Stubbs
2005-10-29 14:22 ` Brian Harring
2005-10-29 15:06 ` Jason Stubbs [this message]
2005-10-29 15:17 ` Brian Harring
2005-10-29 10:52 ` [gentoo-portage-dev] [4/7] " Jason Stubbs
2005-10-29 11:03 ` Jason Stubbs
2005-10-29 10:56 ` [gentoo-portage-dev] [5/7] " Jason Stubbs
2005-10-29 15:25 ` Jason Stubbs
2005-10-29 16:04 ` Brian Harring
2005-10-29 16:40 ` Jason Stubbs
2005-10-29 17:17 ` Jason Stubbs
2005-10-29 17:17 ` Brian Harring
2005-10-29 17:28 ` Jason Stubbs
2005-10-29 17:20 ` Jason Stubbs
2005-10-29 17:28 ` Brian Harring
2005-10-29 10:57 ` [gentoo-portage-dev] [6/7] " Jason Stubbs
2005-10-29 11:01 ` [gentoo-portage-dev] [7/7] " Jason Stubbs
2005-10-29 17:32 ` [gentoo-portage-dev] [0/7] portage_exec cleanups (WAS: [Bug 104705] emerge doesn't print complete error message) Jason Stubbs
2005-10-29 17:38 ` Jason Stubbs
2005-10-30 7:03 ` Jason Stubbs
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=200510300006.11024.jstubbs@gentoo.org \
--to=jstubbs@gentoo.org \
--cc=gentoo-portage-dev@lists.gentoo.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox