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 | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 40 | #include "Process.h" |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 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(); |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 53 | mActiveContainers = new AsecIdCollection(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 54 | mBroadcaster = NULL; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 55 | mUsbMassStorageConnected = false; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | VolumeManager::~VolumeManager() { |
| 59 | delete mBlockDevices; |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 60 | delete mVolumes; |
| 61 | delete mActiveContainers; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | int VolumeManager::start() { |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | int VolumeManager::stop() { |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | int VolumeManager::addVolume(Volume *v) { |
| 73 | mVolumes->push_back(v); |
| 74 | return 0; |
| 75 | } |
| 76 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 77 | void VolumeManager::notifyUmsConnected(bool connected) { |
| 78 | char msg[255]; |
| 79 | |
| 80 | if (connected) { |
| 81 | mUsbMassStorageConnected = true; |
| 82 | } else { |
| 83 | mUsbMassStorageConnected = false; |
| 84 | } |
| 85 | snprintf(msg, sizeof(msg), "Share method ums now %s", |
| 86 | (connected ? "available" : "unavailable")); |
| 87 | |
| 88 | getBroadcaster()->sendBroadcast(ResponseCode::ShareAvailabilityChange, |
| 89 | msg, false); |
| 90 | } |
| 91 | |
| 92 | void VolumeManager::handleSwitchEvent(NetlinkEvent *evt) { |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 93 | const char *devpath = evt->findParam("DEVPATH"); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 94 | const char *name = evt->findParam("SWITCH_NAME"); |
| 95 | const char *state = evt->findParam("SWITCH_STATE"); |
| 96 | |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 97 | if (!name || !state) { |
| 98 | LOGW("Switch %s event missing name/state info", devpath); |
| 99 | return; |
| 100 | } |
| 101 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 102 | if (!strcmp(name, "usb_mass_storage")) { |
| 103 | |
| 104 | if (!strcmp(state, "online")) { |
| 105 | notifyUmsConnected(true); |
| 106 | } else { |
| 107 | notifyUmsConnected(false); |
| 108 | } |
| 109 | } else { |
| 110 | LOGW("Ignoring unknown switch '%s'", name); |
| 111 | } |
| 112 | } |
| 113 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 114 | void VolumeManager::handleBlockEvent(NetlinkEvent *evt) { |
| 115 | const char *devpath = evt->findParam("DEVPATH"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 116 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 117 | /* Lookup a volume to handle this device */ |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 118 | VolumeCollection::iterator it; |
| 119 | bool hit = false; |
| 120 | for (it = mVolumes->begin(); it != mVolumes->end(); ++it) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 121 | if (!(*it)->handleBlockEvent(evt)) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 122 | #ifdef NETLINK_DEBUG |
| 123 | LOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel()); |
| 124 | #endif |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 125 | hit = true; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 126 | break; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (!hit) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 131 | #ifdef NETLINK_DEBUG |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 132 | LOGW("No volumes handled block event for '%s'", devpath); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 133 | #endif |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 137 | int VolumeManager::listVolumes(SocketClient *cli) { |
| 138 | VolumeCollection::iterator i; |
| 139 | |
| 140 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
| 141 | char *buffer; |
| 142 | asprintf(&buffer, "%s %s %d", |
| 143 | (*i)->getLabel(), (*i)->getMountpoint(), |
| 144 | (*i)->getState()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 145 | cli->sendMsg(ResponseCode::VolumeListResult, buffer, false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 146 | free(buffer); |
| 147 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 148 | cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 149 | return 0; |
| 150 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 151 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 152 | int VolumeManager::formatVolume(const char *label) { |
| 153 | Volume *v = lookupVolume(label); |
| 154 | |
| 155 | if (!v) { |
| 156 | errno = ENOENT; |
| 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | return v->formatVol(); |
| 161 | } |
| 162 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 163 | int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 164 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 165 | snprintf(buffer, maxlen, "%s/%s", Volume::ASECDIR, id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 166 | return 0; |
| 167 | } |
| 168 | |
San Mehat | 8b8f71b | 2010-01-11 09:17:25 -0800 | [diff] [blame] | 169 | int VolumeManager::createAsec(const char *id, unsigned int numSectors, |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 170 | const char *fstype, const char *key, int ownerUid) { |
| 171 | |
San Mehat | d31e380 | 2010-02-18 08:37:45 -0800 | [diff] [blame] | 172 | if (numSectors < ((1024*1024)/512)) { |
| 173 | LOGE("Invalid container size specified (%d sectors)", numSectors); |
| 174 | errno = EINVAL; |
| 175 | return -1; |
| 176 | } |
| 177 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 178 | if (lookupVolume(id)) { |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 179 | LOGE("ASEC id '%s' currently exists", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 180 | errno = EADDRINUSE; |
| 181 | return -1; |
| 182 | } |
| 183 | |
| 184 | char asecFileName[255]; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 185 | snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 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 | |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 222 | if (strcmp(fstype, "none")) { |
| 223 | if (strcmp(fstype, "fat")) { |
| 224 | LOGW("Unknown fstype '%s' specified for container", fstype); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 225 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 226 | |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 227 | if (Fat::format(dmDevice)) { |
| 228 | LOGE("ASEC FAT format failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 229 | if (cleanupDm) { |
| 230 | Devmapper::destroy(id); |
| 231 | } |
San Mehat | eb13a90 | 2010-01-07 12:12:50 -0800 | [diff] [blame] | 232 | Loop::destroyByDevice(loopDevice); |
| 233 | unlink(asecFileName); |
| 234 | return -1; |
| 235 | } |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 236 | char mountPoint[255]; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 237 | |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 238 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
| 239 | if (mkdir(mountPoint, 0777)) { |
| 240 | if (errno != EEXIST) { |
| 241 | LOGE("Mountpoint creation failed (%s)", strerror(errno)); |
| 242 | if (cleanupDm) { |
| 243 | Devmapper::destroy(id); |
| 244 | } |
| 245 | Loop::destroyByDevice(loopDevice); |
| 246 | unlink(asecFileName); |
| 247 | return -1; |
| 248 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 249 | } |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 250 | |
| 251 | if (Fat::doMount(dmDevice, mountPoint, false, false, ownerUid, |
| 252 | 0, 0000, false)) { |
| 253 | LOGE("ASEC FAT mount failed (%s)", strerror(errno)); |
| 254 | if (cleanupDm) { |
| 255 | Devmapper::destroy(id); |
| 256 | } |
| 257 | Loop::destroyByDevice(loopDevice); |
| 258 | unlink(asecFileName); |
| 259 | return -1; |
| 260 | } |
| 261 | } else { |
| 262 | LOGI("Created raw secure container %s (no filesystem)", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 263 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 264 | |
| 265 | mActiveContainers->push_back(strdup(id)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | int VolumeManager::finalizeAsec(const char *id) { |
| 270 | char asecFileName[255]; |
| 271 | char loopDevice[255]; |
| 272 | char mountPoint[255]; |
| 273 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 274 | snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 275 | |
| 276 | if (Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) { |
| 277 | LOGE("Unable to finalize %s (%s)", id, strerror(errno)); |
| 278 | return -1; |
| 279 | } |
| 280 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 281 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 282 | // XXX: |
| 283 | if (Fat::doMount(loopDevice, mountPoint, true, true, 0, 0, 0227, false)) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 284 | LOGE("ASEC finalize mount failed (%s)", strerror(errno)); |
| 285 | return -1; |
| 286 | } |
| 287 | |
| 288 | LOGD("ASEC %s finalized", id); |
| 289 | return 0; |
| 290 | } |
| 291 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 292 | int VolumeManager::renameAsec(const char *id1, const char *id2) { |
| 293 | char *asecFilename1; |
| 294 | char *asecFilename2; |
| 295 | char mountPoint[255]; |
| 296 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 297 | asprintf(&asecFilename1, "%s/%s.asec", Volume::SEC_ASECDIR, id1); |
| 298 | asprintf(&asecFilename2, "%s/%s.asec", Volume::SEC_ASECDIR, id2); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 299 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 300 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id1); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 301 | if (isMountpointMounted(mountPoint)) { |
| 302 | LOGW("Rename attempt when src mounted"); |
| 303 | errno = EBUSY; |
| 304 | goto out_err; |
| 305 | } |
| 306 | |
San Mehat | 96956ed | 2010-02-24 08:42:51 -0800 | [diff] [blame] | 307 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id2); |
| 308 | if (isMountpointMounted(mountPoint)) { |
| 309 | LOGW("Rename attempt when dst mounted"); |
| 310 | errno = EBUSY; |
| 311 | goto out_err; |
| 312 | } |
| 313 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 314 | if (!access(asecFilename2, F_OK)) { |
| 315 | LOGE("Rename attempt when dst exists"); |
| 316 | errno = EADDRINUSE; |
| 317 | goto out_err; |
| 318 | } |
| 319 | |
| 320 | if (rename(asecFilename1, asecFilename2)) { |
| 321 | LOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno)); |
| 322 | goto out_err; |
| 323 | } |
| 324 | |
| 325 | free(asecFilename1); |
| 326 | free(asecFilename2); |
| 327 | return 0; |
| 328 | |
| 329 | out_err: |
| 330 | free(asecFilename1); |
| 331 | free(asecFilename2); |
| 332 | return -1; |
| 333 | } |
| 334 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 335 | #define ASEC_UNMOUNT_RETRIES 5 |
| 336 | int VolumeManager::unmountAsec(const char *id, bool force) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 337 | char asecFileName[255]; |
| 338 | char mountPoint[255]; |
| 339 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 340 | snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id); |
| 341 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 342 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 343 | if (!isMountpointMounted(mountPoint)) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 344 | LOGE("Unmount request for ASEC %s when not mounted", id); |
| 345 | errno = EINVAL; |
| 346 | return -1; |
| 347 | } |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 348 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 349 | int i, rc; |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 350 | for (i = 1; i <= ASEC_UNMOUNT_RETRIES; i++) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 351 | rc = umount(mountPoint); |
| 352 | if (!rc) { |
| 353 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 354 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 355 | if (rc && (errno == EINVAL || errno == ENOENT)) { |
San Mehat | 12f4b89 | 2010-02-24 11:43:22 -0800 | [diff] [blame] | 356 | LOGI("Secure container %s unmounted OK", id); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 357 | rc = 0; |
| 358 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 359 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 360 | LOGW("ASEC %s unmount attempt %d failed (%s)", |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 361 | id, i, strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 362 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 363 | int action = 0; // default is to just complain |
| 364 | |
| 365 | if (force) { |
| 366 | if (i > (ASEC_UNMOUNT_RETRIES - 2)) |
| 367 | action = 2; // SIGKILL |
| 368 | else if (i > (ASEC_UNMOUNT_RETRIES - 3)) |
| 369 | action = 1; // SIGHUP |
| 370 | } |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 371 | |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 372 | Process::killProcessesWithOpenFiles(mountPoint, action); |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 373 | usleep(1000 * 1000); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | if (rc) { |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 377 | errno = EBUSY; |
| 378 | LOGE("Failed to unmount container %s (%s)", id, strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 379 | return -1; |
| 380 | } |
| 381 | |
San Mehat | 12f4b89 | 2010-02-24 11:43:22 -0800 | [diff] [blame] | 382 | int retries = 10; |
| 383 | |
| 384 | while(retries--) { |
| 385 | if (!rmdir(mountPoint)) { |
| 386 | break; |
| 387 | } |
| 388 | |
| 389 | LOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno)); |
| 390 | usleep(1000 * 1000); |
| 391 | } |
| 392 | |
| 393 | if (!retries) { |
| 394 | LOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno)); |
San Mehat | f5c6198 | 2010-02-03 11:04:46 -0800 | [diff] [blame] | 395 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 396 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 397 | if (Devmapper::destroy(id) && errno != ENXIO) { |
| 398 | LOGE("Failed to destroy devmapper instance (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | char loopDevice[255]; |
| 402 | if (!Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) { |
| 403 | Loop::destroyByDevice(loopDevice); |
| 404 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 405 | |
| 406 | AsecIdCollection::iterator it; |
| 407 | for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) { |
| 408 | if (!strcmp(*it, id)) { |
| 409 | free(*it); |
| 410 | mActiveContainers->erase(it); |
| 411 | break; |
| 412 | } |
| 413 | } |
| 414 | if (it == mActiveContainers->end()) { |
| 415 | LOGW("mActiveContainers is inconsistent!"); |
| 416 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 417 | return 0; |
| 418 | } |
| 419 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 420 | int VolumeManager::destroyAsec(const char *id, bool force) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 421 | char asecFileName[255]; |
| 422 | char mountPoint[255]; |
| 423 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 424 | snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id); |
San Mehat | 55013f7 | 2010-02-24 12:12:34 -0800 | [diff] [blame] | 425 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 426 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 427 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 68f8ebd | 2010-01-23 07:21:21 -0800 | [diff] [blame] | 428 | LOGD("Unmounting container before destroy"); |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 429 | if (unmountAsec(id, force)) { |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 430 | LOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno)); |
| 431 | return -1; |
| 432 | } |
| 433 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 434 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 435 | if (unlink(asecFileName)) { |
San Mehat | 68f8ebd | 2010-01-23 07:21:21 -0800 | [diff] [blame] | 436 | LOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno)); |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 437 | return -1; |
| 438 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 439 | |
| 440 | LOGD("ASEC %s destroyed", id); |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid) { |
| 445 | char asecFileName[255]; |
| 446 | char mountPoint[255]; |
| 447 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 448 | snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id); |
| 449 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 450 | |
| 451 | if (isMountpointMounted(mountPoint)) { |
| 452 | LOGE("ASEC %s already mounted", id); |
| 453 | errno = EBUSY; |
| 454 | return -1; |
| 455 | } |
| 456 | |
| 457 | char loopDevice[255]; |
| 458 | if (Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) { |
San Mehat | 8da6bcb | 2010-01-09 12:24:05 -0800 | [diff] [blame] | 459 | if (Loop::create(asecFileName, loopDevice, sizeof(loopDevice))) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 460 | LOGE("ASEC loop device creation failed (%s)", strerror(errno)); |
| 461 | return -1; |
| 462 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 463 | LOGD("New loop device created at %s", loopDevice); |
| 464 | } else { |
| 465 | LOGD("Found active loopback for %s at %s", asecFileName, loopDevice); |
| 466 | } |
| 467 | |
| 468 | char dmDevice[255]; |
| 469 | bool cleanupDm = false; |
| 470 | if (strcmp(key, "none")) { |
| 471 | if (Devmapper::lookupActive(id, dmDevice, sizeof(dmDevice))) { |
| 472 | unsigned int nr_sec = 0; |
| 473 | int fd; |
| 474 | |
| 475 | if ((fd = open(loopDevice, O_RDWR)) < 0) { |
| 476 | LOGE("Failed to open loopdevice (%s)", strerror(errno)); |
| 477 | Loop::destroyByDevice(loopDevice); |
| 478 | return -1; |
| 479 | } |
| 480 | |
| 481 | if (ioctl(fd, BLKGETSIZE, &nr_sec)) { |
| 482 | LOGE("Failed to get loop size (%s)", strerror(errno)); |
| 483 | Loop::destroyByDevice(loopDevice); |
| 484 | close(fd); |
| 485 | return -1; |
| 486 | } |
| 487 | close(fd); |
San Mehat | 8b8f71b | 2010-01-11 09:17:25 -0800 | [diff] [blame] | 488 | if (Devmapper::create(id, loopDevice, key, nr_sec, |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 489 | dmDevice, sizeof(dmDevice))) { |
| 490 | LOGE("ASEC device mapping failed (%s)", strerror(errno)); |
| 491 | Loop::destroyByDevice(loopDevice); |
| 492 | return -1; |
| 493 | } |
| 494 | LOGD("New devmapper instance created at %s", dmDevice); |
| 495 | } else { |
| 496 | LOGD("Found active devmapper for %s at %s", asecFileName, dmDevice); |
| 497 | } |
| 498 | cleanupDm = true; |
| 499 | } else { |
| 500 | strcpy(dmDevice, loopDevice); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | if (mkdir(mountPoint, 0777)) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 504 | if (errno != EEXIST) { |
| 505 | LOGE("Mountpoint creation failed (%s)", strerror(errno)); |
| 506 | if (cleanupDm) { |
| 507 | Devmapper::destroy(id); |
| 508 | } |
| 509 | Loop::destroyByDevice(loopDevice); |
| 510 | return -1; |
| 511 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 512 | } |
| 513 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 514 | if (Fat::doMount(dmDevice, mountPoint, true, false, ownerUid, 0, |
San Mehat | cff5ec3 | 2010-01-08 12:31:44 -0800 | [diff] [blame] | 515 | 0222, false)) { |
| 516 | // 0227, false)) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 517 | LOGE("ASEC mount failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 518 | if (cleanupDm) { |
| 519 | Devmapper::destroy(id); |
| 520 | } |
| 521 | Loop::destroyByDevice(loopDevice); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 522 | return -1; |
| 523 | } |
| 524 | |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 525 | mActiveContainers->push_back(strdup(id)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 526 | LOGD("ASEC %s mounted", id); |
| 527 | return 0; |
| 528 | } |
| 529 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 530 | int VolumeManager::mountVolume(const char *label) { |
| 531 | Volume *v = lookupVolume(label); |
| 532 | |
| 533 | if (!v) { |
| 534 | errno = ENOENT; |
| 535 | return -1; |
| 536 | } |
| 537 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 538 | return v->mountVol(); |
| 539 | } |
| 540 | |
| 541 | int VolumeManager::shareAvailable(const char *method, bool *avail) { |
| 542 | |
| 543 | if (strcmp(method, "ums")) { |
| 544 | errno = ENOSYS; |
| 545 | return -1; |
| 546 | } |
| 547 | |
| 548 | if (mUsbMassStorageConnected) |
| 549 | *avail = true; |
| 550 | else |
| 551 | *avail = false; |
| 552 | return 0; |
| 553 | } |
| 554 | |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 555 | int VolumeManager::shareEnabled(const char *label, const char *method, bool *enabled) { |
| 556 | Volume *v = lookupVolume(label); |
| 557 | |
| 558 | if (!v) { |
| 559 | errno = ENOENT; |
| 560 | return -1; |
| 561 | } |
| 562 | |
| 563 | if (strcmp(method, "ums")) { |
| 564 | errno = ENOSYS; |
| 565 | return -1; |
| 566 | } |
| 567 | |
| 568 | if (v->getState() != Volume::State_Shared) { |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 569 | *enabled = false; |
San Mehat | b9aed74 | 2010-02-04 15:07:01 -0800 | [diff] [blame] | 570 | } else { |
| 571 | *enabled = true; |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 572 | } |
| 573 | return 0; |
| 574 | } |
| 575 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 576 | int VolumeManager::simulate(const char *cmd, const char *arg) { |
| 577 | |
| 578 | if (!strcmp(cmd, "ums")) { |
| 579 | if (!strcmp(arg, "connect")) { |
| 580 | notifyUmsConnected(true); |
| 581 | } else if (!strcmp(arg, "disconnect")) { |
| 582 | notifyUmsConnected(false); |
| 583 | } else { |
| 584 | errno = EINVAL; |
| 585 | return -1; |
| 586 | } |
| 587 | } else { |
| 588 | errno = EINVAL; |
| 589 | return -1; |
| 590 | } |
| 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | int VolumeManager::shareVolume(const char *label, const char *method) { |
| 595 | Volume *v = lookupVolume(label); |
| 596 | |
| 597 | if (!v) { |
| 598 | errno = ENOENT; |
| 599 | return -1; |
| 600 | } |
| 601 | |
| 602 | /* |
| 603 | * Eventually, we'll want to support additional share back-ends, |
| 604 | * some of which may work while the media is mounted. For now, |
| 605 | * we just support UMS |
| 606 | */ |
| 607 | if (strcmp(method, "ums")) { |
| 608 | errno = ENOSYS; |
| 609 | return -1; |
| 610 | } |
| 611 | |
| 612 | if (v->getState() == Volume::State_NoMedia) { |
| 613 | errno = ENODEV; |
| 614 | return -1; |
| 615 | } |
| 616 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 617 | if (v->getState() != Volume::State_Idle) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 618 | // You need to unmount manually befoe sharing |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 619 | errno = EBUSY; |
| 620 | return -1; |
| 621 | } |
| 622 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 623 | dev_t d = v->getDiskDevice(); |
| 624 | if ((MAJOR(d) == 0) && (MINOR(d) == 0)) { |
| 625 | // This volume does not support raw disk access |
| 626 | errno = EINVAL; |
| 627 | return -1; |
| 628 | } |
| 629 | |
| 630 | int fd; |
| 631 | char nodepath[255]; |
| 632 | snprintf(nodepath, |
| 633 | sizeof(nodepath), "/dev/block/vold/%d:%d", |
| 634 | MAJOR(d), MINOR(d)); |
| 635 | |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 636 | if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0/file", |
| 637 | O_WRONLY)) < 0) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 638 | LOGE("Unable to open ums lunfile (%s)", strerror(errno)); |
| 639 | return -1; |
| 640 | } |
| 641 | |
| 642 | if (write(fd, nodepath, strlen(nodepath)) < 0) { |
| 643 | LOGE("Unable to write to ums lunfile (%s)", strerror(errno)); |
| 644 | close(fd); |
| 645 | return -1; |
| 646 | } |
| 647 | |
| 648 | close(fd); |
| 649 | v->handleVolumeShared(); |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | int VolumeManager::unshareVolume(const char *label, const char *method) { |
| 654 | Volume *v = lookupVolume(label); |
| 655 | |
| 656 | if (!v) { |
| 657 | errno = ENOENT; |
| 658 | return -1; |
| 659 | } |
| 660 | |
| 661 | if (strcmp(method, "ums")) { |
| 662 | errno = ENOSYS; |
| 663 | return -1; |
| 664 | } |
| 665 | |
| 666 | if (v->getState() != Volume::State_Shared) { |
| 667 | errno = EINVAL; |
| 668 | return -1; |
| 669 | } |
| 670 | |
| 671 | dev_t d = v->getDiskDevice(); |
| 672 | |
| 673 | int fd; |
| 674 | char nodepath[255]; |
| 675 | snprintf(nodepath, |
| 676 | sizeof(nodepath), "/dev/block/vold/%d:%d", |
| 677 | MAJOR(d), MINOR(d)); |
| 678 | |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 679 | 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] | 680 | LOGE("Unable to open ums lunfile (%s)", strerror(errno)); |
| 681 | return -1; |
| 682 | } |
| 683 | |
| 684 | char ch = 0; |
| 685 | if (write(fd, &ch, 1) < 0) { |
| 686 | LOGE("Unable to write to ums lunfile (%s)", strerror(errno)); |
| 687 | close(fd); |
| 688 | return -1; |
| 689 | } |
| 690 | |
| 691 | close(fd); |
| 692 | v->handleVolumeUnshared(); |
| 693 | return 0; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 694 | } |
| 695 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 696 | int VolumeManager::unmountVolume(const char *label, bool force) { |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 697 | Volume *v = lookupVolume(label); |
| 698 | |
| 699 | if (!v) { |
| 700 | errno = ENOENT; |
| 701 | return -1; |
| 702 | } |
| 703 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 704 | if (v->getState() == Volume::State_NoMedia) { |
| 705 | errno = ENODEV; |
| 706 | return -1; |
| 707 | } |
| 708 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 709 | if (v->getState() != Volume::State_Mounted) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 710 | LOGW("Attempt to unmount volume which isn't mounted (%d)\n", |
| 711 | v->getState()); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 712 | errno = EBUSY; |
| 713 | return -1; |
| 714 | } |
| 715 | |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 716 | while(mActiveContainers->size()) { |
| 717 | AsecIdCollection::iterator it = mActiveContainers->begin(); |
| 718 | LOGI("Unmounting ASEC %s (dependant on %s)", *it, v->getMountpoint()); |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 719 | if (unmountAsec(*it, force)) { |
San Mehat | 12f4b89 | 2010-02-24 11:43:22 -0800 | [diff] [blame] | 720 | LOGE("Failed to unmount ASEC %s (%s)", *it, strerror(errno)); |
San Mehat | 0e38253 | 2010-02-24 08:25:55 -0800 | [diff] [blame] | 721 | return -1; |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 722 | } |
| 723 | } |
| 724 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 725 | return v->unmountVol(force); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 726 | } |
| 727 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 728 | /* |
| 729 | * Looks up a volume by it's label or mount-point |
| 730 | */ |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 731 | Volume *VolumeManager::lookupVolume(const char *label) { |
| 732 | VolumeCollection::iterator i; |
| 733 | |
| 734 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 735 | if (label[0] == '/') { |
| 736 | if (!strcmp(label, (*i)->getMountpoint())) |
| 737 | return (*i); |
| 738 | } else { |
| 739 | if (!strcmp(label, (*i)->getLabel())) |
| 740 | return (*i); |
| 741 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 742 | } |
| 743 | return NULL; |
| 744 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 745 | |
| 746 | bool VolumeManager::isMountpointMounted(const char *mp) |
| 747 | { |
| 748 | char device[256]; |
| 749 | char mount_path[256]; |
| 750 | char rest[256]; |
| 751 | FILE *fp; |
| 752 | char line[1024]; |
| 753 | |
| 754 | if (!(fp = fopen("/proc/mounts", "r"))) { |
| 755 | LOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
| 756 | return false; |
| 757 | } |
| 758 | |
| 759 | while(fgets(line, sizeof(line), fp)) { |
| 760 | line[strlen(line)-1] = '\0'; |
| 761 | sscanf(line, "%255s %255s %255s\n", device, mount_path, rest); |
| 762 | if (!strcmp(mount_path, mp)) { |
| 763 | fclose(fp); |
| 764 | return true; |
| 765 | } |
| 766 | |
| 767 | } |
| 768 | |
| 769 | fclose(fp); |
| 770 | return false; |
| 771 | } |
| 772 | |