blob: cc8cc5f218d2c74cebe08aba6efd4621537de790 [file] [log] [blame]
Mårten Kongstad02751232018-04-27 13:16:32 +02001/*
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>
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080021#include <android/os/BnIdmap2.h>
Ryan Mitchell6a2ca782021-01-19 13:51:15 -080022#include <android/os/FabricatedOverlayInfo.h>
Mårten Kongstad02751232018-04-27 13:16:32 +020023#include <binder/BinderService.h>
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080024#include <idmap2/ResourceContainer.h>
25#include <idmap2/Result.h>
Mårten Kongstad02751232018-04-27 13:16:32 +020026
Ryan Mitchellb887f682021-07-15 10:43:42 -070027#include <filesystem>
Mårten Kongstad1195a6b2021-05-11 12:57:01 +000028#include <memory>
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -070029#include <mutex>
30#include <optional>
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070031#include <string>
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -070032#include <variant>
Mårten Kongstad1195a6b2021-05-11 12:57:01 +000033#include <vector>
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070034
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010035namespace android::os {
36
Mårten Kongstad02751232018-04-27 13:16:32 +020037class Idmap2Service : public BinderService<Idmap2Service>, public BnIdmap2 {
38 public:
39 static char const* getServiceName() {
40 return "idmap";
41 }
42
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080043 binder::Status getIdmapPath(const std::string& overlay_path, int32_t user_id,
Mårten Kongstadcb85d9c2018-12-21 08:28:40 +010044 std::string* _aidl_return) override;
Mårten Kongstad02751232018-04-27 13:16:32 +020045
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080046 binder::Status removeIdmap(const std::string& overlay_path, int32_t user_id,
Mårten Kongstadcb85d9c2018-12-21 08:28:40 +010047 bool* _aidl_return) override;
Mårten Kongstad02751232018-04-27 13:16:32 +020048
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080049 binder::Status verifyIdmap(const std::string& target_path, const std::string& overlay_path,
Ryan Mitchell6a2ca782021-01-19 13:51:15 -080050 const std::string& overlay_name, int32_t fulfilled_policies,
51 bool enforce_overlayable, int32_t user_id,
Mårten Kongstadcb85d9c2018-12-21 08:28:40 +010052 bool* _aidl_return) override;
Mårten Kongstadef0695d2018-12-04 14:36:48 +010053
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080054 binder::Status createIdmap(const std::string& target_path, const std::string& overlay_path,
Ryan Mitchell6a2ca782021-01-19 13:51:15 -080055 const std::string& overlay_name, int32_t fulfilled_policies,
56 bool enforce_overlayable, int32_t user_id,
Jooyung Han16bac852020-08-10 12:53:14 +090057 std::optional<std::string>* _aidl_return) override;
Ryan Mitchell7d53f192020-04-24 17:45:25 -070058
Ryan Mitchell6a2ca782021-01-19 13:51:15 -080059 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
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -070066 binder::Status acquireFabricatedOverlayIterator(int32_t* _aidl_return) override;
Ryan Mitchellb887f682021-07-15 10:43:42 -070067
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -070068 binder::Status releaseFabricatedOverlayIterator(int32_t iteratorId) override;
Ryan Mitchellb887f682021-07-15 10:43:42 -070069
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -070070 binder::Status nextFabricatedOverlayInfos(int32_t iteratorId,
Ryan Mitchell6a2ca782021-01-19 13:51:15 -080071 std::vector<os::FabricatedOverlayInfo>* _aidl_return) override;
72
Mårten Kongstad99ae8982021-05-10 11:00:17 +000073 binder::Status dumpIdmap(const std::string& overlay_path, std::string* _aidl_return) override;
74
Ryan Mitchell7d53f192020-04-24 17:45:25 -070075 private:
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080076 // 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
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -070080 int32_t frro_iter_id_ = 0;
Ryan Mitchellb887f682021-07-15 10:43:42 -070081 std::optional<std::filesystem::directory_iterator> frro_iter_;
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -070082 std::mutex frro_iter_mutex_;
Ryan Mitchell6a2ca782021-01-19 13:51:15 -080083
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080084 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);
Mårten Kongstad02751232018-04-27 13:16:32 +020092};
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010093
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080094template <typename T>
95const 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
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100103} // namespace android::os
Mårten Kongstad02751232018-04-27 13:16:32 +0200104
105#endif // IDMAP2_IDMAP2D_IDMAP2SERVICE_H_