blob: 78c8fa526ced63f13f4dd580e98e19b0667a041f [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
17#define LOG_TAG "Vold"
18
19#include "Fat.h"
20#include "PublicVolume.h"
21#include "Utils.h"
22
23#include <cutils/fs.h>
24#include <cutils/log.h>
25#include <utils/file.h>
26#include <utils/stringprintf.h>
27#include <private/android_filesystem_config.h>
28
29#include <fcntl.h>
30#include <stdlib.h>
31#include <sys/mount.h>
32#include <sys/stat.h>
33#include <sys/types.h>
34#include <sys/wait.h>
35
36namespace android {
37namespace vold {
38
39static const char* kBlkidPath = "/system/bin/blkid";
40static const char* kFusePath = "/system/bin/sdcard";
41
42static const char* kUserMountPath = "/mnt/user";
43
44PublicVolume::PublicVolume(dev_t device) :
45 VolumeBase(VolumeType::kPublic), mDevice(device), mFusePid(0), mPrimary(false) {
46 mId = StringPrintf("public:%ud:%ud", major(device), minor(device));
47 mDevPath = StringPrintf("/dev/block/vold/%ud:%ud", major(device), minor(device));
48 mRawPath = StringPrintf("/mnt/media_rw/public_raw_%ud:%ud", major(device), minor(device));
49 mFusePath = StringPrintf("/mnt/media_rw/public_fuse_%ud:%ud", major(device), minor(device));
50
51 CreateDeviceNode(mDevPath, device);
52}
53
54PublicVolume::~PublicVolume() {
55 DestroyDeviceNode(mDevPath);
56}
57
58status_t PublicVolume::readMetadata() {
59 mFsUuid = "";
60 mFsLabel = "";
61
62 std::string path(StringPrintf("%s -c /dev/null %s", kBlkidPath, mDevPath.c_str()));
63 FILE* fp = popen(path.c_str(), "r");
64 if (!fp) {
65 ALOGE("Failed to run %s: %s", path.c_str(), strerror(errno));
66 return -errno;
67 }
68
69 char line[1024];
70 char value[128];
71 if (fgets(line, sizeof(line), fp) != nullptr) {
72 ALOGD("blkid identified as %s", line);
73
74 char* start = strstr(line, "UUID=");
75 if (start != nullptr && sscanf(start + 5, "\"%127[^\"]\"", value) == 1) {
76 mFsUuid = value;
77 }
78
79 start = strstr(line, "LABEL=");
80 if (start != nullptr && sscanf(start + 6, "\"%127[^\"]\"", value) == 1) {
81 mFsLabel = value;
82 }
83 } else {
84 ALOGW("blkid failed to identify %s", mDevPath.c_str());
85 return -ENODATA;
86 }
87
88 pclose(fp);
89
90 // TODO: broadcast ident to framework
91 return OK;
92}
93
94status_t PublicVolume::initAsecStage() {
95 std::string legacyPath(mRawPath + "/android_secure");
96 std::string securePath(mRawPath + "/.android_secure");
97
98 // Recover legacy secure path
99 if (!access(legacyPath.c_str(), R_OK | X_OK)
100 && access(securePath.c_str(), R_OK | X_OK)) {
101 if (rename(legacyPath.c_str(), securePath.c_str())) {
102 SLOGE("Failed to rename legacy ASEC dir: %s", strerror(errno));
103 }
104 }
105
106 if (fs_prepare_dir(securePath.c_str(), 0770, AID_MEDIA_RW, AID_MEDIA_RW) != 0) {
107 SLOGW("fs_prepare_dir failed: %s", strerror(errno));
108 return -errno;
109 }
110
111 return OK;
112}
113
114status_t PublicVolume::doMount() {
115 if (Fat::check(mDevPath.c_str())) {
116 SLOGE("Failed filesystem check; not mounting");
117 return -EIO;
118 }
119
120 if (fs_prepare_dir(mRawPath.c_str(), 0770, AID_MEDIA_RW, AID_MEDIA_RW)) {
121 SLOGE("Failed to create mount point %s: %s", mRawPath.c_str(), strerror(errno));
122 return -errno;
123 }
124 if (fs_prepare_dir(mFusePath.c_str(), 0770, AID_MEDIA_RW, AID_MEDIA_RW)) {
125 SLOGE("Failed to create mount point %s: %s", mFusePath.c_str(), strerror(errno));
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)) {
131 SLOGE("Failed to mount %s: %s", mDevPath.c_str(), strerror(errno));
132 return -EIO;
133 }
134
135 if (!(mFusePid = fork())) {
136 if (mPrimary) {
137 if (execl(kFusePath,
138 "-u", "1023", // AID_MEDIA_RW
139 "-g", "1023", // AID_MEDIA_RW
140 "-d",
141 mRawPath.c_str(),
142 mFusePath.c_str())) {
143 SLOGE("Failed to exec: %s", strerror(errno));
144 }
145 } else {
146 if (execl(kFusePath,
147 "-u", "1023", // AID_MEDIA_RW
148 "-g", "1023", // AID_MEDIA_RW
149 "-w", "1023", // AID_MEDIA_RW
150 "-d",
151 mRawPath.c_str(),
152 mFusePath.c_str())) {
153 SLOGE("Failed to exec: %s", strerror(errno));
154 }
155 }
156
157 _exit(1);
158 }
159
160 if (mFusePid == -1) {
161 SLOGE("Failed to fork: %s", strerror(errno));
162 return -errno;
163 }
164
165 return OK;
166}
167
168status_t PublicVolume::doUnmount() {
169 if (mFusePid > 0) {
170 kill(mFusePid, SIGTERM);
171 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
172 mFusePid = 0;
173 }
174
175 ForceUnmount(mFusePath);
176 ForceUnmount(mRawPath);
177
178 TEMP_FAILURE_RETRY(unlink(mRawPath.c_str()));
179 TEMP_FAILURE_RETRY(unlink(mFusePath.c_str()));
180
181 return OK;
182}
183
184status_t PublicVolume::doFormat() {
185 if (Fat::format(mDevPath.c_str(), 0, true)) {
186 SLOGE("Failed to format: %s", strerror(errno));
187 return -errno;
188 }
189 return OK;
190}
191
192status_t PublicVolume::bindUser(userid_t user) {
193 return bindUserInternal(user, true);
194}
195
196status_t PublicVolume::unbindUser(userid_t user) {
197 return bindUserInternal(user, false);
198}
199
200status_t PublicVolume::bindUserInternal(userid_t user, bool bind) {
201 if (mPrimary) {
202 if (user == 0) {
203 std::string path(StringPrintf("%s/%ud/primary", kUserMountPath, user));
204 if (bind) {
205 mountBind(mFusePath, path);
206 } else {
207 unmountBind(path);
208 }
209 } else {
210 // Public volumes are only visible to owner when primary
211 // storage, so we don't mount for secondary users.
212 }
213 } else {
214 std::string path(StringPrintf("%s/%ud/public_%ud:%ud", kUserMountPath, user,
215 major(mDevice), minor(mDevice)));
216 if (bind) {
217 mountBind(mFusePath, path);
218 } else {
219 unmountBind(path);
220 }
221
222 if (user != 0) {
223 // To prevent information leakage between users, only owner
224 // has access to the Android directory
225 path += "/Android";
226 if (bind) {
227 if (::mount("tmpfs", path.c_str(), "tmpfs", MS_NOSUID, "mode=0000")) {
228 SLOGE("Failed to protect secondary path %s: %s",
229 path.c_str(), strerror(errno));
230 return -errno;
231 }
232 } else {
233 ForceUnmount(path);
234 }
235 }
236 }
237
238 return OK;
239}
240
241void PublicVolume::setPrimary(bool primary) {
242 if (getState() != VolumeState::kUnmounted) {
243 SLOGE("Primary state change requires %s to be unmounted", getId().c_str());
244 return;
245 }
246
247 mPrimary = primary;
248}
249
250} // namespace vold
251} // namespace android