Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1 | /* |
| 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 Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 19 | #include <android-base/logging.h> |
Yurii Zubrytskyi | 0cd8012 | 2020-04-09 23:08:31 -0700 | [diff] [blame] | 20 | #include <android-base/no_destructor.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 21 | #include <binder/IResultReceiver.h> |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 22 | #include <binder/PermissionCache.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 23 | #include <incfs.h> |
| 24 | |
| 25 | #include "ServiceWrappers.h" |
| 26 | #include "jni.h" |
| 27 | #include "nativehelper/JNIHelp.h" |
| 28 | #include "path.h" |
| 29 | |
| 30 | using namespace std::literals; |
| 31 | using namespace android::incremental; |
| 32 | |
| 33 | namespace android::os::incremental { |
| 34 | |
| 35 | static constexpr auto kAndroidDataEnv = "ANDROID_DATA"sv; |
| 36 | static constexpr auto kDataDir = "/data"sv; |
| 37 | static constexpr auto kIncrementalSubDir = "incremental"sv; |
| 38 | |
| 39 | static 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 | |
| 47 | static bool incFsEnabled() { |
| 48 | // TODO(b/136132412): use vold to check /sys/fs/incfs/version (per selinux compliance) |
| 49 | return incfs::enabled(); |
| 50 | } |
| 51 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 52 | static bool incFsValid(const sp<IVold>& vold) { |
| 53 | bool enabled = false; |
| 54 | auto status = vold->incFsEnabled(&enabled); |
| 55 | if (!status.isOk() || !enabled) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 56 | return false; |
| 57 | } |
| 58 | return true; |
| 59 | } |
| 60 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame^] | 61 | BinderIncrementalService::BinderIncrementalService(const sp<IServiceManager>& sm, JNIEnv* env) |
| 62 | : mImpl(RealServiceManager(sm, env), getIncrementalDir()) {} |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 63 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame^] | 64 | BinderIncrementalService* BinderIncrementalService::start(JNIEnv* env) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 65 | 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 Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 80 | if (!incFsValid(vold)) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 81 | return nullptr; |
| 82 | } |
| 83 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame^] | 84 | sp<BinderIncrementalService> self(new BinderIncrementalService(sm, env)); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 85 | 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 Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 92 | // sm->addService increments the reference count, and now we're OK with returning the pointer. |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 93 | return self.get(); |
| 94 | } |
| 95 | |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 96 | status_t BinderIncrementalService::dump(int fd, const Vector<String16>&) { |
Yurii Zubrytskyi | 0cd8012 | 2020-04-09 23:08:31 -0700 | [diff] [blame] | 97 | static const android::base::NoDestructor<String16> kDump("android.permission.DUMP"); |
| 98 | if (!PermissionCache::checkCallingPermission(*kDump)) { |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 99 | return PERMISSION_DENIED; |
| 100 | } |
| 101 | mImpl.onDump(fd); |
| 102 | return NO_ERROR; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void BinderIncrementalService::onSystemReady() { |
| 106 | mImpl.onSystemReady(); |
| 107 | } |
| 108 | |
| 109 | static binder::Status ok() { |
| 110 | return binder::Status::ok(); |
| 111 | } |
| 112 | |
| 113 | binder::Status BinderIncrementalService::openStorage(const std::string& path, |
| 114 | int32_t* _aidl_return) { |
| 115 | *_aidl_return = mImpl.openStorage(path); |
| 116 | return ok(); |
| 117 | } |
| 118 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 119 | binder::Status BinderIncrementalService::createStorage( |
| 120 | const std::string& path, const DataLoaderParamsParcel& params, |
| 121 | const ::android::sp<::android::content::pm::IDataLoaderStatusListener>& listener, |
| 122 | int32_t createMode, int32_t* _aidl_return) { |
| 123 | *_aidl_return = |
| 124 | mImpl.createStorage(path, const_cast<DataLoaderParamsParcel&&>(params), listener, |
| 125 | android::incremental::IncrementalService::CreateOptions( |
| 126 | createMode)); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 127 | return ok(); |
| 128 | } |
| 129 | |
| 130 | binder::Status BinderIncrementalService::createLinkedStorage(const std::string& path, |
| 131 | int32_t otherStorageId, |
| 132 | int32_t createMode, |
| 133 | int32_t* _aidl_return) { |
| 134 | *_aidl_return = |
| 135 | mImpl.createLinkedStorage(path, otherStorageId, |
| 136 | android::incremental::IncrementalService::CreateOptions( |
| 137 | createMode)); |
| 138 | return ok(); |
| 139 | } |
| 140 | |
| 141 | binder::Status BinderIncrementalService::makeBindMount(int32_t storageId, |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 142 | const std::string& sourcePath, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 143 | const std::string& targetFullPath, |
| 144 | int32_t bindType, int32_t* _aidl_return) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 145 | *_aidl_return = mImpl.bind(storageId, sourcePath, targetFullPath, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 146 | android::incremental::IncrementalService::BindKind(bindType)); |
| 147 | return ok(); |
| 148 | } |
| 149 | |
| 150 | binder::Status BinderIncrementalService::deleteBindMount(int32_t storageId, |
| 151 | const std::string& targetFullPath, |
| 152 | int32_t* _aidl_return) { |
| 153 | *_aidl_return = mImpl.unbind(storageId, targetFullPath); |
| 154 | return ok(); |
| 155 | } |
| 156 | |
| 157 | binder::Status BinderIncrementalService::deleteStorage(int32_t storageId) { |
| 158 | mImpl.deleteStorage(storageId); |
| 159 | return ok(); |
| 160 | } |
| 161 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 162 | binder::Status BinderIncrementalService::makeDirectory(int32_t storageId, const std::string& path, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 163 | int32_t* _aidl_return) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 164 | *_aidl_return = mImpl.makeDir(storageId, path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 165 | return ok(); |
| 166 | } |
| 167 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 168 | static std::tuple<int, incfs::FileId, incfs::NewFileParams> toMakeFileParams( |
| 169 | const android::os::incremental::IncrementalNewFileParams& params) { |
| 170 | incfs::FileId id; |
| 171 | if (params.fileId.empty()) { |
| 172 | if (params.metadata.empty()) { |
| 173 | return {EINVAL, {}, {}}; |
| 174 | } |
| 175 | id = IncrementalService::idFromMetadata(params.metadata); |
| 176 | } else if (params.fileId.size() != sizeof(id)) { |
| 177 | return {EINVAL, {}, {}}; |
| 178 | } else { |
| 179 | memcpy(&id, params.fileId.data(), sizeof(id)); |
| 180 | } |
| 181 | incfs::NewFileParams nfp; |
| 182 | nfp.size = params.size; |
| 183 | nfp.metadata = {(const char*)params.metadata.data(), (IncFsSize)params.metadata.size()}; |
| 184 | if (!params.signature) { |
Alex Buynytskyy | f5e605a | 2020-03-13 13:31:12 -0700 | [diff] [blame] | 185 | nfp.signature = {}; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 186 | } else { |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 187 | nfp.signature = {(const char*)params.signature->data(), |
| 188 | (IncFsSize)params.signature->size()}; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 189 | } |
| 190 | return {0, id, nfp}; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 193 | binder::Status BinderIncrementalService::makeFile( |
| 194 | int32_t storageId, const std::string& path, |
| 195 | const ::android::os::incremental::IncrementalNewFileParams& params, int32_t* _aidl_return) { |
| 196 | auto [err, fileId, nfp] = toMakeFileParams(params); |
| 197 | if (err) { |
| 198 | *_aidl_return = err; |
| 199 | return ok(); |
| 200 | } |
| 201 | |
Songchun Fan | 54c6aed | 2020-01-31 16:52:41 -0800 | [diff] [blame] | 202 | *_aidl_return = mImpl.makeFile(storageId, path, 0777, fileId, nfp); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 203 | return ok(); |
| 204 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 205 | binder::Status BinderIncrementalService::makeFileFromRange(int32_t storageId, |
| 206 | const std::string& targetPath, |
| 207 | const std::string& sourcePath, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 208 | int64_t start, int64_t end, |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 209 | int32_t* _aidl_return) { |
| 210 | // TODO(b/136132412): implement this |
| 211 | *_aidl_return = ENOSYS; // not implemented |
| 212 | return ok(); |
| 213 | } |
| 214 | |
| 215 | binder::Status BinderIncrementalService::makeLink(int32_t sourceStorageId, |
| 216 | const std::string& sourcePath, |
| 217 | int32_t destStorageId, |
| 218 | const std::string& destPath, |
| 219 | int32_t* _aidl_return) { |
| 220 | *_aidl_return = mImpl.link(sourceStorageId, sourcePath, destStorageId, destPath); |
| 221 | return ok(); |
| 222 | } |
| 223 | |
| 224 | binder::Status BinderIncrementalService::unlink(int32_t storageId, const std::string& path, |
| 225 | int32_t* _aidl_return) { |
| 226 | *_aidl_return = mImpl.unlink(storageId, path); |
| 227 | return ok(); |
| 228 | } |
| 229 | |
| 230 | binder::Status BinderIncrementalService::isFileRangeLoaded(int32_t storageId, |
| 231 | const std::string& path, int64_t start, |
| 232 | int64_t end, bool* _aidl_return) { |
| 233 | // TODO: implement |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 234 | *_aidl_return = false; |
| 235 | return ok(); |
| 236 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 237 | |
| 238 | binder::Status BinderIncrementalService::getMetadataByPath(int32_t storageId, |
| 239 | const std::string& path, |
| 240 | std::vector<uint8_t>* _aidl_return) { |
| 241 | auto fid = mImpl.nodeFor(storageId, path); |
| 242 | if (fid != kIncFsInvalidFileId) { |
| 243 | auto metadata = mImpl.getMetadata(storageId, fid); |
| 244 | _aidl_return->assign(metadata.begin(), metadata.end()); |
| 245 | } |
| 246 | return ok(); |
| 247 | } |
| 248 | |
| 249 | static FileId toFileId(const std::vector<uint8_t>& id) { |
| 250 | FileId fid; |
| 251 | memcpy(&fid, id.data(), id.size()); |
| 252 | return fid; |
| 253 | } |
| 254 | |
| 255 | binder::Status BinderIncrementalService::getMetadataById(int32_t storageId, |
| 256 | const std::vector<uint8_t>& id, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 257 | std::vector<uint8_t>* _aidl_return) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 258 | if (id.size() != sizeof(incfs::FileId)) { |
| 259 | return ok(); |
| 260 | } |
| 261 | auto fid = toFileId(id); |
| 262 | auto metadata = mImpl.getMetadata(storageId, fid); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 263 | _aidl_return->assign(metadata.begin(), metadata.end()); |
| 264 | return ok(); |
| 265 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 266 | |
| 267 | binder::Status BinderIncrementalService::makeDirectories(int32_t storageId, const std::string& path, |
| 268 | int32_t* _aidl_return) { |
| 269 | *_aidl_return = mImpl.makeDirs(storageId, path); |
| 270 | return ok(); |
| 271 | } |
| 272 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 273 | binder::Status BinderIncrementalService::startLoading(int32_t storageId, bool* _aidl_return) { |
| 274 | *_aidl_return = mImpl.startLoading(storageId); |
| 275 | return ok(); |
| 276 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 277 | |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 278 | binder::Status BinderIncrementalService::configureNativeBinaries( |
| 279 | int32_t storageId, const std::string& apkFullPath, const std::string& libDirRelativePath, |
| 280 | const std::string& abi, bool* _aidl_return) { |
| 281 | *_aidl_return = mImpl.configureNativeBinaries(storageId, apkFullPath, libDirRelativePath, abi); |
| 282 | return ok(); |
| 283 | } |
| 284 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 285 | binder::Status BinderIncrementalService::waitForNativeBinariesExtraction(int storageId, |
| 286 | bool* _aidl_return) { |
| 287 | *_aidl_return = mImpl.waitForNativeBinariesExtraction(storageId); |
| 288 | return ok(); |
| 289 | } |
| 290 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 291 | } // namespace android::os::incremental |
| 292 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame^] | 293 | jlong Incremental_IncrementalService_Start(JNIEnv* env) { |
| 294 | return (jlong)android::os::incremental::BinderIncrementalService::start(env); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 295 | } |
| 296 | void Incremental_IncrementalService_OnSystemReady(jlong self) { |
| 297 | if (self) { |
| 298 | ((android::os::incremental::BinderIncrementalService*)self)->onSystemReady(); |
| 299 | } |
| 300 | } |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 301 | void Incremental_IncrementalService_OnDump(jlong self, jint fd) { |
| 302 | if (self) { |
| 303 | ((android::os::incremental::BinderIncrementalService*)self)->dump(fd, {}); |
| 304 | } else { |
| 305 | dprintf(fd, "BinderIncrementalService is stopped."); |
| 306 | } |
| 307 | } |