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 | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 114 | *_aidl_return = |
| 115 | mImpl.createStorage(path, const_cast<DataLoaderParamsParcel&&>(params), |
| 116 | android::incremental::IncrementalService::CreateOptions( |
| 117 | createMode)); |
| 118 | return ok(); |
| 119 | } |
| 120 | |
| 121 | binder::Status BinderIncrementalService::createLinkedStorage(const std::string& path, |
| 122 | int32_t otherStorageId, |
| 123 | int32_t createMode, |
| 124 | int32_t* _aidl_return) { |
| 125 | *_aidl_return = |
| 126 | mImpl.createLinkedStorage(path, otherStorageId, |
| 127 | android::incremental::IncrementalService::CreateOptions( |
| 128 | createMode)); |
| 129 | return ok(); |
| 130 | } |
| 131 | |
| 132 | binder::Status BinderIncrementalService::makeBindMount(int32_t storageId, |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 133 | const std::string& sourcePath, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 134 | const std::string& targetFullPath, |
| 135 | int32_t bindType, int32_t* _aidl_return) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 136 | *_aidl_return = mImpl.bind(storageId, sourcePath, targetFullPath, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 137 | android::incremental::IncrementalService::BindKind(bindType)); |
| 138 | return ok(); |
| 139 | } |
| 140 | |
| 141 | binder::Status BinderIncrementalService::deleteBindMount(int32_t storageId, |
| 142 | const std::string& targetFullPath, |
| 143 | int32_t* _aidl_return) { |
| 144 | *_aidl_return = mImpl.unbind(storageId, targetFullPath); |
| 145 | return ok(); |
| 146 | } |
| 147 | |
| 148 | binder::Status BinderIncrementalService::deleteStorage(int32_t storageId) { |
| 149 | mImpl.deleteStorage(storageId); |
| 150 | return ok(); |
| 151 | } |
| 152 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 153 | binder::Status BinderIncrementalService::makeDirectory(int32_t storageId, const std::string& path, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 154 | int32_t* _aidl_return) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 155 | *_aidl_return = mImpl.makeDir(storageId, path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 156 | return ok(); |
| 157 | } |
| 158 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 159 | static std::tuple<int, incfs::FileId, incfs::NewFileParams> toMakeFileParams( |
| 160 | const android::os::incremental::IncrementalNewFileParams& params) { |
| 161 | incfs::FileId id; |
| 162 | if (params.fileId.empty()) { |
| 163 | if (params.metadata.empty()) { |
| 164 | return {EINVAL, {}, {}}; |
| 165 | } |
| 166 | id = IncrementalService::idFromMetadata(params.metadata); |
| 167 | } else if (params.fileId.size() != sizeof(id)) { |
| 168 | return {EINVAL, {}, {}}; |
| 169 | } else { |
| 170 | memcpy(&id, params.fileId.data(), sizeof(id)); |
| 171 | } |
| 172 | incfs::NewFileParams nfp; |
| 173 | nfp.size = params.size; |
| 174 | nfp.metadata = {(const char*)params.metadata.data(), (IncFsSize)params.metadata.size()}; |
| 175 | if (!params.signature) { |
| 176 | nfp.verification = {}; |
| 177 | } else { |
| 178 | nfp.verification.hashAlgorithm = IncFsHashAlgortithm(params.signature->hashAlgorithm); |
| 179 | nfp.verification.rootHash = {(const char*)params.signature->rootHash.data(), |
| 180 | (IncFsSize)params.signature->rootHash.size()}; |
| 181 | nfp.verification.additionalData = {(const char*)params.signature->additionalData.data(), |
| 182 | (IncFsSize)params.signature->additionalData.size()}; |
| 183 | nfp.verification.signature = {(const char*)params.signature->signature.data(), |
| 184 | (IncFsSize)params.signature->signature.size()}; |
| 185 | } |
| 186 | return {0, id, nfp}; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 189 | binder::Status BinderIncrementalService::makeFile( |
| 190 | int32_t storageId, const std::string& path, |
| 191 | const ::android::os::incremental::IncrementalNewFileParams& params, int32_t* _aidl_return) { |
| 192 | auto [err, fileId, nfp] = toMakeFileParams(params); |
| 193 | if (err) { |
| 194 | *_aidl_return = err; |
| 195 | return ok(); |
| 196 | } |
| 197 | |
| 198 | *_aidl_return = mImpl.makeFile(storageId, path, 0555, fileId, nfp); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 199 | return ok(); |
| 200 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 201 | binder::Status BinderIncrementalService::makeFileFromRange(int32_t storageId, |
| 202 | const std::string& targetPath, |
| 203 | const std::string& sourcePath, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 204 | int64_t start, int64_t end, |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 205 | int32_t* _aidl_return) { |
| 206 | // TODO(b/136132412): implement this |
| 207 | *_aidl_return = ENOSYS; // not implemented |
| 208 | return ok(); |
| 209 | } |
| 210 | |
| 211 | binder::Status BinderIncrementalService::makeLink(int32_t sourceStorageId, |
| 212 | const std::string& sourcePath, |
| 213 | int32_t destStorageId, |
| 214 | const std::string& destPath, |
| 215 | int32_t* _aidl_return) { |
| 216 | *_aidl_return = mImpl.link(sourceStorageId, sourcePath, destStorageId, destPath); |
| 217 | return ok(); |
| 218 | } |
| 219 | |
| 220 | binder::Status BinderIncrementalService::unlink(int32_t storageId, const std::string& path, |
| 221 | int32_t* _aidl_return) { |
| 222 | *_aidl_return = mImpl.unlink(storageId, path); |
| 223 | return ok(); |
| 224 | } |
| 225 | |
| 226 | binder::Status BinderIncrementalService::isFileRangeLoaded(int32_t storageId, |
| 227 | const std::string& path, int64_t start, |
| 228 | int64_t end, bool* _aidl_return) { |
| 229 | // TODO: implement |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 230 | *_aidl_return = false; |
| 231 | return ok(); |
| 232 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 233 | |
| 234 | binder::Status BinderIncrementalService::getMetadataByPath(int32_t storageId, |
| 235 | const std::string& path, |
| 236 | std::vector<uint8_t>* _aidl_return) { |
| 237 | auto fid = mImpl.nodeFor(storageId, path); |
| 238 | if (fid != kIncFsInvalidFileId) { |
| 239 | auto metadata = mImpl.getMetadata(storageId, fid); |
| 240 | _aidl_return->assign(metadata.begin(), metadata.end()); |
| 241 | } |
| 242 | return ok(); |
| 243 | } |
| 244 | |
| 245 | static FileId toFileId(const std::vector<uint8_t>& id) { |
| 246 | FileId fid; |
| 247 | memcpy(&fid, id.data(), id.size()); |
| 248 | return fid; |
| 249 | } |
| 250 | |
| 251 | binder::Status BinderIncrementalService::getMetadataById(int32_t storageId, |
| 252 | const std::vector<uint8_t>& id, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 253 | std::vector<uint8_t>* _aidl_return) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 254 | if (id.size() != sizeof(incfs::FileId)) { |
| 255 | return ok(); |
| 256 | } |
| 257 | auto fid = toFileId(id); |
| 258 | auto metadata = mImpl.getMetadata(storageId, fid); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 259 | _aidl_return->assign(metadata.begin(), metadata.end()); |
| 260 | return ok(); |
| 261 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 262 | |
| 263 | binder::Status BinderIncrementalService::makeDirectories(int32_t storageId, const std::string& path, |
| 264 | int32_t* _aidl_return) { |
| 265 | *_aidl_return = mImpl.makeDirs(storageId, path); |
| 266 | return ok(); |
| 267 | } |
| 268 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 269 | binder::Status BinderIncrementalService::startLoading(int32_t storageId, bool* _aidl_return) { |
| 270 | *_aidl_return = mImpl.startLoading(storageId); |
| 271 | return ok(); |
| 272 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 273 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 274 | } // namespace android::os::incremental |
| 275 | |
| 276 | jlong Incremental_IncrementalService_Start() { |
| 277 | return (jlong)android::os::incremental::BinderIncrementalService::start(); |
| 278 | } |
| 279 | void Incremental_IncrementalService_OnSystemReady(jlong self) { |
| 280 | if (self) { |
| 281 | ((android::os::incremental::BinderIncrementalService*)self)->onSystemReady(); |
| 282 | } |
| 283 | } |