blob: d8d9198bd56d32ecc29aee14a8678d46b2ab4ad1 [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
Dan Albertae9e8902015-03-16 10:35:17 -070020#include <base/stringprintf.h>
Jeff Sharkey36801cc2015-03-13 16:09:20 -070021#include <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 Sharkey66270a22015-06-24 11:49:24 -070057 mFuseDefault = StringPrintf("/mnt/runtime_default/%s", mLabel.c_str());
58 mFuseRead = StringPrintf("/mnt/runtime_read/%s", mLabel.c_str());
59 mFuseWrite = StringPrintf("/mnt/runtime_write/%s", mLabel.c_str());
60
61 setInternalPath(mRawPath);
62 setPath(StringPrintf("/storage/%s", mLabel.c_str()));
63
64 if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
65 fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
66 fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT)) {
67 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080068 return -errno;
69 }
70
Jeff Sharkey66270a22015-06-24 11:49:24 -070071 dev_t before = GetDevice(mFuseWrite);
Jeff Sharkey36801cc2015-03-13 16:09:20 -070072
Jeff Sharkeydeb24052015-03-02 21:01:40 -080073 if (!(mFusePid = fork())) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070074 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -080075 "-u", "1023", // AID_MEDIA_RW
76 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey66270a22015-06-24 11:49:24 -070077 "-m",
78 "-w",
Jeff Sharkeydeb24052015-03-02 21:01:40 -080079 mRawPath.c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -070080 mLabel.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -070081 NULL)) {
82 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080083 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -070084
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -070085 LOG(ERROR) << "FUSE exiting";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080086 _exit(1);
87 }
88
89 if (mFusePid == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070090 PLOG(ERROR) << getId() << " failed to fork";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080091 return -errno;
92 }
93
Jeff Sharkey66270a22015-06-24 11:49:24 -070094 while (before == GetDevice(mFuseWrite)) {
95 LOG(VERBOSE) << "Waiting for FUSE to spin up...";
96 usleep(50000); // 50ms
97 }
98
Jeff Sharkeydeb24052015-03-02 21:01:40 -080099 return OK;
100}
101
102status_t EmulatedVolume::doUnmount() {
103 if (mFusePid > 0) {
104 kill(mFusePid, SIGTERM);
105 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
106 mFusePid = 0;
107 }
108
Jeff Sharkey66270a22015-06-24 11:49:24 -0700109 ForceUnmount(mFuseDefault);
110 ForceUnmount(mFuseRead);
111 ForceUnmount(mFuseWrite);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800112
Jeff Sharkey66270a22015-06-24 11:49:24 -0700113 rmdir(mFuseDefault.c_str());
114 rmdir(mFuseRead.c_str());
115 rmdir(mFuseWrite.c_str());
116
117 mFuseDefault.clear();
118 mFuseRead.clear();
119 mFuseWrite.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800120
121 return OK;
122}
123
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800124} // namespace vold
125} // namespace android