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> |
| 27 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 28 | #include <linux/kdev_t.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 29 | |
| 30 | #define LOG_TAG "Vold" |
| 31 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 32 | #include <openssl/md5.h> |
| 33 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 34 | #include <cutils/log.h> |
| 35 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 36 | #include <sysutils/NetlinkEvent.h> |
| 37 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 38 | #include <private/android_filesystem_config.h> |
| 39 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 40 | #include "VolumeManager.h" |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 41 | #include "DirectVolume.h" |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 42 | #include "ResponseCode.h" |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 43 | #include "Loop.h" |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 44 | #include "Ext4.h" |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 45 | #include "Fat.h" |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 46 | #include "Devmapper.h" |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 47 | #include "Process.h" |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 48 | #include "Asec.h" |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 49 | #include "cryptfs.h" |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 50 | |
Mike Lockwood | 97f2fc1 | 2011-06-07 10:51:38 -0700 | [diff] [blame] | 51 | #define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file" |
| 52 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 53 | VolumeManager *VolumeManager::sInstance = NULL; |
| 54 | |
| 55 | VolumeManager *VolumeManager::Instance() { |
| 56 | if (!sInstance) |
| 57 | sInstance = new VolumeManager(); |
| 58 | return sInstance; |
| 59 | } |
| 60 | |
| 61 | VolumeManager::VolumeManager() { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 62 | mDebug = false; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 63 | mVolumes = new VolumeCollection(); |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 64 | mActiveContainers = new AsecIdCollection(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 65 | mBroadcaster = NULL; |
Mike Lockwood | a28056b | 2010-10-28 15:21:24 -0400 | [diff] [blame] | 66 | mUmsSharingCount = 0; |
| 67 | mSavedDirtyRatio = -1; |
| 68 | // set dirty ratio to 0 when UMS is active |
| 69 | mUmsDirtyRatio = 0; |
Ken Sumrall | 3b17005 | 2011-07-11 15:38:57 -0700 | [diff] [blame] | 70 | mVolManagerDisabled = 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | VolumeManager::~VolumeManager() { |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 74 | delete mVolumes; |
| 75 | delete mActiveContainers; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 78 | char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) { |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 79 | static const char* digits = "0123456789abcdef"; |
| 80 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 81 | unsigned char sig[MD5_DIGEST_LENGTH]; |
| 82 | |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 83 | if (buffer == NULL) { |
| 84 | SLOGE("Destination buffer is NULL"); |
| 85 | errno = ESPIPE; |
| 86 | return NULL; |
| 87 | } else if (id == NULL) { |
| 88 | SLOGE("Source buffer is NULL"); |
| 89 | errno = ESPIPE; |
| 90 | return NULL; |
| 91 | } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) { |
| 92 | SLOGE("Target hash buffer size < %d bytes (%d)", |
| 93 | MD5_ASCII_LENGTH_PLUS_NULL, len); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 94 | errno = ESPIPE; |
| 95 | return NULL; |
| 96 | } |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 97 | |
| 98 | MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 99 | |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 100 | char *p = buffer; |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 101 | for (int i = 0; i < MD5_DIGEST_LENGTH; i++) { |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 102 | *p++ = digits[sig[i] >> 4]; |
| 103 | *p++ = digits[sig[i] & 0x0F]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 104 | } |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 105 | *p = '\0'; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 106 | |
| 107 | return buffer; |
| 108 | } |
| 109 | |
| 110 | void VolumeManager::setDebug(bool enable) { |
| 111 | mDebug = enable; |
| 112 | VolumeCollection::iterator it; |
| 113 | for (it = mVolumes->begin(); it != mVolumes->end(); ++it) { |
| 114 | (*it)->setDebug(enable); |
| 115 | } |
| 116 | } |
| 117 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 118 | int VolumeManager::start() { |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | int VolumeManager::stop() { |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | int VolumeManager::addVolume(Volume *v) { |
| 127 | mVolumes->push_back(v); |
| 128 | return 0; |
| 129 | } |
| 130 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 131 | void VolumeManager::handleBlockEvent(NetlinkEvent *evt) { |
| 132 | const char *devpath = evt->findParam("DEVPATH"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 133 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 134 | /* Lookup a volume to handle this device */ |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 135 | VolumeCollection::iterator it; |
| 136 | bool hit = false; |
| 137 | for (it = mVolumes->begin(); it != mVolumes->end(); ++it) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 138 | if (!(*it)->handleBlockEvent(evt)) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 139 | #ifdef NETLINK_DEBUG |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 140 | SLOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 141 | #endif |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 142 | hit = true; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 143 | break; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if (!hit) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 148 | #ifdef NETLINK_DEBUG |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 149 | SLOGW("No volumes handled block event for '%s'", devpath); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 150 | #endif |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 154 | int VolumeManager::listVolumes(SocketClient *cli) { |
| 155 | VolumeCollection::iterator i; |
| 156 | |
| 157 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
| 158 | char *buffer; |
| 159 | asprintf(&buffer, "%s %s %d", |
| 160 | (*i)->getLabel(), (*i)->getMountpoint(), |
| 161 | (*i)->getState()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 162 | cli->sendMsg(ResponseCode::VolumeListResult, buffer, false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 163 | free(buffer); |
| 164 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 165 | cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 166 | return 0; |
| 167 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 168 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 169 | int VolumeManager::formatVolume(const char *label) { |
| 170 | Volume *v = lookupVolume(label); |
| 171 | |
| 172 | if (!v) { |
| 173 | errno = ENOENT; |
| 174 | return -1; |
| 175 | } |
| 176 | |
Ken Sumrall | 3b17005 | 2011-07-11 15:38:57 -0700 | [diff] [blame] | 177 | if (mVolManagerDisabled) { |
| 178 | errno = EBUSY; |
| 179 | return -1; |
| 180 | } |
| 181 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 182 | return v->formatVol(); |
| 183 | } |
| 184 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 185 | int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) { |
| 186 | char idHash[33]; |
| 187 | if (!asecHash(sourceFile, idHash, sizeof(idHash))) { |
| 188 | SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno)); |
| 189 | return -1; |
| 190 | } |
| 191 | |
| 192 | memset(mountPath, 0, mountPathLen); |
| 193 | snprintf(mountPath, mountPathLen, "%s/%s", Volume::LOOPDIR, idHash); |
| 194 | |
| 195 | if (access(mountPath, F_OK)) { |
| 196 | errno = ENOENT; |
| 197 | return -1; |
| 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 203 | int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) { |
San Mehat | 88ac2c0 | 2010-03-23 11:15:58 -0700 | [diff] [blame] | 204 | char asecFileName[255]; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 205 | |
| 206 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 207 | SLOGE("Couldn't find ASEC %s", id); |
| 208 | return -1; |
| 209 | } |
San Mehat | 88ac2c0 | 2010-03-23 11:15:58 -0700 | [diff] [blame] | 210 | |
| 211 | memset(buffer, 0, maxlen); |
| 212 | if (access(asecFileName, F_OK)) { |
| 213 | errno = ENOENT; |
| 214 | return -1; |
| 215 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 216 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 217 | snprintf(buffer, maxlen, "%s/%s", Volume::ASECDIR, id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 218 | return 0; |
| 219 | } |
| 220 | |
Dianne Hackborn | 736910c | 2011-06-27 13:37:07 -0700 | [diff] [blame] | 221 | int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) { |
| 222 | char asecFileName[255]; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 223 | |
| 224 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 225 | SLOGE("Couldn't find ASEC %s", id); |
| 226 | return -1; |
| 227 | } |
Dianne Hackborn | 736910c | 2011-06-27 13:37:07 -0700 | [diff] [blame] | 228 | |
| 229 | memset(buffer, 0, maxlen); |
| 230 | if (access(asecFileName, F_OK)) { |
| 231 | errno = ENOENT; |
| 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | snprintf(buffer, maxlen, "%s", asecFileName); |
| 236 | return 0; |
| 237 | } |
| 238 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 239 | int VolumeManager::createAsec(const char *id, unsigned int numSectors, const char *fstype, |
| 240 | const char *key, const int ownerUid, bool isExternal) { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 241 | struct asec_superblock sb; |
| 242 | memset(&sb, 0, sizeof(sb)); |
| 243 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 244 | const bool wantFilesystem = strcmp(fstype, "none"); |
| 245 | bool usingExt4 = false; |
| 246 | if (wantFilesystem) { |
| 247 | usingExt4 = !strcmp(fstype, "ext4"); |
| 248 | if (usingExt4) { |
| 249 | sb.c_opts |= ASEC_SB_C_OPTS_EXT4; |
| 250 | } else if (strcmp(fstype, "fat")) { |
| 251 | SLOGE("Invalid filesystem type %s", fstype); |
| 252 | errno = EINVAL; |
| 253 | return -1; |
| 254 | } |
| 255 | } |
| 256 | |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 257 | sb.magic = ASEC_SB_MAGIC; |
| 258 | sb.ver = ASEC_SB_VER; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 259 | |
San Mehat | d31e380 | 2010-02-18 08:37:45 -0800 | [diff] [blame] | 260 | if (numSectors < ((1024*1024)/512)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 261 | SLOGE("Invalid container size specified (%d sectors)", numSectors); |
San Mehat | d31e380 | 2010-02-18 08:37:45 -0800 | [diff] [blame] | 262 | errno = EINVAL; |
| 263 | return -1; |
| 264 | } |
| 265 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 266 | if (lookupVolume(id)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 267 | SLOGE("ASEC id '%s' currently exists", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 268 | errno = EADDRINUSE; |
| 269 | return -1; |
| 270 | } |
| 271 | |
| 272 | char asecFileName[255]; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 273 | |
| 274 | if (!findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 275 | SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)", |
| 276 | asecFileName, strerror(errno)); |
| 277 | errno = EADDRINUSE; |
| 278 | return -1; |
| 279 | } |
| 280 | |
| 281 | const char *asecDir = isExternal ? Volume::SEC_ASECDIR_EXT : Volume::SEC_ASECDIR_INT; |
| 282 | |
| 283 | snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 284 | |
| 285 | if (!access(asecFileName, F_OK)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 286 | SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)", |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 287 | asecFileName, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 288 | errno = EADDRINUSE; |
| 289 | return -1; |
| 290 | } |
| 291 | |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 292 | /* |
| 293 | * Add some headroom |
| 294 | */ |
| 295 | unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2; |
| 296 | unsigned numImgSectors = numSectors + fatSize + 2; |
| 297 | |
| 298 | if (numImgSectors % 63) { |
| 299 | numImgSectors += (63 - (numImgSectors % 63)); |
| 300 | } |
| 301 | |
| 302 | // Add +1 for our superblock which is at the end |
| 303 | if (Loop::createImageFile(asecFileName, numImgSectors + 1)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 304 | SLOGE("ASEC image file creation failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 305 | return -1; |
| 306 | } |
| 307 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 308 | char idHash[33]; |
| 309 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 310 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 311 | unlink(asecFileName); |
| 312 | return -1; |
| 313 | } |
| 314 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 315 | char loopDevice[255]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 316 | if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 317 | SLOGE("ASEC loop device creation failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 318 | unlink(asecFileName); |
| 319 | return -1; |
| 320 | } |
| 321 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 322 | char dmDevice[255]; |
| 323 | bool cleanupDm = false; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 324 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 325 | if (strcmp(key, "none")) { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 326 | // XXX: This is all we support for now |
| 327 | sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 328 | if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice, |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 329 | sizeof(dmDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 330 | SLOGE("ASEC device mapping failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 331 | Loop::destroyByDevice(loopDevice); |
| 332 | unlink(asecFileName); |
| 333 | return -1; |
| 334 | } |
| 335 | cleanupDm = true; |
| 336 | } else { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 337 | sb.c_cipher = ASEC_SB_C_CIPHER_NONE; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 338 | strcpy(dmDevice, loopDevice); |
| 339 | } |
| 340 | |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 341 | /* |
| 342 | * Drop down the superblock at the end of the file |
| 343 | */ |
| 344 | |
| 345 | int sbfd = open(loopDevice, O_RDWR); |
| 346 | if (sbfd < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 347 | SLOGE("Failed to open new DM device for superblock write (%s)", strerror(errno)); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 348 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 349 | Devmapper::destroy(idHash); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 350 | } |
| 351 | Loop::destroyByDevice(loopDevice); |
| 352 | unlink(asecFileName); |
| 353 | return -1; |
| 354 | } |
| 355 | |
| 356 | if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) { |
| 357 | close(sbfd); |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 358 | SLOGE("Failed to lseek for superblock (%s)", strerror(errno)); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 359 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 360 | Devmapper::destroy(idHash); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 361 | } |
| 362 | Loop::destroyByDevice(loopDevice); |
| 363 | unlink(asecFileName); |
| 364 | return -1; |
| 365 | } |
| 366 | |
| 367 | if (write(sbfd, &sb, sizeof(sb)) != sizeof(sb)) { |
| 368 | close(sbfd); |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 369 | SLOGE("Failed to write superblock (%s)", strerror(errno)); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 370 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 371 | Devmapper::destroy(idHash); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 372 | } |
| 373 | Loop::destroyByDevice(loopDevice); |
| 374 | unlink(asecFileName); |
| 375 | return -1; |
| 376 | } |
| 377 | close(sbfd); |
| 378 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 379 | if (wantFilesystem) { |
| 380 | int formatStatus; |
| 381 | if (usingExt4) { |
| 382 | formatStatus = Ext4::format(dmDevice); |
| 383 | } else { |
| 384 | formatStatus = Fat::format(dmDevice, numImgSectors); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 385 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 386 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 387 | if (formatStatus < 0) { |
| 388 | SLOGE("ASEC fs format failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 389 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 390 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 391 | } |
San Mehat | eb13a90 | 2010-01-07 12:12:50 -0800 | [diff] [blame] | 392 | Loop::destroyByDevice(loopDevice); |
| 393 | unlink(asecFileName); |
| 394 | return -1; |
| 395 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 396 | |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 397 | char mountPoint[255]; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 398 | |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 399 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 400 | if (mkdir(mountPoint, 0000)) { |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 401 | if (errno != EEXIST) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 402 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 403 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 404 | Devmapper::destroy(idHash); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 405 | } |
| 406 | Loop::destroyByDevice(loopDevice); |
| 407 | unlink(asecFileName); |
| 408 | return -1; |
| 409 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 410 | } |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 411 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 412 | int mountStatus; |
| 413 | if (usingExt4) { |
| 414 | mountStatus = Ext4::doMount(dmDevice, mountPoint, false, false, false); |
| 415 | } else { |
| 416 | mountStatus = Fat::doMount(dmDevice, mountPoint, false, false, false, ownerUid, 0, 0000, |
| 417 | false); |
| 418 | } |
| 419 | |
| 420 | if (mountStatus) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 421 | SLOGE("ASEC FAT mount failed (%s)", strerror(errno)); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 422 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 423 | Devmapper::destroy(idHash); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 424 | } |
| 425 | Loop::destroyByDevice(loopDevice); |
| 426 | unlink(asecFileName); |
| 427 | return -1; |
| 428 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 429 | |
| 430 | if (usingExt4) { |
| 431 | int dirfd = open(mountPoint, O_DIRECTORY); |
| 432 | if (dirfd >= 0) { |
| 433 | if (fchown(dirfd, ownerUid, AID_SYSTEM) |
| 434 | || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) { |
| 435 | SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint); |
| 436 | } |
| 437 | close(dirfd); |
| 438 | } |
| 439 | } |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 440 | } else { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 441 | SLOGI("Created raw secure container %s (no filesystem)", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 442 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 443 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 444 | mActiveContainers->push_back(new ContainerData(strdup(id), ASEC)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | int VolumeManager::finalizeAsec(const char *id) { |
| 449 | char asecFileName[255]; |
| 450 | char loopDevice[255]; |
| 451 | char mountPoint[255]; |
| 452 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 453 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 454 | SLOGE("Couldn't find ASEC %s", id); |
| 455 | return -1; |
| 456 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 457 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 458 | char idHash[33]; |
| 459 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 460 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 461 | return -1; |
| 462 | } |
| 463 | |
| 464 | if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 465 | SLOGE("Unable to finalize %s (%s)", id, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 466 | return -1; |
| 467 | } |
| 468 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 469 | unsigned int nr_sec = 0; |
| 470 | struct asec_superblock sb; |
| 471 | |
| 472 | if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) { |
| 473 | return -1; |
| 474 | } |
| 475 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 476 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 477 | |
| 478 | int result = 0; |
| 479 | if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) { |
| 480 | result = Ext4::doMount(loopDevice, mountPoint, true, true, true); |
| 481 | } else { |
| 482 | result = Fat::doMount(loopDevice, mountPoint, true, true, true, 0, 0, 0227, false); |
| 483 | } |
| 484 | |
| 485 | if (result) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 486 | SLOGE("ASEC finalize mount failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 487 | return -1; |
| 488 | } |
| 489 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 490 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 491 | SLOGD("ASEC %s finalized", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 492 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 493 | return 0; |
| 494 | } |
| 495 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 496 | int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) { |
| 497 | char asecFileName[255]; |
| 498 | char loopDevice[255]; |
| 499 | char mountPoint[255]; |
| 500 | |
| 501 | if (gid < AID_APP) { |
| 502 | SLOGE("Group ID is not in application range"); |
| 503 | return -1; |
| 504 | } |
| 505 | |
| 506 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 507 | SLOGE("Couldn't find ASEC %s", id); |
| 508 | return -1; |
| 509 | } |
| 510 | |
| 511 | char idHash[33]; |
| 512 | if (!asecHash(id, idHash, sizeof(idHash))) { |
| 513 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
| 514 | return -1; |
| 515 | } |
| 516 | |
| 517 | if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
| 518 | SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno)); |
| 519 | return -1; |
| 520 | } |
| 521 | |
| 522 | unsigned int nr_sec = 0; |
| 523 | struct asec_superblock sb; |
| 524 | |
| 525 | if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) { |
| 526 | return -1; |
| 527 | } |
| 528 | |
| 529 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
| 530 | |
| 531 | int result = 0; |
| 532 | if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) { |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | int ret = Ext4::doMount(loopDevice, mountPoint, |
| 537 | false /* read-only */, |
| 538 | true /* remount */, |
| 539 | false /* executable */); |
| 540 | if (ret) { |
| 541 | SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno)); |
| 542 | return -1; |
| 543 | } |
| 544 | |
| 545 | char *paths[] = { mountPoint, NULL }; |
| 546 | |
| 547 | FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL); |
| 548 | if (fts) { |
| 549 | // Traverse the entire hierarchy and chown to system UID. |
| 550 | for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) { |
| 551 | // We don't care about the lost+found directory. |
| 552 | if (!strcmp(ftsent->fts_name, "lost+found")) { |
| 553 | continue; |
| 554 | } |
| 555 | |
| 556 | /* |
| 557 | * There can only be one file marked as private right now. |
| 558 | * This should be more robust, but it satisfies the requirements |
| 559 | * we have for right now. |
| 560 | */ |
| 561 | const bool privateFile = !strcmp(ftsent->fts_name, filename); |
| 562 | |
| 563 | int fd = open(ftsent->fts_accpath, O_NOFOLLOW); |
| 564 | if (fd < 0) { |
| 565 | SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno)); |
| 566 | result = -1; |
| 567 | continue; |
| 568 | } |
| 569 | |
| 570 | result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM); |
| 571 | |
| 572 | if (ftsent->fts_info & FTS_D) { |
Kenny Root | 1a673c8 | 2012-05-10 16:45:29 -0700 | [diff] [blame^] | 573 | result |= fchmod(fd, 0755); |
Kenny Root | 348c8ab | 2012-05-10 15:39:53 -0700 | [diff] [blame] | 574 | } else if (ftsent->fts_info & FTS_F) { |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 575 | result |= fchmod(fd, privateFile ? 0640 : 0644); |
| 576 | } |
| 577 | close(fd); |
| 578 | } |
| 579 | fts_close(fts); |
| 580 | |
| 581 | // Finally make the directory readable by everyone. |
| 582 | int dirfd = open(mountPoint, O_DIRECTORY); |
| 583 | if (dirfd < 0 || fchmod(dirfd, 0755)) { |
| 584 | SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno)); |
| 585 | result |= -1; |
| 586 | } |
| 587 | close(dirfd); |
| 588 | } else { |
| 589 | result |= -1; |
| 590 | } |
| 591 | |
| 592 | result |= Ext4::doMount(loopDevice, mountPoint, |
| 593 | true /* read-only */, |
| 594 | true /* remount */, |
| 595 | true /* execute */); |
| 596 | |
| 597 | if (result) { |
| 598 | SLOGE("ASEC fix permissions failed (%s)", strerror(errno)); |
| 599 | return -1; |
| 600 | } |
| 601 | |
| 602 | if (mDebug) { |
| 603 | SLOGD("ASEC %s permissions fixed", id); |
| 604 | } |
| 605 | return 0; |
| 606 | } |
| 607 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 608 | int VolumeManager::renameAsec(const char *id1, const char *id2) { |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 609 | char asecFilename1[255]; |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 610 | char *asecFilename2; |
| 611 | char mountPoint[255]; |
| 612 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 613 | const char *dir; |
| 614 | |
| 615 | if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) { |
| 616 | SLOGE("Couldn't find ASEC %s", id1); |
| 617 | return -1; |
| 618 | } |
| 619 | |
| 620 | asprintf(&asecFilename2, "%s/%s.asec", dir, id2); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 621 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 622 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id1); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 623 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 624 | SLOGW("Rename attempt when src mounted"); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 625 | errno = EBUSY; |
| 626 | goto out_err; |
| 627 | } |
| 628 | |
San Mehat | 96956ed | 2010-02-24 08:42:51 -0800 | [diff] [blame] | 629 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id2); |
| 630 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 631 | SLOGW("Rename attempt when dst mounted"); |
San Mehat | 96956ed | 2010-02-24 08:42:51 -0800 | [diff] [blame] | 632 | errno = EBUSY; |
| 633 | goto out_err; |
| 634 | } |
| 635 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 636 | if (!access(asecFilename2, F_OK)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 637 | SLOGE("Rename attempt when dst exists"); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 638 | errno = EADDRINUSE; |
| 639 | goto out_err; |
| 640 | } |
| 641 | |
| 642 | if (rename(asecFilename1, asecFilename2)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 643 | SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno)); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 644 | goto out_err; |
| 645 | } |
| 646 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 647 | free(asecFilename2); |
| 648 | return 0; |
| 649 | |
| 650 | out_err: |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 651 | free(asecFilename2); |
| 652 | return -1; |
| 653 | } |
| 654 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 655 | #define UNMOUNT_RETRIES 5 |
| 656 | #define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000) |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 657 | int VolumeManager::unmountAsec(const char *id, bool force) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 658 | char asecFileName[255]; |
| 659 | char mountPoint[255]; |
| 660 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 661 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 662 | SLOGE("Couldn't find ASEC %s", id); |
| 663 | return -1; |
| 664 | } |
| 665 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 666 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 667 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 668 | char idHash[33]; |
| 669 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 670 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 671 | return -1; |
| 672 | } |
| 673 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 674 | return unmountLoopImage(id, idHash, asecFileName, mountPoint, force); |
| 675 | } |
| 676 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 677 | int VolumeManager::unmountObb(const char *fileName, bool force) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 678 | char mountPoint[255]; |
| 679 | |
| 680 | char idHash[33]; |
| 681 | if (!asecHash(fileName, idHash, sizeof(idHash))) { |
| 682 | SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno)); |
| 683 | return -1; |
| 684 | } |
| 685 | |
| 686 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash); |
| 687 | |
| 688 | return unmountLoopImage(fileName, idHash, fileName, mountPoint, force); |
| 689 | } |
| 690 | |
| 691 | int VolumeManager::unmountLoopImage(const char *id, const char *idHash, |
| 692 | const char *fileName, const char *mountPoint, bool force) { |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 693 | if (!isMountpointMounted(mountPoint)) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 694 | SLOGE("Unmount request for %s when not mounted", id); |
Kenny Root | 918e5f9 | 2010-09-30 18:00:52 -0700 | [diff] [blame] | 695 | errno = ENOENT; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 696 | return -1; |
| 697 | } |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 698 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 699 | int i, rc; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 700 | for (i = 1; i <= UNMOUNT_RETRIES; i++) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 701 | rc = umount(mountPoint); |
| 702 | if (!rc) { |
| 703 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 704 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 705 | if (rc && (errno == EINVAL || errno == ENOENT)) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 706 | SLOGI("Container %s unmounted OK", id); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 707 | rc = 0; |
| 708 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 709 | } |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 710 | SLOGW("%s unmount attempt %d failed (%s)", |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 711 | id, i, strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 712 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 713 | int action = 0; // default is to just complain |
| 714 | |
| 715 | if (force) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 716 | if (i > (UNMOUNT_RETRIES - 2)) |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 717 | action = 2; // SIGKILL |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 718 | else if (i > (UNMOUNT_RETRIES - 3)) |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 719 | action = 1; // SIGHUP |
| 720 | } |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 721 | |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 722 | Process::killProcessesWithOpenFiles(mountPoint, action); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 723 | usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | if (rc) { |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 727 | errno = EBUSY; |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 728 | SLOGE("Failed to unmount container %s (%s)", id, strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 729 | return -1; |
| 730 | } |
| 731 | |
San Mehat | 12f4b89 | 2010-02-24 11:43:22 -0800 | [diff] [blame] | 732 | int retries = 10; |
| 733 | |
| 734 | while(retries--) { |
| 735 | if (!rmdir(mountPoint)) { |
| 736 | break; |
| 737 | } |
| 738 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 739 | SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno)); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 740 | usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS); |
San Mehat | 12f4b89 | 2010-02-24 11:43:22 -0800 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | if (!retries) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 744 | SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno)); |
San Mehat | f5c6198 | 2010-02-03 11:04:46 -0800 | [diff] [blame] | 745 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 746 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 747 | if (Devmapper::destroy(idHash) && errno != ENXIO) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 748 | SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | char loopDevice[255]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 752 | if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 753 | Loop::destroyByDevice(loopDevice); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 754 | } else { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 755 | SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 756 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 757 | |
| 758 | AsecIdCollection::iterator it; |
| 759 | for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) { |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 760 | ContainerData* cd = *it; |
| 761 | if (!strcmp(cd->id, id)) { |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 762 | free(*it); |
| 763 | mActiveContainers->erase(it); |
| 764 | break; |
| 765 | } |
| 766 | } |
| 767 | if (it == mActiveContainers->end()) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 768 | SLOGW("mActiveContainers is inconsistent!"); |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 769 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 770 | return 0; |
| 771 | } |
| 772 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 773 | int VolumeManager::destroyAsec(const char *id, bool force) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 774 | char asecFileName[255]; |
| 775 | char mountPoint[255]; |
| 776 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 777 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 778 | SLOGE("Couldn't find ASEC %s", id); |
| 779 | return -1; |
| 780 | } |
| 781 | |
San Mehat | 55013f7 | 2010-02-24 12:12:34 -0800 | [diff] [blame] | 782 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 783 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 784 | if (isMountpointMounted(mountPoint)) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 785 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 786 | SLOGD("Unmounting container before destroy"); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 787 | } |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 788 | if (unmountAsec(id, force)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 789 | SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno)); |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 790 | return -1; |
| 791 | } |
| 792 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 793 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 794 | if (unlink(asecFileName)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 795 | SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno)); |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 796 | return -1; |
| 797 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 798 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 799 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 800 | SLOGD("ASEC %s destroyed", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 801 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 802 | return 0; |
| 803 | } |
| 804 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 805 | bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const { |
| 806 | int dirfd = open(dir, O_DIRECTORY); |
| 807 | if (dirfd < 0) { |
| 808 | SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno)); |
| 809 | return -1; |
| 810 | } |
| 811 | |
| 812 | bool ret = false; |
| 813 | |
| 814 | if (!faccessat(dirfd, asecName, F_OK, AT_SYMLINK_NOFOLLOW)) { |
| 815 | ret = true; |
| 816 | } |
| 817 | |
| 818 | close(dirfd); |
| 819 | |
| 820 | return ret; |
| 821 | } |
| 822 | |
| 823 | int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen, |
| 824 | const char **directory) const { |
| 825 | int dirfd, fd; |
| 826 | const int idLen = strlen(id); |
| 827 | char *asecName; |
| 828 | |
| 829 | if (asprintf(&asecName, "%s.asec", id) < 0) { |
| 830 | SLOGE("Couldn't allocate string to write ASEC name"); |
| 831 | return -1; |
| 832 | } |
| 833 | |
| 834 | const char *dir; |
| 835 | if (isAsecInDirectory(Volume::SEC_ASECDIR_INT, asecName)) { |
| 836 | dir = Volume::SEC_ASECDIR_INT; |
| 837 | } else if (isAsecInDirectory(Volume::SEC_ASECDIR_EXT, asecName)) { |
| 838 | dir = Volume::SEC_ASECDIR_EXT; |
| 839 | } else { |
| 840 | free(asecName); |
| 841 | return -1; |
| 842 | } |
| 843 | |
| 844 | if (directory != NULL) { |
| 845 | *directory = dir; |
| 846 | } |
| 847 | |
| 848 | if (asecPath != NULL) { |
| 849 | int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName); |
| 850 | if (written < 0 || static_cast<size_t>(written) >= asecPathLen) { |
| 851 | free(asecName); |
| 852 | return -1; |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | free(asecName); |
| 857 | return 0; |
| 858 | } |
| 859 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 860 | int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid) { |
| 861 | char asecFileName[255]; |
| 862 | char mountPoint[255]; |
| 863 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 864 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 865 | SLOGE("Couldn't find ASEC %s", id); |
| 866 | return -1; |
| 867 | } |
| 868 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 869 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 870 | |
| 871 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 872 | SLOGE("ASEC %s already mounted", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 873 | errno = EBUSY; |
| 874 | return -1; |
| 875 | } |
| 876 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 877 | char idHash[33]; |
| 878 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 879 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 880 | return -1; |
| 881 | } |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 882 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 883 | char loopDevice[255]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 884 | if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
| 885 | if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 886 | SLOGE("ASEC loop device creation failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 887 | return -1; |
| 888 | } |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 889 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 890 | SLOGD("New loop device created at %s", loopDevice); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 891 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 892 | } else { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 893 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 894 | SLOGD("Found active loopback for %s at %s", asecFileName, loopDevice); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 895 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | char dmDevice[255]; |
| 899 | bool cleanupDm = false; |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 900 | int fd; |
| 901 | unsigned int nr_sec = 0; |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 902 | struct asec_superblock sb; |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 903 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 904 | if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) { |
| 905 | return -1; |
| 906 | } |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 907 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 908 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 909 | SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 910 | } |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 911 | if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 912 | SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 913 | Loop::destroyByDevice(loopDevice); |
| 914 | errno = EMEDIUMTYPE; |
| 915 | return -1; |
| 916 | } |
| 917 | nr_sec--; // We don't want the devmapping to extend onto our superblock |
| 918 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 919 | if (strcmp(key, "none")) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 920 | if (Devmapper::lookupActive(idHash, dmDevice, sizeof(dmDevice))) { |
| 921 | if (Devmapper::create(idHash, loopDevice, key, nr_sec, |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 922 | dmDevice, sizeof(dmDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 923 | SLOGE("ASEC device mapping failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 924 | Loop::destroyByDevice(loopDevice); |
| 925 | return -1; |
| 926 | } |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 927 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 928 | SLOGD("New devmapper instance created at %s", dmDevice); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 929 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 930 | } else { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 931 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 932 | SLOGD("Found active devmapper for %s at %s", asecFileName, dmDevice); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 933 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 934 | } |
| 935 | cleanupDm = true; |
| 936 | } else { |
| 937 | strcpy(dmDevice, loopDevice); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 938 | } |
| 939 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 940 | if (mkdir(mountPoint, 0000)) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 941 | if (errno != EEXIST) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 942 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 943 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 944 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 945 | } |
| 946 | Loop::destroyByDevice(loopDevice); |
| 947 | return -1; |
| 948 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 949 | } |
| 950 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 951 | int result; |
| 952 | if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) { |
| 953 | result = Ext4::doMount(dmDevice, mountPoint, true, false, true); |
| 954 | } else { |
| 955 | result = Fat::doMount(dmDevice, mountPoint, true, false, true, ownerUid, 0, 0222, false); |
| 956 | } |
| 957 | |
| 958 | if (result) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 959 | SLOGE("ASEC mount failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 960 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 961 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 962 | } |
| 963 | Loop::destroyByDevice(loopDevice); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 964 | return -1; |
| 965 | } |
| 966 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 967 | mActiveContainers->push_back(new ContainerData(strdup(id), ASEC)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 968 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 969 | SLOGD("ASEC %s mounted", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 970 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 971 | return 0; |
| 972 | } |
| 973 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 974 | /** |
| 975 | * Mounts an image file <code>img</code>. |
| 976 | */ |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 977 | int VolumeManager::mountObb(const char *img, const char *key, int ownerUid) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 978 | char mountPoint[255]; |
| 979 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 980 | char idHash[33]; |
| 981 | if (!asecHash(img, idHash, sizeof(idHash))) { |
| 982 | SLOGE("Hash of '%s' failed (%s)", img, strerror(errno)); |
| 983 | return -1; |
| 984 | } |
| 985 | |
| 986 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash); |
| 987 | |
| 988 | if (isMountpointMounted(mountPoint)) { |
| 989 | SLOGE("Image %s already mounted", img); |
| 990 | errno = EBUSY; |
| 991 | return -1; |
| 992 | } |
| 993 | |
| 994 | char loopDevice[255]; |
| 995 | if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
| 996 | if (Loop::create(idHash, img, loopDevice, sizeof(loopDevice))) { |
| 997 | SLOGE("Image loop device creation failed (%s)", strerror(errno)); |
| 998 | return -1; |
| 999 | } |
| 1000 | if (mDebug) { |
| 1001 | SLOGD("New loop device created at %s", loopDevice); |
| 1002 | } |
| 1003 | } else { |
| 1004 | if (mDebug) { |
| 1005 | SLOGD("Found active loopback for %s at %s", img, loopDevice); |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | char dmDevice[255]; |
| 1010 | bool cleanupDm = false; |
| 1011 | int fd; |
| 1012 | unsigned int nr_sec = 0; |
| 1013 | |
| 1014 | if ((fd = open(loopDevice, O_RDWR)) < 0) { |
| 1015 | SLOGE("Failed to open loopdevice (%s)", strerror(errno)); |
| 1016 | Loop::destroyByDevice(loopDevice); |
| 1017 | return -1; |
| 1018 | } |
| 1019 | |
| 1020 | if (ioctl(fd, BLKGETSIZE, &nr_sec)) { |
| 1021 | SLOGE("Failed to get loop size (%s)", strerror(errno)); |
| 1022 | Loop::destroyByDevice(loopDevice); |
| 1023 | close(fd); |
| 1024 | return -1; |
| 1025 | } |
| 1026 | |
| 1027 | close(fd); |
| 1028 | |
| 1029 | if (strcmp(key, "none")) { |
| 1030 | if (Devmapper::lookupActive(idHash, dmDevice, sizeof(dmDevice))) { |
| 1031 | if (Devmapper::create(idHash, loopDevice, key, nr_sec, |
| 1032 | dmDevice, sizeof(dmDevice))) { |
| 1033 | SLOGE("ASEC device mapping failed (%s)", strerror(errno)); |
| 1034 | Loop::destroyByDevice(loopDevice); |
| 1035 | return -1; |
| 1036 | } |
| 1037 | if (mDebug) { |
| 1038 | SLOGD("New devmapper instance created at %s", dmDevice); |
| 1039 | } |
| 1040 | } else { |
| 1041 | if (mDebug) { |
| 1042 | SLOGD("Found active devmapper for %s at %s", img, dmDevice); |
| 1043 | } |
| 1044 | } |
| 1045 | cleanupDm = true; |
| 1046 | } else { |
| 1047 | strcpy(dmDevice, loopDevice); |
| 1048 | } |
| 1049 | |
| 1050 | if (mkdir(mountPoint, 0755)) { |
| 1051 | if (errno != EEXIST) { |
| 1052 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
| 1053 | if (cleanupDm) { |
| 1054 | Devmapper::destroy(idHash); |
| 1055 | } |
| 1056 | Loop::destroyByDevice(loopDevice); |
| 1057 | return -1; |
| 1058 | } |
| 1059 | } |
| 1060 | |
Kenny Root | a3e0608 | 2010-08-27 08:31:35 -0700 | [diff] [blame] | 1061 | if (Fat::doMount(dmDevice, mountPoint, true, false, true, ownerUid, 0, |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1062 | 0227, false)) { |
| 1063 | SLOGE("Image mount failed (%s)", strerror(errno)); |
| 1064 | if (cleanupDm) { |
| 1065 | Devmapper::destroy(idHash); |
| 1066 | } |
| 1067 | Loop::destroyByDevice(loopDevice); |
| 1068 | return -1; |
| 1069 | } |
| 1070 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1071 | mActiveContainers->push_back(new ContainerData(strdup(img), OBB)); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1072 | if (mDebug) { |
| 1073 | SLOGD("Image %s mounted", img); |
| 1074 | } |
| 1075 | return 0; |
| 1076 | } |
| 1077 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1078 | int VolumeManager::mountVolume(const char *label) { |
| 1079 | Volume *v = lookupVolume(label); |
| 1080 | |
| 1081 | if (!v) { |
| 1082 | errno = ENOENT; |
| 1083 | return -1; |
| 1084 | } |
| 1085 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1086 | return v->mountVol(); |
| 1087 | } |
| 1088 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1089 | int VolumeManager::listMountedObbs(SocketClient* cli) { |
| 1090 | char device[256]; |
| 1091 | char mount_path[256]; |
| 1092 | char rest[256]; |
| 1093 | FILE *fp; |
| 1094 | char line[1024]; |
| 1095 | |
| 1096 | if (!(fp = fopen("/proc/mounts", "r"))) { |
| 1097 | SLOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
| 1098 | return -1; |
| 1099 | } |
| 1100 | |
| 1101 | // Create a string to compare against that has a trailing slash |
| 1102 | int loopDirLen = sizeof(Volume::LOOPDIR); |
| 1103 | char loopDir[loopDirLen + 2]; |
| 1104 | strcpy(loopDir, Volume::LOOPDIR); |
| 1105 | loopDir[loopDirLen++] = '/'; |
| 1106 | loopDir[loopDirLen] = '\0'; |
| 1107 | |
| 1108 | while(fgets(line, sizeof(line), fp)) { |
| 1109 | line[strlen(line)-1] = '\0'; |
| 1110 | |
| 1111 | /* |
| 1112 | * Should look like: |
| 1113 | * /dev/block/loop0 /mnt/obb/fc99df1323fd36424f864dcb76b76d65 ... |
| 1114 | */ |
| 1115 | sscanf(line, "%255s %255s %255s\n", device, mount_path, rest); |
| 1116 | |
| 1117 | if (!strncmp(mount_path, loopDir, loopDirLen)) { |
| 1118 | int fd = open(device, O_RDONLY); |
| 1119 | if (fd >= 0) { |
| 1120 | struct loop_info64 li; |
| 1121 | if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) { |
| 1122 | cli->sendMsg(ResponseCode::AsecListResult, |
| 1123 | (const char*) li.lo_file_name, false); |
| 1124 | } |
| 1125 | close(fd); |
| 1126 | } |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | fclose(fp); |
| 1131 | return 0; |
| 1132 | } |
| 1133 | |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 1134 | int VolumeManager::shareEnabled(const char *label, const char *method, bool *enabled) { |
| 1135 | Volume *v = lookupVolume(label); |
| 1136 | |
| 1137 | if (!v) { |
| 1138 | errno = ENOENT; |
| 1139 | return -1; |
| 1140 | } |
| 1141 | |
| 1142 | if (strcmp(method, "ums")) { |
| 1143 | errno = ENOSYS; |
| 1144 | return -1; |
| 1145 | } |
| 1146 | |
| 1147 | if (v->getState() != Volume::State_Shared) { |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 1148 | *enabled = false; |
San Mehat | b9aed74 | 2010-02-04 15:07:01 -0800 | [diff] [blame] | 1149 | } else { |
| 1150 | *enabled = true; |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 1151 | } |
| 1152 | return 0; |
| 1153 | } |
| 1154 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1155 | int VolumeManager::shareVolume(const char *label, const char *method) { |
| 1156 | Volume *v = lookupVolume(label); |
| 1157 | |
| 1158 | if (!v) { |
| 1159 | errno = ENOENT; |
| 1160 | return -1; |
| 1161 | } |
| 1162 | |
| 1163 | /* |
| 1164 | * Eventually, we'll want to support additional share back-ends, |
| 1165 | * some of which may work while the media is mounted. For now, |
| 1166 | * we just support UMS |
| 1167 | */ |
| 1168 | if (strcmp(method, "ums")) { |
| 1169 | errno = ENOSYS; |
| 1170 | return -1; |
| 1171 | } |
| 1172 | |
| 1173 | if (v->getState() == Volume::State_NoMedia) { |
| 1174 | errno = ENODEV; |
| 1175 | return -1; |
| 1176 | } |
| 1177 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1178 | if (v->getState() != Volume::State_Idle) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1179 | // You need to unmount manually befoe sharing |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1180 | errno = EBUSY; |
| 1181 | return -1; |
| 1182 | } |
| 1183 | |
Ken Sumrall | 3b17005 | 2011-07-11 15:38:57 -0700 | [diff] [blame] | 1184 | if (mVolManagerDisabled) { |
| 1185 | errno = EBUSY; |
| 1186 | return -1; |
| 1187 | } |
| 1188 | |
Mike Lockwood | 2dfe297 | 2010-09-17 18:50:51 -0400 | [diff] [blame] | 1189 | dev_t d = v->getShareDevice(); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1190 | if ((MAJOR(d) == 0) && (MINOR(d) == 0)) { |
| 1191 | // This volume does not support raw disk access |
| 1192 | errno = EINVAL; |
| 1193 | return -1; |
| 1194 | } |
| 1195 | |
| 1196 | int fd; |
| 1197 | char nodepath[255]; |
| 1198 | snprintf(nodepath, |
| 1199 | sizeof(nodepath), "/dev/block/vold/%d:%d", |
| 1200 | MAJOR(d), MINOR(d)); |
| 1201 | |
Mike Lockwood | 97f2fc1 | 2011-06-07 10:51:38 -0700 | [diff] [blame] | 1202 | if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1203 | SLOGE("Unable to open ums lunfile (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1204 | return -1; |
| 1205 | } |
| 1206 | |
| 1207 | if (write(fd, nodepath, strlen(nodepath)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1208 | SLOGE("Unable to write to ums lunfile (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1209 | close(fd); |
| 1210 | return -1; |
| 1211 | } |
| 1212 | |
| 1213 | close(fd); |
| 1214 | v->handleVolumeShared(); |
Mike Lockwood | a28056b | 2010-10-28 15:21:24 -0400 | [diff] [blame] | 1215 | if (mUmsSharingCount++ == 0) { |
| 1216 | FILE* fp; |
| 1217 | mSavedDirtyRatio = -1; // in case we fail |
| 1218 | if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) { |
| 1219 | char line[16]; |
| 1220 | if (fgets(line, sizeof(line), fp) && sscanf(line, "%d", &mSavedDirtyRatio)) { |
| 1221 | fprintf(fp, "%d\n", mUmsDirtyRatio); |
| 1222 | } else { |
| 1223 | SLOGE("Failed to read dirty_ratio (%s)", strerror(errno)); |
| 1224 | } |
| 1225 | fclose(fp); |
| 1226 | } else { |
| 1227 | SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno)); |
| 1228 | } |
| 1229 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1230 | return 0; |
| 1231 | } |
| 1232 | |
| 1233 | int VolumeManager::unshareVolume(const char *label, const char *method) { |
| 1234 | Volume *v = lookupVolume(label); |
| 1235 | |
| 1236 | if (!v) { |
| 1237 | errno = ENOENT; |
| 1238 | return -1; |
| 1239 | } |
| 1240 | |
| 1241 | if (strcmp(method, "ums")) { |
| 1242 | errno = ENOSYS; |
| 1243 | return -1; |
| 1244 | } |
| 1245 | |
| 1246 | if (v->getState() != Volume::State_Shared) { |
| 1247 | errno = EINVAL; |
| 1248 | return -1; |
| 1249 | } |
| 1250 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1251 | int fd; |
Mike Lockwood | 97f2fc1 | 2011-06-07 10:51:38 -0700 | [diff] [blame] | 1252 | if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1253 | SLOGE("Unable to open ums lunfile (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1254 | return -1; |
| 1255 | } |
| 1256 | |
| 1257 | char ch = 0; |
| 1258 | if (write(fd, &ch, 1) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1259 | SLOGE("Unable to write to ums lunfile (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1260 | close(fd); |
| 1261 | return -1; |
| 1262 | } |
| 1263 | |
| 1264 | close(fd); |
| 1265 | v->handleVolumeUnshared(); |
Mike Lockwood | a28056b | 2010-10-28 15:21:24 -0400 | [diff] [blame] | 1266 | if (--mUmsSharingCount == 0 && mSavedDirtyRatio != -1) { |
| 1267 | FILE* fp; |
| 1268 | if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) { |
| 1269 | fprintf(fp, "%d\n", mSavedDirtyRatio); |
| 1270 | fclose(fp); |
| 1271 | } else { |
| 1272 | SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno)); |
| 1273 | } |
| 1274 | mSavedDirtyRatio = -1; |
| 1275 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1276 | return 0; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1277 | } |
| 1278 | |
Ken Sumrall | 3b17005 | 2011-07-11 15:38:57 -0700 | [diff] [blame] | 1279 | extern "C" int vold_disableVol(const char *label) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1280 | VolumeManager *vm = VolumeManager::Instance(); |
Ken Sumrall | 3b17005 | 2011-07-11 15:38:57 -0700 | [diff] [blame] | 1281 | vm->disableVolumeManager(); |
| 1282 | vm->unshareVolume(label, "ums"); |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 1283 | return vm->unmountVolume(label, true, false); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | extern "C" int vold_getNumDirectVolumes(void) { |
| 1287 | VolumeManager *vm = VolumeManager::Instance(); |
| 1288 | return vm->getNumDirectVolumes(); |
| 1289 | } |
| 1290 | |
| 1291 | int VolumeManager::getNumDirectVolumes(void) { |
| 1292 | VolumeCollection::iterator i; |
| 1293 | int n=0; |
| 1294 | |
| 1295 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
| 1296 | if ((*i)->getShareDevice() != (dev_t)0) { |
| 1297 | n++; |
| 1298 | } |
| 1299 | } |
| 1300 | return n; |
| 1301 | } |
| 1302 | |
| 1303 | extern "C" int vold_getDirectVolumeList(struct volume_info *vol_list) { |
| 1304 | VolumeManager *vm = VolumeManager::Instance(); |
| 1305 | return vm->getDirectVolumeList(vol_list); |
| 1306 | } |
| 1307 | |
| 1308 | int VolumeManager::getDirectVolumeList(struct volume_info *vol_list) { |
| 1309 | VolumeCollection::iterator i; |
| 1310 | int n=0; |
| 1311 | dev_t d; |
| 1312 | |
| 1313 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
| 1314 | if ((d=(*i)->getShareDevice()) != (dev_t)0) { |
| 1315 | (*i)->getVolInfo(&vol_list[n]); |
| 1316 | snprintf(vol_list[n].blk_dev, sizeof(vol_list[n].blk_dev), |
| 1317 | "/dev/block/vold/%d:%d",MAJOR(d), MINOR(d)); |
| 1318 | n++; |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | return 0; |
| 1323 | } |
| 1324 | |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 1325 | int VolumeManager::unmountVolume(const char *label, bool force, bool revert) { |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1326 | Volume *v = lookupVolume(label); |
| 1327 | |
| 1328 | if (!v) { |
| 1329 | errno = ENOENT; |
| 1330 | return -1; |
| 1331 | } |
| 1332 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1333 | if (v->getState() == Volume::State_NoMedia) { |
| 1334 | errno = ENODEV; |
| 1335 | return -1; |
| 1336 | } |
| 1337 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1338 | if (v->getState() != Volume::State_Mounted) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1339 | SLOGW("Attempt to unmount volume which isn't mounted (%d)\n", |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1340 | v->getState()); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1341 | errno = EBUSY; |
Ken Sumrall | 319b104 | 2011-06-14 14:01:55 -0700 | [diff] [blame] | 1342 | return UNMOUNT_NOT_MOUNTED_ERR; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1343 | } |
| 1344 | |
San Mehat | 1a06eda | 2010-04-15 12:58:50 -0700 | [diff] [blame] | 1345 | cleanupAsec(v, force); |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1346 | |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 1347 | return v->unmountVol(force, revert); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1348 | } |
| 1349 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1350 | /* |
| 1351 | * Looks up a volume by it's label or mount-point |
| 1352 | */ |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1353 | Volume *VolumeManager::lookupVolume(const char *label) { |
| 1354 | VolumeCollection::iterator i; |
| 1355 | |
| 1356 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1357 | if (label[0] == '/') { |
| 1358 | if (!strcmp(label, (*i)->getMountpoint())) |
| 1359 | return (*i); |
| 1360 | } else { |
| 1361 | if (!strcmp(label, (*i)->getLabel())) |
| 1362 | return (*i); |
| 1363 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1364 | } |
| 1365 | return NULL; |
| 1366 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1367 | |
| 1368 | bool VolumeManager::isMountpointMounted(const char *mp) |
| 1369 | { |
| 1370 | char device[256]; |
| 1371 | char mount_path[256]; |
| 1372 | char rest[256]; |
| 1373 | FILE *fp; |
| 1374 | char line[1024]; |
| 1375 | |
| 1376 | if (!(fp = fopen("/proc/mounts", "r"))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1377 | SLOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1378 | return false; |
| 1379 | } |
| 1380 | |
| 1381 | while(fgets(line, sizeof(line), fp)) { |
| 1382 | line[strlen(line)-1] = '\0'; |
| 1383 | sscanf(line, "%255s %255s %255s\n", device, mount_path, rest); |
| 1384 | if (!strcmp(mount_path, mp)) { |
| 1385 | fclose(fp); |
| 1386 | return true; |
| 1387 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | fclose(fp); |
| 1391 | return false; |
| 1392 | } |
| 1393 | |
San Mehat | 1a06eda | 2010-04-15 12:58:50 -0700 | [diff] [blame] | 1394 | int VolumeManager::cleanupAsec(Volume *v, bool force) { |
| 1395 | while(mActiveContainers->size()) { |
| 1396 | AsecIdCollection::iterator it = mActiveContainers->begin(); |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1397 | ContainerData* cd = *it; |
| 1398 | SLOGI("Unmounting ASEC %s (dependant on %s)", cd->id, v->getMountpoint()); |
| 1399 | if (cd->type == ASEC) { |
| 1400 | if (unmountAsec(cd->id, force)) { |
| 1401 | SLOGE("Failed to unmount ASEC %s (%s)", cd->id, strerror(errno)); |
| 1402 | return -1; |
| 1403 | } |
| 1404 | } else if (cd->type == OBB) { |
| 1405 | if (unmountObb(cd->id, force)) { |
| 1406 | SLOGE("Failed to unmount OBB %s (%s)", cd->id, strerror(errno)); |
| 1407 | return -1; |
| 1408 | } |
| 1409 | } else { |
| 1410 | SLOGE("Unknown container type %d!", cd->type); |
San Mehat | 1a06eda | 2010-04-15 12:58:50 -0700 | [diff] [blame] | 1411 | return -1; |
| 1412 | } |
| 1413 | } |
| 1414 | return 0; |
| 1415 | } |
| 1416 | |