Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 17 | #define LOG_TAG "IncrementalService" |
| 18 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 19 | #include "ServiceWrappers.h" |
| 20 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 21 | #include <MountRegistry.h> |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 22 | #include <android-base/logging.h> |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 23 | #include <android/content/pm/IDataLoaderManager.h> |
| 24 | #include <android/os/IVold.h> |
| 25 | #include <binder/AppOpsManager.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 26 | #include <utils/String16.h> |
| 27 | |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 28 | #include <filesystem> |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 29 | #include <thread> |
| 30 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 31 | #include "IncrementalServiceValidation.h" |
| 32 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 33 | using namespace std::literals; |
| 34 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 35 | namespace android::incremental { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 36 | |
| 37 | static constexpr auto kVoldServiceName = "vold"sv; |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 38 | static constexpr auto kDataLoaderManagerName = "dataloader_manager"sv; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 39 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 40 | class RealVoldService : public VoldServiceWrapper { |
| 41 | public: |
Yurii Zubrytskyi | 510037b | 2020-04-22 15:46:21 -0700 | [diff] [blame] | 42 | RealVoldService(sp<os::IVold> vold) : mInterface(std::move(vold)) {} |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 43 | ~RealVoldService() = default; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 44 | binder::Status mountIncFs( |
| 45 | const std::string& backingPath, const std::string& targetDir, int32_t flags, |
Songchun Fan | f949c37 | 2021-04-27 11:26:25 -0700 | [diff] [blame] | 46 | const std::string& sysfsName, |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 47 | os::incremental::IncrementalFileSystemControlParcel* _aidl_return) const final { |
Songchun Fan | f949c37 | 2021-04-27 11:26:25 -0700 | [diff] [blame] | 48 | return mInterface->mountIncFs(backingPath, targetDir, flags, sysfsName, _aidl_return); |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 49 | } |
| 50 | binder::Status unmountIncFs(const std::string& dir) const final { |
| 51 | return mInterface->unmountIncFs(dir); |
| 52 | } |
| 53 | binder::Status bindMount(const std::string& sourceDir, |
| 54 | const std::string& targetDir) const final { |
| 55 | return mInterface->bindMount(sourceDir, targetDir); |
| 56 | } |
| 57 | binder::Status setIncFsMountOptions( |
| 58 | const ::android::os::incremental::IncrementalFileSystemControlParcel& control, |
Songchun Fan | f6c65bb | 2021-05-10 16:17:30 -0700 | [diff] [blame] | 59 | bool enableReadLogs, bool enableReadTimeouts, |
| 60 | const std::string& sysfsName) const final { |
| 61 | return mInterface->setIncFsMountOptions(control, enableReadLogs, enableReadTimeouts, |
| 62 | sysfsName); |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | private: |
| 66 | sp<os::IVold> mInterface; |
| 67 | }; |
| 68 | |
| 69 | class RealDataLoaderManager : public DataLoaderManagerWrapper { |
| 70 | public: |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 71 | RealDataLoaderManager(sp<content::pm::IDataLoaderManager> manager) |
| 72 | : mInterface(std::move(manager)) {} |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 73 | ~RealDataLoaderManager() = default; |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame] | 74 | binder::Status bindToDataLoader(MountId mountId, |
| 75 | const content::pm::DataLoaderParamsParcel& params, |
Alex Buynytskyy | b19ee3e | 2021-02-06 20:31:43 -0800 | [diff] [blame] | 76 | int bindDelayMs, |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame] | 77 | const sp<content::pm::IDataLoaderStatusListener>& listener, |
| 78 | bool* _aidl_return) const final { |
Alex Buynytskyy | b19ee3e | 2021-02-06 20:31:43 -0800 | [diff] [blame] | 79 | return mInterface->bindToDataLoader(mountId, params, bindDelayMs, listener, _aidl_return); |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 80 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 81 | binder::Status getDataLoader(MountId mountId, |
| 82 | sp<content::pm::IDataLoader>* _aidl_return) const final { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 83 | return mInterface->getDataLoader(mountId, _aidl_return); |
| 84 | } |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame] | 85 | binder::Status unbindFromDataLoader(MountId mountId) const final { |
| 86 | return mInterface->unbindFromDataLoader(mountId); |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | private: |
| 90 | sp<content::pm::IDataLoaderManager> mInterface; |
| 91 | }; |
| 92 | |
| 93 | class RealAppOpsManager : public AppOpsManagerWrapper { |
| 94 | public: |
| 95 | ~RealAppOpsManager() = default; |
| 96 | binder::Status checkPermission(const char* permission, const char* operation, |
| 97 | const char* package) const final { |
| 98 | return android::incremental::CheckPermissionForDataDelivery(permission, operation, package); |
| 99 | } |
| 100 | void startWatchingMode(int32_t op, const String16& packageName, |
| 101 | const sp<IAppOpsCallback>& callback) final { |
| 102 | mAppOpsManager.startWatchingMode(op, packageName, callback); |
| 103 | } |
| 104 | void stopWatchingMode(const sp<IAppOpsCallback>& callback) final { |
| 105 | mAppOpsManager.stopWatchingMode(callback); |
| 106 | } |
| 107 | |
| 108 | private: |
| 109 | android::AppOpsManager mAppOpsManager; |
| 110 | }; |
| 111 | |
| 112 | class RealJniWrapper final : public JniWrapper { |
| 113 | public: |
| 114 | RealJniWrapper(JavaVM* jvm); |
| 115 | void initializeForCurrentThread() const final; |
| 116 | |
| 117 | static JavaVM* getJvm(JNIEnv* env); |
| 118 | |
| 119 | private: |
| 120 | JavaVM* const mJvm; |
| 121 | }; |
| 122 | |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 123 | class RealLooperWrapper final : public LooperWrapper { |
| 124 | public: |
| 125 | int addFd(int fd, int ident, int events, android::Looper_callbackFunc callback, |
| 126 | void* data) final { |
| 127 | return mLooper.addFd(fd, ident, events, callback, data); |
| 128 | } |
| 129 | int removeFd(int fd) final { return mLooper.removeFd(fd); } |
| 130 | void wake() final { return mLooper.wake(); } |
| 131 | int pollAll(int timeoutMillis) final { return mLooper.pollAll(timeoutMillis); } |
| 132 | |
| 133 | private: |
| 134 | struct Looper : public android::Looper { |
| 135 | Looper() : android::Looper(/*allowNonCallbacks=*/false) {} |
| 136 | ~Looper() {} |
| 137 | } mLooper; |
| 138 | }; |
| 139 | |
Yurii Zubrytskyi | 4375a74 | 2021-03-18 16:59:47 -0700 | [diff] [blame] | 140 | std::string IncFsWrapper::toString(FileId fileId) { |
| 141 | return incfs::toString(fileId); |
| 142 | } |
| 143 | |
Yurii Zubrytskyi | a5946f7 | 2021-02-17 14:24:14 -0800 | [diff] [blame] | 144 | class RealIncFs final : public IncFsWrapper { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 145 | public: |
| 146 | RealIncFs() = default; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 147 | ~RealIncFs() final = default; |
Yurii Zubrytskyi | a5946f7 | 2021-02-17 14:24:14 -0800 | [diff] [blame] | 148 | Features features() const final { return incfs::features(); } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 149 | void listExistingMounts(const ExistingMountCallback& cb) const final { |
| 150 | for (auto mount : incfs::defaultMountRegistry().copyMounts()) { |
| 151 | auto binds = mount.binds(); // span() doesn't like rvalue containers, needs to save it. |
| 152 | cb(mount.root(), mount.backingDir(), binds); |
| 153 | } |
| 154 | } |
| 155 | Control openMount(std::string_view path) const final { return incfs::open(path); } |
Yurii Zubrytskyi | 5f69292 | 2020-12-08 07:35:24 -0800 | [diff] [blame] | 156 | Control createControl(IncFsFd cmd, IncFsFd pendingReads, IncFsFd logs, |
| 157 | IncFsFd blocksWritten) const final { |
| 158 | return incfs::createControl(cmd, pendingReads, logs, blocksWritten); |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 159 | } |
| 160 | ErrorCode makeFile(const Control& control, std::string_view path, int mode, FileId id, |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 161 | incfs::NewFileParams params) const final { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 162 | return incfs::makeFile(control, path, mode, id, params); |
| 163 | } |
Yurii Zubrytskyi | a5946f7 | 2021-02-17 14:24:14 -0800 | [diff] [blame] | 164 | ErrorCode makeMappedFile(const Control& control, std::string_view path, int mode, |
| 165 | incfs::NewMappedFileParams params) const final { |
| 166 | return incfs::makeMappedFile(control, path, mode, params); |
| 167 | } |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 168 | ErrorCode makeDir(const Control& control, std::string_view path, int mode) const final { |
| 169 | return incfs::makeDir(control, path, mode); |
| 170 | } |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 171 | ErrorCode makeDirs(const Control& control, std::string_view path, int mode) const final { |
| 172 | return incfs::makeDirs(control, path, mode); |
| 173 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 174 | incfs::RawMetadata getMetadata(const Control& control, FileId fileid) const final { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 175 | return incfs::getMetadata(control, fileid); |
| 176 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 177 | incfs::RawMetadata getMetadata(const Control& control, std::string_view path) const final { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 178 | return incfs::getMetadata(control, path); |
| 179 | } |
| 180 | FileId getFileId(const Control& control, std::string_view path) const final { |
| 181 | return incfs::getFileId(control, path); |
| 182 | } |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 183 | std::pair<IncFsBlockIndex, IncFsBlockIndex> countFilledBlocks( |
| 184 | const Control& control, std::string_view path) const final { |
Yurii Zubrytskyi | 4375a74 | 2021-03-18 16:59:47 -0700 | [diff] [blame] | 185 | if (incfs::features() & Features::v2) { |
| 186 | const auto counts = incfs::getBlockCount(control, path); |
| 187 | if (!counts) { |
| 188 | return {-errno, -errno}; |
| 189 | } |
| 190 | return {counts->filledDataBlocks + counts->filledHashBlocks, |
| 191 | counts->totalDataBlocks + counts->totalHashBlocks}; |
| 192 | } |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 193 | const auto fileId = incfs::getFileId(control, path); |
| 194 | const auto fd = incfs::openForSpecialOps(control, fileId); |
| 195 | int res = fd.get(); |
| 196 | if (!fd.ok()) { |
| 197 | return {res, res}; |
| 198 | } |
| 199 | const auto ranges = incfs::getFilledRanges(res); |
| 200 | res = ranges.first; |
| 201 | if (res) { |
| 202 | return {res, res}; |
| 203 | } |
| 204 | const auto totalBlocksCount = ranges.second.internalRawRanges().endIndex; |
| 205 | int filledBlockCount = 0; |
| 206 | for (const auto& dataRange : ranges.second.dataRanges()) { |
| 207 | filledBlockCount += dataRange.size(); |
| 208 | } |
| 209 | for (const auto& hashRange : ranges.second.hashRanges()) { |
| 210 | filledBlockCount += hashRange.size(); |
| 211 | } |
| 212 | return {filledBlockCount, totalBlocksCount}; |
| 213 | } |
Yurii Zubrytskyi | 256a1a4 | 2021-03-18 14:21:54 -0700 | [diff] [blame] | 214 | incfs::LoadingState isFileFullyLoaded(const Control& control, |
| 215 | std::string_view path) const final { |
| 216 | return incfs::isFullyLoaded(control, path); |
| 217 | } |
Yurii Zubrytskyi | 4cd2492 | 2021-03-24 00:46:29 -0700 | [diff] [blame] | 218 | incfs::LoadingState isFileFullyLoaded(const Control& control, FileId id) const final { |
| 219 | return incfs::isFullyLoaded(control, id); |
| 220 | } |
Yurii Zubrytskyi | 256a1a4 | 2021-03-18 14:21:54 -0700 | [diff] [blame] | 221 | incfs::LoadingState isEverythingFullyLoaded(const Control& control) const final { |
| 222 | return incfs::isEverythingFullyLoaded(control); |
| 223 | } |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 224 | ErrorCode link(const Control& control, std::string_view from, std::string_view to) const final { |
| 225 | return incfs::link(control, from, to); |
| 226 | } |
| 227 | ErrorCode unlink(const Control& control, std::string_view path) const final { |
| 228 | return incfs::unlink(control, path); |
| 229 | } |
Alex Buynytskyy | bc0a7e6 | 2020-08-25 12:45:22 -0700 | [diff] [blame] | 230 | incfs::UniqueFd openForSpecialOps(const Control& control, FileId id) const final { |
| 231 | return incfs::openForSpecialOps(control, id); |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 232 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 233 | ErrorCode writeBlocks(std::span<const incfs::DataBlock> blocks) const final { |
| 234 | return incfs::writeBlocks({blocks.data(), size_t(blocks.size())}); |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 235 | } |
Yurii Zubrytskyi | 4cd2492 | 2021-03-24 00:46:29 -0700 | [diff] [blame] | 236 | ErrorCode reserveSpace(const Control& control, FileId id, IncFsSize size) const final { |
| 237 | return incfs::reserveSpace(control, id, size); |
Yurii Zubrytskyi | 65fc38a | 2021-03-17 13:18:30 -0700 | [diff] [blame] | 238 | } |
Alex Buynytskyy | c144cc4 | 2021-03-31 22:19:42 -0700 | [diff] [blame] | 239 | WaitResult waitForPendingReads( |
| 240 | const Control& control, std::chrono::milliseconds timeout, |
| 241 | std::vector<incfs::ReadInfoWithUid>* pendingReadsBuffer) const final { |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 242 | return incfs::waitForPendingReads(control, timeout, pendingReadsBuffer); |
| 243 | } |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 244 | ErrorCode setUidReadTimeouts(const Control& control, |
| 245 | const std::vector<android::os::incremental::PerUidReadTimeouts>& |
| 246 | perUidReadTimeouts) const final { |
Yurii Zubrytskyi | 4cd2492 | 2021-03-24 00:46:29 -0700 | [diff] [blame] | 247 | std::vector<incfs::UidReadTimeouts> timeouts(perUidReadTimeouts.size()); |
Alex Buynytskyy | fe6b4c0 | 2021-01-26 13:29:24 -0800 | [diff] [blame] | 248 | for (int i = 0, size = perUidReadTimeouts.size(); i < size; ++i) { |
Yurii Zubrytskyi | 4cd2492 | 2021-03-24 00:46:29 -0700 | [diff] [blame] | 249 | auto& timeout = timeouts[i]; |
Alex Buynytskyy | fe6b4c0 | 2021-01-26 13:29:24 -0800 | [diff] [blame] | 250 | const auto& perUidTimeout = perUidReadTimeouts[i]; |
| 251 | timeout.uid = perUidTimeout.uid; |
| 252 | timeout.minTimeUs = perUidTimeout.minTimeUs; |
| 253 | timeout.minPendingTimeUs = perUidTimeout.minPendingTimeUs; |
| 254 | timeout.maxPendingTimeUs = perUidTimeout.maxPendingTimeUs; |
| 255 | } |
| 256 | return incfs::setUidReadTimeouts(control, timeouts); |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 257 | } |
Yurii Zubrytskyi | 4cd2492 | 2021-03-24 00:46:29 -0700 | [diff] [blame] | 258 | ErrorCode forEachFile(const Control& control, FileCallback cb) const final { |
| 259 | return incfs::forEachFile(control, |
| 260 | [&](auto& control, FileId id) { return cb(control, id); }); |
| 261 | } |
| 262 | ErrorCode forEachIncompleteFile(const Control& control, FileCallback cb) const final { |
| 263 | return incfs::forEachIncompleteFile(control, [&](auto& control, FileId id) { |
| 264 | return cb(control, id); |
| 265 | }); |
| 266 | } |
Songchun Fan | f949c37 | 2021-04-27 11:26:25 -0700 | [diff] [blame] | 267 | std::optional<Metrics> getMetrics(std::string_view sysfsName) const final { |
| 268 | return incfs::getMetrics(sysfsName); |
| 269 | } |
Songchun Fan | d48a25e | 2021-04-30 09:50:58 -0700 | [diff] [blame] | 270 | std::optional<LastReadError> getLastReadError(const Control& control) const final { |
| 271 | return incfs::getLastReadError(control); |
| 272 | } |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 273 | }; |
| 274 | |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 275 | static JNIEnv* getOrAttachJniEnv(JavaVM* jvm); |
| 276 | |
Yurii Zubrytskyi | 0583e7f | 2021-03-19 17:00:12 -0700 | [diff] [blame] | 277 | class RealTimedQueueWrapper final : public TimedQueueWrapper { |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 278 | public: |
| 279 | RealTimedQueueWrapper(JavaVM* jvm) { |
| 280 | mThread = std::thread([this, jvm]() { |
| 281 | (void)getOrAttachJniEnv(jvm); |
| 282 | runTimers(); |
| 283 | }); |
| 284 | } |
| 285 | ~RealTimedQueueWrapper() final { |
| 286 | CHECK(!mRunning) << "call stop first"; |
| 287 | CHECK(!mThread.joinable()) << "call stop first"; |
| 288 | } |
| 289 | |
Yurii Zubrytskyi | 0583e7f | 2021-03-19 17:00:12 -0700 | [diff] [blame] | 290 | void addJob(MountId id, Milliseconds timeout, Job what) final { |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 291 | const auto now = Clock::now(); |
| 292 | { |
| 293 | std::unique_lock lock(mMutex); |
Yurii Zubrytskyi | 0583e7f | 2021-03-19 17:00:12 -0700 | [diff] [blame] | 294 | mJobs.insert(TimedJob{id, now + timeout, std::move(what)}); |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 295 | } |
| 296 | mCondition.notify_all(); |
| 297 | } |
| 298 | void removeJobs(MountId id) final { |
| 299 | std::unique_lock lock(mMutex); |
| 300 | std::erase_if(mJobs, [id](auto&& item) { return item.id == id; }); |
| 301 | } |
| 302 | void stop() final { |
| 303 | { |
| 304 | std::unique_lock lock(mMutex); |
| 305 | mRunning = false; |
| 306 | } |
| 307 | mCondition.notify_all(); |
| 308 | mThread.join(); |
| 309 | mJobs.clear(); |
| 310 | } |
| 311 | |
| 312 | private: |
| 313 | void runTimers() { |
| 314 | static constexpr TimePoint kInfinityTs{Clock::duration::max()}; |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 315 | std::unique_lock lock(mMutex); |
| 316 | for (;;) { |
Yurii Zubrytskyi | 0583e7f | 2021-03-19 17:00:12 -0700 | [diff] [blame] | 317 | const TimePoint nextJobTs = mJobs.empty() ? kInfinityTs : mJobs.begin()->when; |
| 318 | mCondition.wait_until(lock, nextJobTs, [this, oldNextJobTs = nextJobTs]() { |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 319 | const auto now = Clock::now(); |
Yurii Zubrytskyi | 0583e7f | 2021-03-19 17:00:12 -0700 | [diff] [blame] | 320 | const auto newFirstJobTs = !mJobs.empty() ? mJobs.begin()->when : kInfinityTs; |
| 321 | return newFirstJobTs <= now || newFirstJobTs < oldNextJobTs || !mRunning; |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 322 | }); |
| 323 | if (!mRunning) { |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | const auto now = Clock::now(); |
Yurii Zubrytskyi | 0583e7f | 2021-03-19 17:00:12 -0700 | [diff] [blame] | 328 | // Always re-acquire begin(). We can't use it after unlock as mTimedJobs can change. |
| 329 | for (auto it = mJobs.begin(); it != mJobs.end() && it->when <= now; |
| 330 | it = mJobs.begin()) { |
Yurii Zubrytskyi | 3fde572 | 2021-02-19 00:08:36 -0800 | [diff] [blame] | 331 | auto jobNode = mJobs.extract(it); |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 332 | |
| 333 | lock.unlock(); |
Yurii Zubrytskyi | 3fde572 | 2021-02-19 00:08:36 -0800 | [diff] [blame] | 334 | jobNode.value().what(); |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 335 | lock.lock(); |
| 336 | } |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
| 340 | struct TimedJob { |
| 341 | MountId id; |
| 342 | TimePoint when; |
| 343 | Job what; |
| 344 | friend bool operator<(const TimedJob& lhs, const TimedJob& rhs) { |
| 345 | return lhs.when < rhs.when; |
| 346 | } |
| 347 | }; |
| 348 | bool mRunning = true; |
Yurii Zubrytskyi | 0583e7f | 2021-03-19 17:00:12 -0700 | [diff] [blame] | 349 | std::multiset<TimedJob> mJobs; |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 350 | std::condition_variable mCondition; |
| 351 | std::mutex mMutex; |
| 352 | std::thread mThread; |
| 353 | }; |
| 354 | |
Yurii Zubrytskyi | 3fde572 | 2021-02-19 00:08:36 -0800 | [diff] [blame] | 355 | class RealFsWrapper final : public FsWrapper { |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 356 | public: |
| 357 | RealFsWrapper() = default; |
| 358 | ~RealFsWrapper() = default; |
| 359 | |
Yurii Zubrytskyi | 3fde572 | 2021-02-19 00:08:36 -0800 | [diff] [blame] | 360 | void listFilesRecursive(std::string_view directoryPath, FileCallback onFile) const final { |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 361 | for (const auto& entry : std::filesystem::recursive_directory_iterator(directoryPath)) { |
| 362 | if (!entry.is_regular_file()) { |
| 363 | continue; |
| 364 | } |
Yurii Zubrytskyi | 3fde572 | 2021-02-19 00:08:36 -0800 | [diff] [blame] | 365 | if (!onFile(entry.path().native())) { |
| 366 | break; |
| 367 | } |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 368 | } |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 369 | } |
| 370 | }; |
| 371 | |
Alex Buynytskyy | 7e06d71 | 2021-03-09 19:24:23 -0800 | [diff] [blame] | 372 | class RealClockWrapper final : public ClockWrapper { |
| 373 | public: |
| 374 | RealClockWrapper() = default; |
| 375 | ~RealClockWrapper() = default; |
| 376 | |
| 377 | TimePoint now() const final { return Clock::now(); } |
| 378 | }; |
| 379 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 380 | RealServiceManager::RealServiceManager(sp<IServiceManager> serviceManager, JNIEnv* env) |
| 381 | : mServiceManager(std::move(serviceManager)), mJvm(RealJniWrapper::getJvm(env)) {} |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 382 | |
| 383 | template <class INTERFACE> |
| 384 | sp<INTERFACE> RealServiceManager::getRealService(std::string_view serviceName) const { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 385 | sp<IBinder> binder = |
| 386 | mServiceManager->getService(String16(serviceName.data(), serviceName.size())); |
| 387 | if (!binder) { |
| 388 | return nullptr; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 389 | } |
| 390 | return interface_cast<INTERFACE>(binder); |
| 391 | } |
| 392 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 393 | std::unique_ptr<VoldServiceWrapper> RealServiceManager::getVoldService() { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 394 | sp<os::IVold> vold = RealServiceManager::getRealService<os::IVold>(kVoldServiceName); |
| 395 | if (vold != 0) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 396 | return std::make_unique<RealVoldService>(vold); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 397 | } |
| 398 | return nullptr; |
| 399 | } |
| 400 | |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 401 | std::unique_ptr<DataLoaderManagerWrapper> RealServiceManager::getDataLoaderManager() { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 402 | sp<content::pm::IDataLoaderManager> manager = |
| 403 | RealServiceManager::getRealService<content::pm::IDataLoaderManager>( |
| 404 | kDataLoaderManagerName); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 405 | if (manager) { |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 406 | return std::make_unique<RealDataLoaderManager>(manager); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 407 | } |
| 408 | return nullptr; |
| 409 | } |
| 410 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 411 | std::unique_ptr<IncFsWrapper> RealServiceManager::getIncFs() { |
| 412 | return std::make_unique<RealIncFs>(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 413 | } |
| 414 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 415 | std::unique_ptr<AppOpsManagerWrapper> RealServiceManager::getAppOpsManager() { |
| 416 | return std::make_unique<RealAppOpsManager>(); |
| 417 | } |
| 418 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 419 | std::unique_ptr<JniWrapper> RealServiceManager::getJni() { |
| 420 | return std::make_unique<RealJniWrapper>(mJvm); |
| 421 | } |
| 422 | |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 423 | std::unique_ptr<LooperWrapper> RealServiceManager::getLooper() { |
| 424 | return std::make_unique<RealLooperWrapper>(); |
| 425 | } |
| 426 | |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 427 | std::unique_ptr<TimedQueueWrapper> RealServiceManager::getTimedQueue() { |
| 428 | return std::make_unique<RealTimedQueueWrapper>(mJvm); |
| 429 | } |
| 430 | |
Songchun Fan | a709859 | 2020-09-03 11:45:53 -0700 | [diff] [blame] | 431 | std::unique_ptr<TimedQueueWrapper> RealServiceManager::getProgressUpdateJobQueue() { |
| 432 | return std::make_unique<RealTimedQueueWrapper>(mJvm); |
| 433 | } |
| 434 | |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 435 | std::unique_ptr<FsWrapper> RealServiceManager::getFs() { |
| 436 | return std::make_unique<RealFsWrapper>(); |
| 437 | } |
| 438 | |
Alex Buynytskyy | 7e06d71 | 2021-03-09 19:24:23 -0800 | [diff] [blame] | 439 | std::unique_ptr<ClockWrapper> RealServiceManager::getClock() { |
| 440 | return std::make_unique<RealClockWrapper>(); |
| 441 | } |
| 442 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 443 | static JavaVM* getJavaVm(JNIEnv* env) { |
| 444 | CHECK(env); |
| 445 | JavaVM* jvm = nullptr; |
| 446 | env->GetJavaVM(&jvm); |
| 447 | CHECK(jvm); |
| 448 | return jvm; |
| 449 | } |
| 450 | |
| 451 | static JNIEnv* getJniEnv(JavaVM* vm) { |
| 452 | JNIEnv* env; |
| 453 | if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) { |
| 454 | return nullptr; |
| 455 | } |
| 456 | return env; |
| 457 | } |
| 458 | |
| 459 | static JNIEnv* getOrAttachJniEnv(JavaVM* jvm) { |
| 460 | if (!jvm) { |
| 461 | LOG(ERROR) << "No JVM instance"; |
| 462 | return nullptr; |
| 463 | } |
| 464 | |
| 465 | JNIEnv* env = getJniEnv(jvm); |
| 466 | if (!env) { |
| 467 | int result = jvm->AttachCurrentThread(&env, nullptr); |
| 468 | if (result != JNI_OK) { |
| 469 | LOG(ERROR) << "JVM thread attach failed: " << result; |
| 470 | return nullptr; |
| 471 | } |
| 472 | struct VmDetacher { |
| 473 | VmDetacher(JavaVM* vm) : mVm(vm) {} |
| 474 | ~VmDetacher() { mVm->DetachCurrentThread(); } |
| 475 | |
| 476 | private: |
| 477 | JavaVM* const mVm; |
| 478 | }; |
| 479 | static thread_local VmDetacher detacher(jvm); |
| 480 | } |
| 481 | |
| 482 | return env; |
| 483 | } |
| 484 | |
| 485 | RealJniWrapper::RealJniWrapper(JavaVM* jvm) : mJvm(jvm) { |
| 486 | CHECK(!!mJvm) << "JVM is unavailable"; |
| 487 | } |
| 488 | |
| 489 | void RealJniWrapper::initializeForCurrentThread() const { |
| 490 | (void)getOrAttachJniEnv(mJvm); |
| 491 | } |
| 492 | |
| 493 | JavaVM* RealJniWrapper::getJvm(JNIEnv* env) { |
| 494 | return getJavaVm(env); |
| 495 | } |
| 496 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 497 | } // namespace android::incremental |