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 | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 40 | |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 41 | extern "C" void KillProcessesWithOpenFiles(const char *, int, int, int); |
| 42 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 43 | VolumeManager *VolumeManager::sInstance = NULL; |
| 44 | |
| 45 | VolumeManager *VolumeManager::Instance() { |
| 46 | if (!sInstance) |
| 47 | sInstance = new VolumeManager(); |
| 48 | return sInstance; |
| 49 | } |
| 50 | |
| 51 | VolumeManager::VolumeManager() { |
| 52 | mBlockDevices = new BlockDeviceCollection(); |
| 53 | mVolumes = new VolumeCollection(); |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame^] | 54 | mActiveContainers = new AsecIdCollection(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 55 | mBroadcaster = NULL; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 56 | mUsbMassStorageConnected = false; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | VolumeManager::~VolumeManager() { |
| 60 | delete mBlockDevices; |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame^] | 61 | delete mVolumes; |
| 62 | delete mActiveContainers; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | int VolumeManager::start() { |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | int VolumeManager::stop() { |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | int VolumeManager::addVolume(Volume *v) { |
| 74 | mVolumes->push_back(v); |
| 75 | return 0; |
| 76 | } |
| 77 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 78 | void VolumeManager::notifyUmsConnected(bool connected) { |
| 79 | char msg[255]; |
| 80 | |
| 81 | if (connected) { |
| 82 | mUsbMassStorageConnected = true; |
| 83 | } else { |
| 84 | mUsbMassStorageConnected = false; |
| 85 | } |
| 86 | snprintf(msg, sizeof(msg), "Share method ums now %s", |
| 87 | (connected ? "available" : "unavailable")); |
| 88 | |
| 89 | getBroadcaster()->sendBroadcast(ResponseCode::ShareAvailabilityChange, |
| 90 | msg, false); |
| 91 | } |
| 92 | |
| 93 | void VolumeManager::handleSwitchEvent(NetlinkEvent *evt) { |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 94 | const char *devpath = evt->findParam("DEVPATH"); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 95 | const char *name = evt->findParam("SWITCH_NAME"); |
| 96 | const char *state = evt->findParam("SWITCH_STATE"); |
| 97 | |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 98 | if (!name || !state) { |
| 99 | LOGW("Switch %s event missing name/state info", devpath); |
| 100 | return; |
| 101 | } |
| 102 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 103 | if (!strcmp(name, "usb_mass_storage")) { |
| 104 | |
| 105 | if (!strcmp(state, "online")) { |
| 106 | notifyUmsConnected(true); |
| 107 | } else { |
| 108 | notifyUmsConnected(false); |
| 109 | } |
| 110 | } else { |
| 111 | LOGW("Ignoring unknown switch '%s'", name); |
| 112 | } |
| 113 | } |
| 114 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 115 | void VolumeManager::handleBlockEvent(NetlinkEvent *evt) { |
| 116 | const char *devpath = evt->findParam("DEVPATH"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 117 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 118 | /* Lookup a volume to handle this device */ |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 119 | VolumeCollection::iterator it; |
| 120 | bool hit = false; |
| 121 | for (it = mVolumes->begin(); it != mVolumes->end(); ++it) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 122 | if (!(*it)->handleBlockEvent(evt)) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 123 | #ifdef NETLINK_DEBUG |
| 124 | LOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel()); |
| 125 | #endif |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 126 | hit = true; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 127 | break; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if (!hit) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 132 | #ifdef NETLINK_DEBUG |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 133 | LOGW("No volumes handled block event for '%s'", devpath); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 134 | #endif |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 138 | int VolumeManager::listVolumes(SocketClient *cli) { |
| 139 | VolumeCollection::iterator i; |
| 140 | |
| 141 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
| 142 | char *buffer; |
| 143 | asprintf(&buffer, "%s %s %d", |
| 144 | (*i)->getLabel(), (*i)->getMountpoint(), |
| 145 | (*i)->getState()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 146 | cli->sendMsg(ResponseCode::VolumeListResult, buffer, false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 147 | free(buffer); |
| 148 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 149 | cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 150 | return 0; |
| 151 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 152 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 153 | int VolumeManager::formatVolume(const char *label) { |
| 154 | Volume *v = lookupVolume(label); |
| 155 | |
| 156 | if (!v) { |
| 157 | errno = ENOENT; |
| 158 | return -1; |
| 159 | } |
| 160 | |
| 161 | return v->formatVol(); |
| 162 | } |
| 163 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 164 | int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) { |
| 165 | char mountPoint[255]; |
| 166 | |
| 167 | snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id); |
| 168 | |
| 169 | if (!isMountpointMounted(mountPoint)) { |
| 170 | errno = ENOENT; |
| 171 | return -1; |
| 172 | } |
| 173 | snprintf(buffer, maxlen, "/asec/%s", id); |
| 174 | return 0; |
| 175 | } |
| 176 | |
San Mehat | 8b8f71b | 2010-01-11 09:17:25 -0800 | [diff] [blame] | 177 | int VolumeManager::createAsec(const char *id, unsigned int numSectors, |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 178 | const char *fstype, const char *key, int ownerUid) { |
| 179 | |
| 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 | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 292 | int VolumeManager::unmountAsec(const char *id) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 293 | char asecFileName[255]; |
| 294 | char mountPoint[255]; |
| 295 | |
| 296 | snprintf(asecFileName, sizeof(asecFileName), |
| 297 | "/sdcard/android_secure/%s.asec", id); |
| 298 | snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id); |
| 299 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 300 | if (!isMountpointMounted(mountPoint)) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 301 | LOGE("Unmount request for ASEC %s when not mounted", id); |
| 302 | errno = EINVAL; |
| 303 | return -1; |
| 304 | } |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 305 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 306 | int i, rc; |
| 307 | for (i = 0; i < 10; i++) { |
| 308 | rc = umount(mountPoint); |
| 309 | if (!rc) { |
| 310 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 311 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 312 | if (rc && (errno == EINVAL || errno == ENOENT)) { |
| 313 | rc = 0; |
| 314 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 315 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 316 | LOGW("ASEC %s unmount attempt %d failed (%s)", |
| 317 | id, i +1, strerror(errno)); |
| 318 | |
| 319 | if (i >= 5) { |
| 320 | KillProcessesWithOpenFiles(mountPoint, (i < 7 ? 0 : 1), |
| 321 | NULL, 0); |
| 322 | } |
| 323 | usleep(1000 * 250); |
| 324 | } |
| 325 | |
| 326 | if (rc) { |
| 327 | LOGE("Failed to unmount ASEC %s", id); |
| 328 | return -1; |
| 329 | } |
| 330 | |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame^] | 331 | unlink(mountPoint); |
| 332 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 333 | if (Devmapper::destroy(id) && errno != ENXIO) { |
| 334 | LOGE("Failed to destroy devmapper instance (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | char loopDevice[255]; |
| 338 | if (!Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) { |
| 339 | Loop::destroyByDevice(loopDevice); |
| 340 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame^] | 341 | |
| 342 | AsecIdCollection::iterator it; |
| 343 | for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) { |
| 344 | if (!strcmp(*it, id)) { |
| 345 | free(*it); |
| 346 | mActiveContainers->erase(it); |
| 347 | break; |
| 348 | } |
| 349 | } |
| 350 | if (it == mActiveContainers->end()) { |
| 351 | LOGW("mActiveContainers is inconsistent!"); |
| 352 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | int VolumeManager::destroyAsec(const char *id) { |
| 357 | char asecFileName[255]; |
| 358 | char mountPoint[255]; |
| 359 | |
| 360 | snprintf(asecFileName, sizeof(asecFileName), |
| 361 | "/sdcard/android_secure/%s.asec", id); |
| 362 | snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id); |
| 363 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 364 | if (isMountpointMounted(mountPoint)) { |
| 365 | if (unmountAsec(id)) { |
| 366 | LOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno)); |
| 367 | return -1; |
| 368 | } |
| 369 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 370 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 371 | if (unlink(asecFileName)) { |
| 372 | LOGE("Failed to unlink asec %s (%s)", id, strerror(errno)); |
| 373 | return -1; |
| 374 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 375 | |
| 376 | LOGD("ASEC %s destroyed", id); |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid) { |
| 381 | char asecFileName[255]; |
| 382 | char mountPoint[255]; |
| 383 | |
| 384 | snprintf(asecFileName, sizeof(asecFileName), |
| 385 | "/sdcard/android_secure/%s.asec", id); |
| 386 | snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id); |
| 387 | |
| 388 | if (isMountpointMounted(mountPoint)) { |
| 389 | LOGE("ASEC %s already mounted", id); |
| 390 | errno = EBUSY; |
| 391 | return -1; |
| 392 | } |
| 393 | |
| 394 | char loopDevice[255]; |
| 395 | if (Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) { |
San Mehat | 8da6bcb | 2010-01-09 12:24:05 -0800 | [diff] [blame] | 396 | if (Loop::create(asecFileName, loopDevice, sizeof(loopDevice))) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 397 | LOGE("ASEC loop device creation failed (%s)", strerror(errno)); |
| 398 | return -1; |
| 399 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 400 | LOGD("New loop device created at %s", loopDevice); |
| 401 | } else { |
| 402 | LOGD("Found active loopback for %s at %s", asecFileName, loopDevice); |
| 403 | } |
| 404 | |
| 405 | char dmDevice[255]; |
| 406 | bool cleanupDm = false; |
| 407 | if (strcmp(key, "none")) { |
| 408 | if (Devmapper::lookupActive(id, dmDevice, sizeof(dmDevice))) { |
| 409 | unsigned int nr_sec = 0; |
| 410 | int fd; |
| 411 | |
| 412 | if ((fd = open(loopDevice, O_RDWR)) < 0) { |
| 413 | LOGE("Failed to open loopdevice (%s)", strerror(errno)); |
| 414 | Loop::destroyByDevice(loopDevice); |
| 415 | return -1; |
| 416 | } |
| 417 | |
| 418 | if (ioctl(fd, BLKGETSIZE, &nr_sec)) { |
| 419 | LOGE("Failed to get loop size (%s)", strerror(errno)); |
| 420 | Loop::destroyByDevice(loopDevice); |
| 421 | close(fd); |
| 422 | return -1; |
| 423 | } |
| 424 | close(fd); |
San Mehat | 8b8f71b | 2010-01-11 09:17:25 -0800 | [diff] [blame] | 425 | if (Devmapper::create(id, loopDevice, key, nr_sec, |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 426 | dmDevice, sizeof(dmDevice))) { |
| 427 | LOGE("ASEC device mapping failed (%s)", strerror(errno)); |
| 428 | Loop::destroyByDevice(loopDevice); |
| 429 | return -1; |
| 430 | } |
| 431 | LOGD("New devmapper instance created at %s", dmDevice); |
| 432 | } else { |
| 433 | LOGD("Found active devmapper for %s at %s", asecFileName, dmDevice); |
| 434 | } |
| 435 | cleanupDm = true; |
| 436 | } else { |
| 437 | strcpy(dmDevice, loopDevice); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | if (mkdir(mountPoint, 0777)) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 441 | if (errno != EEXIST) { |
| 442 | LOGE("Mountpoint creation failed (%s)", strerror(errno)); |
| 443 | if (cleanupDm) { |
| 444 | Devmapper::destroy(id); |
| 445 | } |
| 446 | Loop::destroyByDevice(loopDevice); |
| 447 | return -1; |
| 448 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 449 | } |
| 450 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 451 | if (Fat::doMount(dmDevice, mountPoint, true, false, ownerUid, 0, |
San Mehat | cff5ec3 | 2010-01-08 12:31:44 -0800 | [diff] [blame] | 452 | 0222, false)) { |
| 453 | // 0227, false)) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 454 | LOGE("ASEC mount failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 455 | if (cleanupDm) { |
| 456 | Devmapper::destroy(id); |
| 457 | } |
| 458 | Loop::destroyByDevice(loopDevice); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 459 | return -1; |
| 460 | } |
| 461 | |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame^] | 462 | mActiveContainers->push_back(strdup(id)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 463 | LOGD("ASEC %s mounted", id); |
| 464 | return 0; |
| 465 | } |
| 466 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 467 | int VolumeManager::mountVolume(const char *label) { |
| 468 | Volume *v = lookupVolume(label); |
| 469 | |
| 470 | if (!v) { |
| 471 | errno = ENOENT; |
| 472 | return -1; |
| 473 | } |
| 474 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 475 | return v->mountVol(); |
| 476 | } |
| 477 | |
| 478 | int VolumeManager::shareAvailable(const char *method, bool *avail) { |
| 479 | |
| 480 | if (strcmp(method, "ums")) { |
| 481 | errno = ENOSYS; |
| 482 | return -1; |
| 483 | } |
| 484 | |
| 485 | if (mUsbMassStorageConnected) |
| 486 | *avail = true; |
| 487 | else |
| 488 | *avail = false; |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | int VolumeManager::simulate(const char *cmd, const char *arg) { |
| 493 | |
| 494 | if (!strcmp(cmd, "ums")) { |
| 495 | if (!strcmp(arg, "connect")) { |
| 496 | notifyUmsConnected(true); |
| 497 | } else if (!strcmp(arg, "disconnect")) { |
| 498 | notifyUmsConnected(false); |
| 499 | } else { |
| 500 | errno = EINVAL; |
| 501 | return -1; |
| 502 | } |
| 503 | } else { |
| 504 | errno = EINVAL; |
| 505 | return -1; |
| 506 | } |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | int VolumeManager::shareVolume(const char *label, const char *method) { |
| 511 | Volume *v = lookupVolume(label); |
| 512 | |
| 513 | if (!v) { |
| 514 | errno = ENOENT; |
| 515 | return -1; |
| 516 | } |
| 517 | |
| 518 | /* |
| 519 | * Eventually, we'll want to support additional share back-ends, |
| 520 | * some of which may work while the media is mounted. For now, |
| 521 | * we just support UMS |
| 522 | */ |
| 523 | if (strcmp(method, "ums")) { |
| 524 | errno = ENOSYS; |
| 525 | return -1; |
| 526 | } |
| 527 | |
| 528 | if (v->getState() == Volume::State_NoMedia) { |
| 529 | errno = ENODEV; |
| 530 | return -1; |
| 531 | } |
| 532 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 533 | if (v->getState() != Volume::State_Idle) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 534 | // You need to unmount manually befoe sharing |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 535 | errno = EBUSY; |
| 536 | return -1; |
| 537 | } |
| 538 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 539 | dev_t d = v->getDiskDevice(); |
| 540 | if ((MAJOR(d) == 0) && (MINOR(d) == 0)) { |
| 541 | // This volume does not support raw disk access |
| 542 | errno = EINVAL; |
| 543 | return -1; |
| 544 | } |
| 545 | |
| 546 | int fd; |
| 547 | char nodepath[255]; |
| 548 | snprintf(nodepath, |
| 549 | sizeof(nodepath), "/dev/block/vold/%d:%d", |
| 550 | MAJOR(d), MINOR(d)); |
| 551 | |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 552 | if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0/file", |
| 553 | O_WRONLY)) < 0) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 554 | LOGE("Unable to open ums lunfile (%s)", strerror(errno)); |
| 555 | return -1; |
| 556 | } |
| 557 | |
| 558 | if (write(fd, nodepath, strlen(nodepath)) < 0) { |
| 559 | LOGE("Unable to write to ums lunfile (%s)", strerror(errno)); |
| 560 | close(fd); |
| 561 | return -1; |
| 562 | } |
| 563 | |
| 564 | close(fd); |
| 565 | v->handleVolumeShared(); |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | int VolumeManager::unshareVolume(const char *label, const char *method) { |
| 570 | Volume *v = lookupVolume(label); |
| 571 | |
| 572 | if (!v) { |
| 573 | errno = ENOENT; |
| 574 | return -1; |
| 575 | } |
| 576 | |
| 577 | if (strcmp(method, "ums")) { |
| 578 | errno = ENOSYS; |
| 579 | return -1; |
| 580 | } |
| 581 | |
| 582 | if (v->getState() != Volume::State_Shared) { |
| 583 | errno = EINVAL; |
| 584 | return -1; |
| 585 | } |
| 586 | |
| 587 | dev_t d = v->getDiskDevice(); |
| 588 | |
| 589 | int fd; |
| 590 | char nodepath[255]; |
| 591 | snprintf(nodepath, |
| 592 | sizeof(nodepath), "/dev/block/vold/%d:%d", |
| 593 | MAJOR(d), MINOR(d)); |
| 594 | |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 595 | 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] | 596 | LOGE("Unable to open ums lunfile (%s)", strerror(errno)); |
| 597 | return -1; |
| 598 | } |
| 599 | |
| 600 | char ch = 0; |
| 601 | if (write(fd, &ch, 1) < 0) { |
| 602 | LOGE("Unable to write to ums lunfile (%s)", strerror(errno)); |
| 603 | close(fd); |
| 604 | return -1; |
| 605 | } |
| 606 | |
| 607 | close(fd); |
| 608 | v->handleVolumeUnshared(); |
| 609 | return 0; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | int VolumeManager::unmountVolume(const char *label) { |
| 613 | Volume *v = lookupVolume(label); |
| 614 | |
| 615 | if (!v) { |
| 616 | errno = ENOENT; |
| 617 | return -1; |
| 618 | } |
| 619 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 620 | if (v->getState() == Volume::State_NoMedia) { |
| 621 | errno = ENODEV; |
| 622 | return -1; |
| 623 | } |
| 624 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 625 | if (v->getState() != Volume::State_Mounted) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 626 | LOGW("Attempt to unmount volume which isn't mounted (%d)\n", |
| 627 | v->getState()); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 628 | errno = EBUSY; |
| 629 | return -1; |
| 630 | } |
| 631 | |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame^] | 632 | while(mActiveContainers->size()) { |
| 633 | AsecIdCollection::iterator it = mActiveContainers->begin(); |
| 634 | LOGI("Unmounting ASEC %s (dependant on %s)", *it, v->getMountpoint()); |
| 635 | if (unmountAsec(*it)) { |
| 636 | LOGE("Failed to unmount ASEC %s (%s) - unmount of %s may fail!", *it, |
| 637 | strerror(errno), v->getMountpoint()); |
| 638 | } |
| 639 | } |
| 640 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 641 | return v->unmountVol(); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 642 | } |
| 643 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 644 | /* |
| 645 | * Looks up a volume by it's label or mount-point |
| 646 | */ |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 647 | Volume *VolumeManager::lookupVolume(const char *label) { |
| 648 | VolumeCollection::iterator i; |
| 649 | |
| 650 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 651 | if (label[0] == '/') { |
| 652 | if (!strcmp(label, (*i)->getMountpoint())) |
| 653 | return (*i); |
| 654 | } else { |
| 655 | if (!strcmp(label, (*i)->getLabel())) |
| 656 | return (*i); |
| 657 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 658 | } |
| 659 | return NULL; |
| 660 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 661 | |
| 662 | bool VolumeManager::isMountpointMounted(const char *mp) |
| 663 | { |
| 664 | char device[256]; |
| 665 | char mount_path[256]; |
| 666 | char rest[256]; |
| 667 | FILE *fp; |
| 668 | char line[1024]; |
| 669 | |
| 670 | if (!(fp = fopen("/proc/mounts", "r"))) { |
| 671 | LOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
| 672 | return false; |
| 673 | } |
| 674 | |
| 675 | while(fgets(line, sizeof(line), fp)) { |
| 676 | line[strlen(line)-1] = '\0'; |
| 677 | sscanf(line, "%255s %255s %255s\n", device, mount_path, rest); |
| 678 | if (!strcmp(mount_path, mp)) { |
| 679 | fclose(fp); |
| 680 | return true; |
| 681 | } |
| 682 | |
| 683 | } |
| 684 | |
| 685 | fclose(fp); |
| 686 | return false; |
| 687 | } |
| 688 | |