1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "BinderIncrementalService.h"
#include <android-base/logging.h>
#include <android-base/no_destructor.h>
#include <android/os/IVold.h>
#include <binder/IResultReceiver.h>
#include <binder/PermissionCache.h>
#include <incfs.h>
#include "ServiceWrappers.h"
#include "jni.h"
#include "path.h"
using namespace std::literals;
using namespace android::incremental;
namespace android::os::incremental {
static constexpr auto kAndroidDataEnv = "ANDROID_DATA"sv;
static constexpr auto kDataDir = "/data"sv;
static constexpr auto kIncrementalSubDir = "incremental"sv;
static std::string getIncrementalDir() {
const char* dataDir = getenv(kAndroidDataEnv.data());
if (!dataDir || !*dataDir) {
dataDir = kDataDir.data();
}
return path::normalize(path::join(dataDir, kIncrementalSubDir));
}
static bool incFsEnabled() {
// TODO(b/136132412): use vold to check /sys/fs/incfs/version (per selinux compliance)
return incfs::enabled();
}
static bool incFsValid(const sp<IVold>& vold) {
bool enabled = false;
auto status = vold->incFsEnabled(&enabled);
if (!status.isOk() || !enabled) {
return false;
}
return true;
}
BinderIncrementalService::BinderIncrementalService(const sp<IServiceManager>& sm, JNIEnv* env)
: mImpl(RealServiceManager(sm, env), getIncrementalDir()) {}
BinderIncrementalService* BinderIncrementalService::start(JNIEnv* env) {
if (!incFsEnabled()) {
return nullptr;
}
IPCThreadState::self()->disableBackgroundScheduling(true);
sp<IServiceManager> sm(defaultServiceManager());
if (!sm) {
return nullptr;
}
sp<IBinder> voldBinder(sm->getService(String16("vold")));
if (voldBinder == nullptr) {
return nullptr;
}
sp<IVold> vold = interface_cast<IVold>(voldBinder);
if (!incFsValid(vold)) {
return nullptr;
}
sp<BinderIncrementalService> self(new BinderIncrementalService(sm, env));
status_t ret = sm->addService(String16{getServiceName()}, self);
if (ret != android::OK) {
return nullptr;
}
sp<ProcessState> ps(ProcessState::self());
ps->startThreadPool();
ps->giveThreadPoolName();
// sm->addService increments the reference count, and now we're OK with returning the pointer.
return self.get();
}
status_t BinderIncrementalService::dump(int fd, const Vector<String16>&) {
static const android::base::NoDestructor<String16> kDump("android.permission.DUMP");
if (!PermissionCache::checkCallingPermission(*kDump)) {
return PERMISSION_DENIED;
}
mImpl.onDump(fd);
return NO_ERROR;
}
void BinderIncrementalService::onSystemReady() {
mImpl.onSystemReady();
}
static binder::Status ok() {
return binder::Status::ok();
}
binder::Status BinderIncrementalService::openStorage(const std::string& path,
int32_t* _aidl_return) {
*_aidl_return = mImpl.openStorage(path);
return ok();
}
binder::Status BinderIncrementalService::createStorage(
const ::std::string& path, const ::android::content::pm::DataLoaderParamsParcel& params,
int32_t createMode,
const ::android::sp<::android::content::pm::IDataLoaderStatusListener>& statusListener,
const ::android::os::incremental::StorageHealthCheckParams& healthCheckParams,
const ::android::sp<::android::os::incremental::IStorageHealthListener>& healthListener,
int32_t* _aidl_return) {
*_aidl_return =
mImpl.createStorage(path, const_cast<content::pm::DataLoaderParamsParcel&&>(params),
android::incremental::IncrementalService::CreateOptions(createMode),
statusListener,
const_cast<StorageHealthCheckParams&&>(healthCheckParams),
healthListener);
return ok();
}
binder::Status BinderIncrementalService::createLinkedStorage(const std::string& path,
int32_t otherStorageId,
int32_t createMode,
int32_t* _aidl_return) {
*_aidl_return =
mImpl.createLinkedStorage(path, otherStorageId,
android::incremental::IncrementalService::CreateOptions(
createMode));
return ok();
}
binder::Status BinderIncrementalService::makeBindMount(int32_t storageId,
const std::string& sourcePath,
const std::string& targetFullPath,
int32_t bindType, int32_t* _aidl_return) {
*_aidl_return = mImpl.bind(storageId, sourcePath, targetFullPath,
android::incremental::IncrementalService::BindKind(bindType));
return ok();
}
binder::Status BinderIncrementalService::deleteBindMount(int32_t storageId,
const std::string& targetFullPath,
int32_t* _aidl_return) {
*_aidl_return = mImpl.unbind(storageId, targetFullPath);
return ok();
}
binder::Status BinderIncrementalService::deleteStorage(int32_t storageId) {
mImpl.deleteStorage(storageId);
return ok();
}
binder::Status BinderIncrementalService::disableReadLogs(int32_t storageId) {
mImpl.disableReadLogs(storageId);
return ok();
}
binder::Status BinderIncrementalService::makeDirectory(int32_t storageId, const std::string& path,
int32_t* _aidl_return) {
*_aidl_return = mImpl.makeDir(storageId, path);
return ok();
}
static std::tuple<int, incfs::FileId, incfs::NewFileParams> toMakeFileParams(
const android::os::incremental::IncrementalNewFileParams& params) {
incfs::FileId id;
if (params.fileId.empty()) {
if (params.metadata.empty()) {
return {EINVAL, {}, {}};
}
id = IncrementalService::idFromMetadata(params.metadata);
} else if (params.fileId.size() != sizeof(id)) {
return {EINVAL, {}, {}};
} else {
memcpy(&id, params.fileId.data(), sizeof(id));
}
incfs::NewFileParams nfp;
nfp.size = params.size;
nfp.metadata = {(const char*)params.metadata.data(), (IncFsSize)params.metadata.size()};
if (!params.signature) {
nfp.signature = {};
} else {
nfp.signature = {(const char*)params.signature->data(),
(IncFsSize)params.signature->size()};
}
return {0, id, nfp};
}
binder::Status BinderIncrementalService::makeFile(
int32_t storageId, const std::string& path,
const ::android::os::incremental::IncrementalNewFileParams& params, int32_t* _aidl_return) {
auto [err, fileId, nfp] = toMakeFileParams(params);
if (err) {
*_aidl_return = err;
return ok();
}
*_aidl_return = mImpl.makeFile(storageId, path, 0777, fileId, nfp);
return ok();
}
binder::Status BinderIncrementalService::makeFileFromRange(int32_t storageId,
const std::string& targetPath,
const std::string& sourcePath,
int64_t start, int64_t end,
int32_t* _aidl_return) {
// TODO(b/136132412): implement this
*_aidl_return = ENOSYS; // not implemented
return ok();
}
binder::Status BinderIncrementalService::makeLink(int32_t sourceStorageId,
const std::string& sourcePath,
int32_t destStorageId,
const std::string& destPath,
int32_t* _aidl_return) {
*_aidl_return = mImpl.link(sourceStorageId, sourcePath, destStorageId, destPath);
return ok();
}
binder::Status BinderIncrementalService::unlink(int32_t storageId, const std::string& path,
int32_t* _aidl_return) {
*_aidl_return = mImpl.unlink(storageId, path);
return ok();
}
binder::Status BinderIncrementalService::isFileRangeLoaded(int32_t storageId,
const std::string& path, int64_t start,
int64_t end, bool* _aidl_return) {
// TODO: implement
*_aidl_return = false;
return ok();
}
binder::Status BinderIncrementalService::getMetadataByPath(int32_t storageId,
const std::string& path,
std::vector<uint8_t>* _aidl_return) {
auto metadata = mImpl.getMetadata(storageId, path);
_aidl_return->assign(metadata.begin(), metadata.end());
return ok();
}
static FileId toFileId(const std::vector<uint8_t>& id) {
FileId fid;
memcpy(&fid, id.data(), id.size());
return fid;
}
binder::Status BinderIncrementalService::getMetadataById(int32_t storageId,
const std::vector<uint8_t>& id,
std::vector<uint8_t>* _aidl_return) {
if (id.size() != sizeof(incfs::FileId)) {
return ok();
}
auto fid = toFileId(id);
auto metadata = mImpl.getMetadata(storageId, fid);
_aidl_return->assign(metadata.begin(), metadata.end());
return ok();
}
binder::Status BinderIncrementalService::makeDirectories(int32_t storageId, const std::string& path,
int32_t* _aidl_return) {
*_aidl_return = mImpl.makeDirs(storageId, path);
return ok();
}
binder::Status BinderIncrementalService::startLoading(int32_t storageId, bool* _aidl_return) {
*_aidl_return = mImpl.startLoading(storageId);
return ok();
}
binder::Status BinderIncrementalService::configureNativeBinaries(
int32_t storageId, const std::string& apkFullPath, const std::string& libDirRelativePath,
const std::string& abi, bool extractNativeLibs, bool* _aidl_return) {
*_aidl_return = mImpl.configureNativeBinaries(storageId, apkFullPath, libDirRelativePath, abi,
extractNativeLibs);
return ok();
}
binder::Status BinderIncrementalService::waitForNativeBinariesExtraction(int storageId,
bool* _aidl_return) {
*_aidl_return = mImpl.waitForNativeBinariesExtraction(storageId);
return ok();
}
} // namespace android::os::incremental
jlong Incremental_IncrementalService_Start(JNIEnv* env) {
return (jlong)android::os::incremental::BinderIncrementalService::start(env);
}
void Incremental_IncrementalService_OnSystemReady(jlong self) {
if (self) {
((android::os::incremental::BinderIncrementalService*)self)->onSystemReady();
}
}
void Incremental_IncrementalService_OnDump(jlong self, jint fd) {
if (self) {
((android::os::incremental::BinderIncrementalService*)self)->dump(fd, {});
} else {
dprintf(fd, "BinderIncrementalService is stopped.");
}
}
|