San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER |
| 18 | |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 19 | #include <dirent.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 20 | #include <errno.h> |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 21 | #include <fcntl.h> |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 22 | #include <mntent.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/ioctl.h> |
| 27 | #include <sys/mount.h> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 28 | #include <sys/stat.h> |
Elliott Hughes | 0e08e84 | 2017-05-18 09:08:24 -0700 | [diff] [blame] | 29 | #include <sys/sysmacros.h> |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 30 | #include <sys/types.h> |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 31 | #include <sys/wait.h> |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 32 | #include <unistd.h> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 33 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 34 | #include <linux/kdev_t.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 35 | |
Jiyong Park | 8d21c92 | 2019-01-04 13:35:25 +0900 | [diff] [blame^] | 36 | #include <ApexProperties.sysprop.h> |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 37 | #include <android-base/logging.h> |
Jeff Vander Stoep | 5889083 | 2017-10-23 17:12:31 -0700 | [diff] [blame] | 38 | #include <android-base/parseint.h> |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 39 | #include <android-base/properties.h> |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 40 | #include <android-base/stringprintf.h> |
Jeff Vander Stoep | 5889083 | 2017-10-23 17:12:31 -0700 | [diff] [blame] | 41 | #include <android-base/strings.h> |
| 42 | |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 43 | #include <cutils/fs.h> |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 44 | #include <utils/Trace.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 45 | |
Robert Craig | b9e3ba5 | 2014-02-04 10:53:00 -0500 | [diff] [blame] | 46 | #include <selinux/android.h> |
| 47 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 48 | #include <sysutils/NetlinkEvent.h> |
| 49 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 50 | #include <private/android_filesystem_config.h> |
| 51 | |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 52 | #include <fscrypt/fscrypt.h> |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 53 | |
Risan | ac02a48 | 2018-10-31 21:59:47 -0600 | [diff] [blame] | 54 | #include "AppFuseUtil.h" |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 55 | #include "Devmapper.h" |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 56 | #include "FsCrypt.h" |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 57 | #include "Loop.h" |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 58 | #include "NetlinkManager.h" |
| 59 | #include "Process.h" |
| 60 | #include "Utils.h" |
| 61 | #include "VoldUtil.h" |
| 62 | #include "VolumeManager.h" |
| 63 | #include "cryptfs.h" |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 64 | #include "fs/Ext4.h" |
| 65 | #include "fs/Vfat.h" |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 66 | #include "model/EmulatedVolume.h" |
| 67 | #include "model/ObbVolume.h" |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 68 | #include "model/StubVolume.h" |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 69 | |
Mark Salyzyn | c4405e9 | 2018-09-20 10:09:27 -0700 | [diff] [blame] | 70 | using android::base::StartsWith; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 71 | using android::base::StringPrintf; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 72 | using android::base::unique_fd; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 73 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 74 | static const char* kPathUserMount = "/mnt/user"; |
| 75 | static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk"; |
| 76 | |
| 77 | static const char* kPropVirtualDisk = "persist.sys.virtual_disk"; |
| 78 | |
| 79 | /* 512MiB is large enough for testing purposes */ |
| 80 | static const unsigned int kSizeVirtualDisk = 536870912; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 81 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 82 | static const unsigned int kMajorBlockMmc = 179; |
Yu Ning | 942d4e8 | 2016-01-08 17:36:47 +0800 | [diff] [blame] | 83 | static const unsigned int kMajorBlockExperimentalMin = 240; |
| 84 | static const unsigned int kMajorBlockExperimentalMax = 254; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 85 | |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 86 | VolumeManager* VolumeManager::sInstance = NULL; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 87 | |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 88 | VolumeManager* VolumeManager::Instance() { |
| 89 | if (!sInstance) sInstance = new VolumeManager(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 90 | return sInstance; |
| 91 | } |
| 92 | |
| 93 | VolumeManager::VolumeManager() { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 94 | mDebug = false; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 95 | mNextObbId = 0; |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 96 | mNextStubVolumeId = 0; |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 97 | // For security reasons, assume that a secure keyguard is |
| 98 | // showing until we hear otherwise |
| 99 | mSecureKeyguardShowing = true; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 102 | VolumeManager::~VolumeManager() {} |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 103 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 104 | int VolumeManager::updateVirtualDisk() { |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 105 | ATRACE_NAME("VolumeManager::updateVirtualDisk"); |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 106 | if (android::base::GetBoolProperty(kPropVirtualDisk, false)) { |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 107 | if (access(kPathVirtualDisk, F_OK) != 0) { |
| 108 | Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512); |
| 109 | } |
| 110 | |
| 111 | if (mVirtualDisk == nullptr) { |
| 112 | if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) { |
| 113 | LOG(ERROR) << "Failed to create virtual disk"; |
| 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | struct stat buf; |
| 118 | if (stat(mVirtualDiskPath.c_str(), &buf) < 0) { |
| 119 | PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath; |
| 120 | return -1; |
| 121 | } |
| 122 | |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 123 | auto disk = new android::vold::Disk( |
| 124 | "virtual", buf.st_rdev, "virtual", |
| 125 | android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd); |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 126 | mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 127 | handleDiskAdded(mVirtualDisk); |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 128 | } |
| 129 | } else { |
| 130 | if (mVirtualDisk != nullptr) { |
| 131 | dev_t device = mVirtualDisk->getDevice(); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 132 | handleDiskRemoved(device); |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 133 | |
| 134 | Loop::destroyByDevice(mVirtualDiskPath.c_str()); |
| 135 | mVirtualDisk = nullptr; |
| 136 | } |
| 137 | |
| 138 | if (access(kPathVirtualDisk, F_OK) == 0) { |
| 139 | unlink(kPathVirtualDisk); |
| 140 | } |
| 141 | } |
| 142 | return 0; |
| 143 | } |
| 144 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 145 | int VolumeManager::setDebug(bool enable) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 146 | mDebug = enable; |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 147 | return 0; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 148 | } |
| 149 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 150 | int VolumeManager::start() { |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 151 | ATRACE_NAME("VolumeManager::start"); |
| 152 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 153 | // Always start from a clean slate by unmounting everything in |
| 154 | // directories that we own, in case we crashed. |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 155 | unmountAll(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 156 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 157 | Devmapper::destroyAll(); |
| 158 | Loop::destroyAll(); |
| 159 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 160 | // Assume that we always have an emulated volume on internal |
| 161 | // storage; the framework will decide if it should be mounted. |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 162 | CHECK(mInternalEmulated == nullptr); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 163 | mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>( |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 164 | new android::vold::EmulatedVolume("/data/media")); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 165 | mInternalEmulated->create(); |
| 166 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 167 | // Consider creating a virtual disk |
| 168 | updateVirtualDisk(); |
| 169 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | int VolumeManager::stop() { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 174 | CHECK(mInternalEmulated != nullptr); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 175 | mInternalEmulated->destroy(); |
| 176 | mInternalEmulated = nullptr; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 180 | void VolumeManager::handleBlockEvent(NetlinkEvent* evt) { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 181 | std::lock_guard<std::mutex> lock(mLock); |
| 182 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 183 | if (mDebug) { |
Sudheer Shanka | 4b6ca4e | 2018-09-21 10:54:54 -0700 | [diff] [blame] | 184 | LOG(DEBUG) << "----------------"; |
| 185 | LOG(DEBUG) << "handleBlockEvent with action " << (int)evt->getAction(); |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 186 | evt->dump(); |
| 187 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 188 | |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 189 | std::string eventPath(evt->findParam("DEVPATH") ? evt->findParam("DEVPATH") : ""); |
| 190 | std::string devType(evt->findParam("DEVTYPE") ? evt->findParam("DEVTYPE") : ""); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 191 | |
| 192 | if (devType != "disk") return; |
| 193 | |
Jeff Sharkey | 95440eb | 2017-09-18 18:19:28 -0600 | [diff] [blame] | 194 | int major = std::stoi(evt->findParam("MAJOR")); |
| 195 | int minor = std::stoi(evt->findParam("MINOR")); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 196 | dev_t device = makedev(major, minor); |
| 197 | |
| 198 | switch (evt->getAction()) { |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 199 | case NetlinkEvent::Action::kAdd: { |
| 200 | for (const auto& source : mDiskSources) { |
| 201 | if (source->matches(eventPath)) { |
| 202 | // For now, assume that MMC and virtio-blk (the latter is |
| 203 | // emulator-specific; see Disk.cpp for details) devices are SD, |
| 204 | // and that everything else is USB |
| 205 | int flags = source->getFlags(); |
| 206 | if (major == kMajorBlockMmc || (android::vold::IsRunningInEmulator() && |
| 207 | major >= (int)kMajorBlockExperimentalMin && |
| 208 | major <= (int)kMajorBlockExperimentalMax)) { |
| 209 | flags |= android::vold::Disk::Flags::kSd; |
| 210 | } else { |
| 211 | flags |= android::vold::Disk::Flags::kUsb; |
| 212 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 213 | |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 214 | auto disk = |
| 215 | new android::vold::Disk(eventPath, device, source->getNickname(), flags); |
| 216 | handleDiskAdded(std::shared_ptr<android::vold::Disk>(disk)); |
| 217 | break; |
| 218 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 219 | } |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 220 | break; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 221 | } |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 222 | case NetlinkEvent::Action::kChange: { |
| 223 | LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed"; |
| 224 | handleDiskChanged(device); |
| 225 | break; |
| 226 | } |
| 227 | case NetlinkEvent::Action::kRemove: { |
| 228 | handleDiskRemoved(device); |
| 229 | break; |
| 230 | } |
| 231 | default: { |
| 232 | LOG(WARNING) << "Unexpected block event action " << (int)evt->getAction(); |
| 233 | break; |
| 234 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 238 | void VolumeManager::handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk) { |
| 239 | // For security reasons, if secure keyguard is showing, wait |
| 240 | // until the user unlocks the device to actually touch it |
| 241 | if (mSecureKeyguardShowing) { |
| 242 | LOG(INFO) << "Found disk at " << disk->getEventPath() |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 243 | << " but delaying scan due to secure keyguard"; |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 244 | mPendingDisks.push_back(disk); |
| 245 | } else { |
| 246 | disk->create(); |
| 247 | mDisks.push_back(disk); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | void VolumeManager::handleDiskChanged(dev_t device) { |
| 252 | for (const auto& disk : mDisks) { |
| 253 | if (disk->getDevice() == device) { |
| 254 | disk->readMetadata(); |
| 255 | disk->readPartitions(); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // For security reasons, we ignore all pending disks, since |
| 260 | // we'll scan them once the device is unlocked |
| 261 | } |
| 262 | |
| 263 | void VolumeManager::handleDiskRemoved(dev_t device) { |
| 264 | auto i = mDisks.begin(); |
| 265 | while (i != mDisks.end()) { |
| 266 | if ((*i)->getDevice() == device) { |
| 267 | (*i)->destroy(); |
| 268 | i = mDisks.erase(i); |
| 269 | } else { |
| 270 | ++i; |
| 271 | } |
| 272 | } |
| 273 | auto j = mPendingDisks.begin(); |
| 274 | while (j != mPendingDisks.end()) { |
| 275 | if ((*j)->getDevice() == device) { |
| 276 | j = mPendingDisks.erase(j); |
| 277 | } else { |
| 278 | ++j; |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 283 | void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) { |
Wei Wang | 6b455c2 | 2017-01-20 11:52:33 -0800 | [diff] [blame] | 284 | std::lock_guard<std::mutex> lock(mLock); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 285 | mDiskSources.push_back(diskSource); |
| 286 | } |
| 287 | |
| 288 | std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) { |
| 289 | for (auto disk : mDisks) { |
| 290 | if (disk->getId() == id) { |
| 291 | return disk; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 292 | } |
| 293 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 294 | return nullptr; |
| 295 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 296 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 297 | std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) { |
Gao Xiang | d263da8 | 2017-08-14 11:32:13 +0800 | [diff] [blame] | 298 | // Vold could receive "mount" after "shutdown" command in the extreme case. |
| 299 | // If this happens, mInternalEmulated will equal nullptr and |
| 300 | // we need to deal with it in order to avoid null pointer crash. |
| 301 | if (mInternalEmulated != nullptr && mInternalEmulated->getId() == id) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 302 | return mInternalEmulated; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 303 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 304 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 305 | auto vol = disk->findVolume(id); |
| 306 | if (vol != nullptr) { |
| 307 | return vol; |
| 308 | } |
| 309 | } |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 310 | for (const auto& vol : mStubVolumes) { |
| 311 | if (vol->getId() == id) { |
| 312 | return vol; |
| 313 | } |
| 314 | } |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 315 | for (const auto& vol : mObbVolumes) { |
| 316 | if (vol->getId() == id) { |
| 317 | return vol; |
| 318 | } |
| 319 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 320 | return nullptr; |
| 321 | } |
| 322 | |
Greg Kaiser | ef9abab | 2018-12-18 08:42:08 -0800 | [diff] [blame] | 323 | void VolumeManager::listVolumes(android::vold::VolumeBase::Type type, |
| 324 | std::list<std::string>& list) const { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 325 | list.clear(); |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 326 | for (const auto& disk : mDisks) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 327 | disk->listVolumes(type, list); |
| 328 | } |
| 329 | } |
| 330 | |
Jeff Sharkey | 3ce1825 | 2017-10-24 11:08:45 -0600 | [diff] [blame] | 331 | int VolumeManager::forgetPartition(const std::string& partGuid, const std::string& fsUuid) { |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 332 | std::string normalizedGuid; |
| 333 | if (android::vold::NormalizeHex(partGuid, normalizedGuid)) { |
| 334 | LOG(WARNING) << "Invalid GUID " << partGuid; |
| 335 | return -1; |
| 336 | } |
| 337 | |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 338 | bool success = true; |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 339 | std::string keyPath = android::vold::BuildKeyPath(normalizedGuid); |
| 340 | if (unlink(keyPath.c_str()) != 0) { |
| 341 | LOG(ERROR) << "Failed to unlink " << keyPath; |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 342 | success = false; |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 343 | } |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 344 | if (fscrypt_is_native()) { |
| 345 | if (!fscrypt_destroy_volume_keys(fsUuid)) { |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 346 | success = false; |
| 347 | } |
| 348 | } |
| 349 | return success ? 0 : -1; |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 352 | int VolumeManager::linkPrimary(userid_t userId) { |
| 353 | std::string source(mPrimary->getPath()); |
| 354 | if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) { |
| 355 | source = StringPrintf("%s/%d", source.c_str(), userId); |
Jeff Sharkey | 32679a8 | 2015-07-21 14:22:01 -0700 | [diff] [blame] | 356 | fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | std::string target(StringPrintf("/mnt/user/%d/primary", userId)); |
| 360 | if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) { |
| 361 | if (errno != ENOENT) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 362 | PLOG(WARNING) << "Failed to unlink " << target; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
Jeff Sharkey | 1bfb375 | 2015-04-29 15:22:23 -0700 | [diff] [blame] | 365 | LOG(DEBUG) << "Linking " << source << " to " << target; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 366 | if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 367 | PLOG(WARNING) << "Failed to link"; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 368 | return -errno; |
| 369 | } |
| 370 | return 0; |
| 371 | } |
| 372 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 373 | int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) { |
| 374 | mAddedUsers[userId] = userSerialNumber; |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | int VolumeManager::onUserRemoved(userid_t userId) { |
| 379 | mAddedUsers.erase(userId); |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | int VolumeManager::onUserStarted(userid_t userId) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 384 | // Note that sometimes the system will spin up processes from Zygote |
| 385 | // before actually starting the user, so we're okay if Zygote |
| 386 | // already created this directory. |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 387 | std::string path(StringPrintf("%s/%d", kPathUserMount, userId)); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 388 | fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT); |
| 389 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 390 | mStartedUsers.insert(userId); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 391 | if (mPrimary) { |
| 392 | linkPrimary(userId); |
| 393 | } |
| 394 | return 0; |
| 395 | } |
| 396 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 397 | int VolumeManager::onUserStopped(userid_t userId) { |
| 398 | mStartedUsers.erase(userId); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 399 | return 0; |
| 400 | } |
| 401 | |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 402 | int VolumeManager::onSecureKeyguardStateChanged(bool isShowing) { |
| 403 | mSecureKeyguardShowing = isShowing; |
| 404 | if (!mSecureKeyguardShowing) { |
| 405 | // Now that secure keyguard has been dismissed, process |
| 406 | // any pending disks |
| 407 | for (const auto& disk : mPendingDisks) { |
| 408 | disk->create(); |
| 409 | mDisks.push_back(disk); |
| 410 | } |
| 411 | mPendingDisks.clear(); |
| 412 | } |
| 413 | return 0; |
| 414 | } |
| 415 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 416 | int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) { |
| 417 | mPrimary = vol; |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 418 | for (userid_t userId : mStartedUsers) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 419 | linkPrimary(userId); |
| 420 | } |
| 421 | return 0; |
| 422 | } |
| 423 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 424 | int VolumeManager::remountUid(uid_t uid, const std::string& mode) { |
| 425 | LOG(DEBUG) << "Remounting " << uid << " as mode " << mode; |
| 426 | |
| 427 | DIR* dir; |
| 428 | struct dirent* de; |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 429 | std::string rootName; |
| 430 | std::string pidName; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 431 | int pidFd; |
| 432 | int nsFd; |
| 433 | struct stat sb; |
| 434 | pid_t child; |
| 435 | |
Jiyong Park | 8d21c92 | 2019-01-04 13:35:25 +0900 | [diff] [blame^] | 436 | static bool apexUpdatable = android::sysprop::ApexProperties::updatable().value_or(false); |
| 437 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 438 | if (!(dir = opendir("/proc"))) { |
| 439 | PLOG(ERROR) << "Failed to opendir"; |
| 440 | return -1; |
| 441 | } |
| 442 | |
| 443 | // Figure out root namespace to compare against below |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 444 | if (!android::vold::Readlinkat(dirfd(dir), "1/ns/mnt", &rootName)) { |
| 445 | PLOG(ERROR) << "Failed to read root namespace"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 446 | closedir(dir); |
| 447 | return -1; |
| 448 | } |
| 449 | |
| 450 | // Poke through all running PIDs look for apps running as UID |
| 451 | while ((de = readdir(dir))) { |
Jeff Vander Stoep | 5889083 | 2017-10-23 17:12:31 -0700 | [diff] [blame] | 452 | pid_t pid; |
| 453 | if (de->d_type != DT_DIR) continue; |
| 454 | if (!android::base::ParseInt(de->d_name, &pid)) continue; |
| 455 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 456 | pidFd = -1; |
| 457 | nsFd = -1; |
| 458 | |
| 459 | pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
| 460 | if (pidFd < 0) { |
| 461 | goto next; |
| 462 | } |
| 463 | if (fstat(pidFd, &sb) != 0) { |
| 464 | PLOG(WARNING) << "Failed to stat " << de->d_name; |
| 465 | goto next; |
| 466 | } |
| 467 | if (sb.st_uid != uid) { |
| 468 | goto next; |
| 469 | } |
| 470 | |
| 471 | // Matches so far, but refuse to touch if in root namespace |
| 472 | LOG(DEBUG) << "Found matching PID " << de->d_name; |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 473 | if (!android::vold::Readlinkat(pidFd, "ns/mnt", &pidName)) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 474 | PLOG(WARNING) << "Failed to read namespace for " << de->d_name; |
| 475 | goto next; |
| 476 | } |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 477 | if (rootName == pidName) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 478 | LOG(WARNING) << "Skipping due to root namespace"; |
| 479 | goto next; |
| 480 | } |
| 481 | |
Jiyong Park | 8d21c92 | 2019-01-04 13:35:25 +0900 | [diff] [blame^] | 482 | if (apexUpdatable) { |
| 483 | std::string exeName; |
| 484 | // When ro.apex.bionic_updatable is set to true, |
| 485 | // some early native processes have mount namespaces that are different |
| 486 | // from that of the init. Therefore, above check can't filter them out. |
| 487 | // Since the propagation type of / is 'shared', unmounting /storage |
| 488 | // for the early native processes affects other processes including |
| 489 | // init. Filter out such processes by skipping if a process is a |
| 490 | // non-Java process whose UID is < AID_APP_START. (The UID condition |
| 491 | // is required to not filter out child processes spawned by apps.) |
| 492 | if (!android::vold::Readlinkat(pidFd, "exe", &exeName)) { |
| 493 | PLOG(WARNING) << "Failed to read exe name for " << de->d_name; |
| 494 | goto next; |
| 495 | } |
| 496 | if (!StartsWith(exeName, "/system/bin/app_process") && sb.st_uid < AID_APP_START) { |
| 497 | LOG(WARNING) << "Skipping due to native system process"; |
| 498 | goto next; |
| 499 | } |
| 500 | } |
| 501 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 502 | // We purposefully leave the namespace open across the fork |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 503 | nsFd = openat(pidFd, "ns/mnt", O_RDONLY); // not O_CLOEXEC |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 504 | if (nsFd < 0) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 505 | PLOG(WARNING) << "Failed to open namespace for " << de->d_name; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 506 | goto next; |
| 507 | } |
| 508 | |
| 509 | if (!(child = fork())) { |
| 510 | if (setns(nsFd, CLONE_NEWNS) != 0) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 511 | PLOG(ERROR) << "Failed to setns for " << de->d_name; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 512 | _exit(1); |
| 513 | } |
| 514 | |
Sudheer Shanka | 99d304a | 2018-09-27 14:53:51 -0700 | [diff] [blame] | 515 | android::vold::UnmountTree("/storage/"); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 516 | |
| 517 | std::string storageSource; |
| 518 | if (mode == "default") { |
Jeff Sharkey | 1bd078f | 2015-08-06 11:40:00 -0700 | [diff] [blame] | 519 | storageSource = "/mnt/runtime/default"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 520 | } else if (mode == "read") { |
Jeff Sharkey | 1bd078f | 2015-08-06 11:40:00 -0700 | [diff] [blame] | 521 | storageSource = "/mnt/runtime/read"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 522 | } else if (mode == "write") { |
Jeff Sharkey | 1bd078f | 2015-08-06 11:40:00 -0700 | [diff] [blame] | 523 | storageSource = "/mnt/runtime/write"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 524 | } else { |
| 525 | // Sane default of no storage visible |
| 526 | _exit(0); |
| 527 | } |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 528 | if (TEMP_FAILURE_RETRY( |
| 529 | mount(storageSource.c_str(), "/storage", NULL, MS_BIND | MS_REC, NULL)) == -1) { |
| 530 | PLOG(ERROR) << "Failed to mount " << storageSource << " for " << de->d_name; |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 531 | _exit(1); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 532 | } |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 533 | if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL, MS_REC | MS_SLAVE, NULL)) == -1) { |
| 534 | PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for " << de->d_name; |
Hidehiko Abe | 674bed1 | 2016-03-09 16:42:10 +0900 | [diff] [blame] | 535 | _exit(1); |
| 536 | } |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 537 | |
| 538 | // Mount user-specific symlink helper into place |
| 539 | userid_t user_id = multiuser_get_user_id(uid); |
| 540 | std::string userSource(StringPrintf("/mnt/user/%d", user_id)); |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 541 | if (TEMP_FAILURE_RETRY( |
| 542 | mount(userSource.c_str(), "/storage/self", NULL, MS_BIND, NULL)) == -1) { |
| 543 | PLOG(ERROR) << "Failed to mount " << userSource << " for " << de->d_name; |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 544 | _exit(1); |
| 545 | } |
| 546 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 547 | _exit(0); |
| 548 | } |
| 549 | |
| 550 | if (child == -1) { |
| 551 | PLOG(ERROR) << "Failed to fork"; |
| 552 | goto next; |
| 553 | } else { |
| 554 | TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0)); |
| 555 | } |
| 556 | |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 557 | next: |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 558 | close(nsFd); |
| 559 | close(pidFd); |
| 560 | } |
| 561 | closedir(dir); |
| 562 | return 0; |
| 563 | } |
| 564 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 565 | int VolumeManager::reset() { |
| 566 | // Tear down all existing disks/volumes and start from a blank slate so |
| 567 | // newly connected framework hears all events. |
Gao Xiang | d263da8 | 2017-08-14 11:32:13 +0800 | [diff] [blame] | 568 | if (mInternalEmulated != nullptr) { |
| 569 | mInternalEmulated->destroy(); |
| 570 | mInternalEmulated->create(); |
| 571 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 572 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 573 | disk->destroy(); |
| 574 | disk->create(); |
| 575 | } |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 576 | updateVirtualDisk(); |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 577 | mAddedUsers.clear(); |
| 578 | mStartedUsers.clear(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 579 | return 0; |
| 580 | } |
| 581 | |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 582 | // Can be called twice (sequentially) during shutdown. should be safe for that. |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 583 | int VolumeManager::shutdown() { |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 584 | if (mInternalEmulated == nullptr) { |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 585 | return 0; // already shutdown |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 586 | } |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 587 | android::vold::sSleepOnUnmount = false; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 588 | mInternalEmulated->destroy(); |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 589 | mInternalEmulated = nullptr; |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 590 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 591 | disk->destroy(); |
| 592 | } |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 593 | mStubVolumes.clear(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 594 | mDisks.clear(); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 595 | mPendingDisks.clear(); |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 596 | android::vold::sSleepOnUnmount = true; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 597 | return 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 600 | int VolumeManager::unmountAll() { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 601 | std::lock_guard<std::mutex> lock(mLock); |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 602 | ATRACE_NAME("VolumeManager::unmountAll()"); |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 603 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 604 | // First, try gracefully unmounting all known devices |
| 605 | if (mInternalEmulated != nullptr) { |
| 606 | mInternalEmulated->unmount(); |
| 607 | } |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 608 | for (const auto& stub : mStubVolumes) { |
| 609 | stub->unmount(); |
| 610 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 611 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 612 | disk->unmountAll(); |
| 613 | } |
| 614 | |
| 615 | // Worst case we might have some stale mounts lurking around, so |
| 616 | // force unmount those just to be safe. |
LongPing.WEI | 4f04606 | 2018-11-23 19:27:35 +0800 | [diff] [blame] | 617 | FILE* fp = setmntent("/proc/mounts", "re"); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 618 | if (fp == NULL) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 619 | PLOG(ERROR) << "Failed to open /proc/mounts"; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 620 | return -errno; |
| 621 | } |
| 622 | |
| 623 | // Some volumes can be stacked on each other, so force unmount in |
| 624 | // reverse order to give us the best chance of success. |
| 625 | std::list<std::string> toUnmount; |
| 626 | mntent* mentry; |
| 627 | while ((mentry = getmntent(fp)) != NULL) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 628 | auto test = std::string(mentry->mnt_dir); |
Mark Salyzyn | c4405e9 | 2018-09-20 10:09:27 -0700 | [diff] [blame] | 629 | if ((StartsWith(test, "/mnt/") && |
| 630 | #ifdef __ANDROID_DEBUGGABLE__ |
| 631 | !StartsWith(test, "/mnt/scratch") && |
| 632 | #endif |
| 633 | !StartsWith(test, "/mnt/vendor") && !StartsWith(test, "/mnt/product")) || |
| 634 | StartsWith(test, "/storage/")) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 635 | toUnmount.push_front(test); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | endmntent(fp); |
| 639 | |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 640 | for (const auto& path : toUnmount) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 641 | LOG(DEBUG) << "Tearing down stale mount " << path; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 642 | android::vold::ForceUnmount(path); |
| 643 | } |
| 644 | |
| 645 | return 0; |
| 646 | } |
| 647 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 648 | int VolumeManager::mkdirs(const std::string& path) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 649 | // Only offer to create directories for paths managed by vold |
Mark Salyzyn | c4405e9 | 2018-09-20 10:09:27 -0700 | [diff] [blame] | 650 | if (StartsWith(path, "/storage/")) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 651 | // fs_mkdirs() does symlink checking and relative path enforcement |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 652 | return fs_mkdirs(path.c_str(), 0700); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 653 | } else { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 654 | LOG(ERROR) << "Failed to find mounted volume for " << path; |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 655 | return -EINVAL; |
| 656 | } |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 657 | } |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 658 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 659 | int VolumeManager::createObb(const std::string& sourcePath, const std::string& sourceKey, |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 660 | int32_t ownerGid, std::string* outVolId) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 661 | int id = mNextObbId++; |
| 662 | |
| 663 | auto vol = std::shared_ptr<android::vold::VolumeBase>( |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 664 | new android::vold::ObbVolume(id, sourcePath, sourceKey, ownerGid)); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 665 | vol->create(); |
| 666 | |
| 667 | mObbVolumes.push_back(vol); |
| 668 | *outVolId = vol->getId(); |
| 669 | return android::OK; |
| 670 | } |
| 671 | |
| 672 | int VolumeManager::destroyObb(const std::string& volId) { |
| 673 | auto i = mObbVolumes.begin(); |
| 674 | while (i != mObbVolumes.end()) { |
| 675 | if ((*i)->getId() == volId) { |
| 676 | (*i)->destroy(); |
| 677 | i = mObbVolumes.erase(i); |
| 678 | } else { |
| 679 | ++i; |
| 680 | } |
| 681 | } |
| 682 | return android::OK; |
| 683 | } |
| 684 | |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 685 | int VolumeManager::createStubVolume(const std::string& sourcePath, const std::string& mountPath, |
| 686 | const std::string& fsType, const std::string& fsUuid, |
| 687 | const std::string& fsLabel, std::string* outVolId) { |
| 688 | int id = mNextStubVolumeId++; |
| 689 | auto vol = std::shared_ptr<android::vold::VolumeBase>( |
| 690 | new android::vold::StubVolume(id, sourcePath, mountPath, fsType, fsUuid, fsLabel)); |
| 691 | vol->create(); |
| 692 | |
| 693 | mStubVolumes.push_back(vol); |
| 694 | *outVolId = vol->getId(); |
| 695 | return android::OK; |
| 696 | } |
| 697 | |
| 698 | int VolumeManager::destroyStubVolume(const std::string& volId) { |
| 699 | auto i = mStubVolumes.begin(); |
| 700 | while (i != mStubVolumes.end()) { |
| 701 | if ((*i)->getId() == volId) { |
| 702 | (*i)->destroy(); |
| 703 | i = mStubVolumes.erase(i); |
| 704 | } else { |
| 705 | ++i; |
| 706 | } |
| 707 | } |
| 708 | return android::OK; |
| 709 | } |
| 710 | |
Risan | 5f53cd3 | 2018-10-26 20:56:45 -0600 | [diff] [blame] | 711 | int VolumeManager::mountAppFuse(uid_t uid, int mountId, unique_fd* device_fd) { |
Risan | ac02a48 | 2018-10-31 21:59:47 -0600 | [diff] [blame] | 712 | return android::vold::MountAppFuse(uid, mountId, device_fd); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 713 | } |
| 714 | |
Risan | 5f53cd3 | 2018-10-26 20:56:45 -0600 | [diff] [blame] | 715 | int VolumeManager::unmountAppFuse(uid_t uid, int mountId) { |
Risan | ac02a48 | 2018-10-31 21:59:47 -0600 | [diff] [blame] | 716 | return android::vold::UnmountAppFuse(uid, mountId); |
Risan | 5f53cd3 | 2018-10-26 20:56:45 -0600 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | int VolumeManager::openAppFuseFile(uid_t uid, int mountId, int fileId, int flags) { |
Risan | ac02a48 | 2018-10-31 21:59:47 -0600 | [diff] [blame] | 720 | return android::vold::OpenAppFuseFile(uid, mountId, fileId, flags); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 721 | } |