blob: d2244286450b43c9a50f3ae2cf4e4b4cd52d2733 [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,
121 int32_t createMode,
122 const ::android::sp<::android::content::pm::IDataLoaderStatusListener>& statusListener,
123 const ::android::os::incremental::StorageHealthCheckParams& healthCheckParams,
124 const ::android::sp<::android::os::incremental::IStorageHealthListener>& healthListener,
Alex Buynytskyyaa8e95e2020-12-14 21:50:04 -0800125 const ::std::vector<::android::os::incremental::PerUidReadTimeouts>& perUidReadTimeouts,
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700126 int32_t* _aidl_return) {
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700127 *_aidl_return =
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700128 mImpl.createStorage(path, const_cast<content::pm::DataLoaderParamsParcel&&>(params),
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700129 android::incremental::IncrementalService::CreateOptions(createMode),
130 statusListener,
131 const_cast<StorageHealthCheckParams&&>(healthCheckParams),
Alex Buynytskyyaa8e95e2020-12-14 21:50:04 -0800132 healthListener, perUidReadTimeouts);
Songchun Fan3c82a302019-11-29 14:23:45 -0800133 return ok();
134}
135
136binder::Status BinderIncrementalService::createLinkedStorage(const std::string& path,
137 int32_t otherStorageId,
138 int32_t createMode,
139 int32_t* _aidl_return) {
140 *_aidl_return =
141 mImpl.createLinkedStorage(path, otherStorageId,
142 android::incremental::IncrementalService::CreateOptions(
143 createMode));
144 return ok();
145}
146
147binder::Status BinderIncrementalService::makeBindMount(int32_t storageId,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800148 const std::string& sourcePath,
Songchun Fan3c82a302019-11-29 14:23:45 -0800149 const std::string& targetFullPath,
150 int32_t bindType, int32_t* _aidl_return) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800151 *_aidl_return = mImpl.bind(storageId, sourcePath, targetFullPath,
Songchun Fan3c82a302019-11-29 14:23:45 -0800152 android::incremental::IncrementalService::BindKind(bindType));
153 return ok();
154}
155
156binder::Status BinderIncrementalService::deleteBindMount(int32_t storageId,
157 const std::string& targetFullPath,
158 int32_t* _aidl_return) {
159 *_aidl_return = mImpl.unbind(storageId, targetFullPath);
160 return ok();
161}
162
163binder::Status BinderIncrementalService::deleteStorage(int32_t storageId) {
164 mImpl.deleteStorage(storageId);
165 return ok();
166}
167
Alex Buynytskyyaa8e95e2020-12-14 21:50:04 -0800168binder::Status BinderIncrementalService::disallowReadLogs(int32_t storageId) {
169 mImpl.disallowReadLogs(storageId);
Alex Buynytskyy3697d9e2020-06-06 20:15:58 -0700170 return ok();
171}
172
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800173binder::Status BinderIncrementalService::makeDirectory(int32_t storageId, const std::string& path,
Songchun Fan3c82a302019-11-29 14:23:45 -0800174 int32_t* _aidl_return) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800175 *_aidl_return = mImpl.makeDir(storageId, path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800176 return ok();
177}
178
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800179static std::tuple<int, incfs::FileId, incfs::NewFileParams> toMakeFileParams(
180 const android::os::incremental::IncrementalNewFileParams& params) {
181 incfs::FileId id;
182 if (params.fileId.empty()) {
183 if (params.metadata.empty()) {
184 return {EINVAL, {}, {}};
185 }
186 id = IncrementalService::idFromMetadata(params.metadata);
187 } else if (params.fileId.size() != sizeof(id)) {
188 return {EINVAL, {}, {}};
189 } else {
190 memcpy(&id, params.fileId.data(), sizeof(id));
191 }
192 incfs::NewFileParams nfp;
193 nfp.size = params.size;
194 nfp.metadata = {(const char*)params.metadata.data(), (IncFsSize)params.metadata.size()};
195 if (!params.signature) {
Alex Buynytskyyf5e605a2020-03-13 13:31:12 -0700196 nfp.signature = {};
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800197 } else {
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700198 nfp.signature = {(const char*)params.signature->data(),
199 (IncFsSize)params.signature->size()};
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800200 }
201 return {0, id, nfp};
Songchun Fan3c82a302019-11-29 14:23:45 -0800202}
203
Alex Buynytskyyb39d13e2020-09-12 16:12:36 -0700204static std::span<const uint8_t> toSpan(const ::std::optional<::std::vector<uint8_t>>& content) {
205 if (!content) {
206 return {};
207 }
208 return {content->data(), (int)content->size()};
209}
210
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800211binder::Status BinderIncrementalService::makeFile(
212 int32_t storageId, const std::string& path,
Alex Buynytskyyb39d13e2020-09-12 16:12:36 -0700213 const ::android::os::incremental::IncrementalNewFileParams& params,
214 const ::std::optional<::std::vector<uint8_t>>& content, int32_t* _aidl_return) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800215 auto [err, fileId, nfp] = toMakeFileParams(params);
216 if (err) {
217 *_aidl_return = err;
218 return ok();
219 }
220
Alex Buynytskyyb39d13e2020-09-12 16:12:36 -0700221 *_aidl_return = mImpl.makeFile(storageId, path, 0777, fileId, nfp, toSpan(content));
Songchun Fan3c82a302019-11-29 14:23:45 -0800222 return ok();
223}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800224binder::Status BinderIncrementalService::makeFileFromRange(int32_t storageId,
225 const std::string& targetPath,
226 const std::string& sourcePath,
Songchun Fan3c82a302019-11-29 14:23:45 -0800227 int64_t start, int64_t end,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800228 int32_t* _aidl_return) {
229 // TODO(b/136132412): implement this
230 *_aidl_return = ENOSYS; // not implemented
231 return ok();
232}
233
234binder::Status BinderIncrementalService::makeLink(int32_t sourceStorageId,
235 const std::string& sourcePath,
236 int32_t destStorageId,
237 const std::string& destPath,
238 int32_t* _aidl_return) {
239 *_aidl_return = mImpl.link(sourceStorageId, sourcePath, destStorageId, destPath);
240 return ok();
241}
242
243binder::Status BinderIncrementalService::unlink(int32_t storageId, const std::string& path,
244 int32_t* _aidl_return) {
245 *_aidl_return = mImpl.unlink(storageId, path);
246 return ok();
247}
248
Alex Buynytskyybc0a7e62020-08-25 12:45:22 -0700249binder::Status BinderIncrementalService::isFileFullyLoaded(int32_t storageId,
250 const std::string& path,
251 int32_t* _aidl_return) {
252 *_aidl_return = mImpl.isFileFullyLoaded(storageId, path);
253 return ok();
254}
255
Songchun Fan374f7652020-08-20 08:40:29 -0700256binder::Status BinderIncrementalService::getLoadingProgress(int32_t storageId,
257 float* _aidl_return) {
Alex Buynytskyyaa8e95e2020-12-14 21:50:04 -0800258 *_aidl_return = mImpl.getLoadingProgress(storageId).getProgress();
Songchun Fan3c82a302019-11-29 14:23:45 -0800259 return ok();
260}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800261
262binder::Status BinderIncrementalService::getMetadataByPath(int32_t storageId,
263 const std::string& path,
264 std::vector<uint8_t>* _aidl_return) {
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700265 auto metadata = mImpl.getMetadata(storageId, path);
266 _aidl_return->assign(metadata.begin(), metadata.end());
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800267 return ok();
268}
269
270static FileId toFileId(const std::vector<uint8_t>& id) {
271 FileId fid;
272 memcpy(&fid, id.data(), id.size());
273 return fid;
274}
275
276binder::Status BinderIncrementalService::getMetadataById(int32_t storageId,
277 const std::vector<uint8_t>& id,
Songchun Fan3c82a302019-11-29 14:23:45 -0800278 std::vector<uint8_t>* _aidl_return) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800279 if (id.size() != sizeof(incfs::FileId)) {
280 return ok();
281 }
282 auto fid = toFileId(id);
283 auto metadata = mImpl.getMetadata(storageId, fid);
Songchun Fan3c82a302019-11-29 14:23:45 -0800284 _aidl_return->assign(metadata.begin(), metadata.end());
285 return ok();
286}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800287
288binder::Status BinderIncrementalService::makeDirectories(int32_t storageId, const std::string& path,
289 int32_t* _aidl_return) {
290 *_aidl_return = mImpl.makeDirs(storageId, path);
291 return ok();
292}
293
Songchun Fan3c82a302019-11-29 14:23:45 -0800294binder::Status BinderIncrementalService::startLoading(int32_t storageId, bool* _aidl_return) {
295 *_aidl_return = mImpl.startLoading(storageId);
296 return ok();
297}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800298
Songchun Fan0f8b6fe2020-02-05 17:41:25 -0800299binder::Status BinderIncrementalService::configureNativeBinaries(
300 int32_t storageId, const std::string& apkFullPath, const std::string& libDirRelativePath,
Songchun Fan14f6c3c2020-05-21 18:19:07 -0700301 const std::string& abi, bool extractNativeLibs, bool* _aidl_return) {
302 *_aidl_return = mImpl.configureNativeBinaries(storageId, apkFullPath, libDirRelativePath, abi,
303 extractNativeLibs);
Songchun Fan0f8b6fe2020-02-05 17:41:25 -0800304 return ok();
305}
306
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700307binder::Status BinderIncrementalService::waitForNativeBinariesExtraction(int storageId,
308 bool* _aidl_return) {
309 *_aidl_return = mImpl.waitForNativeBinariesExtraction(storageId);
310 return ok();
311}
312
Songchun Fana7098592020-09-03 11:45:53 -0700313binder::Status BinderIncrementalService::registerLoadingProgressListener(
314 int32_t storageId,
315 const ::android::sp<::android::os::incremental::IStorageLoadingProgressListener>&
316 progressListener,
317 bool* _aidl_return) {
318 *_aidl_return = mImpl.registerLoadingProgressListener(storageId, progressListener);
319 return ok();
320}
321binder::Status BinderIncrementalService::unregisterLoadingProgressListener(int32_t storageId,
322 bool* _aidl_return) {
323 *_aidl_return = mImpl.unregisterLoadingProgressListener(storageId);
324 return ok();
325}
326
Songchun Fan2570ec02020-10-08 17:22:33 -0700327binder::Status BinderIncrementalService::registerStorageHealthListener(
328 int32_t storageId,
329 const ::android::os::incremental::StorageHealthCheckParams& healthCheckParams,
330 const ::android::sp<IStorageHealthListener>& healthListener, bool* _aidl_return) {
331 *_aidl_return = mImpl.registerStorageHealthListener(storageId,
332 const_cast<StorageHealthCheckParams&&>(
333 healthCheckParams),
334 healthListener);
335 return ok();
336}
337
338binder::Status BinderIncrementalService::unregisterStorageHealthListener(int32_t storageId) {
339 mImpl.unregisterStorageHealthListener(storageId);
340 return ok();
341}
342
Songchun Fan3c82a302019-11-29 14:23:45 -0800343} // namespace android::os::incremental
344
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700345jlong Incremental_IncrementalService_Start(JNIEnv* env) {
346 return (jlong)android::os::incremental::BinderIncrementalService::start(env);
Songchun Fan3c82a302019-11-29 14:23:45 -0800347}
348void Incremental_IncrementalService_OnSystemReady(jlong self) {
349 if (self) {
350 ((android::os::incremental::BinderIncrementalService*)self)->onSystemReady();
351 }
352}
Alex Buynytskyy18b07a42020-02-03 20:06:00 -0800353void Incremental_IncrementalService_OnDump(jlong self, jint fd) {
354 if (self) {
355 ((android::os::incremental::BinderIncrementalService*)self)->dump(fd, {});
356 } else {
357 dprintf(fd, "BinderIncrementalService is stopped.");
358 }
359}