Skip to content

Commit

Permalink
fix psutil_raise_ad_or_nsp() so that it raises an exception which mak…
Browse files Browse the repository at this point in the history
…es more sense
  • Loading branch information
giampaolo committed Oct 4, 2016
1 parent 0c286c7 commit b940247
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 16 deletions.
3 changes: 2 additions & 1 deletion psutil/_psutil_bsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
26 changes: 19 additions & 7 deletions psutil/_psutil_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion psutil/_psutil_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 6 additions & 3 deletions psutil/arch/bsd/freebsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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++) {
Expand Down
3 changes: 2 additions & 1 deletion psutil/arch/bsd/freebsd_socks.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion psutil/arch/bsd/netbsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions psutil/arch/bsd/openbsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit b940247

Please sign in to comment.