EmulatedVolume.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #include "EmulatedVolume.h"
  17. #include "Utils.h"
  18. #include "VolumeManager.h"
  19. #include <android-base/logging.h>
  20. #include <android-base/stringprintf.h>
  21. #include <cutils/fs.h>
  22. #include <private/android_filesystem_config.h>
  23. #include <utils/Timers.h>
  24. #include <fcntl.h>
  25. #include <stdlib.h>
  26. #include <sys/mount.h>
  27. #include <sys/stat.h>
  28. #include <sys/sysmacros.h>
  29. #include <sys/types.h>
  30. #include <sys/wait.h>
  31. using android::base::StringPrintf;
  32. namespace android {
  33. namespace vold {
  34. static const char* kFusePath = "/system/bin/sdcard";
  35. EmulatedVolume::EmulatedVolume(const std::string& rawPath)
  36. : VolumeBase(Type::kEmulated), mFusePid(0) {
  37. setId("emulated");
  38. mRawPath = rawPath;
  39. mLabel = "emulated";
  40. }
  41. EmulatedVolume::EmulatedVolume(const std::string& rawPath, dev_t device, const std::string& fsUuid)
  42. : VolumeBase(Type::kEmulated), mFusePid(0) {
  43. setId(StringPrintf("emulated:%u,%u", major(device), minor(device)));
  44. mRawPath = rawPath;
  45. mLabel = fsUuid;
  46. }
  47. EmulatedVolume::~EmulatedVolume() {}
  48. status_t EmulatedVolume::doMount() {
  49. // We could have migrated storage to an adopted private volume, so always
  50. // call primary storage "emulated" to avoid media rescans.
  51. std::string label = mLabel;
  52. if (getMountFlags() & MountFlags::kPrimary) {
  53. label = "emulated";
  54. }
  55. mFuseDefault = StringPrintf("/mnt/runtime/default/%s", label.c_str());
  56. mFuseRead = StringPrintf("/mnt/runtime/read/%s", label.c_str());
  57. mFuseWrite = StringPrintf("/mnt/runtime/write/%s", label.c_str());
  58. mFuseFull = StringPrintf("/mnt/runtime/full/%s", label.c_str());
  59. setInternalPath(mRawPath);
  60. setPath(StringPrintf("/storage/%s", label.c_str()));
  61. if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
  62. fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
  63. fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT) ||
  64. fs_prepare_dir(mFuseFull.c_str(), 0700, AID_ROOT, AID_ROOT)) {
  65. PLOG(ERROR) << getId() << " failed to create mount points";
  66. return -errno;
  67. }
  68. dev_t before = GetDevice(mFuseFull);
  69. if (!(mFusePid = fork())) {
  70. // clang-format off
  71. if (execl(kFusePath, kFusePath,
  72. "-u", "1023", // AID_MEDIA_RW
  73. "-g", "1023", // AID_MEDIA_RW
  74. "-m",
  75. "-w",
  76. "-G",
  77. "-i",
  78. "-o",
  79. mRawPath.c_str(),
  80. label.c_str(),
  81. NULL)) {
  82. // clang-format on
  83. PLOG(ERROR) << "Failed to exec";
  84. }
  85. LOG(ERROR) << "FUSE exiting";
  86. _exit(1);
  87. }
  88. if (mFusePid == -1) {
  89. PLOG(ERROR) << getId() << " failed to fork";
  90. return -errno;
  91. }
  92. nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
  93. while (before == GetDevice(mFuseFull)) {
  94. LOG(DEBUG) << "Waiting for FUSE to spin up...";
  95. usleep(50000); // 50ms
  96. nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
  97. if (nanoseconds_to_milliseconds(now - start) > 5000) {
  98. LOG(WARNING) << "Timed out while waiting for FUSE to spin up";
  99. return -ETIMEDOUT;
  100. }
  101. }
  102. /* sdcardfs will have exited already. FUSE will still be running */
  103. if (TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, WNOHANG)) == mFusePid)
  104. mFusePid = 0;
  105. return OK;
  106. }
  107. status_t EmulatedVolume::doUnmount() {
  108. // Unmount the storage before we kill the FUSE process. If we kill
  109. // the FUSE process first, most file system operations will return
  110. // ENOTCONN until the unmount completes. This is an exotic and unusual
  111. // error code and might cause broken behaviour in applications.
  112. KillProcessesUsingPath(getPath());
  113. ForceUnmount(mFuseDefault);
  114. ForceUnmount(mFuseRead);
  115. ForceUnmount(mFuseWrite);
  116. ForceUnmount(mFuseFull);
  117. rmdir(mFuseDefault.c_str());
  118. rmdir(mFuseRead.c_str());
  119. rmdir(mFuseWrite.c_str());
  120. rmdir(mFuseFull.c_str());
  121. mFuseDefault.clear();
  122. mFuseRead.clear();
  123. mFuseWrite.clear();
  124. mFuseFull.clear();
  125. return OK;
  126. }
  127. } // namespace vold
  128. } // namespace android