| 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 |
#include <Python.h> |
| 8 |
#include <windows.h> |
| 9 |
|
| 10 |
#include "../../arch/all/init.h" |
| 11 |
#include "ntextapi.h" |
| 12 |
|
| 13 |
|
| 14 |
// Needed to make these globally visible. |
| 15 |
int PSUTIL_WINVER; |
| 16 |
SYSTEM_INFO PSUTIL_SYSTEM_INFO; |
| 17 |
CRITICAL_SECTION PSUTIL_CRITICAL_SECTION; |
| 18 |
|
| 19 |
|
| 20 |
// ==================================================================== |
| 21 |
// --- Backward compatibility with missing Python.h APIs |
| 22 |
// ==================================================================== |
| 23 |
|
| 24 |
// PyPy on Windows. Missing APIs added in PyPy 7.3.14. |
| 25 |
#if defined(PYPY_VERSION) |
| 26 |
#if !defined(PyErr_SetFromWindowsErrWithFilename) |
| 27 |
PyObject * |
| 28 |
PyErr_SetFromWindowsErrWithFilename(int winerr, const char *filename) { |
| 29 |
PyObject *py_exc = NULL; |
| 30 |
PyObject *py_winerr = NULL; |
| 31 |
|
| 32 |
if (winerr == 0) |
| 33 |
winerr = GetLastError(); |
| 34 |
if (filename == NULL) { |
| 35 |
py_exc = PyObject_CallFunction(PyExc_OSError, "(is)", winerr, |
| 36 |
strerror(winerr)); |
| 37 |
} |
| 38 |
else { |
| 39 |
py_exc = PyObject_CallFunction(PyExc_OSError, "(iss)", winerr, |
| 40 |
strerror(winerr), filename); |
| 41 |
} |
| 42 |
if (py_exc == NULL) |
| 43 |
return NULL; |
| 44 |
|
| 45 |
py_winerr = Py_BuildValue("i", winerr); |
| 46 |
if (py_winerr == NULL) |
| 47 |
goto error; |
| 48 |
if (PyObject_SetAttrString(py_exc, "winerror", py_winerr) != 0) |
| 49 |
goto error; |
| 50 |
PyErr_SetObject(PyExc_OSError, py_exc); |
| 51 |
Py_XDECREF(py_exc); |
| 52 |
return NULL; |
| 53 |
|
| 54 |
error: |
| 55 |
Py_XDECREF(py_exc); |
| 56 |
Py_XDECREF(py_winerr); |
| 57 |
return NULL; |
| 58 |
} |
| 59 |
#endif // !defined(PyErr_SetFromWindowsErrWithFilename) |
| 60 |
|
| 61 |
|
| 62 |
#if !defined(PyErr_SetExcFromWindowsErrWithFilenameObject) |
| 63 |
PyObject * |
| 64 |
PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 65 |
PyObject *type, int ierr, PyObject *filename) |
| 66 |
{ |
| 67 |
// Original function is too complex. Just raise OSError without |
| 68 |
// filename. |
| 69 |
return PyErr_SetFromWindowsErrWithFilename(ierr, NULL); |
| 70 |
} |
| 71 |
#endif // !defined(PyErr_SetExcFromWindowsErrWithFilenameObject) |
| 72 |
#endif // defined(PYPY_VERSION) |
| 73 |
|
| 74 |
|
| 75 |
// ==================================================================== |
| 76 |
// --- Utils |
| 77 |
// ==================================================================== |
| 78 |
|
| 79 |
// Convert a NTSTATUS value to a Win32 error code and set the proper |
| 80 |
// Python exception. |
| 81 |
PVOID |
| 82 |
psutil_SetFromNTStatusErr(NTSTATUS status, const char *syscall) { |
| 83 |
ULONG err; |
| 84 |
char fullmsg[1024]; |
| 85 |
|
| 86 |
if (NT_NTWIN32(status)) |
| 87 |
err = WIN32_FROM_NTSTATUS(status); |
| 88 |
else |
| 89 |
err = RtlNtStatusToDosErrorNoTeb(status); |
| 90 |
// if (GetLastError() != 0) |
| 91 |
// err = GetLastError(); |
| 92 |
sprintf(fullmsg, "(originated from %s)", syscall); |
| 93 |
return PyErr_SetFromWindowsErrWithFilename(err, fullmsg); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
// A wrapper around GetModuleHandle and GetProcAddress. |
| 98 |
PVOID |
| 99 |
psutil_GetProcAddress(LPCSTR libname, LPCSTR apiname) { |
| 100 |
HMODULE mod; |
| 101 |
FARPROC addr; |
| 102 |
|
| 103 |
if ((mod = GetModuleHandleA(libname)) == NULL) { |
| 104 |
psutil_debug( |
| 105 |
"%s module not supported (needed for %s)", libname, apiname |
| 106 |
); |
| 107 |
PyErr_SetFromWindowsErrWithFilename(0, libname); |
| 108 |
return NULL; |
| 109 |
} |
| 110 |
if ((addr = GetProcAddress(mod, apiname)) == NULL) { |
| 111 |
psutil_debug("%s -> %s API not supported", libname, apiname); |
| 112 |
PyErr_SetFromWindowsErrWithFilename(0, apiname); |
| 113 |
return NULL; |
| 114 |
} |
| 115 |
return addr; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
// A wrapper around LoadLibrary and GetProcAddress. |
| 120 |
PVOID |
| 121 |
psutil_GetProcAddressFromLib(LPCSTR libname, LPCSTR apiname) { |
| 122 |
HMODULE mod; |
| 123 |
FARPROC addr; |
| 124 |
|
| 125 |
Py_BEGIN_ALLOW_THREADS |
| 126 |
mod = LoadLibraryA(libname); |
| 127 |
Py_END_ALLOW_THREADS |
| 128 |
if (mod == NULL) { |
| 129 |
psutil_debug("%s lib not supported (needed for %s)", libname, apiname); |
| 130 |
PyErr_SetFromWindowsErrWithFilename(0, libname); |
| 131 |
return NULL; |
| 132 |
} |
| 133 |
if ((addr = GetProcAddress(mod, apiname)) == NULL) { |
| 134 |
psutil_debug("%s -> %s not supported", libname, apiname); |
| 135 |
PyErr_SetFromWindowsErrWithFilename(0, apiname); |
| 136 |
FreeLibrary(mod); |
| 137 |
return NULL; |
| 138 |
} |
| 139 |
// Causes crash. |
| 140 |
// FreeLibrary(mod); |
| 141 |
return addr; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
// Convert the hi and lo parts of a FILETIME structure or a |
| 146 |
// LARGE_INTEGER to a UNIX time. A FILETIME contains a 64-bit value |
| 147 |
// representing the number of 100-nanosecond intervals since January 1, |
| 148 |
// 1601 (UTC). A UNIX time is the number of seconds that have elapsed |
| 149 |
// since the UNIX epoch, that is the time 00:00:00 UTC on 1 January |
| 150 |
// 1970. |
| 151 |
static double |
| 152 |
_to_unix_time(ULONGLONG hiPart, ULONGLONG loPart) { |
| 153 |
ULONGLONG ret; |
| 154 |
|
| 155 |
// 100 nanosecond intervals since January 1, 1601. |
| 156 |
ret = hiPart << 32; |
| 157 |
ret += loPart; |
| 158 |
// Change starting time to the Epoch (00:00:00 UTC, January 1, 1970). |
| 159 |
ret -= 116444736000000000ull; |
| 160 |
// Convert nano secs to secs. |
| 161 |
return (double) ret / 10000000ull; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
double |
| 166 |
psutil_FiletimeToUnixTime(FILETIME ft) { |
| 167 |
return _to_unix_time( |
| 168 |
(ULONGLONG)ft.dwHighDateTime, (ULONGLONG)ft.dwLowDateTime |
| 169 |
); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
double |
| 174 |
psutil_LargeIntegerToUnixTime(LARGE_INTEGER li) { |
| 175 |
return _to_unix_time( |
| 176 |
(ULONGLONG)li.HighPart, (ULONGLONG)li.LowPart |
| 177 |
); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
// ==================================================================== |
| 182 |
// --- Init / load libs |
| 183 |
// ==================================================================== |
| 184 |
|
| 185 |
|
| 186 |
static int |
| 187 |
psutil_loadlibs() { |
| 188 |
// --- Mandatory |
| 189 |
NtQuerySystemInformation = psutil_GetProcAddressFromLib( |
| 190 |
"ntdll.dll", "NtQuerySystemInformation"); |
| 191 |
if (! NtQuerySystemInformation) |
| 192 |
return 1; |
| 193 |
NtQueryInformationProcess = psutil_GetProcAddress( |
| 194 |
"ntdll.dll", "NtQueryInformationProcess"); |
| 195 |
if (! NtQueryInformationProcess) |
| 196 |
return 1; |
| 197 |
NtSetInformationProcess = psutil_GetProcAddress( |
| 198 |
"ntdll.dll", "NtSetInformationProcess"); |
| 199 |
if (! NtSetInformationProcess) |
| 200 |
return 1; |
| 201 |
NtQueryObject = psutil_GetProcAddressFromLib( |
| 202 |
"ntdll.dll", "NtQueryObject"); |
| 203 |
if (! NtQueryObject) |
| 204 |
return 1; |
| 205 |
RtlIpv4AddressToStringA = psutil_GetProcAddressFromLib( |
| 206 |
"ntdll.dll", "RtlIpv4AddressToStringA"); |
| 207 |
if (! RtlIpv4AddressToStringA) |
| 208 |
return 1; |
| 209 |
GetExtendedTcpTable = psutil_GetProcAddressFromLib( |
| 210 |
"iphlpapi.dll", "GetExtendedTcpTable"); |
| 211 |
if (! GetExtendedTcpTable) |
| 212 |
return 1; |
| 213 |
GetExtendedUdpTable = psutil_GetProcAddressFromLib( |
| 214 |
"iphlpapi.dll", "GetExtendedUdpTable"); |
| 215 |
if (! GetExtendedUdpTable) |
| 216 |
return 1; |
| 217 |
RtlGetVersion = psutil_GetProcAddressFromLib( |
| 218 |
"ntdll.dll", "RtlGetVersion"); |
| 219 |
if (! RtlGetVersion) |
| 220 |
return 1; |
| 221 |
NtSuspendProcess = psutil_GetProcAddressFromLib( |
| 222 |
"ntdll", "NtSuspendProcess"); |
| 223 |
if (! NtSuspendProcess) |
| 224 |
return 1; |
| 225 |
NtResumeProcess = psutil_GetProcAddressFromLib( |
| 226 |
"ntdll", "NtResumeProcess"); |
| 227 |
if (! NtResumeProcess) |
| 228 |
return 1; |
| 229 |
NtQueryVirtualMemory = psutil_GetProcAddressFromLib( |
| 230 |
"ntdll", "NtQueryVirtualMemory"); |
| 231 |
if (! NtQueryVirtualMemory) |
| 232 |
return 1; |
| 233 |
RtlNtStatusToDosErrorNoTeb = psutil_GetProcAddressFromLib( |
| 234 |
"ntdll", "RtlNtStatusToDosErrorNoTeb"); |
| 235 |
if (! RtlNtStatusToDosErrorNoTeb) |
| 236 |
return 1; |
| 237 |
GetTickCount64 = psutil_GetProcAddress( |
| 238 |
"kernel32", "GetTickCount64"); |
| 239 |
if (! GetTickCount64) |
| 240 |
return 1; |
| 241 |
RtlIpv6AddressToStringA = psutil_GetProcAddressFromLib( |
| 242 |
"ntdll.dll", "RtlIpv6AddressToStringA"); |
| 243 |
if (! RtlIpv6AddressToStringA) |
| 244 |
return 1; |
| 245 |
|
| 246 |
// --- Optional |
| 247 |
|
| 248 |
// minimum requirement: Win 7 |
| 249 |
QueryInterruptTime = psutil_GetProcAddressFromLib( |
| 250 |
"kernelbase.dll", "QueryInterruptTime"); |
| 251 |
// minimum requirement: Win 7 |
| 252 |
GetActiveProcessorCount = psutil_GetProcAddress( |
| 253 |
"kernel32", "GetActiveProcessorCount"); |
| 254 |
// minimum requirement: Win 7 |
| 255 |
GetLogicalProcessorInformationEx = psutil_GetProcAddressFromLib( |
| 256 |
"kernel32", "GetLogicalProcessorInformationEx"); |
| 257 |
// minimum requirements: Windows Server Core |
| 258 |
WTSEnumerateSessionsW = psutil_GetProcAddressFromLib( |
| 259 |
"wtsapi32.dll", "WTSEnumerateSessionsW"); |
| 260 |
WTSQuerySessionInformationW = psutil_GetProcAddressFromLib( |
| 261 |
"wtsapi32.dll", "WTSQuerySessionInformationW"); |
| 262 |
WTSFreeMemory = psutil_GetProcAddressFromLib( |
| 263 |
"wtsapi32.dll", "WTSFreeMemory"); |
| 264 |
|
| 265 |
PyErr_Clear(); |
| 266 |
return 0; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
static int |
| 271 |
psutil_set_winver() { |
| 272 |
RTL_OSVERSIONINFOEXW versionInfo; |
| 273 |
ULONG maj; |
| 274 |
ULONG min; |
| 275 |
|
| 276 |
versionInfo.dwOSVersionInfoSize = sizeof(RTL_OSVERSIONINFOEXW); |
| 277 |
memset(&versionInfo, 0, sizeof(RTL_OSVERSIONINFOEXW)); |
| 278 |
RtlGetVersion((PRTL_OSVERSIONINFOW)&versionInfo); |
| 279 |
maj = versionInfo.dwMajorVersion; |
| 280 |
min = versionInfo.dwMinorVersion; |
| 281 |
if (maj == 6 && min == 0) |
| 282 |
PSUTIL_WINVER = PSUTIL_WINDOWS_VISTA; // or Server 2008 |
| 283 |
else if (maj == 6 && min == 1) |
| 284 |
PSUTIL_WINVER = PSUTIL_WINDOWS_7; |
| 285 |
else if (maj == 6 && min == 2) |
| 286 |
PSUTIL_WINVER = PSUTIL_WINDOWS_8; |
| 287 |
else if (maj == 6 && min == 3) |
| 288 |
PSUTIL_WINVER = PSUTIL_WINDOWS_8_1; |
| 289 |
else if (maj == 10 && min == 0) |
| 290 |
PSUTIL_WINVER = PSUTIL_WINDOWS_10; |
| 291 |
else |
| 292 |
PSUTIL_WINVER = PSUTIL_WINDOWS_NEW; |
| 293 |
return 0; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
// Called on module import. |
| 298 |
int |
| 299 |
psutil_setup_windows(void) { |
| 300 |
if (psutil_loadlibs() != 0) |
| 301 |
return 1; |
| 302 |
if (psutil_set_winver() != 0) |
| 303 |
return 1; |
| 304 |
GetSystemInfo(&PSUTIL_SYSTEM_INFO); |
| 305 |
InitializeCriticalSection(&PSUTIL_CRITICAL_SECTION); |
| 306 |
return 0; |
| 307 |
} |