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
17 #include "EmulatedVolume.h"
18 #include "Utils.h"
19
20 #include <android-base/stringprintf.h>
21 #include <android-base/logging.h>
22 #include <cutils/fs.h>
23 #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/sysmacros.h>
31 #include <sys/wait.h>
32
33 using android::base::StringPrintf;
34
35 namespace android {
36 namespace vold {
37
38 static const char* kFusePath = "/system/bin/sdcard";
39
EmulatedVolume(const std::string & rawPath)40 EmulatedVolume::EmulatedVolume(const std::string& rawPath) :
41 VolumeBase(Type::kEmulated), mFusePid(0) {
42 setId("emulated");
43 mRawPath = rawPath;
44 mLabel = "emulated";
45 }
46
EmulatedVolume(const std::string & rawPath,dev_t device,const std::string & fsUuid)47 EmulatedVolume::EmulatedVolume(const std::string& rawPath, dev_t device,
48 const std::string& fsUuid) : VolumeBase(Type::kEmulated), mFusePid(0) {
49 setId(StringPrintf("emulated:%u,%u", major(device), minor(device)));
50 mRawPath = rawPath;
51 mLabel = fsUuid;
52 }
53
~EmulatedVolume()54 EmulatedVolume::~EmulatedVolume() {
55 }
56
doMount()57 status_t EmulatedVolume::doMount() {
58 // We could have migrated storage to an adopted private volume, so always
59 // call primary storage "emulated" to avoid media rescans.
60 std::string label = mLabel;
61 if (getMountFlags() & MountFlags::kPrimary) {
62 label = "emulated";
63 }
64
65 mFuseDefault = StringPrintf("/mnt/runtime/default/%s", label.c_str());
66 mFuseRead = StringPrintf("/mnt/runtime/read/%s", label.c_str());
67 mFuseWrite = StringPrintf("/mnt/runtime/write/%s", label.c_str());
68
69 setInternalPath(mRawPath);
70 setPath(StringPrintf("/storage/%s", label.c_str()));
71
72 if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
73 fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
74 fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT)) {
75 PLOG(ERROR) << getId() << " failed to create mount points";
76 return -errno;
77 }
78
79 dev_t before = GetDevice(mFuseWrite);
80
81 if (!(mFusePid = fork())) {
82 if (execl(kFusePath, kFusePath,
83 "-u", "1023", // AID_MEDIA_RW
84 "-g", "1023", // AID_MEDIA_RW
85 "-m",
86 "-w",
87 "-G",
88 mRawPath.c_str(),
89 label.c_str(),
90 NULL)) {
91 PLOG(ERROR) << "Failed to exec";
92 }
93
94 LOG(ERROR) << "FUSE exiting";
95 _exit(1);
96 }
97
98 if (mFusePid == -1) {
99 PLOG(ERROR) << getId() << " failed to fork";
100 return -errno;
101 }
102
103 while (before == GetDevice(mFuseWrite)) {
104 LOG(VERBOSE) << "Waiting for FUSE to spin up...";
105 usleep(50000); // 50ms
106 }
107 /* sdcardfs will have exited already. FUSE will still be running */
108 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, WNOHANG));
109
110 return OK;
111 }
112
doUnmount()113 status_t EmulatedVolume::doUnmount() {
114 // Unmount the storage before we kill the FUSE process. If we kill
115 // the FUSE process first, most file system operations will return
116 // ENOTCONN until the unmount completes. This is an exotic and unusual
117 // error code and might cause broken behaviour in applications.
118 KillProcessesUsingPath(getPath());
119 ForceUnmount(mFuseDefault);
120 ForceUnmount(mFuseRead);
121 ForceUnmount(mFuseWrite);
122
123 if (mFusePid > 0) {
124 kill(mFusePid, SIGTERM);
125 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
126 mFusePid = 0;
127 }
128
129 rmdir(mFuseDefault.c_str());
130 rmdir(mFuseRead.c_str());
131 rmdir(mFuseWrite.c_str());
132
133 mFuseDefault.clear();
134 mFuseRead.clear();
135 mFuseWrite.clear();
136
137 return OK;
138 }
139
140 } // namespace vold
141 } // namespace android
142