San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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. |
| 15 | */ |
| 16 | |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 17 | #include <dirent.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 18 | #include <errno.h> |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 19 | #include <fcntl.h> |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 20 | #include <fts.h> |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 21 | #include <mntent.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/ioctl.h> |
| 26 | #include <sys/mount.h> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 29 | #include <unistd.h> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 30 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 31 | #include <linux/kdev_t.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 32 | |
| 33 | #define LOG_TAG "Vold" |
| 34 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 35 | #include <openssl/md5.h> |
| 36 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 37 | #include <base/logging.h> |
| 38 | #include <base/stringprintf.h> |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 39 | #include <cutils/fs.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 40 | #include <cutils/log.h> |
| 41 | |
Robert Craig | b9e3ba5 | 2014-02-04 10:53:00 -0500 | [diff] [blame] | 42 | #include <selinux/android.h> |
| 43 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 44 | #include <sysutils/NetlinkEvent.h> |
| 45 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 46 | #include <private/android_filesystem_config.h> |
| 47 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 48 | #include "EmulatedVolume.h" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 49 | #include "VolumeManager.h" |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 50 | #include "NetlinkManager.h" |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 51 | #include "ResponseCode.h" |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 52 | #include "Loop.h" |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 53 | #include "Ext4.h" |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 54 | #include "Fat.h" |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 55 | #include "Utils.h" |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 56 | #include "Devmapper.h" |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 57 | #include "Process.h" |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 58 | #include "Asec.h" |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 59 | #include "VoldUtil.h" |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 60 | #include "cryptfs.h" |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 61 | |
Mike Lockwood | 97f2fc1 | 2011-06-07 10:51:38 -0700 | [diff] [blame] | 62 | #define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file" |
| 63 | |
Daniel Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 64 | #define ROUND_UP_POWER_OF_2(number, po2) (((!!(number & ((1U << po2) - 1))) << po2)\ |
| 65 | + (number & (~((1U << po2) - 1)))) |
| 66 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 67 | using android::base::StringPrintf; |
| 68 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 69 | /* |
| 70 | * Path to external storage where *only* root can access ASEC image files |
| 71 | */ |
| 72 | const char *VolumeManager::SEC_ASECDIR_EXT = "/mnt/secure/asec"; |
| 73 | |
| 74 | /* |
| 75 | * Path to internal storage where *only* root can access ASEC image files |
| 76 | */ |
| 77 | const char *VolumeManager::SEC_ASECDIR_INT = "/data/app-asec"; |
| 78 | |
| 79 | /* |
| 80 | * Path to where secure containers are mounted |
| 81 | */ |
| 82 | const char *VolumeManager::ASECDIR = "/mnt/asec"; |
| 83 | |
| 84 | /* |
| 85 | * Path to where OBBs are mounted |
| 86 | */ |
| 87 | const char *VolumeManager::LOOPDIR = "/mnt/obb"; |
| 88 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 89 | static const char* kUserMountPath = "/mnt/user"; |
| 90 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 91 | static const unsigned int kMajorBlockMmc = 179; |
| 92 | |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 93 | /* writes superblock at end of file or device given by name */ |
| 94 | static int writeSuperBlock(const char* name, struct asec_superblock *sb, unsigned int numImgSectors) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 95 | int sbfd = open(name, O_RDWR | O_CLOEXEC); |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 96 | if (sbfd < 0) { |
| 97 | SLOGE("Failed to open %s for superblock write (%s)", name, strerror(errno)); |
| 98 | return -1; |
| 99 | } |
| 100 | |
| 101 | if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) { |
| 102 | SLOGE("Failed to lseek for superblock (%s)", strerror(errno)); |
| 103 | close(sbfd); |
| 104 | return -1; |
| 105 | } |
| 106 | |
| 107 | if (write(sbfd, sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) { |
| 108 | SLOGE("Failed to write superblock (%s)", strerror(errno)); |
| 109 | close(sbfd); |
| 110 | return -1; |
| 111 | } |
| 112 | close(sbfd); |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | static int adjustSectorNumExt4(unsigned numSectors) { |
Daniel Rosenberg | e9196fe | 2014-06-10 17:16:03 -0700 | [diff] [blame] | 117 | // Ext4 started to reserve 2% or 4096 clusters, whichever is smaller for |
| 118 | // preventing costly operations or unexpected ENOSPC error. |
| 119 | // Ext4::format() uses default block size without clustering. |
| 120 | unsigned clusterSectors = 4096 / 512; |
| 121 | unsigned reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0); |
| 122 | numSectors += reservedSectors > (4096 * clusterSectors) ? (4096 * clusterSectors) : reservedSectors; |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 123 | return ROUND_UP_POWER_OF_2(numSectors, 3); |
| 124 | } |
| 125 | |
| 126 | static int adjustSectorNumFAT(unsigned numSectors) { |
| 127 | /* |
| 128 | * Add some headroom |
| 129 | */ |
| 130 | unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2; |
| 131 | numSectors += fatSize + 2; |
| 132 | /* |
| 133 | * FAT is aligned to 32 kb with 512b sectors. |
| 134 | */ |
| 135 | return ROUND_UP_POWER_OF_2(numSectors, 6); |
| 136 | } |
| 137 | |
| 138 | static int setupLoopDevice(char* buffer, size_t len, const char* asecFileName, const char* idHash, bool debug) { |
| 139 | if (Loop::lookupActive(idHash, buffer, len)) { |
| 140 | if (Loop::create(idHash, asecFileName, buffer, len)) { |
| 141 | SLOGE("ASEC loop device creation failed for %s (%s)", asecFileName, strerror(errno)); |
| 142 | return -1; |
| 143 | } |
| 144 | if (debug) { |
| 145 | SLOGD("New loop device created at %s", buffer); |
| 146 | } |
| 147 | } else { |
| 148 | if (debug) { |
| 149 | SLOGD("Found active loopback for %s at %s", asecFileName, buffer); |
| 150 | } |
| 151 | } |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static int setupDevMapperDevice(char* buffer, size_t len, const char* loopDevice, const char* asecFileName, const char* key, const char* idHash , int numImgSectors, bool* createdDMDevice, bool debug) { |
| 156 | if (strcmp(key, "none")) { |
| 157 | if (Devmapper::lookupActive(idHash, buffer, len)) { |
| 158 | if (Devmapper::create(idHash, loopDevice, key, numImgSectors, |
| 159 | buffer, len)) { |
| 160 | SLOGE("ASEC device mapping failed for %s (%s)", asecFileName, strerror(errno)); |
| 161 | return -1; |
| 162 | } |
| 163 | if (debug) { |
| 164 | SLOGD("New devmapper instance created at %s", buffer); |
| 165 | } |
| 166 | } else { |
| 167 | if (debug) { |
| 168 | SLOGD("Found active devmapper for %s at %s", asecFileName, buffer); |
| 169 | } |
| 170 | } |
| 171 | *createdDMDevice = true; |
| 172 | } else { |
| 173 | strcpy(buffer, loopDevice); |
| 174 | *createdDMDevice = false; |
| 175 | } |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static void waitForDevMapper(const char *dmDevice) { |
| 180 | /* |
| 181 | * Wait for the device mapper node to be created. Sometimes it takes a |
| 182 | * while. Wait for up to 1 second. We could also inspect incoming uevents, |
| 183 | * but that would take more effort. |
| 184 | */ |
| 185 | int tries = 25; |
| 186 | while (tries--) { |
| 187 | if (!access(dmDevice, F_OK) || errno != ENOENT) { |
| 188 | break; |
| 189 | } |
| 190 | usleep(40 * 1000); |
| 191 | } |
| 192 | } |
| 193 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 194 | VolumeManager *VolumeManager::sInstance = NULL; |
| 195 | |
| 196 | VolumeManager *VolumeManager::Instance() { |
| 197 | if (!sInstance) |
| 198 | sInstance = new VolumeManager(); |
| 199 | return sInstance; |
| 200 | } |
| 201 | |
| 202 | VolumeManager::VolumeManager() { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 203 | mDebug = false; |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 204 | mActiveContainers = new AsecIdCollection(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 205 | mBroadcaster = NULL; |
Mike Lockwood | a28056b | 2010-10-28 15:21:24 -0400 | [diff] [blame] | 206 | mUmsSharingCount = 0; |
| 207 | mSavedDirtyRatio = -1; |
| 208 | // set dirty ratio to 0 when UMS is active |
| 209 | mUmsDirtyRatio = 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | VolumeManager::~VolumeManager() { |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 213 | delete mActiveContainers; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 216 | char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) { |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 217 | static const char* digits = "0123456789abcdef"; |
| 218 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 219 | unsigned char sig[MD5_DIGEST_LENGTH]; |
| 220 | |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 221 | if (buffer == NULL) { |
| 222 | SLOGE("Destination buffer is NULL"); |
| 223 | errno = ESPIPE; |
| 224 | return NULL; |
| 225 | } else if (id == NULL) { |
| 226 | SLOGE("Source buffer is NULL"); |
| 227 | errno = ESPIPE; |
| 228 | return NULL; |
| 229 | } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) { |
Colin Cross | 59846b6 | 2014-02-06 20:34:29 -0800 | [diff] [blame] | 230 | SLOGE("Target hash buffer size < %d bytes (%zu)", |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 231 | MD5_ASCII_LENGTH_PLUS_NULL, len); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 232 | errno = ESPIPE; |
| 233 | return NULL; |
| 234 | } |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 235 | |
| 236 | MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 237 | |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 238 | char *p = buffer; |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 239 | for (int i = 0; i < MD5_DIGEST_LENGTH; i++) { |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 240 | *p++ = digits[sig[i] >> 4]; |
| 241 | *p++ = digits[sig[i] & 0x0F]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 242 | } |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 243 | *p = '\0'; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 244 | |
| 245 | return buffer; |
| 246 | } |
| 247 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 248 | int VolumeManager::setDebug(bool enable) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 249 | mDebug = enable; |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 250 | return 0; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 251 | } |
| 252 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 253 | int VolumeManager::start() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 254 | // Always start from a clean slate by unmounting everything in |
| 255 | // directories that we own, in case we crashed. |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 256 | unmountAll(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 257 | |
| 258 | // Assume that we always have an emulated volume on internal |
| 259 | // storage; the framework will decide if it should be mounted. |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 260 | CHECK(mInternalEmulated == nullptr); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 261 | mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>( |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 262 | new android::vold::EmulatedVolume("/data/media")); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 263 | mInternalEmulated->create(); |
| 264 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | int VolumeManager::stop() { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 269 | CHECK(mInternalEmulated != nullptr); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 270 | mInternalEmulated->destroy(); |
| 271 | mInternalEmulated = nullptr; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 272 | return 0; |
| 273 | } |
| 274 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 275 | void VolumeManager::handleBlockEvent(NetlinkEvent *evt) { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 276 | std::lock_guard<std::mutex> lock(mLock); |
| 277 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 278 | if (mDebug) { |
| 279 | LOG(VERBOSE) << "----------------"; |
| 280 | LOG(VERBOSE) << "handleBlockEvent with action " << (int) evt->getAction(); |
| 281 | evt->dump(); |
| 282 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 283 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 284 | std::string eventPath(evt->findParam("DEVPATH")); |
| 285 | std::string devType(evt->findParam("DEVTYPE")); |
| 286 | |
| 287 | if (devType != "disk") return; |
| 288 | |
| 289 | int major = atoi(evt->findParam("MAJOR")); |
| 290 | int minor = atoi(evt->findParam("MINOR")); |
| 291 | dev_t device = makedev(major, minor); |
| 292 | |
| 293 | switch (evt->getAction()) { |
| 294 | case NetlinkEvent::Action::kAdd: { |
| 295 | for (auto source : mDiskSources) { |
| 296 | if (source->matches(eventPath)) { |
| 297 | // For now, assume that MMC devices are SD, and that |
| 298 | // everything else is USB |
| 299 | int flags = source->getFlags(); |
| 300 | if (major == kMajorBlockMmc) { |
| 301 | flags |= android::vold::Disk::Flags::kSd; |
| 302 | } else { |
| 303 | flags |= android::vold::Disk::Flags::kUsb; |
| 304 | } |
| 305 | |
| 306 | auto disk = new android::vold::Disk(eventPath, device, |
| 307 | source->getNickname(), flags); |
| 308 | disk->create(); |
| 309 | mDisks.push_back(std::shared_ptr<android::vold::Disk>(disk)); |
| 310 | break; |
| 311 | } |
| 312 | } |
| 313 | break; |
| 314 | } |
| 315 | case NetlinkEvent::Action::kChange: { |
Jeff Sharkey | 7d9d011 | 2015-04-14 23:14:23 -0700 | [diff] [blame] | 316 | LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed"; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 317 | for (auto disk : mDisks) { |
| 318 | if (disk->getDevice() == device) { |
| 319 | disk->readMetadata(); |
| 320 | disk->readPartitions(); |
| 321 | } |
| 322 | } |
| 323 | break; |
| 324 | } |
| 325 | case NetlinkEvent::Action::kRemove: { |
| 326 | auto i = mDisks.begin(); |
| 327 | while (i != mDisks.end()) { |
| 328 | if ((*i)->getDevice() == device) { |
| 329 | (*i)->destroy(); |
| 330 | i = mDisks.erase(i); |
| 331 | } else { |
| 332 | ++i; |
| 333 | } |
| 334 | } |
| 335 | break; |
| 336 | } |
| 337 | default: { |
| 338 | LOG(WARNING) << "Unexpected block event action " << (int) evt->getAction(); |
| 339 | break; |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) { |
| 345 | mDiskSources.push_back(diskSource); |
| 346 | } |
| 347 | |
| 348 | std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) { |
| 349 | for (auto disk : mDisks) { |
| 350 | if (disk->getId() == id) { |
| 351 | return disk; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 352 | } |
| 353 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 354 | return nullptr; |
| 355 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 356 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 357 | std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) { |
| 358 | if (mInternalEmulated->getId() == id) { |
| 359 | return mInternalEmulated; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 360 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 361 | for (auto disk : mDisks) { |
| 362 | auto vol = disk->findVolume(id); |
| 363 | if (vol != nullptr) { |
| 364 | return vol; |
| 365 | } |
| 366 | } |
| 367 | return nullptr; |
| 368 | } |
| 369 | |
| 370 | int VolumeManager::linkPrimary(userid_t userId) { |
| 371 | std::string source(mPrimary->getPath()); |
| 372 | if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) { |
| 373 | source = StringPrintf("%s/%d", source.c_str(), userId); |
| 374 | } |
| 375 | |
| 376 | std::string target(StringPrintf("/mnt/user/%d/primary", userId)); |
| 377 | if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) { |
| 378 | if (errno != ENOENT) { |
| 379 | SLOGW("Failed to unlink %s: %s", target.c_str(), strerror(errno)); |
| 380 | } |
| 381 | } |
Jeff Sharkey | 1bfb375 | 2015-04-29 15:22:23 -0700 | [diff] [blame^] | 382 | LOG(DEBUG) << "Linking " << source << " to " << target; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 383 | if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) { |
| 384 | SLOGW("Failed to link %s to %s: %s", source.c_str(), target.c_str(), |
| 385 | strerror(errno)); |
| 386 | return -errno; |
| 387 | } |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | int VolumeManager::startUser(userid_t userId) { |
| 392 | // Note that sometimes the system will spin up processes from Zygote |
| 393 | // before actually starting the user, so we're okay if Zygote |
| 394 | // already created this directory. |
| 395 | std::string path(StringPrintf("%s/%d", kUserMountPath, userId)); |
| 396 | fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT); |
| 397 | |
| 398 | mUsers.push_back(userId); |
| 399 | if (mPrimary) { |
| 400 | linkPrimary(userId); |
| 401 | } |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | int VolumeManager::cleanupUser(userid_t userId) { |
| 406 | mUsers.remove(userId); |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) { |
| 411 | mPrimary = vol; |
| 412 | for (userid_t userId : mUsers) { |
| 413 | linkPrimary(userId); |
| 414 | } |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | int VolumeManager::reset() { |
| 419 | // Tear down all existing disks/volumes and start from a blank slate so |
| 420 | // newly connected framework hears all events. |
| 421 | mInternalEmulated->destroy(); |
| 422 | mInternalEmulated->create(); |
| 423 | for (auto disk : mDisks) { |
| 424 | disk->destroy(); |
| 425 | disk->create(); |
| 426 | } |
| 427 | mUsers.clear(); |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | int VolumeManager::shutdown() { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 432 | mInternalEmulated->destroy(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 433 | for (auto disk : mDisks) { |
| 434 | disk->destroy(); |
| 435 | } |
| 436 | mDisks.clear(); |
| 437 | return 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 440 | int VolumeManager::unmountAll() { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 441 | std::lock_guard<std::mutex> lock(mLock); |
| 442 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 443 | // First, try gracefully unmounting all known devices |
| 444 | if (mInternalEmulated != nullptr) { |
| 445 | mInternalEmulated->unmount(); |
| 446 | } |
| 447 | for (auto disk : mDisks) { |
| 448 | disk->unmountAll(); |
| 449 | } |
| 450 | |
| 451 | // Worst case we might have some stale mounts lurking around, so |
| 452 | // force unmount those just to be safe. |
| 453 | FILE* fp = setmntent("/proc/mounts", "r"); |
| 454 | if (fp == NULL) { |
| 455 | SLOGE("Error opening /proc/mounts: %s", strerror(errno)); |
| 456 | return -errno; |
| 457 | } |
| 458 | |
| 459 | // Some volumes can be stacked on each other, so force unmount in |
| 460 | // reverse order to give us the best chance of success. |
| 461 | std::list<std::string> toUnmount; |
| 462 | mntent* mentry; |
| 463 | while ((mentry = getmntent(fp)) != NULL) { |
| 464 | if (strncmp(mentry->mnt_dir, "/mnt/", 5) == 0 |
| 465 | || strncmp(mentry->mnt_dir, "/storage/", 9) == 0) { |
| 466 | toUnmount.push_front(std::string(mentry->mnt_dir)); |
| 467 | } |
| 468 | } |
| 469 | endmntent(fp); |
| 470 | |
| 471 | for (auto path : toUnmount) { |
| 472 | SLOGW("Tearing down stale mount %s", path.c_str()); |
| 473 | android::vold::ForceUnmount(path); |
| 474 | } |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 479 | int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) { |
| 480 | char idHash[33]; |
| 481 | if (!asecHash(sourceFile, idHash, sizeof(idHash))) { |
| 482 | SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno)); |
| 483 | return -1; |
| 484 | } |
| 485 | |
| 486 | memset(mountPath, 0, mountPathLen); |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 487 | int written = snprintf(mountPath, mountPathLen, "%s/%s", VolumeManager::LOOPDIR, idHash); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 488 | if ((written < 0) || (written >= mountPathLen)) { |
| 489 | errno = EINVAL; |
| 490 | return -1; |
| 491 | } |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 492 | |
| 493 | if (access(mountPath, F_OK)) { |
| 494 | errno = ENOENT; |
| 495 | return -1; |
| 496 | } |
| 497 | |
| 498 | return 0; |
| 499 | } |
| 500 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 501 | int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) { |
San Mehat | 88ac2c0 | 2010-03-23 11:15:58 -0700 | [diff] [blame] | 502 | char asecFileName[255]; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 503 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 504 | if (!isLegalAsecId(id)) { |
| 505 | SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id); |
| 506 | errno = EINVAL; |
| 507 | return -1; |
| 508 | } |
| 509 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 510 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 511 | SLOGE("Couldn't find ASEC %s", id); |
| 512 | return -1; |
| 513 | } |
San Mehat | 88ac2c0 | 2010-03-23 11:15:58 -0700 | [diff] [blame] | 514 | |
| 515 | memset(buffer, 0, maxlen); |
| 516 | if (access(asecFileName, F_OK)) { |
| 517 | errno = ENOENT; |
| 518 | return -1; |
| 519 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 520 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 521 | int written = snprintf(buffer, maxlen, "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 522 | if ((written < 0) || (written >= maxlen)) { |
| 523 | SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id); |
| 524 | errno = EINVAL; |
| 525 | return -1; |
| 526 | } |
| 527 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 528 | return 0; |
| 529 | } |
| 530 | |
Dianne Hackborn | 736910c | 2011-06-27 13:37:07 -0700 | [diff] [blame] | 531 | int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) { |
| 532 | char asecFileName[255]; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 533 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 534 | if (!isLegalAsecId(id)) { |
| 535 | SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id); |
| 536 | errno = EINVAL; |
| 537 | return -1; |
| 538 | } |
| 539 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 540 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 541 | SLOGE("Couldn't find ASEC %s", id); |
| 542 | return -1; |
| 543 | } |
Dianne Hackborn | 736910c | 2011-06-27 13:37:07 -0700 | [diff] [blame] | 544 | |
| 545 | memset(buffer, 0, maxlen); |
| 546 | if (access(asecFileName, F_OK)) { |
| 547 | errno = ENOENT; |
| 548 | return -1; |
| 549 | } |
| 550 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 551 | int written = snprintf(buffer, maxlen, "%s", asecFileName); |
| 552 | if ((written < 0) || (written >= maxlen)) { |
| 553 | errno = EINVAL; |
| 554 | return -1; |
| 555 | } |
| 556 | |
Dianne Hackborn | 736910c | 2011-06-27 13:37:07 -0700 | [diff] [blame] | 557 | return 0; |
| 558 | } |
| 559 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 560 | int VolumeManager::createAsec(const char *id, unsigned int numSectors, const char *fstype, |
| 561 | const char *key, const int ownerUid, bool isExternal) { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 562 | struct asec_superblock sb; |
| 563 | memset(&sb, 0, sizeof(sb)); |
| 564 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 565 | if (!isLegalAsecId(id)) { |
| 566 | SLOGE("createAsec: Invalid asec id \"%s\"", id); |
| 567 | errno = EINVAL; |
| 568 | return -1; |
| 569 | } |
| 570 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 571 | const bool wantFilesystem = strcmp(fstype, "none"); |
| 572 | bool usingExt4 = false; |
| 573 | if (wantFilesystem) { |
| 574 | usingExt4 = !strcmp(fstype, "ext4"); |
| 575 | if (usingExt4) { |
| 576 | sb.c_opts |= ASEC_SB_C_OPTS_EXT4; |
| 577 | } else if (strcmp(fstype, "fat")) { |
| 578 | SLOGE("Invalid filesystem type %s", fstype); |
| 579 | errno = EINVAL; |
| 580 | return -1; |
| 581 | } |
| 582 | } |
| 583 | |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 584 | sb.magic = ASEC_SB_MAGIC; |
| 585 | sb.ver = ASEC_SB_VER; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 586 | |
San Mehat | d31e380 | 2010-02-18 08:37:45 -0800 | [diff] [blame] | 587 | if (numSectors < ((1024*1024)/512)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 588 | SLOGE("Invalid container size specified (%d sectors)", numSectors); |
San Mehat | d31e380 | 2010-02-18 08:37:45 -0800 | [diff] [blame] | 589 | errno = EINVAL; |
| 590 | return -1; |
| 591 | } |
| 592 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 593 | char asecFileName[255]; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 594 | |
| 595 | if (!findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 596 | SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)", |
| 597 | asecFileName, strerror(errno)); |
| 598 | errno = EADDRINUSE; |
| 599 | return -1; |
| 600 | } |
| 601 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 602 | const char *asecDir = isExternal ? VolumeManager::SEC_ASECDIR_EXT : VolumeManager::SEC_ASECDIR_INT; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 603 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 604 | int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id); |
| 605 | if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) { |
| 606 | errno = EINVAL; |
| 607 | return -1; |
| 608 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 609 | |
| 610 | if (!access(asecFileName, F_OK)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 611 | SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)", |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 612 | asecFileName, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 613 | errno = EADDRINUSE; |
| 614 | return -1; |
| 615 | } |
| 616 | |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 617 | unsigned numImgSectors; |
Daniel Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 618 | if (usingExt4) |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 619 | numImgSectors = adjustSectorNumExt4(numSectors); |
Daniel Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 620 | else |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 621 | numImgSectors = adjustSectorNumFAT(numSectors); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 622 | |
| 623 | // Add +1 for our superblock which is at the end |
| 624 | if (Loop::createImageFile(asecFileName, numImgSectors + 1)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 625 | SLOGE("ASEC image file creation failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 626 | return -1; |
| 627 | } |
| 628 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 629 | char idHash[33]; |
| 630 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 631 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 632 | unlink(asecFileName); |
| 633 | return -1; |
| 634 | } |
| 635 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 636 | char loopDevice[255]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 637 | if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 638 | SLOGE("ASEC loop device creation failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 639 | unlink(asecFileName); |
| 640 | return -1; |
| 641 | } |
| 642 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 643 | char dmDevice[255]; |
| 644 | bool cleanupDm = false; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 645 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 646 | if (strcmp(key, "none")) { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 647 | // XXX: This is all we support for now |
| 648 | sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 649 | if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice, |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 650 | sizeof(dmDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 651 | SLOGE("ASEC device mapping failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 652 | Loop::destroyByDevice(loopDevice); |
| 653 | unlink(asecFileName); |
| 654 | return -1; |
| 655 | } |
| 656 | cleanupDm = true; |
| 657 | } else { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 658 | sb.c_cipher = ASEC_SB_C_CIPHER_NONE; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 659 | strcpy(dmDevice, loopDevice); |
| 660 | } |
| 661 | |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 662 | /* |
| 663 | * Drop down the superblock at the end of the file |
| 664 | */ |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 665 | if (writeSuperBlock(loopDevice, &sb, numImgSectors)) { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 666 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 667 | Devmapper::destroy(idHash); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 668 | } |
| 669 | Loop::destroyByDevice(loopDevice); |
| 670 | unlink(asecFileName); |
| 671 | return -1; |
| 672 | } |
| 673 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 674 | if (wantFilesystem) { |
| 675 | int formatStatus; |
rpcraig | a54e13a | 2012-09-21 14:17:08 -0400 | [diff] [blame] | 676 | char mountPoint[255]; |
| 677 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 678 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 679 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 680 | SLOGE("ASEC fs format failed: couldn't construct mountPoint"); |
| 681 | if (cleanupDm) { |
| 682 | Devmapper::destroy(idHash); |
| 683 | } |
| 684 | Loop::destroyByDevice(loopDevice); |
| 685 | unlink(asecFileName); |
| 686 | return -1; |
| 687 | } |
rpcraig | a54e13a | 2012-09-21 14:17:08 -0400 | [diff] [blame] | 688 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 689 | if (usingExt4) { |
Daniel Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 690 | formatStatus = Ext4::format(dmDevice, numImgSectors, mountPoint); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 691 | } else { |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 692 | formatStatus = Fat::format(dmDevice, numImgSectors, 0); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 693 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 694 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 695 | if (formatStatus < 0) { |
| 696 | SLOGE("ASEC fs format failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 697 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 698 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 699 | } |
San Mehat | eb13a90 | 2010-01-07 12:12:50 -0800 | [diff] [blame] | 700 | Loop::destroyByDevice(loopDevice); |
| 701 | unlink(asecFileName); |
| 702 | return -1; |
| 703 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 704 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 705 | if (mkdir(mountPoint, 0000)) { |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 706 | if (errno != EEXIST) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 707 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 708 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 709 | Devmapper::destroy(idHash); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 710 | } |
| 711 | Loop::destroyByDevice(loopDevice); |
| 712 | unlink(asecFileName); |
| 713 | return -1; |
| 714 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 715 | } |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 716 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 717 | int mountStatus; |
| 718 | if (usingExt4) { |
| 719 | mountStatus = Ext4::doMount(dmDevice, mountPoint, false, false, false); |
| 720 | } else { |
| 721 | mountStatus = Fat::doMount(dmDevice, mountPoint, false, false, false, ownerUid, 0, 0000, |
| 722 | false); |
| 723 | } |
| 724 | |
| 725 | if (mountStatus) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 726 | SLOGE("ASEC FAT mount failed (%s)", strerror(errno)); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 727 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 728 | Devmapper::destroy(idHash); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 729 | } |
| 730 | Loop::destroyByDevice(loopDevice); |
| 731 | unlink(asecFileName); |
| 732 | return -1; |
| 733 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 734 | |
| 735 | if (usingExt4) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 736 | int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 737 | if (dirfd >= 0) { |
| 738 | if (fchown(dirfd, ownerUid, AID_SYSTEM) |
| 739 | || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) { |
| 740 | SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint); |
| 741 | } |
| 742 | close(dirfd); |
| 743 | } |
| 744 | } |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 745 | } else { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 746 | SLOGI("Created raw secure container %s (no filesystem)", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 747 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 748 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 749 | mActiveContainers->push_back(new ContainerData(strdup(id), ASEC)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 750 | return 0; |
| 751 | } |
| 752 | |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 753 | int VolumeManager::resizeAsec(const char *id, unsigned numSectors, const char *key) { |
| 754 | char asecFileName[255]; |
| 755 | char mountPoint[255]; |
| 756 | bool cleanupDm = false; |
| 757 | |
| 758 | if (!isLegalAsecId(id)) { |
| 759 | SLOGE("resizeAsec: Invalid asec id \"%s\"", id); |
| 760 | errno = EINVAL; |
| 761 | return -1; |
| 762 | } |
| 763 | |
| 764 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 765 | SLOGE("Couldn't find ASEC %s", id); |
| 766 | return -1; |
| 767 | } |
| 768 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 769 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 770 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 771 | SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id); |
| 772 | return -1; |
| 773 | } |
| 774 | |
| 775 | if (isMountpointMounted(mountPoint)) { |
| 776 | SLOGE("ASEC %s mounted. Unmount before resizing", id); |
| 777 | errno = EBUSY; |
| 778 | return -1; |
| 779 | } |
| 780 | |
| 781 | struct asec_superblock sb; |
| 782 | int fd; |
| 783 | unsigned int oldNumSec = 0; |
| 784 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 785 | if ((fd = open(asecFileName, O_RDONLY | O_CLOEXEC)) < 0) { |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 786 | SLOGE("Failed to open ASEC file (%s)", strerror(errno)); |
| 787 | return -1; |
| 788 | } |
| 789 | |
| 790 | struct stat info; |
| 791 | if (fstat(fd, &info) < 0) { |
| 792 | SLOGE("Failed to get file size (%s)", strerror(errno)); |
| 793 | close(fd); |
| 794 | return -1; |
| 795 | } |
| 796 | |
| 797 | oldNumSec = info.st_size / 512; |
| 798 | |
| 799 | unsigned numImgSectors; |
| 800 | if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) |
| 801 | numImgSectors = adjustSectorNumExt4(numSectors); |
| 802 | else |
| 803 | numImgSectors = adjustSectorNumFAT(numSectors); |
| 804 | /* |
| 805 | * add one block for the superblock |
| 806 | */ |
| 807 | SLOGD("Resizing from %d sectors to %d sectors", oldNumSec, numImgSectors + 1); |
Jeff Sharkey | 43ed123 | 2014-08-22 12:29:05 -0700 | [diff] [blame] | 808 | if (oldNumSec == numImgSectors + 1) { |
| 809 | SLOGW("Size unchanged; ignoring resize request"); |
| 810 | return 0; |
| 811 | } else if (oldNumSec > numImgSectors + 1) { |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 812 | SLOGE("Only growing is currently supported."); |
| 813 | close(fd); |
| 814 | return -1; |
| 815 | } |
| 816 | |
| 817 | /* |
| 818 | * Try to read superblock. |
| 819 | */ |
| 820 | memset(&sb, 0, sizeof(struct asec_superblock)); |
| 821 | if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) { |
| 822 | SLOGE("lseek failed (%s)", strerror(errno)); |
| 823 | close(fd); |
| 824 | return -1; |
| 825 | } |
| 826 | if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) { |
| 827 | SLOGE("superblock read failed (%s)", strerror(errno)); |
| 828 | close(fd); |
| 829 | return -1; |
| 830 | } |
| 831 | close(fd); |
| 832 | |
| 833 | if (mDebug) { |
| 834 | SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver); |
| 835 | } |
| 836 | if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) { |
| 837 | SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver); |
| 838 | errno = EMEDIUMTYPE; |
| 839 | return -1; |
| 840 | } |
| 841 | |
| 842 | if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) { |
| 843 | SLOGE("Only ext4 partitions are supported for resize"); |
| 844 | errno = EINVAL; |
| 845 | return -1; |
| 846 | } |
| 847 | |
| 848 | if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) { |
| 849 | SLOGE("Resize of ASEC image file failed. Could not resize %s", id); |
| 850 | return -1; |
| 851 | } |
| 852 | |
| 853 | /* |
| 854 | * Drop down a copy of the superblock at the end of the file |
| 855 | */ |
| 856 | if (writeSuperBlock(asecFileName, &sb, numImgSectors)) |
| 857 | goto fail; |
| 858 | |
| 859 | char idHash[33]; |
| 860 | if (!asecHash(id, idHash, sizeof(idHash))) { |
| 861 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
| 862 | goto fail; |
| 863 | } |
| 864 | |
| 865 | char loopDevice[255]; |
| 866 | if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug)) |
| 867 | goto fail; |
| 868 | |
| 869 | char dmDevice[255]; |
| 870 | |
| 871 | if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) { |
| 872 | Loop::destroyByDevice(loopDevice); |
| 873 | goto fail; |
| 874 | } |
| 875 | |
| 876 | /* |
| 877 | * Wait for the device mapper node to be created. |
| 878 | */ |
| 879 | waitForDevMapper(dmDevice); |
| 880 | |
| 881 | if (Ext4::resize(dmDevice, numImgSectors)) { |
| 882 | SLOGE("Unable to resize %s (%s)", id, strerror(errno)); |
| 883 | if (cleanupDm) { |
| 884 | Devmapper::destroy(idHash); |
| 885 | } |
| 886 | Loop::destroyByDevice(loopDevice); |
| 887 | goto fail; |
| 888 | } |
| 889 | |
| 890 | return 0; |
| 891 | fail: |
| 892 | Loop::resizeImageFile(asecFileName, oldNumSec); |
| 893 | return -1; |
| 894 | } |
| 895 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 896 | int VolumeManager::finalizeAsec(const char *id) { |
| 897 | char asecFileName[255]; |
| 898 | char loopDevice[255]; |
| 899 | char mountPoint[255]; |
| 900 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 901 | if (!isLegalAsecId(id)) { |
| 902 | SLOGE("finalizeAsec: Invalid asec id \"%s\"", id); |
| 903 | errno = EINVAL; |
| 904 | return -1; |
| 905 | } |
| 906 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 907 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 908 | SLOGE("Couldn't find ASEC %s", id); |
| 909 | return -1; |
| 910 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 911 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 912 | char idHash[33]; |
| 913 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 914 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 915 | return -1; |
| 916 | } |
| 917 | |
| 918 | if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 919 | SLOGE("Unable to finalize %s (%s)", id, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 920 | return -1; |
| 921 | } |
| 922 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 923 | unsigned long nr_sec = 0; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 924 | struct asec_superblock sb; |
| 925 | |
| 926 | if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) { |
| 927 | return -1; |
| 928 | } |
| 929 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 930 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 931 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 932 | SLOGE("ASEC finalize failed: couldn't construct mountPoint"); |
| 933 | return -1; |
| 934 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 935 | |
| 936 | int result = 0; |
| 937 | if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) { |
| 938 | result = Ext4::doMount(loopDevice, mountPoint, true, true, true); |
| 939 | } else { |
| 940 | result = Fat::doMount(loopDevice, mountPoint, true, true, true, 0, 0, 0227, false); |
| 941 | } |
| 942 | |
| 943 | if (result) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 944 | SLOGE("ASEC finalize mount failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 945 | return -1; |
| 946 | } |
| 947 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 948 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 949 | SLOGD("ASEC %s finalized", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 950 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 951 | return 0; |
| 952 | } |
| 953 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 954 | int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) { |
| 955 | char asecFileName[255]; |
| 956 | char loopDevice[255]; |
| 957 | char mountPoint[255]; |
| 958 | |
| 959 | if (gid < AID_APP) { |
| 960 | SLOGE("Group ID is not in application range"); |
| 961 | return -1; |
| 962 | } |
| 963 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 964 | if (!isLegalAsecId(id)) { |
| 965 | SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id); |
| 966 | errno = EINVAL; |
| 967 | return -1; |
| 968 | } |
| 969 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 970 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 971 | SLOGE("Couldn't find ASEC %s", id); |
| 972 | return -1; |
| 973 | } |
| 974 | |
| 975 | char idHash[33]; |
| 976 | if (!asecHash(id, idHash, sizeof(idHash))) { |
| 977 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
| 978 | return -1; |
| 979 | } |
| 980 | |
| 981 | if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
| 982 | SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno)); |
| 983 | return -1; |
| 984 | } |
| 985 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 986 | unsigned long nr_sec = 0; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 987 | struct asec_superblock sb; |
| 988 | |
| 989 | if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) { |
| 990 | return -1; |
| 991 | } |
| 992 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 993 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 994 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 995 | SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id); |
| 996 | return -1; |
| 997 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 998 | |
| 999 | int result = 0; |
| 1000 | if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) { |
| 1001 | return 0; |
| 1002 | } |
| 1003 | |
| 1004 | int ret = Ext4::doMount(loopDevice, mountPoint, |
| 1005 | false /* read-only */, |
| 1006 | true /* remount */, |
| 1007 | false /* executable */); |
| 1008 | if (ret) { |
| 1009 | SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno)); |
| 1010 | return -1; |
| 1011 | } |
| 1012 | |
| 1013 | char *paths[] = { mountPoint, NULL }; |
| 1014 | |
| 1015 | FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL); |
| 1016 | if (fts) { |
| 1017 | // Traverse the entire hierarchy and chown to system UID. |
| 1018 | for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) { |
| 1019 | // We don't care about the lost+found directory. |
| 1020 | if (!strcmp(ftsent->fts_name, "lost+found")) { |
| 1021 | continue; |
| 1022 | } |
| 1023 | |
| 1024 | /* |
| 1025 | * There can only be one file marked as private right now. |
| 1026 | * This should be more robust, but it satisfies the requirements |
| 1027 | * we have for right now. |
| 1028 | */ |
| 1029 | const bool privateFile = !strcmp(ftsent->fts_name, filename); |
| 1030 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 1031 | int fd = open(ftsent->fts_accpath, O_NOFOLLOW | O_CLOEXEC); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1032 | if (fd < 0) { |
| 1033 | SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno)); |
| 1034 | result = -1; |
| 1035 | continue; |
| 1036 | } |
| 1037 | |
| 1038 | result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM); |
| 1039 | |
| 1040 | if (ftsent->fts_info & FTS_D) { |
Kenny Root | 1a673c8 | 2012-05-10 16:45:29 -0700 | [diff] [blame] | 1041 | result |= fchmod(fd, 0755); |
Kenny Root | 348c8ab | 2012-05-10 15:39:53 -0700 | [diff] [blame] | 1042 | } else if (ftsent->fts_info & FTS_F) { |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1043 | result |= fchmod(fd, privateFile ? 0640 : 0644); |
| 1044 | } |
Robert Craig | b9e3ba5 | 2014-02-04 10:53:00 -0500 | [diff] [blame] | 1045 | |
Stephen Smalley | 5093e61 | 2014-02-12 09:43:08 -0500 | [diff] [blame] | 1046 | if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) { |
Robert Craig | b9e3ba5 | 2014-02-04 10:53:00 -0500 | [diff] [blame] | 1047 | SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno)); |
| 1048 | result |= -1; |
| 1049 | } |
| 1050 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1051 | close(fd); |
| 1052 | } |
| 1053 | fts_close(fts); |
| 1054 | |
| 1055 | // Finally make the directory readable by everyone. |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 1056 | int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1057 | if (dirfd < 0 || fchmod(dirfd, 0755)) { |
| 1058 | SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno)); |
| 1059 | result |= -1; |
| 1060 | } |
| 1061 | close(dirfd); |
| 1062 | } else { |
| 1063 | result |= -1; |
| 1064 | } |
| 1065 | |
| 1066 | result |= Ext4::doMount(loopDevice, mountPoint, |
| 1067 | true /* read-only */, |
| 1068 | true /* remount */, |
| 1069 | true /* execute */); |
| 1070 | |
| 1071 | if (result) { |
| 1072 | SLOGE("ASEC fix permissions failed (%s)", strerror(errno)); |
| 1073 | return -1; |
| 1074 | } |
| 1075 | |
| 1076 | if (mDebug) { |
| 1077 | SLOGD("ASEC %s permissions fixed", id); |
| 1078 | } |
| 1079 | return 0; |
| 1080 | } |
| 1081 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1082 | int VolumeManager::renameAsec(const char *id1, const char *id2) { |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1083 | char asecFilename1[255]; |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1084 | char *asecFilename2; |
| 1085 | char mountPoint[255]; |
| 1086 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1087 | const char *dir; |
| 1088 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1089 | if (!isLegalAsecId(id1)) { |
| 1090 | SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1); |
| 1091 | errno = EINVAL; |
| 1092 | return -1; |
| 1093 | } |
| 1094 | |
| 1095 | if (!isLegalAsecId(id2)) { |
| 1096 | SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2); |
| 1097 | errno = EINVAL; |
| 1098 | return -1; |
| 1099 | } |
| 1100 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1101 | if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) { |
| 1102 | SLOGE("Couldn't find ASEC %s", id1); |
| 1103 | return -1; |
| 1104 | } |
| 1105 | |
| 1106 | asprintf(&asecFilename2, "%s/%s.asec", dir, id2); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1107 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1108 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id1); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1109 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1110 | SLOGE("Rename failed: couldn't construct mountpoint"); |
| 1111 | goto out_err; |
| 1112 | } |
| 1113 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1114 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1115 | SLOGW("Rename attempt when src mounted"); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1116 | errno = EBUSY; |
| 1117 | goto out_err; |
| 1118 | } |
| 1119 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1120 | written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id2); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1121 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1122 | SLOGE("Rename failed: couldn't construct mountpoint2"); |
| 1123 | goto out_err; |
| 1124 | } |
| 1125 | |
San Mehat | 96956ed | 2010-02-24 08:42:51 -0800 | [diff] [blame] | 1126 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1127 | SLOGW("Rename attempt when dst mounted"); |
San Mehat | 96956ed | 2010-02-24 08:42:51 -0800 | [diff] [blame] | 1128 | errno = EBUSY; |
| 1129 | goto out_err; |
| 1130 | } |
| 1131 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1132 | if (!access(asecFilename2, F_OK)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1133 | SLOGE("Rename attempt when dst exists"); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1134 | errno = EADDRINUSE; |
| 1135 | goto out_err; |
| 1136 | } |
| 1137 | |
| 1138 | if (rename(asecFilename1, asecFilename2)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1139 | SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno)); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1140 | goto out_err; |
| 1141 | } |
| 1142 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1143 | free(asecFilename2); |
| 1144 | return 0; |
| 1145 | |
| 1146 | out_err: |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1147 | free(asecFilename2); |
| 1148 | return -1; |
| 1149 | } |
| 1150 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1151 | #define UNMOUNT_RETRIES 5 |
| 1152 | #define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000) |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1153 | int VolumeManager::unmountAsec(const char *id, bool force) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1154 | char asecFileName[255]; |
| 1155 | char mountPoint[255]; |
| 1156 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1157 | if (!isLegalAsecId(id)) { |
| 1158 | SLOGE("unmountAsec: Invalid asec id \"%s\"", id); |
| 1159 | errno = EINVAL; |
| 1160 | return -1; |
| 1161 | } |
| 1162 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1163 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 1164 | SLOGE("Couldn't find ASEC %s", id); |
| 1165 | return -1; |
| 1166 | } |
| 1167 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1168 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1169 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1170 | SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id); |
| 1171 | return -1; |
| 1172 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1173 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1174 | char idHash[33]; |
| 1175 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1176 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1177 | return -1; |
| 1178 | } |
| 1179 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1180 | return unmountLoopImage(id, idHash, asecFileName, mountPoint, force); |
| 1181 | } |
| 1182 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1183 | int VolumeManager::unmountObb(const char *fileName, bool force) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1184 | char mountPoint[255]; |
| 1185 | |
| 1186 | char idHash[33]; |
| 1187 | if (!asecHash(fileName, idHash, sizeof(idHash))) { |
| 1188 | SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno)); |
| 1189 | return -1; |
| 1190 | } |
| 1191 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1192 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1193 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1194 | SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName); |
| 1195 | return -1; |
| 1196 | } |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1197 | |
| 1198 | return unmountLoopImage(fileName, idHash, fileName, mountPoint, force); |
| 1199 | } |
| 1200 | |
| 1201 | int VolumeManager::unmountLoopImage(const char *id, const char *idHash, |
| 1202 | const char *fileName, const char *mountPoint, bool force) { |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1203 | if (!isMountpointMounted(mountPoint)) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1204 | SLOGE("Unmount request for %s when not mounted", id); |
Kenny Root | 918e5f9 | 2010-09-30 18:00:52 -0700 | [diff] [blame] | 1205 | errno = ENOENT; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1206 | return -1; |
| 1207 | } |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 1208 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1209 | int i, rc; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1210 | for (i = 1; i <= UNMOUNT_RETRIES; i++) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1211 | rc = umount(mountPoint); |
| 1212 | if (!rc) { |
| 1213 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1214 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1215 | if (rc && (errno == EINVAL || errno == ENOENT)) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1216 | SLOGI("Container %s unmounted OK", id); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1217 | rc = 0; |
| 1218 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1219 | } |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1220 | SLOGW("%s unmount attempt %d failed (%s)", |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 1221 | id, i, strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1222 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1223 | int signal = 0; // default is to just complain |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1224 | |
| 1225 | if (force) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1226 | if (i > (UNMOUNT_RETRIES - 2)) |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1227 | signal = SIGKILL; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1228 | else if (i > (UNMOUNT_RETRIES - 3)) |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1229 | signal = SIGTERM; |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1230 | } |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 1231 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1232 | Process::killProcessesWithOpenFiles(mountPoint, signal); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1233 | usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | if (rc) { |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1237 | errno = EBUSY; |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1238 | SLOGE("Failed to unmount container %s (%s)", id, strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1239 | return -1; |
| 1240 | } |
| 1241 | |
San Mehat | 12f4b89 | 2010-02-24 11:43:22 -0800 | [diff] [blame] | 1242 | int retries = 10; |
| 1243 | |
| 1244 | while(retries--) { |
| 1245 | if (!rmdir(mountPoint)) { |
| 1246 | break; |
| 1247 | } |
| 1248 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1249 | SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno)); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1250 | usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS); |
San Mehat | 12f4b89 | 2010-02-24 11:43:22 -0800 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | if (!retries) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1254 | SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno)); |
San Mehat | f5c6198 | 2010-02-03 11:04:46 -0800 | [diff] [blame] | 1255 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1256 | |
Paul Lawrence | 60dec16 | 2014-09-02 10:52:15 -0700 | [diff] [blame] | 1257 | for (i=1; i <= UNMOUNT_RETRIES; i++) { |
| 1258 | if (Devmapper::destroy(idHash) && errno != ENXIO) { |
| 1259 | SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno)); |
| 1260 | usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS); |
| 1261 | continue; |
| 1262 | } else { |
| 1263 | break; |
| 1264 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | char loopDevice[255]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1268 | if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1269 | Loop::destroyByDevice(loopDevice); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1270 | } else { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1271 | SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1272 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1273 | |
| 1274 | AsecIdCollection::iterator it; |
| 1275 | for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) { |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1276 | ContainerData* cd = *it; |
| 1277 | if (!strcmp(cd->id, id)) { |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1278 | free(*it); |
| 1279 | mActiveContainers->erase(it); |
| 1280 | break; |
| 1281 | } |
| 1282 | } |
| 1283 | if (it == mActiveContainers->end()) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1284 | SLOGW("mActiveContainers is inconsistent!"); |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1285 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1286 | return 0; |
| 1287 | } |
| 1288 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1289 | int VolumeManager::destroyAsec(const char *id, bool force) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1290 | char asecFileName[255]; |
| 1291 | char mountPoint[255]; |
| 1292 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1293 | if (!isLegalAsecId(id)) { |
| 1294 | SLOGE("destroyAsec: Invalid asec id \"%s\"", id); |
| 1295 | errno = EINVAL; |
| 1296 | return -1; |
| 1297 | } |
| 1298 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1299 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 1300 | SLOGE("Couldn't find ASEC %s", id); |
| 1301 | return -1; |
| 1302 | } |
| 1303 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1304 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1305 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1306 | SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id); |
| 1307 | return -1; |
| 1308 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1309 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1310 | if (isMountpointMounted(mountPoint)) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1311 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1312 | SLOGD("Unmounting container before destroy"); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1313 | } |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1314 | if (unmountAsec(id, force)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1315 | SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno)); |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1316 | return -1; |
| 1317 | } |
| 1318 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1319 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1320 | if (unlink(asecFileName)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1321 | SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno)); |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1322 | return -1; |
| 1323 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1324 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1325 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1326 | SLOGD("ASEC %s destroyed", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1327 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1328 | return 0; |
| 1329 | } |
| 1330 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1331 | /* |
| 1332 | * Legal ASEC ids consist of alphanumeric characters, '-', |
| 1333 | * '_', or '.'. ".." is not allowed. The first or last character |
| 1334 | * of the ASEC id cannot be '.' (dot). |
| 1335 | */ |
| 1336 | bool VolumeManager::isLegalAsecId(const char *id) const { |
| 1337 | size_t i; |
| 1338 | size_t len = strlen(id); |
| 1339 | |
| 1340 | if (len == 0) { |
| 1341 | return false; |
| 1342 | } |
| 1343 | if ((id[0] == '.') || (id[len - 1] == '.')) { |
| 1344 | return false; |
| 1345 | } |
| 1346 | |
| 1347 | for (i = 0; i < len; i++) { |
| 1348 | if (id[i] == '.') { |
| 1349 | // i=0 is guaranteed never to have a dot. See above. |
| 1350 | if (id[i-1] == '.') return false; |
| 1351 | continue; |
| 1352 | } |
| 1353 | if (id[i] == '_' || id[i] == '-') continue; |
| 1354 | if (id[i] >= 'a' && id[i] <= 'z') continue; |
| 1355 | if (id[i] >= 'A' && id[i] <= 'Z') continue; |
| 1356 | if (id[i] >= '0' && id[i] <= '9') continue; |
| 1357 | return false; |
| 1358 | } |
| 1359 | |
| 1360 | return true; |
| 1361 | } |
| 1362 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1363 | bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 1364 | int dirfd = open(dir, O_DIRECTORY | O_CLOEXEC); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1365 | if (dirfd < 0) { |
| 1366 | SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno)); |
Nick Kralevich | 25e581a | 2015-02-06 08:55:08 -0800 | [diff] [blame] | 1367 | return false; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1368 | } |
| 1369 | |
Nick Kralevich | 25e581a | 2015-02-06 08:55:08 -0800 | [diff] [blame] | 1370 | struct stat sb; |
| 1371 | bool ret = (fstatat(dirfd, asecName, &sb, AT_SYMLINK_NOFOLLOW) == 0) |
| 1372 | && S_ISREG(sb.st_mode); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1373 | |
| 1374 | close(dirfd); |
| 1375 | |
| 1376 | return ret; |
| 1377 | } |
| 1378 | |
| 1379 | int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen, |
| 1380 | const char **directory) const { |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1381 | char *asecName; |
| 1382 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1383 | if (!isLegalAsecId(id)) { |
| 1384 | SLOGE("findAsec: Invalid asec id \"%s\"", id); |
| 1385 | errno = EINVAL; |
| 1386 | return -1; |
| 1387 | } |
| 1388 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1389 | if (asprintf(&asecName, "%s.asec", id) < 0) { |
| 1390 | SLOGE("Couldn't allocate string to write ASEC name"); |
| 1391 | return -1; |
| 1392 | } |
| 1393 | |
| 1394 | const char *dir; |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1395 | if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_INT, asecName)) { |
| 1396 | dir = VolumeManager::SEC_ASECDIR_INT; |
| 1397 | } else if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_EXT, asecName)) { |
| 1398 | dir = VolumeManager::SEC_ASECDIR_EXT; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1399 | } else { |
| 1400 | free(asecName); |
| 1401 | return -1; |
| 1402 | } |
| 1403 | |
| 1404 | if (directory != NULL) { |
| 1405 | *directory = dir; |
| 1406 | } |
| 1407 | |
| 1408 | if (asecPath != NULL) { |
| 1409 | int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1410 | if ((written < 0) || (size_t(written) >= asecPathLen)) { |
| 1411 | SLOGE("findAsec failed for %s: couldn't construct ASEC path", id); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1412 | free(asecName); |
| 1413 | return -1; |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | free(asecName); |
| 1418 | return 0; |
| 1419 | } |
| 1420 | |
Jeff Sharkey | 43ed123 | 2014-08-22 12:29:05 -0700 | [diff] [blame] | 1421 | int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid, bool readOnly) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1422 | char asecFileName[255]; |
| 1423 | char mountPoint[255]; |
| 1424 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1425 | if (!isLegalAsecId(id)) { |
| 1426 | SLOGE("mountAsec: Invalid asec id \"%s\"", id); |
| 1427 | errno = EINVAL; |
| 1428 | return -1; |
| 1429 | } |
| 1430 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1431 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 1432 | SLOGE("Couldn't find ASEC %s", id); |
| 1433 | return -1; |
| 1434 | } |
| 1435 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1436 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1437 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
Colin Cross | 59846b6 | 2014-02-06 20:34:29 -0800 | [diff] [blame] | 1438 | SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1439 | return -1; |
| 1440 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1441 | |
| 1442 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1443 | SLOGE("ASEC %s already mounted", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1444 | errno = EBUSY; |
| 1445 | return -1; |
| 1446 | } |
| 1447 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1448 | char idHash[33]; |
| 1449 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1450 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1451 | return -1; |
| 1452 | } |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 1453 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1454 | char loopDevice[255]; |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1455 | if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug)) |
| 1456 | return -1; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1457 | |
| 1458 | char dmDevice[255]; |
| 1459 | bool cleanupDm = false; |
Tim Murray | 8439dc9 | 2014-12-15 11:56:11 -0800 | [diff] [blame] | 1460 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1461 | unsigned long nr_sec = 0; |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1462 | struct asec_superblock sb; |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1463 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1464 | if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) { |
| 1465 | return -1; |
| 1466 | } |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1467 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1468 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1469 | SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1470 | } |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1471 | if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1472 | SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1473 | Loop::destroyByDevice(loopDevice); |
| 1474 | errno = EMEDIUMTYPE; |
| 1475 | return -1; |
| 1476 | } |
| 1477 | nr_sec--; // We don't want the devmapping to extend onto our superblock |
| 1478 | |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1479 | if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) { |
| 1480 | Loop::destroyByDevice(loopDevice); |
| 1481 | return -1; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1482 | } |
| 1483 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1484 | if (mkdir(mountPoint, 0000)) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1485 | if (errno != EEXIST) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1486 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1487 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1488 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1489 | } |
| 1490 | Loop::destroyByDevice(loopDevice); |
| 1491 | return -1; |
| 1492 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1493 | } |
| 1494 | |
Kenny Root | cdc2a1c | 2012-05-03 13:49:46 -0700 | [diff] [blame] | 1495 | /* |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1496 | * Wait for the device mapper node to be created. |
Kenny Root | cdc2a1c | 2012-05-03 13:49:46 -0700 | [diff] [blame] | 1497 | */ |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1498 | waitForDevMapper(dmDevice); |
Kenny Root | cdc2a1c | 2012-05-03 13:49:46 -0700 | [diff] [blame] | 1499 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1500 | int result; |
| 1501 | if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) { |
Jeff Sharkey | 43ed123 | 2014-08-22 12:29:05 -0700 | [diff] [blame] | 1502 | result = Ext4::doMount(dmDevice, mountPoint, readOnly, false, readOnly); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1503 | } else { |
Jeff Sharkey | 43ed123 | 2014-08-22 12:29:05 -0700 | [diff] [blame] | 1504 | result = Fat::doMount(dmDevice, mountPoint, readOnly, false, readOnly, ownerUid, 0, 0222, false); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | if (result) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1508 | SLOGE("ASEC mount failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1509 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1510 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1511 | } |
| 1512 | Loop::destroyByDevice(loopDevice); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1513 | return -1; |
| 1514 | } |
| 1515 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1516 | mActiveContainers->push_back(new ContainerData(strdup(id), ASEC)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1517 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1518 | SLOGD("ASEC %s mounted", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1519 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1520 | return 0; |
| 1521 | } |
| 1522 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1523 | /** |
| 1524 | * Mounts an image file <code>img</code>. |
| 1525 | */ |
Jeff Sharkey | 6947904 | 2012-09-25 16:14:57 -0700 | [diff] [blame] | 1526 | int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1527 | char mountPoint[255]; |
| 1528 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1529 | char idHash[33]; |
| 1530 | if (!asecHash(img, idHash, sizeof(idHash))) { |
| 1531 | SLOGE("Hash of '%s' failed (%s)", img, strerror(errno)); |
| 1532 | return -1; |
| 1533 | } |
| 1534 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1535 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1536 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
Colin Cross | 59846b6 | 2014-02-06 20:34:29 -0800 | [diff] [blame] | 1537 | SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1538 | return -1; |
| 1539 | } |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1540 | |
| 1541 | if (isMountpointMounted(mountPoint)) { |
| 1542 | SLOGE("Image %s already mounted", img); |
| 1543 | errno = EBUSY; |
| 1544 | return -1; |
| 1545 | } |
| 1546 | |
| 1547 | char loopDevice[255]; |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1548 | if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug)) |
| 1549 | return -1; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1550 | |
| 1551 | char dmDevice[255]; |
| 1552 | bool cleanupDm = false; |
| 1553 | int fd; |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1554 | unsigned long nr_sec = 0; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1555 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 1556 | if ((fd = open(loopDevice, O_RDWR | O_CLOEXEC)) < 0) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1557 | SLOGE("Failed to open loopdevice (%s)", strerror(errno)); |
| 1558 | Loop::destroyByDevice(loopDevice); |
| 1559 | return -1; |
| 1560 | } |
| 1561 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1562 | get_blkdev_size(fd, &nr_sec); |
| 1563 | if (nr_sec == 0) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1564 | SLOGE("Failed to get loop size (%s)", strerror(errno)); |
| 1565 | Loop::destroyByDevice(loopDevice); |
| 1566 | close(fd); |
| 1567 | return -1; |
| 1568 | } |
| 1569 | |
| 1570 | close(fd); |
| 1571 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1572 | if (setupDevMapperDevice(dmDevice, sizeof(loopDevice), loopDevice, img,key, idHash, nr_sec, &cleanupDm, mDebug)) { |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1573 | Loop::destroyByDevice(loopDevice); |
| 1574 | return -1; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | if (mkdir(mountPoint, 0755)) { |
| 1578 | if (errno != EEXIST) { |
| 1579 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
| 1580 | if (cleanupDm) { |
| 1581 | Devmapper::destroy(idHash); |
| 1582 | } |
| 1583 | Loop::destroyByDevice(loopDevice); |
| 1584 | return -1; |
| 1585 | } |
| 1586 | } |
| 1587 | |
yoshiyuki hama | 476a627 | 2015-01-28 16:37:23 +0900 | [diff] [blame] | 1588 | /* |
| 1589 | * Wait for the device mapper node to be created. |
| 1590 | */ |
| 1591 | waitForDevMapper(dmDevice); |
| 1592 | |
Jeff Sharkey | 6947904 | 2012-09-25 16:14:57 -0700 | [diff] [blame] | 1593 | if (Fat::doMount(dmDevice, mountPoint, true, false, true, 0, ownerGid, |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1594 | 0227, false)) { |
| 1595 | SLOGE("Image mount failed (%s)", strerror(errno)); |
| 1596 | if (cleanupDm) { |
| 1597 | Devmapper::destroy(idHash); |
| 1598 | } |
| 1599 | Loop::destroyByDevice(loopDevice); |
| 1600 | return -1; |
| 1601 | } |
| 1602 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1603 | mActiveContainers->push_back(new ContainerData(strdup(img), OBB)); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1604 | if (mDebug) { |
| 1605 | SLOGD("Image %s mounted", img); |
| 1606 | } |
| 1607 | return 0; |
| 1608 | } |
| 1609 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1610 | int VolumeManager::listMountedObbs(SocketClient* cli) { |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1611 | FILE *fp = setmntent("/proc/mounts", "r"); |
| 1612 | if (fp == NULL) { |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1613 | SLOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
| 1614 | return -1; |
| 1615 | } |
| 1616 | |
| 1617 | // Create a string to compare against that has a trailing slash |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1618 | int loopDirLen = strlen(VolumeManager::LOOPDIR); |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1619 | char loopDir[loopDirLen + 2]; |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1620 | strcpy(loopDir, VolumeManager::LOOPDIR); |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1621 | loopDir[loopDirLen++] = '/'; |
| 1622 | loopDir[loopDirLen] = '\0'; |
| 1623 | |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1624 | mntent* mentry; |
| 1625 | while ((mentry = getmntent(fp)) != NULL) { |
| 1626 | if (!strncmp(mentry->mnt_dir, loopDir, loopDirLen)) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 1627 | int fd = open(mentry->mnt_fsname, O_RDONLY | O_CLOEXEC); |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1628 | if (fd >= 0) { |
| 1629 | struct loop_info64 li; |
| 1630 | if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) { |
| 1631 | cli->sendMsg(ResponseCode::AsecListResult, |
| 1632 | (const char*) li.lo_file_name, false); |
| 1633 | } |
| 1634 | close(fd); |
| 1635 | } |
| 1636 | } |
| 1637 | } |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1638 | endmntent(fp); |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1639 | return 0; |
| 1640 | } |
| 1641 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1642 | extern "C" int vold_unmountAll(void) { |
Ken Sumrall | 425524d | 2012-06-14 20:55:28 -0700 | [diff] [blame] | 1643 | VolumeManager *vm = VolumeManager::Instance(); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1644 | return vm->unmountAll(); |
Ken Sumrall | 425524d | 2012-06-14 20:55:28 -0700 | [diff] [blame] | 1645 | } |
| 1646 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1647 | bool VolumeManager::isMountpointMounted(const char *mp) |
| 1648 | { |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1649 | FILE *fp = setmntent("/proc/mounts", "r"); |
| 1650 | if (fp == NULL) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1651 | SLOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1652 | return false; |
| 1653 | } |
| 1654 | |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1655 | bool found_mp = false; |
| 1656 | mntent* mentry; |
| 1657 | while ((mentry = getmntent(fp)) != NULL) { |
| 1658 | if (strcmp(mentry->mnt_dir, mp) == 0) { |
| 1659 | found_mp = true; |
| 1660 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1661 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1662 | } |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1663 | endmntent(fp); |
| 1664 | return found_mp; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1665 | } |
| 1666 | |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1667 | int VolumeManager::mkdirs(char* path) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1668 | // Only offer to create directories for paths managed by vold |
| 1669 | if (strncmp(path, "/storage/", 9) == 0) { |
| 1670 | // fs_mkdirs() does symlink checking and relative path enforcement |
| 1671 | return fs_mkdirs(path, 0700); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1672 | } else { |
Cylen Yao | 27cfee3 | 2014-05-02 19:23:42 +0800 | [diff] [blame] | 1673 | SLOGE("Failed to find mounted volume for %s", path); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1674 | return -EINVAL; |
| 1675 | } |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1676 | } |