diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2020-01-05 12:33:23 -0800 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2020-01-05 12:33:23 -0800 |
commit | 18259d54fc86cc2eb031a3dfb5ca2432574eb266 (patch) | |
tree | 9f6679a64013ae232f3fd19ac09b2bf184131691 | |
parent | 161accb06f7d0eae089d2844ac43720f889a4e8d (diff) | |
download | psutil-18259d54fc86cc2eb031a3dfb5ca2432574eb266.tar.gz |
refactor win C code: use original WinAPI functions and remove psuil_ prefix
-rw-r--r-- | psutil/_psutil_windows.c | 20 | ||||
-rw-r--r-- | psutil/arch/windows/cpu.c | 16 | ||||
-rw-r--r-- | psutil/arch/windows/globals.c | 64 | ||||
-rw-r--r-- | psutil/arch/windows/globals.h | 51 | ||||
-rw-r--r-- | psutil/arch/windows/ntextapi.h | 68 | ||||
-rw-r--r-- | psutil/arch/windows/process_handles.c | 6 | ||||
-rw-r--r-- | psutil/arch/windows/process_info.c | 10 | ||||
-rw-r--r-- | psutil/arch/windows/socks.c | 67 |
8 files changed, 144 insertions, 158 deletions
diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 69e129eb..0e780d9b 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -55,8 +55,8 @@ psutil_get_num_cpus(int fail_on_err) { unsigned int ncpus = 0; // Minimum requirement: Windows 7 - if (psutil_GetActiveProcessorCount != NULL) { - ncpus = psutil_GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); + if (GetActiveProcessorCount != NULL) { + ncpus = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); if ((ncpus == 0) && (fail_on_err == 1)) { PyErr_SetFromWindowsErr(0); } @@ -104,9 +104,9 @@ psutil_boot_time(PyObject *self, PyObject *args) { (fileTime.dwHighDateTime)) << 32) + fileTime.dwLowDateTime; pt = (time_t)((ll - 116444736000000000ull) / 10000000ull); - if (psutil_GetTickCount64 != NULL) { + if (GetTickCount64 != NULL) { // Windows >= Vista - uptime = psutil_GetTickCount64() / 1000ull; + uptime = GetTickCount64() / 1000ull; } else { // Windows XP. @@ -598,7 +598,7 @@ psutil_GetProcWsetInformation( bufferSize = 0x8000; buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bufferSize); - while ((status = psutil_NtQueryVirtualMemory( + while ((status = NtQueryVirtualMemory( hProcess, NULL, MemoryWorkingSetInformation, @@ -752,9 +752,9 @@ psutil_proc_suspend_or_resume(PyObject *self, PyObject *args) { return NULL; if (PyObject_IsTrue(suspend)) - status = psutil_NtSuspendProcess(hProcess); + status = NtSuspendProcess(hProcess); else - status = psutil_NtResumeProcess(hProcess); + status = NtResumeProcess(hProcess); if (! NT_SUCCESS(status)) { CloseHandle(hProcess); @@ -1097,7 +1097,7 @@ psutil_proc_io_priority_get(PyObject *self, PyObject *args) { if (hProcess == NULL) return NULL; - status = psutil_NtQueryInformationProcess( + status = NtQueryInformationProcess( hProcess, ProcessIoPriority, &IoPriority, @@ -1130,7 +1130,7 @@ psutil_proc_io_priority_set(PyObject *self, PyObject *args) { if (hProcess == NULL) return NULL; - status = psutil_NtSetInformationProcess( + status = NtSetInformationProcess( hProcess, ProcessIoPriority, (PVOID)&prio, @@ -1346,7 +1346,7 @@ psutil_users(PyObject *self, PyObject *args) { } // login time - if (! psutil_WinStationQueryInformationW( + if (! WinStationQueryInformationW( hServer, sessionId, WinStationInformation, diff --git a/psutil/arch/windows/cpu.c b/psutil/arch/windows/cpu.c index 479adfeb..6966aa18 100644 --- a/psutil/arch/windows/cpu.c +++ b/psutil/arch/windows/cpu.c @@ -21,8 +21,8 @@ psutil_get_num_cpus(int fail_on_err) { unsigned int ncpus = 0; // Minimum requirement: Windows 7 - if (psutil_GetActiveProcessorCount != NULL) { - ncpus = psutil_GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); + if (GetActiveProcessorCount != NULL) { + ncpus = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); if ((ncpus == 0) && (fail_on_err == 1)) { PyErr_SetFromWindowsErr(0); } @@ -100,7 +100,7 @@ psutil_per_cpu_times(PyObject *self, PyObject *args) { } // gets cpu time informations - status = psutil_NtQuerySystemInformation( + status = NtQuerySystemInformation( SystemProcessorPerformanceInformation, sppi, ncpus * sizeof(_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION), @@ -194,13 +194,13 @@ psutil_cpu_count_phys(PyObject *self, PyObject *args) { // it supports process groups, meaning this is able to report more // than 64 CPUs. See: // https://bugs.python.org/issue33166 - if (psutil_GetLogicalProcessorInformationEx == NULL) { + if (GetLogicalProcessorInformationEx == NULL) { psutil_debug("Win < 7; cpu_count_phys() forced to None"); Py_RETURN_NONE; } while (1) { - rc = psutil_GetLogicalProcessorInformationEx( + rc = GetLogicalProcessorInformationEx( RelationAll, buffer, &length); if (rc == FALSE) { if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { @@ -284,7 +284,7 @@ psutil_cpu_stats(PyObject *self, PyObject *args) { PyErr_NoMemory(); goto error; } - status = psutil_NtQuerySystemInformation( + status = NtQuerySystemInformation( SystemPerformanceInformation, spi, ncpus * sizeof(_SYSTEM_PERFORMANCE_INFORMATION), @@ -303,7 +303,7 @@ psutil_cpu_stats(PyObject *self, PyObject *args) { goto error; } - status = psutil_NtQuerySystemInformation( + status = NtQuerySystemInformation( SystemInterruptInformation, InterruptInformation, ncpus * sizeof(SYSTEM_INTERRUPT_INFORMATION), @@ -325,7 +325,7 @@ psutil_cpu_stats(PyObject *self, PyObject *args) { goto error; } - status = psutil_NtQuerySystemInformation( + status = NtQuerySystemInformation( SystemProcessorPerformanceInformation, sppi, ncpus * sizeof(_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION), diff --git a/psutil/arch/windows/globals.c b/psutil/arch/windows/globals.c index e600c3bd..27d9020c 100644 --- a/psutil/arch/windows/globals.c +++ b/psutil/arch/windows/globals.c @@ -79,7 +79,7 @@ psutil_SetFromNTStatusErr(NTSTATUS Status, const char *syscall) { if (NT_NTWIN32(Status)) err = WIN32_FROM_NTSTATUS(Status); else - err = psutil_RtlNtStatusToDosErrorNoTeb(Status); + err = RtlNtStatusToDosErrorNoTeb(Status); // if (GetLastError() != 0) // err = GetLastError(); sprintf(fullmsg, "(originated from %s)", syscall); @@ -92,88 +92,88 @@ psutil_loadlibs() { /* * Mandatory. */ - psutil_NtQuerySystemInformation = psutil_GetProcAddressFromLib( + NtQuerySystemInformation = psutil_GetProcAddressFromLib( "ntdll.dll", "NtQuerySystemInformation"); - if (psutil_NtQuerySystemInformation == NULL) + if (NtQuerySystemInformation == NULL) return 1; - psutil_NtQueryInformationProcess = psutil_GetProcAddress( + NtQueryInformationProcess = psutil_GetProcAddress( "ntdll.dll", "NtQueryInformationProcess"); - if (! psutil_NtQueryInformationProcess) + if (! NtQueryInformationProcess) return 1; - psutil_NtSetInformationProcess = psutil_GetProcAddress( + NtSetInformationProcess = psutil_GetProcAddress( "ntdll.dll", "NtSetInformationProcess"); - if (! psutil_NtSetInformationProcess) + if (! NtSetInformationProcess) return 1; - psutil_WinStationQueryInformationW = psutil_GetProcAddressFromLib( + WinStationQueryInformationW = psutil_GetProcAddressFromLib( "winsta.dll", "WinStationQueryInformationW"); - if (! psutil_WinStationQueryInformationW) + if (! WinStationQueryInformationW) return 1; - psutil_NtQueryObject = psutil_GetProcAddressFromLib( + NtQueryObject = psutil_GetProcAddressFromLib( "ntdll.dll", "NtQueryObject"); - if (! psutil_NtQueryObject) + if (! NtQueryObject) return 1; - psutil_rtlIpv4AddressToStringA = psutil_GetProcAddressFromLib( + RtlIpv4AddressToStringA = psutil_GetProcAddressFromLib( "ntdll.dll", "RtlIpv4AddressToStringA"); - if (! psutil_rtlIpv4AddressToStringA) + if (! RtlIpv4AddressToStringA) return 1; - psutil_GetExtendedTcpTable = psutil_GetProcAddressFromLib( + GetExtendedTcpTable = psutil_GetProcAddressFromLib( "iphlpapi.dll", "GetExtendedTcpTable"); - if (! psutil_GetExtendedTcpTable) + if (! GetExtendedTcpTable) return 1; - psutil_GetExtendedUdpTable = psutil_GetProcAddressFromLib( + GetExtendedUdpTable = psutil_GetProcAddressFromLib( "iphlpapi.dll", "GetExtendedUdpTable"); - if (! psutil_GetExtendedUdpTable) + if (! GetExtendedUdpTable) return 1; - psutil_RtlGetVersion = psutil_GetProcAddressFromLib( + RtlGetVersion = psutil_GetProcAddressFromLib( "ntdll.dll", "RtlGetVersion"); - if (! psutil_RtlGetVersion) + if (! RtlGetVersion) return 1; - psutil_NtSuspendProcess = psutil_GetProcAddressFromLib( + NtSuspendProcess = psutil_GetProcAddressFromLib( "ntdll", "NtSuspendProcess"); - if (! psutil_NtSuspendProcess) + if (! NtSuspendProcess) return 1; - psutil_NtResumeProcess = psutil_GetProcAddressFromLib( + NtResumeProcess = psutil_GetProcAddressFromLib( "ntdll", "NtResumeProcess"); - if (! psutil_NtResumeProcess) + if (! NtResumeProcess) return 1; - psutil_NtQueryVirtualMemory = psutil_GetProcAddressFromLib( + NtQueryVirtualMemory = psutil_GetProcAddressFromLib( "ntdll", "NtQueryVirtualMemory"); - if (! psutil_NtQueryVirtualMemory) + if (! NtQueryVirtualMemory) return 1; - psutil_RtlNtStatusToDosErrorNoTeb = psutil_GetProcAddressFromLib( + RtlNtStatusToDosErrorNoTeb = psutil_GetProcAddressFromLib( "ntdll", "RtlNtStatusToDosErrorNoTeb"); - if (! psutil_RtlNtStatusToDosErrorNoTeb) + if (! RtlNtStatusToDosErrorNoTeb) return 1; /* * Optional. */ // not available on Wine - psutil_rtlIpv6AddressToStringA = psutil_GetProcAddressFromLib( + RtlIpv6AddressToStringA = psutil_GetProcAddressFromLib( "ntdll.dll", "RtlIpv6AddressToStringA"); // minimum requirement: Win Vista - psutil_GetTickCount64 = psutil_GetProcAddress( + GetTickCount64 = psutil_GetProcAddress( "kernel32", "GetTickCount64"); // minimum requirement: Win 7 - psutil_GetActiveProcessorCount = psutil_GetProcAddress( + GetActiveProcessorCount = psutil_GetProcAddress( "kernel32", "GetActiveProcessorCount"); // minumum requirement: Win 7 - psutil_GetLogicalProcessorInformationEx = psutil_GetProcAddressFromLib( + GetLogicalProcessorInformationEx = psutil_GetProcAddressFromLib( "kernel32", "GetLogicalProcessorInformationEx"); PyErr_Clear(); @@ -189,7 +189,7 @@ psutil_set_winver() { versionInfo.dwOSVersionInfoSize = sizeof(RTL_OSVERSIONINFOEXW); memset(&versionInfo, 0, sizeof(RTL_OSVERSIONINFOEXW)); - psutil_RtlGetVersion((PRTL_OSVERSIONINFOW)&versionInfo); + RtlGetVersion((PRTL_OSVERSIONINFOW)&versionInfo); maj = versionInfo.dwMajorVersion; min = versionInfo.dwMinorVersion; if (maj == 6 && min == 0) diff --git a/psutil/arch/windows/globals.h b/psutil/arch/windows/globals.h index edae4271..f87a4f8b 100644 --- a/psutil/arch/windows/globals.h +++ b/psutil/arch/windows/globals.h @@ -32,54 +32,3 @@ int psutil_load_globals(); PVOID psutil_GetProcAddress(LPCSTR libname, LPCSTR procname); PVOID psutil_GetProcAddressFromLib(LPCSTR libname, LPCSTR procname); PVOID psutil_SetFromNTStatusErr(NTSTATUS Status, const char *syscall); - -_NtQuerySystemInformation \ - psutil_NtQuerySystemInformation; - -_NtQueryInformationProcess \ - psutil_NtQueryInformationProcess; - -_NtSetInformationProcess - psutil_NtSetInformationProcess; - -_WinStationQueryInformationW \ - psutil_WinStationQueryInformationW; - -_RtlIpv4AddressToStringA \ - psutil_rtlIpv4AddressToStringA; - -_RtlIpv6AddressToStringA \ - psutil_rtlIpv6AddressToStringA; - -_GetExtendedTcpTable \ - psutil_GetExtendedTcpTable; - -_GetExtendedUdpTable \ - psutil_GetExtendedUdpTable; - -_GetActiveProcessorCount \ - psutil_GetActiveProcessorCount; - -_GetTickCount64 \ - psutil_GetTickCount64; - -_NtQueryObject \ - psutil_NtQueryObject; - -_GetLogicalProcessorInformationEx \ - psutil_GetLogicalProcessorInformationEx; - -_RtlGetVersion \ - psutil_RtlGetVersion; - -_NtSuspendProcess \ - psutil_NtSuspendProcess; - -_NtResumeProcess \ - psutil_NtResumeProcess; - -_NtQueryVirtualMemory \ - psutil_NtQueryVirtualMemory; - -_RtlNtStatusToDosErrorNoTeb \ - psutil_RtlNtStatusToDosErrorNoTeb; diff --git a/psutil/arch/windows/ntextapi.h b/psutil/arch/windows/ntextapi.h index 3e7147d1..b7b1c976 100644 --- a/psutil/arch/windows/ntextapi.h +++ b/psutil/arch/windows/ntextapi.h @@ -464,12 +464,14 @@ typedef struct { // Type defs for modules loaded at runtime. // ================================================================ -typedef BOOL (WINAPI *_GetLogicalProcessorInformationEx)( +BOOL (WINAPI *_GetLogicalProcessorInformationEx) ( LOGICAL_PROCESSOR_RELATIONSHIP relationship, PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX Buffer, PDWORD ReturnLength); -typedef BOOLEAN (WINAPI * _WinStationQueryInformationW)( +#define GetLogicalProcessorInformationEx _GetLogicalProcessorInformationEx + +BOOLEAN (WINAPI * _WinStationQueryInformationW) ( HANDLE ServerHandle, ULONG SessionId, WINSTATIONINFOCLASS WinStationInformationClass, @@ -477,34 +479,46 @@ typedef BOOLEAN (WINAPI * _WinStationQueryInformationW)( ULONG WinStationInformationLength, PULONG pReturnLength); -typedef NTSTATUS (NTAPI *_NtQueryInformationProcess)( +#define WinStationQueryInformationW _WinStationQueryInformationW + +NTSTATUS (NTAPI *_NtQueryInformationProcess) ( HANDLE ProcessHandle, DWORD ProcessInformationClass, PVOID ProcessInformation, DWORD ProcessInformationLength, PDWORD ReturnLength); -typedef NTSTATUS (NTAPI *_NtQuerySystemInformation)( +#define NtQueryInformationProcess _NtQueryInformationProcess + +NTSTATUS (NTAPI *_NtQuerySystemInformation) ( ULONG SystemInformationClass, PVOID SystemInformation, ULONG SystemInformationLength, PULONG ReturnLength); -typedef NTSTATUS (NTAPI *_NtSetInformationProcess)( +#define NtQuerySystemInformation _NtQuerySystemInformation + +NTSTATUS (NTAPI *_NtSetInformationProcess) ( HANDLE ProcessHandle, DWORD ProcessInformationClass, PVOID ProcessInformation, DWORD ProcessInformationLength); -typedef PSTR (NTAPI * _RtlIpv4AddressToStringA)( +#define NtSetInformationProcess _NtSetInformationProcess + +PSTR (NTAPI * _RtlIpv4AddressToStringA) ( struct in_addr *Addr, PSTR S); -typedef PSTR (NTAPI * _RtlIpv6AddressToStringA)( +#define RtlIpv4AddressToStringA _RtlIpv4AddressToStringA + +PSTR (NTAPI * _RtlIpv6AddressToStringA) ( struct in6_addr *Addr, PSTR P); -typedef DWORD (WINAPI * _GetExtendedTcpTable)( +#define RtlIpv6AddressToStringA _RtlIpv6AddressToStringA + +DWORD (WINAPI * _GetExtendedTcpTable) ( PVOID pTcpTable, PDWORD pdwSize, BOOL bOrder, @@ -512,7 +526,9 @@ typedef DWORD (WINAPI * _GetExtendedTcpTable)( TCP_TABLE_CLASS TableClass, ULONG Reserved); -typedef DWORD (WINAPI * _GetExtendedUdpTable)( +#define GetExtendedTcpTable _GetExtendedTcpTable + +DWORD (WINAPI * _GetExtendedUdpTable) ( PVOID pUdpTable, PDWORD pdwSize, BOOL bOrder, @@ -520,32 +536,46 @@ typedef DWORD (WINAPI * _GetExtendedUdpTable)( UDP_TABLE_CLASS TableClass, ULONG Reserved); -typedef DWORD (CALLBACK *_GetActiveProcessorCount)( +#define GetExtendedUdpTable _GetExtendedUdpTable + +DWORD (CALLBACK *_GetActiveProcessorCount) ( WORD GroupNumber); -typedef ULONGLONG (CALLBACK *_GetTickCount64)( +#define GetActiveProcessorCount _GetActiveProcessorCount + +ULONGLONG (CALLBACK *_GetTickCount64) ( void); -typedef NTSTATUS (NTAPI *_NtQueryObject)( +#define GetTickCount64 _GetTickCount64 + +NTSTATUS (NTAPI *_NtQueryObject) ( HANDLE Handle, OBJECT_INFORMATION_CLASS ObjectInformationClass, PVOID ObjectInformation, ULONG ObjectInformationLength, PULONG ReturnLength); -typedef NTSTATUS (WINAPI *_RtlGetVersion) ( +#define NtQueryObject _NtQueryObject + +NTSTATUS (WINAPI *_RtlGetVersion) ( PRTL_OSVERSIONINFOW lpVersionInformation ); -typedef NTSTATUS (WINAPI *_NtResumeProcess) ( +#define RtlGetVersion _RtlGetVersion + +NTSTATUS (WINAPI *_NtResumeProcess) ( HANDLE hProcess ); -typedef NTSTATUS (WINAPI *_NtSuspendProcess) ( +#define NtResumeProcess _NtResumeProcess + +NTSTATUS (WINAPI *_NtSuspendProcess) ( HANDLE hProcess ); -typedef NTSTATUS (NTAPI *_NtQueryVirtualMemory) ( +#define NtSuspendProcess _NtSuspendProcess + +NTSTATUS (NTAPI *_NtQueryVirtualMemory) ( HANDLE ProcessHandle, PVOID BaseAddress, int MemoryInformationClass, @@ -554,8 +584,12 @@ typedef NTSTATUS (NTAPI *_NtQueryVirtualMemory) ( PSIZE_T ReturnLength ); -typedef ULONG (WINAPI *_RtlNtStatusToDosErrorNoTeb) ( +#define NtQueryVirtualMemory _NtQueryVirtualMemory + +ULONG (WINAPI *_RtlNtStatusToDosErrorNoTeb) ( NTSTATUS status ); +#define RtlNtStatusToDosErrorNoTeb _RtlNtStatusToDosErrorNoTeb + #endif // __NTEXTAPI_H__ diff --git a/psutil/arch/windows/process_handles.c b/psutil/arch/windows/process_handles.c index 024d00a3..bb1ab42c 100644 --- a/psutil/arch/windows/process_handles.c +++ b/psutil/arch/windows/process_handles.c @@ -50,7 +50,7 @@ psutil_wait_thread(LPVOID lpvParam) { WaitForSingleObject(g_hEvtStart, INFINITE); // TODO: return code not checked - g_status = psutil_NtQueryObject( + g_status = NtQueryObject( g_hFile, ObjectNameInformation, g_pNameBuffer, @@ -145,7 +145,7 @@ psutil_get_open_files_ntqueryobject(DWORD dwPid, HANDLE hProcess) { error = TRUE; goto cleanup; } - } while ((status = psutil_NtQuerySystemInformation( + } while ((status = NtQuerySystemInformation( SystemExtendedHandleInformation, pHandleInfo, dwInfoSize, @@ -309,7 +309,7 @@ psutil_get_open_files_getmappedfilename(DWORD dwPid, HANDLE hProcess) { error = TRUE; goto cleanup; } - } while ((status = psutil_NtQuerySystemInformation( + } while ((status = NtQuerySystemInformation( SystemExtendedHandleInformation, pHandleInfo, dwInfoSize, diff --git a/psutil/arch/windows/process_info.c b/psutil/arch/windows/process_info.c index 1ee85e08..5daf0b69 100644 --- a/psutil/arch/windows/process_info.c +++ b/psutil/arch/windows/process_info.c @@ -102,7 +102,7 @@ psutil_get_process_data(long pid, #ifdef _WIN64 /* 64 bit case. Check if the target is a 32 bit process running in WoW64 * mode. */ - status = psutil_NtQueryInformationProcess( + status = NtQueryInformationProcess( hProcess, ProcessWow64Information, &ppeb32, @@ -252,7 +252,7 @@ psutil_get_process_data(long pid, PEB_ peb; RTL_USER_PROCESS_PARAMETERS_ procParameters; - status = psutil_NtQueryInformationProcess( + status = NtQueryInformationProcess( hProcess, ProcessBasicInformation, &pbi, @@ -389,7 +389,7 @@ psutil_cmdline_query_proc(long pid, WCHAR **pdata, SIZE_T *psize) { goto error; // get the right buf size - status = psutil_NtQueryInformationProcess( + status = NtQueryInformationProcess( hProcess, ProcessCommandLineInformation, NULL, @@ -419,7 +419,7 @@ psutil_cmdline_query_proc(long pid, WCHAR **pdata, SIZE_T *psize) { } // get the cmdline - status = psutil_NtQueryInformationProcess( + status = NtQueryInformationProcess( hProcess, ProcessCommandLineInformation, buffer, @@ -589,7 +589,7 @@ psutil_get_proc_info(DWORD pid, PSYSTEM_PROCESS_INFORMATION *retProcess, } while (TRUE) { - status = psutil_NtQuerySystemInformation( + status = NtQuerySystemInformation( SystemProcessInformation, buffer, bufferSize, diff --git a/psutil/arch/windows/socks.c b/psutil/arch/windows/socks.c index 04ea6285..f46ffaee 100644 --- a/psutil/arch/windows/socks.c +++ b/psutil/arch/windows/socks.c @@ -17,13 +17,14 @@ #define BYTESWAP_USHORT(x) ((((USHORT)(x) << 8) | ((USHORT)(x) >> 8)) & 0xffff) +typedef DWORD (WINAPI * TYPE_GetExtendedTcpTable)(); +typedef DWORD (WINAPI * TYPE_GetExtendedUdpTable)(); -// https://msdn.microsoft.com/library/aa365928.aspx -// TODO properly handle return code -static DWORD __GetExtendedTcpTable(_GetExtendedTcpTable call, - ULONG address_family, - PVOID * data, DWORD * size) +static DWORD __GetExtendedTcpTable(TYPE_GetExtendedTcpTable call, + ULONG family, + PVOID *data, + DWORD *size) { // Due to other processes being active on the machine, it's possible // that the size of the table increases between the moment where we @@ -34,8 +35,7 @@ static DWORD __GetExtendedTcpTable(_GetExtendedTcpTable call, DWORD error = ERROR_INSUFFICIENT_BUFFER; *size = 0; *data = NULL; - error = call(NULL, size, FALSE, address_family, - TCP_TABLE_OWNER_PID_ALL, 0); + error = call(NULL, size, FALSE, family, TCP_TABLE_OWNER_PID_ALL, 0); while (error == ERROR_INSUFFICIENT_BUFFER || error == 0xC0000001) { *data = malloc(*size); @@ -43,22 +43,28 @@ static DWORD __GetExtendedTcpTable(_GetExtendedTcpTable call, error = ERROR_NOT_ENOUGH_MEMORY; continue; } - error = call(*data, size, FALSE, address_family, - TCP_TABLE_OWNER_PID_ALL, 0); + error = call(*data, size, FALSE, family, TCP_TABLE_OWNER_PID_ALL, 0); if (error != NO_ERROR) { free(*data); *data = NULL; } } + if (error == ERROR_NOT_ENOUGH_MEMORY) { + PyErr_NoMemory(); + return 1; + } + if (error != NO_ERROR) { + PyErr_SetString(PyExc_RuntimeError, "GetExtendedTcpTable failed"); + return 1; + } return error; } -// https://msdn.microsoft.com/library/aa365930.aspx -// TODO properly check return value -static DWORD __GetExtendedUdpTable(_GetExtendedUdpTable call, - ULONG address_family, - PVOID * data, DWORD * size) +static DWORD __GetExtendedUdpTable(TYPE_GetExtendedUdpTable call, + ULONG family, + PVOID * data, + DWORD * size) { // Due to other processes being active on the machine, it's possible // that the size of the table increases between the moment where we @@ -69,8 +75,7 @@ static DWORD __GetExtendedUdpTable(_GetExtendedUdpTable call, DWORD error = ERROR_INSUFFICIENT_BUFFER; *size = 0; *data = NULL; - error = call(NULL, size, FALSE, address_family, - UDP_TABLE_OWNER_PID, 0); + error = call(NULL, size, FALSE, family, UDP_TABLE_OWNER_PID, 0); while (error == ERROR_INSUFFICIENT_BUFFER || error == 0xC0000001) { *data = malloc(*size); @@ -78,20 +83,18 @@ static DWORD __GetExtendedUdpTable(_GetExtendedUdpTable call, error = ERROR_NOT_ENOUGH_MEMORY; continue; } - error = call(*data, size, FALSE, address_family, - UDP_TABLE_OWNER_PID, 0); + error = call(*data, size, FALSE, family, UDP_TABLE_OWNER_PID, 0); if (error != NO_ERROR) { free(*data); *data = NULL; } } - if (error == ERROR_NOT_ENOUGH_MEMORY) { PyErr_NoMemory(); return 1; } if (error != NO_ERROR) { - PyErr_SetFromWindowsErr(error); + PyErr_SetString(PyExc_RuntimeError, "GetExtendedUdpTable failed"); return 1; } return 0; @@ -174,7 +177,7 @@ psutil_net_connections(PyObject *self, PyObject *args) { py_addr_tuple_remote = NULL; tableSize = 0; - error = __GetExtendedTcpTable(psutil_GetExtendedTcpTable, + error = __GetExtendedTcpTable(GetExtendedTcpTable, AF_INET, &table, &tableSize); if (error != 0) goto error; @@ -192,7 +195,7 @@ psutil_net_connections(PyObject *self, PyObject *args) { struct in_addr addr; addr.S_un.S_addr = tcp4Table->table[i].dwLocalAddr; - psutil_rtlIpv4AddressToStringA(&addr, addressBufferLocal); + RtlIpv4AddressToStringA(&addr, addressBufferLocal); py_addr_tuple_local = Py_BuildValue( "(si)", addressBufferLocal, @@ -214,7 +217,7 @@ psutil_net_connections(PyObject *self, PyObject *args) { struct in_addr addr; addr.S_un.S_addr = tcp4Table->table[i].dwRemoteAddr; - psutil_rtlIpv4AddressToStringA(&addr, addressBufferRemote); + RtlIpv4AddressToStringA(&addr, addressBufferRemote); py_addr_tuple_remote = Py_BuildValue( "(si)", addressBufferRemote, @@ -252,7 +255,7 @@ psutil_net_connections(PyObject *self, PyObject *args) { // TCP IPv6 if ((PySequence_Contains(py_af_filter, _AF_INET6) == 1) && (PySequence_Contains(py_type_filter, _SOCK_STREAM) == 1) && - (psutil_rtlIpv6AddressToStringA != NULL)) + (RtlIpv6AddressToStringA != NULL)) { table = NULL; py_conn_tuple = NULL; @@ -260,7 +263,7 @@ psutil_net_connections(PyObject *self, PyObject *args) { py_addr_tuple_remote = NULL; tableSize = 0; - error = __GetExtendedTcpTable(psutil_GetExtendedTcpTable, + error = __GetExtendedTcpTable(GetExtendedTcpTable, AF_INET6, &table, &tableSize); if (error != 0) goto error; @@ -279,7 +282,7 @@ psutil_net_connections(PyObject *self, PyObject *args) { struct in6_addr addr; memcpy(&addr, tcp6Table->table[i].ucLocalAddr, 16); - psutil_rtlIpv6AddressToStringA(&addr, addressBufferLocal); + RtlIpv6AddressToStringA(&addr, addressBufferLocal); py_addr_tuple_local = Py_BuildValue( "(si)", addressBufferLocal, @@ -302,7 +305,7 @@ psutil_net_connections(PyObject *self, PyObject *args) { struct in6_addr addr; memcpy(&addr, tcp6Table->table[i].ucRemoteAddr, 16); - psutil_rtlIpv6AddressToStringA(&addr, addressBufferRemote); + RtlIpv6AddressToStringA(&addr, addressBufferRemote); py_addr_tuple_remote = Py_BuildValue( "(si)", addressBufferRemote, @@ -346,7 +349,7 @@ psutil_net_connections(PyObject *self, PyObject *args) { py_addr_tuple_local = NULL; py_addr_tuple_remote = NULL; tableSize = 0; - error = __GetExtendedUdpTable(psutil_GetExtendedUdpTable, + error = __GetExtendedUdpTable(GetExtendedUdpTable, AF_INET, &table, &tableSize); if (error != 0) goto error; @@ -365,7 +368,7 @@ psutil_net_connections(PyObject *self, PyObject *args) { struct in_addr addr; addr.S_un.S_addr = udp4Table->table[i].dwLocalAddr; - psutil_rtlIpv4AddressToStringA(&addr, addressBufferLocal); + RtlIpv4AddressToStringA(&addr, addressBufferLocal); py_addr_tuple_local = Py_BuildValue( "(si)", addressBufferLocal, @@ -403,14 +406,14 @@ psutil_net_connections(PyObject *self, PyObject *args) { if ((PySequence_Contains(py_af_filter, _AF_INET6) == 1) && (PySequence_Contains(py_type_filter, _SOCK_DGRAM) == 1) && - (psutil_rtlIpv6AddressToStringA != NULL)) + (RtlIpv6AddressToStringA != NULL)) { table = NULL; py_conn_tuple = NULL; py_addr_tuple_local = NULL; py_addr_tuple_remote = NULL; tableSize = 0; - error = __GetExtendedUdpTable(psutil_GetExtendedUdpTable, + error = __GetExtendedUdpTable(GetExtendedUdpTable, AF_INET6, &table, &tableSize); if (error != 0) goto error; @@ -428,7 +431,7 @@ psutil_net_connections(PyObject *self, PyObject *args) { struct in6_addr addr; memcpy(&addr, udp6Table->table[i].ucLocalAddr, 16); - psutil_rtlIpv6AddressToStringA(&addr, addressBufferLocal); + RtlIpv6AddressToStringA(&addr, addressBufferLocal); py_addr_tuple_local = Py_BuildValue( "(si)", addressBufferLocal, |