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