From e3e66f020bcd7f182a42fe5961a9e3e258ad8f21 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Fri, 9 May 2008 10:12:08 +0000
Subject: [PATCH] =?UTF-8?q?Remove=20utils.CheckDaemonAlive=20and=20use=20?=
 =?UTF-8?q?=E2=80=9Cxm=20info=E2=80=9D=20instead?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

There are a couple of reasons for doing so:
- /proc is not OS independent, it's only supported by Linux (there are
  emulations on other systems, but those might differ from the way
  Linux represents data).
- Checking a daemon's state doesn't necessarily mean it's usable.
  Connecting to the socket using β€œxm info” is much safer.
- Reduce code size.

Reviewed-by: iustinp
---
 lib/hypervisor/hv_xen.py |  6 ++---
 lib/utils.py             | 53 ----------------------------------------
 2 files changed, 3 insertions(+), 56 deletions(-)

diff --git a/lib/hypervisor/hv_xen.py b/lib/hypervisor/hv_xen.py
index 51e10f694..d5a1176eb 100644
--- a/lib/hypervisor/hv_xen.py
+++ b/lib/hypervisor/hv_xen.py
@@ -214,15 +214,15 @@ class XenHypervisor(hv_base.BaseHypervisor):
     """
     raise NotImplementedError
 
-
   def Verify(self):
     """Verify the hypervisor.
 
     For Xen, this verifies that the xend process is running.
 
     """
-    if not utils.CheckDaemonAlive('/var/run/xend.pid', 'xend'):
-      return "xend daemon is not running"
+    result = utils.RunCmd(["xm", "info"])
+    if result.failed:
+      return "'xm info' failed: %s" % result.fail_reason
 
   @staticmethod
   def _GetConfigFileDiskData(disk_template, block_devices):
diff --git a/lib/utils.py b/lib/utils.py
index 72a2622d2..7aa75e105 100644
--- a/lib/utils.py
+++ b/lib/utils.py
@@ -569,59 +569,6 @@ def NiceSort(name_list):
   return [tup[1] for tup in to_sort]
 
 
-def CheckDaemonAlive(pid_file, process_string):
-  """Check wether the specified daemon is alive.
-
-  Args:
-   - pid_file: file to read the daemon pid from, the file is
-               expected to contain only a single line containing
-               only the PID
-   - process_string: a substring that we expect to find in
-                     the command line of the daemon process
-
-  Returns:
-   - True if the daemon is judged to be alive (that is:
-      - the PID file exists, is readable and contains a number
-      - a process of the specified PID is running
-      - that process contains the specified string in its
-        command line
-      - the process is not in state Z (zombie))
-   - False otherwise
-
-  """
-  try:
-    pid_file = file(pid_file, 'r')
-    try:
-      pid = int(pid_file.readline())
-    finally:
-      pid_file.close()
-
-    cmdline_file_path = "/proc/%s/cmdline" % (pid)
-    cmdline_file = open(cmdline_file_path, 'r')
-    try:
-      cmdline = cmdline_file.readline()
-    finally:
-      cmdline_file.close()
-
-    if not process_string in cmdline:
-      return False
-
-    stat_file_path =  "/proc/%s/stat" % (pid)
-    stat_file = open(stat_file_path, 'r')
-    try:
-      process_state = stat_file.readline().split()[2]
-    finally:
-      stat_file.close()
-
-    if process_state == 'Z':
-      return False
-
-  except (IndexError, IOError, ValueError):
-    return False
-
-  return True
-
-
 def TryConvert(fn, val):
   """Try to convert a value ignoring errors.
 
-- 
GitLab