blob: 0b2f1bfbf4d17d3ce27c6bdf3dda8483ec4b45ef [file] [log] [blame]
San Mehatf1b736b2009-10-10 17:22:08 -07001/*
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 Sharkey67b8c492017-09-21 17:08:43 -060017#define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
18
Yabin Cuid1104f72015-01-02 13:28:28 -080019#include <dirent.h>
San Mehatf1b736b2009-10-10 17:22:08 -070020#include <errno.h>
San Mehata2677e42009-12-13 10:40:18 -080021#include <fcntl.h>
Yabin Cuid1104f72015-01-02 13:28:28 -080022#include <mntent.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/ioctl.h>
27#include <sys/mount.h>
San Mehata19b2502010-01-06 10:33:53 -080028#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070029#include <sys/sysmacros.h>
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070030#include <sys/types.h>
Jeff Sharkey66270a22015-06-24 11:49:24 -070031#include <sys/wait.h>
Yabin Cuid1104f72015-01-02 13:28:28 -080032#include <unistd.h>
Sudheer Shanka40ab6742018-09-18 13:07:45 -070033#include <array>
San Mehata19b2502010-01-06 10:33:53 -080034
San Mehata2677e42009-12-13 10:40:18 -080035#include <linux/kdev_t.h>
San Mehatf1b736b2009-10-10 17:22:08 -070036
Elliott Hughes7e128fb2015-12-04 15:50:53 -080037#include <android-base/logging.h>
Jeff Vander Stoep58890832017-10-23 17:12:31 -070038#include <android-base/parseint.h>
Jeff Sharkey3472e522017-10-06 18:02:53 -060039#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080040#include <android-base/stringprintf.h>
Jeff Vander Stoep58890832017-10-23 17:12:31 -070041#include <android-base/strings.h>
42
Jeff Sharkey71ebe152013-09-17 17:24:38 -070043#include <cutils/fs.h>
Jeff Sharkey67b8c492017-09-21 17:08:43 -060044#include <utils/Trace.h>
San Mehatf1b736b2009-10-10 17:22:08 -070045
Robert Craigb9e3ba52014-02-04 10:53:00 -050046#include <selinux/android.h>
47
San Mehatfd7f5872009-10-12 11:32:47 -070048#include <sysutils/NetlinkEvent.h>
49
Kenny Root344ca102012-04-03 17:23:01 -070050#include <private/android_filesystem_config.h>
51
Eric Biggersa701c452018-10-23 13:06:55 -070052#include <fscrypt/fscrypt.h>
Paul Crowleyc6433a22017-10-24 14:54:43 -070053
Risanac02a482018-10-31 21:59:47 -060054#include "AppFuseUtil.h"
Paul Crowleyc6433a22017-10-24 14:54:43 -070055#include "Devmapper.h"
Eric Biggersa701c452018-10-23 13:06:55 -070056#include "FsCrypt.h"
San Mehata19b2502010-01-06 10:33:53 -080057#include "Loop.h"
Paul Crowleyc6433a22017-10-24 14:54:43 -070058#include "NetlinkManager.h"
59#include "Process.h"
60#include "Utils.h"
Sudheer Shanka40ab6742018-09-18 13:07:45 -070061#include "VoldNativeService.h"
Paul Crowleyc6433a22017-10-24 14:54:43 -070062#include "VoldUtil.h"
63#include "VolumeManager.h"
64#include "cryptfs.h"
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070065#include "fs/Ext4.h"
66#include "fs/Vfat.h"
Paul Crowleyc6433a22017-10-24 14:54:43 -070067#include "model/EmulatedVolume.h"
68#include "model/ObbVolume.h"
Risan8c9f3322018-10-29 08:52:56 +090069#include "model/StubVolume.h"
San Mehat23969932010-01-09 07:08:06 -080070
Sudheer Shanka53947a32018-08-01 10:24:13 -070071using android::base::GetBoolProperty;
Mark Salyzyn86e81e72018-09-20 10:09:27 -070072using android::base::StartsWith;
Sudheer Shanka53947a32018-08-01 10:24:13 -070073using android::base::StringAppendF;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070074using android::base::StringPrintf;
Jeff Sharkey11c2d382017-09-11 10:32:01 -060075using android::base::unique_fd;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070076
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060077static const char* kPathUserMount = "/mnt/user";
78static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk";
79
Sudheer Shanka53947a32018-08-01 10:24:13 -070080static const char* kIsolatedStorage = "persist.sys.isolated_storage";
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060081static const char* kPropVirtualDisk = "persist.sys.virtual_disk";
82
Sudheer Shanka53947a32018-08-01 10:24:13 -070083static const std::string kEmptyString("");
84
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060085/* 512MiB is large enough for testing purposes */
86static const unsigned int kSizeVirtualDisk = 536870912;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070087
Jeff Sharkey36801cc2015-03-13 16:09:20 -070088static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +080089static const unsigned int kMajorBlockExperimentalMin = 240;
90static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070091
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070092VolumeManager* VolumeManager::sInstance = NULL;
San Mehatf1b736b2009-10-10 17:22:08 -070093
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070094VolumeManager* VolumeManager::Instance() {
95 if (!sInstance) sInstance = new VolumeManager();
San Mehatf1b736b2009-10-10 17:22:08 -070096 return sInstance;
97}
98
99VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -0800100 mDebug = false;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600101 mNextObbId = 0;
Risan8c9f3322018-10-29 08:52:56 +0900102 mNextStubVolumeId = 0;
Jeff Sharkey401b2602017-12-14 22:15:20 -0700103 // For security reasons, assume that a secure keyguard is
104 // showing until we hear otherwise
105 mSecureKeyguardShowing = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700106}
107
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700108VolumeManager::~VolumeManager() {}
San Mehatd9a4e352010-03-12 13:32:47 -0800109
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600110int VolumeManager::updateVirtualDisk() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600111 ATRACE_NAME("VolumeManager::updateVirtualDisk");
Mark Salyzyn86e81e72018-09-20 10:09:27 -0700112 if (GetBoolProperty(kPropVirtualDisk, false)) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600113 if (access(kPathVirtualDisk, F_OK) != 0) {
114 Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512);
115 }
116
117 if (mVirtualDisk == nullptr) {
118 if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) {
119 LOG(ERROR) << "Failed to create virtual disk";
120 return -1;
121 }
122
123 struct stat buf;
124 if (stat(mVirtualDiskPath.c_str(), &buf) < 0) {
125 PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath;
126 return -1;
127 }
128
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700129 auto disk = new android::vold::Disk(
130 "virtual", buf.st_rdev, "virtual",
131 android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600132 mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk);
Jeff Sharkey401b2602017-12-14 22:15:20 -0700133 handleDiskAdded(mVirtualDisk);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600134 }
135 } else {
136 if (mVirtualDisk != nullptr) {
137 dev_t device = mVirtualDisk->getDevice();
Jeff Sharkey401b2602017-12-14 22:15:20 -0700138 handleDiskRemoved(device);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600139
140 Loop::destroyByDevice(mVirtualDiskPath.c_str());
141 mVirtualDisk = nullptr;
142 }
143
144 if (access(kPathVirtualDisk, F_OK) == 0) {
145 unlink(kPathVirtualDisk);
146 }
147 }
148 return 0;
149}
150
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700151int VolumeManager::setDebug(bool enable) {
San Mehatd9a4e352010-03-12 13:32:47 -0800152 mDebug = enable;
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700153 return 0;
San Mehatd9a4e352010-03-12 13:32:47 -0800154}
155
San Mehatf1b736b2009-10-10 17:22:08 -0700156int VolumeManager::start() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600157 ATRACE_NAME("VolumeManager::start");
158
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700159 // Always start from a clean slate by unmounting everything in
160 // directories that we own, in case we crashed.
Jeff Sharkey9c484982015-03-31 10:35:33 -0700161 unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700162
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600163 Devmapper::destroyAll();
164 Loop::destroyAll();
165
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700166 // Assume that we always have an emulated volume on internal
167 // storage; the framework will decide if it should be mounted.
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700168 CHECK(mInternalEmulated == nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700169 mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700170 new android::vold::EmulatedVolume("/data/media"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700171 mInternalEmulated->create();
172
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600173 // Consider creating a virtual disk
174 updateVirtualDisk();
175
San Mehatf1b736b2009-10-10 17:22:08 -0700176 return 0;
177}
178
179int VolumeManager::stop() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700180 CHECK(mInternalEmulated != nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700181 mInternalEmulated->destroy();
182 mInternalEmulated = nullptr;
San Mehatf1b736b2009-10-10 17:22:08 -0700183 return 0;
184}
185
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700186void VolumeManager::handleBlockEvent(NetlinkEvent* evt) {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700187 std::lock_guard<std::mutex> lock(mLock);
188
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700189 if (mDebug) {
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700190 LOG(DEBUG) << "----------------";
191 LOG(DEBUG) << "handleBlockEvent with action " << (int)evt->getAction();
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700192 evt->dump();
193 }
San Mehatf1b736b2009-10-10 17:22:08 -0700194
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700195 std::string eventPath(evt->findParam("DEVPATH") ? evt->findParam("DEVPATH") : "");
196 std::string devType(evt->findParam("DEVTYPE") ? evt->findParam("DEVTYPE") : "");
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700197
198 if (devType != "disk") return;
199
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600200 int major = std::stoi(evt->findParam("MAJOR"));
201 int minor = std::stoi(evt->findParam("MINOR"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700202 dev_t device = makedev(major, minor);
203
204 switch (evt->getAction()) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700205 case NetlinkEvent::Action::kAdd: {
206 for (const auto& source : mDiskSources) {
207 if (source->matches(eventPath)) {
208 // For now, assume that MMC and virtio-blk (the latter is
209 // emulator-specific; see Disk.cpp for details) devices are SD,
210 // and that everything else is USB
211 int flags = source->getFlags();
212 if (major == kMajorBlockMmc || (android::vold::IsRunningInEmulator() &&
213 major >= (int)kMajorBlockExperimentalMin &&
214 major <= (int)kMajorBlockExperimentalMax)) {
215 flags |= android::vold::Disk::Flags::kSd;
216 } else {
217 flags |= android::vold::Disk::Flags::kUsb;
218 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700219
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700220 auto disk =
221 new android::vold::Disk(eventPath, device, source->getNickname(), flags);
222 handleDiskAdded(std::shared_ptr<android::vold::Disk>(disk));
223 break;
224 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700225 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700226 break;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700227 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700228 case NetlinkEvent::Action::kChange: {
229 LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
230 handleDiskChanged(device);
231 break;
232 }
233 case NetlinkEvent::Action::kRemove: {
234 handleDiskRemoved(device);
235 break;
236 }
237 default: {
238 LOG(WARNING) << "Unexpected block event action " << (int)evt->getAction();
239 break;
240 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700241 }
242}
243
Jeff Sharkey401b2602017-12-14 22:15:20 -0700244void VolumeManager::handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk) {
245 // For security reasons, if secure keyguard is showing, wait
246 // until the user unlocks the device to actually touch it
247 if (mSecureKeyguardShowing) {
248 LOG(INFO) << "Found disk at " << disk->getEventPath()
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700249 << " but delaying scan due to secure keyguard";
Jeff Sharkey401b2602017-12-14 22:15:20 -0700250 mPendingDisks.push_back(disk);
251 } else {
252 disk->create();
253 mDisks.push_back(disk);
254 }
255}
256
257void VolumeManager::handleDiskChanged(dev_t device) {
258 for (const auto& disk : mDisks) {
259 if (disk->getDevice() == device) {
260 disk->readMetadata();
261 disk->readPartitions();
262 }
263 }
264
265 // For security reasons, we ignore all pending disks, since
266 // we'll scan them once the device is unlocked
267}
268
269void VolumeManager::handleDiskRemoved(dev_t device) {
270 auto i = mDisks.begin();
271 while (i != mDisks.end()) {
272 if ((*i)->getDevice() == device) {
273 (*i)->destroy();
274 i = mDisks.erase(i);
275 } else {
276 ++i;
277 }
278 }
279 auto j = mPendingDisks.begin();
280 while (j != mPendingDisks.end()) {
281 if ((*j)->getDevice() == device) {
282 j = mPendingDisks.erase(j);
283 } else {
284 ++j;
285 }
286 }
287}
288
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700289void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
Wei Wang6b455c22017-01-20 11:52:33 -0800290 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700291 mDiskSources.push_back(diskSource);
292}
293
294std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
295 for (auto disk : mDisks) {
296 if (disk->getId() == id) {
297 return disk;
San Mehatf1b736b2009-10-10 17:22:08 -0700298 }
299 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700300 return nullptr;
301}
San Mehatf1b736b2009-10-10 17:22:08 -0700302
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700303std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
Gao Xiangd263da82017-08-14 11:32:13 +0800304 // Vold could receive "mount" after "shutdown" command in the extreme case.
305 // If this happens, mInternalEmulated will equal nullptr and
306 // we need to deal with it in order to avoid null pointer crash.
307 if (mInternalEmulated != nullptr && mInternalEmulated->getId() == id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700308 return mInternalEmulated;
San Mehatf1b736b2009-10-10 17:22:08 -0700309 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700310 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700311 auto vol = disk->findVolume(id);
312 if (vol != nullptr) {
313 return vol;
314 }
315 }
Risan8c9f3322018-10-29 08:52:56 +0900316 for (const auto& vol : mStubVolumes) {
317 if (vol->getId() == id) {
318 return vol;
319 }
320 }
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600321 for (const auto& vol : mObbVolumes) {
322 if (vol->getId() == id) {
323 return vol;
324 }
325 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700326 return nullptr;
327}
328
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700329void VolumeManager::listVolumes(android::vold::VolumeBase::Type type, std::list<std::string>& list) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700330 list.clear();
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700331 for (const auto& disk : mDisks) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700332 disk->listVolumes(type, list);
333 }
334}
335
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600336int VolumeManager::forgetPartition(const std::string& partGuid, const std::string& fsUuid) {
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700337 std::string normalizedGuid;
338 if (android::vold::NormalizeHex(partGuid, normalizedGuid)) {
339 LOG(WARNING) << "Invalid GUID " << partGuid;
340 return -1;
341 }
342
Paul Crowleyc6433a22017-10-24 14:54:43 -0700343 bool success = true;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700344 std::string keyPath = android::vold::BuildKeyPath(normalizedGuid);
345 if (unlink(keyPath.c_str()) != 0) {
346 LOG(ERROR) << "Failed to unlink " << keyPath;
Paul Crowleyc6433a22017-10-24 14:54:43 -0700347 success = false;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700348 }
Eric Biggersa701c452018-10-23 13:06:55 -0700349 if (fscrypt_is_native()) {
350 if (!fscrypt_destroy_volume_keys(fsUuid)) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700351 success = false;
352 }
353 }
354 return success ? 0 : -1;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700355}
356
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700357int VolumeManager::linkPrimary(userid_t userId) {
358 std::string source(mPrimary->getPath());
359 if (mPrimary->isEmulated()) {
360 source = StringPrintf("%s/%d", source.c_str(), userId);
361 fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT);
362 }
363
364 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
365 if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) {
366 if (errno != ENOENT) {
367 PLOG(WARNING) << "Failed to unlink " << target;
368 }
369 }
370 LOG(DEBUG) << "Linking " << source << " to " << target;
371 if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) {
372 PLOG(WARNING) << "Failed to link";
Sudheer Shanka3a7ee5c2018-08-07 17:10:11 -0700373 return -errno;
374 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700375 return 0;
376}
377
378int VolumeManager::mountPkgSpecificDir(const std::string& mntSourceRoot,
379 const std::string& mntTargetRoot,
380 const std::string& packageName, const char* dirName) {
381 std::string mntSourceDir =
382 StringPrintf("%s/Android/%s/%s", mntSourceRoot.c_str(), dirName, packageName.c_str());
383 std::string mntTargetDir =
384 StringPrintf("%s/Android/%s/%s", mntTargetRoot.c_str(), dirName, packageName.c_str());
385 if (umount2(mntTargetDir.c_str(), MNT_DETACH) == -1 && errno != EINVAL && errno != ENOENT) {
386 PLOG(ERROR) << "Failed to unmount " << mntTargetDir;
387 return -1;
388 }
389 if (TEMP_FAILURE_RETRY(mount(mntSourceDir.c_str(), mntTargetDir.c_str(), nullptr,
390 MS_BIND | MS_REC, nullptr)) == -1) {
391 PLOG(ERROR) << "Failed to mount " << mntSourceDir << " to " << mntTargetDir;
392 return -1;
Sudheer Shanka3a7ee5c2018-08-07 17:10:11 -0700393 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700394 if (TEMP_FAILURE_RETRY(
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700395 mount(nullptr, mntTargetDir.c_str(), nullptr, MS_REC | MS_SLAVE, nullptr)) == -1) {
396 PLOG(ERROR) << "Failed to set MS_SLAVE at " << mntTargetDir;
397 return -1;
Sudheer Shanka3a7ee5c2018-08-07 17:10:11 -0700398 }
399 return 0;
400}
401
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700402int VolumeManager::mountPkgSpecificDirsForRunningProcs(
403 userid_t userId, const std::vector<std::string>& packageNames,
404 const std::vector<std::string>& visibleVolLabels) {
405 // TODO: New processes could be started while traversing over the existing
406 // processes which would end up not having the necessary bind mounts. This
407 // issue needs to be fixed, may be by doing multiple passes here?
408 std::unique_ptr<DIR, decltype(&closedir)> dirp(opendir("/proc"), closedir);
409 if (!dirp) {
410 PLOG(ERROR) << "Failed to opendir /proc";
411 return -1;
Sudheer Shankac7562092018-08-24 10:20:56 -0700412 }
Sudheer Shanka53947a32018-08-01 10:24:13 -0700413
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700414 std::string rootName;
415 // Figure out root namespace to compare against below
416 if (!android::vold::Readlinkat(dirfd(dirp.get()), "1/ns/mnt", &rootName)) {
417 PLOG(ERROR) << "Failed to read root namespace";
418 return -1;
Sudheer Shankaf768c272018-08-04 10:10:27 -0700419 }
Sudheer Shanka53947a32018-08-01 10:24:13 -0700420
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700421 struct stat fullWriteSb;
422 if (TEMP_FAILURE_RETRY(stat("/mnt/runtime/write", &fullWriteSb)) == -1) {
423 PLOG(ERROR) << "Failed to stat /mnt/runtime/write";
424 return -1;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700425 }
426
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700427 std::unordered_set<appid_t> validAppIds;
428 for (auto& package : packageNames) {
429 validAppIds.insert(mAppIds[package]);
Sudheer Shankaa695f252018-08-03 18:07:52 -0700430 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700431 std::vector<std::string>& userPackages = mUserPackages[userId];
Sudheer Shanka53947a32018-08-01 10:24:13 -0700432
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700433 struct dirent* de;
434 // Poke through all running PIDs look for apps running in userId
435 while ((de = readdir(dirp.get()))) {
436 pid_t pid;
437 if (de->d_type != DT_DIR) continue;
438 if (!android::base::ParseInt(de->d_name, &pid)) continue;
439
440 const unique_fd pidFd(
441 openat(dirfd(dirp.get()), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC));
442 if (pidFd.get() < 0) {
443 PLOG(WARNING) << "Failed to open /proc/" << pid;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700444 continue;
445 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700446 struct stat sb;
447 if (fstat(pidFd.get(), &sb) != 0) {
448 PLOG(WARNING) << "Failed to stat " << de->d_name;
449 continue;
450 }
451 if (multiuser_get_user_id(sb.st_uid) != userId) {
452 continue;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700453 }
Sudheer Shanka53947a32018-08-01 10:24:13 -0700454
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700455 // Matches so far, but refuse to touch if in root namespace
456 LOG(VERBOSE) << "Found matching PID " << de->d_name;
457 std::string pidName;
458 if (!android::vold::Readlinkat(pidFd.get(), "ns/mnt", &pidName)) {
459 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
460 continue;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700461 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700462 if (rootName == pidName) {
463 LOG(WARNING) << "Skipping due to root namespace";
464 continue;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700465 }
466
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700467 // Only update the mount points of processes running with one of validAppIds.
468 // This should skip any isolated uids.
469 appid_t appId = multiuser_get_app_id(sb.st_uid);
470 if (validAppIds.find(appId) == validAppIds.end()) {
471 continue;
Sudheer Shanka3a7ee5c2018-08-07 17:10:11 -0700472 }
473
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700474 std::vector<std::string> packagesForUid;
475 for (auto& package : userPackages) {
476 if (mAppIds[package] == appId) {
477 packagesForUid.push_back(package);
478 }
479 }
480 if (packagesForUid.empty()) {
481 continue;
482 }
483 const std::string& sandboxId = mSandboxIds[appId];
484
485 // We purposefully leave the namespace open across the fork
486 unique_fd nsFd(openat(pidFd.get(), "ns/mnt", O_RDONLY)); // not O_CLOEXEC
487 if (nsFd.get() < 0) {
488 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
489 continue;
Sudheer Shanka3a7ee5c2018-08-07 17:10:11 -0700490 }
491
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700492 pid_t child;
493 if (!(child = fork())) {
494 if (setns(nsFd.get(), CLONE_NEWNS) != 0) {
495 PLOG(ERROR) << "Failed to setns for " << de->d_name;
496 _exit(1);
497 }
498
499 struct stat storageSb;
500 if (TEMP_FAILURE_RETRY(stat("/storage", &storageSb)) == -1) {
501 PLOG(ERROR) << "Failed to stat /storage";
502 _exit(1);
503 }
504
505 // Some packages have access to full external storage, identify processes belonging
506 // to those packages by comparing inode no.s of /mnt/runtime/write and /storage
507 if (storageSb.st_dev == fullWriteSb.st_dev && storageSb.st_ino == fullWriteSb.st_ino) {
508 _exit(0);
509 } else {
510 // Some packages don't have access to external storage and processes belonging to
511 // those packages don't have anything mounted at /storage. So, identify those
512 // processes by comparing inode no.s of /mnt/user/%d/package/%s
513 // and /storage
514 std::string pkgStorageSource;
515 for (auto& package : packagesForUid) {
516 std::string sandbox =
517 StringPrintf("/mnt/user/%d/package/%s", userId, package.c_str());
518 struct stat s;
519 if (TEMP_FAILURE_RETRY(stat(sandbox.c_str(), &s)) == -1) {
520 PLOG(ERROR) << "Failed to stat " << sandbox;
521 _exit(1);
522 }
523 if (storageSb.st_dev == s.st_dev && storageSb.st_ino == s.st_ino) {
524 pkgStorageSource = sandbox;
525 break;
526 }
527 }
528 if (pkgStorageSource.empty()) {
529 _exit(0);
530 }
531 }
532
533 for (auto& volumeLabel : visibleVolLabels) {
534 std::string mntSource = StringPrintf("/mnt/runtime/write/%s", volumeLabel.c_str());
535 std::string mntTarget = StringPrintf("/storage/%s", volumeLabel.c_str());
536 if (volumeLabel == "emulated") {
537 StringAppendF(&mntSource, "/%d", userId);
538 StringAppendF(&mntTarget, "/%d", userId);
539 }
540 for (auto& package : packagesForUid) {
541 mountPkgSpecificDir(mntSource, mntTarget, package, "data");
542 mountPkgSpecificDir(mntSource, mntTarget, package, "media");
543 mountPkgSpecificDir(mntSource, mntTarget, package, "obb");
544 }
545 }
546 _exit(0);
Sudheer Shanka53947a32018-08-01 10:24:13 -0700547 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700548
549 if (child == -1) {
550 PLOG(ERROR) << "Failed to fork";
551 } else {
552 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
Sudheer Shanka53947a32018-08-01 10:24:13 -0700553 }
554 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700555 return 0;
556}
557
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700558int VolumeManager::prepareSandboxes(userid_t userId, const std::vector<std::string>& packageNames,
559 const std::vector<std::string>& visibleVolLabels) {
560 if (visibleVolLabels.empty()) {
561 return 0;
562 }
563 for (auto& volumeLabel : visibleVolLabels) {
564 std::string volumeRoot(StringPrintf("/mnt/runtime/write/%s", volumeLabel.c_str()));
565 bool isVolPrimaryEmulated = (volumeLabel == mPrimary->getLabel() && mPrimary->isEmulated());
566 if (isVolPrimaryEmulated) {
567 StringAppendF(&volumeRoot, "/%d", userId);
568 if (fs_prepare_dir(volumeRoot.c_str(), 0755, AID_ROOT, AID_ROOT) != 0) {
569 PLOG(ERROR) << "fs_prepare_dir failed on " << volumeRoot;
570 return -errno;
571 }
572 }
573
574 std::string sandboxRoot =
575 prepareSubDirs(volumeRoot, "Android/sandbox/", 0700, AID_ROOT, AID_ROOT);
576 if (sandboxRoot.empty()) {
577 return -errno;
578 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700579
580 if (!createPkgSpecificDirRoots(volumeRoot)) {
581 return -errno;
582 }
583
584 std::string mntTargetRoot = StringPrintf("/mnt/user/%d", userId);
585 if (fs_prepare_dir(mntTargetRoot.c_str(), 0751, AID_ROOT, AID_ROOT) != 0) {
586 PLOG(ERROR) << "fs_prepare_dir failed on " << mntTargetRoot;
587 return -errno;
588 }
589 mntTargetRoot.append("/package");
590 if (fs_prepare_dir(mntTargetRoot.c_str(), 0700, AID_ROOT, AID_ROOT) != 0) {
591 PLOG(ERROR) << "fs_prepare_dir failed on " << mntTargetRoot;
592 return -errno;
593 }
594
595 for (auto& packageName : packageNames) {
596 const auto& it = mAppIds.find(packageName);
597 if (it == mAppIds.end()) {
598 PLOG(ERROR) << "appId is not available for " << packageName;
599 continue;
600 }
601 appid_t appId = it->second;
602 std::string sandboxId = mSandboxIds[appId];
603 uid_t uid = multiuser_get_uid(userId, appId);
604
605 // [1] Create /mnt/runtime/write/emulated/0/Android/sandbox/<sandboxId>
606 // [2] Create /mnt/user/0/package/<packageName>/emulated/0
607 // Mount [1] at [2]
608 std::string pkgSandboxSourceDir = prepareSandboxSource(uid, sandboxId, sandboxRoot);
609 if (pkgSandboxSourceDir.empty()) {
610 return -errno;
611 }
612 std::string pkgSandboxTargetDir = prepareSandboxTarget(
613 packageName, uid, volumeLabel, mntTargetRoot, isVolPrimaryEmulated);
614 if (pkgSandboxTargetDir.empty()) {
615 return -errno;
616 }
617 if (umount2(pkgSandboxTargetDir.c_str(), MNT_DETACH) == -1 && errno != EINVAL &&
618 errno != ENOENT) {
619 PLOG(ERROR) << "Failed to unmount " << pkgSandboxTargetDir;
620 return -errno;
621 }
622 if (TEMP_FAILURE_RETRY(mount(pkgSandboxSourceDir.c_str(), pkgSandboxTargetDir.c_str(),
623 nullptr, MS_BIND | MS_REC, nullptr)) == -1) {
624 PLOG(ERROR) << "Failed to mount " << pkgSandboxSourceDir << " at "
625 << pkgSandboxTargetDir;
626 return -errno;
627 }
628 if (TEMP_FAILURE_RETRY(mount(nullptr, pkgSandboxTargetDir.c_str(), nullptr,
629 MS_SLAVE | MS_REC, nullptr)) == -1) {
630 PLOG(ERROR) << "Failed to mount " << pkgSandboxSourceDir << " at "
631 << pkgSandboxTargetDir;
632 return -errno;
633 }
634
635 // Create Android/{data,media,obb}/<packageName> segments at
636 // [1] /mnt/runtime/write/emulated/0/ and
637 // [2] /mnt/runtime/write/emulated/0/Android/sandbox/<sandboxId>/emulated/0/
638 if (!createPkgSpecificDirs(packageName, uid, volumeRoot, pkgSandboxSourceDir)) {
639 return -errno;
640 }
641
642 if (volumeLabel == mPrimary->getLabel()) {
643 // Create [1] /mnt/user/0/package/<packageName>/self/
644 // Already created [2] /mnt/user/0/package/<packageName>/emulated/0
645 // Mount [2] at [1]
646 std::string pkgPrimaryTargetDir =
647 StringPrintf("%s/%s/self", mntTargetRoot.c_str(), packageName.c_str());
648 if (fs_prepare_dir(pkgPrimaryTargetDir.c_str(), 0755, uid, uid) != 0) {
649 PLOG(ERROR) << "Failed to fs_prepare_dir on " << pkgPrimaryTargetDir;
650 return -errno;
651 }
652 StringAppendF(&pkgPrimaryTargetDir, "/primary");
653 std::string primarySource(mPrimary->getPath());
654 if (isVolPrimaryEmulated) {
655 StringAppendF(&primarySource, "/%d", userId);
656 }
657 if (TEMP_FAILURE_RETRY(unlink(pkgPrimaryTargetDir.c_str()))) {
658 if (errno != ENOENT) {
659 PLOG(ERROR) << "Failed to unlink " << pkgPrimaryTargetDir;
660 }
661 }
662 if (TEMP_FAILURE_RETRY(symlink(primarySource.c_str(), pkgPrimaryTargetDir.c_str()))) {
663 PLOG(ERROR) << "Failed to link " << primarySource << " at "
664 << pkgPrimaryTargetDir;
665 return -errno;
666 }
667 }
668 }
669 }
670 mountPkgSpecificDirsForRunningProcs(userId, packageNames, visibleVolLabels);
671 return 0;
672}
673
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700674std::string VolumeManager::prepareSubDirs(const std::string& pathPrefix, const std::string& subDirs,
675 mode_t mode, uid_t uid, gid_t gid) {
Sudheer Shanka53947a32018-08-01 10:24:13 -0700676 std::string path(pathPrefix);
677 std::vector<std::string> subDirList = android::base::Split(subDirs, "/");
678 for (size_t i = 0; i < subDirList.size(); ++i) {
679 std::string subDir = subDirList[i];
680 if (subDir.empty()) {
681 continue;
682 }
683 StringAppendF(&path, "/%s", subDir.c_str());
684 if (fs_prepare_dir(path.c_str(), mode, uid, gid) != 0) {
685 PLOG(ERROR) << "fs_prepare_dir failed on " << path;
686 return kEmptyString;
687 }
688 }
689 return path;
690}
691
692std::string VolumeManager::prepareSandboxSource(uid_t uid, const std::string& sandboxId,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700693 const std::string& sandboxRootDir) {
Sudheer Shankaf768c272018-08-04 10:10:27 -0700694 std::string sandboxSourceDir(sandboxRootDir);
Sudheer Shanka51a38da2018-10-18 08:51:31 -0700695 StringAppendF(&sandboxSourceDir, "/%s", sandboxId.c_str());
Sudheer Shanka53947a32018-08-01 10:24:13 -0700696 if (fs_prepare_dir(sandboxSourceDir.c_str(), 0755, uid, uid) != 0) {
697 PLOG(ERROR) << "fs_prepare_dir failed on " << sandboxSourceDir;
698 return kEmptyString;
699 }
700 return sandboxSourceDir;
701}
702
703std::string VolumeManager::prepareSandboxTarget(const std::string& packageName, uid_t uid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700704 const std::string& volumeLabel,
705 const std::string& mntTargetRootDir,
706 bool isUserDependent) {
Sudheer Shanka53947a32018-08-01 10:24:13 -0700707 std::string segment;
708 if (isUserDependent) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700709 segment = StringPrintf("%s/%s/%d/", packageName.c_str(), volumeLabel.c_str(),
710 multiuser_get_user_id(uid));
Sudheer Shanka53947a32018-08-01 10:24:13 -0700711 } else {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700712 segment = StringPrintf("%s/%s/", packageName.c_str(), volumeLabel.c_str());
Sudheer Shanka53947a32018-08-01 10:24:13 -0700713 }
714 return prepareSubDirs(mntTargetRootDir, segment.c_str(), 0755, uid, uid);
715}
716
717std::string VolumeManager::preparePkgDataSource(const std::string& packageName, uid_t uid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700718 const std::string& dataRootDir) {
Sudheer Shanka53947a32018-08-01 10:24:13 -0700719 std::string dataSourceDir = StringPrintf("%s/%s", dataRootDir.c_str(), packageName.c_str());
720 if (fs_prepare_dir(dataSourceDir.c_str(), 0755, uid, uid) != 0) {
721 PLOG(ERROR) << "fs_prepare_dir failed on " << dataSourceDir;
722 return kEmptyString;
723 }
724 return dataSourceDir;
725}
726
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700727bool VolumeManager::createPkgSpecificDirRoots(const std::string& volumeRoot) {
728 std::string volumeAndroidRoot = StringPrintf("%s/Android", volumeRoot.c_str());
729 if (fs_prepare_dir(volumeAndroidRoot.c_str(), 0700, AID_ROOT, AID_ROOT) != 0) {
730 PLOG(ERROR) << "fs_prepare_dir failed on " << volumeAndroidRoot;
731 return false;
732 }
733 std::array<std::string, 3> dirs = {"data", "media", "obb"};
734 for (auto& dir : dirs) {
735 std::string path = StringPrintf("%s/%s", volumeAndroidRoot.c_str(), dir.c_str());
736 if (fs_prepare_dir(path.c_str(), 0700, AID_ROOT, AID_ROOT) != 0) {
737 PLOG(ERROR) << "fs_prepare_dir failed on " << path;
738 return false;
739 }
740 }
741 return true;
742}
743
744bool VolumeManager::createPkgSpecificDirs(const std::string& packageName, uid_t uid,
745 const std::string& volumeRoot,
746 const std::string& sandboxDirRoot) {
747 std::array<std::string, 3> dirs = {"data", "media", "obb"};
748 for (auto& dir : dirs) {
749 std::string sourceDir = StringPrintf("%s/Android/%s", volumeRoot.c_str(), dir.c_str());
750 if (prepareSubDirs(sourceDir, packageName, 0755, uid, uid).empty()) {
751 return false;
752 }
753 std::string sandboxSegment =
754 StringPrintf("Android/%s/%s/", dir.c_str(), packageName.c_str());
755 if (prepareSubDirs(sandboxDirRoot, sandboxSegment, 0755, uid, uid).empty()) {
756 return false;
757 }
758 }
759 return true;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700760}
761
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700762int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
763 mAddedUsers[userId] = userSerialNumber;
764 return 0;
765}
766
767int VolumeManager::onUserRemoved(userid_t userId) {
768 mAddedUsers.erase(userId);
769 return 0;
770}
771
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700772int VolumeManager::onUserStarted(userid_t userId, const std::vector<std::string>& packageNames,
773 const std::vector<int>& appIds,
774 const std::vector<std::string>& sandboxIds) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700775 LOG(VERBOSE) << "onUserStarted: " << userId;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700776 // Note that sometimes the system will spin up processes from Zygote
777 // before actually starting the user, so we're okay if Zygote
778 // already created this directory.
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600779 std::string path(StringPrintf("%s/%d", kPathUserMount, userId));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700780 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
781
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700782 mStartedUsers.insert(userId);
Sudheer Shanka62bbb2b2018-08-01 01:09:10 -0700783 mUserPackages[userId] = packageNames;
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700784 for (size_t i = 0; i < packageNames.size(); ++i) {
785 mAppIds[packageNames[i]] = appIds[i];
786 mSandboxIds[appIds[i]] = sandboxIds[i];
787 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700788 if (mPrimary) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700789 linkPrimary(userId);
790 }
791 if (GetBoolProperty(kIsolatedStorage, false)) {
792 std::vector<std::string> visibleVolLabels;
793 for (auto& volId : mVisibleVolumeIds) {
794 auto vol = findVolume(volId);
795 userid_t mountUserId = vol->getMountUserId();
796 if (mountUserId == userId || vol->isEmulated()) {
797 visibleVolLabels.push_back(vol->getLabel());
798 }
799 }
800 if (prepareSandboxes(userId, packageNames, visibleVolLabels) != 0) {
801 return -errno;
802 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700803 }
804 return 0;
805}
806
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700807int VolumeManager::onUserStopped(userid_t userId) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700808 LOG(VERBOSE) << "onUserStopped: " << userId;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700809 mStartedUsers.erase(userId);
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700810
Sudheer Shanka9acc6d42018-10-06 19:03:02 -0700811 if (GetBoolProperty(kIsolatedStorage, false)) {
812 mUserPackages.erase(userId);
813 std::string mntTargetDir = StringPrintf("/mnt/user/%d", userId);
814 if (android::vold::UnmountTree(mntTargetDir) != 0) {
815 PLOG(ERROR) << "unmountTree on " << mntTargetDir << " failed";
816 return -errno;
817 }
818 if (android::vold::DeleteDirContentsAndDir(mntTargetDir) < 0) {
819 PLOG(ERROR) << "DeleteDirContentsAndDir failed on " << mntTargetDir;
820 return -errno;
821 }
822 LOG(VERBOSE) << "Success: DeleteDirContentsAndDir on " << mntTargetDir;
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700823 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700824 return 0;
825}
826
Sudheer Shankad484aa92018-07-31 10:07:34 -0700827int VolumeManager::addAppIds(const std::vector<std::string>& packageNames,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700828 const std::vector<int32_t>& appIds) {
Sudheer Shanka62bbb2b2018-08-01 01:09:10 -0700829 for (size_t i = 0; i < packageNames.size(); ++i) {
830 mAppIds[packageNames[i]] = appIds[i];
831 }
Sudheer Shankad484aa92018-07-31 10:07:34 -0700832 return 0;
833}
834
835int VolumeManager::addSandboxIds(const std::vector<int32_t>& appIds,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700836 const std::vector<std::string>& sandboxIds) {
Sudheer Shanka62bbb2b2018-08-01 01:09:10 -0700837 for (size_t i = 0; i < appIds.size(); ++i) {
838 mSandboxIds[appIds[i]] = sandboxIds[i];
839 }
Sudheer Shankad484aa92018-07-31 10:07:34 -0700840 return 0;
841}
842
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700843int VolumeManager::prepareSandboxForApp(const std::string& packageName, appid_t appId,
844 const std::string& sandboxId, userid_t userId) {
Sudheer Shankac7562092018-08-24 10:20:56 -0700845 if (!GetBoolProperty(kIsolatedStorage, false)) {
846 return 0;
847 } else if (mStartedUsers.find(userId) == mStartedUsers.end()) {
848 // User not started, no need to do anything now. Required bind mounts for the package will
849 // be created when the user starts.
850 return 0;
851 }
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700852 LOG(VERBOSE) << "prepareSandboxForApp: " << packageName << ", appId=" << appId
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700853 << ", sandboxId=" << sandboxId << ", userId=" << userId;
Sudheer Shankac7562092018-08-24 10:20:56 -0700854 mUserPackages[userId].push_back(packageName);
855 mAppIds[packageName] = appId;
856 mSandboxIds[appId] = sandboxId;
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700857
858 std::vector<std::string> visibleVolLabels;
859 for (auto& volId : mVisibleVolumeIds) {
860 auto vol = findVolume(volId);
861 userid_t mountUserId = vol->getMountUserId();
862 if (mountUserId == userId || vol->isEmulated()) {
863 visibleVolLabels.push_back(vol->getLabel());
864 }
Sudheer Shankac7562092018-08-24 10:20:56 -0700865 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700866 return prepareSandboxes(userId, {packageName}, visibleVolLabels);
Sudheer Shankac7562092018-08-24 10:20:56 -0700867}
868
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700869int VolumeManager::destroySandboxForApp(const std::string& packageName,
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700870 const std::string& sandboxId, userid_t userId) {
871 if (!GetBoolProperty(kIsolatedStorage, false)) {
872 return 0;
873 }
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700874 LOG(VERBOSE) << "destroySandboxForApp: " << packageName << ", sandboxId=" << sandboxId
875 << ", userId=" << userId;
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700876 auto& userPackages = mUserPackages[userId];
877 std::remove(userPackages.begin(), userPackages.end(), packageName);
878 // If the package is not uninstalled in any other users, remove appId and sandboxId
879 // corresponding to it from the internal state.
880 bool installedInAnyUser = false;
881 for (auto& it : mUserPackages) {
882 auto& packages = it.second;
883 if (std::find(packages.begin(), packages.end(), packageName) != packages.end()) {
884 installedInAnyUser = true;
885 break;
886 }
887 }
888 if (!installedInAnyUser) {
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700889 const auto& entry = mAppIds.find(packageName);
890 if (entry != mAppIds.end()) {
891 mSandboxIds.erase(entry->second);
892 mAppIds.erase(entry);
893 }
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700894 }
895
896 std::vector<std::string> visibleVolLabels;
897 for (auto& volId : mVisibleVolumeIds) {
898 auto vol = findVolume(volId);
899 userid_t mountUserId = vol->getMountUserId();
900 if (mountUserId == userId || vol->isEmulated()) {
901 if (destroySandboxForAppOnVol(packageName, sandboxId, userId, vol->getLabel()) < 0) {
902 return -errno;
903 }
904 }
905 }
906 return 0;
907}
908
909int VolumeManager::destroySandboxForAppOnVol(const std::string& packageName,
910 const std::string& sandboxId, userid_t userId,
911 const std::string& volLabel) {
912 LOG(VERBOSE) << "destroySandboxOnVol: " << packageName << ", userId=" << userId
913 << ", volLabel=" << volLabel;
914 std::string pkgSandboxTarget =
915 StringPrintf("/mnt/user/%d/package/%s", userId, packageName.c_str());
916 if (android::vold::UnmountTree(pkgSandboxTarget)) {
917 PLOG(ERROR) << "UnmountTree failed on " << pkgSandboxTarget;
918 }
919
920 std::string sandboxDir = StringPrintf("/mnt/runtime/write/%s", volLabel.c_str());
921 if (volLabel == mPrimary->getLabel() && mPrimary->isEmulated()) {
922 StringAppendF(&sandboxDir, "/%d", userId);
923 }
Sudheer Shanka51a38da2018-10-18 08:51:31 -0700924 StringAppendF(&sandboxDir, "/Android/sandbox/%s", sandboxId.c_str());
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700925
926 if (android::vold::DeleteDirContentsAndDir(sandboxDir) < 0) {
927 PLOG(ERROR) << "DeleteDirContentsAndDir failed on " << sandboxDir;
928 return -errno;
929 }
930 return 0;
931}
932
Jeff Sharkey401b2602017-12-14 22:15:20 -0700933int VolumeManager::onSecureKeyguardStateChanged(bool isShowing) {
934 mSecureKeyguardShowing = isShowing;
935 if (!mSecureKeyguardShowing) {
936 // Now that secure keyguard has been dismissed, process
937 // any pending disks
938 for (const auto& disk : mPendingDisks) {
939 disk->create();
940 mDisks.push_back(disk);
941 }
942 mPendingDisks.clear();
943 }
944 return 0;
945}
946
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700947int VolumeManager::onVolumeMounted(android::vold::VolumeBase* vol) {
948 if (!GetBoolProperty(kIsolatedStorage, false)) {
949 return 0;
950 }
951
952 if ((vol->getMountFlags() & android::vold::VoldNativeService::MOUNT_FLAG_VISIBLE) == 0) {
953 return 0;
954 }
955
956 mVisibleVolumeIds.insert(vol->getId());
957 userid_t mountUserId = vol->getMountUserId();
958 if ((vol->getMountFlags() & android::vold::VoldNativeService::MOUNT_FLAG_PRIMARY) != 0) {
959 // We don't want to create another shared_ptr here because then we will end up with
960 // two shared_ptrs owning the underlying pointer without sharing it.
961 mPrimary = findVolume(vol->getId());
962 for (userid_t userId : mStartedUsers) {
963 if (linkPrimary(userId) != 0) {
964 return -errno;
965 }
966 }
967 }
968 if (vol->isEmulated()) {
969 for (userid_t userId : mStartedUsers) {
970 if (prepareSandboxes(userId, mUserPackages[userId], {vol->getLabel()}) != 0) {
971 return -errno;
972 }
973 }
974 } else if (mStartedUsers.find(mountUserId) != mStartedUsers.end()) {
975 if (prepareSandboxes(mountUserId, mUserPackages[mountUserId], {vol->getLabel()}) != 0) {
976 return -errno;
977 }
978 }
979 return 0;
980}
981
982int VolumeManager::onVolumeUnmounted(android::vold::VolumeBase* vol) {
983 if (!GetBoolProperty(kIsolatedStorage, false)) {
984 return 0;
985 }
986
987 if (mVisibleVolumeIds.erase(vol->getId()) == 0) {
988 return 0;
989 }
990
991 if ((vol->getMountFlags() & android::vold::VoldNativeService::MOUNT_FLAG_PRIMARY) != 0) {
992 mPrimary = nullptr;
993 }
994
995 LOG(VERBOSE) << "visibleVolumeUnmounted: " << vol;
996 userid_t mountUserId = vol->getMountUserId();
997 if (vol->isEmulated()) {
998 for (userid_t userId : mStartedUsers) {
999 if (destroySandboxesForVol(vol, userId) != 0) {
1000 return -errno;
1001 }
1002 }
1003 } else if (mStartedUsers.find(mountUserId) != mStartedUsers.end()) {
1004 if (destroySandboxesForVol(vol, mountUserId) != 0) {
1005 return -errno;
1006 }
1007 }
1008 return 0;
1009}
1010
1011int VolumeManager::destroySandboxesForVol(android::vold::VolumeBase* vol, userid_t userId) {
1012 LOG(VERBOSE) << "destroysandboxesForVol: " << vol << " for user=" << userId;
1013 const std::vector<std::string>& packageNames = mUserPackages[userId];
1014 for (auto& packageName : packageNames) {
1015 std::string volSandboxRoot = StringPrintf("/mnt/user/%d/package/%s/%s", userId,
1016 packageName.c_str(), vol->getLabel().c_str());
1017 if (android::vold::UnmountTree(volSandboxRoot) != 0) {
1018 PLOG(ERROR) << "unmountTree on " << volSandboxRoot << " failed";
1019 continue;
1020 }
1021 if (android::vold::DeleteDirContentsAndDir(volSandboxRoot) < 0) {
1022 PLOG(ERROR) << "DeleteDirContentsAndDir failed on " << volSandboxRoot;
1023 continue;
1024 }
1025 LOG(VERBOSE) << "Success: DeleteDirContentsAndDir on " << volSandboxRoot;
1026 }
1027 return 0;
1028}
1029
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001030int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001031 if (GetBoolProperty(kIsolatedStorage, false)) {
1032 return 0;
1033 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001034 mPrimary = vol;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -07001035 for (userid_t userId : mStartedUsers) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001036 linkPrimary(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001037 }
1038 return 0;
1039}
1040
Jeff Sharkey66270a22015-06-24 11:49:24 -07001041int VolumeManager::remountUid(uid_t uid, const std::string& mode) {
Sudheer Shanka53947a32018-08-01 10:24:13 -07001042 // If the isolated storage is enabled, return -1 since in the isolated storage world, there
1043 // are no longer any runtime storage permissions, so this shouldn't be called anymore.
1044 if (GetBoolProperty(kIsolatedStorage, false)) {
1045 return -1;
1046 }
Jeff Sharkey66270a22015-06-24 11:49:24 -07001047 LOG(DEBUG) << "Remounting " << uid << " as mode " << mode;
1048
1049 DIR* dir;
1050 struct dirent* de;
Jeff Sharkey3472e522017-10-06 18:02:53 -06001051 std::string rootName;
1052 std::string pidName;
Jeff Sharkey66270a22015-06-24 11:49:24 -07001053 int pidFd;
1054 int nsFd;
1055 struct stat sb;
1056 pid_t child;
1057
1058 if (!(dir = opendir("/proc"))) {
1059 PLOG(ERROR) << "Failed to opendir";
1060 return -1;
1061 }
1062
1063 // Figure out root namespace to compare against below
Jeff Sharkey3472e522017-10-06 18:02:53 -06001064 if (!android::vold::Readlinkat(dirfd(dir), "1/ns/mnt", &rootName)) {
1065 PLOG(ERROR) << "Failed to read root namespace";
Jeff Sharkey66270a22015-06-24 11:49:24 -07001066 closedir(dir);
1067 return -1;
1068 }
1069
1070 // Poke through all running PIDs look for apps running as UID
1071 while ((de = readdir(dir))) {
Jeff Vander Stoep58890832017-10-23 17:12:31 -07001072 pid_t pid;
1073 if (de->d_type != DT_DIR) continue;
1074 if (!android::base::ParseInt(de->d_name, &pid)) continue;
1075
Jeff Sharkey66270a22015-06-24 11:49:24 -07001076 pidFd = -1;
1077 nsFd = -1;
1078
1079 pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
1080 if (pidFd < 0) {
1081 goto next;
1082 }
1083 if (fstat(pidFd, &sb) != 0) {
1084 PLOG(WARNING) << "Failed to stat " << de->d_name;
1085 goto next;
1086 }
1087 if (sb.st_uid != uid) {
1088 goto next;
1089 }
1090
1091 // Matches so far, but refuse to touch if in root namespace
1092 LOG(DEBUG) << "Found matching PID " << de->d_name;
Jeff Sharkey3472e522017-10-06 18:02:53 -06001093 if (!android::vold::Readlinkat(pidFd, "ns/mnt", &pidName)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -07001094 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
1095 goto next;
1096 }
Jeff Sharkey3472e522017-10-06 18:02:53 -06001097 if (rootName == pidName) {
Jeff Sharkey66270a22015-06-24 11:49:24 -07001098 LOG(WARNING) << "Skipping due to root namespace";
1099 goto next;
1100 }
1101
1102 // We purposefully leave the namespace open across the fork
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001103 nsFd = openat(pidFd, "ns/mnt", O_RDONLY); // not O_CLOEXEC
Jeff Sharkey66270a22015-06-24 11:49:24 -07001104 if (nsFd < 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -07001105 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -07001106 goto next;
1107 }
1108
1109 if (!(child = fork())) {
1110 if (setns(nsFd, CLONE_NEWNS) != 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -07001111 PLOG(ERROR) << "Failed to setns for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -07001112 _exit(1);
1113 }
1114
Sudheer Shanka99d304a2018-09-27 14:53:51 -07001115 android::vold::UnmountTree("/storage/");
Jeff Sharkey66270a22015-06-24 11:49:24 -07001116
1117 std::string storageSource;
1118 if (mode == "default") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -07001119 storageSource = "/mnt/runtime/default";
Jeff Sharkey66270a22015-06-24 11:49:24 -07001120 } else if (mode == "read") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -07001121 storageSource = "/mnt/runtime/read";
Jeff Sharkey66270a22015-06-24 11:49:24 -07001122 } else if (mode == "write") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -07001123 storageSource = "/mnt/runtime/write";
Jeff Sharkey66270a22015-06-24 11:49:24 -07001124 } else {
1125 // Sane default of no storage visible
1126 _exit(0);
1127 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001128 if (TEMP_FAILURE_RETRY(
1129 mount(storageSource.c_str(), "/storage", NULL, MS_BIND | MS_REC, NULL)) == -1) {
1130 PLOG(ERROR) << "Failed to mount " << storageSource << " for " << de->d_name;
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -07001131 _exit(1);
Jeff Sharkey66270a22015-06-24 11:49:24 -07001132 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001133 if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL, MS_REC | MS_SLAVE, NULL)) == -1) {
1134 PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for " << de->d_name;
Hidehiko Abe674bed12016-03-09 16:42:10 +09001135 _exit(1);
1136 }
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -07001137
1138 // Mount user-specific symlink helper into place
1139 userid_t user_id = multiuser_get_user_id(uid);
1140 std::string userSource(StringPrintf("/mnt/user/%d", user_id));
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001141 if (TEMP_FAILURE_RETRY(
1142 mount(userSource.c_str(), "/storage/self", NULL, MS_BIND, NULL)) == -1) {
1143 PLOG(ERROR) << "Failed to mount " << userSource << " for " << de->d_name;
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -07001144 _exit(1);
1145 }
1146
Jeff Sharkey66270a22015-06-24 11:49:24 -07001147 _exit(0);
1148 }
1149
1150 if (child == -1) {
1151 PLOG(ERROR) << "Failed to fork";
1152 goto next;
1153 } else {
1154 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
1155 }
1156
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001157 next:
Jeff Sharkey66270a22015-06-24 11:49:24 -07001158 close(nsFd);
1159 close(pidFd);
1160 }
1161 closedir(dir);
1162 return 0;
1163}
1164
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001165int VolumeManager::reset() {
1166 // Tear down all existing disks/volumes and start from a blank slate so
1167 // newly connected framework hears all events.
Gao Xiangd263da82017-08-14 11:32:13 +08001168 if (mInternalEmulated != nullptr) {
1169 mInternalEmulated->destroy();
1170 mInternalEmulated->create();
1171 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -07001172 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001173 disk->destroy();
1174 disk->create();
1175 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -06001176 updateVirtualDisk();
Jeff Sharkeybd3038d2015-06-10 09:42:01 -07001177 mAddedUsers.clear();
1178 mStartedUsers.clear();
Sudheer Shanka62bbb2b2018-08-01 01:09:10 -07001179
1180 mUserPackages.clear();
1181 mAppIds.clear();
1182 mSandboxIds.clear();
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001183 mVisibleVolumeIds.clear();
Sudheer Shanka3a7ee5c2018-08-07 17:10:11 -07001184
1185 // For unmounting dirs under /mnt/user/<user-id>/package/<package-name>
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001186 android::vold::UnmountTree("/mnt/user/");
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001187 return 0;
1188}
1189
Keun-young Parka5bbb5e2017-03-13 18:02:50 -07001190// Can be called twice (sequentially) during shutdown. should be safe for that.
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001191int VolumeManager::shutdown() {
Keun-young Parka5bbb5e2017-03-13 18:02:50 -07001192 if (mInternalEmulated == nullptr) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001193 return 0; // already shutdown
Keun-young Parka5bbb5e2017-03-13 18:02:50 -07001194 }
Paul Crowley56292ef2017-10-20 08:07:53 -07001195 android::vold::sSleepOnUnmount = false;
Jeff Sharkey9c484982015-03-31 10:35:33 -07001196 mInternalEmulated->destroy();
Keun-young Parka5bbb5e2017-03-13 18:02:50 -07001197 mInternalEmulated = nullptr;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -07001198 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001199 disk->destroy();
1200 }
Risan8c9f3322018-10-29 08:52:56 +09001201 mStubVolumes.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001202 mDisks.clear();
Jeff Sharkey401b2602017-12-14 22:15:20 -07001203 mPendingDisks.clear();
Paul Crowley56292ef2017-10-20 08:07:53 -07001204 android::vold::sSleepOnUnmount = true;
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001205 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -07001206}
1207
Jeff Sharkey9c484982015-03-31 10:35:33 -07001208int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -07001209 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey67b8c492017-09-21 17:08:43 -06001210 ATRACE_NAME("VolumeManager::unmountAll()");
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -07001211
Jeff Sharkey9c484982015-03-31 10:35:33 -07001212 // First, try gracefully unmounting all known devices
1213 if (mInternalEmulated != nullptr) {
1214 mInternalEmulated->unmount();
1215 }
Risan8c9f3322018-10-29 08:52:56 +09001216 for (const auto& stub : mStubVolumes) {
1217 stub->unmount();
1218 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -07001219 for (const auto& disk : mDisks) {
Jeff Sharkey9c484982015-03-31 10:35:33 -07001220 disk->unmountAll();
1221 }
1222
1223 // Worst case we might have some stale mounts lurking around, so
1224 // force unmount those just to be safe.
LongPing.WEI4f046062018-11-23 19:27:35 +08001225 FILE* fp = setmntent("/proc/mounts", "re");
Jeff Sharkey9c484982015-03-31 10:35:33 -07001226 if (fp == NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -06001227 PLOG(ERROR) << "Failed to open /proc/mounts";
Jeff Sharkey9c484982015-03-31 10:35:33 -07001228 return -errno;
1229 }
1230
1231 // Some volumes can be stacked on each other, so force unmount in
1232 // reverse order to give us the best chance of success.
1233 std::list<std::string> toUnmount;
1234 mntent* mentry;
1235 while ((mentry = getmntent(fp)) != NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -06001236 auto test = std::string(mentry->mnt_dir);
Mark Salyzyn86e81e72018-09-20 10:09:27 -07001237 if ((StartsWith(test, "/mnt/") &&
1238#ifdef __ANDROID_DEBUGGABLE__
1239 !StartsWith(test, "/mnt/scratch") &&
1240#endif
1241 !StartsWith(test, "/mnt/vendor") && !StartsWith(test, "/mnt/product")) ||
1242 StartsWith(test, "/storage/")) {
Jeff Sharkey3472e522017-10-06 18:02:53 -06001243 toUnmount.push_front(test);
Jeff Sharkey9c484982015-03-31 10:35:33 -07001244 }
1245 }
1246 endmntent(fp);
1247
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -07001248 for (const auto& path : toUnmount) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001249 LOG(DEBUG) << "Tearing down stale mount " << path;
Jeff Sharkey9c484982015-03-31 10:35:33 -07001250 android::vold::ForceUnmount(path);
1251 }
1252
1253 return 0;
1254}
1255
Jeff Sharkey3472e522017-10-06 18:02:53 -06001256int VolumeManager::mkdirs(const std::string& path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001257 // Only offer to create directories for paths managed by vold
Mark Salyzyn86e81e72018-09-20 10:09:27 -07001258 if (StartsWith(path, "/storage/")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001259 // fs_mkdirs() does symlink checking and relative path enforcement
Jeff Sharkey3472e522017-10-06 18:02:53 -06001260 return fs_mkdirs(path.c_str(), 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001261 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -06001262 LOG(ERROR) << "Failed to find mounted volume for " << path;
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001263 return -EINVAL;
1264 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001265}
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001266
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001267int VolumeManager::createObb(const std::string& sourcePath, const std::string& sourceKey,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001268 int32_t ownerGid, std::string* outVolId) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001269 int id = mNextObbId++;
1270
1271 auto vol = std::shared_ptr<android::vold::VolumeBase>(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001272 new android::vold::ObbVolume(id, sourcePath, sourceKey, ownerGid));
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001273 vol->create();
1274
1275 mObbVolumes.push_back(vol);
1276 *outVolId = vol->getId();
1277 return android::OK;
1278}
1279
1280int VolumeManager::destroyObb(const std::string& volId) {
1281 auto i = mObbVolumes.begin();
1282 while (i != mObbVolumes.end()) {
1283 if ((*i)->getId() == volId) {
1284 (*i)->destroy();
1285 i = mObbVolumes.erase(i);
1286 } else {
1287 ++i;
1288 }
1289 }
1290 return android::OK;
1291}
1292
Risan8c9f3322018-10-29 08:52:56 +09001293int VolumeManager::createStubVolume(const std::string& sourcePath, const std::string& mountPath,
1294 const std::string& fsType, const std::string& fsUuid,
1295 const std::string& fsLabel, std::string* outVolId) {
1296 int id = mNextStubVolumeId++;
1297 auto vol = std::shared_ptr<android::vold::VolumeBase>(
1298 new android::vold::StubVolume(id, sourcePath, mountPath, fsType, fsUuid, fsLabel));
1299 vol->create();
1300
1301 mStubVolumes.push_back(vol);
1302 *outVolId = vol->getId();
1303 return android::OK;
1304}
1305
1306int VolumeManager::destroyStubVolume(const std::string& volId) {
1307 auto i = mStubVolumes.begin();
1308 while (i != mStubVolumes.end()) {
1309 if ((*i)->getId() == volId) {
1310 (*i)->destroy();
1311 i = mStubVolumes.erase(i);
1312 } else {
1313 ++i;
1314 }
1315 }
1316 return android::OK;
1317}
1318
Risan8f6198d2018-10-26 20:56:45 -06001319int VolumeManager::mountAppFuse(uid_t uid, int mountId, unique_fd* device_fd) {
Risanac02a482018-10-31 21:59:47 -06001320 return android::vold::MountAppFuse(uid, mountId, device_fd);
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001321}
1322
Risan8f6198d2018-10-26 20:56:45 -06001323int VolumeManager::unmountAppFuse(uid_t uid, int mountId) {
Risanac02a482018-10-31 21:59:47 -06001324 return android::vold::UnmountAppFuse(uid, mountId);
Risan8f6198d2018-10-26 20:56:45 -06001325}
1326
1327int VolumeManager::openAppFuseFile(uid_t uid, int mountId, int fileId, int flags) {
Risanac02a482018-10-31 21:59:47 -06001328 return android::vold::OpenAppFuseFile(uid, mountId, fileId, flags);
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001329}