summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2019-02-24 07:16:50 -0800
committerGiampaolo Rodola <g.rodola@gmail.com>2019-02-24 07:16:50 -0800
commitd855cbff588f43156296ed323cfe867c80a339a8 (patch)
treebb71ae2deedc19e707733aef896a352a7d6a2b4b
parent54f09d67ed901fd8a5ee21d9f4a4ed3c62034498 (diff)
downloadpsutil-d855cbff588f43156296ed323cfe867c80a339a8.tar.gz
windows implementation
-rw-r--r--psutil/_psutil_common.c14
-rw-r--r--psutil/_psutil_windows.c54
-rw-r--r--psutil/arch/windows/process_info.c44
3 files changed, 65 insertions, 47 deletions
diff --git a/psutil/_psutil_common.c b/psutil/_psutil_common.c
index 119355d2..a4899487 100644
--- a/psutil/_psutil_common.c
+++ b/psutil/_psutil_common.c
@@ -7,8 +7,9 @@
*/
#include <Python.h>
-#include <stdio.h>
-
+#ifdef _WIN32
+#include <windows.h>
+#endif
// Global vars.
int PSUTIL_DEBUG = 0;
@@ -56,13 +57,18 @@ NoSuchProcess(const char *msg) {
*/
PyObject *
PyErr_SetFromOSErrnoWithSyscall(const char *syscall) {
- PyObject *exc;
- char fullmsg[1000];
+ char fullmsg[1024];
+#ifdef _WIN32
+ sprintf(fullmsg, "originated from %s", syscall);
+ PyErr_SetFromWindowsErrWithFilename(GetLastError(), fullmsg);
+#else
+ PyObject *exc;
sprintf(fullmsg, "%s (originated from %s)", strerror(errno), syscall);
exc = PyObject_CallFunction(PyExc_OSError, "(is)", errno, fullmsg);
PyErr_SetObject(PyExc_OSError, exc);
Py_XDECREF(exc);
+#endif
return NULL;
}
diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c
index c0d73645..00570bd2 100644
--- a/psutil/_psutil_windows.c
+++ b/psutil/_psutil_windows.c
@@ -286,7 +286,7 @@ psutil_proc_kill(PyObject *self, PyObject *args) {
err = GetLastError();
// See: https://github.com/giampaolo/psutil/issues/1099
if (err != ERROR_ACCESS_DENIED) {
- PyErr_SetFromWindowsErr(err);
+ PyErr_SetFromOSErrnoWithSyscall("TerminateProcess");
CloseHandle(hProcess);
return NULL;
}
@@ -332,7 +332,7 @@ psutil_proc_wait(PyObject *self, PyObject *args) {
// handle return code
if (retVal == WAIT_FAILED) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("WaitForSingleObject");
CloseHandle(hProcess);
return NULL;
}
@@ -354,7 +354,7 @@ psutil_proc_wait(PyObject *self, PyObject *args) {
// process is gone so we can get its process exit code. The PID
// may still stick around though but we'll handle that from Python.
if (GetExitCodeProcess(hProcess, &ExitCode) == 0) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("GetExitCodeProcess");
CloseHandle(hProcess);
return NULL;
}
@@ -652,7 +652,7 @@ psutil_proc_exe(PyObject *self, PyObject *args) {
#if (_WIN32_WINNT >= 0x0600) // Windows >= Vista
memset(exe, 0, MAX_PATH);
if (QueryFullProcessImageNameW(hProcess, 0, exe, &size) == 0) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("QueryFullProcessImageNameW");
CloseHandle(hProcess);
return NULL;
}
@@ -662,7 +662,7 @@ psutil_proc_exe(PyObject *self, PyObject *args) {
if (GetLastError() == 0)
PyErr_SetFromWindowsErr(ERROR_ACCESS_DENIED);
else
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("GetProcessImageFileNameW");
CloseHandle(hProcess);
return NULL;
}
@@ -689,11 +689,11 @@ psutil_proc_name(PyObject *self, PyObject *args) {
return NULL;
hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, pid);
if (hSnapShot == INVALID_HANDLE_VALUE)
- return PyErr_SetFromWindowsErr(0);
+ return PyErr_SetFromOSErrnoWithSyscall("CreateToolhelp32Snapshot");
pentry.dwSize = sizeof(PROCESSENTRY32W);
ok = Process32FirstW(hSnapShot, &pentry);
if (! ok) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("Process32FirstW");
CloseHandle(hSnapShot);
return NULL;
}
@@ -944,7 +944,7 @@ psutil_per_cpu_times(PyObject *self, PyObject *args) {
// allocates an array of _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION
// structures, one per processor
sppi = (_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION *) \
- malloc(ncpus * sizeof(_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION));
+ malloc(ncpus * sizeof(_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION));
if (sppi == NULL) {
PyErr_NoMemory();
goto error;
@@ -1046,7 +1046,7 @@ psutil_proc_suspend_or_resume(DWORD pid, int suspend) {
hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (hThreadSnap == INVALID_HANDLE_VALUE) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("CreateToolhelp32Snapshot");
return FALSE;
}
@@ -1054,7 +1054,7 @@ psutil_proc_suspend_or_resume(DWORD pid, int suspend) {
te32.dwSize = sizeof(THREADENTRY32);
if (! Thread32First(hThreadSnap, &te32)) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("Thread32First");
CloseHandle(hThreadSnap);
return FALSE;
}
@@ -1067,14 +1067,14 @@ psutil_proc_suspend_or_resume(DWORD pid, int suspend) {
hThread = OpenThread(THREAD_SUSPEND_RESUME, FALSE,
te32.th32ThreadID);
if (hThread == NULL) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("OpenThread");
CloseHandle(hThread);
CloseHandle(hThreadSnap);
return FALSE;
}
if (suspend == 1) {
if (SuspendThread(hThread) == (DWORD) - 1) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("SuspendThread");
CloseHandle(hThread);
CloseHandle(hThreadSnap);
return FALSE;
@@ -1082,7 +1082,7 @@ psutil_proc_suspend_or_resume(DWORD pid, int suspend) {
}
else {
if (ResumeThread(hThread) == (DWORD) - 1) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("ResumeThread");
CloseHandle(hThread);
CloseHandle(hThreadSnap);
return FALSE;
@@ -1156,7 +1156,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) {
hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (hThreadSnap == INVALID_HANDLE_VALUE) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("CreateToolhelp32Snapshot");
goto error;
}
@@ -1164,7 +1164,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) {
te32.dwSize = sizeof(THREADENTRY32);
if (! Thread32First(hThreadSnap, &te32)) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("Thread32First");
goto error;
}
@@ -1184,7 +1184,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) {
rc = GetThreadTimes(hThread, &ftDummy, &ftDummy, &ftKernel,
&ftUser);
if (rc == 0) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("GetThreadTimes");
goto error;
}
@@ -1311,7 +1311,7 @@ psutil_proc_username(PyObject *self, PyObject *args) {
return NULL;
if (!OpenProcessToken(processHandle, TOKEN_QUERY, &tokenHandle)) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("OpenProcessToken");
goto error;
}
@@ -1334,7 +1334,7 @@ psutil_proc_username(PyObject *self, PyObject *args) {
continue;
}
else {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("GetTokenInformation");
goto error;
}
}
@@ -1367,7 +1367,7 @@ psutil_proc_username(PyObject *self, PyObject *args) {
continue;
}
else {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("LookupAccountSidW");
goto error;
}
}
@@ -2606,7 +2606,7 @@ psutil_users(PyObject *self, PyObject *args) {
return NULL;
if (WTSEnumerateSessions(hServer, 0, 1, &sessions, &count) == 0) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("WTSEnumerateSessions");
goto error;
}
@@ -2626,7 +2626,7 @@ psutil_users(PyObject *self, PyObject *args) {
bytes = 0;
if (WTSQuerySessionInformationW(hServer, sessionId, WTSUserName,
&buffer_user, &bytes) == 0) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("WTSQuerySessionInformationW");
goto error;
}
if (bytes <= 2)
@@ -2636,7 +2636,7 @@ psutil_users(PyObject *self, PyObject *args) {
bytes = 0;
if (WTSQuerySessionInformation(hServer, sessionId, WTSClientAddress,
&buffer_addr, &bytes) == 0) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("WTSQuerySessionInformation");
goto error;
}
@@ -2666,6 +2666,7 @@ psutil_users(PyObject *self, PyObject *args) {
sizeof(station_info),
&returnLen))
{
+ PyErr_SetFromOSErrnoWithSyscall("WinStationQueryInformationW");
goto error;
}
@@ -3331,7 +3332,8 @@ psutil_cpu_stats(PyObject *self, PyObject *args) {
ncpus * sizeof(_SYSTEM_PERFORMANCE_INFORMATION),
NULL);
if (status != 0) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall(
+ "NtQuerySystemInformation(SYSTEM_PERFORMANCE_INFORMATION)");
goto error;
}
@@ -3349,7 +3351,8 @@ psutil_cpu_stats(PyObject *self, PyObject *args) {
ncpus * sizeof(SYSTEM_INTERRUPT_INFORMATION),
NULL);
if (status != 0) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall(
+ "NtQuerySystemInformation(SYSTEM_INTERRUPT_INFORMATION)");
goto error;
}
for (i = 0; i < ncpus; i++) {
@@ -3370,7 +3373,8 @@ psutil_cpu_stats(PyObject *self, PyObject *args) {
ncpus * sizeof(_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION),
NULL);
if (status != 0) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall(
+ "NtQuerySystemInformation(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)");
goto error;
}
diff --git a/psutil/arch/windows/process_info.c b/psutil/arch/windows/process_info.c
index 9a698e42..1d556473 100644
--- a/psutil/arch/windows/process_info.c
+++ b/psutil/arch/windows/process_info.c
@@ -227,7 +227,10 @@ psutil_check_phandle(HANDLE hProcess, DWORD pid) {
else if (ret == 0)
return NoSuchProcess("");
else if (ret == -1)
- return PyErr_SetFromWindowsErr(0);
+ if (GetLastError() == ERROR_ACCESS_DENIED)
+ return PyErr_SetFromWindowsErr(0);
+ else
+ return PyErr_SetFromOSErrnoWithSyscall("OpenProcess");
else // -2
return NULL;
}
@@ -406,7 +409,7 @@ psutil_get_process_region_size(HANDLE hProcess, LPCVOID src, SIZE_T *psize) {
MEMORY_BASIC_INFORMATION info;
if (!VirtualQueryEx(hProcess, src, &info, sizeof(info))) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("VirtualQueryEx");
return -1;
}
@@ -488,7 +491,8 @@ psutil_get_process_data(long pid,
sizeof(LPVOID),
NULL)))
{
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall(
+ "NtQueryInformationProcess(ProcessWow64Information)");
goto error;
}
@@ -499,7 +503,7 @@ psutil_get_process_data(long pid,
// read PEB
if (!ReadProcessMemory(hProcess, ppeb32, &peb32, sizeof(peb32), NULL)) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("ReadProcessMemory");
goto error;
}
@@ -509,7 +513,8 @@ psutil_get_process_data(long pid,
&procParameters32,
sizeof(procParameters32),
NULL)) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall(
+ "ReadProcessMemory(ProcessParameters)");
goto error;
}
@@ -530,8 +535,8 @@ psutil_get_process_data(long pid,
#else
/* 32 bit case. Check if the target is also 32 bit. */
if (!IsWow64Process(GetCurrentProcess(), &weAreWow64) ||
- !IsWow64Process(hProcess, &theyAreWow64)) {
- PyErr_SetFromWindowsErr(0);
+ !IsWow64Process(hProcess, &theyAreWow64)) {
+ PyErr_SetFromOSErrnoWithSyscall("IsWow64Process");
goto error;
}
@@ -570,7 +575,8 @@ psutil_get_process_data(long pid,
sizeof(pbi64),
NULL)))
{
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall(
+ "NtWow64QueryInformationProcess64(ProcessBasicInformation)");
goto error;
}
@@ -582,7 +588,7 @@ psutil_get_process_data(long pid,
sizeof(peb64),
NULL)))
{
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("NtWow64ReadVirtualMemory64");
goto error;
}
@@ -594,7 +600,8 @@ psutil_get_process_data(long pid,
sizeof(procParameters64),
NULL)))
{
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall(
+ "NtWow64ReadVirtualMemory64(ProcessParameters)");
goto error;
}
@@ -626,7 +633,8 @@ psutil_get_process_data(long pid,
sizeof(pbi),
NULL)))
{
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall(
+ "NtQueryInformationProcess(ProcessBasicInformation)");
goto error;
}
@@ -636,7 +644,7 @@ psutil_get_process_data(long pid,
&peb,
sizeof(peb),
NULL)) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("ReadProcessMemory");
goto error;
}
@@ -646,7 +654,8 @@ psutil_get_process_data(long pid,
&procParameters,
sizeof(procParameters),
NULL)) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall(
+ "ReadProcessMemory(ProcessParameters)");
goto error;
}
@@ -678,7 +687,6 @@ psutil_get_process_data(long pid,
}
buffer = calloc(size + 2, 1);
-
if (buffer == NULL) {
PyErr_NoMemory();
goto error;
@@ -693,13 +701,13 @@ psutil_get_process_data(long pid,
size,
NULL)))
{
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("NtWow64ReadVirtualMemory64");
goto error;
}
} else
#endif
if (!ReadProcessMemory(hProcess, src, buffer, size, NULL)) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("ReadProcessMemory");
goto error;
}
@@ -751,7 +759,7 @@ psutil_get_cmdline_data(long pid, WCHAR **pdata, SIZE_T *psize) {
&ret_length
);
if (! NT_SUCCESS(status)) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("NtQueryInformationProcess");
goto error;
}
@@ -820,7 +828,7 @@ psutil_get_cmdline(long pid, int use_peb) {
// attempt to parse the command line using Win32 API
szArglist = CommandLineToArgvW(data, &nArgs);
if (szArglist == NULL) {
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromOSErrnoWithSyscall("CommandLineToArgvW");
goto out;
}