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>
|
2010-01-06 19:33:53 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
#include <cutils/log.h>
|
|
|
|
|
2009-10-12 20:32:47 +02:00
|
|
|
#include <sysutils/NetlinkEvent.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"
|
|
|
|
#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-01-09 16:08:06 +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() {
|
|
|
|
mBlockDevices = new BlockDeviceCollection();
|
|
|
|
mVolumes = new VolumeCollection();
|
2010-01-15 18:26:28 +01:00
|
|
|
mActiveContainers = new AsecIdCollection();
|
2009-10-11 02:22:08 +02:00
|
|
|
mBroadcaster = NULL;
|
2009-12-13 19:40:18 +01:00
|
|
|
mUsbMassStorageConnected = false;
|
2009-10-11 02:22:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VolumeManager::~VolumeManager() {
|
|
|
|
delete mBlockDevices;
|
2010-01-15 18:26:28 +01:00
|
|
|
delete mVolumes;
|
|
|
|
delete mActiveContainers;
|
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-12-13 19:40:18 +01:00
|
|
|
void VolumeManager::notifyUmsConnected(bool connected) {
|
|
|
|
char msg[255];
|
|
|
|
|
|
|
|
if (connected) {
|
|
|
|
mUsbMassStorageConnected = true;
|
|
|
|
} else {
|
|
|
|
mUsbMassStorageConnected = false;
|
|
|
|
}
|
|
|
|
snprintf(msg, sizeof(msg), "Share method ums now %s",
|
|
|
|
(connected ? "available" : "unavailable"));
|
|
|
|
|
|
|
|
getBroadcaster()->sendBroadcast(ResponseCode::ShareAvailabilityChange,
|
|
|
|
msg, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeManager::handleSwitchEvent(NetlinkEvent *evt) {
|
2009-12-22 17:32:33 +01:00
|
|
|
const char *devpath = evt->findParam("DEVPATH");
|
2009-12-13 19:40:18 +01:00
|
|
|
const char *name = evt->findParam("SWITCH_NAME");
|
|
|
|
const char *state = evt->findParam("SWITCH_STATE");
|
|
|
|
|
2009-12-22 17:32:33 +01:00
|
|
|
if (!name || !state) {
|
|
|
|
LOGW("Switch %s event missing name/state info", devpath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-12-13 19:40:18 +01:00
|
|
|
if (!strcmp(name, "usb_mass_storage")) {
|
|
|
|
|
|
|
|
if (!strcmp(state, "online")) {
|
|
|
|
notifyUmsConnected(true);
|
|
|
|
} else {
|
|
|
|
notifyUmsConnected(false);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOGW("Ignoring unknown switch '%s'", name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
LOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel());
|
|
|
|
#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
|
2009-10-12 20:32:47 +02:00
|
|
|
LOGW("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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return v->formatVol();
|
|
|
|
}
|
|
|
|
|
2010-01-06 19:33:53 +01:00
|
|
|
int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-01-11 18:17:25 +01:00
|
|
|
int VolumeManager::createAsec(const char *id, unsigned int numSectors,
|
2010-01-06 19:33:53 +01:00
|
|
|
const char *fstype, const char *key, int ownerUid) {
|
|
|
|
|
2010-02-18 17:37:45 +01:00
|
|
|
if (numSectors < ((1024*1024)/512)) {
|
|
|
|
LOGE("Invalid container size specified (%d sectors)", numSectors);
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-01-06 19:33:53 +01:00
|
|
|
if (lookupVolume(id)) {
|
2010-02-20 03:14:36 +01:00
|
|
|
LOGE("ASEC id '%s' currently exists", id);
|
2010-01-06 19:33:53 +01:00
|
|
|
errno = EADDRINUSE;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char asecFileName[255];
|
2010-02-20 03:14:36 +01:00
|
|
|
snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id);
|
2010-01-06 19:33:53 +01:00
|
|
|
|
|
|
|
if (!access(asecFileName, F_OK)) {
|
|
|
|
LOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
|
|
|
|
asecFileName, strerror(errno));
|
|
|
|
errno = EADDRINUSE;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-01-11 18:17:25 +01:00
|
|
|
if (Loop::createImageFile(asecFileName, numSectors)) {
|
2010-01-06 19:33:53 +01:00
|
|
|
LOGE("ASEC image file creation failed (%s)", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char loopDevice[255];
|
2010-01-09 21:24:05 +01:00
|
|
|
if (Loop::create(asecFileName, loopDevice, sizeof(loopDevice))) {
|
2010-01-06 19:33:53 +01:00
|
|
|
LOGE("ASEC loop device creation failed (%s)", strerror(errno));
|
|
|
|
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-01-11 18:17:25 +01:00
|
|
|
if (Devmapper::create(id, loopDevice, key, numSectors, dmDevice,
|
2010-01-10 22:02:12 +01:00
|
|
|
sizeof(dmDevice))) {
|
|
|
|
LOGE("ASEC device mapping failed (%s)", strerror(errno));
|
|
|
|
Loop::destroyByDevice(loopDevice);
|
|
|
|
unlink(asecFileName);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cleanupDm = true;
|
|
|
|
} else {
|
|
|
|
strcpy(dmDevice, loopDevice);
|
|
|
|
}
|
|
|
|
|
2010-03-01 05:17:20 +01:00
|
|
|
if (strcmp(fstype, "none")) {
|
|
|
|
if (strcmp(fstype, "fat")) {
|
|
|
|
LOGW("Unknown fstype '%s' specified for container", fstype);
|
2010-01-10 22:02:12 +01:00
|
|
|
}
|
2010-01-06 19:33:53 +01:00
|
|
|
|
2010-03-01 05:17:20 +01:00
|
|
|
if (Fat::format(dmDevice)) {
|
|
|
|
LOGE("ASEC FAT format failed (%s)", strerror(errno));
|
2010-01-10 22:02:12 +01:00
|
|
|
if (cleanupDm) {
|
|
|
|
Devmapper::destroy(id);
|
|
|
|
}
|
2010-01-07 21:12:50 +01:00
|
|
|
Loop::destroyByDevice(loopDevice);
|
|
|
|
unlink(asecFileName);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-03-01 05:17:20 +01:00
|
|
|
char mountPoint[255];
|
|
|
|
|
|
|
|
snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
|
|
|
|
if (mkdir(mountPoint, 0777)) {
|
|
|
|
if (errno != EEXIST) {
|
|
|
|
LOGE("Mountpoint creation failed (%s)", strerror(errno));
|
|
|
|
if (cleanupDm) {
|
|
|
|
Devmapper::destroy(id);
|
|
|
|
}
|
|
|
|
Loop::destroyByDevice(loopDevice);
|
|
|
|
unlink(asecFileName);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2010-01-06 19:33:53 +01:00
|
|
|
|
2010-03-01 05:17:20 +01:00
|
|
|
if (Fat::doMount(dmDevice, mountPoint, false, false, ownerUid,
|
|
|
|
0, 0000, false)) {
|
|
|
|
LOGE("ASEC FAT mount failed (%s)", strerror(errno));
|
|
|
|
if (cleanupDm) {
|
|
|
|
Devmapper::destroy(id);
|
|
|
|
}
|
|
|
|
Loop::destroyByDevice(loopDevice);
|
|
|
|
unlink(asecFileName);
|
|
|
|
return -1;
|
2010-01-10 22:02:12 +01:00
|
|
|
}
|
2010-03-01 05:17:20 +01:00
|
|
|
} else {
|
|
|
|
LOGI("Created raw secure container %s (no filesystem)", id);
|
2010-01-06 19:33:53 +01:00
|
|
|
}
|
2010-01-15 18:26:28 +01:00
|
|
|
|
|
|
|
mActiveContainers->push_back(strdup(id));
|
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];
|
|
|
|
|
2010-02-20 03:14:36 +01:00
|
|
|
snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id);
|
2010-01-06 19:33:53 +01:00
|
|
|
|
|
|
|
if (Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) {
|
|
|
|
LOGE("Unable to finalize %s (%s)", id, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-02-20 03:14:36 +01:00
|
|
|
snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
|
2010-01-07 04:19:46 +01:00
|
|
|
// XXX:
|
|
|
|
if (Fat::doMount(loopDevice, mountPoint, true, true, 0, 0, 0227, false)) {
|
2010-01-06 19:33:53 +01:00
|
|
|
LOGE("ASEC finalize mount failed (%s)", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGD("ASEC %s finalized", id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-23 17:17:06 +01:00
|
|
|
int VolumeManager::renameAsec(const char *id1, const char *id2) {
|
|
|
|
char *asecFilename1;
|
|
|
|
char *asecFilename2;
|
|
|
|
char mountPoint[255];
|
|
|
|
|
2010-02-20 03:14:36 +01:00
|
|
|
asprintf(&asecFilename1, "%s/%s.asec", Volume::SEC_ASECDIR, id1);
|
|
|
|
asprintf(&asecFilename2, "%s/%s.asec", Volume::SEC_ASECDIR, 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)) {
|
|
|
|
LOGW("Rename attempt when src mounted");
|
|
|
|
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)) {
|
|
|
|
LOGW("Rename attempt when dst mounted");
|
|
|
|
errno = EBUSY;
|
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
|
2010-01-23 17:17:06 +01:00
|
|
|
if (!access(asecFilename2, F_OK)) {
|
|
|
|
LOGE("Rename attempt when dst exists");
|
|
|
|
errno = EADDRINUSE;
|
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rename(asecFilename1, asecFilename2)) {
|
|
|
|
LOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
|
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(asecFilename1);
|
|
|
|
free(asecFilename2);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out_err:
|
|
|
|
free(asecFilename1);
|
|
|
|
free(asecFilename2);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-02-18 18:00:18 +01:00
|
|
|
#define ASEC_UNMOUNT_RETRIES 5
|
|
|
|
int VolumeManager::unmountAsec(const char *id, bool force) {
|
2010-01-06 19:33:53 +01:00
|
|
|
char asecFileName[255];
|
|
|
|
char mountPoint[255];
|
|
|
|
|
2010-02-20 03:14:36 +01:00
|
|
|
snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id);
|
|
|
|
snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
|
2010-01-06 19:33:53 +01:00
|
|
|
|
2010-01-13 00:38:59 +01:00
|
|
|
if (!isMountpointMounted(mountPoint)) {
|
2010-01-10 22:02:12 +01:00
|
|
|
LOGE("Unmount request for ASEC %s when not mounted", id);
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-01-09 16:08:06 +01:00
|
|
|
|
2010-01-10 22:02:12 +01:00
|
|
|
int i, rc;
|
2010-02-13 23:19:53 +01:00
|
|
|
for (i = 1; i <= ASEC_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-02-24 20:43:22 +01:00
|
|
|
LOGI("Secure 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-01-10 22:02:12 +01:00
|
|
|
LOGW("ASEC %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) {
|
|
|
|
if (i > (ASEC_UNMOUNT_RETRIES - 2))
|
|
|
|
action = 2; // SIGKILL
|
|
|
|
else if (i > (ASEC_UNMOUNT_RETRIES - 3))
|
|
|
|
action = 1; // SIGHUP
|
|
|
|
}
|
2010-02-13 23:19:53 +01:00
|
|
|
|
2010-02-17 02:12:00 +01:00
|
|
|
Process::killProcessesWithOpenFiles(mountPoint, action);
|
2010-02-13 23:19:53 +01:00
|
|
|
usleep(1000 * 1000);
|
2010-01-10 22:02:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rc) {
|
2010-02-18 18:00:18 +01:00
|
|
|
errno = EBUSY;
|
|
|
|
LOGE("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;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
|
|
|
|
usleep(1000 * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!retries) {
|
|
|
|
LOGE("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-01-10 22:02:12 +01:00
|
|
|
if (Devmapper::destroy(id) && errno != ENXIO) {
|
|
|
|
LOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
|
2010-01-06 19:33:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char loopDevice[255];
|
|
|
|
if (!Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) {
|
|
|
|
Loop::destroyByDevice(loopDevice);
|
|
|
|
}
|
2010-01-15 18:26:28 +01:00
|
|
|
|
|
|
|
AsecIdCollection::iterator it;
|
|
|
|
for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
|
|
|
|
if (!strcmp(*it, id)) {
|
|
|
|
free(*it);
|
|
|
|
mActiveContainers->erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (it == mActiveContainers->end()) {
|
|
|
|
LOGW("mActiveContainers is inconsistent!");
|
|
|
|
}
|
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];
|
|
|
|
|
2010-02-20 03:14:36 +01:00
|
|
|
snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id);
|
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-01-23 16:21:21 +01:00
|
|
|
LOGD("Unmounting container before destroy");
|
2010-02-18 18:00:18 +01:00
|
|
|
if (unmountAsec(id, force)) {
|
2010-01-13 00:38:59 +01:00
|
|
|
LOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2010-01-06 19:33:53 +01:00
|
|
|
|
2010-01-13 00:38:59 +01:00
|
|
|
if (unlink(asecFileName)) {
|
2010-01-23 16:21:21 +01:00
|
|
|
LOGE("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
|
|
|
|
|
|
|
LOGD("ASEC %s destroyed", id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid) {
|
|
|
|
char asecFileName[255];
|
|
|
|
char mountPoint[255];
|
|
|
|
|
2010-02-20 03:14:36 +01:00
|
|
|
snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id);
|
|
|
|
snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
|
2010-01-06 19:33:53 +01:00
|
|
|
|
|
|
|
if (isMountpointMounted(mountPoint)) {
|
|
|
|
LOGE("ASEC %s already mounted", id);
|
|
|
|
errno = EBUSY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char loopDevice[255];
|
|
|
|
if (Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) {
|
2010-01-09 21:24:05 +01:00
|
|
|
if (Loop::create(asecFileName, loopDevice, sizeof(loopDevice))) {
|
2010-01-06 19:33:53 +01:00
|
|
|
LOGE("ASEC loop device creation failed (%s)", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2010-01-10 22:02:12 +01:00
|
|
|
LOGD("New loop device created at %s", loopDevice);
|
|
|
|
} else {
|
|
|
|
LOGD("Found active loopback for %s at %s", asecFileName, loopDevice);
|
|
|
|
}
|
|
|
|
|
|
|
|
char dmDevice[255];
|
|
|
|
bool cleanupDm = false;
|
|
|
|
if (strcmp(key, "none")) {
|
|
|
|
if (Devmapper::lookupActive(id, dmDevice, sizeof(dmDevice))) {
|
|
|
|
unsigned int nr_sec = 0;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if ((fd = open(loopDevice, O_RDWR)) < 0) {
|
|
|
|
LOGE("Failed to open loopdevice (%s)", strerror(errno));
|
|
|
|
Loop::destroyByDevice(loopDevice);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(fd, BLKGETSIZE, &nr_sec)) {
|
|
|
|
LOGE("Failed to get loop size (%s)", strerror(errno));
|
|
|
|
Loop::destroyByDevice(loopDevice);
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
close(fd);
|
2010-01-11 18:17:25 +01:00
|
|
|
if (Devmapper::create(id, loopDevice, key, nr_sec,
|
2010-01-10 22:02:12 +01:00
|
|
|
dmDevice, sizeof(dmDevice))) {
|
|
|
|
LOGE("ASEC device mapping failed (%s)", strerror(errno));
|
|
|
|
Loop::destroyByDevice(loopDevice);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
LOGD("New devmapper instance created at %s", dmDevice);
|
|
|
|
} else {
|
|
|
|
LOGD("Found active devmapper for %s at %s", asecFileName, dmDevice);
|
|
|
|
}
|
|
|
|
cleanupDm = true;
|
|
|
|
} else {
|
|
|
|
strcpy(dmDevice, loopDevice);
|
2010-01-06 19:33:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mkdir(mountPoint, 0777)) {
|
2010-01-10 22:02:12 +01:00
|
|
|
if (errno != EEXIST) {
|
|
|
|
LOGE("Mountpoint creation failed (%s)", strerror(errno));
|
|
|
|
if (cleanupDm) {
|
|
|
|
Devmapper::destroy(id);
|
|
|
|
}
|
|
|
|
Loop::destroyByDevice(loopDevice);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-01-06 19:33:53 +01:00
|
|
|
}
|
|
|
|
|
2010-01-10 22:02:12 +01:00
|
|
|
if (Fat::doMount(dmDevice, mountPoint, true, false, ownerUid, 0,
|
2010-01-08 21:31:44 +01:00
|
|
|
0222, false)) {
|
|
|
|
// 0227, false)) {
|
2010-01-06 19:33:53 +01:00
|
|
|
LOGE("ASEC mount failed (%s)", strerror(errno));
|
2010-01-10 22:02:12 +01:00
|
|
|
if (cleanupDm) {
|
|
|
|
Devmapper::destroy(id);
|
|
|
|
}
|
|
|
|
Loop::destroyByDevice(loopDevice);
|
2010-01-06 19:33:53 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-01-15 18:26:28 +01:00
|
|
|
mActiveContainers->push_back(strdup(id));
|
2010-01-06 19:33:53 +01:00
|
|
|
LOGD("ASEC %s mounted", id);
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
int VolumeManager::shareAvailable(const char *method, bool *avail) {
|
|
|
|
|
|
|
|
if (strcmp(method, "ums")) {
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mUsbMassStorageConnected)
|
|
|
|
*avail = true;
|
|
|
|
else
|
|
|
|
*avail = false;
|
|
|
|
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::simulate(const char *cmd, const char *arg) {
|
|
|
|
|
|
|
|
if (!strcmp(cmd, "ums")) {
|
|
|
|
if (!strcmp(arg, "connect")) {
|
|
|
|
notifyUmsConnected(true);
|
|
|
|
} else if (!strcmp(arg, "disconnect")) {
|
|
|
|
notifyUmsConnected(false);
|
|
|
|
} else {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-12-13 19:40:18 +01:00
|
|
|
dev_t d = v->getDiskDevice();
|
|
|
|
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));
|
|
|
|
|
2009-12-22 17:32:33 +01:00
|
|
|
if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0/file",
|
|
|
|
O_WRONLY)) < 0) {
|
2009-12-13 19:40:18 +01:00
|
|
|
LOGE("Unable to open ums lunfile (%s)", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write(fd, nodepath, strlen(nodepath)) < 0) {
|
|
|
|
LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
v->handleVolumeShared();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev_t d = v->getDiskDevice();
|
|
|
|
|
|
|
|
int fd;
|
|
|
|
char nodepath[255];
|
|
|
|
snprintf(nodepath,
|
|
|
|
sizeof(nodepath), "/dev/block/vold/%d:%d",
|
|
|
|
MAJOR(d), MINOR(d));
|
|
|
|
|
2009-12-22 17:32:33 +01:00
|
|
|
if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0/file", O_WRONLY)) < 0) {
|
2009-12-13 19:40:18 +01:00
|
|
|
LOGE("Unable to open ums lunfile (%s)", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char ch = 0;
|
|
|
|
if (write(fd, &ch, 1) < 0) {
|
|
|
|
LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
v->handleVolumeUnshared();
|
|
|
|
return 0;
|
2009-10-13 01:29:01 +02:00
|
|
|
}
|
|
|
|
|
2010-02-18 18:00:18 +01:00
|
|
|
int VolumeManager::unmountVolume(const char *label, bool force) {
|
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) {
|
2009-12-13 19:40:18 +01:00
|
|
|
LOGW("Attempt to unmount volume which isn't mounted (%d)\n",
|
|
|
|
v->getState());
|
2009-10-13 01:29:01 +02:00
|
|
|
errno = EBUSY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-01-15 18:26:28 +01:00
|
|
|
while(mActiveContainers->size()) {
|
|
|
|
AsecIdCollection::iterator it = mActiveContainers->begin();
|
|
|
|
LOGI("Unmounting ASEC %s (dependant on %s)", *it, v->getMountpoint());
|
2010-02-18 18:00:18 +01:00
|
|
|
if (unmountAsec(*it, force)) {
|
2010-02-24 20:43:22 +01:00
|
|
|
LOGE("Failed to unmount ASEC %s (%s)", *it, strerror(errno));
|
2010-02-24 17:25:55 +01:00
|
|
|
return -1;
|
2010-01-15 18:26:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-18 18:00:18 +01:00
|
|
|
return v->unmountVol(force);
|
2009-10-13 01:29:01 +02:00
|
|
|
}
|
|
|
|
|
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"))) {
|
|
|
|
LOGE("Error opening /proc/mounts (%s)", strerror(errno));
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|