blob: df91904a7471bc62bd1997884ac19b14df2d7f60 [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",
Jeff Sharkeydeb24052015-03-02 21:01:40 -080087 mRawPath.c_str(),
Jeff Sharkey81f55c62015-07-07 14:37:03 -070088 label.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -070089 NULL)) {
90 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080091 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -070092
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -070093 LOG(ERROR) << "FUSE exiting";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080094 _exit(1);
95 }
96
97 if (mFusePid == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070098 PLOG(ERROR) << getId() << " failed to fork";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080099 return -errno;
100 }
101
Jeff Sharkey66270a22015-06-24 11:49:24 -0700102 while (before == GetDevice(mFuseWrite)) {
103 LOG(VERBOSE) << "Waiting for FUSE to spin up...";
104 usleep(50000); // 50ms
105 }
Daniel Rosenberg1d79d102017-07-11 17:59:55 -0700106 /* sdcardfs will have exited already. FUSE will still be running */
107 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, WNOHANG));
Jeff Sharkey66270a22015-06-24 11:49:24 -0700108
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800109 return OK;
110}
111
112status_t EmulatedVolume::doUnmount() {
Narayan Kamathea243a32016-01-21 12:26:05 +0000113 // Unmount the storage before we kill the FUSE process. If we kill
114 // the FUSE process first, most file system operations will return
115 // ENOTCONN until the unmount completes. This is an exotic and unusual
116 // error code and might cause broken behaviour in applications.
117 KillProcessesUsingPath(getPath());
118 ForceUnmount(mFuseDefault);
119 ForceUnmount(mFuseRead);
120 ForceUnmount(mFuseWrite);
121
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800122 if (mFusePid > 0) {
123 kill(mFusePid, SIGTERM);
124 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
125 mFusePid = 0;
126 }
127
Jeff Sharkey66270a22015-06-24 11:49:24 -0700128 rmdir(mFuseDefault.c_str());
129 rmdir(mFuseRead.c_str());
130 rmdir(mFuseWrite.c_str());
131
132 mFuseDefault.clear();
133 mFuseRead.clear();
134 mFuseWrite.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800135
136 return OK;
137}
138
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800139} // namespace vold
140} // namespace android