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> |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 28 | #include <sys/param.h> |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 29 | |
| 30 | #include <linux/kdev_t.h> |
| 31 | |
| 32 | #include <cutils/properties.h> |
| 33 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 34 | #include <diskconfig/diskconfig.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 35 | |
Mike Lockwood | 9092b1d | 2011-03-23 14:55:49 -0400 | [diff] [blame] | 36 | #include <private/android_filesystem_config.h> |
| 37 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 38 | #define LOG_TAG "Vold" |
| 39 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 40 | #include <cutils/fs.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 41 | #include <cutils/log.h> |
| 42 | |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 43 | #include <string> |
| 44 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 45 | #include "Volume.h" |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 46 | #include "VolumeManager.h" |
| 47 | #include "ResponseCode.h" |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 48 | #include "Fat.h" |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 49 | #include "Process.h" |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 50 | #include "cryptfs.h" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 51 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 52 | extern "C" void dos_partition_dec(void const *pp, struct dos_partition *d); |
| 53 | extern "C" void dos_partition_enc(void *pp, struct dos_partition *d); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 54 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 55 | |
| 56 | /* |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 57 | * Media directory - stuff that only media_rw user can see |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 58 | */ |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 59 | const char *Volume::MEDIA_DIR = "/mnt/media_rw"; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 60 | |
| 61 | /* |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 62 | * Fuse directory - location where fuse wrapped filesystems go |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 63 | */ |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 64 | const char *Volume::FUSE_DIR = "/storage"; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 65 | |
| 66 | /* |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 67 | * Path to external storage where *only* root can access ASEC image files |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 68 | */ |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 69 | const char *Volume::SEC_ASECDIR_EXT = "/mnt/secure/asec"; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 70 | |
| 71 | /* |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 72 | * Path to internal storage where *only* root can access ASEC image files |
| 73 | */ |
| 74 | const char *Volume::SEC_ASECDIR_INT = "/data/app-asec"; |
Jeff Sharkey | 8c2c15b | 2013-10-17 15:17:19 -0700 | [diff] [blame] | 75 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 76 | /* |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 77 | * Path to where secure containers are mounted |
| 78 | */ |
| 79 | const char *Volume::ASECDIR = "/mnt/asec"; |
| 80 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 81 | /* |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 82 | * Path to where OBBs are mounted |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 83 | */ |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 84 | const char *Volume::LOOPDIR = "/mnt/obb"; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 85 | |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 86 | const char *Volume::BLKID_PATH = "/system/bin/blkid"; |
| 87 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 88 | static const char *stateToStr(int state) { |
| 89 | if (state == Volume::State_Init) |
| 90 | return "Initializing"; |
| 91 | else if (state == Volume::State_NoMedia) |
| 92 | return "No-Media"; |
| 93 | else if (state == Volume::State_Idle) |
| 94 | return "Idle-Unmounted"; |
| 95 | else if (state == Volume::State_Pending) |
| 96 | return "Pending"; |
| 97 | else if (state == Volume::State_Mounted) |
| 98 | return "Mounted"; |
| 99 | else if (state == Volume::State_Unmounting) |
| 100 | return "Unmounting"; |
| 101 | else if (state == Volume::State_Checking) |
| 102 | return "Checking"; |
| 103 | else if (state == Volume::State_Formatting) |
| 104 | return "Formatting"; |
| 105 | else if (state == Volume::State_Shared) |
| 106 | return "Shared-Unmounted"; |
| 107 | else if (state == Volume::State_SharedMnt) |
| 108 | return "Shared-Mounted"; |
| 109 | else |
| 110 | return "Unknown-Error"; |
| 111 | } |
| 112 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 113 | Volume::Volume(VolumeManager *vm, const fstab_rec* rec, int flags) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 114 | mVm = vm; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 115 | mDebug = false; |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 116 | mLabel = strdup(rec->label); |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 117 | mUuid = NULL; |
| 118 | mUserLabel = NULL; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 119 | mState = Volume::State_Init; |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 120 | mFlags = flags; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 121 | mCurrentlyMountedKdev = -1; |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 122 | mPartIdx = rec->partnum; |
Joseph Lehrer | 507d31b | 2011-04-11 15:02:50 -0700 | [diff] [blame] | 123 | mRetryMount = false; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | Volume::~Volume() { |
| 127 | free(mLabel); |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 128 | free(mUuid); |
| 129 | free(mUserLabel); |
San Mehat | cb4dac8 | 2010-03-14 13:41:54 -0700 | [diff] [blame] | 130 | } |
| 131 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 132 | void Volume::setDebug(bool enable) { |
| 133 | mDebug = enable; |
| 134 | } |
| 135 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 136 | dev_t Volume::getDiskDevice() { |
| 137 | return MKDEV(0, 0); |
| 138 | }; |
| 139 | |
Mike Lockwood | 2dfe297 | 2010-09-17 18:50:51 -0400 | [diff] [blame] | 140 | dev_t Volume::getShareDevice() { |
| 141 | return getDiskDevice(); |
| 142 | } |
| 143 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 144 | void Volume::handleVolumeShared() { |
| 145 | } |
| 146 | |
| 147 | void Volume::handleVolumeUnshared() { |
| 148 | } |
| 149 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 150 | int Volume::handleBlockEvent(NetlinkEvent *evt) { |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 151 | errno = ENOSYS; |
| 152 | return -1; |
| 153 | } |
| 154 | |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 155 | void Volume::setUuid(const char* uuid) { |
| 156 | char msg[256]; |
| 157 | |
| 158 | if (mUuid) { |
| 159 | free(mUuid); |
| 160 | } |
| 161 | |
| 162 | if (uuid) { |
| 163 | mUuid = strdup(uuid); |
| 164 | snprintf(msg, sizeof(msg), "%s %s \"%s\"", getLabel(), |
| 165 | getFuseMountpoint(), mUuid); |
| 166 | } else { |
| 167 | mUuid = NULL; |
| 168 | snprintf(msg, sizeof(msg), "%s %s", getLabel(), getFuseMountpoint()); |
| 169 | } |
| 170 | |
| 171 | mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeUuidChange, msg, |
| 172 | false); |
| 173 | } |
| 174 | |
| 175 | void Volume::setUserLabel(const char* userLabel) { |
| 176 | char msg[256]; |
| 177 | |
| 178 | if (mUserLabel) { |
| 179 | free(mUserLabel); |
| 180 | } |
| 181 | |
| 182 | if (userLabel) { |
| 183 | mUserLabel = strdup(userLabel); |
| 184 | snprintf(msg, sizeof(msg), "%s %s \"%s\"", getLabel(), |
| 185 | getFuseMountpoint(), mUserLabel); |
| 186 | } else { |
| 187 | mUserLabel = NULL; |
| 188 | snprintf(msg, sizeof(msg), "%s %s", getLabel(), getFuseMountpoint()); |
| 189 | } |
| 190 | |
| 191 | mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeUserLabelChange, |
| 192 | msg, false); |
| 193 | } |
| 194 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 195 | void Volume::setState(int state) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 196 | char msg[255]; |
| 197 | int oldState = mState; |
| 198 | |
| 199 | if (oldState == state) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 200 | SLOGW("Duplicate state (%d)\n", state); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 201 | return; |
| 202 | } |
| 203 | |
Joseph Lehrer | 507d31b | 2011-04-11 15:02:50 -0700 | [diff] [blame] | 204 | if ((oldState == Volume::State_Pending) && (state != Volume::State_Idle)) { |
| 205 | mRetryMount = false; |
| 206 | } |
| 207 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 208 | mState = state; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 209 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 210 | SLOGD("Volume %s state changing %d (%s) -> %d (%s)", mLabel, |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 211 | oldState, stateToStr(oldState), mState, stateToStr(mState)); |
| 212 | snprintf(msg, sizeof(msg), |
| 213 | "Volume %s %s state changed from %d (%s) to %d (%s)", getLabel(), |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 214 | getFuseMountpoint(), oldState, stateToStr(oldState), mState, |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 215 | stateToStr(mState)); |
| 216 | |
| 217 | mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeStateChange, |
| 218 | msg, false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 219 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 220 | |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame] | 221 | int Volume::createDeviceNode(const char *path, int major, int minor) { |
| 222 | mode_t mode = 0660 | S_IFBLK; |
| 223 | dev_t dev = (major << 8) | minor; |
| 224 | if (mknod(path, mode, dev) < 0) { |
| 225 | if (errno != EEXIST) { |
| 226 | return -1; |
| 227 | } |
| 228 | } |
| 229 | return 0; |
| 230 | } |
| 231 | |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 232 | int Volume::formatVol(bool wipe) { |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 233 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 234 | if (getState() == Volume::State_NoMedia) { |
| 235 | errno = ENODEV; |
| 236 | return -1; |
| 237 | } else if (getState() != Volume::State_Idle) { |
| 238 | errno = EBUSY; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 239 | return -1; |
| 240 | } |
| 241 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 242 | if (isMountpointMounted(getMountpoint())) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 243 | SLOGW("Volume is idle but appears to be mounted - fixing"); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 244 | setState(Volume::State_Mounted); |
| 245 | // mCurrentlyMountedKdev = XXX |
| 246 | errno = EBUSY; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 247 | return -1; |
| 248 | } |
| 249 | |
Mike Lockwood | a4886f1 | 2010-09-21 13:56:35 -0400 | [diff] [blame] | 250 | bool formatEntireDevice = (mPartIdx == -1); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 251 | char devicePath[255]; |
| 252 | dev_t diskNode = getDiskDevice(); |
Mike Lockwood | a4886f1 | 2010-09-21 13:56:35 -0400 | [diff] [blame] | 253 | dev_t partNode = MKDEV(MAJOR(diskNode), (formatEntireDevice ? 1 : mPartIdx)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 254 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 255 | setState(Volume::State_Formatting); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 256 | |
Chih-Wei Huang | 64382de | 2010-11-16 13:18:19 +0800 | [diff] [blame] | 257 | int ret = -1; |
Mike Lockwood | a4886f1 | 2010-09-21 13:56:35 -0400 | [diff] [blame] | 258 | // Only initialize the MBR if we are formatting the entire device |
| 259 | if (formatEntireDevice) { |
| 260 | sprintf(devicePath, "/dev/block/vold/%d:%d", |
| 261 | MAJOR(diskNode), MINOR(diskNode)); |
| 262 | |
| 263 | if (initializeMbr(devicePath)) { |
| 264 | SLOGE("Failed to initialize MBR (%s)", strerror(errno)); |
| 265 | goto err; |
| 266 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 267 | } |
| 268 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 269 | sprintf(devicePath, "/dev/block/vold/%d:%d", |
| 270 | MAJOR(partNode), MINOR(partNode)); |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame] | 271 | |
Mike Lockwood | a4886f1 | 2010-09-21 13:56:35 -0400 | [diff] [blame] | 272 | if (mDebug) { |
| 273 | SLOGI("Formatting volume %s (%s)", getLabel(), devicePath); |
| 274 | } |
| 275 | |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 276 | if (Fat::format(devicePath, 0, wipe)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 277 | SLOGE("Failed to format (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 278 | goto err; |
| 279 | } |
| 280 | |
Chih-Wei Huang | 64382de | 2010-11-16 13:18:19 +0800 | [diff] [blame] | 281 | ret = 0; |
| 282 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 283 | err: |
Chih-Wei Huang | 64382de | 2010-11-16 13:18:19 +0800 | [diff] [blame] | 284 | setState(Volume::State_Idle); |
| 285 | return ret; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | bool Volume::isMountpointMounted(const char *path) { |
| 289 | char device[256]; |
| 290 | char mount_path[256]; |
| 291 | char rest[256]; |
| 292 | FILE *fp; |
| 293 | char line[1024]; |
| 294 | |
| 295 | if (!(fp = fopen("/proc/mounts", "r"))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 296 | SLOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 297 | return false; |
| 298 | } |
| 299 | |
| 300 | while(fgets(line, sizeof(line), fp)) { |
| 301 | line[strlen(line)-1] = '\0'; |
| 302 | sscanf(line, "%255s %255s %255s\n", device, mount_path, rest); |
| 303 | if (!strcmp(mount_path, path)) { |
| 304 | fclose(fp); |
| 305 | return true; |
| 306 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | fclose(fp); |
| 310 | return false; |
| 311 | } |
| 312 | |
| 313 | int Volume::mountVol() { |
| 314 | dev_t deviceNodes[4]; |
| 315 | int n, i, rc = 0; |
| 316 | char errmsg[255]; |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 317 | |
| 318 | int flags = getFlags(); |
| 319 | bool providesAsec = (flags & VOL_PROVIDES_ASEC) != 0; |
| 320 | |
| 321 | // TODO: handle "bind" style mounts, for emulated storage |
| 322 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 323 | char decrypt_state[PROPERTY_VALUE_MAX]; |
| 324 | char crypto_state[PROPERTY_VALUE_MAX]; |
| 325 | char encrypt_progress[PROPERTY_VALUE_MAX]; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 326 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 327 | property_get("vold.decrypt", decrypt_state, ""); |
| 328 | property_get("vold.encrypt_progress", encrypt_progress, ""); |
| 329 | |
| 330 | /* Don't try to mount the volumes if we have not yet entered the disk password |
| 331 | * or are in the process of encrypting. |
| 332 | */ |
| 333 | if ((getState() == Volume::State_NoMedia) || |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 334 | ((!strcmp(decrypt_state, "1") || encrypt_progress[0]) && providesAsec)) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 335 | snprintf(errmsg, sizeof(errmsg), |
| 336 | "Volume %s %s mount failed - no media", |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 337 | getLabel(), getFuseMountpoint()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 338 | mVm->getBroadcaster()->sendBroadcast( |
| 339 | ResponseCode::VolumeMountFailedNoMedia, |
| 340 | errmsg, false); |
| 341 | errno = ENODEV; |
| 342 | return -1; |
| 343 | } else if (getState() != Volume::State_Idle) { |
| 344 | errno = EBUSY; |
Joseph Lehrer | 507d31b | 2011-04-11 15:02:50 -0700 | [diff] [blame] | 345 | if (getState() == Volume::State_Pending) { |
| 346 | mRetryMount = true; |
| 347 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 348 | return -1; |
| 349 | } |
| 350 | |
| 351 | if (isMountpointMounted(getMountpoint())) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 352 | SLOGW("Volume is idle but appears to be mounted - fixing"); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 353 | setState(Volume::State_Mounted); |
| 354 | // mCurrentlyMountedKdev = XXX |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | n = getDeviceNodes((dev_t *) &deviceNodes, 4); |
| 359 | if (!n) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 360 | SLOGE("Failed to get device nodes (%s)\n", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 361 | return -1; |
| 362 | } |
| 363 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 364 | /* If we're running encrypted, and the volume is marked as encryptable and nonremovable, |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 365 | * and also marked as providing Asec storage, then we need to decrypt |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 366 | * that partition, and update the volume object to point to it's new decrypted |
| 367 | * block device |
| 368 | */ |
| 369 | property_get("ro.crypto.state", crypto_state, ""); |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 370 | if (providesAsec && |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 371 | ((flags & (VOL_NONREMOVABLE | VOL_ENCRYPTABLE))==(VOL_NONREMOVABLE | VOL_ENCRYPTABLE)) && |
| 372 | !strcmp(crypto_state, "encrypted") && !isDecrypted()) { |
| 373 | char new_sys_path[MAXPATHLEN]; |
| 374 | char nodepath[256]; |
| 375 | int new_major, new_minor; |
| 376 | |
| 377 | if (n != 1) { |
| 378 | /* We only expect one device node returned when mounting encryptable volumes */ |
| 379 | SLOGE("Too many device nodes returned when mounting %d\n", getMountpoint()); |
| 380 | return -1; |
| 381 | } |
| 382 | |
| 383 | if (cryptfs_setup_volume(getLabel(), MAJOR(deviceNodes[0]), MINOR(deviceNodes[0]), |
| 384 | new_sys_path, sizeof(new_sys_path), |
| 385 | &new_major, &new_minor)) { |
| 386 | SLOGE("Cannot setup encryption mapping for %d\n", getMountpoint()); |
| 387 | return -1; |
| 388 | } |
| 389 | /* We now have the new sysfs path for the decrypted block device, and the |
| 390 | * majore and minor numbers for it. So, create the device, update the |
| 391 | * path to the new sysfs path, and continue. |
| 392 | */ |
| 393 | snprintf(nodepath, |
| 394 | sizeof(nodepath), "/dev/block/vold/%d:%d", |
| 395 | new_major, new_minor); |
| 396 | if (createDeviceNode(nodepath, new_major, new_minor)) { |
| 397 | SLOGE("Error making device node '%s' (%s)", nodepath, |
| 398 | strerror(errno)); |
| 399 | } |
| 400 | |
| 401 | // Todo: Either create sys filename from nodepath, or pass in bogus path so |
| 402 | // vold ignores state changes on this internal device. |
| 403 | updateDeviceInfo(nodepath, new_major, new_minor); |
| 404 | |
| 405 | /* Get the device nodes again, because they just changed */ |
| 406 | n = getDeviceNodes((dev_t *) &deviceNodes, 4); |
| 407 | if (!n) { |
| 408 | SLOGE("Failed to get device nodes (%s)\n", strerror(errno)); |
| 409 | return -1; |
| 410 | } |
| 411 | } |
| 412 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 413 | for (i = 0; i < n; i++) { |
| 414 | char devicePath[255]; |
| 415 | |
| 416 | sprintf(devicePath, "/dev/block/vold/%d:%d", MAJOR(deviceNodes[i]), |
| 417 | MINOR(deviceNodes[i])); |
| 418 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 419 | SLOGI("%s being considered for volume %s\n", devicePath, getLabel()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 420 | |
| 421 | errno = 0; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 422 | setState(Volume::State_Checking); |
| 423 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 424 | if (Fat::check(devicePath)) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 425 | if (errno == ENODATA) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 426 | SLOGW("%s does not contain a FAT filesystem\n", devicePath); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 427 | continue; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 428 | } |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 429 | errno = EIO; |
| 430 | /* Badness - abort the mount */ |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 431 | SLOGE("%s failed FS checks (%s)", devicePath, strerror(errno)); |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 432 | setState(Volume::State_Idle); |
| 433 | return -1; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 434 | } |
| 435 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 436 | errno = 0; |
Mike Lockwood | 9092b1d | 2011-03-23 14:55:49 -0400 | [diff] [blame] | 437 | int gid; |
| 438 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 439 | if (Fat::doMount(devicePath, getMountpoint(), false, false, false, |
| 440 | AID_MEDIA_RW, AID_MEDIA_RW, 0007, true)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 441 | SLOGE("%s failed to mount via VFAT (%s)\n", devicePath, strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 442 | continue; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 443 | } |
| 444 | |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 445 | extractMetadata(devicePath); |
San Mehat | cb4dac8 | 2010-03-14 13:41:54 -0700 | [diff] [blame] | 446 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 447 | if (providesAsec && mountAsecExternal() != 0) { |
| 448 | SLOGE("Failed to mount secure area (%s)", strerror(errno)); |
| 449 | umount(getMountpoint()); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 450 | setState(Volume::State_Idle); |
| 451 | return -1; |
| 452 | } |
| 453 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 454 | char service[64]; |
| 455 | snprintf(service, 64, "fuse_%s", getLabel()); |
| 456 | property_set("ctl.start", service); |
| 457 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 458 | setState(Volume::State_Mounted); |
| 459 | mCurrentlyMountedKdev = deviceNodes[i]; |
| 460 | return 0; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 461 | } |
| 462 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 463 | SLOGE("Volume %s found no suitable devices for mounting :(\n", getLabel()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 464 | setState(Volume::State_Idle); |
| 465 | |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 466 | return -1; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 467 | } |
| 468 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 469 | int Volume::mountAsecExternal() { |
| 470 | char legacy_path[PATH_MAX]; |
| 471 | char secure_path[PATH_MAX]; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 472 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 473 | snprintf(legacy_path, PATH_MAX, "%s/android_secure", getMountpoint()); |
| 474 | snprintf(secure_path, PATH_MAX, "%s/.android_secure", getMountpoint()); |
| 475 | |
| 476 | // Recover legacy secure path |
| 477 | if (!access(legacy_path, R_OK | X_OK) && access(secure_path, R_OK | X_OK)) { |
| 478 | if (rename(legacy_path, secure_path)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 479 | SLOGE("Failed to rename legacy asec dir (%s)", strerror(errno)); |
San Mehat | 52c2ccb | 2010-02-23 18:26:13 -0800 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 483 | if (fs_prepare_dir(secure_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) != 0) { |
Jeff Sharkey | 8c2c15b | 2013-10-17 15:17:19 -0700 | [diff] [blame] | 484 | SLOGW("fs_prepare_dir failed: %s", strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 485 | return -1; |
| 486 | } |
| 487 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 488 | if (mount(secure_path, SEC_ASECDIR_EXT, "", MS_BIND, NULL)) { |
| 489 | SLOGE("Failed to bind mount points %s -> %s (%s)", secure_path, |
| 490 | SEC_ASECDIR_EXT, strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 491 | return -1; |
| 492 | } |
| 493 | |
| 494 | return 0; |
| 495 | } |
| 496 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 497 | int Volume::doUnmount(const char *path, bool force) { |
| 498 | int retries = 10; |
| 499 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 500 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 501 | SLOGD("Unmounting {%s}, force = %d", path, force); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 502 | } |
| 503 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 504 | while (retries--) { |
| 505 | if (!umount(path) || errno == EINVAL || errno == ENOENT) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 506 | SLOGI("%s sucessfully unmounted", path); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | int action = 0; |
| 511 | |
| 512 | if (force) { |
| 513 | if (retries == 1) { |
| 514 | action = 2; // SIGKILL |
| 515 | } else if (retries == 2) { |
| 516 | action = 1; // SIGHUP |
| 517 | } |
| 518 | } |
| 519 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 520 | SLOGW("Failed to unmount %s (%s, retries %d, action %d)", |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 521 | path, strerror(errno), retries, action); |
| 522 | |
| 523 | Process::killProcessesWithOpenFiles(path, action); |
| 524 | usleep(1000*1000); |
| 525 | } |
| 526 | errno = EBUSY; |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 527 | SLOGE("Giving up on unmount %s (%s)", path, strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 528 | return -1; |
| 529 | } |
| 530 | |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 531 | int Volume::unmountVol(bool force, bool revert) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 532 | int i, rc; |
| 533 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 534 | int flags = getFlags(); |
| 535 | bool providesAsec = (flags & VOL_PROVIDES_ASEC) != 0; |
| 536 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 537 | if (getState() != Volume::State_Mounted) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 538 | SLOGE("Volume %s unmount request when not mounted", getLabel()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 539 | errno = EINVAL; |
Ken Sumrall | 319b104 | 2011-06-14 14:01:55 -0700 | [diff] [blame] | 540 | return UNMOUNT_NOT_MOUNTED_ERR; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | setState(Volume::State_Unmounting); |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 544 | usleep(1000 * 1000); // Give the framework some time to react |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 545 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 546 | char service[64]; |
| 547 | snprintf(service, 64, "fuse_%s", getLabel()); |
| 548 | property_set("ctl.stop", service); |
| 549 | /* Give it a chance to stop. I wish we had a synchronous way to determine this... */ |
| 550 | sleep(1); |
| 551 | |
| 552 | // TODO: determine failure mode if FUSE times out |
| 553 | |
| 554 | if (providesAsec && doUnmount(Volume::SEC_ASECDIR_EXT, force) != 0) { |
| 555 | SLOGE("Failed to unmount secure area on %s (%s)", getMountpoint(), strerror(errno)); |
| 556 | goto out_mounted; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 557 | } |
| 558 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 559 | /* Now that the fuse daemon is dead, unmount it */ |
| 560 | if (doUnmount(getFuseMountpoint(), force) != 0) { |
| 561 | SLOGE("Failed to unmount %s (%s)", getFuseMountpoint(), strerror(errno)); |
| 562 | goto fail_remount_secure; |
Jeff Sharkey | 7a3c3d4 | 2012-10-04 16:49:22 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 565 | /* Unmount the real sd card */ |
| 566 | if (doUnmount(getMountpoint(), force) != 0) { |
| 567 | SLOGE("Failed to unmount %s (%s)", getMountpoint(), strerror(errno)); |
| 568 | goto fail_remount_secure; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 569 | } |
| 570 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 571 | SLOGI("%s unmounted successfully", getMountpoint()); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 572 | |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 573 | /* If this is an encrypted volume, and we've been asked to undo |
| 574 | * the crypto mapping, then revert the dm-crypt mapping, and revert |
| 575 | * the device info to the original values. |
| 576 | */ |
| 577 | if (revert && isDecrypted()) { |
| 578 | cryptfs_revert_volume(getLabel()); |
| 579 | revertDeviceInfo(); |
| 580 | SLOGI("Encrypted volume %s reverted successfully", getMountpoint()); |
| 581 | } |
| 582 | |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 583 | setUuid(NULL); |
| 584 | setUserLabel(NULL); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 585 | setState(Volume::State_Idle); |
| 586 | mCurrentlyMountedKdev = -1; |
| 587 | return 0; |
| 588 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 589 | fail_remount_secure: |
| 590 | if (providesAsec && mountAsecExternal() != 0) { |
| 591 | SLOGE("Failed to remount secure area (%s)", strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 592 | goto out_nomedia; |
| 593 | } |
| 594 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 595 | out_mounted: |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 596 | setState(Volume::State_Mounted); |
| 597 | return -1; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 598 | |
| 599 | out_nomedia: |
| 600 | setState(Volume::State_NoMedia); |
| 601 | return -1; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 602 | } |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 603 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 604 | int Volume::initializeMbr(const char *deviceNode) { |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 605 | struct disk_info dinfo; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 606 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 607 | memset(&dinfo, 0, sizeof(dinfo)); |
| 608 | |
| 609 | 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] | 610 | SLOGE("Failed to malloc prt_lst"); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 611 | return -1; |
| 612 | } |
| 613 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 614 | memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info)); |
| 615 | dinfo.device = strdup(deviceNode); |
| 616 | dinfo.scheme = PART_SCHEME_MBR; |
| 617 | dinfo.sect_size = 512; |
| 618 | dinfo.skip_lba = 2048; |
| 619 | dinfo.num_lba = 0; |
| 620 | dinfo.num_parts = 1; |
| 621 | |
| 622 | struct part_info *pinfo = &dinfo.part_lst[0]; |
| 623 | |
| 624 | pinfo->name = strdup("android_sdcard"); |
| 625 | pinfo->flags |= PART_ACTIVE_FLAG; |
| 626 | pinfo->type = PC_PART_TYPE_FAT32; |
| 627 | pinfo->len_kb = -1; |
| 628 | |
| 629 | int rc = apply_disk_config(&dinfo, 0); |
| 630 | |
| 631 | if (rc) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 632 | SLOGE("Failed to apply disk configuration (%d)", rc); |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 633 | goto out; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 634 | } |
| 635 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 636 | out: |
| 637 | free(pinfo->name); |
| 638 | free(dinfo.device); |
| 639 | free(dinfo.part_lst); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 640 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 641 | return rc; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 642 | } |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 643 | |
| 644 | /* |
| 645 | * Use blkid to extract UUID and label from device, since it handles many |
| 646 | * obscure edge cases around partition types and formats. Always broadcasts |
| 647 | * updated metadata values. |
| 648 | */ |
| 649 | int Volume::extractMetadata(const char* devicePath) { |
| 650 | int res = 0; |
| 651 | |
| 652 | std::string cmd; |
| 653 | cmd = BLKID_PATH; |
| 654 | cmd += " -c /dev/null "; |
| 655 | cmd += devicePath; |
| 656 | |
| 657 | FILE* fp = popen(cmd.c_str(), "r"); |
| 658 | if (!fp) { |
| 659 | ALOGE("Failed to run %s: %s", cmd.c_str(), strerror(errno)); |
| 660 | res = -1; |
| 661 | goto done; |
| 662 | } |
| 663 | |
| 664 | char line[1024]; |
| 665 | char value[128]; |
| 666 | if (fgets(line, sizeof(line), fp) != NULL) { |
Jeff Sharkey | 8c2c15b | 2013-10-17 15:17:19 -0700 | [diff] [blame] | 667 | ALOGD("blkid identified as %s", line); |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 668 | |
| 669 | char* start = strstr(line, "UUID=") + 5; |
| 670 | if (sscanf(start, "\"%127[^\"]\"", value) == 1) { |
| 671 | setUuid(value); |
| 672 | } else { |
| 673 | setUuid(NULL); |
| 674 | } |
| 675 | |
| 676 | start = strstr(line, "LABEL=") + 6; |
| 677 | if (sscanf(start, "\"%127[^\"]\"", value) == 1) { |
| 678 | setUserLabel(value); |
| 679 | } else { |
| 680 | setUserLabel(NULL); |
| 681 | } |
| 682 | } else { |
Jeff Sharkey | 8c2c15b | 2013-10-17 15:17:19 -0700 | [diff] [blame] | 683 | ALOGW("blkid failed to identify %s", devicePath); |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 684 | res = -1; |
| 685 | } |
| 686 | |
| 687 | pclose(fp); |
| 688 | |
| 689 | done: |
| 690 | if (res == -1) { |
| 691 | setUuid(NULL); |
| 692 | setUserLabel(NULL); |
| 693 | } |
| 694 | return res; |
| 695 | } |