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