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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2009-10-12 20:32:47 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2009-10-11 02:22:08 +02:00
|
|
|
#include <errno.h>
|
2009-12-13 19:40:18 +01:00
|
|
|
#include <fcntl.h>
|
2012-04-04 02:23:01 +02:00
|
|
|
#include <fts.h>
|
|
|
|
#include <unistd.h>
|
2010-01-06 19:33:53 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mount.h>
|
2012-06-15 05:55:28 +02:00
|
|
|
#include <dirent.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
|
|
|
|
|
|
|
#define LOG_TAG "Vold"
|
|
|
|
|
2010-03-15 21:13:41 +01:00
|
|
|
#include <openssl/md5.h>
|
|
|
|
|
2009-10-11 02:22:08 +02:00
|
|
|
#include <cutils/log.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>
|
|
|
|
|
2009-10-11 02:22:08 +02:00
|
|
|
#include "VolumeManager.h"
|
2009-10-12 23:57:05 +02:00
|
|
|
#include "DirectVolume.h"
|
2009-12-13 19:40:18 +01:00
|
|
|
#include "ResponseCode.h"
|
2010-01-06 19:33:53 +01:00
|
|
|
#include "Loop.h"
|
2012-04-04 02:23:01 +02:00
|
|
|
#include "Ext4.h"
|
2010-01-06 19:33:53 +01:00
|
|
|
#include "Fat.h"
|
2010-01-10 22:02:12 +01:00
|
|
|
#include "Devmapper.h"
|
2010-02-17 02:12:00 +01:00
|
|
|
#include "Process.h"
|
2010-03-03 21:37:32 +01:00
|
|
|
#include "Asec.h"
|
2011-05-19 02:20:07 +02:00
|
|
|
#include "cryptfs.h"
|
2010-01-09 16:08:06 +01:00
|
|
|
|
2011-06-07 19:51:38 +02:00
|
|
|
#define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file"
|
|
|
|
|
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;
|
2009-10-11 02:22:08 +02:00
|
|
|
mVolumes = new VolumeCollection();
|
2010-01-15 18:26:28 +01:00
|
|
|
mActiveContainers = new AsecIdCollection();
|
2009-10-11 02:22:08 +02:00
|
|
|
mBroadcaster = NULL;
|
2010-10-28 21:21:24 +02:00
|
|
|
mUmsSharingCount = 0;
|
|
|
|
mSavedDirtyRatio = -1;
|
|
|
|
// set dirty ratio to 0 when UMS is active
|
|
|
|
mUmsDirtyRatio = 0;
|
2011-07-12 00:38:57 +02:00
|
|
|
mVolManagerDisabled = 0;
|
2009-10-11 02:22:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VolumeManager::~VolumeManager() {
|
2010-01-15 18:26:28 +01:00
|
|
|
delete mVolumes;
|
|
|
|
delete mActiveContainers;
|
2009-10-11 02:22:08 +02:00
|
|
|
}
|
|
|
|
|
2010-03-12 22:32:47 +01:00
|
|
|
char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
|
2010-06-19 04:06:50 +02:00
|
|
|
static const char* digits = "0123456789abcdef";
|
|
|
|
|
2010-03-15 21:13:41 +01:00
|
|
|
unsigned char sig[MD5_DIGEST_LENGTH];
|
2010-03-12 22:32:47 +01:00
|
|
|
|
2010-06-19 04:06:50 +02:00
|
|
|
if (buffer == NULL) {
|
|
|
|
SLOGE("Destination buffer is NULL");
|
|
|
|
errno = ESPIPE;
|
|
|
|
return NULL;
|
|
|
|
} else if (id == NULL) {
|
|
|
|
SLOGE("Source buffer is NULL");
|
|
|
|
errno = ESPIPE;
|
|
|
|
return NULL;
|
|
|
|
} else if (len < MD5_ASCII_LENGTH_PLUS_NULL) {
|
|
|
|
SLOGE("Target hash buffer size < %d bytes (%d)",
|
|
|
|
MD5_ASCII_LENGTH_PLUS_NULL, len);
|
2010-03-12 22:32:47 +01:00
|
|
|
errno = ESPIPE;
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-03-15 21:13:41 +01:00
|
|
|
|
|
|
|
MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig);
|
2010-03-12 22:32:47 +01:00
|
|
|
|
2010-06-19 04:06:50 +02:00
|
|
|
char *p = buffer;
|
2010-03-15 21:13:41 +01:00
|
|
|
for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
|
2010-06-19 04:06:50 +02:00
|
|
|
*p++ = digits[sig[i] >> 4];
|
|
|
|
*p++ = digits[sig[i] & 0x0F];
|
2010-03-12 22:32:47 +01:00
|
|
|
}
|
2010-06-19 04:06:50 +02:00
|
|
|
*p = '\0';
|
2010-03-12 22:32:47 +01:00
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeManager::setDebug(bool enable) {
|
|
|
|
mDebug = enable;
|
|
|
|
VolumeCollection::iterator it;
|
|
|
|
for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
|
|
|
|
(*it)->setDebug(enable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-11 02:22:08 +02:00
|
|
|
int VolumeManager::start() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VolumeManager::stop() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VolumeManager::addVolume(Volume *v) {
|
|
|
|
mVolumes->push_back(v);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-12 20:32:47 +02:00
|
|
|
void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
|
|
|
|
const char *devpath = evt->findParam("DEVPATH");
|
2009-10-11 02:22:08 +02:00
|
|
|
|
2009-10-12 20:32:47 +02:00
|
|
|
/* Lookup a volume to handle this device */
|
2009-10-11 02:22:08 +02:00
|
|
|
VolumeCollection::iterator it;
|
|
|
|
bool hit = false;
|
|
|
|
for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
|
2009-10-12 20:32:47 +02:00
|
|
|
if (!(*it)->handleBlockEvent(evt)) {
|
2009-12-13 19:40:18 +01:00
|
|
|
#ifdef NETLINK_DEBUG
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel());
|
2009-12-13 19:40:18 +01:00
|
|
|
#endif
|
2009-10-11 02:22:08 +02:00
|
|
|
hit = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hit) {
|
2009-12-13 19:40:18 +01:00
|
|
|
#ifdef NETLINK_DEBUG
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGW("No volumes handled block event for '%s'", devpath);
|
2009-12-13 19:40:18 +01:00
|
|
|
#endif
|
2009-10-11 02:22:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int VolumeManager::listVolumes(SocketClient *cli) {
|
|
|
|
VolumeCollection::iterator i;
|
|
|
|
|
|
|
|
for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
|
|
|
|
char *buffer;
|
|
|
|
asprintf(&buffer, "%s %s %d",
|
|
|
|
(*i)->getLabel(), (*i)->getMountpoint(),
|
|
|
|
(*i)->getState());
|
2009-12-13 19:40:18 +01:00
|
|
|
cli->sendMsg(ResponseCode::VolumeListResult, buffer, false);
|
2009-10-11 02:22:08 +02:00
|
|
|
free(buffer);
|
|
|
|
}
|
2009-12-13 19:40:18 +01:00
|
|
|
cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false);
|
2009-10-11 02:22:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2009-10-13 01:29:01 +02:00
|
|
|
|
2009-12-13 19:40:18 +01:00
|
|
|
int VolumeManager::formatVolume(const char *label) {
|
|
|
|
Volume *v = lookupVolume(label);
|
|
|
|
|
|
|
|
if (!v) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-07-12 00:38:57 +02:00
|
|
|
if (mVolManagerDisabled) {
|
|
|
|
errno = EBUSY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-12-13 19:40:18 +01:00
|
|
|
return v->formatVol();
|
|
|
|
}
|
|
|
|
|
2010-07-12 18:59:49 +02:00
|
|
|
int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
|
|
|
|
char idHash[33];
|
|
|
|
if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
|
|
|
|
SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(mountPath, 0, mountPathLen);
|
|
|
|
snprintf(mountPath, mountPathLen, "%s/%s", Volume::LOOPDIR, idHash);
|
|
|
|
|
|
|
|
if (access(mountPath, F_OK)) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-06 19:33:53 +01:00
|
|
|
int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
|
2010-03-23 19:15:58 +01:00
|
|
|
char asecFileName[255];
|
2012-04-04 02:23:01 +02:00
|
|
|
|
|
|
|
if (findAsec(id, asecFileName, sizeof(asecFileName))) {
|
|
|
|
SLOGE("Couldn't find ASEC %s", id);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-03-23 19:15:58 +01:00
|
|
|
|
|
|
|
memset(buffer, 0, maxlen);
|
|
|
|
if (access(asecFileName, F_OK)) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-01-06 19:33:53 +01:00
|
|
|
|
2010-02-20 03:14:36 +01:00
|
|
|
snprintf(buffer, maxlen, "%s/%s", Volume::ASECDIR, id);
|
2010-01-06 19:33:53 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-27 22:37:07 +02:00
|
|
|
int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
|
|
|
|
char asecFileName[255];
|
2012-04-04 02:23:01 +02:00
|
|
|
|
|
|
|
if (findAsec(id, asecFileName, sizeof(asecFileName))) {
|
|
|
|
SLOGE("Couldn't find ASEC %s", id);
|
|
|
|
return -1;
|
|
|
|
}
|
2011-06-27 22:37:07 +02:00
|
|
|
|
|
|
|
memset(buffer, 0, maxlen);
|
|
|
|
if (access(asecFileName, F_OK)) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(buffer, maxlen, "%s", asecFileName);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-04 02:23:01 +02:00
|
|
|
int VolumeManager::createAsec(const char *id, unsigned int numSectors, const char *fstype,
|
|
|
|
const char *key, const int ownerUid, bool isExternal) {
|
2010-03-03 21:37:32 +01:00
|
|
|
struct asec_superblock sb;
|
|
|
|
memset(&sb, 0, sizeof(sb));
|
|
|
|
|
2012-04-04 02:23:01 +02:00
|
|
|
const bool wantFilesystem = strcmp(fstype, "none");
|
|
|
|
bool usingExt4 = false;
|
|
|
|
if (wantFilesystem) {
|
|
|
|
usingExt4 = !strcmp(fstype, "ext4");
|
|
|
|
if (usingExt4) {
|
|
|
|
sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
|
|
|
|
} else if (strcmp(fstype, "fat")) {
|
|
|
|
SLOGE("Invalid filesystem type %s", fstype);
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-03 21:37:32 +01:00
|
|
|
sb.magic = ASEC_SB_MAGIC;
|
|
|
|
sb.ver = ASEC_SB_VER;
|
2010-01-06 19:33:53 +01:00
|
|
|
|
2010-02-18 17:37:45 +01:00
|
|
|
if (numSectors < ((1024*1024)/512)) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Invalid container size specified (%d sectors)", numSectors);
|
2010-02-18 17:37:45 +01:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-01-06 19:33:53 +01:00
|
|
|
if (lookupVolume(id)) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("ASEC id '%s' currently exists", id);
|
2010-01-06 19:33:53 +01:00
|
|
|
errno = EADDRINUSE;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char asecFileName[255];
|
2012-04-04 02:23:01 +02:00
|
|
|
|
|
|
|
if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
|
|
|
|
SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
|
|
|
|
asecFileName, strerror(errno));
|
|
|
|
errno = EADDRINUSE;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *asecDir = isExternal ? Volume::SEC_ASECDIR_EXT : Volume::SEC_ASECDIR_INT;
|
|
|
|
|
|
|
|
snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
|
2010-01-06 19:33:53 +01:00
|
|
|
|
|
|
|
if (!access(asecFileName, F_OK)) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
|
2012-04-04 02:23:01 +02:00
|
|
|
asecFileName, strerror(errno));
|
2010-01-06 19:33:53 +01:00
|
|
|
errno = EADDRINUSE;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-03-03 21:37:32 +01:00
|
|
|
/*
|
|
|
|
* Add some headroom
|
|
|
|
*/
|
|
|
|
unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2;
|
|
|
|
unsigned numImgSectors = numSectors + fatSize + 2;
|
|
|
|
|
|
|
|
if (numImgSectors % 63) {
|
|
|
|
numImgSectors += (63 - (numImgSectors % 63));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add +1 for our superblock which is at the end
|
|
|
|
if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("ASEC image file creation failed (%s)", strerror(errno));
|
2010-01-06 19:33:53 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-03-12 22:32:47 +01:00
|
|
|
char idHash[33];
|
|
|
|
if (!asecHash(id, idHash, sizeof(idHash))) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
|
2010-03-12 22:32:47 +01:00
|
|
|
unlink(asecFileName);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-01-06 19:33:53 +01:00
|
|
|
char loopDevice[255];
|
2010-03-12 22:32:47 +01:00
|
|
|
if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
|
2010-01-06 19:33:53 +01:00
|
|
|
unlink(asecFileName);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-01-10 22:02:12 +01:00
|
|
|
char dmDevice[255];
|
|
|
|
bool cleanupDm = false;
|
2010-01-06 19:33:53 +01:00
|
|
|
|
2010-01-10 22:02:12 +01:00
|
|
|
if (strcmp(key, "none")) {
|
2010-03-03 21:37:32 +01:00
|
|
|
// XXX: This is all we support for now
|
|
|
|
sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
|
2010-03-12 22:32:47 +01:00
|
|
|
if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
|
2010-01-10 22:02:12 +01:00
|
|
|
sizeof(dmDevice))) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("ASEC device mapping failed (%s)", strerror(errno));
|
2010-01-10 22:02:12 +01:00
|
|
|
Loop::destroyByDevice(loopDevice);
|
|
|
|
unlink(asecFileName);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cleanupDm = true;
|
|
|
|
} else {
|
2010-03-03 21:37:32 +01:00
|
|
|
sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
|
2010-01-10 22:02:12 +01:00
|
|
|
strcpy(dmDevice, loopDevice);
|
|
|
|
}
|
|
|
|
|
2010-03-03 21:37:32 +01:00
|
|
|
/*
|
|
|
|
* Drop down the superblock at the end of the file
|
|
|
|
*/
|
|
|
|
|
|
|
|
int sbfd = open(loopDevice, O_RDWR);
|
|
|
|
if (sbfd < 0) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Failed to open new DM device for superblock write (%s)", strerror(errno));
|
2010-03-03 21:37:32 +01:00
|
|
|
if (cleanupDm) {
|
2010-03-12 22:32:47 +01:00
|
|
|
Devmapper::destroy(idHash);
|
2010-03-03 21:37:32 +01:00
|
|
|
}
|
|
|
|
Loop::destroyByDevice(loopDevice);
|
|
|
|
unlink(asecFileName);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
|
|
|
|
close(sbfd);
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Failed to lseek for superblock (%s)", strerror(errno));
|
2010-03-03 21:37:32 +01:00
|
|
|
if (cleanupDm) {
|
2010-03-12 22:32:47 +01:00
|
|
|
Devmapper::destroy(idHash);
|
2010-03-03 21:37:32 +01:00
|
|
|
}
|
|
|
|
Loop::destroyByDevice(loopDevice);
|
|
|
|
unlink(asecFileName);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write(sbfd, &sb, sizeof(sb)) != sizeof(sb)) {
|
|
|
|
close(sbfd);
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Failed to write superblock (%s)", strerror(errno));
|
2010-03-03 21:37:32 +01:00
|
|
|
if (cleanupDm) {
|
2010-03-12 22:32:47 +01:00
|
|
|
Devmapper::destroy(idHash);
|
2010-03-03 21:37:32 +01:00
|
|
|
}
|
|
|
|
Loop::destroyByDevice(loopDevice);
|
|
|
|
unlink(asecFileName);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
close(sbfd);
|
|
|
|
|
2012-04-04 02:23:01 +02:00
|
|
|
if (wantFilesystem) {
|
|
|
|
int formatStatus;
|
|
|
|
if (usingExt4) {
|
|
|
|
formatStatus = Ext4::format(dmDevice);
|
|
|
|
} else {
|
|
|
|
formatStatus = Fat::format(dmDevice, numImgSectors);
|
2010-01-10 22:02:12 +01:00
|
|
|
}
|
2010-01-06 19:33:53 +01:00
|
|
|
|
2012-04-04 02:23:01 +02:00
|
|
|
if (formatStatus < 0) {
|
|
|
|
SLOGE("ASEC fs format failed (%s)", strerror(errno));
|
2010-01-10 22:02:12 +01:00
|
|
|
if (cleanupDm) {
|
2010-03-12 22:32:47 +01:00
|
|
|
Devmapper::destroy(idHash);
|
2010-01-10 22:02:12 +01:00
|
|
|
}
|
2010-01-07 21:12:50 +01:00
|
|
|
Loop::destroyByDevice(loopDevice);
|
|
|
|
unlink(asecFileName);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-04-04 02:23:01 +02:00
|
|
|
|
2010-03-01 05:17:20 +01:00
|
|
|
char mountPoint[255];
|
|
|
|
|
|
|
|
snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
|
2012-04-04 02:23:01 +02:00
|
|
|
if (mkdir(mountPoint, 0000)) {
|
2010-03-01 05:17:20 +01:00
|
|
|
if (errno != EEXIST) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Mountpoint creation failed (%s)", strerror(errno));
|
2010-03-01 05:17:20 +01:00
|
|
|
if (cleanupDm) {
|
2010-03-12 22:32:47 +01:00
|
|
|
Devmapper::destroy(idHash);
|
2010-03-01 05:17:20 +01:00
|
|
|
}
|
|
|
|
Loop::destroyByDevice(loopDevice);
|
|
|
|
unlink(asecFileName);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2010-01-06 19:33:53 +01:00
|
|
|
|
2012-04-04 02:23:01 +02:00
|
|
|
int mountStatus;
|
|
|
|
if (usingExt4) {
|
|
|
|
mountStatus = Ext4::doMount(dmDevice, mountPoint, false, false, false);
|
|
|
|
} else {
|
|
|
|
mountStatus = Fat::doMount(dmDevice, mountPoint, false, false, false, ownerUid, 0, 0000,
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mountStatus) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
|
2010-03-01 05:17:20 +01:00
|
|
|
if (cleanupDm) {
|
2010-03-12 22:32:47 +01:00
|
|
|
Devmapper::destroy(idHash);
|
2010-03-01 05:17:20 +01:00
|
|
|
}
|
|
|
|
Loop::destroyByDevice(loopDevice);
|
|
|
|
unlink(asecFileName);
|
|
|
|
return -1;
|
2010-01-10 22:02:12 +01:00
|
|
|
}
|
2012-04-04 02:23:01 +02:00
|
|
|
|
|
|
|
if (usingExt4) {
|
|
|
|
int dirfd = open(mountPoint, O_DIRECTORY);
|
|
|
|
if (dirfd >= 0) {
|
|
|
|
if (fchown(dirfd, ownerUid, AID_SYSTEM)
|
|
|
|
|| fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
|
|
|
|
SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
|
|
|
|
}
|
|
|
|
close(dirfd);
|
|
|
|
}
|
|
|
|
}
|
2010-03-01 05:17:20 +01:00
|
|
|
} else {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGI("Created raw secure container %s (no filesystem)", id);
|
2010-01-06 19:33:53 +01:00
|
|
|
}
|
2010-01-15 18:26:28 +01:00
|
|
|
|
2010-09-25 00:11:48 +02:00
|
|
|
mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
|
2010-01-06 19:33:53 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VolumeManager::finalizeAsec(const char *id) {
|
|
|
|
char asecFileName[255];
|
|
|
|
char loopDevice[255];
|
|
|
|
char mountPoint[255];
|
|
|
|
|
2012-04-04 02:23:01 +02:00
|
|
|
if (findAsec(id, asecFileName, sizeof(asecFileName))) {
|
|
|
|
SLOGE("Couldn't find ASEC %s", id);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-01-06 19:33:53 +01:00
|
|
|
|
2010-03-12 22:32:47 +01:00
|
|
|
char idHash[33];
|
|
|
|
if (!asecHash(id, idHash, sizeof(idHash))) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
|
2010-03-12 22:32:47 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
|
2010-01-06 19:33:53 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-04-04 02:23:01 +02:00
|
|
|
unsigned int nr_sec = 0;
|
|
|
|
struct asec_superblock sb;
|
|
|
|
|
|
|
|
if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-02-20 03:14:36 +01:00
|
|
|
snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
|
2012-04-04 02:23:01 +02:00
|
|
|
|
|
|
|
int result = 0;
|
|
|
|
if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
|
|
|
|
result = Ext4::doMount(loopDevice, mountPoint, true, true, true);
|
|
|
|
} else {
|
|
|
|
result = Fat::doMount(loopDevice, mountPoint, true, true, true, 0, 0, 0227, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
|
2010-01-06 19:33:53 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-03-12 22:32:47 +01:00
|
|
|
if (mDebug) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGD("ASEC %s finalized", id);
|
2010-03-12 22:32:47 +01:00
|
|
|
}
|
2010-01-06 19:33:53 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-04 02:23:01 +02:00
|
|
|
int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
|
|
|
|
char asecFileName[255];
|
|
|
|
char loopDevice[255];
|
|
|
|
char mountPoint[255];
|
|
|
|
|
|
|
|
if (gid < AID_APP) {
|
|
|
|
SLOGE("Group ID is not in application range");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (findAsec(id, asecFileName, sizeof(asecFileName))) {
|
|
|
|
SLOGE("Couldn't find ASEC %s", id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char idHash[33];
|
|
|
|
if (!asecHash(id, idHash, sizeof(idHash))) {
|
|
|
|
SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
|
|
|
|
SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int nr_sec = 0;
|
|
|
|
struct asec_superblock sb;
|
|
|
|
|
|
|
|
if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
|
|
|
|
|
|
|
|
int result = 0;
|
|
|
|
if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret = Ext4::doMount(loopDevice, mountPoint,
|
|
|
|
false /* read-only */,
|
|
|
|
true /* remount */,
|
|
|
|
false /* executable */);
|
|
|
|
if (ret) {
|
|
|
|
SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *paths[] = { mountPoint, NULL };
|
|
|
|
|
|
|
|
FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
|
|
|
|
if (fts) {
|
|
|
|
// Traverse the entire hierarchy and chown to system UID.
|
|
|
|
for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
|
|
|
|
// We don't care about the lost+found directory.
|
|
|
|
if (!strcmp(ftsent->fts_name, "lost+found")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There can only be one file marked as private right now.
|
|
|
|
* This should be more robust, but it satisfies the requirements
|
|
|
|
* we have for right now.
|
|
|
|
*/
|
|
|
|
const bool privateFile = !strcmp(ftsent->fts_name, filename);
|
|
|
|
|
|
|
|
int fd = open(ftsent->fts_accpath, O_NOFOLLOW);
|
|
|
|
if (fd < 0) {
|
|
|
|
SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
|
|
|
|
result = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
|
|
|
|
|
|
|
|
if (ftsent->fts_info & FTS_D) {
|
2012-05-11 01:45:29 +02:00
|
|
|
result |= fchmod(fd, 0755);
|
2012-05-11 00:39:53 +02:00
|
|
|
} else if (ftsent->fts_info & FTS_F) {
|
2012-04-04 02:23:01 +02:00
|
|
|
result |= fchmod(fd, privateFile ? 0640 : 0644);
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
fts_close(fts);
|
|
|
|
|
|
|
|
// Finally make the directory readable by everyone.
|
|
|
|
int dirfd = open(mountPoint, O_DIRECTORY);
|
|
|
|
if (dirfd < 0 || fchmod(dirfd, 0755)) {
|
|
|
|
SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
|
|
|
|
result |= -1;
|
|
|
|
}
|
|
|
|
close(dirfd);
|
|
|
|
} else {
|
|
|
|
result |= -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
result |= Ext4::doMount(loopDevice, mountPoint,
|
|
|
|
true /* read-only */,
|
|
|
|
true /* remount */,
|
|
|
|
true /* execute */);
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mDebug) {
|
|
|
|
SLOGD("ASEC %s permissions fixed", id);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-23 17:17:06 +01:00
|
|
|
int VolumeManager::renameAsec(const char *id1, const char *id2) {
|
2012-04-04 02:23:01 +02:00
|
|
|
char asecFilename1[255];
|
2010-01-23 17:17:06 +01:00
|
|
|
char *asecFilename2;
|
|
|
|
char mountPoint[255];
|
|
|
|
|
2012-04-04 02:23:01 +02:00
|
|
|
const char *dir;
|
|
|
|
|
|
|
|
if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
|
|
|
|
SLOGE("Couldn't find ASEC %s", id1);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
|
2010-01-23 17:17:06 +01:00
|
|
|
|
2010-02-20 03:14:36 +01:00
|
|
|
snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id1);
|
2010-01-23 17:17:06 +01:00
|
|
|
if (isMountpointMounted(mountPoint)) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGW("Rename attempt when src mounted");
|
2010-01-23 17:17:06 +01:00
|
|
|
errno = EBUSY;
|
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
|
2010-02-24 17:42:51 +01:00
|
|
|
snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id2);
|
|
|
|
if (isMountpointMounted(mountPoint)) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGW("Rename attempt when dst mounted");
|
2010-02-24 17:42:51 +01:00
|
|
|
errno = EBUSY;
|
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
|
2010-01-23 17:17:06 +01:00
|
|
|
if (!access(asecFilename2, F_OK)) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Rename attempt when dst exists");
|
2010-01-23 17:17:06 +01:00
|
|
|
errno = EADDRINUSE;
|
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rename(asecFilename1, asecFilename2)) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
|
2010-01-23 17:17:06 +01:00
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(asecFilename2);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out_err:
|
|
|
|
free(asecFilename2);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-07-01 03:48:41 +02:00
|
|
|
#define UNMOUNT_RETRIES 5
|
|
|
|
#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
|
2010-02-18 18:00:18 +01:00
|
|
|
int VolumeManager::unmountAsec(const char *id, bool force) {
|
2010-01-06 19:33:53 +01:00
|
|
|
char asecFileName[255];
|
|
|
|
char mountPoint[255];
|
|
|
|
|
2012-04-04 02:23:01 +02:00
|
|
|
if (findAsec(id, asecFileName, sizeof(asecFileName))) {
|
|
|
|
SLOGE("Couldn't find ASEC %s", id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-02-20 03:14:36 +01:00
|
|
|
snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
|
2010-01-06 19:33:53 +01:00
|
|
|
|
2010-03-12 22:32:47 +01:00
|
|
|
char idHash[33];
|
|
|
|
if (!asecHash(id, idHash, sizeof(idHash))) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
|
2010-03-12 22:32:47 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-07-01 03:48:41 +02:00
|
|
|
return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
|
|
|
|
}
|
|
|
|
|
2010-07-12 18:59:49 +02:00
|
|
|
int VolumeManager::unmountObb(const char *fileName, bool force) {
|
2010-07-01 03:48:41 +02:00
|
|
|
char mountPoint[255];
|
|
|
|
|
|
|
|
char idHash[33];
|
|
|
|
if (!asecHash(fileName, idHash, sizeof(idHash))) {
|
|
|
|
SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash);
|
|
|
|
|
|
|
|
return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
|
|
|
|
}
|
|
|
|
|
|
|
|
int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
|
|
|
|
const char *fileName, const char *mountPoint, bool force) {
|
2010-01-13 00:38:59 +01:00
|
|
|
if (!isMountpointMounted(mountPoint)) {
|
2010-07-01 03:48:41 +02:00
|
|
|
SLOGE("Unmount request for %s when not mounted", id);
|
2010-10-01 03:00:52 +02:00
|
|
|
errno = ENOENT;
|
2010-01-10 22:02:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2010-01-09 16:08:06 +01:00
|
|
|
|
2010-01-10 22:02:12 +01:00
|
|
|
int i, rc;
|
2010-07-01 03:48:41 +02:00
|
|
|
for (i = 1; i <= UNMOUNT_RETRIES; i++) {
|
2010-01-10 22:02:12 +01:00
|
|
|
rc = umount(mountPoint);
|
|
|
|
if (!rc) {
|
|
|
|
break;
|
2010-01-06 19:33:53 +01:00
|
|
|
}
|
2010-01-10 22:02:12 +01:00
|
|
|
if (rc && (errno == EINVAL || errno == ENOENT)) {
|
2010-07-01 03:48:41 +02:00
|
|
|
SLOGI("Container %s unmounted OK", id);
|
2010-01-10 22:02:12 +01:00
|
|
|
rc = 0;
|
|
|
|
break;
|
2010-01-06 19:33:53 +01:00
|
|
|
}
|
2010-07-01 03:48:41 +02:00
|
|
|
SLOGW("%s unmount attempt %d failed (%s)",
|
2010-02-13 23:19:53 +01:00
|
|
|
id, i, strerror(errno));
|
|
|
|
|
2010-02-18 18:00:18 +01:00
|
|
|
int action = 0; // default is to just complain
|
|
|
|
|
|
|
|
if (force) {
|
2010-07-01 03:48:41 +02:00
|
|
|
if (i > (UNMOUNT_RETRIES - 2))
|
2010-02-18 18:00:18 +01:00
|
|
|
action = 2; // SIGKILL
|
2010-07-01 03:48:41 +02:00
|
|
|
else if (i > (UNMOUNT_RETRIES - 3))
|
2010-02-18 18:00:18 +01:00
|
|
|
action = 1; // SIGHUP
|
|
|
|
}
|
2010-02-13 23:19:53 +01:00
|
|
|
|
2010-02-17 02:12:00 +01:00
|
|
|
Process::killProcessesWithOpenFiles(mountPoint, action);
|
2010-07-01 03:48:41 +02:00
|
|
|
usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
|
2010-01-10 22:02:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rc) {
|
2010-02-18 18:00:18 +01:00
|
|
|
errno = EBUSY;
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
|
2010-01-10 22:02:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-02-24 20:43:22 +01:00
|
|
|
int retries = 10;
|
|
|
|
|
|
|
|
while(retries--) {
|
|
|
|
if (!rmdir(mountPoint)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
|
2010-07-01 03:48:41 +02:00
|
|
|
usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
|
2010-02-24 20:43:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!retries) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
|
2010-02-03 20:04:46 +01:00
|
|
|
}
|
2010-01-15 18:26:28 +01:00
|
|
|
|
2010-03-12 22:32:47 +01:00
|
|
|
if (Devmapper::destroy(idHash) && errno != ENXIO) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
|
2010-01-06 19:33:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char loopDevice[255];
|
2010-03-12 22:32:47 +01:00
|
|
|
if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
|
2010-01-06 19:33:53 +01:00
|
|
|
Loop::destroyByDevice(loopDevice);
|
2010-03-12 22:32:47 +01:00
|
|
|
} else {
|
2010-07-01 03:48:41 +02:00
|
|
|
SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
|
2010-01-06 19:33:53 +01:00
|
|
|
}
|
2010-01-15 18:26:28 +01:00
|
|
|
|
|
|
|
AsecIdCollection::iterator it;
|
|
|
|
for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
|
2010-09-25 00:11:48 +02:00
|
|
|
ContainerData* cd = *it;
|
|
|
|
if (!strcmp(cd->id, id)) {
|
2010-01-15 18:26:28 +01:00
|
|
|
free(*it);
|
|
|
|
mActiveContainers->erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (it == mActiveContainers->end()) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGW("mActiveContainers is inconsistent!");
|
2010-01-15 18:26:28 +01:00
|
|
|
}
|
2010-01-10 22:02:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-02-18 18:00:18 +01:00
|
|
|
int VolumeManager::destroyAsec(const char *id, bool force) {
|
2010-01-10 22:02:12 +01:00
|
|
|
char asecFileName[255];
|
|
|
|
char mountPoint[255];
|
|
|
|
|
2012-04-04 02:23:01 +02:00
|
|
|
if (findAsec(id, asecFileName, sizeof(asecFileName))) {
|
|
|
|
SLOGE("Couldn't find ASEC %s", id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-02-24 21:12:34 +01:00
|
|
|
snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
|
2010-01-10 22:02:12 +01:00
|
|
|
|
2010-01-13 00:38:59 +01:00
|
|
|
if (isMountpointMounted(mountPoint)) {
|
2010-03-12 22:32:47 +01:00
|
|
|
if (mDebug) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGD("Unmounting container before destroy");
|
2010-03-12 22:32:47 +01:00
|
|
|
}
|
2010-02-18 18:00:18 +01:00
|
|
|
if (unmountAsec(id, force)) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
|
2010-01-13 00:38:59 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2010-01-06 19:33:53 +01:00
|
|
|
|
2010-01-13 00:38:59 +01:00
|
|
|
if (unlink(asecFileName)) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
|
2010-01-13 00:38:59 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2010-01-06 19:33:53 +01:00
|
|
|
|
2010-03-12 22:32:47 +01:00
|
|
|
if (mDebug) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGD("ASEC %s destroyed", id);
|
2010-03-12 22:32:47 +01:00
|
|
|
}
|
2010-01-06 19:33:53 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-04 02:23:01 +02:00
|
|
|
bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
|
|
|
|
int dirfd = open(dir, O_DIRECTORY);
|
|
|
|
if (dirfd < 0) {
|
|
|
|
SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
if (!faccessat(dirfd, asecName, F_OK, AT_SYMLINK_NOFOLLOW)) {
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(dirfd);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
|
|
|
|
const char **directory) const {
|
|
|
|
int dirfd, fd;
|
|
|
|
const int idLen = strlen(id);
|
|
|
|
char *asecName;
|
|
|
|
|
|
|
|
if (asprintf(&asecName, "%s.asec", id) < 0) {
|
|
|
|
SLOGE("Couldn't allocate string to write ASEC name");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *dir;
|
|
|
|
if (isAsecInDirectory(Volume::SEC_ASECDIR_INT, asecName)) {
|
|
|
|
dir = Volume::SEC_ASECDIR_INT;
|
|
|
|
} else if (isAsecInDirectory(Volume::SEC_ASECDIR_EXT, asecName)) {
|
|
|
|
dir = Volume::SEC_ASECDIR_EXT;
|
|
|
|
} else {
|
|
|
|
free(asecName);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (directory != NULL) {
|
|
|
|
*directory = dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (asecPath != NULL) {
|
|
|
|
int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
|
|
|
|
if (written < 0 || static_cast<size_t>(written) >= asecPathLen) {
|
|
|
|
free(asecName);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(asecName);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-06 19:33:53 +01:00
|
|
|
int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid) {
|
|
|
|
char asecFileName[255];
|
|
|
|
char mountPoint[255];
|
|
|
|
|
2012-04-04 02:23:01 +02:00
|
|
|
if (findAsec(id, asecFileName, sizeof(asecFileName))) {
|
|
|
|
SLOGE("Couldn't find ASEC %s", id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-02-20 03:14:36 +01:00
|
|
|
snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
|
2010-01-06 19:33:53 +01:00
|
|
|
|
|
|
|
if (isMountpointMounted(mountPoint)) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("ASEC %s already mounted", id);
|
2010-01-06 19:33:53 +01:00
|
|
|
errno = EBUSY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-03-12 22:32:47 +01:00
|
|
|
char idHash[33];
|
|
|
|
if (!asecHash(id, idHash, sizeof(idHash))) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
|
2010-03-12 22:32:47 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2010-03-15 21:13:41 +01:00
|
|
|
|
2010-01-06 19:33:53 +01:00
|
|
|
char loopDevice[255];
|
2010-03-12 22:32:47 +01:00
|
|
|
if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
|
|
|
|
if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
|
2010-01-06 19:33:53 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2010-03-12 22:32:47 +01:00
|
|
|
if (mDebug) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGD("New loop device created at %s", loopDevice);
|
2010-03-12 22:32:47 +01:00
|
|
|
}
|
2010-01-10 22:02:12 +01:00
|
|
|
} else {
|
2010-03-12 22:32:47 +01:00
|
|
|
if (mDebug) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGD("Found active loopback for %s at %s", asecFileName, loopDevice);
|
2010-03-12 22:32:47 +01:00
|
|
|
}
|
2010-01-10 22:02:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char dmDevice[255];
|
|
|
|
bool cleanupDm = false;
|
2010-03-03 21:37:32 +01:00
|
|
|
int fd;
|
|
|
|
unsigned int nr_sec = 0;
|
|
|
|
struct asec_superblock sb;
|
2012-04-04 02:23:01 +02:00
|
|
|
|
|
|
|
if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
|
2010-03-03 21:37:32 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-03-12 22:32:47 +01:00
|
|
|
if (mDebug) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
|
2010-03-12 22:32:47 +01:00
|
|
|
}
|
2010-03-03 21:37:32 +01:00
|
|
|
if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
|
2010-03-03 21:37:32 +01:00
|
|
|
Loop::destroyByDevice(loopDevice);
|
|
|
|
errno = EMEDIUMTYPE;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
nr_sec--; // We don't want the devmapping to extend onto our superblock
|
|
|
|
|
|
|
|
if (strcmp(key, "none")) {
|
2010-03-12 22:32:47 +01:00
|
|
|
if (Devmapper::lookupActive(idHash, dmDevice, sizeof(dmDevice))) {
|
|
|
|
if (Devmapper::create(idHash, loopDevice, key, nr_sec,
|
2010-01-10 22:02:12 +01:00
|
|
|
dmDevice, sizeof(dmDevice))) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("ASEC device mapping failed (%s)", strerror(errno));
|
2010-01-10 22:02:12 +01:00
|
|
|
Loop::destroyByDevice(loopDevice);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-03-12 22:32:47 +01:00
|
|
|
if (mDebug) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGD("New devmapper instance created at %s", dmDevice);
|
2010-03-12 22:32:47 +01:00
|
|
|
}
|
2010-01-10 22:02:12 +01:00
|
|
|
} else {
|
2010-03-12 22:32:47 +01:00
|
|
|
if (mDebug) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGD("Found active devmapper for %s at %s", asecFileName, dmDevice);
|
2010-03-12 22:32:47 +01:00
|
|
|
}
|
2010-01-10 22:02:12 +01:00
|
|
|
}
|
|
|
|
cleanupDm = true;
|
|
|
|
} else {
|
|
|
|
strcpy(dmDevice, loopDevice);
|
2010-01-06 19:33:53 +01:00
|
|
|
}
|
|
|
|
|
2012-04-04 02:23:01 +02:00
|
|
|
if (mkdir(mountPoint, 0000)) {
|
2010-01-10 22:02:12 +01:00
|
|
|
if (errno != EEXIST) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Mountpoint creation failed (%s)", strerror(errno));
|
2010-01-10 22:02:12 +01:00
|
|
|
if (cleanupDm) {
|
2010-03-12 22:32:47 +01:00
|
|
|
Devmapper::destroy(idHash);
|
2010-01-10 22:02:12 +01:00
|
|
|
}
|
|
|
|
Loop::destroyByDevice(loopDevice);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-01-06 19:33:53 +01:00
|
|
|
}
|
|
|
|
|
2012-05-03 22:49:46 +02:00
|
|
|
/*
|
|
|
|
* The device mapper node needs to be created. Sometimes it takes a
|
|
|
|
* while. Wait for up to 1 second. We could also inspect incoming uevents,
|
|
|
|
* but that would take more effort.
|
|
|
|
*/
|
|
|
|
int tries = 25;
|
|
|
|
while (tries--) {
|
|
|
|
if (!access(dmDevice, F_OK) || errno != ENOENT) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
usleep(40 * 1000);
|
|
|
|
}
|
|
|
|
|
2012-04-04 02:23:01 +02:00
|
|
|
int result;
|
|
|
|
if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
|
|
|
|
result = Ext4::doMount(dmDevice, mountPoint, true, false, true);
|
|
|
|
} else {
|
|
|
|
result = Fat::doMount(dmDevice, mountPoint, true, false, true, ownerUid, 0, 0222, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("ASEC mount failed (%s)", strerror(errno));
|
2010-01-10 22:02:12 +01:00
|
|
|
if (cleanupDm) {
|
2010-03-12 22:32:47 +01:00
|
|
|
Devmapper::destroy(idHash);
|
2010-01-10 22:02:12 +01:00
|
|
|
}
|
|
|
|
Loop::destroyByDevice(loopDevice);
|
2010-01-06 19:33:53 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-25 00:11:48 +02:00
|
|
|
mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
|
2010-03-12 22:32:47 +01:00
|
|
|
if (mDebug) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGD("ASEC %s mounted", id);
|
2010-03-12 22:32:47 +01:00
|
|
|
}
|
2010-01-06 19:33:53 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-01 03:48:41 +02:00
|
|
|
/**
|
|
|
|
* Mounts an image file <code>img</code>.
|
|
|
|
*/
|
2010-07-12 18:59:49 +02:00
|
|
|
int VolumeManager::mountObb(const char *img, const char *key, int ownerUid) {
|
2010-07-01 03:48:41 +02:00
|
|
|
char mountPoint[255];
|
|
|
|
|
|
|
|
char idHash[33];
|
|
|
|
if (!asecHash(img, idHash, sizeof(idHash))) {
|
|
|
|
SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash);
|
|
|
|
|
|
|
|
if (isMountpointMounted(mountPoint)) {
|
|
|
|
SLOGE("Image %s already mounted", img);
|
|
|
|
errno = EBUSY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char loopDevice[255];
|
|
|
|
if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
|
|
|
|
if (Loop::create(idHash, img, loopDevice, sizeof(loopDevice))) {
|
|
|
|
SLOGE("Image loop device creation failed (%s)", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (mDebug) {
|
|
|
|
SLOGD("New loop device created at %s", loopDevice);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (mDebug) {
|
|
|
|
SLOGD("Found active loopback for %s at %s", img, loopDevice);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char dmDevice[255];
|
|
|
|
bool cleanupDm = false;
|
|
|
|
int fd;
|
|
|
|
unsigned int nr_sec = 0;
|
|
|
|
|
|
|
|
if ((fd = open(loopDevice, O_RDWR)) < 0) {
|
|
|
|
SLOGE("Failed to open loopdevice (%s)", strerror(errno));
|
|
|
|
Loop::destroyByDevice(loopDevice);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(fd, BLKGETSIZE, &nr_sec)) {
|
|
|
|
SLOGE("Failed to get loop size (%s)", strerror(errno));
|
|
|
|
Loop::destroyByDevice(loopDevice);
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (strcmp(key, "none")) {
|
|
|
|
if (Devmapper::lookupActive(idHash, dmDevice, sizeof(dmDevice))) {
|
|
|
|
if (Devmapper::create(idHash, loopDevice, key, nr_sec,
|
|
|
|
dmDevice, sizeof(dmDevice))) {
|
|
|
|
SLOGE("ASEC device mapping failed (%s)", strerror(errno));
|
|
|
|
Loop::destroyByDevice(loopDevice);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (mDebug) {
|
|
|
|
SLOGD("New devmapper instance created at %s", dmDevice);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (mDebug) {
|
|
|
|
SLOGD("Found active devmapper for %s at %s", img, dmDevice);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cleanupDm = true;
|
|
|
|
} else {
|
|
|
|
strcpy(dmDevice, loopDevice);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mkdir(mountPoint, 0755)) {
|
|
|
|
if (errno != EEXIST) {
|
|
|
|
SLOGE("Mountpoint creation failed (%s)", strerror(errno));
|
|
|
|
if (cleanupDm) {
|
|
|
|
Devmapper::destroy(idHash);
|
|
|
|
}
|
|
|
|
Loop::destroyByDevice(loopDevice);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-27 17:31:35 +02:00
|
|
|
if (Fat::doMount(dmDevice, mountPoint, true, false, true, ownerUid, 0,
|
2010-07-01 03:48:41 +02:00
|
|
|
0227, false)) {
|
|
|
|
SLOGE("Image mount failed (%s)", strerror(errno));
|
|
|
|
if (cleanupDm) {
|
|
|
|
Devmapper::destroy(idHash);
|
|
|
|
}
|
|
|
|
Loop::destroyByDevice(loopDevice);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-25 00:11:48 +02:00
|
|
|
mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
|
2010-07-01 03:48:41 +02:00
|
|
|
if (mDebug) {
|
|
|
|
SLOGD("Image %s mounted", img);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-13 01:29:01 +02:00
|
|
|
int VolumeManager::mountVolume(const char *label) {
|
|
|
|
Volume *v = lookupVolume(label);
|
|
|
|
|
|
|
|
if (!v) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-12-13 19:40:18 +01:00
|
|
|
return v->mountVol();
|
|
|
|
}
|
|
|
|
|
2010-07-12 18:59:49 +02:00
|
|
|
int VolumeManager::listMountedObbs(SocketClient* cli) {
|
|
|
|
char device[256];
|
|
|
|
char mount_path[256];
|
|
|
|
char rest[256];
|
|
|
|
FILE *fp;
|
|
|
|
char line[1024];
|
|
|
|
|
|
|
|
if (!(fp = fopen("/proc/mounts", "r"))) {
|
|
|
|
SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a string to compare against that has a trailing slash
|
|
|
|
int loopDirLen = sizeof(Volume::LOOPDIR);
|
|
|
|
char loopDir[loopDirLen + 2];
|
|
|
|
strcpy(loopDir, Volume::LOOPDIR);
|
|
|
|
loopDir[loopDirLen++] = '/';
|
|
|
|
loopDir[loopDirLen] = '\0';
|
|
|
|
|
|
|
|
while(fgets(line, sizeof(line), fp)) {
|
|
|
|
line[strlen(line)-1] = '\0';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Should look like:
|
|
|
|
* /dev/block/loop0 /mnt/obb/fc99df1323fd36424f864dcb76b76d65 ...
|
|
|
|
*/
|
|
|
|
sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
|
|
|
|
|
|
|
|
if (!strncmp(mount_path, loopDir, loopDirLen)) {
|
|
|
|
int fd = open(device, O_RDONLY);
|
|
|
|
if (fd >= 0) {
|
|
|
|
struct loop_info64 li;
|
|
|
|
if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
|
|
|
|
cli->sendMsg(ResponseCode::AsecListResult,
|
|
|
|
(const char*) li.lo_file_name, false);
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-29 14:15:16 +01:00
|
|
|
int VolumeManager::shareEnabled(const char *label, const char *method, bool *enabled) {
|
|
|
|
Volume *v = lookupVolume(label);
|
|
|
|
|
|
|
|
if (!v) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(method, "ums")) {
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v->getState() != Volume::State_Shared) {
|
|
|
|
*enabled = false;
|
2010-02-05 00:07:01 +01:00
|
|
|
} else {
|
|
|
|
*enabled = true;
|
2010-01-29 14:15:16 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-13 19:40:18 +01:00
|
|
|
int VolumeManager::shareVolume(const char *label, const char *method) {
|
|
|
|
Volume *v = lookupVolume(label);
|
|
|
|
|
|
|
|
if (!v) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Eventually, we'll want to support additional share back-ends,
|
|
|
|
* some of which may work while the media is mounted. For now,
|
|
|
|
* we just support UMS
|
|
|
|
*/
|
|
|
|
if (strcmp(method, "ums")) {
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v->getState() == Volume::State_NoMedia) {
|
|
|
|
errno = ENODEV;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-10-13 01:29:01 +02:00
|
|
|
if (v->getState() != Volume::State_Idle) {
|
2009-12-13 19:40:18 +01:00
|
|
|
// You need to unmount manually befoe sharing
|
2009-10-13 01:29:01 +02:00
|
|
|
errno = EBUSY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-07-12 00:38:57 +02:00
|
|
|
if (mVolManagerDisabled) {
|
|
|
|
errno = EBUSY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-18 00:50:51 +02:00
|
|
|
dev_t d = v->getShareDevice();
|
2009-12-13 19:40:18 +01:00
|
|
|
if ((MAJOR(d) == 0) && (MINOR(d) == 0)) {
|
|
|
|
// This volume does not support raw disk access
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd;
|
|
|
|
char nodepath[255];
|
|
|
|
snprintf(nodepath,
|
|
|
|
sizeof(nodepath), "/dev/block/vold/%d:%d",
|
|
|
|
MAJOR(d), MINOR(d));
|
|
|
|
|
2011-06-07 19:51:38 +02:00
|
|
|
if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
|
2009-12-13 19:40:18 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write(fd, nodepath, strlen(nodepath)) < 0) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
|
2009-12-13 19:40:18 +01:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
v->handleVolumeShared();
|
2010-10-28 21:21:24 +02:00
|
|
|
if (mUmsSharingCount++ == 0) {
|
|
|
|
FILE* fp;
|
|
|
|
mSavedDirtyRatio = -1; // in case we fail
|
|
|
|
if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
|
|
|
|
char line[16];
|
|
|
|
if (fgets(line, sizeof(line), fp) && sscanf(line, "%d", &mSavedDirtyRatio)) {
|
|
|
|
fprintf(fp, "%d\n", mUmsDirtyRatio);
|
|
|
|
} else {
|
|
|
|
SLOGE("Failed to read dirty_ratio (%s)", strerror(errno));
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
} else {
|
|
|
|
SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
2009-12-13 19:40:18 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VolumeManager::unshareVolume(const char *label, const char *method) {
|
|
|
|
Volume *v = lookupVolume(label);
|
|
|
|
|
|
|
|
if (!v) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(method, "ums")) {
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v->getState() != Volume::State_Shared) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd;
|
2011-06-07 19:51:38 +02:00
|
|
|
if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
|
2009-12-13 19:40:18 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char ch = 0;
|
|
|
|
if (write(fd, &ch, 1) < 0) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
|
2009-12-13 19:40:18 +01:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
v->handleVolumeUnshared();
|
2010-10-28 21:21:24 +02:00
|
|
|
if (--mUmsSharingCount == 0 && mSavedDirtyRatio != -1) {
|
|
|
|
FILE* fp;
|
|
|
|
if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
|
|
|
|
fprintf(fp, "%d\n", mSavedDirtyRatio);
|
|
|
|
fclose(fp);
|
|
|
|
} else {
|
|
|
|
SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
|
|
|
|
}
|
|
|
|
mSavedDirtyRatio = -1;
|
|
|
|
}
|
2009-12-13 19:40:18 +01:00
|
|
|
return 0;
|
2009-10-13 01:29:01 +02:00
|
|
|
}
|
|
|
|
|
2011-07-12 00:38:57 +02:00
|
|
|
extern "C" int vold_disableVol(const char *label) {
|
2011-05-19 02:20:07 +02:00
|
|
|
VolumeManager *vm = VolumeManager::Instance();
|
2011-07-12 00:38:57 +02:00
|
|
|
vm->disableVolumeManager();
|
|
|
|
vm->unshareVolume(label, "ums");
|
2011-09-01 01:14:23 +02:00
|
|
|
return vm->unmountVolume(label, true, false);
|
2011-05-19 02:20:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int vold_getNumDirectVolumes(void) {
|
|
|
|
VolumeManager *vm = VolumeManager::Instance();
|
|
|
|
return vm->getNumDirectVolumes();
|
|
|
|
}
|
|
|
|
|
|
|
|
int VolumeManager::getNumDirectVolumes(void) {
|
|
|
|
VolumeCollection::iterator i;
|
|
|
|
int n=0;
|
|
|
|
|
|
|
|
for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
|
|
|
|
if ((*i)->getShareDevice() != (dev_t)0) {
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int vold_getDirectVolumeList(struct volume_info *vol_list) {
|
|
|
|
VolumeManager *vm = VolumeManager::Instance();
|
|
|
|
return vm->getDirectVolumeList(vol_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
int VolumeManager::getDirectVolumeList(struct volume_info *vol_list) {
|
|
|
|
VolumeCollection::iterator i;
|
|
|
|
int n=0;
|
|
|
|
dev_t d;
|
|
|
|
|
|
|
|
for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
|
|
|
|
if ((d=(*i)->getShareDevice()) != (dev_t)0) {
|
|
|
|
(*i)->getVolInfo(&vol_list[n]);
|
|
|
|
snprintf(vol_list[n].blk_dev, sizeof(vol_list[n].blk_dev),
|
|
|
|
"/dev/block/vold/%d:%d",MAJOR(d), MINOR(d));
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-01 01:14:23 +02:00
|
|
|
int VolumeManager::unmountVolume(const char *label, bool force, bool revert) {
|
2009-10-13 01:29:01 +02:00
|
|
|
Volume *v = lookupVolume(label);
|
|
|
|
|
|
|
|
if (!v) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-12-13 19:40:18 +01:00
|
|
|
if (v->getState() == Volume::State_NoMedia) {
|
|
|
|
errno = ENODEV;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-10-13 01:29:01 +02:00
|
|
|
if (v->getState() != Volume::State_Mounted) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGW("Attempt to unmount volume which isn't mounted (%d)\n",
|
2009-12-13 19:40:18 +01:00
|
|
|
v->getState());
|
2009-10-13 01:29:01 +02:00
|
|
|
errno = EBUSY;
|
2011-06-14 23:01:55 +02:00
|
|
|
return UNMOUNT_NOT_MOUNTED_ERR;
|
2009-10-13 01:29:01 +02:00
|
|
|
}
|
|
|
|
|
2010-04-15 21:58:50 +02:00
|
|
|
cleanupAsec(v, force);
|
2010-01-15 18:26:28 +01:00
|
|
|
|
2011-09-01 01:14:23 +02:00
|
|
|
return v->unmountVol(force, revert);
|
2009-10-13 01:29:01 +02:00
|
|
|
}
|
|
|
|
|
2012-06-15 05:55:28 +02:00
|
|
|
extern "C" int vold_unmountAllAsecs(void) {
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
VolumeManager *vm = VolumeManager::Instance();
|
|
|
|
rc = vm->unmountAllAsecsInDir(Volume::SEC_ASECDIR_EXT);
|
|
|
|
if (vm->unmountAllAsecsInDir(Volume::SEC_ASECDIR_INT)) {
|
|
|
|
rc = -1;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ID_BUF_LEN 256
|
|
|
|
#define ASEC_SUFFIX ".asec"
|
|
|
|
#define ASEC_SUFFIX_LEN (sizeof(ASEC_SUFFIX) - 1)
|
|
|
|
int VolumeManager::unmountAllAsecsInDir(const char *directory) {
|
|
|
|
DIR *d = opendir(directory);
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
if (!d) {
|
|
|
|
SLOGE("Could not open asec dir %s", directory);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t dirent_len = offsetof(struct dirent, d_name) +
|
|
|
|
pathconf(directory, _PC_NAME_MAX) + 1;
|
|
|
|
|
|
|
|
struct dirent *dent = (struct dirent *) malloc(dirent_len);
|
|
|
|
if (dent == NULL) {
|
|
|
|
SLOGE("Failed to allocate memory for asec dir");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dirent *result;
|
|
|
|
while (!readdir_r(d, dent, &result) && result != NULL) {
|
|
|
|
if (dent->d_name[0] == '.')
|
|
|
|
continue;
|
|
|
|
if (dent->d_type != DT_REG)
|
|
|
|
continue;
|
|
|
|
size_t name_len = strlen(dent->d_name);
|
|
|
|
if (name_len > 5 && name_len < (ID_BUF_LEN + ASEC_SUFFIX_LEN - 1) &&
|
|
|
|
!strcmp(&dent->d_name[name_len - 5], ASEC_SUFFIX)) {
|
|
|
|
char id[ID_BUF_LEN];
|
|
|
|
strlcpy(id, dent->d_name, name_len - 4);
|
|
|
|
if (unmountAsec(id, true)) {
|
|
|
|
/* Register the error, but try to unmount more asecs */
|
|
|
|
rc = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(d);
|
|
|
|
|
|
|
|
free(dent);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2009-12-13 19:40:18 +01:00
|
|
|
/*
|
|
|
|
* Looks up a volume by it's label or mount-point
|
|
|
|
*/
|
2009-10-13 01:29:01 +02:00
|
|
|
Volume *VolumeManager::lookupVolume(const char *label) {
|
|
|
|
VolumeCollection::iterator i;
|
|
|
|
|
|
|
|
for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
|
2009-12-13 19:40:18 +01:00
|
|
|
if (label[0] == '/') {
|
|
|
|
if (!strcmp(label, (*i)->getMountpoint()))
|
|
|
|
return (*i);
|
|
|
|
} else {
|
|
|
|
if (!strcmp(label, (*i)->getLabel()))
|
|
|
|
return (*i);
|
|
|
|
}
|
2009-10-13 01:29:01 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-01-06 19:33:53 +01:00
|
|
|
|
|
|
|
bool VolumeManager::isMountpointMounted(const char *mp)
|
|
|
|
{
|
|
|
|
char device[256];
|
|
|
|
char mount_path[256];
|
|
|
|
char rest[256];
|
|
|
|
FILE *fp;
|
|
|
|
char line[1024];
|
|
|
|
|
|
|
|
if (!(fp = fopen("/proc/mounts", "r"))) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
|
2010-01-06 19:33:53 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(fgets(line, sizeof(line), fp)) {
|
|
|
|
line[strlen(line)-1] = '\0';
|
|
|
|
sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
|
|
|
|
if (!strcmp(mount_path, mp)) {
|
|
|
|
fclose(fp);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-04-15 21:58:50 +02:00
|
|
|
int VolumeManager::cleanupAsec(Volume *v, bool force) {
|
|
|
|
while(mActiveContainers->size()) {
|
|
|
|
AsecIdCollection::iterator it = mActiveContainers->begin();
|
2010-09-25 00:11:48 +02:00
|
|
|
ContainerData* cd = *it;
|
|
|
|
SLOGI("Unmounting ASEC %s (dependant on %s)", cd->id, v->getMountpoint());
|
|
|
|
if (cd->type == ASEC) {
|
|
|
|
if (unmountAsec(cd->id, force)) {
|
|
|
|
SLOGE("Failed to unmount ASEC %s (%s)", cd->id, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (cd->type == OBB) {
|
|
|
|
if (unmountObb(cd->id, force)) {
|
|
|
|
SLOGE("Failed to unmount OBB %s (%s)", cd->id, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
SLOGE("Unknown container type %d!", cd->type);
|
2010-04-15 21:58:50 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|