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 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 17 | #define LOG_TAG "IncrementalService" |
| 18 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 19 | #include "ServiceWrappers.h" |
| 20 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 21 | #include <MountRegistry.h> |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 22 | #include <android-base/logging.h> |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 23 | #include <android/content/pm/IDataLoaderManager.h> |
| 24 | #include <android/os/IVold.h> |
| 25 | #include <binder/AppOpsManager.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 26 | #include <utils/String16.h> |
| 27 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 28 | #include "IncrementalServiceValidation.h" |
| 29 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 30 | using namespace std::literals; |
| 31 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 32 | namespace android::incremental { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 33 | |
| 34 | static constexpr auto kVoldServiceName = "vold"sv; |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 35 | static constexpr auto kDataLoaderManagerName = "dataloader_manager"sv; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 36 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 37 | class RealVoldService : public VoldServiceWrapper { |
| 38 | public: |
Yurii Zubrytskyi | 510037b | 2020-04-22 15:46:21 -0700 | [diff] [blame] | 39 | RealVoldService(sp<os::IVold> vold) : mInterface(std::move(vold)) {} |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 40 | ~RealVoldService() = default; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 41 | binder::Status mountIncFs( |
| 42 | const std::string& backingPath, const std::string& targetDir, int32_t flags, |
| 43 | os::incremental::IncrementalFileSystemControlParcel* _aidl_return) const final { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 44 | return mInterface->mountIncFs(backingPath, targetDir, flags, _aidl_return); |
| 45 | } |
| 46 | binder::Status unmountIncFs(const std::string& dir) const final { |
| 47 | return mInterface->unmountIncFs(dir); |
| 48 | } |
| 49 | binder::Status bindMount(const std::string& sourceDir, |
| 50 | const std::string& targetDir) const final { |
| 51 | return mInterface->bindMount(sourceDir, targetDir); |
| 52 | } |
| 53 | binder::Status setIncFsMountOptions( |
| 54 | const ::android::os::incremental::IncrementalFileSystemControlParcel& control, |
| 55 | bool enableReadLogs) const final { |
| 56 | return mInterface->setIncFsMountOptions(control, enableReadLogs); |
| 57 | } |
| 58 | |
| 59 | private: |
| 60 | sp<os::IVold> mInterface; |
| 61 | }; |
| 62 | |
| 63 | class RealDataLoaderManager : public DataLoaderManagerWrapper { |
| 64 | public: |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 65 | RealDataLoaderManager(sp<content::pm::IDataLoaderManager> manager) |
| 66 | : mInterface(std::move(manager)) {} |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 67 | ~RealDataLoaderManager() = default; |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame] | 68 | binder::Status bindToDataLoader(MountId mountId, |
| 69 | const content::pm::DataLoaderParamsParcel& params, |
| 70 | const sp<content::pm::IDataLoaderStatusListener>& listener, |
| 71 | bool* _aidl_return) const final { |
| 72 | return mInterface->bindToDataLoader(mountId, params, listener, _aidl_return); |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 73 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 74 | binder::Status getDataLoader(MountId mountId, |
| 75 | sp<content::pm::IDataLoader>* _aidl_return) const final { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 76 | return mInterface->getDataLoader(mountId, _aidl_return); |
| 77 | } |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame] | 78 | binder::Status unbindFromDataLoader(MountId mountId) const final { |
| 79 | return mInterface->unbindFromDataLoader(mountId); |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | private: |
| 83 | sp<content::pm::IDataLoaderManager> mInterface; |
| 84 | }; |
| 85 | |
| 86 | class RealAppOpsManager : public AppOpsManagerWrapper { |
| 87 | public: |
| 88 | ~RealAppOpsManager() = default; |
| 89 | binder::Status checkPermission(const char* permission, const char* operation, |
| 90 | const char* package) const final { |
| 91 | return android::incremental::CheckPermissionForDataDelivery(permission, operation, package); |
| 92 | } |
| 93 | void startWatchingMode(int32_t op, const String16& packageName, |
| 94 | const sp<IAppOpsCallback>& callback) final { |
| 95 | mAppOpsManager.startWatchingMode(op, packageName, callback); |
| 96 | } |
| 97 | void stopWatchingMode(const sp<IAppOpsCallback>& callback) final { |
| 98 | mAppOpsManager.stopWatchingMode(callback); |
| 99 | } |
| 100 | |
| 101 | private: |
| 102 | android::AppOpsManager mAppOpsManager; |
| 103 | }; |
| 104 | |
| 105 | class RealJniWrapper final : public JniWrapper { |
| 106 | public: |
| 107 | RealJniWrapper(JavaVM* jvm); |
| 108 | void initializeForCurrentThread() const final; |
| 109 | |
| 110 | static JavaVM* getJvm(JNIEnv* env); |
| 111 | |
| 112 | private: |
| 113 | JavaVM* const mJvm; |
| 114 | }; |
| 115 | |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame^] | 116 | class RealLooperWrapper final : public LooperWrapper { |
| 117 | public: |
| 118 | int addFd(int fd, int ident, int events, android::Looper_callbackFunc callback, |
| 119 | void* data) final { |
| 120 | return mLooper.addFd(fd, ident, events, callback, data); |
| 121 | } |
| 122 | int removeFd(int fd) final { return mLooper.removeFd(fd); } |
| 123 | void wake() final { return mLooper.wake(); } |
| 124 | int pollAll(int timeoutMillis) final { return mLooper.pollAll(timeoutMillis); } |
| 125 | |
| 126 | private: |
| 127 | struct Looper : public android::Looper { |
| 128 | Looper() : android::Looper(/*allowNonCallbacks=*/false) {} |
| 129 | ~Looper() {} |
| 130 | } mLooper; |
| 131 | }; |
| 132 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 133 | class RealIncFs : public IncFsWrapper { |
| 134 | public: |
| 135 | RealIncFs() = default; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 136 | ~RealIncFs() final = default; |
| 137 | void listExistingMounts(const ExistingMountCallback& cb) const final { |
| 138 | for (auto mount : incfs::defaultMountRegistry().copyMounts()) { |
| 139 | auto binds = mount.binds(); // span() doesn't like rvalue containers, needs to save it. |
| 140 | cb(mount.root(), mount.backingDir(), binds); |
| 141 | } |
| 142 | } |
| 143 | Control openMount(std::string_view path) const final { return incfs::open(path); } |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 144 | Control createControl(IncFsFd cmd, IncFsFd pendingReads, IncFsFd logs) const final { |
| 145 | return incfs::createControl(cmd, pendingReads, logs); |
| 146 | } |
| 147 | ErrorCode makeFile(const Control& control, std::string_view path, int mode, FileId id, |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 148 | incfs::NewFileParams params) const final { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 149 | return incfs::makeFile(control, path, mode, id, params); |
| 150 | } |
| 151 | ErrorCode makeDir(const Control& control, std::string_view path, int mode) const final { |
| 152 | return incfs::makeDir(control, path, mode); |
| 153 | } |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 154 | ErrorCode makeDirs(const Control& control, std::string_view path, int mode) const final { |
| 155 | return incfs::makeDirs(control, path, mode); |
| 156 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 157 | incfs::RawMetadata getMetadata(const Control& control, FileId fileid) const final { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 158 | return incfs::getMetadata(control, fileid); |
| 159 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 160 | incfs::RawMetadata getMetadata(const Control& control, std::string_view path) const final { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 161 | return incfs::getMetadata(control, path); |
| 162 | } |
| 163 | FileId getFileId(const Control& control, std::string_view path) const final { |
| 164 | return incfs::getFileId(control, path); |
| 165 | } |
| 166 | ErrorCode link(const Control& control, std::string_view from, std::string_view to) const final { |
| 167 | return incfs::link(control, from, to); |
| 168 | } |
| 169 | ErrorCode unlink(const Control& control, std::string_view path) const final { |
| 170 | return incfs::unlink(control, path); |
| 171 | } |
| 172 | base::unique_fd openForSpecialOps(const Control& control, FileId id) const final { |
| 173 | return base::unique_fd{incfs::openForSpecialOps(control, id).release()}; |
| 174 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 175 | ErrorCode writeBlocks(std::span<const incfs::DataBlock> blocks) const final { |
| 176 | return incfs::writeBlocks({blocks.data(), size_t(blocks.size())}); |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 177 | } |
| 178 | }; |
| 179 | |
| 180 | RealServiceManager::RealServiceManager(sp<IServiceManager> serviceManager, JNIEnv* env) |
| 181 | : mServiceManager(std::move(serviceManager)), mJvm(RealJniWrapper::getJvm(env)) {} |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 182 | |
| 183 | template <class INTERFACE> |
| 184 | sp<INTERFACE> RealServiceManager::getRealService(std::string_view serviceName) const { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 185 | sp<IBinder> binder = |
| 186 | mServiceManager->getService(String16(serviceName.data(), serviceName.size())); |
| 187 | if (!binder) { |
| 188 | return nullptr; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 189 | } |
| 190 | return interface_cast<INTERFACE>(binder); |
| 191 | } |
| 192 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 193 | std::unique_ptr<VoldServiceWrapper> RealServiceManager::getVoldService() { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 194 | sp<os::IVold> vold = RealServiceManager::getRealService<os::IVold>(kVoldServiceName); |
| 195 | if (vold != 0) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 196 | return std::make_unique<RealVoldService>(vold); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 197 | } |
| 198 | return nullptr; |
| 199 | } |
| 200 | |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 201 | std::unique_ptr<DataLoaderManagerWrapper> RealServiceManager::getDataLoaderManager() { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 202 | sp<content::pm::IDataLoaderManager> manager = |
| 203 | RealServiceManager::getRealService<content::pm::IDataLoaderManager>( |
| 204 | kDataLoaderManagerName); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 205 | if (manager) { |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 206 | return std::make_unique<RealDataLoaderManager>(manager); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 207 | } |
| 208 | return nullptr; |
| 209 | } |
| 210 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 211 | std::unique_ptr<IncFsWrapper> RealServiceManager::getIncFs() { |
| 212 | return std::make_unique<RealIncFs>(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 215 | std::unique_ptr<AppOpsManagerWrapper> RealServiceManager::getAppOpsManager() { |
| 216 | return std::make_unique<RealAppOpsManager>(); |
| 217 | } |
| 218 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 219 | std::unique_ptr<JniWrapper> RealServiceManager::getJni() { |
| 220 | return std::make_unique<RealJniWrapper>(mJvm); |
| 221 | } |
| 222 | |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame^] | 223 | std::unique_ptr<LooperWrapper> RealServiceManager::getLooper() { |
| 224 | return std::make_unique<RealLooperWrapper>(); |
| 225 | } |
| 226 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 227 | static JavaVM* getJavaVm(JNIEnv* env) { |
| 228 | CHECK(env); |
| 229 | JavaVM* jvm = nullptr; |
| 230 | env->GetJavaVM(&jvm); |
| 231 | CHECK(jvm); |
| 232 | return jvm; |
| 233 | } |
| 234 | |
| 235 | static JNIEnv* getJniEnv(JavaVM* vm) { |
| 236 | JNIEnv* env; |
| 237 | if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) { |
| 238 | return nullptr; |
| 239 | } |
| 240 | return env; |
| 241 | } |
| 242 | |
| 243 | static JNIEnv* getOrAttachJniEnv(JavaVM* jvm) { |
| 244 | if (!jvm) { |
| 245 | LOG(ERROR) << "No JVM instance"; |
| 246 | return nullptr; |
| 247 | } |
| 248 | |
| 249 | JNIEnv* env = getJniEnv(jvm); |
| 250 | if (!env) { |
| 251 | int result = jvm->AttachCurrentThread(&env, nullptr); |
| 252 | if (result != JNI_OK) { |
| 253 | LOG(ERROR) << "JVM thread attach failed: " << result; |
| 254 | return nullptr; |
| 255 | } |
| 256 | struct VmDetacher { |
| 257 | VmDetacher(JavaVM* vm) : mVm(vm) {} |
| 258 | ~VmDetacher() { mVm->DetachCurrentThread(); } |
| 259 | |
| 260 | private: |
| 261 | JavaVM* const mVm; |
| 262 | }; |
| 263 | static thread_local VmDetacher detacher(jvm); |
| 264 | } |
| 265 | |
| 266 | return env; |
| 267 | } |
| 268 | |
| 269 | RealJniWrapper::RealJniWrapper(JavaVM* jvm) : mJvm(jvm) { |
| 270 | CHECK(!!mJvm) << "JVM is unavailable"; |
| 271 | } |
| 272 | |
| 273 | void RealJniWrapper::initializeForCurrentThread() const { |
| 274 | (void)getOrAttachJniEnv(mJvm); |
| 275 | } |
| 276 | |
| 277 | JavaVM* RealJniWrapper::getJvm(JNIEnv* env) { |
| 278 | return getJavaVm(env); |
| 279 | } |
| 280 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 281 | } // namespace android::incremental |