diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2020-01-14 03:09:25 -0800 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2020-01-14 03:09:25 -0800 |
commit | 5140642a7ac95889222cbcc2a8118ca803f0ae3d (patch) | |
tree | 5b67381b9fa501f0a135234418b8ed0344cdb462 | |
parent | bab3a310ab561bfdd01d44b1d2121daac7469e8f (diff) | |
download | psutil-5140642a7ac95889222cbcc2a8118ca803f0ae3d.tar.gz |
print/set syscall origin when raising NSP or AD
-rw-r--r-- | psutil/_psutil_aix.c | 2 | ||||
-rw-r--r-- | psutil/_psutil_common.c | 24 | ||||
-rw-r--r-- | psutil/_psutil_osx.c | 8 | ||||
-rw-r--r-- | psutil/_psutil_posix.c | 2 | ||||
-rw-r--r-- | psutil/_psutil_sunos.c | 2 | ||||
-rw-r--r-- | psutil/_psutil_windows.c | 28 | ||||
-rw-r--r-- | psutil/arch/freebsd/specific.c | 8 | ||||
-rw-r--r-- | psutil/arch/netbsd/specific.c | 10 | ||||
-rw-r--r-- | psutil/arch/openbsd/specific.c | 6 | ||||
-rw-r--r-- | psutil/arch/osx/process_info.c | 6 | ||||
-rw-r--r-- | psutil/arch/windows/process_info.c | 2 | ||||
-rw-r--r-- | psutil/arch/windows/process_utils.c | 4 | ||||
-rw-r--r-- | psutil/arch/windows/socks.c | 2 |
13 files changed, 54 insertions, 50 deletions
diff --git a/psutil/_psutil_aix.c b/psutil/_psutil_aix.c index d7fcaefc..b8584f26 100644 --- a/psutil/_psutil_aix.c +++ b/psutil/_psutil_aix.c @@ -461,7 +461,7 @@ psutil_proc_num_ctx_switches(PyObject *self, PyObject *args) { /* finished iteration without finding requested pid */ free(processes); - return NoSuchProcess(""); + return NoSuchProcess("psutil_read_process_table (no PID found)"); } diff --git a/psutil/_psutil_common.c b/psutil/_psutil_common.c index 1b83f750..9075fda3 100644 --- a/psutil/_psutil_common.c +++ b/psutil/_psutil_common.c @@ -69,14 +69,16 @@ PyErr_SetFromOSErrnoWithSyscall(const char *syscall) { // ==================================================================== /* - * Set OSError(errno=ESRCH, strerror="No such process") Python exception. - * If msg != "" the exception message will change in accordance. + * Set OSError(errno=ESRCH, strerror="No such process (originated from") + * Python exception. */ PyObject * -NoSuchProcess(const char *msg) { +NoSuchProcess(const char *syscall) { PyObject *exc; - exc = PyObject_CallFunction( - PyExc_OSError, "(is)", ESRCH, strlen(msg) ? msg : strerror(ESRCH)); + char msg[1024]; + + sprintf(msg, "No such process (originated from %s)", syscall); + exc = PyObject_CallFunction(PyExc_OSError, "(is)", ESRCH, msg); PyErr_SetObject(PyExc_OSError, exc); Py_XDECREF(exc); return NULL; @@ -84,14 +86,16 @@ NoSuchProcess(const char *msg) { /* - * Set OSError(errno=EACCES, strerror="Permission denied") Python exception. - * If msg != "" the exception message will change in accordance. + * Set OSError(errno=EACCES, strerror="Permission denied" (originated from ...) + * Python exception. */ PyObject * -AccessDenied(const char *msg) { +AccessDenied(const char *syscall) { PyObject *exc; - exc = PyObject_CallFunction( - PyExc_OSError, "(is)", EACCES, strlen(msg) ? msg : strerror(EACCES)); + char msg[1024]; + + sprintf(msg, "Access denied (originated from %s)", syscall); + exc = PyObject_CallFunction(PyExc_OSError, "(is)", EACCES, msg); PyErr_SetObject(PyExc_OSError, exc); Py_XDECREF(exc); return NULL; diff --git a/psutil/_psutil_osx.c b/psutil/_psutil_osx.c index 76ec0ee8..8d086122 100644 --- a/psutil/_psutil_osx.c +++ b/psutil/_psutil_osx.c @@ -96,7 +96,7 @@ psutil_task_for_pid(long pid, mach_port_t *task) err = task_for_pid(mach_task_self(), (pid_t)pid, task); if (err != KERN_SUCCESS) { if (psutil_pid_exists(pid) == 0) - NoSuchProcess("task_for_pid() failed"); + NoSuchProcess("task_for_pid"); else if (psutil_is_zombie(pid) == 1) PyErr_SetString(ZombieProcessError, "task_for_pid() failed"); else { @@ -104,7 +104,7 @@ psutil_task_for_pid(long pid, mach_port_t *task) "task_for_pid() failed (pid=%ld, err=%i, errno=%i, msg='%s'); " "setting AccessDenied()", pid, err, errno, mach_error_string(err)); - AccessDenied("task_for_pid() failed"); + AccessDenied("task_for_pid"); } return 1; } @@ -298,7 +298,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("automatically set for PID 0"); else psutil_raise_for_pid(pid, "proc_pidpath()"); return NULL; @@ -894,7 +894,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) { if (err != KERN_SUCCESS) { // errcode 4 is "invalid argument" (access denied) if (err == 4) { - AccessDenied(""); + AccessDenied("task_info"); } else { // otherwise throw a runtime error with appropriate error code diff --git a/psutil/_psutil_posix.c b/psutil/_psutil_posix.c index aa600849..7fbdcc48 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("psutil_pid_exists"); } else { PyErr_Format(PyExc_RuntimeError, "%s syscall failed", syscall_name); diff --git a/psutil/_psutil_sunos.c b/psutil/_psutil_sunos.c index bcfb448f..8aa7eadd 100644 --- a/psutil/_psutil_sunos.c +++ b/psutil/_psutil_sunos.c @@ -275,7 +275,7 @@ psutil_proc_environ(PyObject *self, PyObject *args) { goto error; if (! info.pr_envp) { - AccessDenied(""); + AccessDenied("/proc/pid/psinfo struct not set"); goto error; } diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index d11dbdc7..d19e32e9 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -174,7 +174,7 @@ psutil_proc_kill(PyObject *self, PyObject *args) { if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; if (pid == 0) - return AccessDenied(""); + return AccessDenied("automatically set for PID 0"); hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pid); if (hProcess == NULL) { @@ -182,7 +182,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("OpenProcess"); } else { PyErr_SetFromWindowsErr(0); @@ -218,7 +218,7 @@ psutil_proc_wait(PyObject *self, PyObject *args) { if (! PyArg_ParseTuple(args, "ll", &pid, &timeout)) return NULL; if (pid == 0) - return AccessDenied(""); + return AccessDenied("automatically set for PID 0"); hProcess = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION, FALSE, pid); @@ -296,7 +296,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 - NoSuchProcess(""); + NoSuchProcess("GetProcessTimes"); } else { PyErr_SetFromWindowsErr(0); @@ -351,7 +351,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 - NoSuchProcess(""); + NoSuchProcess("GetProcessTimes"); } else { PyErr_SetFromWindowsErr(0); @@ -372,7 +372,7 @@ psutil_proc_create_time(PyObject *self, PyObject *args) { CloseHandle(hProcess); if (ret != 0) { if (exitCode != STILL_ACTIVE) - return NoSuchProcess(""); + return NoSuchProcess("GetExitCodeProcess"); } else { // Ignore access denied as it means the process is still alive. @@ -412,7 +412,7 @@ psutil_proc_cmdline(PyObject *self, PyObject *args, PyObject *kwdict) { pid_return = psutil_pid_is_running(pid); if (pid_return == 0) - return NoSuchProcess(""); + return NoSuchProcess("psutil_pid_is_running"); if (pid_return == -1) return NULL; @@ -436,7 +436,7 @@ psutil_proc_environ(PyObject *self, PyObject *args) { pid_return = psutil_pid_is_running(pid); if (pid_return == 0) - return NoSuchProcess(""); + return NoSuchProcess("psutil_pid_is_running"); if (pid_return == -1) return NULL; @@ -507,7 +507,7 @@ psutil_proc_name(PyObject *self, PyObject *args) { } CloseHandle(hSnapShot); - NoSuchProcess(""); + NoSuchProcess("CreateToolhelp32Snapshot loop (no PID found)"); return NULL; } @@ -605,10 +605,10 @@ psutil_GetProcWsetInformation( if (!NT_SUCCESS(status)) { if (status == STATUS_ACCESS_DENIED) { - AccessDenied("originated from NtQueryVirtualMemory"); + AccessDenied("NtQueryVirtualMemory"); } else if (psutil_pid_is_running(pid) == 0) { - NoSuchProcess(""); + NoSuchProcess("psutil_pid_is_running"); } else { PyErr_Clear(); @@ -711,7 +711,7 @@ psutil_proc_cwd(PyObject *self, PyObject *args) { pid_return = psutil_pid_is_running(pid); if (pid_return == 0) - return NoSuchProcess(""); + return NoSuchProcess("psutil_pid_is_running"); if (pid_return == -1) return NULL; @@ -770,13 +770,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("automatically set for PID 0"); goto error; } pid_return = psutil_pid_is_running(pid); if (pid_return == 0) { - NoSuchProcess(""); + NoSuchProcess("psutil_pid_is_running"); goto error; } if (pid_return == -1) diff --git a/psutil/arch/freebsd/specific.c b/psutil/arch/freebsd/specific.c index bc267acd..90ea81e8 100644 --- a/psutil/arch/freebsd/specific.c +++ b/psutil/arch/freebsd/specific.c @@ -61,7 +61,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("sysctl (size = 0)"); return -1; } return 0; @@ -301,7 +301,7 @@ psutil_proc_exe(PyObject *self, PyObject *args) { if (ret == -1) return NULL; else if (ret == 0) - return NoSuchProcess(""); + return NoSuchProcess("psutil_pid_exists"); else strcpy(pathname, ""); } @@ -358,7 +358,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) { goto error; } if (size == 0) { - NoSuchProcess(""); + NoSuchProcess("sysctl (size = 0)"); goto error; } @@ -374,7 +374,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) { goto error; } if (size == 0) { - NoSuchProcess(""); + NoSuchProcess("sysctl (size = 0)"); goto error; } diff --git a/psutil/arch/netbsd/specific.c b/psutil/arch/netbsd/specific.c index 681b2377..62e09325 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("sysctl (size = 0)"); return -1; } return 0; @@ -139,7 +139,7 @@ psutil_proc_cwd(PyObject *self, PyObject *args) { free(buf); if (len == -1) { if (errno == ENOENT) - NoSuchProcess(""); + NoSuchProcess("readlink (ENOENT)"); else PyErr_SetFromErrno(PyExc_OSError); return NULL; @@ -195,7 +195,7 @@ psutil_proc_exe(PyObject *self, PyObject *args) { if (ret == -1) return NULL; else if (ret == 0) - return NoSuchProcess(""); + return NoSuchProcess("psutil_pid_exists"); else strcpy(pathname, ""); } @@ -247,7 +247,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) { goto error; } if (size == 0) { - NoSuchProcess(""); + NoSuchProcess("sysctl (size = 0)"); goto error; } @@ -264,7 +264,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) { goto error; } if (size == 0) { - NoSuchProcess(""); + NoSuchProcess("sysctl (size = 0)"); goto error; } diff --git a/psutil/arch/openbsd/specific.c b/psutil/arch/openbsd/specific.c index 33ebdeec..f8d3cf96 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("sysctl (size = 0)"); 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("kvm_openfiles"); 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("kvm_getprocs"); 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 9877fd6b..47cf864f 100644 --- a/psutil/arch/osx/process_info.c +++ b/psutil/arch/osx/process_info.c @@ -167,7 +167,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("sysctl"); else PyErr_SetFromErrno(PyExc_OSError); goto error; @@ -259,7 +259,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("sysctl"); else PyErr_SetFromErrno(PyExc_OSError); goto error; @@ -359,7 +359,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("sysctl (len == 0)"); return -1; } return 0; diff --git a/psutil/arch/windows/process_info.c b/psutil/arch/windows/process_info.c index 1c5a3c33..64ff0f0d 100644 --- a/psutil/arch/windows/process_info.c +++ b/psutil/arch/windows/process_info.c @@ -636,7 +636,7 @@ psutil_get_proc_info(DWORD pid, PSYSTEM_PROCESS_INFORMATION *retProcess, } } while ((process = PSUTIL_NEXT_PROCESS(process))); - NoSuchProcess(""); + NoSuchProcess("NtQuerySystemInformation (no PID found)"); goto error; error: diff --git a/psutil/arch/windows/process_utils.c b/psutil/arch/windows/process_utils.c index fd516bea..f6867ca1 100644 --- a/psutil/arch/windows/process_utils.c +++ b/psutil/arch/windows/process_utils.c @@ -142,7 +142,7 @@ psutil_check_phandle(HANDLE hProcess, DWORD pid) { return hProcess; } else if (ret == 0) { - return NoSuchProcess(""); + return NoSuchProcess("psutil_is_phandle_running"); } else if (ret == -1) { if (GetLastError() == ERROR_ACCESS_DENIED) @@ -169,7 +169,7 @@ psutil_handle_from_pid(DWORD pid, DWORD access) { if (pid == 0) { // otherwise we'd get NoSuchProcess - return AccessDenied(""); + return AccessDenied("automatically set for PID 0"); } // needed for GetExitCodeProcess access |= PROCESS_QUERY_LIMITED_INFORMATION; diff --git a/psutil/arch/windows/socks.c b/psutil/arch/windows/socks.c index b316ddb8..5272e127 100644 --- a/psutil/arch/windows/socks.c +++ b/psutil/arch/windows/socks.c @@ -151,7 +151,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("psutil_pid_is_running"); } else if (pid_return == -1) { psutil_conn_decref_objs(); |