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