San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 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 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_VOLD_VOLUME_MANAGER_H |
| 18 | #define ANDROID_VOLD_VOLUME_MANAGER_H |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 19 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 20 | #include <fnmatch.h> |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 21 | #include <pthread.h> |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 22 | #include <stdlib.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 23 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 24 | #include <list> |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 25 | #include <mutex> |
| 26 | #include <string> |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 27 | #include <unordered_map> |
| 28 | #include <unordered_set> |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 29 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 30 | #include <android-base/unique_fd.h> |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 31 | #include <cutils/multiuser.h> |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 32 | #include <sysutils/NetlinkEvent.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 33 | #include <utils/List.h> |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 34 | #include <utils/Timers.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 35 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 36 | #include "android/os/IVoldListener.h" |
| 37 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 38 | #include "model/Disk.h" |
| 39 | #include "model/VolumeBase.h" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 40 | |
| 41 | class VolumeManager { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 42 | private: |
| 43 | static VolumeManager* sInstance; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 44 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 45 | bool mDebug; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 46 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 47 | public: |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 48 | virtual ~VolumeManager(); |
| 49 | |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 50 | // TODO: pipe all requests through VM to avoid exposing this lock |
| 51 | std::mutex& getLock() { return mLock; } |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 52 | std::mutex& getCryptLock() { return mCryptLock; } |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 53 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 54 | void setListener(android::sp<android::os::IVoldListener> listener) { mListener = listener; } |
Greg Kaiser | 2bc201e | 2018-12-18 08:42:08 -0800 | [diff] [blame] | 55 | android::sp<android::os::IVoldListener> getListener() const { return mListener; } |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 56 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 57 | int start(); |
| 58 | int stop(); |
| 59 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 60 | void handleBlockEvent(NetlinkEvent* evt); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 61 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 62 | class DiskSource { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 63 | public: |
| 64 | DiskSource(const std::string& sysPattern, const std::string& nickname, int flags) |
| 65 | : mSysPattern(sysPattern), mNickname(nickname), mFlags(flags) {} |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 66 | |
| 67 | bool matches(const std::string& sysPath) { |
| 68 | return !fnmatch(mSysPattern.c_str(), sysPath.c_str(), 0); |
| 69 | } |
| 70 | |
Greg Kaiser | 2bc201e | 2018-12-18 08:42:08 -0800 | [diff] [blame] | 71 | const std::string& getNickname() const { return mNickname; } |
| 72 | int getFlags() const { return mFlags; } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 73 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 74 | private: |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 75 | std::string mSysPattern; |
| 76 | std::string mNickname; |
| 77 | int mFlags; |
| 78 | }; |
| 79 | |
| 80 | void addDiskSource(const std::shared_ptr<DiskSource>& diskSource); |
| 81 | |
| 82 | std::shared_ptr<android::vold::Disk> findDisk(const std::string& id); |
| 83 | std::shared_ptr<android::vold::VolumeBase> findVolume(const std::string& id); |
| 84 | |
Greg Kaiser | 2bc201e | 2018-12-18 08:42:08 -0800 | [diff] [blame] | 85 | void listVolumes(android::vold::VolumeBase::Type type, std::list<std::string>& list) const; |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 86 | |
Jeff Sharkey | 3ce1825 | 2017-10-24 11:08:45 -0600 | [diff] [blame] | 87 | int forgetPartition(const std::string& partGuid, const std::string& fsUuid); |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 88 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 89 | int onUserAdded(userid_t userId, int userSerialNumber); |
| 90 | int onUserRemoved(userid_t userId); |
Sudheer Shanka | 69bc40f | 2018-10-25 11:06:55 -0700 | [diff] [blame] | 91 | int onUserStarted(userid_t userId, const std::vector<std::string>& packageNames, |
| 92 | const std::vector<int>& appIds, const std::vector<std::string>& sandboxIds); |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 93 | int onUserStopped(userid_t userId); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 94 | |
Sudheer Shanka | d484aa9 | 2018-07-31 10:07:34 -0700 | [diff] [blame] | 95 | int addAppIds(const std::vector<std::string>& packageNames, const std::vector<int32_t>& appIds); |
| 96 | int addSandboxIds(const std::vector<int32_t>& appIds, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 97 | const std::vector<std::string>& sandboxIds); |
Sudheer Shanka | fa6a174 | 2018-10-04 16:26:22 -0700 | [diff] [blame] | 98 | int prepareSandboxForApp(const std::string& packageName, appid_t appId, |
| 99 | const std::string& sandboxId, userid_t userId); |
Sudheer Shanka | 69bc40f | 2018-10-25 11:06:55 -0700 | [diff] [blame] | 100 | int destroySandboxForApp(const std::string& packageName, const std::string& sandboxId, |
| 101 | userid_t userId); |
Sudheer Shanka | d484aa9 | 2018-07-31 10:07:34 -0700 | [diff] [blame] | 102 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 103 | int onVolumeMounted(android::vold::VolumeBase* vol); |
| 104 | int onVolumeUnmounted(android::vold::VolumeBase* vol); |
| 105 | |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 106 | int onSecureKeyguardStateChanged(bool isShowing); |
| 107 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 108 | int setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol); |
| 109 | |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 110 | int remountUid(uid_t uid, int32_t remountMode); |
| 111 | int remountUidLegacy(uid_t uid, int32_t remountMode); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 112 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 113 | /* Reset all internal state, typically during framework boot */ |
| 114 | int reset(); |
| 115 | /* Prepare for device shutdown, safely unmounting all devices */ |
| 116 | int shutdown(); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 117 | /* Unmount all volumes, usually for encryption */ |
| 118 | int unmountAll(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 119 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 120 | int updateVirtualDisk(); |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 121 | int setDebug(bool enable); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 122 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 123 | static VolumeManager* Instance(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 124 | |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 125 | /* |
| 126 | * Ensure that all directories along given path exist, creating parent |
| 127 | * directories as needed. Validates that given path is absolute and that |
| 128 | * it contains no relative "." or ".." paths or symlinks. Last path segment |
| 129 | * is treated as filename and ignored, unless the path ends with "/". Also |
| 130 | * ensures that path belongs to a volume managed by vold. |
| 131 | */ |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 132 | int mkdirs(const std::string& path); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 133 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 134 | int createObb(const std::string& path, const std::string& key, int32_t ownerGid, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 135 | std::string* outVolId); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 136 | int destroyObb(const std::string& volId); |
| 137 | |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 138 | int createStubVolume(const std::string& sourcePath, const std::string& mountPath, |
| 139 | const std::string& fsType, const std::string& fsUuid, |
| 140 | const std::string& fsLabel, std::string* outVolId); |
| 141 | int destroyStubVolume(const std::string& volId); |
| 142 | |
Risan | 8f6198d | 2018-10-26 20:56:45 -0600 | [diff] [blame] | 143 | int mountAppFuse(uid_t uid, int mountId, android::base::unique_fd* device_fd); |
| 144 | int unmountAppFuse(uid_t uid, int mountId); |
| 145 | int openAppFuseFile(uid_t uid, int mountId, int fileId, int flags); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 146 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 147 | private: |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 148 | VolumeManager(); |
Mike Lockwood | 99635f6 | 2010-06-25 23:04:04 -0400 | [diff] [blame] | 149 | void readInitialState(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 150 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 151 | int linkPrimary(userid_t userId); |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 152 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 153 | int prepareSandboxes(userid_t userId, const std::vector<std::string>& packageNames, |
| 154 | const std::vector<std::string>& visibleVolLabels); |
| 155 | int mountPkgSpecificDirsForRunningProcs(userid_t userId, |
| 156 | const std::vector<std::string>& packageNames, |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 157 | const std::vector<std::string>& visibleVolLabels, |
| 158 | int remountMode); |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 159 | int destroySandboxesForVol(android::vold::VolumeBase* vol, userid_t userId); |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 160 | std::string prepareSandboxSource(uid_t uid, const std::string& sandboxId, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 161 | const std::string& sandboxRootDir); |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 162 | std::string prepareSandboxTarget(const std::string& packageName, uid_t uid, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 163 | const std::string& volumeLabel, |
| 164 | const std::string& mntTargetRootDir, bool isUserDependent); |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 165 | std::string preparePkgDataSource(const std::string& packageName, uid_t uid, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 166 | const std::string& dataRootDir); |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 167 | std::string prepareSubDirs(const std::string& pathPrefix, const std::string& subDirs, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 168 | mode_t mode, uid_t uid, gid_t gid); |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 169 | bool createPkgSpecificDirRoots(const std::string& volumeRoot); |
| 170 | bool createPkgSpecificDirs(const std::string& packageName, uid_t uid, |
| 171 | const std::string& volumeRoot, const std::string& sandboxDirRoot); |
| 172 | int mountPkgSpecificDir(const std::string& mntSourceRoot, const std::string& mntTargetRoot, |
| 173 | const std::string& packageName, const char* dirName); |
Sudheer Shanka | fa6a174 | 2018-10-04 16:26:22 -0700 | [diff] [blame] | 174 | int destroySandboxForAppOnVol(const std::string& packageName, const std::string& sandboxId, |
| 175 | userid_t userId, const std::string& volLabel); |
Sudheer Shanka | 03992e3 | 2018-12-12 12:43:38 -0800 | [diff] [blame] | 176 | int getMountModeForRunningProc(const std::vector<std::string>& packagesForUid, userid_t userId, |
| 177 | struct stat& mntWriteStat); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 178 | |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 179 | void handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk); |
| 180 | void handleDiskChanged(dev_t device); |
| 181 | void handleDiskRemoved(dev_t device); |
| 182 | |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 183 | std::mutex mLock; |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 184 | std::mutex mCryptLock; |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 185 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 186 | android::sp<android::os::IVoldListener> mListener; |
| 187 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 188 | std::list<std::shared_ptr<DiskSource>> mDiskSources; |
| 189 | std::list<std::shared_ptr<android::vold::Disk>> mDisks; |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 190 | std::list<std::shared_ptr<android::vold::Disk>> mPendingDisks; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 191 | std::list<std::shared_ptr<android::vold::VolumeBase>> mObbVolumes; |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 192 | std::list<std::shared_ptr<android::vold::VolumeBase>> mStubVolumes; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 193 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 194 | std::unordered_map<userid_t, int> mAddedUsers; |
| 195 | std::unordered_set<userid_t> mStartedUsers; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 196 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 197 | std::string mVirtualDiskPath; |
| 198 | std::shared_ptr<android::vold::Disk> mVirtualDisk; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 199 | std::shared_ptr<android::vold::VolumeBase> mInternalEmulated; |
| 200 | std::shared_ptr<android::vold::VolumeBase> mPrimary; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 201 | |
Sudheer Shanka | 62bbb2b | 2018-08-01 01:09:10 -0700 | [diff] [blame] | 202 | std::unordered_map<std::string, appid_t> mAppIds; |
| 203 | std::unordered_map<appid_t, std::string> mSandboxIds; |
| 204 | std::unordered_map<userid_t, std::vector<std::string>> mUserPackages; |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 205 | std::unordered_set<std::string> mVisibleVolumeIds; |
Sudheer Shanka | 62bbb2b | 2018-08-01 01:09:10 -0700 | [diff] [blame] | 206 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 207 | int mNextObbId; |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 208 | int mNextStubVolumeId; |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 209 | bool mSecureKeyguardShowing; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 210 | }; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 211 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 212 | #endif |