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