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