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