From e5d400fc2bc8f9ce9d9ebfdd73b7cfe23ad25f46 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Sun, 1 Oct 2023 13:29:26 -0700 Subject: [PATCH] give PROCESS_QUERY_LIMITED_INFO access for GetExitCodeProcess --- psutil/arch/windows/proc.c | 11 ++++++----- psutil/tests/__init__.py | 30 ++++++++++++++---------------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/psutil/arch/windows/proc.c b/psutil/arch/windows/proc.c index 28ef8b6f47..af3df267ac 100644 --- a/psutil/arch/windows/proc.c +++ b/psutil/arch/windows/proc.c @@ -99,14 +99,14 @@ PyObject * psutil_proc_kill(PyObject *self, PyObject *args) { HANDLE hProcess; DWORD pid; + DWORD access = PROCESS_TERMINATE | PROCESS_QUERY_LIMITED_INFORMATION; if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) return NULL; if (pid == 0) return AccessDenied("automatically set for PID 0"); - hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pid); - hProcess = psutil_check_phandle(hProcess, pid, 0); + hProcess = psutil_handle_from_pid(pid, access); if (hProcess == NULL) { return NULL; } @@ -540,12 +540,13 @@ psutil_proc_suspend_or_resume(PyObject *self, PyObject *args) { DWORD pid; NTSTATUS status; HANDLE hProcess; + DWORD access = PROCESS_SUSPEND_RESUME | PROCESS_QUERY_LIMITED_INFORMATION; PyObject* suspend; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID "O", &pid, &suspend)) - return NULL; + if (! PyArg_ParseTuple(args, _Py_PARSE_PID "O", &pid, &suspend)) + return NULL; - hProcess = psutil_handle_from_pid(pid, PROCESS_SUSPEND_RESUME); + hProcess = psutil_handle_from_pid(pid, access); if (hProcess == NULL) return NULL; diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py index 4c683c8d10..1aba0418fa 100644 --- a/psutil/tests/__init__.py +++ b/psutil/tests/__init__.py @@ -971,22 +971,20 @@ def assertPidGone(self, pid): self.assertNotIn(pid, [x.pid for x in psutil.process_iter()]) def assertProcessGone(self, proc): - with self.assertRaises(psutil.NoSuchProcess): - proc.kill() - # self.assertPidGone(proc.pid) - # ns = process_namespace(proc) - # for fun, name in ns.iter(ns.all, clear_cache=True): - # with self.subTest(proc=proc, name=name): - # try: - # ret = fun() - # except psutil.ZombieProcess: - # raise - # except psutil.NoSuchProcess as exc: - # self._check_proc_exc(proc, exc) - # else: - # msg = "Process.%s() didn't raise NSP and returned %r" % ( - # name, ret) - # raise AssertionError(msg) + self.assertPidGone(proc.pid) + ns = process_namespace(proc) + for fun, name in ns.iter(ns.all, clear_cache=True): + with self.subTest(proc=proc, name=name): + try: + ret = fun() + except psutil.ZombieProcess: + raise + except psutil.NoSuchProcess as exc: + self._check_proc_exc(proc, exc) + else: + msg = "Process.%s() didn't raise NSP and returned %r" % ( + name, ret) + raise AssertionError(msg) proc.wait(timeout=0) # assert not raise TimeoutExpired def assertProcessZombie(self, proc):