Skip to content

Commit

Permalink
fix #924: [OSX] Process.exe() for PID 0 erroneously raise ZombieProce…
Browse files Browse the repository at this point in the history
…ss. Also test all process methods with PID 0
  • Loading branch information
giampaolo committed Oct 17, 2016
1 parent 67eb992 commit 5887f56
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 33 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Bug tracker at https://github.com/giampaolo/psutil/issues
- #921: psutil.Popen now defines a __del__ special method which calls the
original one, hopefully helping the gc to free resources.
- #923: [OSX] free memory is wrong (does not match vm_stat command).
- #924: [OSX] Process.exe() for PID 0 erroneously raise ZombieProcess.


4.3.1 - 2016-09-01
Expand Down
5 changes: 4 additions & 1 deletion psutil/_psutil_osx.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ psutil_proc_exe(PyObject *self, PyObject *args) {
errno = 0;
ret = proc_pidpath((pid_t)pid, &buf, sizeof(buf));
if (ret == 0) {
psutil_raise_for_pid(pid, "proc_pidpath() syscall failed");
if (pid == 0)
AccessDenied();
else
psutil_raise_for_pid(pid, "proc_pidpath() syscall failed");
return NULL;
}
#if PY_MAJOR_VERSION >= 3
Expand Down
48 changes: 16 additions & 32 deletions psutil/tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -1402,46 +1402,30 @@ def succeed_or_zombie_p_exc(fun, *args, **kwargs):

def test_pid_0(self):
# Process(0) is supposed to work on all platforms except Linux
if 0 not in psutil.pids() and not OPENBSD:
if 0 not in psutil.pids():
self.assertRaises(psutil.NoSuchProcess, psutil.Process, 0)
return

# test all methods
p = psutil.Process(0)
self.assertTrue(p.name())

if POSIX:
for name in psutil._as_dict_attrnames:
if name == 'pid':
continue
meth = getattr(p, name)
try:
self.assertEqual(p.uids().real, 0)
self.assertEqual(p.gids().real, 0)
ret = meth()
except psutil.AccessDenied:
pass

self.assertRaisesRegex(
ValueError, "preventing sending signal to process with PID 0",
p.send_signal, signal.SIGTERM)

self.assertIn(p.ppid(), (0, 1))
# self.assertEqual(p.exe(), "")
p.cmdline()
try:
p.num_threads()
except psutil.AccessDenied:
pass

try:
p.memory_info()
except psutil.AccessDenied:
pass

try:
if POSIX:
self.assertEqual(p.username(), 'root')
elif WINDOWS:
self.assertEqual(p.username(), 'NT AUTHORITY\\SYSTEM')
else:
p.username()
except psutil.AccessDenied:
pass
if name in ("uids", "gids"):
self.assertEqual(ret.real, 0)
elif name == "username":
if POSIX:
self.assertEqual(p.username(), 'root')
elif WINDOWS:
self.assertEqual(p.username(), 'NT AUTHORITY\\SYSTEM')
elif name == "name":
assert name, name

p.as_dict()

Expand Down

0 comments on commit 5887f56

Please sign in to comment.