'fdmap[childFD] = parentFD
The child wants to end up with \'childFD\' attached to what used to be
the parent\'s parentFD. As an example, a bash command run like
\'command 2>&1\' would correspond to an fdmap of {0:0, 1:1, 2:1}.
\'command >foo.txt\' would be {0:0, 1:os.open(\'foo.txt\'), 2:2}.
This is accomplished in two steps::
1. close all file descriptors that aren\'t values of fdmap.  This
means 0 .. maxfds (or just the open fds within that range, if
the platform supports \'/proc/<pid>/fd\').
2. for each childFD::
- if fdmap[childFD] == childFD, the descriptor is already in
place.  Make sure the CLOEXEC flag is not set, then delete
the entry from fdmap.
- if childFD is in fdmap.values(), then the target descriptor
is busy. Use os.dup() to move it elsewhere, update all
fdmap[childFD] items that point to it, then close the
original. Then fall through to the next case.
- now fdmap[childFD] is not in fdmap.values(), and is free.
Use os.dup2() to move it to the right place, then close the
original.'

def _setupChild(self, fdmap):

    debug = self.debug_child
    if debug:
        errfd = sys.stderr
        errfd.write('starting    _setupChild\n')
    destList = fdmap.values()
    for fd in _listOpenFDs():
        if (fd in destList):
            continue
        if (debug and (fd == errfd.fileno())):
            continue
        try:
            os.close(fd)
        except:
            pass
    if debug:
        print('fdmap', fdmap, file=errfd)
    for child in sorted(fdmap.keys()):
        target = fdmap[child]
        if (target == child):
            if debug:
                print(('%d    already    in    place' % target), file=errfd)
            fdesc._unsetCloseOnExec(child)
        else:
            if (child in fdmap.values()):
                newtarget = os.dup(child)
                if debug:
                    print(('os.dup(%d)    ->    %d' % (child, newtarget)), file=errfd)
                os.close(child)
                for (c, p) in items(fdmap):
                    if (p == child):
                        fdmap[c] = newtarget
            if debug:
                print(('os.dup2(%d,%d)' % (target, child)), file=errfd)
            os.dup2(target, child)
    old = []
    for fd in fdmap.values():
        if (not (fd in old)):
            if (not (fd in fdmap.keys())):
                old.append(fd)
    if debug:
        print('old', old, file=errfd)
    for fd in old:
        os.close(fd)
    self._resetSignalDisposition()
