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"); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 42 | mRawPath = rawPath; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 43 | mLabel = "emulated"; |
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))); |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 49 | mRawPath = rawPath; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 50 | mLabel = fsUuid; |
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 | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 57 | // We could have migrated storage to an adopted private volume, so always |
| 58 | // call primary storage "emulated" to avoid media rescans. |
| 59 | std::string label = mLabel; |
| 60 | if (getMountFlags() & MountFlags::kPrimary) { |
| 61 | label = "emulated"; |
| 62 | } |
| 63 | |
Jeff Sharkey | 1bd078f | 2015-08-06 11:40:00 -0700 | [diff] [blame] | 64 | mFuseDefault = StringPrintf("/mnt/runtime/default/%s", label.c_str()); |
| 65 | mFuseRead = StringPrintf("/mnt/runtime/read/%s", label.c_str()); |
| 66 | mFuseWrite = StringPrintf("/mnt/runtime/write/%s", label.c_str()); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 67 | |
| 68 | setInternalPath(mRawPath); |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 69 | setPath(StringPrintf("/storage/%s", label.c_str())); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 70 | |
| 71 | if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) || |
| 72 | fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) || |
| 73 | fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT)) { |
| 74 | PLOG(ERROR) << getId() << " failed to create mount points"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 75 | return -errno; |
| 76 | } |
| 77 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 78 | dev_t before = GetDevice(mFuseWrite); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 79 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 80 | if (!(mFusePid = fork())) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 81 | if (execl(kFusePath, kFusePath, |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 82 | "-u", "1023", // AID_MEDIA_RW |
| 83 | "-g", "1023", // AID_MEDIA_RW |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 84 | "-m", |
| 85 | "-w", |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 86 | mRawPath.c_str(), |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 87 | label.c_str(), |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 88 | NULL)) { |
| 89 | PLOG(ERROR) << "Failed to exec"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 90 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 91 | |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 92 | LOG(ERROR) << "FUSE exiting"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 93 | _exit(1); |
| 94 | } |
| 95 | |
| 96 | if (mFusePid == -1) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 97 | PLOG(ERROR) << getId() << " failed to fork"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 98 | return -errno; |
| 99 | } |
| 100 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 101 | while (before == GetDevice(mFuseWrite)) { |
| 102 | LOG(VERBOSE) << "Waiting for FUSE to spin up..."; |
| 103 | usleep(50000); // 50ms |
| 104 | } |
| 105 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 106 | return OK; |
| 107 | } |
| 108 | |
| 109 | status_t EmulatedVolume::doUnmount() { |
| 110 | if (mFusePid > 0) { |
| 111 | kill(mFusePid, SIGTERM); |
| 112 | TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0)); |
| 113 | mFusePid = 0; |
| 114 | } |
| 115 | |
Jeff Sharkey | 89f74fb | 2015-10-21 12:16:12 -0700 | [diff] [blame] | 116 | KillProcessesUsingPath(getPath()); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 117 | ForceUnmount(mFuseDefault); |
| 118 | ForceUnmount(mFuseRead); |
| 119 | ForceUnmount(mFuseWrite); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 120 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 121 | rmdir(mFuseDefault.c_str()); |
| 122 | rmdir(mFuseRead.c_str()); |
| 123 | rmdir(mFuseWrite.c_str()); |
| 124 | |
| 125 | mFuseDefault.clear(); |
| 126 | mFuseRead.clear(); |
| 127 | mFuseWrite.clear(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 128 | |
| 129 | return OK; |
| 130 | } |
| 131 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 132 | } // namespace vold |
| 133 | } // namespace android |