blob: 581c32275f6c04a11a8d2767f5a68b90b8f9a1bd [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 "EmulatedVolume.h"
18#include "Utils.h"
19
Elliott Hughes7e128fb2015-12-04 15:50:53 -080020#include <android-base/stringprintf.h>
21#include <android-base/logging.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080022#include <cutils/fs.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080023#include <private/android_filesystem_config.h>
24
25#include <fcntl.h>
26#include <stdlib.h>
27#include <sys/mount.h>
28#include <sys/stat.h>
29#include <sys/types.h>
30#include <sys/wait.h>
31
Dan Albertae9e8902015-03-16 10:35:17 -070032using android::base::StringPrintf;
33
Jeff Sharkeydeb24052015-03-02 21:01:40 -080034namespace android {
35namespace vold {
36
37static const char* kFusePath = "/system/bin/sdcard";
38
Jeff Sharkey3161fb32015-04-12 16:03:33 -070039EmulatedVolume::EmulatedVolume(const std::string& rawPath) :
40 VolumeBase(Type::kEmulated), mFusePid(0) {
41 setId("emulated");
Jeff Sharkeydeb24052015-03-02 21:01:40 -080042 mRawPath = rawPath;
Jeff Sharkey66270a22015-06-24 11:49:24 -070043 mLabel = "emulated";
Jeff Sharkey3161fb32015-04-12 16:03:33 -070044}
45
46EmulatedVolume::EmulatedVolume(const std::string& rawPath, dev_t device,
47 const std::string& fsUuid) : VolumeBase(Type::kEmulated), mFusePid(0) {
48 setId(StringPrintf("emulated:%u,%u", major(device), minor(device)));
Jeff Sharkey3161fb32015-04-12 16:03:33 -070049 mRawPath = rawPath;
Jeff Sharkey66270a22015-06-24 11:49:24 -070050 mLabel = fsUuid;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080051}
52
53EmulatedVolume::~EmulatedVolume() {
54}
55
56status_t EmulatedVolume::doMount() {
Jeff Sharkey81f55c62015-07-07 14:37:03 -070057 // We could have migrated storage to an adopted private volume, so always
58 // call primary storage "emulated" to avoid media rescans.
59 std::string label = mLabel;
60 if (getMountFlags() & MountFlags::kPrimary) {
61 label = "emulated";
62 }
63
Jeff Sharkey1bd078f2015-08-06 11:40:00 -070064 mFuseDefault = StringPrintf("/mnt/runtime/default/%s", label.c_str());
65 mFuseRead = StringPrintf("/mnt/runtime/read/%s", label.c_str());
66 mFuseWrite = StringPrintf("/mnt/runtime/write/%s", label.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -070067
68 setInternalPath(mRawPath);
Jeff Sharkey81f55c62015-07-07 14:37:03 -070069 setPath(StringPrintf("/storage/%s", label.c_str()));
Jeff Sharkey66270a22015-06-24 11:49:24 -070070
71 if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
72 fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
73 fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT)) {
74 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080075 return -errno;
76 }
77
Jeff Sharkey66270a22015-06-24 11:49:24 -070078 dev_t before = GetDevice(mFuseWrite);
Jeff Sharkey36801cc2015-03-13 16:09:20 -070079
Jeff Sharkeydeb24052015-03-02 21:01:40 -080080 if (!(mFusePid = fork())) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070081 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -080082 "-u", "1023", // AID_MEDIA_RW
83 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey66270a22015-06-24 11:49:24 -070084 "-m",
85 "-w",
Jeff Sharkeydeb24052015-03-02 21:01:40 -080086 mRawPath.c_str(),
Jeff Sharkey81f55c62015-07-07 14:37:03 -070087 label.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -070088 NULL)) {
89 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080090 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -070091
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -070092 LOG(ERROR) << "FUSE exiting";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080093 _exit(1);
94 }
95
96 if (mFusePid == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070097 PLOG(ERROR) << getId() << " failed to fork";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080098 return -errno;
99 }
100
Jeff Sharkey66270a22015-06-24 11:49:24 -0700101 while (before == GetDevice(mFuseWrite)) {
102 LOG(VERBOSE) << "Waiting for FUSE to spin up...";
103 usleep(50000); // 50ms
104 }
105
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800106 return OK;
107}
108
109status_t EmulatedVolume::doUnmount() {
Narayan Kamathea243a32016-01-21 12:26:05 +0000110 // Unmount the storage before we kill the FUSE process. If we kill
111 // the FUSE process first, most file system operations will return
112 // ENOTCONN until the unmount completes. This is an exotic and unusual
113 // error code and might cause broken behaviour in applications.
114 KillProcessesUsingPath(getPath());
115 ForceUnmount(mFuseDefault);
116 ForceUnmount(mFuseRead);
117 ForceUnmount(mFuseWrite);
118
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800119 if (mFusePid > 0) {
120 kill(mFusePid, SIGTERM);
121 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
122 mFusePid = 0;
123 }
124
Jeff Sharkey66270a22015-06-24 11:49:24 -0700125 rmdir(mFuseDefault.c_str());
126 rmdir(mFuseRead.c_str());
127 rmdir(mFuseWrite.c_str());
128
129 mFuseDefault.clear();
130 mFuseRead.clear();
131 mFuseWrite.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800132
133 return OK;
134}
135
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800136} // namespace vold
137} // namespace android