1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef SYS_INSTALLER_SERVER_H 16 #define SYS_INSTALLER_SERVER_H 17 18 #include <iostream> 19 #include <shared_mutex> 20 #include <thread> 21 #include <unordered_set> 22 #include "if_system_ability_manager.h" 23 #include "sys_installer_manager.h" 24 #include "stream_installer_manager.h" 25 #include "ipc_skeleton.h" 26 #include "iremote_stub.h" 27 #include "isys_installer.h" 28 #include "status_manager.h" 29 #include "stream_status_manager.h" 30 #include "system_ability.h" 31 #include "sys_installer_common.h" 32 #include "sys_installer_stub.h" 33 34 namespace OHOS { 35 namespace SysInstaller { 36 class SysInstallerServer : public SystemAbility, public SysInstallerStub { 37 class SysInstallerExitGuard { 38 public: 39 constexpr static int MAX_RUNNING_SET_SIZE = 256; SysInstallerExitGuard(const std::string & tag)40 SysInstallerExitGuard(const std::string &tag) : tag_(tag) 41 { 42 std::lock_guard<std::shared_mutex> guard(setLock_); 43 if (runningSet_.size() < MAX_RUNNING_SET_SIZE) { 44 runningSet_.insert(tag_); 45 isTagInserted_ = true; 46 } 47 } ~SysInstallerExitGuard()48 ~SysInstallerExitGuard() 49 { 50 std::lock_guard<std::shared_mutex> guard(setLock_); 51 if (isTagInserted_) { 52 runningSet_.erase(tag_); 53 } 54 } GetRunningSet(void)55 static const auto &GetRunningSet(void) 56 { 57 std::shared_lock<std::shared_mutex> guard(setLock_); 58 return runningSet_; 59 } 60 private: 61 bool isTagInserted_ {false}; 62 std::string tag_ {}; 63 static inline std::unordered_multiset<std::string> runningSet_ {}; 64 static inline std::shared_mutex setLock_ {}; 65 }; 66 #define DEFINE_EXIT_GUARD() SysInstallerExitGuard exitGuard(__FUNCTION__) 67 public: 68 DECLARE_SYSTEM_ABILITY(SysInstallerServer); 69 DISALLOW_COPY_AND_MOVE(SysInstallerServer); 70 SysInstallerServer(int32_t systemAbilityId, bool runOnCreate = false); 71 ~SysInstallerServer() override; 72 73 int32_t SysInstallerInit(const std::string &taskId, bool bStreamUpgrade) override; 74 int32_t StartUpdatePackageZip(const std::string &taskId, const std::string &pkgPath) override; 75 int32_t StartStreamUpdate() override; 76 int32_t StopStreamUpdate() override; 77 int32_t ProcessStreamData(const BufferInfoParcel &bufferParcel) override; 78 int32_t SetUpdateCallback(const std::string &taskId, const sptr<ISysInstallerCallback> &updateCallback) override; 79 int32_t GetUpdateStatus(const std::string &taskId) override; 80 int32_t StartUpdateParaZip(const std::string &taskId, const std::string &pkgPath, 81 const std::string &location, const std::string &cfgDir) override; 82 int32_t StartDeleteParaZip(const std::string &taskId, const std::string &location, 83 const std::string &cfgDir) override; 84 int32_t AccDecompressAndVerifyPkg(const std::string &taskId, const std::string &srcPath, 85 const std::string &dstPath, const uint32_t type) override; 86 int32_t AccDeleteDir(const std::string &taskId, const std::string &dstPath) override; 87 int32_t StartUpdateVabPackageZip(const std::string &taskId, const std::vector<std::string> &pkgPath) override; 88 int32_t CancelUpdateVabPackageZip(const std::string &taskId) override; 89 int32_t StartVabMerge(const std::string &taskId) override; 90 int32_t CreateVabSnapshotCowImg(const std::unordered_map<std::string, uint64_t> &partitionInfo) override; 91 int32_t ClearVabMetadataAndCow() override; 92 int32_t GetUpdateResult(const std::string &taskId, const std::string &taskType, 93 const std::string &resultType, std::string &updateResult) override; 94 int32_t VabUpdateActive() override; 95 int32_t GetMetadataResult(const std::string &action, bool &result) override; 96 int32_t InstallCloudRom(const std::string &taskId, InstallMode installMode, 97 const std::vector<FeatureInfo> &featureInfos, RebootStatus rebootStatus) override; 98 int32_t UninstallCloudRom(const std::string &taskId, 99 const std::vector<FeatureInfo> &featureInfos, RebootStatus rebootStatus) override; 100 int32_t GetFeatureStatus(const std::vector<FeatureInfo> &featureInfos, 101 std::vector<FeatureStatus> &statusInfos) override; 102 int32_t GetAllFeatureStatus(const std::string &baseVersion, std::vector<FeatureStatus> &statusInfos) override; 103 int32_t ClearCloudRom(const std::string &baseVersion, const std::string &featureName) override; 104 int32_t ExitSysInstaller() override; 105 int32_t StartAbSync() override; 106 int32_t SetCpuAffinity(const std::string &taskId, uint32_t reservedCores) override; 107 108 bool CheckCallingPerm(void); 109 bool IsPermissionGranted(void); 110 int32_t CallbackEnter([[maybe_unused]] uint32_t code) override; 111 int32_t CallbackExit([[maybe_unused]] uint32_t code, [[maybe_unused]] int32_t result) override; 112 113 #ifndef UPDATER_UT 114 private: 115 #else 116 public: 117 #endif 118 void OnStart() override; 119 void OnStop() override; 120 121 private: 122 bool IsTaskRunning(void); 123 std::string GetRunningTask(void); 124 bool logInit_ = false; 125 bool bStreamUpgrade_ = false; 126 std::mutex sysInstallerServerLock_; 127 }; 128 } // namespace SysInstaller 129 } // namespace OHOS 130 #endif // SYS_INSTALLER_SERVER_H 131