| 1 |
/* |
| 2 |
* Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. |
| 3 |
* Use of this source code is governed by a BSD-style license that can be |
| 4 |
* found in the LICENSE file. |
| 5 |
*/ |
| 6 |
|
| 7 |
/* |
| 8 |
* This module retrieves handles opened by a process. |
| 9 |
* We use NtQuerySystemInformation to enumerate them and NtQueryObject |
| 10 |
* to obtain the corresponding file name. |
| 11 |
* Since NtQueryObject hangs for certain handle types we call it in a |
| 12 |
* separate thread which gets killed if it doesn't complete within 100ms. |
| 13 |
* This is a limitation of the Windows API and ProcessHacker uses the |
| 14 |
* same trick: https://github.com/giampaolo/psutil/pull/597 |
| 15 |
* |
| 16 |
* CREDITS: original implementation was written by Jeff Tang. |
| 17 |
* It was then rewritten by Giampaolo Rodola many years later. |
| 18 |
* Utility functions for getting the file handles and names were re-adapted |
| 19 |
* from the excellent ProcessHacker. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <windows.h> |
| 23 |
#include <Python.h> |
| 24 |
|
| 25 |
#include "../../arch/all/init.h" |
| 26 |
|
| 27 |
|
| 28 |
#define THREAD_TIMEOUT 100 // ms |
| 29 |
// Global object shared between the 2 threads. |
| 30 |
PUNICODE_STRING globalFileName = NULL; |
| 31 |
|
| 32 |
|
| 33 |
static int |
| 34 |
psutil_enum_handles(PSYSTEM_HANDLE_INFORMATION_EX *handles) { |
| 35 |
static ULONG initialBufferSize = 0x10000; |
| 36 |
NTSTATUS status; |
| 37 |
PVOID buffer; |
| 38 |
ULONG bufferSize; |
| 39 |
|
| 40 |
bufferSize = initialBufferSize; |
| 41 |
buffer = MALLOC_ZERO(bufferSize); |
| 42 |
if (buffer == NULL) { |
| 43 |
PyErr_NoMemory(); |
| 44 |
return 1; |
| 45 |
} |
| 46 |
|
| 47 |
while ((status = NtQuerySystemInformation( |
| 48 |
SystemExtendedHandleInformation, |
| 49 |
buffer, |
| 50 |
bufferSize, |
| 51 |
NULL |
| 52 |
)) == STATUS_INFO_LENGTH_MISMATCH) |
| 53 |
{ |
| 54 |
FREE(buffer); |
| 55 |
bufferSize *= 2; |
| 56 |
|
| 57 |
// Fail if we're resizing the buffer to something very large. |
| 58 |
if (bufferSize > 256 * 1024 * 1024) { |
| 59 |
PyErr_SetString( |
| 60 |
PyExc_RuntimeError, |
| 61 |
"SystemExtendedHandleInformation buffer too big"); |
| 62 |
return 1; |
| 63 |
} |
| 64 |
|
| 65 |
buffer = MALLOC_ZERO(bufferSize); |
| 66 |
if (buffer == NULL) { |
| 67 |
PyErr_NoMemory(); |
| 68 |
return 1; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
if (! NT_SUCCESS(status)) { |
| 73 |
psutil_SetFromNTStatusErr(status, "NtQuerySystemInformation"); |
| 74 |
FREE(buffer); |
| 75 |
return 1; |
| 76 |
} |
| 77 |
|
| 78 |
*handles = (PSYSTEM_HANDLE_INFORMATION_EX)buffer; |
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
static int |
| 84 |
psutil_get_filename(LPVOID lpvParam) { |
| 85 |
HANDLE hFile = *((HANDLE*)lpvParam); |
| 86 |
NTSTATUS status; |
| 87 |
ULONG bufferSize; |
| 88 |
ULONG attempts = 8; |
| 89 |
|
| 90 |
bufferSize = 0x200; |
| 91 |
globalFileName = MALLOC_ZERO(bufferSize); |
| 92 |
if (globalFileName == NULL) { |
| 93 |
PyErr_NoMemory(); |
| 94 |
goto error; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
// Note: also this is supposed to hang, hence why we do it in here. |
| 99 |
if (GetFileType(hFile) != FILE_TYPE_DISK) { |
| 100 |
SetLastError(0); |
| 101 |
globalFileName->Length = 0; |
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
// A loop is needed because the I/O subsystem likes to give us the |
| 106 |
// wrong return lengths... |
| 107 |
do { |
| 108 |
status = NtQueryObject( |
| 109 |
hFile, |
| 110 |
ObjectNameInformation, |
| 111 |
globalFileName, |
| 112 |
bufferSize, |
| 113 |
&bufferSize |
| 114 |
); |
| 115 |
if (status == STATUS_BUFFER_OVERFLOW || |
| 116 |
status == STATUS_INFO_LENGTH_MISMATCH || |
| 117 |
status == STATUS_BUFFER_TOO_SMALL) |
| 118 |
{ |
| 119 |
FREE(globalFileName); |
| 120 |
globalFileName = MALLOC_ZERO(bufferSize); |
| 121 |
if (globalFileName == NULL) { |
| 122 |
PyErr_NoMemory(); |
| 123 |
goto error; |
| 124 |
} |
| 125 |
} |
| 126 |
else { |
| 127 |
break; |
| 128 |
} |
| 129 |
} while (--attempts); |
| 130 |
|
| 131 |
if (! NT_SUCCESS(status)) { |
| 132 |
psutil_SetFromNTStatusErr(status, "NtQuerySystemInformation"); |
| 133 |
FREE(globalFileName); |
| 134 |
globalFileName = NULL; |
| 135 |
return 1; |
| 136 |
} |
| 137 |
|
| 138 |
return 0; |
| 139 |
|
| 140 |
error: |
| 141 |
if (globalFileName != NULL) { |
| 142 |
FREE(globalFileName); |
| 143 |
globalFileName = NULL; |
| 144 |
} |
| 145 |
return 1; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
static DWORD |
| 150 |
psutil_threaded_get_filename(HANDLE hFile) { |
| 151 |
DWORD dwWait; |
| 152 |
HANDLE hThread; |
| 153 |
DWORD threadRetValue; |
| 154 |
|
| 155 |
hThread = CreateThread( |
| 156 |
NULL, 0, (LPTHREAD_START_ROUTINE)psutil_get_filename, &hFile, 0, NULL); |
| 157 |
if (hThread == NULL) { |
| 158 |
psutil_PyErr_SetFromOSErrnoWithSyscall("CreateThread"); |
| 159 |
return 1; |
| 160 |
} |
| 161 |
|
| 162 |
// Wait for the worker thread to finish. |
| 163 |
dwWait = WaitForSingleObject(hThread, THREAD_TIMEOUT); |
| 164 |
|
| 165 |
// If the thread hangs, kill it and cleanup. |
| 166 |
if (dwWait == WAIT_TIMEOUT) { |
| 167 |
psutil_debug( |
| 168 |
"get handle name thread timed out after %i ms", THREAD_TIMEOUT); |
| 169 |
if (TerminateThread(hThread, 0) == 0) { |
| 170 |
psutil_PyErr_SetFromOSErrnoWithSyscall("TerminateThread"); |
| 171 |
CloseHandle(hThread); |
| 172 |
return 1; |
| 173 |
} |
| 174 |
CloseHandle(hThread); |
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
if (dwWait == WAIT_FAILED) { |
| 179 |
psutil_debug("WaitForSingleObject -> WAIT_FAILED"); |
| 180 |
if (TerminateThread(hThread, 0) == 0) { |
| 181 |
psutil_PyErr_SetFromOSErrnoWithSyscall( |
| 182 |
"WaitForSingleObject -> WAIT_FAILED -> TerminateThread" |
| 183 |
); |
| 184 |
CloseHandle(hThread); |
| 185 |
return 1; |
| 186 |
} |
| 187 |
psutil_PyErr_SetFromOSErrnoWithSyscall("WaitForSingleObject"); |
| 188 |
CloseHandle(hThread); |
| 189 |
return 1; |
| 190 |
} |
| 191 |
|
| 192 |
if (GetExitCodeThread(hThread, &threadRetValue) == 0) { |
| 193 |
if (TerminateThread(hThread, 0) == 0) { |
| 194 |
psutil_PyErr_SetFromOSErrnoWithSyscall( |
| 195 |
"GetExitCodeThread (failed) -> TerminateThread" |
| 196 |
); |
| 197 |
CloseHandle(hThread); |
| 198 |
return 1; |
| 199 |
} |
| 200 |
|
| 201 |
CloseHandle(hThread); |
| 202 |
psutil_PyErr_SetFromOSErrnoWithSyscall("GetExitCodeThread"); |
| 203 |
return 1; |
| 204 |
} |
| 205 |
CloseHandle(hThread); |
| 206 |
return threadRetValue; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
PyObject * |
| 211 |
psutil_get_open_files(DWORD dwPid, HANDLE hProcess) { |
| 212 |
PSYSTEM_HANDLE_INFORMATION_EX handlesList = NULL; |
| 213 |
PSYSTEM_HANDLE_TABLE_ENTRY_INFO_EX hHandle = NULL; |
| 214 |
HANDLE hFile = NULL; |
| 215 |
ULONG i = 0; |
| 216 |
BOOLEAN errorOccurred = FALSE; |
| 217 |
PyObject* py_path = NULL; |
| 218 |
PyObject* py_retlist = PyList_New(0);; |
| 219 |
|
| 220 |
if (!py_retlist) |
| 221 |
return NULL; |
| 222 |
|
| 223 |
// Due to the use of global variables, ensure only 1 call |
| 224 |
// to psutil_get_open_files() is running. |
| 225 |
EnterCriticalSection(&PSUTIL_CRITICAL_SECTION); |
| 226 |
|
| 227 |
if (psutil_enum_handles(&handlesList) != 0) |
| 228 |
goto error; |
| 229 |
|
| 230 |
for (i = 0; i < handlesList->NumberOfHandles; i++) { |
| 231 |
hHandle = &handlesList->Handles[i]; |
| 232 |
if ((ULONG_PTR)hHandle->UniqueProcessId != dwPid) |
| 233 |
continue; |
| 234 |
if (! DuplicateHandle( |
| 235 |
hProcess, |
| 236 |
hHandle->HandleValue, |
| 237 |
GetCurrentProcess(), |
| 238 |
&hFile, |
| 239 |
0, |
| 240 |
TRUE, |
| 241 |
DUPLICATE_SAME_ACCESS)) |
| 242 |
{ |
| 243 |
// Will fail if not a regular file; just skip it. |
| 244 |
continue; |
| 245 |
} |
| 246 |
|
| 247 |
// This will set *globalFileName* global variable. |
| 248 |
if (psutil_threaded_get_filename(hFile) != 0) |
| 249 |
goto error; |
| 250 |
|
| 251 |
if ((globalFileName != NULL) && (globalFileName->Length > 0)) { |
| 252 |
py_path = PyUnicode_FromWideChar(globalFileName->Buffer, |
| 253 |
wcslen(globalFileName->Buffer)); |
| 254 |
if (! py_path) |
| 255 |
goto error; |
| 256 |
if (PyList_Append(py_retlist, py_path)) |
| 257 |
goto error; |
| 258 |
Py_CLEAR(py_path); // also sets to NULL |
| 259 |
} |
| 260 |
|
| 261 |
// Loop cleanup section. |
| 262 |
if (globalFileName != NULL) { |
| 263 |
FREE(globalFileName); |
| 264 |
globalFileName = NULL; |
| 265 |
} |
| 266 |
CloseHandle(hFile); |
| 267 |
hFile = NULL; |
| 268 |
} |
| 269 |
|
| 270 |
goto exit; |
| 271 |
|
| 272 |
error: |
| 273 |
Py_XDECREF(py_retlist); |
| 274 |
errorOccurred = TRUE; |
| 275 |
goto exit; |
| 276 |
|
| 277 |
exit: |
| 278 |
if (hFile != NULL) |
| 279 |
CloseHandle(hFile); |
| 280 |
if (globalFileName != NULL) { |
| 281 |
FREE(globalFileName); |
| 282 |
globalFileName = NULL; |
| 283 |
} |
| 284 |
if (py_path != NULL) |
| 285 |
Py_DECREF(py_path); |
| 286 |
if (handlesList != NULL) |
| 287 |
FREE(handlesList); |
| 288 |
|
| 289 |
LeaveCriticalSection(&PSUTIL_CRITICAL_SECTION); |
| 290 |
if (errorOccurred == TRUE) |
| 291 |
return NULL; |
| 292 |
return py_retlist; |
| 293 |
} |