Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arguments for NoSuchProcess and AccessDenied for the C ext #1180

Merged
merged 3 commits into from
Nov 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion psutil/_psutil_aix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
}


Expand Down
14 changes: 8 additions & 6 deletions psutil/_psutil_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions psutil/_psutil_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...);
Expand Down
12 changes: 6 additions & 6 deletions psutil/_psutil_osx.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,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;
Expand Down Expand Up @@ -559,9 +559,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;
}

Expand Down Expand Up @@ -1006,9 +1006,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;
}

Expand All @@ -1018,7 +1018,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
Expand Down
2 changes: 1 addition & 1 deletion psutil/_psutil_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion psutil/_psutil_sunos.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ psutil_proc_environ(PyObject *self, PyObject *args) {
goto error;

if (! info.pr_envp) {
AccessDenied();
AccessDenied("");
goto error;
}

Expand Down
28 changes: 14 additions & 14 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,15 +335,15 @@ 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) {
if (GetLastError() == ERROR_INVALID_PARAMETER) {
// see https://github.com/giampaolo/psutil/issues/24
psutil_debug("OpenProcess -> ERROR_INVALID_PARAMETER turned "
"into NoSuchProcess");
NoSuchProcess();
NoSuchProcess("");
}
else {
PyErr_SetFromWindowsErr(0);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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.
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -719,7 +719,7 @@ psutil_proc_name(PyObject *self, PyObject *args) {
}

CloseHandle(hSnapShot);
NoSuchProcess();
NoSuchProcess("");
return NULL;
}

Expand Down Expand Up @@ -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;

Expand All @@ -1069,7 +1069,7 @@ psutil_proc_suspend_or_resume(DWORD pid, int suspend) {
THREADENTRY32 te32 = {0};

if (pid == 0) {
AccessDenied();
AccessDenied("");
return FALSE;
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions psutil/arch/freebsd/specific.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, "");
}
Expand Down Expand Up @@ -354,7 +354,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) {
goto error;
}
if (size == 0) {
NoSuchProcess();
NoSuchProcess("");
goto error;
}

Expand All @@ -370,7 +370,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) {
goto error;
}
if (size == 0) {
NoSuchProcess();
NoSuchProcess("");
goto error;
}

Expand Down
8 changes: 4 additions & 4 deletions psutil/arch/netbsd/specific.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, "");
}
Expand Down Expand Up @@ -209,7 +209,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) {
goto error;
}
if (size == 0) {
NoSuchProcess();
NoSuchProcess("");
goto error;
}

Expand All @@ -226,7 +226,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) {
goto error;
}
if (size == 0) {
NoSuchProcess();
NoSuchProcess("");
goto error;
}

Expand Down
6 changes: 3 additions & 3 deletions psutil/arch/openbsd/specific.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions psutil/arch/osx/process_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions psutil/arch/windows/process_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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:
Expand Down