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 | edf7a4e | 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> |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 33 | #include <array> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 34 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 35 | #include <linux/kdev_t.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 36 | |
Jiyong Park | 8d21c92 | 2019-01-04 13:35:25 +0900 | [diff] [blame] | 37 | #include <ApexProperties.sysprop.h> |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 38 | #include <android-base/logging.h> |
Jeff Vander Stoep | 5889083 | 2017-10-23 17:12:31 -0700 | [diff] [blame] | 39 | #include <android-base/parseint.h> |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 40 | #include <android-base/properties.h> |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 41 | #include <android-base/stringprintf.h> |
Jeff Vander Stoep | 5889083 | 2017-10-23 17:12:31 -0700 | [diff] [blame] | 42 | #include <android-base/strings.h> |
Narayan Kamath | 02efdf5 | 2019-11-27 10:53:51 +0000 | [diff] [blame] | 43 | #include <async_safe/log.h> |
Jeff Vander Stoep | 5889083 | 2017-10-23 17:12:31 -0700 | [diff] [blame] | 44 | |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 45 | #include <cutils/fs.h> |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 46 | #include <utils/Trace.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 47 | |
Robert Craig | b9e3ba5 | 2014-02-04 10:53:00 -0500 | [diff] [blame] | 48 | #include <selinux/android.h> |
| 49 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 50 | #include <sysutils/NetlinkEvent.h> |
| 51 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 52 | #include <private/android_filesystem_config.h> |
| 53 | |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 54 | #include <fscrypt/fscrypt.h> |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 55 | |
Risan | ac02a48 | 2018-10-31 21:59:47 -0600 | [diff] [blame] | 56 | #include "AppFuseUtil.h" |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 57 | #include "Devmapper.h" |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 58 | #include "FsCrypt.h" |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 59 | #include "Loop.h" |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 60 | #include "NetlinkManager.h" |
| 61 | #include "Process.h" |
| 62 | #include "Utils.h" |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 63 | #include "VoldNativeService.h" |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 64 | #include "VoldUtil.h" |
| 65 | #include "VolumeManager.h" |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 66 | #include "fs/Ext4.h" |
| 67 | #include "fs/Vfat.h" |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 68 | #include "model/EmulatedVolume.h" |
| 69 | #include "model/ObbVolume.h" |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 70 | #include "model/PrivateVolume.h" |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 71 | #include "model/StubVolume.h" |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 72 | |
Sudheer Shanka | f9b38a5 | 2019-02-14 19:09:51 +0000 | [diff] [blame] | 73 | using android::OK; |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 74 | using android::base::GetBoolProperty; |
Mark Salyzyn | 86e81e7 | 2018-09-20 10:09:27 -0700 | [diff] [blame] | 75 | using android::base::StartsWith; |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 76 | using android::base::StringAppendF; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 77 | using android::base::StringPrintf; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 78 | using android::base::unique_fd; |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 79 | using android::vold::BindMount; |
Sudheer Shanka | f9b38a5 | 2019-02-14 19:09:51 +0000 | [diff] [blame] | 80 | using android::vold::CreateDir; |
Sudheer Shanka | 30df1c6 | 2019-02-22 17:03:02 -0800 | [diff] [blame] | 81 | using android::vold::DeleteDirContents; |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 82 | using android::vold::DeleteDirContentsAndDir; |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 83 | using android::vold::IsFilesystemSupported; |
| 84 | using android::vold::PrepareAndroidDirs; |
| 85 | using android::vold::PrepareAppDirsFromRoot; |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 86 | using android::vold::PrivateVolume; |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 87 | using android::vold::Symlink; |
| 88 | using android::vold::Unlink; |
| 89 | using android::vold::UnmountTree; |
Sudheer Shanka | 03992e3 | 2018-12-12 12:43:38 -0800 | [diff] [blame] | 90 | using android::vold::VoldNativeService; |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 91 | using android::vold::VolumeBase; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 92 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 93 | static const char* kPathUserMount = "/mnt/user"; |
| 94 | static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk"; |
| 95 | |
| 96 | static const char* kPropVirtualDisk = "persist.sys.virtual_disk"; |
| 97 | |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 98 | static const std::string kEmptyString(""); |
| 99 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 100 | /* 512MiB is large enough for testing purposes */ |
| 101 | static const unsigned int kSizeVirtualDisk = 536870912; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 102 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 103 | static const unsigned int kMajorBlockMmc = 179; |
Yu Ning | 942d4e8 | 2016-01-08 17:36:47 +0800 | [diff] [blame] | 104 | static const unsigned int kMajorBlockExperimentalMin = 240; |
| 105 | static const unsigned int kMajorBlockExperimentalMax = 254; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 106 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 107 | VolumeManager* VolumeManager::sInstance = NULL; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 108 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 109 | VolumeManager* VolumeManager::Instance() { |
| 110 | if (!sInstance) sInstance = new VolumeManager(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 111 | return sInstance; |
| 112 | } |
| 113 | |
| 114 | VolumeManager::VolumeManager() { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 115 | mDebug = false; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 116 | mNextObbId = 0; |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 117 | mNextStubVolumeId = 0; |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 118 | // For security reasons, assume that a secure keyguard is |
| 119 | // showing until we hear otherwise |
| 120 | mSecureKeyguardShowing = true; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 123 | VolumeManager::~VolumeManager() {} |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 124 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 125 | int VolumeManager::updateVirtualDisk() { |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 126 | ATRACE_NAME("VolumeManager::updateVirtualDisk"); |
Mark Salyzyn | 86e81e7 | 2018-09-20 10:09:27 -0700 | [diff] [blame] | 127 | if (GetBoolProperty(kPropVirtualDisk, false)) { |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 128 | if (access(kPathVirtualDisk, F_OK) != 0) { |
| 129 | Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512); |
| 130 | } |
| 131 | |
| 132 | if (mVirtualDisk == nullptr) { |
| 133 | if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) { |
| 134 | LOG(ERROR) << "Failed to create virtual disk"; |
| 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | struct stat buf; |
| 139 | if (stat(mVirtualDiskPath.c_str(), &buf) < 0) { |
| 140 | PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath; |
| 141 | return -1; |
| 142 | } |
| 143 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 144 | auto disk = new android::vold::Disk( |
| 145 | "virtual", buf.st_rdev, "virtual", |
| 146 | android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd); |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 147 | mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 148 | handleDiskAdded(mVirtualDisk); |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 149 | } |
| 150 | } else { |
| 151 | if (mVirtualDisk != nullptr) { |
| 152 | dev_t device = mVirtualDisk->getDevice(); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 153 | handleDiskRemoved(device); |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 154 | |
| 155 | Loop::destroyByDevice(mVirtualDiskPath.c_str()); |
| 156 | mVirtualDisk = nullptr; |
| 157 | } |
| 158 | |
| 159 | if (access(kPathVirtualDisk, F_OK) == 0) { |
| 160 | unlink(kPathVirtualDisk); |
| 161 | } |
| 162 | } |
| 163 | return 0; |
| 164 | } |
| 165 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 166 | int VolumeManager::setDebug(bool enable) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 167 | mDebug = enable; |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 168 | return 0; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 169 | } |
| 170 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 171 | int VolumeManager::start() { |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 172 | ATRACE_NAME("VolumeManager::start"); |
| 173 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 174 | // Always start from a clean slate by unmounting everything in |
| 175 | // directories that we own, in case we crashed. |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 176 | unmountAll(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 177 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 178 | Devmapper::destroyAll(); |
| 179 | Loop::destroyAll(); |
| 180 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 181 | // Assume that we always have an emulated volume on internal |
| 182 | // storage; the framework will decide if it should be mounted. |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 183 | CHECK(mInternalEmulatedVolumes.empty()); |
| 184 | |
| 185 | auto vol = std::shared_ptr<android::vold::VolumeBase>( |
| 186 | new android::vold::EmulatedVolume("/data/media", 0)); |
| 187 | vol->setMountUserId(0); |
| 188 | vol->create(); |
| 189 | mInternalEmulatedVolumes.push_back(vol); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 190 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 191 | // Consider creating a virtual disk |
| 192 | updateVirtualDisk(); |
| 193 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | int VolumeManager::stop() { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 198 | CHECK(!mInternalEmulatedVolumes.empty()); |
| 199 | for (const auto& vol : mInternalEmulatedVolumes) { |
| 200 | vol->destroy(); |
| 201 | } |
| 202 | mInternalEmulatedVolumes.clear(); |
| 203 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| 206 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 207 | void VolumeManager::handleBlockEvent(NetlinkEvent* evt) { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 208 | std::lock_guard<std::mutex> lock(mLock); |
| 209 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 210 | if (mDebug) { |
Sudheer Shanka | 4b6ca4e | 2018-09-21 10:54:54 -0700 | [diff] [blame] | 211 | LOG(DEBUG) << "----------------"; |
| 212 | LOG(DEBUG) << "handleBlockEvent with action " << (int)evt->getAction(); |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 213 | evt->dump(); |
| 214 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 215 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 216 | std::string eventPath(evt->findParam("DEVPATH") ? evt->findParam("DEVPATH") : ""); |
| 217 | std::string devType(evt->findParam("DEVTYPE") ? evt->findParam("DEVTYPE") : ""); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 218 | |
| 219 | if (devType != "disk") return; |
| 220 | |
Jeff Sharkey | 95440eb | 2017-09-18 18:19:28 -0600 | [diff] [blame] | 221 | int major = std::stoi(evt->findParam("MAJOR")); |
| 222 | int minor = std::stoi(evt->findParam("MINOR")); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 223 | dev_t device = makedev(major, minor); |
| 224 | |
| 225 | switch (evt->getAction()) { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 226 | case NetlinkEvent::Action::kAdd: { |
| 227 | for (const auto& source : mDiskSources) { |
| 228 | if (source->matches(eventPath)) { |
| 229 | // For now, assume that MMC and virtio-blk (the latter is |
| 230 | // emulator-specific; see Disk.cpp for details) devices are SD, |
| 231 | // and that everything else is USB |
| 232 | int flags = source->getFlags(); |
| 233 | if (major == kMajorBlockMmc || (android::vold::IsRunningInEmulator() && |
| 234 | major >= (int)kMajorBlockExperimentalMin && |
| 235 | major <= (int)kMajorBlockExperimentalMax)) { |
| 236 | flags |= android::vold::Disk::Flags::kSd; |
| 237 | } else { |
| 238 | flags |= android::vold::Disk::Flags::kUsb; |
| 239 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 240 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 241 | auto disk = |
| 242 | new android::vold::Disk(eventPath, device, source->getNickname(), flags); |
| 243 | handleDiskAdded(std::shared_ptr<android::vold::Disk>(disk)); |
| 244 | break; |
| 245 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 246 | } |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 247 | break; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 248 | } |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 249 | case NetlinkEvent::Action::kChange: { |
| 250 | LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed"; |
| 251 | handleDiskChanged(device); |
| 252 | break; |
| 253 | } |
| 254 | case NetlinkEvent::Action::kRemove: { |
| 255 | handleDiskRemoved(device); |
| 256 | break; |
| 257 | } |
| 258 | default: { |
| 259 | LOG(WARNING) << "Unexpected block event action " << (int)evt->getAction(); |
| 260 | break; |
| 261 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 265 | void VolumeManager::handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk) { |
| 266 | // For security reasons, if secure keyguard is showing, wait |
| 267 | // until the user unlocks the device to actually touch it |
Martijn Coenen | cf5916f | 2020-01-03 14:36:45 +0100 | [diff] [blame] | 268 | // Additionally, wait until user 0 is actually started, since we need |
| 269 | // the user to be up before we can mount a FUSE daemon to handle the disk. |
| 270 | bool userZeroStarted = mStartedUsers.find(0) != mStartedUsers.end(); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 271 | if (mSecureKeyguardShowing) { |
| 272 | LOG(INFO) << "Found disk at " << disk->getEventPath() |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 273 | << " but delaying scan due to secure keyguard"; |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 274 | mPendingDisks.push_back(disk); |
Martijn Coenen | cf5916f | 2020-01-03 14:36:45 +0100 | [diff] [blame] | 275 | } else if (!userZeroStarted) { |
| 276 | LOG(INFO) << "Found disk at " << disk->getEventPath() |
| 277 | << " but delaying scan due to user zero not having started"; |
| 278 | mPendingDisks.push_back(disk); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 279 | } else { |
| 280 | disk->create(); |
| 281 | mDisks.push_back(disk); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | void VolumeManager::handleDiskChanged(dev_t device) { |
| 286 | for (const auto& disk : mDisks) { |
| 287 | if (disk->getDevice() == device) { |
| 288 | disk->readMetadata(); |
| 289 | disk->readPartitions(); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | // For security reasons, we ignore all pending disks, since |
| 294 | // we'll scan them once the device is unlocked |
| 295 | } |
| 296 | |
| 297 | void VolumeManager::handleDiskRemoved(dev_t device) { |
| 298 | auto i = mDisks.begin(); |
| 299 | while (i != mDisks.end()) { |
| 300 | if ((*i)->getDevice() == device) { |
| 301 | (*i)->destroy(); |
| 302 | i = mDisks.erase(i); |
| 303 | } else { |
| 304 | ++i; |
| 305 | } |
| 306 | } |
| 307 | auto j = mPendingDisks.begin(); |
| 308 | while (j != mPendingDisks.end()) { |
| 309 | if ((*j)->getDevice() == device) { |
| 310 | j = mPendingDisks.erase(j); |
| 311 | } else { |
| 312 | ++j; |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 317 | void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) { |
Wei Wang | 6b455c2 | 2017-01-20 11:52:33 -0800 | [diff] [blame] | 318 | std::lock_guard<std::mutex> lock(mLock); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 319 | mDiskSources.push_back(diskSource); |
| 320 | } |
| 321 | |
| 322 | std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) { |
| 323 | for (auto disk : mDisks) { |
| 324 | if (disk->getId() == id) { |
| 325 | return disk; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 326 | } |
| 327 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 328 | return nullptr; |
| 329 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 330 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 331 | std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 332 | for (const auto& vol : mInternalEmulatedVolumes) { |
| 333 | if (vol->getId() == id) { |
| 334 | return vol; |
| 335 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 336 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 337 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 338 | auto vol = disk->findVolume(id); |
| 339 | if (vol != nullptr) { |
| 340 | return vol; |
| 341 | } |
| 342 | } |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 343 | for (const auto& vol : mStubVolumes) { |
| 344 | if (vol->getId() == id) { |
| 345 | return vol; |
| 346 | } |
| 347 | } |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 348 | for (const auto& vol : mObbVolumes) { |
| 349 | if (vol->getId() == id) { |
| 350 | return vol; |
| 351 | } |
| 352 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 353 | return nullptr; |
| 354 | } |
| 355 | |
Greg Kaiser | 2bc201e | 2018-12-18 08:42:08 -0800 | [diff] [blame] | 356 | void VolumeManager::listVolumes(android::vold::VolumeBase::Type type, |
| 357 | std::list<std::string>& list) const { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 358 | list.clear(); |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 359 | for (const auto& disk : mDisks) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 360 | disk->listVolumes(type, list); |
| 361 | } |
| 362 | } |
| 363 | |
Jeff Sharkey | 3ce1825 | 2017-10-24 11:08:45 -0600 | [diff] [blame] | 364 | int VolumeManager::forgetPartition(const std::string& partGuid, const std::string& fsUuid) { |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 365 | std::string normalizedGuid; |
| 366 | if (android::vold::NormalizeHex(partGuid, normalizedGuid)) { |
| 367 | LOG(WARNING) << "Invalid GUID " << partGuid; |
| 368 | return -1; |
| 369 | } |
| 370 | |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 371 | bool success = true; |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 372 | std::string keyPath = android::vold::BuildKeyPath(normalizedGuid); |
| 373 | if (unlink(keyPath.c_str()) != 0) { |
| 374 | LOG(ERROR) << "Failed to unlink " << keyPath; |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 375 | success = false; |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 376 | } |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 377 | if (fscrypt_is_native()) { |
| 378 | if (!fscrypt_destroy_volume_keys(fsUuid)) { |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 379 | success = false; |
| 380 | } |
| 381 | } |
| 382 | return success ? 0 : -1; |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 385 | int VolumeManager::linkPrimary(userid_t userId) { |
Abhijeet Kaur | 01fa0e0 | 2019-12-13 10:26:32 +0000 | [diff] [blame] | 386 | if (!GetBoolProperty(android::vold::kPropFuse, false)) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 387 | std::string source(mPrimary->getPath()); |
| 388 | if (mPrimary->isEmulated()) { |
| 389 | source = StringPrintf("%s/%d", source.c_str(), userId); |
| 390 | fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT); |
| 391 | } |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 392 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 393 | std::string target(StringPrintf("/mnt/user/%d/primary", userId)); |
| 394 | LOG(DEBUG) << "Linking " << source << " to " << target; |
| 395 | Symlink(source, target); |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 396 | } |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 397 | return 0; |
| 398 | } |
| 399 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 400 | void VolumeManager::destroyEmulatedVolumesForUser(userid_t userId) { |
| 401 | // Destroy and remove all unstacked EmulatedVolumes for the user |
| 402 | auto i = mInternalEmulatedVolumes.begin(); |
| 403 | while (i != mInternalEmulatedVolumes.end()) { |
| 404 | auto vol = *i; |
| 405 | if (vol->getMountUserId() == userId) { |
| 406 | vol->destroy(); |
| 407 | i = mInternalEmulatedVolumes.erase(i); |
| 408 | } else { |
| 409 | i++; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | // Destroy and remove all stacked EmulatedVolumes for the user on each mounted private volume |
| 414 | std::list<std::string> private_vols; |
| 415 | listVolumes(VolumeBase::Type::kPrivate, private_vols); |
| 416 | for (const std::string& id : private_vols) { |
| 417 | PrivateVolume* pvol = static_cast<PrivateVolume*>(findVolume(id).get()); |
| 418 | std::list<std::shared_ptr<VolumeBase>> vols_to_remove; |
| 419 | if (pvol->getState() == VolumeBase::State::kMounted) { |
| 420 | for (const auto& vol : pvol->getVolumes()) { |
| 421 | if (vol->getMountUserId() == userId) { |
| 422 | vols_to_remove.push_back(vol); |
| 423 | } |
| 424 | } |
| 425 | for (const auto& vol : vols_to_remove) { |
| 426 | vol->destroy(); |
| 427 | pvol->removeVolume(vol); |
| 428 | } |
| 429 | } // else EmulatedVolumes will be destroyed on VolumeBase#unmount |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | void VolumeManager::createEmulatedVolumesForUser(userid_t userId) { |
| 434 | // Create unstacked EmulatedVolumes for the user |
| 435 | auto vol = std::shared_ptr<android::vold::VolumeBase>( |
| 436 | new android::vold::EmulatedVolume("/data/media", userId)); |
| 437 | vol->setMountUserId(userId); |
| 438 | mInternalEmulatedVolumes.push_back(vol); |
| 439 | vol->create(); |
| 440 | |
| 441 | // Create stacked EmulatedVolumes for the user on each PrivateVolume |
| 442 | std::list<std::string> private_vols; |
| 443 | listVolumes(VolumeBase::Type::kPrivate, private_vols); |
| 444 | for (const std::string& id : private_vols) { |
| 445 | PrivateVolume* pvol = static_cast<PrivateVolume*>(findVolume(id).get()); |
| 446 | if (pvol->getState() == VolumeBase::State::kMounted) { |
| 447 | auto evol = |
| 448 | std::shared_ptr<android::vold::VolumeBase>(new android::vold::EmulatedVolume( |
| 449 | pvol->getPath() + "/media", pvol->getRawDevice(), pvol->getFsUuid(), |
| 450 | userId)); |
| 451 | evol->setMountUserId(userId); |
| 452 | pvol->addVolume(evol); |
| 453 | evol->create(); |
| 454 | } // else EmulatedVolumes will be created per user when on PrivateVolume#doMount |
| 455 | } |
| 456 | } |
| 457 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 458 | int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 459 | LOG(INFO) << "onUserAdded: " << userId; |
| 460 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 461 | mAddedUsers[userId] = userSerialNumber; |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | int VolumeManager::onUserRemoved(userid_t userId) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 466 | LOG(INFO) << "onUserRemoved: " << userId; |
| 467 | |
Zim | 2d45d9b | 2019-11-14 16:19:05 +0000 | [diff] [blame] | 468 | onUserStopped(userId); |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 469 | mAddedUsers.erase(userId); |
| 470 | return 0; |
| 471 | } |
| 472 | |
Sudheer Shanka | ebce4cc | 2019-04-29 10:46:35 -0700 | [diff] [blame] | 473 | int VolumeManager::onUserStarted(userid_t userId) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 474 | LOG(INFO) << "onUserStarted: " << userId; |
| 475 | |
Zim | 2d45d9b | 2019-11-14 16:19:05 +0000 | [diff] [blame] | 476 | if (mStartedUsers.find(userId) == mStartedUsers.end()) { |
| 477 | createEmulatedVolumesForUser(userId); |
| 478 | } |
| 479 | |
Abhijeet Kaur | 01fa0e0 | 2019-12-13 10:26:32 +0000 | [diff] [blame] | 480 | if (!GetBoolProperty(android::vold::kPropFuse, false)) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 481 | // Note that sometimes the system will spin up processes from Zygote |
| 482 | // before actually starting the user, so we're okay if Zygote |
| 483 | // already created this directory. |
| 484 | std::string path(StringPrintf("%s/%d", kPathUserMount, userId)); |
| 485 | fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT); |
| 486 | |
| 487 | if (mPrimary) { |
| 488 | linkPrimary(userId); |
| 489 | } |
| 490 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 491 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 492 | mStartedUsers.insert(userId); |
Martijn Coenen | cf5916f | 2020-01-03 14:36:45 +0100 | [diff] [blame] | 493 | |
| 494 | createPendingDisksIfNeeded(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 495 | return 0; |
| 496 | } |
| 497 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 498 | int VolumeManager::onUserStopped(userid_t userId) { |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 499 | LOG(VERBOSE) << "onUserStopped: " << userId; |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 500 | |
Zim | 2d45d9b | 2019-11-14 16:19:05 +0000 | [diff] [blame] | 501 | if (mStartedUsers.find(userId) != mStartedUsers.end()) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 502 | destroyEmulatedVolumesForUser(userId); |
| 503 | } |
| 504 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 505 | mStartedUsers.erase(userId); |
Sudheer Shanka | fa6a174 | 2018-10-04 16:26:22 -0700 | [diff] [blame] | 506 | return 0; |
| 507 | } |
| 508 | |
Martijn Coenen | cf5916f | 2020-01-03 14:36:45 +0100 | [diff] [blame] | 509 | void VolumeManager::createPendingDisksIfNeeded() { |
| 510 | bool userZeroStarted = mStartedUsers.find(0) != mStartedUsers.end(); |
| 511 | if (!mSecureKeyguardShowing && userZeroStarted) { |
| 512 | // Now that secure keyguard has been dismissed and user 0 has |
| 513 | // started, process any pending disks |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 514 | for (const auto& disk : mPendingDisks) { |
| 515 | disk->create(); |
| 516 | mDisks.push_back(disk); |
| 517 | } |
| 518 | mPendingDisks.clear(); |
| 519 | } |
Martijn Coenen | cf5916f | 2020-01-03 14:36:45 +0100 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | int VolumeManager::onSecureKeyguardStateChanged(bool isShowing) { |
| 523 | mSecureKeyguardShowing = isShowing; |
| 524 | createPendingDisksIfNeeded(); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 525 | return 0; |
| 526 | } |
| 527 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 528 | int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) { |
| 529 | mPrimary = vol; |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 530 | for (userid_t userId : mStartedUsers) { |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 531 | linkPrimary(userId); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 532 | } |
| 533 | return 0; |
| 534 | } |
| 535 | |
Narayan Kamath | 02efdf5 | 2019-11-27 10:53:51 +0000 | [diff] [blame] | 536 | // This code is executed after a fork so it's very important that the set of |
| 537 | // methods we call here is strictly limited. |
| 538 | // |
| 539 | // TODO: Get rid of this guesswork altogether and instead exec a process |
| 540 | // immediately after fork to do our bindding for us. |
| 541 | static bool childProcess(const char* storageSource, const char* userSource, int nsFd, |
| 542 | struct dirent* de) { |
| 543 | if (setns(nsFd, CLONE_NEWNS) != 0) { |
| 544 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to setns for %s :%s", de->d_name, |
| 545 | strerror(errno)); |
| 546 | return false; |
| 547 | } |
| 548 | |
| 549 | // NOTE: Inlined from vold::UnmountTree here to avoid using PLOG methods and |
| 550 | // to also protect against future changes that may cause issues across a |
| 551 | // fork. |
| 552 | if (TEMP_FAILURE_RETRY(umount2("/storage/", MNT_DETACH)) < 0 && errno != EINVAL && |
| 553 | errno != ENOENT) { |
| 554 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to unmount /storage/ :%s", |
| 555 | strerror(errno)); |
| 556 | return false; |
| 557 | } |
| 558 | |
| 559 | if (TEMP_FAILURE_RETRY(mount(storageSource, "/storage", NULL, MS_BIND | MS_REC, NULL)) == -1) { |
| 560 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to mount %s for %s :%s", |
| 561 | storageSource, de->d_name, strerror(errno)); |
| 562 | return false; |
| 563 | } |
| 564 | |
| 565 | if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL, MS_REC | MS_SLAVE, NULL)) == -1) { |
| 566 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", |
| 567 | "Failed to set MS_SLAVE to /storage for %s :%s", de->d_name, |
| 568 | strerror(errno)); |
| 569 | return false; |
| 570 | } |
| 571 | |
| 572 | if (TEMP_FAILURE_RETRY(mount(userSource, "/storage/self", NULL, MS_BIND, NULL)) == -1) { |
| 573 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to mount %s for %s :%s", |
| 574 | userSource, de->d_name, strerror(errno)); |
| 575 | return false; |
| 576 | } |
| 577 | |
| 578 | return true; |
| 579 | } |
| 580 | |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 581 | int VolumeManager::remountUid(uid_t uid, int32_t mountMode) { |
Abhijeet Kaur | 01fa0e0 | 2019-12-13 10:26:32 +0000 | [diff] [blame] | 582 | if (GetBoolProperty(android::vold::kPropFuse, false)) { |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 583 | // TODO(135341433): Implement fuse specific logic. |
| 584 | return 0; |
| 585 | } |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 586 | std::string mode; |
| 587 | switch (mountMode) { |
| 588 | case VoldNativeService::REMOUNT_MODE_NONE: |
| 589 | mode = "none"; |
| 590 | break; |
| 591 | case VoldNativeService::REMOUNT_MODE_DEFAULT: |
| 592 | mode = "default"; |
| 593 | break; |
| 594 | case VoldNativeService::REMOUNT_MODE_READ: |
| 595 | mode = "read"; |
| 596 | break; |
| 597 | case VoldNativeService::REMOUNT_MODE_WRITE: |
Sudheer Shanka | a05ea74 | 2019-04-12 13:55:28 -0700 | [diff] [blame] | 598 | case VoldNativeService::REMOUNT_MODE_LEGACY: |
| 599 | case VoldNativeService::REMOUNT_MODE_INSTALLER: |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 600 | mode = "write"; |
| 601 | break; |
Sudheer Shanka | 36bdf7a | 2019-04-18 15:18:30 -0700 | [diff] [blame] | 602 | case VoldNativeService::REMOUNT_MODE_FULL: |
| 603 | mode = "full"; |
| 604 | break; |
Zim | 981222f | 2019-09-09 10:24:44 +0100 | [diff] [blame] | 605 | case VoldNativeService::REMOUNT_MODE_PASS_THROUGH: |
| 606 | mode = "pass_through"; |
| 607 | break; |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 608 | default: |
| 609 | PLOG(ERROR) << "Unknown mode " << std::to_string(mountMode); |
| 610 | return -1; |
| 611 | } |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 612 | LOG(DEBUG) << "Remounting " << uid << " as mode " << mode; |
| 613 | |
| 614 | DIR* dir; |
| 615 | struct dirent* de; |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 616 | std::string rootName; |
| 617 | std::string pidName; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 618 | int pidFd; |
| 619 | int nsFd; |
| 620 | struct stat sb; |
| 621 | pid_t child; |
Narayan Kamath | 02efdf5 | 2019-11-27 10:53:51 +0000 | [diff] [blame] | 622 | std::string userSource; |
| 623 | std::string storageSource; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 624 | |
Jiyong Park | 8d21c92 | 2019-01-04 13:35:25 +0900 | [diff] [blame] | 625 | static bool apexUpdatable = android::sysprop::ApexProperties::updatable().value_or(false); |
| 626 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 627 | if (!(dir = opendir("/proc"))) { |
| 628 | PLOG(ERROR) << "Failed to opendir"; |
| 629 | return -1; |
| 630 | } |
| 631 | |
| 632 | // Figure out root namespace to compare against below |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 633 | if (!android::vold::Readlinkat(dirfd(dir), "1/ns/mnt", &rootName)) { |
| 634 | PLOG(ERROR) << "Failed to read root namespace"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 635 | closedir(dir); |
| 636 | return -1; |
| 637 | } |
| 638 | |
| 639 | // Poke through all running PIDs look for apps running as UID |
| 640 | while ((de = readdir(dir))) { |
Jeff Vander Stoep | 5889083 | 2017-10-23 17:12:31 -0700 | [diff] [blame] | 641 | pid_t pid; |
| 642 | if (de->d_type != DT_DIR) continue; |
| 643 | if (!android::base::ParseInt(de->d_name, &pid)) continue; |
| 644 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 645 | pidFd = -1; |
| 646 | nsFd = -1; |
| 647 | |
| 648 | pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
| 649 | if (pidFd < 0) { |
| 650 | goto next; |
| 651 | } |
| 652 | if (fstat(pidFd, &sb) != 0) { |
| 653 | PLOG(WARNING) << "Failed to stat " << de->d_name; |
| 654 | goto next; |
| 655 | } |
| 656 | if (sb.st_uid != uid) { |
| 657 | goto next; |
| 658 | } |
| 659 | |
| 660 | // Matches so far, but refuse to touch if in root namespace |
| 661 | LOG(DEBUG) << "Found matching PID " << de->d_name; |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 662 | if (!android::vold::Readlinkat(pidFd, "ns/mnt", &pidName)) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 663 | PLOG(WARNING) << "Failed to read namespace for " << de->d_name; |
| 664 | goto next; |
| 665 | } |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 666 | if (rootName == pidName) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 667 | LOG(WARNING) << "Skipping due to root namespace"; |
| 668 | goto next; |
| 669 | } |
| 670 | |
Jiyong Park | 8d21c92 | 2019-01-04 13:35:25 +0900 | [diff] [blame] | 671 | if (apexUpdatable) { |
| 672 | std::string exeName; |
| 673 | // When ro.apex.bionic_updatable is set to true, |
| 674 | // some early native processes have mount namespaces that are different |
| 675 | // from that of the init. Therefore, above check can't filter them out. |
| 676 | // Since the propagation type of / is 'shared', unmounting /storage |
| 677 | // for the early native processes affects other processes including |
| 678 | // init. Filter out such processes by skipping if a process is a |
| 679 | // non-Java process whose UID is < AID_APP_START. (The UID condition |
| 680 | // is required to not filter out child processes spawned by apps.) |
| 681 | if (!android::vold::Readlinkat(pidFd, "exe", &exeName)) { |
| 682 | PLOG(WARNING) << "Failed to read exe name for " << de->d_name; |
| 683 | goto next; |
| 684 | } |
| 685 | if (!StartsWith(exeName, "/system/bin/app_process") && sb.st_uid < AID_APP_START) { |
| 686 | LOG(WARNING) << "Skipping due to native system process"; |
| 687 | goto next; |
| 688 | } |
| 689 | } |
| 690 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 691 | // We purposefully leave the namespace open across the fork |
Nick Kralevich | e7e89ac | 2019-03-29 16:03:51 -0700 | [diff] [blame] | 692 | // NOLINTNEXTLINE(android-cloexec-open): Deliberately not O_CLOEXEC |
| 693 | nsFd = openat(pidFd, "ns/mnt", O_RDONLY); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 694 | if (nsFd < 0) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 695 | PLOG(WARNING) << "Failed to open namespace for " << de->d_name; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 696 | goto next; |
| 697 | } |
| 698 | |
Narayan Kamath | 02efdf5 | 2019-11-27 10:53:51 +0000 | [diff] [blame] | 699 | if (mode == "default") { |
| 700 | storageSource = "/mnt/runtime/default"; |
| 701 | } else if (mode == "read") { |
| 702 | storageSource = "/mnt/runtime/read"; |
| 703 | } else if (mode == "write") { |
| 704 | storageSource = "/mnt/runtime/write"; |
| 705 | } else if (mode == "full") { |
| 706 | storageSource = "/mnt/runtime/full"; |
| 707 | } else { |
| 708 | // Sane default of no storage visible. No need to fork a child |
| 709 | // to remount uid. |
| 710 | goto next; |
| 711 | } |
| 712 | |
| 713 | // Mount user-specific symlink helper into place |
| 714 | userSource = StringPrintf("/mnt/user/%d", multiuser_get_user_id(uid)); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 715 | if (!(child = fork())) { |
Narayan Kamath | 02efdf5 | 2019-11-27 10:53:51 +0000 | [diff] [blame] | 716 | if (childProcess(storageSource.c_str(), userSource.c_str(), nsFd, de)) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 717 | _exit(0); |
Narayan Kamath | 02efdf5 | 2019-11-27 10:53:51 +0000 | [diff] [blame] | 718 | } else { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 719 | _exit(1); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 720 | } |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | if (child == -1) { |
| 724 | PLOG(ERROR) << "Failed to fork"; |
| 725 | goto next; |
| 726 | } else { |
| 727 | TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0)); |
| 728 | } |
| 729 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 730 | next: |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 731 | close(nsFd); |
| 732 | close(pidFd); |
| 733 | } |
| 734 | closedir(dir); |
| 735 | return 0; |
| 736 | } |
| 737 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 738 | int VolumeManager::reset() { |
| 739 | // Tear down all existing disks/volumes and start from a blank slate so |
| 740 | // newly connected framework hears all events. |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 741 | for (const auto& vol : mInternalEmulatedVolumes) { |
| 742 | vol->destroy(); |
Gao Xiang | d263da8 | 2017-08-14 11:32:13 +0800 | [diff] [blame] | 743 | } |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 744 | mInternalEmulatedVolumes.clear(); |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 745 | |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 746 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 747 | disk->destroy(); |
| 748 | disk->create(); |
| 749 | } |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 750 | updateVirtualDisk(); |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 751 | mAddedUsers.clear(); |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 752 | mStartedUsers.clear(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 753 | return 0; |
| 754 | } |
| 755 | |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 756 | // Can be called twice (sequentially) during shutdown. should be safe for that. |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 757 | int VolumeManager::shutdown() { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 758 | if (mInternalEmulatedVolumes.empty()) { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 759 | return 0; // already shutdown |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 760 | } |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 761 | android::vold::sSleepOnUnmount = false; |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 762 | for (const auto& vol : mInternalEmulatedVolumes) { |
| 763 | vol->destroy(); |
| 764 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 765 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 766 | disk->destroy(); |
| 767 | } |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 768 | |
| 769 | mInternalEmulatedVolumes.clear(); |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 770 | mStubVolumes.clear(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 771 | mDisks.clear(); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 772 | mPendingDisks.clear(); |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 773 | android::vold::sSleepOnUnmount = true; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 774 | return 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 777 | int VolumeManager::unmountAll() { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 778 | std::lock_guard<std::mutex> lock(mLock); |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 779 | ATRACE_NAME("VolumeManager::unmountAll()"); |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 780 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 781 | // First, try gracefully unmounting all known devices |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 782 | for (const auto& vol : mInternalEmulatedVolumes) { |
| 783 | vol->unmount(); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 784 | } |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 785 | for (const auto& stub : mStubVolumes) { |
| 786 | stub->unmount(); |
| 787 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 788 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 789 | disk->unmountAll(); |
| 790 | } |
| 791 | |
| 792 | // Worst case we might have some stale mounts lurking around, so |
| 793 | // force unmount those just to be safe. |
LongPing.WEI | 4f04606 | 2018-11-23 19:27:35 +0800 | [diff] [blame] | 794 | FILE* fp = setmntent("/proc/mounts", "re"); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 795 | if (fp == NULL) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 796 | PLOG(ERROR) << "Failed to open /proc/mounts"; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 797 | return -errno; |
| 798 | } |
| 799 | |
| 800 | // Some volumes can be stacked on each other, so force unmount in |
| 801 | // reverse order to give us the best chance of success. |
| 802 | std::list<std::string> toUnmount; |
| 803 | mntent* mentry; |
| 804 | while ((mentry = getmntent(fp)) != NULL) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 805 | auto test = std::string(mentry->mnt_dir); |
Mark Salyzyn | 86e81e7 | 2018-09-20 10:09:27 -0700 | [diff] [blame] | 806 | if ((StartsWith(test, "/mnt/") && |
| 807 | #ifdef __ANDROID_DEBUGGABLE__ |
| 808 | !StartsWith(test, "/mnt/scratch") && |
| 809 | #endif |
Martijn Coenen | b0e977a | 2020-01-11 19:24:26 +0100 | [diff] [blame] | 810 | !StartsWith(test, "/mnt/vendor") && !StartsWith(test, "/mnt/product") && |
| 811 | !StartsWith(test, "/mnt/installer")) || |
Mark Salyzyn | 86e81e7 | 2018-09-20 10:09:27 -0700 | [diff] [blame] | 812 | StartsWith(test, "/storage/")) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 813 | toUnmount.push_front(test); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 814 | } |
| 815 | } |
| 816 | endmntent(fp); |
| 817 | |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 818 | for (const auto& path : toUnmount) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 819 | LOG(DEBUG) << "Tearing down stale mount " << path; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 820 | android::vold::ForceUnmount(path); |
| 821 | } |
| 822 | |
| 823 | return 0; |
| 824 | } |
| 825 | |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 826 | static gid_t getAppDirGid(const std::string& appDir) { |
| 827 | // Create app-specific dirs with the correct UID/GID |
| 828 | gid_t gid = AID_MEDIA_RW; |
| 829 | if (!IsFilesystemSupported("sdcardfs")) { |
| 830 | if (appDir == android::vold::kAppDataDir) { |
| 831 | gid = AID_EXT_DATA_RW; |
| 832 | } else if (appDir == android::vold::kAppObbDir) { |
| 833 | gid = AID_EXT_OBB_RW; |
| 834 | } else if (appDir == android::vold::kAppMediaDir) { |
| 835 | gid = AID_MEDIA_RW; |
| 836 | } else { |
| 837 | gid = AID_MEDIA_RW; |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | return gid; |
| 842 | } |
| 843 | |
| 844 | static bool isValidAppDirRoot(const std::string& appDirRoot) { |
| 845 | return appDirRoot == android::vold::kAppDataDir || appDirRoot == android::vold::kAppMediaDir || |
| 846 | appDirRoot == android::vold::kAppObbDir; |
| 847 | } |
| 848 | |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 849 | int VolumeManager::setupAppDir(const std::string& path, const std::string& appDirRoot, |
| 850 | int32_t appUid) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 851 | // Only offer to create directories for paths managed by vold |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 852 | if (!StartsWith(path, "/storage/")) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 853 | LOG(ERROR) << "Failed to find mounted volume for " << path; |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 854 | return -EINVAL; |
| 855 | } |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 856 | |
Martijn Coenen | 0a7e992 | 2020-01-24 16:17:32 +0100 | [diff] [blame] | 857 | // Find the volume it belongs to |
| 858 | auto filter_fn = [&](const VolumeBase& vol) { |
| 859 | if (vol.getState() != VolumeBase::State::kMounted) { |
| 860 | // The volume must be mounted |
| 861 | return false; |
| 862 | } |
| 863 | if ((vol.getMountFlags() & VolumeBase::MountFlags::kVisible) == 0) { |
| 864 | // and visible |
| 865 | return false; |
| 866 | } |
| 867 | if (vol.getInternalPath().empty()) { |
| 868 | return false; |
| 869 | } |
| 870 | if (vol.getMountUserId() != USER_UNKNOWN && |
| 871 | vol.getMountUserId() != multiuser_get_user_id(appUid)) { |
| 872 | // The app dir must be created on a volume with the same user-id |
| 873 | return false; |
| 874 | } |
| 875 | if (!path.empty() && StartsWith(path, vol.getPath())) { |
| 876 | return true; |
| 877 | } |
| 878 | |
| 879 | return false; |
| 880 | }; |
| 881 | auto volume = findVolumeWithFilter(filter_fn); |
| 882 | if (volume == nullptr) { |
| 883 | LOG(ERROR) << "Failed to find mounted volume for " << path; |
| 884 | return -EINVAL; |
| 885 | } |
Zim | c59d774 | 2020-01-06 21:48:16 +0000 | [diff] [blame] | 886 | // Convert paths to lower filesystem paths to avoid making FUSE requests for these reasons: |
| 887 | // 1. A FUSE request from vold puts vold at risk of hanging if the FUSE daemon is down |
| 888 | // 2. The FUSE daemon prevents requests on /mnt/user/0/emulated/<userid != 0> and a request |
| 889 | // on /storage/emulated/10 means /mnt/user/0/emulated/10 |
Martijn Coenen | 0a7e992 | 2020-01-24 16:17:32 +0100 | [diff] [blame] | 890 | const std::string lowerPath = |
| 891 | volume->getInternalPath() + path.substr(volume->getPath().length()); |
| 892 | const std::string lowerAppDirRoot = |
| 893 | volume->getInternalPath() + appDirRoot.substr(volume->getPath().length()); |
Zim | c59d774 | 2020-01-06 21:48:16 +0000 | [diff] [blame] | 894 | |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 895 | // Do some sanity checking on the app dir (relative from root) |
| 896 | const std::string volumeRoot = volume->getRootPath(); // eg /data/media/0 |
| 897 | |
| 898 | // eg, if lowerAppDirRoot = /data/media/0/Android/data, this is /Android/data |
| 899 | const std::string relativeAppRoot = lowerAppDirRoot.substr(volumeRoot.length()); |
| 900 | if (!isValidAppDirRoot(relativeAppRoot)) { |
| 901 | LOG(ERROR) << path << " is not a valid application directory."; |
| 902 | return -EINVAL; |
| 903 | } |
| 904 | |
| 905 | // Make sure the Android/ directories exist and are setup correctly |
| 906 | int ret = PrepareAndroidDirs(volumeRoot); |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 907 | if (ret != 0) { |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 908 | LOG(ERROR) << "Failed to prepare Android/ directories."; |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 909 | return ret; |
| 910 | } |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 911 | |
| 912 | gid_t gid = getAppDirGid(relativeAppRoot); |
| 913 | return PrepareAppDirsFromRoot(lowerPath, lowerAppDirRoot, 0770, appUid, gid); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 914 | } |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 915 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 916 | int VolumeManager::createObb(const std::string& sourcePath, const std::string& sourceKey, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 917 | int32_t ownerGid, std::string* outVolId) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 918 | int id = mNextObbId++; |
| 919 | |
| 920 | auto vol = std::shared_ptr<android::vold::VolumeBase>( |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 921 | new android::vold::ObbVolume(id, sourcePath, sourceKey, ownerGid)); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 922 | vol->create(); |
| 923 | |
| 924 | mObbVolumes.push_back(vol); |
| 925 | *outVolId = vol->getId(); |
| 926 | return android::OK; |
| 927 | } |
| 928 | |
| 929 | int VolumeManager::destroyObb(const std::string& volId) { |
| 930 | auto i = mObbVolumes.begin(); |
| 931 | while (i != mObbVolumes.end()) { |
| 932 | if ((*i)->getId() == volId) { |
| 933 | (*i)->destroy(); |
| 934 | i = mObbVolumes.erase(i); |
| 935 | } else { |
| 936 | ++i; |
| 937 | } |
| 938 | } |
| 939 | return android::OK; |
| 940 | } |
| 941 | |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 942 | int VolumeManager::createStubVolume(const std::string& sourcePath, const std::string& mountPath, |
| 943 | const std::string& fsType, const std::string& fsUuid, |
| 944 | const std::string& fsLabel, std::string* outVolId) { |
| 945 | int id = mNextStubVolumeId++; |
| 946 | auto vol = std::shared_ptr<android::vold::VolumeBase>( |
| 947 | new android::vold::StubVolume(id, sourcePath, mountPath, fsType, fsUuid, fsLabel)); |
| 948 | vol->create(); |
| 949 | |
| 950 | mStubVolumes.push_back(vol); |
| 951 | *outVolId = vol->getId(); |
| 952 | return android::OK; |
| 953 | } |
| 954 | |
| 955 | int VolumeManager::destroyStubVolume(const std::string& volId) { |
| 956 | auto i = mStubVolumes.begin(); |
| 957 | while (i != mStubVolumes.end()) { |
| 958 | if ((*i)->getId() == volId) { |
| 959 | (*i)->destroy(); |
| 960 | i = mStubVolumes.erase(i); |
| 961 | } else { |
| 962 | ++i; |
| 963 | } |
| 964 | } |
| 965 | return android::OK; |
| 966 | } |
| 967 | |
Risan | 8f6198d | 2018-10-26 20:56:45 -0600 | [diff] [blame] | 968 | int VolumeManager::mountAppFuse(uid_t uid, int mountId, unique_fd* device_fd) { |
Risan | ac02a48 | 2018-10-31 21:59:47 -0600 | [diff] [blame] | 969 | return android::vold::MountAppFuse(uid, mountId, device_fd); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 970 | } |
| 971 | |
Risan | 8f6198d | 2018-10-26 20:56:45 -0600 | [diff] [blame] | 972 | int VolumeManager::unmountAppFuse(uid_t uid, int mountId) { |
Risan | ac02a48 | 2018-10-31 21:59:47 -0600 | [diff] [blame] | 973 | return android::vold::UnmountAppFuse(uid, mountId); |
Risan | 8f6198d | 2018-10-26 20:56:45 -0600 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | int VolumeManager::openAppFuseFile(uid_t uid, int mountId, int fileId, int flags) { |
Risan | ac02a48 | 2018-10-31 21:59:47 -0600 | [diff] [blame] | 977 | return android::vold::OpenAppFuseFile(uid, mountId, fileId, flags); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 978 | } |