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>
32 #include <linux/kdev_t.h>
34 #define LOG_TAG "Vold"
36 #include <openssl/md5.h>
38 #include <android-base/logging.h>
39 #include <android-base/stringprintf.h>
40 #include <cutils/fs.h>
41 #include <cutils/log.h>
43 #include <selinux/android.h>
45 #include <sysutils/NetlinkEvent.h>
47 #include <private/android_filesystem_config.h>
49 #include "Benchmark.h"
50 #include "EmulatedVolume.h"
51 #include "VolumeManager.h"
52 #include "NetlinkManager.h"
53 #include "ResponseCode.h"
58 #include "Devmapper.h"
64 #define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file"
66 #define ROUND_UP_POWER_OF_2(number, po2) (((!!((number) & ((1U << (po2)) - 1))) << (po2))\
67 + ((number) & (~((1U << (po2)) - 1))))
69 using android::base::StringPrintf;
72 * Path to external storage where *only* root can access ASEC image files
74 const char *VolumeManager::SEC_ASECDIR_EXT = "/mnt/secure/asec";
77 * Path to internal storage where *only* root can access ASEC image files
79 const char *VolumeManager::SEC_ASECDIR_INT = "/data/app-asec";
82 * Path to where secure containers are mounted
84 const char *VolumeManager::ASECDIR = "/mnt/asec";
87 * Path to where OBBs are mounted
89 const char *VolumeManager::LOOPDIR = "/mnt/obb";
91 static const char* kUserMountPath = "/mnt/user";
93 static const unsigned int kMajorBlockMmc = 179;
94 static const unsigned int kMajorBlockExperimentalMin = 240;
95 static const unsigned int kMajorBlockExperimentalMax = 254;
97 /* writes superblock at end of file or device given by name */
98 static int writeSuperBlock(const char* name, struct asec_superblock *sb, unsigned int numImgSectors) {
99 int sbfd = open(name, O_RDWR | O_CLOEXEC);
101 SLOGE("Failed to open %s for superblock write (%s)", name, strerror(errno));
105 if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
106 SLOGE("Failed to lseek for superblock (%s)", strerror(errno));
111 if (write(sbfd, sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
112 SLOGE("Failed to write superblock (%s)", strerror(errno));
120 static unsigned long adjustSectorNumExt4(unsigned long numSectors) {
121 // Ext4 started to reserve 2% or 4096 clusters, whichever is smaller for
122 // preventing costly operations or unexpected ENOSPC error.
123 // Ext4::format() uses default block size without clustering.
124 unsigned long clusterSectors = 4096 / 512;
125 unsigned long reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0);
126 numSectors += reservedSectors > (4096 * clusterSectors) ? (4096 * clusterSectors) : reservedSectors;
127 return ROUND_UP_POWER_OF_2(numSectors, 3);
130 static unsigned long adjustSectorNumFAT(unsigned long numSectors) {
134 unsigned long fatSize = (((numSectors * 4) / 512) + 1) * 2;
135 numSectors += fatSize + 2;
137 * FAT is aligned to 32 kb with 512b sectors.
139 return ROUND_UP_POWER_OF_2(numSectors, 6);
142 static int setupLoopDevice(char* buffer, size_t len, const char* asecFileName, const char* idHash, bool debug) {
143 if (Loop::lookupActive(idHash, buffer, len)) {
144 if (Loop::create(idHash, asecFileName, buffer, len)) {
145 SLOGE("ASEC loop device creation failed for %s (%s)", asecFileName, strerror(errno));
149 SLOGD("New loop device created at %s", buffer);
153 SLOGD("Found active loopback for %s at %s", asecFileName, buffer);
159 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) {
160 if (strcmp(key, "none")) {
161 if (Devmapper::lookupActive(idHash, buffer, len)) {
162 if (Devmapper::create(idHash, loopDevice, key, numImgSectors,
164 SLOGE("ASEC device mapping failed for %s (%s)", asecFileName, strerror(errno));
168 SLOGD("New devmapper instance created at %s", buffer);
172 SLOGD("Found active devmapper for %s at %s", asecFileName, buffer);
175 *createdDMDevice = true;
177 strcpy(buffer, loopDevice);
178 *createdDMDevice = false;
183 static void waitForDevMapper(const char *dmDevice) {
185 * Wait for the device mapper node to be created. Sometimes it takes a
186 * while. Wait for up to 1 second. We could also inspect incoming uevents,
187 * but that would take more effort.
191 if (!access(dmDevice, F_OK) || errno != ENOENT) {
198 VolumeManager *VolumeManager::sInstance = NULL;
200 VolumeManager *VolumeManager::Instance() {
202 sInstance = new VolumeManager();
206 VolumeManager::VolumeManager() {
208 mActiveContainers = new AsecIdCollection();
210 mUmsSharingCount = 0;
211 mSavedDirtyRatio = -1;
212 // set dirty ratio to 0 when UMS is active
216 VolumeManager::~VolumeManager() {
217 delete mActiveContainers;
220 char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
221 static const char* digits = "0123456789abcdef";
223 unsigned char sig[MD5_DIGEST_LENGTH];
225 if (buffer == NULL) {
226 SLOGE("Destination buffer is NULL");
229 } else if (id == NULL) {
230 SLOGE("Source buffer is NULL");
233 } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) {
234 SLOGE("Target hash buffer size < %d bytes (%zu)",
235 MD5_ASCII_LENGTH_PLUS_NULL, len);
240 MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig);
243 for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
244 *p++ = digits[sig[i] >> 4];
245 *p++ = digits[sig[i] & 0x0F];
252 int VolumeManager::setDebug(bool enable) {
257 int VolumeManager::start() {
258 // Always start from a clean slate by unmounting everything in
259 // directories that we own, in case we crashed.
262 // Assume that we always have an emulated volume on internal
263 // storage; the framework will decide if it should be mounted.
264 CHECK(mInternalEmulated == nullptr);
265 mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
266 new android::vold::EmulatedVolume("/data/media"));
267 mInternalEmulated->create();
272 int VolumeManager::stop() {
273 CHECK(mInternalEmulated != nullptr);
274 mInternalEmulated->destroy();
275 mInternalEmulated = nullptr;
279 void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
280 std::lock_guard<std::mutex> lock(mLock);
283 LOG(VERBOSE) << "----------------";
284 LOG(VERBOSE) << "handleBlockEvent with action " << (int) evt->getAction();
288 std::string eventPath(evt->findParam("DEVPATH")?evt->findParam("DEVPATH"):"");
289 std::string devType(evt->findParam("DEVTYPE")?evt->findParam("DEVTYPE"):"");
291 if (devType != "disk") return;
293 int major = atoi(evt->findParam("MAJOR"));
294 int minor = atoi(evt->findParam("MINOR"));
295 dev_t device = makedev(major, minor);
297 switch (evt->getAction()) {
298 case NetlinkEvent::Action::kAdd: {
299 for (const auto& source : mDiskSources) {
300 if (source->matches(eventPath)) {
301 // For now, assume that MMC and virtio-blk (the latter is
302 // emulator-specific; see Disk.cpp for details) devices are SD,
303 // and that everything else is USB
304 int flags = source->getFlags();
305 if (major == kMajorBlockMmc
306 || (android::vold::IsRunningInEmulator()
307 && major >= (int) kMajorBlockExperimentalMin
308 && major <= (int) kMajorBlockExperimentalMax)) {
309 flags |= android::vold::Disk::Flags::kSd;
311 flags |= android::vold::Disk::Flags::kUsb;
314 auto disk = new android::vold::Disk(eventPath, device,
315 source->getNickname(), flags);
317 mDisks.push_back(std::shared_ptr<android::vold::Disk>(disk));
323 case NetlinkEvent::Action::kChange: {
324 LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
325 for (const auto& disk : mDisks) {
326 if (disk->getDevice() == device) {
327 disk->readMetadata();
328 disk->readPartitions();
333 case NetlinkEvent::Action::kRemove: {
334 auto i = mDisks.begin();
335 while (i != mDisks.end()) {
336 if ((*i)->getDevice() == device) {
346 LOG(WARNING) << "Unexpected block event action " << (int) evt->getAction();
352 void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
353 mDiskSources.push_back(diskSource);
356 std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
357 for (auto disk : mDisks) {
358 if (disk->getId() == id) {
365 std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
366 if (mInternalEmulated->getId() == id) {
367 return mInternalEmulated;
369 for (const auto& disk : mDisks) {
370 auto vol = disk->findVolume(id);
371 if (vol != nullptr) {
378 void VolumeManager::listVolumes(android::vold::VolumeBase::Type type,
379 std::list<std::string>& list) {
381 for (const auto& disk : mDisks) {
382 disk->listVolumes(type, list);
386 nsecs_t VolumeManager::benchmarkPrivate(const std::string& id) {
388 if (id == "private" || id == "null") {
391 auto vol = findVolume(id);
392 if (vol != nullptr && vol->getState() == android::vold::VolumeBase::State::kMounted) {
393 path = vol->getPath();
398 LOG(WARNING) << "Failed to find volume for " << id;
402 return android::vold::BenchmarkPrivate(path);
405 int VolumeManager::forgetPartition(const std::string& partGuid) {
406 std::string normalizedGuid;
407 if (android::vold::NormalizeHex(partGuid, normalizedGuid)) {
408 LOG(WARNING) << "Invalid GUID " << partGuid;
412 std::string keyPath = android::vold::BuildKeyPath(normalizedGuid);
413 if (unlink(keyPath.c_str()) != 0) {
414 LOG(ERROR) << "Failed to unlink " << keyPath;
421 int VolumeManager::linkPrimary(userid_t userId) {
422 std::string source(mPrimary->getPath());
423 if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) {
424 source = StringPrintf("%s/%d", source.c_str(), userId);
425 fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT);
428 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
429 if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) {
430 if (errno != ENOENT) {
431 SLOGW("Failed to unlink %s: %s", target.c_str(), strerror(errno));
434 LOG(DEBUG) << "Linking " << source << " to " << target;
435 if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) {
436 SLOGW("Failed to link %s to %s: %s", source.c_str(), target.c_str(),
443 int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
444 mAddedUsers[userId] = userSerialNumber;
448 int VolumeManager::onUserRemoved(userid_t userId) {
449 mAddedUsers.erase(userId);
453 int VolumeManager::onUserStarted(userid_t userId) {
454 // Note that sometimes the system will spin up processes from Zygote
455 // before actually starting the user, so we're okay if Zygote
456 // already created this directory.
457 std::string path(StringPrintf("%s/%d", kUserMountPath, userId));
458 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
460 mStartedUsers.insert(userId);
467 int VolumeManager::onUserStopped(userid_t userId) {
468 mStartedUsers.erase(userId);
472 int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
474 for (userid_t userId : mStartedUsers) {
480 static int unmount_tree(const char* path) {
481 size_t path_len = strlen(path);
483 FILE* fp = setmntent("/proc/mounts", "r");
485 ALOGE("Error opening /proc/mounts: %s", strerror(errno));
489 // Some volumes can be stacked on each other, so force unmount in
490 // reverse order to give us the best chance of success.
491 std::list<std::string> toUnmount;
493 while ((mentry = getmntent(fp)) != NULL) {
494 if (strncmp(mentry->mnt_dir, path, path_len) == 0) {
495 toUnmount.push_front(std::string(mentry->mnt_dir));
500 for (const auto& path : toUnmount) {
501 if (umount2(path.c_str(), MNT_DETACH)) {
502 ALOGW("Failed to unmount %s: %s", path.c_str(), strerror(errno));
508 int VolumeManager::remountUid(uid_t uid, const std::string& mode) {
509 LOG(DEBUG) << "Remounting " << uid << " as mode " << mode;
513 char rootName[PATH_MAX];
514 char pidName[PATH_MAX];
520 if (!(dir = opendir("/proc"))) {
521 PLOG(ERROR) << "Failed to opendir";
525 // Figure out root namespace to compare against below
526 if (android::vold::SaneReadLinkAt(dirfd(dir), "1/ns/mnt", rootName, PATH_MAX) == -1) {
527 PLOG(ERROR) << "Failed to readlink";
532 // Poke through all running PIDs look for apps running as UID
533 while ((de = readdir(dir))) {
537 pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
541 if (fstat(pidFd, &sb) != 0) {
542 PLOG(WARNING) << "Failed to stat " << de->d_name;
545 if (sb.st_uid != uid) {
549 // Matches so far, but refuse to touch if in root namespace
550 LOG(DEBUG) << "Found matching PID " << de->d_name;
551 if (android::vold::SaneReadLinkAt(pidFd, "ns/mnt", pidName, PATH_MAX) == -1) {
552 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
555 if (!strcmp(rootName, pidName)) {
556 LOG(WARNING) << "Skipping due to root namespace";
560 // We purposefully leave the namespace open across the fork
561 nsFd = openat(pidFd, "ns/mnt", O_RDONLY);
563 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
567 if (!(child = fork())) {
568 if (setns(nsFd, CLONE_NEWNS) != 0) {
569 PLOG(ERROR) << "Failed to setns for " << de->d_name;
573 unmount_tree("/storage");
575 std::string storageSource;
576 if (mode == "default") {
577 storageSource = "/mnt/runtime/default";
578 } else if (mode == "read") {
579 storageSource = "/mnt/runtime/read";
580 } else if (mode == "write") {
581 storageSource = "/mnt/runtime/write";
583 // Sane default of no storage visible
586 if (TEMP_FAILURE_RETRY(mount(storageSource.c_str(), "/storage",
587 NULL, MS_BIND | MS_REC, NULL)) == -1) {
588 PLOG(ERROR) << "Failed to mount " << storageSource << " for "
592 if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL,
593 MS_REC | MS_SLAVE, NULL)) == -1) {
594 PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for "
599 // Mount user-specific symlink helper into place
600 userid_t user_id = multiuser_get_user_id(uid);
601 std::string userSource(StringPrintf("/mnt/user/%d", user_id));
602 if (TEMP_FAILURE_RETRY(mount(userSource.c_str(), "/storage/self",
603 NULL, MS_BIND, NULL)) == -1) {
604 PLOG(ERROR) << "Failed to mount " << userSource << " for "
613 PLOG(ERROR) << "Failed to fork";
616 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
627 int VolumeManager::reset() {
628 // Tear down all existing disks/volumes and start from a blank slate so
629 // newly connected framework hears all events.
630 mInternalEmulated->destroy();
631 mInternalEmulated->create();
632 for (const auto& disk : mDisks) {
637 mStartedUsers.clear();
641 int VolumeManager::shutdown() {
642 mInternalEmulated->destroy();
643 for (const auto& disk : mDisks) {
650 int VolumeManager::unmountAll() {
651 std::lock_guard<std::mutex> lock(mLock);
653 // First, try gracefully unmounting all known devices
654 if (mInternalEmulated != nullptr) {
655 mInternalEmulated->unmount();
657 for (const auto& disk : mDisks) {
661 // Worst case we might have some stale mounts lurking around, so
662 // force unmount those just to be safe.
663 FILE* fp = setmntent("/proc/mounts", "r");
665 SLOGE("Error opening /proc/mounts: %s", strerror(errno));
669 // Some volumes can be stacked on each other, so force unmount in
670 // reverse order to give us the best chance of success.
671 std::list<std::string> toUnmount;
673 while ((mentry = getmntent(fp)) != NULL) {
674 if (strncmp(mentry->mnt_dir, "/mnt/", 5) == 0
675 || strncmp(mentry->mnt_dir, "/storage/", 9) == 0) {
676 toUnmount.push_front(std::string(mentry->mnt_dir));
681 for (const auto& path : toUnmount) {
682 SLOGW("Tearing down stale mount %s", path.c_str());
683 android::vold::ForceUnmount(path);
689 int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
691 if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
692 SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
696 memset(mountPath, 0, mountPathLen);
697 int written = snprintf(mountPath, mountPathLen, "%s/%s", VolumeManager::LOOPDIR, idHash);
698 if ((written < 0) || (written >= mountPathLen)) {
703 if (access(mountPath, F_OK)) {
711 int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
712 char asecFileName[255];
714 if (!isLegalAsecId(id)) {
715 SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id);
720 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
721 SLOGE("Couldn't find ASEC %s", id);
725 memset(buffer, 0, maxlen);
726 if (access(asecFileName, F_OK)) {
731 int written = snprintf(buffer, maxlen, "%s/%s", VolumeManager::ASECDIR, id);
732 if ((written < 0) || (written >= maxlen)) {
733 SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id);
741 int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
742 char asecFileName[255];
744 if (!isLegalAsecId(id)) {
745 SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id);
750 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
751 SLOGE("Couldn't find ASEC %s", id);
755 memset(buffer, 0, maxlen);
756 if (access(asecFileName, F_OK)) {
761 int written = snprintf(buffer, maxlen, "%s", asecFileName);
762 if ((written < 0) || (written >= maxlen)) {
770 int VolumeManager::createAsec(const char *id, unsigned long numSectors, const char *fstype,
771 const char *key, const int ownerUid, bool isExternal) {
772 struct asec_superblock sb;
773 memset(&sb, 0, sizeof(sb));
775 if (!isLegalAsecId(id)) {
776 SLOGE("createAsec: Invalid asec id \"%s\"", id);
781 const bool wantFilesystem = strcmp(fstype, "none");
782 bool usingExt4 = false;
783 if (wantFilesystem) {
784 usingExt4 = !strcmp(fstype, "ext4");
786 sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
787 } else if (strcmp(fstype, "fat")) {
788 SLOGE("Invalid filesystem type %s", fstype);
794 sb.magic = ASEC_SB_MAGIC;
795 sb.ver = ASEC_SB_VER;
797 if (numSectors < ((1024*1024)/512)) {
798 SLOGE("Invalid container size specified (%lu sectors)", numSectors);
803 char asecFileName[255];
805 if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
806 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
807 asecFileName, strerror(errno));
812 const char *asecDir = isExternal ? VolumeManager::SEC_ASECDIR_EXT : VolumeManager::SEC_ASECDIR_INT;
814 int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
815 if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) {
820 if (!access(asecFileName, F_OK)) {
821 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
822 asecFileName, strerror(errno));
827 unsigned long numImgSectors;
829 numImgSectors = adjustSectorNumExt4(numSectors);
831 numImgSectors = adjustSectorNumFAT(numSectors);
833 // Add +1 for our superblock which is at the end
834 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
835 SLOGE("ASEC image file creation failed (%s)", strerror(errno));
840 if (!asecHash(id, idHash, sizeof(idHash))) {
841 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
842 unlink(asecFileName);
846 char loopDevice[255];
847 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
848 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
849 unlink(asecFileName);
854 bool cleanupDm = false;
856 if (strcmp(key, "none")) {
857 // XXX: This is all we support for now
858 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
859 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
861 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
862 Loop::destroyByDevice(loopDevice);
863 unlink(asecFileName);
868 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
869 strcpy(dmDevice, loopDevice);
873 * Drop down the superblock at the end of the file
875 if (writeSuperBlock(loopDevice, &sb, numImgSectors)) {
877 Devmapper::destroy(idHash);
879 Loop::destroyByDevice(loopDevice);
880 unlink(asecFileName);
884 if (wantFilesystem) {
886 char mountPoint[255];
888 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
889 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
890 SLOGE("ASEC fs format failed: couldn't construct mountPoint");
892 Devmapper::destroy(idHash);
894 Loop::destroyByDevice(loopDevice);
895 unlink(asecFileName);
900 formatStatus = android::vold::ext4::Format(dmDevice, numImgSectors, mountPoint);
902 formatStatus = android::vold::vfat::Format(dmDevice, numImgSectors);
905 if (formatStatus < 0) {
906 SLOGE("ASEC fs format failed (%s)", strerror(errno));
908 Devmapper::destroy(idHash);
910 Loop::destroyByDevice(loopDevice);
911 unlink(asecFileName);
915 if (mkdir(mountPoint, 0000)) {
916 if (errno != EEXIST) {
917 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
919 Devmapper::destroy(idHash);
921 Loop::destroyByDevice(loopDevice);
922 unlink(asecFileName);
929 mountStatus = android::vold::ext4::Mount(dmDevice, mountPoint,
930 false, false, false);
932 mountStatus = android::vold::vfat::Mount(dmDevice, mountPoint,
933 false, false, false, ownerUid, 0, 0000, false);
937 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
939 Devmapper::destroy(idHash);
941 Loop::destroyByDevice(loopDevice);
942 unlink(asecFileName);
947 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
949 if (fchown(dirfd, ownerUid, AID_SYSTEM)
950 || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
951 SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
957 SLOGI("Created raw secure container %s (no filesystem)", id);
960 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
964 int VolumeManager::resizeAsec(const char *id, unsigned long numSectors, const char *key) {
965 char asecFileName[255];
966 char mountPoint[255];
967 bool cleanupDm = false;
969 if (!isLegalAsecId(id)) {
970 SLOGE("resizeAsec: Invalid asec id \"%s\"", id);
975 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
976 SLOGE("Couldn't find ASEC %s", id);
980 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
981 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
982 SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id);
986 if (isMountpointMounted(mountPoint)) {
987 SLOGE("ASEC %s mounted. Unmount before resizing", id);
992 struct asec_superblock sb;
994 unsigned long oldNumSec = 0;
996 if ((fd = open(asecFileName, O_RDONLY | O_CLOEXEC)) < 0) {
997 SLOGE("Failed to open ASEC file (%s)", strerror(errno));
1002 if (fstat(fd, &info) < 0) {
1003 SLOGE("Failed to get file size (%s)", strerror(errno));
1008 oldNumSec = info.st_size / 512;
1011 * Try to read superblock.
1013 memset(&sb, 0, sizeof(struct asec_superblock));
1014 if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) {
1015 SLOGE("lseek failed (%s)", strerror(errno));
1019 if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
1020 SLOGE("superblock read failed (%s)", strerror(errno));
1027 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
1029 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
1030 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
1031 errno = EMEDIUMTYPE;
1035 unsigned long numImgSectors;
1036 if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) {
1037 SLOGE("Only ext4 partitions are supported for resize");
1041 numImgSectors = adjustSectorNumExt4(numSectors);
1045 * add one block for the superblock
1047 SLOGD("Resizing from %lu sectors to %lu sectors", oldNumSec, numImgSectors + 1);
1048 if (oldNumSec == numImgSectors + 1) {
1049 SLOGW("Size unchanged; ignoring resize request");
1051 } else if (oldNumSec > numImgSectors + 1) {
1052 SLOGE("Only growing is currently supported.");
1057 if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) {
1058 SLOGE("Resize of ASEC image file failed. Could not resize %s", id);
1063 * Drop down a copy of the superblock at the end of the file
1065 if (writeSuperBlock(asecFileName, &sb, numImgSectors))
1069 if (!asecHash(id, idHash, sizeof(idHash))) {
1070 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1074 char loopDevice[255];
1075 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1080 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) {
1081 Loop::destroyByDevice(loopDevice);
1086 * Wait for the device mapper node to be created.
1088 waitForDevMapper(dmDevice);
1090 if (android::vold::ext4::Resize(dmDevice, numImgSectors)) {
1091 SLOGE("Unable to resize %s (%s)", id, strerror(errno));
1093 Devmapper::destroy(idHash);
1095 Loop::destroyByDevice(loopDevice);
1101 Loop::resizeImageFile(asecFileName, oldNumSec);
1105 int VolumeManager::finalizeAsec(const char *id) {
1106 char asecFileName[255];
1107 char loopDevice[255];
1108 char mountPoint[255];
1110 if (!isLegalAsecId(id)) {
1111 SLOGE("finalizeAsec: Invalid asec id \"%s\"", id);
1116 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1117 SLOGE("Couldn't find ASEC %s", id);
1122 if (!asecHash(id, idHash, sizeof(idHash))) {
1123 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1127 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
1128 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
1132 unsigned long nr_sec = 0;
1133 struct asec_superblock sb;
1135 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1139 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
1140 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1141 SLOGE("ASEC finalize failed: couldn't construct mountPoint");
1146 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
1147 result = android::vold::ext4::Mount(loopDevice, mountPoint,
1150 result = android::vold::vfat::Mount(loopDevice, mountPoint,
1151 true, true, true, 0, 0, 0227, false);
1155 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
1160 SLOGD("ASEC %s finalized", id);
1165 int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
1166 char asecFileName[255];
1167 char loopDevice[255];
1168 char mountPoint[255];
1170 if (gid < AID_APP) {
1171 SLOGE("Group ID is not in application range");
1175 if (!isLegalAsecId(id)) {
1176 SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id);
1181 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1182 SLOGE("Couldn't find ASEC %s", id);
1187 if (!asecHash(id, idHash, sizeof(idHash))) {
1188 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1192 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
1193 SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
1197 unsigned long nr_sec = 0;
1198 struct asec_superblock sb;
1200 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1204 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
1205 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1206 SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id);
1211 if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
1215 int ret = android::vold::ext4::Mount(loopDevice, mountPoint,
1216 false /* read-only */,
1218 false /* executable */);
1220 SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
1224 char *paths[] = { mountPoint, NULL };
1226 FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
1228 // Traverse the entire hierarchy and chown to system UID.
1229 for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
1230 // We don't care about the lost+found directory.
1231 if (!strcmp(ftsent->fts_name, "lost+found")) {
1236 * There can only be one file marked as private right now.
1237 * This should be more robust, but it satisfies the requirements
1238 * we have for right now.
1240 const bool privateFile = !strcmp(ftsent->fts_name, filename);
1242 int fd = open(ftsent->fts_accpath, O_NOFOLLOW | O_CLOEXEC);
1244 SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
1249 result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
1251 if (ftsent->fts_info & FTS_D) {
1252 result |= fchmod(fd, 0755);
1253 } else if (ftsent->fts_info & FTS_F) {
1254 result |= fchmod(fd, privateFile ? 0640 : 0644);
1257 if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) {
1258 SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno));
1266 // Finally make the directory readable by everyone.
1267 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
1268 if (dirfd < 0 || fchmod(dirfd, 0755)) {
1269 SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
1277 result |= android::vold::ext4::Mount(loopDevice, mountPoint,
1278 true /* read-only */,
1280 true /* execute */);
1283 SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
1288 SLOGD("ASEC %s permissions fixed", id);
1293 int VolumeManager::renameAsec(const char *id1, const char *id2) {
1294 char asecFilename1[255];
1295 char *asecFilename2;
1296 char mountPoint[255];
1300 if (!isLegalAsecId(id1)) {
1301 SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1);
1306 if (!isLegalAsecId(id2)) {
1307 SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2);
1312 if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
1313 SLOGE("Couldn't find ASEC %s", id1);
1317 asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
1319 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id1);
1320 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1321 SLOGE("Rename failed: couldn't construct mountpoint");
1325 if (isMountpointMounted(mountPoint)) {
1326 SLOGW("Rename attempt when src mounted");
1331 written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id2);
1332 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1333 SLOGE("Rename failed: couldn't construct mountpoint2");
1337 if (isMountpointMounted(mountPoint)) {
1338 SLOGW("Rename attempt when dst mounted");
1343 if (!access(asecFilename2, F_OK)) {
1344 SLOGE("Rename attempt when dst exists");
1349 if (rename(asecFilename1, asecFilename2)) {
1350 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
1354 free(asecFilename2);
1358 free(asecFilename2);
1362 #define UNMOUNT_RETRIES 5
1363 #define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
1364 int VolumeManager::unmountAsec(const char *id, bool force) {
1365 char asecFileName[255];
1366 char mountPoint[255];
1368 if (!isLegalAsecId(id)) {
1369 SLOGE("unmountAsec: Invalid asec id \"%s\"", id);
1374 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1375 SLOGE("Couldn't find ASEC %s", id);
1379 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
1380 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1381 SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id);
1386 if (!asecHash(id, idHash, sizeof(idHash))) {
1387 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1391 return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
1394 int VolumeManager::unmountObb(const char *fileName, bool force) {
1395 char mountPoint[255];
1398 if (!asecHash(fileName, idHash, sizeof(idHash))) {
1399 SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
1403 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
1404 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1405 SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName);
1409 return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
1412 int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
1413 const char *fileName, const char *mountPoint, bool force) {
1414 if (!isMountpointMounted(mountPoint)) {
1415 SLOGE("Unmount request for %s when not mounted", id);
1421 for (i = 1; i <= UNMOUNT_RETRIES; i++) {
1422 rc = umount(mountPoint);
1426 if (rc && (errno == EINVAL || errno == ENOENT)) {
1427 SLOGI("Container %s unmounted OK", id);
1431 SLOGW("%s unmount attempt %d failed (%s)",
1432 id, i, strerror(errno));
1434 int signal = 0; // default is to just complain
1437 if (i > (UNMOUNT_RETRIES - 2))
1439 else if (i > (UNMOUNT_RETRIES - 3))
1443 Process::killProcessesWithOpenFiles(mountPoint, signal);
1444 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
1449 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
1456 if (!rmdir(mountPoint)) {
1460 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
1461 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
1465 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
1468 for (i=1; i <= UNMOUNT_RETRIES; i++) {
1469 if (Devmapper::destroy(idHash) && errno != ENXIO) {
1470 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
1471 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
1478 char loopDevice[255];
1479 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
1480 Loop::destroyByDevice(loopDevice);
1482 SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
1485 AsecIdCollection::iterator it;
1486 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
1487 ContainerData* cd = *it;
1488 if (!strcmp(cd->id, id)) {
1490 mActiveContainers->erase(it);
1494 if (it == mActiveContainers->end()) {
1495 SLOGW("mActiveContainers is inconsistent!");
1500 int VolumeManager::destroyAsec(const char *id, bool force) {
1501 char asecFileName[255];
1502 char mountPoint[255];
1504 if (!isLegalAsecId(id)) {
1505 SLOGE("destroyAsec: Invalid asec id \"%s\"", id);
1510 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1511 SLOGE("Couldn't find ASEC %s", id);
1515 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
1516 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1517 SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id);
1521 if (isMountpointMounted(mountPoint)) {
1523 SLOGD("Unmounting container before destroy");
1525 if (unmountAsec(id, force)) {
1526 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
1531 if (unlink(asecFileName)) {
1532 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
1537 SLOGD("ASEC %s destroyed", id);
1543 * Legal ASEC ids consist of alphanumeric characters, '-',
1544 * '_', or '.'. ".." is not allowed. The first or last character
1545 * of the ASEC id cannot be '.' (dot).
1547 bool VolumeManager::isLegalAsecId(const char *id) const {
1549 size_t len = strlen(id);
1554 if ((id[0] == '.') || (id[len - 1] == '.')) {
1558 for (i = 0; i < len; i++) {
1560 // i=0 is guaranteed never to have a dot. See above.
1561 if (id[i-1] == '.') return false;
1564 if (id[i] == '_' || id[i] == '-') continue;
1565 if (id[i] >= 'a' && id[i] <= 'z') continue;
1566 if (id[i] >= 'A' && id[i] <= 'Z') continue;
1567 if (id[i] >= '0' && id[i] <= '9') continue;
1574 bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
1575 int dirfd = open(dir, O_DIRECTORY | O_CLOEXEC);
1577 SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
1582 bool ret = (fstatat(dirfd, asecName, &sb, AT_SYMLINK_NOFOLLOW) == 0)
1583 && S_ISREG(sb.st_mode);
1590 int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
1591 const char **directory) const {
1594 if (!isLegalAsecId(id)) {
1595 SLOGE("findAsec: Invalid asec id \"%s\"", id);
1600 if (asprintf(&asecName, "%s.asec", id) < 0) {
1601 SLOGE("Couldn't allocate string to write ASEC name");
1606 if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_INT, asecName)) {
1607 dir = VolumeManager::SEC_ASECDIR_INT;
1608 } else if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_EXT, asecName)) {
1609 dir = VolumeManager::SEC_ASECDIR_EXT;
1615 if (directory != NULL) {
1619 if (asecPath != NULL) {
1620 int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
1621 if ((written < 0) || (size_t(written) >= asecPathLen)) {
1622 SLOGE("findAsec failed for %s: couldn't construct ASEC path", id);
1632 int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid, bool readOnly) {
1633 char asecFileName[255];
1634 char mountPoint[255];
1636 if (!isLegalAsecId(id)) {
1637 SLOGE("mountAsec: Invalid asec id \"%s\"", id);
1642 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1643 SLOGE("Couldn't find ASEC %s", id);
1647 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
1648 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1649 SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id);
1653 if (isMountpointMounted(mountPoint)) {
1654 SLOGE("ASEC %s already mounted", id);
1660 if (!asecHash(id, idHash, sizeof(idHash))) {
1661 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1665 char loopDevice[255];
1666 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1670 bool cleanupDm = false;
1672 unsigned long nr_sec = 0;
1673 struct asec_superblock sb;
1675 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1680 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
1682 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
1683 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
1684 Loop::destroyByDevice(loopDevice);
1685 errno = EMEDIUMTYPE;
1688 nr_sec--; // We don't want the devmapping to extend onto our superblock
1690 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) {
1691 Loop::destroyByDevice(loopDevice);
1695 if (mkdir(mountPoint, 0000)) {
1696 if (errno != EEXIST) {
1697 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1699 Devmapper::destroy(idHash);
1701 Loop::destroyByDevice(loopDevice);
1707 * Wait for the device mapper node to be created.
1709 waitForDevMapper(dmDevice);
1712 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
1713 result = android::vold::ext4::Mount(dmDevice, mountPoint,
1714 readOnly, false, readOnly);
1716 result = android::vold::vfat::Mount(dmDevice, mountPoint,
1717 readOnly, false, readOnly, ownerUid, 0, 0222, false);
1721 SLOGE("ASEC mount failed (%s)", strerror(errno));
1723 Devmapper::destroy(idHash);
1725 Loop::destroyByDevice(loopDevice);
1729 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
1731 SLOGD("ASEC %s mounted", id);
1737 * Mounts an image file <code>img</code>.
1739 int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) {
1740 char mountPoint[255];
1743 if (!asecHash(img, idHash, sizeof(idHash))) {
1744 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
1748 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
1749 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1750 SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img);
1754 if (isMountpointMounted(mountPoint)) {
1755 SLOGE("Image %s already mounted", img);
1760 char loopDevice[255];
1761 if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug))
1765 bool cleanupDm = false;
1767 unsigned long nr_sec = 0;
1769 if ((fd = open(loopDevice, O_RDWR | O_CLOEXEC)) < 0) {
1770 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1771 Loop::destroyByDevice(loopDevice);
1775 get_blkdev_size(fd, &nr_sec);
1777 SLOGE("Failed to get loop size (%s)", strerror(errno));
1778 Loop::destroyByDevice(loopDevice);
1785 if (setupDevMapperDevice(dmDevice, sizeof(loopDevice), loopDevice, img,key, idHash, nr_sec, &cleanupDm, mDebug)) {
1786 Loop::destroyByDevice(loopDevice);
1790 if (mkdir(mountPoint, 0755)) {
1791 if (errno != EEXIST) {
1792 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1794 Devmapper::destroy(idHash);
1796 Loop::destroyByDevice(loopDevice);
1802 * Wait for the device mapper node to be created.
1804 waitForDevMapper(dmDevice);
1806 if (android::vold::vfat::Mount(dmDevice, mountPoint,
1807 true, false, true, 0, ownerGid, 0227, false)) {
1808 SLOGE("Image mount failed (%s)", strerror(errno));
1810 Devmapper::destroy(idHash);
1812 Loop::destroyByDevice(loopDevice);
1816 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
1818 SLOGD("Image %s mounted", img);
1823 int VolumeManager::listMountedObbs(SocketClient* cli) {
1824 FILE *fp = setmntent("/proc/mounts", "r");
1826 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1830 // Create a string to compare against that has a trailing slash
1831 int loopDirLen = strlen(VolumeManager::LOOPDIR);
1832 char loopDir[loopDirLen + 2];
1833 strcpy(loopDir, VolumeManager::LOOPDIR);
1834 loopDir[loopDirLen++] = '/';
1835 loopDir[loopDirLen] = '\0';
1838 while ((mentry = getmntent(fp)) != NULL) {
1839 if (!strncmp(mentry->mnt_dir, loopDir, loopDirLen)) {
1840 int fd = open(mentry->mnt_fsname, O_RDONLY | O_CLOEXEC);
1842 struct loop_info64 li;
1843 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1844 cli->sendMsg(ResponseCode::AsecListResult,
1845 (const char*) li.lo_file_name, false);
1855 extern "C" int vold_unmountAll(void) {
1856 VolumeManager *vm = VolumeManager::Instance();
1857 return vm->unmountAll();
1860 bool VolumeManager::isMountpointMounted(const char *mp)
1862 FILE *fp = setmntent("/proc/mounts", "r");
1864 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1868 bool found_mp = false;
1870 while ((mentry = getmntent(fp)) != NULL) {
1871 if (strcmp(mentry->mnt_dir, mp) == 0) {
1880 int VolumeManager::mkdirs(char* path) {
1881 // Only offer to create directories for paths managed by vold
1882 if (strncmp(path, "/storage/", 9) == 0) {
1883 // fs_mkdirs() does symlink checking and relative path enforcement
1884 return fs_mkdirs(path, 0700);
1886 SLOGE("Failed to find mounted volume for %s", path);