Skip to content

Commit

Permalink
#1527 add better logging for connection problems
Browse files Browse the repository at this point in the history
git-svn-id: https://xpra.org/svn/Xpra/trunk@23620 3bb7dfac-3a0b-4e04-842a-767bc560f471
  • Loading branch information
totaam committed Aug 28, 2019
1 parent 105f496 commit aba7e1a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
30 changes: 30 additions & 0 deletions src/xpra/platform/win32/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
12 changes: 12 additions & 0 deletions src/xpra/platform/win32/namedpipes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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_ = [
Expand Down Expand Up @@ -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
13 changes: 8 additions & 5 deletions src/xpra/platform/win32/namedpipes/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/xpra/platform/win32/namedpipes/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit aba7e1a

Please sign in to comment.