Commit 44bf25ff authored by Iustin Pop's avatar Iustin Pop
Browse files

Remove zombie special case from IsProcessAlive

Based on the discussion on the mailing list, we remove the special
casing of zombie processes.

Note we don't use kill -0 since that has a different meaning than "check
process is alive", so this function is still linux-specific.

Reviewed-by: imsnah
parent ff5251bc
......@@ -352,8 +352,8 @@ def CheckDict(target, template, logname=None):
def IsProcessAlive(pid):
"""Check if a given pid exists on the system.
@note: zombie processes treated as not alive, and giving a
pid M{<= 0} causes the function to return False.
@note: zombie status is not handled, so zombie processes
will be returned as alive
@type pid: int
@param pid: the process ID to check
@rtype: boolean
......@@ -364,22 +364,12 @@ def IsProcessAlive(pid):
return False
f = open("/proc/%d/status" % pid)
except IOError, err:
os.stat("/proc/%d/status" % pid)
return True
except EnvironmentError, err:
if err.errno in (errno.ENOENT, errno.ENOTDIR):
return False
alive = True
data = f.readlines()
if len(data) > 1:
state = data[1].split()
if len(state) > 1 and state[1] == "Z":
alive = False
return alive
def ReadPidFile(pidfile):
......@@ -50,37 +50,11 @@ from ganeti.errors import LockError, UnitParseError, GenericError, \
class TestIsProcessAlive(unittest.TestCase):
"""Testing case for IsProcessAlive"""
def _CreateZombie(self):
# create a zombie
r_fd, w_fd = os.pipe()
pid_zombie = os.fork()
if pid_zombie == 0:
# explicit close of read, write end will be closed only due to exit
elif pid_zombie < 0:
raise SystemError("can't fork")
# parent: we close our end of the w_fd, so reads will error as
# soon as the OS cleans the child's filedescriptors on its exit
# wait for 60 seconds at max for the exit (pathological case, just
# so that the test doesn't hang indefinitely)
r_set, w_set, e_set =[r_fd], [], [], 60)
if not r_set and not e_set:"Timeout exceeded in zombie creation")
return pid_zombie
def testExists(self):
mypid = os.getpid()
"can't find myself running")
def testZombie(self):
pid_zombie = self._CreateZombie()
is_zombie = not IsProcessAlive(pid_zombie)
self.assert_(is_zombie, "zombie not detected as zombie")
os.waitpid(pid_zombie, os.WNOHANG)
def testNotExisting(self):
pid_non_existing = os.fork()
if pid_non_existing == 0:
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment