summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2017-12-03 15:56:00 +0100
committerGiampaolo Rodola <g.rodola@gmail.com>2017-12-03 15:56:00 +0100
commita4361cddd71a60c37ba00d6fca1f6e11e1217558 (patch)
tree7fcd6eb2a0980e4004964aae3f4d5431d9156b2d
parentf2324dd3068c9a7ce9ed37ab5d69e07f9732f5f6 (diff)
downloadpsutil-a4361cddd71a60c37ba00d6fca1f6e11e1217558.tar.gz
work around pid 0 on osx
-rw-r--r--psutil/_psosx.py26
-rw-r--r--psutil/_psutil_osx.c1
-rwxr-xr-xpsutil/tests/test_process.py2
3 files changed, 25 insertions, 4 deletions
diff --git a/psutil/_psosx.py b/psutil/_psosx.py
index 9fa7716d..64b289fa 100644
--- a/psutil/_psosx.py
+++ b/psutil/_psosx.py
@@ -18,6 +18,7 @@ from . import _psutil_posix as cext_posix
from ._common import AF_INET6
from ._common import conn_tmap
from ._common import isfile_strict
+from ._common import memoize
from ._common import memoize_when_activated
from ._common import parse_environ_block
from ._common import sockfam_to_enum
@@ -301,7 +302,30 @@ def users():
# =====================================================================
-pids = cext.pids
+@memoize
+def _add_pid_0(pids):
+ # On certain OSX versions pids() C implementation does not return
+ # PID 0 but "ps" does and the process is querable via sysctl().
+ if 0 in pids:
+ return False
+ else:
+ try:
+ Process(0).create_time()
+ except NoSuchProcess:
+ return False
+ except AccessDenied:
+ return True
+ else:
+ return True
+
+
+def pids():
+ ls = cext.pids()
+ if _add_pid_0(ls):
+ ls.append(0)
+ return ls
+
+
pid_exists = _psposix.pid_exists
diff --git a/psutil/_psutil_osx.c b/psutil/_psutil_osx.c
index b388bfd6..55dd64ca 100644
--- a/psutil/_psutil_osx.c
+++ b/psutil/_psutil_osx.c
@@ -101,7 +101,6 @@ psutil_pids(PyObject *self, PyObject *args) {
// save the address of proclist so we can free it later
orig_address = proclist;
for (idx = 0; idx < num_processes; idx++) {
- printf("%i\n", proclist->kp_proc.p_pid);
py_pid = Py_BuildValue("i", proclist->kp_proc.p_pid);
if (! py_pid)
goto error;
diff --git a/psutil/tests/test_process.py b/psutil/tests/test_process.py
index 8e4e89e7..1e01aea5 100755
--- a/psutil/tests/test_process.py
+++ b/psutil/tests/test_process.py
@@ -1341,8 +1341,6 @@ class TestProcess(unittest.TestCase):
def test_pid_0(self):
# Process(0) is supposed to work on all platforms except Linux
- from pprint import pprint as pp
- pp(psutil.Process(0).as_dict())
if 0 not in psutil.pids():
self.assertRaises(psutil.NoSuchProcess, psutil.Process, 0)
return