def test_pipe_handle(self): h, _ = windows_utils.pipe(overlapped=(True, True)) _winapi.CloseHandle(_) p = windows_utils.PipeHandle(h) self.assertEqual(p.fileno(), h) self.assertEqual(p.handle, h) # check garbage collection of p closes handle del p support.gc_collect() try: _winapi.CloseHandle(h) except OSError as e: self.assertEqual(e.winerror, 6) # ERROR_INVALID_HANDLE else: raise RuntimeError('expected ERROR_INVALID_HANDLE')
def test_pipe_handle(self): h, _ = windows_utils.pipe(overlapped=(True, True)) _winapi.CloseHandle(_) p = windows_utils.PipeHandle(h) self.assertEqual(p.fileno(), h) self.assertEqual(p.handle, h) with warnings.catch_warnings(): warnings.filterwarnings('ignore', '', ResourceWarning) del p support.gc_collect() try: _winapi.CloseHandle(h) except OSError as e: self.assertEqual(e.winerror, 6) else: raise RuntimeError('expected ERROR_INVALID_HANDLE')
def _server_pipe_handle(self, first): # Return a wrapper for a new pipe handle. try: if self.closed(): return None except AttributeError: if self._address is None: return None # Create a new SECURITY_ATTRIBUTES object to open up write permissions to non-elevated clients. # Get the SID of this process' owner to add to the new DACL owner_sid = win32security.GetTokenInformation( win32security.OpenProcessToken(win32api.GetCurrentProcess(), win32security.TOKEN_QUERY), win32security.TokenOwner) # Build the new ACL -- SYSTEM, built-in Administrators and the Owner get full control (like default) acl = win32security.ACL() # Default buffer size of 64 OK acl.AddAccessAllowedAce( win32file.FILE_ALL_ACCESS, win32security.CreateWellKnownSid(win32security.WinLocalSystemSid)) acl.AddAccessAllowedAce( win32file.FILE_ALL_ACCESS, win32security.CreateWellKnownSid( win32security.WinBuiltinAdministratorsSid)) acl.AddAccessAllowedAce(win32file.FILE_ALL_ACCESS, owner_sid) # Allow all Users to both Read and Write to the pipe: # See http://stackoverflow.com/questions/29947524/c-let-user-process-write-to-local-system-named-pipe-custom-security-descrip acl.AddAccessAllowedAce( ntsecuritycon.FILE_GENERIC_READ | ntsecuritycon.FILE_WRITE_DATA, win32security.CreateWellKnownSid(win32security.WinBuiltinUsersSid)) # Construct new SECUIRTY_ATTRIBUTES AND SECURITY_DESCRIPTOR objects new_sa = win32security.SECURITY_ATTRIBUTES() new_sd = win32security.SECURITY_DESCRIPTOR() # Add the ACL to the SECURITY_DESCRIPTOR: new_sd.SetDacl(True, acl, False) # Add the SECURITY_DESCRIPTOR to the SECURITY_ATTRIBUTES object and set the Inheritance flag new_sa.SECURITY_DESCRIPTOR = new_sd new_sa.bInheritHandle = False PIPE_REJECT_REMOTE_CLIENTS = 0x8 flags = _winapi.PIPE_ACCESS_DUPLEX | _winapi.FILE_FLAG_OVERLAPPED if first: flags |= _winapi.FILE_FLAG_FIRST_PIPE_INSTANCE py_pipe = win32pipe.CreateNamedPipe( self._address, flags, win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_READMODE_MESSAGE | win32pipe.PIPE_WAIT | PIPE_REJECT_REMOTE_CLIENTS, win32pipe.PIPE_UNLIMITED_INSTANCES, windows_utils.BUFSIZE, windows_utils.BUFSIZE, win32pipe.NMPWAIT_WAIT_FOREVER, new_sa) # Extract the handle number from the PyHandle Object and pass it the Aysncio PipeHandle constructor pipe = windows_utils.PipeHandle(py_pipe.handle) # IMPORTANT: Detach the handle from the PyHandle object so it is not auto-closed when py_pipe is destroyed! py_pipe.Detach() self._free_instances.add(pipe) return pipe