blob: 5e5be2506567d1b67db6a1d705878bc6b8e5967d [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
17#include "BinderIncrementalService.h"
18
Songchun Fan0f8b6fe2020-02-05 17:41:25 -080019#include <android-base/logging.h>
Yurii Zubrytskyi0cd80122020-04-09 23:08:31 -070020#include <android-base/no_destructor.h>
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -070021#include <android/os/IVold.h>
Songchun Fan3c82a302019-11-29 14:23:45 -080022#include <binder/IResultReceiver.h>
Alex Buynytskyy18b07a42020-02-03 20:06:00 -080023#include <binder/PermissionCache.h>
Songchun Fan3c82a302019-11-29 14:23:45 -080024#include <incfs.h>
25
26#include "ServiceWrappers.h"
27#include "jni.h"
Songchun Fan3c82a302019-11-29 14:23:45 -080028#include "path.h"
29
30using namespace std::literals;
31using namespace android::incremental;
32
33namespace android::os::incremental {
34
35static constexpr auto kAndroidDataEnv = "ANDROID_DATA"sv;
36static constexpr auto kDataDir = "/data"sv;
37static constexpr auto kIncrementalSubDir = "incremental"sv;
38
39static std::string getIncrementalDir() {
40 const char* dataDir = getenv(kAndroidDataEnv.data());
41 if (!dataDir || !*dataDir) {
42 dataDir = kDataDir.data();
43 }
44 return path::normalize(path::join(dataDir, kIncrementalSubDir));
45}
46
47static bool incFsEnabled() {
48 // TODO(b/136132412): use vold to check /sys/fs/incfs/version (per selinux compliance)
49 return incfs::enabled();
50}
51
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080052static bool incFsValid(const sp<IVold>& vold) {
53 bool enabled = false;
54 auto status = vold->incFsEnabled(&enabled);
55 if (!status.isOk() || !enabled) {
Songchun Fan3c82a302019-11-29 14:23:45 -080056 return false;
57 }
58 return true;
59}
60
Yurii Zubrytskyi86321402020-04-09 19:22:30 -070061BinderIncrementalService::BinderIncrementalService(const sp<IServiceManager>& sm, JNIEnv* env)
62 : mImpl(RealServiceManager(sm, env), getIncrementalDir()) {}
Songchun Fan3c82a302019-11-29 14:23:45 -080063
Yurii Zubrytskyi86321402020-04-09 19:22:30 -070064BinderIncrementalService* BinderIncrementalService::start(JNIEnv* env) {
Songchun Fan3c82a302019-11-29 14:23:45 -080065 if (!incFsEnabled()) {
66 return nullptr;
67 }
68
69 IPCThreadState::self()->disableBackgroundScheduling(true);
70 sp<IServiceManager> sm(defaultServiceManager());
71 if (!sm) {
72 return nullptr;
73 }
74
75 sp<IBinder> voldBinder(sm->getService(String16("vold")));
76 if (voldBinder == nullptr) {
77 return nullptr;
78 }
79 sp<IVold> vold = interface_cast<IVold>(voldBinder);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080080 if (!incFsValid(vold)) {
Songchun Fan3c82a302019-11-29 14:23:45 -080081 return nullptr;
82 }
83
Yurii Zubrytskyi86321402020-04-09 19:22:30 -070084 sp<BinderIncrementalService> self(new BinderIncrementalService(sm, env));
Songchun Fan3c82a302019-11-29 14:23:45 -080085 status_t ret = sm->addService(String16{getServiceName()}, self);
86 if (ret != android::OK) {
87 return nullptr;
88 }
89 sp<ProcessState> ps(ProcessState::self());
90 ps->startThreadPool();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080091 // sm->addService increments the reference count, and now we're OK with returning the pointer.
Songchun Fan3c82a302019-11-29 14:23:45 -080092 return self.get();
93}
94
Alex Buynytskyy18b07a42020-02-03 20:06:00 -080095status_t BinderIncrementalService::dump(int fd, const Vector<String16>&) {
Yurii Zubrytskyi0cd80122020-04-09 23:08:31 -070096 static const android::base::NoDestructor<String16> kDump("android.permission.DUMP");
97 if (!PermissionCache::checkCallingPermission(*kDump)) {
Alex Buynytskyy18b07a42020-02-03 20:06:00 -080098 return PERMISSION_DENIED;
99 }
100 mImpl.onDump(fd);
101 return NO_ERROR;
Songchun Fan3c82a302019-11-29 14:23:45 -0800102}
103
104void BinderIncrementalService::onSystemReady() {
105 mImpl.onSystemReady();
106}
107
108static binder::Status ok() {
109 return binder::Status::ok();
110}
111
112binder::Status BinderIncrementalService::openStorage(const std::string& path,
113 int32_t* _aidl_return) {
114 *_aidl_return = mImpl.openStorage(path);
115 return ok();
116}
117
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700118binder::Status BinderIncrementalService::createStorage(
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700119 const ::std::string& path, const ::android::content::pm::DataLoaderParamsParcel& params,
Alex Buynytskyy07694ed2021-01-27 06:58:55 -0800120 int32_t createMode, int32_t* _aidl_return) {
121 *_aidl_return = mImpl.createStorage(path, params,
122 android::incremental::IncrementalService::CreateOptions(
123 createMode));
Songchun Fan3c82a302019-11-29 14:23:45 -0800124 return ok();
125}
126
127binder::Status BinderIncrementalService::createLinkedStorage(const std::string& path,
128 int32_t otherStorageId,
129 int32_t createMode,
130 int32_t* _aidl_return) {
131 *_aidl_return =
132 mImpl.createLinkedStorage(path, otherStorageId,
133 android::incremental::IncrementalService::CreateOptions(
134 createMode));
135 return ok();
136}
137
Alex Buynytskyy07694ed2021-01-27 06:58:55 -0800138binder::Status BinderIncrementalService::startLoading(
139 int32_t storageId, const ::android::content::pm::DataLoaderParamsParcel& params,
140 const ::android::sp<::android::content::pm::IDataLoaderStatusListener>& statusListener,
141 const ::android::os::incremental::StorageHealthCheckParams& healthCheckParams,
142 const ::android::sp<IStorageHealthListener>& healthListener,
143 const ::std::vector<::android::os::incremental::PerUidReadTimeouts>& perUidReadTimeouts,
144 bool* _aidl_return) {
145 *_aidl_return =
146 mImpl.startLoading(storageId, const_cast<content::pm::DataLoaderParamsParcel&&>(params),
147 statusListener,
148 const_cast<StorageHealthCheckParams&&>(healthCheckParams),
149 healthListener, perUidReadTimeouts);
150 return ok();
151}
152
Songchun Fan3c82a302019-11-29 14:23:45 -0800153binder::Status BinderIncrementalService::makeBindMount(int32_t storageId,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800154 const std::string& sourcePath,
Songchun Fan3c82a302019-11-29 14:23:45 -0800155 const std::string& targetFullPath,
156 int32_t bindType, int32_t* _aidl_return) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800157 *_aidl_return = mImpl.bind(storageId, sourcePath, targetFullPath,
Songchun Fan3c82a302019-11-29 14:23:45 -0800158 android::incremental::IncrementalService::BindKind(bindType));
159 return ok();
160}
161
162binder::Status BinderIncrementalService::deleteBindMount(int32_t storageId,
163 const std::string& targetFullPath,
164 int32_t* _aidl_return) {
165 *_aidl_return = mImpl.unbind(storageId, targetFullPath);
166 return ok();
167}
168
169binder::Status BinderIncrementalService::deleteStorage(int32_t storageId) {
170 mImpl.deleteStorage(storageId);
171 return ok();
172}
173
Alex Buynytskyyaa8e95e2020-12-14 21:50:04 -0800174binder::Status BinderIncrementalService::disallowReadLogs(int32_t storageId) {
175 mImpl.disallowReadLogs(storageId);
Alex Buynytskyy3697d9e2020-06-06 20:15:58 -0700176 return ok();
177}
178
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800179binder::Status BinderIncrementalService::makeDirectory(int32_t storageId, const std::string& path,
Songchun Fan3c82a302019-11-29 14:23:45 -0800180 int32_t* _aidl_return) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800181 *_aidl_return = mImpl.makeDir(storageId, path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800182 return ok();
183}
184
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800185static std::tuple<int, incfs::FileId, incfs::NewFileParams> toMakeFileParams(
186 const android::os::incremental::IncrementalNewFileParams& params) {
187 incfs::FileId id;
188 if (params.fileId.empty()) {
189 if (params.metadata.empty()) {
190 return {EINVAL, {}, {}};
191 }
192 id = IncrementalService::idFromMetadata(params.metadata);
193 } else if (params.fileId.size() != sizeof(id)) {
194 return {EINVAL, {}, {}};
195 } else {
196 memcpy(&id, params.fileId.data(), sizeof(id));
197 }
198 incfs::NewFileParams nfp;
199 nfp.size = params.size;
200 nfp.metadata = {(const char*)params.metadata.data(), (IncFsSize)params.metadata.size()};
201 if (!params.signature) {
Alex Buynytskyyf5e605a2020-03-13 13:31:12 -0700202 nfp.signature = {};
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800203 } else {
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700204 nfp.signature = {(const char*)params.signature->data(),
205 (IncFsSize)params.signature->size()};
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800206 }
207 return {0, id, nfp};
Songchun Fan3c82a302019-11-29 14:23:45 -0800208}
209
Alex Buynytskyyb39d13e2020-09-12 16:12:36 -0700210static std::span<const uint8_t> toSpan(const ::std::optional<::std::vector<uint8_t>>& content) {
211 if (!content) {
212 return {};
213 }
214 return {content->data(), (int)content->size()};
215}
216
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800217binder::Status BinderIncrementalService::makeFile(
218 int32_t storageId, const std::string& path,
Alex Buynytskyyb39d13e2020-09-12 16:12:36 -0700219 const ::android::os::incremental::IncrementalNewFileParams& params,
220 const ::std::optional<::std::vector<uint8_t>>& content, int32_t* _aidl_return) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800221 auto [err, fileId, nfp] = toMakeFileParams(params);
222 if (err) {
223 *_aidl_return = err;
224 return ok();
225 }
226
Alex Buynytskyyb39d13e2020-09-12 16:12:36 -0700227 *_aidl_return = mImpl.makeFile(storageId, path, 0777, fileId, nfp, toSpan(content));
Songchun Fan3c82a302019-11-29 14:23:45 -0800228 return ok();
229}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800230binder::Status BinderIncrementalService::makeFileFromRange(int32_t storageId,
231 const std::string& targetPath,
232 const std::string& sourcePath,
Songchun Fan3c82a302019-11-29 14:23:45 -0800233 int64_t start, int64_t end,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800234 int32_t* _aidl_return) {
235 // TODO(b/136132412): implement this
236 *_aidl_return = ENOSYS; // not implemented
237 return ok();
238}
239
240binder::Status BinderIncrementalService::makeLink(int32_t sourceStorageId,
241 const std::string& sourcePath,
242 int32_t destStorageId,
243 const std::string& destPath,
244 int32_t* _aidl_return) {
245 *_aidl_return = mImpl.link(sourceStorageId, sourcePath, destStorageId, destPath);
246 return ok();
247}
248
249binder::Status BinderIncrementalService::unlink(int32_t storageId, const std::string& path,
250 int32_t* _aidl_return) {
251 *_aidl_return = mImpl.unlink(storageId, path);
252 return ok();
253}
254
Alex Buynytskyybc0a7e62020-08-25 12:45:22 -0700255binder::Status BinderIncrementalService::isFileFullyLoaded(int32_t storageId,
256 const std::string& path,
257 int32_t* _aidl_return) {
Yurii Zubrytskyi256a1a42021-03-18 14:21:54 -0700258 *_aidl_return = (int)mImpl.isFileFullyLoaded(storageId, path);
Alex Buynytskyybc0a7e62020-08-25 12:45:22 -0700259 return ok();
260}
261
Alex Buynytskyy07694ed2021-01-27 06:58:55 -0800262binder::Status BinderIncrementalService::isFullyLoaded(int32_t storageId, int32_t* _aidl_return) {
Yurii Zubrytskyi256a1a42021-03-18 14:21:54 -0700263 *_aidl_return = (int)mImpl.isMountFullyLoaded(storageId);
Alex Buynytskyy07694ed2021-01-27 06:58:55 -0800264 return ok();
265}
266
Songchun Fan374f7652020-08-20 08:40:29 -0700267binder::Status BinderIncrementalService::getLoadingProgress(int32_t storageId,
268 float* _aidl_return) {
Alex Buynytskyy07694ed2021-01-27 06:58:55 -0800269 *_aidl_return =
270 mImpl.getLoadingProgress(storageId, /*stopOnFirstIncomplete=*/false).getProgress();
Songchun Fan3c82a302019-11-29 14:23:45 -0800271 return ok();
272}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800273
274binder::Status BinderIncrementalService::getMetadataByPath(int32_t storageId,
275 const std::string& path,
276 std::vector<uint8_t>* _aidl_return) {
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700277 auto metadata = mImpl.getMetadata(storageId, path);
278 _aidl_return->assign(metadata.begin(), metadata.end());
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800279 return ok();
280}
281
282static FileId toFileId(const std::vector<uint8_t>& id) {
Yurii Zubrytskyif5a6fb92021-03-18 19:29:19 -0700283 FileId fid = {};
284 memcpy(&fid, id.data(), std::min(sizeof(fid), id.size()));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800285 return fid;
286}
287
288binder::Status BinderIncrementalService::getMetadataById(int32_t storageId,
289 const std::vector<uint8_t>& id,
Songchun Fan3c82a302019-11-29 14:23:45 -0800290 std::vector<uint8_t>* _aidl_return) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800291 if (id.size() != sizeof(incfs::FileId)) {
292 return ok();
293 }
294 auto fid = toFileId(id);
295 auto metadata = mImpl.getMetadata(storageId, fid);
Songchun Fan3c82a302019-11-29 14:23:45 -0800296 _aidl_return->assign(metadata.begin(), metadata.end());
297 return ok();
298}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800299
300binder::Status BinderIncrementalService::makeDirectories(int32_t storageId, const std::string& path,
301 int32_t* _aidl_return) {
302 *_aidl_return = mImpl.makeDirs(storageId, path);
303 return ok();
304}
305
Songchun Fan0f8b6fe2020-02-05 17:41:25 -0800306binder::Status BinderIncrementalService::configureNativeBinaries(
307 int32_t storageId, const std::string& apkFullPath, const std::string& libDirRelativePath,
Songchun Fan14f6c3c2020-05-21 18:19:07 -0700308 const std::string& abi, bool extractNativeLibs, bool* _aidl_return) {
309 *_aidl_return = mImpl.configureNativeBinaries(storageId, apkFullPath, libDirRelativePath, abi,
310 extractNativeLibs);
Songchun Fan0f8b6fe2020-02-05 17:41:25 -0800311 return ok();
312}
313
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700314binder::Status BinderIncrementalService::waitForNativeBinariesExtraction(int storageId,
315 bool* _aidl_return) {
316 *_aidl_return = mImpl.waitForNativeBinariesExtraction(storageId);
317 return ok();
318}
319
Songchun Fana7098592020-09-03 11:45:53 -0700320binder::Status BinderIncrementalService::registerLoadingProgressListener(
321 int32_t storageId,
322 const ::android::sp<::android::os::incremental::IStorageLoadingProgressListener>&
323 progressListener,
324 bool* _aidl_return) {
325 *_aidl_return = mImpl.registerLoadingProgressListener(storageId, progressListener);
326 return ok();
327}
328binder::Status BinderIncrementalService::unregisterLoadingProgressListener(int32_t storageId,
329 bool* _aidl_return) {
330 *_aidl_return = mImpl.unregisterLoadingProgressListener(storageId);
331 return ok();
332}
333
Songchun Fan2570ec02020-10-08 17:22:33 -0700334binder::Status BinderIncrementalService::registerStorageHealthListener(
335 int32_t storageId,
336 const ::android::os::incremental::StorageHealthCheckParams& healthCheckParams,
337 const ::android::sp<IStorageHealthListener>& healthListener, bool* _aidl_return) {
338 *_aidl_return = mImpl.registerStorageHealthListener(storageId,
339 const_cast<StorageHealthCheckParams&&>(
340 healthCheckParams),
341 healthListener);
342 return ok();
343}
344
345binder::Status BinderIncrementalService::unregisterStorageHealthListener(int32_t storageId) {
346 mImpl.unregisterStorageHealthListener(storageId);
347 return ok();
348}
349
Songchun Fan1b76ccf2021-02-24 22:25:59 +0000350binder::Status BinderIncrementalService::getMetrics(int32_t storageId,
351 android::os::PersistableBundle* _aidl_return) {
352 mImpl.getMetrics(storageId, _aidl_return);
353 return ok();
354}
355
Songchun Fan3c82a302019-11-29 14:23:45 -0800356} // namespace android::os::incremental
357
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700358jlong Incremental_IncrementalService_Start(JNIEnv* env) {
359 return (jlong)android::os::incremental::BinderIncrementalService::start(env);
Songchun Fan3c82a302019-11-29 14:23:45 -0800360}
361void Incremental_IncrementalService_OnSystemReady(jlong self) {
362 if (self) {
363 ((android::os::incremental::BinderIncrementalService*)self)->onSystemReady();
364 }
365}
Alex Buynytskyy18b07a42020-02-03 20:06:00 -0800366void Incremental_IncrementalService_OnDump(jlong self, jint fd) {
367 if (self) {
368 ((android::os::incremental::BinderIncrementalService*)self)->dump(fd, {});
369 } else {
370 dprintf(fd, "BinderIncrementalService is stopped.");
371 }
372}