Unmount old postinstall mountpoint from previous runs.

When update_engine crashes, is killed or a developer runs
"stop update_engine" while we are waiting for postinstall to finish
the other partition will continue to be mounted (read-only) on the
/postinsall path. This will prevent to mount the new postinstall
step on top of it due to different SELinux labels when mounted and
unmounted. After failing to run postinstall due to the failed mount
operation we would cleanup the mountpoint, so this situation fixes
itself after one failed update attempt, which can then be resumed
from the very end.

This patch attempts to unmount /postinstall if a filesystem is mounted
there at the time we need to use the mountpoint.

Bug: 36391471
Test: Added unittests.
Change-Id: Idffd7a9319715bfb4ab6a9994c6757d27028d40a
diff --git a/common/utils.cc b/common/utils.cc
index 1cb37e5..ea748c1 100644
--- a/common/utils.cc
+++ b/common/utils.cc
@@ -703,6 +703,32 @@
   return true;
 }
 
+bool IsMountpoint(const std::string& mountpoint) {
+  struct stat stdir, stparent;
+
+  // Check whether the passed mountpoint is a directory and the /.. is in the
+  // same device or not. If mountpoint/.. is in a different device it means that
+  // there is a filesystem mounted there. If it is not, but they both point to
+  // the same inode it basically is the special case of /.. pointing to /. This
+  // test doesn't play well with bind mount but that's out of the scope of what
+  // we want to detect here.
+  if (lstat(mountpoint.c_str(), &stdir) != 0) {
+    PLOG(ERROR) << "Error stat'ing " << mountpoint;
+    return false;
+  }
+  if (!S_ISDIR(stdir.st_mode))
+    return false;
+
+  base::FilePath parent(mountpoint);
+  parent = parent.Append("..");
+  if (lstat(parent.value().c_str(), &stparent) != 0) {
+    PLOG(ERROR) << "Error stat'ing " << parent.value();
+    return false;
+  }
+  return S_ISDIR(stparent.st_mode) &&
+         (stparent.st_dev != stdir.st_dev || stparent.st_ino == stdir.st_ino);
+}
+
 // Tries to parse the header of an ELF file to obtain a human-readable
 // description of it on the |output| string.
 static bool GetFileFormatELF(const uint8_t* buffer, size_t size,