• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 [_, inserted] =
66         mounted_apexes_[package].emplace(std::forward<Args>(args)...);
67     CHECK(inserted);
68   }
69 
70   template <typename... Args>
AddMountedApex(const std::string & package,Args &&...args)71   inline void AddMountedApex(const std::string& package, Args&&... args)
72       REQUIRES(!mounted_apexes_mutex_) {
73     std::lock_guard lock(mounted_apexes_mutex_);
74     AddMountedApexLocked(package, std::forward<Args>(args)...);
75   }
76 
RemoveMountedApex(const std::string & package,const std::string & full_path)77   inline void RemoveMountedApex(const std::string& package,
78                                 const std::string& full_path)
79       REQUIRES(!mounted_apexes_mutex_) {
80     std::lock_guard lock(mounted_apexes_mutex_);
81     auto it = mounted_apexes_.find(package);
82     if (it == mounted_apexes_.end()) {
83       return;
84     }
85 
86     auto& pkg_set = it->second;
87 
88     for (auto pkg_it = pkg_set.begin(); pkg_it != pkg_set.end(); ++pkg_it) {
89       if (pkg_it->full_path == full_path) {
90         pkg_set.erase(pkg_it);
91         return;
92       }
93     }
94   }
95 
96   // 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)97   inline base::Result<void> DoIfLatest(
98       const std::string& package, const std::string& full_path,
99       const std::function<base::Result<void>()>& handler)
100       REQUIRES(!mounted_apexes_mutex_) {
101     std::lock_guard lock(mounted_apexes_mutex_);
102     auto it = mounted_apexes_.find(package);
103     CHECK(it != mounted_apexes_.end());
104     CHECK(!it->second.empty());
105 
106     auto latest = it->second.rbegin();
107     if (latest->full_path == full_path) {
108       return handler();
109     }
110     return {};
111   }
112 
113   template <typename T>
ForallMountedApexes(const std::string & package,const T & handler)114   inline void ForallMountedApexes(const std::string& package,
115                                   const T& handler) const
116       REQUIRES(!mounted_apexes_mutex_) {
117     std::lock_guard lock(mounted_apexes_mutex_);
118     auto outer_it = mounted_apexes_.find(package);
119     if (outer_it == mounted_apexes_.end()) {
120       return;
121     }
122     for (auto it = outer_it->second.rbegin(), end = outer_it->second.rend();
123          it != end; it++) {
124       bool latest = (it == outer_it->second.rbegin());
125       handler(*it, latest);
126     }
127   }
128 
129   template <typename T>
ForallMountedApexes(const T & handler)130   inline void ForallMountedApexes(const T& handler) const
131       REQUIRES(!mounted_apexes_mutex_) {
132     std::lock_guard lock(mounted_apexes_mutex_);
133     for (const auto& pkg : mounted_apexes_) {
134       for (auto it = pkg.second.rbegin(), end = pkg.second.rend(); it != end;
135            it++) {
136         bool latest = (it == pkg.second.rbegin());
137         handler(pkg.first, *it, latest);
138       }
139     }
140   }
141 
GetLatestMountedApex(const std::string & package)142   inline std::optional<MountedApexData> GetLatestMountedApex(
143       const std::string& package) const REQUIRES(!mounted_apexes_mutex_) {
144     std::optional<MountedApexData> ret;
145     ForallMountedApexes(package,
146                         [&ret](const MountedApexData& data, bool latest) {
147                           if (latest) {
148                             ret.emplace(data);
149                           }
150                         });
151     return ret;
152   }
153 
154   void PopulateFromMounts(const std::vector<std::string>& data_dirs);
155 
156   // Resets state of the database. Should only be used in testing.
Reset()157   inline void Reset() REQUIRES(!mounted_apexes_mutex_) {
158     std::lock_guard lock(mounted_apexes_mutex_);
159     mounted_apexes_.clear();
160   }
161 
162  private:
163   // A map from package name to mounted apexes.
164   // Note: using std::maps to
165   //         a) so we do not have to worry about iterator invalidation.
166   //         b) do not have to const_cast (over std::set)
167   // TODO(b/158467745): This structure (and functions) need to be guarded by
168   //   locks.
169   std::map<std::string, std::set<MountedApexData>> mounted_apexes_
170       GUARDED_BY(mounted_apexes_mutex_);
171 
172   // To fix thread safety negative capability warning
173   class Mutex : public std::mutex {
174    public:
175     // for negative capabilities
176     const Mutex& operator!() const { return *this; }
177   };
178   mutable Mutex mounted_apexes_mutex_;
179 };
180 
181 }  // namespace apex
182 }  // namespace android
183 
184 #endif  // ANDROID_APEXD_APEX_DATABASE_H_
185