diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2019-02-20 06:17:29 -0800 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2019-02-20 06:17:29 -0800 |
commit | b8133ff4a11b1ae5a6d88613a08fabd121d55ebd (patch) | |
tree | 64e5766c0e6b9ae04278d8186d6a90b25ad6617d | |
parent | e26ec99873f763f83df4fa8c75e3b80d6689350d (diff) | |
download | psutil-b8133ff4a11b1ae5a6d88613a08fabd121d55ebd.tar.gz |
refactor process_handles
-rw-r--r-- | psutil/arch/windows/global.c | 2 | ||||
-rw-r--r-- | psutil/arch/windows/process_handles.c | 168 | ||||
-rw-r--r-- | 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 cb565f8c..75199b72 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 4910f4ef..aa64a57d 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 @@ cleanup: } -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 @@ cleanup: 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 dc0b7655..f24adb67 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__ |