blob: 5dcab7259e0931ca0f9c6f2b0e4a7d9ad706f90f [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 Sharkey36801cc2015-03-13 16:09:20 -070039EmulatedVolume::EmulatedVolume(const std::string& rawPath,
40 const std::string& fsUuid) : VolumeBase(Type::kEmulated), mFusePid(0) {
41 if (fsUuid.empty()) {
42 setId("emulated");
43 } else {
44 setId(StringPrintf("emulated:%s", fsUuid.c_str()));
45 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -080046
Jeff Sharkeydeb24052015-03-02 21:01:40 -080047 mRawPath = rawPath;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070048 mFusePath = StringPrintf("/storage/%s", getId().c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -080049}
50
51EmulatedVolume::~EmulatedVolume() {
52}
53
54status_t EmulatedVolume::doMount() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070055 if (fs_prepare_dir(mFusePath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070056 PLOG(ERROR) << getId() << " failed to create mount point " << mFusePath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080057 return -errno;
58 }
59
Jeff Sharkey36801cc2015-03-13 16:09:20 -070060 setPath(mFusePath);
61
Jeff Sharkeydeb24052015-03-02 21:01:40 -080062 if (!(mFusePid = fork())) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070063 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -080064 "-u", "1023", // AID_MEDIA_RW
65 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey36801cc2015-03-13 16:09:20 -070066 "-l",
Jeff Sharkeydeb24052015-03-02 21:01:40 -080067 mRawPath.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -070068 mFusePath.c_str(),
69 NULL)) {
70 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080071 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -070072
73 PLOG(DEBUG) << "FUSE exiting";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080074 _exit(1);
75 }
76
77 if (mFusePid == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070078 PLOG(ERROR) << getId() << " failed to fork";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080079 return -errno;
80 }
81
82 return OK;
83}
84
85status_t EmulatedVolume::doUnmount() {
86 if (mFusePid > 0) {
87 kill(mFusePid, SIGTERM);
88 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
89 mFusePid = 0;
90 }
91
92 ForceUnmount(mFusePath);
Jeff Sharkey36801cc2015-03-13 16:09:20 -070093 ForceUnmount(mRawPath);
Jeff Sharkeydeb24052015-03-02 21:01:40 -080094
Jeff Sharkey36801cc2015-03-13 16:09:20 -070095 if (TEMP_FAILURE_RETRY(rmdir(mFusePath.c_str()))) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070096 PLOG(ERROR) << getId() << " failed to rmdir mount point " << mFusePath;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070097 return -errno;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080098 }
99
100 return OK;
101}
102
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800103} // namespace vold
104} // namespace android