blob: 6fa66d50f7eee70c29fb34dd0def20fc3df0cf89 [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>
Elliott Hughes0e08e842017-05-18 09:08:24 -070030#include <sys/sysmacros.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080031#include <sys/wait.h>
32
Dan Albertae9e8902015-03-16 10:35:17 -070033using android::base::StringPrintf;
34
Jeff Sharkeydeb24052015-03-02 21:01:40 -080035namespace android {
36namespace vold {
37
38static const char* kFusePath = "/system/bin/sdcard";
39
Jeff Sharkey3161fb32015-04-12 16:03:33 -070040EmulatedVolume::EmulatedVolume(const std::string& rawPath) :
41 VolumeBase(Type::kEmulated), mFusePid(0) {
42 setId("emulated");
Jeff Sharkeydeb24052015-03-02 21:01:40 -080043 mRawPath = rawPath;
Jeff Sharkey66270a22015-06-24 11:49:24 -070044 mLabel = "emulated";
Jeff Sharkey3161fb32015-04-12 16:03:33 -070045}
46
47EmulatedVolume::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)));
Jeff Sharkey3161fb32015-04-12 16:03:33 -070050 mRawPath = rawPath;
Jeff Sharkey66270a22015-06-24 11:49:24 -070051 mLabel = fsUuid;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080052}
53
54EmulatedVolume::~EmulatedVolume() {
55}
56
57status_t EmulatedVolume::doMount() {
Jeff Sharkey81f55c62015-07-07 14:37:03 -070058 // 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
Jeff Sharkey1bd078f2015-08-06 11:40:00 -070065 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());
Jeff Sharkey66270a22015-06-24 11:49:24 -070068
69 setInternalPath(mRawPath);
Jeff Sharkey81f55c62015-07-07 14:37:03 -070070 setPath(StringPrintf("/storage/%s", label.c_str()));
Jeff Sharkey66270a22015-06-24 11:49:24 -070071
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";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080076 return -errno;
77 }
78
Jeff Sharkey66270a22015-06-24 11:49:24 -070079 dev_t before = GetDevice(mFuseWrite);
Jeff Sharkey36801cc2015-03-13 16:09:20 -070080
Jeff Sharkeydeb24052015-03-02 21:01:40 -080081 if (!(mFusePid = fork())) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070082 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -080083 "-u", "1023", // AID_MEDIA_RW
84 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey66270a22015-06-24 11:49:24 -070085 "-m",
86 "-w",
Rom Lemarchand958c2162017-09-15 18:48:01 +000087 "-G",
Jeff Sharkeydeb24052015-03-02 21:01:40 -080088 mRawPath.c_str(),
Jeff Sharkey81f55c62015-07-07 14:37:03 -070089 label.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -070090 NULL)) {
91 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080092 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -070093
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -070094 LOG(ERROR) << "FUSE exiting";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080095 _exit(1);
96 }
97
98 if (mFusePid == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070099 PLOG(ERROR) << getId() << " failed to fork";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800100 return -errno;
101 }
102
Jeff Sharkey66270a22015-06-24 11:49:24 -0700103 while (before == GetDevice(mFuseWrite)) {
104 LOG(VERBOSE) << "Waiting for FUSE to spin up...";
105 usleep(50000); // 50ms
106 }
Daniel Rosenberg1d79d102017-07-11 17:59:55 -0700107 /* sdcardfs will have exited already. FUSE will still be running */
Daniel Rosenberg8b9a5b32018-03-12 15:47:23 -0700108 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
109 mFusePid = 0;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700110
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800111 return OK;
112}
113
114status_t EmulatedVolume::doUnmount() {
Narayan Kamathea243a32016-01-21 12:26:05 +0000115 // Unmount the storage before we kill the FUSE process. If we kill
116 // the FUSE process first, most file system operations will return
117 // ENOTCONN until the unmount completes. This is an exotic and unusual
118 // error code and might cause broken behaviour in applications.
119 KillProcessesUsingPath(getPath());
120 ForceUnmount(mFuseDefault);
121 ForceUnmount(mFuseRead);
122 ForceUnmount(mFuseWrite);
123
Jeff Sharkey66270a22015-06-24 11:49:24 -0700124 rmdir(mFuseDefault.c_str());
125 rmdir(mFuseRead.c_str());
126 rmdir(mFuseWrite.c_str());
127
128 mFuseDefault.clear();
129 mFuseRead.clear();
130 mFuseWrite.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800131
132 return OK;
133}
134
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800135} // namespace vold
136} // namespace android