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 IDMAP2_IDMAP2D_IDMAP2SERVICE_H_ 18 #define IDMAP2_IDMAP2D_IDMAP2SERVICE_H_ 19 20 #include <android-base/unique_fd.h> 21 #include <android/os/BnIdmap2.h> 22 #include <android/os/FabricatedOverlayInfo.h> 23 #include <android/os/OverlayConstraint.h> 24 #include <binder/BinderService.h> 25 #include <idmap2/ResourceContainer.h> 26 #include <idmap2/Result.h> 27 28 #include <filesystem> 29 #include <memory> 30 #include <mutex> 31 #include <optional> 32 #include <string> 33 #include <variant> 34 #include <vector> 35 36 namespace android::os { 37 38 class Idmap2Service : public BinderService<Idmap2Service>, public BnIdmap2 { 39 public: getServiceName()40 static char const* getServiceName() { 41 return "idmap"; 42 } 43 44 binder::Status getIdmapPath(const std::string& overlay_path, int32_t user_id, 45 std::string* _aidl_return) override; 46 47 binder::Status removeIdmap(const std::string& overlay_path, int32_t user_id, 48 bool* _aidl_return) override; 49 50 binder::Status verifyIdmap(const std::string& target_path, const std::string& overlay_path, 51 const std::string& overlay_name, int32_t fulfilled_policies, 52 bool enforce_overlayable, int32_t user_id, 53 const std::vector<os::OverlayConstraint>& constraints, 54 bool* _aidl_return) override; 55 56 binder::Status createIdmap(const std::string& target_path, const std::string& overlay_path, 57 const std::string& overlay_name, int32_t fulfilled_policies, 58 bool enforce_overlayable, int32_t user_id, 59 const std::vector<os::OverlayConstraint>& constraints, 60 std::optional<std::string>* _aidl_return) override; 61 62 binder::Status createFabricatedOverlay( 63 const os::FabricatedOverlayInternal& overlay, 64 std::optional<os::FabricatedOverlayInfo>* _aidl_return) override; 65 66 binder::Status deleteFabricatedOverlay(const std::string& overlay_path, 67 bool* _aidl_return) override; 68 69 binder::Status acquireFabricatedOverlayIterator(int32_t* _aidl_return) override; 70 71 binder::Status releaseFabricatedOverlayIterator(int32_t iteratorId) override; 72 73 binder::Status nextFabricatedOverlayInfos(int32_t iteratorId, 74 std::vector<os::FabricatedOverlayInfo>* _aidl_return) override; 75 76 binder::Status dumpIdmap(const std::string& overlay_path, std::string* _aidl_return) override; 77 78 private: 79 // idmap2d is killed after a period of inactivity, so any information stored on this class should 80 // be able to be recalculated if idmap2 dies and restarts. 81 82 // A cache item for the resource containers (apks or frros), with all information needed to 83 // detect if it has changed since it was parsed: 84 // - (dev, inode) pair uniquely identifies a file on a particular device partition (see stat(2)). 85 // - (mtime, size) ensure the file data hasn't changed inside that file. 86 struct CachedContainer { 87 dev_t dev; 88 ino_t inode; 89 int64_t size; 90 struct timespec mtime; 91 std::shared_ptr<idmap2::TargetResourceContainer> apk; 92 }; 93 std::unordered_map<std::string, CachedContainer> container_cache_; 94 std::mutex container_cache_mutex_; 95 96 int32_t frro_iter_id_ = 0; 97 std::optional<std::filesystem::directory_iterator> frro_iter_; 98 std::mutex frro_iter_mutex_; 99 100 template <typename T> 101 using OwningPtr = std::variant<std::unique_ptr<T>, std::shared_ptr<T>>; 102 103 using TargetResourceContainerPtr = OwningPtr<idmap2::TargetResourceContainer>; 104 idmap2::Result<TargetResourceContainerPtr> GetTargetContainer(const std::string& target_path); 105 106 template <typename T> 107 WARN_UNUSED static const T* GetPointer(const OwningPtr<T>& ptr); 108 }; 109 110 } // namespace android::os 111 112 #endif // IDMAP2_IDMAP2D_IDMAP2SERVICE_H_ 113