Add unmountTree to utils.

Bug: 111890351
Test: builds without any errors
Change-Id: I62a94c9e8d101756b686b402774f08a1d71cf875
diff --git a/Utils.cpp b/Utils.cpp
index 9773cc0..cdca85e 100644
--- a/Utils.cpp
+++ b/Utils.cpp
@@ -31,6 +31,8 @@
 #include <dirent.h>
 #include <fcntl.h>
 #include <linux/fs.h>
+#include <mntent.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <sys/mount.h>
 #include <sys/stat.h>
@@ -38,6 +40,7 @@
 #include <sys/sysmacros.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <list>
 #include <mutex>
 
 #ifndef UMOUNT_NOFOLLOW
@@ -758,5 +761,32 @@
     return android::base::GetBoolProperty("ro.kernel.qemu", false);
 }
 
+status_t UnmountTree(const std::string& prefix) {
+    FILE* fp = setmntent("/proc/mounts", "r");
+    if (fp == NULL) {
+        PLOG(ERROR) << "Failed to open /proc/mounts";
+        return -errno;
+    }
+
+    // Some volumes can be stacked on each other, so force unmount in
+    // reverse order to give us the best chance of success.
+    std::list<std::string> toUnmount;
+    mntent* mentry;
+    while ((mentry = getmntent(fp)) != NULL) {
+        auto test = std::string(mentry->mnt_dir) + "/";
+        if (android::base::StartsWith(test, prefix)) {
+            toUnmount.push_front(test);
+        }
+    }
+    endmntent(fp);
+
+    for (const auto& path : toUnmount) {
+        if (umount2(path.c_str(), MNT_DETACH)) {
+            PLOG(ERROR) << "Failed to unmount " << path;
+        }
+    }
+    return OK;
+}
+
 }  // namespace vold
 }  // namespace android