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> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/mount.h> |
| 25 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 26 | #include <linux/kdev_t.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 27 | |
| 28 | #define LOG_TAG "Vold" |
| 29 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 30 | #include <openssl/md5.h> |
| 31 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 32 | #include <cutils/log.h> |
| 33 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 34 | #include <sysutils/NetlinkEvent.h> |
| 35 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 36 | #include "VolumeManager.h" |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 37 | #include "DirectVolume.h" |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 38 | #include "ResponseCode.h" |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 39 | #include "Loop.h" |
| 40 | #include "Fat.h" |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 41 | #include "Devmapper.h" |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 42 | #include "Process.h" |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 43 | #include "Asec.h" |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 44 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 45 | VolumeManager *VolumeManager::sInstance = NULL; |
| 46 | |
| 47 | VolumeManager *VolumeManager::Instance() { |
| 48 | if (!sInstance) |
| 49 | sInstance = new VolumeManager(); |
| 50 | return sInstance; |
| 51 | } |
| 52 | |
| 53 | VolumeManager::VolumeManager() { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 54 | mDebug = false; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 55 | mVolumes = new VolumeCollection(); |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 56 | mActiveContainers = new AsecIdCollection(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 57 | mBroadcaster = NULL; |
Mike Lockwood | 99635f6 | 2010-06-25 23:04:04 -0400 | [diff] [blame] | 58 | mUsbMassStorageEnabled = false; |
| 59 | mUsbConnected = false; |
| 60 | |
| 61 | readInitialState(); |
| 62 | } |
| 63 | |
| 64 | void VolumeManager::readInitialState() { |
| 65 | FILE *fp; |
| 66 | char state[255]; |
| 67 | |
| 68 | /* |
| 69 | * Read the initial mass storage enabled state |
| 70 | */ |
| 71 | if ((fp = fopen("/sys/devices/virtual/usb_composite/usb_mass_storage/enable", "r"))) { |
| 72 | if (fgets(state, sizeof(state), fp)) { |
| 73 | mUsbMassStorageEnabled = !strncmp(state, "1", 1); |
| 74 | } else { |
| 75 | SLOGE("Failed to read usb_mass_storage enabled state (%s)", strerror(errno)); |
| 76 | } |
| 77 | fclose(fp); |
| 78 | } else { |
| 79 | SLOGD("USB mass storage support is not enabled in the kernel"); |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Read the initial USB connected state |
| 84 | */ |
| 85 | if ((fp = fopen("/sys/devices/virtual/switch/usb_configuration/state", "r"))) { |
| 86 | if (fgets(state, sizeof(state), fp)) { |
| 87 | mUsbConnected = !strncmp(state, "1", 1); |
| 88 | } else { |
| 89 | SLOGE("Failed to read usb_configuration switch (%s)", strerror(errno)); |
| 90 | } |
| 91 | fclose(fp); |
| 92 | } else { |
| 93 | SLOGD("usb_configuration switch is not enabled in the kernel"); |
| 94 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | VolumeManager::~VolumeManager() { |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 98 | delete mVolumes; |
| 99 | delete mActiveContainers; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 102 | char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) { |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 103 | static const char* digits = "0123456789abcdef"; |
| 104 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 105 | unsigned char sig[MD5_DIGEST_LENGTH]; |
| 106 | |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 107 | if (buffer == NULL) { |
| 108 | SLOGE("Destination buffer is NULL"); |
| 109 | errno = ESPIPE; |
| 110 | return NULL; |
| 111 | } else if (id == NULL) { |
| 112 | SLOGE("Source buffer is NULL"); |
| 113 | errno = ESPIPE; |
| 114 | return NULL; |
| 115 | } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) { |
| 116 | SLOGE("Target hash buffer size < %d bytes (%d)", |
| 117 | MD5_ASCII_LENGTH_PLUS_NULL, len); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 118 | errno = ESPIPE; |
| 119 | return NULL; |
| 120 | } |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 121 | |
| 122 | MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 123 | |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 124 | char *p = buffer; |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 125 | for (int i = 0; i < MD5_DIGEST_LENGTH; i++) { |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 126 | *p++ = digits[sig[i] >> 4]; |
| 127 | *p++ = digits[sig[i] & 0x0F]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 128 | } |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 129 | *p = '\0'; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 130 | |
| 131 | return buffer; |
| 132 | } |
| 133 | |
| 134 | void VolumeManager::setDebug(bool enable) { |
| 135 | mDebug = enable; |
| 136 | VolumeCollection::iterator it; |
| 137 | for (it = mVolumes->begin(); it != mVolumes->end(); ++it) { |
| 138 | (*it)->setDebug(enable); |
| 139 | } |
| 140 | } |
| 141 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 142 | int VolumeManager::start() { |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | int VolumeManager::stop() { |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | int VolumeManager::addVolume(Volume *v) { |
| 151 | mVolumes->push_back(v); |
| 152 | return 0; |
| 153 | } |
| 154 | |
Mike Lockwood | 99635f6 | 2010-06-25 23:04:04 -0400 | [diff] [blame] | 155 | void VolumeManager::notifyUmsAvailable(bool available) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 156 | char msg[255]; |
| 157 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 158 | snprintf(msg, sizeof(msg), "Share method ums now %s", |
Mike Lockwood | 99635f6 | 2010-06-25 23:04:04 -0400 | [diff] [blame] | 159 | (available ? "available" : "unavailable")); |
| 160 | SLOGD(msg); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 161 | getBroadcaster()->sendBroadcast(ResponseCode::ShareAvailabilityChange, |
| 162 | msg, false); |
| 163 | } |
| 164 | |
| 165 | void VolumeManager::handleSwitchEvent(NetlinkEvent *evt) { |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 166 | const char *devpath = evt->findParam("DEVPATH"); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 167 | const char *name = evt->findParam("SWITCH_NAME"); |
| 168 | const char *state = evt->findParam("SWITCH_STATE"); |
| 169 | |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 170 | if (!name || !state) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 171 | SLOGW("Switch %s event missing name/state info", devpath); |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 172 | return; |
| 173 | } |
| 174 | |
Mike Lockwood | 99635f6 | 2010-06-25 23:04:04 -0400 | [diff] [blame] | 175 | bool oldAvailable = massStorageAvailable(); |
| 176 | if (!strcmp(name, "usb_configuration")) { |
| 177 | mUsbConnected = !strcmp(state, "1"); |
| 178 | SLOGD("USB %s", mUsbConnected ? "connected" : "disconnected"); |
| 179 | bool newAvailable = massStorageAvailable(); |
| 180 | if (newAvailable != oldAvailable) { |
| 181 | notifyUmsAvailable(newAvailable); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 182 | } |
| 183 | } else { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 184 | SLOGW("Ignoring unknown switch '%s'", name); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 185 | } |
| 186 | } |
Mike Lockwood | 99635f6 | 2010-06-25 23:04:04 -0400 | [diff] [blame] | 187 | void VolumeManager::handleUsbCompositeEvent(NetlinkEvent *evt) { |
| 188 | const char *function = evt->findParam("FUNCTION"); |
| 189 | const char *enabled = evt->findParam("ENABLED"); |
| 190 | |
| 191 | if (!function || !enabled) { |
| 192 | SLOGW("usb_composite event missing function/enabled info"); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | if (!strcmp(function, "usb_mass_storage")) { |
| 197 | bool oldAvailable = massStorageAvailable(); |
| 198 | mUsbMassStorageEnabled = !strcmp(enabled, "1"); |
| 199 | SLOGD("usb_mass_storage function %s", mUsbMassStorageEnabled ? "enabled" : "disabled"); |
| 200 | bool newAvailable = massStorageAvailable(); |
| 201 | if (newAvailable != oldAvailable) { |
| 202 | notifyUmsAvailable(newAvailable); |
| 203 | } |
| 204 | } |
| 205 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 206 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 207 | void VolumeManager::handleBlockEvent(NetlinkEvent *evt) { |
| 208 | const char *devpath = evt->findParam("DEVPATH"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 209 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 210 | /* Lookup a volume to handle this device */ |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 211 | VolumeCollection::iterator it; |
| 212 | bool hit = false; |
| 213 | for (it = mVolumes->begin(); it != mVolumes->end(); ++it) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 214 | if (!(*it)->handleBlockEvent(evt)) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 215 | #ifdef NETLINK_DEBUG |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 216 | SLOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 217 | #endif |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 218 | hit = true; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 219 | break; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | if (!hit) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 224 | #ifdef NETLINK_DEBUG |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 225 | SLOGW("No volumes handled block event for '%s'", devpath); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 226 | #endif |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 230 | int VolumeManager::listVolumes(SocketClient *cli) { |
| 231 | VolumeCollection::iterator i; |
| 232 | |
| 233 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
| 234 | char *buffer; |
| 235 | asprintf(&buffer, "%s %s %d", |
| 236 | (*i)->getLabel(), (*i)->getMountpoint(), |
| 237 | (*i)->getState()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 238 | cli->sendMsg(ResponseCode::VolumeListResult, buffer, false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 239 | free(buffer); |
| 240 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 241 | cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 242 | return 0; |
| 243 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 244 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 245 | int VolumeManager::formatVolume(const char *label) { |
| 246 | Volume *v = lookupVolume(label); |
| 247 | |
| 248 | if (!v) { |
| 249 | errno = ENOENT; |
| 250 | return -1; |
| 251 | } |
| 252 | |
| 253 | return v->formatVol(); |
| 254 | } |
| 255 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 256 | int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) { |
| 257 | char idHash[33]; |
| 258 | if (!asecHash(sourceFile, idHash, sizeof(idHash))) { |
| 259 | SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno)); |
| 260 | return -1; |
| 261 | } |
| 262 | |
| 263 | memset(mountPath, 0, mountPathLen); |
| 264 | snprintf(mountPath, mountPathLen, "%s/%s", Volume::LOOPDIR, idHash); |
| 265 | |
| 266 | if (access(mountPath, F_OK)) { |
| 267 | errno = ENOENT; |
| 268 | return -1; |
| 269 | } |
| 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 274 | int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) { |
San Mehat | 88ac2c0 | 2010-03-23 11:15:58 -0700 | [diff] [blame] | 275 | char asecFileName[255]; |
| 276 | snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id); |
| 277 | |
| 278 | memset(buffer, 0, maxlen); |
| 279 | if (access(asecFileName, F_OK)) { |
| 280 | errno = ENOENT; |
| 281 | return -1; |
| 282 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 283 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 284 | snprintf(buffer, maxlen, "%s/%s", Volume::ASECDIR, id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 285 | return 0; |
| 286 | } |
| 287 | |
San Mehat | 8b8f71b | 2010-01-11 09:17:25 -0800 | [diff] [blame] | 288 | int VolumeManager::createAsec(const char *id, unsigned int numSectors, |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 289 | const char *fstype, const char *key, int ownerUid) { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 290 | struct asec_superblock sb; |
| 291 | memset(&sb, 0, sizeof(sb)); |
| 292 | |
| 293 | sb.magic = ASEC_SB_MAGIC; |
| 294 | sb.ver = ASEC_SB_VER; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 295 | |
San Mehat | d31e380 | 2010-02-18 08:37:45 -0800 | [diff] [blame] | 296 | if (numSectors < ((1024*1024)/512)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 297 | SLOGE("Invalid container size specified (%d sectors)", numSectors); |
San Mehat | d31e380 | 2010-02-18 08:37:45 -0800 | [diff] [blame] | 298 | errno = EINVAL; |
| 299 | return -1; |
| 300 | } |
| 301 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 302 | if (lookupVolume(id)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 303 | SLOGE("ASEC id '%s' currently exists", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 304 | errno = EADDRINUSE; |
| 305 | return -1; |
| 306 | } |
| 307 | |
| 308 | char asecFileName[255]; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 309 | snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 310 | |
| 311 | if (!access(asecFileName, F_OK)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 312 | SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)", |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 313 | asecFileName, strerror(errno)); |
| 314 | errno = EADDRINUSE; |
| 315 | return -1; |
| 316 | } |
| 317 | |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 318 | /* |
| 319 | * Add some headroom |
| 320 | */ |
| 321 | unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2; |
| 322 | unsigned numImgSectors = numSectors + fatSize + 2; |
| 323 | |
| 324 | if (numImgSectors % 63) { |
| 325 | numImgSectors += (63 - (numImgSectors % 63)); |
| 326 | } |
| 327 | |
| 328 | // Add +1 for our superblock which is at the end |
| 329 | if (Loop::createImageFile(asecFileName, numImgSectors + 1)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 330 | SLOGE("ASEC image file creation failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 331 | return -1; |
| 332 | } |
| 333 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 334 | char idHash[33]; |
| 335 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 336 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 337 | unlink(asecFileName); |
| 338 | return -1; |
| 339 | } |
| 340 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 341 | char loopDevice[255]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 342 | if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 343 | SLOGE("ASEC loop device creation failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 344 | unlink(asecFileName); |
| 345 | return -1; |
| 346 | } |
| 347 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 348 | char dmDevice[255]; |
| 349 | bool cleanupDm = false; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 350 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 351 | if (strcmp(key, "none")) { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 352 | // XXX: This is all we support for now |
| 353 | sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 354 | if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice, |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 355 | sizeof(dmDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 356 | SLOGE("ASEC device mapping failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 357 | Loop::destroyByDevice(loopDevice); |
| 358 | unlink(asecFileName); |
| 359 | return -1; |
| 360 | } |
| 361 | cleanupDm = true; |
| 362 | } else { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 363 | sb.c_cipher = ASEC_SB_C_CIPHER_NONE; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 364 | strcpy(dmDevice, loopDevice); |
| 365 | } |
| 366 | |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 367 | /* |
| 368 | * Drop down the superblock at the end of the file |
| 369 | */ |
| 370 | |
| 371 | int sbfd = open(loopDevice, O_RDWR); |
| 372 | if (sbfd < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 373 | 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] | 374 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 375 | Devmapper::destroy(idHash); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 376 | } |
| 377 | Loop::destroyByDevice(loopDevice); |
| 378 | unlink(asecFileName); |
| 379 | return -1; |
| 380 | } |
| 381 | |
| 382 | if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) { |
| 383 | close(sbfd); |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 384 | SLOGE("Failed to lseek for superblock (%s)", strerror(errno)); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 385 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 386 | Devmapper::destroy(idHash); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 387 | } |
| 388 | Loop::destroyByDevice(loopDevice); |
| 389 | unlink(asecFileName); |
| 390 | return -1; |
| 391 | } |
| 392 | |
| 393 | if (write(sbfd, &sb, sizeof(sb)) != sizeof(sb)) { |
| 394 | close(sbfd); |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 395 | SLOGE("Failed to write superblock (%s)", strerror(errno)); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 396 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 397 | Devmapper::destroy(idHash); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 398 | } |
| 399 | Loop::destroyByDevice(loopDevice); |
| 400 | unlink(asecFileName); |
| 401 | return -1; |
| 402 | } |
| 403 | close(sbfd); |
| 404 | |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 405 | if (strcmp(fstype, "none")) { |
| 406 | if (strcmp(fstype, "fat")) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 407 | SLOGW("Unknown fstype '%s' specified for container", fstype); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 408 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 409 | |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 410 | if (Fat::format(dmDevice, numImgSectors)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 411 | SLOGE("ASEC FAT format failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 412 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 413 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 414 | } |
San Mehat | eb13a90 | 2010-01-07 12:12:50 -0800 | [diff] [blame] | 415 | Loop::destroyByDevice(loopDevice); |
| 416 | unlink(asecFileName); |
| 417 | return -1; |
| 418 | } |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 419 | char mountPoint[255]; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 420 | |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 421 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
| 422 | if (mkdir(mountPoint, 0777)) { |
| 423 | if (errno != EEXIST) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 424 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 425 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 426 | Devmapper::destroy(idHash); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 427 | } |
| 428 | Loop::destroyByDevice(loopDevice); |
| 429 | unlink(asecFileName); |
| 430 | return -1; |
| 431 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 432 | } |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 433 | |
Kenny Root | a3e0608 | 2010-08-27 08:31:35 -0700 | [diff] [blame^] | 434 | if (Fat::doMount(dmDevice, mountPoint, false, false, false, ownerUid, |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 435 | 0, 0000, false)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 436 | SLOGE("ASEC FAT mount failed (%s)", strerror(errno)); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 437 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 438 | Devmapper::destroy(idHash); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 439 | } |
| 440 | Loop::destroyByDevice(loopDevice); |
| 441 | unlink(asecFileName); |
| 442 | return -1; |
| 443 | } |
| 444 | } else { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 445 | SLOGI("Created raw secure container %s (no filesystem)", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 446 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 447 | |
| 448 | mActiveContainers->push_back(strdup(id)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | int VolumeManager::finalizeAsec(const char *id) { |
| 453 | char asecFileName[255]; |
| 454 | char loopDevice[255]; |
| 455 | char mountPoint[255]; |
| 456 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 457 | snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id); |
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 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 470 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 471 | // XXX: |
Kenny Root | a3e0608 | 2010-08-27 08:31:35 -0700 | [diff] [blame^] | 472 | if (Fat::doMount(loopDevice, mountPoint, true, true, true, 0, 0, 0227, false)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 473 | SLOGE("ASEC finalize mount failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 474 | return -1; |
| 475 | } |
| 476 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 477 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 478 | SLOGD("ASEC %s finalized", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 479 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 480 | return 0; |
| 481 | } |
| 482 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 483 | int VolumeManager::renameAsec(const char *id1, const char *id2) { |
| 484 | char *asecFilename1; |
| 485 | char *asecFilename2; |
| 486 | char mountPoint[255]; |
| 487 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 488 | asprintf(&asecFilename1, "%s/%s.asec", Volume::SEC_ASECDIR, id1); |
| 489 | asprintf(&asecFilename2, "%s/%s.asec", Volume::SEC_ASECDIR, id2); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 490 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 491 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id1); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 492 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 493 | SLOGW("Rename attempt when src mounted"); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 494 | errno = EBUSY; |
| 495 | goto out_err; |
| 496 | } |
| 497 | |
San Mehat | 96956ed | 2010-02-24 08:42:51 -0800 | [diff] [blame] | 498 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id2); |
| 499 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 500 | SLOGW("Rename attempt when dst mounted"); |
San Mehat | 96956ed | 2010-02-24 08:42:51 -0800 | [diff] [blame] | 501 | errno = EBUSY; |
| 502 | goto out_err; |
| 503 | } |
| 504 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 505 | if (!access(asecFilename2, F_OK)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 506 | SLOGE("Rename attempt when dst exists"); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 507 | errno = EADDRINUSE; |
| 508 | goto out_err; |
| 509 | } |
| 510 | |
| 511 | if (rename(asecFilename1, asecFilename2)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 512 | SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno)); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 513 | goto out_err; |
| 514 | } |
| 515 | |
| 516 | free(asecFilename1); |
| 517 | free(asecFilename2); |
| 518 | return 0; |
| 519 | |
| 520 | out_err: |
| 521 | free(asecFilename1); |
| 522 | free(asecFilename2); |
| 523 | return -1; |
| 524 | } |
| 525 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 526 | #define UNMOUNT_RETRIES 5 |
| 527 | #define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000) |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 528 | int VolumeManager::unmountAsec(const char *id, bool force) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 529 | char asecFileName[255]; |
| 530 | char mountPoint[255]; |
| 531 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 532 | snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id); |
| 533 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 534 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 535 | char idHash[33]; |
| 536 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 537 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 538 | return -1; |
| 539 | } |
| 540 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 541 | return unmountLoopImage(id, idHash, asecFileName, mountPoint, force); |
| 542 | } |
| 543 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 544 | int VolumeManager::unmountObb(const char *fileName, bool force) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 545 | char mountPoint[255]; |
| 546 | |
| 547 | char idHash[33]; |
| 548 | if (!asecHash(fileName, idHash, sizeof(idHash))) { |
| 549 | SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno)); |
| 550 | return -1; |
| 551 | } |
| 552 | |
| 553 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash); |
| 554 | |
| 555 | return unmountLoopImage(fileName, idHash, fileName, mountPoint, force); |
| 556 | } |
| 557 | |
| 558 | int VolumeManager::unmountLoopImage(const char *id, const char *idHash, |
| 559 | const char *fileName, const char *mountPoint, bool force) { |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 560 | if (!isMountpointMounted(mountPoint)) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 561 | SLOGE("Unmount request for %s when not mounted", id); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 562 | errno = EINVAL; |
| 563 | return -1; |
| 564 | } |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 565 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 566 | int i, rc; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 567 | for (i = 1; i <= UNMOUNT_RETRIES; i++) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 568 | rc = umount(mountPoint); |
| 569 | if (!rc) { |
| 570 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 571 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 572 | if (rc && (errno == EINVAL || errno == ENOENT)) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 573 | SLOGI("Container %s unmounted OK", id); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 574 | rc = 0; |
| 575 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 576 | } |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 577 | SLOGW("%s unmount attempt %d failed (%s)", |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 578 | id, i, strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 579 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 580 | int action = 0; // default is to just complain |
| 581 | |
| 582 | if (force) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 583 | if (i > (UNMOUNT_RETRIES - 2)) |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 584 | action = 2; // SIGKILL |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 585 | else if (i > (UNMOUNT_RETRIES - 3)) |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 586 | action = 1; // SIGHUP |
| 587 | } |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 588 | |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 589 | Process::killProcessesWithOpenFiles(mountPoint, action); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 590 | usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | if (rc) { |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 594 | errno = EBUSY; |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 595 | SLOGE("Failed to unmount container %s (%s)", id, strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 596 | return -1; |
| 597 | } |
| 598 | |
San Mehat | 12f4b89 | 2010-02-24 11:43:22 -0800 | [diff] [blame] | 599 | int retries = 10; |
| 600 | |
| 601 | while(retries--) { |
| 602 | if (!rmdir(mountPoint)) { |
| 603 | break; |
| 604 | } |
| 605 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 606 | SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno)); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 607 | usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS); |
San Mehat | 12f4b89 | 2010-02-24 11:43:22 -0800 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | if (!retries) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 611 | SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno)); |
San Mehat | f5c6198 | 2010-02-03 11:04:46 -0800 | [diff] [blame] | 612 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 613 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 614 | if (Devmapper::destroy(idHash) && errno != ENXIO) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 615 | SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | char loopDevice[255]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 619 | if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 620 | Loop::destroyByDevice(loopDevice); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 621 | } else { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 622 | SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 623 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 624 | |
| 625 | AsecIdCollection::iterator it; |
| 626 | for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) { |
| 627 | if (!strcmp(*it, id)) { |
| 628 | free(*it); |
| 629 | mActiveContainers->erase(it); |
| 630 | break; |
| 631 | } |
| 632 | } |
| 633 | if (it == mActiveContainers->end()) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 634 | SLOGW("mActiveContainers is inconsistent!"); |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 635 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 636 | return 0; |
| 637 | } |
| 638 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 639 | int VolumeManager::destroyAsec(const char *id, bool force) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 640 | char asecFileName[255]; |
| 641 | char mountPoint[255]; |
| 642 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 643 | snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id); |
San Mehat | 55013f7 | 2010-02-24 12:12:34 -0800 | [diff] [blame] | 644 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 645 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 646 | if (isMountpointMounted(mountPoint)) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 647 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 648 | SLOGD("Unmounting container before destroy"); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 649 | } |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 650 | if (unmountAsec(id, force)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 651 | SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno)); |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 652 | return -1; |
| 653 | } |
| 654 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 655 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 656 | if (unlink(asecFileName)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 657 | SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno)); |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 658 | return -1; |
| 659 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 660 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 661 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 662 | SLOGD("ASEC %s destroyed", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 663 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid) { |
| 668 | char asecFileName[255]; |
| 669 | char mountPoint[255]; |
| 670 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 671 | snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id); |
| 672 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 673 | |
| 674 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 675 | SLOGE("ASEC %s already mounted", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 676 | errno = EBUSY; |
| 677 | return -1; |
| 678 | } |
| 679 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 680 | char idHash[33]; |
| 681 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 682 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 683 | return -1; |
| 684 | } |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 685 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 686 | char loopDevice[255]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 687 | if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
| 688 | if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 689 | SLOGE("ASEC loop device creation failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 690 | return -1; |
| 691 | } |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 692 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 693 | SLOGD("New loop device created at %s", loopDevice); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 694 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 695 | } else { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 696 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 697 | SLOGD("Found active loopback for %s at %s", asecFileName, loopDevice); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 698 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | char dmDevice[255]; |
| 702 | bool cleanupDm = false; |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 703 | int fd; |
| 704 | unsigned int nr_sec = 0; |
| 705 | |
| 706 | if ((fd = open(loopDevice, O_RDWR)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 707 | SLOGE("Failed to open loopdevice (%s)", strerror(errno)); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 708 | Loop::destroyByDevice(loopDevice); |
| 709 | return -1; |
| 710 | } |
| 711 | |
| 712 | if (ioctl(fd, BLKGETSIZE, &nr_sec)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 713 | SLOGE("Failed to get loop size (%s)", strerror(errno)); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 714 | Loop::destroyByDevice(loopDevice); |
| 715 | close(fd); |
| 716 | return -1; |
| 717 | } |
| 718 | |
| 719 | /* |
| 720 | * Validate superblock |
| 721 | */ |
| 722 | struct asec_superblock sb; |
| 723 | memset(&sb, 0, sizeof(sb)); |
| 724 | if (lseek(fd, ((nr_sec-1) * 512), SEEK_SET) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 725 | SLOGE("lseek failed (%s)", strerror(errno)); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 726 | close(fd); |
| 727 | Loop::destroyByDevice(loopDevice); |
| 728 | return -1; |
| 729 | } |
| 730 | if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 731 | SLOGE("superblock read failed (%s)", strerror(errno)); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 732 | close(fd); |
| 733 | Loop::destroyByDevice(loopDevice); |
| 734 | return -1; |
| 735 | } |
| 736 | |
| 737 | close(fd); |
| 738 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 739 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 740 | SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 741 | } |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 742 | if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 743 | SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 744 | Loop::destroyByDevice(loopDevice); |
| 745 | errno = EMEDIUMTYPE; |
| 746 | return -1; |
| 747 | } |
| 748 | nr_sec--; // We don't want the devmapping to extend onto our superblock |
| 749 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 750 | if (strcmp(key, "none")) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 751 | if (Devmapper::lookupActive(idHash, dmDevice, sizeof(dmDevice))) { |
| 752 | if (Devmapper::create(idHash, loopDevice, key, nr_sec, |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 753 | dmDevice, sizeof(dmDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 754 | SLOGE("ASEC device mapping failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 755 | Loop::destroyByDevice(loopDevice); |
| 756 | return -1; |
| 757 | } |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 758 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 759 | SLOGD("New devmapper instance created at %s", dmDevice); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 760 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 761 | } else { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 762 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 763 | SLOGD("Found active devmapper for %s at %s", asecFileName, dmDevice); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 764 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 765 | } |
| 766 | cleanupDm = true; |
| 767 | } else { |
| 768 | strcpy(dmDevice, loopDevice); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | if (mkdir(mountPoint, 0777)) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 772 | if (errno != EEXIST) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 773 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 774 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 775 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 776 | } |
| 777 | Loop::destroyByDevice(loopDevice); |
| 778 | return -1; |
| 779 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 780 | } |
| 781 | |
Kenny Root | a3e0608 | 2010-08-27 08:31:35 -0700 | [diff] [blame^] | 782 | if (Fat::doMount(dmDevice, mountPoint, true, false, true, ownerUid, 0, |
San Mehat | cff5ec3 | 2010-01-08 12:31:44 -0800 | [diff] [blame] | 783 | 0222, false)) { |
| 784 | // 0227, false)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 785 | SLOGE("ASEC mount failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 786 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 787 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 788 | } |
| 789 | Loop::destroyByDevice(loopDevice); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 790 | return -1; |
| 791 | } |
| 792 | |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 793 | mActiveContainers->push_back(strdup(id)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 794 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 795 | SLOGD("ASEC %s mounted", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 796 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 797 | return 0; |
| 798 | } |
| 799 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 800 | /** |
| 801 | * Mounts an image file <code>img</code>. |
| 802 | */ |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 803 | int VolumeManager::mountObb(const char *img, const char *key, int ownerUid) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 804 | char mountPoint[255]; |
| 805 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 806 | char idHash[33]; |
| 807 | if (!asecHash(img, idHash, sizeof(idHash))) { |
| 808 | SLOGE("Hash of '%s' failed (%s)", img, strerror(errno)); |
| 809 | return -1; |
| 810 | } |
| 811 | |
| 812 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash); |
| 813 | |
| 814 | if (isMountpointMounted(mountPoint)) { |
| 815 | SLOGE("Image %s already mounted", img); |
| 816 | errno = EBUSY; |
| 817 | return -1; |
| 818 | } |
| 819 | |
| 820 | char loopDevice[255]; |
| 821 | if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
| 822 | if (Loop::create(idHash, img, loopDevice, sizeof(loopDevice))) { |
| 823 | SLOGE("Image loop device creation failed (%s)", strerror(errno)); |
| 824 | return -1; |
| 825 | } |
| 826 | if (mDebug) { |
| 827 | SLOGD("New loop device created at %s", loopDevice); |
| 828 | } |
| 829 | } else { |
| 830 | if (mDebug) { |
| 831 | SLOGD("Found active loopback for %s at %s", img, loopDevice); |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | char dmDevice[255]; |
| 836 | bool cleanupDm = false; |
| 837 | int fd; |
| 838 | unsigned int nr_sec = 0; |
| 839 | |
| 840 | if ((fd = open(loopDevice, O_RDWR)) < 0) { |
| 841 | SLOGE("Failed to open loopdevice (%s)", strerror(errno)); |
| 842 | Loop::destroyByDevice(loopDevice); |
| 843 | return -1; |
| 844 | } |
| 845 | |
| 846 | if (ioctl(fd, BLKGETSIZE, &nr_sec)) { |
| 847 | SLOGE("Failed to get loop size (%s)", strerror(errno)); |
| 848 | Loop::destroyByDevice(loopDevice); |
| 849 | close(fd); |
| 850 | return -1; |
| 851 | } |
| 852 | |
| 853 | close(fd); |
| 854 | |
| 855 | if (strcmp(key, "none")) { |
| 856 | if (Devmapper::lookupActive(idHash, dmDevice, sizeof(dmDevice))) { |
| 857 | if (Devmapper::create(idHash, loopDevice, key, nr_sec, |
| 858 | dmDevice, sizeof(dmDevice))) { |
| 859 | SLOGE("ASEC device mapping failed (%s)", strerror(errno)); |
| 860 | Loop::destroyByDevice(loopDevice); |
| 861 | return -1; |
| 862 | } |
| 863 | if (mDebug) { |
| 864 | SLOGD("New devmapper instance created at %s", dmDevice); |
| 865 | } |
| 866 | } else { |
| 867 | if (mDebug) { |
| 868 | SLOGD("Found active devmapper for %s at %s", img, dmDevice); |
| 869 | } |
| 870 | } |
| 871 | cleanupDm = true; |
| 872 | } else { |
| 873 | strcpy(dmDevice, loopDevice); |
| 874 | } |
| 875 | |
| 876 | if (mkdir(mountPoint, 0755)) { |
| 877 | if (errno != EEXIST) { |
| 878 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
| 879 | if (cleanupDm) { |
| 880 | Devmapper::destroy(idHash); |
| 881 | } |
| 882 | Loop::destroyByDevice(loopDevice); |
| 883 | return -1; |
| 884 | } |
| 885 | } |
| 886 | |
Kenny Root | a3e0608 | 2010-08-27 08:31:35 -0700 | [diff] [blame^] | 887 | if (Fat::doMount(dmDevice, mountPoint, true, false, true, ownerUid, 0, |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 888 | 0227, false)) { |
| 889 | SLOGE("Image mount failed (%s)", strerror(errno)); |
| 890 | if (cleanupDm) { |
| 891 | Devmapper::destroy(idHash); |
| 892 | } |
| 893 | Loop::destroyByDevice(loopDevice); |
| 894 | return -1; |
| 895 | } |
| 896 | |
| 897 | mActiveContainers->push_back(strdup(img)); |
| 898 | if (mDebug) { |
| 899 | SLOGD("Image %s mounted", img); |
| 900 | } |
| 901 | return 0; |
| 902 | } |
| 903 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 904 | int VolumeManager::mountVolume(const char *label) { |
| 905 | Volume *v = lookupVolume(label); |
| 906 | |
| 907 | if (!v) { |
| 908 | errno = ENOENT; |
| 909 | return -1; |
| 910 | } |
| 911 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 912 | return v->mountVol(); |
| 913 | } |
| 914 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 915 | int VolumeManager::listMountedObbs(SocketClient* cli) { |
| 916 | char device[256]; |
| 917 | char mount_path[256]; |
| 918 | char rest[256]; |
| 919 | FILE *fp; |
| 920 | char line[1024]; |
| 921 | |
| 922 | if (!(fp = fopen("/proc/mounts", "r"))) { |
| 923 | SLOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
| 924 | return -1; |
| 925 | } |
| 926 | |
| 927 | // Create a string to compare against that has a trailing slash |
| 928 | int loopDirLen = sizeof(Volume::LOOPDIR); |
| 929 | char loopDir[loopDirLen + 2]; |
| 930 | strcpy(loopDir, Volume::LOOPDIR); |
| 931 | loopDir[loopDirLen++] = '/'; |
| 932 | loopDir[loopDirLen] = '\0'; |
| 933 | |
| 934 | while(fgets(line, sizeof(line), fp)) { |
| 935 | line[strlen(line)-1] = '\0'; |
| 936 | |
| 937 | /* |
| 938 | * Should look like: |
| 939 | * /dev/block/loop0 /mnt/obb/fc99df1323fd36424f864dcb76b76d65 ... |
| 940 | */ |
| 941 | sscanf(line, "%255s %255s %255s\n", device, mount_path, rest); |
| 942 | |
| 943 | if (!strncmp(mount_path, loopDir, loopDirLen)) { |
| 944 | int fd = open(device, O_RDONLY); |
| 945 | if (fd >= 0) { |
| 946 | struct loop_info64 li; |
| 947 | if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) { |
| 948 | cli->sendMsg(ResponseCode::AsecListResult, |
| 949 | (const char*) li.lo_file_name, false); |
| 950 | } |
| 951 | close(fd); |
| 952 | } |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | fclose(fp); |
| 957 | return 0; |
| 958 | } |
| 959 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 960 | int VolumeManager::shareAvailable(const char *method, bool *avail) { |
| 961 | |
| 962 | if (strcmp(method, "ums")) { |
| 963 | errno = ENOSYS; |
| 964 | return -1; |
| 965 | } |
| 966 | |
Mike Lockwood | 99635f6 | 2010-06-25 23:04:04 -0400 | [diff] [blame] | 967 | *avail = massStorageAvailable(); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 968 | return 0; |
| 969 | } |
| 970 | |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 971 | int VolumeManager::shareEnabled(const char *label, const char *method, bool *enabled) { |
| 972 | Volume *v = lookupVolume(label); |
| 973 | |
| 974 | if (!v) { |
| 975 | errno = ENOENT; |
| 976 | return -1; |
| 977 | } |
| 978 | |
| 979 | if (strcmp(method, "ums")) { |
| 980 | errno = ENOSYS; |
| 981 | return -1; |
| 982 | } |
| 983 | |
| 984 | if (v->getState() != Volume::State_Shared) { |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 985 | *enabled = false; |
San Mehat | b9aed74 | 2010-02-04 15:07:01 -0800 | [diff] [blame] | 986 | } else { |
| 987 | *enabled = true; |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 988 | } |
| 989 | return 0; |
| 990 | } |
| 991 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 992 | int VolumeManager::simulate(const char *cmd, const char *arg) { |
| 993 | |
| 994 | if (!strcmp(cmd, "ums")) { |
| 995 | if (!strcmp(arg, "connect")) { |
Mike Lockwood | 99635f6 | 2010-06-25 23:04:04 -0400 | [diff] [blame] | 996 | notifyUmsAvailable(true); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 997 | } else if (!strcmp(arg, "disconnect")) { |
Mike Lockwood | 99635f6 | 2010-06-25 23:04:04 -0400 | [diff] [blame] | 998 | notifyUmsAvailable(false); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 999 | } else { |
| 1000 | errno = EINVAL; |
| 1001 | return -1; |
| 1002 | } |
| 1003 | } else { |
| 1004 | errno = EINVAL; |
| 1005 | return -1; |
| 1006 | } |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
| 1010 | int VolumeManager::shareVolume(const char *label, const char *method) { |
| 1011 | Volume *v = lookupVolume(label); |
| 1012 | |
| 1013 | if (!v) { |
| 1014 | errno = ENOENT; |
| 1015 | return -1; |
| 1016 | } |
| 1017 | |
| 1018 | /* |
| 1019 | * Eventually, we'll want to support additional share back-ends, |
| 1020 | * some of which may work while the media is mounted. For now, |
| 1021 | * we just support UMS |
| 1022 | */ |
| 1023 | if (strcmp(method, "ums")) { |
| 1024 | errno = ENOSYS; |
| 1025 | return -1; |
| 1026 | } |
| 1027 | |
| 1028 | if (v->getState() == Volume::State_NoMedia) { |
| 1029 | errno = ENODEV; |
| 1030 | return -1; |
| 1031 | } |
| 1032 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1033 | if (v->getState() != Volume::State_Idle) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1034 | // You need to unmount manually befoe sharing |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1035 | errno = EBUSY; |
| 1036 | return -1; |
| 1037 | } |
| 1038 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1039 | dev_t d = v->getDiskDevice(); |
| 1040 | if ((MAJOR(d) == 0) && (MINOR(d) == 0)) { |
| 1041 | // This volume does not support raw disk access |
| 1042 | errno = EINVAL; |
| 1043 | return -1; |
| 1044 | } |
| 1045 | |
| 1046 | int fd; |
| 1047 | char nodepath[255]; |
| 1048 | snprintf(nodepath, |
| 1049 | sizeof(nodepath), "/dev/block/vold/%d:%d", |
| 1050 | MAJOR(d), MINOR(d)); |
| 1051 | |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 1052 | if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0/file", |
| 1053 | O_WRONLY)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1054 | SLOGE("Unable to open ums lunfile (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1055 | return -1; |
| 1056 | } |
| 1057 | |
| 1058 | if (write(fd, nodepath, strlen(nodepath)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1059 | SLOGE("Unable to write to ums lunfile (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1060 | close(fd); |
| 1061 | return -1; |
| 1062 | } |
| 1063 | |
| 1064 | close(fd); |
| 1065 | v->handleVolumeShared(); |
| 1066 | return 0; |
| 1067 | } |
| 1068 | |
| 1069 | int VolumeManager::unshareVolume(const char *label, const char *method) { |
| 1070 | Volume *v = lookupVolume(label); |
| 1071 | |
| 1072 | if (!v) { |
| 1073 | errno = ENOENT; |
| 1074 | return -1; |
| 1075 | } |
| 1076 | |
| 1077 | if (strcmp(method, "ums")) { |
| 1078 | errno = ENOSYS; |
| 1079 | return -1; |
| 1080 | } |
| 1081 | |
| 1082 | if (v->getState() != Volume::State_Shared) { |
| 1083 | errno = EINVAL; |
| 1084 | return -1; |
| 1085 | } |
| 1086 | |
| 1087 | dev_t d = v->getDiskDevice(); |
| 1088 | |
| 1089 | int fd; |
| 1090 | char nodepath[255]; |
| 1091 | snprintf(nodepath, |
| 1092 | sizeof(nodepath), "/dev/block/vold/%d:%d", |
| 1093 | MAJOR(d), MINOR(d)); |
| 1094 | |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 1095 | if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0/file", O_WRONLY)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1096 | SLOGE("Unable to open ums lunfile (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1097 | return -1; |
| 1098 | } |
| 1099 | |
| 1100 | char ch = 0; |
| 1101 | if (write(fd, &ch, 1) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1102 | SLOGE("Unable to write to ums lunfile (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1103 | close(fd); |
| 1104 | return -1; |
| 1105 | } |
| 1106 | |
| 1107 | close(fd); |
| 1108 | v->handleVolumeUnshared(); |
| 1109 | return 0; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1112 | int VolumeManager::unmountVolume(const char *label, bool force) { |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1113 | Volume *v = lookupVolume(label); |
| 1114 | |
| 1115 | if (!v) { |
| 1116 | errno = ENOENT; |
| 1117 | return -1; |
| 1118 | } |
| 1119 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1120 | if (v->getState() == Volume::State_NoMedia) { |
| 1121 | errno = ENODEV; |
| 1122 | return -1; |
| 1123 | } |
| 1124 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1125 | if (v->getState() != Volume::State_Mounted) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1126 | SLOGW("Attempt to unmount volume which isn't mounted (%d)\n", |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1127 | v->getState()); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1128 | errno = EBUSY; |
| 1129 | return -1; |
| 1130 | } |
| 1131 | |
San Mehat | 1a06eda | 2010-04-15 12:58:50 -0700 | [diff] [blame] | 1132 | cleanupAsec(v, force); |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1133 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1134 | return v->unmountVol(force); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1135 | } |
| 1136 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1137 | /* |
| 1138 | * Looks up a volume by it's label or mount-point |
| 1139 | */ |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1140 | Volume *VolumeManager::lookupVolume(const char *label) { |
| 1141 | VolumeCollection::iterator i; |
| 1142 | |
| 1143 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1144 | if (label[0] == '/') { |
| 1145 | if (!strcmp(label, (*i)->getMountpoint())) |
| 1146 | return (*i); |
| 1147 | } else { |
| 1148 | if (!strcmp(label, (*i)->getLabel())) |
| 1149 | return (*i); |
| 1150 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1151 | } |
| 1152 | return NULL; |
| 1153 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1154 | |
| 1155 | bool VolumeManager::isMountpointMounted(const char *mp) |
| 1156 | { |
| 1157 | char device[256]; |
| 1158 | char mount_path[256]; |
| 1159 | char rest[256]; |
| 1160 | FILE *fp; |
| 1161 | char line[1024]; |
| 1162 | |
| 1163 | if (!(fp = fopen("/proc/mounts", "r"))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1164 | SLOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1165 | return false; |
| 1166 | } |
| 1167 | |
| 1168 | while(fgets(line, sizeof(line), fp)) { |
| 1169 | line[strlen(line)-1] = '\0'; |
| 1170 | sscanf(line, "%255s %255s %255s\n", device, mount_path, rest); |
| 1171 | if (!strcmp(mount_path, mp)) { |
| 1172 | fclose(fp); |
| 1173 | return true; |
| 1174 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | fclose(fp); |
| 1178 | return false; |
| 1179 | } |
| 1180 | |
San Mehat | 1a06eda | 2010-04-15 12:58:50 -0700 | [diff] [blame] | 1181 | int VolumeManager::cleanupAsec(Volume *v, bool force) { |
| 1182 | while(mActiveContainers->size()) { |
| 1183 | AsecIdCollection::iterator it = mActiveContainers->begin(); |
| 1184 | SLOGI("Unmounting ASEC %s (dependant on %s)", *it, v->getMountpoint()); |
| 1185 | if (unmountAsec(*it, force)) { |
| 1186 | SLOGE("Failed to unmount ASEC %s (%s)", *it, strerror(errno)); |
| 1187 | return -1; |
| 1188 | } |
| 1189 | } |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |