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