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