summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2017-11-24 14:07:06 +0100
committerGitHub <noreply@github.com>2017-11-24 14:07:06 +0100
commit34fb3666859bda349afdc9b4397b0b7a715f08de (patch)
tree74ff50536fd2275efc945b4329482acc938d45e1
parent71c4f5683460e110693c976b21b06ab4034ae8a3 (diff)
downloadpsutil-34fb3666859bda349afdc9b4397b0b7a715f08de.tar.gz
Arguments for NoSuchProcess and AccessDenied for the C ext (#1180)
* change NoSuchProcess and AccessDenied C exceptions signatures * fix arg call on win
-rw-r--r--psutil/_psutil_aix.c2
-rw-r--r--psutil/_psutil_common.c14
-rw-r--r--psutil/_psutil_common.h4
-rw-r--r--psutil/_psutil_osx.c12
-rw-r--r--psutil/_psutil_posix.c2
-rw-r--r--psutil/_psutil_sunos.c2
-rw-r--r--psutil/_psutil_windows.c28
-rw-r--r--psutil/arch/freebsd/specific.c8
-rw-r--r--psutil/arch/netbsd/specific.c8
-rw-r--r--psutil/arch/openbsd/specific.c6
-rw-r--r--psutil/arch/osx/process_info.c6
-rw-r--r--psutil/arch/windows/process_info.c6
12 files changed, 50 insertions, 48 deletions
diff --git a/psutil/_psutil_aix.c b/psutil/_psutil_aix.c
index 4d522ba2..916254d5 100644
--- a/psutil/_psutil_aix.c
+++ b/psutil/_psutil_aix.c
@@ -341,7 +341,7 @@ psutil_proc_num_ctx_switches(PyObject *self, PyObject *args) {
/* finished iteration without finding requested pid */
free(processes);
- return NoSuchProcess();
+ return NoSuchProcess("");
}
diff --git a/psutil/_psutil_common.c b/psutil/_psutil_common.c
index e9fce85e..908dbf14 100644
--- a/psutil/_psutil_common.c
+++ b/psutil/_psutil_common.c
@@ -37,12 +37,13 @@ PyUnicode_DecodeFSDefaultAndSize(char *s, Py_ssize_t size) {
/*
* Set OSError(errno=ESRCH, strerror="No such process") Python exception.
+ * If msg != "" the exception message will change in accordance.
*/
PyObject *
-NoSuchProcess(void) {
+NoSuchProcess(char *msg) {
PyObject *exc;
- char *msg = strerror(ESRCH);
- exc = PyObject_CallFunction(PyExc_OSError, "(is)", ESRCH, msg);
+ exc = PyObject_CallFunction(
+ PyExc_OSError, "(is)", ESRCH, strlen(msg) ? msg : strerror(ESRCH));
PyErr_SetObject(PyExc_OSError, exc);
Py_XDECREF(exc);
return NULL;
@@ -51,12 +52,13 @@ NoSuchProcess(void) {
/*
* Set OSError(errno=EACCES, strerror="Permission denied") Python exception.
+ * If msg != "" the exception message will change in accordance.
*/
PyObject *
-AccessDenied(void) {
+AccessDenied(char *msg) {
PyObject *exc;
- char *msg = strerror(EACCES);
- exc = PyObject_CallFunction(PyExc_OSError, "(is)", EACCES, msg);
+ exc = PyObject_CallFunction(
+ PyExc_OSError, "(is)", EACCES, strlen(msg) ? msg : strerror(EACCES));
PyErr_SetObject(PyExc_OSError, exc);
Py_XDECREF(exc);
return NULL;
diff --git a/psutil/_psutil_common.h b/psutil/_psutil_common.h
index 965966af..3db3f5ed 100644
--- a/psutil/_psutil_common.h
+++ b/psutil/_psutil_common.h
@@ -17,8 +17,8 @@ PyObject* PyUnicode_DecodeFSDefault(char *s);
PyObject* PyUnicode_DecodeFSDefaultAndSize(char *s, Py_ssize_t size);
#endif
-PyObject* AccessDenied(void);
-PyObject* NoSuchProcess(void);
+PyObject* AccessDenied(char *msg);
+PyObject* NoSuchProcess(char *msg);
PyObject* psutil_set_testing(PyObject *self, PyObject *args);
void psutil_debug(const char* format, ...);
diff --git a/psutil/_psutil_osx.c b/psutil/_psutil_osx.c
index 6c520e5d..fef61ca8 100644
--- a/psutil/_psutil_osx.c
+++ b/psutil/_psutil_osx.c
@@ -266,7 +266,7 @@ psutil_proc_exe(PyObject *self, PyObject *args) {
ret = proc_pidpath((pid_t)pid, &buf, sizeof(buf));
if (ret == 0) {
if (pid == 0)
- AccessDenied();
+ AccessDenied("");
else
psutil_raise_for_pid(pid, "proc_pidpath()");
return NULL;
@@ -571,9 +571,9 @@ psutil_proc_memory_uss(PyObject *self, PyObject *args) {
err = task_for_pid(mach_task_self(), (pid_t)pid, &task);
if (err != KERN_SUCCESS) {
if (psutil_pid_exists(pid) == 0)
- NoSuchProcess();
+ NoSuchProcess("");
else
- AccessDenied();
+ AccessDenied("");
return NULL;
}
@@ -1018,9 +1018,9 @@ psutil_proc_threads(PyObject *self, PyObject *args) {
err = task_for_pid(mach_task_self(), (pid_t)pid, &task);
if (err != KERN_SUCCESS) {
if (psutil_pid_exists(pid) == 0)
- NoSuchProcess();
+ NoSuchProcess("");
else
- AccessDenied();
+ AccessDenied("");
goto error;
}
@@ -1030,7 +1030,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) {
if (err != KERN_SUCCESS) {
// errcode 4 is "invalid argument" (access denied)
if (err == 4) {
- AccessDenied();
+ AccessDenied("");
}
else {
// otherwise throw a runtime error with appropriate error code
diff --git a/psutil/_psutil_posix.c b/psutil/_psutil_posix.c
index 76cf2db0..cc827273 100644
--- a/psutil/_psutil_posix.c
+++ b/psutil/_psutil_posix.c
@@ -122,7 +122,7 @@ psutil_raise_for_pid(long pid, char *syscall_name) {
else if (psutil_pid_exists(pid) == 0) {
psutil_debug("%s syscall failed and PID %i no longer exists; "
"assume NoSuchProcess", syscall_name, pid);
- NoSuchProcess();
+ NoSuchProcess("");
}
else {
PyErr_Format(PyExc_RuntimeError, "%s syscall failed", syscall_name);
diff --git a/psutil/_psutil_sunos.c b/psutil/_psutil_sunos.c
index 15508461..c6673642 100644
--- a/psutil/_psutil_sunos.c
+++ b/psutil/_psutil_sunos.c
@@ -185,7 +185,7 @@ psutil_proc_environ(PyObject *self, PyObject *args) {
goto error;
if (! info.pr_envp) {
- AccessDenied();
+ AccessDenied("");
goto error;
}
diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c
index 1f757385..c840a2b7 100644
--- a/psutil/_psutil_windows.c
+++ b/psutil/_psutil_windows.c
@@ -335,7 +335,7 @@ psutil_proc_kill(PyObject *self, PyObject *args) {
if (! PyArg_ParseTuple(args, "l", &pid))
return NULL;
if (pid == 0)
- return AccessDenied();
+ return AccessDenied("");
hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
if (hProcess == NULL) {
@@ -343,7 +343,7 @@ psutil_proc_kill(PyObject *self, PyObject *args) {
// see https://github.com/giampaolo/psutil/issues/24
psutil_debug("OpenProcess -> ERROR_INVALID_PARAMETER turned "
"into NoSuchProcess");
- NoSuchProcess();
+ NoSuchProcess("");
}
else {
PyErr_SetFromWindowsErr(0);
@@ -381,7 +381,7 @@ psutil_proc_wait(PyObject *self, PyObject *args) {
if (! PyArg_ParseTuple(args, "ll", &pid, &timeout))
return NULL;
if (pid == 0)
- return AccessDenied();
+ return AccessDenied("");
hProcess = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION,
FALSE, pid);
@@ -444,7 +444,7 @@ psutil_proc_cpu_times(PyObject *self, PyObject *args) {
if (GetLastError() == ERROR_ACCESS_DENIED) {
// usually means the process has died so we throw a NoSuchProcess
// here
- return NoSuchProcess();
+ return NoSuchProcess("");
}
else {
return PyErr_SetFromWindowsErr(0);
@@ -498,7 +498,7 @@ psutil_proc_create_time(PyObject *self, PyObject *args) {
if (GetLastError() == ERROR_ACCESS_DENIED) {
// usually means the process has died so we throw a
// NoSuchProcess here
- return NoSuchProcess();
+ return NoSuchProcess("");
}
else {
return PyErr_SetFromWindowsErr(0);
@@ -517,7 +517,7 @@ psutil_proc_create_time(PyObject *self, PyObject *args) {
CloseHandle(hProcess);
if (ret != 0) {
if (exitCode != STILL_ACTIVE)
- return NoSuchProcess();
+ return NoSuchProcess("");
}
else {
// Ignore access denied as it means the process is still alive.
@@ -631,7 +631,7 @@ psutil_proc_cmdline(PyObject *self, PyObject *args) {
pid_return = psutil_pid_is_running(pid);
if (pid_return == 0)
- return NoSuchProcess();
+ return NoSuchProcess("");
if (pid_return == -1)
return NULL;
@@ -654,7 +654,7 @@ psutil_proc_environ(PyObject *self, PyObject *args) {
pid_return = psutil_pid_is_running(pid);
if (pid_return == 0)
- return NoSuchProcess();
+ return NoSuchProcess("");
if (pid_return == -1)
return NULL;
@@ -719,7 +719,7 @@ psutil_proc_name(PyObject *self, PyObject *args) {
}
CloseHandle(hSnapShot);
- NoSuchProcess();
+ NoSuchProcess("");
return NULL;
}
@@ -1050,7 +1050,7 @@ psutil_proc_cwd(PyObject *self, PyObject *args) {
pid_return = psutil_pid_is_running(pid);
if (pid_return == 0)
- return NoSuchProcess();
+ return NoSuchProcess("");
if (pid_return == -1)
return NULL;
@@ -1069,7 +1069,7 @@ psutil_proc_suspend_or_resume(DWORD pid, int suspend) {
THREADENTRY32 te32 = {0};
if (pid == 0) {
- AccessDenied();
+ AccessDenied("");
return FALSE;
}
@@ -1171,13 +1171,13 @@ psutil_proc_threads(PyObject *self, PyObject *args) {
if (pid == 0) {
// raise AD instead of returning 0 as procexp is able to
// retrieve useful information somehow
- AccessDenied();
+ AccessDenied("");
goto error;
}
pid_return = psutil_pid_is_running(pid);
if (pid_return == 0) {
- NoSuchProcess();
+ NoSuchProcess("");
goto error;
}
if (pid_return == -1)
@@ -1568,7 +1568,7 @@ psutil_net_connections(PyObject *self, PyObject *args) {
pid_return = psutil_pid_is_running(pid);
if (pid_return == 0) {
_psutil_conn_decref_objs();
- return NoSuchProcess();
+ return NoSuchProcess("");
}
else if (pid_return == -1) {
_psutil_conn_decref_objs();
diff --git a/psutil/arch/freebsd/specific.c b/psutil/arch/freebsd/specific.c
index ff128e65..2c8944dd 100644
--- a/psutil/arch/freebsd/specific.c
+++ b/psutil/arch/freebsd/specific.c
@@ -59,7 +59,7 @@ psutil_kinfo_proc(const pid_t pid, struct kinfo_proc *proc) {
// sysctl stores 0 in the size if we can't find the process information.
if (size == 0) {
- NoSuchProcess();
+ NoSuchProcess("");
return -1;
}
return 0;
@@ -297,7 +297,7 @@ psutil_proc_exe(PyObject *self, PyObject *args) {
if (ret == -1)
return NULL;
else if (ret == 0)
- return NoSuchProcess();
+ return NoSuchProcess("");
else
strcpy(pathname, "");
}
@@ -354,7 +354,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) {
goto error;
}
if (size == 0) {
- NoSuchProcess();
+ NoSuchProcess("");
goto error;
}
@@ -370,7 +370,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) {
goto error;
}
if (size == 0) {
- NoSuchProcess();
+ NoSuchProcess("");
goto error;
}
diff --git a/psutil/arch/netbsd/specific.c b/psutil/arch/netbsd/specific.c
index 0a32139d..cab60d60 100644
--- a/psutil/arch/netbsd/specific.c
+++ b/psutil/arch/netbsd/specific.c
@@ -72,7 +72,7 @@ psutil_kinfo_proc(pid_t pid, kinfo_proc *proc) {
}
// sysctl stores 0 in the size if we can't find the process information.
if (size == 0) {
- NoSuchProcess();
+ NoSuchProcess("");
return -1;
}
return 0;
@@ -157,7 +157,7 @@ psutil_proc_exe(PyObject *self, PyObject *args) {
if (ret == -1)
return NULL;
else if (ret == 0)
- return NoSuchProcess();
+ return NoSuchProcess("");
else
strcpy(pathname, "");
}
@@ -209,7 +209,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) {
goto error;
}
if (size == 0) {
- NoSuchProcess();
+ NoSuchProcess("");
goto error;
}
@@ -226,7 +226,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) {
goto error;
}
if (size == 0) {
- NoSuchProcess();
+ NoSuchProcess("");
goto error;
}
diff --git a/psutil/arch/openbsd/specific.c b/psutil/arch/openbsd/specific.c
index 2a0d30ce..33ebdeec 100644
--- a/psutil/arch/openbsd/specific.c
+++ b/psutil/arch/openbsd/specific.c
@@ -67,7 +67,7 @@ psutil_kinfo_proc(pid_t pid, struct kinfo_proc *proc) {
}
// sysctl stores 0 in the size if we can't find the process information.
if (size == 0) {
- NoSuchProcess();
+ NoSuchProcess("");
return -1;
}
return 0;
@@ -242,7 +242,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) {
kd = kvm_openfiles(0, 0, 0, O_RDONLY, errbuf);
if (! kd) {
if (strstr(errbuf, "Permission denied") != NULL)
- AccessDenied();
+ AccessDenied("");
else
PyErr_Format(PyExc_RuntimeError, "kvm_openfiles() syscall failed");
goto error;
@@ -253,7 +253,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) {
sizeof(*kp), &nentries);
if (! kp) {
if (strstr(errbuf, "Permission denied") != NULL)
- AccessDenied();
+ AccessDenied("");
else
PyErr_Format(PyExc_RuntimeError, "kvm_getprocs() syscall failed");
goto error;
diff --git a/psutil/arch/osx/process_info.c b/psutil/arch/osx/process_info.c
index f0a01132..40c79a2c 100644
--- a/psutil/arch/osx/process_info.c
+++ b/psutil/arch/osx/process_info.c
@@ -144,7 +144,7 @@ psutil_get_cmdline(long pid) {
// In case of zombie process we'll get EINVAL. We translate it
// to NSP and _psosx.py will translate it to ZP.
if ((errno == EINVAL) && (psutil_pid_exists(pid)))
- NoSuchProcess();
+ NoSuchProcess("");
else
PyErr_SetFromErrno(PyExc_OSError);
goto error;
@@ -238,7 +238,7 @@ psutil_get_environ(long pid) {
// In case of zombie process we'll get EINVAL. We translate it
// to NSP and _psosx.py will translate it to ZP.
if ((errno == EINVAL) && (psutil_pid_exists(pid)))
- NoSuchProcess();
+ NoSuchProcess("");
else
PyErr_SetFromErrno(PyExc_OSError);
goto error;
@@ -338,7 +338,7 @@ psutil_get_kinfo_proc(long pid, struct kinfo_proc *kp) {
// sysctl succeeds but len is zero, happens when process has gone away
if (len == 0) {
- NoSuchProcess();
+ NoSuchProcess("");
return -1;
}
return 0;
diff --git a/psutil/arch/windows/process_info.c b/psutil/arch/windows/process_info.c
index 9a54d854..ffd3c80e 100644
--- a/psutil/arch/windows/process_info.c
+++ b/psutil/arch/windows/process_info.c
@@ -256,7 +256,7 @@ psutil_check_phandle(HANDLE hProcess, DWORD pid) {
if (ret == 1)
return hProcess;
else if (ret == 0)
- return NoSuchProcess();
+ return NoSuchProcess("");
else if (ret == -1)
return PyErr_SetFromWindowsErr(0);
else // -2
@@ -277,7 +277,7 @@ psutil_handle_from_pid_waccess(DWORD pid, DWORD dwDesiredAccess) {
if (pid == 0) {
// otherwise we'd get NoSuchProcess
- return AccessDenied();
+ return AccessDenied("");
}
hProcess = OpenProcess(dwDesiredAccess, FALSE, pid);
@@ -959,7 +959,7 @@ psutil_get_proc_info(DWORD pid, PSYSTEM_PROCESS_INFORMATION *retProcess,
}
} while ( (process = PSUTIL_NEXT_PROCESS(process)) );
- NoSuchProcess();
+ NoSuchProcess("");
goto error;
error: