From b940247f6c85c307378bfa6b9c86d33f4b97a8d9 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 5 Oct 2016 00:14:06 +0200 Subject: [PATCH] fix psutil_raise_ad_or_nsp() so that it raises an exception which makes more sense --- psutil/_psutil_bsd.c | 3 ++- psutil/_psutil_common.c | 26 +++++++++++++++++++------- psutil/_psutil_common.h | 2 +- psutil/arch/bsd/freebsd.c | 9 ++++++--- psutil/arch/bsd/freebsd_socks.c | 3 ++- psutil/arch/bsd/netbsd.c | 3 ++- psutil/arch/bsd/openbsd.c | 6 ++++-- 7 files changed, 36 insertions(+), 16 deletions(-) diff --git a/psutil/_psutil_bsd.c b/psutil/_psutil_bsd.c index 4e0e2d98f..73499edf0 100644 --- a/psutil/_psutil_bsd.c +++ b/psutil/_psutil_bsd.c @@ -559,9 +559,10 @@ psutil_proc_open_files(PyObject *self, PyObject *args) { if (psutil_kinfo_proc(pid, &kipp) == -1) goto error; + errno = 0; freep = kinfo_getfile(pid, &cnt); if (freep == NULL) { - psutil_raise_ad_or_nsp(pid); + psutil_raise_for_pid(pid, "kinfo_getfile() failed"); goto error; } diff --git a/psutil/_psutil_common.c b/psutil/_psutil_common.c index dd22a29fc..6ad797b63 100644 --- a/psutil/_psutil_common.c +++ b/psutil/_psutil_common.c @@ -88,15 +88,27 @@ psutil_pid_exists(long pid) { } +/* + * Utility used for those syscalls which do not return a meaningful + * error that we can translate into an exception which makes sense. + * As such, we'll have to guess. + * On UNIX, if errno is set, we return that one (OSError). + * Else, if PID does not exist we assume the syscall failed because + * of that so we raise NoSuchProcess. + * If none of this is true we giveup and raise RuntimeError(msg). + * This will always set a Python exception and return NULL. + */ int -psutil_raise_ad_or_nsp(long pid) { +psutil_raise_for_pid(long pid, char *msg) { // Set exception to AccessDenied if pid exists else NoSuchProcess. - int ret; - ret = psutil_pid_exists(pid); - if (ret == 0) + if (errno != 0) { + PyErr_SetFromErrno(PyExc_OSError); + return 0; + } + if (psutil_pid_exists(pid) == 0) NoSuchProcess(); - else if (ret == 1) - AccessDenied(); - return ret; + else + PyErr_SetString(PyExc_RuntimeError, msg); + return 0; } #endif diff --git a/psutil/_psutil_common.h b/psutil/_psutil_common.h index 7529c2880..982c59c7c 100644 --- a/psutil/_psutil_common.h +++ b/psutil/_psutil_common.h @@ -11,5 +11,5 @@ PyObject* NoSuchProcess(void); #ifdef PSUTIL_POSIX int psutil_pid_exists(long pid); -int psutil_raise_ad_or_nsp(long pid); +void psutil_raise_for_pid(long pid, char *msg); #endif diff --git a/psutil/arch/bsd/freebsd.c b/psutil/arch/bsd/freebsd.c index 7d6e13d0a..54bc0df60 100644 --- a/psutil/arch/bsd/freebsd.c +++ b/psutil/arch/bsd/freebsd.c @@ -559,9 +559,10 @@ psutil_proc_cwd(PyObject *self, PyObject *args) { if (psutil_kinfo_proc(pid, &kipp) == -1) goto error; + errno = 0; freep = kinfo_getfile(pid, &cnt); if (freep == NULL) { - psutil_raise_ad_or_nsp(pid); + psutil_raise_for_pid(pid, "kinfo_getfile() failed"); goto error; } @@ -611,9 +612,10 @@ psutil_proc_num_fds(PyObject *self, PyObject *args) { if (psutil_kinfo_proc(pid, &kipp) == -1) return NULL; + errno = 0; freep = kinfo_getfile(pid, &cnt); if (freep == NULL) { - psutil_raise_ad_or_nsp(pid); + psutil_raise_for_pid(pid, "kinfo_getfile() failed"); return NULL; } free(freep); @@ -777,9 +779,10 @@ psutil_proc_memory_maps(PyObject *self, PyObject *args) { if (psutil_kinfo_proc(pid, &kp) == -1) goto error; + errno = 0; freep = kinfo_getvmmap(pid, &cnt); if (freep == NULL) { - psutil_raise_ad_or_nsp(pid); + psutil_raise_for_pid(pid, "kinfo_getvmmap() failed"); goto error; } for (i = 0; i < cnt; i++) { diff --git a/psutil/arch/bsd/freebsd_socks.c b/psutil/arch/bsd/freebsd_socks.c index 9e18216fa..826b27f77 100644 --- a/psutil/arch/bsd/freebsd_socks.c +++ b/psutil/arch/bsd/freebsd_socks.c @@ -497,9 +497,10 @@ psutil_proc_connections(PyObject *self, PyObject *args) { goto error; } + errno = 0; freep = kinfo_getfile(pid, &cnt); if (freep == NULL) { - psutil_raise_ad_or_nsp(pid); + psutil_raise_for_pid(pid, "kinfo_getfile() failed"); goto error; } diff --git a/psutil/arch/bsd/netbsd.c b/psutil/arch/bsd/netbsd.c index 160cbe7ea..852588709 100644 --- a/psutil/arch/bsd/netbsd.c +++ b/psutil/arch/bsd/netbsd.c @@ -513,9 +513,10 @@ psutil_proc_num_fds(PyObject *self, PyObject *args) { if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; + errno = 0; freep = kinfo_getfile(pid, &cnt); if (freep == NULL) { - psutil_raise_ad_or_nsp(pid); + psutil_raise_for_pid(pid, "kinfo_getfile() failed"); return NULL; } free(freep); diff --git a/psutil/arch/bsd/openbsd.c b/psutil/arch/bsd/openbsd.c index 5c459244d..af67092fe 100644 --- a/psutil/arch/bsd/openbsd.c +++ b/psutil/arch/bsd/openbsd.c @@ -404,9 +404,10 @@ psutil_proc_num_fds(PyObject *self, PyObject *args) { if (psutil_kinfo_proc(pid, &kipp) == -1) return NULL; + errno = 0; freep = kinfo_getfile(pid, &cnt); if (freep == NULL) { - psutil_raise_ad_or_nsp(pid); + psutil_raise_for_pid(pid, "kinfo_getfile() failed"); return NULL; } free(freep); @@ -505,9 +506,10 @@ psutil_proc_connections(PyObject *self, PyObject *args) { goto error; } + errno = 0; freep = kinfo_getfile(pid, &cnt); if (freep == NULL) { - psutil_raise_ad_or_nsp(pid); + psutil_raise_for_pid(pid, "kinfo_getfile() failed"); goto error; }