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 | |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 20 | #include <android-base/stringprintf.h> |
| 21 | #include <android-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> |
Elliott Hughes | 0e08e84 | 2017-05-18 09:08:24 -0700 | [diff] [blame] | 30 | #include <sys/sysmacros.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 31 | #include <sys/wait.h> |
| 32 | |
Dan Albert | ae9e890 | 2015-03-16 10:35:17 -0700 | [diff] [blame] | 33 | using android::base::StringPrintf; |
| 34 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 35 | namespace android { |
| 36 | namespace vold { |
| 37 | |
| 38 | static const char* kFusePath = "/system/bin/sdcard"; |
| 39 | |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 40 | EmulatedVolume::EmulatedVolume(const std::string& rawPath) : |
| 41 | VolumeBase(Type::kEmulated), mFusePid(0) { |
| 42 | setId("emulated"); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 43 | mRawPath = rawPath; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 44 | mLabel = "emulated"; |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | EmulatedVolume::EmulatedVolume(const std::string& rawPath, dev_t device, |
| 48 | const std::string& fsUuid) : VolumeBase(Type::kEmulated), mFusePid(0) { |
| 49 | setId(StringPrintf("emulated:%u,%u", major(device), minor(device))); |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 50 | mRawPath = rawPath; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 51 | mLabel = fsUuid; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | EmulatedVolume::~EmulatedVolume() { |
| 55 | } |
| 56 | |
| 57 | status_t EmulatedVolume::doMount() { |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 58 | // We could have migrated storage to an adopted private volume, so always |
| 59 | // call primary storage "emulated" to avoid media rescans. |
| 60 | std::string label = mLabel; |
| 61 | if (getMountFlags() & MountFlags::kPrimary) { |
| 62 | label = "emulated"; |
| 63 | } |
| 64 | |
Jeff Sharkey | 1bd078f | 2015-08-06 11:40:00 -0700 | [diff] [blame] | 65 | mFuseDefault = StringPrintf("/mnt/runtime/default/%s", label.c_str()); |
| 66 | mFuseRead = StringPrintf("/mnt/runtime/read/%s", label.c_str()); |
| 67 | mFuseWrite = StringPrintf("/mnt/runtime/write/%s", label.c_str()); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 68 | |
| 69 | setInternalPath(mRawPath); |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 70 | setPath(StringPrintf("/storage/%s", label.c_str())); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 71 | |
| 72 | if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) || |
| 73 | fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) || |
| 74 | fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT)) { |
| 75 | PLOG(ERROR) << getId() << " failed to create mount points"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 76 | return -errno; |
| 77 | } |
| 78 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 79 | dev_t before = GetDevice(mFuseWrite); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 80 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 81 | if (!(mFusePid = fork())) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 82 | if (execl(kFusePath, kFusePath, |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 83 | "-u", "1023", // AID_MEDIA_RW |
| 84 | "-g", "1023", // AID_MEDIA_RW |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 85 | "-m", |
| 86 | "-w", |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 87 | mRawPath.c_str(), |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 88 | label.c_str(), |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 89 | NULL)) { |
| 90 | PLOG(ERROR) << "Failed to exec"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 91 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 92 | |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 93 | LOG(ERROR) << "FUSE exiting"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 94 | _exit(1); |
| 95 | } |
| 96 | |
| 97 | if (mFusePid == -1) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 98 | PLOG(ERROR) << getId() << " failed to fork"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 99 | return -errno; |
| 100 | } |
| 101 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 102 | while (before == GetDevice(mFuseWrite)) { |
| 103 | LOG(VERBOSE) << "Waiting for FUSE to spin up..."; |
| 104 | usleep(50000); // 50ms |
| 105 | } |
Daniel Rosenberg | 1d79d10 | 2017-07-11 17:59:55 -0700 | [diff] [blame] | 106 | /* sdcardfs will have exited already. FUSE will still be running */ |
| 107 | TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, WNOHANG)); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 108 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 109 | return OK; |
| 110 | } |
| 111 | |
| 112 | status_t EmulatedVolume::doUnmount() { |
Narayan Kamath | ea243a3 | 2016-01-21 12:26:05 +0000 | [diff] [blame] | 113 | // Unmount the storage before we kill the FUSE process. If we kill |
| 114 | // the FUSE process first, most file system operations will return |
| 115 | // ENOTCONN until the unmount completes. This is an exotic and unusual |
| 116 | // error code and might cause broken behaviour in applications. |
| 117 | KillProcessesUsingPath(getPath()); |
| 118 | ForceUnmount(mFuseDefault); |
| 119 | ForceUnmount(mFuseRead); |
| 120 | ForceUnmount(mFuseWrite); |
| 121 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 122 | if (mFusePid > 0) { |
| 123 | kill(mFusePid, SIGTERM); |
| 124 | TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0)); |
| 125 | mFusePid = 0; |
| 126 | } |
| 127 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 128 | rmdir(mFuseDefault.c_str()); |
| 129 | rmdir(mFuseRead.c_str()); |
| 130 | rmdir(mFuseWrite.c_str()); |
| 131 | |
| 132 | mFuseDefault.clear(); |
| 133 | mFuseRead.clear(); |
| 134 | mFuseWrite.clear(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 135 | |
| 136 | return OK; |
| 137 | } |
| 138 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 139 | } // namespace vold |
| 140 | } // namespace android |