2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
25 #include <sys/ioctl.h>
26 #include <sys/mount.h>
28 #include <sys/types.h>
29 #include <sys/sysmacros.h>
33 #include <linux/kdev_t.h>
35 #define LOG_TAG "Vold"
37 #include <openssl/md5.h>
39 #include <android-base/logging.h>
40 #include <android-base/stringprintf.h>
41 #include <cutils/fs.h>
42 #include <cutils/log.h>
44 #include <selinux/android.h>
46 #include <sysutils/NetlinkEvent.h>
48 #include <private/android_filesystem_config.h>
50 #include "Benchmark.h"
51 #include "EmulatedVolume.h"
52 #include "VolumeManager.h"
53 #include "NetlinkManager.h"
54 #include "ResponseCode.h"
59 #include "Devmapper.h"
65 #define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file"
67 #define ROUND_UP_POWER_OF_2(number, po2) (((!!((number) & ((1U << (po2)) - 1))) << (po2))\
68 + ((number) & (~((1U << (po2)) - 1))))
70 using android::base::StringPrintf;
73 * Path to external storage where *only* root can access ASEC image files
75 const char *VolumeManager::SEC_ASECDIR_EXT = "/mnt/secure/asec";
78 * Path to internal storage where *only* root can access ASEC image files
80 const char *VolumeManager::SEC_ASECDIR_INT = "/data/app-asec";
83 * Path to where secure containers are mounted
85 const char *VolumeManager::ASECDIR = "/mnt/asec";
88 * Path to where OBBs are mounted
90 const char *VolumeManager::LOOPDIR = "/mnt/obb";
92 static const char* kPathUserMount = "/mnt/user";
93 static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk";
95 static const char* kPropVirtualDisk = "persist.sys.virtual_disk";
97 /* 512MiB is large enough for testing purposes */
98 static const unsigned int kSizeVirtualDisk = 536870912;
100 static const unsigned int kMajorBlockMmc = 179;
101 static const unsigned int kMajorBlockExperimentalMin = 240;
102 static const unsigned int kMajorBlockExperimentalMax = 254;
104 /* writes superblock at end of file or device given by name */
105 static int writeSuperBlock(const char* name, struct asec_superblock *sb, unsigned int numImgSectors) {
106 int sbfd = open(name, O_RDWR | O_CLOEXEC);
108 SLOGE("Failed to open %s for superblock write (%s)", name, strerror(errno));
112 if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
113 SLOGE("Failed to lseek for superblock (%s)", strerror(errno));
118 if (write(sbfd, sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
119 SLOGE("Failed to write superblock (%s)", strerror(errno));
127 static unsigned long adjustSectorNumExt4(unsigned long numSectors) {
128 // Ext4 started to reserve 2% or 4096 clusters, whichever is smaller for
129 // preventing costly operations or unexpected ENOSPC error.
130 // Ext4::format() uses default block size without clustering.
131 unsigned long clusterSectors = 4096 / 512;
132 unsigned long reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0);
133 numSectors += reservedSectors > (4096 * clusterSectors) ? (4096 * clusterSectors) : reservedSectors;
134 return ROUND_UP_POWER_OF_2(numSectors, 3);
137 static unsigned long adjustSectorNumFAT(unsigned long numSectors) {
141 unsigned long fatSize = (((numSectors * 4) / 512) + 1) * 2;
142 numSectors += fatSize + 2;
144 * FAT is aligned to 32 kb with 512b sectors.
146 return ROUND_UP_POWER_OF_2(numSectors, 6);
149 static int setupLoopDevice(char* buffer, size_t len, const char* asecFileName, const char* idHash, bool debug) {
150 if (Loop::lookupActive(idHash, buffer, len)) {
151 if (Loop::create(idHash, asecFileName, buffer, len)) {
152 SLOGE("ASEC loop device creation failed for %s (%s)", asecFileName, strerror(errno));
156 SLOGD("New loop device created at %s", buffer);
160 SLOGD("Found active loopback for %s at %s", asecFileName, buffer);
166 static int setupDevMapperDevice(char* buffer, size_t len, const char* loopDevice, const char* asecFileName, const char* key, const char* idHash , unsigned long numImgSectors, bool* createdDMDevice, bool debug) {
167 if (strcmp(key, "none")) {
168 if (Devmapper::lookupActive(idHash, buffer, len)) {
169 if (Devmapper::create(idHash, loopDevice, key, numImgSectors,
171 SLOGE("ASEC device mapping failed for %s (%s)", asecFileName, strerror(errno));
175 SLOGD("New devmapper instance created at %s", buffer);
179 SLOGD("Found active devmapper for %s at %s", asecFileName, buffer);
182 *createdDMDevice = true;
184 strlcpy(buffer, loopDevice, len);
185 *createdDMDevice = false;
190 static void waitForDevMapper(const char *dmDevice) {
192 * Wait for the device mapper node to be created. Sometimes it takes a
193 * while. Wait for up to 1 second. We could also inspect incoming uevents,
194 * but that would take more effort.
198 if (!access(dmDevice, F_OK) || errno != ENOENT) {
205 VolumeManager *VolumeManager::sInstance = NULL;
207 VolumeManager *VolumeManager::Instance() {
209 sInstance = new VolumeManager();
213 VolumeManager::VolumeManager() {
215 mActiveContainers = new AsecIdCollection();
217 mUmsSharingCount = 0;
218 mSavedDirtyRatio = -1;
219 // set dirty ratio to 0 when UMS is active
223 VolumeManager::~VolumeManager() {
224 delete mActiveContainers;
227 char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
228 static const char* digits = "0123456789abcdef";
230 unsigned char sig[MD5_DIGEST_LENGTH];
232 if (buffer == NULL) {
233 SLOGE("Destination buffer is NULL");
236 } else if (id == NULL) {
237 SLOGE("Source buffer is NULL");
240 } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) {
241 SLOGE("Target hash buffer size < %d bytes (%zu)",
242 MD5_ASCII_LENGTH_PLUS_NULL, len);
247 MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig);
250 for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
251 *p++ = digits[sig[i] >> 4];
252 *p++ = digits[sig[i] & 0x0F];
259 int VolumeManager::updateVirtualDisk() {
260 if (property_get_bool(kPropVirtualDisk, false)) {
261 if (access(kPathVirtualDisk, F_OK) != 0) {
262 Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512);
265 if (mVirtualDisk == nullptr) {
266 if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) {
267 LOG(ERROR) << "Failed to create virtual disk";
272 if (stat(mVirtualDiskPath.c_str(), &buf) < 0) {
273 PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath;
277 auto disk = new android::vold::Disk("virtual", buf.st_rdev, "virtual",
278 android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd);
280 mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk);
281 mDisks.push_back(mVirtualDisk);
284 if (mVirtualDisk != nullptr) {
285 dev_t device = mVirtualDisk->getDevice();
287 auto i = mDisks.begin();
288 while (i != mDisks.end()) {
289 if ((*i)->getDevice() == device) {
297 Loop::destroyByDevice(mVirtualDiskPath.c_str());
298 mVirtualDisk = nullptr;
301 if (access(kPathVirtualDisk, F_OK) == 0) {
302 unlink(kPathVirtualDisk);
308 int VolumeManager::setDebug(bool enable) {
313 int VolumeManager::start() {
314 // Always start from a clean slate by unmounting everything in
315 // directories that we own, in case we crashed.
318 // Assume that we always have an emulated volume on internal
319 // storage; the framework will decide if it should be mounted.
320 CHECK(mInternalEmulated == nullptr);
321 mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
322 new android::vold::EmulatedVolume("/data/media"));
323 mInternalEmulated->create();
325 // Consider creating a virtual disk
331 int VolumeManager::stop() {
332 CHECK(mInternalEmulated != nullptr);
333 mInternalEmulated->destroy();
334 mInternalEmulated = nullptr;
338 void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
339 std::lock_guard<std::mutex> lock(mLock);
342 LOG(VERBOSE) << "----------------";
343 LOG(VERBOSE) << "handleBlockEvent with action " << (int) evt->getAction();
347 std::string eventPath(evt->findParam("DEVPATH")?evt->findParam("DEVPATH"):"");
348 std::string devType(evt->findParam("DEVTYPE")?evt->findParam("DEVTYPE"):"");
350 if (devType != "disk") return;
352 int major = atoi(evt->findParam("MAJOR"));
353 int minor = atoi(evt->findParam("MINOR"));
354 dev_t device = makedev(major, minor);
356 switch (evt->getAction()) {
357 case NetlinkEvent::Action::kAdd: {
358 for (const auto& source : mDiskSources) {
359 if (source->matches(eventPath)) {
360 // For now, assume that MMC and virtio-blk (the latter is
361 // emulator-specific; see Disk.cpp for details) devices are SD,
362 // and that everything else is USB
363 int flags = source->getFlags();
364 if (major == kMajorBlockMmc
365 || (android::vold::IsRunningInEmulator()
366 && major >= (int) kMajorBlockExperimentalMin
367 && major <= (int) kMajorBlockExperimentalMax)) {
368 flags |= android::vold::Disk::Flags::kSd;
370 flags |= android::vold::Disk::Flags::kUsb;
373 auto disk = new android::vold::Disk(eventPath, device,
374 source->getNickname(), flags);
376 mDisks.push_back(std::shared_ptr<android::vold::Disk>(disk));
382 case NetlinkEvent::Action::kChange: {
383 LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
384 for (const auto& disk : mDisks) {
385 if (disk->getDevice() == device) {
386 disk->readMetadata();
387 disk->readPartitions();
392 case NetlinkEvent::Action::kRemove: {
393 auto i = mDisks.begin();
394 while (i != mDisks.end()) {
395 if ((*i)->getDevice() == device) {
405 LOG(WARNING) << "Unexpected block event action " << (int) evt->getAction();
411 void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
412 std::lock_guard<std::mutex> lock(mLock);
413 mDiskSources.push_back(diskSource);
416 std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
417 for (auto disk : mDisks) {
418 if (disk->getId() == id) {
425 std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
426 if (mInternalEmulated->getId() == id) {
427 return mInternalEmulated;
429 for (const auto& disk : mDisks) {
430 auto vol = disk->findVolume(id);
431 if (vol != nullptr) {
438 void VolumeManager::listVolumes(android::vold::VolumeBase::Type type,
439 std::list<std::string>& list) {
441 for (const auto& disk : mDisks) {
442 disk->listVolumes(type, list);
446 nsecs_t VolumeManager::benchmarkPrivate(const std::string& id) {
448 if (id == "private" || id == "null") {
451 auto vol = findVolume(id);
452 if (vol != nullptr && vol->getState() == android::vold::VolumeBase::State::kMounted) {
453 path = vol->getPath();
458 LOG(WARNING) << "Failed to find volume for " << id;
462 return android::vold::BenchmarkPrivate(path);
465 int VolumeManager::forgetPartition(const std::string& partGuid) {
466 std::string normalizedGuid;
467 if (android::vold::NormalizeHex(partGuid, normalizedGuid)) {
468 LOG(WARNING) << "Invalid GUID " << partGuid;
472 std::string keyPath = android::vold::BuildKeyPath(normalizedGuid);
473 if (unlink(keyPath.c_str()) != 0) {
474 LOG(ERROR) << "Failed to unlink " << keyPath;
481 int VolumeManager::linkPrimary(userid_t userId) {
482 std::string source(mPrimary->getPath());
483 if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) {
484 source = StringPrintf("%s/%d", source.c_str(), userId);
485 fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT);
488 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
489 if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) {
490 if (errno != ENOENT) {
491 SLOGW("Failed to unlink %s: %s", target.c_str(), strerror(errno));
494 LOG(DEBUG) << "Linking " << source << " to " << target;
495 if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) {
496 SLOGW("Failed to link %s to %s: %s", source.c_str(), target.c_str(),
503 int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
504 mAddedUsers[userId] = userSerialNumber;
508 int VolumeManager::onUserRemoved(userid_t userId) {
509 mAddedUsers.erase(userId);
513 int VolumeManager::onUserStarted(userid_t userId) {
514 // Note that sometimes the system will spin up processes from Zygote
515 // before actually starting the user, so we're okay if Zygote
516 // already created this directory.
517 std::string path(StringPrintf("%s/%d", kPathUserMount, userId));
518 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
520 mStartedUsers.insert(userId);
527 int VolumeManager::onUserStopped(userid_t userId) {
528 mStartedUsers.erase(userId);
532 int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
534 for (userid_t userId : mStartedUsers) {
540 static int unmount_tree(const char* path) {
541 size_t path_len = strlen(path);
543 FILE* fp = setmntent("/proc/mounts", "r");
545 ALOGE("Error opening /proc/mounts: %s", strerror(errno));
549 // Some volumes can be stacked on each other, so force unmount in
550 // reverse order to give us the best chance of success.
551 std::list<std::string> toUnmount;
553 while ((mentry = getmntent(fp)) != NULL) {
554 if (strncmp(mentry->mnt_dir, path, path_len) == 0) {
555 toUnmount.push_front(std::string(mentry->mnt_dir));
560 for (const auto& path : toUnmount) {
561 if (umount2(path.c_str(), MNT_DETACH)) {
562 ALOGW("Failed to unmount %s: %s", path.c_str(), strerror(errno));
568 int VolumeManager::remountUid(uid_t uid, const std::string& mode) {
569 LOG(DEBUG) << "Remounting " << uid << " as mode " << mode;
573 char rootName[PATH_MAX];
574 char pidName[PATH_MAX];
580 if (!(dir = opendir("/proc"))) {
581 PLOG(ERROR) << "Failed to opendir";
585 // Figure out root namespace to compare against below
586 if (android::vold::SaneReadLinkAt(dirfd(dir), "1/ns/mnt", rootName, PATH_MAX) == -1) {
587 PLOG(ERROR) << "Failed to readlink";
592 // Poke through all running PIDs look for apps running as UID
593 while ((de = readdir(dir))) {
597 pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
601 if (fstat(pidFd, &sb) != 0) {
602 PLOG(WARNING) << "Failed to stat " << de->d_name;
605 if (sb.st_uid != uid) {
609 // Matches so far, but refuse to touch if in root namespace
610 LOG(DEBUG) << "Found matching PID " << de->d_name;
611 if (android::vold::SaneReadLinkAt(pidFd, "ns/mnt", pidName, PATH_MAX) == -1) {
612 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
615 if (!strcmp(rootName, pidName)) {
616 LOG(WARNING) << "Skipping due to root namespace";
620 // We purposefully leave the namespace open across the fork
621 nsFd = openat(pidFd, "ns/mnt", O_RDONLY); // not O_CLOEXEC
623 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
627 if (!(child = fork())) {
628 if (setns(nsFd, CLONE_NEWNS) != 0) {
629 PLOG(ERROR) << "Failed to setns for " << de->d_name;
633 unmount_tree("/storage");
635 std::string storageSource;
636 if (mode == "default") {
637 storageSource = "/mnt/runtime/default";
638 } else if (mode == "read") {
639 storageSource = "/mnt/runtime/read";
640 } else if (mode == "write") {
641 storageSource = "/mnt/runtime/write";
643 // Sane default of no storage visible
646 if (TEMP_FAILURE_RETRY(mount(storageSource.c_str(), "/storage",
647 NULL, MS_BIND | MS_REC, NULL)) == -1) {
648 PLOG(ERROR) << "Failed to mount " << storageSource << " for "
652 if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL,
653 MS_REC | MS_SLAVE, NULL)) == -1) {
654 PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for "
659 // Mount user-specific symlink helper into place
660 userid_t user_id = multiuser_get_user_id(uid);
661 std::string userSource(StringPrintf("/mnt/user/%d", user_id));
662 if (TEMP_FAILURE_RETRY(mount(userSource.c_str(), "/storage/self",
663 NULL, MS_BIND, NULL)) == -1) {
664 PLOG(ERROR) << "Failed to mount " << userSource << " for "
673 PLOG(ERROR) << "Failed to fork";
676 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
687 int VolumeManager::reset() {
688 // Tear down all existing disks/volumes and start from a blank slate so
689 // newly connected framework hears all events.
690 mInternalEmulated->destroy();
691 mInternalEmulated->create();
692 for (const auto& disk : mDisks) {
698 mStartedUsers.clear();
702 // Can be called twice (sequentially) during shutdown. should be safe for that.
703 int VolumeManager::shutdown() {
704 if (mInternalEmulated == nullptr) {
705 return 0; // already shutdown
707 mInternalEmulated->destroy();
708 mInternalEmulated = nullptr;
709 for (const auto& disk : mDisks) {
716 int VolumeManager::unmountAll() {
717 std::lock_guard<std::mutex> lock(mLock);
719 // First, try gracefully unmounting all known devices
720 if (mInternalEmulated != nullptr) {
721 mInternalEmulated->unmount();
723 for (const auto& disk : mDisks) {
727 // Worst case we might have some stale mounts lurking around, so
728 // force unmount those just to be safe.
729 FILE* fp = setmntent("/proc/mounts", "r");
731 SLOGE("Error opening /proc/mounts: %s", strerror(errno));
735 // Some volumes can be stacked on each other, so force unmount in
736 // reverse order to give us the best chance of success.
737 std::list<std::string> toUnmount;
739 while ((mentry = getmntent(fp)) != NULL) {
740 if (strncmp(mentry->mnt_dir, "/mnt/", 5) == 0
741 || strncmp(mentry->mnt_dir, "/storage/", 9) == 0) {
742 toUnmount.push_front(std::string(mentry->mnt_dir));
747 for (const auto& path : toUnmount) {
748 SLOGW("Tearing down stale mount %s", path.c_str());
749 android::vold::ForceUnmount(path);
755 int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
757 if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
758 SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
762 memset(mountPath, 0, mountPathLen);
763 int written = snprintf(mountPath, mountPathLen, "%s/%s", VolumeManager::LOOPDIR, idHash);
764 if ((written < 0) || (written >= mountPathLen)) {
769 if (access(mountPath, F_OK)) {
777 int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
778 char asecFileName[255];
780 if (!isLegalAsecId(id)) {
781 SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id);
786 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
787 SLOGE("Couldn't find ASEC %s", id);
791 memset(buffer, 0, maxlen);
792 if (access(asecFileName, F_OK)) {
797 int written = snprintf(buffer, maxlen, "%s/%s", VolumeManager::ASECDIR, id);
798 if ((written < 0) || (written >= maxlen)) {
799 SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id);
807 int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
808 char asecFileName[255];
810 if (!isLegalAsecId(id)) {
811 SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id);
816 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
817 SLOGE("Couldn't find ASEC %s", id);
821 memset(buffer, 0, maxlen);
822 if (access(asecFileName, F_OK)) {
827 int written = snprintf(buffer, maxlen, "%s", asecFileName);
828 if ((written < 0) || (written >= maxlen)) {
836 int VolumeManager::createAsec(const char *id, unsigned long numSectors, const char *fstype,
837 const char *key, const int ownerUid, bool isExternal) {
838 struct asec_superblock sb;
839 memset(&sb, 0, sizeof(sb));
841 if (!isLegalAsecId(id)) {
842 SLOGE("createAsec: Invalid asec id \"%s\"", id);
847 const bool wantFilesystem = strcmp(fstype, "none");
848 bool usingExt4 = false;
849 if (wantFilesystem) {
850 usingExt4 = !strcmp(fstype, "ext4");
852 sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
853 } else if (strcmp(fstype, "fat")) {
854 SLOGE("Invalid filesystem type %s", fstype);
860 sb.magic = ASEC_SB_MAGIC;
861 sb.ver = ASEC_SB_VER;
863 if (numSectors < ((1024*1024)/512)) {
864 SLOGE("Invalid container size specified (%lu sectors)", numSectors);
869 char asecFileName[255];
871 if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
872 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
873 asecFileName, strerror(errno));
878 const char *asecDir = isExternal ? VolumeManager::SEC_ASECDIR_EXT : VolumeManager::SEC_ASECDIR_INT;
880 int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
881 if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) {
886 if (!access(asecFileName, F_OK)) {
887 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
888 asecFileName, strerror(errno));
893 unsigned long numImgSectors;
895 numImgSectors = adjustSectorNumExt4(numSectors);
897 numImgSectors = adjustSectorNumFAT(numSectors);
899 // Add +1 for our superblock which is at the end
900 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
901 SLOGE("ASEC image file creation failed (%s)", strerror(errno));
906 if (!asecHash(id, idHash, sizeof(idHash))) {
907 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
908 unlink(asecFileName);
912 char loopDevice[255];
913 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
914 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
915 unlink(asecFileName);
920 bool cleanupDm = false;
922 if (strcmp(key, "none")) {
923 // XXX: This is all we support for now
924 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
925 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
927 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
928 Loop::destroyByDevice(loopDevice);
929 unlink(asecFileName);
934 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
935 strlcpy(dmDevice, loopDevice, sizeof(dmDevice));
939 * Drop down the superblock at the end of the file
941 if (writeSuperBlock(loopDevice, &sb, numImgSectors)) {
943 Devmapper::destroy(idHash);
945 Loop::destroyByDevice(loopDevice);
946 unlink(asecFileName);
950 if (wantFilesystem) {
952 char mountPoint[255];
954 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
955 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
956 SLOGE("ASEC fs format failed: couldn't construct mountPoint");
958 Devmapper::destroy(idHash);
960 Loop::destroyByDevice(loopDevice);
961 unlink(asecFileName);
966 formatStatus = android::vold::ext4::Format(dmDevice, numImgSectors, mountPoint);
968 formatStatus = android::vold::vfat::Format(dmDevice, numImgSectors);
971 if (formatStatus < 0) {
972 SLOGE("ASEC fs format failed (%s)", strerror(errno));
974 Devmapper::destroy(idHash);
976 Loop::destroyByDevice(loopDevice);
977 unlink(asecFileName);
981 if (mkdir(mountPoint, 0000)) {
982 if (errno != EEXIST) {
983 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
985 Devmapper::destroy(idHash);
987 Loop::destroyByDevice(loopDevice);
988 unlink(asecFileName);
995 mountStatus = android::vold::ext4::Mount(dmDevice, mountPoint,
996 false, false, false);
998 mountStatus = android::vold::vfat::Mount(dmDevice, mountPoint,
999 false, false, false, ownerUid, 0, 0000, false);
1003 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
1005 Devmapper::destroy(idHash);
1007 Loop::destroyByDevice(loopDevice);
1008 unlink(asecFileName);
1013 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
1015 if (fchown(dirfd, ownerUid, AID_SYSTEM)
1016 || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
1017 SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
1023 SLOGI("Created raw secure container %s (no filesystem)", id);
1026 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
1030 int VolumeManager::resizeAsec(const char *id, unsigned long numSectors, const char *key) {
1031 char asecFileName[255];
1032 char mountPoint[255];
1033 bool cleanupDm = false;
1035 if (!isLegalAsecId(id)) {
1036 SLOGE("resizeAsec: Invalid asec id \"%s\"", id);
1041 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1042 SLOGE("Couldn't find ASEC %s", id);
1046 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
1047 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1048 SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id);
1052 if (isMountpointMounted(mountPoint)) {
1053 SLOGE("ASEC %s mounted. Unmount before resizing", id);
1058 struct asec_superblock sb;
1060 unsigned long oldNumSec = 0;
1062 if ((fd = open(asecFileName, O_RDONLY | O_CLOEXEC)) < 0) {
1063 SLOGE("Failed to open ASEC file (%s)", strerror(errno));
1068 if (fstat(fd, &info) < 0) {
1069 SLOGE("Failed to get file size (%s)", strerror(errno));
1074 oldNumSec = info.st_size / 512;
1077 * Try to read superblock.
1079 memset(&sb, 0, sizeof(struct asec_superblock));
1080 if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) {
1081 SLOGE("lseek failed (%s)", strerror(errno));
1085 if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
1086 SLOGE("superblock read failed (%s)", strerror(errno));
1093 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
1095 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
1096 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
1097 errno = EMEDIUMTYPE;
1101 unsigned long numImgSectors;
1102 if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) {
1103 SLOGE("Only ext4 partitions are supported for resize");
1107 numImgSectors = adjustSectorNumExt4(numSectors);
1111 * add one block for the superblock
1113 SLOGD("Resizing from %lu sectors to %lu sectors", oldNumSec, numImgSectors + 1);
1114 if (oldNumSec == numImgSectors + 1) {
1115 SLOGW("Size unchanged; ignoring resize request");
1117 } else if (oldNumSec > numImgSectors + 1) {
1118 SLOGE("Only growing is currently supported.");
1123 if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) {
1124 SLOGE("Resize of ASEC image file failed. Could not resize %s", id);
1129 * Drop down a copy of the superblock at the end of the file
1131 if (writeSuperBlock(asecFileName, &sb, numImgSectors))
1135 if (!asecHash(id, idHash, sizeof(idHash))) {
1136 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1140 char loopDevice[255];
1141 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1146 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) {
1147 Loop::destroyByDevice(loopDevice);
1152 * Wait for the device mapper node to be created.
1154 waitForDevMapper(dmDevice);
1156 if (android::vold::ext4::Resize(dmDevice, numImgSectors)) {
1157 SLOGE("Unable to resize %s (%s)", id, strerror(errno));
1159 Devmapper::destroy(idHash);
1161 Loop::destroyByDevice(loopDevice);
1167 Loop::resizeImageFile(asecFileName, oldNumSec);
1171 int VolumeManager::finalizeAsec(const char *id) {
1172 char asecFileName[255];
1173 char loopDevice[255];
1174 char mountPoint[255];
1176 if (!isLegalAsecId(id)) {
1177 SLOGE("finalizeAsec: Invalid asec id \"%s\"", id);
1182 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1183 SLOGE("Couldn't find ASEC %s", id);
1188 if (!asecHash(id, idHash, sizeof(idHash))) {
1189 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1193 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
1194 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
1198 unsigned long nr_sec = 0;
1199 struct asec_superblock sb;
1201 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1205 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
1206 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1207 SLOGE("ASEC finalize failed: couldn't construct mountPoint");
1212 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
1213 result = android::vold::ext4::Mount(loopDevice, mountPoint,
1216 result = android::vold::vfat::Mount(loopDevice, mountPoint,
1217 true, true, true, 0, 0, 0227, false);
1221 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
1226 SLOGD("ASEC %s finalized", id);
1231 int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
1232 char asecFileName[255];
1233 char loopDevice[255];
1234 char mountPoint[255];
1236 if (gid < AID_APP) {
1237 SLOGE("Group ID is not in application range");
1241 if (!isLegalAsecId(id)) {
1242 SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id);
1247 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1248 SLOGE("Couldn't find ASEC %s", id);
1253 if (!asecHash(id, idHash, sizeof(idHash))) {
1254 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1258 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
1259 SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
1263 unsigned long nr_sec = 0;
1264 struct asec_superblock sb;
1266 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1270 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
1271 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1272 SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id);
1277 if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
1281 int ret = android::vold::ext4::Mount(loopDevice, mountPoint,
1282 false /* read-only */,
1284 false /* executable */);
1286 SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
1290 char *paths[] = { mountPoint, NULL };
1292 FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
1294 // Traverse the entire hierarchy and chown to system UID.
1295 for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
1296 // We don't care about the lost+found directory.
1297 if (!strcmp(ftsent->fts_name, "lost+found")) {
1302 * There can only be one file marked as private right now.
1303 * This should be more robust, but it satisfies the requirements
1304 * we have for right now.
1306 const bool privateFile = !strcmp(ftsent->fts_name, filename);
1308 int fd = open(ftsent->fts_accpath, O_NOFOLLOW | O_CLOEXEC);
1310 SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
1315 result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
1317 if (ftsent->fts_info & FTS_D) {
1318 result |= fchmod(fd, 0755);
1319 } else if (ftsent->fts_info & FTS_F) {
1320 result |= fchmod(fd, privateFile ? 0640 : 0644);
1323 if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) {
1324 SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno));
1332 // Finally make the directory readable by everyone.
1333 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
1334 if (dirfd < 0 || fchmod(dirfd, 0755)) {
1335 SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
1343 result |= android::vold::ext4::Mount(loopDevice, mountPoint,
1344 true /* read-only */,
1346 true /* execute */);
1349 SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
1354 SLOGD("ASEC %s permissions fixed", id);
1359 int VolumeManager::renameAsec(const char *id1, const char *id2) {
1360 char asecFilename1[255];
1361 char *asecFilename2;
1362 char mountPoint[255];
1366 if (!isLegalAsecId(id1)) {
1367 SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1);
1372 if (!isLegalAsecId(id2)) {
1373 SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2);
1378 if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
1379 SLOGE("Couldn't find ASEC %s", id1);
1383 asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
1385 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id1);
1386 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1387 SLOGE("Rename failed: couldn't construct mountpoint");
1391 if (isMountpointMounted(mountPoint)) {
1392 SLOGW("Rename attempt when src mounted");
1397 written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id2);
1398 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1399 SLOGE("Rename failed: couldn't construct mountpoint2");
1403 if (isMountpointMounted(mountPoint)) {
1404 SLOGW("Rename attempt when dst mounted");
1409 if (!access(asecFilename2, F_OK)) {
1410 SLOGE("Rename attempt when dst exists");
1415 if (rename(asecFilename1, asecFilename2)) {
1416 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
1420 free(asecFilename2);
1424 free(asecFilename2);
1428 #define UNMOUNT_RETRIES 5
1429 #define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
1430 int VolumeManager::unmountAsec(const char *id, bool force) {
1431 char asecFileName[255];
1432 char mountPoint[255];
1434 if (!isLegalAsecId(id)) {
1435 SLOGE("unmountAsec: Invalid asec id \"%s\"", id);
1440 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1441 SLOGE("Couldn't find ASEC %s", id);
1445 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
1446 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1447 SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id);
1452 if (!asecHash(id, idHash, sizeof(idHash))) {
1453 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1457 return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
1460 int VolumeManager::unmountObb(const char *fileName, bool force) {
1461 char mountPoint[255];
1464 if (!asecHash(fileName, idHash, sizeof(idHash))) {
1465 SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
1469 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
1470 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1471 SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName);
1475 return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
1478 int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
1479 const char *fileName, const char *mountPoint, bool force) {
1480 if (!isMountpointMounted(mountPoint)) {
1481 SLOGE("Unmount request for %s when not mounted", id);
1487 for (i = 1; i <= UNMOUNT_RETRIES; i++) {
1488 rc = umount(mountPoint);
1492 if (rc && (errno == EINVAL || errno == ENOENT)) {
1493 SLOGI("Container %s unmounted OK", id);
1497 SLOGW("%s unmount attempt %d failed (%s)",
1498 id, i, strerror(errno));
1500 int signal = 0; // default is to just complain
1503 if (i > (UNMOUNT_RETRIES - 2))
1505 else if (i > (UNMOUNT_RETRIES - 3))
1509 Process::killProcessesWithOpenFiles(mountPoint, signal);
1510 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
1515 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
1522 if (!rmdir(mountPoint)) {
1526 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
1527 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
1531 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
1534 for (i=1; i <= UNMOUNT_RETRIES; i++) {
1535 if (Devmapper::destroy(idHash) && errno != ENXIO) {
1536 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
1537 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
1544 char loopDevice[255];
1545 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
1546 Loop::destroyByDevice(loopDevice);
1548 SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
1551 AsecIdCollection::iterator it;
1552 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
1553 ContainerData* cd = *it;
1554 if (!strcmp(cd->id, id)) {
1556 mActiveContainers->erase(it);
1560 if (it == mActiveContainers->end()) {
1561 SLOGW("mActiveContainers is inconsistent!");
1566 int VolumeManager::destroyAsec(const char *id, bool force) {
1567 char asecFileName[255];
1568 char mountPoint[255];
1570 if (!isLegalAsecId(id)) {
1571 SLOGE("destroyAsec: Invalid asec id \"%s\"", id);
1576 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1577 SLOGE("Couldn't find ASEC %s", id);
1581 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
1582 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1583 SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id);
1587 if (isMountpointMounted(mountPoint)) {
1589 SLOGD("Unmounting container before destroy");
1591 if (unmountAsec(id, force)) {
1592 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
1597 if (unlink(asecFileName)) {
1598 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
1603 SLOGD("ASEC %s destroyed", id);
1609 * Legal ASEC ids consist of alphanumeric characters, '-',
1610 * '_', or '.'. ".." is not allowed. The first or last character
1611 * of the ASEC id cannot be '.' (dot).
1613 bool VolumeManager::isLegalAsecId(const char *id) const {
1615 size_t len = strlen(id);
1620 if ((id[0] == '.') || (id[len - 1] == '.')) {
1624 for (i = 0; i < len; i++) {
1626 // i=0 is guaranteed never to have a dot. See above.
1627 if (id[i-1] == '.') return false;
1630 if (id[i] == '_' || id[i] == '-') continue;
1631 if (id[i] >= 'a' && id[i] <= 'z') continue;
1632 if (id[i] >= 'A' && id[i] <= 'Z') continue;
1633 if (id[i] >= '0' && id[i] <= '9') continue;
1640 bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
1641 int dirfd = open(dir, O_DIRECTORY | O_CLOEXEC);
1643 SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
1648 bool ret = (fstatat(dirfd, asecName, &sb, AT_SYMLINK_NOFOLLOW) == 0)
1649 && S_ISREG(sb.st_mode);
1656 int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
1657 const char **directory) const {
1660 if (!isLegalAsecId(id)) {
1661 SLOGE("findAsec: Invalid asec id \"%s\"", id);
1666 if (asprintf(&asecName, "%s.asec", id) < 0) {
1667 SLOGE("Couldn't allocate string to write ASEC name");
1672 if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_INT, asecName)) {
1673 dir = VolumeManager::SEC_ASECDIR_INT;
1674 } else if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_EXT, asecName)) {
1675 dir = VolumeManager::SEC_ASECDIR_EXT;
1681 if (directory != NULL) {
1685 if (asecPath != NULL) {
1686 int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
1687 if ((written < 0) || (size_t(written) >= asecPathLen)) {
1688 SLOGE("findAsec failed for %s: couldn't construct ASEC path", id);
1698 int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid, bool readOnly) {
1699 char asecFileName[255];
1700 char mountPoint[255];
1702 if (!isLegalAsecId(id)) {
1703 SLOGE("mountAsec: Invalid asec id \"%s\"", id);
1708 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1709 SLOGE("Couldn't find ASEC %s", id);
1713 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
1714 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1715 SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id);
1719 if (isMountpointMounted(mountPoint)) {
1720 SLOGE("ASEC %s already mounted", id);
1726 if (!asecHash(id, idHash, sizeof(idHash))) {
1727 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1731 char loopDevice[255];
1732 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1736 bool cleanupDm = false;
1738 unsigned long nr_sec = 0;
1739 struct asec_superblock sb;
1741 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1746 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
1748 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
1749 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
1750 Loop::destroyByDevice(loopDevice);
1751 errno = EMEDIUMTYPE;
1754 nr_sec--; // We don't want the devmapping to extend onto our superblock
1756 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) {
1757 Loop::destroyByDevice(loopDevice);
1761 if (mkdir(mountPoint, 0000)) {
1762 if (errno != EEXIST) {
1763 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1765 Devmapper::destroy(idHash);
1767 Loop::destroyByDevice(loopDevice);
1773 * Wait for the device mapper node to be created.
1775 waitForDevMapper(dmDevice);
1778 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
1779 result = android::vold::ext4::Mount(dmDevice, mountPoint,
1780 readOnly, false, readOnly);
1782 result = android::vold::vfat::Mount(dmDevice, mountPoint,
1783 readOnly, false, readOnly, ownerUid, 0, 0222, false);
1787 SLOGE("ASEC mount failed (%s)", strerror(errno));
1789 Devmapper::destroy(idHash);
1791 Loop::destroyByDevice(loopDevice);
1795 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
1797 SLOGD("ASEC %s mounted", id);
1803 * Mounts an image file <code>img</code>.
1805 int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) {
1806 char mountPoint[255];
1809 if (!asecHash(img, idHash, sizeof(idHash))) {
1810 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
1814 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
1815 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1816 SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img);
1820 if (isMountpointMounted(mountPoint)) {
1821 SLOGE("Image %s already mounted", img);
1826 char loopDevice[255];
1827 if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug))
1831 bool cleanupDm = false;
1833 unsigned long nr_sec = 0;
1835 if ((fd = open(loopDevice, O_RDWR | O_CLOEXEC)) < 0) {
1836 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1837 Loop::destroyByDevice(loopDevice);
1841 get_blkdev_size(fd, &nr_sec);
1843 SLOGE("Failed to get loop size (%s)", strerror(errno));
1844 Loop::destroyByDevice(loopDevice);
1851 if (setupDevMapperDevice(dmDevice, sizeof(loopDevice), loopDevice, img,key, idHash, nr_sec, &cleanupDm, mDebug)) {
1852 Loop::destroyByDevice(loopDevice);
1856 if (mkdir(mountPoint, 0755)) {
1857 if (errno != EEXIST) {
1858 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1860 Devmapper::destroy(idHash);
1862 Loop::destroyByDevice(loopDevice);
1868 * Wait for the device mapper node to be created.
1870 waitForDevMapper(dmDevice);
1872 if (android::vold::vfat::Mount(dmDevice, mountPoint,
1873 true, false, true, 0, ownerGid, 0227, false)) {
1874 SLOGE("Image mount failed (%s)", strerror(errno));
1876 Devmapper::destroy(idHash);
1878 Loop::destroyByDevice(loopDevice);
1882 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
1884 SLOGD("Image %s mounted", img);
1889 int VolumeManager::listMountedObbs(SocketClient* cli) {
1890 FILE *fp = setmntent("/proc/mounts", "r");
1892 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1896 // Create a string to compare against that has a trailing slash
1897 int loopDirLen = strlen(VolumeManager::LOOPDIR);
1898 char loopDir[loopDirLen + 2];
1899 strlcpy(loopDir, VolumeManager::LOOPDIR, sizeof(loopDir));
1900 loopDir[loopDirLen++] = '/';
1901 loopDir[loopDirLen] = '\0';
1904 while ((mentry = getmntent(fp)) != NULL) {
1905 if (!strncmp(mentry->mnt_dir, loopDir, loopDirLen)) {
1906 int fd = open(mentry->mnt_fsname, O_RDONLY | O_CLOEXEC);
1908 struct loop_info64 li;
1909 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1910 cli->sendMsg(ResponseCode::AsecListResult,
1911 (const char*) li.lo_file_name, false);
1921 extern "C" int vold_unmountAll(void) {
1922 VolumeManager *vm = VolumeManager::Instance();
1923 return vm->unmountAll();
1926 bool VolumeManager::isMountpointMounted(const char *mp)
1928 FILE *fp = setmntent("/proc/mounts", "r");
1930 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1934 bool found_mp = false;
1936 while ((mentry = getmntent(fp)) != NULL) {
1937 if (strcmp(mentry->mnt_dir, mp) == 0) {
1946 int VolumeManager::mkdirs(char* path) {
1947 // Only offer to create directories for paths managed by vold
1948 if (strncmp(path, "/storage/", 9) == 0) {
1949 // fs_mkdirs() does symlink checking and relative path enforcement
1950 return fs_mkdirs(path, 0700);
1952 SLOGE("Failed to find mounted volume for %s", path);