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 | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 39 | |
| 40 | VolumeManager *VolumeManager::sInstance = NULL; |
| 41 | |
| 42 | VolumeManager *VolumeManager::Instance() { |
| 43 | if (!sInstance) |
| 44 | sInstance = new VolumeManager(); |
| 45 | return sInstance; |
| 46 | } |
| 47 | |
| 48 | VolumeManager::VolumeManager() { |
| 49 | mBlockDevices = new BlockDeviceCollection(); |
| 50 | mVolumes = new VolumeCollection(); |
| 51 | mBroadcaster = NULL; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 52 | mUsbMassStorageConnected = false; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | VolumeManager::~VolumeManager() { |
| 56 | delete mBlockDevices; |
| 57 | } |
| 58 | |
| 59 | int VolumeManager::start() { |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | int VolumeManager::stop() { |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | int VolumeManager::addVolume(Volume *v) { |
| 68 | mVolumes->push_back(v); |
| 69 | return 0; |
| 70 | } |
| 71 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 72 | void VolumeManager::notifyUmsConnected(bool connected) { |
| 73 | char msg[255]; |
| 74 | |
| 75 | if (connected) { |
| 76 | mUsbMassStorageConnected = true; |
| 77 | } else { |
| 78 | mUsbMassStorageConnected = false; |
| 79 | } |
| 80 | snprintf(msg, sizeof(msg), "Share method ums now %s", |
| 81 | (connected ? "available" : "unavailable")); |
| 82 | |
| 83 | getBroadcaster()->sendBroadcast(ResponseCode::ShareAvailabilityChange, |
| 84 | msg, false); |
| 85 | } |
| 86 | |
| 87 | void VolumeManager::handleSwitchEvent(NetlinkEvent *evt) { |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 88 | const char *devpath = evt->findParam("DEVPATH"); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 89 | const char *name = evt->findParam("SWITCH_NAME"); |
| 90 | const char *state = evt->findParam("SWITCH_STATE"); |
| 91 | |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 92 | if (!name || !state) { |
| 93 | LOGW("Switch %s event missing name/state info", devpath); |
| 94 | return; |
| 95 | } |
| 96 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 97 | if (!strcmp(name, "usb_mass_storage")) { |
| 98 | |
| 99 | if (!strcmp(state, "online")) { |
| 100 | notifyUmsConnected(true); |
| 101 | } else { |
| 102 | notifyUmsConnected(false); |
| 103 | } |
| 104 | } else { |
| 105 | LOGW("Ignoring unknown switch '%s'", name); |
| 106 | } |
| 107 | } |
| 108 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 109 | void VolumeManager::handleBlockEvent(NetlinkEvent *evt) { |
| 110 | const char *devpath = evt->findParam("DEVPATH"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 111 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 112 | /* Lookup a volume to handle this device */ |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 113 | VolumeCollection::iterator it; |
| 114 | bool hit = false; |
| 115 | for (it = mVolumes->begin(); it != mVolumes->end(); ++it) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 116 | if (!(*it)->handleBlockEvent(evt)) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 117 | #ifdef NETLINK_DEBUG |
| 118 | LOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel()); |
| 119 | #endif |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 120 | hit = true; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if (!hit) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 126 | #ifdef NETLINK_DEBUG |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 127 | LOGW("No volumes handled block event for '%s'", devpath); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 128 | #endif |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 132 | int VolumeManager::listVolumes(SocketClient *cli) { |
| 133 | VolumeCollection::iterator i; |
| 134 | |
| 135 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
| 136 | char *buffer; |
| 137 | asprintf(&buffer, "%s %s %d", |
| 138 | (*i)->getLabel(), (*i)->getMountpoint(), |
| 139 | (*i)->getState()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 140 | cli->sendMsg(ResponseCode::VolumeListResult, buffer, false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 141 | free(buffer); |
| 142 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 143 | cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 144 | return 0; |
| 145 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 146 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 147 | int VolumeManager::formatVolume(const char *label) { |
| 148 | Volume *v = lookupVolume(label); |
| 149 | |
| 150 | if (!v) { |
| 151 | errno = ENOENT; |
| 152 | return -1; |
| 153 | } |
| 154 | |
| 155 | return v->formatVol(); |
| 156 | } |
| 157 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 158 | int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) { |
| 159 | char mountPoint[255]; |
| 160 | |
| 161 | snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id); |
| 162 | |
| 163 | if (!isMountpointMounted(mountPoint)) { |
| 164 | errno = ENOENT; |
| 165 | return -1; |
| 166 | } |
| 167 | snprintf(buffer, maxlen, "/asec/%s", id); |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | int VolumeManager::createAsec(const char *id, int sizeMb, |
| 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 | |
| 193 | if (Loop::createImageFile(asecFileName, sizeMb)) { |
| 194 | LOGE("ASEC image file creation failed (%s)", strerror(errno)); |
| 195 | return -1; |
| 196 | } |
| 197 | |
| 198 | char loopDevice[255]; |
| 199 | if (Loop::getNextAvailable(loopDevice, sizeof(loopDevice))) { |
| 200 | unlink(asecFileName); |
| 201 | return -1; |
| 202 | } |
| 203 | |
| 204 | if (Loop::create(loopDevice, asecFileName)) { |
| 205 | LOGE("ASEC loop device creation failed (%s)", strerror(errno)); |
| 206 | unlink(asecFileName); |
| 207 | return -1; |
| 208 | } |
| 209 | |
| 210 | /* XXX: Start devmapper */ |
| 211 | |
| 212 | if (Fat::format(loopDevice)) { |
| 213 | LOGE("ASEC FAT format failed (%s)", strerror(errno)); |
| 214 | Loop::destroyByDevice(loopDevice); |
| 215 | unlink(asecFileName); |
| 216 | return -1; |
| 217 | } |
| 218 | |
| 219 | char mountPoint[255]; |
| 220 | |
| 221 | snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id); |
| 222 | if (mkdir(mountPoint, 0777)) { |
San Mehat | eb13a90 | 2010-01-07 12:12:50 -0800 | [diff] [blame] | 223 | if (errno != EEXIST) { |
| 224 | LOGE("Mountpoint creation failed (%s)", strerror(errno)); |
| 225 | Loop::destroyByDevice(loopDevice); |
| 226 | unlink(asecFileName); |
| 227 | return -1; |
| 228 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 229 | } |
| 230 | |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 231 | if (Fat::doMount(loopDevice, mountPoint, false, false, ownerUid, |
| 232 | 0, 0007, false)) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 233 | LOGE("ASEC FAT mount failed (%s)", strerror(errno)); |
| 234 | Loop::destroyByDevice(loopDevice); |
| 235 | unlink(asecFileName); |
| 236 | return -1; |
| 237 | } |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | int VolumeManager::finalizeAsec(const char *id) { |
| 243 | char asecFileName[255]; |
| 244 | char loopDevice[255]; |
| 245 | char mountPoint[255]; |
| 246 | |
| 247 | snprintf(asecFileName, sizeof(asecFileName), |
| 248 | "/sdcard/android_secure/%s.asec", id); |
| 249 | |
| 250 | if (Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) { |
| 251 | LOGE("Unable to finalize %s (%s)", id, strerror(errno)); |
| 252 | return -1; |
| 253 | } |
| 254 | |
| 255 | snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id); |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 256 | // XXX: |
| 257 | if (Fat::doMount(loopDevice, mountPoint, true, true, 0, 0, 0227, false)) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 258 | LOGE("ASEC finalize mount failed (%s)", strerror(errno)); |
| 259 | return -1; |
| 260 | } |
| 261 | |
| 262 | LOGD("ASEC %s finalized", id); |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | int VolumeManager::destroyAsec(const char *id) { |
| 267 | char asecFileName[255]; |
| 268 | char mountPoint[255]; |
| 269 | |
| 270 | snprintf(asecFileName, sizeof(asecFileName), |
| 271 | "/sdcard/android_secure/%s.asec", id); |
| 272 | snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id); |
| 273 | |
| 274 | if (isMountpointMounted(mountPoint)) { |
| 275 | int i, rc; |
| 276 | for (i = 0; i < 10; i++) { |
| 277 | rc = umount(mountPoint); |
| 278 | if (!rc) { |
| 279 | break; |
| 280 | } |
| 281 | if (rc && (errno == EINVAL || errno == ENOENT)) { |
| 282 | rc = 0; |
| 283 | break; |
| 284 | } |
| 285 | LOGW("ASEC %s unmount attempt %d failed (%s)", |
| 286 | id, i +1, strerror(errno)); |
| 287 | usleep(1000 * 250); |
| 288 | } |
| 289 | if (rc) { |
| 290 | LOGE("Failed to unmount ASEC %s for destroy", id); |
| 291 | return -1; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | char loopDevice[255]; |
| 296 | if (!Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) { |
| 297 | Loop::destroyByDevice(loopDevice); |
| 298 | } |
| 299 | |
| 300 | unlink(asecFileName); |
| 301 | |
| 302 | LOGD("ASEC %s destroyed", id); |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid) { |
| 307 | char asecFileName[255]; |
| 308 | char mountPoint[255]; |
| 309 | |
| 310 | snprintf(asecFileName, sizeof(asecFileName), |
| 311 | "/sdcard/android_secure/%s.asec", id); |
| 312 | snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id); |
| 313 | |
| 314 | if (isMountpointMounted(mountPoint)) { |
| 315 | LOGE("ASEC %s already mounted", id); |
| 316 | errno = EBUSY; |
| 317 | return -1; |
| 318 | } |
| 319 | |
| 320 | char loopDevice[255]; |
| 321 | if (Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) { |
| 322 | if (Loop::getNextAvailable(loopDevice, sizeof(loopDevice))) { |
| 323 | LOGE("Unable to find loop device for ASEC mount"); |
| 324 | return -1; |
| 325 | } |
| 326 | |
| 327 | if (Loop::create(loopDevice, asecFileName)) { |
| 328 | LOGE("ASEC loop device creation failed (%s)", strerror(errno)); |
| 329 | return -1; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | if (mkdir(mountPoint, 0777)) { |
| 334 | LOGE("Mountpoint creation failed (%s)", strerror(errno)); |
| 335 | return -1; |
| 336 | } |
| 337 | |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 338 | if (Fat::doMount(loopDevice, mountPoint, true, false, ownerUid, 0, |
| 339 | 0227, false)) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 340 | LOGE("ASEC mount failed (%s)", strerror(errno)); |
| 341 | return -1; |
| 342 | } |
| 343 | |
| 344 | LOGD("ASEC %s mounted", id); |
| 345 | return 0; |
| 346 | } |
| 347 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 348 | int VolumeManager::mountVolume(const char *label) { |
| 349 | Volume *v = lookupVolume(label); |
| 350 | |
| 351 | if (!v) { |
| 352 | errno = ENOENT; |
| 353 | return -1; |
| 354 | } |
| 355 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 356 | return v->mountVol(); |
| 357 | } |
| 358 | |
| 359 | int VolumeManager::shareAvailable(const char *method, bool *avail) { |
| 360 | |
| 361 | if (strcmp(method, "ums")) { |
| 362 | errno = ENOSYS; |
| 363 | return -1; |
| 364 | } |
| 365 | |
| 366 | if (mUsbMassStorageConnected) |
| 367 | *avail = true; |
| 368 | else |
| 369 | *avail = false; |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | int VolumeManager::simulate(const char *cmd, const char *arg) { |
| 374 | |
| 375 | if (!strcmp(cmd, "ums")) { |
| 376 | if (!strcmp(arg, "connect")) { |
| 377 | notifyUmsConnected(true); |
| 378 | } else if (!strcmp(arg, "disconnect")) { |
| 379 | notifyUmsConnected(false); |
| 380 | } else { |
| 381 | errno = EINVAL; |
| 382 | return -1; |
| 383 | } |
| 384 | } else { |
| 385 | errno = EINVAL; |
| 386 | return -1; |
| 387 | } |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | int VolumeManager::shareVolume(const char *label, const char *method) { |
| 392 | Volume *v = lookupVolume(label); |
| 393 | |
| 394 | if (!v) { |
| 395 | errno = ENOENT; |
| 396 | return -1; |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | * Eventually, we'll want to support additional share back-ends, |
| 401 | * some of which may work while the media is mounted. For now, |
| 402 | * we just support UMS |
| 403 | */ |
| 404 | if (strcmp(method, "ums")) { |
| 405 | errno = ENOSYS; |
| 406 | return -1; |
| 407 | } |
| 408 | |
| 409 | if (v->getState() == Volume::State_NoMedia) { |
| 410 | errno = ENODEV; |
| 411 | return -1; |
| 412 | } |
| 413 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 414 | if (v->getState() != Volume::State_Idle) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 415 | // You need to unmount manually befoe sharing |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 416 | errno = EBUSY; |
| 417 | return -1; |
| 418 | } |
| 419 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 420 | dev_t d = v->getDiskDevice(); |
| 421 | if ((MAJOR(d) == 0) && (MINOR(d) == 0)) { |
| 422 | // This volume does not support raw disk access |
| 423 | errno = EINVAL; |
| 424 | return -1; |
| 425 | } |
| 426 | |
| 427 | int fd; |
| 428 | char nodepath[255]; |
| 429 | snprintf(nodepath, |
| 430 | sizeof(nodepath), "/dev/block/vold/%d:%d", |
| 431 | MAJOR(d), MINOR(d)); |
| 432 | |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 433 | if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0/file", |
| 434 | O_WRONLY)) < 0) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 435 | LOGE("Unable to open ums lunfile (%s)", strerror(errno)); |
| 436 | return -1; |
| 437 | } |
| 438 | |
| 439 | if (write(fd, nodepath, strlen(nodepath)) < 0) { |
| 440 | LOGE("Unable to write to ums lunfile (%s)", strerror(errno)); |
| 441 | close(fd); |
| 442 | return -1; |
| 443 | } |
| 444 | |
| 445 | close(fd); |
| 446 | v->handleVolumeShared(); |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | int VolumeManager::unshareVolume(const char *label, const char *method) { |
| 451 | Volume *v = lookupVolume(label); |
| 452 | |
| 453 | if (!v) { |
| 454 | errno = ENOENT; |
| 455 | return -1; |
| 456 | } |
| 457 | |
| 458 | if (strcmp(method, "ums")) { |
| 459 | errno = ENOSYS; |
| 460 | return -1; |
| 461 | } |
| 462 | |
| 463 | if (v->getState() != Volume::State_Shared) { |
| 464 | errno = EINVAL; |
| 465 | return -1; |
| 466 | } |
| 467 | |
| 468 | dev_t d = v->getDiskDevice(); |
| 469 | |
| 470 | int fd; |
| 471 | char nodepath[255]; |
| 472 | snprintf(nodepath, |
| 473 | sizeof(nodepath), "/dev/block/vold/%d:%d", |
| 474 | MAJOR(d), MINOR(d)); |
| 475 | |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 476 | 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] | 477 | LOGE("Unable to open ums lunfile (%s)", strerror(errno)); |
| 478 | return -1; |
| 479 | } |
| 480 | |
| 481 | char ch = 0; |
| 482 | if (write(fd, &ch, 1) < 0) { |
| 483 | LOGE("Unable to write to ums lunfile (%s)", strerror(errno)); |
| 484 | close(fd); |
| 485 | return -1; |
| 486 | } |
| 487 | |
| 488 | close(fd); |
| 489 | v->handleVolumeUnshared(); |
| 490 | return 0; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | int VolumeManager::unmountVolume(const char *label) { |
| 494 | Volume *v = lookupVolume(label); |
| 495 | |
| 496 | if (!v) { |
| 497 | errno = ENOENT; |
| 498 | return -1; |
| 499 | } |
| 500 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 501 | if (v->getState() == Volume::State_NoMedia) { |
| 502 | errno = ENODEV; |
| 503 | return -1; |
| 504 | } |
| 505 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 506 | if (v->getState() != Volume::State_Mounted) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 507 | LOGW("Attempt to unmount volume which isn't mounted (%d)\n", |
| 508 | v->getState()); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 509 | errno = EBUSY; |
| 510 | return -1; |
| 511 | } |
| 512 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 513 | return v->unmountVol(); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 514 | } |
| 515 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 516 | /* |
| 517 | * Looks up a volume by it's label or mount-point |
| 518 | */ |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 519 | Volume *VolumeManager::lookupVolume(const char *label) { |
| 520 | VolumeCollection::iterator i; |
| 521 | |
| 522 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 523 | if (label[0] == '/') { |
| 524 | if (!strcmp(label, (*i)->getMountpoint())) |
| 525 | return (*i); |
| 526 | } else { |
| 527 | if (!strcmp(label, (*i)->getLabel())) |
| 528 | return (*i); |
| 529 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 530 | } |
| 531 | return NULL; |
| 532 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 533 | |
| 534 | bool VolumeManager::isMountpointMounted(const char *mp) |
| 535 | { |
| 536 | char device[256]; |
| 537 | char mount_path[256]; |
| 538 | char rest[256]; |
| 539 | FILE *fp; |
| 540 | char line[1024]; |
| 541 | |
| 542 | if (!(fp = fopen("/proc/mounts", "r"))) { |
| 543 | LOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
| 544 | return false; |
| 545 | } |
| 546 | |
| 547 | while(fgets(line, sizeof(line), fp)) { |
| 548 | line[strlen(line)-1] = '\0'; |
| 549 | sscanf(line, "%255s %255s %255s\n", device, mount_path, rest); |
| 550 | if (!strcmp(mount_path, mp)) { |
| 551 | fclose(fp); |
| 552 | return true; |
| 553 | } |
| 554 | |
| 555 | } |
| 556 | |
| 557 | fclose(fp); |
| 558 | return false; |
| 559 | } |
| 560 | |