diff options
Diffstat (limited to 'psutil/arch/windows/proc.c')
-rw-r--r-- | psutil/arch/windows/proc.c | 340 |
1 files changed, 39 insertions, 301 deletions
diff --git a/psutil/arch/windows/proc.c b/psutil/arch/windows/proc.c index ef854332..d9b69744 100644 --- a/psutil/arch/windows/proc.c +++ b/psutil/arch/windows/proc.c @@ -1,19 +1,16 @@ /* - * Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. + * Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. * 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. - * - * List of undocumented Windows NT APIs which are used in here and in - * other modules: - * - NtQuerySystemInformation - * - NtQueryInformationProcess - * - NtQueryObject - * - NtSuspendProcess - * - NtResumeProcess */ +/* + * Process related functions. Original code was moved in here from + * psutil/_psutil_windows.c in 2023. For reference, here's the GIT blame + * history before the move: + * https://github.com/giampaolo/psutil/blame/59504a5/psutil/_psutil_windows.c +*/ + // Fixes clash between winsock2.h and windows.h #define WIN32_LEAN_AND_MEAN @@ -26,31 +23,22 @@ // Link with Iphlpapi.lib #pragma comment(lib, "IPHLPAPI.lib") -#include "_psutil_common.h" -#include "arch/windows/cpu.h" -#include "arch/windows/disk.h" -#include "arch/windows/mem.h" -#include "arch/windows/net.h" -#include "arch/windows/process_handles.h" -#include "arch/windows/process_info.h" -#include "arch/windows/process_utils.h" -#include "arch/windows/security.h" -#include "arch/windows/sensors.h" -#include "arch/windows/services.h" -#include "arch/windows/socks.h" -#include "arch/windows/sys.h" -#include "arch/windows/wmi.h" +#include "../../_psutil_common.h" +#include "proc.h" +#include "proc_info.h" +#include "proc_handles.h" +#include "proc_utils.h" -// Raised by Process.wait(). -static PyObject *TimeoutExpired; -static PyObject *TimeoutAbandoned; +// Raised by Process.wait(). +PyObject *TimeoutExpired; +PyObject *TimeoutAbandoned; /* * Return 1 if PID exists in the current process list, else 0. */ -static PyObject * +PyObject * psutil_pid_exists(PyObject *self, PyObject *args) { DWORD pid; int status; @@ -68,7 +56,7 @@ psutil_pid_exists(PyObject *self, PyObject *args) { /* * Return a Python list of all the PIDs running on the system. */ -static PyObject * +PyObject * psutil_pids(PyObject *self, PyObject *args) { DWORD *proclist = NULL; DWORD numberOfReturnedPIDs; @@ -107,7 +95,7 @@ error: /* * Kill a process given its PID. */ -static PyObject * +PyObject * psutil_proc_kill(PyObject *self, PyObject *args) { HANDLE hProcess; DWORD pid; @@ -141,7 +129,7 @@ psutil_proc_kill(PyObject *self, PyObject *args) { /* * Wait for process to terminate and return its exit code. */ -static PyObject * +PyObject * psutil_proc_wait(PyObject *self, PyObject *args) { HANDLE hProcess; DWORD ExitCode; @@ -215,7 +203,7 @@ psutil_proc_wait(PyObject *self, PyObject *args) { /* * Return a Python tuple (user_time, kernel_time) */ -static PyObject * +PyObject * psutil_proc_times(PyObject *self, PyObject *args) { DWORD pid; HANDLE hProcess; @@ -269,7 +257,7 @@ psutil_proc_times(PyObject *self, PyObject *args) { * since it succeeds even when a process is gone (but not if a PID never * existed). */ -static PyObject * +PyObject * psutil_proc_exe(PyObject *self, PyObject *args) { DWORD pid; NTSTATUS status; @@ -370,7 +358,7 @@ psutil_proc_exe(PyObject *self, PyObject *args) { /* * Return process memory information as a Python tuple. */ -static PyObject * +PyObject * psutil_proc_memory_info(PyObject *self, PyObject *args) { HANDLE hProcess; DWORD pid; @@ -492,7 +480,7 @@ psutil_GetProcWsetInformation( * https://dxr.mozilla.org/mozilla-central/source/xpcom/base/ * nsMemoryReporterManager.cpp */ -static PyObject * +PyObject * psutil_proc_memory_uss(PyObject *self, PyObject *args) { DWORD pid; HANDLE hProcess; @@ -542,7 +530,7 @@ psutil_proc_memory_uss(PyObject *self, PyObject *args) { /* * Resume or suspends a process */ -static PyObject * +PyObject * psutil_proc_suspend_or_resume(PyObject *self, PyObject *args) { DWORD pid; NTSTATUS status; @@ -571,7 +559,7 @@ psutil_proc_suspend_or_resume(PyObject *self, PyObject *args) { } -static PyObject * +PyObject * psutil_proc_threads(PyObject *self, PyObject *args) { HANDLE hThread = NULL; THREADENTRY32 te32 = {0}; @@ -676,7 +664,7 @@ error: } -static PyObject * +PyObject * psutil_proc_open_files(PyObject *self, PyObject *args) { DWORD pid; HANDLE processHandle; @@ -750,7 +738,7 @@ error: /* * Return process username as a "DOMAIN//USERNAME" string. */ -static PyObject * +PyObject * psutil_proc_username(PyObject *self, PyObject *args) { DWORD pid; PTOKEN_USER userToken = NULL; @@ -844,7 +832,7 @@ error: /* * Get process priority as a Python integer. */ -static PyObject * +PyObject * psutil_proc_priority_get(PyObject *self, PyObject *args) { DWORD pid; DWORD priority; @@ -871,7 +859,7 @@ psutil_proc_priority_get(PyObject *self, PyObject *args) { /* * Set process priority. */ -static PyObject * +PyObject * psutil_proc_priority_set(PyObject *self, PyObject *args) { DWORD pid; int priority; @@ -900,7 +888,7 @@ psutil_proc_priority_set(PyObject *self, PyObject *args) { /* * Get process IO priority as a Python integer. */ -static PyObject * +PyObject * psutil_proc_io_priority_get(PyObject *self, PyObject *args) { DWORD pid; HANDLE hProcess; @@ -932,7 +920,7 @@ psutil_proc_io_priority_get(PyObject *self, PyObject *args) { /* * Set process IO priority. */ -static PyObject * +PyObject * psutil_proc_io_priority_set(PyObject *self, PyObject *args) { DWORD pid; DWORD prio; @@ -964,7 +952,7 @@ psutil_proc_io_priority_set(PyObject *self, PyObject *args) { /* * Return a Python tuple referencing process I/O counters. */ -static PyObject * +PyObject * psutil_proc_io_counters(PyObject *self, PyObject *args) { DWORD pid; HANDLE hProcess; @@ -996,7 +984,7 @@ psutil_proc_io_counters(PyObject *self, PyObject *args) { /* * Return process CPU affinity as a bitmask */ -static PyObject * +PyObject * psutil_proc_cpu_affinity_get(PyObject *self, PyObject *args) { DWORD pid; HANDLE hProcess; @@ -1027,7 +1015,7 @@ psutil_proc_cpu_affinity_get(PyObject *self, PyObject *args) { /* * Set process CPU affinity */ -static PyObject * +PyObject * psutil_proc_cpu_affinity_set(PyObject *self, PyObject *args) { DWORD pid; HANDLE hProcess; @@ -1060,7 +1048,7 @@ psutil_proc_cpu_affinity_set(PyObject *self, PyObject *args) { /* * Return True if all process threads are in waiting/suspended state. */ -static PyObject * +PyObject * psutil_proc_is_suspended(PyObject *self, PyObject *args) { DWORD pid; ULONG i; @@ -1087,7 +1075,7 @@ psutil_proc_is_suspended(PyObject *self, PyObject *args) { /* * Return the number of handles opened by process. */ -static PyObject * +PyObject * psutil_proc_num_handles(PyObject *self, PyObject *args) { DWORD pid; HANDLE hProcess; @@ -1135,7 +1123,7 @@ static char *get_region_protection_string(ULONG protection) { /* * Return a list of process's memory mappings. */ -static PyObject * +PyObject * psutil_proc_memory_maps(PyObject *self, PyObject *args) { MEMORY_BASIC_INFORMATION basicInfo; DWORD pid; @@ -1212,7 +1200,7 @@ error: /* * Return a {pid:ppid, ...} dict for all running processes. */ -static PyObject * +PyObject * psutil_ppid_map(PyObject *self, PyObject *args) { PyObject *py_pid = NULL; PyObject *py_ppid = NULL; @@ -1255,253 +1243,3 @@ error: CloseHandle(handle); return NULL; } - - -// ------------------------ Python init --------------------------- - -static PyMethodDef -PsutilMethods[] = { - // --- per-process functions - {"proc_cmdline", (PyCFunction)(void(*)(void))psutil_proc_cmdline, - METH_VARARGS | METH_KEYWORDS}, - {"proc_cpu_affinity_get", psutil_proc_cpu_affinity_get, METH_VARARGS}, - {"proc_cpu_affinity_set", psutil_proc_cpu_affinity_set, METH_VARARGS}, - {"proc_cwd", psutil_proc_cwd, METH_VARARGS}, - {"proc_environ", psutil_proc_environ, METH_VARARGS}, - {"proc_exe", psutil_proc_exe, METH_VARARGS}, - {"proc_io_counters", psutil_proc_io_counters, METH_VARARGS}, - {"proc_io_priority_get", psutil_proc_io_priority_get, METH_VARARGS}, - {"proc_io_priority_set", psutil_proc_io_priority_set, METH_VARARGS}, - {"proc_is_suspended", psutil_proc_is_suspended, METH_VARARGS}, - {"proc_kill", psutil_proc_kill, METH_VARARGS}, - {"proc_memory_info", psutil_proc_memory_info, METH_VARARGS}, - {"proc_memory_maps", psutil_proc_memory_maps, METH_VARARGS}, - {"proc_memory_uss", psutil_proc_memory_uss, METH_VARARGS}, - {"proc_num_handles", psutil_proc_num_handles, METH_VARARGS}, - {"proc_open_files", psutil_proc_open_files, METH_VARARGS}, - {"proc_priority_get", psutil_proc_priority_get, METH_VARARGS}, - {"proc_priority_set", psutil_proc_priority_set, METH_VARARGS}, - {"proc_suspend_or_resume", psutil_proc_suspend_or_resume, METH_VARARGS}, - {"proc_threads", psutil_proc_threads, METH_VARARGS}, - {"proc_times", psutil_proc_times, METH_VARARGS}, - {"proc_username", psutil_proc_username, METH_VARARGS}, - {"proc_wait", psutil_proc_wait, METH_VARARGS}, - - // --- alternative pinfo interface - {"proc_info", psutil_proc_info, METH_VARARGS}, - - // --- system-related functions - {"boot_time", psutil_boot_time, METH_VARARGS}, - {"cpu_count_cores", psutil_cpu_count_cores, METH_VARARGS}, - {"cpu_count_logical", psutil_cpu_count_logical, METH_VARARGS}, - {"cpu_freq", psutil_cpu_freq, METH_VARARGS}, - {"cpu_stats", psutil_cpu_stats, METH_VARARGS}, - {"cpu_times", psutil_cpu_times, METH_VARARGS}, - {"disk_io_counters", psutil_disk_io_counters, METH_VARARGS}, - {"disk_partitions", psutil_disk_partitions, METH_VARARGS}, - {"disk_usage", psutil_disk_usage, METH_VARARGS}, - {"getloadavg", (PyCFunction)psutil_get_loadavg, METH_VARARGS}, - {"getpagesize", psutil_getpagesize, METH_VARARGS}, - {"swap_percent", psutil_swap_percent, METH_VARARGS}, - {"init_loadavg_counter", (PyCFunction)psutil_init_loadavg_counter, METH_VARARGS}, - {"net_connections", psutil_net_connections, METH_VARARGS}, - {"net_if_addrs", psutil_net_if_addrs, METH_VARARGS}, - {"net_if_stats", psutil_net_if_stats, METH_VARARGS}, - {"net_io_counters", psutil_net_io_counters, METH_VARARGS}, - {"per_cpu_times", psutil_per_cpu_times, METH_VARARGS}, - {"pid_exists", psutil_pid_exists, METH_VARARGS}, - {"pids", psutil_pids, METH_VARARGS}, - {"ppid_map", psutil_ppid_map, METH_VARARGS}, - {"sensors_battery", psutil_sensors_battery, METH_VARARGS}, - {"users", psutil_users, METH_VARARGS}, - {"virtual_mem", psutil_virtual_mem, METH_VARARGS}, - - // --- windows services - {"winservice_enumerate", psutil_winservice_enumerate, METH_VARARGS}, - {"winservice_query_config", psutil_winservice_query_config, METH_VARARGS}, - {"winservice_query_descr", psutil_winservice_query_descr, METH_VARARGS}, - {"winservice_query_status", psutil_winservice_query_status, METH_VARARGS}, - {"winservice_start", psutil_winservice_start, METH_VARARGS}, - {"winservice_stop", psutil_winservice_stop, METH_VARARGS}, - - // --- windows API bindings - {"QueryDosDevice", psutil_QueryDosDevice, METH_VARARGS}, - - // --- others - {"set_debug", psutil_set_debug, METH_VARARGS}, - - {NULL, NULL, 0, NULL} -}; - - -struct module_state { - PyObject *error; -}; - -#if PY_MAJOR_VERSION >= 3 -#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m)) -#else -#define GETSTATE(m) (&_state) -static struct module_state _state; -#endif - -#if PY_MAJOR_VERSION >= 3 - -static int psutil_windows_traverse(PyObject *m, visitproc visit, void *arg) { - Py_VISIT(GETSTATE(m)->error); - return 0; -} - -static int psutil_windows_clear(PyObject *m) { - Py_CLEAR(GETSTATE(m)->error); - return 0; -} - -static struct PyModuleDef moduledef = { - PyModuleDef_HEAD_INIT, - "psutil_windows", - NULL, - sizeof(struct module_state), - PsutilMethods, - NULL, - psutil_windows_traverse, - psutil_windows_clear, - NULL -}; - -#define INITERROR return NULL - -PyMODINIT_FUNC PyInit__psutil_windows(void) - -#else -#define INITERROR return -void init_psutil_windows(void) -#endif -{ - struct module_state *st = NULL; -#if PY_MAJOR_VERSION >= 3 - PyObject *module = PyModule_Create(&moduledef); -#else - PyObject *module = Py_InitModule("_psutil_windows", PsutilMethods); -#endif - if (module == NULL) - INITERROR; - - if (psutil_setup() != 0) - INITERROR; - if (psutil_set_se_debug() != 0) - INITERROR; - - st = GETSTATE(module); - st->error = PyErr_NewException("_psutil_windows.Error", NULL, NULL); - if (st->error == NULL) { - Py_DECREF(module); - INITERROR; - } - - // Exceptions. - TimeoutExpired = PyErr_NewException( - "_psutil_windows.TimeoutExpired", NULL, NULL); - Py_INCREF(TimeoutExpired); - PyModule_AddObject(module, "TimeoutExpired", TimeoutExpired); - - TimeoutAbandoned = PyErr_NewException( - "_psutil_windows.TimeoutAbandoned", NULL, NULL); - Py_INCREF(TimeoutAbandoned); - PyModule_AddObject(module, "TimeoutAbandoned", TimeoutAbandoned); - - // version constant - PyModule_AddIntConstant(module, "version", PSUTIL_VERSION); - - // process status constants - // http://msdn.microsoft.com/en-us/library/ms683211(v=vs.85).aspx - PyModule_AddIntConstant( - module, "ABOVE_NORMAL_PRIORITY_CLASS", ABOVE_NORMAL_PRIORITY_CLASS); - PyModule_AddIntConstant( - module, "BELOW_NORMAL_PRIORITY_CLASS", BELOW_NORMAL_PRIORITY_CLASS); - PyModule_AddIntConstant( - module, "HIGH_PRIORITY_CLASS", HIGH_PRIORITY_CLASS); - PyModule_AddIntConstant( - module, "IDLE_PRIORITY_CLASS", IDLE_PRIORITY_CLASS); - PyModule_AddIntConstant( - module, "NORMAL_PRIORITY_CLASS", NORMAL_PRIORITY_CLASS); - PyModule_AddIntConstant( - module, "REALTIME_PRIORITY_CLASS", REALTIME_PRIORITY_CLASS); - - // connection status constants - // http://msdn.microsoft.com/en-us/library/cc669305.aspx - PyModule_AddIntConstant( - module, "MIB_TCP_STATE_CLOSED", MIB_TCP_STATE_CLOSED); - PyModule_AddIntConstant( - module, "MIB_TCP_STATE_CLOSING", MIB_TCP_STATE_CLOSING); - PyModule_AddIntConstant( - module, "MIB_TCP_STATE_CLOSE_WAIT", MIB_TCP_STATE_CLOSE_WAIT); - PyModule_AddIntConstant( - module, "MIB_TCP_STATE_LISTEN", MIB_TCP_STATE_LISTEN); - PyModule_AddIntConstant( - module, "MIB_TCP_STATE_ESTAB", MIB_TCP_STATE_ESTAB); - PyModule_AddIntConstant( - module, "MIB_TCP_STATE_SYN_SENT", MIB_TCP_STATE_SYN_SENT); - PyModule_AddIntConstant( - module, "MIB_TCP_STATE_SYN_RCVD", MIB_TCP_STATE_SYN_RCVD); - PyModule_AddIntConstant( - module, "MIB_TCP_STATE_FIN_WAIT1", MIB_TCP_STATE_FIN_WAIT1); - PyModule_AddIntConstant( - module, "MIB_TCP_STATE_FIN_WAIT2", MIB_TCP_STATE_FIN_WAIT2); - PyModule_AddIntConstant( - module, "MIB_TCP_STATE_LAST_ACK", MIB_TCP_STATE_LAST_ACK); - PyModule_AddIntConstant( - module, "MIB_TCP_STATE_TIME_WAIT", MIB_TCP_STATE_TIME_WAIT); - PyModule_AddIntConstant( - module, "MIB_TCP_STATE_TIME_WAIT", MIB_TCP_STATE_TIME_WAIT); - PyModule_AddIntConstant( - module, "MIB_TCP_STATE_DELETE_TCB", MIB_TCP_STATE_DELETE_TCB); - PyModule_AddIntConstant( - module, "PSUTIL_CONN_NONE", PSUTIL_CONN_NONE); - - // service status constants - /* - PyModule_AddIntConstant( - module, "SERVICE_CONTINUE_PENDING", SERVICE_CONTINUE_PENDING); - PyModule_AddIntConstant( - module, "SERVICE_PAUSE_PENDING", SERVICE_PAUSE_PENDING); - PyModule_AddIntConstant( - module, "SERVICE_PAUSED", SERVICE_PAUSED); - PyModule_AddIntConstant( - module, "SERVICE_RUNNING", SERVICE_RUNNING); - PyModule_AddIntConstant( - module, "SERVICE_START_PENDING", SERVICE_START_PENDING); - PyModule_AddIntConstant( - module, "SERVICE_STOP_PENDING", SERVICE_STOP_PENDING); - PyModule_AddIntConstant( - module, "SERVICE_STOPPED", SERVICE_STOPPED); - */ - - // ...for internal use in _psutil_windows.py - PyModule_AddIntConstant( - module, "INFINITE", INFINITE); - PyModule_AddIntConstant( - module, "ERROR_ACCESS_DENIED", ERROR_ACCESS_DENIED); - PyModule_AddIntConstant( - 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( - module, "WINDOWS_VISTA", PSUTIL_WINDOWS_VISTA); - PyModule_AddIntConstant( - module, "WINDOWS_7", PSUTIL_WINDOWS_7); - PyModule_AddIntConstant( - module, "WINDOWS_8", PSUTIL_WINDOWS_8); - PyModule_AddIntConstant( - module, "WINDOWS_8_1", PSUTIL_WINDOWS_8_1); - PyModule_AddIntConstant( - module, "WINDOWS_10", PSUTIL_WINDOWS_10); - -#if PY_MAJOR_VERSION >= 3 - return module; -#endif -} |