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; |
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 | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 138 | ErrorCode makeDirs(const Control& control, std::string_view path, int mode) const final { |
| 139 | return incfs::makeDirs(control, path, mode); |
| 140 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 141 | incfs::RawMetadata getMetadata(const Control& control, FileId fileid) const final { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 142 | return incfs::getMetadata(control, fileid); |
| 143 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 144 | incfs::RawMetadata getMetadata(const Control& control, std::string_view path) const final { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 145 | return incfs::getMetadata(control, path); |
| 146 | } |
| 147 | FileId getFileId(const Control& control, std::string_view path) const final { |
| 148 | return incfs::getFileId(control, path); |
| 149 | } |
| 150 | ErrorCode link(const Control& control, std::string_view from, std::string_view to) const final { |
| 151 | return incfs::link(control, from, to); |
| 152 | } |
| 153 | ErrorCode unlink(const Control& control, std::string_view path) const final { |
| 154 | return incfs::unlink(control, path); |
| 155 | } |
| 156 | base::unique_fd openForSpecialOps(const Control& control, FileId id) const final { |
| 157 | return base::unique_fd{incfs::openForSpecialOps(control, id).release()}; |
| 158 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 159 | ErrorCode writeBlocks(std::span<const incfs::DataBlock> blocks) const final { |
| 160 | return incfs::writeBlocks({blocks.data(), size_t(blocks.size())}); |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 161 | } |
| 162 | }; |
| 163 | |
| 164 | RealServiceManager::RealServiceManager(sp<IServiceManager> serviceManager, JNIEnv* env) |
| 165 | : mServiceManager(std::move(serviceManager)), mJvm(RealJniWrapper::getJvm(env)) {} |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 166 | |
| 167 | template <class INTERFACE> |
| 168 | sp<INTERFACE> RealServiceManager::getRealService(std::string_view serviceName) const { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 169 | sp<IBinder> binder = |
| 170 | mServiceManager->getService(String16(serviceName.data(), serviceName.size())); |
| 171 | if (!binder) { |
| 172 | return nullptr; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 173 | } |
| 174 | return interface_cast<INTERFACE>(binder); |
| 175 | } |
| 176 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 177 | std::unique_ptr<VoldServiceWrapper> RealServiceManager::getVoldService() { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 178 | sp<os::IVold> vold = RealServiceManager::getRealService<os::IVold>(kVoldServiceName); |
| 179 | if (vold != 0) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 180 | return std::make_unique<RealVoldService>(vold); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 181 | } |
| 182 | return nullptr; |
| 183 | } |
| 184 | |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 185 | std::unique_ptr<DataLoaderManagerWrapper> RealServiceManager::getDataLoaderManager() { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 186 | sp<content::pm::IDataLoaderManager> manager = |
| 187 | RealServiceManager::getRealService<content::pm::IDataLoaderManager>( |
| 188 | kDataLoaderManagerName); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 189 | if (manager) { |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 190 | return std::make_unique<RealDataLoaderManager>(manager); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 191 | } |
| 192 | return nullptr; |
| 193 | } |
| 194 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 195 | std::unique_ptr<IncFsWrapper> RealServiceManager::getIncFs() { |
| 196 | return std::make_unique<RealIncFs>(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 199 | std::unique_ptr<AppOpsManagerWrapper> RealServiceManager::getAppOpsManager() { |
| 200 | return std::make_unique<RealAppOpsManager>(); |
| 201 | } |
| 202 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 203 | std::unique_ptr<JniWrapper> RealServiceManager::getJni() { |
| 204 | return std::make_unique<RealJniWrapper>(mJvm); |
| 205 | } |
| 206 | |
| 207 | static JavaVM* getJavaVm(JNIEnv* env) { |
| 208 | CHECK(env); |
| 209 | JavaVM* jvm = nullptr; |
| 210 | env->GetJavaVM(&jvm); |
| 211 | CHECK(jvm); |
| 212 | return jvm; |
| 213 | } |
| 214 | |
| 215 | static JNIEnv* getJniEnv(JavaVM* vm) { |
| 216 | JNIEnv* env; |
| 217 | if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) { |
| 218 | return nullptr; |
| 219 | } |
| 220 | return env; |
| 221 | } |
| 222 | |
| 223 | static JNIEnv* getOrAttachJniEnv(JavaVM* jvm) { |
| 224 | if (!jvm) { |
| 225 | LOG(ERROR) << "No JVM instance"; |
| 226 | return nullptr; |
| 227 | } |
| 228 | |
| 229 | JNIEnv* env = getJniEnv(jvm); |
| 230 | if (!env) { |
| 231 | int result = jvm->AttachCurrentThread(&env, nullptr); |
| 232 | if (result != JNI_OK) { |
| 233 | LOG(ERROR) << "JVM thread attach failed: " << result; |
| 234 | return nullptr; |
| 235 | } |
| 236 | struct VmDetacher { |
| 237 | VmDetacher(JavaVM* vm) : mVm(vm) {} |
| 238 | ~VmDetacher() { mVm->DetachCurrentThread(); } |
| 239 | |
| 240 | private: |
| 241 | JavaVM* const mVm; |
| 242 | }; |
| 243 | static thread_local VmDetacher detacher(jvm); |
| 244 | } |
| 245 | |
| 246 | return env; |
| 247 | } |
| 248 | |
| 249 | RealJniWrapper::RealJniWrapper(JavaVM* jvm) : mJvm(jvm) { |
| 250 | CHECK(!!mJvm) << "JVM is unavailable"; |
| 251 | } |
| 252 | |
| 253 | void RealJniWrapper::initializeForCurrentThread() const { |
| 254 | (void)getOrAttachJniEnv(mJvm); |
| 255 | } |
| 256 | |
| 257 | JavaVM* RealJniWrapper::getJvm(JNIEnv* env) { |
| 258 | return getJavaVm(env); |
| 259 | } |
| 260 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 261 | } // namespace android::incremental |