blob: 21b290a96f2b5534f914b9daa7b6d4cefe5ed4da [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 */
108 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, WNOHANG));
Jeff Sharkey66270a22015-06-24 11:49:24 -0700109
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800110 return OK;
111}
112
113status_t EmulatedVolume::doUnmount() {
Narayan Kamathea243a32016-01-21 12:26:05 +0000114 // 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
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800123 if (mFusePid > 0) {
124 kill(mFusePid, SIGTERM);
125 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
126 mFusePid = 0;
127 }
128
Jeff Sharkey66270a22015-06-24 11:49:24 -0700129 rmdir(mFuseDefault.c_str());
130 rmdir(mFuseRead.c_str());
131 rmdir(mFuseWrite.c_str());
132
133 mFuseDefault.clear();
134 mFuseRead.clear();
135 mFuseWrite.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800136
137 return OK;
138}
139
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800140} // namespace vold
141} // namespace android