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 | |
| 30 | #include <cutils/log.h> |
| 31 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 32 | #include <sysutils/NetlinkEvent.h> |
| 33 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 34 | #include "VolumeManager.h" |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 35 | #include "DirectVolume.h" |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 36 | #include "ResponseCode.h" |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 37 | #include "Loop.h" |
| 38 | #include "Fat.h" |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 39 | #include "Devmapper.h" |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 40 | #include "Process.h" |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 41 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 42 | VolumeManager *VolumeManager::sInstance = NULL; |
| 43 | |
| 44 | VolumeManager *VolumeManager::Instance() { |
| 45 | if (!sInstance) |
| 46 | sInstance = new VolumeManager(); |
| 47 | return sInstance; |
| 48 | } |
| 49 | |
| 50 | VolumeManager::VolumeManager() { |
| 51 | mBlockDevices = new BlockDeviceCollection(); |
| 52 | mVolumes = new VolumeCollection(); |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 53 | mActiveContainers = new AsecIdCollection(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 54 | mBroadcaster = NULL; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 55 | mUsbMassStorageConnected = false; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | VolumeManager::~VolumeManager() { |
| 59 | delete mBlockDevices; |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 60 | delete mVolumes; |
| 61 | delete mActiveContainers; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | int VolumeManager::start() { |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | int VolumeManager::stop() { |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | int VolumeManager::addVolume(Volume *v) { |
| 73 | mVolumes->push_back(v); |
| 74 | return 0; |
| 75 | } |
| 76 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 77 | void VolumeManager::notifyUmsConnected(bool connected) { |
| 78 | char msg[255]; |
| 79 | |
| 80 | if (connected) { |
| 81 | mUsbMassStorageConnected = true; |
| 82 | } else { |
| 83 | mUsbMassStorageConnected = false; |
| 84 | } |
| 85 | snprintf(msg, sizeof(msg), "Share method ums now %s", |
| 86 | (connected ? "available" : "unavailable")); |
| 87 | |
| 88 | getBroadcaster()->sendBroadcast(ResponseCode::ShareAvailabilityChange, |
| 89 | msg, false); |
| 90 | } |
| 91 | |
| 92 | void VolumeManager::handleSwitchEvent(NetlinkEvent *evt) { |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 93 | const char *devpath = evt->findParam("DEVPATH"); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 94 | const char *name = evt->findParam("SWITCH_NAME"); |
| 95 | const char *state = evt->findParam("SWITCH_STATE"); |
| 96 | |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 97 | if (!name || !state) { |
| 98 | LOGW("Switch %s event missing name/state info", devpath); |
| 99 | return; |
| 100 | } |
| 101 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 102 | if (!strcmp(name, "usb_mass_storage")) { |
| 103 | |
| 104 | if (!strcmp(state, "online")) { |
| 105 | notifyUmsConnected(true); |
| 106 | } else { |
| 107 | notifyUmsConnected(false); |
| 108 | } |
| 109 | } else { |
| 110 | LOGW("Ignoring unknown switch '%s'", name); |
| 111 | } |
| 112 | } |
| 113 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 114 | void VolumeManager::handleBlockEvent(NetlinkEvent *evt) { |
| 115 | const char *devpath = evt->findParam("DEVPATH"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 116 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 117 | /* Lookup a volume to handle this device */ |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 118 | VolumeCollection::iterator it; |
| 119 | bool hit = false; |
| 120 | for (it = mVolumes->begin(); it != mVolumes->end(); ++it) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 121 | if (!(*it)->handleBlockEvent(evt)) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 122 | #ifdef NETLINK_DEBUG |
| 123 | LOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel()); |
| 124 | #endif |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 125 | hit = true; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 126 | break; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (!hit) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 131 | #ifdef NETLINK_DEBUG |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 132 | LOGW("No volumes handled block event for '%s'", devpath); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 133 | #endif |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 137 | int VolumeManager::listVolumes(SocketClient *cli) { |
| 138 | VolumeCollection::iterator i; |
| 139 | |
| 140 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
| 141 | char *buffer; |
| 142 | asprintf(&buffer, "%s %s %d", |
| 143 | (*i)->getLabel(), (*i)->getMountpoint(), |
| 144 | (*i)->getState()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 145 | cli->sendMsg(ResponseCode::VolumeListResult, buffer, false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 146 | free(buffer); |
| 147 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 148 | cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 149 | return 0; |
| 150 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 151 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 152 | int VolumeManager::formatVolume(const char *label) { |
| 153 | Volume *v = lookupVolume(label); |
| 154 | |
| 155 | if (!v) { |
| 156 | errno = ENOENT; |
| 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | return v->formatVol(); |
| 161 | } |
| 162 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 163 | int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) { |
| 164 | char mountPoint[255]; |
| 165 | |
| 166 | snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 167 | snprintf(buffer, maxlen, "/asec/%s", id); |
| 168 | return 0; |
| 169 | } |
| 170 | |
San Mehat | 8b8f71b | 2010-01-11 09:17:25 -0800 | [diff] [blame] | 171 | int VolumeManager::createAsec(const char *id, unsigned int numSectors, |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 172 | const char *fstype, const char *key, int ownerUid) { |
| 173 | |
San Mehat | d31e380 | 2010-02-18 08:37:45 -0800 | [diff] [blame] | 174 | if (numSectors < ((1024*1024)/512)) { |
| 175 | LOGE("Invalid container size specified (%d sectors)", numSectors); |
| 176 | errno = EINVAL; |
| 177 | return -1; |
| 178 | } |
| 179 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 180 | mkdir("/sdcard/android_secure", 0777); |
| 181 | |
| 182 | if (lookupVolume(id)) { |
| 183 | LOGE("ASEC volume '%s' currently exists", id); |
| 184 | errno = EADDRINUSE; |
| 185 | return -1; |
| 186 | } |
| 187 | |
| 188 | char asecFileName[255]; |
| 189 | snprintf(asecFileName, sizeof(asecFileName), |
| 190 | "/sdcard/android_secure/%s.asec", id); |
| 191 | |
| 192 | if (!access(asecFileName, F_OK)) { |
| 193 | LOGE("ASEC file '%s' currently exists - destroy it first! (%s)", |
| 194 | asecFileName, strerror(errno)); |
| 195 | errno = EADDRINUSE; |
| 196 | return -1; |
| 197 | } |
| 198 | |
San Mehat | 8b8f71b | 2010-01-11 09:17:25 -0800 | [diff] [blame] | 199 | if (Loop::createImageFile(asecFileName, numSectors)) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 200 | LOGE("ASEC image file creation failed (%s)", strerror(errno)); |
| 201 | return -1; |
| 202 | } |
| 203 | |
| 204 | char loopDevice[255]; |
San Mehat | 8da6bcb | 2010-01-09 12:24:05 -0800 | [diff] [blame] | 205 | if (Loop::create(asecFileName, loopDevice, sizeof(loopDevice))) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 206 | LOGE("ASEC loop device creation failed (%s)", strerror(errno)); |
| 207 | unlink(asecFileName); |
| 208 | return -1; |
| 209 | } |
| 210 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 211 | char dmDevice[255]; |
| 212 | bool cleanupDm = false; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 213 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 214 | if (strcmp(key, "none")) { |
San Mehat | 8b8f71b | 2010-01-11 09:17:25 -0800 | [diff] [blame] | 215 | if (Devmapper::create(id, loopDevice, key, numSectors, dmDevice, |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 216 | sizeof(dmDevice))) { |
| 217 | LOGE("ASEC device mapping failed (%s)", strerror(errno)); |
| 218 | Loop::destroyByDevice(loopDevice); |
| 219 | unlink(asecFileName); |
| 220 | return -1; |
| 221 | } |
| 222 | cleanupDm = true; |
| 223 | } else { |
| 224 | strcpy(dmDevice, loopDevice); |
| 225 | } |
| 226 | |
| 227 | if (Fat::format(dmDevice)) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 228 | LOGE("ASEC FAT format failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 229 | if (cleanupDm) { |
| 230 | Devmapper::destroy(id); |
| 231 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 232 | Loop::destroyByDevice(loopDevice); |
| 233 | unlink(asecFileName); |
| 234 | return -1; |
| 235 | } |
| 236 | |
| 237 | char mountPoint[255]; |
| 238 | |
| 239 | snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id); |
| 240 | if (mkdir(mountPoint, 0777)) { |
San Mehat | eb13a90 | 2010-01-07 12:12:50 -0800 | [diff] [blame] | 241 | if (errno != EEXIST) { |
| 242 | LOGE("Mountpoint creation failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 243 | if (cleanupDm) { |
| 244 | Devmapper::destroy(id); |
| 245 | } |
San Mehat | eb13a90 | 2010-01-07 12:12:50 -0800 | [diff] [blame] | 246 | Loop::destroyByDevice(loopDevice); |
| 247 | unlink(asecFileName); |
| 248 | return -1; |
| 249 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 250 | } |
| 251 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 252 | if (Fat::doMount(dmDevice, mountPoint, false, false, ownerUid, |
San Mehat | cff5ec3 | 2010-01-08 12:31:44 -0800 | [diff] [blame] | 253 | 0, 0000, false)) { |
| 254 | // 0, 0007, false)) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 255 | LOGE("ASEC FAT mount failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 256 | if (cleanupDm) { |
| 257 | Devmapper::destroy(id); |
| 258 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 259 | Loop::destroyByDevice(loopDevice); |
| 260 | unlink(asecFileName); |
| 261 | return -1; |
| 262 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 263 | |
| 264 | mActiveContainers->push_back(strdup(id)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | int VolumeManager::finalizeAsec(const char *id) { |
| 269 | char asecFileName[255]; |
| 270 | char loopDevice[255]; |
| 271 | char mountPoint[255]; |
| 272 | |
| 273 | snprintf(asecFileName, sizeof(asecFileName), |
| 274 | "/sdcard/android_secure/%s.asec", id); |
| 275 | |
| 276 | if (Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) { |
| 277 | LOGE("Unable to finalize %s (%s)", id, strerror(errno)); |
| 278 | return -1; |
| 279 | } |
| 280 | |
| 281 | snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id); |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 282 | // XXX: |
| 283 | if (Fat::doMount(loopDevice, mountPoint, true, true, 0, 0, 0227, false)) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 284 | LOGE("ASEC finalize mount failed (%s)", strerror(errno)); |
| 285 | return -1; |
| 286 | } |
| 287 | |
| 288 | LOGD("ASEC %s finalized", id); |
| 289 | return 0; |
| 290 | } |
| 291 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 292 | int VolumeManager::renameAsec(const char *id1, const char *id2) { |
| 293 | char *asecFilename1; |
| 294 | char *asecFilename2; |
| 295 | char mountPoint[255]; |
| 296 | |
| 297 | asprintf(&asecFilename1, "/sdcard/android_secure/%s.asec", id1); |
| 298 | asprintf(&asecFilename2, "/sdcard/android_secure/%s.asec", id2); |
| 299 | |
| 300 | snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id1); |
| 301 | if (isMountpointMounted(mountPoint)) { |
| 302 | LOGW("Rename attempt when src mounted"); |
| 303 | errno = EBUSY; |
| 304 | goto out_err; |
| 305 | } |
| 306 | |
| 307 | if (!access(asecFilename2, F_OK)) { |
| 308 | LOGE("Rename attempt when dst exists"); |
| 309 | errno = EADDRINUSE; |
| 310 | goto out_err; |
| 311 | } |
| 312 | |
| 313 | if (rename(asecFilename1, asecFilename2)) { |
| 314 | LOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno)); |
| 315 | goto out_err; |
| 316 | } |
| 317 | |
| 318 | free(asecFilename1); |
| 319 | free(asecFilename2); |
| 320 | return 0; |
| 321 | |
| 322 | out_err: |
| 323 | free(asecFilename1); |
| 324 | free(asecFilename2); |
| 325 | return -1; |
| 326 | } |
| 327 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame^] | 328 | #define ASEC_UNMOUNT_RETRIES 5 |
| 329 | int VolumeManager::unmountAsec(const char *id, bool force) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 330 | char asecFileName[255]; |
| 331 | char mountPoint[255]; |
| 332 | |
| 333 | snprintf(asecFileName, sizeof(asecFileName), |
| 334 | "/sdcard/android_secure/%s.asec", id); |
| 335 | snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id); |
| 336 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 337 | if (!isMountpointMounted(mountPoint)) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 338 | LOGE("Unmount request for ASEC %s when not mounted", id); |
| 339 | errno = EINVAL; |
| 340 | return -1; |
| 341 | } |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 342 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 343 | int i, rc; |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 344 | for (i = 1; i <= ASEC_UNMOUNT_RETRIES; i++) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 345 | rc = umount(mountPoint); |
| 346 | if (!rc) { |
| 347 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 348 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 349 | if (rc && (errno == EINVAL || errno == ENOENT)) { |
| 350 | rc = 0; |
| 351 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 352 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 353 | LOGW("ASEC %s unmount attempt %d failed (%s)", |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 354 | id, i, strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 355 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame^] | 356 | int action = 0; // default is to just complain |
| 357 | |
| 358 | if (force) { |
| 359 | if (i > (ASEC_UNMOUNT_RETRIES - 2)) |
| 360 | action = 2; // SIGKILL |
| 361 | else if (i > (ASEC_UNMOUNT_RETRIES - 3)) |
| 362 | action = 1; // SIGHUP |
| 363 | } |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 364 | |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 365 | Process::killProcessesWithOpenFiles(mountPoint, action); |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 366 | usleep(1000 * 1000); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | if (rc) { |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame^] | 370 | errno = EBUSY; |
| 371 | LOGE("Failed to unmount container %s (%s)", id, strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 372 | return -1; |
| 373 | } |
| 374 | |
San Mehat | f5c6198 | 2010-02-03 11:04:46 -0800 | [diff] [blame] | 375 | if (rmdir(mountPoint)) { |
| 376 | LOGE("Failed to rmdir mountpoint (%s)", strerror(errno)); |
| 377 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 378 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 379 | if (Devmapper::destroy(id) && errno != ENXIO) { |
| 380 | LOGE("Failed to destroy devmapper instance (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | char loopDevice[255]; |
| 384 | if (!Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) { |
| 385 | Loop::destroyByDevice(loopDevice); |
| 386 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 387 | |
| 388 | AsecIdCollection::iterator it; |
| 389 | for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) { |
| 390 | if (!strcmp(*it, id)) { |
| 391 | free(*it); |
| 392 | mActiveContainers->erase(it); |
| 393 | break; |
| 394 | } |
| 395 | } |
| 396 | if (it == mActiveContainers->end()) { |
| 397 | LOGW("mActiveContainers is inconsistent!"); |
| 398 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 399 | return 0; |
| 400 | } |
| 401 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame^] | 402 | int VolumeManager::destroyAsec(const char *id, bool force) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 403 | char asecFileName[255]; |
| 404 | char mountPoint[255]; |
| 405 | |
| 406 | snprintf(asecFileName, sizeof(asecFileName), |
| 407 | "/sdcard/android_secure/%s.asec", id); |
| 408 | snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id); |
| 409 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 410 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 68f8ebd | 2010-01-23 07:21:21 -0800 | [diff] [blame] | 411 | LOGD("Unmounting container before destroy"); |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame^] | 412 | if (unmountAsec(id, force)) { |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 413 | LOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno)); |
| 414 | return -1; |
| 415 | } |
| 416 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 417 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 418 | if (unlink(asecFileName)) { |
San Mehat | 68f8ebd | 2010-01-23 07:21:21 -0800 | [diff] [blame] | 419 | LOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno)); |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 420 | return -1; |
| 421 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 422 | |
| 423 | LOGD("ASEC %s destroyed", id); |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid) { |
| 428 | char asecFileName[255]; |
| 429 | char mountPoint[255]; |
| 430 | |
| 431 | snprintf(asecFileName, sizeof(asecFileName), |
| 432 | "/sdcard/android_secure/%s.asec", id); |
| 433 | snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id); |
| 434 | |
| 435 | if (isMountpointMounted(mountPoint)) { |
| 436 | LOGE("ASEC %s already mounted", id); |
| 437 | errno = EBUSY; |
| 438 | return -1; |
| 439 | } |
| 440 | |
| 441 | char loopDevice[255]; |
| 442 | if (Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) { |
San Mehat | 8da6bcb | 2010-01-09 12:24:05 -0800 | [diff] [blame] | 443 | if (Loop::create(asecFileName, loopDevice, sizeof(loopDevice))) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 444 | LOGE("ASEC loop device creation failed (%s)", strerror(errno)); |
| 445 | return -1; |
| 446 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 447 | LOGD("New loop device created at %s", loopDevice); |
| 448 | } else { |
| 449 | LOGD("Found active loopback for %s at %s", asecFileName, loopDevice); |
| 450 | } |
| 451 | |
| 452 | char dmDevice[255]; |
| 453 | bool cleanupDm = false; |
| 454 | if (strcmp(key, "none")) { |
| 455 | if (Devmapper::lookupActive(id, dmDevice, sizeof(dmDevice))) { |
| 456 | unsigned int nr_sec = 0; |
| 457 | int fd; |
| 458 | |
| 459 | if ((fd = open(loopDevice, O_RDWR)) < 0) { |
| 460 | LOGE("Failed to open loopdevice (%s)", strerror(errno)); |
| 461 | Loop::destroyByDevice(loopDevice); |
| 462 | return -1; |
| 463 | } |
| 464 | |
| 465 | if (ioctl(fd, BLKGETSIZE, &nr_sec)) { |
| 466 | LOGE("Failed to get loop size (%s)", strerror(errno)); |
| 467 | Loop::destroyByDevice(loopDevice); |
| 468 | close(fd); |
| 469 | return -1; |
| 470 | } |
| 471 | close(fd); |
San Mehat | 8b8f71b | 2010-01-11 09:17:25 -0800 | [diff] [blame] | 472 | if (Devmapper::create(id, loopDevice, key, nr_sec, |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 473 | dmDevice, sizeof(dmDevice))) { |
| 474 | LOGE("ASEC device mapping failed (%s)", strerror(errno)); |
| 475 | Loop::destroyByDevice(loopDevice); |
| 476 | return -1; |
| 477 | } |
| 478 | LOGD("New devmapper instance created at %s", dmDevice); |
| 479 | } else { |
| 480 | LOGD("Found active devmapper for %s at %s", asecFileName, dmDevice); |
| 481 | } |
| 482 | cleanupDm = true; |
| 483 | } else { |
| 484 | strcpy(dmDevice, loopDevice); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | if (mkdir(mountPoint, 0777)) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 488 | if (errno != EEXIST) { |
| 489 | LOGE("Mountpoint creation failed (%s)", strerror(errno)); |
| 490 | if (cleanupDm) { |
| 491 | Devmapper::destroy(id); |
| 492 | } |
| 493 | Loop::destroyByDevice(loopDevice); |
| 494 | return -1; |
| 495 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 496 | } |
| 497 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 498 | if (Fat::doMount(dmDevice, mountPoint, true, false, ownerUid, 0, |
San Mehat | cff5ec3 | 2010-01-08 12:31:44 -0800 | [diff] [blame] | 499 | 0222, false)) { |
| 500 | // 0227, false)) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 501 | LOGE("ASEC mount failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 502 | if (cleanupDm) { |
| 503 | Devmapper::destroy(id); |
| 504 | } |
| 505 | Loop::destroyByDevice(loopDevice); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 506 | return -1; |
| 507 | } |
| 508 | |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 509 | mActiveContainers->push_back(strdup(id)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 510 | LOGD("ASEC %s mounted", id); |
| 511 | return 0; |
| 512 | } |
| 513 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 514 | int VolumeManager::mountVolume(const char *label) { |
| 515 | Volume *v = lookupVolume(label); |
| 516 | |
| 517 | if (!v) { |
| 518 | errno = ENOENT; |
| 519 | return -1; |
| 520 | } |
| 521 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 522 | return v->mountVol(); |
| 523 | } |
| 524 | |
| 525 | int VolumeManager::shareAvailable(const char *method, bool *avail) { |
| 526 | |
| 527 | if (strcmp(method, "ums")) { |
| 528 | errno = ENOSYS; |
| 529 | return -1; |
| 530 | } |
| 531 | |
| 532 | if (mUsbMassStorageConnected) |
| 533 | *avail = true; |
| 534 | else |
| 535 | *avail = false; |
| 536 | return 0; |
| 537 | } |
| 538 | |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 539 | int VolumeManager::shareEnabled(const char *label, const char *method, bool *enabled) { |
| 540 | Volume *v = lookupVolume(label); |
| 541 | |
| 542 | if (!v) { |
| 543 | errno = ENOENT; |
| 544 | return -1; |
| 545 | } |
| 546 | |
| 547 | if (strcmp(method, "ums")) { |
| 548 | errno = ENOSYS; |
| 549 | return -1; |
| 550 | } |
| 551 | |
| 552 | if (v->getState() != Volume::State_Shared) { |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 553 | *enabled = false; |
San Mehat | b9aed74 | 2010-02-04 15:07:01 -0800 | [diff] [blame] | 554 | } else { |
| 555 | *enabled = true; |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 556 | } |
| 557 | return 0; |
| 558 | } |
| 559 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 560 | int VolumeManager::simulate(const char *cmd, const char *arg) { |
| 561 | |
| 562 | if (!strcmp(cmd, "ums")) { |
| 563 | if (!strcmp(arg, "connect")) { |
| 564 | notifyUmsConnected(true); |
| 565 | } else if (!strcmp(arg, "disconnect")) { |
| 566 | notifyUmsConnected(false); |
| 567 | } else { |
| 568 | errno = EINVAL; |
| 569 | return -1; |
| 570 | } |
| 571 | } else { |
| 572 | errno = EINVAL; |
| 573 | return -1; |
| 574 | } |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | int VolumeManager::shareVolume(const char *label, const char *method) { |
| 579 | Volume *v = lookupVolume(label); |
| 580 | |
| 581 | if (!v) { |
| 582 | errno = ENOENT; |
| 583 | return -1; |
| 584 | } |
| 585 | |
| 586 | /* |
| 587 | * Eventually, we'll want to support additional share back-ends, |
| 588 | * some of which may work while the media is mounted. For now, |
| 589 | * we just support UMS |
| 590 | */ |
| 591 | if (strcmp(method, "ums")) { |
| 592 | errno = ENOSYS; |
| 593 | return -1; |
| 594 | } |
| 595 | |
| 596 | if (v->getState() == Volume::State_NoMedia) { |
| 597 | errno = ENODEV; |
| 598 | return -1; |
| 599 | } |
| 600 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 601 | if (v->getState() != Volume::State_Idle) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 602 | // You need to unmount manually befoe sharing |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 603 | errno = EBUSY; |
| 604 | return -1; |
| 605 | } |
| 606 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 607 | dev_t d = v->getDiskDevice(); |
| 608 | if ((MAJOR(d) == 0) && (MINOR(d) == 0)) { |
| 609 | // This volume does not support raw disk access |
| 610 | errno = EINVAL; |
| 611 | return -1; |
| 612 | } |
| 613 | |
| 614 | int fd; |
| 615 | char nodepath[255]; |
| 616 | snprintf(nodepath, |
| 617 | sizeof(nodepath), "/dev/block/vold/%d:%d", |
| 618 | MAJOR(d), MINOR(d)); |
| 619 | |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 620 | if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0/file", |
| 621 | O_WRONLY)) < 0) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 622 | LOGE("Unable to open ums lunfile (%s)", strerror(errno)); |
| 623 | return -1; |
| 624 | } |
| 625 | |
| 626 | if (write(fd, nodepath, strlen(nodepath)) < 0) { |
| 627 | LOGE("Unable to write to ums lunfile (%s)", strerror(errno)); |
| 628 | close(fd); |
| 629 | return -1; |
| 630 | } |
| 631 | |
| 632 | close(fd); |
| 633 | v->handleVolumeShared(); |
| 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | int VolumeManager::unshareVolume(const char *label, const char *method) { |
| 638 | Volume *v = lookupVolume(label); |
| 639 | |
| 640 | if (!v) { |
| 641 | errno = ENOENT; |
| 642 | return -1; |
| 643 | } |
| 644 | |
| 645 | if (strcmp(method, "ums")) { |
| 646 | errno = ENOSYS; |
| 647 | return -1; |
| 648 | } |
| 649 | |
| 650 | if (v->getState() != Volume::State_Shared) { |
| 651 | errno = EINVAL; |
| 652 | return -1; |
| 653 | } |
| 654 | |
| 655 | dev_t d = v->getDiskDevice(); |
| 656 | |
| 657 | int fd; |
| 658 | char nodepath[255]; |
| 659 | snprintf(nodepath, |
| 660 | sizeof(nodepath), "/dev/block/vold/%d:%d", |
| 661 | MAJOR(d), MINOR(d)); |
| 662 | |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 663 | if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0/file", O_WRONLY)) < 0) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 664 | LOGE("Unable to open ums lunfile (%s)", strerror(errno)); |
| 665 | return -1; |
| 666 | } |
| 667 | |
| 668 | char ch = 0; |
| 669 | if (write(fd, &ch, 1) < 0) { |
| 670 | LOGE("Unable to write to ums lunfile (%s)", strerror(errno)); |
| 671 | close(fd); |
| 672 | return -1; |
| 673 | } |
| 674 | |
| 675 | close(fd); |
| 676 | v->handleVolumeUnshared(); |
| 677 | return 0; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 678 | } |
| 679 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame^] | 680 | int VolumeManager::unmountVolume(const char *label, bool force) { |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 681 | Volume *v = lookupVolume(label); |
| 682 | |
| 683 | if (!v) { |
| 684 | errno = ENOENT; |
| 685 | return -1; |
| 686 | } |
| 687 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 688 | if (v->getState() == Volume::State_NoMedia) { |
| 689 | errno = ENODEV; |
| 690 | return -1; |
| 691 | } |
| 692 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 693 | if (v->getState() != Volume::State_Mounted) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 694 | LOGW("Attempt to unmount volume which isn't mounted (%d)\n", |
| 695 | v->getState()); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 696 | errno = EBUSY; |
| 697 | return -1; |
| 698 | } |
| 699 | |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 700 | while(mActiveContainers->size()) { |
| 701 | AsecIdCollection::iterator it = mActiveContainers->begin(); |
| 702 | LOGI("Unmounting ASEC %s (dependant on %s)", *it, v->getMountpoint()); |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame^] | 703 | if (unmountAsec(*it, force)) { |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 704 | LOGE("Failed to unmount ASEC %s (%s) - unmount of %s may fail!", *it, |
| 705 | strerror(errno), v->getMountpoint()); |
| 706 | } |
| 707 | } |
| 708 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame^] | 709 | return v->unmountVol(force); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 710 | } |
| 711 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 712 | /* |
| 713 | * Looks up a volume by it's label or mount-point |
| 714 | */ |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 715 | Volume *VolumeManager::lookupVolume(const char *label) { |
| 716 | VolumeCollection::iterator i; |
| 717 | |
| 718 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 719 | if (label[0] == '/') { |
| 720 | if (!strcmp(label, (*i)->getMountpoint())) |
| 721 | return (*i); |
| 722 | } else { |
| 723 | if (!strcmp(label, (*i)->getLabel())) |
| 724 | return (*i); |
| 725 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 726 | } |
| 727 | return NULL; |
| 728 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 729 | |
| 730 | bool VolumeManager::isMountpointMounted(const char *mp) |
| 731 | { |
| 732 | char device[256]; |
| 733 | char mount_path[256]; |
| 734 | char rest[256]; |
| 735 | FILE *fp; |
| 736 | char line[1024]; |
| 737 | |
| 738 | if (!(fp = fopen("/proc/mounts", "r"))) { |
| 739 | LOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
| 740 | return false; |
| 741 | } |
| 742 | |
| 743 | while(fgets(line, sizeof(line), fp)) { |
| 744 | line[strlen(line)-1] = '\0'; |
| 745 | sscanf(line, "%255s %255s %255s\n", device, mount_path, rest); |
| 746 | if (!strcmp(mount_path, mp)) { |
| 747 | fclose(fp); |
| 748 | return true; |
| 749 | } |
| 750 | |
| 751 | } |
| 752 | |
| 753 | fclose(fp); |
| 754 | return false; |
| 755 | } |
| 756 | |