summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2016-10-04 23:08:04 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2016-10-04 23:08:04 +0200
commit9ee4e8313834eff0660894fc580c69c0f27c8348 (patch)
tree3bdb43f5d09dcf5fa74b8f9df91807ca8cd0336b
parent8d59696c3740dd348a2e3cb423fb0d6980beb75d (diff)
downloadpsutil-9ee4e8313834eff0660894fc580c69c0f27c8348.tar.gz
bsd: move pid_exists() and raise_ad_or_nsp out of bsd C modules
-rw-r--r--psutil/_psutil_bsd.c2
-rw-r--r--psutil/_psutil_common.c65
-rw-r--r--psutil/_psutil_common.h5
-rw-r--r--psutil/arch/bsd/freebsd.c45
-rw-r--r--psutil/arch/bsd/freebsd.h2
-rw-r--r--psutil/arch/bsd/freebsd_socks.c2
-rw-r--r--psutil/arch/bsd/netbsd.c36
-rw-r--r--psutil/arch/bsd/netbsd.h4
-rw-r--r--psutil/arch/bsd/openbsd.c37
9 files changed, 74 insertions, 124 deletions
diff --git a/psutil/_psutil_bsd.c b/psutil/_psutil_bsd.c
index 6b1e07d9..4e0e2d98 100644
--- a/psutil/_psutil_bsd.c
+++ b/psutil/_psutil_bsd.c
@@ -59,6 +59,8 @@
#include <netinet/in.h> // process open files/connections
#include <sys/un.h>
+#include "_psutil_common.h"
+
#ifdef __FreeBSD__
#include "arch/bsd/freebsd.h"
#include "arch/bsd/freebsd_socks.h"
diff --git a/psutil/_psutil_common.c b/psutil/_psutil_common.c
index 1c530d4d..dd22a29f 100644
--- a/psutil/_psutil_common.c
+++ b/psutil/_psutil_common.c
@@ -6,6 +6,11 @@
* Routines common to all platforms.
*/
+#ifdef PSUTIL_POSIX
+#include <sys/types.h>
+#include <signal.h>
+#endif
+
#include <Python.h>
@@ -35,3 +40,63 @@ 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 seignal to
+ // every process in the process group of the calling process.
+ // Not what we want.
+ if (pid == 0) {
+#if defined(PSUTIL_LINUX) || defined(BSD)
+ // PID 0 does not exist at leas on Linux and all BSDs.
+ return 0;
+#else
+ // On OSX it does.
+ // TODO: check Solaris.
+ return 1;
+#endif
+ }
+
+ ret = kill(pid , 0);
+ if (ret == 0)
+ return 1;
+ else {
+ if (errno == ESRCH)
+ return 0;
+ else if (errno == EPERM)
+ return 1;
+ else {
+ PyErr_SetFromErrno(PyExc_OSError);
+ return -1;
+ }
+ }
+}
+
+
+int
+psutil_raise_ad_or_nsp(long pid) {
+ // Set exception to AccessDenied if pid exists else NoSuchProcess.
+ int ret;
+ ret = psutil_pid_exists(pid);
+ if (ret == 0)
+ NoSuchProcess();
+ else if (ret == 1)
+ AccessDenied();
+ return ret;
+}
+#endif
diff --git a/psutil/_psutil_common.h b/psutil/_psutil_common.h
index 43021a72..7529c288 100644
--- a/psutil/_psutil_common.h
+++ b/psutil/_psutil_common.h
@@ -8,3 +8,8 @@
PyObject* AccessDenied(void);
PyObject* NoSuchProcess(void);
+
+#ifdef PSUTIL_POSIX
+int psutil_pid_exists(long pid);
+int psutil_raise_ad_or_nsp(long pid);
+#endif
diff --git a/psutil/arch/bsd/freebsd.c b/psutil/arch/bsd/freebsd.c
index 50be8619..4af9f07b 100644
--- a/psutil/arch/bsd/freebsd.c
+++ b/psutil/arch/bsd/freebsd.c
@@ -66,51 +66,6 @@ psutil_kinfo_proc(const pid_t pid, struct kinfo_proc *proc) {
}
-/*
- * Return 1 if PID exists in the current process list, else 0, -1
- * on error.
- * TODO: this should live in _psutil_posix.c but for some reason if I
- * move it there I get a "include undefined symbol" error.
- */
-int
-psutil_pid_exists(long pid) {
- int ret;
-
- if (pid < 0)
- return 0;
- if (pid == 0)
- return 1;
-
- ret = kill(pid , 0);
- if (ret == 0)
- return 1;
- else {
- if (errno == ESRCH)
- return 0;
- else if (errno == EPERM)
- return 1;
- else {
- PyErr_SetFromErrno(PyExc_OSError);
- return -1;
- }
- }
-}
-
-
-
-int
-psutil_raise_ad_or_nsp(long pid) {
- // Set exception to AccessDenied if pid exists else NoSuchProcess.
- int ret;
- ret = psutil_pid_exists(pid);
- if (ret == 0)
- NoSuchProcess();
- else if (ret == 1)
- AccessDenied();
- return ret;
-}
-
-
// remove spaces from string
static void psutil_remove_spaces(char *str) {
char *p1 = str;
diff --git a/psutil/arch/bsd/freebsd.h b/psutil/arch/bsd/freebsd.h
index 09a2e9f2..e15706c6 100644
--- a/psutil/arch/bsd/freebsd.h
+++ b/psutil/arch/bsd/freebsd.h
@@ -10,8 +10,6 @@ typedef struct kinfo_proc kinfo_proc;
int psutil_get_proc_list(struct kinfo_proc **procList, size_t *procCount);
int psutil_kinfo_proc(const pid_t pid, struct kinfo_proc *proc);
-int psutil_pid_exists(long pid);
-int psutil_raise_ad_or_nsp(long pid);
//
PyObject* psutil_cpu_count_phys(PyObject* self, PyObject* args);
diff --git a/psutil/arch/bsd/freebsd_socks.c b/psutil/arch/bsd/freebsd_socks.c
index e26645af..9e18216f 100644
--- a/psutil/arch/bsd/freebsd_socks.c
+++ b/psutil/arch/bsd/freebsd_socks.c
@@ -25,7 +25,7 @@
#include <net/if_media.h>
#include <libutil.h>
-#include "freebsd.h"
+#include "../../_psutil_common.h"
#define HASHSIZE 1009
diff --git a/psutil/arch/bsd/netbsd.c b/psutil/arch/bsd/netbsd.c
index 1e24d58d..160cbe7e 100644
--- a/psutil/arch/bsd/netbsd.c
+++ b/psutil/arch/bsd/netbsd.c
@@ -39,7 +39,6 @@
#include <arpa/inet.h>
-#include "netbsd.h"
#include "netbsd_socks.h"
#include "../../_psutil_common.h"
@@ -53,16 +52,6 @@
int
-psutil_raise_ad_or_nsp(long pid) {
- // Set exception to AccessDenied if pid exists else NoSuchProcess.
- if (psutil_pid_exists(pid) == 0)
- NoSuchProcess();
- else
- AccessDenied();
-}
-
-
-int
psutil_kinfo_proc(pid_t pid, kinfo_proc *proc) {
// Fills a kinfo_proc struct based on process pid.
int ret;
@@ -124,31 +113,6 @@ kinfo_getfile(pid_t pid, int* cnt) {
}
-int
-psutil_pid_exists(pid_t pid) {
- // Return 1 if PID exists in the current process list, else 0, -1
- // on error.
- // TODO: this should live in _psutil_posix.c but for some reason if I
- // move it there I get a "include undefined symbol" error.
- int ret;
- if (pid < 0)
- return 0;
- ret = kill(pid , 0);
- if (ret == 0)
- return 1;
- else {
- if (ret == ESRCH)
- return 0;
- else if (ret == EPERM)
- return 1;
- else {
- PyErr_SetFromErrno(PyExc_OSError);
- return -1;
- }
- }
-}
-
-
// XXX: This is no longer used as per
// https://github.com/giampaolo/psutil/pull/557#issuecomment-171912820
// Current implementation uses /proc instead.
diff --git a/psutil/arch/bsd/netbsd.h b/psutil/arch/bsd/netbsd.h
index 2c8edae6..96ad9f7d 100644
--- a/psutil/arch/bsd/netbsd.h
+++ b/psutil/arch/bsd/netbsd.h
@@ -13,11 +13,9 @@ int psutil_kinfo_proc(pid_t pid, kinfo_proc *proc);
struct kinfo_file * kinfo_getfile(pid_t pid, int* cnt);
int psutil_get_proc_list(kinfo_proc **procList, size_t *procCount);
char *psutil_get_cmd_args(pid_t pid, size_t *argsize);
-PyObject * psutil_get_cmdline(pid_t pid);
-int psutil_pid_exists(pid_t pid);
-int psutil_raise_ad_or_nsp(long pid);
//
+PyObject *psutil_get_cmdline(pid_t pid);
PyObject *psutil_proc_threads(PyObject *self, PyObject *args);
PyObject *psutil_virtual_mem(PyObject *self, PyObject *args);
PyObject *psutil_swap_mem(PyObject *self, PyObject *args);
diff --git a/psutil/arch/bsd/openbsd.c b/psutil/arch/bsd/openbsd.c
index 242045dc..5c459244 100644
--- a/psutil/arch/bsd/openbsd.c
+++ b/psutil/arch/bsd/openbsd.c
@@ -36,7 +36,6 @@
#include <err.h> // for warn() & err()
-#include "openbsd.h"
#include "../../_psutil_common.h"
#define PSUTIL_KPT2DOUBLE(t) (t ## _sec + t ## _usec / 1000000.0)
@@ -112,42 +111,6 @@ kinfo_getfile(long pid, int* cnt) {
}
-int
-psutil_pid_exists(long pid) {
- // Return 1 if PID exists in the current process list, else 0, -1
- // on error.
- // TODO: this should live in _psutil_posix.c but for some reason if I
- // move it there I get a "include undefined symbol" error.
- int ret;
- if (pid < 0)
- return 0;
- ret = kill(pid , 0);
- if (ret == 0)
- return 1;
- else {
- if (ret == ESRCH)
- return 0;
- else if (ret == EPERM)
- return 1;
- else {
- PyErr_SetFromErrno(PyExc_OSError);
- return -1;
- }
- }
-}
-
-
-int
-psutil_raise_ad_or_nsp(long pid) {
- // Set exception to AccessDenied if pid exists else NoSuchProcess.
- if (psutil_pid_exists(pid) == 0)
- NoSuchProcess();
- else
- AccessDenied();
- return 0;
-}
-
-
// ============================================================================
// APIS
// ============================================================================