blob: 7098872b3b24fcde13672b5030c1bbeef00b7c5f [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");
42 mFusePath = "/storage/emulated";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080043 mRawPath = rawPath;
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)));
49 mFusePath = StringPrintf("/storage/%s", fsUuid.c_str());
50 mRawPath = rawPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080051}
52
53EmulatedVolume::~EmulatedVolume() {
54}
55
56status_t EmulatedVolume::doMount() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070057 if (fs_prepare_dir(mFusePath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070058 PLOG(ERROR) << getId() << " failed to create mount point " << mFusePath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080059 return -errno;
60 }
61
Jeff Sharkey36801cc2015-03-13 16:09:20 -070062 setPath(mFusePath);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070063 setInternalPath(mRawPath);
Jeff Sharkey36801cc2015-03-13 16:09:20 -070064
Jeff Sharkeydeb24052015-03-02 21:01:40 -080065 if (!(mFusePid = fork())) {
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070066 // TODO: protect when not mounted as visible
Jeff Sharkey36801cc2015-03-13 16:09:20 -070067 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -080068 "-u", "1023", // AID_MEDIA_RW
69 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey36801cc2015-03-13 16:09:20 -070070 "-l",
Jeff Sharkeydeb24052015-03-02 21:01:40 -080071 mRawPath.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -070072 mFusePath.c_str(),
73 NULL)) {
74 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080075 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -070076
77 PLOG(DEBUG) << "FUSE exiting";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080078 _exit(1);
79 }
80
81 if (mFusePid == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070082 PLOG(ERROR) << getId() << " failed to fork";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080083 return -errno;
84 }
85
86 return OK;
87}
88
89status_t EmulatedVolume::doUnmount() {
90 if (mFusePid > 0) {
91 kill(mFusePid, SIGTERM);
92 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
93 mFusePid = 0;
94 }
95
96 ForceUnmount(mFusePath);
Jeff Sharkey36801cc2015-03-13 16:09:20 -070097 ForceUnmount(mRawPath);
Jeff Sharkeydeb24052015-03-02 21:01:40 -080098
Jeff Sharkey36801cc2015-03-13 16:09:20 -070099 if (TEMP_FAILURE_RETRY(rmdir(mFusePath.c_str()))) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700100 PLOG(ERROR) << getId() << " failed to rmdir mount point " << mFusePath;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700101 return -errno;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800102 }
103
104 return OK;
105}
106
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800107} // namespace vold
108} // namespace android