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: |
| 39 | RealVoldService(const sp<os::IVold> vold) : mInterface(std::move(vold)) {} |
| 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; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame^] | 68 | binder::Status initializeDataLoader(MountId mountId, |
| 69 | const content::pm::DataLoaderParamsParcel& params, |
| 70 | const content::pm::FileSystemControlParcel& control, |
| 71 | const sp<content::pm::IDataLoaderStatusListener>& listener, |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 72 | bool* _aidl_return) const final { |
| 73 | return mInterface->initializeDataLoader(mountId, params, control, listener, _aidl_return); |
| 74 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame^] | 75 | binder::Status getDataLoader(MountId mountId, |
| 76 | sp<content::pm::IDataLoader>* _aidl_return) const final { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 77 | return mInterface->getDataLoader(mountId, _aidl_return); |
| 78 | } |
| 79 | binder::Status destroyDataLoader(MountId mountId) const final { |
| 80 | return mInterface->destroyDataLoader(mountId); |
| 81 | } |
| 82 | |
| 83 | private: |
| 84 | sp<content::pm::IDataLoaderManager> mInterface; |
| 85 | }; |
| 86 | |
| 87 | class RealAppOpsManager : public AppOpsManagerWrapper { |
| 88 | public: |
| 89 | ~RealAppOpsManager() = default; |
| 90 | binder::Status checkPermission(const char* permission, const char* operation, |
| 91 | const char* package) const final { |
| 92 | return android::incremental::CheckPermissionForDataDelivery(permission, operation, package); |
| 93 | } |
| 94 | void startWatchingMode(int32_t op, const String16& packageName, |
| 95 | const sp<IAppOpsCallback>& callback) final { |
| 96 | mAppOpsManager.startWatchingMode(op, packageName, callback); |
| 97 | } |
| 98 | void stopWatchingMode(const sp<IAppOpsCallback>& callback) final { |
| 99 | mAppOpsManager.stopWatchingMode(callback); |
| 100 | } |
| 101 | |
| 102 | private: |
| 103 | android::AppOpsManager mAppOpsManager; |
| 104 | }; |
| 105 | |
| 106 | class RealJniWrapper final : public JniWrapper { |
| 107 | public: |
| 108 | RealJniWrapper(JavaVM* jvm); |
| 109 | void initializeForCurrentThread() const final; |
| 110 | |
| 111 | static JavaVM* getJvm(JNIEnv* env); |
| 112 | |
| 113 | private: |
| 114 | JavaVM* const mJvm; |
| 115 | }; |
| 116 | |
| 117 | class RealIncFs : public IncFsWrapper { |
| 118 | public: |
| 119 | RealIncFs() = default; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame^] | 120 | ~RealIncFs() final = default; |
| 121 | void listExistingMounts(const ExistingMountCallback& cb) const final { |
| 122 | for (auto mount : incfs::defaultMountRegistry().copyMounts()) { |
| 123 | auto binds = mount.binds(); // span() doesn't like rvalue containers, needs to save it. |
| 124 | cb(mount.root(), mount.backingDir(), binds); |
| 125 | } |
| 126 | } |
| 127 | Control openMount(std::string_view path) const final { return incfs::open(path); } |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 128 | Control createControl(IncFsFd cmd, IncFsFd pendingReads, IncFsFd logs) const final { |
| 129 | return incfs::createControl(cmd, pendingReads, logs); |
| 130 | } |
| 131 | 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^] | 132 | incfs::NewFileParams params) const final { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 133 | return incfs::makeFile(control, path, mode, id, params); |
| 134 | } |
| 135 | ErrorCode makeDir(const Control& control, std::string_view path, int mode) const final { |
| 136 | return incfs::makeDir(control, path, mode); |
| 137 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame^] | 138 | incfs::RawMetadata getMetadata(const Control& control, FileId fileid) const final { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 139 | return incfs::getMetadata(control, fileid); |
| 140 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame^] | 141 | incfs::RawMetadata getMetadata(const Control& control, std::string_view path) const final { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 142 | return incfs::getMetadata(control, path); |
| 143 | } |
| 144 | FileId getFileId(const Control& control, std::string_view path) const final { |
| 145 | return incfs::getFileId(control, path); |
| 146 | } |
| 147 | ErrorCode link(const Control& control, std::string_view from, std::string_view to) const final { |
| 148 | return incfs::link(control, from, to); |
| 149 | } |
| 150 | ErrorCode unlink(const Control& control, std::string_view path) const final { |
| 151 | return incfs::unlink(control, path); |
| 152 | } |
| 153 | base::unique_fd openForSpecialOps(const Control& control, FileId id) const final { |
| 154 | return base::unique_fd{incfs::openForSpecialOps(control, id).release()}; |
| 155 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame^] | 156 | ErrorCode writeBlocks(std::span<const incfs::DataBlock> blocks) const final { |
| 157 | return incfs::writeBlocks({blocks.data(), size_t(blocks.size())}); |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 158 | } |
| 159 | }; |
| 160 | |
| 161 | RealServiceManager::RealServiceManager(sp<IServiceManager> serviceManager, JNIEnv* env) |
| 162 | : mServiceManager(std::move(serviceManager)), mJvm(RealJniWrapper::getJvm(env)) {} |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 163 | |
| 164 | template <class INTERFACE> |
| 165 | sp<INTERFACE> RealServiceManager::getRealService(std::string_view serviceName) const { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 166 | sp<IBinder> binder = |
| 167 | mServiceManager->getService(String16(serviceName.data(), serviceName.size())); |
| 168 | if (!binder) { |
| 169 | return nullptr; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 170 | } |
| 171 | return interface_cast<INTERFACE>(binder); |
| 172 | } |
| 173 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 174 | std::unique_ptr<VoldServiceWrapper> RealServiceManager::getVoldService() { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 175 | sp<os::IVold> vold = RealServiceManager::getRealService<os::IVold>(kVoldServiceName); |
| 176 | if (vold != 0) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 177 | return std::make_unique<RealVoldService>(vold); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 178 | } |
| 179 | return nullptr; |
| 180 | } |
| 181 | |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 182 | std::unique_ptr<DataLoaderManagerWrapper> RealServiceManager::getDataLoaderManager() { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame^] | 183 | sp<content::pm::IDataLoaderManager> manager = |
| 184 | RealServiceManager::getRealService<content::pm::IDataLoaderManager>( |
| 185 | kDataLoaderManagerName); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 186 | if (manager) { |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 187 | return std::make_unique<RealDataLoaderManager>(manager); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 188 | } |
| 189 | return nullptr; |
| 190 | } |
| 191 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 192 | std::unique_ptr<IncFsWrapper> RealServiceManager::getIncFs() { |
| 193 | return std::make_unique<RealIncFs>(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 196 | std::unique_ptr<AppOpsManagerWrapper> RealServiceManager::getAppOpsManager() { |
| 197 | return std::make_unique<RealAppOpsManager>(); |
| 198 | } |
| 199 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 200 | std::unique_ptr<JniWrapper> RealServiceManager::getJni() { |
| 201 | return std::make_unique<RealJniWrapper>(mJvm); |
| 202 | } |
| 203 | |
| 204 | static JavaVM* getJavaVm(JNIEnv* env) { |
| 205 | CHECK(env); |
| 206 | JavaVM* jvm = nullptr; |
| 207 | env->GetJavaVM(&jvm); |
| 208 | CHECK(jvm); |
| 209 | return jvm; |
| 210 | } |
| 211 | |
| 212 | static JNIEnv* getJniEnv(JavaVM* vm) { |
| 213 | JNIEnv* env; |
| 214 | if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) { |
| 215 | return nullptr; |
| 216 | } |
| 217 | return env; |
| 218 | } |
| 219 | |
| 220 | static JNIEnv* getOrAttachJniEnv(JavaVM* jvm) { |
| 221 | if (!jvm) { |
| 222 | LOG(ERROR) << "No JVM instance"; |
| 223 | return nullptr; |
| 224 | } |
| 225 | |
| 226 | JNIEnv* env = getJniEnv(jvm); |
| 227 | if (!env) { |
| 228 | int result = jvm->AttachCurrentThread(&env, nullptr); |
| 229 | if (result != JNI_OK) { |
| 230 | LOG(ERROR) << "JVM thread attach failed: " << result; |
| 231 | return nullptr; |
| 232 | } |
| 233 | struct VmDetacher { |
| 234 | VmDetacher(JavaVM* vm) : mVm(vm) {} |
| 235 | ~VmDetacher() { mVm->DetachCurrentThread(); } |
| 236 | |
| 237 | private: |
| 238 | JavaVM* const mVm; |
| 239 | }; |
| 240 | static thread_local VmDetacher detacher(jvm); |
| 241 | } |
| 242 | |
| 243 | return env; |
| 244 | } |
| 245 | |
| 246 | RealJniWrapper::RealJniWrapper(JavaVM* jvm) : mJvm(jvm) { |
| 247 | CHECK(!!mJvm) << "JVM is unavailable"; |
| 248 | } |
| 249 | |
| 250 | void RealJniWrapper::initializeForCurrentThread() const { |
| 251 | (void)getOrAttachJniEnv(mJvm); |
| 252 | } |
| 253 | |
| 254 | JavaVM* RealJniWrapper::getJvm(JNIEnv* env) { |
| 255 | return getJavaVm(env); |
| 256 | } |
| 257 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame^] | 258 | } // namespace android::incremental |