| 1 |
/* |
| 2 |
* Copyright (c) 2009, Jay Loden, 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 |
System related functions. Original code moved in here from |
| 9 |
psutil/_psutil_windows.c in 2023. For reference, here's the GIT blame |
| 10 |
history before the move: |
| 11 |
|
| 12 |
* boot_time(): https://github.com/giampaolo/psutil/blame/efd7ed3/psutil/_psutil_windows.c#L51-L60 |
| 13 |
* users(): https://github.com/giampaolo/psutil/blame/efd7ed3/psutil/_psutil_windows.c#L1103-L1244 |
| 14 |
*/ |
| 15 |
|
| 16 |
#include <Python.h> |
| 17 |
#include <windows.h> |
| 18 |
|
| 19 |
#include "ntextapi.h" |
| 20 |
#include "../../arch/all/init.h" |
| 21 |
|
| 22 |
|
| 23 |
// The number of seconds passed since boot. This is a monotonic timer, |
| 24 |
// not affected by system clock updates. On Windows 7+ it also includes |
| 25 |
// the time spent during suspend / hybernate. |
| 26 |
PyObject * |
| 27 |
psutil_uptime(PyObject *self, PyObject *args) { |
| 28 |
double uptimeSeconds; |
| 29 |
ULONGLONG interruptTime100ns = 0; |
| 30 |
|
| 31 |
if (QueryInterruptTime) { // Windows 7+ |
| 32 |
QueryInterruptTime(&interruptTime100ns); |
| 33 |
// Convert from 100-nanosecond to seconds. |
| 34 |
uptimeSeconds = interruptTime100ns / 10000000.0; |
| 35 |
} |
| 36 |
else { |
| 37 |
// Convert from milliseconds to seconds. |
| 38 |
uptimeSeconds = (double)GetTickCount64() / 1000.0; |
| 39 |
} |
| 40 |
return Py_BuildValue("d", uptimeSeconds); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
PyObject * |
| 45 |
psutil_users(PyObject *self, PyObject *args) { |
| 46 |
HANDLE hServer = WTS_CURRENT_SERVER_HANDLE; |
| 47 |
LPWSTR buffer_user = NULL; |
| 48 |
LPWSTR buffer_addr = NULL; |
| 49 |
LPWSTR buffer_info = NULL; |
| 50 |
PWTS_SESSION_INFOW sessions = NULL; |
| 51 |
DWORD count; |
| 52 |
DWORD i; |
| 53 |
DWORD sessionId; |
| 54 |
DWORD bytes; |
| 55 |
PWTS_CLIENT_ADDRESS address; |
| 56 |
char address_str[50]; |
| 57 |
PWTSINFOW wts_info; |
| 58 |
PyObject *py_tuple = NULL; |
| 59 |
PyObject *py_address = NULL; |
| 60 |
PyObject *py_username = NULL; |
| 61 |
PyObject *py_retlist = PyList_New(0); |
| 62 |
|
| 63 |
if (py_retlist == NULL) |
| 64 |
return NULL; |
| 65 |
|
| 66 |
if (WTSEnumerateSessionsW == NULL || |
| 67 |
WTSQuerySessionInformationW == NULL || |
| 68 |
WTSFreeMemory == NULL) { |
| 69 |
// If we don't run in an environment that is a Remote Desktop Services environment |
| 70 |
// the Wtsapi32 proc might not be present. |
| 71 |
// https://docs.microsoft.com/en-us/windows/win32/termserv/run-time-linking-to-wtsapi32-dll |
| 72 |
return py_retlist; |
| 73 |
} |
| 74 |
|
| 75 |
if (WTSEnumerateSessionsW(hServer, 0, 1, &sessions, &count) == 0) { |
| 76 |
if (ERROR_CALL_NOT_IMPLEMENTED == GetLastError()) { |
| 77 |
// On Windows Nano server, the Wtsapi32 API can be present, but return WinError 120. |
| 78 |
return py_retlist; |
| 79 |
} |
| 80 |
psutil_PyErr_SetFromOSErrnoWithSyscall("WTSEnumerateSessionsW"); |
| 81 |
goto error; |
| 82 |
} |
| 83 |
|
| 84 |
for (i = 0; i < count; i++) { |
| 85 |
py_address = NULL; |
| 86 |
py_tuple = NULL; |
| 87 |
sessionId = sessions[i].SessionId; |
| 88 |
if (buffer_user != NULL) |
| 89 |
WTSFreeMemory(buffer_user); |
| 90 |
if (buffer_addr != NULL) |
| 91 |
WTSFreeMemory(buffer_addr); |
| 92 |
if (buffer_info != NULL) |
| 93 |
WTSFreeMemory(buffer_info); |
| 94 |
|
| 95 |
buffer_user = NULL; |
| 96 |
buffer_addr = NULL; |
| 97 |
buffer_info = NULL; |
| 98 |
|
| 99 |
// username |
| 100 |
bytes = 0; |
| 101 |
if (WTSQuerySessionInformationW(hServer, sessionId, WTSUserName, |
| 102 |
&buffer_user, &bytes) == 0) { |
| 103 |
psutil_PyErr_SetFromOSErrnoWithSyscall( |
| 104 |
"WTSQuerySessionInformationW" |
| 105 |
); |
| 106 |
goto error; |
| 107 |
} |
| 108 |
if (bytes <= 2) |
| 109 |
continue; |
| 110 |
|
| 111 |
// address |
| 112 |
bytes = 0; |
| 113 |
if (WTSQuerySessionInformationW(hServer, sessionId, WTSClientAddress, |
| 114 |
&buffer_addr, &bytes) == 0) { |
| 115 |
psutil_PyErr_SetFromOSErrnoWithSyscall( |
| 116 |
"WTSQuerySessionInformationW" |
| 117 |
); |
| 118 |
goto error; |
| 119 |
} |
| 120 |
|
| 121 |
address = (PWTS_CLIENT_ADDRESS)buffer_addr; |
| 122 |
if (address->AddressFamily == 2) { // AF_INET == 2 |
| 123 |
sprintf_s(address_str, |
| 124 |
_countof(address_str), |
| 125 |
"%u.%u.%u.%u", |
| 126 |
// The IP address is offset by two bytes from the start of the Address member of the WTS_CLIENT_ADDRESS structure. |
| 127 |
address->Address[2], |
| 128 |
address->Address[3], |
| 129 |
address->Address[4], |
| 130 |
address->Address[5]); |
| 131 |
py_address = Py_BuildValue("s", address_str); |
| 132 |
if (!py_address) |
| 133 |
goto error; |
| 134 |
} |
| 135 |
else { |
| 136 |
Py_INCREF(Py_None); |
| 137 |
py_address = Py_None; |
| 138 |
} |
| 139 |
|
| 140 |
// login time |
| 141 |
bytes = 0; |
| 142 |
if (WTSQuerySessionInformationW(hServer, sessionId, WTSSessionInfo, |
| 143 |
&buffer_info, &bytes) == 0) { |
| 144 |
psutil_PyErr_SetFromOSErrnoWithSyscall( |
| 145 |
"WTSQuerySessionInformationW" |
| 146 |
); |
| 147 |
goto error; |
| 148 |
} |
| 149 |
wts_info = (PWTSINFOW)buffer_info; |
| 150 |
|
| 151 |
py_username = PyUnicode_FromWideChar(buffer_user, wcslen(buffer_user)); |
| 152 |
if (py_username == NULL) |
| 153 |
goto error; |
| 154 |
|
| 155 |
py_tuple = Py_BuildValue( |
| 156 |
"OOd", |
| 157 |
py_username, |
| 158 |
py_address, |
| 159 |
psutil_LargeIntegerToUnixTime(wts_info->ConnectTime) |
| 160 |
); |
| 161 |
if (!py_tuple) |
| 162 |
goto error; |
| 163 |
if (PyList_Append(py_retlist, py_tuple)) |
| 164 |
goto error; |
| 165 |
Py_CLEAR(py_username); |
| 166 |
Py_CLEAR(py_address); |
| 167 |
Py_CLEAR(py_tuple); |
| 168 |
} |
| 169 |
|
| 170 |
WTSFreeMemory(sessions); |
| 171 |
WTSFreeMemory(buffer_user); |
| 172 |
WTSFreeMemory(buffer_addr); |
| 173 |
WTSFreeMemory(buffer_info); |
| 174 |
return py_retlist; |
| 175 |
|
| 176 |
error: |
| 177 |
Py_XDECREF(py_username); |
| 178 |
Py_XDECREF(py_tuple); |
| 179 |
Py_XDECREF(py_address); |
| 180 |
Py_DECREF(py_retlist); |
| 181 |
|
| 182 |
if (sessions != NULL) |
| 183 |
WTSFreeMemory(sessions); |
| 184 |
if (buffer_user != NULL) |
| 185 |
WTSFreeMemory(buffer_user); |
| 186 |
if (buffer_addr != NULL) |
| 187 |
WTSFreeMemory(buffer_addr); |
| 188 |
if (buffer_info != NULL) |
| 189 |
WTSFreeMemory(buffer_info); |
| 190 |
return NULL; |
| 191 |
} |