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 // Whenever apex file specified in full_path was deleted. 45 bool deleted = false; 46 47 MountedApexData() = default; MountedApexDataMountedApexData48 MountedApexData(int version, const std::string& loop_name, 49 const std::string& full_path, 50 const std::string& mount_point, 51 const std::string& device_name) 52 : version(version), 53 loop_name(loop_name), 54 full_path(full_path), 55 mount_point(mount_point), 56 device_name(device_name), 57 deleted(false) {} 58 59 inline auto operator<=>(const MountedApexData& rhs) const = default; 60 }; 61 62 template <typename... Args> AddMountedApexLocked(const std::string & package,Args &&...args)63 inline void AddMountedApexLocked(const std::string& package, Args&&... args) 64 REQUIRES(mounted_apexes_mutex_) { 65 auto it = mounted_apexes_.find(package); 66 if (it == mounted_apexes_.end()) { 67 auto insert_it = 68 mounted_apexes_.emplace(package, std::set<MountedApexData>()); 69 CHECK(insert_it.second); 70 it = insert_it.first; 71 } 72 73 auto check_it = 74 it->second.emplace(MountedApexData(std::forward<Args>(args)...)); 75 CHECK(check_it.second); 76 77 CheckUniqueLoopDm(); 78 } 79 80 template <typename... Args> AddMountedApex(const std::string & package,Args &&...args)81 inline void AddMountedApex(const std::string& package, Args&&... args) 82 REQUIRES(!mounted_apexes_mutex_) { 83 std::lock_guard lock(mounted_apexes_mutex_); 84 AddMountedApexLocked(package, args...); 85 } 86 RemoveMountedApex(const std::string & package,const std::string & full_path)87 inline void RemoveMountedApex(const std::string& package, 88 const std::string& full_path) 89 REQUIRES(!mounted_apexes_mutex_) { 90 std::lock_guard lock(mounted_apexes_mutex_); 91 auto it = mounted_apexes_.find(package); 92 if (it == mounted_apexes_.end()) { 93 return; 94 } 95 96 auto& pkg_set = it->second; 97 98 for (auto pkg_it = pkg_set.begin(); pkg_it != pkg_set.end(); ++pkg_it) { 99 if (pkg_it->full_path == full_path) { 100 pkg_set.erase(pkg_it); 101 return; 102 } 103 } 104 } 105 106 // 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)107 inline base::Result<void> DoIfLatest( 108 const std::string& package, const std::string& full_path, 109 const std::function<base::Result<void>()>& handler) 110 REQUIRES(!mounted_apexes_mutex_) { 111 std::lock_guard lock(mounted_apexes_mutex_); 112 auto it = mounted_apexes_.find(package); 113 CHECK(it != mounted_apexes_.end()); 114 CHECK(!it->second.empty()); 115 116 auto latest = it->second.rbegin(); 117 if (latest->full_path == full_path) { 118 return handler(); 119 } 120 return {}; 121 } 122 123 template <typename T> ForallMountedApexes(const std::string & package,const T & handler)124 inline void ForallMountedApexes(const std::string& package, 125 const T& handler) const 126 REQUIRES(!mounted_apexes_mutex_) { 127 std::lock_guard lock(mounted_apexes_mutex_); 128 auto outer_it = mounted_apexes_.find(package); 129 if (outer_it == mounted_apexes_.end()) { 130 return; 131 } 132 for (auto it = outer_it->second.rbegin(), end = outer_it->second.rend(); 133 it != end; it++) { 134 bool latest = (it == outer_it->second.rbegin()); 135 handler(*it, latest); 136 } 137 } 138 139 template <typename T> ForallMountedApexes(const T & handler)140 inline void ForallMountedApexes(const T& handler) const 141 REQUIRES(!mounted_apexes_mutex_) { 142 std::lock_guard lock(mounted_apexes_mutex_); 143 for (const auto& pkg : mounted_apexes_) { 144 for (auto it = pkg.second.rbegin(), end = pkg.second.rend(); it != end; 145 it++) { 146 bool latest = (it == pkg.second.rbegin()); 147 handler(pkg.first, *it, latest); 148 } 149 } 150 } 151 GetLatestMountedApex(const std::string & package)152 inline std::optional<MountedApexData> GetLatestMountedApex( 153 const std::string& package) REQUIRES(!mounted_apexes_mutex_) { 154 std::optional<MountedApexData> ret; 155 ForallMountedApexes(package, 156 [&ret](const MountedApexData& data, bool latest) { 157 if (latest) { 158 ret.emplace(data); 159 } 160 }); 161 return ret; 162 } 163 164 void PopulateFromMounts(const std::vector<std::string>& data_dirs); 165 166 // Resets state of the database. Should only be used in testing. Reset()167 inline void Reset() REQUIRES(!mounted_apexes_mutex_) { 168 std::lock_guard lock(mounted_apexes_mutex_); 169 mounted_apexes_.clear(); 170 } 171 172 private: 173 // A map from package name to mounted apexes. 174 // Note: using std::maps to 175 // a) so we do not have to worry about iterator invalidation. 176 // b) do not have to const_cast (over std::set) 177 // TODO(b/158467745): This structure (and functions) need to be guarded by 178 // locks. 179 std::map<std::string, std::set<MountedApexData>> mounted_apexes_ 180 GUARDED_BY(mounted_apexes_mutex_); 181 182 // To fix thread safety negative capability warning 183 class Mutex : public std::mutex { 184 public: 185 // for negative capabilities 186 const Mutex& operator!() const { return *this; } 187 }; 188 mutable Mutex mounted_apexes_mutex_; 189 CheckUniqueLoopDm()190 inline void CheckUniqueLoopDm() REQUIRES(mounted_apexes_mutex_) { 191 std::unordered_set<std::string> loop_devices; 192 std::unordered_set<std::string> dm_devices; 193 for (const auto& apex_set : mounted_apexes_) { 194 for (const auto& mount : apex_set.second) { 195 if (mount.loop_name != "") { 196 CHECK(loop_devices.insert(mount.loop_name).second) 197 << "Duplicate loop device: " << mount.loop_name; 198 } 199 if (mount.device_name != "") { 200 CHECK(dm_devices.insert(mount.device_name).second) 201 << "Duplicate dm device: " << mount.device_name; 202 } 203 } 204 } 205 } 206 }; 207 208 } // namespace apex 209 } // namespace android 210 211 #endif // ANDROID_APEXD_APEX_DATABASE_H_ 212