Example #1
0
 def duplicate(handle, target_process=None, inheritable=False):
     if target_process is None:
         target_process = _subprocess.GetCurrentProcess()
     return _subprocess.DuplicateHandle(
         _subprocess.GetCurrentProcess(), handle, target_process,
         0, inheritable, _subprocess.DUPLICATE_SAME_ACCESS
         ).Detach()
Example #2
0
 def duplicate(handle, target_process=None, inheritable=False):
     if target_process is None:
         target_process = _subprocess.GetCurrentProcess()
     h = _subprocess.DuplicateHandle(_subprocess.GetCurrentProcess(),
                                     handle, target_process, 0, inheritable,
                                     _subprocess.DUPLICATE_SAME_ACCESS)
     if sys.version_info[0] < 3 or (sys.version_info[0] == 3
                                    and sys.version_info[1] < 3):
         h.Detach()
     return h
Example #3
0
def _mk_inheritable(fd):
    if sys.version_info[:2] > (3, 3):
        if sys.platform == 'win32':
            # Change to Windwos file handle
            import msvcrt
            fdh = msvcrt.get_osfhandle(fd)
            os.set_handle_inheritable(fdh, True)
            return fdh
        else:
            os.set_inheritable(fd, True)
            return fd
    elif sys.platform == 'win32':
        # TODO: find a hack??
        # Not yet working
        import msvcrt
        import _subprocess

        curproc = _subprocess.GetCurrentProcess()
        fdh = msvcrt.get_osfhandle(fd)
        fdh = _subprocess.DuplicateHandle(
            curproc,
            fdh,
            curproc,
            0,
            True,  # set inheritable FLAG
            _subprocess.DUPLICATE_SAME_ACCESS)
        return fdh
    else:
        return fd
Example #4
0
    def _launch_pov_win(self, mainproc, pipe):
        import _subprocess as _sp

        cmd = [self.pov]
        if self.pov_seed:
            cmd.append('seed=%s' % self.pov_seed)

        # The pipe HANDLE isn't inheritable, make a duplicate that is
        cur_proc = _sp.GetCurrentProcess()
        inh_pipe = _sp.DuplicateHandle(cur_proc,       # Source process
                                       pipe.fileno(),  # HANDLE
                                       cur_proc,       # Target process
                                       0,              # Desired access
                                       1,              # Inheritable
                                       _sp.DUPLICATE_SAME_ACCESS)  # Options

        # Run the POV
        pov_proc = sp.Popen(cmd,
                            # Passing the HANDLE value here through an environment variable
                            # libpov will grab this and open it in fd 3
                            # see: include/libpov/pov.c - DLLMain
                            env={'POV_FD': str(int(inh_pipe))},

                            # stdin/out connect to the cb directly
                            stdin=mainproc.stdout,
                            stdout=mainproc.stdin)
        pov_proc.wait()
Example #5
0
 def sendHandle(conn, handle, destination_pid):
     process_handle = win32.OpenProcess(win32.PROCESS_ALL_ACCESS, False,
                                        destination_pid)
     try:
         new_handle = _subprocess.DuplicateHandle(
             _subprocess.GetCurrentProcess(), handle, process_handle, 0,
             False, _subprocess.DUPLICATE_SAME_ACCESS)
         conn.send(new_handle.Detach())
     finally:
         win32.CloseHandle(process_handle)
Example #6
0
 def CreatePipe(*args):
     rfd, wfd = os.pipe()
     _current_process = win_api.GetCurrentProcess()
     rhandle = win_api.DuplicateHandle(
         _current_process, msvcrt.get_osfhandle(rfd),
         _current_process, 0, True, win_api.DUPLICATE_SAME_ACCESS)
     if sys.version_info[:2] < (3, 3):
         rhandle = rhandle.Detach()
     os.close(rfd)
     return rhandle, wfd
Example #7
0
 def _make_inheritable(handle):
     """Return inheritable Handle, close the original."""
     # new handles are created uninheritable by default, but they can be
     # made inheritable on duplication:
     current_process = _winapi.GetCurrentProcess()
     dup = Handle(
         _winapi.DuplicateHandle(current_process, handle, current_process,
                                 0, True, _winapi.DUPLICATE_SAME_ACCESS))
     handle.Close()
     return dup
