From aba7e1acc9a95284cd39b6ad2ce50b2e06fc5dc9 Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Wed, 28 Aug 2019 09:03:25 +0000 Subject: [PATCH] #1527 add better logging for connection problems git-svn-id: https://xpra.org/svn/Xpra/trunk@23620 3bb7dfac-3a0b-4e04-842a-767bc560f471 --- src/xpra/platform/win32/common.py | 30 +++++++++++++++++++ src/xpra/platform/win32/namedpipes/common.py | 12 ++++++++ .../platform/win32/namedpipes/connection.py | 13 ++++---- .../platform/win32/namedpipes/listener.py | 1 + 4 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/xpra/platform/win32/common.py b/src/xpra/platform/win32/common.py index af3b89f51b..02aaafb86b 100644 --- a/src/xpra/platform/win32/common.py +++ b/src/xpra/platform/win32/common.py @@ -156,6 +156,8 @@ def GetMonitorInfo(hmonitor): } kernel32 = WinDLL("kernel32", use_last_error=True) +LocalFree = kernel32.LocalFree +FormatMessageW = kernel32.FormatMessageW SetConsoleTitleA = kernel32.SetConsoleTitleA SetConsoleTitleA.restype = INT SetConsoleTitleA.argtypes = [LPCTSTR] @@ -609,3 +611,31 @@ def __str__(self): ERROR_COUNTER_TIMEOUT : "COUNTER_TIMEOUT", ERROR_PIPE_BUSY : "PIPE_BUSY", } + +#https://gist.github.com/EBNull/6135237 +LANG_NEUTRAL = 0x00 +SUBLANG_NEUTRAL = 0x00 +SUBLANG_DEFAULT = 0x01 + +LANG_ENGLISH = 0x09 +SUBLANG_ENGLISH_US = 0x01 + +def MAKELANGID(primary, sublang): + return (primary & 0xFF) | (sublang & 0xFF) << 16 + +LCID_ENGLISH = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US) +LCID_DEFAULT = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT) +LCID_NEUTRAL = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL) + +def FormatMessageSystem(message_id, langid=LCID_ENGLISH): + from xpra.platform.win32.constants import FORMAT_MESSAGE_ALLOCATE_BUFFER, FORMAT_MESSAGE_FROM_SYSTEM, FORMAT_MESSAGE_IGNORE_INSERTS + sys_flag = FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS + bufptr = LPWSTR() + chars = kernel32.FormatMessageW(sys_flag, None, message_id, langid, byref(bufptr), 0, None) + if not chars: + chars = FormatMessageW(sys_flag, None, message_id, LCID_NEUTRAL, byref(bufptr), 0, None) + if not chars: + return str(message_id) + val = bufptr.value[:chars] + LocalFree(bufptr) + return val diff --git a/src/xpra/platform/win32/namedpipes/common.py b/src/xpra/platform/win32/namedpipes/common.py index 06558dff06..35d8d3180e 100644 --- a/src/xpra/platform/win32/namedpipes/common.py +++ b/src/xpra/platform/win32/namedpipes/common.py @@ -12,6 +12,8 @@ from ctypes.wintypes import LPCSTR from xpra.platform.win32.constants import WAIT_ABANDONED, WAIT_OBJECT_0, WAIT_TIMEOUT, WAIT_FAILED +PDWORD = POINTER(DWORD) +PHANDLE = POINTER(HANDLE) LPDWORD = POINTER(DWORD) LPCVOID = c_void_p LPVOID = c_void_p @@ -65,6 +67,7 @@ class SECURITY_DESCRIPTOR(Structure): ('Sacl', c_void_p), ('Dacl', c_void_p), ] +PSECURITY_DESCRIPTOR = POINTER(SECURITY_DESCRIPTOR) class TOKEN_USER(Structure): _fields_ = [ @@ -108,12 +111,21 @@ class TOKEN_USER(Structure): FlushFileBuffers.argtypes = [HANDLE] FlushFileBuffers.restype = BOOL GetLastError = kernel32.GetLastError +GetLastError.argtypes = [] +GetLastError.restype = DWORD GetCurrentProcess = kernel32.GetCurrentProcess +GetCurrentProcess.argtypes = [] GetCurrentProcess.restype = HANDLE advapi32 = WinDLL("advapi32", use_last_error=True) InitializeSecurityDescriptor = advapi32.InitializeSecurityDescriptor SetSecurityDescriptorOwner = advapi32.SetSecurityDescriptorOwner SetSecurityDescriptorDacl = advapi32.SetSecurityDescriptorDacl +#SetSecurityDescriptorDacl.argtypes = [PSECURITY_DESCRIPTOR, BOOL, PACL, BOOL] +SetSecurityDescriptorDacl.restype = BOOL OpenProcessToken = advapi32.OpenProcessToken +OpenProcessToken.argtypes = [HANDLE, DWORD, PHANDLE] +OpenProcessToken.restype = BOOL GetTokenInformation = advapi32.GetTokenInformation +#GetTokenInformation.argtypes = [HANDLE, TOKEN_INFORMATION_CLASS, LPVOID, DWORD, PDWORD] +GetTokenInformation.restype = BOOL diff --git a/src/xpra/platform/win32/namedpipes/connection.py b/src/xpra/platform/win32/namedpipes/connection.py index 08da0be9df..64f558a366 100644 --- a/src/xpra/platform/win32/namedpipes/connection.py +++ b/src/xpra/platform/win32/namedpipes/connection.py @@ -13,7 +13,7 @@ from xpra.net.bytestreams import Connection from xpra.net.common import ConnectionClosedException from xpra.platform.win32.common import ( - CloseHandle, + CloseHandle, FormatMessageSystem, ERROR_PIPE_BUSY, ERROR_PIPE_NOT_CONNECTED, IO_ERROR_STR, ERROR_BROKEN_PIPE, ERROR_IO_PENDING, ) @@ -198,10 +198,13 @@ def connect_to_namedpipe(pipe_name, timeout=10): log("CreateFileA(%s)=%#x", pipe_name, pipe_handle) if pipe_handle!=INVALID_HANDLE_VALUE: break - if GetLastError()!=ERROR_PIPE_BUSY: - raise Exception("cannot open named pipe '%s'" % pipe_name) - if WaitNamedPipeA(pipe_name, timeout*10000)==0: - raise Exception("timeout waiting for named pipe '%s'" % pipe_name) + err = GetLastError() + log("CreateFileA(..) error=%s", err) + if err==ERROR_PIPE_BUSY: + if WaitNamedPipeA(pipe_name, timeout*10000)==0: + raise Exception("timeout waiting for named pipe '%s'" % pipe_name) + else: + raise Exception("cannot open named pipe '%s': %s" % (pipe_name, FormatMessageSystem(err))) #we have a valid handle! dwMode = c_ulong(PIPE_READMODE_BYTE) r = SetNamedPipeHandleState(pipe_handle, byref(dwMode), None, None) diff --git a/src/xpra/platform/win32/namedpipes/listener.py b/src/xpra/platform/win32/namedpipes/listener.py index e37b5948ee..b271a524a9 100644 --- a/src/xpra/platform/win32/namedpipes/listener.py +++ b/src/xpra/platform/win32/namedpipes/listener.py @@ -121,6 +121,7 @@ def CreatePipeHandle(self): sa = self.CreateUnrestrictedPipeSecurityObject() else: sa = self.CreatePipeSecurityObject() + log("CreateNamedPipeA using %s (UNRESTRICTED=%s)", sa, UNRESTRICTED) return CreateNamedPipeA(strtobytes(self.pipe_name), PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT | PIPE_ACCEPT_REMOTE_CLIENTS, PIPE_UNLIMITED_INSTANCES, BUFSIZE, BUFSIZE, NMPWAIT_USE_DEFAULT_WAIT, ctypes.byref(sa))