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