2009-10-11 02:22:08 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-09-22 01:08:43 +02:00
|
|
|
#define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
|
|
|
|
|
2015-01-02 22:28:28 +01:00
|
|
|
#include <dirent.h>
|
2009-10-11 02:22:08 +02:00
|
|
|
#include <errno.h>
|
2009-12-13 19:40:18 +01:00
|
|
|
#include <fcntl.h>
|
2015-01-02 22:28:28 +01:00
|
|
|
#include <mntent.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mount.h>
|
2010-01-06 19:33:53 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2017-05-18 18:08:24 +02:00
|
|
|
#include <sys/sysmacros.h>
|
2015-06-24 20:49:24 +02:00
|
|
|
#include <sys/wait.h>
|
2015-01-02 22:28:28 +01:00
|
|
|
#include <unistd.h>
|
2010-01-06 19:33:53 +01:00
|
|
|
|
2009-12-13 19:40:18 +01:00
|
|
|
#include <linux/kdev_t.h>
|
2009-10-11 02:22:08 +02:00
|
|
|
|
2015-12-05 00:50:53 +01:00
|
|
|
#include <android-base/logging.h>
|
2017-10-24 02:12:31 +02:00
|
|
|
#include <android-base/parseint.h>
|
2017-10-07 02:02:53 +02:00
|
|
|
#include <android-base/properties.h>
|
2015-12-05 00:50:53 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
2017-10-24 02:12:31 +02:00
|
|
|
#include <android-base/strings.h>
|
|
|
|
|
2013-09-18 02:24:38 +02:00
|
|
|
#include <cutils/fs.h>
|
2017-09-22 01:08:43 +02:00
|
|
|
#include <utils/Trace.h>
|
2009-10-11 02:22:08 +02:00
|
|
|
|
2014-02-04 16:53:00 +01:00
|
|
|
#include <selinux/android.h>
|
|
|
|
|
2009-10-12 20:32:47 +02:00
|
|
|
#include <sysutils/NetlinkEvent.h>
|
|
|
|
|
2012-04-04 02:23:01 +02:00
|
|
|
#include <private/android_filesystem_config.h>
|
|
|
|
|
2017-10-24 23:54:43 +02:00
|
|
|
#include <ext4_utils/ext4_crypt.h>
|
|
|
|
|
2010-01-10 22:02:12 +01:00
|
|
|
#include "Devmapper.h"
|
2017-10-24 23:54:43 +02:00
|
|
|
#include "Ext4Crypt.h"
|
|
|
|
#include "Loop.h"
|
|
|
|
#include "NetlinkManager.h"
|
2010-02-17 02:12:00 +01:00
|
|
|
#include "Process.h"
|
2017-10-24 23:54:43 +02:00
|
|
|
#include "Utils.h"
|
2015-02-04 05:29:15 +01:00
|
|
|
#include "VoldUtil.h"
|
2017-10-24 23:54:43 +02:00
|
|
|
#include "VolumeManager.h"
|
2011-05-19 02:20:07 +02:00
|
|
|
#include "cryptfs.h"
|
2017-10-24 23:54:43 +02:00
|
|
|
#include "fs/Ext4.h"
|
|
|
|
#include "fs/Vfat.h"
|
|
|
|
#include "model/EmulatedVolume.h"
|
|
|
|
#include "model/ObbVolume.h"
|
2010-01-09 16:08:06 +01:00
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
using android::base::StringPrintf;
|
2017-09-11 18:32:01 +02:00
|
|
|
using android::base::unique_fd;
|
2015-03-14 00:09:20 +01:00
|
|
|
|
2017-03-26 06:49:13 +02:00
|
|
|
static const char* kPathUserMount = "/mnt/user";
|
|
|
|
static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk";
|
|
|
|
|
|
|
|
static const char* kPropVirtualDisk = "persist.sys.virtual_disk";
|
|
|
|
|
|
|
|
/* 512MiB is large enough for testing purposes */
|
|
|
|
static const unsigned int kSizeVirtualDisk = 536870912;
|
2015-03-14 00:09:20 +01:00
|
|
|
|
|
|
|
static const unsigned int kMajorBlockMmc = 179;
|
2016-01-08 10:36:47 +01:00
|
|
|
static const unsigned int kMajorBlockExperimentalMin = 240;
|
|
|
|
static const unsigned int kMajorBlockExperimentalMax = 254;
|
2015-03-14 00:09:20 +01:00
|
|
|
|
2009-10-11 02:22:08 +02:00
|
|
|
VolumeManager *VolumeManager::sInstance = NULL;
|
|
|
|
|
|
|
|
VolumeManager *VolumeManager::Instance() {
|
|
|
|
if (!sInstance)
|
|
|
|
sInstance = new VolumeManager();
|
|
|
|
return sInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
VolumeManager::VolumeManager() {
|
2010-03-12 22:32:47 +01:00
|
|
|
mDebug = false;
|
2017-09-11 18:32:01 +02:00
|
|
|
mNextObbId = 0;
|
2017-12-15 06:15:20 +01:00
|
|
|
// For security reasons, assume that a secure keyguard is
|
|
|
|
// showing until we hear otherwise
|
|
|
|
mSecureKeyguardShowing = true;
|
2009-10-11 02:22:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VolumeManager::~VolumeManager() {
|
2010-03-12 22:32:47 +01:00
|
|
|
}
|
|
|
|
|
2017-03-26 06:49:13 +02:00
|
|
|
int VolumeManager::updateVirtualDisk() {
|
2017-09-22 01:08:43 +02:00
|
|
|
ATRACE_NAME("VolumeManager::updateVirtualDisk");
|
2017-10-07 02:02:53 +02:00
|
|
|
if (android::base::GetBoolProperty(kPropVirtualDisk, false)) {
|
2017-03-26 06:49:13 +02:00
|
|
|
if (access(kPathVirtualDisk, F_OK) != 0) {
|
|
|
|
Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mVirtualDisk == nullptr) {
|
|
|
|
if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) {
|
|
|
|
LOG(ERROR) << "Failed to create virtual disk";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat buf;
|
|
|
|
if (stat(mVirtualDiskPath.c_str(), &buf) < 0) {
|
|
|
|
PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto disk = new android::vold::Disk("virtual", buf.st_rdev, "virtual",
|
|
|
|
android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd);
|
|
|
|
mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk);
|
2017-12-15 06:15:20 +01:00
|
|
|
handleDiskAdded(mVirtualDisk);
|
2017-03-26 06:49:13 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (mVirtualDisk != nullptr) {
|
|
|
|
dev_t device = mVirtualDisk->getDevice();
|
2017-12-15 06:15:20 +01:00
|
|
|
handleDiskRemoved(device);
|
2017-03-26 06:49:13 +02:00
|
|
|
|
|
|
|
Loop::destroyByDevice(mVirtualDiskPath.c_str());
|
|
|
|
mVirtualDisk = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (access(kPathVirtualDisk, F_OK) == 0) {
|
|
|
|
unlink(kPathVirtualDisk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-18 02:35:20 +02:00
|
|
|
int VolumeManager::setDebug(bool enable) {
|
2010-03-12 22:32:47 +01:00
|
|
|
mDebug = enable;
|
2015-04-18 02:35:20 +02:00
|
|
|
return 0;
|
2010-03-12 22:32:47 +01:00
|
|
|
}
|
|
|
|
|
2009-10-11 02:22:08 +02:00
|
|
|
int VolumeManager::start() {
|
2017-09-22 01:08:43 +02:00
|
|
|
ATRACE_NAME("VolumeManager::start");
|
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
// Always start from a clean slate by unmounting everything in
|
|
|
|
// directories that we own, in case we crashed.
|
2015-03-31 19:35:33 +02:00
|
|
|
unmountAll();
|
2015-03-14 00:09:20 +01:00
|
|
|
|
2017-09-11 18:32:01 +02:00
|
|
|
Devmapper::destroyAll();
|
|
|
|
Loop::destroyAll();
|
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
// Assume that we always have an emulated volume on internal
|
|
|
|
// storage; the framework will decide if it should be mounted.
|
2015-04-21 21:14:17 +02:00
|
|
|
CHECK(mInternalEmulated == nullptr);
|
2015-03-14 00:09:20 +01:00
|
|
|
mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
|
2015-04-13 01:03:33 +02:00
|
|
|
new android::vold::EmulatedVolume("/data/media"));
|
2015-03-14 00:09:20 +01:00
|
|
|
mInternalEmulated->create();
|
|
|
|
|
2017-03-26 06:49:13 +02:00
|
|
|
// Consider creating a virtual disk
|
|
|
|
updateVirtualDisk();
|
|
|
|
|
2009-10-11 02:22:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VolumeManager::stop() {
|
2015-04-21 21:14:17 +02:00
|
|
|
CHECK(mInternalEmulated != nullptr);
|
2015-03-14 00:09:20 +01:00
|
|
|
mInternalEmulated->destroy();
|
|
|
|
mInternalEmulated = nullptr;
|
2009-10-11 02:22:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-12 20:32:47 +02:00
|
|
|
void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
|
2015-04-21 21:14:17 +02:00
|
|
|
std::lock_guard<std::mutex> lock(mLock);
|
|
|
|
|
2015-04-18 02:35:20 +02:00
|
|
|
if (mDebug) {
|
|
|
|
LOG(VERBOSE) << "----------------";
|
|
|
|
LOG(VERBOSE) << "handleBlockEvent with action " << (int) evt->getAction();
|
|
|
|
evt->dump();
|
|
|
|
}
|
2009-10-11 02:22:08 +02:00
|
|
|
|
2015-08-03 16:39:19 +02:00
|
|
|
std::string eventPath(evt->findParam("DEVPATH")?evt->findParam("DEVPATH"):"");
|
|
|
|
std::string devType(evt->findParam("DEVTYPE")?evt->findParam("DEVTYPE"):"");
|
2015-03-14 00:09:20 +01:00
|
|
|
|
|
|
|
if (devType != "disk") return;
|
|
|
|
|
2017-09-19 02:19:28 +02:00
|
|
|
int major = std::stoi(evt->findParam("MAJOR"));
|
|
|
|
int minor = std::stoi(evt->findParam("MINOR"));
|
2015-03-14 00:09:20 +01:00
|
|
|
dev_t device = makedev(major, minor);
|
|
|
|
|
|
|
|
switch (evt->getAction()) {
|
|
|
|
case NetlinkEvent::Action::kAdd: {
|
2016-07-27 23:11:02 +02:00
|
|
|
for (const auto& source : mDiskSources) {
|
2015-03-14 00:09:20 +01:00
|
|
|
if (source->matches(eventPath)) {
|
2016-01-08 10:36:47 +01:00
|
|
|
// For now, assume that MMC and virtio-blk (the latter is
|
|
|
|
// emulator-specific; see Disk.cpp for details) devices are SD,
|
|
|
|
// and that everything else is USB
|
2015-03-14 00:09:20 +01:00
|
|
|
int flags = source->getFlags();
|
2016-01-08 10:36:47 +01:00
|
|
|
if (major == kMajorBlockMmc
|
|
|
|
|| (android::vold::IsRunningInEmulator()
|
|
|
|
&& major >= (int) kMajorBlockExperimentalMin
|
|
|
|
&& major <= (int) kMajorBlockExperimentalMax)) {
|
2015-03-14 00:09:20 +01:00
|
|
|
flags |= android::vold::Disk::Flags::kSd;
|
|
|
|
} else {
|
|
|
|
flags |= android::vold::Disk::Flags::kUsb;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto disk = new android::vold::Disk(eventPath, device,
|
|
|
|
source->getNickname(), flags);
|
2017-12-15 06:15:20 +01:00
|
|
|
handleDiskAdded(std::shared_ptr<android::vold::Disk>(disk));
|
2015-03-14 00:09:20 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NetlinkEvent::Action::kChange: {
|
2015-04-15 08:14:23 +02:00
|
|
|
LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
|
2017-12-15 06:15:20 +01:00
|
|
|
handleDiskChanged(device);
|
2015-03-14 00:09:20 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NetlinkEvent::Action::kRemove: {
|
2017-12-15 06:15:20 +01:00
|
|
|
handleDiskRemoved(device);
|
2015-03-14 00:09:20 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
LOG(WARNING) << "Unexpected block event action " << (int) evt->getAction();
|
|
|
|
break;
|
2009-10-11 02:22:08 +02:00
|
|
|
}
|
2015-03-14 00:09:20 +01:00
|
|
|
}
|
|
|
|
}
|
2009-10-11 02:22:08 +02:00
|
|
|
|
2017-12-15 06:15:20 +01:00
|
|
|
void VolumeManager::handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk) {
|
|
|
|
// For security reasons, if secure keyguard is showing, wait
|
|
|
|
// until the user unlocks the device to actually touch it
|
|
|
|
if (mSecureKeyguardShowing) {
|
|
|
|
LOG(INFO) << "Found disk at " << disk->getEventPath()
|
|
|
|
<< " but delaying scan due to secure keyguard";
|
|
|
|
mPendingDisks.push_back(disk);
|
|
|
|
} else {
|
|
|
|
disk->create();
|
|
|
|
mDisks.push_back(disk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeManager::handleDiskChanged(dev_t device) {
|
|
|
|
for (const auto& disk : mDisks) {
|
|
|
|
if (disk->getDevice() == device) {
|
|
|
|
disk->readMetadata();
|
|
|
|
disk->readPartitions();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For security reasons, we ignore all pending disks, since
|
|
|
|
// we'll scan them once the device is unlocked
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeManager::handleDiskRemoved(dev_t device) {
|
|
|
|
auto i = mDisks.begin();
|
|
|
|
while (i != mDisks.end()) {
|
|
|
|
if ((*i)->getDevice() == device) {
|
|
|
|
(*i)->destroy();
|
|
|
|
i = mDisks.erase(i);
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto j = mPendingDisks.begin();
|
|
|
|
while (j != mPendingDisks.end()) {
|
|
|
|
if ((*j)->getDevice() == device) {
|
|
|
|
j = mPendingDisks.erase(j);
|
|
|
|
} else {
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
|
2017-01-20 20:52:33 +01:00
|
|
|
std::lock_guard<std::mutex> lock(mLock);
|
2015-03-14 00:09:20 +01:00
|
|
|
mDiskSources.push_back(diskSource);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
|
|
|
|
for (auto disk : mDisks) {
|
|
|
|
if (disk->getId() == id) {
|
|
|
|
return disk;
|
|
|
|
}
|
2009-10-11 02:22:08 +02:00
|
|
|
}
|
2015-03-14 00:09:20 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
|
2017-08-14 05:32:13 +02:00
|
|
|
// Vold could receive "mount" after "shutdown" command in the extreme case.
|
|
|
|
// If this happens, mInternalEmulated will equal nullptr and
|
|
|
|
// we need to deal with it in order to avoid null pointer crash.
|
|
|
|
if (mInternalEmulated != nullptr && mInternalEmulated->getId() == id) {
|
2015-03-14 00:09:20 +01:00
|
|
|
return mInternalEmulated;
|
|
|
|
}
|
2016-07-27 23:11:02 +02:00
|
|
|
for (const auto& disk : mDisks) {
|
2015-03-14 00:09:20 +01:00
|
|
|
auto vol = disk->findVolume(id);
|
|
|
|
if (vol != nullptr) {
|
|
|
|
return vol;
|
|
|
|
}
|
|
|
|
}
|
2017-09-11 18:32:01 +02:00
|
|
|
for (const auto& vol : mObbVolumes) {
|
|
|
|
if (vol->getId() == id) {
|
|
|
|
return vol;
|
|
|
|
}
|
|
|
|
}
|
2015-03-14 00:09:20 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-06-26 23:02:09 +02:00
|
|
|
void VolumeManager::listVolumes(android::vold::VolumeBase::Type type,
|
|
|
|
std::list<std::string>& list) {
|
|
|
|
list.clear();
|
2016-07-27 23:11:02 +02:00
|
|
|
for (const auto& disk : mDisks) {
|
2015-06-26 23:02:09 +02:00
|
|
|
disk->listVolumes(type, list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-24 19:08:45 +02:00
|
|
|
int VolumeManager::forgetPartition(const std::string& partGuid, const std::string& fsUuid) {
|
2015-06-18 23:25:08 +02:00
|
|
|
std::string normalizedGuid;
|
|
|
|
if (android::vold::NormalizeHex(partGuid, normalizedGuid)) {
|
|
|
|
LOG(WARNING) << "Invalid GUID " << partGuid;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-10-24 23:54:43 +02:00
|
|
|
bool success = true;
|
2015-06-18 23:25:08 +02:00
|
|
|
std::string keyPath = android::vold::BuildKeyPath(normalizedGuid);
|
|
|
|
if (unlink(keyPath.c_str()) != 0) {
|
|
|
|
LOG(ERROR) << "Failed to unlink " << keyPath;
|
2017-10-24 23:54:43 +02:00
|
|
|
success = false;
|
2015-06-18 23:25:08 +02:00
|
|
|
}
|
2017-10-24 23:54:43 +02:00
|
|
|
if (e4crypt_is_native()) {
|
|
|
|
if (!e4crypt_destroy_volume_keys(fsUuid)) {
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return success ? 0 : -1;
|
2015-06-18 23:25:08 +02:00
|
|
|
}
|
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
int VolumeManager::linkPrimary(userid_t userId) {
|
|
|
|
std::string source(mPrimary->getPath());
|
|
|
|
if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) {
|
|
|
|
source = StringPrintf("%s/%d", source.c_str(), userId);
|
2015-07-21 23:22:01 +02:00
|
|
|
fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT);
|
2015-03-14 00:09:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string target(StringPrintf("/mnt/user/%d/primary", userId));
|
|
|
|
if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) {
|
|
|
|
if (errno != ENOENT) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(WARNING) << "Failed to unlink " << target;
|
2015-03-14 00:09:20 +01:00
|
|
|
}
|
|
|
|
}
|
2015-04-30 00:22:23 +02:00
|
|
|
LOG(DEBUG) << "Linking " << source << " to " << target;
|
2015-03-14 00:09:20 +01:00
|
|
|
if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(WARNING) << "Failed to link";
|
2015-03-14 00:09:20 +01:00
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-10 18:42:01 +02:00
|
|
|
int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
|
|
|
|
mAddedUsers[userId] = userSerialNumber;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VolumeManager::onUserRemoved(userid_t userId) {
|
|
|
|
mAddedUsers.erase(userId);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VolumeManager::onUserStarted(userid_t userId) {
|
2015-03-14 00:09:20 +01:00
|
|
|
// Note that sometimes the system will spin up processes from Zygote
|
|
|
|
// before actually starting the user, so we're okay if Zygote
|
|
|
|
// already created this directory.
|
2017-03-26 06:49:13 +02:00
|
|
|
std::string path(StringPrintf("%s/%d", kPathUserMount, userId));
|
2015-03-14 00:09:20 +01:00
|
|
|
fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
|
|
|
|
|
2015-06-10 18:42:01 +02:00
|
|
|
mStartedUsers.insert(userId);
|
2015-03-14 00:09:20 +01:00
|
|
|
if (mPrimary) {
|
|
|
|
linkPrimary(userId);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-10 18:42:01 +02:00
|
|
|
int VolumeManager::onUserStopped(userid_t userId) {
|
|
|
|
mStartedUsers.erase(userId);
|
2015-03-14 00:09:20 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-15 06:15:20 +01:00
|
|
|
int VolumeManager::onSecureKeyguardStateChanged(bool isShowing) {
|
|
|
|
mSecureKeyguardShowing = isShowing;
|
|
|
|
if (!mSecureKeyguardShowing) {
|
|
|
|
// Now that secure keyguard has been dismissed, process
|
|
|
|
// any pending disks
|
|
|
|
for (const auto& disk : mPendingDisks) {
|
|
|
|
disk->create();
|
|
|
|
mDisks.push_back(disk);
|
|
|
|
}
|
|
|
|
mPendingDisks.clear();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
|
|
|
|
mPrimary = vol;
|
2015-06-10 18:42:01 +02:00
|
|
|
for (userid_t userId : mStartedUsers) {
|
2015-03-14 00:09:20 +01:00
|
|
|
linkPrimary(userId);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-07 02:02:53 +02:00
|
|
|
static int unmount_tree(const std::string& prefix) {
|
2015-07-01 00:54:17 +02:00
|
|
|
FILE* fp = setmntent("/proc/mounts", "r");
|
|
|
|
if (fp == NULL) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Failed to open /proc/mounts";
|
2015-07-01 00:54:17 +02:00
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some volumes can be stacked on each other, so force unmount in
|
|
|
|
// reverse order to give us the best chance of success.
|
|
|
|
std::list<std::string> toUnmount;
|
|
|
|
mntent* mentry;
|
|
|
|
while ((mentry = getmntent(fp)) != NULL) {
|
2017-10-07 02:02:53 +02:00
|
|
|
auto test = std::string(mentry->mnt_dir) + "/";
|
|
|
|
if (android::base::StartsWith(test, prefix.c_str())) {
|
|
|
|
toUnmount.push_front(test);
|
2015-07-01 00:54:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
endmntent(fp);
|
|
|
|
|
2016-07-27 23:11:02 +02:00
|
|
|
for (const auto& path : toUnmount) {
|
2015-07-01 00:54:17 +02:00
|
|
|
if (umount2(path.c_str(), MNT_DETACH)) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Failed to unmount " << path;
|
2015-07-01 00:54:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-24 20:49:24 +02:00
|
|
|
int VolumeManager::remountUid(uid_t uid, const std::string& mode) {
|
|
|
|
LOG(DEBUG) << "Remounting " << uid << " as mode " << mode;
|
|
|
|
|
|
|
|
DIR* dir;
|
|
|
|
struct dirent* de;
|
2017-10-07 02:02:53 +02:00
|
|
|
std::string rootName;
|
|
|
|
std::string pidName;
|
2015-06-24 20:49:24 +02:00
|
|
|
int pidFd;
|
|
|
|
int nsFd;
|
|
|
|
struct stat sb;
|
|
|
|
pid_t child;
|
|
|
|
|
|
|
|
if (!(dir = opendir("/proc"))) {
|
|
|
|
PLOG(ERROR) << "Failed to opendir";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out root namespace to compare against below
|
2017-10-07 02:02:53 +02:00
|
|
|
if (!android::vold::Readlinkat(dirfd(dir), "1/ns/mnt", &rootName)) {
|
|
|
|
PLOG(ERROR) << "Failed to read root namespace";
|
2015-06-24 20:49:24 +02:00
|
|
|
closedir(dir);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Poke through all running PIDs look for apps running as UID
|
|
|
|
while ((de = readdir(dir))) {
|
2017-10-24 02:12:31 +02:00
|
|
|
pid_t pid;
|
|
|
|
if (de->d_type != DT_DIR) continue;
|
|
|
|
if (!android::base::ParseInt(de->d_name, &pid)) continue;
|
|
|
|
|
2015-06-24 20:49:24 +02:00
|
|
|
pidFd = -1;
|
|
|
|
nsFd = -1;
|
|
|
|
|
|
|
|
pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
|
|
|
|
if (pidFd < 0) {
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
if (fstat(pidFd, &sb) != 0) {
|
|
|
|
PLOG(WARNING) << "Failed to stat " << de->d_name;
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
if (sb.st_uid != uid) {
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matches so far, but refuse to touch if in root namespace
|
|
|
|
LOG(DEBUG) << "Found matching PID " << de->d_name;
|
2017-10-07 02:02:53 +02:00
|
|
|
if (!android::vold::Readlinkat(pidFd, "ns/mnt", &pidName)) {
|
2015-06-24 20:49:24 +02:00
|
|
|
PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
|
|
|
|
goto next;
|
|
|
|
}
|
2017-10-07 02:02:53 +02:00
|
|
|
if (rootName == pidName) {
|
2015-06-24 20:49:24 +02:00
|
|
|
LOG(WARNING) << "Skipping due to root namespace";
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We purposefully leave the namespace open across the fork
|
2017-03-27 18:49:21 +02:00
|
|
|
nsFd = openat(pidFd, "ns/mnt", O_RDONLY); // not O_CLOEXEC
|
2015-06-24 20:49:24 +02:00
|
|
|
if (nsFd < 0) {
|
2015-07-01 00:54:17 +02:00
|
|
|
PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
|
2015-06-24 20:49:24 +02:00
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(child = fork())) {
|
|
|
|
if (setns(nsFd, CLONE_NEWNS) != 0) {
|
2015-07-01 00:54:17 +02:00
|
|
|
PLOG(ERROR) << "Failed to setns for " << de->d_name;
|
2015-06-24 20:49:24 +02:00
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
|
2017-10-07 02:02:53 +02:00
|
|
|
unmount_tree("/storage/");
|
2015-06-24 20:49:24 +02:00
|
|
|
|
|
|
|
std::string storageSource;
|
|
|
|
if (mode == "default") {
|
2015-08-06 20:40:00 +02:00
|
|
|
storageSource = "/mnt/runtime/default";
|
2015-06-24 20:49:24 +02:00
|
|
|
} else if (mode == "read") {
|
2015-08-06 20:40:00 +02:00
|
|
|
storageSource = "/mnt/runtime/read";
|
2015-06-24 20:49:24 +02:00
|
|
|
} else if (mode == "write") {
|
2015-08-06 20:40:00 +02:00
|
|
|
storageSource = "/mnt/runtime/write";
|
2015-06-24 20:49:24 +02:00
|
|
|
} else {
|
|
|
|
// Sane default of no storage visible
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
if (TEMP_FAILURE_RETRY(mount(storageSource.c_str(), "/storage",
|
2016-03-09 08:42:10 +01:00
|
|
|
NULL, MS_BIND | MS_REC, NULL)) == -1) {
|
2015-07-01 00:54:17 +02:00
|
|
|
PLOG(ERROR) << "Failed to mount " << storageSource << " for "
|
|
|
|
<< de->d_name;
|
|
|
|
_exit(1);
|
2015-06-24 20:49:24 +02:00
|
|
|
}
|
2016-03-09 08:42:10 +01:00
|
|
|
if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL,
|
|
|
|
MS_REC | MS_SLAVE, NULL)) == -1) {
|
|
|
|
PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for "
|
|
|
|
<< de->d_name;
|
|
|
|
_exit(1);
|
|
|
|
}
|
2015-07-01 00:54:17 +02:00
|
|
|
|
|
|
|
// Mount user-specific symlink helper into place
|
|
|
|
userid_t user_id = multiuser_get_user_id(uid);
|
|
|
|
std::string userSource(StringPrintf("/mnt/user/%d", user_id));
|
|
|
|
if (TEMP_FAILURE_RETRY(mount(userSource.c_str(), "/storage/self",
|
|
|
|
NULL, MS_BIND, NULL)) == -1) {
|
|
|
|
PLOG(ERROR) << "Failed to mount " << userSource << " for "
|
|
|
|
<< de->d_name;
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
|
2015-06-24 20:49:24 +02:00
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (child == -1) {
|
|
|
|
PLOG(ERROR) << "Failed to fork";
|
|
|
|
goto next;
|
|
|
|
} else {
|
|
|
|
TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
next:
|
|
|
|
close(nsFd);
|
|
|
|
close(pidFd);
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
int VolumeManager::reset() {
|
|
|
|
// Tear down all existing disks/volumes and start from a blank slate so
|
|
|
|
// newly connected framework hears all events.
|
2017-08-14 05:32:13 +02:00
|
|
|
if (mInternalEmulated != nullptr) {
|
|
|
|
mInternalEmulated->destroy();
|
|
|
|
mInternalEmulated->create();
|
|
|
|
}
|
2016-07-27 23:11:02 +02:00
|
|
|
for (const auto& disk : mDisks) {
|
2015-03-14 00:09:20 +01:00
|
|
|
disk->destroy();
|
|
|
|
disk->create();
|
|
|
|
}
|
2017-03-26 06:49:13 +02:00
|
|
|
updateVirtualDisk();
|
2015-06-10 18:42:01 +02:00
|
|
|
mAddedUsers.clear();
|
|
|
|
mStartedUsers.clear();
|
2015-03-14 00:09:20 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-14 02:02:50 +01:00
|
|
|
// Can be called twice (sequentially) during shutdown. should be safe for that.
|
2015-03-14 00:09:20 +01:00
|
|
|
int VolumeManager::shutdown() {
|
2017-03-14 02:02:50 +01:00
|
|
|
if (mInternalEmulated == nullptr) {
|
|
|
|
return 0; // already shutdown
|
|
|
|
}
|
2017-10-20 17:07:53 +02:00
|
|
|
android::vold::sSleepOnUnmount = false;
|
2015-03-31 19:35:33 +02:00
|
|
|
mInternalEmulated->destroy();
|
2017-03-14 02:02:50 +01:00
|
|
|
mInternalEmulated = nullptr;
|
2016-07-27 23:11:02 +02:00
|
|
|
for (const auto& disk : mDisks) {
|
2015-03-14 00:09:20 +01:00
|
|
|
disk->destroy();
|
|
|
|
}
|
|
|
|
mDisks.clear();
|
2017-12-15 06:15:20 +01:00
|
|
|
mPendingDisks.clear();
|
2017-10-20 17:07:53 +02:00
|
|
|
android::vold::sSleepOnUnmount = true;
|
2015-03-14 00:09:20 +01:00
|
|
|
return 0;
|
2009-10-11 02:22:08 +02:00
|
|
|
}
|
|
|
|
|
2015-03-31 19:35:33 +02:00
|
|
|
int VolumeManager::unmountAll() {
|
2015-04-21 21:14:17 +02:00
|
|
|
std::lock_guard<std::mutex> lock(mLock);
|
2017-09-22 01:08:43 +02:00
|
|
|
ATRACE_NAME("VolumeManager::unmountAll()");
|
2015-04-21 21:14:17 +02:00
|
|
|
|
2015-03-31 19:35:33 +02:00
|
|
|
// First, try gracefully unmounting all known devices
|
|
|
|
if (mInternalEmulated != nullptr) {
|
|
|
|
mInternalEmulated->unmount();
|
|
|
|
}
|
2016-07-27 23:11:02 +02:00
|
|
|
for (const auto& disk : mDisks) {
|
2015-03-31 19:35:33 +02:00
|
|
|
disk->unmountAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Worst case we might have some stale mounts lurking around, so
|
|
|
|
// force unmount those just to be safe.
|
|
|
|
FILE* fp = setmntent("/proc/mounts", "r");
|
|
|
|
if (fp == NULL) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Failed to open /proc/mounts";
|
2015-03-31 19:35:33 +02:00
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some volumes can be stacked on each other, so force unmount in
|
|
|
|
// reverse order to give us the best chance of success.
|
|
|
|
std::list<std::string> toUnmount;
|
|
|
|
mntent* mentry;
|
|
|
|
while ((mentry = getmntent(fp)) != NULL) {
|
2017-10-07 02:02:53 +02:00
|
|
|
auto test = std::string(mentry->mnt_dir);
|
|
|
|
if (android::base::StartsWith(test, "/mnt/")
|
|
|
|
|| android::base::StartsWith(test, "/storage/")) {
|
|
|
|
toUnmount.push_front(test);
|
2015-03-31 19:35:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
endmntent(fp);
|
|
|
|
|
2016-07-27 23:11:02 +02:00
|
|
|
for (const auto& path : toUnmount) {
|
2017-09-11 18:32:01 +02:00
|
|
|
LOG(DEBUG) << "Tearing down stale mount " << path;
|
2015-03-31 19:35:33 +02:00
|
|
|
android::vold::ForceUnmount(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-07 02:02:53 +02:00
|
|
|
int VolumeManager::mkdirs(const std::string& path) {
|
2015-03-14 00:09:20 +01:00
|
|
|
// Only offer to create directories for paths managed by vold
|
2017-10-07 02:02:53 +02:00
|
|
|
if (android::base::StartsWith(path, "/storage/")) {
|
2015-03-14 00:09:20 +01:00
|
|
|
// fs_mkdirs() does symlink checking and relative path enforcement
|
2017-10-07 02:02:53 +02:00
|
|
|
return fs_mkdirs(path.c_str(), 0700);
|
2013-09-18 02:24:38 +02:00
|
|
|
} else {
|
2017-10-07 02:02:53 +02:00
|
|
|
LOG(ERROR) << "Failed to find mounted volume for " << path;
|
2013-09-18 02:24:38 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
2017-09-11 18:32:01 +02:00
|
|
|
|
|
|
|
static size_t kAppFuseMaxMountPointName = 32;
|
|
|
|
|
|
|
|
static android::status_t getMountPath(uid_t uid, const std::string& name, std::string* path) {
|
|
|
|
if (name.size() > kAppFuseMaxMountPointName) {
|
|
|
|
LOG(ERROR) << "AppFuse mount name is too long.";
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < name.size(); i++) {
|
|
|
|
if (!isalnum(name[i])) {
|
|
|
|
LOG(ERROR) << "AppFuse mount name contains invalid character.";
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*path = android::base::StringPrintf("/mnt/appfuse/%d_%s", uid, name.c_str());
|
|
|
|
return android::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static android::status_t mountInNamespace(uid_t uid, int device_fd, const std::string& path) {
|
|
|
|
// Remove existing mount.
|
|
|
|
android::vold::ForceUnmount(path);
|
|
|
|
|
|
|
|
const auto opts = android::base::StringPrintf(
|
|
|
|
"fd=%i,"
|
|
|
|
"rootmode=40000,"
|
|
|
|
"default_permissions,"
|
|
|
|
"allow_other,"
|
|
|
|
"user_id=%d,group_id=%d,"
|
|
|
|
"context=\"u:object_r:app_fuse_file:s0\","
|
|
|
|
"fscontext=u:object_r:app_fusefs:s0",
|
|
|
|
device_fd,
|
|
|
|
uid,
|
|
|
|
uid);
|
|
|
|
|
|
|
|
const int result = TEMP_FAILURE_RETRY(mount(
|
|
|
|
"/dev/fuse", path.c_str(), "fuse",
|
|
|
|
MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME, opts.c_str()));
|
|
|
|
if (result != 0) {
|
|
|
|
PLOG(ERROR) << "Failed to mount " << path;
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
return android::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static android::status_t runCommandInNamespace(const std::string& command,
|
|
|
|
uid_t uid,
|
|
|
|
pid_t pid,
|
|
|
|
const std::string& path,
|
|
|
|
int device_fd) {
|
|
|
|
if (DEBUG_APPFUSE) {
|
|
|
|
LOG(DEBUG) << "Run app fuse command " << command << " for the path " << path
|
|
|
|
<< " in namespace " << uid;
|
|
|
|
}
|
|
|
|
|
|
|
|
unique_fd dir(open("/proc", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
|
|
|
|
if (dir.get() == -1) {
|
|
|
|
PLOG(ERROR) << "Failed to open /proc";
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Obtains process file descriptor.
|
|
|
|
const std::string pid_str = android::base::StringPrintf("%d", pid);
|
|
|
|
const unique_fd pid_fd(
|
|
|
|
openat(dir.get(), pid_str.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC));
|
|
|
|
if (pid_fd.get() == -1) {
|
|
|
|
PLOG(ERROR) << "Failed to open /proc/" << pid;
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check UID of process.
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
const int result = fstat(pid_fd.get(), &sb);
|
|
|
|
if (result == -1) {
|
|
|
|
PLOG(ERROR) << "Failed to stat /proc/" << pid;
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
if (sb.st_uid != AID_SYSTEM) {
|
|
|
|
LOG(ERROR) << "Only system can mount appfuse. UID expected=" << AID_SYSTEM
|
|
|
|
<< ", actual=" << sb.st_uid;
|
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matches so far, but refuse to touch if in root namespace
|
|
|
|
{
|
2017-10-07 02:02:53 +02:00
|
|
|
std::string rootName;
|
|
|
|
std::string pidName;
|
|
|
|
if (!android::vold::Readlinkat(dir.get(), "1/ns/mnt", &rootName)
|
|
|
|
|| !android::vold::Readlinkat(pid_fd.get(), "ns/mnt", &pidName)) {
|
|
|
|
PLOG(ERROR) << "Failed to read namespaces";
|
2017-09-11 18:32:01 +02:00
|
|
|
return -EPERM;
|
|
|
|
}
|
2017-10-07 02:02:53 +02:00
|
|
|
if (rootName == pidName) {
|
2017-09-11 18:32:01 +02:00
|
|
|
LOG(ERROR) << "Don't mount appfuse in root namespace";
|
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We purposefully leave the namespace open across the fork
|
|
|
|
unique_fd ns_fd(openat(pid_fd.get(), "ns/mnt", O_RDONLY)); // not O_CLOEXEC
|
|
|
|
if (ns_fd.get() < 0) {
|
|
|
|
PLOG(ERROR) << "Failed to open namespace for /proc/" << pid << "/ns/mnt";
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
int child = fork();
|
|
|
|
if (child == 0) {
|
|
|
|
if (setns(ns_fd.get(), CLONE_NEWNS) != 0) {
|
|
|
|
PLOG(ERROR) << "Failed to setns";
|
|
|
|
_exit(-errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (command == "mount") {
|
|
|
|
_exit(mountInNamespace(uid, device_fd, path));
|
|
|
|
} else if (command == "unmount") {
|
|
|
|
// If it's just after all FD opened on mount point are closed, umount2 can fail with
|
|
|
|
// EBUSY. To avoid the case, specify MNT_DETACH.
|
|
|
|
if (umount2(path.c_str(), UMOUNT_NOFOLLOW | MNT_DETACH) != 0 &&
|
|
|
|
errno != EINVAL && errno != ENOENT) {
|
|
|
|
PLOG(ERROR) << "Failed to unmount directory.";
|
|
|
|
_exit(-errno);
|
|
|
|
}
|
|
|
|
if (rmdir(path.c_str()) != 0) {
|
|
|
|
PLOG(ERROR) << "Failed to remove the mount directory.";
|
|
|
|
_exit(-errno);
|
|
|
|
}
|
|
|
|
_exit(android::OK);
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Unknown appfuse command " << command;
|
|
|
|
_exit(-EPERM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (child == -1) {
|
|
|
|
PLOG(ERROR) << "Failed to folk child process";
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
android::status_t status;
|
|
|
|
TEMP_FAILURE_RETRY(waitpid(child, &status, 0));
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VolumeManager::createObb(const std::string& sourcePath, const std::string& sourceKey,
|
|
|
|
int32_t ownerGid, std::string* outVolId) {
|
|
|
|
int id = mNextObbId++;
|
|
|
|
|
|
|
|
auto vol = std::shared_ptr<android::vold::VolumeBase>(
|
|
|
|
new android::vold::ObbVolume(id, sourcePath, sourceKey, ownerGid));
|
|
|
|
vol->create();
|
|
|
|
|
|
|
|
mObbVolumes.push_back(vol);
|
|
|
|
*outVolId = vol->getId();
|
|
|
|
return android::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VolumeManager::destroyObb(const std::string& volId) {
|
|
|
|
auto i = mObbVolumes.begin();
|
|
|
|
while (i != mObbVolumes.end()) {
|
|
|
|
if ((*i)->getId() == volId) {
|
|
|
|
(*i)->destroy();
|
|
|
|
i = mObbVolumes.erase(i);
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return android::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VolumeManager::mountAppFuse(uid_t uid, pid_t pid, int mountId,
|
|
|
|
android::base::unique_fd* device_fd) {
|
|
|
|
std::string name = std::to_string(mountId);
|
|
|
|
|
|
|
|
// Check mount point name.
|
|
|
|
std::string path;
|
|
|
|
if (getMountPath(uid, name, &path) != android::OK) {
|
|
|
|
LOG(ERROR) << "Invalid mount point name";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create directories.
|
|
|
|
const android::status_t result = android::vold::PrepareDir(path, 0700, 0, 0);
|
|
|
|
if (result != android::OK) {
|
|
|
|
PLOG(ERROR) << "Failed to prepare directory " << path;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open device FD.
|
|
|
|
device_fd->reset(open("/dev/fuse", O_RDWR)); // not O_CLOEXEC
|
|
|
|
if (device_fd->get() == -1) {
|
|
|
|
PLOG(ERROR) << "Failed to open /dev/fuse";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mount.
|
|
|
|
return runCommandInNamespace("mount", uid, pid, path, device_fd->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
int VolumeManager::unmountAppFuse(uid_t uid, pid_t pid, int mountId) {
|
|
|
|
std::string name = std::to_string(mountId);
|
|
|
|
|
|
|
|
// Check mount point name.
|
|
|
|
std::string path;
|
|
|
|
if (getMountPath(uid, name, &path) != android::OK) {
|
|
|
|
LOG(ERROR) << "Invalid mount point name";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return runCommandInNamespace("unmount", uid, pid, path, -1 /* device_fd */);
|
|
|
|
}
|