summaryrefslogtreecommitdiff
path: root/psutil/_psutil_windows.c
diff options
context:
space:
mode:
Diffstat (limited to 'psutil/_psutil_windows.c')
-rw-r--r--psutil/_psutil_windows.c71
1 files changed, 48 insertions, 23 deletions
diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c
index 4dfae2d5..a1a68857 100644
--- a/psutil/_psutil_windows.c
+++ b/psutil/_psutil_windows.c
@@ -3,7 +3,15 @@
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
- * Windows platform-specific module methods for _psutil_windows
+ * Windows platform-specific module methods for _psutil_windows.
+ *
+ * List of undocumented Windows NT APIs which are used in here and in
+ * other modules:
+ * - NtQuerySystemInformation
+ * - NtQueryInformationProcess
+ * - NtQueryObject
+ * - NtSuspendProcess
+ * - NtResumeProcess
*/
// Fixes clash between winsock2.h and windows.h
@@ -797,8 +805,8 @@ psutil_GetProcWsetInformation(
}
else {
PyErr_Clear();
- psutil_debug("NtQueryVirtualMemory failed with %i", status);
- PyErr_SetString(PyExc_RuntimeError, "NtQueryVirtualMemory failed");
+ psutil_SetFromNTStatusErr(
+ status, "NtQueryVirtualMemory(MemoryWorkingSetInformation)");
}
HeapFree(GetProcessHeap(), 0, buffer);
return 1;
@@ -946,8 +954,11 @@ psutil_per_cpu_times(PyObject *self, PyObject *args) {
sppi,
ncpus * sizeof(_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION),
NULL);
- if (status != 0) {
- PyErr_SetFromWindowsErr(0);
+ if (! NT_SUCCESS(status)) {
+ psutil_SetFromNTStatusErr(
+ status,
+ "NtQuerySystemInformation(SystemProcessorPerformanceInformation)"
+ );
goto error;
}
@@ -1025,7 +1036,7 @@ psutil_proc_cwd(PyObject *self, PyObject *args) {
static PyObject *
psutil_proc_suspend_or_resume(PyObject *self, PyObject *args) {
long pid;
- int ret;
+ NTSTATUS status;
HANDLE hProcess;
PyObject* suspend;
@@ -1037,15 +1048,15 @@ psutil_proc_suspend_or_resume(PyObject *self, PyObject *args) {
return NULL;
if (PyObject_IsTrue(suspend))
- ret = psutil_NtSuspendProcess(hProcess);
+ status = psutil_NtSuspendProcess(hProcess);
else
- ret = psutil_NtResumeProcess(hProcess);
+ status = psutil_NtResumeProcess(hProcess);
- if (ret != 0) {
- PyErr_SetFromWindowsErr(0);
+ if (! NT_SUCCESS(status)) {
CloseHandle(hProcess);
- return NULL;
+ return psutil_SetFromNTStatusErr(status, "NtSuspend|ResumeProcess");
}
+
CloseHandle(hProcess);
Py_RETURN_NONE;
}
@@ -1339,6 +1350,7 @@ error:
// https://msdn.microsoft.com/library/aa365928.aspx
+// TODO properly handle return code
static DWORD __GetExtendedTcpTable(_GetExtendedTcpTable call,
ULONG address_family,
PVOID * data, DWORD * size)
@@ -1373,6 +1385,7 @@ static DWORD __GetExtendedTcpTable(_GetExtendedTcpTable call,
// https://msdn.microsoft.com/library/aa365930.aspx
+// TODO properly check return value
static DWORD __GetExtendedUdpTable(_GetExtendedUdpTable call,
ULONG address_family,
PVOID * data, DWORD * size)
@@ -1859,20 +1872,26 @@ psutil_proc_io_priority_get(PyObject *self, PyObject *args) {
long pid;
HANDLE hProcess;
DWORD IoPriority;
+ NTSTATUS status;
if (! PyArg_ParseTuple(args, "l", &pid))
return NULL;
+
hProcess = psutil_handle_from_pid(pid, PROCESS_QUERY_LIMITED_INFORMATION);
if (hProcess == NULL)
return NULL;
- psutil_NtQueryInformationProcess(
+
+ status = psutil_NtQueryInformationProcess(
hProcess,
ProcessIoPriority,
&IoPriority,
sizeof(DWORD),
NULL
);
+
CloseHandle(hProcess);
+ if (! NT_SUCCESS(status))
+ return psutil_SetFromNTStatusErr(status, "NtQueryInformationProcess");
return Py_BuildValue("i", IoPriority);
}
@@ -1885,15 +1904,17 @@ psutil_proc_io_priority_set(PyObject *self, PyObject *args) {
long pid;
DWORD prio;
HANDLE hProcess;
+ NTSTATUS status;
DWORD access = PROCESS_QUERY_INFORMATION | PROCESS_SET_INFORMATION;
if (! PyArg_ParseTuple(args, "li", &pid, &prio))
return NULL;
+
hProcess = psutil_handle_from_pid(pid, access);
if (hProcess == NULL)
return NULL;
- psutil_NtSetInformationProcess(
+ status = psutil_NtSetInformationProcess(
hProcess,
ProcessIoPriority,
(PVOID)&prio,
@@ -1901,6 +1922,8 @@ psutil_proc_io_priority_set(PyObject *self, PyObject *args) {
);
CloseHandle(hProcess);
+ if (! NT_SUCCESS(status))
+ return psutil_SetFromNTStatusErr(status, "NtSetInformationProcess");
Py_RETURN_NONE;
}
#endif
@@ -3217,9 +3240,9 @@ psutil_cpu_stats(PyObject *self, PyObject *args) {
spi,
ncpus * sizeof(_SYSTEM_PERFORMANCE_INFORMATION),
NULL);
- if (status != 0) {
- PyErr_SetFromOSErrnoWithSyscall(
- "NtQuerySystemInformation(SYSTEM_PERFORMANCE_INFORMATION)");
+ if (! NT_SUCCESS(status)) {
+ psutil_SetFromNTStatusErr(
+ status, "NtQuerySystemInformation(SystemPerformanceInformation)");
goto error;
}
@@ -3236,9 +3259,9 @@ psutil_cpu_stats(PyObject *self, PyObject *args) {
InterruptInformation,
ncpus * sizeof(SYSTEM_INTERRUPT_INFORMATION),
NULL);
- if (status != 0) {
- PyErr_SetFromOSErrnoWithSyscall(
- "NtQuerySystemInformation(SYSTEM_INTERRUPT_INFORMATION)");
+ if (! NT_SUCCESS(status)) {
+ psutil_SetFromNTStatusErr(
+ status, "NtQuerySystemInformation(SystemInterruptInformation)");
goto error;
}
for (i = 0; i < ncpus; i++) {
@@ -3258,9 +3281,10 @@ psutil_cpu_stats(PyObject *self, PyObject *args) {
sppi,
ncpus * sizeof(_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION),
NULL);
- if (status != 0) {
- PyErr_SetFromOSErrnoWithSyscall(
- "NtQuerySystemInformation(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)");
+ if (! NT_SUCCESS(status)) {
+ psutil_SetFromNTStatusErr(
+ status,
+ "NtQuerySystemInformation(SystemProcessorPerformanceInformation)");
goto error;
}
@@ -3661,7 +3685,8 @@ void init_psutil_windows(void)
module, "ERROR_INVALID_NAME", ERROR_INVALID_NAME);
PyModule_AddIntConstant(
module, "ERROR_SERVICE_DOES_NOT_EXIST", ERROR_SERVICE_DOES_NOT_EXIST);
-
+ PyModule_AddIntConstant(
+ module, "ERROR_PRIVILEGE_NOT_HELD", ERROR_PRIVILEGE_NOT_HELD);
PyModule_AddIntConstant(
module, "WINVER", PSUTIL_WINVER);
PyModule_AddIntConstant(