blob: e4ed9c14e9c1fd759e7e47852a1df39c69415669 [file] [log] [blame]
Jeff Sharkeydeb24052015-03-02 21:01:40 -08001/*
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 Sharkeydeb24052015-03-02 21:01:40 -080017#include "Fat.h"
18#include "PublicVolume.h"
19#include "Utils.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070020#include "VolumeManager.h"
21#include "ResponseCode.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080022
Dan Albertae9e8902015-03-16 10:35:17 -070023#include <base/stringprintf.h>
Jeff Sharkey36801cc2015-03-13 16:09:20 -070024#include <base/logging.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080025#include <cutils/fs.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080026#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 Albertae9e8902015-03-16 10:35:17 -070035using android::base::StringPrintf;
36
Jeff Sharkeydeb24052015-03-02 21:01:40 -080037namespace android {
38namespace vold {
39
40static const char* kBlkidPath = "/system/bin/blkid";
41static const char* kFusePath = "/system/bin/sdcard";
42
Jeff Sharkey36801cc2015-03-13 16:09:20 -070043static const char* kAsecPath = "/mnt/secure/asec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080044
45PublicVolume::PublicVolume(dev_t device) :
Jeff Sharkey36801cc2015-03-13 16:09:20 -070046 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 Sharkeydeb24052015-03-02 21:01:40 -080050}
51
52PublicVolume::~PublicVolume() {
53 DestroyDeviceNode(mDevPath);
54}
55
56status_t PublicVolume::readMetadata() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070057 mFsType.clear();
58 mFsUuid.clear();
59 mFsLabel.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -080060
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 Sharkey36801cc2015-03-13 16:09:20 -070064 PLOG(ERROR) << "Failed to run " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080065 return -errno;
66 }
67
Jeff Sharkey36801cc2015-03-13 16:09:20 -070068 status_t res = OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080069 char line[1024];
70 char value[128];
71 if (fgets(line, sizeof(line), fp) != nullptr) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070072 LOG(DEBUG) << "blkid identified as " << line;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080073
Jeff Sharkey36801cc2015-03-13 16:09:20 -070074 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 Sharkeydeb24052015-03-02 21:01:40 -080080 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 Sharkey36801cc2015-03-13 16:09:20 -070089 LOG(WARNING) << "blkid failed to identify " << mDevPath;
90 res = -ENODATA;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080091 }
92
93 pclose(fp);
94
Jeff Sharkey36801cc2015-03-13 16:09:20 -070095 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 Sharkeydeb24052015-03-02 21:01:40 -0800106}
107
108status_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 Sharkey36801cc2015-03-13 16:09:20 -0700116 PLOG(WARNING) << "Failed to rename legacy ASEC dir";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800117 }
118 }
119
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700120 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 Sharkeydeb24052015-03-02 21:01:40 -0800125 }
126
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700127 BindMount(securePath, kAsecPath);
128
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800129 return OK;
130}
131
132status_t PublicVolume::doMount() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700133 // TODO: expand to support mounting other filesystems
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800134 if (Fat::check(mDevPath.c_str())) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700135 LOG(ERROR) << "Failed filesystem check; not mounting";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800136 return -EIO;
137 }
138
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700139 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 Sharkeydeb24052015-03-02 21:01:40 -0800153 return -errno;
154 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700155 if (fs_prepare_dir(mFusePath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
156 PLOG(ERROR) << "Failed to create mount point " << mFusePath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800157 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 Sharkey36801cc2015-03-13 16:09:20 -0700162 PLOG(ERROR) << "Failed to mount " << mDevPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800163 return -EIO;
164 }
165
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700166 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 Sharkeydeb24052015-03-02 21:01:40 -0800176 if (!(mFusePid = fork())) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700177 if (getFlags() & Flags::kPrimary) {
178 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800179 "-u", "1023", // AID_MEDIA_RW
180 "-g", "1023", // AID_MEDIA_RW
181 "-d",
182 mRawPath.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700183 mFusePath.c_str(),
184 NULL)) {
185 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800186 }
187 } else {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700188 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800189 "-u", "1023", // AID_MEDIA_RW
190 "-g", "1023", // AID_MEDIA_RW
191 "-w", "1023", // AID_MEDIA_RW
192 "-d",
193 mRawPath.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700194 mFusePath.c_str(),
195 NULL)) {
196 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800197 }
198 }
199
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700200 PLOG(DEBUG) << "FUSE exiting";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800201 _exit(1);
202 }
203
204 if (mFusePid == -1) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700205 PLOG(ERROR) << "Failed to fork";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800206 return -errno;
207 }
208
209 return OK;
210}
211
212status_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 Sharkey36801cc2015-03-13 16:09:20 -0700222 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 Sharkeydeb24052015-03-02 21:01:40 -0800231
232 return OK;
233}
234
235status_t PublicVolume::doFormat() {
236 if (Fat::format(mDevPath.c_str(), 0, true)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700237 LOG(ERROR) << "Failed to format";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800238 return -errno;
239 }
240 return OK;
241}
242
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800243} // namespace vold
244} // namespace android