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> |
Ricky Wai | e96b34f | 2020-05-07 16:01:33 +0100 | [diff] [blame] | 22 | #include <limits.h> |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 23 | #include <mntent.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <sys/ioctl.h> |
| 28 | #include <sys/mount.h> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 29 | #include <sys/stat.h> |
Elliott Hughes | 0e08e84 | 2017-05-18 09:08:24 -0700 | [diff] [blame] | 30 | #include <sys/sysmacros.h> |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 31 | #include <sys/types.h> |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 32 | #include <sys/wait.h> |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 33 | #include <unistd.h> |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 34 | #include <array> |
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 | |
Paul Lawrence | d73dfd4 | 2023-08-11 10:27:24 -0700 | [diff] [blame] | 38 | #include <android-base/file.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> |
Narayan Kamath | 02efdf5 | 2019-11-27 10:53:51 +0000 | [diff] [blame] | 44 | #include <async_safe/log.h> |
Jeff Vander Stoep | 5889083 | 2017-10-23 17:12:31 -0700 | [diff] [blame] | 45 | |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 46 | #include <cutils/fs.h> |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 47 | #include <utils/Trace.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 48 | |
Robert Craig | b9e3ba5 | 2014-02-04 10:53:00 -0500 | [diff] [blame] | 49 | #include <selinux/android.h> |
| 50 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 51 | #include <sysutils/NetlinkEvent.h> |
| 52 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 53 | #include <private/android_filesystem_config.h> |
| 54 | |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 55 | #include <fscrypt/fscrypt.h> |
Paul Lawrence | d73dfd4 | 2023-08-11 10:27:24 -0700 | [diff] [blame] | 56 | #include <libdm/dm.h> |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 57 | |
Risan | ac02a48 | 2018-10-31 21:59:47 -0600 | [diff] [blame] | 58 | #include "AppFuseUtil.h" |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 59 | #include "FsCrypt.h" |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 60 | #include "Loop.h" |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 61 | #include "NetlinkManager.h" |
| 62 | #include "Process.h" |
| 63 | #include "Utils.h" |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 64 | #include "VoldNativeService.h" |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 65 | #include "VoldUtil.h" |
| 66 | #include "VolumeManager.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" |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 71 | #include "model/PrivateVolume.h" |
himanshuz | 0ad0862 | 2023-07-27 21:15:00 +0000 | [diff] [blame] | 72 | #include "model/PublicVolume.h" |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 73 | #include "model/StubVolume.h" |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 74 | |
Sudheer Shanka | f9b38a5 | 2019-02-14 19:09:51 +0000 | [diff] [blame] | 75 | using android::OK; |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 76 | using android::base::GetBoolProperty; |
Mark Salyzyn | 86e81e7 | 2018-09-20 10:09:27 -0700 | [diff] [blame] | 77 | using android::base::StartsWith; |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 78 | using android::base::StringAppendF; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 79 | using android::base::StringPrintf; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 80 | using android::base::unique_fd; |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 81 | using android::vold::BindMount; |
Sudheer Shanka | f9b38a5 | 2019-02-14 19:09:51 +0000 | [diff] [blame] | 82 | using android::vold::CreateDir; |
Sudheer Shanka | 30df1c6 | 2019-02-22 17:03:02 -0800 | [diff] [blame] | 83 | using android::vold::DeleteDirContents; |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 84 | using android::vold::DeleteDirContentsAndDir; |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 85 | using android::vold::EnsureDirExists; |
Martijn Coenen | 73e3010 | 2022-07-12 08:11:02 +0000 | [diff] [blame] | 86 | using android::vold::GetFuseMountPathForUser; |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 87 | using android::vold::IsFilesystemSupported; |
Daniel Rosenberg | f36bddd | 2020-05-11 22:58:42 -0700 | [diff] [blame] | 88 | using android::vold::IsSdcardfsUsed; |
Alistair Delva | ff1fc9b | 2020-05-14 16:35:03 -0700 | [diff] [blame] | 89 | using android::vold::IsVirtioBlkDevice; |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 90 | using android::vold::PrepareAndroidDirs; |
Martijn Coenen | 04bb17f | 2020-02-10 23:48:11 +0100 | [diff] [blame] | 91 | using android::vold::PrepareAppDirFromRoot; |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 92 | using android::vold::PrivateVolume; |
himanshuz | 0ad0862 | 2023-07-27 21:15:00 +0000 | [diff] [blame] | 93 | using android::vold::PublicVolume; |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 94 | using android::vold::Symlink; |
| 95 | using android::vold::Unlink; |
| 96 | using android::vold::UnmountTree; |
Sudheer Shanka | 03992e3 | 2018-12-12 12:43:38 -0800 | [diff] [blame] | 97 | using android::vold::VoldNativeService; |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 98 | using android::vold::VolumeBase; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 99 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 100 | static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk"; |
| 101 | |
| 102 | static const char* kPropVirtualDisk = "persist.sys.virtual_disk"; |
| 103 | |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 104 | static const std::string kEmptyString(""); |
| 105 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 106 | /* 512MiB is large enough for testing purposes */ |
| 107 | static const unsigned int kSizeVirtualDisk = 536870912; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 108 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 109 | static const unsigned int kMajorBlockMmc = 179; |
| 110 | |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 111 | using ScanProcCallback = bool(*)(uid_t uid, pid_t pid, int nsFd, const char* name, void* params); |
| 112 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 113 | VolumeManager* VolumeManager::sInstance = NULL; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 114 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 115 | VolumeManager* VolumeManager::Instance() { |
| 116 | if (!sInstance) sInstance = new VolumeManager(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 117 | return sInstance; |
| 118 | } |
| 119 | |
| 120 | VolumeManager::VolumeManager() { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 121 | mDebug = false; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 122 | mNextObbId = 0; |
Risan | 82e90de | 2020-02-04 16:07:21 +0900 | [diff] [blame] | 123 | mNextStubId = 0; |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 124 | // For security reasons, assume that a secure keyguard is |
| 125 | // showing until we hear otherwise |
| 126 | mSecureKeyguardShowing = true; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 129 | VolumeManager::~VolumeManager() {} |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 130 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 131 | int VolumeManager::updateVirtualDisk() { |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 132 | ATRACE_NAME("VolumeManager::updateVirtualDisk"); |
Mark Salyzyn | 86e81e7 | 2018-09-20 10:09:27 -0700 | [diff] [blame] | 133 | if (GetBoolProperty(kPropVirtualDisk, false)) { |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 134 | if (access(kPathVirtualDisk, F_OK) != 0) { |
| 135 | Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512); |
| 136 | } |
| 137 | |
| 138 | if (mVirtualDisk == nullptr) { |
| 139 | if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) { |
| 140 | LOG(ERROR) << "Failed to create virtual disk"; |
| 141 | return -1; |
| 142 | } |
| 143 | |
| 144 | struct stat buf; |
| 145 | if (stat(mVirtualDiskPath.c_str(), &buf) < 0) { |
| 146 | PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath; |
| 147 | return -1; |
| 148 | } |
| 149 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 150 | auto disk = new android::vold::Disk( |
| 151 | "virtual", buf.st_rdev, "virtual", |
| 152 | android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd); |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 153 | mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 154 | handleDiskAdded(mVirtualDisk); |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 155 | } |
| 156 | } else { |
| 157 | if (mVirtualDisk != nullptr) { |
| 158 | dev_t device = mVirtualDisk->getDevice(); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 159 | handleDiskRemoved(device); |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 160 | |
| 161 | Loop::destroyByDevice(mVirtualDiskPath.c_str()); |
| 162 | mVirtualDisk = nullptr; |
| 163 | } |
| 164 | |
| 165 | if (access(kPathVirtualDisk, F_OK) == 0) { |
| 166 | unlink(kPathVirtualDisk); |
| 167 | } |
| 168 | } |
| 169 | return 0; |
| 170 | } |
| 171 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 172 | int VolumeManager::setDebug(bool enable) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 173 | mDebug = enable; |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 174 | return 0; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 175 | } |
| 176 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 177 | int VolumeManager::start() { |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 178 | ATRACE_NAME("VolumeManager::start"); |
| 179 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 180 | // Always start from a clean slate by unmounting everything in |
| 181 | // directories that we own, in case we crashed. |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 182 | unmountAll(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 183 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 184 | Loop::destroyAll(); |
| 185 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 186 | // Assume that we always have an emulated volume on internal |
| 187 | // storage; the framework will decide if it should be mounted. |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 188 | CHECK(mInternalEmulatedVolumes.empty()); |
| 189 | |
| 190 | auto vol = std::shared_ptr<android::vold::VolumeBase>( |
| 191 | new android::vold::EmulatedVolume("/data/media", 0)); |
| 192 | vol->setMountUserId(0); |
| 193 | vol->create(); |
| 194 | mInternalEmulatedVolumes.push_back(vol); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 195 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 196 | // Consider creating a virtual disk |
| 197 | updateVirtualDisk(); |
| 198 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 199 | return 0; |
| 200 | } |
| 201 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 202 | void VolumeManager::handleBlockEvent(NetlinkEvent* evt) { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 203 | std::lock_guard<std::mutex> lock(mLock); |
| 204 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 205 | if (mDebug) { |
Sudheer Shanka | 4b6ca4e | 2018-09-21 10:54:54 -0700 | [diff] [blame] | 206 | LOG(DEBUG) << "----------------"; |
| 207 | LOG(DEBUG) << "handleBlockEvent with action " << (int)evt->getAction(); |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 208 | evt->dump(); |
| 209 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 210 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 211 | std::string eventPath(evt->findParam("DEVPATH") ? evt->findParam("DEVPATH") : ""); |
| 212 | std::string devType(evt->findParam("DEVTYPE") ? evt->findParam("DEVTYPE") : ""); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 213 | |
| 214 | if (devType != "disk") return; |
| 215 | |
Jeff Sharkey | 95440eb | 2017-09-18 18:19:28 -0600 | [diff] [blame] | 216 | int major = std::stoi(evt->findParam("MAJOR")); |
| 217 | int minor = std::stoi(evt->findParam("MINOR")); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 218 | dev_t device = makedev(major, minor); |
| 219 | |
| 220 | switch (evt->getAction()) { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 221 | case NetlinkEvent::Action::kAdd: { |
| 222 | for (const auto& source : mDiskSources) { |
| 223 | if (source->matches(eventPath)) { |
| 224 | // For now, assume that MMC and virtio-blk (the latter is |
Alistair Delva | ff1fc9b | 2020-05-14 16:35:03 -0700 | [diff] [blame] | 225 | // specific to virtual platforms; see Utils.cpp for details) |
| 226 | // devices are SD, and that everything else is USB |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 227 | int flags = source->getFlags(); |
Alistair Delva | ff1fc9b | 2020-05-14 16:35:03 -0700 | [diff] [blame] | 228 | if (major == kMajorBlockMmc || IsVirtioBlkDevice(major)) { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 229 | flags |= android::vold::Disk::Flags::kSd; |
| 230 | } else { |
| 231 | flags |= android::vold::Disk::Flags::kUsb; |
| 232 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 233 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 234 | auto disk = |
| 235 | new android::vold::Disk(eventPath, device, source->getNickname(), flags); |
| 236 | handleDiskAdded(std::shared_ptr<android::vold::Disk>(disk)); |
| 237 | break; |
| 238 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 239 | } |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 240 | break; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 241 | } |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 242 | case NetlinkEvent::Action::kChange: { |
Nikita Ioffe | b881fc4 | 2021-07-28 02:58:57 +0100 | [diff] [blame] | 243 | LOG(VERBOSE) << "Disk at " << major << ":" << minor << " changed"; |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 244 | handleDiskChanged(device); |
| 245 | break; |
| 246 | } |
| 247 | case NetlinkEvent::Action::kRemove: { |
| 248 | handleDiskRemoved(device); |
| 249 | break; |
| 250 | } |
| 251 | default: { |
| 252 | LOG(WARNING) << "Unexpected block event action " << (int)evt->getAction(); |
| 253 | break; |
| 254 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 258 | void VolumeManager::handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk) { |
| 259 | // For security reasons, if secure keyguard is showing, wait |
| 260 | // until the user unlocks the device to actually touch it |
Martijn Coenen | cf5916f | 2020-01-03 14:36:45 +0100 | [diff] [blame] | 261 | // Additionally, wait until user 0 is actually started, since we need |
| 262 | // the user to be up before we can mount a FUSE daemon to handle the disk. |
| 263 | bool userZeroStarted = mStartedUsers.find(0) != mStartedUsers.end(); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 264 | if (mSecureKeyguardShowing) { |
| 265 | LOG(INFO) << "Found disk at " << disk->getEventPath() |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 266 | << " but delaying scan due to secure keyguard"; |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 267 | mPendingDisks.push_back(disk); |
Martijn Coenen | cf5916f | 2020-01-03 14:36:45 +0100 | [diff] [blame] | 268 | } else if (!userZeroStarted) { |
| 269 | LOG(INFO) << "Found disk at " << disk->getEventPath() |
| 270 | << " but delaying scan due to user zero not having started"; |
| 271 | mPendingDisks.push_back(disk); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 272 | } else { |
| 273 | disk->create(); |
| 274 | mDisks.push_back(disk); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | void VolumeManager::handleDiskChanged(dev_t device) { |
| 279 | for (const auto& disk : mDisks) { |
| 280 | if (disk->getDevice() == device) { |
| 281 | disk->readMetadata(); |
| 282 | disk->readPartitions(); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | // For security reasons, we ignore all pending disks, since |
| 287 | // we'll scan them once the device is unlocked |
| 288 | } |
| 289 | |
| 290 | void VolumeManager::handleDiskRemoved(dev_t device) { |
| 291 | auto i = mDisks.begin(); |
| 292 | while (i != mDisks.end()) { |
| 293 | if ((*i)->getDevice() == device) { |
| 294 | (*i)->destroy(); |
| 295 | i = mDisks.erase(i); |
| 296 | } else { |
| 297 | ++i; |
| 298 | } |
| 299 | } |
| 300 | auto j = mPendingDisks.begin(); |
| 301 | while (j != mPendingDisks.end()) { |
| 302 | if ((*j)->getDevice() == device) { |
| 303 | j = mPendingDisks.erase(j); |
| 304 | } else { |
| 305 | ++j; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 310 | void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) { |
Wei Wang | 6b455c2 | 2017-01-20 11:52:33 -0800 | [diff] [blame] | 311 | std::lock_guard<std::mutex> lock(mLock); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 312 | mDiskSources.push_back(diskSource); |
| 313 | } |
| 314 | |
| 315 | std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) { |
| 316 | for (auto disk : mDisks) { |
| 317 | if (disk->getId() == id) { |
| 318 | return disk; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 321 | return nullptr; |
| 322 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 323 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 324 | std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 325 | for (const auto& vol : mInternalEmulatedVolumes) { |
| 326 | if (vol->getId() == id) { |
| 327 | return vol; |
| 328 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 329 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 330 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 331 | auto vol = disk->findVolume(id); |
| 332 | if (vol != nullptr) { |
| 333 | return vol; |
| 334 | } |
| 335 | } |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 336 | for (const auto& vol : mObbVolumes) { |
| 337 | if (vol->getId() == id) { |
| 338 | return vol; |
| 339 | } |
| 340 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 341 | return nullptr; |
| 342 | } |
| 343 | |
Greg Kaiser | 2bc201e | 2018-12-18 08:42:08 -0800 | [diff] [blame] | 344 | void VolumeManager::listVolumes(android::vold::VolumeBase::Type type, |
| 345 | std::list<std::string>& list) const { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 346 | list.clear(); |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 347 | for (const auto& disk : mDisks) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 348 | disk->listVolumes(type, list); |
| 349 | } |
| 350 | } |
| 351 | |
Eric Biggers | 7862729 | 2023-08-01 22:36:55 +0000 | [diff] [blame] | 352 | bool VolumeManager::forgetPartition(const std::string& partGuid, const std::string& fsUuid) { |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 353 | std::string normalizedGuid; |
| 354 | if (android::vold::NormalizeHex(partGuid, normalizedGuid)) { |
| 355 | LOG(WARNING) << "Invalid GUID " << partGuid; |
Eric Biggers | 7862729 | 2023-08-01 22:36:55 +0000 | [diff] [blame] | 356 | return false; |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | std::string keyPath = android::vold::BuildKeyPath(normalizedGuid); |
| 360 | if (unlink(keyPath.c_str()) != 0) { |
| 361 | LOG(ERROR) << "Failed to unlink " << keyPath; |
Eric Biggers | 7862729 | 2023-08-01 22:36:55 +0000 | [diff] [blame] | 362 | return false; |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 363 | } |
Eric Biggers | 7862729 | 2023-08-01 22:36:55 +0000 | [diff] [blame] | 364 | return true; |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 367 | void VolumeManager::destroyEmulatedVolumesForUser(userid_t userId) { |
| 368 | // Destroy and remove all unstacked EmulatedVolumes for the user |
| 369 | auto i = mInternalEmulatedVolumes.begin(); |
| 370 | while (i != mInternalEmulatedVolumes.end()) { |
| 371 | auto vol = *i; |
| 372 | if (vol->getMountUserId() == userId) { |
| 373 | vol->destroy(); |
| 374 | i = mInternalEmulatedVolumes.erase(i); |
| 375 | } else { |
| 376 | i++; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | // Destroy and remove all stacked EmulatedVolumes for the user on each mounted private volume |
| 381 | std::list<std::string> private_vols; |
| 382 | listVolumes(VolumeBase::Type::kPrivate, private_vols); |
| 383 | for (const std::string& id : private_vols) { |
| 384 | PrivateVolume* pvol = static_cast<PrivateVolume*>(findVolume(id).get()); |
| 385 | std::list<std::shared_ptr<VolumeBase>> vols_to_remove; |
| 386 | if (pvol->getState() == VolumeBase::State::kMounted) { |
| 387 | for (const auto& vol : pvol->getVolumes()) { |
| 388 | if (vol->getMountUserId() == userId) { |
| 389 | vols_to_remove.push_back(vol); |
| 390 | } |
| 391 | } |
| 392 | for (const auto& vol : vols_to_remove) { |
| 393 | vol->destroy(); |
| 394 | pvol->removeVolume(vol); |
| 395 | } |
| 396 | } // else EmulatedVolumes will be destroyed on VolumeBase#unmount |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | void VolumeManager::createEmulatedVolumesForUser(userid_t userId) { |
| 401 | // Create unstacked EmulatedVolumes for the user |
| 402 | auto vol = std::shared_ptr<android::vold::VolumeBase>( |
| 403 | new android::vold::EmulatedVolume("/data/media", userId)); |
| 404 | vol->setMountUserId(userId); |
| 405 | mInternalEmulatedVolumes.push_back(vol); |
| 406 | vol->create(); |
| 407 | |
| 408 | // Create stacked EmulatedVolumes for the user on each PrivateVolume |
| 409 | std::list<std::string> private_vols; |
| 410 | listVolumes(VolumeBase::Type::kPrivate, private_vols); |
| 411 | for (const std::string& id : private_vols) { |
| 412 | PrivateVolume* pvol = static_cast<PrivateVolume*>(findVolume(id).get()); |
| 413 | if (pvol->getState() == VolumeBase::State::kMounted) { |
| 414 | auto evol = |
| 415 | std::shared_ptr<android::vold::VolumeBase>(new android::vold::EmulatedVolume( |
| 416 | pvol->getPath() + "/media", pvol->getRawDevice(), pvol->getFsUuid(), |
| 417 | userId)); |
| 418 | evol->setMountUserId(userId); |
| 419 | pvol->addVolume(evol); |
| 420 | evol->create(); |
| 421 | } // else EmulatedVolumes will be created per user when on PrivateVolume#doMount |
| 422 | } |
| 423 | } |
| 424 | |
Martijn Coenen | 73e3010 | 2022-07-12 08:11:02 +0000 | [diff] [blame] | 425 | userid_t VolumeManager::getSharedStorageUser(userid_t userId) { |
| 426 | if (mSharedStorageUser.find(userId) == mSharedStorageUser.end()) { |
| 427 | return USER_UNKNOWN; |
| 428 | } |
| 429 | return mSharedStorageUser.at(userId); |
| 430 | } |
| 431 | |
| 432 | int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber, |
| 433 | userid_t sharesStorageWithUserId) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 434 | LOG(INFO) << "onUserAdded: " << userId; |
| 435 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 436 | mAddedUsers[userId] = userSerialNumber; |
Martijn Coenen | 73e3010 | 2022-07-12 08:11:02 +0000 | [diff] [blame] | 437 | if (sharesStorageWithUserId != USER_UNKNOWN) { |
| 438 | mSharedStorageUser[userId] = sharesStorageWithUserId; |
| 439 | } |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | int VolumeManager::onUserRemoved(userid_t userId) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 444 | LOG(INFO) << "onUserRemoved: " << userId; |
| 445 | |
Zim | 2d45d9b | 2019-11-14 16:19:05 +0000 | [diff] [blame] | 446 | onUserStopped(userId); |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 447 | mAddedUsers.erase(userId); |
Martijn Coenen | 73e3010 | 2022-07-12 08:11:02 +0000 | [diff] [blame] | 448 | mSharedStorageUser.erase(userId); |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 449 | return 0; |
| 450 | } |
| 451 | |
Sudheer Shanka | ebce4cc | 2019-04-29 10:46:35 -0700 | [diff] [blame] | 452 | int VolumeManager::onUserStarted(userid_t userId) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 453 | LOG(INFO) << "onUserStarted: " << userId; |
| 454 | |
Zim | 2d45d9b | 2019-11-14 16:19:05 +0000 | [diff] [blame] | 455 | if (mStartedUsers.find(userId) == mStartedUsers.end()) { |
| 456 | createEmulatedVolumesForUser(userId); |
himanshuz | 0ad0862 | 2023-07-27 21:15:00 +0000 | [diff] [blame] | 457 | std::list<std::string> public_vols; |
| 458 | listVolumes(VolumeBase::Type::kPublic, public_vols); |
| 459 | for (const std::string& id : public_vols) { |
| 460 | PublicVolume* pvol = static_cast<PublicVolume*>(findVolume(id).get()); |
| 461 | if (pvol->getState() != VolumeBase::State::kMounted) { |
| 462 | continue; |
| 463 | } |
| 464 | if (pvol->isVisible() == 0) { |
| 465 | continue; |
| 466 | } |
| 467 | userid_t mountUserId = pvol->getMountUserId(); |
| 468 | if (userId == mountUserId) { |
| 469 | // No need to bind mount for the user that owns the mount |
| 470 | continue; |
| 471 | } |
| 472 | if (mountUserId != VolumeManager::Instance()->getSharedStorageUser(userId)) { |
| 473 | // No need to bind if the user does not share storage with the mount owner |
| 474 | continue; |
| 475 | } |
Himanshu Gupta | 4f17969 | 2024-05-06 11:40:19 +0100 | [diff] [blame] | 476 | // Create mount directory for the user as there is a chance that no other Volume is |
| 477 | // mounted for the user (ex: if the user is just started), so /mnt/user/user_id does |
| 478 | // not exist yet. |
| 479 | auto mountDirStatus = android::vold::PrepareMountDirForUser(userId); |
| 480 | if (mountDirStatus != OK) { |
| 481 | LOG(ERROR) << "Failed to create Mount Directory for user " << userId; |
| 482 | } |
himanshuz | 0ad0862 | 2023-07-27 21:15:00 +0000 | [diff] [blame] | 483 | auto bindMountStatus = pvol->bindMountForUser(userId); |
| 484 | if (bindMountStatus != OK) { |
| 485 | LOG(ERROR) << "Bind Mounting Public Volume: " << pvol << " for user: " << userId |
| 486 | << "Failed. Error: " << bindMountStatus; |
| 487 | } |
| 488 | } |
Zim | 2d45d9b | 2019-11-14 16:19:05 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 491 | mStartedUsers.insert(userId); |
Martijn Coenen | cf5916f | 2020-01-03 14:36:45 +0100 | [diff] [blame] | 492 | |
| 493 | createPendingDisksIfNeeded(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 494 | return 0; |
| 495 | } |
| 496 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 497 | int VolumeManager::onUserStopped(userid_t userId) { |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 498 | LOG(VERBOSE) << "onUserStopped: " << userId; |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 499 | |
Zim | 2d45d9b | 2019-11-14 16:19:05 +0000 | [diff] [blame] | 500 | if (mStartedUsers.find(userId) != mStartedUsers.end()) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 501 | destroyEmulatedVolumesForUser(userId); |
| 502 | } |
| 503 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 504 | mStartedUsers.erase(userId); |
Sudheer Shanka | fa6a174 | 2018-10-04 16:26:22 -0700 | [diff] [blame] | 505 | return 0; |
| 506 | } |
| 507 | |
Martijn Coenen | cf5916f | 2020-01-03 14:36:45 +0100 | [diff] [blame] | 508 | void VolumeManager::createPendingDisksIfNeeded() { |
| 509 | bool userZeroStarted = mStartedUsers.find(0) != mStartedUsers.end(); |
| 510 | if (!mSecureKeyguardShowing && userZeroStarted) { |
| 511 | // Now that secure keyguard has been dismissed and user 0 has |
| 512 | // started, process any pending disks |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 513 | for (const auto& disk : mPendingDisks) { |
| 514 | disk->create(); |
| 515 | mDisks.push_back(disk); |
| 516 | } |
| 517 | mPendingDisks.clear(); |
| 518 | } |
Martijn Coenen | cf5916f | 2020-01-03 14:36:45 +0100 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | int VolumeManager::onSecureKeyguardStateChanged(bool isShowing) { |
| 522 | mSecureKeyguardShowing = isShowing; |
| 523 | createPendingDisksIfNeeded(); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 524 | return 0; |
| 525 | } |
| 526 | |
Narayan Kamath | 02efdf5 | 2019-11-27 10:53:51 +0000 | [diff] [blame] | 527 | // This code is executed after a fork so it's very important that the set of |
| 528 | // methods we call here is strictly limited. |
| 529 | // |
| 530 | // TODO: Get rid of this guesswork altogether and instead exec a process |
| 531 | // immediately after fork to do our bindding for us. |
| 532 | static bool childProcess(const char* storageSource, const char* userSource, int nsFd, |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 533 | const char* name) { |
Narayan Kamath | 02efdf5 | 2019-11-27 10:53:51 +0000 | [diff] [blame] | 534 | if (setns(nsFd, CLONE_NEWNS) != 0) { |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 535 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to setns for %s :%s", name, |
Narayan Kamath | 02efdf5 | 2019-11-27 10:53:51 +0000 | [diff] [blame] | 536 | strerror(errno)); |
| 537 | return false; |
| 538 | } |
| 539 | |
| 540 | // NOTE: Inlined from vold::UnmountTree here to avoid using PLOG methods and |
| 541 | // to also protect against future changes that may cause issues across a |
| 542 | // fork. |
| 543 | if (TEMP_FAILURE_RETRY(umount2("/storage/", MNT_DETACH)) < 0 && errno != EINVAL && |
| 544 | errno != ENOENT) { |
| 545 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to unmount /storage/ :%s", |
| 546 | strerror(errno)); |
| 547 | return false; |
| 548 | } |
| 549 | |
| 550 | if (TEMP_FAILURE_RETRY(mount(storageSource, "/storage", NULL, MS_BIND | MS_REC, NULL)) == -1) { |
| 551 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to mount %s for %s :%s", |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 552 | storageSource, name, strerror(errno)); |
Narayan Kamath | 02efdf5 | 2019-11-27 10:53:51 +0000 | [diff] [blame] | 553 | return false; |
| 554 | } |
| 555 | |
| 556 | if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL, MS_REC | MS_SLAVE, NULL)) == -1) { |
| 557 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 558 | "Failed to set MS_SLAVE to /storage for %s :%s", name, |
Narayan Kamath | 02efdf5 | 2019-11-27 10:53:51 +0000 | [diff] [blame] | 559 | strerror(errno)); |
| 560 | return false; |
| 561 | } |
| 562 | |
| 563 | if (TEMP_FAILURE_RETRY(mount(userSource, "/storage/self", NULL, MS_BIND, NULL)) == -1) { |
| 564 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to mount %s for %s :%s", |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 565 | userSource, name, strerror(errno)); |
Narayan Kamath | 02efdf5 | 2019-11-27 10:53:51 +0000 | [diff] [blame] | 566 | return false; |
| 567 | } |
| 568 | |
| 569 | return true; |
| 570 | } |
| 571 | |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 572 | // Fork the process and remount storage |
| 573 | bool forkAndRemountChild(uid_t uid, pid_t pid, int nsFd, const char* name, void* params) { |
| 574 | int32_t mountMode = *static_cast<int32_t*>(params); |
| 575 | std::string userSource; |
| 576 | std::string storageSource; |
| 577 | pid_t child; |
| 578 | // Need to fix these paths to account for when sdcardfs is gone |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 579 | switch (mountMode) { |
| 580 | case VoldNativeService::REMOUNT_MODE_NONE: |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 581 | return true; |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 582 | case VoldNativeService::REMOUNT_MODE_DEFAULT: |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 583 | storageSource = "/mnt/runtime/default"; |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 584 | break; |
Abhijeet Kaur | e715ec1 | 2021-02-24 12:33:25 +0000 | [diff] [blame] | 585 | case VoldNativeService::REMOUNT_MODE_ANDROID_WRITABLE: |
Sudheer Shanka | a05ea74 | 2019-04-12 13:55:28 -0700 | [diff] [blame] | 586 | case VoldNativeService::REMOUNT_MODE_INSTALLER: |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 587 | storageSource = "/mnt/runtime/write"; |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 588 | break; |
Zim | 981222f | 2019-09-09 10:24:44 +0100 | [diff] [blame] | 589 | case VoldNativeService::REMOUNT_MODE_PASS_THROUGH: |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 590 | return true; |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 591 | default: |
| 592 | PLOG(ERROR) << "Unknown mode " << std::to_string(mountMode); |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 593 | return false; |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 594 | } |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 595 | LOG(DEBUG) << "Remounting " << uid << " as " << storageSource; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 596 | |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 597 | // Fork a child to mount user-specific symlink helper into place |
| 598 | userSource = StringPrintf("/mnt/user/%d", multiuser_get_user_id(uid)); |
| 599 | if (!(child = fork())) { |
| 600 | if (childProcess(storageSource.c_str(), userSource.c_str(), nsFd, name)) { |
| 601 | _exit(0); |
| 602 | } else { |
| 603 | _exit(1); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | if (child == -1) { |
| 608 | PLOG(ERROR) << "Failed to fork"; |
| 609 | return false; |
| 610 | } else { |
| 611 | TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0)); |
| 612 | } |
| 613 | return true; |
| 614 | } |
| 615 | |
| 616 | // Helper function to scan all processes in /proc and call the callback if: |
| 617 | // 1). pid belongs to an app process |
| 618 | // 2). If input uid is 0 or it matches the process uid |
| 619 | // 3). If userId is not -1 or userId matches the process userId |
| 620 | bool scanProcProcesses(uid_t uid, userid_t userId, ScanProcCallback callback, void* params) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 621 | DIR* dir; |
| 622 | struct dirent* de; |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 623 | std::string rootName; |
| 624 | std::string pidName; |
Jooyung Han | a9f2171 | 2023-08-25 15:38:12 +0900 | [diff] [blame] | 625 | std::string exeName; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 626 | int pidFd; |
| 627 | int nsFd; |
| 628 | struct stat sb; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 629 | |
| 630 | if (!(dir = opendir("/proc"))) { |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 631 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to opendir"); |
| 632 | return false; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | // Figure out root namespace to compare against below |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 636 | if (!android::vold::Readlinkat(dirfd(dir), "1/ns/mnt", &rootName)) { |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 637 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to read root namespace"); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 638 | closedir(dir); |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 639 | return false; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 642 | async_safe_format_log(ANDROID_LOG_INFO, "vold", "Start scanning all processes"); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 643 | // Poke through all running PIDs look for apps running as UID |
| 644 | while ((de = readdir(dir))) { |
Jeff Vander Stoep | 5889083 | 2017-10-23 17:12:31 -0700 | [diff] [blame] | 645 | pid_t pid; |
| 646 | if (de->d_type != DT_DIR) continue; |
| 647 | if (!android::base::ParseInt(de->d_name, &pid)) continue; |
| 648 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 649 | pidFd = -1; |
| 650 | nsFd = -1; |
| 651 | |
| 652 | pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
| 653 | if (pidFd < 0) { |
| 654 | goto next; |
| 655 | } |
| 656 | if (fstat(pidFd, &sb) != 0) { |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 657 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to stat %s", de->d_name); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 658 | goto next; |
| 659 | } |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 660 | if (uid != 0 && sb.st_uid != uid) { |
| 661 | goto next; |
| 662 | } |
| 663 | if (userId != static_cast<userid_t>(-1) && multiuser_get_user_id(sb.st_uid) != userId) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 664 | goto next; |
| 665 | } |
| 666 | |
| 667 | // Matches so far, but refuse to touch if in root namespace |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 668 | if (!android::vold::Readlinkat(pidFd, "ns/mnt", &pidName)) { |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 669 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", |
| 670 | "Failed to read namespacefor %s", de->d_name); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 671 | goto next; |
| 672 | } |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 673 | if (rootName == pidName) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 674 | goto next; |
| 675 | } |
| 676 | |
Jooyung Han | a9f2171 | 2023-08-25 15:38:12 +0900 | [diff] [blame] | 677 | // Some early native processes have mount namespaces that are different |
| 678 | // from that of the init. Therefore, above check can't filter them out. |
| 679 | // Since the propagation type of / is 'shared', unmounting /storage |
| 680 | // for the early native processes affects other processes including |
| 681 | // init. Filter out such processes by skipping if a process is a |
| 682 | // non-Java process whose UID is < AID_APP_START. (The UID condition |
| 683 | // is required to not filter out child processes spawned by apps.) |
| 684 | if (!android::vold::Readlinkat(pidFd, "exe", &exeName)) { |
| 685 | goto next; |
| 686 | } |
| 687 | if (!StartsWith(exeName, "/system/bin/app_process") && sb.st_uid < AID_APP_START) { |
| 688 | goto next; |
Jiyong Park | 8d21c92 | 2019-01-04 13:35:25 +0900 | [diff] [blame] | 689 | } |
| 690 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 691 | // We purposefully leave the namespace open across the fork |
Nick Kralevich | e7e89ac | 2019-03-29 16:03:51 -0700 | [diff] [blame] | 692 | // NOLINTNEXTLINE(android-cloexec-open): Deliberately not O_CLOEXEC |
| 693 | nsFd = openat(pidFd, "ns/mnt", O_RDONLY); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 694 | if (nsFd < 0) { |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 695 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", |
| 696 | "Failed to open namespace for %s", de->d_name); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 697 | goto next; |
| 698 | } |
| 699 | |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 700 | if (!callback(sb.st_uid, pid, nsFd, de->d_name, params)) { |
| 701 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed in callback"); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 704 | next: |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 705 | close(nsFd); |
| 706 | close(pidFd); |
| 707 | } |
| 708 | closedir(dir); |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 709 | async_safe_format_log(ANDROID_LOG_INFO, "vold", "Finished scanning all processes"); |
| 710 | return true; |
| 711 | } |
| 712 | |
Ricky Wai | a2ca11e | 2021-01-15 14:03:23 +0000 | [diff] [blame] | 713 | // In each app's namespace, unmount obb and data dirs |
| 714 | static bool umountStorageDirs(int nsFd, const char* android_data_dir, const char* android_obb_dir, |
| 715 | int uid, const char* targets[], int size) { |
| 716 | // This code is executed after a fork so it's very important that the set of |
| 717 | // methods we call here is strictly limited. |
| 718 | if (setns(nsFd, CLONE_NEWNS) != 0) { |
| 719 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to setns %s", strerror(errno)); |
| 720 | return false; |
| 721 | } |
| 722 | |
| 723 | // Unmount of Android/data/foo needs to be done before Android/data below. |
| 724 | bool result = true; |
| 725 | for (int i = 0; i < size; i++) { |
| 726 | if (TEMP_FAILURE_RETRY(umount2(targets[i], MNT_DETACH)) < 0 && errno != EINVAL && |
| 727 | errno != ENOENT) { |
| 728 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to umount %s: %s", |
| 729 | targets[i], strerror(errno)); |
| 730 | result = false; |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | // Mount tmpfs on Android/data and Android/obb |
| 735 | if (TEMP_FAILURE_RETRY(umount2(android_data_dir, MNT_DETACH)) < 0 && errno != EINVAL && |
| 736 | errno != ENOENT) { |
| 737 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to umount %s :%s", |
| 738 | android_data_dir, strerror(errno)); |
| 739 | result = false; |
| 740 | } |
| 741 | if (TEMP_FAILURE_RETRY(umount2(android_obb_dir, MNT_DETACH)) < 0 && errno != EINVAL && |
| 742 | errno != ENOENT) { |
| 743 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to umount %s :%s", |
| 744 | android_obb_dir, strerror(errno)); |
| 745 | result = false; |
| 746 | } |
| 747 | return result; |
| 748 | } |
| 749 | |
Ricky Wai | e96b34f | 2020-05-07 16:01:33 +0100 | [diff] [blame] | 750 | // In each app's namespace, mount tmpfs on obb and data dir, and bind mount obb and data |
| 751 | // package dirs. |
| 752 | static bool remountStorageDirs(int nsFd, const char* android_data_dir, const char* android_obb_dir, |
| 753 | int uid, const char* sources[], const char* targets[], int size) { |
Ricky Wai | 6b12257 | 2020-02-28 16:30:47 +0000 | [diff] [blame] | 754 | // This code is executed after a fork so it's very important that the set of |
| 755 | // methods we call here is strictly limited. |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 756 | if (setns(nsFd, CLONE_NEWNS) != 0) { |
| 757 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to setns %s", strerror(errno)); |
| 758 | return false; |
| 759 | } |
| 760 | |
Ricky Wai | e96b34f | 2020-05-07 16:01:33 +0100 | [diff] [blame] | 761 | // Mount tmpfs on Android/data and Android/obb |
| 762 | if (TEMP_FAILURE_RETRY(mount("tmpfs", android_data_dir, "tmpfs", |
| 763 | MS_NOSUID | MS_NODEV | MS_NOEXEC, "uid=0,gid=0,mode=0751")) == -1) { |
| 764 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to mount tmpfs to %s :%s", |
| 765 | android_data_dir, strerror(errno)); |
| 766 | return false; |
| 767 | } |
| 768 | if (TEMP_FAILURE_RETRY(mount("tmpfs", android_obb_dir, "tmpfs", |
| 769 | MS_NOSUID | MS_NODEV | MS_NOEXEC, "uid=0,gid=0,mode=0751")) == -1) { |
| 770 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to mount tmpfs to %s :%s", |
| 771 | android_obb_dir, strerror(errno)); |
| 772 | return false; |
| 773 | } |
| 774 | |
Ricky Wai | 6b12257 | 2020-02-28 16:30:47 +0000 | [diff] [blame] | 775 | for (int i = 0; i < size; i++) { |
Ricky Wai | e96b34f | 2020-05-07 16:01:33 +0100 | [diff] [blame] | 776 | // Create package dir and bind mount it to the actual one. |
| 777 | if (TEMP_FAILURE_RETRY(mkdir(targets[i], 0700)) == -1) { |
| 778 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to mkdir %s %s", |
| 779 | targets[i], strerror(errno)); |
| 780 | return false; |
| 781 | } |
Ricky Wai | 6b12257 | 2020-02-28 16:30:47 +0000 | [diff] [blame] | 782 | if (TEMP_FAILURE_RETRY(mount(sources[i], targets[i], NULL, MS_BIND | MS_REC, NULL)) == -1) { |
| 783 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to mount %s to %s :%s", |
| 784 | sources[i], targets[i], strerror(errno)); |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 785 | return false; |
| 786 | } |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 787 | } |
| 788 | return true; |
| 789 | } |
| 790 | |
Ricky Wai | 6b12257 | 2020-02-28 16:30:47 +0000 | [diff] [blame] | 791 | static std::string getStorageDirSrc(userid_t userId, const std::string& dirName, |
| 792 | const std::string& packageName) { |
Daniel Rosenberg | f36bddd | 2020-05-11 22:58:42 -0700 | [diff] [blame] | 793 | if (IsSdcardfsUsed()) { |
Ricky Wai | 6b12257 | 2020-02-28 16:30:47 +0000 | [diff] [blame] | 794 | return StringPrintf("/mnt/runtime/default/emulated/%d/%s/%s", |
| 795 | userId, dirName.c_str(), packageName.c_str()); |
| 796 | } else { |
| 797 | return StringPrintf("/mnt/pass_through/%d/emulated/%d/%s/%s", |
| 798 | userId, userId, dirName.c_str(), packageName.c_str()); |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 799 | } |
Ricky Wai | 6b12257 | 2020-02-28 16:30:47 +0000 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | static std::string getStorageDirTarget(userid_t userId, std::string dirName, |
| 803 | std::string packageName) { |
| 804 | return StringPrintf("/storage/emulated/%d/%s/%s", |
| 805 | userId, dirName.c_str(), packageName.c_str()); |
| 806 | } |
| 807 | |
Ricky Wai | a2ca11e | 2021-01-15 14:03:23 +0000 | [diff] [blame] | 808 | // Fork the process and remount / unmount app data and obb dirs |
| 809 | bool VolumeManager::forkAndRemountStorage(int uid, int pid, bool doUnmount, |
Linus Tufvesson | 75973cb | 2020-03-23 11:59:43 +0000 | [diff] [blame] | 810 | const std::vector<std::string>& packageNames) { |
Ricky Wai | 6b12257 | 2020-02-28 16:30:47 +0000 | [diff] [blame] | 811 | userid_t userId = multiuser_get_user_id(uid); |
| 812 | std::string mnt_path = StringPrintf("/proc/%d/ns/mnt", pid); |
| 813 | android::base::unique_fd nsFd( |
| 814 | TEMP_FAILURE_RETRY(open(mnt_path.c_str(), O_RDONLY | O_CLOEXEC))); |
| 815 | if (nsFd == -1) { |
| 816 | PLOG(ERROR) << "Unable to open " << mnt_path.c_str(); |
| 817 | return false; |
| 818 | } |
| 819 | // Storing both Android/obb and Android/data paths. |
| 820 | int size = packageNames.size() * 2; |
| 821 | |
| 822 | std::unique_ptr<std::string[]> sources(new std::string[size]); |
| 823 | std::unique_ptr<std::string[]> targets(new std::string[size]); |
| 824 | std::unique_ptr<const char*[]> sources_uptr(new const char*[size]); |
| 825 | std::unique_ptr<const char*[]> targets_uptr(new const char*[size]); |
| 826 | const char** sources_cstr = sources_uptr.get(); |
| 827 | const char** targets_cstr = targets_uptr.get(); |
| 828 | |
| 829 | for (int i = 0; i < size; i += 2) { |
| 830 | std::string const& packageName = packageNames[i/2]; |
| 831 | sources[i] = getStorageDirSrc(userId, "Android/data", packageName); |
| 832 | targets[i] = getStorageDirTarget(userId, "Android/data", packageName); |
| 833 | sources[i+1] = getStorageDirSrc(userId, "Android/obb", packageName); |
| 834 | targets[i+1] = getStorageDirTarget(userId, "Android/obb", packageName); |
| 835 | |
| 836 | sources_cstr[i] = sources[i].c_str(); |
| 837 | targets_cstr[i] = targets[i].c_str(); |
| 838 | sources_cstr[i+1] = sources[i+1].c_str(); |
| 839 | targets_cstr[i+1] = targets[i+1].c_str(); |
| 840 | } |
| 841 | |
| 842 | for (int i = 0; i < size; i++) { |
Linus Tufvesson | 75973cb | 2020-03-23 11:59:43 +0000 | [diff] [blame] | 843 | // Make sure /storage/emulated/... paths are setup correctly |
Ricky Wai | f6da79c | 2021-01-19 11:27:36 +0000 | [diff] [blame] | 844 | // This needs to be done before EnsureDirExists to ensure Android/ is created. |
| 845 | auto status = setupAppDir(targets_cstr[i], uid, false /* fixupExistingOnly */); |
Ricky Wai | 6b12257 | 2020-02-28 16:30:47 +0000 | [diff] [blame] | 846 | if (status != OK) { |
| 847 | PLOG(ERROR) << "Failed to create dir: " << targets_cstr[i]; |
| 848 | return false; |
| 849 | } |
Ricky Wai | f6da79c | 2021-01-19 11:27:36 +0000 | [diff] [blame] | 850 | status = EnsureDirExists(sources_cstr[i], 0771, AID_MEDIA_RW, AID_MEDIA_RW); |
| 851 | if (status != OK) { |
| 852 | PLOG(ERROR) << "Failed to create dir: " << sources_cstr[i]; |
| 853 | return false; |
| 854 | } |
Ricky Wai | 6b12257 | 2020-02-28 16:30:47 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Ricky Wai | e96b34f | 2020-05-07 16:01:33 +0100 | [diff] [blame] | 857 | char android_data_dir[PATH_MAX]; |
| 858 | char android_obb_dir[PATH_MAX]; |
| 859 | snprintf(android_data_dir, PATH_MAX, "/storage/emulated/%d/Android/data", userId); |
| 860 | snprintf(android_obb_dir, PATH_MAX, "/storage/emulated/%d/Android/obb", userId); |
| 861 | |
Ricky Wai | 6b12257 | 2020-02-28 16:30:47 +0000 | [diff] [blame] | 862 | pid_t child; |
| 863 | // Fork a child to mount Android/obb android Android/data dirs, as we don't want it to affect |
| 864 | // original vold process mount namespace. |
| 865 | if (!(child = fork())) { |
Ricky Wai | a2ca11e | 2021-01-15 14:03:23 +0000 | [diff] [blame] | 866 | if (doUnmount) { |
| 867 | if (umountStorageDirs(nsFd, android_data_dir, android_obb_dir, uid, |
| 868 | targets_cstr, size)) { |
| 869 | _exit(0); |
| 870 | } else { |
| 871 | _exit(1); |
| 872 | } |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 873 | } else { |
Ricky Wai | a2ca11e | 2021-01-15 14:03:23 +0000 | [diff] [blame] | 874 | if (remountStorageDirs(nsFd, android_data_dir, android_obb_dir, uid, |
| 875 | sources_cstr, targets_cstr, size)) { |
| 876 | _exit(0); |
| 877 | } else { |
| 878 | _exit(1); |
| 879 | } |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 880 | } |
| 881 | } |
Ricky Wai | 6b12257 | 2020-02-28 16:30:47 +0000 | [diff] [blame] | 882 | |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 883 | if (child == -1) { |
| 884 | PLOG(ERROR) << "Failed to fork"; |
Ricky Wai | 6b12257 | 2020-02-28 16:30:47 +0000 | [diff] [blame] | 885 | return false; |
| 886 | } else { |
| 887 | int status; |
| 888 | if (TEMP_FAILURE_RETRY(waitpid(child, &status, 0)) == -1) { |
| 889 | PLOG(ERROR) << "Failed to waitpid: " << child; |
| 890 | return false; |
| 891 | } |
| 892 | if (!WIFEXITED(status)) { |
| 893 | PLOG(ERROR) << "Process did not exit normally, status: " << status; |
| 894 | return false; |
| 895 | } |
| 896 | if (WEXITSTATUS(status)) { |
| 897 | PLOG(ERROR) << "Process exited with code: " << WEXITSTATUS(status); |
| 898 | return false; |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 899 | } |
| 900 | } |
Ricky Wai | 6b12257 | 2020-02-28 16:30:47 +0000 | [diff] [blame] | 901 | return true; |
| 902 | } |
| 903 | |
Ricky Wai | a2ca11e | 2021-01-15 14:03:23 +0000 | [diff] [blame] | 904 | int VolumeManager::handleAppStorageDirs(int uid, int pid, |
| 905 | bool doUnmount, const std::vector<std::string>& packageNames) { |
Ricky Wai | 6b12257 | 2020-02-28 16:30:47 +0000 | [diff] [blame] | 906 | // Only run the remount if fuse is mounted for that user. |
| 907 | userid_t userId = multiuser_get_user_id(uid); |
| 908 | bool fuseMounted = false; |
| 909 | for (auto& vol : mInternalEmulatedVolumes) { |
| 910 | if (vol->getMountUserId() == userId && vol->getState() == VolumeBase::State::kMounted) { |
| 911 | auto* emulatedVol = static_cast<android::vold::EmulatedVolume*>(vol.get()); |
| 912 | if (emulatedVol) { |
| 913 | fuseMounted = emulatedVol->isFuseMounted(); |
| 914 | } |
| 915 | break; |
| 916 | } |
| 917 | } |
| 918 | if (fuseMounted) { |
Ricky Wai | a2ca11e | 2021-01-15 14:03:23 +0000 | [diff] [blame] | 919 | forkAndRemountStorage(uid, pid, doUnmount, packageNames); |
Ricky Wai | 6b12257 | 2020-02-28 16:30:47 +0000 | [diff] [blame] | 920 | } |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 921 | return 0; |
| 922 | } |
| 923 | |
Martijn Coenen | 23c0445 | 2020-04-29 07:49:41 +0200 | [diff] [blame] | 924 | int VolumeManager::abortFuse() { |
| 925 | return android::vold::AbortFuseConnections(); |
| 926 | } |
| 927 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 928 | int VolumeManager::reset() { |
| 929 | // Tear down all existing disks/volumes and start from a blank slate so |
| 930 | // newly connected framework hears all events. |
Momoko Hattori | b4e033e | 2024-02-22 16:07:24 +0900 | [diff] [blame] | 931 | |
| 932 | // Destroy StubVolume disks. This needs to be done before destroying |
| 933 | // EmulatedVolumes because in ARC (Android on ChromeOS), ChromeOS Downloads |
| 934 | // directory (which is in a StubVolume) is bind-mounted to |
| 935 | // /data/media/0/Download. |
| 936 | // We do not recreate StubVolumes here because they are managed from outside |
| 937 | // Android (e.g. from ChromeOS) and their disk recreation on reset events |
| 938 | // should be handled from outside by calling createStubVolume() again. |
| 939 | for (const auto& disk : mDisks) { |
| 940 | if (disk->isStub()) { |
| 941 | disk->destroy(); |
| 942 | } |
| 943 | } |
| 944 | // Remove StubVolume from both mDisks and mPendingDisks. |
| 945 | const auto isStub = [](const auto& disk) { return disk->isStub(); }; |
| 946 | mDisks.remove_if(isStub); |
| 947 | mPendingDisks.remove_if(isStub); |
| 948 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 949 | for (const auto& vol : mInternalEmulatedVolumes) { |
| 950 | vol->destroy(); |
Gao Xiang | d263da8 | 2017-08-14 11:32:13 +0800 | [diff] [blame] | 951 | } |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 952 | mInternalEmulatedVolumes.clear(); |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 953 | |
Momoko Hattori | b4e033e | 2024-02-22 16:07:24 +0900 | [diff] [blame] | 954 | // Destroy and recreate non-StubVolume disks. |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 955 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 956 | disk->destroy(); |
Momoko Hattori | b4e033e | 2024-02-22 16:07:24 +0900 | [diff] [blame] | 957 | disk->create(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 958 | } |
Youkichi Hosoi | dfaff1d | 2021-11-25 16:24:35 +0900 | [diff] [blame] | 959 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 960 | updateVirtualDisk(); |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 961 | mAddedUsers.clear(); |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 962 | mStartedUsers.clear(); |
Martijn Coenen | 73e3010 | 2022-07-12 08:11:02 +0000 | [diff] [blame] | 963 | mSharedStorageUser.clear(); |
Zim | 817f224 | 2022-05-19 16:53:22 +0100 | [diff] [blame] | 964 | |
| 965 | // Abort all FUSE connections to avoid deadlocks if the FUSE daemon was killed |
| 966 | // with FUSE fds open. |
| 967 | abortFuse(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 968 | return 0; |
| 969 | } |
| 970 | |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 971 | // Can be called twice (sequentially) during shutdown. should be safe for that. |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 972 | int VolumeManager::shutdown() { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 973 | if (mInternalEmulatedVolumes.empty()) { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 974 | return 0; // already shutdown |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 975 | } |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 976 | android::vold::sSleepOnUnmount = false; |
Momoko Hattori | b4e033e | 2024-02-22 16:07:24 +0900 | [diff] [blame] | 977 | // Destroy StubVolume disks before destroying EmulatedVolumes (see the |
| 978 | // comment in VolumeManager::reset()). |
| 979 | for (const auto& disk : mDisks) { |
| 980 | if (disk->isStub()) { |
| 981 | disk->destroy(); |
| 982 | } |
| 983 | } |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 984 | for (const auto& vol : mInternalEmulatedVolumes) { |
| 985 | vol->destroy(); |
| 986 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 987 | for (const auto& disk : mDisks) { |
Momoko Hattori | b4e033e | 2024-02-22 16:07:24 +0900 | [diff] [blame] | 988 | if (!disk->isStub()) { |
| 989 | disk->destroy(); |
| 990 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 991 | } |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 992 | |
| 993 | mInternalEmulatedVolumes.clear(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 994 | mDisks.clear(); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 995 | mPendingDisks.clear(); |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 996 | android::vold::sSleepOnUnmount = true; |
Martijn Coenen | 23c0445 | 2020-04-29 07:49:41 +0200 | [diff] [blame] | 997 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 998 | return 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1001 | int VolumeManager::unmountAll() { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 1002 | std::lock_guard<std::mutex> lock(mLock); |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 1003 | ATRACE_NAME("VolumeManager::unmountAll()"); |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 1004 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1005 | // First, try gracefully unmounting all known devices |
Momoko Hattori | b4e033e | 2024-02-22 16:07:24 +0900 | [diff] [blame] | 1006 | // Unmount StubVolume disks before unmounting EmulatedVolumes (see the |
| 1007 | // comment in VolumeManager::reset()). |
| 1008 | for (const auto& disk : mDisks) { |
| 1009 | if (disk->isStub()) { |
| 1010 | disk->unmountAll(); |
| 1011 | } |
| 1012 | } |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1013 | for (const auto& vol : mInternalEmulatedVolumes) { |
| 1014 | vol->unmount(); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1015 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 1016 | for (const auto& disk : mDisks) { |
Momoko Hattori | b4e033e | 2024-02-22 16:07:24 +0900 | [diff] [blame] | 1017 | if (!disk->isStub()) { |
| 1018 | disk->unmountAll(); |
| 1019 | } |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | // Worst case we might have some stale mounts lurking around, so |
| 1023 | // force unmount those just to be safe. |
LongPing.WEI | 4f04606 | 2018-11-23 19:27:35 +0800 | [diff] [blame] | 1024 | FILE* fp = setmntent("/proc/mounts", "re"); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1025 | if (fp == NULL) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1026 | PLOG(ERROR) << "Failed to open /proc/mounts"; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1027 | return -errno; |
| 1028 | } |
| 1029 | |
| 1030 | // Some volumes can be stacked on each other, so force unmount in |
| 1031 | // reverse order to give us the best chance of success. |
| 1032 | std::list<std::string> toUnmount; |
| 1033 | mntent* mentry; |
| 1034 | while ((mentry = getmntent(fp)) != NULL) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1035 | auto test = std::string(mentry->mnt_dir); |
Mark Salyzyn | 86e81e7 | 2018-09-20 10:09:27 -0700 | [diff] [blame] | 1036 | if ((StartsWith(test, "/mnt/") && |
| 1037 | #ifdef __ANDROID_DEBUGGABLE__ |
| 1038 | !StartsWith(test, "/mnt/scratch") && |
| 1039 | #endif |
Martijn Coenen | b0e977a | 2020-01-11 19:24:26 +0100 | [diff] [blame] | 1040 | !StartsWith(test, "/mnt/vendor") && !StartsWith(test, "/mnt/product") && |
Ricky Wai | ef63921 | 2020-04-07 13:43:20 +0100 | [diff] [blame] | 1041 | !StartsWith(test, "/mnt/installer") && !StartsWith(test, "/mnt/androidwritable")) || |
Mark Salyzyn | 86e81e7 | 2018-09-20 10:09:27 -0700 | [diff] [blame] | 1042 | StartsWith(test, "/storage/")) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1043 | toUnmount.push_front(test); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | endmntent(fp); |
| 1047 | |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 1048 | for (const auto& path : toUnmount) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1049 | LOG(DEBUG) << "Tearing down stale mount " << path; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1050 | android::vold::ForceUnmount(path); |
| 1051 | } |
| 1052 | |
| 1053 | return 0; |
| 1054 | } |
| 1055 | |
Ricky Wai | bbfb6ea | 2020-12-03 15:32:52 +0000 | [diff] [blame] | 1056 | int VolumeManager::ensureAppDirsCreated(const std::vector<std::string>& paths, int32_t appUid) { |
Ricky Wai | bbfb6ea | 2020-12-03 15:32:52 +0000 | [diff] [blame] | 1057 | int size = paths.size(); |
| 1058 | for (int i = 0; i < size; i++) { |
| 1059 | int result = setupAppDir(paths[i], appUid, false /* fixupExistingOnly */, |
| 1060 | true /* skipIfDirExists */); |
| 1061 | if (result != OK) { |
| 1062 | return result; |
| 1063 | } |
| 1064 | } |
| 1065 | return OK; |
| 1066 | } |
| 1067 | |
| 1068 | int VolumeManager::setupAppDir(const std::string& path, int32_t appUid, bool fixupExistingOnly, |
| 1069 | bool skipIfDirExists) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1070 | // Only offer to create directories for paths managed by vold |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 1071 | if (!StartsWith(path, "/storage/")) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1072 | LOG(ERROR) << "Failed to find mounted volume for " << path; |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1073 | return -EINVAL; |
| 1074 | } |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 1075 | |
Martijn Coenen | 0a7e992 | 2020-01-24 16:17:32 +0100 | [diff] [blame] | 1076 | // Find the volume it belongs to |
| 1077 | auto filter_fn = [&](const VolumeBase& vol) { |
| 1078 | if (vol.getState() != VolumeBase::State::kMounted) { |
| 1079 | // The volume must be mounted |
| 1080 | return false; |
| 1081 | } |
Youkichi Hosoi | 2991cbe | 2021-11-11 11:23:01 +0900 | [diff] [blame] | 1082 | if (!vol.isVisibleForWrite()) { |
| 1083 | // App dirs should only be created for writable volumes. |
Martijn Coenen | 0a7e992 | 2020-01-24 16:17:32 +0100 | [diff] [blame] | 1084 | return false; |
| 1085 | } |
| 1086 | if (vol.getInternalPath().empty()) { |
| 1087 | return false; |
| 1088 | } |
| 1089 | if (vol.getMountUserId() != USER_UNKNOWN && |
| 1090 | vol.getMountUserId() != multiuser_get_user_id(appUid)) { |
| 1091 | // The app dir must be created on a volume with the same user-id |
| 1092 | return false; |
| 1093 | } |
| 1094 | if (!path.empty() && StartsWith(path, vol.getPath())) { |
| 1095 | return true; |
| 1096 | } |
| 1097 | |
| 1098 | return false; |
| 1099 | }; |
| 1100 | auto volume = findVolumeWithFilter(filter_fn); |
| 1101 | if (volume == nullptr) { |
| 1102 | LOG(ERROR) << "Failed to find mounted volume for " << path; |
| 1103 | return -EINVAL; |
| 1104 | } |
Zim | c59d774 | 2020-01-06 21:48:16 +0000 | [diff] [blame] | 1105 | // Convert paths to lower filesystem paths to avoid making FUSE requests for these reasons: |
| 1106 | // 1. A FUSE request from vold puts vold at risk of hanging if the FUSE daemon is down |
| 1107 | // 2. The FUSE daemon prevents requests on /mnt/user/0/emulated/<userid != 0> and a request |
| 1108 | // on /storage/emulated/10 means /mnt/user/0/emulated/10 |
Martijn Coenen | 0a7e992 | 2020-01-24 16:17:32 +0100 | [diff] [blame] | 1109 | const std::string lowerPath = |
| 1110 | volume->getInternalPath() + path.substr(volume->getPath().length()); |
Zim | c59d774 | 2020-01-06 21:48:16 +0000 | [diff] [blame] | 1111 | |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 1112 | const std::string volumeRoot = volume->getRootPath(); // eg /data/media/0 |
| 1113 | |
Ricky Wai | bbfb6ea | 2020-12-03 15:32:52 +0000 | [diff] [blame] | 1114 | const int access_result = access(lowerPath.c_str(), F_OK); |
| 1115 | if (fixupExistingOnly && access_result != 0) { |
Martijn Coenen | 816f4d9 | 2020-02-18 15:06:37 +0100 | [diff] [blame] | 1116 | // Nothing to fixup |
| 1117 | return OK; |
| 1118 | } |
| 1119 | |
Ricky Wai | bbfb6ea | 2020-12-03 15:32:52 +0000 | [diff] [blame] | 1120 | if (skipIfDirExists && access_result == 0) { |
| 1121 | // It's safe to assume it's ok as it will be used for zygote to bind mount dir only, |
| 1122 | // which the dir doesn't need to have correct permission for now yet. |
| 1123 | return OK; |
| 1124 | } |
| 1125 | |
Martijn Coenen | bf205ab | 2020-04-20 15:14:48 +0200 | [diff] [blame] | 1126 | if (volume->getType() == VolumeBase::Type::kPublic) { |
| 1127 | // On public volumes, we don't need to setup permissions, as everything goes through |
| 1128 | // FUSE; just create the dirs and be done with it. |
| 1129 | return fs_mkdirs(lowerPath.c_str(), 0700); |
| 1130 | } |
| 1131 | |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 1132 | // Create the app paths we need from the root |
Martijn Coenen | 816f4d9 | 2020-02-18 15:06:37 +0100 | [diff] [blame] | 1133 | return PrepareAppDirFromRoot(lowerPath, volumeRoot, appUid, fixupExistingOnly); |
| 1134 | } |
| 1135 | |
| 1136 | int VolumeManager::fixupAppDir(const std::string& path, int32_t appUid) { |
Daniel Rosenberg | f36bddd | 2020-05-11 22:58:42 -0700 | [diff] [blame] | 1137 | if (IsSdcardfsUsed()) { |
Martijn Coenen | 816f4d9 | 2020-02-18 15:06:37 +0100 | [diff] [blame] | 1138 | //sdcardfs magically does this for us |
| 1139 | return OK; |
| 1140 | } |
| 1141 | return setupAppDir(path, appUid, true /* fixupExistingOnly */); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1142 | } |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1143 | |
Eric Biggers | 7e79a43 | 2022-03-01 21:19:18 +0000 | [diff] [blame] | 1144 | int VolumeManager::createObb(const std::string& sourcePath, int32_t ownerGid, |
| 1145 | std::string* outVolId) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1146 | int id = mNextObbId++; |
| 1147 | |
Martijn Coenen | d6a612a | 2020-11-19 10:49:45 +0100 | [diff] [blame] | 1148 | std::string lowerSourcePath; |
| 1149 | |
| 1150 | // Convert to lower filesystem path |
| 1151 | if (StartsWith(sourcePath, "/storage/")) { |
| 1152 | auto filter_fn = [&](const VolumeBase& vol) { |
| 1153 | if (vol.getState() != VolumeBase::State::kMounted) { |
| 1154 | // The volume must be mounted |
| 1155 | return false; |
| 1156 | } |
Youkichi Hosoi | 2991cbe | 2021-11-11 11:23:01 +0900 | [diff] [blame] | 1157 | if (!vol.isVisibleForWrite()) { |
| 1158 | // Obb volume should only be created for writable volumes. |
Martijn Coenen | d6a612a | 2020-11-19 10:49:45 +0100 | [diff] [blame] | 1159 | return false; |
| 1160 | } |
| 1161 | if (vol.getInternalPath().empty()) { |
| 1162 | return false; |
| 1163 | } |
| 1164 | if (!sourcePath.empty() && StartsWith(sourcePath, vol.getPath())) { |
| 1165 | return true; |
| 1166 | } |
| 1167 | |
| 1168 | return false; |
| 1169 | }; |
| 1170 | auto volume = findVolumeWithFilter(filter_fn); |
| 1171 | if (volume == nullptr) { |
| 1172 | LOG(ERROR) << "Failed to find mounted volume for " << sourcePath; |
| 1173 | return -EINVAL; |
| 1174 | } else { |
| 1175 | lowerSourcePath = |
| 1176 | volume->getInternalPath() + sourcePath.substr(volume->getPath().length()); |
| 1177 | } |
| 1178 | } else { |
| 1179 | lowerSourcePath = sourcePath; |
| 1180 | } |
| 1181 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1182 | auto vol = std::shared_ptr<android::vold::VolumeBase>( |
Eric Biggers | 7e79a43 | 2022-03-01 21:19:18 +0000 | [diff] [blame] | 1183 | new android::vold::ObbVolume(id, lowerSourcePath, ownerGid)); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1184 | vol->create(); |
| 1185 | |
| 1186 | mObbVolumes.push_back(vol); |
| 1187 | *outVolId = vol->getId(); |
| 1188 | return android::OK; |
| 1189 | } |
| 1190 | |
| 1191 | int VolumeManager::destroyObb(const std::string& volId) { |
| 1192 | auto i = mObbVolumes.begin(); |
| 1193 | while (i != mObbVolumes.end()) { |
| 1194 | if ((*i)->getId() == volId) { |
| 1195 | (*i)->destroy(); |
| 1196 | i = mObbVolumes.erase(i); |
| 1197 | } else { |
| 1198 | ++i; |
| 1199 | } |
| 1200 | } |
| 1201 | return android::OK; |
| 1202 | } |
| 1203 | |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 1204 | int VolumeManager::createStubVolume(const std::string& sourcePath, const std::string& mountPath, |
| 1205 | const std::string& fsType, const std::string& fsUuid, |
Risan | 73a7a85 | 2020-02-07 18:03:44 +0900 | [diff] [blame] | 1206 | const std::string& fsLabel, int32_t flags, |
Risan | 82e90de | 2020-02-04 16:07:21 +0900 | [diff] [blame] | 1207 | std::string* outVolId) { |
| 1208 | dev_t stubId = --mNextStubId; |
| 1209 | auto vol = std::shared_ptr<android::vold::StubVolume>( |
| 1210 | new android::vold::StubVolume(stubId, sourcePath, mountPath, fsType, fsUuid, fsLabel)); |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 1211 | |
Youkichi Hosoi | defc045 | 2020-07-08 06:08:48 +0900 | [diff] [blame] | 1212 | int32_t passedFlags = 0; |
Risan | 73a7a85 | 2020-02-07 18:03:44 +0900 | [diff] [blame] | 1213 | passedFlags |= (flags & android::vold::Disk::Flags::kUsb); |
| 1214 | passedFlags |= (flags & android::vold::Disk::Flags::kSd); |
Youkichi Hosoi | defc045 | 2020-07-08 06:08:48 +0900 | [diff] [blame] | 1215 | if (flags & android::vold::Disk::Flags::kStubVisible) { |
| 1216 | passedFlags |= (flags & android::vold::Disk::Flags::kStubVisible); |
| 1217 | } else { |
| 1218 | passedFlags |= (flags & android::vold::Disk::Flags::kStubInvisible); |
| 1219 | } |
Risan | 82e90de | 2020-02-04 16:07:21 +0900 | [diff] [blame] | 1220 | // StubDisk doesn't have device node corresponds to it. So, a fake device |
Risan | 73a7a85 | 2020-02-07 18:03:44 +0900 | [diff] [blame] | 1221 | // number is used. |
Risan | 82e90de | 2020-02-04 16:07:21 +0900 | [diff] [blame] | 1222 | auto disk = std::shared_ptr<android::vold::Disk>( |
Risan | 73a7a85 | 2020-02-07 18:03:44 +0900 | [diff] [blame] | 1223 | new android::vold::Disk("stub", stubId, "stub", passedFlags)); |
Risan | 82e90de | 2020-02-04 16:07:21 +0900 | [diff] [blame] | 1224 | disk->initializePartition(vol); |
| 1225 | handleDiskAdded(disk); |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 1226 | *outVolId = vol->getId(); |
| 1227 | return android::OK; |
| 1228 | } |
| 1229 | |
| 1230 | int VolumeManager::destroyStubVolume(const std::string& volId) { |
Risan | 82e90de | 2020-02-04 16:07:21 +0900 | [diff] [blame] | 1231 | auto tokens = android::base::Split(volId, ":"); |
| 1232 | CHECK(tokens.size() == 2); |
| 1233 | dev_t stubId; |
| 1234 | CHECK(android::base::ParseUint(tokens[1], &stubId)); |
| 1235 | handleDiskRemoved(stubId); |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 1236 | return android::OK; |
| 1237 | } |
| 1238 | |
Risan | 8f6198d | 2018-10-26 20:56:45 -0600 | [diff] [blame] | 1239 | int VolumeManager::mountAppFuse(uid_t uid, int mountId, unique_fd* device_fd) { |
Risan | ac02a48 | 2018-10-31 21:59:47 -0600 | [diff] [blame] | 1240 | return android::vold::MountAppFuse(uid, mountId, device_fd); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1241 | } |
| 1242 | |
Risan | 8f6198d | 2018-10-26 20:56:45 -0600 | [diff] [blame] | 1243 | int VolumeManager::unmountAppFuse(uid_t uid, int mountId) { |
Risan | ac02a48 | 2018-10-31 21:59:47 -0600 | [diff] [blame] | 1244 | return android::vold::UnmountAppFuse(uid, mountId); |
Risan | 8f6198d | 2018-10-26 20:56:45 -0600 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | int VolumeManager::openAppFuseFile(uid_t uid, int mountId, int fileId, int flags) { |
Risan | ac02a48 | 2018-10-31 21:59:47 -0600 | [diff] [blame] | 1248 | return android::vold::OpenAppFuseFile(uid, mountId, fileId, flags); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1249 | } |
Paul Lawrence | d73dfd4 | 2023-08-11 10:27:24 -0700 | [diff] [blame] | 1250 | |
| 1251 | android::status_t android::vold::GetStorageSize(int64_t* storageSize) { |
| 1252 | // Start with the /data mount point from fs_mgr |
| 1253 | auto entry = android::fs_mgr::GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT); |
| 1254 | if (entry == nullptr) { |
| 1255 | LOG(ERROR) << "No mount point entry for " << DATA_MNT_POINT; |
| 1256 | return EINVAL; |
| 1257 | } |
| 1258 | |
| 1259 | // Follow any symbolic links |
| 1260 | std::string blkDevice = entry->blk_device; |
| 1261 | std::string dataDevice; |
| 1262 | if (!android::base::Realpath(blkDevice, &dataDevice)) { |
| 1263 | dataDevice = blkDevice; |
| 1264 | } |
| 1265 | |
| 1266 | // Handle mapped volumes. |
| 1267 | auto& dm = android::dm::DeviceMapper::Instance(); |
| 1268 | for (;;) { |
| 1269 | auto parent = dm.GetParentBlockDeviceByPath(dataDevice); |
| 1270 | if (!parent.has_value()) break; |
| 1271 | dataDevice = *parent; |
| 1272 | } |
| 1273 | |
| 1274 | // Get the potential /sys/block entry |
| 1275 | std::size_t leaf = dataDevice.rfind('/'); |
| 1276 | if (leaf == std::string::npos) { |
| 1277 | LOG(ERROR) << "data device " << dataDevice << " is not a path"; |
| 1278 | return EINVAL; |
| 1279 | } |
| 1280 | if (dataDevice.substr(0, leaf) != "/dev/block") { |
| 1281 | LOG(ERROR) << "data device " << dataDevice << " is not a block device"; |
| 1282 | return EINVAL; |
| 1283 | } |
| 1284 | std::string sysfs = std::string() + "/sys/block/" + dataDevice.substr(leaf + 1); |
| 1285 | |
| 1286 | // Look for a directory in /sys/block containing size where the name is a shortened |
| 1287 | // version of the name we now have |
| 1288 | // Typically we start with something like /sys/block/sda2, and we want /sys/block/sda |
| 1289 | // Note that this directory only contains actual disks, not partitions, so this is |
| 1290 | // not going to find anything other than the disks |
| 1291 | std::string size; |
| 1292 | std::string sizeFile; |
| 1293 | for (std::string sysfsDir = sysfs;; sysfsDir = sysfsDir.substr(0, sysfsDir.size() - 1)) { |
| 1294 | if (sysfsDir.back() == '/') { |
| 1295 | LOG(ERROR) << "Could not find valid block device from " << sysfs; |
| 1296 | return EINVAL; |
| 1297 | } |
| 1298 | sizeFile = sysfsDir + "/size"; |
| 1299 | if (android::base::ReadFileToString(sizeFile, &size, true)) { |
| 1300 | break; |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | // Read the size file and be done |
| 1305 | std::stringstream ssSize(size); |
| 1306 | ssSize >> *storageSize; |
| 1307 | if (ssSize.fail()) { |
| 1308 | LOG(ERROR) << sizeFile << " cannot be read as an integer"; |
| 1309 | return EINVAL; |
| 1310 | } |
| 1311 | |
| 1312 | *storageSize *= 512; |
| 1313 | return OK; |
| 1314 | } |