1 /* 2 * Copyright (C) 2018 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 #ifndef ANDROID_APEXD_APEX_DATABASE_H_ 18 #define ANDROID_APEXD_APEX_DATABASE_H_ 19 20 #include <android-base/logging.h> 21 #include <android-base/result.h> 22 #include <android-base/thread_annotations.h> 23 24 #include <map> 25 #include <mutex> 26 #include <optional> 27 #include <set> 28 #include <string> 29 #include <unordered_set> 30 31 namespace android { 32 namespace apex { 33 34 class MountedApexDatabase { 35 public: 36 // Stores associated low-level data for a mounted APEX. To conserve memory, 37 // the APEX file isn't stored, but must be opened to retrieve specific data. 38 struct MountedApexData { 39 int version = 0; // APEX version for this mount 40 std::string loop_name; // Loop device used (fs path). 41 std::string full_path; // Full path to the apex file. 42 std::string mount_point; // Path this apex is mounted on. 43 std::string device_name; // Name of the dm verity device. 44 // Name of the loop device backing up hashtree or empty string in case 45 // hashtree is embedded inside an APEX. 46 std::string hashtree_loop_name; 47 // Whenever apex file specified in full_path was deleted. 48 bool deleted; 49 // Whether the mount is a temp mount or not. 50 bool is_temp_mount; 51 MountedApexDataMountedApexData52 MountedApexData() : deleted(false), is_temp_mount(false) {} 53 MountedApexData(int version, const std::string& loop_name, 54 const std::string& full_path, 55 const std::string& mount_point, 56 const std::string& device_name, 57 const std::string& hashtree_loop_name, 58 bool is_temp_mount = false) versionMountedApexData59 : version(version), 60 loop_name(loop_name), 61 full_path(full_path), 62 mount_point(mount_point), 63 device_name(device_name), 64 hashtree_loop_name(hashtree_loop_name), 65 deleted(false), 66 is_temp_mount(is_temp_mount) {} 67 68 inline bool operator<(const MountedApexData& rhs) const { 69 if (version != rhs.version) { 70 return version < rhs.version; 71 } 72 int compare_val = loop_name.compare(rhs.loop_name); 73 if (compare_val < 0) { 74 return true; 75 } else if (compare_val > 0) { 76 return false; 77 } 78 compare_val = full_path.compare(rhs.full_path); 79 if (compare_val < 0) { 80 return true; 81 } else if (compare_val > 0) { 82 return false; 83 } 84 compare_val = mount_point.compare(rhs.mount_point); 85 if (compare_val < 0) { 86 return true; 87 } else if (compare_val > 0) { 88 return false; 89 } 90 compare_val = device_name.compare(rhs.device_name); 91 if (compare_val < 0) { 92 return true; 93 } else if (compare_val > 0) { 94 return false; 95 } 96 return hashtree_loop_name < rhs.hashtree_loop_name; 97 } 98 }; 99 100 template <typename... Args> AddMountedApexLocked(const std::string & package,Args &&...args)101 inline void AddMountedApexLocked(const std::string& package, Args&&... args) 102 REQUIRES(mounted_apexes_mutex_) { 103 auto it = mounted_apexes_.find(package); 104 if (it == mounted_apexes_.end()) { 105 auto insert_it = 106 mounted_apexes_.emplace(package, std::set<MountedApexData>()); 107 CHECK(insert_it.second); 108 it = insert_it.first; 109 } 110 111 auto check_it = 112 it->second.emplace(MountedApexData(std::forward<Args>(args)...)); 113 CHECK(check_it.second); 114 115 CheckUniqueLoopDm(); 116 } 117 118 template <typename... Args> AddMountedApex(const std::string & package,Args &&...args)119 inline void AddMountedApex(const std::string& package, Args&&... args) 120 REQUIRES(!mounted_apexes_mutex_) { 121 std::lock_guard lock(mounted_apexes_mutex_); 122 AddMountedApexLocked(package, args...); 123 } 124 125 inline void RemoveMountedApex(const std::string& package, 126 const std::string& full_path, 127 bool match_temp_mounts = false) 128 REQUIRES(!mounted_apexes_mutex_) { 129 std::lock_guard lock(mounted_apexes_mutex_); 130 auto it = mounted_apexes_.find(package); 131 if (it == mounted_apexes_.end()) { 132 return; 133 } 134 135 auto& pkg_set = it->second; 136 137 for (auto pkg_it = pkg_set.begin(); pkg_it != pkg_set.end(); ++pkg_it) { 138 if (pkg_it->full_path == full_path && 139 pkg_it->is_temp_mount == match_temp_mounts) { 140 pkg_set.erase(pkg_it); 141 return; 142 } 143 } 144 } 145 146 // Invoke handler if the passed package is the latest DoIfLatest(const std::string & package,const std::string & full_path,const std::function<base::Result<void> ()> & handler)147 inline base::Result<void> DoIfLatest( 148 const std::string& package, const std::string& full_path, 149 const std::function<base::Result<void>()>& handler) 150 REQUIRES(!mounted_apexes_mutex_) { 151 std::lock_guard lock(mounted_apexes_mutex_); 152 auto it = mounted_apexes_.find(package); 153 CHECK(it != mounted_apexes_.end()); 154 CHECK(!it->second.empty()); 155 156 auto latest = it->second.rbegin(); 157 if (latest->full_path == full_path) { 158 return handler(); 159 } 160 return {}; 161 } 162 163 template <typename T> 164 inline void ForallMountedApexes(const std::string& package, const T& handler, 165 bool match_temp_mounts = false) const 166 REQUIRES(!mounted_apexes_mutex_) { 167 std::lock_guard lock(mounted_apexes_mutex_); 168 auto outer_it = mounted_apexes_.find(package); 169 if (outer_it == mounted_apexes_.end()) { 170 return; 171 } 172 for (auto it = outer_it->second.rbegin(), end = outer_it->second.rend(); 173 it != end; it++) { 174 if (it->is_temp_mount == match_temp_mounts) { 175 bool latest = (it == outer_it->second.rbegin()); 176 handler(*it, latest); 177 } 178 } 179 } 180 181 template <typename T> 182 inline void ForallMountedApexes(const T& handler, 183 bool match_temp_mounts = false) const 184 REQUIRES(!mounted_apexes_mutex_) { 185 std::lock_guard lock(mounted_apexes_mutex_); 186 for (const auto& pkg : mounted_apexes_) { 187 for (auto it = pkg.second.rbegin(), end = pkg.second.rend(); it != end; 188 it++) { 189 if (it->is_temp_mount == match_temp_mounts) { 190 bool latest = (it == pkg.second.rbegin()); 191 handler(pkg.first, *it, latest); 192 } 193 } 194 } 195 } 196 GetLatestMountedApex(const std::string & package)197 inline std::optional<MountedApexData> GetLatestMountedApex( 198 const std::string& package) REQUIRES(!mounted_apexes_mutex_) { 199 std::optional<MountedApexData> ret; 200 ForallMountedApexes(package, 201 [&ret](const MountedApexData& data, bool latest) { 202 if (latest) { 203 ret.emplace(data); 204 } 205 }); 206 return ret; 207 } 208 209 void PopulateFromMounts(const std::vector<std::string>& data_dirs, 210 const std::string& apex_hash_tree_dir); 211 212 // Resets state of the database. Should only be used in testing. Reset()213 inline void Reset() REQUIRES(!mounted_apexes_mutex_) { 214 std::lock_guard lock(mounted_apexes_mutex_); 215 mounted_apexes_.clear(); 216 } 217 218 private: 219 // A map from package name to mounted apexes. 220 // Note: using std::maps to 221 // a) so we do not have to worry about iterator invalidation. 222 // b) do not have to const_cast (over std::set) 223 // TODO(b/158467745): This structure (and functions) need to be guarded by 224 // locks. 225 std::map<std::string, std::set<MountedApexData>> mounted_apexes_ 226 GUARDED_BY(mounted_apexes_mutex_); 227 228 // To fix thread safety negative capability warning 229 class Mutex : public std::mutex { 230 public: 231 // for negative capabilities 232 const Mutex& operator!() const { return *this; } 233 }; 234 mutable Mutex mounted_apexes_mutex_; 235 CheckUniqueLoopDm()236 inline void CheckUniqueLoopDm() REQUIRES(mounted_apexes_mutex_) { 237 std::unordered_set<std::string> loop_devices; 238 std::unordered_set<std::string> dm_devices; 239 for (const auto& apex_set : mounted_apexes_) { 240 for (const auto& mount : apex_set.second) { 241 if (mount.loop_name != "") { 242 CHECK(loop_devices.insert(mount.loop_name).second) 243 << "Duplicate loop device: " << mount.loop_name; 244 } 245 if (mount.device_name != "") { 246 CHECK(dm_devices.insert(mount.device_name).second) 247 << "Duplicate dm device: " << mount.device_name; 248 } 249 if (mount.hashtree_loop_name != "") { 250 CHECK(loop_devices.insert(mount.hashtree_loop_name).second) 251 << "Duplicate loop device: " << mount.hashtree_loop_name; 252 } 253 } 254 } 255 } 256 }; 257 258 } // namespace apex 259 } // namespace android 260 261 #endif // ANDROID_APEXD_APEX_DATABASE_H_ 262