From fee80e90dc7357edcadbc27c03b80558a6dd391f Mon Sep 17 00:00:00 2001 From: Guido Trotter <ultrotter@google.com> Date: Wed, 23 Jul 2008 14:23:18 +0000 Subject: [PATCH] Add utils.IsPidFileAlive function This helper function reads a pid from a file containing it and checks whether it refers to a live process. Reviewed-by: iustinp --- lib/utils.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/utils.py b/lib/utils.py index 7548a7e15..05240eabb 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -290,6 +290,31 @@ def IsProcessAlive(pid): return alive +def IsPidFileAlive(pidfile): + """Check whether the given pidfile points to a live process. + + @param pidfile: Path to a file containing the pid to be checked + @type pidfile: string (filename) + + """ + try: + pf = open(pidfile, 'r') + except EnvironmentError, open_err: + if open_err.errno == errno.ENOENT: + return False + else: + raise errors.GenericError("Cannot open file %s. Error: %s" % + (pidfile, str(open_err))) + + try: + pid = int(pf.read()) + except ValueError: + raise errors.GenericError("Invalid pid string in %s" % + (pidfile,)) + + return IsProcessAlive(pid) + + def MatchNameComponent(key, name_list): """Try to match a name against a list. -- GitLab