cros_vm: Improve robustness if VM is killed.

Check that VM process is actually running, don't just believe the pid
file.

BUG=chromium:591624
TEST=manual

Change-Id: I113804e4a4c315f72126a7fd3e5dddc14dfe78b1
Reviewed-on: https://chromium-review.googlesource.com/395588
Commit-Ready: Achuith Bhandarkar <achuith@chromium.org>
Tested-by: Achuith Bhandarkar <achuith@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
diff --git a/scripts/cros_vm.py b/scripts/cros_vm.py
index b32486b..2f1bc4a 100644
--- a/scripts/cros_vm.py
+++ b/scripts/cros_vm.py
@@ -108,6 +108,7 @@
 
     self.Stop()
 
+    logging.debug('Start VM')
     if not self.kvm_path:
       self.kvm_path = self._FindKVMBinary()
     logging.debug('kvm path=%s', self.kvm_path)
@@ -154,7 +155,7 @@
                                         redirect_stdout=True).output.rstrip()
     if not pid.isdigit():
       raise VMError('%s in %s is not a pid.' % (pid, self.pidfile))
-    return pid
+    return int(pid)
 
   def IsRunning(self):
     """Returns True if there's a running VM.
@@ -166,18 +167,27 @@
       pid = self._GetVMPid()
     except VMError:
       return False
-    return bool(pid)
+
+    if not pid:
+      return False
+
+    # Make sure the process actually exists.
+    res = cros_build_lib.SudoRunCommand(['kill', '-0', str(pid)],
+                                        error_code_ok=True)
+    return res.returncode == 0
 
   def Stop(self):
     """Stop the VM."""
+    logging.debug('Stop VM')
 
     pid = self._GetVMPid()
     if not pid:
       return
 
-    logging.info('Killing %s.', pid)
+    logging.info('Killing %d.', pid)
     if not self.dry_run:
-      cros_build_lib.SudoRunCommand(['kill', '-9', pid])
+      cros_build_lib.SudoRunCommand(['kill', '-9', str(pid)],
+                                    error_code_ok=True)
     self._CleanupFiles(recreate=False)
 
   def WaitForBoot(self, timeout=10, poll_interval=0.1):
@@ -209,7 +219,6 @@
 
     Args:
       cmd: command to run, of list type.
-      verbose: verbose logging output.
     """
     if not isinstance(cmd, list):
       raise VMError('cmd must be a list.')