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 | |
| 19 | #include <binder/IResultReceiver.h> |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame^] | 20 | #include <binder/PermissionCache.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 21 | #include <incfs.h> |
| 22 | |
| 23 | #include "ServiceWrappers.h" |
| 24 | #include "jni.h" |
| 25 | #include "nativehelper/JNIHelp.h" |
| 26 | #include "path.h" |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame^] | 27 | #include <android-base/logging.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 28 | |
| 29 | using namespace std::literals; |
| 30 | using namespace android::incremental; |
| 31 | |
| 32 | namespace android::os::incremental { |
| 33 | |
| 34 | static constexpr auto kAndroidDataEnv = "ANDROID_DATA"sv; |
| 35 | static constexpr auto kDataDir = "/data"sv; |
| 36 | static constexpr auto kIncrementalSubDir = "incremental"sv; |
| 37 | |
| 38 | static 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 | |
| 46 | static bool incFsEnabled() { |
| 47 | // TODO(b/136132412): use vold to check /sys/fs/incfs/version (per selinux compliance) |
| 48 | return incfs::enabled(); |
| 49 | } |
| 50 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 51 | static bool incFsValid(const sp<IVold>& vold) { |
| 52 | bool enabled = false; |
| 53 | auto status = vold->incFsEnabled(&enabled); |
| 54 | if (!status.isOk() || !enabled) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | BinderIncrementalService::BinderIncrementalService(const sp<IServiceManager>& sm) |
| 61 | : mImpl(RealServiceManager(sm), getIncrementalDir()) {} |
| 62 | |
| 63 | BinderIncrementalService* 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 Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 79 | if (!incFsValid(vold)) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 80 | 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 Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 91 | // 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] | 92 | return self.get(); |
| 93 | } |
| 94 | |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame^] | 95 | status_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 Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void BinderIncrementalService::onSystemReady() { |
| 105 | mImpl.onSystemReady(); |
| 106 | } |
| 107 | |
| 108 | static binder::Status ok() { |
| 109 | return binder::Status::ok(); |
| 110 | } |
| 111 | |
| 112 | binder::Status BinderIncrementalService::openStorage(const std::string& path, |
| 113 | int32_t* _aidl_return) { |
| 114 | *_aidl_return = mImpl.openStorage(path); |
| 115 | return ok(); |
| 116 | } |
| 117 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 118 | binder::Status BinderIncrementalService::createStorage(const std::string& path, |
| 119 | const DataLoaderParamsParcel& params, |
| 120 | int32_t createMode, int32_t* _aidl_return) { |
Songchun Fan | 54c6aed | 2020-01-31 16:52:41 -0800 | [diff] [blame] | 121 | *_aidl_return = mImpl.createStorage(path, const_cast<DataLoaderParamsParcel&&>(params), |
| 122 | android::incremental::IncrementalService::CreateOptions( |
| 123 | createMode)); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 124 | return ok(); |
| 125 | } |
| 126 | |
| 127 | binder::Status BinderIncrementalService::createLinkedStorage(const std::string& path, |
| 128 | int32_t otherStorageId, |
| 129 | int32_t createMode, |
| 130 | int32_t* _aidl_return) { |
| 131 | *_aidl_return = |
| 132 | mImpl.createLinkedStorage(path, otherStorageId, |
| 133 | android::incremental::IncrementalService::CreateOptions( |
| 134 | createMode)); |
| 135 | return ok(); |
| 136 | } |
| 137 | |
| 138 | binder::Status BinderIncrementalService::makeBindMount(int32_t storageId, |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 139 | const std::string& sourcePath, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 140 | const std::string& targetFullPath, |
| 141 | int32_t bindType, int32_t* _aidl_return) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 142 | *_aidl_return = mImpl.bind(storageId, sourcePath, targetFullPath, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 143 | android::incremental::IncrementalService::BindKind(bindType)); |
| 144 | return ok(); |
| 145 | } |
| 146 | |
| 147 | binder::Status BinderIncrementalService::deleteBindMount(int32_t storageId, |
| 148 | const std::string& targetFullPath, |
| 149 | int32_t* _aidl_return) { |
| 150 | *_aidl_return = mImpl.unbind(storageId, targetFullPath); |
| 151 | return ok(); |
| 152 | } |
| 153 | |
| 154 | binder::Status BinderIncrementalService::deleteStorage(int32_t storageId) { |
| 155 | mImpl.deleteStorage(storageId); |
| 156 | return ok(); |
| 157 | } |
| 158 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 159 | binder::Status BinderIncrementalService::makeDirectory(int32_t storageId, const std::string& path, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 160 | int32_t* _aidl_return) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 161 | *_aidl_return = mImpl.makeDir(storageId, path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 162 | return ok(); |
| 163 | } |
| 164 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 165 | static std::tuple<int, incfs::FileId, incfs::NewFileParams> toMakeFileParams( |
| 166 | const android::os::incremental::IncrementalNewFileParams& params) { |
| 167 | incfs::FileId id; |
| 168 | if (params.fileId.empty()) { |
| 169 | if (params.metadata.empty()) { |
| 170 | return {EINVAL, {}, {}}; |
| 171 | } |
| 172 | id = IncrementalService::idFromMetadata(params.metadata); |
| 173 | } else if (params.fileId.size() != sizeof(id)) { |
| 174 | return {EINVAL, {}, {}}; |
| 175 | } else { |
| 176 | memcpy(&id, params.fileId.data(), sizeof(id)); |
| 177 | } |
| 178 | incfs::NewFileParams nfp; |
| 179 | nfp.size = params.size; |
| 180 | nfp.metadata = {(const char*)params.metadata.data(), (IncFsSize)params.metadata.size()}; |
| 181 | if (!params.signature) { |
| 182 | nfp.verification = {}; |
| 183 | } else { |
| 184 | nfp.verification.hashAlgorithm = IncFsHashAlgortithm(params.signature->hashAlgorithm); |
| 185 | nfp.verification.rootHash = {(const char*)params.signature->rootHash.data(), |
| 186 | (IncFsSize)params.signature->rootHash.size()}; |
| 187 | nfp.verification.additionalData = {(const char*)params.signature->additionalData.data(), |
| 188 | (IncFsSize)params.signature->additionalData.size()}; |
| 189 | nfp.verification.signature = {(const char*)params.signature->signature.data(), |
| 190 | (IncFsSize)params.signature->signature.size()}; |
| 191 | } |
| 192 | return {0, id, nfp}; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 195 | binder::Status BinderIncrementalService::makeFile( |
| 196 | int32_t storageId, const std::string& path, |
| 197 | const ::android::os::incremental::IncrementalNewFileParams& params, int32_t* _aidl_return) { |
| 198 | auto [err, fileId, nfp] = toMakeFileParams(params); |
| 199 | if (err) { |
| 200 | *_aidl_return = err; |
| 201 | return ok(); |
| 202 | } |
| 203 | |
Songchun Fan | 54c6aed | 2020-01-31 16:52:41 -0800 | [diff] [blame] | 204 | *_aidl_return = mImpl.makeFile(storageId, path, 0777, fileId, nfp); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 205 | return ok(); |
| 206 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 207 | binder::Status BinderIncrementalService::makeFileFromRange(int32_t storageId, |
| 208 | const std::string& targetPath, |
| 209 | const std::string& sourcePath, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 210 | int64_t start, int64_t end, |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 211 | int32_t* _aidl_return) { |
| 212 | // TODO(b/136132412): implement this |
| 213 | *_aidl_return = ENOSYS; // not implemented |
| 214 | return ok(); |
| 215 | } |
| 216 | |
| 217 | binder::Status BinderIncrementalService::makeLink(int32_t sourceStorageId, |
| 218 | const std::string& sourcePath, |
| 219 | int32_t destStorageId, |
| 220 | const std::string& destPath, |
| 221 | int32_t* _aidl_return) { |
| 222 | *_aidl_return = mImpl.link(sourceStorageId, sourcePath, destStorageId, destPath); |
| 223 | return ok(); |
| 224 | } |
| 225 | |
| 226 | binder::Status BinderIncrementalService::unlink(int32_t storageId, const std::string& path, |
| 227 | int32_t* _aidl_return) { |
| 228 | *_aidl_return = mImpl.unlink(storageId, path); |
| 229 | return ok(); |
| 230 | } |
| 231 | |
| 232 | binder::Status BinderIncrementalService::isFileRangeLoaded(int32_t storageId, |
| 233 | const std::string& path, int64_t start, |
| 234 | int64_t end, bool* _aidl_return) { |
| 235 | // TODO: implement |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 236 | *_aidl_return = false; |
| 237 | return ok(); |
| 238 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 239 | |
| 240 | binder::Status BinderIncrementalService::getMetadataByPath(int32_t storageId, |
| 241 | const std::string& path, |
| 242 | std::vector<uint8_t>* _aidl_return) { |
| 243 | auto fid = mImpl.nodeFor(storageId, path); |
| 244 | if (fid != kIncFsInvalidFileId) { |
| 245 | auto metadata = mImpl.getMetadata(storageId, fid); |
| 246 | _aidl_return->assign(metadata.begin(), metadata.end()); |
| 247 | } |
| 248 | return ok(); |
| 249 | } |
| 250 | |
| 251 | static FileId toFileId(const std::vector<uint8_t>& id) { |
| 252 | FileId fid; |
| 253 | memcpy(&fid, id.data(), id.size()); |
| 254 | return fid; |
| 255 | } |
| 256 | |
| 257 | binder::Status BinderIncrementalService::getMetadataById(int32_t storageId, |
| 258 | const std::vector<uint8_t>& id, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 259 | std::vector<uint8_t>* _aidl_return) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 260 | if (id.size() != sizeof(incfs::FileId)) { |
| 261 | return ok(); |
| 262 | } |
| 263 | auto fid = toFileId(id); |
| 264 | auto metadata = mImpl.getMetadata(storageId, fid); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 265 | _aidl_return->assign(metadata.begin(), metadata.end()); |
| 266 | return ok(); |
| 267 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 268 | |
| 269 | binder::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 Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 275 | binder::Status BinderIncrementalService::startLoading(int32_t storageId, bool* _aidl_return) { |
| 276 | *_aidl_return = mImpl.startLoading(storageId); |
| 277 | return ok(); |
| 278 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 279 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 280 | } // namespace android::os::incremental |
| 281 | |
| 282 | jlong Incremental_IncrementalService_Start() { |
| 283 | return (jlong)android::os::incremental::BinderIncrementalService::start(); |
| 284 | } |
| 285 | void Incremental_IncrementalService_OnSystemReady(jlong self) { |
| 286 | if (self) { |
| 287 | ((android::os::incremental::BinderIncrementalService*)self)->onSystemReady(); |
| 288 | } |
| 289 | } |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame^] | 290 | void 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 | } |