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