From 89ddf9911973711e8a565101939d3a2a5f3a4b37 Mon Sep 17 00:00:00 2001 From: Sudheer Shanka Date: Tue, 25 Sep 2018 14:22:07 -0700 Subject: [PATCH] Add unmountTree to utils. Bug: 111890351 Test: builds without any errors Change-Id: I62a94c9e8d101756b686b402774f08a1d71cf875 --- Utils.cpp | 30 ++++++++++++++++++++++++++++++ Utils.h | 2 ++ VolumeManager.cpp | 25 +------------------------ 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/Utils.cpp b/Utils.cpp index 9773cc0..cdca85e 100644 --- a/Utils.cpp +++ b/Utils.cpp @@ -31,6 +31,8 @@ #include #include #include +#include +#include #include #include #include @@ -38,6 +40,7 @@ #include #include #include +#include #include #ifndef UMOUNT_NOFOLLOW @@ -758,5 +761,32 @@ bool IsRunningInEmulator() { 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 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 diff --git a/Utils.h b/Utils.h index 40f4d0a..48d605a 100644 --- a/Utils.h +++ b/Utils.h @@ -130,6 +130,8 @@ bool Readlinkat(int dirfd, const std::string& path, std::string* result); /* Checks if Android is running in QEMU */ bool IsRunningInEmulator(); +status_t UnmountTree(const std::string& prefix); + } // namespace vold } // namespace android diff --git a/VolumeManager.cpp b/VolumeManager.cpp index 53380af..db47e4d 100644 --- a/VolumeManager.cpp +++ b/VolumeManager.cpp @@ -411,30 +411,7 @@ int VolumeManager::setPrimary(const std::shared_ptr& } static int unmount_tree(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 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 0; + return android::vold::UnmountTree(prefix); } int VolumeManager::remountUid(uid_t uid, const std::string& mode) {