blob: 8d2afc1bea3756ff1abd43d7bd1e7fc282966ac5 [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,
125 int32_t* _aidl_return) {
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700126 *_aidl_return =
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700127 mImpl.createStorage(path, const_cast<content::pm::DataLoaderParamsParcel&&>(params),
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700128 android::incremental::IncrementalService::CreateOptions(createMode),
129 statusListener,
130 const_cast<StorageHealthCheckParams&&>(healthCheckParams),
131 healthListener);
Songchun Fan3c82a302019-11-29 14:23:45 -0800132 return ok();
133}
134
135binder::Status BinderIncrementalService::createLinkedStorage(const std::string& path,
136 int32_t otherStorageId,
137 int32_t createMode,
138 int32_t* _aidl_return) {
139 *_aidl_return =
140 mImpl.createLinkedStorage(path, otherStorageId,
141 android::incremental::IncrementalService::CreateOptions(
142 createMode));
143 return ok();
144}
145
146binder::Status BinderIncrementalService::makeBindMount(int32_t storageId,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800147 const std::string& sourcePath,
Songchun Fan3c82a302019-11-29 14:23:45 -0800148 const std::string& targetFullPath,
149 int32_t bindType, int32_t* _aidl_return) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800150 *_aidl_return = mImpl.bind(storageId, sourcePath, targetFullPath,
Songchun Fan3c82a302019-11-29 14:23:45 -0800151 android::incremental::IncrementalService::BindKind(bindType));
152 return ok();
153}
154
155binder::Status BinderIncrementalService::deleteBindMount(int32_t storageId,
156 const std::string& targetFullPath,
157 int32_t* _aidl_return) {
158 *_aidl_return = mImpl.unbind(storageId, targetFullPath);
159 return ok();
160}
161
162binder::Status BinderIncrementalService::deleteStorage(int32_t storageId) {
163 mImpl.deleteStorage(storageId);
164 return ok();
165}
166
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800167binder::Status BinderIncrementalService::makeDirectory(int32_t storageId, const std::string& path,
Songchun Fan3c82a302019-11-29 14:23:45 -0800168 int32_t* _aidl_return) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800169 *_aidl_return = mImpl.makeDir(storageId, path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800170 return ok();
171}
172
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800173static std::tuple<int, incfs::FileId, incfs::NewFileParams> toMakeFileParams(
174 const android::os::incremental::IncrementalNewFileParams& params) {
175 incfs::FileId id;
176 if (params.fileId.empty()) {
177 if (params.metadata.empty()) {
178 return {EINVAL, {}, {}};
179 }
180 id = IncrementalService::idFromMetadata(params.metadata);
181 } else if (params.fileId.size() != sizeof(id)) {
182 return {EINVAL, {}, {}};
183 } else {
184 memcpy(&id, params.fileId.data(), sizeof(id));
185 }
186 incfs::NewFileParams nfp;
187 nfp.size = params.size;
188 nfp.metadata = {(const char*)params.metadata.data(), (IncFsSize)params.metadata.size()};
189 if (!params.signature) {
Alex Buynytskyyf5e605a2020-03-13 13:31:12 -0700190 nfp.signature = {};
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800191 } else {
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700192 nfp.signature = {(const char*)params.signature->data(),
193 (IncFsSize)params.signature->size()};
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800194 }
195 return {0, id, nfp};
Songchun Fan3c82a302019-11-29 14:23:45 -0800196}
197
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800198binder::Status BinderIncrementalService::makeFile(
199 int32_t storageId, const std::string& path,
200 const ::android::os::incremental::IncrementalNewFileParams& params, int32_t* _aidl_return) {
201 auto [err, fileId, nfp] = toMakeFileParams(params);
202 if (err) {
203 *_aidl_return = err;
204 return ok();
205 }
206
Songchun Fan54c6aed2020-01-31 16:52:41 -0800207 *_aidl_return = mImpl.makeFile(storageId, path, 0777, fileId, nfp);
Songchun Fan3c82a302019-11-29 14:23:45 -0800208 return ok();
209}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800210binder::Status BinderIncrementalService::makeFileFromRange(int32_t storageId,
211 const std::string& targetPath,
212 const std::string& sourcePath,
Songchun Fan3c82a302019-11-29 14:23:45 -0800213 int64_t start, int64_t end,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800214 int32_t* _aidl_return) {
215 // TODO(b/136132412): implement this
216 *_aidl_return = ENOSYS; // not implemented
217 return ok();
218}
219
220binder::Status BinderIncrementalService::makeLink(int32_t sourceStorageId,
221 const std::string& sourcePath,
222 int32_t destStorageId,
223 const std::string& destPath,
224 int32_t* _aidl_return) {
225 *_aidl_return = mImpl.link(sourceStorageId, sourcePath, destStorageId, destPath);
226 return ok();
227}
228
229binder::Status BinderIncrementalService::unlink(int32_t storageId, const std::string& path,
230 int32_t* _aidl_return) {
231 *_aidl_return = mImpl.unlink(storageId, path);
232 return ok();
233}
234
235binder::Status BinderIncrementalService::isFileRangeLoaded(int32_t storageId,
236 const std::string& path, int64_t start,
237 int64_t end, bool* _aidl_return) {
238 // TODO: implement
Songchun Fan3c82a302019-11-29 14:23:45 -0800239 *_aidl_return = false;
240 return ok();
241}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800242
243binder::Status BinderIncrementalService::getMetadataByPath(int32_t storageId,
244 const std::string& path,
245 std::vector<uint8_t>* _aidl_return) {
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700246 auto metadata = mImpl.getMetadata(storageId, path);
247 _aidl_return->assign(metadata.begin(), metadata.end());
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800248 return ok();
249}
250
251static FileId toFileId(const std::vector<uint8_t>& id) {
252 FileId fid;
253 memcpy(&fid, id.data(), id.size());
254 return fid;
255}
256
257binder::Status BinderIncrementalService::getMetadataById(int32_t storageId,
258 const std::vector<uint8_t>& id,
Songchun Fan3c82a302019-11-29 14:23:45 -0800259 std::vector<uint8_t>* _aidl_return) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800260 if (id.size() != sizeof(incfs::FileId)) {
261 return ok();
262 }
263 auto fid = toFileId(id);
264 auto metadata = mImpl.getMetadata(storageId, fid);
Songchun Fan3c82a302019-11-29 14:23:45 -0800265 _aidl_return->assign(metadata.begin(), metadata.end());
266 return ok();
267}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800268
269binder::Status BinderIncrementalService::makeDirectories(int32_t storageId, const std::string& path,
270 int32_t* _aidl_return) {
271 *_aidl_return = mImpl.makeDirs(storageId, path);
272 return ok();
273}
274
Songchun Fan3c82a302019-11-29 14:23:45 -0800275binder::Status BinderIncrementalService::startLoading(int32_t storageId, bool* _aidl_return) {
276 *_aidl_return = mImpl.startLoading(storageId);
277 return ok();
278}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800279
Songchun Fan0f8b6fe2020-02-05 17:41:25 -0800280binder::Status BinderIncrementalService::configureNativeBinaries(
281 int32_t storageId, const std::string& apkFullPath, const std::string& libDirRelativePath,
Songchun Fan14f6c3c2020-05-21 18:19:07 -0700282 const std::string& abi, bool extractNativeLibs, bool* _aidl_return) {
283 *_aidl_return = mImpl.configureNativeBinaries(storageId, apkFullPath, libDirRelativePath, abi,
284 extractNativeLibs);
Songchun Fan0f8b6fe2020-02-05 17:41:25 -0800285 return ok();
286}
287
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700288binder::Status BinderIncrementalService::waitForNativeBinariesExtraction(int storageId,
289 bool* _aidl_return) {
290 *_aidl_return = mImpl.waitForNativeBinariesExtraction(storageId);
291 return ok();
292}
293
Songchun Fan3c82a302019-11-29 14:23:45 -0800294} // namespace android::os::incremental
295
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700296jlong Incremental_IncrementalService_Start(JNIEnv* env) {
297 return (jlong)android::os::incremental::BinderIncrementalService::start(env);
Songchun Fan3c82a302019-11-29 14:23:45 -0800298}
299void Incremental_IncrementalService_OnSystemReady(jlong self) {
300 if (self) {
301 ((android::os::incremental::BinderIncrementalService*)self)->onSystemReady();
302 }
303}
Alex Buynytskyy18b07a42020-02-03 20:06:00 -0800304void Incremental_IncrementalService_OnDump(jlong self, jint fd) {
305 if (self) {
306 ((android::os::incremental::BinderIncrementalService*)self)->dump(fd, {});
307 } else {
308 dprintf(fd, "BinderIncrementalService is stopped.");
309 }
310}