blob: f3bd905af7fd16f61b8f91456ddcb0bc199e1ee5 [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
Jeff Sharkeydeb24052015-03-02 21:01:40 -080040static const char* kFusePath = "/system/bin/sdcard";
41
Jeff Sharkey36801cc2015-03-13 16:09:20 -070042static const char* kAsecPath = "/mnt/secure/asec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080043
44PublicVolume::PublicVolume(dev_t device) :
Jeff Sharkey36801cc2015-03-13 16:09:20 -070045 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 Sharkeydeb24052015-03-02 21:01:40 -080048}
49
50PublicVolume::~PublicVolume() {
Jeff Sharkeydeb24052015-03-02 21:01:40 -080051}
52
53status_t PublicVolume::readMetadata() {
Jeff Sharkey9c484982015-03-31 10:35:33 -070054 status_t res = ReadMetadata(mDevPath, mFsType, mFsUuid, mFsLabel);
Jeff Sharkeydeb24052015-03-02 21:01:40 -080055
Jeff Sharkey36801cc2015-03-13 16:09:20 -070056 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 Sharkeydeb24052015-03-02 21:01:40 -080067}
68
69status_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 Sharkey9c484982015-03-31 10:35:33 -070077 PLOG(WARNING) << getId() << " failed to rename legacy ASEC dir";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080078 }
79 }
80
Jeff Sharkey36801cc2015-03-13 16:09:20 -070081 if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) {
82 if (errno != EEXIST) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070083 PLOG(WARNING) << getId() << " creating ASEC stage failed";
Jeff Sharkey36801cc2015-03-13 16:09:20 -070084 return -errno;
85 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -080086 }
87
Jeff Sharkey36801cc2015-03-13 16:09:20 -070088 BindMount(securePath, kAsecPath);
89
Jeff Sharkeydeb24052015-03-02 21:01:40 -080090 return OK;
91}
92
Jeff Sharkey9c484982015-03-31 10:35:33 -070093status_t PublicVolume::doCreate() {
94 return CreateDeviceNode(mDevPath, mDevice);
95}
96
97status_t PublicVolume::doDestroy() {
98 return DestroyDeviceNode(mDevPath);
99}
100
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800101status_t PublicVolume::doMount() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700102 // TODO: expand to support mounting other filesystems
Jeff Sharkey9c484982015-03-31 10:35:33 -0700103 readMetadata();
104
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800105 if (Fat::check(mDevPath.c_str())) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700106 LOG(ERROR) << getId() << " failed filesystem check";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800107 return -EIO;
108 }
109
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700110 // 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 Sharkey9c484982015-03-31 10:35:33 -0700121 PLOG(ERROR) << getId() << " failed to create mount point " << mRawPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800122 return -errno;
123 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700124 if (fs_prepare_dir(mFusePath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700125 PLOG(ERROR) << getId() << " failed to create mount point " << mFusePath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800126 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 Sharkey9c484982015-03-31 10:35:33 -0700131 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800132 return -EIO;
133 }
134
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700135 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 Sharkeydeb24052015-03-02 21:01:40 -0800145 if (!(mFusePid = fork())) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700146 if (getFlags() & Flags::kPrimary) {
147 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800148 "-u", "1023", // AID_MEDIA_RW
149 "-g", "1023", // AID_MEDIA_RW
150 "-d",
151 mRawPath.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700152 mFusePath.c_str(),
153 NULL)) {
154 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800155 }
156 } else {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700157 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800158 "-u", "1023", // AID_MEDIA_RW
159 "-g", "1023", // AID_MEDIA_RW
160 "-w", "1023", // AID_MEDIA_RW
161 "-d",
162 mRawPath.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700163 mFusePath.c_str(),
164 NULL)) {
165 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800166 }
167 }
168
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700169 PLOG(DEBUG) << "FUSE exiting";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800170 _exit(1);
171 }
172
173 if (mFusePid == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700174 PLOG(ERROR) << getId() << " failed to fork";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800175 return -errno;
176 }
177
178 return OK;
179}
180
181status_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 Sharkey36801cc2015-03-13 16:09:20 -0700191 if (TEMP_FAILURE_RETRY(rmdir(mRawPath.c_str()))) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700192 PLOG(ERROR) << getId() << " failed to rmdir mount point " << mRawPath;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700193 }
194 if (TEMP_FAILURE_RETRY(rmdir(mFusePath.c_str()))) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700195 PLOG(ERROR) << getId() << " failed to rmdir mount point " << mFusePath;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700196 }
197
198 mFusePath.clear();
199 mRawPath.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800200
201 return OK;
202}
203
204status_t PublicVolume::doFormat() {
205 if (Fat::format(mDevPath.c_str(), 0, true)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700206 LOG(ERROR) << getId() << " failed to format";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800207 return -errno;
208 }
209 return OK;
210}
211
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800212} // namespace vold
213} // namespace android