From a8191b4edd804f890104c7b2df20d4d9ab095c6c Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 04:15:24 -0800 Subject: [PATCH 01/47] port NtQuerySystemInformation --- psutil/_psutil_windows.c | 26 +++------ psutil/arch/windows/global.c | 104 +++++++++++++++++++++++++++++++++++ psutil/arch/windows/global.h | 60 ++++++++++++++++++++ setup.py | 1 + 4 files changed, 172 insertions(+), 19 deletions(-) create mode 100644 psutil/arch/windows/global.c create mode 100644 psutil/arch/windows/global.h diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index d6058b8bc..3fe847acc 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -37,6 +37,7 @@ #include "arch/windows/ntextapi.h" #include "arch/windows/inet_ntop.h" #include "arch/windows/services.h" +#include "arch/windows/global.h" /* @@ -1044,10 +1045,6 @@ psutil_cpu_times(PyObject *self, PyObject *args) { */ static PyObject * psutil_per_cpu_times(PyObject *self, PyObject *args) { - // NtQuerySystemInformation stuff - typedef DWORD (_stdcall * NTQSI_PROC) (int, PVOID, ULONG, PULONG); - NTQSI_PROC NtQuerySystemInformation; - double idle, kernel, systemt, user, interrupt, dpc; NTSTATUS status; _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION *sppi = NULL; @@ -1058,10 +1055,6 @@ psutil_per_cpu_times(PyObject *self, PyObject *args) { if (py_retlist == NULL) return NULL; - NtQuerySystemInformation = \ - psutil_GetProcAddressFromLib("ntdll.dll", "NtQuerySystemInformation"); - if (NtQuerySystemInformation == NULL) - goto error; // retrieves number of processors ncpus = psutil_get_num_cpus(1); @@ -1078,7 +1071,7 @@ psutil_per_cpu_times(PyObject *self, PyObject *args) { } // gets cpu time informations - status = NtQuerySystemInformation( + status = psutil_NtQuerySystemInformation( SystemProcessorPerformanceInformation, sppi, ncpus * sizeof(_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION), @@ -3478,8 +3471,6 @@ psutil_net_if_stats(PyObject *self, PyObject *args) { */ static PyObject * psutil_cpu_stats(PyObject *self, PyObject *args) { - typedef DWORD (_stdcall * NTQSI_PROC) (int, PVOID, ULONG, PULONG); - NTQSI_PROC NtQuerySystemInformation; NTSTATUS status; _SYSTEM_PERFORMANCE_INFORMATION *spi = NULL; _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION *sppi = NULL; @@ -3489,11 +3480,6 @@ psutil_cpu_stats(PyObject *self, PyObject *args) { ULONG64 dpcs = 0; ULONG interrupts = 0; - NtQuerySystemInformation = \ - psutil_GetProcAddressFromLib("ntdll.dll", "NtQuerySystemInformation"); - if (NtQuerySystemInformation == NULL) - return NULL; - // retrieves number of processors ncpus = psutil_get_num_cpus(1); if (ncpus == 0) @@ -3506,7 +3492,7 @@ psutil_cpu_stats(PyObject *self, PyObject *args) { PyErr_NoMemory(); goto error; } - status = NtQuerySystemInformation( + status = psutil_NtQuerySystemInformation( SystemPerformanceInformation, spi, ncpus * sizeof(_SYSTEM_PERFORMANCE_INFORMATION), @@ -3524,7 +3510,7 @@ psutil_cpu_stats(PyObject *self, PyObject *args) { goto error; } - status = NtQuerySystemInformation( + status = psutil_NtQuerySystemInformation( SystemInterruptInformation, InterruptInformation, ncpus * sizeof(SYSTEM_INTERRUPT_INFORMATION), @@ -3545,7 +3531,7 @@ psutil_cpu_stats(PyObject *self, PyObject *args) { goto error; } - status = NtQuerySystemInformation( + status = psutil_NtQuerySystemInformation( SystemProcessorPerformanceInformation, sppi, ncpus * sizeof(_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION), @@ -3939,6 +3925,8 @@ void init_psutil_windows(void) // set SeDebug for the current process psutil_set_se_debug(); psutil_setup(); + if (psutil_load_globals() != 0) + return NULL; #if PY_MAJOR_VERSION >= 3 return module; diff --git a/psutil/arch/windows/global.c b/psutil/arch/windows/global.c new file mode 100644 index 000000000..4e4643552 --- /dev/null +++ b/psutil/arch/windows/global.c @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include +#include +#include +#include "ntextapi.h" +#include "global.h" + + +// A wrapper around GetModuleHandle and GetProcAddress. +static PVOID +ps_GetProcAddress(LPCSTR libname, LPCSTR procname) { + HMODULE mod; + FARPROC addr; + + if ((mod = GetModuleHandleA(libname)) == NULL) { + PyErr_SetFromWindowsErrWithFilename(0, libname); + return NULL; + } + if ((addr = GetProcAddress(mod, procname)) == NULL) { + PyErr_SetFromWindowsErrWithFilename(0, procname); + return NULL; + } + return addr; +} + + +// A wrapper around LoadLibrary and GetProcAddress. +static PVOID +ps_GetProcAddressFromLib(LPCSTR libname, LPCSTR procname) { + HMODULE mod; + FARPROC addr; + + if ((mod = LoadLibraryA(libname)) == NULL) { + PyErr_SetFromWindowsErrWithFilename(0, libname); + return NULL; + } + if ((addr = GetProcAddress(mod, procname)) == NULL) { + PyErr_SetFromWindowsErrWithFilename(0, procname); + FreeLibrary(mod); + return NULL; + } + // FreeLibrary(mod); + return addr; +} + + +int +psutil_load_globals() { + // Mandatory. + + psutil_NtQuerySystemInformation = ps_GetProcAddressFromLib( + "ntdll.dll", "NtQuerySystemInformation"); + if (psutil_NtQuerySystemInformation == NULL) + return 1; + +/* + psutil_NtQueryInformationProcess = ps_GetProcAddress( + "ntdll.dll", "NtQueryInformationProcess"); + if (! psutil_NtQueryInformationProcess) + return 1; + + psutil_NtSetInformationProcess = ps_GetProcAddress( + "ntdll.dll", "NtSetInformationProcess"); + if (! psutil_NtSetInformationProcess) + return 1; + + psutil_WinStationQueryInformationW = ps_GetProcAddressFromLib( + "winsta.dll", "WinStationQueryInformationW"); + if (! psutil_WinStationQueryInformationW) + return 1; + + psutil_rtlIpv4AddressToStringA = ps_GetProcAddressFromLib( + "ntdll.dll", "RtlIpv4AddressToStringA"); + if (! psutil_rtlIpv4AddressToStringA) + return 1; + + psutil_rtlIpv6AddressToStringA = ps_GetProcAddressFromLib( + "ntdll.dll", "RtlIpv6AddressToStringA"); + if (! psutil_rtlIpv6AddressToStringA) + return 1; + + psutil_GetExtendedTcpTable = ps_GetProcAddressFromLib( + "iphlpapi.dll", "GetExtendedTcpTable"); + if (! psutil_GetExtendedTcpTable) + return 1; + + psutil_GetExtendedUdpTable = ps_GetProcAddressFromLib( + "iphlpapi.dll", "GetExtendedUdpTable"); + if (! psutil_GetExtendedUdpTable) + return 1; + + // Optionals. + + psutil_GetActiveProcessorCount = ps_GetProcAddress( + "kernel32", "GetActiveProcessorCount"); +*/ + + return 0; +} diff --git a/psutil/arch/windows/global.h b/psutil/arch/windows/global.h new file mode 100644 index 000000000..b61d844bd --- /dev/null +++ b/psutil/arch/windows/global.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "ntextapi.h" +#include + +typedef DWORD (_stdcall * NTQSI_PROC) (int, PVOID, ULONG, PULONG); + +// probably unnecessary? +/* +#ifndef NT_SUCCESS +#define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0) +#endif + +typedef PSTR (NTAPI * _RtlIpv4AddressToStringA)(struct in_addr *, PSTR); +typedef PSTR (NTAPI * _RtlIpv6AddressToStringA)(struct in6_addr *, PSTR); +typedef DWORD (CALLBACK *_GetActiveProcessorCount)(WORD); +typedef ULONGLONG (CALLBACK *_GetTickCount64)(void); +typedef DWORD (WINAPI * _GetExtendedTcpTable)(PVOID, PDWORD, BOOL, ULONG, + TCP_TABLE_CLASS, ULONG); +typedef DWORD (WINAPI * _GetExtendedUdpTable)(PVOID, PDWORD, BOOL, ULONG, + UDP_TABLE_CLASS, ULONG); + + +_RtlIpv4AddressToStringA \ + psutil_rtlIpv4AddressToStringA; + +_NtQueryInformationProcess \ + psutil_NtQueryInformationProcess; + +_RtlIpv6AddressToStringA \ + psutil_rtlIpv6AddressToStringA; + +_NtSetInformationProcess + psutil_NtSetInformationProcess; + +_GetActiveProcessorCount \ + psutil_GetActiveProcessorCount; + +PWINSTATIONQUERYINFORMATIONW \ + psutil_WinStationQueryInformationW; + +_GetTickCount64 \ + psutil_GetTickCount64; + +_GetExtendedTcpTable \ + psutil_GetExtendedTcpTable; + +_GetExtendedUdpTable \ + psutil_GetExtendedUdpTable; +*/ + +NTQSI_PROC \ + psutil_NtQuerySystemInformation; + + +int psutil_load_globals(); diff --git a/setup.py b/setup.py index e28532369..764f8b31d 100755 --- a/setup.py +++ b/setup.py @@ -134,6 +134,7 @@ def get_winver(): 'psutil/arch/windows/security.c', 'psutil/arch/windows/inet_ntop.c', 'psutil/arch/windows/services.c', + 'psutil/arch/windows/global.c', ], define_macros=macros, libraries=[ From 86c95587642642c2027845d0fac16ac0d1ac2cec Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 04:16:51 -0800 Subject: [PATCH 02/47] port NtQuerySystemInformation --- psutil/arch/windows/process_info.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/psutil/arch/windows/process_info.c b/psutil/arch/windows/process_info.c index a2edca58d..4a249fb0b 100644 --- a/psutil/arch/windows/process_info.c +++ b/psutil/arch/windows/process_info.c @@ -971,13 +971,6 @@ psutil_get_proc_info(DWORD pid, PSYSTEM_PROCESS_INFORMATION *retProcess, PVOID buffer; ULONG bufferSize; PSYSTEM_PROCESS_INFORMATION process; - typedef DWORD (_stdcall * NTQSI_PROC) (int, PVOID, ULONG, PULONG); - NTQSI_PROC NtQuerySystemInformation; - - NtQuerySystemInformation = \ - psutil_GetProcAddressFromLib("ntdll.dll", "NtQuerySystemInformation"); - if (NtQuerySystemInformation == NULL) - goto error; bufferSize = initialBufferSize; buffer = malloc(bufferSize); @@ -987,8 +980,11 @@ psutil_get_proc_info(DWORD pid, PSYSTEM_PROCESS_INFORMATION *retProcess, } while (TRUE) { - status = NtQuerySystemInformation(SystemProcessInformation, buffer, - bufferSize, &bufferSize); + status = psutil_NtQuerySystemInformation( + SystemProcessInformation, + buffer, + bufferSize, + &bufferSize); if (status == STATUS_BUFFER_TOO_SMALL || status == STATUS_INFO_LENGTH_MISMATCH) { From 4277cf2a64866f6945bb2d17a3151033ca930991 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 04:20:32 -0800 Subject: [PATCH 03/47] port NtQuerySystemInformation --- psutil/_psutil_windows.c | 2 +- psutil/arch/windows/process_handles.c | 14 ++++++-------- psutil/arch/windows/process_info.c | 1 + 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 3fe847acc..a6b314708 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -35,9 +35,9 @@ #include "arch/windows/process_info.h" #include "arch/windows/process_handles.h" #include "arch/windows/ntextapi.h" +#include "arch/windows/global.h" #include "arch/windows/inet_ntop.h" #include "arch/windows/services.h" -#include "arch/windows/global.h" /* diff --git a/psutil/arch/windows/process_handles.c b/psutil/arch/windows/process_handles.c index 4ac02b910..ad2bf238a 100644 --- a/psutil/arch/windows/process_handles.c +++ b/psutil/arch/windows/process_handles.c @@ -4,11 +4,12 @@ * found in the LICENSE file. * */ + #include "process_handles.h" #include "process_info.h" +#include "global.h" #include "../../_psutil_common.h" -static _NtQuerySystemInformation __NtQuerySystemInformation = NULL; static _NtQueryObject __NtQueryObject = NULL; CRITICAL_SECTION g_cs; @@ -45,8 +46,6 @@ psutil_get_open_files_init(BOOL threaded) { return; // Resolve the Windows API calls - __NtQuerySystemInformation = psutil_GetProcAddressFromLib( - "ntdll.dll", "NtQuerySystemInformation"); __NtQueryObject = psutil_GetProcAddressFromLib( "ntdll.dll", "NtQueryObject"); @@ -81,8 +80,7 @@ psutil_get_open_files_ntqueryobject(long dwPid, HANDLE hProcess) { // to psutil_get_open_files() is running EnterCriticalSection(&g_cs); - if (__NtQuerySystemInformation == NULL || - __NtQueryObject == NULL || + if (__NtQueryObject == NULL || g_hEvtStart == NULL || g_hEvtFinish == NULL) @@ -117,7 +115,7 @@ psutil_get_open_files_ntqueryobject(long dwPid, HANDLE hProcess) { error = TRUE; goto cleanup; } - } while ((status = __NtQuerySystemInformation( + } while ((status = psutil_NtQuerySystemInformation( SystemExtendedHandleInformation, pHandleInfo, dwInfoSize, @@ -339,7 +337,7 @@ psutil_get_open_files_getmappedfilename(long dwPid, HANDLE hProcess) { if (g_initialized == FALSE) psutil_get_open_files_init(FALSE); - if (__NtQuerySystemInformation == NULL || __NtQueryObject == NULL) { + if (__NtQueryObject == NULL) { PyErr_SetFromWindowsErr(0); error = TRUE; goto cleanup; @@ -370,7 +368,7 @@ psutil_get_open_files_getmappedfilename(long dwPid, HANDLE hProcess) { error = TRUE; goto cleanup; } - } while ((status = __NtQuerySystemInformation( + } while ((status = psutil_NtQuerySystemInformation( SystemExtendedHandleInformation, pHandleInfo, dwInfoSize, diff --git a/psutil/arch/windows/process_info.c b/psutil/arch/windows/process_info.c index 4a249fb0b..afc37fd38 100644 --- a/psutil/arch/windows/process_info.c +++ b/psutil/arch/windows/process_info.c @@ -15,6 +15,7 @@ #include "security.h" #include "process_info.h" #include "ntextapi.h" +#include "global.h" #include "../../_psutil_common.h" From 5ebac31f1e349a78aa8acdbfa640e7b8aacfa774 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 04:25:45 -0800 Subject: [PATCH 04/47] port NtQueryInformationProcess --- psutil/_psutil_windows.c | 8 +------ psutil/arch/windows/global.c | 2 +- psutil/arch/windows/global.h | 6 ++--- psutil/arch/windows/process_info.c | 38 ++++++++++++------------------ 4 files changed, 20 insertions(+), 34 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index a6b314708..c7667d78a 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -2116,19 +2116,13 @@ psutil_proc_io_priority_get(PyObject *self, PyObject *args) { long pid; HANDLE hProcess; DWORD IoPriority; - _NtQueryInformationProcess NtQueryInformationProcess; - NtQueryInformationProcess = \ - psutil_GetProcAddress("ntdll.dll", "NtQueryInformationProcess"); - if (NtQueryInformationProcess == NULL) - return NULL; if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; hProcess = psutil_handle_from_pid(pid, PROCESS_QUERY_LIMITED_INFORMATION); if (hProcess == NULL) return NULL; - - NtQueryInformationProcess( + psutil_NtQueryInformationProcess( hProcess, ProcessIoPriority, &IoPriority, diff --git a/psutil/arch/windows/global.c b/psutil/arch/windows/global.c index 4e4643552..ffc29263c 100644 --- a/psutil/arch/windows/global.c +++ b/psutil/arch/windows/global.c @@ -58,12 +58,12 @@ psutil_load_globals() { if (psutil_NtQuerySystemInformation == NULL) return 1; -/* psutil_NtQueryInformationProcess = ps_GetProcAddress( "ntdll.dll", "NtQueryInformationProcess"); if (! psutil_NtQueryInformationProcess) return 1; +/* psutil_NtSetInformationProcess = ps_GetProcAddress( "ntdll.dll", "NtSetInformationProcess"); if (! psutil_NtSetInformationProcess) diff --git a/psutil/arch/windows/global.h b/psutil/arch/windows/global.h index b61d844bd..aee5b986b 100644 --- a/psutil/arch/windows/global.h +++ b/psutil/arch/windows/global.h @@ -28,9 +28,6 @@ typedef DWORD (WINAPI * _GetExtendedUdpTable)(PVOID, PDWORD, BOOL, ULONG, _RtlIpv4AddressToStringA \ psutil_rtlIpv4AddressToStringA; -_NtQueryInformationProcess \ - psutil_NtQueryInformationProcess; - _RtlIpv6AddressToStringA \ psutil_rtlIpv6AddressToStringA; @@ -56,5 +53,8 @@ _GetExtendedUdpTable \ NTQSI_PROC \ psutil_NtQuerySystemInformation; +_NtQueryInformationProcess \ + psutil_NtQueryInformationProcess; + int psutil_load_globals(); diff --git a/psutil/arch/windows/process_info.c b/psutil/arch/windows/process_info.c index afc37fd38..5b5e82391 100644 --- a/psutil/arch/windows/process_info.c +++ b/psutil/arch/windows/process_info.c @@ -518,7 +518,6 @@ psutil_get_process_data(long pid, http://stackoverflow.com/a/14012919 http://www.drdobbs.com/embracing-64-bit-windows/184401966 */ - _NtQueryInformationProcess NtQueryInformationProcess = NULL; #ifndef _WIN64 static _NtQueryInformationProcess NtWow64QueryInformationProcess64 = NULL; static _NtWow64ReadVirtualMemory64 NtWow64ReadVirtualMemory64 = NULL; @@ -536,11 +535,6 @@ psutil_get_process_data(long pid, #endif DWORD access = PROCESS_QUERY_INFORMATION | PROCESS_VM_READ; - NtQueryInformationProcess = \ - psutil_GetProcAddress("ntdll.dll", "NtQueryInformationProcess"); - if (NtQueryInformationProcess == NULL) - return -1; - hProcess = psutil_handle_from_pid(pid, access); if (hProcess == NULL) return -1; @@ -548,11 +542,13 @@ psutil_get_process_data(long pid, #ifdef _WIN64 /* 64 bit case. Check if the target is a 32 bit process running in WoW64 * mode. */ - if (!NT_SUCCESS(NtQueryInformationProcess(hProcess, - ProcessWow64Information, - &ppeb32, - sizeof(LPVOID), - NULL))) { + if (!NT_SUCCESS(psutil_NtQueryInformationProcess( + hProcess, + ProcessWow64Information, + &ppeb32, + sizeof(LPVOID), + NULL))) + { PyErr_SetFromWindowsErr(0); goto error; } @@ -679,11 +675,13 @@ psutil_get_process_data(long pid, PEB_ peb; RTL_USER_PROCESS_PARAMETERS_ procParameters; - if (!NT_SUCCESS(NtQueryInformationProcess(hProcess, - ProcessBasicInformation, - &pbi, - sizeof(pbi), - NULL))) { + if (!NT_SUCCESS(psutil_NtQueryInformationProcess( + hProcess, + ProcessBasicInformation, + &pbi, + sizeof(pbi), + NULL))) + { PyErr_SetFromWindowsErr(0); goto error; } @@ -789,12 +787,6 @@ psutil_get_cmdline_data(long pid, WCHAR **pdata, SIZE_T *psize) { WCHAR * cmdline_buffer_wchar = NULL; PUNICODE_STRING tmp = NULL; DWORD string_size; - _NtQueryInformationProcess NtQueryInformationProcess; - - NtQueryInformationProcess = \ - psutil_GetProcAddress("ntdll.dll", "NtQueryInformationProcess"); - if (NtQueryInformationProcess == NULL) - goto error; cmdline_buffer = calloc(ret_length, 1); if (cmdline_buffer == NULL) { @@ -805,7 +797,7 @@ psutil_get_cmdline_data(long pid, WCHAR **pdata, SIZE_T *psize) { hProcess = psutil_handle_from_pid(pid, PROCESS_QUERY_LIMITED_INFORMATION); if (hProcess == NULL) goto error; - status = NtQueryInformationProcess( + status = psutil_NtQueryInformationProcess( hProcess, 60, // ProcessCommandLineInformation cmdline_buffer, From cf5fa7341eb0a05635704f6bbd683c5438d35353 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 04:27:36 -0800 Subject: [PATCH 05/47] port NtSetInformationProcess --- psutil/_psutil_windows.c | 7 +------ psutil/arch/windows/global.c | 3 ++- psutil/arch/windows/global.h | 6 +++--- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index c7667d78a..118a9e596 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -2143,19 +2143,14 @@ psutil_proc_io_priority_set(PyObject *self, PyObject *args) { DWORD prio; HANDLE hProcess; DWORD access = PROCESS_QUERY_INFORMATION | PROCESS_SET_INFORMATION; - _NtSetInformationProcess NtSetInformationProcess; - NtSetInformationProcess = \ - psutil_GetProcAddress("ntdll.dll", "NtSetInformationProcess"); - if (NtSetInformationProcess == NULL) - return NULL; if (! PyArg_ParseTuple(args, "li", &pid, &prio)) return NULL; hProcess = psutil_handle_from_pid(pid, access); if (hProcess == NULL) return NULL; - NtSetInformationProcess( + psutil_NtSetInformationProcess( hProcess, ProcessIoPriority, (PVOID)&prio, diff --git a/psutil/arch/windows/global.c b/psutil/arch/windows/global.c index ffc29263c..cfddcb951 100644 --- a/psutil/arch/windows/global.c +++ b/psutil/arch/windows/global.c @@ -63,12 +63,13 @@ psutil_load_globals() { if (! psutil_NtQueryInformationProcess) return 1; -/* psutil_NtSetInformationProcess = ps_GetProcAddress( "ntdll.dll", "NtSetInformationProcess"); if (! psutil_NtSetInformationProcess) return 1; + +/* psutil_WinStationQueryInformationW = ps_GetProcAddressFromLib( "winsta.dll", "WinStationQueryInformationW"); if (! psutil_WinStationQueryInformationW) diff --git a/psutil/arch/windows/global.h b/psutil/arch/windows/global.h index aee5b986b..7d14b464f 100644 --- a/psutil/arch/windows/global.h +++ b/psutil/arch/windows/global.h @@ -31,9 +31,6 @@ _RtlIpv4AddressToStringA \ _RtlIpv6AddressToStringA \ psutil_rtlIpv6AddressToStringA; -_NtSetInformationProcess - psutil_NtSetInformationProcess; - _GetActiveProcessorCount \ psutil_GetActiveProcessorCount; @@ -56,5 +53,8 @@ NTQSI_PROC \ _NtQueryInformationProcess \ psutil_NtQueryInformationProcess; +_NtSetInformationProcess + psutil_NtSetInformationProcess; + int psutil_load_globals(); From b2979388a263c669d40ffe9900afe02224033214 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 04:29:28 -0800 Subject: [PATCH 06/47] port WinStationQueryInformationW --- psutil/_psutil_windows.c | 18 +++++++----------- psutil/arch/windows/global.c | 4 ++-- psutil/arch/windows/global.h | 6 +++--- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 118a9e596..db2f27a85 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -2757,11 +2757,6 @@ psutil_users(PyObject *self, PyObject *args) { if (py_retlist == NULL) return NULL; - WinStationQueryInformationW = psutil_GetProcAddressFromLib( - "winsta.dll", "WinStationQueryInformationW"); - if (WinStationQueryInformationW == NULL) - goto error; - if (WTSEnumerateSessions(hServer, 0, 1, &sessions, &count) == 0) { PyErr_SetFromWindowsErr(0); goto error; @@ -2815,12 +2810,13 @@ psutil_users(PyObject *self, PyObject *args) { } // login time - if (!WinStationQueryInformationW(hServer, - sessionId, - WinStationInformation, - &station_info, - sizeof(station_info), - &returnLen)) + if (! psutil_WinStationQueryInformationW( + hServer, + sessionId, + WinStationInformation, + &station_info, + sizeof(station_info), + &returnLen)) { goto error; } diff --git a/psutil/arch/windows/global.c b/psutil/arch/windows/global.c index cfddcb951..bf19c777c 100644 --- a/psutil/arch/windows/global.c +++ b/psutil/arch/windows/global.c @@ -68,13 +68,13 @@ psutil_load_globals() { if (! psutil_NtSetInformationProcess) return 1; - -/* psutil_WinStationQueryInformationW = ps_GetProcAddressFromLib( "winsta.dll", "WinStationQueryInformationW"); if (! psutil_WinStationQueryInformationW) return 1; + +/* psutil_rtlIpv4AddressToStringA = ps_GetProcAddressFromLib( "ntdll.dll", "RtlIpv4AddressToStringA"); if (! psutil_rtlIpv4AddressToStringA) diff --git a/psutil/arch/windows/global.h b/psutil/arch/windows/global.h index 7d14b464f..69211d867 100644 --- a/psutil/arch/windows/global.h +++ b/psutil/arch/windows/global.h @@ -34,9 +34,6 @@ _RtlIpv6AddressToStringA \ _GetActiveProcessorCount \ psutil_GetActiveProcessorCount; -PWINSTATIONQUERYINFORMATIONW \ - psutil_WinStationQueryInformationW; - _GetTickCount64 \ psutil_GetTickCount64; @@ -56,5 +53,8 @@ _NtQueryInformationProcess \ _NtSetInformationProcess psutil_NtSetInformationProcess; +PWINSTATIONQUERYINFORMATIONW \ + psutil_WinStationQueryInformationW; + int psutil_load_globals(); From 850e6a8fc3d65e99edf43751dae4886f68f4daab Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 04:33:01 -0800 Subject: [PATCH 07/47] port RtlIpv4AddressToStringA and rtlIpv6AddressToStringA --- psutil/_psutil_windows.c | 24 ++++++------------------ psutil/arch/windows/global.c | 3 +-- psutil/arch/windows/global.h | 16 ++++++++-------- 3 files changed, 15 insertions(+), 28 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index db2f27a85..0b2c9cd0d 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -1614,10 +1614,6 @@ psutil_net_connections(PyObject *self, PyObject *args) { static long null_address[4] = { 0, 0, 0, 0 }; unsigned long pid; int pid_return; - typedef PSTR (NTAPI * _RtlIpv4AddressToStringA)(struct in_addr *, PSTR); - _RtlIpv4AddressToStringA rtlIpv4AddressToStringA; - typedef PSTR (NTAPI * _RtlIpv6AddressToStringA)(struct in6_addr *, PSTR); - _RtlIpv6AddressToStringA rtlIpv6AddressToStringA; _GetExtendedTcpTable getExtendedTcpTable; _GetExtendedUdpTable getExtendedUdpTable; PVOID table = NULL; @@ -1643,14 +1639,6 @@ psutil_net_connections(PyObject *self, PyObject *args) { PyObject *_SOCK_DGRAM = PyLong_FromLong((long)SOCK_DGRAM); // Import some functions. - rtlIpv4AddressToStringA = psutil_GetProcAddressFromLib( - "ntdll.dll", "RtlIpv4AddressToStringA"); - if (rtlIpv4AddressToStringA == NULL) - goto error; - rtlIpv6AddressToStringA = psutil_GetProcAddressFromLib( - "ntdll.dll", "RtlIpv6AddressToStringA"); - if (rtlIpv6AddressToStringA == NULL) - goto error; getExtendedTcpTable = psutil_GetProcAddressFromLib( "iphlpapi.dll", "GetExtendedTcpTable"); if (getExtendedTcpTable == NULL) @@ -1730,7 +1718,7 @@ psutil_net_connections(PyObject *self, PyObject *args) { struct in_addr addr; addr.S_un.S_addr = tcp4Table->table[i].dwLocalAddr; - rtlIpv4AddressToStringA(&addr, addressBufferLocal); + psutil_rtlIpv4AddressToStringA(&addr, addressBufferLocal); py_addr_tuple_local = Py_BuildValue( "(si)", addressBufferLocal, @@ -1752,7 +1740,7 @@ psutil_net_connections(PyObject *self, PyObject *args) { struct in_addr addr; addr.S_un.S_addr = tcp4Table->table[i].dwRemoteAddr; - rtlIpv4AddressToStringA(&addr, addressBufferRemote); + psutil_rtlIpv4AddressToStringA(&addr, addressBufferRemote); py_addr_tuple_remote = Py_BuildValue( "(si)", addressBufferRemote, @@ -1827,7 +1815,7 @@ psutil_net_connections(PyObject *self, PyObject *args) { struct in6_addr addr; memcpy(&addr, tcp6Table->table[i].ucLocalAddr, 16); - rtlIpv6AddressToStringA(&addr, addressBufferLocal); + psutil_rtlIpv6AddressToStringA(&addr, addressBufferLocal); py_addr_tuple_local = Py_BuildValue( "(si)", addressBufferLocal, @@ -1850,7 +1838,7 @@ psutil_net_connections(PyObject *self, PyObject *args) { struct in6_addr addr; memcpy(&addr, tcp6Table->table[i].ucRemoteAddr, 16); - rtlIpv6AddressToStringA(&addr, addressBufferRemote); + psutil_rtlIpv6AddressToStringA(&addr, addressBufferRemote); py_addr_tuple_remote = Py_BuildValue( "(si)", addressBufferRemote, @@ -1924,7 +1912,7 @@ psutil_net_connections(PyObject *self, PyObject *args) { struct in_addr addr; addr.S_un.S_addr = udp4Table->table[i].dwLocalAddr; - rtlIpv4AddressToStringA(&addr, addressBufferLocal); + psutil_rtlIpv4AddressToStringA(&addr, addressBufferLocal); py_addr_tuple_local = Py_BuildValue( "(si)", addressBufferLocal, @@ -1997,7 +1985,7 @@ psutil_net_connections(PyObject *self, PyObject *args) { struct in6_addr addr; memcpy(&addr, udp6Table->table[i].ucLocalAddr, 16); - rtlIpv6AddressToStringA(&addr, addressBufferLocal); + psutil_rtlIpv6AddressToStringA(&addr, addressBufferLocal); py_addr_tuple_local = Py_BuildValue( "(si)", addressBufferLocal, diff --git a/psutil/arch/windows/global.c b/psutil/arch/windows/global.c index bf19c777c..d7e92737f 100644 --- a/psutil/arch/windows/global.c +++ b/psutil/arch/windows/global.c @@ -73,8 +73,6 @@ psutil_load_globals() { if (! psutil_WinStationQueryInformationW) return 1; - -/* psutil_rtlIpv4AddressToStringA = ps_GetProcAddressFromLib( "ntdll.dll", "RtlIpv4AddressToStringA"); if (! psutil_rtlIpv4AddressToStringA) @@ -84,6 +82,7 @@ psutil_load_globals() { "ntdll.dll", "RtlIpv6AddressToStringA"); if (! psutil_rtlIpv6AddressToStringA) return 1; +/* psutil_GetExtendedTcpTable = ps_GetProcAddressFromLib( "iphlpapi.dll", "GetExtendedTcpTable"); diff --git a/psutil/arch/windows/global.h b/psutil/arch/windows/global.h index 69211d867..6e4fa1433 100644 --- a/psutil/arch/windows/global.h +++ b/psutil/arch/windows/global.h @@ -8,6 +8,8 @@ #include typedef DWORD (_stdcall * NTQSI_PROC) (int, PVOID, ULONG, PULONG); +typedef PSTR (NTAPI * _RtlIpv4AddressToStringA)(struct in_addr *, PSTR); +typedef PSTR (NTAPI * _RtlIpv6AddressToStringA)(struct in6_addr *, PSTR); // probably unnecessary? /* @@ -15,8 +17,6 @@ typedef DWORD (_stdcall * NTQSI_PROC) (int, PVOID, ULONG, PULONG); #define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0) #endif -typedef PSTR (NTAPI * _RtlIpv4AddressToStringA)(struct in_addr *, PSTR); -typedef PSTR (NTAPI * _RtlIpv6AddressToStringA)(struct in6_addr *, PSTR); typedef DWORD (CALLBACK *_GetActiveProcessorCount)(WORD); typedef ULONGLONG (CALLBACK *_GetTickCount64)(void); typedef DWORD (WINAPI * _GetExtendedTcpTable)(PVOID, PDWORD, BOOL, ULONG, @@ -25,12 +25,6 @@ typedef DWORD (WINAPI * _GetExtendedUdpTable)(PVOID, PDWORD, BOOL, ULONG, UDP_TABLE_CLASS, ULONG); -_RtlIpv4AddressToStringA \ - psutil_rtlIpv4AddressToStringA; - -_RtlIpv6AddressToStringA \ - psutil_rtlIpv6AddressToStringA; - _GetActiveProcessorCount \ psutil_GetActiveProcessorCount; @@ -56,5 +50,11 @@ _NtSetInformationProcess PWINSTATIONQUERYINFORMATIONW \ psutil_WinStationQueryInformationW; +_RtlIpv4AddressToStringA \ + psutil_rtlIpv4AddressToStringA; + +_RtlIpv6AddressToStringA \ + psutil_rtlIpv6AddressToStringA; + int psutil_load_globals(); From fd48e950c5f1aa41ad456ae2fe2ee6f377094481 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 04:42:57 -0800 Subject: [PATCH 08/47] port GetExtendedTcpTable --- psutil/_psutil_windows.c | 11 +++-------- psutil/arch/windows/global.c | 2 +- psutil/arch/windows/global.h | 10 +++++----- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 0b2c9cd0d..f99b88cc5 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -1614,7 +1614,6 @@ psutil_net_connections(PyObject *self, PyObject *args) { static long null_address[4] = { 0, 0, 0, 0 }; unsigned long pid; int pid_return; - _GetExtendedTcpTable getExtendedTcpTable; _GetExtendedUdpTable getExtendedUdpTable; PVOID table = NULL; DWORD tableSize; @@ -1639,10 +1638,6 @@ psutil_net_connections(PyObject *self, PyObject *args) { PyObject *_SOCK_DGRAM = PyLong_FromLong((long)SOCK_DGRAM); // Import some functions. - getExtendedTcpTable = psutil_GetProcAddressFromLib( - "iphlpapi.dll", "GetExtendedTcpTable"); - if (getExtendedTcpTable == NULL) - goto error; getExtendedUdpTable = psutil_GetProcAddressFromLib( "iphlpapi.dll", "GetExtendedUdpTable"); if (getExtendedUdpTable == NULL) @@ -1669,7 +1664,7 @@ psutil_net_connections(PyObject *self, PyObject *args) { } } - if ((getExtendedTcpTable == NULL) || (getExtendedUdpTable == NULL)) { + if (getExtendedUdpTable == NULL) { PyErr_SetString(PyExc_NotImplementedError, "feature not supported on this Windows version"); _psutil_conn_decref_objs(); @@ -1693,7 +1688,7 @@ psutil_net_connections(PyObject *self, PyObject *args) { py_addr_tuple_remote = NULL; tableSize = 0; - error = __GetExtendedTcpTable(getExtendedTcpTable, + error = __GetExtendedTcpTable(psutil_GetExtendedTcpTable, AF_INET, &table, &tableSize); if (error == ERROR_NOT_ENOUGH_MEMORY) { PyErr_NoMemory(); @@ -1790,7 +1785,7 @@ psutil_net_connections(PyObject *self, PyObject *args) { py_addr_tuple_remote = NULL; tableSize = 0; - error = __GetExtendedTcpTable(getExtendedTcpTable, + error = __GetExtendedTcpTable(psutil_GetExtendedTcpTable, AF_INET6, &table, &tableSize); if (error == ERROR_NOT_ENOUGH_MEMORY) { PyErr_NoMemory(); diff --git a/psutil/arch/windows/global.c b/psutil/arch/windows/global.c index d7e92737f..d34aade7c 100644 --- a/psutil/arch/windows/global.c +++ b/psutil/arch/windows/global.c @@ -82,13 +82,13 @@ psutil_load_globals() { "ntdll.dll", "RtlIpv6AddressToStringA"); if (! psutil_rtlIpv6AddressToStringA) return 1; -/* psutil_GetExtendedTcpTable = ps_GetProcAddressFromLib( "iphlpapi.dll", "GetExtendedTcpTable"); if (! psutil_GetExtendedTcpTable) return 1; +/* psutil_GetExtendedUdpTable = ps_GetProcAddressFromLib( "iphlpapi.dll", "GetExtendedUdpTable"); if (! psutil_GetExtendedUdpTable) diff --git a/psutil/arch/windows/global.h b/psutil/arch/windows/global.h index 6e4fa1433..341e3a276 100644 --- a/psutil/arch/windows/global.h +++ b/psutil/arch/windows/global.h @@ -10,6 +10,8 @@ typedef DWORD (_stdcall * NTQSI_PROC) (int, PVOID, ULONG, PULONG); typedef PSTR (NTAPI * _RtlIpv4AddressToStringA)(struct in_addr *, PSTR); typedef PSTR (NTAPI * _RtlIpv6AddressToStringA)(struct in6_addr *, PSTR); +typedef DWORD (WINAPI * _GetExtendedTcpTable)(PVOID, PDWORD, BOOL, ULONG, + TCP_TABLE_CLASS, ULONG); // probably unnecessary? /* @@ -19,8 +21,6 @@ typedef PSTR (NTAPI * _RtlIpv6AddressToStringA)(struct in6_addr *, PSTR); typedef DWORD (CALLBACK *_GetActiveProcessorCount)(WORD); typedef ULONGLONG (CALLBACK *_GetTickCount64)(void); -typedef DWORD (WINAPI * _GetExtendedTcpTable)(PVOID, PDWORD, BOOL, ULONG, - TCP_TABLE_CLASS, ULONG); typedef DWORD (WINAPI * _GetExtendedUdpTable)(PVOID, PDWORD, BOOL, ULONG, UDP_TABLE_CLASS, ULONG); @@ -31,9 +31,6 @@ _GetActiveProcessorCount \ _GetTickCount64 \ psutil_GetTickCount64; -_GetExtendedTcpTable \ - psutil_GetExtendedTcpTable; - _GetExtendedUdpTable \ psutil_GetExtendedUdpTable; */ @@ -56,5 +53,8 @@ _RtlIpv4AddressToStringA \ _RtlIpv6AddressToStringA \ psutil_rtlIpv6AddressToStringA; +_GetExtendedTcpTable \ + psutil_GetExtendedTcpTable; + int psutil_load_globals(); From 8c54a5f7b96af534252b20ea8f87a93fbb10a1aa Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 04:45:05 -0800 Subject: [PATCH 09/47] port GetExtendedUdpTable --- psutil/_psutil_windows.c | 21 ++------------------- psutil/arch/windows/global.c | 2 +- psutil/arch/windows/global.h | 10 +++++----- 3 files changed, 8 insertions(+), 25 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index f99b88cc5..d995f2007 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -1568,10 +1568,6 @@ static DWORD __GetExtendedTcpTable(_GetExtendedTcpTable call, } -typedef DWORD (WINAPI * _GetExtendedUdpTable)(PVOID, PDWORD, BOOL, ULONG, - UDP_TABLE_CLASS, ULONG); - - // https://msdn.microsoft.com/library/aa365930.aspx static DWORD __GetExtendedUdpTable(_GetExtendedUdpTable call, ULONG address_family, @@ -1614,7 +1610,6 @@ psutil_net_connections(PyObject *self, PyObject *args) { static long null_address[4] = { 0, 0, 0, 0 }; unsigned long pid; int pid_return; - _GetExtendedUdpTable getExtendedUdpTable; PVOID table = NULL; DWORD tableSize; DWORD error; @@ -1638,11 +1633,6 @@ psutil_net_connections(PyObject *self, PyObject *args) { PyObject *_SOCK_DGRAM = PyLong_FromLong((long)SOCK_DGRAM); // Import some functions. - getExtendedUdpTable = psutil_GetProcAddressFromLib( - "iphlpapi.dll", "GetExtendedUdpTable"); - if (getExtendedUdpTable == NULL) - goto error; - if (! PyArg_ParseTuple(args, "lOO", &pid, &py_af_filter, &py_type_filter)) goto error; @@ -1664,13 +1654,6 @@ psutil_net_connections(PyObject *self, PyObject *args) { } } - if (getExtendedUdpTable == NULL) { - PyErr_SetString(PyExc_NotImplementedError, - "feature not supported on this Windows version"); - _psutil_conn_decref_objs(); - return NULL; - } - py_retlist = PyList_New(0); if (py_retlist == NULL) { _psutil_conn_decref_objs(); @@ -1882,7 +1865,7 @@ psutil_net_connections(PyObject *self, PyObject *args) { py_addr_tuple_local = NULL; py_addr_tuple_remote = NULL; tableSize = 0; - error = __GetExtendedUdpTable(getExtendedUdpTable, + error = __GetExtendedUdpTable(psutil_GetExtendedUdpTable, AF_INET, &table, &tableSize); if (error == ERROR_NOT_ENOUGH_MEMORY) { PyErr_NoMemory(); @@ -1956,7 +1939,7 @@ psutil_net_connections(PyObject *self, PyObject *args) { py_addr_tuple_local = NULL; py_addr_tuple_remote = NULL; tableSize = 0; - error = __GetExtendedUdpTable(getExtendedUdpTable, + error = __GetExtendedUdpTable(psutil_GetExtendedUdpTable, AF_INET6, &table, &tableSize); if (error == ERROR_NOT_ENOUGH_MEMORY) { PyErr_NoMemory(); diff --git a/psutil/arch/windows/global.c b/psutil/arch/windows/global.c index d34aade7c..026a37265 100644 --- a/psutil/arch/windows/global.c +++ b/psutil/arch/windows/global.c @@ -88,11 +88,11 @@ psutil_load_globals() { if (! psutil_GetExtendedTcpTable) return 1; -/* psutil_GetExtendedUdpTable = ps_GetProcAddressFromLib( "iphlpapi.dll", "GetExtendedUdpTable"); if (! psutil_GetExtendedUdpTable) return 1; +/* // Optionals. diff --git a/psutil/arch/windows/global.h b/psutil/arch/windows/global.h index 341e3a276..4957200fc 100644 --- a/psutil/arch/windows/global.h +++ b/psutil/arch/windows/global.h @@ -12,6 +12,8 @@ typedef PSTR (NTAPI * _RtlIpv4AddressToStringA)(struct in_addr *, PSTR); typedef PSTR (NTAPI * _RtlIpv6AddressToStringA)(struct in6_addr *, PSTR); typedef DWORD (WINAPI * _GetExtendedTcpTable)(PVOID, PDWORD, BOOL, ULONG, TCP_TABLE_CLASS, ULONG); +typedef DWORD (WINAPI * _GetExtendedUdpTable)(PVOID, PDWORD, BOOL, ULONG, + UDP_TABLE_CLASS, ULONG); // probably unnecessary? /* @@ -21,9 +23,6 @@ typedef DWORD (WINAPI * _GetExtendedTcpTable)(PVOID, PDWORD, BOOL, ULONG, typedef DWORD (CALLBACK *_GetActiveProcessorCount)(WORD); typedef ULONGLONG (CALLBACK *_GetTickCount64)(void); -typedef DWORD (WINAPI * _GetExtendedUdpTable)(PVOID, PDWORD, BOOL, ULONG, - UDP_TABLE_CLASS, ULONG); - _GetActiveProcessorCount \ psutil_GetActiveProcessorCount; @@ -31,8 +30,6 @@ _GetActiveProcessorCount \ _GetTickCount64 \ psutil_GetTickCount64; -_GetExtendedUdpTable \ - psutil_GetExtendedUdpTable; */ NTQSI_PROC \ @@ -56,5 +53,8 @@ _RtlIpv6AddressToStringA \ _GetExtendedTcpTable \ psutil_GetExtendedTcpTable; +_GetExtendedUdpTable \ + psutil_GetExtendedUdpTable; + int psutil_load_globals(); From a7ff4a33c2cb2c9cefa916a60016e0b9211758fa Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 04:49:40 -0800 Subject: [PATCH 10/47] port GetActiveProcessorCount --- psutil/_psutil_windows.c | 7 ++----- psutil/arch/windows/global.c | 2 -- psutil/arch/windows/global.h | 8 ++++---- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index d995f2007..260e04a0b 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -205,15 +205,12 @@ unsigned int psutil_get_num_cpus(int fail_on_err) { unsigned int ncpus = 0; SYSTEM_INFO sysinfo; - static DWORD(CALLBACK *_GetActiveProcessorCount)(WORD) = NULL; // GetActiveProcessorCount is available only on 64 bit versions // of Windows from Windows 7 onward. // Windows Vista 64 bit and Windows XP don't have it. - _GetActiveProcessorCount = \ - psutil_GetProcAddress("kernel32", "GetActiveProcessorCount"); - if (_GetActiveProcessorCount != NULL) { - ncpus = _GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); + if (psutil_GetActiveProcessorCount != NULL) { + ncpus = psutil_GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); if ((ncpus == 0) && (fail_on_err == 1)) { PyErr_SetFromWindowsErr(0); } diff --git a/psutil/arch/windows/global.c b/psutil/arch/windows/global.c index 026a37265..a185a9c20 100644 --- a/psutil/arch/windows/global.c +++ b/psutil/arch/windows/global.c @@ -92,13 +92,11 @@ psutil_load_globals() { "iphlpapi.dll", "GetExtendedUdpTable"); if (! psutil_GetExtendedUdpTable) return 1; -/* // Optionals. psutil_GetActiveProcessorCount = ps_GetProcAddress( "kernel32", "GetActiveProcessorCount"); -*/ return 0; } diff --git a/psutil/arch/windows/global.h b/psutil/arch/windows/global.h index 4957200fc..02ae6ca3a 100644 --- a/psutil/arch/windows/global.h +++ b/psutil/arch/windows/global.h @@ -14,6 +14,7 @@ typedef DWORD (WINAPI * _GetExtendedTcpTable)(PVOID, PDWORD, BOOL, ULONG, TCP_TABLE_CLASS, ULONG); typedef DWORD (WINAPI * _GetExtendedUdpTable)(PVOID, PDWORD, BOOL, ULONG, UDP_TABLE_CLASS, ULONG); +typedef DWORD (CALLBACK *_GetActiveProcessorCount)(WORD); // probably unnecessary? /* @@ -21,12 +22,8 @@ typedef DWORD (WINAPI * _GetExtendedUdpTable)(PVOID, PDWORD, BOOL, ULONG, #define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0) #endif -typedef DWORD (CALLBACK *_GetActiveProcessorCount)(WORD); typedef ULONGLONG (CALLBACK *_GetTickCount64)(void); -_GetActiveProcessorCount \ - psutil_GetActiveProcessorCount; - _GetTickCount64 \ psutil_GetTickCount64; @@ -56,5 +53,8 @@ _GetExtendedTcpTable \ _GetExtendedUdpTable \ psutil_GetExtendedUdpTable; +_GetActiveProcessorCount \ + psutil_GetActiveProcessorCount; + int psutil_load_globals(); From 2ca2119355f0de8745fe79228b644bbf12aebbc1 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 04:54:40 -0800 Subject: [PATCH 11/47] port GetTickCount64 --- psutil/_psutil_windows.c | 6 ++---- psutil/arch/windows/global.c | 2 ++ psutil/arch/windows/global.h | 10 ++++------ 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 260e04a0b..cdd52c0b9 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -240,8 +240,6 @@ psutil_get_num_cpus(int fail_on_err) { static PyObject *TimeoutExpired; static PyObject *TimeoutAbandoned; -static ULONGLONG (*psutil_GetTickCount64)(void) = NULL; - /* * Return a Python float representing the system uptime expressed in seconds * since the epoch. @@ -256,7 +254,6 @@ psutil_boot_time(PyObject *self, PyObject *args) { time_t pt; FILETIME fileTime; long long ll; - psutil_GetTickCount64; GetSystemTimeAsFileTime(&fileTime); /* @@ -285,7 +282,6 @@ psutil_boot_time(PyObject *self, PyObject *args) { // "#if (_WIN32_WINNT >= 0x0600)" pre-processor but that way // the produced exe/wheels cannot be used on Windows XP, see: // https://github.com/giampaolo/psutil/issues/811#issuecomment-230639178 - psutil_GetTickCount64 = psutil_GetProcAddress("kernel32", "GetTickCount64"); if (psutil_GetTickCount64 != NULL) { // Windows >= Vista uptime = psutil_GetTickCount64() / (ULONGLONG)1000.00f; @@ -295,6 +291,8 @@ psutil_boot_time(PyObject *self, PyObject *args) { // Windows XP. // GetTickCount() time will wrap around to zero if the // system is run continuously for 49.7 days. + psutil_debug("Windows < Vista; using GetTickCount() instead of " + "GetTickCount64()"); uptime = GetTickCount() / (LONGLONG)1000.00f; return Py_BuildValue("L", pt - uptime); } diff --git a/psutil/arch/windows/global.c b/psutil/arch/windows/global.c index a185a9c20..950c291d2 100644 --- a/psutil/arch/windows/global.c +++ b/psutil/arch/windows/global.c @@ -94,6 +94,8 @@ psutil_load_globals() { return 1; // Optionals. + psutil_GetTickCount64 = ps_GetProcAddress( + "kernel32", "GetTickCount64"); psutil_GetActiveProcessorCount = ps_GetProcAddress( "kernel32", "GetActiveProcessorCount"); diff --git a/psutil/arch/windows/global.h b/psutil/arch/windows/global.h index 02ae6ca3a..da2d7293d 100644 --- a/psutil/arch/windows/global.h +++ b/psutil/arch/windows/global.h @@ -15,18 +15,13 @@ typedef DWORD (WINAPI * _GetExtendedTcpTable)(PVOID, PDWORD, BOOL, ULONG, typedef DWORD (WINAPI * _GetExtendedUdpTable)(PVOID, PDWORD, BOOL, ULONG, UDP_TABLE_CLASS, ULONG); typedef DWORD (CALLBACK *_GetActiveProcessorCount)(WORD); +typedef ULONGLONG (CALLBACK *_GetTickCount64)(void); // probably unnecessary? /* #ifndef NT_SUCCESS #define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0) #endif - -typedef ULONGLONG (CALLBACK *_GetTickCount64)(void); - -_GetTickCount64 \ - psutil_GetTickCount64; - */ NTQSI_PROC \ @@ -56,5 +51,8 @@ _GetExtendedUdpTable \ _GetActiveProcessorCount \ psutil_GetActiveProcessorCount; +_GetTickCount64 \ + psutil_GetTickCount64; + int psutil_load_globals(); From 8d28579ac949ad21ef76f0c81bd43d42c925b2d5 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 05:06:42 -0800 Subject: [PATCH 12/47] rename function and make it private --- psutil/arch/windows/process_handles.c | 6 +++--- psutil/arch/windows/process_handles.h | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/psutil/arch/windows/process_handles.c b/psutil/arch/windows/process_handles.c index ad2bf238a..33a9c7781 100644 --- a/psutil/arch/windows/process_handles.c +++ b/psutil/arch/windows/process_handles.c @@ -178,7 +178,7 @@ psutil_get_open_files_ntqueryobject(long dwPid, HANDLE hProcess) { goto loop_cleanup; } - dwWait = psutil_NtQueryObject(); + dwWait = psutil_create_thread(); // If the call does not return, skip this handle if (dwWait != WAIT_OBJECT_0) @@ -266,8 +266,8 @@ psutil_get_open_files_ntqueryobject(long dwPid, HANDLE hProcess) { } -DWORD -psutil_NtQueryObject() { +static DWORD +psutil_create_thread() { DWORD dwWait = 0; if (g_hThread == NULL) diff --git a/psutil/arch/windows/process_handles.h b/psutil/arch/windows/process_handles.h index 4a022c1c1..050d6ec3d 100644 --- a/psutil/arch/windows/process_handles.h +++ b/psutil/arch/windows/process_handles.h @@ -105,7 +105,6 @@ VOID psutil_get_open_files_init(BOOL threaded); PyObject* psutil_get_open_files(long pid, HANDLE processHandle); PyObject* psutil_get_open_files_ntqueryobject(long dwPid, HANDLE hProcess); PyObject* psutil_get_open_files_getmappedfilename(long dwPid, HANDLE hProcess); -DWORD psutil_NtQueryObject(void); DWORD WINAPI psutil_NtQueryObjectThread(LPVOID lpvParam); #endif // __PROCESS_HANDLES_H__ From aeccf0c9acc142e376dbfe45ccf755f7723b15c0 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 05:13:18 -0800 Subject: [PATCH 13/47] port NtQueryObject --- psutil/arch/windows/global.c | 8 +++++++- psutil/arch/windows/global.h | 10 ++++++++++ psutil/arch/windows/process_handles.c | 26 +++++++------------------- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/psutil/arch/windows/global.c b/psutil/arch/windows/global.c index 950c291d2..f76d41d59 100644 --- a/psutil/arch/windows/global.c +++ b/psutil/arch/windows/global.c @@ -73,6 +73,11 @@ psutil_load_globals() { if (! psutil_WinStationQueryInformationW) return 1; + psutil_NtQueryObject = ps_GetProcAddressFromLib( + "ntdll.dll", "NtQueryObject"); + if (! psutil_NtQueryObject) + return 1; + psutil_rtlIpv4AddressToStringA = ps_GetProcAddressFromLib( "ntdll.dll", "RtlIpv4AddressToStringA"); if (! psutil_rtlIpv4AddressToStringA) @@ -93,7 +98,8 @@ psutil_load_globals() { if (! psutil_GetExtendedUdpTable) return 1; - // Optionals. + // Optional. + psutil_GetTickCount64 = ps_GetProcAddress( "kernel32", "GetTickCount64"); diff --git a/psutil/arch/windows/global.h b/psutil/arch/windows/global.h index da2d7293d..b4d7f7d7c 100644 --- a/psutil/arch/windows/global.h +++ b/psutil/arch/windows/global.h @@ -16,6 +16,14 @@ typedef DWORD (WINAPI * _GetExtendedUdpTable)(PVOID, PDWORD, BOOL, ULONG, UDP_TABLE_CLASS, ULONG); typedef DWORD (CALLBACK *_GetActiveProcessorCount)(WORD); typedef ULONGLONG (CALLBACK *_GetTickCount64)(void); +typedef NTSTATUS (NTAPI *_NtQueryObject)( + HANDLE ObjectHandle, + ULONG ObjectInformationClass, + PVOID ObjectInformation, + ULONG ObjectInformationLength, + PULONG ReturnLength +); + // probably unnecessary? /* @@ -54,5 +62,7 @@ _GetActiveProcessorCount \ _GetTickCount64 \ psutil_GetTickCount64; +_NtQueryObject \ + psutil_NtQueryObject; int psutil_load_globals(); diff --git a/psutil/arch/windows/process_handles.c b/psutil/arch/windows/process_handles.c index 33a9c7781..4910f4efd 100644 --- a/psutil/arch/windows/process_handles.c +++ b/psutil/arch/windows/process_handles.c @@ -10,8 +10,6 @@ #include "global.h" #include "../../_psutil_common.h" -static _NtQueryObject __NtQueryObject = NULL; - CRITICAL_SECTION g_cs; BOOL g_initialized = FALSE; NTSTATUS g_status; @@ -45,10 +43,6 @@ psutil_get_open_files_init(BOOL threaded) { if (g_initialized == TRUE) return; - // Resolve the Windows API calls - __NtQueryObject = psutil_GetProcAddressFromLib( - "ntdll.dll", "NtQueryObject"); - // Create events for signalling work between threads if (threaded == TRUE) { g_hEvtStart = CreateEvent(NULL, FALSE, FALSE, NULL); @@ -80,8 +74,7 @@ psutil_get_open_files_ntqueryobject(long dwPid, HANDLE hProcess) { // to psutil_get_open_files() is running EnterCriticalSection(&g_cs); - if (__NtQueryObject == NULL || - g_hEvtStart == NULL || + if (g_hEvtStart == NULL || g_hEvtFinish == NULL) { @@ -307,11 +300,12 @@ psutil_NtQueryObjectThread(LPVOID lpvParam) { while (TRUE) { WaitForSingleObject(g_hEvtStart, INFINITE); - g_status = __NtQueryObject(g_hFile, - ObjectNameInformation, - g_pNameBuffer, - g_dwSize, - &g_dwLength); + g_status = psutil_NtQueryObject( + g_hFile, + ObjectNameInformation, + g_pNameBuffer, + g_dwSize, + &g_dwLength); SetEvent(g_hEvtFinish); } } @@ -337,12 +331,6 @@ psutil_get_open_files_getmappedfilename(long dwPid, HANDLE hProcess) { if (g_initialized == FALSE) psutil_get_open_files_init(FALSE); - if (__NtQueryObject == NULL) { - PyErr_SetFromWindowsErr(0); - error = TRUE; - goto cleanup; - } - // Py_BuildValue raises an exception if NULL is returned py_retlist = PyList_New(0); if (py_retlist == NULL) { From afc95f9473a851dfde3782ab65bf867b8c9cbc0d Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 05:17:21 -0800 Subject: [PATCH 14/47] fix compilation warning --- psutil/arch/windows/process_handles.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/psutil/arch/windows/process_handles.c b/psutil/arch/windows/process_handles.c index 4910f4efd..89bc7d8fa 100644 --- a/psutil/arch/windows/process_handles.c +++ b/psutil/arch/windows/process_handles.c @@ -259,7 +259,7 @@ psutil_get_open_files_ntqueryobject(long dwPid, HANDLE hProcess) { } -static DWORD +DWORD psutil_create_thread() { DWORD dwWait = 0; From d141020000688bb48064cb00dd0246776b89d093 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 05:26:02 -0800 Subject: [PATCH 15/47] port NtWow64QueryInformationProcess64 --- psutil/arch/windows/global.c | 3 +++ psutil/arch/windows/global.h | 4 ++++ psutil/arch/windows/process_info.c | 15 ++++----------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/psutil/arch/windows/global.c b/psutil/arch/windows/global.c index f76d41d59..c45c2d7da 100644 --- a/psutil/arch/windows/global.c +++ b/psutil/arch/windows/global.c @@ -106,5 +106,8 @@ psutil_load_globals() { psutil_GetActiveProcessorCount = ps_GetProcAddress( "kernel32", "GetActiveProcessorCount"); + psutil_NtWow64QueryInformationProcess64 = psutil_GetProcAddressFromLib( + "ntdll.dll", "NtWow64QueryInformationProcess64"); + return 0; } diff --git a/psutil/arch/windows/global.h b/psutil/arch/windows/global.h index b4d7f7d7c..6e608097e 100644 --- a/psutil/arch/windows/global.h +++ b/psutil/arch/windows/global.h @@ -65,4 +65,8 @@ _GetTickCount64 \ _NtQueryObject \ psutil_NtQueryObject; +// XXX: just an alias; probably unnecessary +_NtQueryInformationProcess \ + psutil_NtWow64QueryInformationProcess64; + int psutil_load_globals(); diff --git a/psutil/arch/windows/process_info.c b/psutil/arch/windows/process_info.c index 5b5e82391..279b8de2c 100644 --- a/psutil/arch/windows/process_info.c +++ b/psutil/arch/windows/process_info.c @@ -519,7 +519,6 @@ psutil_get_process_data(long pid, http://www.drdobbs.com/embracing-64-bit-windows/184401966 */ #ifndef _WIN64 - static _NtQueryInformationProcess NtWow64QueryInformationProcess64 = NULL; static _NtWow64ReadVirtualMemory64 NtWow64ReadVirtualMemory64 = NULL; #endif HANDLE hProcess = NULL; @@ -602,15 +601,9 @@ psutil_get_process_data(long pid, PEB64 peb64; RTL_USER_PROCESS_PARAMETERS64 procParameters64; - if (NtWow64QueryInformationProcess64 == NULL) { - NtWow64QueryInformationProcess64 = \ - psutil_GetProcAddressFromLib( - "ntdll.dll", "NtWow64QueryInformationProcess64"); - if (NtWow64QueryInformationProcess64 == NULL) { - // Too complicated. Give up. - AccessDenied("can't query 64-bit process in 32-bit-WoW mode"); - goto error; - } + if (psutil_NtWow64QueryInformationProcess64 == NULL) { + AccessDenied("can't query 64-bit process in 32-bit-WoW mode"); + goto error; } if (NtWow64ReadVirtualMemory64 == NULL) { NtWow64ReadVirtualMemory64 = \ @@ -623,7 +616,7 @@ psutil_get_process_data(long pid, } } - if (!NT_SUCCESS(NtWow64QueryInformationProcess64( + if (!NT_SUCCESS(psutil_NtWow64QueryInformationProcess64( hProcess, ProcessBasicInformation, &pbi64, From 4a386e488425d81ac4989f7dad802714733a3e3e Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 05:31:47 -0800 Subject: [PATCH 16/47] port NtWow64ReadVirtualMemory64 --- psutil/arch/windows/global.c | 3 ++ psutil/arch/windows/global.h | 9 ++++ psutil/arch/windows/process_info.c | 86 ++++++++++++------------------ 3 files changed, 45 insertions(+), 53 deletions(-) diff --git a/psutil/arch/windows/global.c b/psutil/arch/windows/global.c index c45c2d7da..f085735a0 100644 --- a/psutil/arch/windows/global.c +++ b/psutil/arch/windows/global.c @@ -109,5 +109,8 @@ psutil_load_globals() { psutil_NtWow64QueryInformationProcess64 = psutil_GetProcAddressFromLib( "ntdll.dll", "NtWow64QueryInformationProcess64"); + psutil_NtWow64ReadVirtualMemory64 = ps_GetProcAddressFromLib( + "ntdll.dll", "NtWow64ReadVirtualMemory64"); + return 0; } diff --git a/psutil/arch/windows/global.h b/psutil/arch/windows/global.h index 6e608097e..cb2f99b81 100644 --- a/psutil/arch/windows/global.h +++ b/psutil/arch/windows/global.h @@ -23,6 +23,12 @@ typedef NTSTATUS (NTAPI *_NtQueryObject)( ULONG ObjectInformationLength, PULONG ReturnLength ); +typedef NTSTATUS (NTAPI *_NtWow64ReadVirtualMemory64)( + IN HANDLE ProcessHandle, + IN PVOID64 BaseAddress, + OUT PVOID Buffer, + IN ULONG64 Size, + OUT PULONG64 NumberOfBytesRead); // probably unnecessary? @@ -69,4 +75,7 @@ _NtQueryObject \ _NtQueryInformationProcess \ psutil_NtWow64QueryInformationProcess64; +_NtWow64ReadVirtualMemory64 \ + psutil_NtWow64ReadVirtualMemory64; + int psutil_load_globals(); diff --git a/psutil/arch/windows/process_info.c b/psutil/arch/windows/process_info.c index 279b8de2c..f9343d3d2 100644 --- a/psutil/arch/windows/process_info.c +++ b/psutil/arch/windows/process_info.c @@ -25,11 +25,6 @@ // but unfortunately not in a usable way. // ==================================================================== -// see http://msdn2.microsoft.com/en-us/library/aa489609.aspx -#ifndef NT_SUCCESS -#define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0) -#endif - // http://msdn.microsoft.com/en-us/library/aa813741(VS.85).aspx typedef struct { BYTE Reserved1[16]; @@ -94,16 +89,6 @@ typedef struct { /* More fields ... */ } PEB32; #else -/* When we are a 32 bit (WoW64) process accessing a 64 bit process we need to - use the 64 bit structure layout and a special function to read its memory. - */ -typedef NTSTATUS (NTAPI *_NtWow64ReadVirtualMemory64)( - IN HANDLE ProcessHandle, - IN PVOID64 BaseAddress, - OUT PVOID Buffer, - IN ULONG64 Size, - OUT PULONG64 NumberOfBytesRead); - typedef enum { MemoryInformationBasic } MEMORY_INFORMATION_CLASS; @@ -518,9 +503,6 @@ psutil_get_process_data(long pid, http://stackoverflow.com/a/14012919 http://www.drdobbs.com/embracing-64-bit-windows/184401966 */ -#ifndef _WIN64 - static _NtWow64ReadVirtualMemory64 NtWow64ReadVirtualMemory64 = NULL; -#endif HANDLE hProcess = NULL; LPCVOID src; SIZE_T size; @@ -541,7 +523,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. */ - if (!NT_SUCCESS(psutil_NtQueryInformationProcess( + if (! NT_SUCCESS(psutil_NtQueryInformationProcess( hProcess, ProcessWow64Information, &ppeb32, @@ -601,47 +583,43 @@ psutil_get_process_data(long pid, PEB64 peb64; RTL_USER_PROCESS_PARAMETERS64 procParameters64; - if (psutil_NtWow64QueryInformationProcess64 == NULL) { + if ((psutil_NtWow64QueryInformationProcess64 == NULL) || + (psutil_NtWow64ReadVirtualMemory64 == NULL)) { AccessDenied("can't query 64-bit process in 32-bit-WoW mode"); goto error; } - if (NtWow64ReadVirtualMemory64 == NULL) { - NtWow64ReadVirtualMemory64 = \ - psutil_GetProcAddressFromLib( - "ntdll.dll", "NtWow64ReadVirtualMemory64"); - if (NtWow64ReadVirtualMemory64 == NULL) { - // Too complicated. Give up. - AccessDenied("can't query 64-bit process in 32-bit-WoW mode"); - goto error; - } - } - if (!NT_SUCCESS(psutil_NtWow64QueryInformationProcess64( - hProcess, - ProcessBasicInformation, - &pbi64, - sizeof(pbi64), - NULL))) { + if (! NT_SUCCESS(psutil_NtWow64QueryInformationProcess64( + hProcess, + ProcessBasicInformation, + &pbi64, + sizeof(pbi64), + NULL))) + { PyErr_SetFromWindowsErr(0); goto error; } // read peb - if (!NT_SUCCESS(NtWow64ReadVirtualMemory64(hProcess, - pbi64.PebBaseAddress, - &peb64, - sizeof(peb64), - NULL))) { + if (! NT_SUCCESS(psutil_NtWow64ReadVirtualMemory64( + hProcess, + pbi64.PebBaseAddress, + &peb64, + sizeof(peb64), + NULL))) + { PyErr_SetFromWindowsErr(0); goto error; } // read process parameters - if (!NT_SUCCESS(NtWow64ReadVirtualMemory64(hProcess, - peb64.ProcessParameters, - &procParameters64, - sizeof(procParameters64), - NULL))) { + if (! NT_SUCCESS(psutil_NtWow64ReadVirtualMemory64( + hProcess, + peb64.ProcessParameters, + &procParameters64, + sizeof(procParameters64), + NULL))) + { PyErr_SetFromWindowsErr(0); goto error; } @@ -668,7 +646,7 @@ psutil_get_process_data(long pid, PEB_ peb; RTL_USER_PROCESS_PARAMETERS_ procParameters; - if (!NT_SUCCESS(psutil_NtQueryInformationProcess( + if (! NT_SUCCESS(psutil_NtQueryInformationProcess( hProcess, ProcessBasicInformation, &pbi, @@ -735,11 +713,13 @@ psutil_get_process_data(long pid, #ifndef _WIN64 if (weAreWow64 && !theyAreWow64) { - if (!NT_SUCCESS(NtWow64ReadVirtualMemory64(hProcess, - src64, - buffer, - size, - NULL))) { + if (! NT_SUCCESS(psutil_NtWow64ReadVirtualMemory64( + hProcess, + src64, + buffer, + size, + NULL))) + { PyErr_SetFromWindowsErr(0); goto error; } @@ -797,7 +777,7 @@ psutil_get_cmdline_data(long pid, WCHAR **pdata, SIZE_T *psize) { ret_length, &ret_length ); - if (!NT_SUCCESS(status)) { + if (! NT_SUCCESS(status)) { PyErr_SetFromWindowsErr(0); goto error; } From 265ee244a3aeb5103106aebe39f508f68ada7c33 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 05:38:10 -0800 Subject: [PATCH 17/47] move definitions in ntextapi.h --- psutil/_psutil_windows.c | 2 +- psutil/arch/windows/global.c | 2 +- psutil/arch/windows/global.h | 76 +--------------------------------- psutil/arch/windows/ntextapi.h | 65 ++++++++++++++++++++++++++++- 4 files changed, 67 insertions(+), 78 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index cdd52c0b9..72ca50368 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -3871,7 +3871,7 @@ void init_psutil_windows(void) // set SeDebug for the current process psutil_set_se_debug(); psutil_setup(); - if (psutil_load_globals() != 0) + if (psutil_loadlibs() != 0) return NULL; #if PY_MAJOR_VERSION >= 3 diff --git a/psutil/arch/windows/global.c b/psutil/arch/windows/global.c index f085735a0..e426b5610 100644 --- a/psutil/arch/windows/global.c +++ b/psutil/arch/windows/global.c @@ -50,7 +50,7 @@ ps_GetProcAddressFromLib(LPCSTR libname, LPCSTR procname) { int -psutil_load_globals() { +psutil_loadlibs() { // Mandatory. psutil_NtQuerySystemInformation = ps_GetProcAddressFromLib( diff --git a/psutil/arch/windows/global.h b/psutil/arch/windows/global.h index cb2f99b81..0a6cd25db 100644 --- a/psutil/arch/windows/global.h +++ b/psutil/arch/windows/global.h @@ -4,78 +4,4 @@ * found in the LICENSE file. */ -#include "ntextapi.h" -#include - -typedef DWORD (_stdcall * NTQSI_PROC) (int, PVOID, ULONG, PULONG); -typedef PSTR (NTAPI * _RtlIpv4AddressToStringA)(struct in_addr *, PSTR); -typedef PSTR (NTAPI * _RtlIpv6AddressToStringA)(struct in6_addr *, PSTR); -typedef DWORD (WINAPI * _GetExtendedTcpTable)(PVOID, PDWORD, BOOL, ULONG, - TCP_TABLE_CLASS, ULONG); -typedef DWORD (WINAPI * _GetExtendedUdpTable)(PVOID, PDWORD, BOOL, ULONG, - UDP_TABLE_CLASS, ULONG); -typedef DWORD (CALLBACK *_GetActiveProcessorCount)(WORD); -typedef ULONGLONG (CALLBACK *_GetTickCount64)(void); -typedef NTSTATUS (NTAPI *_NtQueryObject)( - HANDLE ObjectHandle, - ULONG ObjectInformationClass, - PVOID ObjectInformation, - ULONG ObjectInformationLength, - PULONG ReturnLength -); -typedef NTSTATUS (NTAPI *_NtWow64ReadVirtualMemory64)( - IN HANDLE ProcessHandle, - IN PVOID64 BaseAddress, - OUT PVOID Buffer, - IN ULONG64 Size, - OUT PULONG64 NumberOfBytesRead); - - -// probably unnecessary? -/* -#ifndef NT_SUCCESS -#define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0) -#endif -*/ - -NTQSI_PROC \ - psutil_NtQuerySystemInformation; - -_NtQueryInformationProcess \ - psutil_NtQueryInformationProcess; - -_NtSetInformationProcess - psutil_NtSetInformationProcess; - -PWINSTATIONQUERYINFORMATIONW \ - psutil_WinStationQueryInformationW; - -_RtlIpv4AddressToStringA \ - psutil_rtlIpv4AddressToStringA; - -_RtlIpv6AddressToStringA \ - psutil_rtlIpv6AddressToStringA; - -_GetExtendedTcpTable \ - psutil_GetExtendedTcpTable; - -_GetExtendedUdpTable \ - psutil_GetExtendedUdpTable; - -_GetActiveProcessorCount \ - psutil_GetActiveProcessorCount; - -_GetTickCount64 \ - psutil_GetTickCount64; - -_NtQueryObject \ - psutil_NtQueryObject; - -// XXX: just an alias; probably unnecessary -_NtQueryInformationProcess \ - psutil_NtWow64QueryInformationProcess64; - -_NtWow64ReadVirtualMemory64 \ - psutil_NtWow64ReadVirtualMemory64; - -int psutil_load_globals(); +int psutil_loadlibs(); diff --git a/psutil/arch/windows/ntextapi.h b/psutil/arch/windows/ntextapi.h index ea23ddb72..38f89dd97 100644 --- a/psutil/arch/windows/ntextapi.h +++ b/psutil/arch/windows/ntextapi.h @@ -6,7 +6,7 @@ #if !defined(__NTEXTAPI_H__) #define __NTEXTAPI_H__ #include - +#include typedef struct { LARGE_INTEGER IdleTime; @@ -342,4 +342,67 @@ typedef enum _PROCESSINFOCLASS2 { #define ProcessImageFileName _ProcessImageFileName #define ProcessBreakOnTermination _ProcessBreakOnTermination +typedef DWORD (_stdcall * NTQSI_PROC) (int, PVOID, ULONG, PULONG); +typedef PSTR (NTAPI * _RtlIpv4AddressToStringA)(struct in_addr *, PSTR); +typedef PSTR (NTAPI * _RtlIpv6AddressToStringA)(struct in6_addr *, PSTR); +typedef DWORD (WINAPI * _GetExtendedTcpTable)(PVOID, PDWORD, BOOL, ULONG, + TCP_TABLE_CLASS, ULONG); +typedef DWORD (WINAPI * _GetExtendedUdpTable)(PVOID, PDWORD, BOOL, ULONG, + UDP_TABLE_CLASS, ULONG); +typedef DWORD (CALLBACK *_GetActiveProcessorCount)(WORD); +typedef ULONGLONG (CALLBACK *_GetTickCount64)(void); +typedef NTSTATUS (NTAPI *_NtQueryObject)( + HANDLE ObjectHandle, + ULONG ObjectInformationClass, + PVOID ObjectInformation, + ULONG ObjectInformationLength, + PULONG ReturnLength +); +typedef NTSTATUS (NTAPI *_NtWow64ReadVirtualMemory64)( + IN HANDLE ProcessHandle, + IN PVOID64 BaseAddress, + OUT PVOID Buffer, + IN ULONG64 Size, + OUT PULONG64 NumberOfBytesRead); + +NTQSI_PROC \ + psutil_NtQuerySystemInformation; + +_NtQueryInformationProcess \ + psutil_NtQueryInformationProcess; + +_NtSetInformationProcess + psutil_NtSetInformationProcess; + +PWINSTATIONQUERYINFORMATIONW \ + psutil_WinStationQueryInformationW; + +_RtlIpv4AddressToStringA \ + psutil_rtlIpv4AddressToStringA; + +_RtlIpv6AddressToStringA \ + psutil_rtlIpv6AddressToStringA; + +_GetExtendedTcpTable \ + psutil_GetExtendedTcpTable; + +_GetExtendedUdpTable \ + psutil_GetExtendedUdpTable; + +_GetActiveProcessorCount \ + psutil_GetActiveProcessorCount; + +_GetTickCount64 \ + psutil_GetTickCount64; + +_NtQueryObject \ + psutil_NtQueryObject; + +// XXX: just an alias; probably unnecessary +_NtQueryInformationProcess \ + psutil_NtWow64QueryInformationProcess64; + +_NtWow64ReadVirtualMemory64 \ + psutil_NtWow64ReadVirtualMemory64; + #endif // __NTEXTAPI_H__ From 710008df5ff86ef85388a6273bb6598c472f4fae Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 05:41:47 -0800 Subject: [PATCH 18/47] remove duplicated definition --- psutil/_psutil_windows.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 72ca50368..3d9ca18cd 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -1525,10 +1525,6 @@ psutil_proc_username(PyObject *self, PyObject *args) { } -typedef DWORD (WINAPI * _GetExtendedTcpTable)(PVOID, PDWORD, BOOL, ULONG, - TCP_TABLE_CLASS, ULONG); - - // https://msdn.microsoft.com/library/aa365928.aspx static DWORD __GetExtendedTcpTable(_GetExtendedTcpTable call, ULONG address_family, From 7a32431b118d52d7892c20fbf15cbc680cfbb456 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 05:47:04 -0800 Subject: [PATCH 19/47] add comments re. platform availability --- psutil/_psutil_windows.c | 4 +--- psutil/arch/windows/global.c | 4 ++++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 3d9ca18cd..20b97bab3 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -206,9 +206,7 @@ psutil_get_num_cpus(int fail_on_err) { unsigned int ncpus = 0; SYSTEM_INFO sysinfo; - // GetActiveProcessorCount is available only on 64 bit versions - // of Windows from Windows 7 onward. - // Windows Vista 64 bit and Windows XP don't have it. + // Minimum requirement: Windows 7 if (psutil_GetActiveProcessorCount != NULL) { ncpus = psutil_GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); if ((ncpus == 0) && (fail_on_err == 1)) { diff --git a/psutil/arch/windows/global.c b/psutil/arch/windows/global.c index e426b5610..cb565f8c1 100644 --- a/psutil/arch/windows/global.c +++ b/psutil/arch/windows/global.c @@ -88,11 +88,13 @@ psutil_loadlibs() { if (! psutil_rtlIpv6AddressToStringA) return 1; + // minimum requirement: Win XP SP3 psutil_GetExtendedTcpTable = ps_GetProcAddressFromLib( "iphlpapi.dll", "GetExtendedTcpTable"); if (! psutil_GetExtendedTcpTable) return 1; + // minimum requirement: Win XP SP3 psutil_GetExtendedUdpTable = ps_GetProcAddressFromLib( "iphlpapi.dll", "GetExtendedUdpTable"); if (! psutil_GetExtendedUdpTable) @@ -100,9 +102,11 @@ psutil_loadlibs() { // Optional. + // minimum requirement: Win Vista psutil_GetTickCount64 = ps_GetProcAddress( "kernel32", "GetTickCount64"); + // minimum requirement: Win 7 64-bit psutil_GetActiveProcessorCount = ps_GetProcAddress( "kernel32", "GetActiveProcessorCount"); From f609d75481da23c635c1884275a6e0366e871013 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 06:00:10 -0800 Subject: [PATCH 20/47] refactor definitions --- psutil/arch/windows/ntextapi.h | 100 +++++++++++++++++---------------- 1 file changed, 52 insertions(+), 48 deletions(-) diff --git a/psutil/arch/windows/ntextapi.h b/psutil/arch/windows/ntextapi.h index 38f89dd97..e6a9d0283 100644 --- a/psutil/arch/windows/ntextapi.h +++ b/psutil/arch/windows/ntextapi.h @@ -8,6 +8,8 @@ #include #include +typedef LONG NTSTATUS; + typedef struct { LARGE_INTEGER IdleTime; LARGE_INTEGER KernelTime; @@ -17,7 +19,6 @@ typedef struct { ULONG InterruptCount; } _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION; - typedef struct { LARGE_INTEGER IdleProcessTime; LARGE_INTEGER IoReadTransferCount; @@ -93,10 +94,8 @@ typedef struct { ULONG FirstLevelTbFills; ULONG SecondLevelTbFills; ULONG SystemCalls; - } _SYSTEM_PERFORMANCE_INFORMATION; - typedef struct { ULONG ContextSwitches; ULONG DpcCount; @@ -106,7 +105,6 @@ typedef struct { ULONG ApcBypassCount; } _SYSTEM_INTERRUPT_INFORMATION; - typedef enum _KTHREAD_STATE { Initialized, Ready, @@ -120,7 +118,6 @@ typedef enum _KTHREAD_STATE { MaximumThreadState } KTHREAD_STATE, *PKTHREAD_STATE; - typedef enum _KWAIT_REASON { Executive = 0, FreePage = 1, @@ -162,7 +159,6 @@ typedef enum _KWAIT_REASON { MaximumWaitReason = 37 } KWAIT_REASON, *PKWAIT_REASON; - typedef struct _CLIENT_ID2 { HANDLE UniqueProcess; HANDLE UniqueThread; @@ -190,8 +186,6 @@ typedef struct _SYSTEM_THREAD_INFORMATION2 { typedef struct _TEB *PTEB; - -// private typedef struct _SYSTEM_EXTENDED_THREAD_INFORMATION { SYSTEM_THREAD_INFORMATION ThreadInfo; PVOID StackBase; @@ -203,7 +197,6 @@ typedef struct _SYSTEM_EXTENDED_THREAD_INFORMATION { ULONG_PTR Reserved4; } SYSTEM_EXTENDED_THREAD_INFORMATION, *PSYSTEM_EXTENDED_THREAD_INFORMATION; - typedef struct _SYSTEM_PROCESS_INFORMATION2 { ULONG NextEntryOffset; ULONG NumberOfThreads; @@ -244,11 +237,6 @@ typedef struct _SYSTEM_PROCESS_INFORMATION2 { #define SYSTEM_PROCESS_INFORMATION SYSTEM_PROCESS_INFORMATION2 #define PSYSTEM_PROCESS_INFORMATION PSYSTEM_PROCESS_INFORMATION2 - -// ================================================ -// psutil.users() support -// ================================================ - typedef struct _WINSTATION_INFO { BYTE Reserved1[72]; ULONG SessionId; @@ -261,19 +249,9 @@ typedef struct _WINSTATION_INFO { FILETIME CurrentTime; } WINSTATION_INFO, *PWINSTATION_INFO; - typedef BOOLEAN (WINAPI * PWINSTATIONQUERYINFORMATIONW) (HANDLE,ULONG,WINSTATIONINFOCLASS,PVOID,ULONG,PULONG); - -/* - * NtQueryInformationProcess code taken from - * http://wj32.wordpress.com/2009/01/24/howto-get-the-command-line-of-processes/ - * typedefs needed to compile against ntdll functions not exposted in the API - */ -typedef LONG NTSTATUS; - - typedef NTSTATUS (NTAPI *_NtQueryInformationProcess)( HANDLE ProcessHandle, DWORD ProcessInformationClass, @@ -282,7 +260,6 @@ typedef NTSTATUS (NTAPI *_NtQueryInformationProcess)( PDWORD ReturnLength ); - typedef NTSTATUS (NTAPI *_NtSetInformationProcess)( HANDLE ProcessHandle, DWORD ProcessInformationClass, @@ -290,6 +267,52 @@ typedef NTSTATUS (NTAPI *_NtSetInformationProcess)( DWORD ProcessInformationLength ); +typedef DWORD (_stdcall * NTQSI_PROC) + (int, PVOID, ULONG, PULONG); + +typedef PSTR (NTAPI * _RtlIpv4AddressToStringA) + (struct in_addr *, PSTR); + +typedef PSTR (NTAPI * _RtlIpv6AddressToStringA) + (struct in6_addr *, PSTR); + +typedef DWORD (WINAPI * _GetExtendedTcpTable)( + PVOID pTcpTable, + PDWORD pdwSize, + BOOL bOrder, + ULONG ulAf, + TCP_TABLE_CLASS TableClass, + ULONG Reserved +); + +typedef DWORD (WINAPI * _GetExtendedUdpTable)( + PVOID pUdpTable, + PDWORD pdwSize, + BOOL bOrder, + ULONG ulAf, + UDP_TABLE_CLASS TableClass, + ULONG Reserved +); + +typedef DWORD (CALLBACK *_GetActiveProcessorCount)( + WORD GroupNumber); + +typedef ULONGLONG (CALLBACK *_GetTickCount64)(void); + +typedef NTSTATUS (NTAPI *_NtQueryObject)( + HANDLE Handle, + OBJECT_INFORMATION_CLASS ObjectInformationClass, + PVOID ObjectInformation, + ULONG ObjectInformationLength, + PULONG ReturnLength +); + +typedef NTSTATUS (NTAPI *_NtWow64ReadVirtualMemory64)( + IN HANDLE ProcessHandle, + IN PVOID64 BaseAddress, + OUT PVOID Buffer, + IN ULONG64 Size, + OUT PULONG64 NumberOfBytesRead); typedef enum _PROCESSINFOCLASS2 { _ProcessBasicInformation, @@ -334,7 +357,6 @@ typedef enum _PROCESSINFOCLASS2 { MaxProcessInfoClass } PROCESSINFOCLASS2; - #define PROCESSINFOCLASS PROCESSINFOCLASS2 #define ProcessBasicInformation _ProcessBasicInformation #define ProcessWow64Information _ProcessWow64Information @@ -342,28 +364,10 @@ typedef enum _PROCESSINFOCLASS2 { #define ProcessImageFileName _ProcessImageFileName #define ProcessBreakOnTermination _ProcessBreakOnTermination -typedef DWORD (_stdcall * NTQSI_PROC) (int, PVOID, ULONG, PULONG); -typedef PSTR (NTAPI * _RtlIpv4AddressToStringA)(struct in_addr *, PSTR); -typedef PSTR (NTAPI * _RtlIpv6AddressToStringA)(struct in6_addr *, PSTR); -typedef DWORD (WINAPI * _GetExtendedTcpTable)(PVOID, PDWORD, BOOL, ULONG, - TCP_TABLE_CLASS, ULONG); -typedef DWORD (WINAPI * _GetExtendedUdpTable)(PVOID, PDWORD, BOOL, ULONG, - UDP_TABLE_CLASS, ULONG); -typedef DWORD (CALLBACK *_GetActiveProcessorCount)(WORD); -typedef ULONGLONG (CALLBACK *_GetTickCount64)(void); -typedef NTSTATUS (NTAPI *_NtQueryObject)( - HANDLE ObjectHandle, - ULONG ObjectInformationClass, - PVOID ObjectInformation, - ULONG ObjectInformationLength, - PULONG ReturnLength -); -typedef NTSTATUS (NTAPI *_NtWow64ReadVirtualMemory64)( - IN HANDLE ProcessHandle, - IN PVOID64 BaseAddress, - OUT PVOID Buffer, - IN ULONG64 Size, - OUT PULONG64 NumberOfBytesRead); +/* + * Custom psutil definitions. These are dynamically set in global.c + * on module import. + */ NTQSI_PROC \ psutil_NtQuerySystemInformation; From e26ec99873f763f83df4fa8c75e3b80d6689350d Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 06:06:03 -0800 Subject: [PATCH 21/47] types refactoring --- psutil/arch/windows/ntextapi.h | 9 ++++++++- psutil/arch/windows/process_handles.c | 2 +- psutil/arch/windows/process_handles.h | 15 --------------- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/psutil/arch/windows/ntextapi.h b/psutil/arch/windows/ntextapi.h index e6a9d0283..548c437ce 100644 --- a/psutil/arch/windows/ntextapi.h +++ b/psutil/arch/windows/ntextapi.h @@ -260,6 +260,13 @@ typedef NTSTATUS (NTAPI *_NtQueryInformationProcess)( PDWORD ReturnLength ); +typedef NTSTATUS (NTAPI *_NtQuerySystemInformation)( + ULONG SystemInformationClass, + PVOID SystemInformation, + ULONG SystemInformationLength, + PULONG ReturnLength +); + typedef NTSTATUS (NTAPI *_NtSetInformationProcess)( HANDLE ProcessHandle, DWORD ProcessInformationClass, @@ -369,7 +376,7 @@ typedef enum _PROCESSINFOCLASS2 { * on module import. */ -NTQSI_PROC \ +_NtQuerySystemInformation \ psutil_NtQuerySystemInformation; _NtQueryInformationProcess \ diff --git a/psutil/arch/windows/process_handles.c b/psutil/arch/windows/process_handles.c index 89bc7d8fa..4910f4efd 100644 --- a/psutil/arch/windows/process_handles.c +++ b/psutil/arch/windows/process_handles.c @@ -259,7 +259,7 @@ psutil_get_open_files_ntqueryobject(long dwPid, HANDLE hProcess) { } -DWORD +static DWORD psutil_create_thread() { DWORD dwWait = 0; diff --git a/psutil/arch/windows/process_handles.h b/psutil/arch/windows/process_handles.h index 050d6ec3d..dc0b76551 100644 --- a/psutil/arch/windows/process_handles.h +++ b/psutil/arch/windows/process_handles.h @@ -30,21 +30,6 @@ #define HANDLE_TYPE_FILE 28 #define NTQO_TIMEOUT 100 -typedef NTSTATUS (NTAPI *_NtQuerySystemInformation)( - ULONG SystemInformationClass, - PVOID SystemInformation, - ULONG SystemInformationLength, - PULONG ReturnLength -); - -typedef NTSTATUS (NTAPI *_NtQueryObject)( - HANDLE ObjectHandle, - ULONG ObjectInformationClass, - PVOID ObjectInformation, - ULONG ObjectInformationLength, - PULONG ReturnLength -); - // Undocumented FILE_INFORMATION_CLASS: FileNameInformation static const SYSTEM_INFORMATION_CLASS SystemExtendedHandleInformation = (SYSTEM_INFORMATION_CLASS)64; From b8133ff4a11b1ae5a6d88613a08fabd121d55ebd Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 06:17:29 -0800 Subject: [PATCH 22/47] refactor process_handles --- psutil/arch/windows/global.c | 2 +- psutil/arch/windows/process_handles.c | 168 +++++++++++++------------- psutil/arch/windows/process_handles.h | 5 - 3 files changed, 87 insertions(+), 88 deletions(-) diff --git a/psutil/arch/windows/global.c b/psutil/arch/windows/global.c index cb565f8c1..75199b723 100644 --- a/psutil/arch/windows/global.c +++ b/psutil/arch/windows/global.c @@ -110,7 +110,7 @@ psutil_loadlibs() { psutil_GetActiveProcessorCount = ps_GetProcAddress( "kernel32", "GetActiveProcessorCount"); - psutil_NtWow64QueryInformationProcess64 = psutil_GetProcAddressFromLib( + psutil_NtWow64QueryInformationProcess64 = ps_GetProcAddressFromLib( "ntdll.dll", "NtWow64QueryInformationProcess64"); psutil_NtWow64ReadVirtualMemory64 = ps_GetProcAddressFromLib( diff --git a/psutil/arch/windows/process_handles.c b/psutil/arch/windows/process_handles.c index 4910f4efd..aa64a57d7 100644 --- a/psutil/arch/windows/process_handles.c +++ b/psutil/arch/windows/process_handles.c @@ -22,23 +22,7 @@ ULONG g_dwSize = 0; ULONG g_dwLength = 0; -PyObject * -psutil_get_open_files(long dwPid, HANDLE hProcess) { - OSVERSIONINFO osvi; - - ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); - osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); - GetVersionEx(&osvi); - - // Threaded version only works for Vista+ - if (osvi.dwMajorVersion >= 6) - return psutil_get_open_files_ntqueryobject(dwPid, hProcess); - else - return psutil_get_open_files_getmappedfilename(dwPid, hProcess); -} - - -VOID +static VOID psutil_get_open_files_init(BOOL threaded) { if (g_initialized == TRUE) return; @@ -54,7 +38,59 @@ psutil_get_open_files_init(BOOL threaded) { } -PyObject * +static DWORD WINAPI +psutil_wait_thread(LPVOID lpvParam) { + // Loop infinitely waiting for work + while (TRUE) { + WaitForSingleObject(g_hEvtStart, INFINITE); + + g_status = psutil_NtQueryObject( + g_hFile, + ObjectNameInformation, + g_pNameBuffer, + g_dwSize, + &g_dwLength); + SetEvent(g_hEvtFinish); + } +} + + +static DWORD +psutil_create_thread() { + DWORD dwWait = 0; + + if (g_hThread == NULL) + g_hThread = CreateThread( + NULL, + 0, + psutil_wait_thread, + NULL, + 0, + NULL); + if (g_hThread == NULL) + return GetLastError(); + + // Signal the worker thread to start + SetEvent(g_hEvtStart); + + // Wait for the worker thread to finish + dwWait = WaitForSingleObject(g_hEvtFinish, NTQO_TIMEOUT); + + // If the thread hangs, kill it and cleanup + if (dwWait == WAIT_TIMEOUT) { + SuspendThread(g_hThread); + TerminateThread(g_hThread, 1); + WaitForSingleObject(g_hThread, INFINITE); + CloseHandle(g_hThread); + + g_hThread = NULL; + } + + return dwWait; +} + + +static PyObject * psutil_get_open_files_ntqueryobject(long dwPid, HANDLE hProcess) { NTSTATUS status; PSYSTEM_HANDLE_INFORMATION_EX pHandleInfo = NULL; @@ -219,19 +255,19 @@ psutil_get_open_files_ntqueryobject(long dwPid, HANDLE hProcess) { } loop_cleanup: - Py_XDECREF(py_path); - py_path = NULL; + Py_XDECREF(py_path); + py_path = NULL; - if (g_pNameBuffer != NULL) - HeapFree(GetProcessHeap(), 0, g_pNameBuffer); - g_pNameBuffer = NULL; - g_dwSize = 0; - g_dwLength = 0; + if (g_pNameBuffer != NULL) + HeapFree(GetProcessHeap(), 0, g_pNameBuffer); + g_pNameBuffer = NULL; + g_dwSize = 0; + g_dwLength = 0; - if (g_hFile != NULL) - CloseHandle(g_hFile); - g_hFile = NULL; - } + if (g_hFile != NULL) + CloseHandle(g_hFile); + g_hFile = NULL; +} cleanup: if (g_pNameBuffer != NULL) @@ -259,59 +295,7 @@ psutil_get_open_files_ntqueryobject(long dwPid, HANDLE hProcess) { } -static DWORD -psutil_create_thread() { - DWORD dwWait = 0; - - if (g_hThread == NULL) - g_hThread = CreateThread( - NULL, - 0, - psutil_NtQueryObjectThread, - NULL, - 0, - NULL); - if (g_hThread == NULL) - return GetLastError(); - - // Signal the worker thread to start - SetEvent(g_hEvtStart); - - // Wait for the worker thread to finish - dwWait = WaitForSingleObject(g_hEvtFinish, NTQO_TIMEOUT); - - // If the thread hangs, kill it and cleanup - if (dwWait == WAIT_TIMEOUT) { - SuspendThread(g_hThread); - TerminateThread(g_hThread, 1); - WaitForSingleObject(g_hThread, INFINITE); - CloseHandle(g_hThread); - - g_hThread = NULL; - } - - return dwWait; -} - - -DWORD WINAPI -psutil_NtQueryObjectThread(LPVOID lpvParam) { - // Loop infinitely waiting for work - while (TRUE) { - WaitForSingleObject(g_hEvtStart, INFINITE); - - g_status = psutil_NtQueryObject( - g_hFile, - ObjectNameInformation, - g_pNameBuffer, - g_dwSize, - &g_dwLength); - SetEvent(g_hEvtFinish); - } -} - - -PyObject * +static PyObject * psutil_get_open_files_getmappedfilename(long dwPid, HANDLE hProcess) { NTSTATUS status; PSYSTEM_HANDLE_INFORMATION_EX pHandleInfo = NULL; @@ -503,3 +487,23 @@ psutil_get_open_files_getmappedfilename(long dwPid, HANDLE hProcess) { return py_retlist; } + + +/* + * The public function. + */ +PyObject * +psutil_get_open_files(long dwPid, HANDLE hProcess) { + OSVERSIONINFO osvi; + + ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); + osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); + GetVersionEx(&osvi); + + // Threaded version only works for Vista+ + if (osvi.dwMajorVersion >= 6) + return psutil_get_open_files_ntqueryobject(dwPid, hProcess); + else + return psutil_get_open_files_getmappedfilename(dwPid, hProcess); +} + diff --git a/psutil/arch/windows/process_handles.h b/psutil/arch/windows/process_handles.h index dc0b76551..f24adb67a 100644 --- a/psutil/arch/windows/process_handles.h +++ b/psutil/arch/windows/process_handles.h @@ -85,11 +85,6 @@ typedef struct _OBJECT_TYPE_INFORMATION { ULONG NonPagedPoolUsage; } OBJECT_TYPE_INFORMATION, *POBJECT_TYPE_INFORMATION; -PVOID GetLibraryProcAddress(PSTR LibraryName, PSTR ProcName); -VOID psutil_get_open_files_init(BOOL threaded); PyObject* psutil_get_open_files(long pid, HANDLE processHandle); -PyObject* psutil_get_open_files_ntqueryobject(long dwPid, HANDLE hProcess); -PyObject* psutil_get_open_files_getmappedfilename(long dwPid, HANDLE hProcess); -DWORD WINAPI psutil_NtQueryObjectThread(LPVOID lpvParam); #endif // __PROCESS_HANDLES_H__ From ec17917a67bd0e3081a46a43a5423751b954e745 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 06:23:44 -0800 Subject: [PATCH 23/47] remove unused def; move another one in ntextapi.h --- psutil/arch/windows/ntextapi.h | 18 +++++++++ psutil/arch/windows/process_handles.h | 55 +-------------------------- 2 files changed, 19 insertions(+), 54 deletions(-) diff --git a/psutil/arch/windows/ntextapi.h b/psutil/arch/windows/ntextapi.h index 548c437ce..edb3f7d6c 100644 --- a/psutil/arch/windows/ntextapi.h +++ b/psutil/arch/windows/ntextapi.h @@ -9,6 +9,7 @@ #include typedef LONG NTSTATUS; +#define STATUS_INFO_LENGTH_MISMATCH 0xc0000004 typedef struct { LARGE_INTEGER IdleTime; @@ -159,6 +160,23 @@ typedef enum _KWAIT_REASON { MaximumWaitReason = 37 } KWAIT_REASON, *PKWAIT_REASON; +typedef struct _SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX { + PVOID Object; + HANDLE UniqueProcessId; + HANDLE HandleValue; + ULONG GrantedAccess; + USHORT CreatorBackTraceIndex; + USHORT ObjectTypeIndex; + ULONG HandleAttributes; + ULONG Reserved; +} SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX, *PSYSTEM_HANDLE_TABLE_ENTRY_INFO_EX; + +typedef struct _SYSTEM_HANDLE_INFORMATION_EX { + ULONG_PTR NumberOfHandles; + ULONG_PTR Reserved; + SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX Handles[1]; +} SYSTEM_HANDLE_INFORMATION_EX, *PSYSTEM_HANDLE_INFORMATION_EX; + typedef struct _CLIENT_ID2 { HANDLE UniqueProcess; HANDLE UniqueThread; diff --git a/psutil/arch/windows/process_handles.h b/psutil/arch/windows/process_handles.h index f24adb67a..e14b08b34 100644 --- a/psutil/arch/windows/process_handles.h +++ b/psutil/arch/windows/process_handles.h @@ -17,13 +17,12 @@ #include #include #include - +#include "ntextapi.h" #ifndef NT_SUCCESS #define NT_SUCCESS(x) ((x) >= 0) #endif -#define STATUS_INFO_LENGTH_MISMATCH 0xc0000004 #define ObjectBasicInformation 0 #define ObjectNameInformation 1 #define ObjectTypeInformation 2 @@ -33,58 +32,6 @@ // Undocumented FILE_INFORMATION_CLASS: FileNameInformation static const SYSTEM_INFORMATION_CLASS SystemExtendedHandleInformation = (SYSTEM_INFORMATION_CLASS)64; -typedef struct _SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX { - PVOID Object; - HANDLE UniqueProcessId; - HANDLE HandleValue; - ULONG GrantedAccess; - USHORT CreatorBackTraceIndex; - USHORT ObjectTypeIndex; - ULONG HandleAttributes; - ULONG Reserved; -} SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX, *PSYSTEM_HANDLE_TABLE_ENTRY_INFO_EX; - -typedef struct _SYSTEM_HANDLE_INFORMATION_EX { - ULONG_PTR NumberOfHandles; - ULONG_PTR Reserved; - SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX Handles[1]; -} SYSTEM_HANDLE_INFORMATION_EX, *PSYSTEM_HANDLE_INFORMATION_EX; - -typedef enum _POOL_TYPE { - NonPagedPool, - PagedPool, - NonPagedPoolMustSucceed, - DontUseThisType, - NonPagedPoolCacheAligned, - PagedPoolCacheAligned, - NonPagedPoolCacheAlignedMustS -} POOL_TYPE, *PPOOL_TYPE; - -typedef struct _OBJECT_TYPE_INFORMATION { - UNICODE_STRING Name; - ULONG TotalNumberOfObjects; - ULONG TotalNumberOfHandles; - ULONG TotalPagedPoolUsage; - ULONG TotalNonPagedPoolUsage; - ULONG TotalNamePoolUsage; - ULONG TotalHandleTableUsage; - ULONG HighWaterNumberOfObjects; - ULONG HighWaterNumberOfHandles; - ULONG HighWaterPagedPoolUsage; - ULONG HighWaterNonPagedPoolUsage; - ULONG HighWaterNamePoolUsage; - ULONG HighWaterHandleTableUsage; - ULONG InvalidAttributes; - GENERIC_MAPPING GenericMapping; - ULONG ValidAccess; - BOOLEAN SecurityRequired; - BOOLEAN MaintainHandleCount; - USHORT MaintainTypeList; - POOL_TYPE PoolType; - ULONG PagedPoolUsage; - ULONG NonPagedPoolUsage; -} OBJECT_TYPE_INFORMATION, *POBJECT_TYPE_INFORMATION; - PyObject* psutil_get_open_files(long pid, HANDLE processHandle); #endif // __PROCESS_HANDLES_H__ From d2cdf617ae4248a431c1dbbcbea9354b2b4c5536 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 06:39:25 -0800 Subject: [PATCH 24/47] remove unnecessary definitions --- psutil/arch/windows/ntextapi.h | 2 ++ psutil/arch/windows/process_handles.c | 4 ++++ psutil/arch/windows/process_handles.h | 10 ---------- psutil/arch/windows/process_info.c | 4 ---- 4 files changed, 6 insertions(+), 14 deletions(-) diff --git a/psutil/arch/windows/ntextapi.h b/psutil/arch/windows/ntextapi.h index edb3f7d6c..f6bca5919 100644 --- a/psutil/arch/windows/ntextapi.h +++ b/psutil/arch/windows/ntextapi.h @@ -9,7 +9,9 @@ #include typedef LONG NTSTATUS; + #define STATUS_INFO_LENGTH_MISMATCH 0xc0000004 +#define STATUS_BUFFER_TOO_SMALL 0xC0000023L typedef struct { LARGE_INTEGER IdleTime; diff --git a/psutil/arch/windows/process_handles.c b/psutil/arch/windows/process_handles.c index aa64a57d7..8242b99cd 100644 --- a/psutil/arch/windows/process_handles.c +++ b/psutil/arch/windows/process_handles.c @@ -22,6 +22,10 @@ ULONG g_dwSize = 0; ULONG g_dwLength = 0; +#define ObjectNameInformation 1 +#define NTQO_TIMEOUT 100 + + static VOID psutil_get_open_files_init(BOOL threaded) { if (g_initialized == TRUE) diff --git a/psutil/arch/windows/process_handles.h b/psutil/arch/windows/process_handles.h index e14b08b34..d187def57 100644 --- a/psutil/arch/windows/process_handles.h +++ b/psutil/arch/windows/process_handles.h @@ -19,16 +19,6 @@ #include #include "ntextapi.h" -#ifndef NT_SUCCESS -#define NT_SUCCESS(x) ((x) >= 0) -#endif - -#define ObjectBasicInformation 0 -#define ObjectNameInformation 1 -#define ObjectTypeInformation 2 -#define HANDLE_TYPE_FILE 28 -#define NTQO_TIMEOUT 100 - // Undocumented FILE_INFORMATION_CLASS: FileNameInformation static const SYSTEM_INFORMATION_CLASS SystemExtendedHandleInformation = (SYSTEM_INFORMATION_CLASS)64; diff --git a/psutil/arch/windows/process_info.c b/psutil/arch/windows/process_info.c index f9343d3d2..747fe5f0d 100644 --- a/psutil/arch/windows/process_info.c +++ b/psutil/arch/windows/process_info.c @@ -136,10 +136,6 @@ typedef struct { (PSYSTEM_PROCESS_INFORMATION)((PCHAR)(Process) + \ ((PSYSTEM_PROCESS_INFORMATION)(Process))->NextEntryOffset) : NULL) -const int STATUS_INFO_LENGTH_MISMATCH = 0xC0000004; -const int STATUS_BUFFER_TOO_SMALL = 0xC0000023L; - - // A wrapper around GetModuleHandle and GetProcAddress. PVOID psutil_GetProcAddress(LPCSTR libname, LPCSTR procname) { From 071786df740d693602106bd39fb9680a78fd2a23 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 06:46:58 -0800 Subject: [PATCH 25/47] move definitions --- psutil/arch/windows/ntextapi.h | 1 + psutil/arch/windows/process_handles.c | 3 +++ psutil/arch/windows/process_handles.h | 17 ----------------- 3 files changed, 4 insertions(+), 17 deletions(-) diff --git a/psutil/arch/windows/ntextapi.h b/psutil/arch/windows/ntextapi.h index f6bca5919..6f364b76f 100644 --- a/psutil/arch/windows/ntextapi.h +++ b/psutil/arch/windows/ntextapi.h @@ -12,6 +12,7 @@ typedef LONG NTSTATUS; #define STATUS_INFO_LENGTH_MISMATCH 0xc0000004 #define STATUS_BUFFER_TOO_SMALL 0xC0000023L +#define SystemExtendedHandleInformation 64 typedef struct { LARGE_INTEGER IdleTime; diff --git a/psutil/arch/windows/process_handles.c b/psutil/arch/windows/process_handles.c index 8242b99cd..b193feee9 100644 --- a/psutil/arch/windows/process_handles.c +++ b/psutil/arch/windows/process_handles.c @@ -5,6 +5,9 @@ * */ +#include +#include +#include #include "process_handles.h" #include "process_info.h" #include "global.h" diff --git a/psutil/arch/windows/process_handles.h b/psutil/arch/windows/process_handles.h index d187def57..342ce8fd2 100644 --- a/psutil/arch/windows/process_handles.h +++ b/psutil/arch/windows/process_handles.h @@ -4,24 +4,7 @@ * found in the LICENSE file. */ -#ifndef __PROCESS_HANDLES_H__ -#define __PROCESS_HANDLES_H__ - -#ifndef UNICODE -#define UNICODE -#endif - #include -#include #include -#include -#include -#include -#include "ntextapi.h" - -// Undocumented FILE_INFORMATION_CLASS: FileNameInformation -static const SYSTEM_INFORMATION_CLASS SystemExtendedHandleInformation = (SYSTEM_INFORMATION_CLASS)64; PyObject* psutil_get_open_files(long pid, HANDLE processHandle); - -#endif // __PROCESS_HANDLES_H__ From 86a18e40292b02b55b2e58eda59505de6badf349 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 07:01:00 -0800 Subject: [PATCH 26/47] move struct --- psutil/_psutil_windows.c | 10 ---------- psutil/arch/windows/ntextapi.h | 13 +++++++++++++ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 20b97bab3..281fa1df1 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -149,16 +149,6 @@ typedef struct _MIB_UDP6TABLE_OWNER_PID { } MIB_UDP6TABLE_OWNER_PID, *PMIB_UDP6TABLE_OWNER_PID; #endif -typedef struct _PROCESSOR_POWER_INFORMATION { - ULONG Number; - ULONG MaxMhz; - ULONG CurrentMhz; - ULONG MhzLimit; - ULONG MaxIdleState; - ULONG CurrentIdleState; -} PROCESSOR_POWER_INFORMATION, *PPROCESSOR_POWER_INFORMATION; - - PIP_ADAPTER_ADDRESSES psutil_get_nic_addresses() { // allocate a 15 KB buffer to start with diff --git a/psutil/arch/windows/ntextapi.h b/psutil/arch/windows/ntextapi.h index 6f364b76f..96de388fd 100644 --- a/psutil/arch/windows/ntextapi.h +++ b/psutil/arch/windows/ntextapi.h @@ -258,6 +258,19 @@ typedef struct _SYSTEM_PROCESS_INFORMATION2 { #define SYSTEM_PROCESS_INFORMATION SYSTEM_PROCESS_INFORMATION2 #define PSYSTEM_PROCESS_INFORMATION PSYSTEM_PROCESS_INFORMATION2 +typedef struct _PROCESSOR_POWER_INFORMATION { + ULONG Number; + ULONG MaxMhz; + ULONG CurrentMhz; + ULONG MhzLimit; + ULONG MaxIdleState; + ULONG CurrentIdleState; +} PROCESSOR_POWER_INFORMATION, *PPROCESSOR_POWER_INFORMATION; + +/* + * Type defs + */ + typedef struct _WINSTATION_INFO { BYTE Reserved1[72]; ULONG SessionId; From b1046adfab1848ec9096b2585a475d2f6b920668 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 07:03:00 -0800 Subject: [PATCH 27/47] remove unused struct def --- psutil/_psutil_windows.c | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 281fa1df1..f74309a5a 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -68,26 +68,6 @@ typedef BOOL (WINAPI *PFN_GETLOGICALPROCESSORINFORMATIONEX)( static PFN_GETLOGICALPROCESSORINFORMATIONEX _GetLogicalProcessorInformationEx; #endif -// Fix for mingw32, see: -// https://github.com/giampaolo/psutil/issues/351#c2 -// This is actually a DISK_PERFORMANCE struct: -// https://msdn.microsoft.com/en-us/library/windows/desktop/ -// aa363991(v=vs.85).aspx -typedef struct _DISK_PERFORMANCE_WIN_2008 { - LARGE_INTEGER BytesRead; - LARGE_INTEGER BytesWritten; - LARGE_INTEGER ReadTime; - LARGE_INTEGER WriteTime; - LARGE_INTEGER IdleTime; - DWORD ReadCount; - DWORD WriteCount; - DWORD QueueDepth; - DWORD SplitCount; - LARGE_INTEGER QueryTime; - DWORD StorageDeviceNumber; - WCHAR StorageManagerName[8]; -} DISK_PERFORMANCE_WIN_2008; - // --- network connections mingw32 support #ifndef _IPRTRMIB_H #if (_WIN32_WINNT < 0x0600) // Windows XP @@ -2388,7 +2368,7 @@ psutil_net_io_counters(PyObject *self, PyObject *args) { */ static PyObject * psutil_disk_io_counters(PyObject *self, PyObject *args) { - DISK_PERFORMANCE_WIN_2008 diskPerformance; + DISK_PERFORMANCE diskPerformance; DWORD dwSize; HANDLE hDevice = NULL; char szDevice[MAX_PATH]; From 4ab6854542baaf5a9ad30e3c46a3499a344d023e Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 07:10:08 -0800 Subject: [PATCH 28/47] move stuff around --- psutil/arch/windows/ntextapi.h | 122 +++++++++++++++++++-------------- 1 file changed, 69 insertions(+), 53 deletions(-) diff --git a/psutil/arch/windows/ntextapi.h b/psutil/arch/windows/ntextapi.h index 96de388fd..029a83c32 100644 --- a/psutil/arch/windows/ntextapi.h +++ b/psutil/arch/windows/ntextapi.h @@ -14,6 +14,68 @@ typedef LONG NTSTATUS; #define STATUS_BUFFER_TOO_SMALL 0xC0000023L #define SystemExtendedHandleInformation 64 +/* + * ================================================================ + * Enums. + * ================================================================ + */ + +typedef enum _PROCESSINFOCLASS2 { + _ProcessBasicInformation, + ProcessQuotaLimits, + ProcessIoCounters, + ProcessVmCounters, + ProcessTimes, + ProcessBasePriority, + ProcessRaisePriority, + _ProcessDebugPort, + ProcessExceptionPort, + ProcessAccessToken, + ProcessLdtInformation, + ProcessLdtSize, + ProcessDefaultHardErrorMode, + ProcessIoPortHandlers, + ProcessPooledUsageAndLimits, + ProcessWorkingSetWatch, + ProcessUserModeIOPL, + ProcessEnableAlignmentFaultFixup, + ProcessPriorityClass, + ProcessWx86Information, + ProcessHandleCount, + ProcessAffinityMask, + ProcessPriorityBoost, + ProcessDeviceMap, + ProcessSessionInformation, + ProcessForegroundInformation, + _ProcessWow64Information, + /* added after XP+ */ + _ProcessImageFileName, + ProcessLUIDDeviceMapsEnabled, + _ProcessBreakOnTermination, + ProcessDebugObjectHandle, + ProcessDebugFlags, + ProcessHandleTracing, + ProcessIoPriority, + ProcessExecuteFlags, + ProcessResourceManagement, + ProcessCookie, + ProcessImageInformation, + MaxProcessInfoClass +} PROCESSINFOCLASS2; + +#define PROCESSINFOCLASS PROCESSINFOCLASS2 +#define ProcessBasicInformation _ProcessBasicInformation +#define ProcessWow64Information _ProcessWow64Information +#define ProcessDebugPort _ProcessDebugPort +#define ProcessImageFileName _ProcessImageFileName +#define ProcessBreakOnTermination _ProcessBreakOnTermination + +/* + * ================================================================ + * Structs. + * ================================================================ + */ + typedef struct { LARGE_INTEGER IdleTime; LARGE_INTEGER KernelTime; @@ -268,7 +330,9 @@ typedef struct _PROCESSOR_POWER_INFORMATION { } PROCESSOR_POWER_INFORMATION, *PPROCESSOR_POWER_INFORMATION; /* - * Type defs + * ================================================================ + * Type defs. + * ================================================================ */ typedef struct _WINSTATION_INFO { @@ -355,59 +419,11 @@ typedef NTSTATUS (NTAPI *_NtWow64ReadVirtualMemory64)( IN ULONG64 Size, OUT PULONG64 NumberOfBytesRead); -typedef enum _PROCESSINFOCLASS2 { - _ProcessBasicInformation, - ProcessQuotaLimits, - ProcessIoCounters, - ProcessVmCounters, - ProcessTimes, - ProcessBasePriority, - ProcessRaisePriority, - _ProcessDebugPort, - ProcessExceptionPort, - ProcessAccessToken, - ProcessLdtInformation, - ProcessLdtSize, - ProcessDefaultHardErrorMode, - ProcessIoPortHandlers, - ProcessPooledUsageAndLimits, - ProcessWorkingSetWatch, - ProcessUserModeIOPL, - ProcessEnableAlignmentFaultFixup, - ProcessPriorityClass, - ProcessWx86Information, - ProcessHandleCount, - ProcessAffinityMask, - ProcessPriorityBoost, - ProcessDeviceMap, - ProcessSessionInformation, - ProcessForegroundInformation, - _ProcessWow64Information, - /* added after XP+ */ - _ProcessImageFileName, - ProcessLUIDDeviceMapsEnabled, - _ProcessBreakOnTermination, - ProcessDebugObjectHandle, - ProcessDebugFlags, - ProcessHandleTracing, - ProcessIoPriority, - ProcessExecuteFlags, - ProcessResourceManagement, - ProcessCookie, - ProcessImageInformation, - MaxProcessInfoClass -} PROCESSINFOCLASS2; - -#define PROCESSINFOCLASS PROCESSINFOCLASS2 -#define ProcessBasicInformation _ProcessBasicInformation -#define ProcessWow64Information _ProcessWow64Information -#define ProcessDebugPort _ProcessDebugPort -#define ProcessImageFileName _ProcessImageFileName -#define ProcessBreakOnTermination _ProcessBreakOnTermination - /* - * Custom psutil definitions. These are dynamically set in global.c - * on module import. + * ================================================================ + * Custom psutil definitions. + * These are dynamically set in global.c on module import. + * ================================================================ */ _NtQuerySystemInformation \ From bb045afa1c4c7c36d2f61650e90a8343bb08f625 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 07:11:31 -0800 Subject: [PATCH 29/47] remove mingw32 support --- psutil/_psutil_windows.c | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index f74309a5a..72dc5fb69 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -68,27 +68,6 @@ typedef BOOL (WINAPI *PFN_GETLOGICALPROCESSORINFORMATIONEX)( static PFN_GETLOGICALPROCESSORINFORMATIONEX _GetLogicalProcessorInformationEx; #endif -// --- network connections mingw32 support -#ifndef _IPRTRMIB_H -#if (_WIN32_WINNT < 0x0600) // Windows XP -typedef struct _MIB_TCP6ROW_OWNER_PID { - UCHAR ucLocalAddr[16]; - DWORD dwLocalScopeId; - DWORD dwLocalPort; - UCHAR ucRemoteAddr[16]; - DWORD dwRemoteScopeId; - DWORD dwRemotePort; - DWORD dwState; - DWORD dwOwningPid; -} MIB_TCP6ROW_OWNER_PID, *PMIB_TCP6ROW_OWNER_PID; - -typedef struct _MIB_TCP6TABLE_OWNER_PID { - DWORD dwNumEntries; - MIB_TCP6ROW_OWNER_PID table[ANY_SIZE]; -} MIB_TCP6TABLE_OWNER_PID, *PMIB_TCP6TABLE_OWNER_PID; -#endif -#endif - #ifndef __IPHLPAPI_H__ typedef struct in6_addr { union { From 661f5bb88aa2e71b1cde25b9fedec5b88787c4e6 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 07:12:58 -0800 Subject: [PATCH 30/47] remove unused def --- psutil/_psutil_windows.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 72dc5fb69..2cfc35f5c 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -22,6 +22,7 @@ #include #endif #include +#include #include #include #include @@ -76,12 +77,6 @@ typedef struct in6_addr { } u; } IN6_ADDR, *PIN6_ADDR, FAR *LPIN6_ADDR; -typedef enum _UDP_TABLE_CLASS { - UDP_TABLE_BASIC, - UDP_TABLE_OWNER_PID, - UDP_TABLE_OWNER_MODULE -} UDP_TABLE_CLASS, *PUDP_TABLE_CLASS; - typedef struct _MIB_UDPROW_OWNER_PID { DWORD dwLocalAddr; DWORD dwLocalPort; From 9a03d6d9557e6177d4b5172a4b4bf13777b43a45 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 07:14:29 -0800 Subject: [PATCH 31/47] remove unused struct --- psutil/_psutil_windows.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 2cfc35f5c..1110bbd3e 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -23,6 +23,7 @@ #endif #include #include +#include #include #include #include @@ -83,10 +84,6 @@ typedef struct _MIB_UDPROW_OWNER_PID { DWORD dwOwningPid; } MIB_UDPROW_OWNER_PID, *PMIB_UDPROW_OWNER_PID; -typedef struct _MIB_UDPTABLE_OWNER_PID { - DWORD dwNumEntries; - MIB_UDPROW_OWNER_PID table[ANY_SIZE]; -} MIB_UDPTABLE_OWNER_PID, *PMIB_UDPTABLE_OWNER_PID; #endif #if (_WIN32_WINNT < 0x0600) // Windows XP From aba261d95fcdd95594db7be28267e38cd30d0103 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 07:17:37 -0800 Subject: [PATCH 32/47] remove win xp support --- psutil/_psutil_windows.c | 30 ------------------------------ psutil/arch/windows/ntextapi.h | 9 +++++++++ 2 files changed, 9 insertions(+), 30 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 1110bbd3e..84a865449 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -70,36 +70,6 @@ typedef BOOL (WINAPI *PFN_GETLOGICALPROCESSORINFORMATIONEX)( static PFN_GETLOGICALPROCESSORINFORMATIONEX _GetLogicalProcessorInformationEx; #endif -#ifndef __IPHLPAPI_H__ -typedef struct in6_addr { - union { - UCHAR Byte[16]; - USHORT Word[8]; - } u; -} IN6_ADDR, *PIN6_ADDR, FAR *LPIN6_ADDR; - -typedef struct _MIB_UDPROW_OWNER_PID { - DWORD dwLocalAddr; - DWORD dwLocalPort; - DWORD dwOwningPid; -} MIB_UDPROW_OWNER_PID, *PMIB_UDPROW_OWNER_PID; - -#endif - -#if (_WIN32_WINNT < 0x0600) // Windows XP -typedef struct _MIB_UDP6ROW_OWNER_PID { - UCHAR ucLocalAddr[16]; - DWORD dwLocalScopeId; - DWORD dwLocalPort; - DWORD dwOwningPid; -} MIB_UDP6ROW_OWNER_PID, *PMIB_UDP6ROW_OWNER_PID; - -typedef struct _MIB_UDP6TABLE_OWNER_PID { - DWORD dwNumEntries; - MIB_UDP6ROW_OWNER_PID table[ANY_SIZE]; -} MIB_UDP6TABLE_OWNER_PID, *PMIB_UDP6TABLE_OWNER_PID; -#endif - PIP_ADAPTER_ADDRESSES psutil_get_nic_addresses() { // allocate a 15 KB buffer to start with diff --git a/psutil/arch/windows/ntextapi.h b/psutil/arch/windows/ntextapi.h index 029a83c32..e94245a25 100644 --- a/psutil/arch/windows/ntextapi.h +++ b/psutil/arch/windows/ntextapi.h @@ -329,6 +329,15 @@ typedef struct _PROCESSOR_POWER_INFORMATION { ULONG CurrentIdleState; } PROCESSOR_POWER_INFORMATION, *PPROCESSOR_POWER_INFORMATION; +#ifndef __IPHLPAPI_H__ +typedef struct in6_addr { + union { + UCHAR Byte[16]; + USHORT Word[8]; + } u; +} IN6_ADDR, *PIN6_ADDR, FAR *LPIN6_ADDR; +#endif + /* * ================================================================ * Type defs. From 8f2f5b1e0754673ba9bac3018247bc8c8ac825bf Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 07:25:29 -0800 Subject: [PATCH 33/47] move struct around --- psutil/arch/windows/ntextapi.h | 13 +++++++++++++ psutil/arch/windows/process_info.c | 13 ------------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/psutil/arch/windows/ntextapi.h b/psutil/arch/windows/ntextapi.h index e94245a25..968be20ab 100644 --- a/psutil/arch/windows/ntextapi.h +++ b/psutil/arch/windows/ntextapi.h @@ -338,6 +338,19 @@ typedef struct in6_addr { } IN6_ADDR, *PIN6_ADDR, FAR *LPIN6_ADDR; #endif +// http://msdn.microsoft.com/en-us/library/aa813741(VS.85).aspx +typedef struct { + BYTE Reserved1[16]; + PVOID Reserved2[5]; + UNICODE_STRING CurrentDirectoryPath; + PVOID CurrentDirectoryHandle; + UNICODE_STRING DllPath; + UNICODE_STRING ImagePathName; + UNICODE_STRING CommandLine; + LPCWSTR env; +} RTL_USER_PROCESS_PARAMETERS_, *PRTL_USER_PROCESS_PARAMETERS_; + + /* * ================================================================ * Type defs. diff --git a/psutil/arch/windows/process_info.c b/psutil/arch/windows/process_info.c index 747fe5f0d..b098de758 100644 --- a/psutil/arch/windows/process_info.c +++ b/psutil/arch/windows/process_info.c @@ -25,18 +25,6 @@ // but unfortunately not in a usable way. // ==================================================================== -// http://msdn.microsoft.com/en-us/library/aa813741(VS.85).aspx -typedef struct { - BYTE Reserved1[16]; - PVOID Reserved2[5]; - UNICODE_STRING CurrentDirectoryPath; - PVOID CurrentDirectoryHandle; - UNICODE_STRING DllPath; - UNICODE_STRING ImagePathName; - UNICODE_STRING CommandLine; - LPCWSTR env; -} RTL_USER_PROCESS_PARAMETERS_, *PRTL_USER_PROCESS_PARAMETERS_; - // https://msdn.microsoft.com/en-us/library/aa813706(v=vs.85).aspx #ifdef _WIN64 typedef struct { @@ -128,7 +116,6 @@ typedef struct { } PEB64; #endif - #define PSUTIL_FIRST_PROCESS(Processes) ( \ (PSYSTEM_PROCESS_INFORMATION)(Processes)) #define PSUTIL_NEXT_PROCESS(Process) ( \ From d785adf927684090904bf748ad99d79ba56fe472 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 08:13:27 -0800 Subject: [PATCH 34/47] port GetLogicalProcessorInformationEx --- psutil/_psutil_windows.c | 29 ++++------------------ psutil/arch/windows/global.c | 6 ++++- psutil/arch/windows/ntextapi.h | 44 ++++++++++++++++++++++++---------- 3 files changed, 42 insertions(+), 37 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 84a865449..9bb631eea 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -62,13 +62,6 @@ Py_DECREF(_SOCK_STREAM);\ Py_DECREF(_SOCK_DGRAM); -#if (_WIN32_WINNT >= 0x0601) // Windows 7 -typedef BOOL (WINAPI *PFN_GETLOGICALPROCESSORINFORMATIONEX)( - LOGICAL_PROCESSOR_RELATIONSHIP relationship, - PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX Buffer, - PDWORD ReturnLength); -static PFN_GETLOGICALPROCESSORINFORMATIONEX _GetLogicalProcessorInformationEx; -#endif PIP_ADAPTER_ADDRESSES psutil_get_nic_addresses() { @@ -516,17 +509,6 @@ psutil_cpu_count_logical(PyObject *self, PyObject *args) { * Return the number of physical CPU cores (hyper-thread CPUs count * is excluded). */ -#if (_WIN32_WINNT < 0x0601) // < Windows 7 (namely Vista and XP) -static PyObject * -psutil_cpu_count_phys(PyObject *self, PyObject *args) { - // Note: we may have used GetLogicalProcessorInformation() - // but I don't want to prolong support for Windows XP and Vista. - // On such old systems psutil will compile but this API will - // just return None. - psutil_debug("Win < 7; cpu_count_phys() forced to None"); - Py_RETURN_NONE; -} -#else // Windows >= 7 static PyObject * psutil_cpu_count_phys(PyObject *self, PyObject *args) { DWORD rc; @@ -541,13 +523,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 - _GetLogicalProcessorInformationEx = psutil_GetProcAddressFromLib( - "kernel32", "GetLogicalProcessorInformationEx"); - if (_GetLogicalProcessorInformationEx == NULL) - return NULL; + if (psutil_GetLogicalProcessorInformationEx == NULL) { + psutil_debug("Win < 7; cpu_count_phys() forced to None"); + Py_RETURN_NONE; + } while (1) { - rc = _GetLogicalProcessorInformationEx( + rc = psutil_GetLogicalProcessorInformationEx( RelationAll, buffer, &length); if (rc == FALSE) { if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { @@ -596,7 +578,6 @@ psutil_cpu_count_phys(PyObject *self, PyObject *args) { free(buffer); Py_RETURN_NONE; } -#endif /* diff --git a/psutil/arch/windows/global.c b/psutil/arch/windows/global.c index 75199b723..f7094f512 100644 --- a/psutil/arch/windows/global.c +++ b/psutil/arch/windows/global.c @@ -106,10 +106,14 @@ psutil_loadlibs() { psutil_GetTickCount64 = ps_GetProcAddress( "kernel32", "GetTickCount64"); - // minimum requirement: Win 7 64-bit + // minimum requirement: Win 7 psutil_GetActiveProcessorCount = ps_GetProcAddress( "kernel32", "GetActiveProcessorCount"); + // minumum requirement: Win 7 + psutil_GetLogicalProcessorInformationEx = ps_GetProcAddressFromLib( + "kernel32", "GetLogicalProcessorInformationEx"); + psutil_NtWow64QueryInformationProcess64 = ps_GetProcAddressFromLib( "ntdll.dll", "NtWow64QueryInformationProcess64"); diff --git a/psutil/arch/windows/ntextapi.h b/psutil/arch/windows/ntextapi.h index 968be20ab..24b2585a3 100644 --- a/psutil/arch/windows/ntextapi.h +++ b/psutil/arch/windows/ntextapi.h @@ -350,13 +350,6 @@ typedef struct { LPCWSTR env; } RTL_USER_PROCESS_PARAMETERS_, *PRTL_USER_PROCESS_PARAMETERS_; - -/* - * ================================================================ - * Type defs. - * ================================================================ - */ - typedef struct _WINSTATION_INFO { BYTE Reserved1[72]; ULONG SessionId; @@ -369,8 +362,33 @@ typedef struct _WINSTATION_INFO { FILETIME CurrentTime; } WINSTATION_INFO, *PWINSTATION_INFO; -typedef BOOLEAN (WINAPI * PWINSTATIONQUERYINFORMATIONW) - (HANDLE,ULONG,WINSTATIONINFOCLASS,PVOID,ULONG,PULONG); +#if (_WIN32_WINNT < 0x0601) // Windows < 7 (Vista and XP) +typedef struct _SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX { + LOGICAL_PROCESSOR_RELATIONSHIP Relationship; + DWORD Size; + _ANONYMOUS_UNION + union { + PROCESSOR_RELATIONSHIP Processor; + NUMA_NODE_RELATIONSHIP NumaNode; + CACHE_RELATIONSHIP Cache; + GROUP_RELATIONSHIP Group; + } DUMMYUNIONNAME; +} SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX, *PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX; +#endif + +/* + * ================================================================ + * Type defs for modules loaded at runtime. + * ================================================================ + */ + +typedef BOOL (WINAPI *_GetLogicalProcessorInformationEx)( + LOGICAL_PROCESSOR_RELATIONSHIP relationship, + PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX Buffer, + PDWORD ReturnLength); + +typedef BOOLEAN (WINAPI * _WinStationQueryInformationW)( + HANDLE, ULONG, WINSTATIONINFOCLASS, PVOID, ULONG, PULONG); typedef NTSTATUS (NTAPI *_NtQueryInformationProcess)( HANDLE ProcessHandle, @@ -443,8 +461,7 @@ typedef NTSTATUS (NTAPI *_NtWow64ReadVirtualMemory64)( /* * ================================================================ - * Custom psutil definitions. - * These are dynamically set in global.c on module import. + * Custom psutil definitions for modules loaded at runtime. * ================================================================ */ @@ -457,7 +474,7 @@ _NtQueryInformationProcess \ _NtSetInformationProcess psutil_NtSetInformationProcess; -PWINSTATIONQUERYINFORMATIONW \ +_WinStationQueryInformationW \ psutil_WinStationQueryInformationW; _RtlIpv4AddressToStringA \ @@ -488,4 +505,7 @@ _NtQueryInformationProcess \ _NtWow64ReadVirtualMemory64 \ psutil_NtWow64ReadVirtualMemory64; +_GetLogicalProcessorInformationEx \ + psutil_GetLogicalProcessorInformationEx; + #endif // __NTEXTAPI_H__ From 8f70a95ffd1b8db9e61461c28d148ffa08ff7881 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 08:17:20 -0800 Subject: [PATCH 35/47] proper definition of WinStationQueryInformationW --- psutil/arch/windows/ntextapi.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/psutil/arch/windows/ntextapi.h b/psutil/arch/windows/ntextapi.h index 24b2585a3..ae090a15d 100644 --- a/psutil/arch/windows/ntextapi.h +++ b/psutil/arch/windows/ntextapi.h @@ -388,7 +388,12 @@ typedef BOOL (WINAPI *_GetLogicalProcessorInformationEx)( PDWORD ReturnLength); typedef BOOLEAN (WINAPI * _WinStationQueryInformationW)( - HANDLE, ULONG, WINSTATIONINFOCLASS, PVOID, ULONG, PULONG); + HANDLE ServerHandle, + ULONG SessionId, + WINSTATIONINFOCLASS WinStationInformationClass, + PVOID pWinStationInformation, + ULONG WinStationInformationLength, + PULONG pReturnLength); typedef NTSTATUS (NTAPI *_NtQueryInformationProcess)( HANDLE ProcessHandle, From 5e4df5f0333a76406c119dd50893b298b72d96c7 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 08:21:21 -0800 Subject: [PATCH 36/47] proper typedef redefinition --- psutil/arch/windows/ntextapi.h | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/psutil/arch/windows/ntextapi.h b/psutil/arch/windows/ntextapi.h index ae090a15d..719c58363 100644 --- a/psutil/arch/windows/ntextapi.h +++ b/psutil/arch/windows/ntextapi.h @@ -417,14 +417,13 @@ typedef NTSTATUS (NTAPI *_NtSetInformationProcess)( DWORD ProcessInformationLength ); -typedef DWORD (_stdcall * NTQSI_PROC) - (int, PVOID, ULONG, PULONG); +typedef PSTR (NTAPI * _RtlIpv4AddressToStringA)( + struct in_addr *Addr, + PSTR S); -typedef PSTR (NTAPI * _RtlIpv4AddressToStringA) - (struct in_addr *, PSTR); - -typedef PSTR (NTAPI * _RtlIpv6AddressToStringA) - (struct in6_addr *, PSTR); +typedef PSTR (NTAPI * _RtlIpv6AddressToStringA)( + struct in6_addr *Addr, + PSTR P); typedef DWORD (WINAPI * _GetExtendedTcpTable)( PVOID pTcpTable, @@ -447,7 +446,8 @@ typedef DWORD (WINAPI * _GetExtendedUdpTable)( typedef DWORD (CALLBACK *_GetActiveProcessorCount)( WORD GroupNumber); -typedef ULONGLONG (CALLBACK *_GetTickCount64)(void); +typedef ULONGLONG (CALLBACK *_GetTickCount64)( + void); typedef NTSTATUS (NTAPI *_NtQueryObject)( HANDLE Handle, @@ -458,11 +458,11 @@ typedef NTSTATUS (NTAPI *_NtQueryObject)( ); typedef NTSTATUS (NTAPI *_NtWow64ReadVirtualMemory64)( - IN HANDLE ProcessHandle, - IN PVOID64 BaseAddress, - OUT PVOID Buffer, - IN ULONG64 Size, - OUT PULONG64 NumberOfBytesRead); + HANDLE ProcessHandle, + PVOID64 BaseAddress, + PVOID Buffer, + ULONG64 Size, + PULONG64 NumberOfBytesRead); /* * ================================================================ @@ -503,8 +503,7 @@ _GetTickCount64 \ _NtQueryObject \ psutil_NtQueryObject; -// XXX: just an alias; probably unnecessary -_NtQueryInformationProcess \ +_NtQueryInformationProcess \ // XXX: just an alias; probably unnecessary psutil_NtWow64QueryInformationProcess64; _NtWow64ReadVirtualMemory64 \ From c2b26cb179036c71314ffe091a1836beb3a53897 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 08:28:32 -0800 Subject: [PATCH 37/47] move load-lib utility functions in global.c --- psutil/arch/windows/global.c | 40 ++++++++++++++++-------------- psutil/arch/windows/ntextapi.h | 3 ++- psutil/arch/windows/process_info.c | 37 --------------------------- psutil/arch/windows/process_info.h | 2 -- 4 files changed, 24 insertions(+), 58 deletions(-) diff --git a/psutil/arch/windows/global.c b/psutil/arch/windows/global.c index f7094f512..28a9e0083 100644 --- a/psutil/arch/windows/global.c +++ b/psutil/arch/windows/global.c @@ -13,7 +13,7 @@ // A wrapper around GetModuleHandle and GetProcAddress. static PVOID -ps_GetProcAddress(LPCSTR libname, LPCSTR procname) { +psutil_GetProcAddress(LPCSTR libname, LPCSTR procname) { HMODULE mod; FARPROC addr; @@ -31,7 +31,7 @@ ps_GetProcAddress(LPCSTR libname, LPCSTR procname) { // A wrapper around LoadLibrary and GetProcAddress. static PVOID -ps_GetProcAddressFromLib(LPCSTR libname, LPCSTR procname) { +psutil_GetProcAddressFromLib(LPCSTR libname, LPCSTR procname) { HMODULE mod; FARPROC addr; @@ -51,73 +51,77 @@ ps_GetProcAddressFromLib(LPCSTR libname, LPCSTR procname) { int psutil_loadlibs() { - // Mandatory. + /* + * Mandatory. + */ - psutil_NtQuerySystemInformation = ps_GetProcAddressFromLib( + psutil_NtQuerySystemInformation = psutil_GetProcAddressFromLib( "ntdll.dll", "NtQuerySystemInformation"); if (psutil_NtQuerySystemInformation == NULL) return 1; - psutil_NtQueryInformationProcess = ps_GetProcAddress( + psutil_NtQueryInformationProcess = psutil_GetProcAddress( "ntdll.dll", "NtQueryInformationProcess"); if (! psutil_NtQueryInformationProcess) return 1; - psutil_NtSetInformationProcess = ps_GetProcAddress( + psutil_NtSetInformationProcess = psutil_GetProcAddress( "ntdll.dll", "NtSetInformationProcess"); if (! psutil_NtSetInformationProcess) return 1; - psutil_WinStationQueryInformationW = ps_GetProcAddressFromLib( + psutil_WinStationQueryInformationW = psutil_GetProcAddressFromLib( "winsta.dll", "WinStationQueryInformationW"); if (! psutil_WinStationQueryInformationW) return 1; - psutil_NtQueryObject = ps_GetProcAddressFromLib( + psutil_NtQueryObject = psutil_GetProcAddressFromLib( "ntdll.dll", "NtQueryObject"); if (! psutil_NtQueryObject) return 1; - psutil_rtlIpv4AddressToStringA = ps_GetProcAddressFromLib( + psutil_rtlIpv4AddressToStringA = psutil_GetProcAddressFromLib( "ntdll.dll", "RtlIpv4AddressToStringA"); if (! psutil_rtlIpv4AddressToStringA) return 1; - psutil_rtlIpv6AddressToStringA = ps_GetProcAddressFromLib( + psutil_rtlIpv6AddressToStringA = psutil_GetProcAddressFromLib( "ntdll.dll", "RtlIpv6AddressToStringA"); if (! psutil_rtlIpv6AddressToStringA) return 1; // minimum requirement: Win XP SP3 - psutil_GetExtendedTcpTable = ps_GetProcAddressFromLib( + psutil_GetExtendedTcpTable = psutil_GetProcAddressFromLib( "iphlpapi.dll", "GetExtendedTcpTable"); if (! psutil_GetExtendedTcpTable) return 1; // minimum requirement: Win XP SP3 - psutil_GetExtendedUdpTable = ps_GetProcAddressFromLib( + psutil_GetExtendedUdpTable = psutil_GetProcAddressFromLib( "iphlpapi.dll", "GetExtendedUdpTable"); if (! psutil_GetExtendedUdpTable) return 1; - // Optional. + /* + * Optional. + */ // minimum requirement: Win Vista - psutil_GetTickCount64 = ps_GetProcAddress( + psutil_GetTickCount64 = psutil_GetProcAddress( "kernel32", "GetTickCount64"); // minimum requirement: Win 7 - psutil_GetActiveProcessorCount = ps_GetProcAddress( + psutil_GetActiveProcessorCount = psutil_GetProcAddress( "kernel32", "GetActiveProcessorCount"); // minumum requirement: Win 7 - psutil_GetLogicalProcessorInformationEx = ps_GetProcAddressFromLib( + psutil_GetLogicalProcessorInformationEx = psutil_GetProcAddressFromLib( "kernel32", "GetLogicalProcessorInformationEx"); - psutil_NtWow64QueryInformationProcess64 = ps_GetProcAddressFromLib( + psutil_NtWow64QueryInformationProcess64 = psutil_GetProcAddressFromLib( "ntdll.dll", "NtWow64QueryInformationProcess64"); - psutil_NtWow64ReadVirtualMemory64 = ps_GetProcAddressFromLib( + psutil_NtWow64ReadVirtualMemory64 = psutil_GetProcAddressFromLib( "ntdll.dll", "NtWow64ReadVirtualMemory64"); return 0; diff --git a/psutil/arch/windows/ntextapi.h b/psutil/arch/windows/ntextapi.h index 719c58363..e2b8329ef 100644 --- a/psutil/arch/windows/ntextapi.h +++ b/psutil/arch/windows/ntextapi.h @@ -503,7 +503,8 @@ _GetTickCount64 \ _NtQueryObject \ psutil_NtQueryObject; -_NtQueryInformationProcess \ // XXX: just an alias; probably unnecessary +// XXX: just an alias; probably unnecessary +_NtQueryInformationProcess \ psutil_NtWow64QueryInformationProcess64; _NtWow64ReadVirtualMemory64 \ diff --git a/psutil/arch/windows/process_info.c b/psutil/arch/windows/process_info.c index b098de758..17878f9b6 100644 --- a/psutil/arch/windows/process_info.c +++ b/psutil/arch/windows/process_info.c @@ -123,43 +123,6 @@ typedef struct { (PSYSTEM_PROCESS_INFORMATION)((PCHAR)(Process) + \ ((PSYSTEM_PROCESS_INFORMATION)(Process))->NextEntryOffset) : NULL) -// A wrapper around GetModuleHandle and GetProcAddress. -PVOID -psutil_GetProcAddress(LPCSTR libname, LPCSTR procname) { - HMODULE mod; - FARPROC addr; - - if ((mod = GetModuleHandleA(libname)) == NULL) { - PyErr_SetFromWindowsErrWithFilename(0, libname); - return NULL; - } - if ((addr = GetProcAddress(mod, procname)) == NULL) { - PyErr_SetFromWindowsErrWithFilename(0, procname); - return NULL; - } - return addr; -} - - -// A wrapper around LoadLibrary and GetProcAddress. -PVOID -psutil_GetProcAddressFromLib(LPCSTR libname, LPCSTR procname) { - HMODULE mod; - FARPROC addr; - - if ((mod = LoadLibraryA(libname)) == NULL) { - PyErr_SetFromWindowsErrWithFilename(0, libname); - return NULL; - } - if ((addr = GetProcAddress(mod, procname)) == NULL) { - PyErr_SetFromWindowsErrWithFilename(0, procname); - FreeLibrary(mod); - return NULL; - } - FreeLibrary(mod); - return addr; -} - // ==================================================================== // Process and PIDs utiilties. diff --git a/psutil/arch/windows/process_info.h b/psutil/arch/windows/process_info.h index d2e60b7c5..4278c4df9 100644 --- a/psutil/arch/windows/process_info.h +++ b/psutil/arch/windows/process_info.h @@ -23,8 +23,6 @@ int psutil_get_proc_info(DWORD pid, PSYSTEM_PROCESS_INFORMATION *retProcess, int psutil_assert_pid_exists(DWORD pid, char *err); int psutil_assert_pid_not_exists(DWORD pid, char *err); -PVOID psutil_GetProcAddress(LPCSTR libname, LPCSTR procname); -PVOID psutil_GetProcAddressFromLib(LPCSTR libname, LPCSTR procname); PyObject* psutil_get_cmdline(long pid, int use_peb); PyObject* psutil_get_cwd(long pid); From 388a537f2d2c7dad07598a155aad086d0dee80e0 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 08:33:23 -0800 Subject: [PATCH 38/47] fix compiler warning --- psutil/_psutil_windows.c | 1 - psutil/arch/windows/global.c | 6 ++++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 9bb631eea..d1fec4005 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -2593,7 +2593,6 @@ psutil_users(PyObject *self, PyObject *args) { PWTS_CLIENT_ADDRESS address; char address_str[50]; long long unix_time; - PWINSTATIONQUERYINFORMATIONW WinStationQueryInformationW; WINSTATION_INFO station_info; ULONG returnLen; PyObject *py_tuple = NULL; diff --git a/psutil/arch/windows/global.c b/psutil/arch/windows/global.c index 28a9e0083..ac633442f 100644 --- a/psutil/arch/windows/global.c +++ b/psutil/arch/windows/global.c @@ -4,7 +4,6 @@ * found in the LICENSE file. */ -#include #include #include #include "ntextapi.h" @@ -49,7 +48,10 @@ psutil_GetProcAddressFromLib(LPCSTR libname, LPCSTR procname) { } -int +/* + * This is executed on import and loads Windows APIs so that they + * are available globally. + */ psutil_loadlibs() { /* * Mandatory. From b2597350955d8fb7362ccecb697b1baeef0dd205 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 08:38:07 -0800 Subject: [PATCH 39/47] remove unused enum --- psutil/arch/windows/process_info.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/psutil/arch/windows/process_info.c b/psutil/arch/windows/process_info.c index 17878f9b6..bd8952a9b 100644 --- a/psutil/arch/windows/process_info.c +++ b/psutil/arch/windows/process_info.c @@ -77,10 +77,6 @@ typedef struct { /* More fields ... */ } PEB32; #else -typedef enum { - MemoryInformationBasic -} MEMORY_INFORMATION_CLASS; - typedef struct { PVOID Reserved1[2]; PVOID64 PebBaseAddress; From 8b41229166f1d12e021f4734fda92b4ad392f920 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 08:39:40 -0800 Subject: [PATCH 40/47] move stuff around --- psutil/_psutil_windows.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index d1fec4005..36a800f6a 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -56,11 +56,6 @@ #ifndef AF_INET6 #define AF_INET6 23 #endif -#define _psutil_conn_decref_objs() \ - Py_DECREF(_AF_INET); \ - Py_DECREF(_AF_INET6);\ - Py_DECREF(_SOCK_STREAM);\ - Py_DECREF(_SOCK_DGRAM); PIP_ADAPTER_ADDRESSES @@ -1483,6 +1478,13 @@ static DWORD __GetExtendedUdpTable(_GetExtendedUdpTable call, } +#define psutil_conn_decref_objs() \ + Py_DECREF(_AF_INET); \ + Py_DECREF(_AF_INET6);\ + Py_DECREF(_SOCK_STREAM);\ + Py_DECREF(_SOCK_DGRAM); + + /* * Return a list of network connections opened by a process */ @@ -1518,7 +1520,7 @@ psutil_net_connections(PyObject *self, PyObject *args) { goto error; if (!PySequence_Check(py_af_filter) || !PySequence_Check(py_type_filter)) { - _psutil_conn_decref_objs(); + psutil_conn_decref_objs(); PyErr_SetString(PyExc_TypeError, "arg 2 or 3 is not a sequence"); return NULL; } @@ -1526,18 +1528,18 @@ psutil_net_connections(PyObject *self, PyObject *args) { if (pid != -1) { pid_return = psutil_pid_is_running(pid); if (pid_return == 0) { - _psutil_conn_decref_objs(); + psutil_conn_decref_objs(); return NoSuchProcess(""); } else if (pid_return == -1) { - _psutil_conn_decref_objs(); + psutil_conn_decref_objs(); return NULL; } } py_retlist = PyList_New(0); if (py_retlist == NULL) { - _psutil_conn_decref_objs(); + psutil_conn_decref_objs(); return NULL; } @@ -1883,11 +1885,11 @@ psutil_net_connections(PyObject *self, PyObject *args) { tableSize = 0; } - _psutil_conn_decref_objs(); + psutil_conn_decref_objs(); return py_retlist; error: - _psutil_conn_decref_objs(); + psutil_conn_decref_objs(); Py_XDECREF(py_conn_tuple); Py_XDECREF(py_addr_tuple_local); Py_XDECREF(py_addr_tuple_remote); From b46ec41a5cf3d9638fc33f16b00f235859371940 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 09:05:15 -0800 Subject: [PATCH 41/47] use Py_BEGIN/ENDALLOW_THREADS; use PyErr_Clear() --- psutil/arch/windows/global.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/psutil/arch/windows/global.c b/psutil/arch/windows/global.c index ac633442f..d7ca4fce0 100644 --- a/psutil/arch/windows/global.c +++ b/psutil/arch/windows/global.c @@ -34,7 +34,10 @@ psutil_GetProcAddressFromLib(LPCSTR libname, LPCSTR procname) { HMODULE mod; FARPROC addr; - if ((mod = LoadLibraryA(libname)) == NULL) { + Py_BEGIN_ALLOW_THREADS + mod = LoadLibraryA(libname); + Py_END_ALLOW_THREADS + if (mod == NULL) { PyErr_SetFromWindowsErrWithFilename(0, libname); return NULL; } @@ -43,6 +46,7 @@ psutil_GetProcAddressFromLib(LPCSTR libname, LPCSTR procname) { FreeLibrary(mod); return NULL; } + // Causes crash. // FreeLibrary(mod); return addr; } @@ -126,5 +130,6 @@ psutil_loadlibs() { psutil_NtWow64ReadVirtualMemory64 = psutil_GetProcAddressFromLib( "ntdll.dll", "NtWow64ReadVirtualMemory64"); + PyErr_Clear(); return 0; } From fd690d477b7d520f05d9cf7824334c5bc2fc671e Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 09:38:21 -0800 Subject: [PATCH 42/47] add benchmark script --- scripts/internal/bench_win_loadlib.py | 49 +++++++++++++++++++++++++++ scripts/internal/winmake.py | 7 ++++ 2 files changed, 56 insertions(+) create mode 100644 scripts/internal/bench_win_loadlib.py diff --git a/scripts/internal/bench_win_loadlib.py b/scripts/internal/bench_win_loadlib.py new file mode 100644 index 000000000..61e2146fc --- /dev/null +++ b/scripts/internal/bench_win_loadlib.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python + +# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +""" +A simple micro benchmark script which tests the speedup introduced in: +https://github.com/giampaolo/psutil/pull/1422/ +""" + +from __future__ import print_function +import sys +import timeit + +import psutil + + +ITERATIONS = 10000 +apis = [ + 'psutil.boot_time()', + 'psutil.disk_io_counters()', + 'psutil.cpu_count(logical=False)', + 'psutil.cpu_count(logical=True)', + 'psutil.cpu_times(percpu=True)', + 'psutil.users()', + 'psutil.cpu_stats()', + # 'psutil.net_connections(kind="inet4")', # slow + 'proc.ionice()', + 'proc.ionice(0)', + # 'proc.open_files()', # slow + 'proc.cmdline()', + 'proc.cwd()', + # 'proc.environ()', # slow +] +apis = sorted(set(apis)) +setup = "import psutil; proc = psutil.Process()" + + +def main(): + if not psutil.WINDOWS: + sys.exit("Windows only") + for api in apis: + elapsed = timeit.timeit(api, setup=setup, number=ITERATIONS) + print("%-40s %.3f" % (api, elapsed)) + + +if __name__ == '__main__': + main() diff --git a/scripts/internal/winmake.py b/scripts/internal/winmake.py index b1ce7b8a4..7edae55f7 100755 --- a/scripts/internal/winmake.py +++ b/scripts/internal/winmake.py @@ -483,6 +483,13 @@ def bench_oneshot_2(): sh("%s -Wa scripts\\internal\\bench_oneshot_2.py" % PYTHON) +@cmd +def bench_loadlib(): + """Benchmarks for oneshot() ctx manager (see #1422).""" + install() + sh("%s -Wa scripts\\internal\\bench_win_loadlib.py" % PYTHON) + + def set_python(s): global PYTHON if os.path.isabs(s): From 71bb78db99fac9998979677cec2966d08e73ab3b Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 10:56:07 -0800 Subject: [PATCH 43/47] too complicated to load NtWow64ReadVirtualMemory64 dynamically; restore the original code --- make.bat | 2 + psutil/_psutil_windows.c | 6 +-- psutil/arch/windows/global.c | 7 +-- psutil/arch/windows/global.h | 4 ++ psutil/arch/windows/ntextapi.h | 10 ----- psutil/arch/windows/process_handles.c | 3 +- psutil/arch/windows/process_info.c | 61 ++++++++++++++++++++------- 7 files changed, 58 insertions(+), 35 deletions(-) diff --git a/make.bat b/make.bat index d47eaecc7..c83ec4cbd 100644 --- a/make.bat +++ b/make.bat @@ -27,6 +27,8 @@ if "%PYTHON%" == "" ( ) ) +set PYTHON=C:\Python36-32\python.exe + if "%TSCRIPT%" == "" ( set TSCRIPT=psutil\tests\__main__.py ) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 36a800f6a..3908884e1 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -32,14 +32,14 @@ // Link with Iphlpapi.lib #pragma comment(lib, "IPHLPAPI.lib") -#include "_psutil_common.h" +#include "arch/windows/ntextapi.h" +#include "arch/windows/global.h" #include "arch/windows/security.h" #include "arch/windows/process_info.h" #include "arch/windows/process_handles.h" -#include "arch/windows/ntextapi.h" -#include "arch/windows/global.h" #include "arch/windows/inet_ntop.h" #include "arch/windows/services.h" +#include "_psutil_common.h" /* diff --git a/psutil/arch/windows/global.c b/psutil/arch/windows/global.c index d7ca4fce0..c669620b7 100644 --- a/psutil/arch/windows/global.c +++ b/psutil/arch/windows/global.c @@ -11,7 +11,7 @@ // A wrapper around GetModuleHandle and GetProcAddress. -static PVOID +PVOID psutil_GetProcAddress(LPCSTR libname, LPCSTR procname) { HMODULE mod; FARPROC addr; @@ -29,7 +29,7 @@ psutil_GetProcAddress(LPCSTR libname, LPCSTR procname) { // A wrapper around LoadLibrary and GetProcAddress. -static PVOID +PVOID psutil_GetProcAddressFromLib(LPCSTR libname, LPCSTR procname) { HMODULE mod; FARPROC addr; @@ -127,9 +127,6 @@ psutil_loadlibs() { psutil_NtWow64QueryInformationProcess64 = psutil_GetProcAddressFromLib( "ntdll.dll", "NtWow64QueryInformationProcess64"); - psutil_NtWow64ReadVirtualMemory64 = psutil_GetProcAddressFromLib( - "ntdll.dll", "NtWow64ReadVirtualMemory64"); - PyErr_Clear(); return 0; } diff --git a/psutil/arch/windows/global.h b/psutil/arch/windows/global.h index 0a6cd25db..8d8287765 100644 --- a/psutil/arch/windows/global.h +++ b/psutil/arch/windows/global.h @@ -4,4 +4,8 @@ * found in the LICENSE file. */ +#include + int psutil_loadlibs(); +PVOID psutil_GetProcAddress(LPCSTR libname, LPCSTR procname); +PVOID psutil_GetProcAddressFromLib(LPCSTR libname, LPCSTR procname); diff --git a/psutil/arch/windows/ntextapi.h b/psutil/arch/windows/ntextapi.h index e2b8329ef..838b48e8c 100644 --- a/psutil/arch/windows/ntextapi.h +++ b/psutil/arch/windows/ntextapi.h @@ -457,13 +457,6 @@ typedef NTSTATUS (NTAPI *_NtQueryObject)( PULONG ReturnLength ); -typedef NTSTATUS (NTAPI *_NtWow64ReadVirtualMemory64)( - HANDLE ProcessHandle, - PVOID64 BaseAddress, - PVOID Buffer, - ULONG64 Size, - PULONG64 NumberOfBytesRead); - /* * ================================================================ * Custom psutil definitions for modules loaded at runtime. @@ -507,9 +500,6 @@ _NtQueryObject \ _NtQueryInformationProcess \ psutil_NtWow64QueryInformationProcess64; -_NtWow64ReadVirtualMemory64 \ - psutil_NtWow64ReadVirtualMemory64; - _GetLogicalProcessorInformationEx \ psutil_GetLogicalProcessorInformationEx; diff --git a/psutil/arch/windows/process_handles.c b/psutil/arch/windows/process_handles.c index b193feee9..f295f18d1 100644 --- a/psutil/arch/windows/process_handles.c +++ b/psutil/arch/windows/process_handles.c @@ -8,9 +8,10 @@ #include #include #include +#include "ntextapi.h" +#include "global.h" #include "process_handles.h" #include "process_info.h" -#include "global.h" #include "../../_psutil_common.h" CRITICAL_SECTION g_cs; diff --git a/psutil/arch/windows/process_info.c b/psutil/arch/windows/process_info.c index bd8952a9b..83c7b8bd7 100644 --- a/psutil/arch/windows/process_info.c +++ b/psutil/arch/windows/process_info.c @@ -12,10 +12,10 @@ #include #include -#include "security.h" -#include "process_info.h" #include "ntextapi.h" #include "global.h" +#include "security.h" +#include "process_info.h" #include "../../_psutil_common.h" @@ -77,6 +77,16 @@ typedef struct { /* More fields ... */ } PEB32; #else +/* When we are a 32 bit (WoW64) process accessing a 64 bit process we need to + use the 64 bit structure layout and a special function to read its memory. + */ +typedef NTSTATUS (NTAPI *_NtWow64ReadVirtualMemory64)( + HANDLE ProcessHandle, + PVOID64 BaseAddress, + PVOID Buffer, + ULONG64 Size, + PULONG64 NumberOfBytesRead); + typedef struct { PVOID Reserved1[2]; PVOID64 PebBaseAddress; @@ -112,6 +122,7 @@ typedef struct { } PEB64; #endif + #define PSUTIL_FIRST_PROCESS(Processes) ( \ (PSYSTEM_PROCESS_INFORMATION)(Processes)) #define PSUTIL_NEXT_PROCESS(Process) ( \ @@ -445,6 +456,11 @@ psutil_get_process_data(long pid, http://stackoverflow.com/a/14012919 http://www.drdobbs.com/embracing-64-bit-windows/184401966 */ + _NtQueryInformationProcess NtQueryInformationProcess = NULL; +#ifndef _WIN64 + static _NtQueryInformationProcess NtWow64QueryInformationProcess64 = NULL; + static _NtWow64ReadVirtualMemory64 NtWow64ReadVirtualMemory64 = NULL; +#endif HANDLE hProcess = NULL; LPCVOID src; SIZE_T size; @@ -525,13 +541,27 @@ psutil_get_process_data(long pid, PEB64 peb64; RTL_USER_PROCESS_PARAMETERS64 procParameters64; - if ((psutil_NtWow64QueryInformationProcess64 == NULL) || - (psutil_NtWow64ReadVirtualMemory64 == NULL)) { - AccessDenied("can't query 64-bit process in 32-bit-WoW mode"); - goto error; + if (NtWow64QueryInformationProcess64 == NULL) { + NtWow64QueryInformationProcess64 = \ + psutil_GetProcAddressFromLib( + "ntdll.dll", "NtWow64QueryInformationProcess64"); + if (NtWow64QueryInformationProcess64 == NULL) { + AccessDenied("can't query 64-bit process in 32-bit-WoW mode"); + goto error; + } + } + if (NtWow64ReadVirtualMemory64 == NULL) { + NtWow64ReadVirtualMemory64 = \ + psutil_GetProcAddressFromLib( + "ntdll.dll", "NtWow64ReadVirtualMemory64"); + if (NtWow64ReadVirtualMemory64 == NULL) { + AccessDenied("can't query 64-bit process in 32-bit-WoW mode"); + goto error; + } } - if (! NT_SUCCESS(psutil_NtWow64QueryInformationProcess64( + if (! NT_SUCCESS( + NtWow64QueryInformationProcess64( hProcess, ProcessBasicInformation, &pbi64, @@ -543,19 +573,19 @@ psutil_get_process_data(long pid, } // read peb - if (! NT_SUCCESS(psutil_NtWow64ReadVirtualMemory64( - hProcess, - pbi64.PebBaseAddress, - &peb64, - sizeof(peb64), - NULL))) + if (! NT_SUCCESS(NtWow64ReadVirtualMemory64( + hProcess, + pbi64.PebBaseAddress, + &peb64, + sizeof(peb64), + NULL))) { PyErr_SetFromWindowsErr(0); goto error; } // read process parameters - if (! NT_SUCCESS(psutil_NtWow64ReadVirtualMemory64( + if (! NT_SUCCESS(NtWow64ReadVirtualMemory64( hProcess, peb64.ProcessParameters, &procParameters64, @@ -581,7 +611,6 @@ psutil_get_process_data(long pid, } } else #endif - /* Target process is of the same bitness as us. */ { PROCESS_BASIC_INFORMATION pbi; @@ -655,7 +684,7 @@ psutil_get_process_data(long pid, #ifndef _WIN64 if (weAreWow64 && !theyAreWow64) { - if (! NT_SUCCESS(psutil_NtWow64ReadVirtualMemory64( + if (! NT_SUCCESS(NtWow64ReadVirtualMemory64( hProcess, src64, buffer, From e1e83201de0e874f6931174793123615136895d0 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 10:58:08 -0800 Subject: [PATCH 44/47] update benchmark script --- make.bat | 2 -- scripts/internal/bench_win_loadlib.py | 3 --- 2 files changed, 5 deletions(-) diff --git a/make.bat b/make.bat index c83ec4cbd..d47eaecc7 100644 --- a/make.bat +++ b/make.bat @@ -27,8 +27,6 @@ if "%PYTHON%" == "" ( ) ) -set PYTHON=C:\Python36-32\python.exe - if "%TSCRIPT%" == "" ( set TSCRIPT=psutil\tests\__main__.py ) diff --git a/scripts/internal/bench_win_loadlib.py b/scripts/internal/bench_win_loadlib.py index 61e2146fc..03dcb31db 100644 --- a/scripts/internal/bench_win_loadlib.py +++ b/scripts/internal/bench_win_loadlib.py @@ -29,9 +29,6 @@ 'proc.ionice()', 'proc.ionice(0)', # 'proc.open_files()', # slow - 'proc.cmdline()', - 'proc.cwd()', - # 'proc.environ()', # slow ] apis = sorted(set(apis)) setup = "import psutil; proc = psutil.Process()" From 1a286798a73a1a80ee2172926b13180e9cdbcfe1 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Feb 2019 11:38:37 -0800 Subject: [PATCH 45/47] fix test --- psutil/tests/test_windows.py | 5 +-- scripts/internal/bench_win_loadlib.py | 46 --------------------------- scripts/internal/winmake.py | 7 ---- 3 files changed, 3 insertions(+), 55 deletions(-) delete mode 100644 scripts/internal/bench_win_loadlib.py diff --git a/psutil/tests/test_windows.py b/psutil/tests/test_windows.py index 7be12d9c5..c98d892cf 100755 --- a/psutil/tests/test_windows.py +++ b/psutil/tests/test_windows.py @@ -778,10 +778,11 @@ def test_environ_32(self): self.assertEquals(e["THINK_OF_A_NUMBER"], str(os.getpid())) def test_environ_64(self): - # Environ 32 is not supported. p = psutil.Process(self.proc64.pid) - with self.assertRaises(psutil.AccessDenied): + try: p.environ() + except psutil.AccessDenied: + pass # =================================================================== diff --git a/scripts/internal/bench_win_loadlib.py b/scripts/internal/bench_win_loadlib.py deleted file mode 100644 index 03dcb31db..000000000 --- a/scripts/internal/bench_win_loadlib.py +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/env python - -# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -""" -A simple micro benchmark script which tests the speedup introduced in: -https://github.com/giampaolo/psutil/pull/1422/ -""" - -from __future__ import print_function -import sys -import timeit - -import psutil - - -ITERATIONS = 10000 -apis = [ - 'psutil.boot_time()', - 'psutil.disk_io_counters()', - 'psutil.cpu_count(logical=False)', - 'psutil.cpu_count(logical=True)', - 'psutil.cpu_times(percpu=True)', - 'psutil.users()', - 'psutil.cpu_stats()', - # 'psutil.net_connections(kind="inet4")', # slow - 'proc.ionice()', - 'proc.ionice(0)', - # 'proc.open_files()', # slow -] -apis = sorted(set(apis)) -setup = "import psutil; proc = psutil.Process()" - - -def main(): - if not psutil.WINDOWS: - sys.exit("Windows only") - for api in apis: - elapsed = timeit.timeit(api, setup=setup, number=ITERATIONS) - print("%-40s %.3f" % (api, elapsed)) - - -if __name__ == '__main__': - main() diff --git a/scripts/internal/winmake.py b/scripts/internal/winmake.py index 7edae55f7..b1ce7b8a4 100755 --- a/scripts/internal/winmake.py +++ b/scripts/internal/winmake.py @@ -483,13 +483,6 @@ def bench_oneshot_2(): sh("%s -Wa scripts\\internal\\bench_oneshot_2.py" % PYTHON) -@cmd -def bench_loadlib(): - """Benchmarks for oneshot() ctx manager (see #1422).""" - install() - sh("%s -Wa scripts\\internal\\bench_win_loadlib.py" % PYTHON) - - def set_python(s): global PYTHON if os.path.isabs(s): From 3dbfee8306b26acbb8f25c02f7885fe7d6e0bce7 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Thu, 21 Feb 2019 06:48:56 -0800 Subject: [PATCH 46/47] remove unused definition --- psutil/arch/windows/global.c | 3 --- psutil/arch/windows/ntextapi.h | 4 ---- 2 files changed, 7 deletions(-) diff --git a/psutil/arch/windows/global.c b/psutil/arch/windows/global.c index c669620b7..bb9970fea 100644 --- a/psutil/arch/windows/global.c +++ b/psutil/arch/windows/global.c @@ -124,9 +124,6 @@ psutil_loadlibs() { psutil_GetLogicalProcessorInformationEx = psutil_GetProcAddressFromLib( "kernel32", "GetLogicalProcessorInformationEx"); - psutil_NtWow64QueryInformationProcess64 = psutil_GetProcAddressFromLib( - "ntdll.dll", "NtWow64QueryInformationProcess64"); - PyErr_Clear(); return 0; } diff --git a/psutil/arch/windows/ntextapi.h b/psutil/arch/windows/ntextapi.h index 838b48e8c..e0006c7b9 100644 --- a/psutil/arch/windows/ntextapi.h +++ b/psutil/arch/windows/ntextapi.h @@ -496,10 +496,6 @@ _GetTickCount64 \ _NtQueryObject \ psutil_NtQueryObject; -// XXX: just an alias; probably unnecessary -_NtQueryInformationProcess \ - psutil_NtWow64QueryInformationProcess64; - _GetLogicalProcessorInformationEx \ psutil_GetLogicalProcessorInformationEx; From 03fca72137c7f3d34fb659fe7be75fc9ee6c9977 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Thu, 21 Feb 2019 06:54:42 -0800 Subject: [PATCH 47/47] add PyErr_Clear() --- psutil/arch/windows/process_info.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/psutil/arch/windows/process_info.c b/psutil/arch/windows/process_info.c index 83c7b8bd7..9a698e424 100644 --- a/psutil/arch/windows/process_info.c +++ b/psutil/arch/windows/process_info.c @@ -546,6 +546,7 @@ psutil_get_process_data(long pid, psutil_GetProcAddressFromLib( "ntdll.dll", "NtWow64QueryInformationProcess64"); if (NtWow64QueryInformationProcess64 == NULL) { + PyErr_Clear(); AccessDenied("can't query 64-bit process in 32-bit-WoW mode"); goto error; } @@ -555,6 +556,7 @@ psutil_get_process_data(long pid, psutil_GetProcAddressFromLib( "ntdll.dll", "NtWow64ReadVirtualMemory64"); if (NtWow64ReadVirtualMemory64 == NULL) { + PyErr_Clear(); AccessDenied("can't query 64-bit process in 32-bit-WoW mode"); goto error; }