Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 1 | /* |
| 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 Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 17 | #include "EmulatedVolume.h" |
| 18 | #include "Utils.h" |
| 19 | |
Dan Albert | ae9e890 | 2015-03-16 10:35:17 -0700 | [diff] [blame] | 20 | #include <base/stringprintf.h> |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 21 | #include <base/logging.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 22 | #include <cutils/fs.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 23 | #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 Albert | ae9e890 | 2015-03-16 10:35:17 -0700 | [diff] [blame] | 32 | using android::base::StringPrintf; |
| 33 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 34 | namespace android { |
| 35 | namespace vold { |
| 36 | |
| 37 | static const char* kFusePath = "/system/bin/sdcard"; |
| 38 | |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 39 | EmulatedVolume::EmulatedVolume(const std::string& rawPath) : |
| 40 | VolumeBase(Type::kEmulated), mFusePid(0) { |
| 41 | setId("emulated"); |
| 42 | mFusePath = "/storage/emulated"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 43 | mRawPath = rawPath; |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | EmulatedVolume::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 Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | EmulatedVolume::~EmulatedVolume() { |
| 54 | } |
| 55 | |
| 56 | status_t EmulatedVolume::doMount() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 57 | if (fs_prepare_dir(mFusePath.c_str(), 0700, AID_ROOT, AID_ROOT)) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 58 | PLOG(ERROR) << getId() << " failed to create mount point " << mFusePath; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 59 | return -errno; |
| 60 | } |
| 61 | |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 62 | setInternalPath(mRawPath); |
Jeff Sharkey | 8d15cb9 | 2015-05-13 12:36:48 -0700 | [diff] [blame] | 63 | setPath(mFusePath); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 64 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 65 | if (!(mFusePid = fork())) { |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 66 | // TODO: protect when not mounted as visible |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 67 | if (execl(kFusePath, kFusePath, |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 68 | "-u", "1023", // AID_MEDIA_RW |
| 69 | "-g", "1023", // AID_MEDIA_RW |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 70 | "-l", |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 71 | mRawPath.c_str(), |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 72 | mFusePath.c_str(), |
| 73 | NULL)) { |
| 74 | PLOG(ERROR) << "Failed to exec"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 75 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 76 | |
| 77 | PLOG(DEBUG) << "FUSE exiting"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 78 | _exit(1); |
| 79 | } |
| 80 | |
| 81 | if (mFusePid == -1) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 82 | PLOG(ERROR) << getId() << " failed to fork"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 83 | return -errno; |
| 84 | } |
| 85 | |
| 86 | return OK; |
| 87 | } |
| 88 | |
| 89 | status_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 Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 97 | ForceUnmount(mRawPath); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 98 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 99 | if (TEMP_FAILURE_RETRY(rmdir(mFusePath.c_str()))) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 100 | PLOG(ERROR) << getId() << " failed to rmdir mount point " << mFusePath; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 101 | return -errno; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | return OK; |
| 105 | } |
| 106 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 107 | } // namespace vold |
| 108 | } // namespace android |