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