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, bool isAtomicService = false); 76 /** 77 * @brief Remove a module data directory. 78 * @param ModuleDir Indicates the module data directory path that to be created. 79 * @param userid Indicates userid to be set to the directory. 80 * @return Returns ERR_OK if the data directories created successfully; returns error code otherwise. 81 */ 82 ErrCode RemoveModuleDataDir(const std::string &ModuleDir, const int userid); 83 /** 84 * @brief Remove a directory. 85 * @param dir Indicates the directory path that to be removed. 86 * @return Returns ERR_OK if the directory removed successfully; returns error code otherwise. 87 */ 88 ErrCode RemoveDir(const std::string &dir); 89 /** 90 * @brief Get disk usage for dir. 91 * @param dir Indicates the directory. 92 * @param isRealPath Indicates isRealPath. 93 * @return Returns true if successfully; returns false otherwise. 94 */ 95 int64_t GetDiskUsage(const std::string &dir, bool isRealPath = false); 96 /** 97 * @brief Clean all files in a bundle data directory. 98 * @param bundleDir Indicates the data directory path that to be cleaned. 99 * @return Returns ERR_OK if the data directory cleaned successfully; returns error code otherwise. 100 */ 101 ErrCode CleanBundleDataDir(const std::string &bundleDir); 102 /** 103 * @brief Clean a bundle data directory. 104 * @param bundleName Indicates the bundleName data directory path that to be cleaned. 105 * @param userid Indicates userid to be set to the directory. 106 * @param appIndex Indicates app index to be set to the directory. 107 * @return Returns ERR_OK if the bundle data directory cleaned successfully; returns error code otherwise. 108 */ 109 ErrCode CleanBundleDataDirByName(const std::string &bundleName, const int userid, const int appIndex = 0); 110 /** 111 * @brief Get bundle Stats. 112 * @param bundleName Indicates the bundle name. 113 * @param userId Indicates the user Id. 114 * @param bundleStats Indicates the bundle Stats. 115 * @return Returns ERR_OK if get stats successfully; returns error code otherwise. 116 */ 117 ErrCode GetBundleStats(const std::string &bundleName, const int32_t userId, 118 std::vector<int64_t> &bundleStats, const int32_t uid = Constants::INVALID_UID, const int32_t appIndex = 0); 119 120 ErrCode GetAllBundleStats(const int32_t userId, 121 std::vector<int64_t> &bundleStats, const std::vector<int32_t> &uids); 122 123 /** 124 * @brief Reset the installd proxy object when installd service died. 125 * @return 126 */ 127 void ResetInstalldProxy(); 128 129 /** 130 * @brief Set dir apl. 131 * @param dir Indicates the data dir. 132 * @param bundleName Indicates the bundle name. 133 * @param apl Indicates the apl type. 134 * @param isPreInstallApp Indicates the bundle install type. 135 * @param debug Indicates the bundle debug mode. 136 * @return Returns ERR_OK if set apl successfully; returns error code otherwise. 137 */ 138 ErrCode SetDirApl(const std::string &dir, const std::string &bundleName, const std::string &apl, 139 bool isPreInstallApp, bool debug); 140 141 /** 142 * @brief Get all cache file path. 143 * @param dir Indicates the data dir. 144 * @param cachesPath Indicates the cache file path. 145 * @return Returns ERR_OK if get cache file path successfully; returns error code otherwise. 146 */ 147 ErrCode GetBundleCachePath(const std::string &dir, std::vector<std::string> &cachePath); 148 149 ErrCode ScanDir( 150 const std::string &dir, ScanMode scanMode, ResultMode resultMode, std::vector<std::string> &paths); 151 152 ErrCode MoveFile(const std::string &oldPath, const std::string &newPath); 153 154 ErrCode CopyFile(const std::string &oldPath, const std::string &newPath, 155 const std::string &signatureFilePath = ""); 156 157 ErrCode Mkdir( 158 const std::string &dir, const int32_t mode, const int32_t uid, const int32_t gid); 159 160 ErrCode GetFileStat(const std::string &file, FileStat &fileStat); 161 162 ErrCode ExtractDiffFiles(const std::string &filePath, const std::string &targetPath, 163 const std::string &cpuAbi); 164 165 ErrCode ApplyDiffPatch(const std::string &oldSoPath, const std::string &diffFilePath, 166 const std::string &newSoPath, int32_t uid = Constants::INVALID_UID); 167 168 ErrCode IsExistDir(const std::string &dir, bool &isExist); 169 170 ErrCode IsExistFile(const std::string &path, bool &isExist); 171 172 ErrCode IsExistApFile(const std::string &path, bool &isExist); 173 174 ErrCode IsDirEmpty(const std::string &dir, bool &isDirEmpty); 175 176 ErrCode ObtainQuickFixFileDir(const std::string &dir, std::vector<std::string> &dirVec); 177 178 ErrCode CopyFiles(const std::string &sourceDir, const std::string &destinationDir); 179 180 ErrCode ExtractFiles(const ExtractParam &extractParam); 181 182 ErrCode ExtractHnpFiles(const std::string &hnpPackageInfo, const ExtractParam &extractParam); 183 184 ErrCode ProcessBundleInstallNative(const std::string &userId, const std::string &hnpRootPath, 185 const std::string &hapPath, const std::string &cpuAbi, const std::string &packageName); 186 187 ErrCode ProcessBundleUnInstallNative(const std::string &userId, const std::string &bundleName); 188 189 ErrCode GetNativeLibraryFileNames(const std::string &filePath, const std::string &cpuAbi, 190 std::vector<std::string> &fileNames); 191 192 ErrCode ExecuteAOT(const AOTArgs &aotArgs, std::vector<uint8_t> &pendSignData); 193 194 ErrCode PendSignAOT(const std::string &anFileName, const std::vector<uint8_t> &signData); 195 196 ErrCode StopAOT(); 197 198 ErrCode VerifyCodeSignature(const CodeSignatureParam &codeSignatureParam); 199 200 ErrCode VerifyCodeSignatureForHap(const CodeSignatureParam &codeSignatureParam); 201 202 ErrCode CheckEncryption(const CheckEncryptionParam &checkEncryptionParam, bool &isEncryption); 203 204 ErrCode MoveFiles(const std::string &srcDir, const std::string &desDir); 205 206 ErrCode ExtractDriverSoFiles(const std::string &srcPath, 207 const std::unordered_multimap<std::string, std::string> &dirMap); 208 209 void OnLoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject); 210 211 void OnLoadSystemAbilityFail(); 212 213 bool StartInstalldService(); 214 215 ErrCode ExtractEncryptedSoFiles(const std::string &hapPath, const std::string &realSoFilesPath, 216 const std::string &cpuAbi, const std::string &tmpSoPath, int32_t uid); 217 218 ErrCode DeliverySignProfile(const std::string &bundleName, int32_t profileBlockLength, 219 const unsigned char *profileBlock); 220 221 ErrCode RemoveSignProfile(const std::string &bundleName); 222 223 ErrCode SetEncryptionPolicy(int32_t uid, const std::string &bundleName, 224 const int32_t userId, std::string &keyId); 225 226 ErrCode DeleteEncryptionKeyId(const std::string &bundleName, const int32_t userId); 227 228 ErrCode RemoveExtensionDir(int32_t userId, const std::vector<std::string> &extensionBundleDirs); 229 230 ErrCode IsExistExtensionDir(int32_t userId, const std::string &extensionBundleDir, bool &isExist); 231 232 ErrCode CreateExtensionDataDir(const CreateDirParam &createDirParam); 233 234 ErrCode GetExtensionSandboxTypeList(std::vector<std::string> &typeList); 235 236 ErrCode AddUserDirDeleteDfx(int32_t userId); 237 238 ErrCode MoveHapToCodeDir(const std::string &originPath, const std::string &targetPath); 239 240 private: 241 sptr<IInstalld> GetInstalldProxy(); 242 bool LoadInstalldService(); 243 244 template<typename F, typename... Args> CallService(F func, Args&&... args)245 ErrCode CallService(F func, Args&&... args) 246 { 247 int32_t maxRetryTimes = 2; 248 ErrCode errCode = ERR_APPEXECFWK_INSTALLD_SERVICE_DIED; 249 for (int32_t retryTimes = 0; retryTimes < maxRetryTimes; retryTimes++) { 250 auto proxy = GetInstalldProxy(); 251 if (proxy == nullptr) { 252 return ERR_APPEXECFWK_INSTALLD_GET_PROXY_ERROR; 253 } 254 errCode = (proxy->*func)(std::forward<Args>(args)...); 255 if (errCode == ERR_APPEXECFWK_INSTALLD_SERVICE_DIED) { 256 APP_LOGE("CallService failed, retry times: %{public}d", retryTimes + 1); 257 ResetInstalldProxy(); 258 } else { 259 return errCode; 260 } 261 } 262 return errCode; 263 } 264 265 private: 266 bool loadSaFinished_; 267 std::mutex mutex_; 268 std::mutex loadSaMutex_; 269 std::mutex getProxyMutex_; 270 std::condition_variable loadSaCondition_; 271 sptr<IInstalld> installdProxy_; 272 sptr<IRemoteObject::DeathRecipient> recipient_; 273 }; 274 } // namespace AppExecFwk 275 } // namespace OHOS 276 #endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_CLIENT_H