Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 17 | #include "Fat.h" |
| 18 | #include "PublicVolume.h" |
| 19 | #include "Utils.h" |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 20 | #include "VolumeManager.h" |
| 21 | #include "ResponseCode.h" |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 22 | |
Dan Albert | ae9e890 | 2015-03-16 10:35:17 -0700 | [diff] [blame] | 23 | #include <base/stringprintf.h> |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 24 | #include <base/logging.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 25 | #include <cutils/fs.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 26 | #include <private/android_filesystem_config.h> |
| 27 | |
| 28 | #include <fcntl.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <sys/mount.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/wait.h> |
| 34 | |
Dan Albert | ae9e890 | 2015-03-16 10:35:17 -0700 | [diff] [blame] | 35 | using android::base::StringPrintf; |
| 36 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 37 | namespace android { |
| 38 | namespace vold { |
| 39 | |
| 40 | static const char* kBlkidPath = "/system/bin/blkid"; |
| 41 | static const char* kFusePath = "/system/bin/sdcard"; |
| 42 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 43 | static const char* kAsecPath = "/mnt/secure/asec"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 44 | |
| 45 | PublicVolume::PublicVolume(dev_t device) : |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 46 | VolumeBase(Type::kPublic), mDevice(device), mFusePid(0) { |
| 47 | setId(StringPrintf("public:%u,%u", major(device), minor(device))); |
| 48 | mDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str()); |
| 49 | CreateDeviceNode(mDevPath, mDevice); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | PublicVolume::~PublicVolume() { |
| 53 | DestroyDeviceNode(mDevPath); |
| 54 | } |
| 55 | |
| 56 | status_t PublicVolume::readMetadata() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 57 | mFsType.clear(); |
| 58 | mFsUuid.clear(); |
| 59 | mFsLabel.clear(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 60 | |
| 61 | std::string path(StringPrintf("%s -c /dev/null %s", kBlkidPath, mDevPath.c_str())); |
| 62 | FILE* fp = popen(path.c_str(), "r"); |
| 63 | if (!fp) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 64 | PLOG(ERROR) << "Failed to run " << path; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 65 | return -errno; |
| 66 | } |
| 67 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 68 | status_t res = OK; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 69 | char line[1024]; |
| 70 | char value[128]; |
| 71 | if (fgets(line, sizeof(line), fp) != nullptr) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 72 | LOG(DEBUG) << "blkid identified as " << line; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 73 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 74 | char* start = strstr(line, "TYPE="); |
| 75 | if (start != nullptr && sscanf(start + 5, "\"%127[^\"]\"", value) == 1) { |
| 76 | mFsType = value; |
| 77 | } |
| 78 | |
| 79 | start = strstr(line, "UUID="); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 80 | if (start != nullptr && sscanf(start + 5, "\"%127[^\"]\"", value) == 1) { |
| 81 | mFsUuid = value; |
| 82 | } |
| 83 | |
| 84 | start = strstr(line, "LABEL="); |
| 85 | if (start != nullptr && sscanf(start + 6, "\"%127[^\"]\"", value) == 1) { |
| 86 | mFsLabel = value; |
| 87 | } |
| 88 | } else { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 89 | LOG(WARNING) << "blkid failed to identify " << mDevPath; |
| 90 | res = -ENODATA; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | pclose(fp); |
| 94 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 95 | VolumeManager::Instance()->getBroadcaster()->sendBroadcast( |
| 96 | ResponseCode::VolumeFsTypeChanged, |
| 97 | StringPrintf("%s %s", getId().c_str(), mFsType.c_str()).c_str(), false); |
| 98 | VolumeManager::Instance()->getBroadcaster()->sendBroadcast( |
| 99 | ResponseCode::VolumeFsUuidChanged, |
| 100 | StringPrintf("%s %s", getId().c_str(), mFsUuid.c_str()).c_str(), false); |
| 101 | VolumeManager::Instance()->getBroadcaster()->sendBroadcast( |
| 102 | ResponseCode::VolumeFsLabelChanged, |
| 103 | StringPrintf("%s %s", getId().c_str(), mFsLabel.c_str()).c_str(), false); |
| 104 | |
| 105 | return res; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | status_t PublicVolume::initAsecStage() { |
| 109 | std::string legacyPath(mRawPath + "/android_secure"); |
| 110 | std::string securePath(mRawPath + "/.android_secure"); |
| 111 | |
| 112 | // Recover legacy secure path |
| 113 | if (!access(legacyPath.c_str(), R_OK | X_OK) |
| 114 | && access(securePath.c_str(), R_OK | X_OK)) { |
| 115 | if (rename(legacyPath.c_str(), securePath.c_str())) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 116 | PLOG(WARNING) << "Failed to rename legacy ASEC dir"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 120 | if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) { |
| 121 | if (errno != EEXIST) { |
| 122 | PLOG(WARNING) << "Creating ASEC stage failed"; |
| 123 | return -errno; |
| 124 | } |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 127 | BindMount(securePath, kAsecPath); |
| 128 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 129 | return OK; |
| 130 | } |
| 131 | |
| 132 | status_t PublicVolume::doMount() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 133 | // TODO: expand to support mounting other filesystems |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 134 | if (Fat::check(mDevPath.c_str())) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 135 | LOG(ERROR) << "Failed filesystem check; not mounting"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 136 | return -EIO; |
| 137 | } |
| 138 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 139 | readMetadata(); |
| 140 | |
| 141 | // Use UUID as stable name, if available |
| 142 | std::string stableName = getId(); |
| 143 | if (!mFsUuid.empty()) { |
| 144 | stableName = "public:" + mFsUuid; |
| 145 | } |
| 146 | |
| 147 | mRawPath = StringPrintf("/mnt/media_rw/%s", stableName.c_str()); |
| 148 | mFusePath = StringPrintf("/storage/%s", stableName.c_str()); |
| 149 | setPath(mFusePath); |
| 150 | |
| 151 | if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT)) { |
| 152 | PLOG(ERROR) << "Failed to create mount point " << mRawPath; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 153 | return -errno; |
| 154 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 155 | if (fs_prepare_dir(mFusePath.c_str(), 0700, AID_ROOT, AID_ROOT)) { |
| 156 | PLOG(ERROR) << "Failed to create mount point " << mFusePath; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 157 | return -errno; |
| 158 | } |
| 159 | |
| 160 | if (Fat::doMount(mDevPath.c_str(), mRawPath.c_str(), false, false, false, |
| 161 | AID_MEDIA_RW, AID_MEDIA_RW, 0007, true)) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 162 | PLOG(ERROR) << "Failed to mount " << mDevPath; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 163 | return -EIO; |
| 164 | } |
| 165 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 166 | if (getFlags() & Flags::kPrimary) { |
| 167 | initAsecStage(); |
| 168 | } |
| 169 | |
| 170 | // Only need to spin up FUSE when visible |
| 171 | if (!(getFlags() & Flags::kVisible)) { |
| 172 | return OK; |
| 173 | } |
| 174 | |
| 175 | // TODO: teach FUSE daemon to protect itself with user-specific GID |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 176 | if (!(mFusePid = fork())) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 177 | if (getFlags() & Flags::kPrimary) { |
| 178 | if (execl(kFusePath, kFusePath, |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 179 | "-u", "1023", // AID_MEDIA_RW |
| 180 | "-g", "1023", // AID_MEDIA_RW |
| 181 | "-d", |
| 182 | mRawPath.c_str(), |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 183 | mFusePath.c_str(), |
| 184 | NULL)) { |
| 185 | PLOG(ERROR) << "Failed to exec"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 186 | } |
| 187 | } else { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 188 | if (execl(kFusePath, kFusePath, |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 189 | "-u", "1023", // AID_MEDIA_RW |
| 190 | "-g", "1023", // AID_MEDIA_RW |
| 191 | "-w", "1023", // AID_MEDIA_RW |
| 192 | "-d", |
| 193 | mRawPath.c_str(), |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 194 | mFusePath.c_str(), |
| 195 | NULL)) { |
| 196 | PLOG(ERROR) << "Failed to exec"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 200 | PLOG(DEBUG) << "FUSE exiting"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 201 | _exit(1); |
| 202 | } |
| 203 | |
| 204 | if (mFusePid == -1) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 205 | PLOG(ERROR) << "Failed to fork"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 206 | return -errno; |
| 207 | } |
| 208 | |
| 209 | return OK; |
| 210 | } |
| 211 | |
| 212 | status_t PublicVolume::doUnmount() { |
| 213 | if (mFusePid > 0) { |
| 214 | kill(mFusePid, SIGTERM); |
| 215 | TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0)); |
| 216 | mFusePid = 0; |
| 217 | } |
| 218 | |
| 219 | ForceUnmount(mFusePath); |
| 220 | ForceUnmount(mRawPath); |
| 221 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 222 | if (TEMP_FAILURE_RETRY(rmdir(mRawPath.c_str()))) { |
| 223 | PLOG(ERROR) << "Failed to rmdir mount point " << mRawPath; |
| 224 | } |
| 225 | if (TEMP_FAILURE_RETRY(rmdir(mFusePath.c_str()))) { |
| 226 | PLOG(ERROR) << "Failed to rmdir mount point " << mFusePath; |
| 227 | } |
| 228 | |
| 229 | mFusePath.clear(); |
| 230 | mRawPath.clear(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 231 | |
| 232 | return OK; |
| 233 | } |
| 234 | |
| 235 | status_t PublicVolume::doFormat() { |
| 236 | if (Fat::format(mDevPath.c_str(), 0, true)) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 237 | LOG(ERROR) << "Failed to format"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 238 | return -errno; |
| 239 | } |
| 240 | return OK; |
| 241 | } |
| 242 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 243 | } // namespace vold |
| 244 | } // namespace android |