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 | |
| 17 | #define LOG_TAG "Vold" |
| 18 | |
| 19 | #include "EmulatedVolume.h" |
| 20 | #include "Utils.h" |
| 21 | |
Dan Albert | ae9e890 | 2015-03-16 10:35:17 -0700 | [diff] [blame] | 22 | #include <base/stringprintf.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 23 | #include <cutils/fs.h> |
| 24 | #include <cutils/log.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 25 | #include <private/android_filesystem_config.h> |
| 26 | |
| 27 | #include <fcntl.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <sys/mount.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/wait.h> |
| 33 | |
Dan Albert | ae9e890 | 2015-03-16 10:35:17 -0700 | [diff] [blame] | 34 | using android::base::StringPrintf; |
| 35 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 36 | namespace android { |
| 37 | namespace vold { |
| 38 | |
| 39 | static const char* kFusePath = "/system/bin/sdcard"; |
| 40 | |
| 41 | static const char* kUserMountPath = "/mnt/user"; |
| 42 | |
| 43 | EmulatedVolume::EmulatedVolume(const std::string& rawPath, const std::string& nickname) : |
| 44 | VolumeBase(VolumeType::kEmulated), mFusePid(0), mPrimary(false) { |
| 45 | mRawPath = rawPath; |
| 46 | mFusePath = StringPrintf("/mnt/media_rw/emulated_fuse_%s", nickname.c_str()); |
| 47 | } |
| 48 | |
| 49 | EmulatedVolume::~EmulatedVolume() { |
| 50 | } |
| 51 | |
| 52 | status_t EmulatedVolume::doMount() { |
| 53 | if (fs_prepare_dir(mFusePath.c_str(), 0770, AID_MEDIA_RW, AID_MEDIA_RW)) { |
| 54 | SLOGE("Failed to create mount point %s: %s", mFusePath.c_str(), strerror(errno)); |
| 55 | return -errno; |
| 56 | } |
| 57 | |
| 58 | if (!(mFusePid = fork())) { |
| 59 | if (execl(kFusePath, |
| 60 | "-u", "1023", // AID_MEDIA_RW |
| 61 | "-g", "1023", // AID_MEDIA_RW |
| 62 | "-d", |
| 63 | mRawPath.c_str(), |
| 64 | mFusePath.c_str())) { |
| 65 | SLOGE("Failed to exec: %s", strerror(errno)); |
| 66 | } |
| 67 | _exit(1); |
| 68 | } |
| 69 | |
| 70 | if (mFusePid == -1) { |
| 71 | SLOGE("Failed to fork: %s", strerror(errno)); |
| 72 | return -errno; |
| 73 | } |
| 74 | |
| 75 | return OK; |
| 76 | } |
| 77 | |
| 78 | status_t EmulatedVolume::doUnmount() { |
| 79 | if (mFusePid > 0) { |
| 80 | kill(mFusePid, SIGTERM); |
| 81 | TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0)); |
| 82 | mFusePid = 0; |
| 83 | } |
| 84 | |
| 85 | ForceUnmount(mFusePath); |
| 86 | |
| 87 | TEMP_FAILURE_RETRY(unlink(mFusePath.c_str())); |
| 88 | |
| 89 | return OK; |
| 90 | } |
| 91 | |
| 92 | status_t EmulatedVolume::doFormat() { |
| 93 | return -ENOTSUP; |
| 94 | } |
| 95 | |
| 96 | status_t EmulatedVolume::bindUser(userid_t user) { |
| 97 | return bindUserInternal(user, true); |
| 98 | } |
| 99 | |
| 100 | status_t EmulatedVolume::unbindUser(userid_t user) { |
| 101 | return bindUserInternal(user, false); |
| 102 | } |
| 103 | |
| 104 | status_t EmulatedVolume::bindUserInternal(userid_t user, bool bind) { |
| 105 | if (!mPrimary) { |
| 106 | // Emulated volumes are only bound when primary |
| 107 | return OK; |
| 108 | } |
| 109 | |
| 110 | std::string fromPath(StringPrintf("%s/%ud", mFusePath.c_str(), user)); |
| 111 | std::string toPath(StringPrintf("%s/%ud/primary", kUserMountPath, user)); |
| 112 | |
| 113 | if (bind) { |
| 114 | mountBind(fromPath, toPath); |
| 115 | } else { |
| 116 | unmountBind(toPath); |
| 117 | } |
| 118 | |
| 119 | return OK; |
| 120 | } |
| 121 | |
| 122 | void EmulatedVolume::setPrimary(bool primary) { |
| 123 | if (getState() != VolumeState::kUnmounted) { |
| 124 | SLOGE("Primary state change requires %s to be unmounted", getId().c_str()); |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | mPrimary = primary; |
| 129 | } |
| 130 | |
| 131 | } // namespace vold |
| 132 | } // namespace android |