From d4becc3f156bdc7565a64061f1c5711eb9f6791a Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Thu, 12 Nov 2015 06:09:28 +0100 Subject: last fixes --- psutil/_psbsd.py | 5 ++--- psutil/_psutil_openbsd.c | 53 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/psutil/_psbsd.py b/psutil/_psbsd.py index 8b980b2d..57d847b9 100644 --- a/psutil/_psbsd.py +++ b/psutil/_psbsd.py @@ -111,7 +111,7 @@ def virtual_memory(): def swap_memory(): """System swap memory as (total, used, free, sin, sout) namedtuple.""" - if sys.platform.startswith("openbsd"): + if OPENBSD: PAGESIZE = 1 total, used, free, sin, sout = [x * PAGESIZE for x in cext.swap_mem()] percent = usage_percent(used, total, _round=1) @@ -305,8 +305,7 @@ def wrap_exceptions(fun): ZombieProcess is None): raise if err.errno == errno.ESRCH: - # if not pid_exists(self.pid): - if self.pid not in pids(): + if not pid_exists(self.pid): raise NoSuchProcess(self.pid, self._name) else: raise ZombieProcess(self.pid, self._name, self._ppid) diff --git a/psutil/_psutil_openbsd.c b/psutil/_psutil_openbsd.c index 8406d083..9c979306 100644 --- a/psutil/_psutil_openbsd.c +++ b/psutil/_psutil_openbsd.c @@ -32,7 +32,6 @@ #include #include #include - #include // for struct xsocket #include #include @@ -86,8 +85,6 @@ #include #undef _KERNEL #include // for CPUSTATES & CP_* - #include - #include #endif @@ -134,6 +131,24 @@ psutil_kinfo_proc(const pid_t pid, struct kinfo_proc *proc) { #endif +#ifdef __FreeBSD__ +/* + * Set exception to AccessDenied if pid exists else NoSuchProcess. + */ +void +psutil_raise_ad_or_nsp(long pid) { + int ret; + ret = psutil_pid_exists(pid); + if (ret == 0) + NoSuchProcess(); + else if (ret == 1) + AccessDenied(); + else + return NULL; +} +#endif + + /* * Return a Python list of all the PIDs running on the system. */ @@ -641,10 +656,13 @@ psutil_proc_io_counters(PyObject *self, PyObject *args) { } +#ifdef __OpenBSD__ +#define ptoa(x) ((paddr_t)(x) << PAGE_SHIFT) +#endif + /* * Return extended memory info for a process as a Python tuple. */ -#define ptoa(x) ((paddr_t)(x) << PAGE_SHIFT) static PyObject * psutil_proc_memory_info(PyObject *self, PyObject *args) { long pid; @@ -1724,6 +1742,7 @@ psutil_users(PyObject *self, PyObject *args) { if (py_retlist == NULL) return NULL; +#if __FreeBSD_version < 900000 || __OpenBSD__ struct utmp ut; FILE *fp; @@ -1754,7 +1773,33 @@ psutil_users(PyObject *self, PyObject *args) { } fclose(fp); +#else + struct utmpx *utx; + + while ((utx = getutxent()) != NULL) { + if (utx->ut_type != USER_PROCESS) + continue; + py_tuple = Py_BuildValue( + "(sssf)", + utx->ut_user, // username + utx->ut_line, // tty + utx->ut_host, // hostname + (float)utx->ut_tv.tv_sec // start time + ); + + if (!py_tuple) { + endutxent(); + goto error; + } + if (PyList_Append(py_retlist, py_tuple)) { + endutxent(); + goto error; + } + Py_DECREF(py_tuple); + } + endutxent(); +#endif return py_retlist; error: -- cgit v1.2.1