Example #8
0
 def detach(self):
     # retrieve handle from process which currently owns it
     if self._pid == os.getpid():
         return self._handle
     proc = _winapi.OpenProcess(_winapi.PROCESS_DUP_HANDLE, False,
                                self._pid)
     try:
         return _winapi.DuplicateHandle(proc, self._handle,
                                        _winapi.GetCurrentProcess(),
                                        self._access, False,
                                        _winapi.DUPLICATE_CLOSE_SOURCE)
     finally:
         _winapi.CloseHandle(proc)
Example #9
0
 def __init__(self, handle, access, pid=None):
     # duplicate handle for process with given pid
     if pid is None:
         pid = os.getpid()
     proc = _winapi.OpenProcess(_winapi.PROCESS_DUP_HANDLE, False, pid)
     try:
         self._handle = _winapi.DuplicateHandle(_winapi.GetCurrentProcess(),
                                                handle, proc, access, False,
                                                0)
     finally:
         _winapi.CloseHandle(proc)
     self._access = access
     self._pid = pid
Example #10
0
def setup_pipes():
    global pipearg, pipein

    pipeout, pipein = os.pipe()

    # Prepare to pass to child process
    if sys.platform == "win32":
        curproc = _subprocess.GetCurrentProcess()
        pipeouth = msvcrt.get_osfhandle(pipeout)
        pipeoutih = _subprocess.DuplicateHandle(curproc, pipeouth, curproc, 0, 1, _subprocess.DUPLICATE_SAME_ACCESS)

        pipearg = str(int(pipeoutih))
    else:
        pipearg = str(pipeout)

        # Must close pipe input if child will block waiting for end
        # Can also be closed in a preexec_fn passed to subprocess.Popen
        fcntl.fcntl(pipein, fcntl.F_SETFD, fcntl.FD_CLOEXEC)
Example #11
0
 def _make_inheritable(self, handle):
     """Return a duplicate of handle, which is inheritable"""
     return _subprocess.DuplicateHandle(_subprocess.GetCurrentProcess(),
                         handle, _subprocess.GetCurrentProcess(), 0, 1,
                         _subprocess.DUPLICATE_SAME_ACCESS)
Example #12
0
 def duplicateHandle(handle):
     return _subprocess.DuplicateHandle(
         _subprocess.GetCurrentProcess(), handle,
         _subprocess.GetCurrentProcess(), 0, False,
         _subprocess.DUPLICATE_SAME_ACCESS).Detach()
Example #13
0
 def _duplicate(handle):
   target_process = _subprocess.GetCurrentProcess()
   return _subprocess.DuplicateHandle(
       _subprocess.GetCurrentProcess(), handle, target_process,
       0, False, _subprocess.DUPLICATE_SAME_ACCESS).Detach()
Example #14
0
import os
import subprocess
import sys

if sys.platform == "win32":
    import msvcrt
    import _subprocess
else:
    import fcntl

# Create pipe for communication
pipeout, pipein = os.pipe()

# Prepare to pass to child process
if sys.platform == "win32":
    curproc = _subprocess.GetCurrentProcess()
    pipeouth = msvcrt.get_osfhandle(pipeout)
    pipeoutih = _subprocess.DuplicateHandle(curproc, pipeouth, curproc, 0, 1,
                                            _subprocess.DUPLICATE_SAME_ACCESS)

    pipearg = str(int(pipeoutih))
    print pipearg
else:
    pipearg = str(pipeout)

    # Must close pipe input if child will block waiting for end
    # Can also be closed in a preexec_fn passed to subprocess.Popen
    fcntl.fcntl(pipein, fcntl.F_SETFD, fcntl.FD_CLOEXEC)

# Start child with argument indicating which FD/FH to read from
subproc = subprocess.Popen(['python', 'child.py', pipearg], close_fds=False)
 def _make_inheritable(self, handle):
     return _subprocess.DuplicateHandle(_subprocess.GetCurrentProcess(), handle, _subprocess.GetCurrentProcess(), 0, 1, _subprocess.DUPLICATE_SAME_ACCESS)