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 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 40 | static const char* kFusePath = "/system/bin/sdcard"; |
| 41 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 42 | static const char* kAsecPath = "/mnt/secure/asec"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 43 | |
| 44 | PublicVolume::PublicVolume(dev_t device) : |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 45 | VolumeBase(Type::kPublic), mDevice(device), mFusePid(0) { |
| 46 | setId(StringPrintf("public:%u,%u", major(device), minor(device))); |
| 47 | mDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str()); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | PublicVolume::~PublicVolume() { |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | status_t PublicVolume::readMetadata() { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame^] | 54 | status_t res = ReadMetadata(mDevPath, mFsType, mFsUuid, mFsLabel); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 55 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 56 | VolumeManager::Instance()->getBroadcaster()->sendBroadcast( |
| 57 | ResponseCode::VolumeFsTypeChanged, |
| 58 | StringPrintf("%s %s", getId().c_str(), mFsType.c_str()).c_str(), false); |
| 59 | VolumeManager::Instance()->getBroadcaster()->sendBroadcast( |
| 60 | ResponseCode::VolumeFsUuidChanged, |
| 61 | StringPrintf("%s %s", getId().c_str(), mFsUuid.c_str()).c_str(), false); |
| 62 | VolumeManager::Instance()->getBroadcaster()->sendBroadcast( |
| 63 | ResponseCode::VolumeFsLabelChanged, |
| 64 | StringPrintf("%s %s", getId().c_str(), mFsLabel.c_str()).c_str(), false); |
| 65 | |
| 66 | return res; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | status_t PublicVolume::initAsecStage() { |
| 70 | std::string legacyPath(mRawPath + "/android_secure"); |
| 71 | std::string securePath(mRawPath + "/.android_secure"); |
| 72 | |
| 73 | // Recover legacy secure path |
| 74 | if (!access(legacyPath.c_str(), R_OK | X_OK) |
| 75 | && access(securePath.c_str(), R_OK | X_OK)) { |
| 76 | if (rename(legacyPath.c_str(), securePath.c_str())) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame^] | 77 | PLOG(WARNING) << getId() << " failed to rename legacy ASEC dir"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 81 | if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) { |
| 82 | if (errno != EEXIST) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame^] | 83 | PLOG(WARNING) << getId() << " creating ASEC stage failed"; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 84 | return -errno; |
| 85 | } |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 88 | BindMount(securePath, kAsecPath); |
| 89 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 90 | return OK; |
| 91 | } |
| 92 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame^] | 93 | status_t PublicVolume::doCreate() { |
| 94 | return CreateDeviceNode(mDevPath, mDevice); |
| 95 | } |
| 96 | |
| 97 | status_t PublicVolume::doDestroy() { |
| 98 | return DestroyDeviceNode(mDevPath); |
| 99 | } |
| 100 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 101 | status_t PublicVolume::doMount() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 102 | // TODO: expand to support mounting other filesystems |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame^] | 103 | readMetadata(); |
| 104 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 105 | if (Fat::check(mDevPath.c_str())) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame^] | 106 | LOG(ERROR) << getId() << " failed filesystem check"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 107 | return -EIO; |
| 108 | } |
| 109 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 110 | // Use UUID as stable name, if available |
| 111 | std::string stableName = getId(); |
| 112 | if (!mFsUuid.empty()) { |
| 113 | stableName = "public:" + mFsUuid; |
| 114 | } |
| 115 | |
| 116 | mRawPath = StringPrintf("/mnt/media_rw/%s", stableName.c_str()); |
| 117 | mFusePath = StringPrintf("/storage/%s", stableName.c_str()); |
| 118 | setPath(mFusePath); |
| 119 | |
| 120 | if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT)) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame^] | 121 | PLOG(ERROR) << getId() << " failed to create mount point " << mRawPath; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 122 | return -errno; |
| 123 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 124 | if (fs_prepare_dir(mFusePath.c_str(), 0700, AID_ROOT, AID_ROOT)) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame^] | 125 | PLOG(ERROR) << getId() << " failed to create mount point " << mFusePath; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 126 | return -errno; |
| 127 | } |
| 128 | |
| 129 | if (Fat::doMount(mDevPath.c_str(), mRawPath.c_str(), false, false, false, |
| 130 | AID_MEDIA_RW, AID_MEDIA_RW, 0007, true)) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame^] | 131 | PLOG(ERROR) << getId() << " failed to mount " << mDevPath; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 132 | return -EIO; |
| 133 | } |
| 134 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 135 | if (getFlags() & Flags::kPrimary) { |
| 136 | initAsecStage(); |
| 137 | } |
| 138 | |
| 139 | // Only need to spin up FUSE when visible |
| 140 | if (!(getFlags() & Flags::kVisible)) { |
| 141 | return OK; |
| 142 | } |
| 143 | |
| 144 | // TODO: teach FUSE daemon to protect itself with user-specific GID |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 145 | if (!(mFusePid = fork())) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 146 | if (getFlags() & Flags::kPrimary) { |
| 147 | if (execl(kFusePath, kFusePath, |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 148 | "-u", "1023", // AID_MEDIA_RW |
| 149 | "-g", "1023", // AID_MEDIA_RW |
| 150 | "-d", |
| 151 | mRawPath.c_str(), |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 152 | mFusePath.c_str(), |
| 153 | NULL)) { |
| 154 | PLOG(ERROR) << "Failed to exec"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 155 | } |
| 156 | } else { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 157 | if (execl(kFusePath, kFusePath, |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 158 | "-u", "1023", // AID_MEDIA_RW |
| 159 | "-g", "1023", // AID_MEDIA_RW |
| 160 | "-w", "1023", // AID_MEDIA_RW |
| 161 | "-d", |
| 162 | mRawPath.c_str(), |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 163 | mFusePath.c_str(), |
| 164 | NULL)) { |
| 165 | PLOG(ERROR) << "Failed to exec"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 169 | PLOG(DEBUG) << "FUSE exiting"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 170 | _exit(1); |
| 171 | } |
| 172 | |
| 173 | if (mFusePid == -1) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame^] | 174 | PLOG(ERROR) << getId() << " failed to fork"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 175 | return -errno; |
| 176 | } |
| 177 | |
| 178 | return OK; |
| 179 | } |
| 180 | |
| 181 | status_t PublicVolume::doUnmount() { |
| 182 | if (mFusePid > 0) { |
| 183 | kill(mFusePid, SIGTERM); |
| 184 | TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0)); |
| 185 | mFusePid = 0; |
| 186 | } |
| 187 | |
| 188 | ForceUnmount(mFusePath); |
| 189 | ForceUnmount(mRawPath); |
| 190 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 191 | if (TEMP_FAILURE_RETRY(rmdir(mRawPath.c_str()))) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame^] | 192 | PLOG(ERROR) << getId() << " failed to rmdir mount point " << mRawPath; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 193 | } |
| 194 | if (TEMP_FAILURE_RETRY(rmdir(mFusePath.c_str()))) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame^] | 195 | PLOG(ERROR) << getId() << " failed to rmdir mount point " << mFusePath; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | mFusePath.clear(); |
| 199 | mRawPath.clear(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 200 | |
| 201 | return OK; |
| 202 | } |
| 203 | |
| 204 | status_t PublicVolume::doFormat() { |
| 205 | if (Fat::format(mDevPath.c_str(), 0, true)) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame^] | 206 | LOG(ERROR) << getId() << " failed to format"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 207 | return -errno; |
| 208 | } |
| 209 | return OK; |
| 210 | } |
| 211 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 212 | } // namespace vold |
| 213 | } // namespace android |