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