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> |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 21 | #include <android/os/IVold.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 22 | #include <binder/IResultReceiver.h> |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 23 | #include <binder/PermissionCache.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 24 | #include <incfs.h> |
| 25 | |
| 26 | #include "ServiceWrappers.h" |
| 27 | #include "jni.h" |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 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( |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 120 | 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 Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 126 | *_aidl_return = |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 127 | mImpl.createStorage(path, const_cast<content::pm::DataLoaderParamsParcel&&>(params), |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 128 | android::incremental::IncrementalService::CreateOptions(createMode), |
| 129 | statusListener, |
| 130 | const_cast<StorageHealthCheckParams&&>(healthCheckParams), |
| 131 | healthListener); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 132 | return ok(); |
| 133 | } |
| 134 | |
| 135 | binder::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 | |
| 146 | binder::Status BinderIncrementalService::makeBindMount(int32_t storageId, |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 147 | const std::string& sourcePath, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 148 | const std::string& targetFullPath, |
| 149 | int32_t bindType, int32_t* _aidl_return) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 150 | *_aidl_return = mImpl.bind(storageId, sourcePath, targetFullPath, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 151 | android::incremental::IncrementalService::BindKind(bindType)); |
| 152 | return ok(); |
| 153 | } |
| 154 | |
| 155 | binder::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 | |
| 162 | binder::Status BinderIncrementalService::deleteStorage(int32_t storageId) { |
| 163 | mImpl.deleteStorage(storageId); |
| 164 | return ok(); |
| 165 | } |
| 166 | |
Alex Buynytskyy | 3697d9e | 2020-06-06 20:15:58 -0700 | [diff] [blame] | 167 | binder::Status BinderIncrementalService::disableReadLogs(int32_t storageId) { |
| 168 | mImpl.disableReadLogs(storageId); |
| 169 | return ok(); |
| 170 | } |
| 171 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 172 | binder::Status BinderIncrementalService::makeDirectory(int32_t storageId, const std::string& path, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 173 | int32_t* _aidl_return) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 174 | *_aidl_return = mImpl.makeDir(storageId, path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 175 | return ok(); |
| 176 | } |
| 177 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 178 | static std::tuple<int, incfs::FileId, incfs::NewFileParams> toMakeFileParams( |
| 179 | const android::os::incremental::IncrementalNewFileParams& params) { |
| 180 | incfs::FileId id; |
| 181 | if (params.fileId.empty()) { |
| 182 | if (params.metadata.empty()) { |
| 183 | return {EINVAL, {}, {}}; |
| 184 | } |
| 185 | id = IncrementalService::idFromMetadata(params.metadata); |
| 186 | } else if (params.fileId.size() != sizeof(id)) { |
| 187 | return {EINVAL, {}, {}}; |
| 188 | } else { |
| 189 | memcpy(&id, params.fileId.data(), sizeof(id)); |
| 190 | } |
| 191 | incfs::NewFileParams nfp; |
| 192 | nfp.size = params.size; |
| 193 | nfp.metadata = {(const char*)params.metadata.data(), (IncFsSize)params.metadata.size()}; |
| 194 | if (!params.signature) { |
Alex Buynytskyy | f5e605a | 2020-03-13 13:31:12 -0700 | [diff] [blame] | 195 | nfp.signature = {}; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 196 | } else { |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 197 | nfp.signature = {(const char*)params.signature->data(), |
| 198 | (IncFsSize)params.signature->size()}; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 199 | } |
| 200 | return {0, id, nfp}; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 203 | binder::Status BinderIncrementalService::makeFile( |
| 204 | int32_t storageId, const std::string& path, |
| 205 | const ::android::os::incremental::IncrementalNewFileParams& params, int32_t* _aidl_return) { |
| 206 | auto [err, fileId, nfp] = toMakeFileParams(params); |
| 207 | if (err) { |
| 208 | *_aidl_return = err; |
| 209 | return ok(); |
| 210 | } |
| 211 | |
Songchun Fan | 54c6aed | 2020-01-31 16:52:41 -0800 | [diff] [blame] | 212 | *_aidl_return = mImpl.makeFile(storageId, path, 0777, fileId, nfp); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 213 | return ok(); |
| 214 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 215 | binder::Status BinderIncrementalService::makeFileFromRange(int32_t storageId, |
| 216 | const std::string& targetPath, |
| 217 | const std::string& sourcePath, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 218 | int64_t start, int64_t end, |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 219 | int32_t* _aidl_return) { |
| 220 | // TODO(b/136132412): implement this |
| 221 | *_aidl_return = ENOSYS; // not implemented |
| 222 | return ok(); |
| 223 | } |
| 224 | |
| 225 | binder::Status BinderIncrementalService::makeLink(int32_t sourceStorageId, |
| 226 | const std::string& sourcePath, |
| 227 | int32_t destStorageId, |
| 228 | const std::string& destPath, |
| 229 | int32_t* _aidl_return) { |
| 230 | *_aidl_return = mImpl.link(sourceStorageId, sourcePath, destStorageId, destPath); |
| 231 | return ok(); |
| 232 | } |
| 233 | |
| 234 | binder::Status BinderIncrementalService::unlink(int32_t storageId, const std::string& path, |
| 235 | int32_t* _aidl_return) { |
| 236 | *_aidl_return = mImpl.unlink(storageId, path); |
| 237 | return ok(); |
| 238 | } |
| 239 | |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame^] | 240 | binder::Status BinderIncrementalService::getLoadingProgress(int32_t storageId, |
| 241 | float* _aidl_return) { |
| 242 | *_aidl_return = mImpl.getLoadingProgress(storageId); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 243 | return ok(); |
| 244 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 245 | |
| 246 | binder::Status BinderIncrementalService::getMetadataByPath(int32_t storageId, |
| 247 | const std::string& path, |
| 248 | std::vector<uint8_t>* _aidl_return) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 249 | auto metadata = mImpl.getMetadata(storageId, path); |
| 250 | _aidl_return->assign(metadata.begin(), metadata.end()); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 251 | return ok(); |
| 252 | } |
| 253 | |
| 254 | static FileId toFileId(const std::vector<uint8_t>& id) { |
| 255 | FileId fid; |
| 256 | memcpy(&fid, id.data(), id.size()); |
| 257 | return fid; |
| 258 | } |
| 259 | |
| 260 | binder::Status BinderIncrementalService::getMetadataById(int32_t storageId, |
| 261 | const std::vector<uint8_t>& id, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 262 | std::vector<uint8_t>* _aidl_return) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 263 | if (id.size() != sizeof(incfs::FileId)) { |
| 264 | return ok(); |
| 265 | } |
| 266 | auto fid = toFileId(id); |
| 267 | auto metadata = mImpl.getMetadata(storageId, fid); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 268 | _aidl_return->assign(metadata.begin(), metadata.end()); |
| 269 | return ok(); |
| 270 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 271 | |
| 272 | binder::Status BinderIncrementalService::makeDirectories(int32_t storageId, const std::string& path, |
| 273 | int32_t* _aidl_return) { |
| 274 | *_aidl_return = mImpl.makeDirs(storageId, path); |
| 275 | return ok(); |
| 276 | } |
| 277 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 278 | binder::Status BinderIncrementalService::startLoading(int32_t storageId, bool* _aidl_return) { |
| 279 | *_aidl_return = mImpl.startLoading(storageId); |
| 280 | return ok(); |
| 281 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 282 | |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 283 | binder::Status BinderIncrementalService::configureNativeBinaries( |
| 284 | int32_t storageId, const std::string& apkFullPath, const std::string& libDirRelativePath, |
Songchun Fan | 14f6c3c | 2020-05-21 18:19:07 -0700 | [diff] [blame] | 285 | const std::string& abi, bool extractNativeLibs, bool* _aidl_return) { |
| 286 | *_aidl_return = mImpl.configureNativeBinaries(storageId, apkFullPath, libDirRelativePath, abi, |
| 287 | extractNativeLibs); |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 288 | return ok(); |
| 289 | } |
| 290 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 291 | binder::Status BinderIncrementalService::waitForNativeBinariesExtraction(int storageId, |
| 292 | bool* _aidl_return) { |
| 293 | *_aidl_return = mImpl.waitForNativeBinariesExtraction(storageId); |
| 294 | return ok(); |
| 295 | } |
| 296 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 297 | } // namespace android::os::incremental |
| 298 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 299 | jlong Incremental_IncrementalService_Start(JNIEnv* env) { |
| 300 | return (jlong)android::os::incremental::BinderIncrementalService::start(env); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 301 | } |
| 302 | void Incremental_IncrementalService_OnSystemReady(jlong self) { |
| 303 | if (self) { |
| 304 | ((android::os::incremental::BinderIncrementalService*)self)->onSystemReady(); |
| 305 | } |
| 306 | } |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 307 | void Incremental_IncrementalService_OnDump(jlong self, jint fd) { |
| 308 | if (self) { |
| 309 | ((android::os::incremental::BinderIncrementalService*)self)->dump(fd, {}); |
| 310 | } else { |
| 311 | dprintf(fd, "BinderIncrementalService is stopped."); |
| 312 | } |
| 313 | } |