blob: b2efc71860fb9ce65800a1f062797234293412a1 [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();
91 ps->giveThreadPoolName();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080092 // sm->addService increments the reference count, and now we're OK with returning the pointer.
Songchun Fan3c82a302019-11-29 14:23:45 -080093 return self.get();
94}
95
Alex Buynytskyy18b07a42020-02-03 20:06:00 -080096status_t BinderIncrementalService::dump(int fd, const Vector<String16>&) {
Yurii Zubrytskyi0cd80122020-04-09 23:08:31 -070097 static const android::base::NoDestructor<String16> kDump("android.permission.DUMP");
98 if (!PermissionCache::checkCallingPermission(*kDump)) {
Alex Buynytskyy18b07a42020-02-03 20:06:00 -080099 return PERMISSION_DENIED;
100 }
101 mImpl.onDump(fd);
102 return NO_ERROR;
Songchun Fan3c82a302019-11-29 14:23:45 -0800103}
104
105void BinderIncrementalService::onSystemReady() {
106 mImpl.onSystemReady();
107}
108
109static binder::Status ok() {
110 return binder::Status::ok();
111}
112
113binder::Status BinderIncrementalService::openStorage(const std::string& path,
114 int32_t* _aidl_return) {
115 *_aidl_return = mImpl.openStorage(path);
116 return ok();
117}
118
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700119binder::Status BinderIncrementalService::createStorage(
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700120 const ::std::string& path, const ::android::content::pm::DataLoaderParamsParcel& params,
Alex Buynytskyy07694ed2021-01-27 06:58:55 -0800121 int32_t createMode, int32_t* _aidl_return) {
122 *_aidl_return = mImpl.createStorage(path, params,
123 android::incremental::IncrementalService::CreateOptions(
124 createMode));
Songchun Fan3c82a302019-11-29 14:23:45 -0800125 return ok();
126}
127
128binder::Status BinderIncrementalService::createLinkedStorage(const std::string& path,
129 int32_t otherStorageId,
130 int32_t createMode,
131 int32_t* _aidl_return) {
132 *_aidl_return =
133 mImpl.createLinkedStorage(path, otherStorageId,
134 android::incremental::IncrementalService::CreateOptions(
135 createMode));
136 return ok();
137}
138
Alex Buynytskyy07694ed2021-01-27 06:58:55 -0800139binder::Status BinderIncrementalService::startLoading(
140 int32_t storageId, const ::android::content::pm::DataLoaderParamsParcel& params,
141 const ::android::sp<::android::content::pm::IDataLoaderStatusListener>& statusListener,
142 const ::android::os::incremental::StorageHealthCheckParams& healthCheckParams,
143 const ::android::sp<IStorageHealthListener>& healthListener,
144 const ::std::vector<::android::os::incremental::PerUidReadTimeouts>& perUidReadTimeouts,
145 bool* _aidl_return) {
146 *_aidl_return =
147 mImpl.startLoading(storageId, const_cast<content::pm::DataLoaderParamsParcel&&>(params),
148 statusListener,
149 const_cast<StorageHealthCheckParams&&>(healthCheckParams),
150 healthListener, perUidReadTimeouts);
151 return ok();
152}
153
Songchun Fan3c82a302019-11-29 14:23:45 -0800154binder::Status BinderIncrementalService::makeBindMount(int32_t storageId,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800155 const std::string& sourcePath,
Songchun Fan3c82a302019-11-29 14:23:45 -0800156 const std::string& targetFullPath,
157 int32_t bindType, int32_t* _aidl_return) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800158 *_aidl_return = mImpl.bind(storageId, sourcePath, targetFullPath,
Songchun Fan3c82a302019-11-29 14:23:45 -0800159 android::incremental::IncrementalService::BindKind(bindType));
160 return ok();
161}
162
163binder::Status BinderIncrementalService::deleteBindMount(int32_t storageId,
164 const std::string& targetFullPath,
165 int32_t* _aidl_return) {
166 *_aidl_return = mImpl.unbind(storageId, targetFullPath);
167 return ok();
168}
169
170binder::Status BinderIncrementalService::deleteStorage(int32_t storageId) {
171 mImpl.deleteStorage(storageId);
172 return ok();
173}
174
Alex Buynytskyyaa8e95e2020-12-14 21:50:04 -0800175binder::Status BinderIncrementalService::disallowReadLogs(int32_t storageId) {
176 mImpl.disallowReadLogs(storageId);
Alex Buynytskyy3697d9e2020-06-06 20:15:58 -0700177 return ok();
178}
179
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800180binder::Status BinderIncrementalService::makeDirectory(int32_t storageId, const std::string& path,
Songchun Fan3c82a302019-11-29 14:23:45 -0800181 int32_t* _aidl_return) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800182 *_aidl_return = mImpl.makeDir(storageId, path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800183 return ok();
184}
185
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800186static std::tuple<int, incfs::FileId, incfs::NewFileParams> toMakeFileParams(
187 const android::os::incremental::IncrementalNewFileParams& params) {
188 incfs::FileId id;
189 if (params.fileId.empty()) {
190 if (params.metadata.empty()) {
191 return {EINVAL, {}, {}};
192 }
193 id = IncrementalService::idFromMetadata(params.metadata);
194 } else if (params.fileId.size() != sizeof(id)) {
195 return {EINVAL, {}, {}};
196 } else {
197 memcpy(&id, params.fileId.data(), sizeof(id));
198 }
199 incfs::NewFileParams nfp;
200 nfp.size = params.size;
201 nfp.metadata = {(const char*)params.metadata.data(), (IncFsSize)params.metadata.size()};
202 if (!params.signature) {
Alex Buynytskyyf5e605a2020-03-13 13:31:12 -0700203 nfp.signature = {};
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800204 } else {
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700205 nfp.signature = {(const char*)params.signature->data(),
206 (IncFsSize)params.signature->size()};
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800207 }
208 return {0, id, nfp};
Songchun Fan3c82a302019-11-29 14:23:45 -0800209}
210
Alex Buynytskyyb39d13e2020-09-12 16:12:36 -0700211static std::span<const uint8_t> toSpan(const ::std::optional<::std::vector<uint8_t>>& content) {
212 if (!content) {
213 return {};
214 }
215 return {content->data(), (int)content->size()};
216}
217
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800218binder::Status BinderIncrementalService::makeFile(
219 int32_t storageId, const std::string& path,
Alex Buynytskyyb39d13e2020-09-12 16:12:36 -0700220 const ::android::os::incremental::IncrementalNewFileParams& params,
221 const ::std::optional<::std::vector<uint8_t>>& content, int32_t* _aidl_return) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800222 auto [err, fileId, nfp] = toMakeFileParams(params);
223 if (err) {
224 *_aidl_return = err;
225 return ok();
226 }
227
Alex Buynytskyyb39d13e2020-09-12 16:12:36 -0700228 *_aidl_return = mImpl.makeFile(storageId, path, 0777, fileId, nfp, toSpan(content));
Songchun Fan3c82a302019-11-29 14:23:45 -0800229 return ok();
230}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800231binder::Status BinderIncrementalService::makeFileFromRange(int32_t storageId,
232 const std::string& targetPath,
233 const std::string& sourcePath,
Songchun Fan3c82a302019-11-29 14:23:45 -0800234 int64_t start, int64_t end,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800235 int32_t* _aidl_return) {
236 // TODO(b/136132412): implement this
237 *_aidl_return = ENOSYS; // not implemented
238 return ok();
239}
240
241binder::Status BinderIncrementalService::makeLink(int32_t sourceStorageId,
242 const std::string& sourcePath,
243 int32_t destStorageId,
244 const std::string& destPath,
245 int32_t* _aidl_return) {
246 *_aidl_return = mImpl.link(sourceStorageId, sourcePath, destStorageId, destPath);
247 return ok();
248}
249
250binder::Status BinderIncrementalService::unlink(int32_t storageId, const std::string& path,
251 int32_t* _aidl_return) {
252 *_aidl_return = mImpl.unlink(storageId, path);
253 return ok();
254}
255
Alex Buynytskyybc0a7e62020-08-25 12:45:22 -0700256binder::Status BinderIncrementalService::isFileFullyLoaded(int32_t storageId,
257 const std::string& path,
258 int32_t* _aidl_return) {
259 *_aidl_return = mImpl.isFileFullyLoaded(storageId, path);
260 return ok();
261}
262
Alex Buynytskyy07694ed2021-01-27 06:58:55 -0800263binder::Status BinderIncrementalService::isFullyLoaded(int32_t storageId, int32_t* _aidl_return) {
264 *_aidl_return = mImpl.getLoadingProgress(storageId, /*stopOnFirstIncomplete=*/true)
265 .blocksRemainingOrError();
266 return ok();
267}
268
Songchun Fan374f7652020-08-20 08:40:29 -0700269binder::Status BinderIncrementalService::getLoadingProgress(int32_t storageId,
270 float* _aidl_return) {
Alex Buynytskyy07694ed2021-01-27 06:58:55 -0800271 *_aidl_return =
272 mImpl.getLoadingProgress(storageId, /*stopOnFirstIncomplete=*/false).getProgress();
Songchun Fan3c82a302019-11-29 14:23:45 -0800273 return ok();
274}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800275
276binder::Status BinderIncrementalService::getMetadataByPath(int32_t storageId,
277 const std::string& path,
278 std::vector<uint8_t>* _aidl_return) {
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700279 auto metadata = mImpl.getMetadata(storageId, path);
280 _aidl_return->assign(metadata.begin(), metadata.end());
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800281 return ok();
282}
283
284static FileId toFileId(const std::vector<uint8_t>& id) {
285 FileId fid;
286 memcpy(&fid, id.data(), id.size());
287 return fid;
288}
289
290binder::Status BinderIncrementalService::getMetadataById(int32_t storageId,
291 const std::vector<uint8_t>& id,
Songchun Fan3c82a302019-11-29 14:23:45 -0800292 std::vector<uint8_t>* _aidl_return) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800293 if (id.size() != sizeof(incfs::FileId)) {
294 return ok();
295 }
296 auto fid = toFileId(id);
297 auto metadata = mImpl.getMetadata(storageId, fid);
Songchun Fan3c82a302019-11-29 14:23:45 -0800298 _aidl_return->assign(metadata.begin(), metadata.end());
299 return ok();
300}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800301
302binder::Status BinderIncrementalService::makeDirectories(int32_t storageId, const std::string& path,
303 int32_t* _aidl_return) {
304 *_aidl_return = mImpl.makeDirs(storageId, path);
305 return ok();
306}
307
Songchun Fan0f8b6fe2020-02-05 17:41:25 -0800308binder::Status BinderIncrementalService::configureNativeBinaries(
309 int32_t storageId, const std::string& apkFullPath, const std::string& libDirRelativePath,
Songchun Fan14f6c3c2020-05-21 18:19:07 -0700310 const std::string& abi, bool extractNativeLibs, bool* _aidl_return) {
311 *_aidl_return = mImpl.configureNativeBinaries(storageId, apkFullPath, libDirRelativePath, abi,
312 extractNativeLibs);
Songchun Fan0f8b6fe2020-02-05 17:41:25 -0800313 return ok();
314}
315
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700316binder::Status BinderIncrementalService::waitForNativeBinariesExtraction(int storageId,
317 bool* _aidl_return) {
318 *_aidl_return = mImpl.waitForNativeBinariesExtraction(storageId);
319 return ok();
320}
321
Songchun Fana7098592020-09-03 11:45:53 -0700322binder::Status BinderIncrementalService::registerLoadingProgressListener(
323 int32_t storageId,
324 const ::android::sp<::android::os::incremental::IStorageLoadingProgressListener>&
325 progressListener,
326 bool* _aidl_return) {
327 *_aidl_return = mImpl.registerLoadingProgressListener(storageId, progressListener);
328 return ok();
329}
330binder::Status BinderIncrementalService::unregisterLoadingProgressListener(int32_t storageId,
331 bool* _aidl_return) {
332 *_aidl_return = mImpl.unregisterLoadingProgressListener(storageId);
333 return ok();
334}
335
Songchun Fan2570ec02020-10-08 17:22:33 -0700336binder::Status BinderIncrementalService::registerStorageHealthListener(
337 int32_t storageId,
338 const ::android::os::incremental::StorageHealthCheckParams& healthCheckParams,
339 const ::android::sp<IStorageHealthListener>& healthListener, bool* _aidl_return) {
340 *_aidl_return = mImpl.registerStorageHealthListener(storageId,
341 const_cast<StorageHealthCheckParams&&>(
342 healthCheckParams),
343 healthListener);
344 return ok();
345}
346
347binder::Status BinderIncrementalService::unregisterStorageHealthListener(int32_t storageId) {
348 mImpl.unregisterStorageHealthListener(storageId);
349 return ok();
350}
351
Songchun Fan3c82a302019-11-29 14:23:45 -0800352} // namespace android::os::incremental
353
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700354jlong Incremental_IncrementalService_Start(JNIEnv* env) {
355 return (jlong)android::os::incremental::BinderIncrementalService::start(env);
Songchun Fan3c82a302019-11-29 14:23:45 -0800356}
357void Incremental_IncrementalService_OnSystemReady(jlong self) {
358 if (self) {
359 ((android::os::incremental::BinderIncrementalService*)self)->onSystemReady();
360 }
361}
Alex Buynytskyy18b07a42020-02-03 20:06:00 -0800362void Incremental_IncrementalService_OnDump(jlong self, jint fd) {
363 if (self) {
364 ((android::os::incremental::BinderIncrementalService*)self)->dump(fd, {});
365 } else {
366 dprintf(fd, "BinderIncrementalService is stopped.");
367 }
368}