| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 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> | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 21 | #include <android/os/BnIdmap2.h> | 
| Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 22 | #include <android/os/FabricatedOverlayInfo.h> | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 23 | #include <binder/BinderService.h> | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 24 | #include <idmap2/ResourceContainer.h> | 
|  | 25 | #include <idmap2/Result.h> | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 26 |  | 
| Ryan Mitchell | b887f68 | 2021-07-15 10:43:42 -0700 | [diff] [blame] | 27 | #include <filesystem> | 
| Mårten Kongstad | 1195a6b | 2021-05-11 12:57:01 +0000 | [diff] [blame] | 28 | #include <memory> | 
| Yurii Zubrytskyi | de47d0b | 2022-09-29 16:43:04 -0700 | [diff] [blame] | 29 | #include <mutex> | 
|  | 30 | #include <optional> | 
| Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 31 | #include <string> | 
| Yurii Zubrytskyi | de47d0b | 2022-09-29 16:43:04 -0700 | [diff] [blame] | 32 | #include <variant> | 
| Mårten Kongstad | 1195a6b | 2021-05-11 12:57:01 +0000 | [diff] [blame] | 33 | #include <vector> | 
| Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 34 |  | 
| Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 35 | namespace android::os { | 
|  | 36 |  | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 37 | class Idmap2Service : public BinderService<Idmap2Service>, public BnIdmap2 { | 
|  | 38 | public: | 
|  | 39 | static char const* getServiceName() { | 
|  | 40 | return "idmap"; | 
|  | 41 | } | 
|  | 42 |  | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 43 | binder::Status getIdmapPath(const std::string& overlay_path, int32_t user_id, | 
| Mårten Kongstad | cb85d9c | 2018-12-21 08:28:40 +0100 | [diff] [blame] | 44 | std::string* _aidl_return) override; | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 45 |  | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 46 | binder::Status removeIdmap(const std::string& overlay_path, int32_t user_id, | 
| Mårten Kongstad | cb85d9c | 2018-12-21 08:28:40 +0100 | [diff] [blame] | 47 | bool* _aidl_return) override; | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 48 |  | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 49 | binder::Status verifyIdmap(const std::string& target_path, const std::string& overlay_path, | 
| Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 50 | const std::string& overlay_name, int32_t fulfilled_policies, | 
|  | 51 | bool enforce_overlayable, int32_t user_id, | 
| Mårten Kongstad | cb85d9c | 2018-12-21 08:28:40 +0100 | [diff] [blame] | 52 | bool* _aidl_return) override; | 
| Mårten Kongstad | ef0695d | 2018-12-04 14:36:48 +0100 | [diff] [blame] | 53 |  | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 54 | binder::Status createIdmap(const std::string& target_path, const std::string& overlay_path, | 
| Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 55 | const std::string& overlay_name, int32_t fulfilled_policies, | 
|  | 56 | bool enforce_overlayable, int32_t user_id, | 
| Jooyung Han | 16bac85 | 2020-08-10 12:53:14 +0900 | [diff] [blame] | 57 | std::optional<std::string>* _aidl_return) override; | 
| Ryan Mitchell | 7d53f19 | 2020-04-24 17:45:25 -0700 | [diff] [blame] | 58 |  | 
| Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 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 |  | 
| Yurii Zubrytskyi | de47d0b | 2022-09-29 16:43:04 -0700 | [diff] [blame] | 66 | binder::Status acquireFabricatedOverlayIterator(int32_t* _aidl_return) override; | 
| Ryan Mitchell | b887f68 | 2021-07-15 10:43:42 -0700 | [diff] [blame] | 67 |  | 
| Yurii Zubrytskyi | de47d0b | 2022-09-29 16:43:04 -0700 | [diff] [blame] | 68 | binder::Status releaseFabricatedOverlayIterator(int32_t iteratorId) override; | 
| Ryan Mitchell | b887f68 | 2021-07-15 10:43:42 -0700 | [diff] [blame] | 69 |  | 
| Yurii Zubrytskyi | de47d0b | 2022-09-29 16:43:04 -0700 | [diff] [blame] | 70 | binder::Status nextFabricatedOverlayInfos(int32_t iteratorId, | 
| Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 71 | std::vector<os::FabricatedOverlayInfo>* _aidl_return) override; | 
|  | 72 |  | 
| Mårten Kongstad | 99ae898 | 2021-05-10 11:00:17 +0000 | [diff] [blame] | 73 | binder::Status dumpIdmap(const std::string& overlay_path, std::string* _aidl_return) override; | 
|  | 74 |  | 
| Ryan Mitchell | 7d53f19 | 2020-04-24 17:45:25 -0700 | [diff] [blame] | 75 | private: | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 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. | 
| Yurii Zubrytskyi | 70ded19 | 2023-05-01 21:58:51 -0700 | [diff] [blame] | 78 |  | 
|  | 79 | // A cache item for the resource containers (apks or frros), with all information needed to | 
|  | 80 | // detect if it has changed since it was parsed: | 
|  | 81 | //  - (dev, inode) pair uniquely identifies a file on a particular device partition (see stat(2)). | 
|  | 82 | //  - (mtime, size) ensure the file data hasn't changed inside that file. | 
|  | 83 | struct CachedContainer { | 
|  | 84 | dev_t dev; | 
|  | 85 | ino_t inode; | 
|  | 86 | int64_t size; | 
|  | 87 | struct timespec mtime; | 
| Yurii Zubrytskyi | 3d13a4f | 2024-08-05 11:59:27 -0700 | [diff] [blame] | 88 | std::shared_ptr<idmap2::TargetResourceContainer> apk; | 
| Yurii Zubrytskyi | 70ded19 | 2023-05-01 21:58:51 -0700 | [diff] [blame] | 89 | }; | 
|  | 90 | std::unordered_map<std::string, CachedContainer> container_cache_; | 
|  | 91 | std::mutex container_cache_mutex_; | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 92 |  | 
| Yurii Zubrytskyi | de47d0b | 2022-09-29 16:43:04 -0700 | [diff] [blame] | 93 | int32_t frro_iter_id_ = 0; | 
| Ryan Mitchell | b887f68 | 2021-07-15 10:43:42 -0700 | [diff] [blame] | 94 | std::optional<std::filesystem::directory_iterator> frro_iter_; | 
| Yurii Zubrytskyi | de47d0b | 2022-09-29 16:43:04 -0700 | [diff] [blame] | 95 | std::mutex frro_iter_mutex_; | 
| Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 96 |  | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 97 | template <typename T> | 
| Yurii Zubrytskyi | 3d13a4f | 2024-08-05 11:59:27 -0700 | [diff] [blame] | 98 | using OwningPtr = std::variant<std::unique_ptr<T>, std::shared_ptr<T>>; | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 99 |  | 
| Yurii Zubrytskyi | 3d13a4f | 2024-08-05 11:59:27 -0700 | [diff] [blame] | 100 | using TargetResourceContainerPtr = OwningPtr<idmap2::TargetResourceContainer>; | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 101 | idmap2::Result<TargetResourceContainerPtr> GetTargetContainer(const std::string& target_path); | 
|  | 102 |  | 
|  | 103 | template <typename T> | 
| Yurii Zubrytskyi | 3d13a4f | 2024-08-05 11:59:27 -0700 | [diff] [blame] | 104 | WARN_UNUSED static const T* GetPointer(const OwningPtr<T>& ptr); | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 105 | }; | 
| Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 106 |  | 
|  | 107 | }  // namespace android::os | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 108 |  | 
|  | 109 | #endif  // IDMAP2_IDMAP2D_IDMAP2SERVICE_H_ |