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