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