1 /* 2 * Copyright (c) 2021-2024 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 16 #ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_CLIENT_H 17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_CLIENT_H 18 19 #include <condition_variable> 20 #include <memory> 21 #include <mutex> 22 #include <shared_mutex> 23 #include <string> 24 25 #include "nocopyable.h" 26 #include "singleton.h" 27 28 #include "app_log_wrapper.h" 29 #include "appexecfwk_errors.h" 30 #include "bundle_constants.h" 31 #include "ipc/installd_interface.h" 32 33 namespace OHOS { 34 namespace AppExecFwk { 35 class InstalldClient : public DelayedSingleton<InstalldClient> { 36 public: 37 /** 38 * @brief Create a bundle code directory through an installd proxy object. 39 * @param bundleDir Indicates the bundle code directory path that to be created. 40 * @return Returns ERR_OK if the bundle directory created successfully; returns error code otherwise. 41 */ 42 ErrCode CreateBundleDir(const std::string &bundleDir); 43 /** 44 * @brief Extract the files of a HAP module to the code directory. 45 * @param srcModulePath Indicates the HAP file path. 46 * @param targetPath normal files decompression path. 47 * @param targetSoPath so files decompression path. 48 * @param cpuAbi cpuAbi. 49 * @return Returns ERR_OK if the HAP file extracted successfully; returns error code otherwise. 50 */ 51 ErrCode ExtractModuleFiles(const std::string &srcModulePath, const std::string &targetPath, 52 const std::string &targetSoPath, const std::string &cpuAbi); 53 /** 54 * @brief Rename the module directory from temporaily path to the real path. 55 * @param oldPath Indicates the old path name. 56 * @param newPath Indicates the new path name. 57 * @return Returns ERR_OK if the module directory renamed successfully; returns error code otherwise. 58 */ 59 ErrCode RenameModuleDir(const std::string &oldPath, const std::string &newPath); 60 /** 61 * @brief Create a bundle data directory. 62 * @param createDirParam Indicates param to be set to the directory. 63 * @return Returns ERR_OK if the bundle data directory created successfully; returns error code otherwise. 64 */ 65 ErrCode CreateBundleDataDir(const CreateDirParam &createDirParam); 66 67 ErrCode CreateBundleDataDirWithVector(const std::vector<CreateDirParam> &createDirParams); 68 69 /** 70 * @brief Remove a bundle data directory. 71 * @param bundleName Indicates the bundleName data directory path that to be created. 72 * @param userid Indicates userid to be set to the directory. 73 * @return Returns ERR_OK if the bundle data directory created successfully; returns error code otherwise. 74 */ 75 ErrCode RemoveBundleDataDir(const std::string &bundleName, const int32_t userId, 76 bool isAtomicService = false, const bool async = false); 77 /** 78 * @brief Remove a module data directory. 79 * @param ModuleDir Indicates the module data directory path that to be created. 80 * @param userid Indicates userid to be set to the directory. 81 * @return Returns ERR_OK if the data directories created successfully; returns error code otherwise. 82 */ 83 ErrCode RemoveModuleDataDir(const std::string &ModuleDir, const int userid); 84 /** 85 * @brief Remove a directory. 86 * @param dir Indicates the directory path that to be removed. 87 * @return Returns ERR_OK if the directory removed successfully; returns error code otherwise. 88 */ 89 ErrCode RemoveDir(const std::string &dir); 90 /** 91 * @brief Get disk usage for dir. 92 * @param dir Indicates the directory. 93 * @param isRealPath Indicates isRealPath. 94 * @return Returns true if successfully; returns false otherwise. 95 */ 96 int64_t GetDiskUsage(const std::string &dir, bool isRealPath = false); 97 98 /** 99 * @brief Get disk usage for dir. 100 * @param path Indicates the directory vector. 101 * @return Returns true if successfully; returns false otherwise. 102 */ 103 ErrCode GetDiskUsageFromPath(const std::vector<std::string> &path, int64_t &statSize); 104 105 /** 106 * @brief Clean all files in a bundle data directory. 107 * @param bundleDir Indicates the data directory path that to be cleaned. 108 * @return Returns ERR_OK if the data directory cleaned successfully; returns error code otherwise. 109 */ 110 ErrCode CleanBundleDataDir(const std::string &bundleDir); 111 /** 112 * @brief Clean a bundle data directory. 113 * @param bundleName Indicates the bundleName data directory path that to be cleaned. 114 * @param userid Indicates userid to be set to the directory. 115 * @param appIndex Indicates app index to be set to the directory. 116 * @return Returns ERR_OK if the bundle data directory cleaned successfully; returns error code otherwise. 117 */ 118 ErrCode CleanBundleDataDirByName(const std::string &bundleName, const int userid, const int appIndex = 0); 119 /** 120 * @brief Get bundle Stats. 121 * @param bundleName Indicates the bundle name. 122 * @param userId Indicates the user Id. 123 * @param bundleStats Indicates the bundle Stats. 124 * @return Returns ERR_OK if get stats successfully; returns error code otherwise. 125 */ 126 ErrCode GetBundleStats(const std::string &bundleName, const int32_t userId, 127 std::vector<int64_t> &bundleStats, const int32_t uid = Constants::INVALID_UID, 128 const int32_t appIndex = 0, const uint32_t statFlag = 0, 129 const std::vector<std::string> &moduleNameList = {}); 130 131 ErrCode GetAllBundleStats(const int32_t userId, 132 std::vector<int64_t> &bundleStats, const std::vector<int32_t> &uids); 133 134 /** 135 * @brief Reset the installd proxy object when installd service died. 136 * @return 137 */ 138 void ResetInstalldProxy(); 139 140 /** 141 * @brief Set dir apl. 142 * @param dir Indicates the data dir. 143 * @param bundleName Indicates the bundle name. 144 * @param apl Indicates the apl type. 145 * @param isPreInstallApp Indicates the bundle install type. 146 * @param debug Indicates the bundle debug mode. 147 * @return Returns ERR_OK if set apl successfully; returns error code otherwise. 148 */ 149 ErrCode SetDirApl(const std::string &dir, const std::string &bundleName, const std::string &apl, 150 bool isPreInstallApp, bool debug); 151 152 /** 153 * @brief Get all cache file path. 154 * @param dir Indicates the data dir. 155 * @param cachesPath Indicates the cache file path. 156 * @return Returns ERR_OK if get cache file path successfully; returns error code otherwise. 157 */ 158 ErrCode GetBundleCachePath(const std::string &dir, std::vector<std::string> &cachePath); 159 160 ErrCode ScanDir( 161 const std::string &dir, ScanMode scanMode, ResultMode resultMode, std::vector<std::string> &paths); 162 163 ErrCode MoveFile(const std::string &oldPath, const std::string &newPath); 164 165 ErrCode CopyFile(const std::string &oldPath, const std::string &newPath, 166 const std::string &signatureFilePath = ""); 167 168 ErrCode Mkdir( 169 const std::string &dir, const int32_t mode, const int32_t uid, const int32_t gid); 170 171 ErrCode GetFileStat(const std::string &file, FileStat &fileStat); 172 173 ErrCode ExtractDiffFiles(const std::string &filePath, const std::string &targetPath, 174 const std::string &cpuAbi); 175 176 ErrCode ApplyDiffPatch(const std::string &oldSoPath, const std::string &diffFilePath, 177 const std::string &newSoPath, int32_t uid = Constants::INVALID_UID); 178 179 ErrCode IsExistDir(const std::string &dir, bool &isExist); 180 181 ErrCode IsExistFile(const std::string &path, bool &isExist); 182 183 ErrCode IsExistApFile(const std::string &path, bool &isExist); 184 185 ErrCode IsDirEmpty(const std::string &dir, bool &isDirEmpty); 186 187 ErrCode ObtainQuickFixFileDir(const std::string &dir, std::vector<std::string> &dirVec); 188 189 ErrCode CopyFiles(const std::string &sourceDir, const std::string &destinationDir); 190 191 ErrCode ExtractFiles(const ExtractParam &extractParam); 192 193 ErrCode ExtractHnpFiles(const std::string &hnpPackageInfo, const ExtractParam &extractParam); 194 195 ErrCode ProcessBundleInstallNative(const std::string &userId, const std::string &hnpRootPath, 196 const std::string &hapPath, const std::string &cpuAbi, const std::string &packageName); 197 198 ErrCode ProcessBundleUnInstallNative(const std::string &userId, const std::string &bundleName); 199 200 ErrCode GetNativeLibraryFileNames(const std::string &filePath, const std::string &cpuAbi, 201 std::vector<std::string> &fileNames); 202 203 ErrCode ExecuteAOT(const AOTArgs &aotArgs, std::vector<uint8_t> &pendSignData); 204 205 ErrCode PendSignAOT(const std::string &anFileName, const std::vector<uint8_t> &signData); 206 207 ErrCode StopAOT(); 208 209 ErrCode DeleteUninstallTmpDirs(const std::vector<std::string> &dirs); 210 211 ErrCode VerifyCodeSignature(const CodeSignatureParam &codeSignatureParam); 212 213 ErrCode VerifyCodeSignatureForHap(const CodeSignatureParam &codeSignatureParam); 214 215 ErrCode CheckEncryption(const CheckEncryptionParam &checkEncryptionParam, bool &isEncryption); 216 217 ErrCode MoveFiles(const std::string &srcDir, const std::string &desDir); 218 219 ErrCode ExtractDriverSoFiles(const std::string &srcPath, 220 const std::unordered_multimap<std::string, std::string> &dirMap); 221 222 void OnLoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject); 223 224 void OnLoadSystemAbilityFail(); 225 226 bool StartInstalldService(); 227 228 ErrCode ExtractEncryptedSoFiles(const std::string &hapPath, const std::string &realSoFilesPath, 229 const std::string &cpuAbi, const std::string &tmpSoPath, int32_t uid); 230 231 ErrCode DeliverySignProfile(const std::string &bundleName, int32_t profileBlockLength, 232 const unsigned char *profileBlock); 233 234 ErrCode RemoveSignProfile(const std::string &bundleName); 235 236 ErrCode SetEncryptionPolicy(int32_t uid, const std::string &bundleName, 237 const int32_t userId, std::string &keyId); 238 239 ErrCode DeleteEncryptionKeyId(const std::string &bundleName, const int32_t userId); 240 241 ErrCode RemoveExtensionDir(int32_t userId, const std::vector<std::string> &extensionBundleDirs); 242 243 ErrCode IsExistExtensionDir(int32_t userId, const std::string &extensionBundleDir, bool &isExist); 244 245 ErrCode CreateExtensionDataDir(const CreateDirParam &createDirParam); 246 247 ErrCode GetExtensionSandboxTypeList(std::vector<std::string> &typeList); 248 249 ErrCode MoveHapToCodeDir(const std::string &originPath, const std::string &targetPath); 250 251 private: 252 sptr<IInstalld> GetInstalldProxy(); 253 bool LoadInstalldService(); 254 255 template<typename F, typename... Args> CallService(F func,Args &&...args)256 ErrCode CallService(F func, Args&&... args) 257 { 258 int32_t maxRetryTimes = 2; 259 ErrCode errCode = ERR_APPEXECFWK_INSTALLD_SERVICE_DIED; 260 for (int32_t retryTimes = 0; retryTimes < maxRetryTimes; retryTimes++) { 261 auto proxy = GetInstalldProxy(); 262 if (proxy == nullptr) { 263 errCode = ERR_APPEXECFWK_INSTALLD_GET_PROXY_ERROR; 264 continue; 265 } 266 errCode = (proxy->*func)(std::forward<Args>(args)...); 267 if (errCode == ERR_APPEXECFWK_INSTALLD_SERVICE_DIED) { 268 APP_LOGE("CallService failed, retry times: %{public}d", retryTimes + 1); 269 ResetInstalldProxy(); 270 } else { 271 return errCode; 272 } 273 } 274 return errCode; 275 } 276 277 private: 278 bool loadSaFinished_; 279 std::mutex mutex_; 280 std::mutex loadSaMutex_; 281 std::mutex getProxyMutex_; 282 std::condition_variable loadSaCondition_; 283 sptr<IInstalld> installdProxy_; 284 sptr<IRemoteObject::DeathRecipient> recipient_; 285 }; 286 } // namespace AppExecFwk 287 } // namespace OHOS 288 #endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_CLIENT_H