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