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 <binder/BinderService.h>
24 #include <idmap2/ResourceContainer.h>
25 #include <idmap2/Result.h>
26
27 #include <filesystem>
28 #include <memory>
29 #include <mutex>
30 #include <optional>
31 #include <string>
32 #include <variant>
33 #include <vector>
34
35 namespace android::os {
36
37 class Idmap2Service : public BinderService<Idmap2Service>, public BnIdmap2 {
38 public:
getServiceName()39 static char const* getServiceName() {
40 return "idmap";
41 }
42
43 binder::Status getIdmapPath(const std::string& overlay_path, int32_t user_id,
44 std::string* _aidl_return) override;
45
46 binder::Status removeIdmap(const std::string& overlay_path, int32_t user_id,
47 bool* _aidl_return) override;
48
49 binder::Status verifyIdmap(const std::string& target_path, const std::string& overlay_path,
50 const std::string& overlay_name, int32_t fulfilled_policies,
51 bool enforce_overlayable, int32_t user_id,
52 bool* _aidl_return) override;
53
54 binder::Status createIdmap(const std::string& target_path, const std::string& overlay_path,
55 const std::string& overlay_name, int32_t fulfilled_policies,
56 bool enforce_overlayable, int32_t user_id,
57 std::optional<std::string>* _aidl_return) override;
58
59 binder::Status createFabricatedOverlay(
60 const os::FabricatedOverlayInternal& overlay,
61 std::optional<os::FabricatedOverlayInfo>* _aidl_return) override;
62
63 binder::Status deleteFabricatedOverlay(const std::string& overlay_path,
64 bool* _aidl_return) override;
65
66 binder::Status acquireFabricatedOverlayIterator(int32_t* _aidl_return) override;
67
68 binder::Status releaseFabricatedOverlayIterator(int32_t iteratorId) override;
69
70 binder::Status nextFabricatedOverlayInfos(int32_t iteratorId,
71 std::vector<os::FabricatedOverlayInfo>* _aidl_return) override;
72
73 binder::Status dumpIdmap(const std::string& overlay_path, std::string* _aidl_return) override;
74
75 private:
76 // idmap2d is killed after a period of inactivity, so any information stored on this class should
77 // be able to be recalculated if idmap2 dies and restarts.
78 std::unique_ptr<idmap2::TargetResourceContainer> framework_apk_cache_;
79
80 int32_t frro_iter_id_ = 0;
81 std::optional<std::filesystem::directory_iterator> frro_iter_;
82 std::mutex frro_iter_mutex_;
83
84 template <typename T>
85 using MaybeUniquePtr = std::variant<std::unique_ptr<T>, T*>;
86
87 using TargetResourceContainerPtr = MaybeUniquePtr<idmap2::TargetResourceContainer>;
88 idmap2::Result<TargetResourceContainerPtr> GetTargetContainer(const std::string& target_path);
89
90 template <typename T>
91 WARN_UNUSED static const T* GetPointer(const MaybeUniquePtr<T>& ptr);
92 };
93
94 template <typename T>
GetPointer(const MaybeUniquePtr<T> & ptr)95 const T* Idmap2Service::GetPointer(const MaybeUniquePtr<T>& ptr) {
96 auto u = std::get_if<T*>(&ptr);
97 if (u != nullptr) {
98 return *u;
99 }
100 return std::get<std::unique_ptr<T>>(ptr).get();
101 }
102
103 } // namespace android::os
104
105 #endif // IDMAP2_IDMAP2D_IDMAP2SERVICE_H_
106