EbuildFetcher: close uneeded fds in subprocess
authorZac Medico <zmedico@gentoo.org>
Fri, 22 Oct 2010 10:09:55 +0000 (03:09 -0700)
committerZac Medico <zmedico@gentoo.org>
Fri, 22 Oct 2010 10:09:55 +0000 (03:09 -0700)
pym/_emerge/EbuildFetcher.py
pym/portage/process.py

index b1a10d90010ec5888083493eff430361be24c1b5..1abe846d76db4050cb865df1e183710fbb01e0c5 100644 (file)
@@ -85,16 +85,7 @@ class EbuildFetcher(SpawnProcess):
                        portage.process.spawned_pids.append(pid)
                        return [pid]
 
-               # Set up the command's pipes.
-               my_fds = {}
-               # To protect from cases where direct assignment could
-               # clobber needed fds ({1:2, 2:1}) we first dupe the fds
-               # into unused fds.
-               for fd in fd_pipes:
-                       my_fds[fd] = os.dup(fd_pipes[fd])
-               # Then assign them to what they should be.
-               for fd in my_fds:
-                       os.dup2(my_fds[fd], fd)
+               portage.process._setup_pipes(fd_pipes)
 
                # Force consistent color output, in case we are capturing fetch
                # output through a normal pipe due to unavailability of ptys.
index 37b482a0ef3571c02084df478a5b1f223c9fe970..e4d1d9523e0d05fb646957851630e57db80da156 100644 (file)
@@ -352,7 +352,25 @@ def _exec(binary, mycommand, opt_name, fd_pipes, env, gid, groups, uid, umask,
        # Quiet killing of subprocesses by SIGPIPE (see bug #309001).
        signal.signal(signal.SIGPIPE, signal.SIG_DFL)
 
-       # Set up the command's pipes.
+       _setup_pipes(fd_pipes)
+
+       # Set requested process permissions.
+       if gid:
+               os.setgid(gid)
+       if groups:
+               os.setgroups(groups)
+       if uid:
+               os.setuid(uid)
+       if umask:
+               os.umask(umask)
+       if pre_exec:
+               pre_exec()
+
+       # And switch to the new process.
+       os.execve(binary, myargs, env)
+
+def _setup_pipes(fd_pipes):
+       """Setup pipes for a forked process."""
        my_fds = {}
        # To protect from cases where direct assignment could
        # clobber needed fds ({1:2, 2:1}) we first dupe the fds
@@ -371,21 +389,6 @@ def _exec(binary, mycommand, opt_name, fd_pipes, env, gid, groups, uid, umask,
                        except OSError:
                                pass
 
-       # Set requested process permissions.
-       if gid:
-               os.setgid(gid)
-       if groups:
-               os.setgroups(groups)
-       if uid:
-               os.setuid(uid)
-       if umask:
-               os.umask(umask)
-       if pre_exec:
-               pre_exec()
-
-       # And switch to the new process.
-       os.execve(binary, myargs, env)
-
 def find_binary(binary):
        """
        Given a binary name, find the binary in PATH