San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 17 | #include <dirent.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 18 | #include <errno.h> |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 19 | #include <fcntl.h> |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 20 | #include <fts.h> |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 21 | #include <mntent.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/ioctl.h> |
| 26 | #include <sys/mount.h> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 29 | #include <unistd.h> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 30 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 31 | #include <linux/kdev_t.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 32 | |
| 33 | #define LOG_TAG "Vold" |
| 34 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 35 | #include <openssl/md5.h> |
| 36 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 37 | #include <base/logging.h> |
| 38 | #include <base/stringprintf.h> |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 39 | #include <cutils/fs.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 40 | #include <cutils/log.h> |
| 41 | |
Robert Craig | b9e3ba5 | 2014-02-04 10:53:00 -0500 | [diff] [blame] | 42 | #include <selinux/android.h> |
| 43 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 44 | #include <sysutils/NetlinkEvent.h> |
| 45 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 46 | #include <private/android_filesystem_config.h> |
| 47 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 48 | #include "EmulatedVolume.h" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 49 | #include "VolumeManager.h" |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 50 | #include "NetlinkManager.h" |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 51 | #include "DirectVolume.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" |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 54 | #include "Ext4.h" |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 55 | #include "Fat.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 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 63 | #define DEBUG_NETLINK 0 |
| 64 | |
Mike Lockwood | 97f2fc1 | 2011-06-07 10:51:38 -0700 | [diff] [blame] | 65 | #define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file" |
| 66 | |
Daniel Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 67 | #define ROUND_UP_POWER_OF_2(number, po2) (((!!(number & ((1U << po2) - 1))) << po2)\ |
| 68 | + (number & (~((1U << po2) - 1)))) |
| 69 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 70 | using android::base::StringPrintf; |
| 71 | |
| 72 | static const char* kUserMountPath = "/mnt/user"; |
| 73 | |
| 74 | static const unsigned int kMajorBlockScsi = 8; |
| 75 | static const unsigned int kMajorBlockMmc = 179; |
| 76 | |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 77 | /* writes superblock at end of file or device given by name */ |
| 78 | static int writeSuperBlock(const char* name, struct asec_superblock *sb, unsigned int numImgSectors) { |
| 79 | int sbfd = open(name, O_RDWR); |
| 80 | if (sbfd < 0) { |
| 81 | SLOGE("Failed to open %s for superblock write (%s)", name, strerror(errno)); |
| 82 | return -1; |
| 83 | } |
| 84 | |
| 85 | if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) { |
| 86 | SLOGE("Failed to lseek for superblock (%s)", strerror(errno)); |
| 87 | close(sbfd); |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | if (write(sbfd, sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) { |
| 92 | SLOGE("Failed to write superblock (%s)", strerror(errno)); |
| 93 | close(sbfd); |
| 94 | return -1; |
| 95 | } |
| 96 | close(sbfd); |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static int adjustSectorNumExt4(unsigned numSectors) { |
Daniel Rosenberg | e9196fe | 2014-06-10 17:16:03 -0700 | [diff] [blame] | 101 | // Ext4 started to reserve 2% or 4096 clusters, whichever is smaller for |
| 102 | // preventing costly operations or unexpected ENOSPC error. |
| 103 | // Ext4::format() uses default block size without clustering. |
| 104 | unsigned clusterSectors = 4096 / 512; |
| 105 | unsigned reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0); |
| 106 | numSectors += reservedSectors > (4096 * clusterSectors) ? (4096 * clusterSectors) : reservedSectors; |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 107 | return ROUND_UP_POWER_OF_2(numSectors, 3); |
| 108 | } |
| 109 | |
| 110 | static int adjustSectorNumFAT(unsigned numSectors) { |
| 111 | /* |
| 112 | * Add some headroom |
| 113 | */ |
| 114 | unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2; |
| 115 | numSectors += fatSize + 2; |
| 116 | /* |
| 117 | * FAT is aligned to 32 kb with 512b sectors. |
| 118 | */ |
| 119 | return ROUND_UP_POWER_OF_2(numSectors, 6); |
| 120 | } |
| 121 | |
| 122 | static int setupLoopDevice(char* buffer, size_t len, const char* asecFileName, const char* idHash, bool debug) { |
| 123 | if (Loop::lookupActive(idHash, buffer, len)) { |
| 124 | if (Loop::create(idHash, asecFileName, buffer, len)) { |
| 125 | SLOGE("ASEC loop device creation failed for %s (%s)", asecFileName, strerror(errno)); |
| 126 | return -1; |
| 127 | } |
| 128 | if (debug) { |
| 129 | SLOGD("New loop device created at %s", buffer); |
| 130 | } |
| 131 | } else { |
| 132 | if (debug) { |
| 133 | SLOGD("Found active loopback for %s at %s", asecFileName, buffer); |
| 134 | } |
| 135 | } |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | 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) { |
| 140 | if (strcmp(key, "none")) { |
| 141 | if (Devmapper::lookupActive(idHash, buffer, len)) { |
| 142 | if (Devmapper::create(idHash, loopDevice, key, numImgSectors, |
| 143 | buffer, len)) { |
| 144 | SLOGE("ASEC device mapping failed for %s (%s)", asecFileName, strerror(errno)); |
| 145 | return -1; |
| 146 | } |
| 147 | if (debug) { |
| 148 | SLOGD("New devmapper instance created at %s", buffer); |
| 149 | } |
| 150 | } else { |
| 151 | if (debug) { |
| 152 | SLOGD("Found active devmapper for %s at %s", asecFileName, buffer); |
| 153 | } |
| 154 | } |
| 155 | *createdDMDevice = true; |
| 156 | } else { |
| 157 | strcpy(buffer, loopDevice); |
| 158 | *createdDMDevice = false; |
| 159 | } |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | static void waitForDevMapper(const char *dmDevice) { |
| 164 | /* |
| 165 | * Wait for the device mapper node to be created. Sometimes it takes a |
| 166 | * while. Wait for up to 1 second. We could also inspect incoming uevents, |
| 167 | * but that would take more effort. |
| 168 | */ |
| 169 | int tries = 25; |
| 170 | while (tries--) { |
| 171 | if (!access(dmDevice, F_OK) || errno != ENOENT) { |
| 172 | break; |
| 173 | } |
| 174 | usleep(40 * 1000); |
| 175 | } |
| 176 | } |
| 177 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 178 | VolumeManager *VolumeManager::sInstance = NULL; |
| 179 | |
| 180 | VolumeManager *VolumeManager::Instance() { |
| 181 | if (!sInstance) |
| 182 | sInstance = new VolumeManager(); |
| 183 | return sInstance; |
| 184 | } |
| 185 | |
| 186 | VolumeManager::VolumeManager() { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 187 | mDebug = false; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 188 | mVolumes = new VolumeCollection(); |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 189 | mActiveContainers = new AsecIdCollection(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 190 | mBroadcaster = NULL; |
Mike Lockwood | a28056b | 2010-10-28 15:21:24 -0400 | [diff] [blame] | 191 | mUmsSharingCount = 0; |
| 192 | mSavedDirtyRatio = -1; |
| 193 | // set dirty ratio to 0 when UMS is active |
| 194 | mUmsDirtyRatio = 0; |
Ken Sumrall | 3b17005 | 2011-07-11 15:38:57 -0700 | [diff] [blame] | 195 | mVolManagerDisabled = 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | VolumeManager::~VolumeManager() { |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 199 | delete mVolumes; |
| 200 | delete mActiveContainers; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 203 | char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) { |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 204 | static const char* digits = "0123456789abcdef"; |
| 205 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 206 | unsigned char sig[MD5_DIGEST_LENGTH]; |
| 207 | |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 208 | if (buffer == NULL) { |
| 209 | SLOGE("Destination buffer is NULL"); |
| 210 | errno = ESPIPE; |
| 211 | return NULL; |
| 212 | } else if (id == NULL) { |
| 213 | SLOGE("Source buffer is NULL"); |
| 214 | errno = ESPIPE; |
| 215 | return NULL; |
| 216 | } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) { |
Colin Cross | 59846b6 | 2014-02-06 20:34:29 -0800 | [diff] [blame] | 217 | SLOGE("Target hash buffer size < %d bytes (%zu)", |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 218 | MD5_ASCII_LENGTH_PLUS_NULL, len); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 219 | errno = ESPIPE; |
| 220 | return NULL; |
| 221 | } |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 222 | |
| 223 | MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 224 | |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 225 | char *p = buffer; |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 226 | for (int i = 0; i < MD5_DIGEST_LENGTH; i++) { |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 227 | *p++ = digits[sig[i] >> 4]; |
| 228 | *p++ = digits[sig[i] & 0x0F]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 229 | } |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 230 | *p = '\0'; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 231 | |
| 232 | return buffer; |
| 233 | } |
| 234 | |
| 235 | void VolumeManager::setDebug(bool enable) { |
| 236 | mDebug = enable; |
| 237 | VolumeCollection::iterator it; |
| 238 | for (it = mVolumes->begin(); it != mVolumes->end(); ++it) { |
| 239 | (*it)->setDebug(enable); |
| 240 | } |
| 241 | } |
| 242 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 243 | int VolumeManager::start() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 244 | // Always start from a clean slate by unmounting everything in |
| 245 | // directories that we own, in case we crashed. |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame^] | 246 | unmountAll(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 247 | |
| 248 | // Assume that we always have an emulated volume on internal |
| 249 | // storage; the framework will decide if it should be mounted. |
| 250 | mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>( |
| 251 | new android::vold::EmulatedVolume("/data/media", "")); |
| 252 | mInternalEmulated->create(); |
| 253 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | int VolumeManager::stop() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 258 | mInternalEmulated->destroy(); |
| 259 | mInternalEmulated = nullptr; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | int VolumeManager::addVolume(Volume *v) { |
| 264 | mVolumes->push_back(v); |
| 265 | return 0; |
| 266 | } |
| 267 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 268 | void VolumeManager::handleBlockEvent(NetlinkEvent *evt) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 269 | #if DEBUG_NETLINK |
| 270 | LOG(VERBOSE) << "handleBlockEvent with action " << (int) evt->getAction(); |
| 271 | evt->dump(); |
Tim Murray | 8439dc9 | 2014-12-15 11:56:11 -0800 | [diff] [blame] | 272 | #endif |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 273 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 274 | std::string eventPath(evt->findParam("DEVPATH")); |
| 275 | std::string devType(evt->findParam("DEVTYPE")); |
| 276 | |
| 277 | if (devType != "disk") return; |
| 278 | |
| 279 | int major = atoi(evt->findParam("MAJOR")); |
| 280 | int minor = atoi(evt->findParam("MINOR")); |
| 281 | dev_t device = makedev(major, minor); |
| 282 | |
| 283 | switch (evt->getAction()) { |
| 284 | case NetlinkEvent::Action::kAdd: { |
| 285 | for (auto source : mDiskSources) { |
| 286 | if (source->matches(eventPath)) { |
| 287 | // For now, assume that MMC devices are SD, and that |
| 288 | // everything else is USB |
| 289 | int flags = source->getFlags(); |
| 290 | if (major == kMajorBlockMmc) { |
| 291 | flags |= android::vold::Disk::Flags::kSd; |
| 292 | } else { |
| 293 | flags |= android::vold::Disk::Flags::kUsb; |
| 294 | } |
| 295 | |
| 296 | auto disk = new android::vold::Disk(eventPath, device, |
| 297 | source->getNickname(), flags); |
| 298 | disk->create(); |
| 299 | mDisks.push_back(std::shared_ptr<android::vold::Disk>(disk)); |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 | break; |
| 304 | } |
| 305 | case NetlinkEvent::Action::kChange: { |
| 306 | for (auto disk : mDisks) { |
| 307 | if (disk->getDevice() == device) { |
| 308 | disk->readMetadata(); |
| 309 | disk->readPartitions(); |
| 310 | } |
| 311 | } |
| 312 | break; |
| 313 | } |
| 314 | case NetlinkEvent::Action::kRemove: { |
| 315 | auto i = mDisks.begin(); |
| 316 | while (i != mDisks.end()) { |
| 317 | if ((*i)->getDevice() == device) { |
| 318 | (*i)->destroy(); |
| 319 | i = mDisks.erase(i); |
| 320 | } else { |
| 321 | ++i; |
| 322 | } |
| 323 | } |
| 324 | break; |
| 325 | } |
| 326 | default: { |
| 327 | LOG(WARNING) << "Unexpected block event action " << (int) evt->getAction(); |
| 328 | break; |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) { |
| 334 | mDiskSources.push_back(diskSource); |
| 335 | } |
| 336 | |
| 337 | std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) { |
| 338 | for (auto disk : mDisks) { |
| 339 | if (disk->getId() == id) { |
| 340 | return disk; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 341 | } |
| 342 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 343 | return nullptr; |
| 344 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 345 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 346 | std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) { |
| 347 | if (mInternalEmulated->getId() == id) { |
| 348 | return mInternalEmulated; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 349 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 350 | for (auto disk : mDisks) { |
| 351 | auto vol = disk->findVolume(id); |
| 352 | if (vol != nullptr) { |
| 353 | return vol; |
| 354 | } |
| 355 | } |
| 356 | return nullptr; |
| 357 | } |
| 358 | |
| 359 | int VolumeManager::linkPrimary(userid_t userId) { |
| 360 | std::string source(mPrimary->getPath()); |
| 361 | if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) { |
| 362 | source = StringPrintf("%s/%d", source.c_str(), userId); |
| 363 | } |
| 364 | |
| 365 | std::string target(StringPrintf("/mnt/user/%d/primary", userId)); |
| 366 | if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) { |
| 367 | if (errno != ENOENT) { |
| 368 | SLOGW("Failed to unlink %s: %s", target.c_str(), strerror(errno)); |
| 369 | } |
| 370 | } |
| 371 | if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) { |
| 372 | SLOGW("Failed to link %s to %s: %s", source.c_str(), target.c_str(), |
| 373 | strerror(errno)); |
| 374 | return -errno; |
| 375 | } |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | int VolumeManager::startUser(userid_t userId) { |
| 380 | // Note that sometimes the system will spin up processes from Zygote |
| 381 | // before actually starting the user, so we're okay if Zygote |
| 382 | // already created this directory. |
| 383 | std::string path(StringPrintf("%s/%d", kUserMountPath, userId)); |
| 384 | fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT); |
| 385 | |
| 386 | mUsers.push_back(userId); |
| 387 | if (mPrimary) { |
| 388 | linkPrimary(userId); |
| 389 | } |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | int VolumeManager::cleanupUser(userid_t userId) { |
| 394 | mUsers.remove(userId); |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) { |
| 399 | mPrimary = vol; |
| 400 | for (userid_t userId : mUsers) { |
| 401 | linkPrimary(userId); |
| 402 | } |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | int VolumeManager::reset() { |
| 407 | // Tear down all existing disks/volumes and start from a blank slate so |
| 408 | // newly connected framework hears all events. |
| 409 | mInternalEmulated->destroy(); |
| 410 | mInternalEmulated->create(); |
| 411 | for (auto disk : mDisks) { |
| 412 | disk->destroy(); |
| 413 | disk->create(); |
| 414 | } |
| 415 | mUsers.clear(); |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | int VolumeManager::shutdown() { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame^] | 420 | mInternalEmulated->destroy(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 421 | for (auto disk : mDisks) { |
| 422 | disk->destroy(); |
| 423 | } |
| 424 | mDisks.clear(); |
| 425 | return 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame^] | 428 | int VolumeManager::unmountAll() { |
| 429 | // First, try gracefully unmounting all known devices |
| 430 | if (mInternalEmulated != nullptr) { |
| 431 | mInternalEmulated->unmount(); |
| 432 | } |
| 433 | for (auto disk : mDisks) { |
| 434 | disk->unmountAll(); |
| 435 | } |
| 436 | |
| 437 | // Worst case we might have some stale mounts lurking around, so |
| 438 | // force unmount those just to be safe. |
| 439 | FILE* fp = setmntent("/proc/mounts", "r"); |
| 440 | if (fp == NULL) { |
| 441 | SLOGE("Error opening /proc/mounts: %s", strerror(errno)); |
| 442 | return -errno; |
| 443 | } |
| 444 | |
| 445 | // Some volumes can be stacked on each other, so force unmount in |
| 446 | // reverse order to give us the best chance of success. |
| 447 | std::list<std::string> toUnmount; |
| 448 | mntent* mentry; |
| 449 | while ((mentry = getmntent(fp)) != NULL) { |
| 450 | if (strncmp(mentry->mnt_dir, "/mnt/", 5) == 0 |
| 451 | || strncmp(mentry->mnt_dir, "/storage/", 9) == 0) { |
| 452 | toUnmount.push_front(std::string(mentry->mnt_dir)); |
| 453 | } |
| 454 | } |
| 455 | endmntent(fp); |
| 456 | |
| 457 | for (auto path : toUnmount) { |
| 458 | SLOGW("Tearing down stale mount %s", path.c_str()); |
| 459 | android::vold::ForceUnmount(path); |
| 460 | } |
| 461 | |
| 462 | return 0; |
| 463 | } |
| 464 | |
JP Abgrall | 40b64a6 | 2014-07-24 18:02:16 -0700 | [diff] [blame] | 465 | int VolumeManager::listVolumes(SocketClient *cli, bool broadcast) { |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 466 | VolumeCollection::iterator i; |
JP Abgrall | 40b64a6 | 2014-07-24 18:02:16 -0700 | [diff] [blame] | 467 | char msg[256]; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 468 | |
| 469 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
| 470 | char *buffer; |
| 471 | asprintf(&buffer, "%s %s %d", |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 472 | (*i)->getLabel(), (*i)->getFuseMountpoint(), |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 473 | (*i)->getState()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 474 | cli->sendMsg(ResponseCode::VolumeListResult, buffer, false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 475 | free(buffer); |
JP Abgrall | 40b64a6 | 2014-07-24 18:02:16 -0700 | [diff] [blame] | 476 | if (broadcast) { |
| 477 | if((*i)->getUuid()) { |
| 478 | snprintf(msg, sizeof(msg), "%s %s \"%s\"", (*i)->getLabel(), |
| 479 | (*i)->getFuseMountpoint(), (*i)->getUuid()); |
| 480 | mBroadcaster->sendBroadcast(ResponseCode::VolumeUuidChange, |
| 481 | msg, false); |
| 482 | } |
| 483 | if((*i)->getUserLabel()) { |
| 484 | snprintf(msg, sizeof(msg), "%s %s \"%s\"", (*i)->getLabel(), |
| 485 | (*i)->getFuseMountpoint(), (*i)->getUserLabel()); |
| 486 | mBroadcaster->sendBroadcast(ResponseCode::VolumeUserLabelChange, |
| 487 | msg, false); |
| 488 | } |
| 489 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 490 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 491 | cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 492 | return 0; |
| 493 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 494 | |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 495 | int VolumeManager::formatVolume(const char *label, bool wipe) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 496 | Volume *v = lookupVolume(label); |
| 497 | |
| 498 | if (!v) { |
| 499 | errno = ENOENT; |
| 500 | return -1; |
| 501 | } |
| 502 | |
Ken Sumrall | 3b17005 | 2011-07-11 15:38:57 -0700 | [diff] [blame] | 503 | if (mVolManagerDisabled) { |
| 504 | errno = EBUSY; |
| 505 | return -1; |
| 506 | } |
| 507 | |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 508 | return v->formatVol(wipe); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 509 | } |
| 510 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 511 | int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) { |
| 512 | char idHash[33]; |
| 513 | if (!asecHash(sourceFile, idHash, sizeof(idHash))) { |
| 514 | SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno)); |
| 515 | return -1; |
| 516 | } |
| 517 | |
| 518 | memset(mountPath, 0, mountPathLen); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 519 | int written = snprintf(mountPath, mountPathLen, "%s/%s", Volume::LOOPDIR, idHash); |
| 520 | if ((written < 0) || (written >= mountPathLen)) { |
| 521 | errno = EINVAL; |
| 522 | return -1; |
| 523 | } |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 524 | |
| 525 | if (access(mountPath, F_OK)) { |
| 526 | errno = ENOENT; |
| 527 | return -1; |
| 528 | } |
| 529 | |
| 530 | return 0; |
| 531 | } |
| 532 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 533 | int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) { |
San Mehat | 88ac2c0 | 2010-03-23 11:15:58 -0700 | [diff] [blame] | 534 | char asecFileName[255]; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 535 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 536 | if (!isLegalAsecId(id)) { |
| 537 | SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id); |
| 538 | errno = EINVAL; |
| 539 | return -1; |
| 540 | } |
| 541 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 542 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 543 | SLOGE("Couldn't find ASEC %s", id); |
| 544 | return -1; |
| 545 | } |
San Mehat | 88ac2c0 | 2010-03-23 11:15:58 -0700 | [diff] [blame] | 546 | |
| 547 | memset(buffer, 0, maxlen); |
| 548 | if (access(asecFileName, F_OK)) { |
| 549 | errno = ENOENT; |
| 550 | return -1; |
| 551 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 552 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 553 | int written = snprintf(buffer, maxlen, "%s/%s", Volume::ASECDIR, id); |
| 554 | if ((written < 0) || (written >= maxlen)) { |
| 555 | SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id); |
| 556 | errno = EINVAL; |
| 557 | return -1; |
| 558 | } |
| 559 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 560 | return 0; |
| 561 | } |
| 562 | |
Dianne Hackborn | 736910c | 2011-06-27 13:37:07 -0700 | [diff] [blame] | 563 | int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) { |
| 564 | char asecFileName[255]; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 565 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 566 | if (!isLegalAsecId(id)) { |
| 567 | SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id); |
| 568 | errno = EINVAL; |
| 569 | return -1; |
| 570 | } |
| 571 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 572 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 573 | SLOGE("Couldn't find ASEC %s", id); |
| 574 | return -1; |
| 575 | } |
Dianne Hackborn | 736910c | 2011-06-27 13:37:07 -0700 | [diff] [blame] | 576 | |
| 577 | memset(buffer, 0, maxlen); |
| 578 | if (access(asecFileName, F_OK)) { |
| 579 | errno = ENOENT; |
| 580 | return -1; |
| 581 | } |
| 582 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 583 | int written = snprintf(buffer, maxlen, "%s", asecFileName); |
| 584 | if ((written < 0) || (written >= maxlen)) { |
| 585 | errno = EINVAL; |
| 586 | return -1; |
| 587 | } |
| 588 | |
Dianne Hackborn | 736910c | 2011-06-27 13:37:07 -0700 | [diff] [blame] | 589 | return 0; |
| 590 | } |
| 591 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 592 | int VolumeManager::createAsec(const char *id, unsigned int numSectors, const char *fstype, |
| 593 | const char *key, const int ownerUid, bool isExternal) { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 594 | struct asec_superblock sb; |
| 595 | memset(&sb, 0, sizeof(sb)); |
| 596 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 597 | if (!isLegalAsecId(id)) { |
| 598 | SLOGE("createAsec: Invalid asec id \"%s\"", id); |
| 599 | errno = EINVAL; |
| 600 | return -1; |
| 601 | } |
| 602 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 603 | const bool wantFilesystem = strcmp(fstype, "none"); |
| 604 | bool usingExt4 = false; |
| 605 | if (wantFilesystem) { |
| 606 | usingExt4 = !strcmp(fstype, "ext4"); |
| 607 | if (usingExt4) { |
| 608 | sb.c_opts |= ASEC_SB_C_OPTS_EXT4; |
| 609 | } else if (strcmp(fstype, "fat")) { |
| 610 | SLOGE("Invalid filesystem type %s", fstype); |
| 611 | errno = EINVAL; |
| 612 | return -1; |
| 613 | } |
| 614 | } |
| 615 | |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 616 | sb.magic = ASEC_SB_MAGIC; |
| 617 | sb.ver = ASEC_SB_VER; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 618 | |
San Mehat | d31e380 | 2010-02-18 08:37:45 -0800 | [diff] [blame] | 619 | if (numSectors < ((1024*1024)/512)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 620 | SLOGE("Invalid container size specified (%d sectors)", numSectors); |
San Mehat | d31e380 | 2010-02-18 08:37:45 -0800 | [diff] [blame] | 621 | errno = EINVAL; |
| 622 | return -1; |
| 623 | } |
| 624 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 625 | if (lookupVolume(id)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 626 | SLOGE("ASEC id '%s' currently exists", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 627 | errno = EADDRINUSE; |
| 628 | return -1; |
| 629 | } |
| 630 | |
| 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 | |
| 640 | const char *asecDir = isExternal ? Volume::SEC_ASECDIR_EXT : Volume::SEC_ASECDIR_INT; |
| 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 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 716 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
| 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) { |
Daniel Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 728 | formatStatus = Ext4::format(dmDevice, numImgSectors, mountPoint); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 729 | } else { |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 730 | formatStatus = Fat::format(dmDevice, numImgSectors, 0); |
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) { |
| 757 | mountStatus = Ext4::doMount(dmDevice, mountPoint, false, false, false); |
| 758 | } else { |
| 759 | mountStatus = Fat::doMount(dmDevice, mountPoint, false, false, false, ownerUid, 0, 0000, |
| 760 | false); |
| 761 | } |
| 762 | |
| 763 | if (mountStatus) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 764 | SLOGE("ASEC FAT mount failed (%s)", strerror(errno)); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 765 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 766 | Devmapper::destroy(idHash); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 767 | } |
| 768 | Loop::destroyByDevice(loopDevice); |
| 769 | unlink(asecFileName); |
| 770 | return -1; |
| 771 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 772 | |
| 773 | if (usingExt4) { |
| 774 | int dirfd = open(mountPoint, O_DIRECTORY); |
| 775 | if (dirfd >= 0) { |
| 776 | if (fchown(dirfd, ownerUid, AID_SYSTEM) |
| 777 | || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) { |
| 778 | SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint); |
| 779 | } |
| 780 | close(dirfd); |
| 781 | } |
| 782 | } |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 783 | } else { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 784 | SLOGI("Created raw secure container %s (no filesystem)", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 785 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 786 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 787 | mActiveContainers->push_back(new ContainerData(strdup(id), ASEC)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 788 | return 0; |
| 789 | } |
| 790 | |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 791 | int VolumeManager::resizeAsec(const char *id, unsigned numSectors, const char *key) { |
| 792 | char asecFileName[255]; |
| 793 | char mountPoint[255]; |
| 794 | bool cleanupDm = false; |
| 795 | |
| 796 | if (!isLegalAsecId(id)) { |
| 797 | SLOGE("resizeAsec: Invalid asec id \"%s\"", id); |
| 798 | errno = EINVAL; |
| 799 | return -1; |
| 800 | } |
| 801 | |
| 802 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 803 | SLOGE("Couldn't find ASEC %s", id); |
| 804 | return -1; |
| 805 | } |
| 806 | |
| 807 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
| 808 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 809 | SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id); |
| 810 | return -1; |
| 811 | } |
| 812 | |
| 813 | if (isMountpointMounted(mountPoint)) { |
| 814 | SLOGE("ASEC %s mounted. Unmount before resizing", id); |
| 815 | errno = EBUSY; |
| 816 | return -1; |
| 817 | } |
| 818 | |
| 819 | struct asec_superblock sb; |
| 820 | int fd; |
| 821 | unsigned int oldNumSec = 0; |
| 822 | |
| 823 | if ((fd = open(asecFileName, O_RDONLY)) < 0) { |
| 824 | SLOGE("Failed to open ASEC file (%s)", strerror(errno)); |
| 825 | return -1; |
| 826 | } |
| 827 | |
| 828 | struct stat info; |
| 829 | if (fstat(fd, &info) < 0) { |
| 830 | SLOGE("Failed to get file size (%s)", strerror(errno)); |
| 831 | close(fd); |
| 832 | return -1; |
| 833 | } |
| 834 | |
| 835 | oldNumSec = info.st_size / 512; |
| 836 | |
| 837 | unsigned numImgSectors; |
| 838 | if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) |
| 839 | numImgSectors = adjustSectorNumExt4(numSectors); |
| 840 | else |
| 841 | numImgSectors = adjustSectorNumFAT(numSectors); |
| 842 | /* |
| 843 | * add one block for the superblock |
| 844 | */ |
| 845 | SLOGD("Resizing from %d sectors to %d sectors", oldNumSec, numImgSectors + 1); |
Jeff Sharkey | 43ed123 | 2014-08-22 12:29:05 -0700 | [diff] [blame] | 846 | if (oldNumSec == numImgSectors + 1) { |
| 847 | SLOGW("Size unchanged; ignoring resize request"); |
| 848 | return 0; |
| 849 | } else if (oldNumSec > numImgSectors + 1) { |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 850 | SLOGE("Only growing is currently supported."); |
| 851 | close(fd); |
| 852 | return -1; |
| 853 | } |
| 854 | |
| 855 | /* |
| 856 | * Try to read superblock. |
| 857 | */ |
| 858 | memset(&sb, 0, sizeof(struct asec_superblock)); |
| 859 | if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) { |
| 860 | SLOGE("lseek failed (%s)", strerror(errno)); |
| 861 | close(fd); |
| 862 | return -1; |
| 863 | } |
| 864 | if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) { |
| 865 | SLOGE("superblock read failed (%s)", strerror(errno)); |
| 866 | close(fd); |
| 867 | return -1; |
| 868 | } |
| 869 | close(fd); |
| 870 | |
| 871 | if (mDebug) { |
| 872 | SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver); |
| 873 | } |
| 874 | if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) { |
| 875 | SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver); |
| 876 | errno = EMEDIUMTYPE; |
| 877 | return -1; |
| 878 | } |
| 879 | |
| 880 | if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) { |
| 881 | SLOGE("Only ext4 partitions are supported for resize"); |
| 882 | errno = EINVAL; |
| 883 | return -1; |
| 884 | } |
| 885 | |
| 886 | if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) { |
| 887 | SLOGE("Resize of ASEC image file failed. Could not resize %s", id); |
| 888 | return -1; |
| 889 | } |
| 890 | |
| 891 | /* |
| 892 | * Drop down a copy of the superblock at the end of the file |
| 893 | */ |
| 894 | if (writeSuperBlock(asecFileName, &sb, numImgSectors)) |
| 895 | goto fail; |
| 896 | |
| 897 | char idHash[33]; |
| 898 | if (!asecHash(id, idHash, sizeof(idHash))) { |
| 899 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
| 900 | goto fail; |
| 901 | } |
| 902 | |
| 903 | char loopDevice[255]; |
| 904 | if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug)) |
| 905 | goto fail; |
| 906 | |
| 907 | char dmDevice[255]; |
| 908 | |
| 909 | if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) { |
| 910 | Loop::destroyByDevice(loopDevice); |
| 911 | goto fail; |
| 912 | } |
| 913 | |
| 914 | /* |
| 915 | * Wait for the device mapper node to be created. |
| 916 | */ |
| 917 | waitForDevMapper(dmDevice); |
| 918 | |
| 919 | if (Ext4::resize(dmDevice, numImgSectors)) { |
| 920 | SLOGE("Unable to resize %s (%s)", id, strerror(errno)); |
| 921 | if (cleanupDm) { |
| 922 | Devmapper::destroy(idHash); |
| 923 | } |
| 924 | Loop::destroyByDevice(loopDevice); |
| 925 | goto fail; |
| 926 | } |
| 927 | |
| 928 | return 0; |
| 929 | fail: |
| 930 | Loop::resizeImageFile(asecFileName, oldNumSec); |
| 931 | return -1; |
| 932 | } |
| 933 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 934 | int VolumeManager::finalizeAsec(const char *id) { |
| 935 | char asecFileName[255]; |
| 936 | char loopDevice[255]; |
| 937 | char mountPoint[255]; |
| 938 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 939 | if (!isLegalAsecId(id)) { |
| 940 | SLOGE("finalizeAsec: Invalid asec id \"%s\"", id); |
| 941 | errno = EINVAL; |
| 942 | return -1; |
| 943 | } |
| 944 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 945 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 946 | SLOGE("Couldn't find ASEC %s", id); |
| 947 | return -1; |
| 948 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 949 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 950 | char idHash[33]; |
| 951 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 952 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 953 | return -1; |
| 954 | } |
| 955 | |
| 956 | if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 957 | SLOGE("Unable to finalize %s (%s)", id, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 958 | return -1; |
| 959 | } |
| 960 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 961 | unsigned long nr_sec = 0; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 962 | struct asec_superblock sb; |
| 963 | |
| 964 | if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) { |
| 965 | return -1; |
| 966 | } |
| 967 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 968 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
| 969 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 970 | SLOGE("ASEC finalize failed: couldn't construct mountPoint"); |
| 971 | return -1; |
| 972 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 973 | |
| 974 | int result = 0; |
| 975 | if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) { |
| 976 | result = Ext4::doMount(loopDevice, mountPoint, true, true, true); |
| 977 | } else { |
| 978 | result = Fat::doMount(loopDevice, mountPoint, true, true, true, 0, 0, 0227, false); |
| 979 | } |
| 980 | |
| 981 | if (result) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 982 | SLOGE("ASEC finalize mount failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 983 | return -1; |
| 984 | } |
| 985 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 986 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 987 | SLOGD("ASEC %s finalized", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 988 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 989 | return 0; |
| 990 | } |
| 991 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 992 | int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) { |
| 993 | char asecFileName[255]; |
| 994 | char loopDevice[255]; |
| 995 | char mountPoint[255]; |
| 996 | |
| 997 | if (gid < AID_APP) { |
| 998 | SLOGE("Group ID is not in application range"); |
| 999 | return -1; |
| 1000 | } |
| 1001 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1002 | if (!isLegalAsecId(id)) { |
| 1003 | SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id); |
| 1004 | errno = EINVAL; |
| 1005 | return -1; |
| 1006 | } |
| 1007 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1008 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 1009 | SLOGE("Couldn't find ASEC %s", id); |
| 1010 | return -1; |
| 1011 | } |
| 1012 | |
| 1013 | char idHash[33]; |
| 1014 | if (!asecHash(id, idHash, sizeof(idHash))) { |
| 1015 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
| 1016 | return -1; |
| 1017 | } |
| 1018 | |
| 1019 | if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
| 1020 | SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno)); |
| 1021 | return -1; |
| 1022 | } |
| 1023 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1024 | unsigned long nr_sec = 0; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1025 | struct asec_superblock sb; |
| 1026 | |
| 1027 | if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) { |
| 1028 | return -1; |
| 1029 | } |
| 1030 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1031 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
| 1032 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1033 | SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id); |
| 1034 | return -1; |
| 1035 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1036 | |
| 1037 | int result = 0; |
| 1038 | if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) { |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
| 1042 | int ret = Ext4::doMount(loopDevice, mountPoint, |
| 1043 | false /* read-only */, |
| 1044 | true /* remount */, |
| 1045 | false /* executable */); |
| 1046 | if (ret) { |
| 1047 | SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno)); |
| 1048 | return -1; |
| 1049 | } |
| 1050 | |
| 1051 | char *paths[] = { mountPoint, NULL }; |
| 1052 | |
| 1053 | FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL); |
| 1054 | if (fts) { |
| 1055 | // Traverse the entire hierarchy and chown to system UID. |
| 1056 | for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) { |
| 1057 | // We don't care about the lost+found directory. |
| 1058 | if (!strcmp(ftsent->fts_name, "lost+found")) { |
| 1059 | continue; |
| 1060 | } |
| 1061 | |
| 1062 | /* |
| 1063 | * There can only be one file marked as private right now. |
| 1064 | * This should be more robust, but it satisfies the requirements |
| 1065 | * we have for right now. |
| 1066 | */ |
| 1067 | const bool privateFile = !strcmp(ftsent->fts_name, filename); |
| 1068 | |
| 1069 | int fd = open(ftsent->fts_accpath, O_NOFOLLOW); |
| 1070 | if (fd < 0) { |
| 1071 | SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno)); |
| 1072 | result = -1; |
| 1073 | continue; |
| 1074 | } |
| 1075 | |
| 1076 | result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM); |
| 1077 | |
| 1078 | if (ftsent->fts_info & FTS_D) { |
Kenny Root | 1a673c8 | 2012-05-10 16:45:29 -0700 | [diff] [blame] | 1079 | result |= fchmod(fd, 0755); |
Kenny Root | 348c8ab | 2012-05-10 15:39:53 -0700 | [diff] [blame] | 1080 | } else if (ftsent->fts_info & FTS_F) { |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1081 | result |= fchmod(fd, privateFile ? 0640 : 0644); |
| 1082 | } |
Robert Craig | b9e3ba5 | 2014-02-04 10:53:00 -0500 | [diff] [blame] | 1083 | |
Stephen Smalley | 5093e61 | 2014-02-12 09:43:08 -0500 | [diff] [blame] | 1084 | if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) { |
Robert Craig | b9e3ba5 | 2014-02-04 10:53:00 -0500 | [diff] [blame] | 1085 | SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno)); |
| 1086 | result |= -1; |
| 1087 | } |
| 1088 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1089 | close(fd); |
| 1090 | } |
| 1091 | fts_close(fts); |
| 1092 | |
| 1093 | // Finally make the directory readable by everyone. |
| 1094 | int dirfd = open(mountPoint, O_DIRECTORY); |
| 1095 | if (dirfd < 0 || fchmod(dirfd, 0755)) { |
| 1096 | SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno)); |
| 1097 | result |= -1; |
| 1098 | } |
| 1099 | close(dirfd); |
| 1100 | } else { |
| 1101 | result |= -1; |
| 1102 | } |
| 1103 | |
| 1104 | result |= Ext4::doMount(loopDevice, mountPoint, |
| 1105 | true /* read-only */, |
| 1106 | true /* remount */, |
| 1107 | true /* execute */); |
| 1108 | |
| 1109 | if (result) { |
| 1110 | SLOGE("ASEC fix permissions failed (%s)", strerror(errno)); |
| 1111 | return -1; |
| 1112 | } |
| 1113 | |
| 1114 | if (mDebug) { |
| 1115 | SLOGD("ASEC %s permissions fixed", id); |
| 1116 | } |
| 1117 | return 0; |
| 1118 | } |
| 1119 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1120 | int VolumeManager::renameAsec(const char *id1, const char *id2) { |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1121 | char asecFilename1[255]; |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1122 | char *asecFilename2; |
| 1123 | char mountPoint[255]; |
| 1124 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1125 | const char *dir; |
| 1126 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1127 | if (!isLegalAsecId(id1)) { |
| 1128 | SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1); |
| 1129 | errno = EINVAL; |
| 1130 | return -1; |
| 1131 | } |
| 1132 | |
| 1133 | if (!isLegalAsecId(id2)) { |
| 1134 | SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2); |
| 1135 | errno = EINVAL; |
| 1136 | return -1; |
| 1137 | } |
| 1138 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1139 | if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) { |
| 1140 | SLOGE("Couldn't find ASEC %s", id1); |
| 1141 | return -1; |
| 1142 | } |
| 1143 | |
| 1144 | asprintf(&asecFilename2, "%s/%s.asec", dir, id2); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1145 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1146 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id1); |
| 1147 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1148 | SLOGE("Rename failed: couldn't construct mountpoint"); |
| 1149 | goto out_err; |
| 1150 | } |
| 1151 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1152 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1153 | SLOGW("Rename attempt when src mounted"); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1154 | errno = EBUSY; |
| 1155 | goto out_err; |
| 1156 | } |
| 1157 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1158 | written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id2); |
| 1159 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1160 | SLOGE("Rename failed: couldn't construct mountpoint2"); |
| 1161 | goto out_err; |
| 1162 | } |
| 1163 | |
San Mehat | 96956ed | 2010-02-24 08:42:51 -0800 | [diff] [blame] | 1164 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1165 | SLOGW("Rename attempt when dst mounted"); |
San Mehat | 96956ed | 2010-02-24 08:42:51 -0800 | [diff] [blame] | 1166 | errno = EBUSY; |
| 1167 | goto out_err; |
| 1168 | } |
| 1169 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1170 | if (!access(asecFilename2, F_OK)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1171 | SLOGE("Rename attempt when dst exists"); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1172 | errno = EADDRINUSE; |
| 1173 | goto out_err; |
| 1174 | } |
| 1175 | |
| 1176 | if (rename(asecFilename1, asecFilename2)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1177 | SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno)); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1178 | goto out_err; |
| 1179 | } |
| 1180 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1181 | free(asecFilename2); |
| 1182 | return 0; |
| 1183 | |
| 1184 | out_err: |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1185 | free(asecFilename2); |
| 1186 | return -1; |
| 1187 | } |
| 1188 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1189 | #define UNMOUNT_RETRIES 5 |
| 1190 | #define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000) |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1191 | int VolumeManager::unmountAsec(const char *id, bool force) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1192 | char asecFileName[255]; |
| 1193 | char mountPoint[255]; |
| 1194 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1195 | if (!isLegalAsecId(id)) { |
| 1196 | SLOGE("unmountAsec: Invalid asec id \"%s\"", id); |
| 1197 | errno = EINVAL; |
| 1198 | return -1; |
| 1199 | } |
| 1200 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1201 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 1202 | SLOGE("Couldn't find ASEC %s", id); |
| 1203 | return -1; |
| 1204 | } |
| 1205 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1206 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
| 1207 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1208 | SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id); |
| 1209 | return -1; |
| 1210 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1211 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1212 | char idHash[33]; |
| 1213 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1214 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1215 | return -1; |
| 1216 | } |
| 1217 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1218 | return unmountLoopImage(id, idHash, asecFileName, mountPoint, force); |
| 1219 | } |
| 1220 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1221 | int VolumeManager::unmountObb(const char *fileName, bool force) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1222 | char mountPoint[255]; |
| 1223 | |
| 1224 | char idHash[33]; |
| 1225 | if (!asecHash(fileName, idHash, sizeof(idHash))) { |
| 1226 | SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno)); |
| 1227 | return -1; |
| 1228 | } |
| 1229 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1230 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash); |
| 1231 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1232 | SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName); |
| 1233 | return -1; |
| 1234 | } |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1235 | |
| 1236 | return unmountLoopImage(fileName, idHash, fileName, mountPoint, force); |
| 1237 | } |
| 1238 | |
| 1239 | int VolumeManager::unmountLoopImage(const char *id, const char *idHash, |
| 1240 | const char *fileName, const char *mountPoint, bool force) { |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1241 | if (!isMountpointMounted(mountPoint)) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1242 | SLOGE("Unmount request for %s when not mounted", id); |
Kenny Root | 918e5f9 | 2010-09-30 18:00:52 -0700 | [diff] [blame] | 1243 | errno = ENOENT; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1244 | return -1; |
| 1245 | } |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 1246 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1247 | int i, rc; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1248 | for (i = 1; i <= UNMOUNT_RETRIES; i++) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1249 | rc = umount(mountPoint); |
| 1250 | if (!rc) { |
| 1251 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1252 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1253 | if (rc && (errno == EINVAL || errno == ENOENT)) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1254 | SLOGI("Container %s unmounted OK", id); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1255 | rc = 0; |
| 1256 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1257 | } |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1258 | SLOGW("%s unmount attempt %d failed (%s)", |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 1259 | id, i, strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1260 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1261 | int signal = 0; // default is to just complain |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1262 | |
| 1263 | if (force) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1264 | if (i > (UNMOUNT_RETRIES - 2)) |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1265 | signal = SIGKILL; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1266 | else if (i > (UNMOUNT_RETRIES - 3)) |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1267 | signal = SIGTERM; |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1268 | } |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 1269 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1270 | Process::killProcessesWithOpenFiles(mountPoint, signal); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1271 | usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | if (rc) { |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1275 | errno = EBUSY; |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1276 | SLOGE("Failed to unmount container %s (%s)", id, strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1277 | return -1; |
| 1278 | } |
| 1279 | |
San Mehat | 12f4b89 | 2010-02-24 11:43:22 -0800 | [diff] [blame] | 1280 | int retries = 10; |
| 1281 | |
| 1282 | while(retries--) { |
| 1283 | if (!rmdir(mountPoint)) { |
| 1284 | break; |
| 1285 | } |
| 1286 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1287 | SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno)); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1288 | usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS); |
San Mehat | 12f4b89 | 2010-02-24 11:43:22 -0800 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | if (!retries) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1292 | SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno)); |
San Mehat | f5c6198 | 2010-02-03 11:04:46 -0800 | [diff] [blame] | 1293 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1294 | |
Paul Lawrence | 60dec16 | 2014-09-02 10:52:15 -0700 | [diff] [blame] | 1295 | for (i=1; i <= UNMOUNT_RETRIES; i++) { |
| 1296 | if (Devmapper::destroy(idHash) && errno != ENXIO) { |
| 1297 | SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno)); |
| 1298 | usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS); |
| 1299 | continue; |
| 1300 | } else { |
| 1301 | break; |
| 1302 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | char loopDevice[255]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1306 | if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1307 | Loop::destroyByDevice(loopDevice); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1308 | } else { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1309 | SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1310 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1311 | |
| 1312 | AsecIdCollection::iterator it; |
| 1313 | for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) { |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1314 | ContainerData* cd = *it; |
| 1315 | if (!strcmp(cd->id, id)) { |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1316 | free(*it); |
| 1317 | mActiveContainers->erase(it); |
| 1318 | break; |
| 1319 | } |
| 1320 | } |
| 1321 | if (it == mActiveContainers->end()) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1322 | SLOGW("mActiveContainers is inconsistent!"); |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1323 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1324 | return 0; |
| 1325 | } |
| 1326 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1327 | int VolumeManager::destroyAsec(const char *id, bool force) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1328 | char asecFileName[255]; |
| 1329 | char mountPoint[255]; |
| 1330 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1331 | if (!isLegalAsecId(id)) { |
| 1332 | SLOGE("destroyAsec: Invalid asec id \"%s\"", id); |
| 1333 | errno = EINVAL; |
| 1334 | return -1; |
| 1335 | } |
| 1336 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1337 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 1338 | SLOGE("Couldn't find ASEC %s", id); |
| 1339 | return -1; |
| 1340 | } |
| 1341 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1342 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
| 1343 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1344 | SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id); |
| 1345 | return -1; |
| 1346 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1347 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1348 | if (isMountpointMounted(mountPoint)) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1349 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1350 | SLOGD("Unmounting container before destroy"); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1351 | } |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1352 | if (unmountAsec(id, force)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1353 | SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno)); |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1354 | return -1; |
| 1355 | } |
| 1356 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1357 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1358 | if (unlink(asecFileName)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1359 | SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno)); |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1360 | return -1; |
| 1361 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1362 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1363 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1364 | SLOGD("ASEC %s destroyed", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1365 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1366 | return 0; |
| 1367 | } |
| 1368 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1369 | /* |
| 1370 | * Legal ASEC ids consist of alphanumeric characters, '-', |
| 1371 | * '_', or '.'. ".." is not allowed. The first or last character |
| 1372 | * of the ASEC id cannot be '.' (dot). |
| 1373 | */ |
| 1374 | bool VolumeManager::isLegalAsecId(const char *id) const { |
| 1375 | size_t i; |
| 1376 | size_t len = strlen(id); |
| 1377 | |
| 1378 | if (len == 0) { |
| 1379 | return false; |
| 1380 | } |
| 1381 | if ((id[0] == '.') || (id[len - 1] == '.')) { |
| 1382 | return false; |
| 1383 | } |
| 1384 | |
| 1385 | for (i = 0; i < len; i++) { |
| 1386 | if (id[i] == '.') { |
| 1387 | // i=0 is guaranteed never to have a dot. See above. |
| 1388 | if (id[i-1] == '.') return false; |
| 1389 | continue; |
| 1390 | } |
| 1391 | if (id[i] == '_' || id[i] == '-') continue; |
| 1392 | if (id[i] >= 'a' && id[i] <= 'z') continue; |
| 1393 | if (id[i] >= 'A' && id[i] <= 'Z') continue; |
| 1394 | if (id[i] >= '0' && id[i] <= '9') continue; |
| 1395 | return false; |
| 1396 | } |
| 1397 | |
| 1398 | return true; |
| 1399 | } |
| 1400 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1401 | bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const { |
| 1402 | int dirfd = open(dir, O_DIRECTORY); |
| 1403 | if (dirfd < 0) { |
| 1404 | SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno)); |
Nick Kralevich | 25e581a | 2015-02-06 08:55:08 -0800 | [diff] [blame] | 1405 | return false; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1406 | } |
| 1407 | |
Nick Kralevich | 25e581a | 2015-02-06 08:55:08 -0800 | [diff] [blame] | 1408 | struct stat sb; |
| 1409 | bool ret = (fstatat(dirfd, asecName, &sb, AT_SYMLINK_NOFOLLOW) == 0) |
| 1410 | && S_ISREG(sb.st_mode); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1411 | |
| 1412 | close(dirfd); |
| 1413 | |
| 1414 | return ret; |
| 1415 | } |
| 1416 | |
| 1417 | int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen, |
| 1418 | const char **directory) const { |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1419 | char *asecName; |
| 1420 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1421 | if (!isLegalAsecId(id)) { |
| 1422 | SLOGE("findAsec: Invalid asec id \"%s\"", id); |
| 1423 | errno = EINVAL; |
| 1424 | return -1; |
| 1425 | } |
| 1426 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1427 | if (asprintf(&asecName, "%s.asec", id) < 0) { |
| 1428 | SLOGE("Couldn't allocate string to write ASEC name"); |
| 1429 | return -1; |
| 1430 | } |
| 1431 | |
| 1432 | const char *dir; |
| 1433 | if (isAsecInDirectory(Volume::SEC_ASECDIR_INT, asecName)) { |
| 1434 | dir = Volume::SEC_ASECDIR_INT; |
| 1435 | } else if (isAsecInDirectory(Volume::SEC_ASECDIR_EXT, asecName)) { |
| 1436 | dir = Volume::SEC_ASECDIR_EXT; |
| 1437 | } else { |
| 1438 | free(asecName); |
| 1439 | return -1; |
| 1440 | } |
| 1441 | |
| 1442 | if (directory != NULL) { |
| 1443 | *directory = dir; |
| 1444 | } |
| 1445 | |
| 1446 | if (asecPath != NULL) { |
| 1447 | int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1448 | if ((written < 0) || (size_t(written) >= asecPathLen)) { |
| 1449 | SLOGE("findAsec failed for %s: couldn't construct ASEC path", id); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1450 | free(asecName); |
| 1451 | return -1; |
| 1452 | } |
| 1453 | } |
| 1454 | |
| 1455 | free(asecName); |
| 1456 | return 0; |
| 1457 | } |
| 1458 | |
Jeff Sharkey | 43ed123 | 2014-08-22 12:29:05 -0700 | [diff] [blame] | 1459 | 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] | 1460 | char asecFileName[255]; |
| 1461 | char mountPoint[255]; |
| 1462 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1463 | if (!isLegalAsecId(id)) { |
| 1464 | SLOGE("mountAsec: Invalid asec id \"%s\"", id); |
| 1465 | errno = EINVAL; |
| 1466 | return -1; |
| 1467 | } |
| 1468 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1469 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 1470 | SLOGE("Couldn't find ASEC %s", id); |
| 1471 | return -1; |
| 1472 | } |
| 1473 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1474 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
| 1475 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
Colin Cross | 59846b6 | 2014-02-06 20:34:29 -0800 | [diff] [blame] | 1476 | SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1477 | return -1; |
| 1478 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1479 | |
| 1480 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1481 | SLOGE("ASEC %s already mounted", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1482 | errno = EBUSY; |
| 1483 | return -1; |
| 1484 | } |
| 1485 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1486 | char idHash[33]; |
| 1487 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1488 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1489 | return -1; |
| 1490 | } |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 1491 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1492 | char loopDevice[255]; |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1493 | if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug)) |
| 1494 | return -1; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1495 | |
| 1496 | char dmDevice[255]; |
| 1497 | bool cleanupDm = false; |
Tim Murray | 8439dc9 | 2014-12-15 11:56:11 -0800 | [diff] [blame] | 1498 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1499 | unsigned long nr_sec = 0; |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1500 | struct asec_superblock sb; |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1501 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1502 | if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) { |
| 1503 | return -1; |
| 1504 | } |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1505 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1506 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1507 | SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1508 | } |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1509 | if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1510 | SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1511 | Loop::destroyByDevice(loopDevice); |
| 1512 | errno = EMEDIUMTYPE; |
| 1513 | return -1; |
| 1514 | } |
| 1515 | nr_sec--; // We don't want the devmapping to extend onto our superblock |
| 1516 | |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1517 | if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) { |
| 1518 | Loop::destroyByDevice(loopDevice); |
| 1519 | return -1; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1520 | } |
| 1521 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1522 | if (mkdir(mountPoint, 0000)) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1523 | if (errno != EEXIST) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1524 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1525 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1526 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1527 | } |
| 1528 | Loop::destroyByDevice(loopDevice); |
| 1529 | return -1; |
| 1530 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1531 | } |
| 1532 | |
Kenny Root | cdc2a1c | 2012-05-03 13:49:46 -0700 | [diff] [blame] | 1533 | /* |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1534 | * Wait for the device mapper node to be created. |
Kenny Root | cdc2a1c | 2012-05-03 13:49:46 -0700 | [diff] [blame] | 1535 | */ |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1536 | waitForDevMapper(dmDevice); |
Kenny Root | cdc2a1c | 2012-05-03 13:49:46 -0700 | [diff] [blame] | 1537 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1538 | int result; |
| 1539 | if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) { |
Jeff Sharkey | 43ed123 | 2014-08-22 12:29:05 -0700 | [diff] [blame] | 1540 | result = Ext4::doMount(dmDevice, mountPoint, readOnly, false, readOnly); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1541 | } else { |
Jeff Sharkey | 43ed123 | 2014-08-22 12:29:05 -0700 | [diff] [blame] | 1542 | result = Fat::doMount(dmDevice, mountPoint, readOnly, false, readOnly, ownerUid, 0, 0222, false); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | if (result) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1546 | SLOGE("ASEC mount failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1547 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1548 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1549 | } |
| 1550 | Loop::destroyByDevice(loopDevice); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1551 | return -1; |
| 1552 | } |
| 1553 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1554 | mActiveContainers->push_back(new ContainerData(strdup(id), ASEC)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1555 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1556 | SLOGD("ASEC %s mounted", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1557 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1558 | return 0; |
| 1559 | } |
| 1560 | |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 1561 | Volume* VolumeManager::getVolumeForFile(const char *fileName) { |
| 1562 | VolumeCollection::iterator i; |
| 1563 | |
| 1564 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 1565 | const char* mountPoint = (*i)->getFuseMountpoint(); |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 1566 | if (!strncmp(fileName, mountPoint, strlen(mountPoint))) { |
| 1567 | return *i; |
| 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | return NULL; |
| 1572 | } |
| 1573 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1574 | /** |
| 1575 | * Mounts an image file <code>img</code>. |
| 1576 | */ |
Jeff Sharkey | 6947904 | 2012-09-25 16:14:57 -0700 | [diff] [blame] | 1577 | int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1578 | char mountPoint[255]; |
| 1579 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1580 | char idHash[33]; |
| 1581 | if (!asecHash(img, idHash, sizeof(idHash))) { |
| 1582 | SLOGE("Hash of '%s' failed (%s)", img, strerror(errno)); |
| 1583 | return -1; |
| 1584 | } |
| 1585 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1586 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash); |
| 1587 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
Colin Cross | 59846b6 | 2014-02-06 20:34:29 -0800 | [diff] [blame] | 1588 | SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1589 | return -1; |
| 1590 | } |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1591 | |
| 1592 | if (isMountpointMounted(mountPoint)) { |
| 1593 | SLOGE("Image %s already mounted", img); |
| 1594 | errno = EBUSY; |
| 1595 | return -1; |
| 1596 | } |
| 1597 | |
| 1598 | char loopDevice[255]; |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1599 | if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug)) |
| 1600 | return -1; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1601 | |
| 1602 | char dmDevice[255]; |
| 1603 | bool cleanupDm = false; |
| 1604 | int fd; |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1605 | unsigned long nr_sec = 0; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1606 | |
| 1607 | if ((fd = open(loopDevice, O_RDWR)) < 0) { |
| 1608 | SLOGE("Failed to open loopdevice (%s)", strerror(errno)); |
| 1609 | Loop::destroyByDevice(loopDevice); |
| 1610 | return -1; |
| 1611 | } |
| 1612 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1613 | get_blkdev_size(fd, &nr_sec); |
| 1614 | if (nr_sec == 0) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1615 | SLOGE("Failed to get loop size (%s)", strerror(errno)); |
| 1616 | Loop::destroyByDevice(loopDevice); |
| 1617 | close(fd); |
| 1618 | return -1; |
| 1619 | } |
| 1620 | |
| 1621 | close(fd); |
| 1622 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1623 | 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] | 1624 | Loop::destroyByDevice(loopDevice); |
| 1625 | return -1; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | if (mkdir(mountPoint, 0755)) { |
| 1629 | if (errno != EEXIST) { |
| 1630 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
| 1631 | if (cleanupDm) { |
| 1632 | Devmapper::destroy(idHash); |
| 1633 | } |
| 1634 | Loop::destroyByDevice(loopDevice); |
| 1635 | return -1; |
| 1636 | } |
| 1637 | } |
| 1638 | |
Jeff Sharkey | 6947904 | 2012-09-25 16:14:57 -0700 | [diff] [blame] | 1639 | if (Fat::doMount(dmDevice, mountPoint, true, false, true, 0, ownerGid, |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1640 | 0227, false)) { |
| 1641 | SLOGE("Image mount failed (%s)", strerror(errno)); |
| 1642 | if (cleanupDm) { |
| 1643 | Devmapper::destroy(idHash); |
| 1644 | } |
| 1645 | Loop::destroyByDevice(loopDevice); |
| 1646 | return -1; |
| 1647 | } |
| 1648 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1649 | mActiveContainers->push_back(new ContainerData(strdup(img), OBB)); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1650 | if (mDebug) { |
| 1651 | SLOGD("Image %s mounted", img); |
| 1652 | } |
| 1653 | return 0; |
| 1654 | } |
| 1655 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1656 | int VolumeManager::mountVolume(const char *label) { |
| 1657 | Volume *v = lookupVolume(label); |
| 1658 | |
| 1659 | if (!v) { |
| 1660 | errno = ENOENT; |
| 1661 | return -1; |
| 1662 | } |
| 1663 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1664 | return v->mountVol(); |
| 1665 | } |
| 1666 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1667 | int VolumeManager::listMountedObbs(SocketClient* cli) { |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1668 | FILE *fp = setmntent("/proc/mounts", "r"); |
| 1669 | if (fp == NULL) { |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1670 | SLOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
| 1671 | return -1; |
| 1672 | } |
| 1673 | |
| 1674 | // Create a string to compare against that has a trailing slash |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 1675 | int loopDirLen = strlen(Volume::LOOPDIR); |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1676 | char loopDir[loopDirLen + 2]; |
| 1677 | strcpy(loopDir, Volume::LOOPDIR); |
| 1678 | loopDir[loopDirLen++] = '/'; |
| 1679 | loopDir[loopDirLen] = '\0'; |
| 1680 | |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1681 | mntent* mentry; |
| 1682 | while ((mentry = getmntent(fp)) != NULL) { |
| 1683 | if (!strncmp(mentry->mnt_dir, loopDir, loopDirLen)) { |
| 1684 | int fd = open(mentry->mnt_fsname, O_RDONLY); |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1685 | if (fd >= 0) { |
| 1686 | struct loop_info64 li; |
| 1687 | if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) { |
| 1688 | cli->sendMsg(ResponseCode::AsecListResult, |
| 1689 | (const char*) li.lo_file_name, false); |
| 1690 | } |
| 1691 | close(fd); |
| 1692 | } |
| 1693 | } |
| 1694 | } |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1695 | endmntent(fp); |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1696 | return 0; |
| 1697 | } |
| 1698 | |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 1699 | int VolumeManager::shareEnabled(const char *label, const char *method, bool *enabled) { |
| 1700 | Volume *v = lookupVolume(label); |
| 1701 | |
| 1702 | if (!v) { |
| 1703 | errno = ENOENT; |
| 1704 | return -1; |
| 1705 | } |
| 1706 | |
| 1707 | if (strcmp(method, "ums")) { |
| 1708 | errno = ENOSYS; |
| 1709 | return -1; |
| 1710 | } |
| 1711 | |
| 1712 | if (v->getState() != Volume::State_Shared) { |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 1713 | *enabled = false; |
San Mehat | b9aed74 | 2010-02-04 15:07:01 -0800 | [diff] [blame] | 1714 | } else { |
| 1715 | *enabled = true; |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 1716 | } |
| 1717 | return 0; |
| 1718 | } |
| 1719 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1720 | int VolumeManager::shareVolume(const char *label, const char *method) { |
| 1721 | Volume *v = lookupVolume(label); |
| 1722 | |
| 1723 | if (!v) { |
| 1724 | errno = ENOENT; |
| 1725 | return -1; |
| 1726 | } |
| 1727 | |
| 1728 | /* |
| 1729 | * Eventually, we'll want to support additional share back-ends, |
| 1730 | * some of which may work while the media is mounted. For now, |
| 1731 | * we just support UMS |
| 1732 | */ |
| 1733 | if (strcmp(method, "ums")) { |
| 1734 | errno = ENOSYS; |
| 1735 | return -1; |
| 1736 | } |
| 1737 | |
| 1738 | if (v->getState() == Volume::State_NoMedia) { |
| 1739 | errno = ENODEV; |
| 1740 | return -1; |
| 1741 | } |
| 1742 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1743 | if (v->getState() != Volume::State_Idle) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1744 | // You need to unmount manually befoe sharing |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1745 | errno = EBUSY; |
| 1746 | return -1; |
| 1747 | } |
| 1748 | |
Ken Sumrall | 3b17005 | 2011-07-11 15:38:57 -0700 | [diff] [blame] | 1749 | if (mVolManagerDisabled) { |
| 1750 | errno = EBUSY; |
| 1751 | return -1; |
| 1752 | } |
| 1753 | |
Mike Lockwood | 2dfe297 | 2010-09-17 18:50:51 -0400 | [diff] [blame] | 1754 | dev_t d = v->getShareDevice(); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1755 | if ((MAJOR(d) == 0) && (MINOR(d) == 0)) { |
| 1756 | // This volume does not support raw disk access |
| 1757 | errno = EINVAL; |
| 1758 | return -1; |
| 1759 | } |
| 1760 | |
| 1761 | int fd; |
| 1762 | char nodepath[255]; |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1763 | int written = snprintf(nodepath, |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1764 | sizeof(nodepath), "/dev/block/vold/%d:%d", |
Colin Cross | 346c5b2 | 2014-01-22 23:59:41 -0800 | [diff] [blame] | 1765 | major(d), minor(d)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1766 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1767 | if ((written < 0) || (size_t(written) >= sizeof(nodepath))) { |
| 1768 | SLOGE("shareVolume failed: couldn't construct nodepath"); |
| 1769 | return -1; |
| 1770 | } |
| 1771 | |
Mike Lockwood | 97f2fc1 | 2011-06-07 10:51:38 -0700 | [diff] [blame] | 1772 | if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1773 | SLOGE("Unable to open ums lunfile (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1774 | return -1; |
| 1775 | } |
| 1776 | |
| 1777 | if (write(fd, nodepath, strlen(nodepath)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1778 | SLOGE("Unable to write to ums lunfile (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1779 | close(fd); |
| 1780 | return -1; |
| 1781 | } |
| 1782 | |
| 1783 | close(fd); |
| 1784 | v->handleVolumeShared(); |
Mike Lockwood | a28056b | 2010-10-28 15:21:24 -0400 | [diff] [blame] | 1785 | if (mUmsSharingCount++ == 0) { |
| 1786 | FILE* fp; |
| 1787 | mSavedDirtyRatio = -1; // in case we fail |
| 1788 | if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) { |
| 1789 | char line[16]; |
| 1790 | if (fgets(line, sizeof(line), fp) && sscanf(line, "%d", &mSavedDirtyRatio)) { |
| 1791 | fprintf(fp, "%d\n", mUmsDirtyRatio); |
| 1792 | } else { |
| 1793 | SLOGE("Failed to read dirty_ratio (%s)", strerror(errno)); |
| 1794 | } |
| 1795 | fclose(fp); |
| 1796 | } else { |
| 1797 | SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno)); |
| 1798 | } |
| 1799 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1800 | return 0; |
| 1801 | } |
| 1802 | |
| 1803 | int VolumeManager::unshareVolume(const char *label, const char *method) { |
| 1804 | Volume *v = lookupVolume(label); |
| 1805 | |
| 1806 | if (!v) { |
| 1807 | errno = ENOENT; |
| 1808 | return -1; |
| 1809 | } |
| 1810 | |
| 1811 | if (strcmp(method, "ums")) { |
| 1812 | errno = ENOSYS; |
| 1813 | return -1; |
| 1814 | } |
| 1815 | |
| 1816 | if (v->getState() != Volume::State_Shared) { |
| 1817 | errno = EINVAL; |
| 1818 | return -1; |
| 1819 | } |
| 1820 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1821 | int fd; |
Mike Lockwood | 97f2fc1 | 2011-06-07 10:51:38 -0700 | [diff] [blame] | 1822 | if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1823 | SLOGE("Unable to open ums lunfile (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1824 | return -1; |
| 1825 | } |
| 1826 | |
| 1827 | char ch = 0; |
| 1828 | if (write(fd, &ch, 1) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1829 | SLOGE("Unable to write to ums lunfile (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1830 | close(fd); |
| 1831 | return -1; |
| 1832 | } |
| 1833 | |
| 1834 | close(fd); |
| 1835 | v->handleVolumeUnshared(); |
Mike Lockwood | a28056b | 2010-10-28 15:21:24 -0400 | [diff] [blame] | 1836 | if (--mUmsSharingCount == 0 && mSavedDirtyRatio != -1) { |
| 1837 | FILE* fp; |
| 1838 | if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) { |
| 1839 | fprintf(fp, "%d\n", mSavedDirtyRatio); |
| 1840 | fclose(fp); |
| 1841 | } else { |
| 1842 | SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno)); |
| 1843 | } |
| 1844 | mSavedDirtyRatio = -1; |
| 1845 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1846 | return 0; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1847 | } |
| 1848 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1849 | int VolumeManager::getDirectVolumeList(struct volume_info *vol_list) { |
| 1850 | VolumeCollection::iterator i; |
| 1851 | int n=0; |
| 1852 | dev_t d; |
| 1853 | |
| 1854 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
| 1855 | if ((d=(*i)->getShareDevice()) != (dev_t)0) { |
| 1856 | (*i)->getVolInfo(&vol_list[n]); |
| 1857 | snprintf(vol_list[n].blk_dev, sizeof(vol_list[n].blk_dev), |
Colin Cross | 346c5b2 | 2014-01-22 23:59:41 -0800 | [diff] [blame] | 1858 | "/dev/block/vold/%d:%d", major(d), minor(d)); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1859 | n++; |
| 1860 | } |
| 1861 | } |
| 1862 | |
| 1863 | return 0; |
| 1864 | } |
| 1865 | |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 1866 | int VolumeManager::unmountVolume(const char *label, bool force, bool revert) { |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1867 | Volume *v = lookupVolume(label); |
| 1868 | |
| 1869 | if (!v) { |
| 1870 | errno = ENOENT; |
| 1871 | return -1; |
| 1872 | } |
| 1873 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1874 | if (v->getState() == Volume::State_NoMedia) { |
| 1875 | errno = ENODEV; |
| 1876 | return -1; |
| 1877 | } |
| 1878 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1879 | if (v->getState() != Volume::State_Mounted) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1880 | SLOGW("Attempt to unmount volume which isn't mounted (%d)\n", |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1881 | v->getState()); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1882 | errno = EBUSY; |
Ken Sumrall | 319b104 | 2011-06-14 14:01:55 -0700 | [diff] [blame] | 1883 | return UNMOUNT_NOT_MOUNTED_ERR; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1884 | } |
| 1885 | |
San Mehat | 1a06eda | 2010-04-15 12:58:50 -0700 | [diff] [blame] | 1886 | cleanupAsec(v, force); |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1887 | |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 1888 | return v->unmountVol(force, revert); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1889 | } |
| 1890 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame^] | 1891 | extern "C" int vold_unmountAll(void) { |
Ken Sumrall | 425524d | 2012-06-14 20:55:28 -0700 | [diff] [blame] | 1892 | VolumeManager *vm = VolumeManager::Instance(); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame^] | 1893 | return vm->unmountAll(); |
Ken Sumrall | 425524d | 2012-06-14 20:55:28 -0700 | [diff] [blame] | 1894 | } |
| 1895 | |
| 1896 | #define ID_BUF_LEN 256 |
| 1897 | #define ASEC_SUFFIX ".asec" |
| 1898 | #define ASEC_SUFFIX_LEN (sizeof(ASEC_SUFFIX) - 1) |
| 1899 | int VolumeManager::unmountAllAsecsInDir(const char *directory) { |
| 1900 | DIR *d = opendir(directory); |
| 1901 | int rc = 0; |
| 1902 | |
| 1903 | if (!d) { |
| 1904 | SLOGE("Could not open asec dir %s", directory); |
| 1905 | return -1; |
| 1906 | } |
| 1907 | |
| 1908 | size_t dirent_len = offsetof(struct dirent, d_name) + |
Elliott Hughes | 8c480f7 | 2012-10-26 16:57:19 -0700 | [diff] [blame] | 1909 | fpathconf(dirfd(d), _PC_NAME_MAX) + 1; |
Ken Sumrall | 425524d | 2012-06-14 20:55:28 -0700 | [diff] [blame] | 1910 | |
| 1911 | struct dirent *dent = (struct dirent *) malloc(dirent_len); |
| 1912 | if (dent == NULL) { |
| 1913 | SLOGE("Failed to allocate memory for asec dir"); |
| 1914 | return -1; |
| 1915 | } |
| 1916 | |
| 1917 | struct dirent *result; |
| 1918 | while (!readdir_r(d, dent, &result) && result != NULL) { |
| 1919 | if (dent->d_name[0] == '.') |
| 1920 | continue; |
| 1921 | if (dent->d_type != DT_REG) |
| 1922 | continue; |
| 1923 | size_t name_len = strlen(dent->d_name); |
| 1924 | if (name_len > 5 && name_len < (ID_BUF_LEN + ASEC_SUFFIX_LEN - 1) && |
| 1925 | !strcmp(&dent->d_name[name_len - 5], ASEC_SUFFIX)) { |
| 1926 | char id[ID_BUF_LEN]; |
| 1927 | strlcpy(id, dent->d_name, name_len - 4); |
| 1928 | if (unmountAsec(id, true)) { |
| 1929 | /* Register the error, but try to unmount more asecs */ |
| 1930 | rc = -1; |
| 1931 | } |
| 1932 | } |
| 1933 | } |
| 1934 | closedir(d); |
| 1935 | |
| 1936 | free(dent); |
| 1937 | |
| 1938 | return rc; |
| 1939 | } |
| 1940 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1941 | /* |
| 1942 | * Looks up a volume by it's label or mount-point |
| 1943 | */ |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1944 | Volume *VolumeManager::lookupVolume(const char *label) { |
| 1945 | VolumeCollection::iterator i; |
| 1946 | |
| 1947 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1948 | if (label[0] == '/') { |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 1949 | if (!strcmp(label, (*i)->getFuseMountpoint())) |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1950 | return (*i); |
| 1951 | } else { |
| 1952 | if (!strcmp(label, (*i)->getLabel())) |
| 1953 | return (*i); |
| 1954 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1955 | } |
| 1956 | return NULL; |
| 1957 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1958 | |
| 1959 | bool VolumeManager::isMountpointMounted(const char *mp) |
| 1960 | { |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1961 | FILE *fp = setmntent("/proc/mounts", "r"); |
| 1962 | if (fp == NULL) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1963 | SLOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1964 | return false; |
| 1965 | } |
| 1966 | |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1967 | bool found_mp = false; |
| 1968 | mntent* mentry; |
| 1969 | while ((mentry = getmntent(fp)) != NULL) { |
| 1970 | if (strcmp(mentry->mnt_dir, mp) == 0) { |
| 1971 | found_mp = true; |
| 1972 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1973 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1974 | } |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1975 | endmntent(fp); |
| 1976 | return found_mp; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1977 | } |
| 1978 | |
San Mehat | 1a06eda | 2010-04-15 12:58:50 -0700 | [diff] [blame] | 1979 | int VolumeManager::cleanupAsec(Volume *v, bool force) { |
Jeff Sharkey | 8c2c15b | 2013-10-17 15:17:19 -0700 | [diff] [blame] | 1980 | int rc = 0; |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 1981 | |
Jeff Sharkey | 8c2c15b | 2013-10-17 15:17:19 -0700 | [diff] [blame] | 1982 | char asecFileName[255]; |
| 1983 | |
| 1984 | AsecIdCollection removeAsec; |
| 1985 | AsecIdCollection removeObb; |
| 1986 | |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 1987 | for (AsecIdCollection::iterator it = mActiveContainers->begin(); it != mActiveContainers->end(); |
| 1988 | ++it) { |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1989 | ContainerData* cd = *it; |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 1990 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1991 | if (cd->type == ASEC) { |
Jeff Sharkey | 8c2c15b | 2013-10-17 15:17:19 -0700 | [diff] [blame] | 1992 | if (findAsec(cd->id, asecFileName, sizeof(asecFileName))) { |
| 1993 | SLOGE("Couldn't find ASEC %s; cleaning up", cd->id); |
| 1994 | removeAsec.push_back(cd); |
| 1995 | } else { |
| 1996 | SLOGD("Found ASEC at path %s", asecFileName); |
| 1997 | if (!strncmp(asecFileName, Volume::SEC_ASECDIR_EXT, |
| 1998 | strlen(Volume::SEC_ASECDIR_EXT))) { |
| 1999 | removeAsec.push_back(cd); |
| 2000 | } |
| 2001 | } |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 2002 | } else if (cd->type == OBB) { |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 2003 | if (v == getVolumeForFile(cd->id)) { |
Jeff Sharkey | 8c2c15b | 2013-10-17 15:17:19 -0700 | [diff] [blame] | 2004 | removeObb.push_back(cd); |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 2005 | } |
| 2006 | } else { |
| 2007 | SLOGE("Unknown container type %d!", cd->type); |
San Mehat | 1a06eda | 2010-04-15 12:58:50 -0700 | [diff] [blame] | 2008 | } |
| 2009 | } |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 2010 | |
Jeff Sharkey | 8c2c15b | 2013-10-17 15:17:19 -0700 | [diff] [blame] | 2011 | for (AsecIdCollection::iterator it = removeAsec.begin(); it != removeAsec.end(); ++it) { |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 2012 | ContainerData *cd = *it; |
Jeff Sharkey | 8c2c15b | 2013-10-17 15:17:19 -0700 | [diff] [blame] | 2013 | SLOGI("Unmounting ASEC %s (dependent on %s)", cd->id, v->getLabel()); |
| 2014 | if (unmountAsec(cd->id, force)) { |
| 2015 | SLOGE("Failed to unmount ASEC %s (%s)", cd->id, strerror(errno)); |
| 2016 | rc = -1; |
| 2017 | } |
| 2018 | } |
| 2019 | |
| 2020 | for (AsecIdCollection::iterator it = removeObb.begin(); it != removeObb.end(); ++it) { |
| 2021 | ContainerData *cd = *it; |
| 2022 | SLOGI("Unmounting OBB %s (dependent on %s)", cd->id, v->getLabel()); |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 2023 | if (unmountObb(cd->id, force)) { |
| 2024 | SLOGE("Failed to unmount OBB %s (%s)", cd->id, strerror(errno)); |
| 2025 | rc = -1; |
| 2026 | } |
| 2027 | } |
| 2028 | |
| 2029 | return rc; |
San Mehat | 1a06eda | 2010-04-15 12:58:50 -0700 | [diff] [blame] | 2030 | } |
| 2031 | |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 2032 | int VolumeManager::mkdirs(char* path) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 2033 | // Only offer to create directories for paths managed by vold |
| 2034 | if (strncmp(path, "/storage/", 9) == 0) { |
| 2035 | // fs_mkdirs() does symlink checking and relative path enforcement |
| 2036 | return fs_mkdirs(path, 0700); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 2037 | } else { |
Cylen Yao | 27cfee3 | 2014-05-02 19:23:42 +0800 | [diff] [blame] | 2038 | SLOGE("Failed to find mounted volume for %s", path); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 2039 | return -EINVAL; |
| 2040 | } |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 2041 | } |