From cea9a527bc5fbdb2287671186681a54bc69d1e21 Mon Sep 17 00:00:00 2001 From: Nick Anderson Date: Tue, 8 May 2018 23:00:41 -0700 Subject: [PATCH] windows: updating close logic for server and client pipes --- osquery/TPipe.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/osquery/TPipe.py b/osquery/TPipe.py index b91f38a..f6cedba 100644 --- a/osquery/TPipe.py +++ b/osquery/TPipe.py @@ -32,7 +32,7 @@ def close(self): in the same way """ if self._handle is not None: - win32pipe.DisconnectNamedPipe(self._handle) + win32file.CloseHandle(self._handle) self._handle = None @@ -67,13 +67,10 @@ def open(self): while conns < self._max_conn_attempts: try: h = win32file.CreateFile( - self._pipe_name, - win32file.GENERIC_READ | win32file.GENERIC_WRITE, - 0, - None, - win32file.OPEN_EXISTING, - win32file.FILE_FLAG_OVERLAPPED, - None) + self._pipe_name, + win32file.GENERIC_READ | win32file.GENERIC_WRITE, 0, None, + win32file.OPEN_EXISTING, win32file.FILE_FLAG_OVERLAPPED, + None) except pywintypes.error as e: if e.winerror != winerror.ERROR_PIPE_BUSY: raise TTransportException( @@ -198,8 +195,8 @@ def create_named_pipe(self): self._handle = win32pipe.CreateNamedPipe( self._pipe_name, openMode, pipeMode, - win32pipe.PIPE_UNLIMITED_INSTANCES, self._buff_size, self._buff_size, - win32pipe.NMPWAIT_WAIT_FOREVER, saAttr) + win32pipe.PIPE_UNLIMITED_INSTANCES, self._buff_size, + self._buff_size, win32pipe.NMPWAIT_WAIT_FOREVER, saAttr) err = win32api.GetLastError() if self._handle.handle == winerror.ERROR_INVALID_HANDLE: @@ -223,3 +220,13 @@ def initiate_named_connect(self): if ret == winerror.ERROR_PIPE_CONNECTED: win32event.SetEvent(self._overlapped.hEvent) break + + def close(self): + """ + The server must ensure to disconnect so that subsequent reconnect + attempts are successful + """ + if self._handle is not None: + win32pipe.DisconnectNamedPipe(self._handle) + win32file.CloseHandle(self._handle) + self._handle = None