summaryrefslogtreecommitdiff
path: root/psutil/_psutil_common.c
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2017-04-30 19:05:57 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2017-04-30 19:05:57 +0200
commit89534a538f56353ece672e5dec209f29e1b5bbdf (patch)
tree6e78930e381560de8e6813dbf7dd94f60e89e282 /psutil/_psutil_common.c
parentef6727cd13e347741ed8f2c731fdb4f42768002a (diff)
downloadpsutil-89534a538f56353ece672e5dec209f29e1b5bbdf.tar.gz
move psutil_pid_exists() and psutil_raise_for_pid() from _psutil_common.c to _psutil_posix.c
Diffstat (limited to 'psutil/_psutil_common.c')
-rw-r--r--psutil/_psutil_common.c90
1 files changed, 0 insertions, 90 deletions
diff --git a/psutil/_psutil_common.c b/psutil/_psutil_common.c
index 5d025739..c8d736e8 100644
--- a/psutil/_psutil_common.c
+++ b/psutil/_psutil_common.c
@@ -6,14 +6,8 @@
* Routines common to all platforms.
*/
-#ifdef PSUTIL_POSIX
-#include <sys/types.h>
-#include <signal.h>
-#endif
-
#include <Python.h>
-
/*
* Set OSError(errno=ESRCH, strerror="No such process") Python exception.
*/
@@ -40,87 +34,3 @@ AccessDenied(void) {
Py_XDECREF(exc);
return NULL;
}
-
-
-#ifdef PSUTIL_POSIX
-/*
- * Check if PID exists. Return values:
- * 1: exists
- * 0: does not exist
- * -1: error (Python exception is set)
- */
-int
-psutil_pid_exists(long pid) {
- int ret;
-
- // No negative PID exists, plus -1 is an alias for sending signal
- // too all processes except system ones. Not what we want.
- if (pid < 0)
- return 0;
-
- // As per "man 2 kill" PID 0 is an alias for sending the signal to
- // every process in the process group of the calling process.
- // Not what we want. Some platforms have PID 0, some do not.
- // We decide that at runtime.
- if (pid == 0) {
-#if defined(PSUTIL_LINUX) || defined(PSUTIL_FREEBSD)
- return 0;
-#else
- return 1;
-#endif
- }
-
-#if defined(PSUTIL_OSX)
- ret = kill((pid_t)pid , 0);
-#else
- ret = kill(pid , 0);
-#endif
-
- if (ret == 0)
- return 1;
- else {
- if (errno == ESRCH) {
- // ESRCH == No such process
- return 0;
- }
- else if (errno == EPERM) {
- // EPERM clearly indicates there's a process to deny
- // access to.
- return 1;
- }
- else {
- // According to "man 2 kill" possible error values are
- // (EINVAL, EPERM, ESRCH) therefore we should never get
- // here. If we do let's be explicit in considering this
- // an error.
- PyErr_SetFromErrno(PyExc_OSError);
- return -1;
- }
- }
-}
-
-
-/*
- * 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_for_pid(long pid, char *msg) {
- // Set exception to AccessDenied if pid exists else NoSuchProcess.
- if (errno != 0) {
- PyErr_SetFromErrno(PyExc_OSError);
- return 0;
- }
- if (psutil_pid_exists(pid) == 0)
- NoSuchProcess();
- else
- PyErr_SetString(PyExc_RuntimeError, msg);
- return 0;
-}
-#endif