summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2020-01-25 22:34:07 +0000
committerGiampaolo Rodola <g.rodola@gmail.com>2020-01-25 22:34:07 +0000
commitb42943a17217698c2a1f340e0207141ab6f5e162 (patch)
tree0047586f5bca5c8a1c98b602f06131db930056af
parentb178279e908476fa8501008d9cef20c8f362dad1 (diff)
downloadpsutil-b42943a17217698c2a1f340e0207141ab6f5e162.tar.gz
write utility fun to convert kvm_openfiles() err
-rw-r--r--psutil/_psutil_bsd.c12
-rw-r--r--psutil/arch/openbsd/specific.c34
2 files changed, 24 insertions, 22 deletions
diff --git a/psutil/_psutil_bsd.c b/psutil/_psutil_bsd.c
index f58cc72f..812dc509 100644
--- a/psutil/_psutil_bsd.c
+++ b/psutil/_psutil_bsd.c
@@ -125,18 +125,8 @@ psutil_pids(PyObject *self, PyObject *args) {
if (py_retlist == NULL)
return NULL;
- // TODO: RuntimeError is inappropriate here; we could return the
- // original error instead.
- if (psutil_get_proc_list(&proclist, &num_processes) != 0) {
- if (errno != 0) {
- PyErr_SetFromErrno(PyExc_OSError);
- }
- else {
- PyErr_SetString(PyExc_RuntimeError,
- "failed to retrieve process list");
- }
+ if (psutil_get_proc_list(&proclist, &num_processes) != 0)
goto error;
- }
if (num_processes > 0) {
orig_address = proclist; // save so we can free it after we're done
diff --git a/psutil/arch/openbsd/specific.c b/psutil/arch/openbsd/specific.c
index f8d3cf96..be635a11 100644
--- a/psutil/arch/openbsd/specific.c
+++ b/psutil/arch/openbsd/specific.c
@@ -46,6 +46,21 @@
// Utility functions
// ============================================================================
+
+void
+convert_kvm_err(const char *syscall, char *errbuf) {
+ char fullmsg[8192];
+
+ sprintf(fullmsg, "(originated from %s: %s)", syscall, errbuf);
+ if (strstr(errbuf, "Permission denied") != NULL)
+ AccessDenied(fullmsg);
+ else if (strstr(errbuf, "Operation not permitted") != NULL)
+ AccessDenied(fullmsg);
+ else
+ PyErr_Format(PyExc_RuntimeError, fullmsg);
+}
+
+
int
psutil_kinfo_proc(pid_t pid, struct kinfo_proc *proc) {
// Fills a kinfo_proc struct based on process pid.
@@ -133,16 +148,16 @@ psutil_get_proc_list(struct kinfo_proc **procList, size_t *procCount) {
assert(procCount != NULL);
kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
-
- if (kd == NULL) {
- return errno;
+ if (! kd) {
+ convert_kvm_err("kvm_openfiles", errbuf);
+ return 1;
}
result = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &cnt);
if (result == NULL) {
+ PyErr_Format(PyExc_RuntimeError, "kvm_getprocs syscall failed");
kvm_close(kd);
- err(1, NULL);
- return errno;
+ return 1;
}
*procCount = (size_t)cnt;
@@ -150,9 +165,9 @@ psutil_get_proc_list(struct kinfo_proc **procList, size_t *procCount) {
size_t mlen = cnt * sizeof(struct kinfo_proc);
if ((*procList = malloc(mlen)) == NULL) {
+ PyErr_NoMemory();
kvm_close(kd);
- err(1, NULL);
- return errno;
+ return 1;
}
memcpy(*procList, result, mlen);
@@ -241,10 +256,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("kvm_openfiles");
- else
- PyErr_Format(PyExc_RuntimeError, "kvm_openfiles() syscall failed");
+ convert_kvm_err("kvm_openfiles()", errbuf);
goto error;
}