blob: 6fabc589cf9521b8630dbc42a530e5c4314c141b [file] [log] [blame]
Songchun Fan3c82a302019-11-29 14:23:45 -08001/*
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 Zubrytskyi86321402020-04-09 19:22:30 -070017#define LOG_TAG "IncrementalService"
18
Songchun Fan3c82a302019-11-29 14:23:45 -080019#include "ServiceWrappers.h"
20
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -070021#include <MountRegistry.h>
Yurii Zubrytskyi86321402020-04-09 19:22:30 -070022#include <android-base/logging.h>
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -070023#include <android/content/pm/IDataLoaderManager.h>
24#include <android/os/IVold.h>
25#include <binder/AppOpsManager.h>
Songchun Fan3c82a302019-11-29 14:23:45 -080026#include <utils/String16.h>
27
Songchun Fan374f7652020-08-20 08:40:29 -070028#include <filesystem>
Alex Buynytskyy46d3ddb2020-05-29 12:05:05 -070029#include <thread>
30
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -070031#include "IncrementalServiceValidation.h"
32
Songchun Fan3c82a302019-11-29 14:23:45 -080033using namespace std::literals;
34
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -070035namespace android::incremental {
Songchun Fan3c82a302019-11-29 14:23:45 -080036
37static constexpr auto kVoldServiceName = "vold"sv;
Songchun Fan68645c42020-02-27 15:57:35 -080038static constexpr auto kDataLoaderManagerName = "dataloader_manager"sv;
Songchun Fan3c82a302019-11-29 14:23:45 -080039
Yurii Zubrytskyi86321402020-04-09 19:22:30 -070040class RealVoldService : public VoldServiceWrapper {
41public:
Yurii Zubrytskyi510037b2020-04-22 15:46:21 -070042 RealVoldService(sp<os::IVold> vold) : mInterface(std::move(vold)) {}
Yurii Zubrytskyi86321402020-04-09 19:22:30 -070043 ~RealVoldService() = default;
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -070044 binder::Status mountIncFs(
45 const std::string& backingPath, const std::string& targetDir, int32_t flags,
46 os::incremental::IncrementalFileSystemControlParcel* _aidl_return) const final {
Yurii Zubrytskyi86321402020-04-09 19:22:30 -070047 return mInterface->mountIncFs(backingPath, targetDir, flags, _aidl_return);
48 }
49 binder::Status unmountIncFs(const std::string& dir) const final {
50 return mInterface->unmountIncFs(dir);
51 }
52 binder::Status bindMount(const std::string& sourceDir,
53 const std::string& targetDir) const final {
54 return mInterface->bindMount(sourceDir, targetDir);
55 }
56 binder::Status setIncFsMountOptions(
57 const ::android::os::incremental::IncrementalFileSystemControlParcel& control,
58 bool enableReadLogs) const final {
59 return mInterface->setIncFsMountOptions(control, enableReadLogs);
60 }
61
62private:
63 sp<os::IVold> mInterface;
64};
65
66class RealDataLoaderManager : public DataLoaderManagerWrapper {
67public:
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -070068 RealDataLoaderManager(sp<content::pm::IDataLoaderManager> manager)
69 : mInterface(std::move(manager)) {}
Yurii Zubrytskyi86321402020-04-09 19:22:30 -070070 ~RealDataLoaderManager() = default;
Alex Buynytskyyea1390f2020-04-22 16:08:50 -070071 binder::Status bindToDataLoader(MountId mountId,
72 const content::pm::DataLoaderParamsParcel& params,
73 const sp<content::pm::IDataLoaderStatusListener>& listener,
74 bool* _aidl_return) const final {
75 return mInterface->bindToDataLoader(mountId, params, listener, _aidl_return);
Yurii Zubrytskyi86321402020-04-09 19:22:30 -070076 }
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -070077 binder::Status getDataLoader(MountId mountId,
78 sp<content::pm::IDataLoader>* _aidl_return) const final {
Yurii Zubrytskyi86321402020-04-09 19:22:30 -070079 return mInterface->getDataLoader(mountId, _aidl_return);
80 }
Alex Buynytskyyea1390f2020-04-22 16:08:50 -070081 binder::Status unbindFromDataLoader(MountId mountId) const final {
82 return mInterface->unbindFromDataLoader(mountId);
Yurii Zubrytskyi86321402020-04-09 19:22:30 -070083 }
84
85private:
86 sp<content::pm::IDataLoaderManager> mInterface;
87};
88
89class RealAppOpsManager : public AppOpsManagerWrapper {
90public:
91 ~RealAppOpsManager() = default;
92 binder::Status checkPermission(const char* permission, const char* operation,
93 const char* package) const final {
94 return android::incremental::CheckPermissionForDataDelivery(permission, operation, package);
95 }
96 void startWatchingMode(int32_t op, const String16& packageName,
97 const sp<IAppOpsCallback>& callback) final {
98 mAppOpsManager.startWatchingMode(op, packageName, callback);
99 }
100 void stopWatchingMode(const sp<IAppOpsCallback>& callback) final {
101 mAppOpsManager.stopWatchingMode(callback);
102 }
103
104private:
105 android::AppOpsManager mAppOpsManager;
106};
107
108class RealJniWrapper final : public JniWrapper {
109public:
110 RealJniWrapper(JavaVM* jvm);
111 void initializeForCurrentThread() const final;
112
113 static JavaVM* getJvm(JNIEnv* env);
114
115private:
116 JavaVM* const mJvm;
117};
118
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700119class RealLooperWrapper final : public LooperWrapper {
120public:
121 int addFd(int fd, int ident, int events, android::Looper_callbackFunc callback,
122 void* data) final {
123 return mLooper.addFd(fd, ident, events, callback, data);
124 }
125 int removeFd(int fd) final { return mLooper.removeFd(fd); }
126 void wake() final { return mLooper.wake(); }
127 int pollAll(int timeoutMillis) final { return mLooper.pollAll(timeoutMillis); }
128
129private:
130 struct Looper : public android::Looper {
131 Looper() : android::Looper(/*allowNonCallbacks=*/false) {}
132 ~Looper() {}
133 } mLooper;
134};
135
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700136class RealIncFs : public IncFsWrapper {
137public:
138 RealIncFs() = default;
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700139 ~RealIncFs() final = default;
140 void listExistingMounts(const ExistingMountCallback& cb) const final {
141 for (auto mount : incfs::defaultMountRegistry().copyMounts()) {
142 auto binds = mount.binds(); // span() doesn't like rvalue containers, needs to save it.
143 cb(mount.root(), mount.backingDir(), binds);
144 }
145 }
146 Control openMount(std::string_view path) const final { return incfs::open(path); }
Yurii Zubrytskyi5f692922020-12-08 07:35:24 -0800147 Control createControl(IncFsFd cmd, IncFsFd pendingReads, IncFsFd logs,
148 IncFsFd blocksWritten) const final {
149 return incfs::createControl(cmd, pendingReads, logs, blocksWritten);
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700150 }
151 ErrorCode makeFile(const Control& control, std::string_view path, int mode, FileId id,
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700152 incfs::NewFileParams params) const final {
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700153 return incfs::makeFile(control, path, mode, id, params);
154 }
155 ErrorCode makeDir(const Control& control, std::string_view path, int mode) const final {
156 return incfs::makeDir(control, path, mode);
157 }
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700158 ErrorCode makeDirs(const Control& control, std::string_view path, int mode) const final {
159 return incfs::makeDirs(control, path, mode);
160 }
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700161 incfs::RawMetadata getMetadata(const Control& control, FileId fileid) const final {
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700162 return incfs::getMetadata(control, fileid);
163 }
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700164 incfs::RawMetadata getMetadata(const Control& control, std::string_view path) const final {
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700165 return incfs::getMetadata(control, path);
166 }
167 FileId getFileId(const Control& control, std::string_view path) const final {
168 return incfs::getFileId(control, path);
169 }
Songchun Fan6944f1e2020-11-06 15:24:24 -0800170 std::string toString(FileId fileId) const final { return incfs::toString(fileId); }
Songchun Fan374f7652020-08-20 08:40:29 -0700171 std::pair<IncFsBlockIndex, IncFsBlockIndex> countFilledBlocks(
172 const Control& control, std::string_view path) const final {
173 const auto fileId = incfs::getFileId(control, path);
174 const auto fd = incfs::openForSpecialOps(control, fileId);
175 int res = fd.get();
176 if (!fd.ok()) {
177 return {res, res};
178 }
179 const auto ranges = incfs::getFilledRanges(res);
180 res = ranges.first;
181 if (res) {
182 return {res, res};
183 }
184 const auto totalBlocksCount = ranges.second.internalRawRanges().endIndex;
185 int filledBlockCount = 0;
186 for (const auto& dataRange : ranges.second.dataRanges()) {
187 filledBlockCount += dataRange.size();
188 }
189 for (const auto& hashRange : ranges.second.hashRanges()) {
190 filledBlockCount += hashRange.size();
191 }
192 return {filledBlockCount, totalBlocksCount};
193 }
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700194 ErrorCode link(const Control& control, std::string_view from, std::string_view to) const final {
195 return incfs::link(control, from, to);
196 }
197 ErrorCode unlink(const Control& control, std::string_view path) const final {
198 return incfs::unlink(control, path);
199 }
Alex Buynytskyybc0a7e62020-08-25 12:45:22 -0700200 incfs::UniqueFd openForSpecialOps(const Control& control, FileId id) const final {
201 return incfs::openForSpecialOps(control, id);
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700202 }
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700203 ErrorCode writeBlocks(std::span<const incfs::DataBlock> blocks) const final {
204 return incfs::writeBlocks({blocks.data(), size_t(blocks.size())});
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700205 }
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700206 WaitResult waitForPendingReads(const Control& control, std::chrono::milliseconds timeout,
207 std::vector<incfs::ReadInfo>* pendingReadsBuffer) const final {
208 return incfs::waitForPendingReads(control, timeout, pendingReadsBuffer);
209 }
Alex Buynytskyyaa8e95e2020-12-14 21:50:04 -0800210 ErrorCode setUidReadTimeouts(const Control& control,
211 const std::vector<android::os::incremental::PerUidReadTimeouts>&
212 perUidReadTimeouts) const final {
213 return -ENOTSUP;
214 }
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700215};
216
Alex Buynytskyy46d3ddb2020-05-29 12:05:05 -0700217static JNIEnv* getOrAttachJniEnv(JavaVM* jvm);
218
219class RealTimedQueueWrapper : public TimedQueueWrapper {
220public:
221 RealTimedQueueWrapper(JavaVM* jvm) {
222 mThread = std::thread([this, jvm]() {
223 (void)getOrAttachJniEnv(jvm);
224 runTimers();
225 });
226 }
227 ~RealTimedQueueWrapper() final {
228 CHECK(!mRunning) << "call stop first";
229 CHECK(!mThread.joinable()) << "call stop first";
230 }
231
232 void addJob(MountId id, Milliseconds after, Job what) final {
233 const auto now = Clock::now();
234 {
235 std::unique_lock lock(mMutex);
236 mJobs.insert(TimedJob{id, now + after, std::move(what)});
237 }
238 mCondition.notify_all();
239 }
240 void removeJobs(MountId id) final {
241 std::unique_lock lock(mMutex);
242 std::erase_if(mJobs, [id](auto&& item) { return item.id == id; });
243 }
244 void stop() final {
245 {
246 std::unique_lock lock(mMutex);
247 mRunning = false;
248 }
249 mCondition.notify_all();
250 mThread.join();
251 mJobs.clear();
252 }
253
254private:
255 void runTimers() {
256 static constexpr TimePoint kInfinityTs{Clock::duration::max()};
257 TimePoint nextJobTs = kInfinityTs;
258 std::unique_lock lock(mMutex);
259 for (;;) {
260 mCondition.wait_until(lock, nextJobTs, [this, nextJobTs]() {
261 const auto now = Clock::now();
262 const auto firstJobTs = !mJobs.empty() ? mJobs.begin()->when : kInfinityTs;
263 return !mRunning || firstJobTs <= now || firstJobTs < nextJobTs;
264 });
265 if (!mRunning) {
266 return;
267 }
268
269 const auto now = Clock::now();
270 auto it = mJobs.begin();
271 // Always acquire begin(). We can't use it after unlock as mTimedJobs can change.
272 for (; it != mJobs.end() && it->when <= now; it = mJobs.begin()) {
273 auto job = std::move(it->what);
274 mJobs.erase(it);
275
276 lock.unlock();
277 job();
278 lock.lock();
279 }
280 nextJobTs = it != mJobs.end() ? it->when : kInfinityTs;
281 }
282 }
283
284 struct TimedJob {
285 MountId id;
286 TimePoint when;
287 Job what;
288 friend bool operator<(const TimedJob& lhs, const TimedJob& rhs) {
289 return lhs.when < rhs.when;
290 }
291 };
292 bool mRunning = true;
293 std::set<TimedJob> mJobs;
294 std::condition_variable mCondition;
295 std::mutex mMutex;
296 std::thread mThread;
297};
298
Songchun Fan374f7652020-08-20 08:40:29 -0700299class RealFsWrapper : public FsWrapper {
300public:
301 RealFsWrapper() = default;
302 ~RealFsWrapper() = default;
303
304 std::vector<std::string> listFilesRecursive(std::string_view directoryPath) const final {
305 std::vector<std::string> files;
306 for (const auto& entry : std::filesystem::recursive_directory_iterator(directoryPath)) {
307 if (!entry.is_regular_file()) {
308 continue;
309 }
310 files.push_back(entry.path().c_str());
311 }
312 return files;
313 }
314};
315
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700316RealServiceManager::RealServiceManager(sp<IServiceManager> serviceManager, JNIEnv* env)
317 : mServiceManager(std::move(serviceManager)), mJvm(RealJniWrapper::getJvm(env)) {}
Songchun Fan3c82a302019-11-29 14:23:45 -0800318
319template <class INTERFACE>
320sp<INTERFACE> RealServiceManager::getRealService(std::string_view serviceName) const {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800321 sp<IBinder> binder =
322 mServiceManager->getService(String16(serviceName.data(), serviceName.size()));
323 if (!binder) {
324 return nullptr;
Songchun Fan3c82a302019-11-29 14:23:45 -0800325 }
326 return interface_cast<INTERFACE>(binder);
327}
328
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800329std::unique_ptr<VoldServiceWrapper> RealServiceManager::getVoldService() {
Songchun Fan3c82a302019-11-29 14:23:45 -0800330 sp<os::IVold> vold = RealServiceManager::getRealService<os::IVold>(kVoldServiceName);
331 if (vold != 0) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800332 return std::make_unique<RealVoldService>(vold);
Songchun Fan3c82a302019-11-29 14:23:45 -0800333 }
334 return nullptr;
335}
336
Songchun Fan68645c42020-02-27 15:57:35 -0800337std::unique_ptr<DataLoaderManagerWrapper> RealServiceManager::getDataLoaderManager() {
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700338 sp<content::pm::IDataLoaderManager> manager =
339 RealServiceManager::getRealService<content::pm::IDataLoaderManager>(
340 kDataLoaderManagerName);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800341 if (manager) {
Songchun Fan68645c42020-02-27 15:57:35 -0800342 return std::make_unique<RealDataLoaderManager>(manager);
Songchun Fan3c82a302019-11-29 14:23:45 -0800343 }
344 return nullptr;
345}
346
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800347std::unique_ptr<IncFsWrapper> RealServiceManager::getIncFs() {
348 return std::make_unique<RealIncFs>();
Songchun Fan3c82a302019-11-29 14:23:45 -0800349}
350
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700351std::unique_ptr<AppOpsManagerWrapper> RealServiceManager::getAppOpsManager() {
352 return std::make_unique<RealAppOpsManager>();
353}
354
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700355std::unique_ptr<JniWrapper> RealServiceManager::getJni() {
356 return std::make_unique<RealJniWrapper>(mJvm);
357}
358
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700359std::unique_ptr<LooperWrapper> RealServiceManager::getLooper() {
360 return std::make_unique<RealLooperWrapper>();
361}
362
Alex Buynytskyy46d3ddb2020-05-29 12:05:05 -0700363std::unique_ptr<TimedQueueWrapper> RealServiceManager::getTimedQueue() {
364 return std::make_unique<RealTimedQueueWrapper>(mJvm);
365}
366
Songchun Fana7098592020-09-03 11:45:53 -0700367std::unique_ptr<TimedQueueWrapper> RealServiceManager::getProgressUpdateJobQueue() {
368 return std::make_unique<RealTimedQueueWrapper>(mJvm);
369}
370
Songchun Fan374f7652020-08-20 08:40:29 -0700371std::unique_ptr<FsWrapper> RealServiceManager::getFs() {
372 return std::make_unique<RealFsWrapper>();
373}
374
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700375static JavaVM* getJavaVm(JNIEnv* env) {
376 CHECK(env);
377 JavaVM* jvm = nullptr;
378 env->GetJavaVM(&jvm);
379 CHECK(jvm);
380 return jvm;
381}
382
383static JNIEnv* getJniEnv(JavaVM* vm) {
384 JNIEnv* env;
385 if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
386 return nullptr;
387 }
388 return env;
389}
390
391static JNIEnv* getOrAttachJniEnv(JavaVM* jvm) {
392 if (!jvm) {
393 LOG(ERROR) << "No JVM instance";
394 return nullptr;
395 }
396
397 JNIEnv* env = getJniEnv(jvm);
398 if (!env) {
399 int result = jvm->AttachCurrentThread(&env, nullptr);
400 if (result != JNI_OK) {
401 LOG(ERROR) << "JVM thread attach failed: " << result;
402 return nullptr;
403 }
404 struct VmDetacher {
405 VmDetacher(JavaVM* vm) : mVm(vm) {}
406 ~VmDetacher() { mVm->DetachCurrentThread(); }
407
408 private:
409 JavaVM* const mVm;
410 };
411 static thread_local VmDetacher detacher(jvm);
412 }
413
414 return env;
415}
416
417RealJniWrapper::RealJniWrapper(JavaVM* jvm) : mJvm(jvm) {
418 CHECK(!!mJvm) << "JVM is unavailable";
419}
420
421void RealJniWrapper::initializeForCurrentThread() const {
422 (void)getOrAttachJniEnv(mJvm);
423}
424
425JavaVM* RealJniWrapper::getJvm(JNIEnv* env) {
426 return getJavaVm(env);
427}
428
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700429} // namespace android::incremental