summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2019-11-20 00:46:35 -0800
committerGiampaolo Rodola <g.rodola@gmail.com>2019-11-20 00:46:35 -0800
commit72c84cb4edb5c0968a83c1f45ad5cc51235e0af3 (patch)
tree2dd87b484b08d3d1037b01a102b14a073d1b60a6
parent1f8d432db12a907544ac533b66a5a61ba25321fb (diff)
downloadpsutil-72c84cb4edb5c0968a83c1f45ad5cc51235e0af3.tar.gz
#fix #1595 / windows: kill() may not raise AccessDenied
-rw-r--r--HISTORY.rst1
-rw-r--r--psutil/_psutil_windows.c30
-rwxr-xr-xscripts/internal/download_exes.py2
3 files changed, 25 insertions, 8 deletions
diff --git a/HISTORY.rst b/HISTORY.rst
index 5aaf9da1..045bb5d5 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -7,6 +7,7 @@ XXXX-XX-XX
**Bug fixes**
+- 1595_: [Windows] Process.kill() may not throw AccessDenied.
- 1616_: use of Py_DECREF instead of Py_CLEAR will result in double free and
segfault (CVE). (patch by Riccardo Schirone)
- 1619_: [OpenBSD] compilation fails due to C syntax error. (patch by Nathan
diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c
index 08b208dc..e01fa50d 100644
--- a/psutil/_psutil_windows.c
+++ b/psutil/_psutil_windows.c
@@ -243,8 +243,8 @@ error:
static PyObject *
psutil_proc_kill(PyObject *self, PyObject *args) {
HANDLE hProcess;
- DWORD err;
long pid;
+ DWORD exitCode;
if (! PyArg_ParseTuple(args, "l", &pid))
return NULL;
@@ -265,19 +265,35 @@ psutil_proc_kill(PyObject *self, PyObject *args) {
return NULL;
}
- // kill the process
if (! TerminateProcess(hProcess, SIGTERM)) {
- err = GetLastError();
- // See: https://github.com/giampaolo/psutil/issues/1099
- if (err != ERROR_ACCESS_DENIED) {
- PyErr_SetFromOSErrnoWithSyscall("TerminateProcess");
+ if (GetLastError() == ERROR_ACCESS_DENIED) {
+ // ERROR_ACCESS_DENIED (winerror 5) may happen if the
+ // process already died. See:
+ // https://github.com/giampaolo/psutil/issues/1099
+ // https://github.com/giampaolo/psutil/issues/1595
+ if (GetExitCodeProcess(hProcess, &exitCode) == 0) {
+ PyErr_SetFromOSErrnoWithSyscall("GetExitCodeProcess");
+ goto error;
+ }
+ if (exitCode == STILL_ACTIVE) {
+ PyErr_SetFromOSErrnoWithSyscall("TerminateProcess");
+ goto error;
+ }
CloseHandle(hProcess);
- return NULL;
+ Py_RETURN_NONE;
+ }
+ else {
+ PyErr_SetFromOSErrnoWithSyscall("TerminateProcess");
+ goto error;
}
}
CloseHandle(hProcess);
Py_RETURN_NONE;
+
+error:
+ CloseHandle(hProcess);
+ return NULL;
}
diff --git a/scripts/internal/download_exes.py b/scripts/internal/download_exes.py
index 1b72a177..4a559bb0 100755
--- a/scripts/internal/download_exes.py
+++ b/scripts/internal/download_exes.py
@@ -26,7 +26,7 @@ from scriptutils import printerr, exit
BASE_URL = 'https://ci.appveyor.com/api'
-PY_VERSIONS = ['2.7', '3.5', '3.6', '3.7']
+PY_VERSIONS = ['2.7', '3.5', '3.6', '3.7', '3.8']
TIMEOUT = 30
COLORS = True