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