Skip to content

Commit

Permalink
work around pid 0 on osx
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Dec 3, 2017
1 parent f2324dd commit a4361cd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
26 changes: 25 additions & 1 deletion psutil/_psosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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
Expand Down Expand Up @@ -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


Expand Down
1 change: 0 additions & 1 deletion psutil/_psutil_osx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions psutil/tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -1341,8 +1341,6 @@ def test_zombie_process_status_w_exc(self):

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
Expand Down

0 comments on commit a4361cd

Please sign in to comment.