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