VolumeManager.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (C) 2008 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. #ifndef ANDROID_VOLD_VOLUME_MANAGER_H
  17. #define ANDROID_VOLD_VOLUME_MANAGER_H
  18. #include <fnmatch.h>
  19. #include <pthread.h>
  20. #include <stdlib.h>
  21. #include <list>
  22. #include <mutex>
  23. #include <string>
  24. #include <unordered_map>
  25. #include <unordered_set>
  26. #include <android-base/unique_fd.h>
  27. #include <cutils/multiuser.h>
  28. #include <sysutils/NetlinkEvent.h>
  29. #include <utils/List.h>
  30. #include <utils/Timers.h>
  31. #include "android/os/IVoldListener.h"
  32. #include "model/Disk.h"
  33. #include "model/VolumeBase.h"
  34. class VolumeManager {
  35. private:
  36. static VolumeManager* sInstance;
  37. bool mDebug;
  38. public:
  39. virtual ~VolumeManager();
  40. // TODO: pipe all requests through VM to avoid exposing this lock
  41. std::mutex& getLock() { return mLock; }
  42. std::mutex& getCryptLock() { return mCryptLock; }
  43. void setListener(android::sp<android::os::IVoldListener> listener) { mListener = listener; }
  44. android::sp<android::os::IVoldListener> getListener() const { return mListener; }
  45. int start();
  46. int stop();
  47. void handleBlockEvent(NetlinkEvent* evt);
  48. class DiskSource {
  49. public:
  50. DiskSource(const std::string& sysPattern, const std::string& nickname, int flags)
  51. : mSysPattern(sysPattern), mNickname(nickname), mFlags(flags) {}
  52. bool matches(const std::string& sysPath) {
  53. return !fnmatch(mSysPattern.c_str(), sysPath.c_str(), 0);
  54. }
  55. const std::string& getNickname() const { return mNickname; }
  56. int getFlags() const { return mFlags; }
  57. private:
  58. std::string mSysPattern;
  59. std::string mNickname;
  60. int mFlags;
  61. };
  62. void addDiskSource(const std::shared_ptr<DiskSource>& diskSource);
  63. std::shared_ptr<android::vold::Disk> findDisk(const std::string& id);
  64. std::shared_ptr<android::vold::VolumeBase> findVolume(const std::string& id);
  65. void listVolumes(android::vold::VolumeBase::Type type, std::list<std::string>& list) const;
  66. int forgetPartition(const std::string& partGuid, const std::string& fsUuid);
  67. int onUserAdded(userid_t userId, int userSerialNumber);
  68. int onUserRemoved(userid_t userId);
  69. int onUserStarted(userid_t userId);
  70. int onUserStopped(userid_t userId);
  71. int onSecureKeyguardStateChanged(bool isShowing);
  72. int setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol);
  73. int remountUid(uid_t uid, int32_t remountMode);
  74. /* Reset all internal state, typically during framework boot */
  75. int reset();
  76. /* Prepare for device shutdown, safely unmounting all devices */
  77. int shutdown();
  78. /* Unmount all volumes, usually for encryption */
  79. int unmountAll();
  80. int updateVirtualDisk();
  81. int setDebug(bool enable);
  82. static VolumeManager* Instance();
  83. /*
  84. * Ensure that all directories along given path exist, creating parent
  85. * directories as needed. Validates that given path is absolute and that
  86. * it contains no relative "." or ".." paths or symlinks. Last path segment
  87. * is treated as filename and ignored, unless the path ends with "/". Also
  88. * ensures that path belongs to a volume managed by vold.
  89. */
  90. int mkdirs(const std::string& path);
  91. int createObb(const std::string& path, const std::string& key, int32_t ownerGid,
  92. std::string* outVolId);
  93. int destroyObb(const std::string& volId);
  94. int createStubVolume(const std::string& sourcePath, const std::string& mountPath,
  95. const std::string& fsType, const std::string& fsUuid,
  96. const std::string& fsLabel, std::string* outVolId);
  97. int destroyStubVolume(const std::string& volId);
  98. int mountAppFuse(uid_t uid, int mountId, android::base::unique_fd* device_fd);
  99. int unmountAppFuse(uid_t uid, int mountId);
  100. int openAppFuseFile(uid_t uid, int mountId, int fileId, int flags);
  101. private:
  102. VolumeManager();
  103. void readInitialState();
  104. int linkPrimary(userid_t userId);
  105. void handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk);
  106. void handleDiskChanged(dev_t device);
  107. void handleDiskRemoved(dev_t device);
  108. std::mutex mLock;
  109. std::mutex mCryptLock;
  110. android::sp<android::os::IVoldListener> mListener;
  111. std::list<std::shared_ptr<DiskSource>> mDiskSources;
  112. std::list<std::shared_ptr<android::vold::Disk>> mDisks;
  113. std::list<std::shared_ptr<android::vold::Disk>> mPendingDisks;
  114. std::list<std::shared_ptr<android::vold::VolumeBase>> mObbVolumes;
  115. std::list<std::shared_ptr<android::vold::VolumeBase>> mStubVolumes;
  116. std::unordered_map<userid_t, int> mAddedUsers;
  117. std::unordered_set<userid_t> mStartedUsers;
  118. std::string mVirtualDiskPath;
  119. std::shared_ptr<android::vold::Disk> mVirtualDisk;
  120. std::shared_ptr<android::vold::VolumeBase> mInternalEmulated;
  121. std::shared_ptr<android::vold::VolumeBase> mPrimary;
  122. int mNextObbId;
  123. int mNextStubVolumeId;
  124. bool mSecureKeyguardShowing;
  125. };
  126. #endif