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 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 17 | #include <stdlib.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 18 | #include <string.h> |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 19 | #include <dirent.h> |
| 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
| 22 | |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/mman.h> |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 27 | #include <sys/mount.h> |
| 28 | |
| 29 | #include <linux/kdev_t.h> |
| 30 | |
| 31 | #include <cutils/properties.h> |
| 32 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 33 | #include <diskconfig/diskconfig.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 34 | |
| 35 | #define LOG_TAG "Vold" |
| 36 | |
| 37 | #include <cutils/log.h> |
| 38 | |
| 39 | #include "Volume.h" |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 40 | #include "VolumeManager.h" |
| 41 | #include "ResponseCode.h" |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 42 | #include "Fat.h" |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 43 | #include "Process.h" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 44 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 45 | extern "C" void dos_partition_dec(void const *pp, struct dos_partition *d); |
| 46 | extern "C" void dos_partition_enc(void *pp, struct dos_partition *d); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 47 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 48 | |
| 49 | /* |
| 50 | * Secure directory - stuff that only root can see |
| 51 | */ |
| 52 | const char *Volume::SECDIR = "/mnt/secure"; |
| 53 | |
| 54 | /* |
| 55 | * Secure staging directory - where media is mounted for preparation |
| 56 | */ |
| 57 | const char *Volume::SEC_STGDIR = "/mnt/secure/staging"; |
| 58 | |
| 59 | /* |
| 60 | * Path to the directory on the media which contains publicly accessable |
| 61 | * asec imagefiles. This path will be obscured before the mount is |
| 62 | * exposed to non priviledged users. |
| 63 | */ |
San Mehat | 52c2ccb | 2010-02-23 18:26:13 -0800 | [diff] [blame] | 64 | const char *Volume::SEC_STG_SECIMGDIR = "/mnt/secure/staging/.android_secure"; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * Path to where *only* root can access asec imagefiles |
| 68 | */ |
| 69 | const char *Volume::SEC_ASECDIR = "/mnt/secure/asec"; |
| 70 | |
| 71 | /* |
| 72 | * Path to where secure containers are mounted |
| 73 | */ |
| 74 | const char *Volume::ASECDIR = "/mnt/asec"; |
| 75 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 76 | static const char *stateToStr(int state) { |
| 77 | if (state == Volume::State_Init) |
| 78 | return "Initializing"; |
| 79 | else if (state == Volume::State_NoMedia) |
| 80 | return "No-Media"; |
| 81 | else if (state == Volume::State_Idle) |
| 82 | return "Idle-Unmounted"; |
| 83 | else if (state == Volume::State_Pending) |
| 84 | return "Pending"; |
| 85 | else if (state == Volume::State_Mounted) |
| 86 | return "Mounted"; |
| 87 | else if (state == Volume::State_Unmounting) |
| 88 | return "Unmounting"; |
| 89 | else if (state == Volume::State_Checking) |
| 90 | return "Checking"; |
| 91 | else if (state == Volume::State_Formatting) |
| 92 | return "Formatting"; |
| 93 | else if (state == Volume::State_Shared) |
| 94 | return "Shared-Unmounted"; |
| 95 | else if (state == Volume::State_SharedMnt) |
| 96 | return "Shared-Mounted"; |
| 97 | else |
| 98 | return "Unknown-Error"; |
| 99 | } |
| 100 | |
| 101 | Volume::Volume(VolumeManager *vm, const char *label, const char *mount_point) { |
| 102 | mVm = vm; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 103 | mDebug = false; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 104 | mLabel = strdup(label); |
| 105 | mMountpoint = strdup(mount_point); |
| 106 | mState = Volume::State_Init; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 107 | mCurrentlyMountedKdev = -1; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | Volume::~Volume() { |
| 111 | free(mLabel); |
| 112 | free(mMountpoint); |
| 113 | } |
| 114 | |
San Mehat | cb4dac8 | 2010-03-14 13:41:54 -0700 | [diff] [blame] | 115 | void Volume::protectFromAutorunStupidity() { |
| 116 | char filename[255]; |
| 117 | |
| 118 | snprintf(filename, sizeof(filename), "%s/autorun.inf", SEC_STGDIR); |
| 119 | if (!access(filename, F_OK)) { |
| 120 | LOGW("Volume contains an autorun.inf! - removing"); |
| 121 | /* |
| 122 | * Ensure the filename is all lower-case so |
| 123 | * the process killer can find the inode. |
| 124 | * Probably being paranoid here but meh. |
| 125 | */ |
| 126 | rename(filename, filename); |
| 127 | Process::killProcessesWithOpenFiles(filename, 2); |
| 128 | if (unlink(filename)) { |
| 129 | LOGE("Failed to remove %s (%s)", filename, strerror(errno)); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 134 | void Volume::setDebug(bool enable) { |
| 135 | mDebug = enable; |
| 136 | } |
| 137 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 138 | dev_t Volume::getDiskDevice() { |
| 139 | return MKDEV(0, 0); |
| 140 | }; |
| 141 | |
| 142 | void Volume::handleVolumeShared() { |
| 143 | } |
| 144 | |
| 145 | void Volume::handleVolumeUnshared() { |
| 146 | } |
| 147 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 148 | int Volume::handleBlockEvent(NetlinkEvent *evt) { |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 149 | errno = ENOSYS; |
| 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | void Volume::setState(int state) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 154 | char msg[255]; |
| 155 | int oldState = mState; |
| 156 | |
| 157 | if (oldState == state) { |
| 158 | LOGW("Duplicate state (%d)\n", state); |
| 159 | return; |
| 160 | } |
| 161 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 162 | mState = state; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 163 | |
| 164 | LOGD("Volume %s state changing %d (%s) -> %d (%s)", mLabel, |
| 165 | oldState, stateToStr(oldState), mState, stateToStr(mState)); |
| 166 | snprintf(msg, sizeof(msg), |
| 167 | "Volume %s %s state changed from %d (%s) to %d (%s)", getLabel(), |
| 168 | getMountpoint(), oldState, stateToStr(oldState), mState, |
| 169 | stateToStr(mState)); |
| 170 | |
| 171 | mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeStateChange, |
| 172 | msg, false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 173 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 174 | |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame] | 175 | int Volume::createDeviceNode(const char *path, int major, int minor) { |
| 176 | mode_t mode = 0660 | S_IFBLK; |
| 177 | dev_t dev = (major << 8) | minor; |
| 178 | if (mknod(path, mode, dev) < 0) { |
| 179 | if (errno != EEXIST) { |
| 180 | return -1; |
| 181 | } |
| 182 | } |
| 183 | return 0; |
| 184 | } |
| 185 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 186 | int Volume::formatVol() { |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 187 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 188 | if (getState() == Volume::State_NoMedia) { |
| 189 | errno = ENODEV; |
| 190 | return -1; |
| 191 | } else if (getState() != Volume::State_Idle) { |
| 192 | errno = EBUSY; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 193 | return -1; |
| 194 | } |
| 195 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 196 | if (isMountpointMounted(getMountpoint())) { |
| 197 | LOGW("Volume is idle but appears to be mounted - fixing"); |
| 198 | setState(Volume::State_Mounted); |
| 199 | // mCurrentlyMountedKdev = XXX |
| 200 | errno = EBUSY; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 201 | return -1; |
| 202 | } |
| 203 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 204 | char devicePath[255]; |
| 205 | dev_t diskNode = getDiskDevice(); |
| 206 | dev_t partNode = MKDEV(MAJOR(diskNode), 1); // XXX: Hmmm |
| 207 | |
| 208 | sprintf(devicePath, "/dev/block/vold/%d:%d", |
| 209 | MAJOR(diskNode), MINOR(diskNode)); |
| 210 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 211 | if (mDebug) { |
| 212 | LOGI("Formatting volume %s (%s)", getLabel(), devicePath); |
| 213 | } |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 214 | setState(Volume::State_Formatting); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 215 | |
| 216 | if (initializeMbr(devicePath)) { |
| 217 | LOGE("Failed to initialize MBR (%s)", strerror(errno)); |
| 218 | goto err; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 219 | } |
| 220 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 221 | sprintf(devicePath, "/dev/block/vold/%d:%d", |
| 222 | MAJOR(partNode), MINOR(partNode)); |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame] | 223 | |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 224 | if (Fat::format(devicePath, 0)) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 225 | LOGE("Failed to format (%s)", strerror(errno)); |
| 226 | goto err; |
| 227 | } |
| 228 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 229 | setState(Volume::State_Idle); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 230 | return 0; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 231 | err: |
| 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | bool Volume::isMountpointMounted(const char *path) { |
| 236 | char device[256]; |
| 237 | char mount_path[256]; |
| 238 | char rest[256]; |
| 239 | FILE *fp; |
| 240 | char line[1024]; |
| 241 | |
| 242 | if (!(fp = fopen("/proc/mounts", "r"))) { |
| 243 | LOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | while(fgets(line, sizeof(line), fp)) { |
| 248 | line[strlen(line)-1] = '\0'; |
| 249 | sscanf(line, "%255s %255s %255s\n", device, mount_path, rest); |
| 250 | if (!strcmp(mount_path, path)) { |
| 251 | fclose(fp); |
| 252 | return true; |
| 253 | } |
| 254 | |
| 255 | } |
| 256 | |
| 257 | fclose(fp); |
| 258 | return false; |
| 259 | } |
| 260 | |
| 261 | int Volume::mountVol() { |
| 262 | dev_t deviceNodes[4]; |
| 263 | int n, i, rc = 0; |
| 264 | char errmsg[255]; |
| 265 | |
| 266 | if (getState() == Volume::State_NoMedia) { |
| 267 | snprintf(errmsg, sizeof(errmsg), |
| 268 | "Volume %s %s mount failed - no media", |
| 269 | getLabel(), getMountpoint()); |
| 270 | mVm->getBroadcaster()->sendBroadcast( |
| 271 | ResponseCode::VolumeMountFailedNoMedia, |
| 272 | errmsg, false); |
| 273 | errno = ENODEV; |
| 274 | return -1; |
| 275 | } else if (getState() != Volume::State_Idle) { |
| 276 | errno = EBUSY; |
| 277 | return -1; |
| 278 | } |
| 279 | |
| 280 | if (isMountpointMounted(getMountpoint())) { |
| 281 | LOGW("Volume is idle but appears to be mounted - fixing"); |
| 282 | setState(Volume::State_Mounted); |
| 283 | // mCurrentlyMountedKdev = XXX |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | n = getDeviceNodes((dev_t *) &deviceNodes, 4); |
| 288 | if (!n) { |
| 289 | LOGE("Failed to get device nodes (%s)\n", strerror(errno)); |
| 290 | return -1; |
| 291 | } |
| 292 | |
| 293 | for (i = 0; i < n; i++) { |
| 294 | char devicePath[255]; |
| 295 | |
| 296 | sprintf(devicePath, "/dev/block/vold/%d:%d", MAJOR(deviceNodes[i]), |
| 297 | MINOR(deviceNodes[i])); |
| 298 | |
| 299 | LOGI("%s being considered for volume %s\n", devicePath, getLabel()); |
| 300 | |
| 301 | errno = 0; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 302 | setState(Volume::State_Checking); |
| 303 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 304 | if (Fat::check(devicePath)) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 305 | if (errno == ENODATA) { |
| 306 | LOGW("%s does not contain a FAT filesystem\n", devicePath); |
| 307 | continue; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 308 | } |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 309 | errno = EIO; |
| 310 | /* Badness - abort the mount */ |
| 311 | LOGE("%s failed FS checks (%s)", devicePath, strerror(errno)); |
| 312 | setState(Volume::State_Idle); |
| 313 | return -1; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 314 | } |
| 315 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 316 | /* |
| 317 | * Mount the device on our internal staging mountpoint so we can |
| 318 | * muck with it before exposing it to non priviledged users. |
| 319 | */ |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 320 | errno = 0; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 321 | if (Fat::doMount(devicePath, "/mnt/secure/staging", false, false, 1000, 1015, 0702, true)) { |
| 322 | LOGE("%s failed to mount via VFAT (%s)\n", devicePath, strerror(errno)); |
| 323 | continue; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 324 | } |
| 325 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 326 | LOGI("Device %s, target %s mounted @ /mnt/secure/staging", devicePath, getMountpoint()); |
| 327 | |
San Mehat | cb4dac8 | 2010-03-14 13:41:54 -0700 | [diff] [blame] | 328 | protectFromAutorunStupidity(); |
| 329 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 330 | if (createBindMounts()) { |
| 331 | LOGE("Failed to create bindmounts (%s)", strerror(errno)); |
| 332 | umount("/mnt/secure/staging"); |
| 333 | setState(Volume::State_Idle); |
| 334 | return -1; |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * Now that the bindmount trickery is done, atomically move the |
| 339 | * whole subtree to expose it to non priviledged users. |
| 340 | */ |
| 341 | if (doMoveMount("/mnt/secure/staging", getMountpoint(), false)) { |
| 342 | LOGE("Failed to move mount (%s)", strerror(errno)); |
| 343 | umount("/mnt/secure/staging"); |
| 344 | setState(Volume::State_Idle); |
| 345 | return -1; |
| 346 | } |
| 347 | setState(Volume::State_Mounted); |
| 348 | mCurrentlyMountedKdev = deviceNodes[i]; |
| 349 | return 0; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 350 | } |
| 351 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 352 | LOGE("Volume %s found no suitable devices for mounting :(\n", getLabel()); |
| 353 | setState(Volume::State_Idle); |
| 354 | |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 355 | return -1; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 356 | } |
| 357 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 358 | int Volume::createBindMounts() { |
| 359 | unsigned long flags; |
| 360 | |
| 361 | /* |
San Mehat | 52c2ccb | 2010-02-23 18:26:13 -0800 | [diff] [blame] | 362 | * Rename old /android_secure -> /.android_secure |
| 363 | */ |
| 364 | if (!access("/mnt/secure/staging/android_secure", R_OK | X_OK) && |
| 365 | access(SEC_STG_SECIMGDIR, R_OK | X_OK)) { |
| 366 | if (rename("/mnt/secure/staging/android_secure", SEC_STG_SECIMGDIR)) { |
| 367 | LOGE("Failed to rename legacy asec dir (%s)", strerror(errno)); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | /* |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 372 | * Ensure that /android_secure exists and is a directory |
| 373 | */ |
| 374 | if (access(SEC_STG_SECIMGDIR, R_OK | X_OK)) { |
| 375 | if (errno == ENOENT) { |
| 376 | if (mkdir(SEC_STG_SECIMGDIR, 0777)) { |
| 377 | LOGE("Failed to create %s (%s)", SEC_STG_SECIMGDIR, strerror(errno)); |
| 378 | return -1; |
| 379 | } |
| 380 | } else { |
| 381 | LOGE("Failed to access %s (%s)", SEC_STG_SECIMGDIR, strerror(errno)); |
| 382 | return -1; |
| 383 | } |
| 384 | } else { |
| 385 | struct stat sbuf; |
| 386 | |
| 387 | if (stat(SEC_STG_SECIMGDIR, &sbuf)) { |
| 388 | LOGE("Failed to stat %s (%s)", SEC_STG_SECIMGDIR, strerror(errno)); |
| 389 | return -1; |
| 390 | } |
| 391 | if (!S_ISDIR(sbuf.st_mode)) { |
| 392 | LOGE("%s is not a directory", SEC_STG_SECIMGDIR); |
| 393 | errno = ENOTDIR; |
| 394 | return -1; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | * Bind mount /mnt/secure/staging/android_secure -> /mnt/secure/asec so we'll |
| 400 | * have a root only accessable mountpoint for it. |
| 401 | */ |
| 402 | if (mount(SEC_STG_SECIMGDIR, SEC_ASECDIR, "", MS_BIND, NULL)) { |
| 403 | LOGE("Failed to bind mount points %s -> %s (%s)", |
| 404 | SEC_STG_SECIMGDIR, SEC_ASECDIR, strerror(errno)); |
| 405 | return -1; |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * Mount a read-only, zero-sized tmpfs on <mountpoint>/android_secure to |
| 410 | * obscure the underlying directory from everybody - sneaky eh? ;) |
| 411 | */ |
| 412 | if (mount("tmpfs", SEC_STG_SECIMGDIR, "tmpfs", MS_RDONLY, "size=0,mode=000,uid=0,gid=0")) { |
| 413 | LOGE("Failed to obscure %s (%s)", SEC_STG_SECIMGDIR, strerror(errno)); |
| 414 | umount("/mnt/asec_secure"); |
| 415 | return -1; |
| 416 | } |
| 417 | |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | int Volume::doMoveMount(const char *src, const char *dst, bool force) { |
| 422 | unsigned int flags = MS_MOVE; |
| 423 | int retries = 5; |
| 424 | |
| 425 | while(retries--) { |
| 426 | if (!mount(src, dst, "", flags, NULL)) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 427 | if (mDebug) { |
| 428 | LOGD("Moved mount %s -> %s sucessfully", src, dst); |
| 429 | } |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 430 | return 0; |
| 431 | } else if (errno != EBUSY) { |
| 432 | LOGE("Failed to move mount %s -> %s (%s)", src, dst, strerror(errno)); |
| 433 | return -1; |
| 434 | } |
| 435 | int action = 0; |
| 436 | |
| 437 | if (force) { |
| 438 | if (retries == 1) { |
| 439 | action = 2; // SIGKILL |
| 440 | } else if (retries == 2) { |
| 441 | action = 1; // SIGHUP |
| 442 | } |
| 443 | } |
| 444 | LOGW("Failed to move %s -> %s (%s, retries %d, action %d)", |
| 445 | src, dst, strerror(errno), retries, action); |
| 446 | Process::killProcessesWithOpenFiles(src, action); |
| 447 | usleep(1000*250); |
| 448 | } |
| 449 | |
| 450 | errno = EBUSY; |
| 451 | LOGE("Giving up on move %s -> %s (%s)", src, dst, strerror(errno)); |
| 452 | return -1; |
| 453 | } |
| 454 | |
| 455 | int Volume::doUnmount(const char *path, bool force) { |
| 456 | int retries = 10; |
| 457 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 458 | if (mDebug) { |
| 459 | LOGD("Unmounting {%s}, force = %d", path, force); |
| 460 | } |
| 461 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 462 | while (retries--) { |
| 463 | if (!umount(path) || errno == EINVAL || errno == ENOENT) { |
| 464 | LOGI("%s sucessfully unmounted", path); |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | int action = 0; |
| 469 | |
| 470 | if (force) { |
| 471 | if (retries == 1) { |
| 472 | action = 2; // SIGKILL |
| 473 | } else if (retries == 2) { |
| 474 | action = 1; // SIGHUP |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | LOGW("Failed to unmount %s (%s, retries %d, action %d)", |
| 479 | path, strerror(errno), retries, action); |
| 480 | |
| 481 | Process::killProcessesWithOpenFiles(path, action); |
| 482 | usleep(1000*1000); |
| 483 | } |
| 484 | errno = EBUSY; |
| 485 | LOGE("Giving up on unmount %s (%s)", path, strerror(errno)); |
| 486 | return -1; |
| 487 | } |
| 488 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 489 | int Volume::unmountVol(bool force) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 490 | int i, rc; |
| 491 | |
| 492 | if (getState() != Volume::State_Mounted) { |
| 493 | LOGE("Volume %s unmount request when not mounted", getLabel()); |
| 494 | errno = EINVAL; |
| 495 | return -1; |
| 496 | } |
| 497 | |
| 498 | setState(Volume::State_Unmounting); |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 499 | usleep(1000 * 1000); // Give the framework some time to react |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 500 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 501 | /* |
| 502 | * First move the mountpoint back to our internal staging point |
| 503 | * so nobody else can muck with it while we work. |
| 504 | */ |
| 505 | if (doMoveMount(getMountpoint(), SEC_STGDIR, force)) { |
| 506 | LOGE("Failed to move mount %s => %s (%s)", getMountpoint(), SEC_STGDIR, strerror(errno)); |
| 507 | setState(Volume::State_Mounted); |
| 508 | return -1; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 509 | } |
| 510 | |
San Mehat | cb4dac8 | 2010-03-14 13:41:54 -0700 | [diff] [blame] | 511 | protectFromAutorunStupidity(); |
| 512 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 513 | /* |
| 514 | * Unmount the tmpfs which was obscuring the asec image directory |
| 515 | * from non root users |
| 516 | */ |
| 517 | |
| 518 | if (doUnmount(Volume::SEC_STG_SECIMGDIR, force)) { |
| 519 | LOGE("Failed to unmount tmpfs on %s (%s)", SEC_STG_SECIMGDIR, strerror(errno)); |
| 520 | goto fail_republish; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 521 | } |
| 522 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 523 | /* |
| 524 | * Remove the bindmount we were using to keep a reference to |
| 525 | * the previously obscured directory. |
| 526 | */ |
| 527 | |
| 528 | if (doUnmount(Volume::SEC_ASECDIR, force)) { |
| 529 | LOGE("Failed to remove bindmount on %s (%s)", SEC_ASECDIR, strerror(errno)); |
| 530 | goto fail_remount_tmpfs; |
| 531 | } |
| 532 | |
| 533 | /* |
| 534 | * Finally, unmount the actual block device from the staging dir |
| 535 | */ |
| 536 | if (doUnmount(Volume::SEC_STGDIR, force)) { |
| 537 | LOGE("Failed to unmount %s (%s)", SEC_STGDIR, strerror(errno)); |
| 538 | goto fail_recreate_bindmount; |
| 539 | } |
| 540 | |
| 541 | LOGI("%s unmounted sucessfully", getMountpoint()); |
| 542 | |
| 543 | setState(Volume::State_Idle); |
| 544 | mCurrentlyMountedKdev = -1; |
| 545 | return 0; |
| 546 | |
| 547 | /* |
| 548 | * Failure handling - try to restore everything back the way it was |
| 549 | */ |
| 550 | fail_recreate_bindmount: |
| 551 | if (mount(SEC_STG_SECIMGDIR, SEC_ASECDIR, "", MS_BIND, NULL)) { |
| 552 | LOGE("Failed to restore bindmount after failure! - Storage will appear offline!"); |
| 553 | goto out_nomedia; |
| 554 | } |
| 555 | fail_remount_tmpfs: |
| 556 | if (mount("tmpfs", SEC_STG_SECIMGDIR, "tmpfs", MS_RDONLY, "size=0,mode=0,uid=0,gid=0")) { |
| 557 | LOGE("Failed to restore tmpfs after failure! - Storage will appear offline!"); |
| 558 | goto out_nomedia; |
| 559 | } |
| 560 | fail_republish: |
| 561 | if (doMoveMount(SEC_STGDIR, getMountpoint(), force)) { |
| 562 | LOGE("Failed to republish mount after failure! - Storage will appear offline!"); |
| 563 | goto out_nomedia; |
| 564 | } |
| 565 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 566 | setState(Volume::State_Mounted); |
| 567 | return -1; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 568 | |
| 569 | out_nomedia: |
| 570 | setState(Volume::State_NoMedia); |
| 571 | return -1; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | int Volume::initializeMbr(const char *deviceNode) { |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 575 | struct disk_info dinfo; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 576 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 577 | memset(&dinfo, 0, sizeof(dinfo)); |
| 578 | |
| 579 | if (!(dinfo.part_lst = (struct part_info *) malloc(MAX_NUM_PARTS * sizeof(struct part_info)))) { |
| 580 | LOGE("Failed to malloc prt_lst"); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 581 | return -1; |
| 582 | } |
| 583 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 584 | memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info)); |
| 585 | dinfo.device = strdup(deviceNode); |
| 586 | dinfo.scheme = PART_SCHEME_MBR; |
| 587 | dinfo.sect_size = 512; |
| 588 | dinfo.skip_lba = 2048; |
| 589 | dinfo.num_lba = 0; |
| 590 | dinfo.num_parts = 1; |
| 591 | |
| 592 | struct part_info *pinfo = &dinfo.part_lst[0]; |
| 593 | |
| 594 | pinfo->name = strdup("android_sdcard"); |
| 595 | pinfo->flags |= PART_ACTIVE_FLAG; |
| 596 | pinfo->type = PC_PART_TYPE_FAT32; |
| 597 | pinfo->len_kb = -1; |
| 598 | |
| 599 | int rc = apply_disk_config(&dinfo, 0); |
| 600 | |
| 601 | if (rc) { |
| 602 | LOGE("Failed to apply disk configuration (%d)", rc); |
| 603 | goto out; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 604 | } |
| 605 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 606 | out: |
| 607 | free(pinfo->name); |
| 608 | free(dinfo.device); |
| 609 | free(dinfo.part_lst); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 610 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 611 | return rc; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 612 | } |