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 | } |
| 382 | if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) { |
| 383 | SLOGW("Failed to link %s to %s: %s", source.c_str(), target.c_str(), |
| 384 | strerror(errno)); |
| 385 | return -errno; |
| 386 | } |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | int VolumeManager::startUser(userid_t userId) { |
| 391 | // Note that sometimes the system will spin up processes from Zygote |
| 392 | // before actually starting the user, so we're okay if Zygote |
| 393 | // already created this directory. |
| 394 | std::string path(StringPrintf("%s/%d", kUserMountPath, userId)); |
| 395 | fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT); |
| 396 | |
| 397 | mUsers.push_back(userId); |
| 398 | if (mPrimary) { |
| 399 | linkPrimary(userId); |
| 400 | } |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | int VolumeManager::cleanupUser(userid_t userId) { |
| 405 | mUsers.remove(userId); |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) { |
| 410 | mPrimary = vol; |
| 411 | for (userid_t userId : mUsers) { |
| 412 | linkPrimary(userId); |
| 413 | } |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | int VolumeManager::reset() { |
| 418 | // Tear down all existing disks/volumes and start from a blank slate so |
| 419 | // newly connected framework hears all events. |
| 420 | mInternalEmulated->destroy(); |
| 421 | mInternalEmulated->create(); |
| 422 | for (auto disk : mDisks) { |
| 423 | disk->destroy(); |
| 424 | disk->create(); |
| 425 | } |
| 426 | mUsers.clear(); |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | int VolumeManager::shutdown() { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 431 | mInternalEmulated->destroy(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 432 | for (auto disk : mDisks) { |
| 433 | disk->destroy(); |
| 434 | } |
| 435 | mDisks.clear(); |
| 436 | return 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 439 | int VolumeManager::unmountAll() { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame^] | 440 | std::lock_guard<std::mutex> lock(mLock); |
| 441 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 442 | // First, try gracefully unmounting all known devices |
| 443 | if (mInternalEmulated != nullptr) { |
| 444 | mInternalEmulated->unmount(); |
| 445 | } |
| 446 | for (auto disk : mDisks) { |
| 447 | disk->unmountAll(); |
| 448 | } |
| 449 | |
| 450 | // Worst case we might have some stale mounts lurking around, so |
| 451 | // force unmount those just to be safe. |
| 452 | FILE* fp = setmntent("/proc/mounts", "r"); |
| 453 | if (fp == NULL) { |
| 454 | SLOGE("Error opening /proc/mounts: %s", strerror(errno)); |
| 455 | return -errno; |
| 456 | } |
| 457 | |
| 458 | // Some volumes can be stacked on each other, so force unmount in |
| 459 | // reverse order to give us the best chance of success. |
| 460 | std::list<std::string> toUnmount; |
| 461 | mntent* mentry; |
| 462 | while ((mentry = getmntent(fp)) != NULL) { |
| 463 | if (strncmp(mentry->mnt_dir, "/mnt/", 5) == 0 |
| 464 | || strncmp(mentry->mnt_dir, "/storage/", 9) == 0) { |
| 465 | toUnmount.push_front(std::string(mentry->mnt_dir)); |
| 466 | } |
| 467 | } |
| 468 | endmntent(fp); |
| 469 | |
| 470 | for (auto path : toUnmount) { |
| 471 | SLOGW("Tearing down stale mount %s", path.c_str()); |
| 472 | android::vold::ForceUnmount(path); |
| 473 | } |
| 474 | |
| 475 | return 0; |
| 476 | } |
| 477 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 478 | int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) { |
| 479 | char idHash[33]; |
| 480 | if (!asecHash(sourceFile, idHash, sizeof(idHash))) { |
| 481 | SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno)); |
| 482 | return -1; |
| 483 | } |
| 484 | |
| 485 | memset(mountPath, 0, mountPathLen); |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 486 | int written = snprintf(mountPath, mountPathLen, "%s/%s", VolumeManager::LOOPDIR, idHash); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 487 | if ((written < 0) || (written >= mountPathLen)) { |
| 488 | errno = EINVAL; |
| 489 | return -1; |
| 490 | } |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 491 | |
| 492 | if (access(mountPath, F_OK)) { |
| 493 | errno = ENOENT; |
| 494 | return -1; |
| 495 | } |
| 496 | |
| 497 | return 0; |
| 498 | } |
| 499 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 500 | int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) { |
San Mehat | 88ac2c0 | 2010-03-23 11:15:58 -0700 | [diff] [blame] | 501 | char asecFileName[255]; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 502 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 503 | if (!isLegalAsecId(id)) { |
| 504 | SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id); |
| 505 | errno = EINVAL; |
| 506 | return -1; |
| 507 | } |
| 508 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 509 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 510 | SLOGE("Couldn't find ASEC %s", id); |
| 511 | return -1; |
| 512 | } |
San Mehat | 88ac2c0 | 2010-03-23 11:15:58 -0700 | [diff] [blame] | 513 | |
| 514 | memset(buffer, 0, maxlen); |
| 515 | if (access(asecFileName, F_OK)) { |
| 516 | errno = ENOENT; |
| 517 | return -1; |
| 518 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 519 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 520 | int written = snprintf(buffer, maxlen, "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 521 | if ((written < 0) || (written >= maxlen)) { |
| 522 | SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id); |
| 523 | errno = EINVAL; |
| 524 | return -1; |
| 525 | } |
| 526 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 527 | return 0; |
| 528 | } |
| 529 | |
Dianne Hackborn | 736910c | 2011-06-27 13:37:07 -0700 | [diff] [blame] | 530 | int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) { |
| 531 | char asecFileName[255]; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 532 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 533 | if (!isLegalAsecId(id)) { |
| 534 | SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id); |
| 535 | errno = EINVAL; |
| 536 | return -1; |
| 537 | } |
| 538 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 539 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 540 | SLOGE("Couldn't find ASEC %s", id); |
| 541 | return -1; |
| 542 | } |
Dianne Hackborn | 736910c | 2011-06-27 13:37:07 -0700 | [diff] [blame] | 543 | |
| 544 | memset(buffer, 0, maxlen); |
| 545 | if (access(asecFileName, F_OK)) { |
| 546 | errno = ENOENT; |
| 547 | return -1; |
| 548 | } |
| 549 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 550 | int written = snprintf(buffer, maxlen, "%s", asecFileName); |
| 551 | if ((written < 0) || (written >= maxlen)) { |
| 552 | errno = EINVAL; |
| 553 | return -1; |
| 554 | } |
| 555 | |
Dianne Hackborn | 736910c | 2011-06-27 13:37:07 -0700 | [diff] [blame] | 556 | return 0; |
| 557 | } |
| 558 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 559 | int VolumeManager::createAsec(const char *id, unsigned int numSectors, const char *fstype, |
| 560 | const char *key, const int ownerUid, bool isExternal) { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 561 | struct asec_superblock sb; |
| 562 | memset(&sb, 0, sizeof(sb)); |
| 563 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 564 | if (!isLegalAsecId(id)) { |
| 565 | SLOGE("createAsec: Invalid asec id \"%s\"", id); |
| 566 | errno = EINVAL; |
| 567 | return -1; |
| 568 | } |
| 569 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 570 | const bool wantFilesystem = strcmp(fstype, "none"); |
| 571 | bool usingExt4 = false; |
| 572 | if (wantFilesystem) { |
| 573 | usingExt4 = !strcmp(fstype, "ext4"); |
| 574 | if (usingExt4) { |
| 575 | sb.c_opts |= ASEC_SB_C_OPTS_EXT4; |
| 576 | } else if (strcmp(fstype, "fat")) { |
| 577 | SLOGE("Invalid filesystem type %s", fstype); |
| 578 | errno = EINVAL; |
| 579 | return -1; |
| 580 | } |
| 581 | } |
| 582 | |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 583 | sb.magic = ASEC_SB_MAGIC; |
| 584 | sb.ver = ASEC_SB_VER; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 585 | |
San Mehat | d31e380 | 2010-02-18 08:37:45 -0800 | [diff] [blame] | 586 | if (numSectors < ((1024*1024)/512)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 587 | SLOGE("Invalid container size specified (%d sectors)", numSectors); |
San Mehat | d31e380 | 2010-02-18 08:37:45 -0800 | [diff] [blame] | 588 | errno = EINVAL; |
| 589 | return -1; |
| 590 | } |
| 591 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 592 | char asecFileName[255]; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 593 | |
| 594 | if (!findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 595 | SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)", |
| 596 | asecFileName, strerror(errno)); |
| 597 | errno = EADDRINUSE; |
| 598 | return -1; |
| 599 | } |
| 600 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 601 | const char *asecDir = isExternal ? VolumeManager::SEC_ASECDIR_EXT : VolumeManager::SEC_ASECDIR_INT; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 602 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 603 | int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id); |
| 604 | if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) { |
| 605 | errno = EINVAL; |
| 606 | return -1; |
| 607 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 608 | |
| 609 | if (!access(asecFileName, F_OK)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 610 | SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)", |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 611 | asecFileName, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 612 | errno = EADDRINUSE; |
| 613 | return -1; |
| 614 | } |
| 615 | |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 616 | unsigned numImgSectors; |
Daniel Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 617 | if (usingExt4) |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 618 | numImgSectors = adjustSectorNumExt4(numSectors); |
Daniel Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 619 | else |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 620 | numImgSectors = adjustSectorNumFAT(numSectors); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 621 | |
| 622 | // Add +1 for our superblock which is at the end |
| 623 | if (Loop::createImageFile(asecFileName, numImgSectors + 1)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 624 | SLOGE("ASEC image file creation failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 625 | return -1; |
| 626 | } |
| 627 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 628 | char idHash[33]; |
| 629 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 630 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 631 | unlink(asecFileName); |
| 632 | return -1; |
| 633 | } |
| 634 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 635 | char loopDevice[255]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 636 | if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 637 | SLOGE("ASEC loop device creation failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 638 | unlink(asecFileName); |
| 639 | return -1; |
| 640 | } |
| 641 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 642 | char dmDevice[255]; |
| 643 | bool cleanupDm = false; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 644 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 645 | if (strcmp(key, "none")) { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 646 | // XXX: This is all we support for now |
| 647 | sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 648 | if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice, |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 649 | sizeof(dmDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 650 | SLOGE("ASEC device mapping failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 651 | Loop::destroyByDevice(loopDevice); |
| 652 | unlink(asecFileName); |
| 653 | return -1; |
| 654 | } |
| 655 | cleanupDm = true; |
| 656 | } else { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 657 | sb.c_cipher = ASEC_SB_C_CIPHER_NONE; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 658 | strcpy(dmDevice, loopDevice); |
| 659 | } |
| 660 | |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 661 | /* |
| 662 | * Drop down the superblock at the end of the file |
| 663 | */ |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 664 | if (writeSuperBlock(loopDevice, &sb, numImgSectors)) { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 665 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 666 | Devmapper::destroy(idHash); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 667 | } |
| 668 | Loop::destroyByDevice(loopDevice); |
| 669 | unlink(asecFileName); |
| 670 | return -1; |
| 671 | } |
| 672 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 673 | if (wantFilesystem) { |
| 674 | int formatStatus; |
rpcraig | a54e13a | 2012-09-21 14:17:08 -0400 | [diff] [blame] | 675 | char mountPoint[255]; |
| 676 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 677 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 678 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 679 | SLOGE("ASEC fs format failed: couldn't construct mountPoint"); |
| 680 | if (cleanupDm) { |
| 681 | Devmapper::destroy(idHash); |
| 682 | } |
| 683 | Loop::destroyByDevice(loopDevice); |
| 684 | unlink(asecFileName); |
| 685 | return -1; |
| 686 | } |
rpcraig | a54e13a | 2012-09-21 14:17:08 -0400 | [diff] [blame] | 687 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 688 | if (usingExt4) { |
Daniel Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 689 | formatStatus = Ext4::format(dmDevice, numImgSectors, mountPoint); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 690 | } else { |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 691 | formatStatus = Fat::format(dmDevice, numImgSectors, 0); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 692 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 693 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 694 | if (formatStatus < 0) { |
| 695 | SLOGE("ASEC fs format failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 696 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 697 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 698 | } |
San Mehat | eb13a90 | 2010-01-07 12:12:50 -0800 | [diff] [blame] | 699 | Loop::destroyByDevice(loopDevice); |
| 700 | unlink(asecFileName); |
| 701 | return -1; |
| 702 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 703 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 704 | if (mkdir(mountPoint, 0000)) { |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 705 | if (errno != EEXIST) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 706 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 707 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 708 | Devmapper::destroy(idHash); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 709 | } |
| 710 | Loop::destroyByDevice(loopDevice); |
| 711 | unlink(asecFileName); |
| 712 | return -1; |
| 713 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 714 | } |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 715 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 716 | int mountStatus; |
| 717 | if (usingExt4) { |
| 718 | mountStatus = Ext4::doMount(dmDevice, mountPoint, false, false, false); |
| 719 | } else { |
| 720 | mountStatus = Fat::doMount(dmDevice, mountPoint, false, false, false, ownerUid, 0, 0000, |
| 721 | false); |
| 722 | } |
| 723 | |
| 724 | if (mountStatus) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 725 | SLOGE("ASEC FAT mount failed (%s)", strerror(errno)); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 726 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 727 | Devmapper::destroy(idHash); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 728 | } |
| 729 | Loop::destroyByDevice(loopDevice); |
| 730 | unlink(asecFileName); |
| 731 | return -1; |
| 732 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 733 | |
| 734 | if (usingExt4) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 735 | int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 736 | if (dirfd >= 0) { |
| 737 | if (fchown(dirfd, ownerUid, AID_SYSTEM) |
| 738 | || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) { |
| 739 | SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint); |
| 740 | } |
| 741 | close(dirfd); |
| 742 | } |
| 743 | } |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 744 | } else { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 745 | SLOGI("Created raw secure container %s (no filesystem)", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 746 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 747 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 748 | mActiveContainers->push_back(new ContainerData(strdup(id), ASEC)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 749 | return 0; |
| 750 | } |
| 751 | |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 752 | int VolumeManager::resizeAsec(const char *id, unsigned numSectors, const char *key) { |
| 753 | char asecFileName[255]; |
| 754 | char mountPoint[255]; |
| 755 | bool cleanupDm = false; |
| 756 | |
| 757 | if (!isLegalAsecId(id)) { |
| 758 | SLOGE("resizeAsec: Invalid asec id \"%s\"", id); |
| 759 | errno = EINVAL; |
| 760 | return -1; |
| 761 | } |
| 762 | |
| 763 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 764 | SLOGE("Couldn't find ASEC %s", id); |
| 765 | return -1; |
| 766 | } |
| 767 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 768 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 769 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 770 | SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id); |
| 771 | return -1; |
| 772 | } |
| 773 | |
| 774 | if (isMountpointMounted(mountPoint)) { |
| 775 | SLOGE("ASEC %s mounted. Unmount before resizing", id); |
| 776 | errno = EBUSY; |
| 777 | return -1; |
| 778 | } |
| 779 | |
| 780 | struct asec_superblock sb; |
| 781 | int fd; |
| 782 | unsigned int oldNumSec = 0; |
| 783 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 784 | if ((fd = open(asecFileName, O_RDONLY | O_CLOEXEC)) < 0) { |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 785 | SLOGE("Failed to open ASEC file (%s)", strerror(errno)); |
| 786 | return -1; |
| 787 | } |
| 788 | |
| 789 | struct stat info; |
| 790 | if (fstat(fd, &info) < 0) { |
| 791 | SLOGE("Failed to get file size (%s)", strerror(errno)); |
| 792 | close(fd); |
| 793 | return -1; |
| 794 | } |
| 795 | |
| 796 | oldNumSec = info.st_size / 512; |
| 797 | |
| 798 | unsigned numImgSectors; |
| 799 | if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) |
| 800 | numImgSectors = adjustSectorNumExt4(numSectors); |
| 801 | else |
| 802 | numImgSectors = adjustSectorNumFAT(numSectors); |
| 803 | /* |
| 804 | * add one block for the superblock |
| 805 | */ |
| 806 | SLOGD("Resizing from %d sectors to %d sectors", oldNumSec, numImgSectors + 1); |
Jeff Sharkey | 43ed123 | 2014-08-22 12:29:05 -0700 | [diff] [blame] | 807 | if (oldNumSec == numImgSectors + 1) { |
| 808 | SLOGW("Size unchanged; ignoring resize request"); |
| 809 | return 0; |
| 810 | } else if (oldNumSec > numImgSectors + 1) { |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 811 | SLOGE("Only growing is currently supported."); |
| 812 | close(fd); |
| 813 | return -1; |
| 814 | } |
| 815 | |
| 816 | /* |
| 817 | * Try to read superblock. |
| 818 | */ |
| 819 | memset(&sb, 0, sizeof(struct asec_superblock)); |
| 820 | if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) { |
| 821 | SLOGE("lseek failed (%s)", strerror(errno)); |
| 822 | close(fd); |
| 823 | return -1; |
| 824 | } |
| 825 | if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) { |
| 826 | SLOGE("superblock read failed (%s)", strerror(errno)); |
| 827 | close(fd); |
| 828 | return -1; |
| 829 | } |
| 830 | close(fd); |
| 831 | |
| 832 | if (mDebug) { |
| 833 | SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver); |
| 834 | } |
| 835 | if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) { |
| 836 | SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver); |
| 837 | errno = EMEDIUMTYPE; |
| 838 | return -1; |
| 839 | } |
| 840 | |
| 841 | if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) { |
| 842 | SLOGE("Only ext4 partitions are supported for resize"); |
| 843 | errno = EINVAL; |
| 844 | return -1; |
| 845 | } |
| 846 | |
| 847 | if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) { |
| 848 | SLOGE("Resize of ASEC image file failed. Could not resize %s", id); |
| 849 | return -1; |
| 850 | } |
| 851 | |
| 852 | /* |
| 853 | * Drop down a copy of the superblock at the end of the file |
| 854 | */ |
| 855 | if (writeSuperBlock(asecFileName, &sb, numImgSectors)) |
| 856 | goto fail; |
| 857 | |
| 858 | char idHash[33]; |
| 859 | if (!asecHash(id, idHash, sizeof(idHash))) { |
| 860 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
| 861 | goto fail; |
| 862 | } |
| 863 | |
| 864 | char loopDevice[255]; |
| 865 | if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug)) |
| 866 | goto fail; |
| 867 | |
| 868 | char dmDevice[255]; |
| 869 | |
| 870 | if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) { |
| 871 | Loop::destroyByDevice(loopDevice); |
| 872 | goto fail; |
| 873 | } |
| 874 | |
| 875 | /* |
| 876 | * Wait for the device mapper node to be created. |
| 877 | */ |
| 878 | waitForDevMapper(dmDevice); |
| 879 | |
| 880 | if (Ext4::resize(dmDevice, numImgSectors)) { |
| 881 | SLOGE("Unable to resize %s (%s)", id, strerror(errno)); |
| 882 | if (cleanupDm) { |
| 883 | Devmapper::destroy(idHash); |
| 884 | } |
| 885 | Loop::destroyByDevice(loopDevice); |
| 886 | goto fail; |
| 887 | } |
| 888 | |
| 889 | return 0; |
| 890 | fail: |
| 891 | Loop::resizeImageFile(asecFileName, oldNumSec); |
| 892 | return -1; |
| 893 | } |
| 894 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 895 | int VolumeManager::finalizeAsec(const char *id) { |
| 896 | char asecFileName[255]; |
| 897 | char loopDevice[255]; |
| 898 | char mountPoint[255]; |
| 899 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 900 | if (!isLegalAsecId(id)) { |
| 901 | SLOGE("finalizeAsec: Invalid asec id \"%s\"", id); |
| 902 | errno = EINVAL; |
| 903 | return -1; |
| 904 | } |
| 905 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 906 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 907 | SLOGE("Couldn't find ASEC %s", id); |
| 908 | return -1; |
| 909 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 910 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 911 | char idHash[33]; |
| 912 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 913 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 914 | return -1; |
| 915 | } |
| 916 | |
| 917 | if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 918 | SLOGE("Unable to finalize %s (%s)", id, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 919 | return -1; |
| 920 | } |
| 921 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 922 | unsigned long nr_sec = 0; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 923 | struct asec_superblock sb; |
| 924 | |
| 925 | if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) { |
| 926 | return -1; |
| 927 | } |
| 928 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 929 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 930 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 931 | SLOGE("ASEC finalize failed: couldn't construct mountPoint"); |
| 932 | return -1; |
| 933 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 934 | |
| 935 | int result = 0; |
| 936 | if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) { |
| 937 | result = Ext4::doMount(loopDevice, mountPoint, true, true, true); |
| 938 | } else { |
| 939 | result = Fat::doMount(loopDevice, mountPoint, true, true, true, 0, 0, 0227, false); |
| 940 | } |
| 941 | |
| 942 | if (result) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 943 | SLOGE("ASEC finalize mount failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 944 | return -1; |
| 945 | } |
| 946 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 947 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 948 | SLOGD("ASEC %s finalized", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 949 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 950 | return 0; |
| 951 | } |
| 952 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 953 | int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) { |
| 954 | char asecFileName[255]; |
| 955 | char loopDevice[255]; |
| 956 | char mountPoint[255]; |
| 957 | |
| 958 | if (gid < AID_APP) { |
| 959 | SLOGE("Group ID is not in application range"); |
| 960 | return -1; |
| 961 | } |
| 962 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 963 | if (!isLegalAsecId(id)) { |
| 964 | SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id); |
| 965 | errno = EINVAL; |
| 966 | return -1; |
| 967 | } |
| 968 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 969 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 970 | SLOGE("Couldn't find ASEC %s", id); |
| 971 | return -1; |
| 972 | } |
| 973 | |
| 974 | char idHash[33]; |
| 975 | if (!asecHash(id, idHash, sizeof(idHash))) { |
| 976 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
| 977 | return -1; |
| 978 | } |
| 979 | |
| 980 | if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
| 981 | SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno)); |
| 982 | return -1; |
| 983 | } |
| 984 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 985 | unsigned long nr_sec = 0; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 986 | struct asec_superblock sb; |
| 987 | |
| 988 | if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) { |
| 989 | return -1; |
| 990 | } |
| 991 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 992 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 993 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 994 | SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id); |
| 995 | return -1; |
| 996 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 997 | |
| 998 | int result = 0; |
| 999 | if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) { |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
| 1003 | int ret = Ext4::doMount(loopDevice, mountPoint, |
| 1004 | false /* read-only */, |
| 1005 | true /* remount */, |
| 1006 | false /* executable */); |
| 1007 | if (ret) { |
| 1008 | SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno)); |
| 1009 | return -1; |
| 1010 | } |
| 1011 | |
| 1012 | char *paths[] = { mountPoint, NULL }; |
| 1013 | |
| 1014 | FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL); |
| 1015 | if (fts) { |
| 1016 | // Traverse the entire hierarchy and chown to system UID. |
| 1017 | for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) { |
| 1018 | // We don't care about the lost+found directory. |
| 1019 | if (!strcmp(ftsent->fts_name, "lost+found")) { |
| 1020 | continue; |
| 1021 | } |
| 1022 | |
| 1023 | /* |
| 1024 | * There can only be one file marked as private right now. |
| 1025 | * This should be more robust, but it satisfies the requirements |
| 1026 | * we have for right now. |
| 1027 | */ |
| 1028 | const bool privateFile = !strcmp(ftsent->fts_name, filename); |
| 1029 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 1030 | int fd = open(ftsent->fts_accpath, O_NOFOLLOW | O_CLOEXEC); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1031 | if (fd < 0) { |
| 1032 | SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno)); |
| 1033 | result = -1; |
| 1034 | continue; |
| 1035 | } |
| 1036 | |
| 1037 | result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM); |
| 1038 | |
| 1039 | if (ftsent->fts_info & FTS_D) { |
Kenny Root | 1a673c8 | 2012-05-10 16:45:29 -0700 | [diff] [blame] | 1040 | result |= fchmod(fd, 0755); |
Kenny Root | 348c8ab | 2012-05-10 15:39:53 -0700 | [diff] [blame] | 1041 | } else if (ftsent->fts_info & FTS_F) { |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1042 | result |= fchmod(fd, privateFile ? 0640 : 0644); |
| 1043 | } |
Robert Craig | b9e3ba5 | 2014-02-04 10:53:00 -0500 | [diff] [blame] | 1044 | |
Stephen Smalley | 5093e61 | 2014-02-12 09:43:08 -0500 | [diff] [blame] | 1045 | if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) { |
Robert Craig | b9e3ba5 | 2014-02-04 10:53:00 -0500 | [diff] [blame] | 1046 | SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno)); |
| 1047 | result |= -1; |
| 1048 | } |
| 1049 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1050 | close(fd); |
| 1051 | } |
| 1052 | fts_close(fts); |
| 1053 | |
| 1054 | // Finally make the directory readable by everyone. |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 1055 | int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1056 | if (dirfd < 0 || fchmod(dirfd, 0755)) { |
| 1057 | SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno)); |
| 1058 | result |= -1; |
| 1059 | } |
| 1060 | close(dirfd); |
| 1061 | } else { |
| 1062 | result |= -1; |
| 1063 | } |
| 1064 | |
| 1065 | result |= Ext4::doMount(loopDevice, mountPoint, |
| 1066 | true /* read-only */, |
| 1067 | true /* remount */, |
| 1068 | true /* execute */); |
| 1069 | |
| 1070 | if (result) { |
| 1071 | SLOGE("ASEC fix permissions failed (%s)", strerror(errno)); |
| 1072 | return -1; |
| 1073 | } |
| 1074 | |
| 1075 | if (mDebug) { |
| 1076 | SLOGD("ASEC %s permissions fixed", id); |
| 1077 | } |
| 1078 | return 0; |
| 1079 | } |
| 1080 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1081 | int VolumeManager::renameAsec(const char *id1, const char *id2) { |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1082 | char asecFilename1[255]; |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1083 | char *asecFilename2; |
| 1084 | char mountPoint[255]; |
| 1085 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1086 | const char *dir; |
| 1087 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1088 | if (!isLegalAsecId(id1)) { |
| 1089 | SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1); |
| 1090 | errno = EINVAL; |
| 1091 | return -1; |
| 1092 | } |
| 1093 | |
| 1094 | if (!isLegalAsecId(id2)) { |
| 1095 | SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2); |
| 1096 | errno = EINVAL; |
| 1097 | return -1; |
| 1098 | } |
| 1099 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1100 | if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) { |
| 1101 | SLOGE("Couldn't find ASEC %s", id1); |
| 1102 | return -1; |
| 1103 | } |
| 1104 | |
| 1105 | asprintf(&asecFilename2, "%s/%s.asec", dir, id2); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1106 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1107 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id1); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1108 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1109 | SLOGE("Rename failed: couldn't construct mountpoint"); |
| 1110 | goto out_err; |
| 1111 | } |
| 1112 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1113 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1114 | SLOGW("Rename attempt when src mounted"); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1115 | errno = EBUSY; |
| 1116 | goto out_err; |
| 1117 | } |
| 1118 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1119 | written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id2); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1120 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1121 | SLOGE("Rename failed: couldn't construct mountpoint2"); |
| 1122 | goto out_err; |
| 1123 | } |
| 1124 | |
San Mehat | 96956ed | 2010-02-24 08:42:51 -0800 | [diff] [blame] | 1125 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1126 | SLOGW("Rename attempt when dst mounted"); |
San Mehat | 96956ed | 2010-02-24 08:42:51 -0800 | [diff] [blame] | 1127 | errno = EBUSY; |
| 1128 | goto out_err; |
| 1129 | } |
| 1130 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1131 | if (!access(asecFilename2, F_OK)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1132 | SLOGE("Rename attempt when dst exists"); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1133 | errno = EADDRINUSE; |
| 1134 | goto out_err; |
| 1135 | } |
| 1136 | |
| 1137 | if (rename(asecFilename1, asecFilename2)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1138 | SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno)); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1139 | goto out_err; |
| 1140 | } |
| 1141 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1142 | free(asecFilename2); |
| 1143 | return 0; |
| 1144 | |
| 1145 | out_err: |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1146 | free(asecFilename2); |
| 1147 | return -1; |
| 1148 | } |
| 1149 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1150 | #define UNMOUNT_RETRIES 5 |
| 1151 | #define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000) |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1152 | int VolumeManager::unmountAsec(const char *id, bool force) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1153 | char asecFileName[255]; |
| 1154 | char mountPoint[255]; |
| 1155 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1156 | if (!isLegalAsecId(id)) { |
| 1157 | SLOGE("unmountAsec: Invalid asec id \"%s\"", id); |
| 1158 | errno = EINVAL; |
| 1159 | return -1; |
| 1160 | } |
| 1161 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1162 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 1163 | SLOGE("Couldn't find ASEC %s", id); |
| 1164 | return -1; |
| 1165 | } |
| 1166 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1167 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1168 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1169 | SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id); |
| 1170 | return -1; |
| 1171 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1172 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1173 | char idHash[33]; |
| 1174 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1175 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1176 | return -1; |
| 1177 | } |
| 1178 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1179 | return unmountLoopImage(id, idHash, asecFileName, mountPoint, force); |
| 1180 | } |
| 1181 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1182 | int VolumeManager::unmountObb(const char *fileName, bool force) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1183 | char mountPoint[255]; |
| 1184 | |
| 1185 | char idHash[33]; |
| 1186 | if (!asecHash(fileName, idHash, sizeof(idHash))) { |
| 1187 | SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno)); |
| 1188 | return -1; |
| 1189 | } |
| 1190 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1191 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1192 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1193 | SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName); |
| 1194 | return -1; |
| 1195 | } |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1196 | |
| 1197 | return unmountLoopImage(fileName, idHash, fileName, mountPoint, force); |
| 1198 | } |
| 1199 | |
| 1200 | int VolumeManager::unmountLoopImage(const char *id, const char *idHash, |
| 1201 | const char *fileName, const char *mountPoint, bool force) { |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1202 | if (!isMountpointMounted(mountPoint)) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1203 | SLOGE("Unmount request for %s when not mounted", id); |
Kenny Root | 918e5f9 | 2010-09-30 18:00:52 -0700 | [diff] [blame] | 1204 | errno = ENOENT; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1205 | return -1; |
| 1206 | } |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 1207 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1208 | int i, rc; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1209 | for (i = 1; i <= UNMOUNT_RETRIES; i++) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1210 | rc = umount(mountPoint); |
| 1211 | if (!rc) { |
| 1212 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1213 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1214 | if (rc && (errno == EINVAL || errno == ENOENT)) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1215 | SLOGI("Container %s unmounted OK", id); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1216 | rc = 0; |
| 1217 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1218 | } |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1219 | SLOGW("%s unmount attempt %d failed (%s)", |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 1220 | id, i, strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1221 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1222 | int signal = 0; // default is to just complain |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1223 | |
| 1224 | if (force) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1225 | if (i > (UNMOUNT_RETRIES - 2)) |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1226 | signal = SIGKILL; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1227 | else if (i > (UNMOUNT_RETRIES - 3)) |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1228 | signal = SIGTERM; |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1229 | } |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 1230 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1231 | Process::killProcessesWithOpenFiles(mountPoint, signal); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1232 | usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | if (rc) { |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1236 | errno = EBUSY; |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1237 | SLOGE("Failed to unmount container %s (%s)", id, strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1238 | return -1; |
| 1239 | } |
| 1240 | |
San Mehat | 12f4b89 | 2010-02-24 11:43:22 -0800 | [diff] [blame] | 1241 | int retries = 10; |
| 1242 | |
| 1243 | while(retries--) { |
| 1244 | if (!rmdir(mountPoint)) { |
| 1245 | break; |
| 1246 | } |
| 1247 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1248 | SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno)); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1249 | usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS); |
San Mehat | 12f4b89 | 2010-02-24 11:43:22 -0800 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | if (!retries) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1253 | SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno)); |
San Mehat | f5c6198 | 2010-02-03 11:04:46 -0800 | [diff] [blame] | 1254 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1255 | |
Paul Lawrence | 60dec16 | 2014-09-02 10:52:15 -0700 | [diff] [blame] | 1256 | for (i=1; i <= UNMOUNT_RETRIES; i++) { |
| 1257 | if (Devmapper::destroy(idHash) && errno != ENXIO) { |
| 1258 | SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno)); |
| 1259 | usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS); |
| 1260 | continue; |
| 1261 | } else { |
| 1262 | break; |
| 1263 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | char loopDevice[255]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1267 | if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1268 | Loop::destroyByDevice(loopDevice); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1269 | } else { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1270 | SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1271 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1272 | |
| 1273 | AsecIdCollection::iterator it; |
| 1274 | for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) { |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1275 | ContainerData* cd = *it; |
| 1276 | if (!strcmp(cd->id, id)) { |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1277 | free(*it); |
| 1278 | mActiveContainers->erase(it); |
| 1279 | break; |
| 1280 | } |
| 1281 | } |
| 1282 | if (it == mActiveContainers->end()) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1283 | SLOGW("mActiveContainers is inconsistent!"); |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1284 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1285 | return 0; |
| 1286 | } |
| 1287 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1288 | int VolumeManager::destroyAsec(const char *id, bool force) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1289 | char asecFileName[255]; |
| 1290 | char mountPoint[255]; |
| 1291 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1292 | if (!isLegalAsecId(id)) { |
| 1293 | SLOGE("destroyAsec: Invalid asec id \"%s\"", id); |
| 1294 | errno = EINVAL; |
| 1295 | return -1; |
| 1296 | } |
| 1297 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1298 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 1299 | SLOGE("Couldn't find ASEC %s", id); |
| 1300 | return -1; |
| 1301 | } |
| 1302 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1303 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1304 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1305 | SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id); |
| 1306 | return -1; |
| 1307 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1308 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1309 | if (isMountpointMounted(mountPoint)) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1310 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1311 | SLOGD("Unmounting container before destroy"); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1312 | } |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1313 | if (unmountAsec(id, force)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1314 | SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno)); |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1315 | return -1; |
| 1316 | } |
| 1317 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1318 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1319 | if (unlink(asecFileName)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1320 | SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno)); |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1321 | return -1; |
| 1322 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1323 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1324 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1325 | SLOGD("ASEC %s destroyed", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1326 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1327 | return 0; |
| 1328 | } |
| 1329 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1330 | /* |
| 1331 | * Legal ASEC ids consist of alphanumeric characters, '-', |
| 1332 | * '_', or '.'. ".." is not allowed. The first or last character |
| 1333 | * of the ASEC id cannot be '.' (dot). |
| 1334 | */ |
| 1335 | bool VolumeManager::isLegalAsecId(const char *id) const { |
| 1336 | size_t i; |
| 1337 | size_t len = strlen(id); |
| 1338 | |
| 1339 | if (len == 0) { |
| 1340 | return false; |
| 1341 | } |
| 1342 | if ((id[0] == '.') || (id[len - 1] == '.')) { |
| 1343 | return false; |
| 1344 | } |
| 1345 | |
| 1346 | for (i = 0; i < len; i++) { |
| 1347 | if (id[i] == '.') { |
| 1348 | // i=0 is guaranteed never to have a dot. See above. |
| 1349 | if (id[i-1] == '.') return false; |
| 1350 | continue; |
| 1351 | } |
| 1352 | if (id[i] == '_' || id[i] == '-') continue; |
| 1353 | if (id[i] >= 'a' && id[i] <= 'z') continue; |
| 1354 | if (id[i] >= 'A' && id[i] <= 'Z') continue; |
| 1355 | if (id[i] >= '0' && id[i] <= '9') continue; |
| 1356 | return false; |
| 1357 | } |
| 1358 | |
| 1359 | return true; |
| 1360 | } |
| 1361 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1362 | bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 1363 | int dirfd = open(dir, O_DIRECTORY | O_CLOEXEC); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1364 | if (dirfd < 0) { |
| 1365 | SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno)); |
Nick Kralevich | 25e581a | 2015-02-06 08:55:08 -0800 | [diff] [blame] | 1366 | return false; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1367 | } |
| 1368 | |
Nick Kralevich | 25e581a | 2015-02-06 08:55:08 -0800 | [diff] [blame] | 1369 | struct stat sb; |
| 1370 | bool ret = (fstatat(dirfd, asecName, &sb, AT_SYMLINK_NOFOLLOW) == 0) |
| 1371 | && S_ISREG(sb.st_mode); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1372 | |
| 1373 | close(dirfd); |
| 1374 | |
| 1375 | return ret; |
| 1376 | } |
| 1377 | |
| 1378 | int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen, |
| 1379 | const char **directory) const { |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1380 | char *asecName; |
| 1381 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1382 | if (!isLegalAsecId(id)) { |
| 1383 | SLOGE("findAsec: Invalid asec id \"%s\"", id); |
| 1384 | errno = EINVAL; |
| 1385 | return -1; |
| 1386 | } |
| 1387 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1388 | if (asprintf(&asecName, "%s.asec", id) < 0) { |
| 1389 | SLOGE("Couldn't allocate string to write ASEC name"); |
| 1390 | return -1; |
| 1391 | } |
| 1392 | |
| 1393 | const char *dir; |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1394 | if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_INT, asecName)) { |
| 1395 | dir = VolumeManager::SEC_ASECDIR_INT; |
| 1396 | } else if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_EXT, asecName)) { |
| 1397 | dir = VolumeManager::SEC_ASECDIR_EXT; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1398 | } else { |
| 1399 | free(asecName); |
| 1400 | return -1; |
| 1401 | } |
| 1402 | |
| 1403 | if (directory != NULL) { |
| 1404 | *directory = dir; |
| 1405 | } |
| 1406 | |
| 1407 | if (asecPath != NULL) { |
| 1408 | int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1409 | if ((written < 0) || (size_t(written) >= asecPathLen)) { |
| 1410 | SLOGE("findAsec failed for %s: couldn't construct ASEC path", id); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1411 | free(asecName); |
| 1412 | return -1; |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | free(asecName); |
| 1417 | return 0; |
| 1418 | } |
| 1419 | |
Jeff Sharkey | 43ed123 | 2014-08-22 12:29:05 -0700 | [diff] [blame] | 1420 | 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] | 1421 | char asecFileName[255]; |
| 1422 | char mountPoint[255]; |
| 1423 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1424 | if (!isLegalAsecId(id)) { |
| 1425 | SLOGE("mountAsec: Invalid asec id \"%s\"", id); |
| 1426 | errno = EINVAL; |
| 1427 | return -1; |
| 1428 | } |
| 1429 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1430 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 1431 | SLOGE("Couldn't find ASEC %s", id); |
| 1432 | return -1; |
| 1433 | } |
| 1434 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1435 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1436 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
Colin Cross | 59846b6 | 2014-02-06 20:34:29 -0800 | [diff] [blame] | 1437 | SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1438 | return -1; |
| 1439 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1440 | |
| 1441 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1442 | SLOGE("ASEC %s already mounted", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1443 | errno = EBUSY; |
| 1444 | return -1; |
| 1445 | } |
| 1446 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1447 | char idHash[33]; |
| 1448 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1449 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1450 | return -1; |
| 1451 | } |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 1452 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1453 | char loopDevice[255]; |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1454 | if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug)) |
| 1455 | return -1; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1456 | |
| 1457 | char dmDevice[255]; |
| 1458 | bool cleanupDm = false; |
Tim Murray | 8439dc9 | 2014-12-15 11:56:11 -0800 | [diff] [blame] | 1459 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1460 | unsigned long nr_sec = 0; |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1461 | struct asec_superblock sb; |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1462 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1463 | if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) { |
| 1464 | return -1; |
| 1465 | } |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1466 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1467 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1468 | SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1469 | } |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1470 | if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1471 | SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1472 | Loop::destroyByDevice(loopDevice); |
| 1473 | errno = EMEDIUMTYPE; |
| 1474 | return -1; |
| 1475 | } |
| 1476 | nr_sec--; // We don't want the devmapping to extend onto our superblock |
| 1477 | |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1478 | if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) { |
| 1479 | Loop::destroyByDevice(loopDevice); |
| 1480 | return -1; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1481 | } |
| 1482 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1483 | if (mkdir(mountPoint, 0000)) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1484 | if (errno != EEXIST) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1485 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1486 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1487 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1488 | } |
| 1489 | Loop::destroyByDevice(loopDevice); |
| 1490 | return -1; |
| 1491 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1492 | } |
| 1493 | |
Kenny Root | cdc2a1c | 2012-05-03 13:49:46 -0700 | [diff] [blame] | 1494 | /* |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1495 | * Wait for the device mapper node to be created. |
Kenny Root | cdc2a1c | 2012-05-03 13:49:46 -0700 | [diff] [blame] | 1496 | */ |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1497 | waitForDevMapper(dmDevice); |
Kenny Root | cdc2a1c | 2012-05-03 13:49:46 -0700 | [diff] [blame] | 1498 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1499 | int result; |
| 1500 | if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) { |
Jeff Sharkey | 43ed123 | 2014-08-22 12:29:05 -0700 | [diff] [blame] | 1501 | result = Ext4::doMount(dmDevice, mountPoint, readOnly, false, readOnly); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1502 | } else { |
Jeff Sharkey | 43ed123 | 2014-08-22 12:29:05 -0700 | [diff] [blame] | 1503 | result = Fat::doMount(dmDevice, mountPoint, readOnly, false, readOnly, ownerUid, 0, 0222, false); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | if (result) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1507 | SLOGE("ASEC mount failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1508 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1509 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1510 | } |
| 1511 | Loop::destroyByDevice(loopDevice); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1512 | return -1; |
| 1513 | } |
| 1514 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1515 | mActiveContainers->push_back(new ContainerData(strdup(id), ASEC)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1516 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1517 | SLOGD("ASEC %s mounted", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1518 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1519 | return 0; |
| 1520 | } |
| 1521 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1522 | /** |
| 1523 | * Mounts an image file <code>img</code>. |
| 1524 | */ |
Jeff Sharkey | 6947904 | 2012-09-25 16:14:57 -0700 | [diff] [blame] | 1525 | int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1526 | char mountPoint[255]; |
| 1527 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1528 | char idHash[33]; |
| 1529 | if (!asecHash(img, idHash, sizeof(idHash))) { |
| 1530 | SLOGE("Hash of '%s' failed (%s)", img, strerror(errno)); |
| 1531 | return -1; |
| 1532 | } |
| 1533 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1534 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1535 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
Colin Cross | 59846b6 | 2014-02-06 20:34:29 -0800 | [diff] [blame] | 1536 | SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1537 | return -1; |
| 1538 | } |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1539 | |
| 1540 | if (isMountpointMounted(mountPoint)) { |
| 1541 | SLOGE("Image %s already mounted", img); |
| 1542 | errno = EBUSY; |
| 1543 | return -1; |
| 1544 | } |
| 1545 | |
| 1546 | char loopDevice[255]; |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1547 | if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug)) |
| 1548 | return -1; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1549 | |
| 1550 | char dmDevice[255]; |
| 1551 | bool cleanupDm = false; |
| 1552 | int fd; |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1553 | unsigned long nr_sec = 0; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1554 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 1555 | if ((fd = open(loopDevice, O_RDWR | O_CLOEXEC)) < 0) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1556 | SLOGE("Failed to open loopdevice (%s)", strerror(errno)); |
| 1557 | Loop::destroyByDevice(loopDevice); |
| 1558 | return -1; |
| 1559 | } |
| 1560 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1561 | get_blkdev_size(fd, &nr_sec); |
| 1562 | if (nr_sec == 0) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1563 | SLOGE("Failed to get loop size (%s)", strerror(errno)); |
| 1564 | Loop::destroyByDevice(loopDevice); |
| 1565 | close(fd); |
| 1566 | return -1; |
| 1567 | } |
| 1568 | |
| 1569 | close(fd); |
| 1570 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1571 | 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] | 1572 | Loop::destroyByDevice(loopDevice); |
| 1573 | return -1; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | if (mkdir(mountPoint, 0755)) { |
| 1577 | if (errno != EEXIST) { |
| 1578 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
| 1579 | if (cleanupDm) { |
| 1580 | Devmapper::destroy(idHash); |
| 1581 | } |
| 1582 | Loop::destroyByDevice(loopDevice); |
| 1583 | return -1; |
| 1584 | } |
| 1585 | } |
| 1586 | |
Jeff Sharkey | 6947904 | 2012-09-25 16:14:57 -0700 | [diff] [blame] | 1587 | if (Fat::doMount(dmDevice, mountPoint, true, false, true, 0, ownerGid, |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1588 | 0227, false)) { |
| 1589 | SLOGE("Image mount failed (%s)", strerror(errno)); |
| 1590 | if (cleanupDm) { |
| 1591 | Devmapper::destroy(idHash); |
| 1592 | } |
| 1593 | Loop::destroyByDevice(loopDevice); |
| 1594 | return -1; |
| 1595 | } |
| 1596 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1597 | mActiveContainers->push_back(new ContainerData(strdup(img), OBB)); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1598 | if (mDebug) { |
| 1599 | SLOGD("Image %s mounted", img); |
| 1600 | } |
| 1601 | return 0; |
| 1602 | } |
| 1603 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1604 | int VolumeManager::listMountedObbs(SocketClient* cli) { |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1605 | FILE *fp = setmntent("/proc/mounts", "r"); |
| 1606 | if (fp == NULL) { |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1607 | SLOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
| 1608 | return -1; |
| 1609 | } |
| 1610 | |
| 1611 | // Create a string to compare against that has a trailing slash |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1612 | int loopDirLen = strlen(VolumeManager::LOOPDIR); |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1613 | char loopDir[loopDirLen + 2]; |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1614 | strcpy(loopDir, VolumeManager::LOOPDIR); |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1615 | loopDir[loopDirLen++] = '/'; |
| 1616 | loopDir[loopDirLen] = '\0'; |
| 1617 | |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1618 | mntent* mentry; |
| 1619 | while ((mentry = getmntent(fp)) != NULL) { |
| 1620 | if (!strncmp(mentry->mnt_dir, loopDir, loopDirLen)) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 1621 | int fd = open(mentry->mnt_fsname, O_RDONLY | O_CLOEXEC); |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1622 | if (fd >= 0) { |
| 1623 | struct loop_info64 li; |
| 1624 | if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) { |
| 1625 | cli->sendMsg(ResponseCode::AsecListResult, |
| 1626 | (const char*) li.lo_file_name, false); |
| 1627 | } |
| 1628 | close(fd); |
| 1629 | } |
| 1630 | } |
| 1631 | } |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1632 | endmntent(fp); |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1633 | return 0; |
| 1634 | } |
| 1635 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1636 | extern "C" int vold_unmountAll(void) { |
Ken Sumrall | 425524d | 2012-06-14 20:55:28 -0700 | [diff] [blame] | 1637 | VolumeManager *vm = VolumeManager::Instance(); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1638 | return vm->unmountAll(); |
Ken Sumrall | 425524d | 2012-06-14 20:55:28 -0700 | [diff] [blame] | 1639 | } |
| 1640 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1641 | bool VolumeManager::isMountpointMounted(const char *mp) |
| 1642 | { |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1643 | FILE *fp = setmntent("/proc/mounts", "r"); |
| 1644 | if (fp == NULL) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1645 | SLOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1646 | return false; |
| 1647 | } |
| 1648 | |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1649 | bool found_mp = false; |
| 1650 | mntent* mentry; |
| 1651 | while ((mentry = getmntent(fp)) != NULL) { |
| 1652 | if (strcmp(mentry->mnt_dir, mp) == 0) { |
| 1653 | found_mp = true; |
| 1654 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1655 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1656 | } |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1657 | endmntent(fp); |
| 1658 | return found_mp; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1659 | } |
| 1660 | |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1661 | int VolumeManager::mkdirs(char* path) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1662 | // Only offer to create directories for paths managed by vold |
| 1663 | if (strncmp(path, "/storage/", 9) == 0) { |
| 1664 | // fs_mkdirs() does symlink checking and relative path enforcement |
| 1665 | return fs_mkdirs(path, 0700); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1666 | } else { |
Cylen Yao | 27cfee3 | 2014-05-02 19:23:42 +0800 | [diff] [blame] | 1667 | SLOGE("Failed to find mounted volume for %s", path); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1668 | return -EINVAL; |
| 1669 | } |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1670 | } |