blob: 2dbbc5ac6806035a4e8519a5ef122b51f576d707 [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>
Songchun Fan3c82a302019-11-29 14:23:45 -080020#include <binder/IResultReceiver.h>
Alex Buynytskyy18b07a42020-02-03 20:06:00 -080021#include <binder/PermissionCache.h>
Songchun Fan3c82a302019-11-29 14:23:45 -080022#include <incfs.h>
23
24#include "ServiceWrappers.h"
25#include "jni.h"
26#include "nativehelper/JNIHelp.h"
27#include "path.h"
28
29using namespace std::literals;
30using namespace android::incremental;
31
32namespace android::os::incremental {
33
34static constexpr auto kAndroidDataEnv = "ANDROID_DATA"sv;
35static constexpr auto kDataDir = "/data"sv;
36static constexpr auto kIncrementalSubDir = "incremental"sv;
37
38static std::string getIncrementalDir() {
39 const char* dataDir = getenv(kAndroidDataEnv.data());
40 if (!dataDir || !*dataDir) {
41 dataDir = kDataDir.data();
42 }
43 return path::normalize(path::join(dataDir, kIncrementalSubDir));
44}
45
46static bool incFsEnabled() {
47 // TODO(b/136132412): use vold to check /sys/fs/incfs/version (per selinux compliance)
48 return incfs::enabled();
49}
50
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080051static bool incFsValid(const sp<IVold>& vold) {
52 bool enabled = false;
53 auto status = vold->incFsEnabled(&enabled);
54 if (!status.isOk() || !enabled) {
Songchun Fan3c82a302019-11-29 14:23:45 -080055 return false;
56 }
57 return true;
58}
59
60BinderIncrementalService::BinderIncrementalService(const sp<IServiceManager>& sm)
61 : mImpl(RealServiceManager(sm), getIncrementalDir()) {}
62
63BinderIncrementalService* BinderIncrementalService::start() {
64 if (!incFsEnabled()) {
65 return nullptr;
66 }
67
68 IPCThreadState::self()->disableBackgroundScheduling(true);
69 sp<IServiceManager> sm(defaultServiceManager());
70 if (!sm) {
71 return nullptr;
72 }
73
74 sp<IBinder> voldBinder(sm->getService(String16("vold")));
75 if (voldBinder == nullptr) {
76 return nullptr;
77 }
78 sp<IVold> vold = interface_cast<IVold>(voldBinder);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080079 if (!incFsValid(vold)) {
Songchun Fan3c82a302019-11-29 14:23:45 -080080 return nullptr;
81 }
82
83 sp<BinderIncrementalService> self(new BinderIncrementalService(sm));
84 status_t ret = sm->addService(String16{getServiceName()}, self);
85 if (ret != android::OK) {
86 return nullptr;
87 }
88 sp<ProcessState> ps(ProcessState::self());
89 ps->startThreadPool();
90 ps->giveThreadPoolName();
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>&) {
96 static const String16 kDump("android.permission.DUMP");
97 if (!PermissionCache::checkCallingPermission(kDump)) {
98 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 Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800118binder::Status BinderIncrementalService::createStorage(const std::string& path,
119 const DataLoaderParamsParcel& params,
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800120 const ::android::sp<::android::content::pm::IDataLoaderStatusListener>& listener,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800121 int32_t createMode, int32_t* _aidl_return) {
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800122 *_aidl_return = mImpl.createStorage(path, const_cast<DataLoaderParamsParcel&&>(params), listener, android::incremental::IncrementalService::CreateOptions(createMode));
Songchun Fan3c82a302019-11-29 14:23:45 -0800123 return ok();
124}
125
126binder::Status BinderIncrementalService::createLinkedStorage(const std::string& path,
127 int32_t otherStorageId,
128 int32_t createMode,
129 int32_t* _aidl_return) {
130 *_aidl_return =
131 mImpl.createLinkedStorage(path, otherStorageId,
132 android::incremental::IncrementalService::CreateOptions(
133 createMode));
134 return ok();
135}
136
137binder::Status BinderIncrementalService::makeBindMount(int32_t storageId,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800138 const std::string& sourcePath,
Songchun Fan3c82a302019-11-29 14:23:45 -0800139 const std::string& targetFullPath,
140 int32_t bindType, int32_t* _aidl_return) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800141 *_aidl_return = mImpl.bind(storageId, sourcePath, targetFullPath,
Songchun Fan3c82a302019-11-29 14:23:45 -0800142 android::incremental::IncrementalService::BindKind(bindType));
143 return ok();
144}
145
146binder::Status BinderIncrementalService::deleteBindMount(int32_t storageId,
147 const std::string& targetFullPath,
148 int32_t* _aidl_return) {
149 *_aidl_return = mImpl.unbind(storageId, targetFullPath);
150 return ok();
151}
152
153binder::Status BinderIncrementalService::deleteStorage(int32_t storageId) {
154 mImpl.deleteStorage(storageId);
155 return ok();
156}
157
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800158binder::Status BinderIncrementalService::makeDirectory(int32_t storageId, const std::string& path,
Songchun Fan3c82a302019-11-29 14:23:45 -0800159 int32_t* _aidl_return) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800160 *_aidl_return = mImpl.makeDir(storageId, path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800161 return ok();
162}
163
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800164static std::tuple<int, incfs::FileId, incfs::NewFileParams> toMakeFileParams(
165 const android::os::incremental::IncrementalNewFileParams& params) {
166 incfs::FileId id;
167 if (params.fileId.empty()) {
168 if (params.metadata.empty()) {
169 return {EINVAL, {}, {}};
170 }
171 id = IncrementalService::idFromMetadata(params.metadata);
172 } else if (params.fileId.size() != sizeof(id)) {
173 return {EINVAL, {}, {}};
174 } else {
175 memcpy(&id, params.fileId.data(), sizeof(id));
176 }
177 incfs::NewFileParams nfp;
178 nfp.size = params.size;
179 nfp.metadata = {(const char*)params.metadata.data(), (IncFsSize)params.metadata.size()};
180 if (!params.signature) {
Alex Buynytskyyf5e605a2020-03-13 13:31:12 -0700181 nfp.signature = {};
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800182 } else {
Alex Buynytskyyf5e605a2020-03-13 13:31:12 -0700183 nfp.signature = {(const char*)params.signature->data(), (IncFsSize)params.signature->size()};
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800184 }
185 return {0, id, nfp};
Songchun Fan3c82a302019-11-29 14:23:45 -0800186}
187
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800188binder::Status BinderIncrementalService::makeFile(
189 int32_t storageId, const std::string& path,
190 const ::android::os::incremental::IncrementalNewFileParams& params, int32_t* _aidl_return) {
191 auto [err, fileId, nfp] = toMakeFileParams(params);
192 if (err) {
193 *_aidl_return = err;
194 return ok();
195 }
196
Songchun Fan54c6aed2020-01-31 16:52:41 -0800197 *_aidl_return = mImpl.makeFile(storageId, path, 0777, fileId, nfp);
Songchun Fan3c82a302019-11-29 14:23:45 -0800198 return ok();
199}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800200binder::Status BinderIncrementalService::makeFileFromRange(int32_t storageId,
201 const std::string& targetPath,
202 const std::string& sourcePath,
Songchun Fan3c82a302019-11-29 14:23:45 -0800203 int64_t start, int64_t end,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800204 int32_t* _aidl_return) {
205 // TODO(b/136132412): implement this
206 *_aidl_return = ENOSYS; // not implemented
207 return ok();
208}
209
210binder::Status BinderIncrementalService::makeLink(int32_t sourceStorageId,
211 const std::string& sourcePath,
212 int32_t destStorageId,
213 const std::string& destPath,
214 int32_t* _aidl_return) {
215 *_aidl_return = mImpl.link(sourceStorageId, sourcePath, destStorageId, destPath);
216 return ok();
217}
218
219binder::Status BinderIncrementalService::unlink(int32_t storageId, const std::string& path,
220 int32_t* _aidl_return) {
221 *_aidl_return = mImpl.unlink(storageId, path);
222 return ok();
223}
224
225binder::Status BinderIncrementalService::isFileRangeLoaded(int32_t storageId,
226 const std::string& path, int64_t start,
227 int64_t end, bool* _aidl_return) {
228 // TODO: implement
Songchun Fan3c82a302019-11-29 14:23:45 -0800229 *_aidl_return = false;
230 return ok();
231}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800232
233binder::Status BinderIncrementalService::getMetadataByPath(int32_t storageId,
234 const std::string& path,
235 std::vector<uint8_t>* _aidl_return) {
236 auto fid = mImpl.nodeFor(storageId, path);
237 if (fid != kIncFsInvalidFileId) {
238 auto metadata = mImpl.getMetadata(storageId, fid);
239 _aidl_return->assign(metadata.begin(), metadata.end());
240 }
241 return ok();
242}
243
244static FileId toFileId(const std::vector<uint8_t>& id) {
245 FileId fid;
246 memcpy(&fid, id.data(), id.size());
247 return fid;
248}
249
250binder::Status BinderIncrementalService::getMetadataById(int32_t storageId,
251 const std::vector<uint8_t>& id,
Songchun Fan3c82a302019-11-29 14:23:45 -0800252 std::vector<uint8_t>* _aidl_return) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800253 if (id.size() != sizeof(incfs::FileId)) {
254 return ok();
255 }
256 auto fid = toFileId(id);
257 auto metadata = mImpl.getMetadata(storageId, fid);
Songchun Fan3c82a302019-11-29 14:23:45 -0800258 _aidl_return->assign(metadata.begin(), metadata.end());
259 return ok();
260}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800261
262binder::Status BinderIncrementalService::makeDirectories(int32_t storageId, const std::string& path,
263 int32_t* _aidl_return) {
264 *_aidl_return = mImpl.makeDirs(storageId, path);
265 return ok();
266}
267
Songchun Fan3c82a302019-11-29 14:23:45 -0800268binder::Status BinderIncrementalService::startLoading(int32_t storageId, bool* _aidl_return) {
269 *_aidl_return = mImpl.startLoading(storageId);
270 return ok();
271}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800272
Songchun Fan0f8b6fe2020-02-05 17:41:25 -0800273binder::Status BinderIncrementalService::configureNativeBinaries(
274 int32_t storageId, const std::string& apkFullPath, const std::string& libDirRelativePath,
275 const std::string& abi, bool* _aidl_return) {
276 *_aidl_return = mImpl.configureNativeBinaries(storageId, apkFullPath, libDirRelativePath, abi);
277 return ok();
278}
279
Songchun Fan3c82a302019-11-29 14:23:45 -0800280} // namespace android::os::incremental
281
282jlong Incremental_IncrementalService_Start() {
283 return (jlong)android::os::incremental::BinderIncrementalService::start();
284}
285void Incremental_IncrementalService_OnSystemReady(jlong self) {
286 if (self) {
287 ((android::os::incremental::BinderIncrementalService*)self)->onSystemReady();
288 }
289}
Alex Buynytskyy18b07a42020-02-03 20:06:00 -0800290void Incremental_IncrementalService_OnDump(jlong self, jint fd) {
291 if (self) {
292 ((android::os::incremental::BinderIncrementalService*)self)->dump(fd, {});
293 } else {
294 dprintf(fd, "BinderIncrementalService is stopped.");
295 }
296}