• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 
16 #include "installd_client.h"
17 
18 #include "app_log_wrapper.h"
19 #include "bundle_constants.h"
20 #include "if_system_ability_manager.h"
21 #include "installd/installd_load_callback.h"
22 #include "installd_death_recipient.h"
23 #include "iservice_registry.h"
24 #include "system_ability_definition.h"
25 #include "system_ability_helper.h"
26 
27 namespace OHOS {
28 namespace AppExecFwk {
29 namespace {
30 const int LOAD_SA_TIMEOUT_MS = 4 * 1000;
31 } // namespace
32 
CreateBundleDir(const std::string & bundleDir)33 ErrCode InstalldClient::CreateBundleDir(const std::string &bundleDir)
34 {
35     if (bundleDir.empty()) {
36         APP_LOGE("bundle dir is empty");
37         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
38     }
39 
40     return CallService(&IInstalld::CreateBundleDir, bundleDir);
41 }
42 
ExtractModuleFiles(const std::string & srcModulePath,const std::string & targetPath,const std::string & targetSoPath,const std::string & cpuAbi)43 ErrCode InstalldClient::ExtractModuleFiles(const std::string &srcModulePath, const std::string &targetPath,
44     const std::string &targetSoPath, const std::string &cpuAbi)
45 {
46     if (srcModulePath.empty() || targetPath.empty()) {
47         APP_LOGE("src module path or target path is empty");
48         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
49     }
50 
51     return CallService(&IInstalld::ExtractModuleFiles, srcModulePath, targetPath, targetSoPath, cpuAbi);
52 }
53 
ExtractFiles(const ExtractParam & extractParam)54 ErrCode InstalldClient::ExtractFiles(const ExtractParam &extractParam)
55 {
56     if (extractParam.srcPath.empty() || extractParam.targetPath.empty()) {
57         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
58     }
59     return CallService(&IInstalld::ExtractFiles, extractParam);
60 }
61 
ExecuteAOT(const AOTArgs & aotArgs)62 ErrCode InstalldClient::ExecuteAOT(const AOTArgs &aotArgs)
63 {
64     return CallService(&IInstalld::ExecuteAOT, aotArgs);
65 }
66 
RenameModuleDir(const std::string & oldPath,const std::string & newPath)67 ErrCode InstalldClient::RenameModuleDir(const std::string &oldPath, const std::string &newPath)
68 {
69     if (oldPath.empty() || newPath.empty()) {
70         APP_LOGE("rename path is empty");
71         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
72     }
73 
74     return CallService(&IInstalld::RenameModuleDir, oldPath, newPath);
75 }
76 
CreateBundleDataDir(const CreateDirParam & createDirParam)77 ErrCode InstalldClient::CreateBundleDataDir(const CreateDirParam &createDirParam)
78 {
79     if (createDirParam.bundleName.empty() || createDirParam.userId < 0
80         || createDirParam.uid < 0 || createDirParam.gid < 0) {
81         APP_LOGE("params are invalid");
82         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
83     }
84 
85     return CallService(&IInstalld::CreateBundleDataDir, createDirParam);
86 }
87 
CreateBundleDataDirWithVector(const std::vector<CreateDirParam> & createDirParams)88 ErrCode InstalldClient::CreateBundleDataDirWithVector(const std::vector<CreateDirParam> &createDirParams)
89 {
90     return CallService(&IInstalld::CreateBundleDataDirWithVector, createDirParams);
91 }
92 
RemoveBundleDataDir(const std::string & bundleName,const int userid)93 ErrCode InstalldClient::RemoveBundleDataDir(
94     const std::string &bundleName, const int userid)
95 {
96     if (bundleName.empty() || userid < 0) {
97         APP_LOGE("params are invalid");
98         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
99     }
100 
101     return CallService(&IInstalld::RemoveBundleDataDir, bundleName, userid);
102 }
103 
RemoveModuleDataDir(const std::string & ModuleName,const int userid)104 ErrCode InstalldClient::RemoveModuleDataDir(const std::string &ModuleName, const int userid)
105 {
106     if (ModuleName.empty() || userid < 0) {
107         APP_LOGE("params are invalid");
108         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
109     }
110 
111     return CallService(&IInstalld::RemoveModuleDataDir, ModuleName, userid);
112 }
113 
RemoveDir(const std::string & dir)114 ErrCode InstalldClient::RemoveDir(const std::string &dir)
115 {
116     if (dir.empty()) {
117         APP_LOGE("dir removed is empty");
118         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
119     }
120 
121     return CallService(&IInstalld::RemoveDir, dir);
122 }
123 
CleanBundleDataDir(const std::string & bundleDir)124 ErrCode InstalldClient::CleanBundleDataDir(const std::string &bundleDir)
125 {
126     if (bundleDir.empty()) {
127         APP_LOGE("bundle dir is empty");
128         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
129     }
130 
131     return CallService(&IInstalld::CleanBundleDataDir, bundleDir);
132 }
133 
CleanBundleDataDirByName(const std::string & bundleName,const int userid)134 ErrCode InstalldClient::CleanBundleDataDirByName(const std::string &bundleName, const int userid)
135 {
136     if (bundleName.empty() || userid < 0) {
137         APP_LOGE("params are invalid");
138         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
139     }
140 
141     return CallService(&IInstalld::CleanBundleDataDirByName, bundleName, userid);
142 }
143 
GetBundleStats(const std::string & bundleName,const int32_t userId,std::vector<int64_t> & bundleStats,const int32_t uid)144 ErrCode InstalldClient::GetBundleStats(
145     const std::string &bundleName, const int32_t userId, std::vector<int64_t> &bundleStats, const int32_t uid)
146 {
147     if (bundleName.empty()) {
148         APP_LOGE("bundleName is empty");
149         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
150     }
151 
152     return CallService(&IInstalld::GetBundleStats, bundleName, userId, bundleStats, uid);
153 }
154 
GetAllBundleStats(const std::vector<std::string> & bundleNames,const int32_t userId,std::vector<int64_t> & bundleStats,const std::vector<int32_t> & uids)155 ErrCode InstalldClient::GetAllBundleStats(const std::vector<std::string> &bundleNames, const int32_t userId,
156     std::vector<int64_t> &bundleStats, const std::vector<int32_t> &uids)
157 {
158     if (bundleNames.empty()) {
159         APP_LOGE("bundleName is empty");
160         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
161     }
162 
163     if (bundleNames.size() != uids.size()) {
164         APP_LOGE("bundleNames size is not equal to uids size");
165         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
166     }
167     return CallService(&IInstalld::GetAllBundleStats, bundleNames, userId, bundleStats, uids);
168 }
169 
SetDirApl(const std::string & dir,const std::string & bundleName,const std::string & apl,bool isPreInstallApp,bool debug)170 ErrCode InstalldClient::SetDirApl(const std::string &dir, const std::string &bundleName, const std::string &apl,
171     bool isPreInstallApp, bool debug)
172 {
173     if (dir.empty() || bundleName.empty() || apl.empty()) {
174         APP_LOGE("params are invalid");
175         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
176     }
177 
178     return CallService(&IInstalld::SetDirApl, dir, bundleName, apl, isPreInstallApp, debug);
179 }
180 
GetBundleCachePath(const std::string & dir,std::vector<std::string> & cachePath)181 ErrCode InstalldClient::GetBundleCachePath(const std::string &dir, std::vector<std::string> &cachePath)
182 {
183     if (dir.empty()) {
184         APP_LOGE("params are invalid");
185         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
186     }
187 
188     return CallService(&IInstalld::GetBundleCachePath, dir, cachePath);
189 }
190 
ResetInstalldProxy()191 void InstalldClient::ResetInstalldProxy()
192 {
193     if ((installdProxy_ != nullptr) && (installdProxy_->AsObject() != nullptr)) {
194         installdProxy_->AsObject()->RemoveDeathRecipient(recipient_);
195     }
196     SystemAbilityHelper::UnloadSystemAbility(INSTALLD_SERVICE_ID);
197     installdProxy_ = nullptr;
198 }
199 
LoadInstalldService()200 bool InstalldClient::LoadInstalldService()
201 {
202     {
203         std::unique_lock<std::mutex> lock(loadSaMutex_);
204         loadSaFinished_ = false;
205     }
206     auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
207     if (systemAbilityMgr == nullptr) {
208         APP_LOGE("Failed to get SystemAbilityManager.");
209         return false;
210     }
211     sptr<InstalldLoadCallback> loadCallback = new (std::nothrow) InstalldLoadCallback();
212     if (loadCallback == nullptr) {
213         APP_LOGE("Create load callback failed.");
214         return false;
215     }
216     auto ret = systemAbilityMgr->LoadSystemAbility(INSTALLD_SERVICE_ID, loadCallback);
217     if (ret != 0) {
218         APP_LOGE("Load system ability %{public}d failed with %{public}d.", INSTALLD_SERVICE_ID, ret);
219         return false;
220     }
221 
222     {
223         std::unique_lock<std::mutex> lock(loadSaMutex_);
224         auto waitStatus = loadSaCondition_.wait_for(lock, std::chrono::milliseconds(LOAD_SA_TIMEOUT_MS),
225             [this]() {
226                 return loadSaFinished_;
227             });
228         if (!waitStatus) {
229             APP_LOGE("Wait for load sa timeout.");
230             return false;
231         }
232     }
233     return true;
234 }
235 
GetInstalldProxy()236 bool InstalldClient::GetInstalldProxy()
237 {
238     std::lock_guard<std::mutex> lock(getProxyMutex_);
239     if (installdProxy_ != nullptr) {
240         APP_LOGD("installd ready");
241         return true;
242     }
243 
244     APP_LOGD("try to get installd proxy");
245     if (!LoadInstalldService()) {
246         APP_LOGE("load installd service failed");
247         return false;
248     }
249     if ((installdProxy_ == nullptr) || (installdProxy_->AsObject() == nullptr)) {
250         APP_LOGE("the installd proxy or remote object is null");
251         return false;
252     }
253 
254     recipient_ = new (std::nothrow) InstalldDeathRecipient();
255     if (recipient_ == nullptr) {
256         APP_LOGE("the death recipient is nullptr");
257         return false;
258     }
259     installdProxy_->AsObject()->AddDeathRecipient(recipient_);
260     return true;
261 }
262 
ScanDir(const std::string & dir,ScanMode scanMode,ResultMode resultMode,std::vector<std::string> & paths)263 ErrCode InstalldClient::ScanDir(
264     const std::string &dir, ScanMode scanMode, ResultMode resultMode, std::vector<std::string> &paths)
265 {
266     if (dir.empty()) {
267         APP_LOGE("params are invalid");
268         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
269     }
270 
271     return CallService(&IInstalld::ScanDir, dir, scanMode, resultMode, paths);
272 }
273 
MoveFile(const std::string & oldPath,const std::string & newPath)274 ErrCode InstalldClient::MoveFile(const std::string &oldPath, const std::string &newPath)
275 {
276     if (oldPath.empty() || newPath.empty()) {
277         APP_LOGE("params are invalid");
278         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
279     }
280 
281     return CallService(&IInstalld::MoveFile, oldPath, newPath);
282 }
283 
CopyFile(const std::string & oldPath,const std::string & newPath,const std::string & signatureFilePath)284 ErrCode InstalldClient::CopyFile(const std::string &oldPath, const std::string &newPath,
285     const std::string &signatureFilePath)
286 {
287     if (oldPath.empty() || newPath.empty()) {
288         APP_LOGE("params are invalid");
289         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
290     }
291 
292     return CallService(&IInstalld::CopyFile, oldPath, newPath, signatureFilePath);
293 }
294 
Mkdir(const std::string & dir,const int32_t mode,const int32_t uid,const int32_t gid)295 ErrCode InstalldClient::Mkdir(
296     const std::string &dir, const int32_t mode, const int32_t uid, const int32_t gid)
297 {
298     if (dir.empty()) {
299         APP_LOGE("params are invalid");
300         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
301     }
302 
303     return CallService(&IInstalld::Mkdir, dir, mode, uid, gid);
304 }
305 
GetFileStat(const std::string & file,FileStat & fileStat)306 ErrCode InstalldClient::GetFileStat(const std::string &file, FileStat &fileStat)
307 {
308     if (file.empty()) {
309         APP_LOGE("params are invalid");
310         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
311     }
312 
313     return CallService(&IInstalld::GetFileStat, file, fileStat);
314 }
315 
ExtractDiffFiles(const std::string & filePath,const std::string & targetPath,const std::string & cpuAbi)316 ErrCode InstalldClient::ExtractDiffFiles(const std::string &filePath, const std::string &targetPath,
317     const std::string &cpuAbi)
318 {
319     if (filePath.empty() || targetPath.empty() || cpuAbi.empty()) {
320         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
321     }
322     return CallService(&IInstalld::ExtractDiffFiles, filePath, targetPath, cpuAbi);
323 }
324 
ApplyDiffPatch(const std::string & oldSoPath,const std::string & diffFilePath,const std::string & newSoPath,int32_t uid)325 ErrCode InstalldClient::ApplyDiffPatch(const std::string &oldSoPath, const std::string &diffFilePath,
326     const std::string &newSoPath, int32_t uid)
327 {
328     if (oldSoPath.empty() || diffFilePath.empty() || newSoPath.empty()) {
329         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
330     }
331     return CallService(&IInstalld::ApplyDiffPatch, oldSoPath, diffFilePath, newSoPath, uid);
332 }
333 
IsExistDir(const std::string & dir,bool & isExist)334 ErrCode InstalldClient::IsExistDir(const std::string &dir, bool &isExist)
335 {
336     return CallService(&IInstalld::IsExistDir, dir, isExist);
337 }
338 
IsExistFile(const std::string & path,bool & isExist)339 ErrCode InstalldClient::IsExistFile(const std::string &path, bool &isExist)
340 {
341     return CallService(&IInstalld::IsExistFile, path, isExist);
342 }
343 
IsExistApFile(const std::string & path,bool & isExist)344 ErrCode InstalldClient::IsExistApFile(const std::string &path, bool &isExist)
345 {
346     return CallService(&IInstalld::IsExistApFile, path, isExist);
347 }
348 
IsDirEmpty(const std::string & dir,bool & isDirEmpty)349 ErrCode InstalldClient::IsDirEmpty(const std::string &dir, bool &isDirEmpty)
350 {
351     return CallService(&IInstalld::IsDirEmpty, dir, isDirEmpty);
352 }
353 
ObtainQuickFixFileDir(const std::string & dir,std::vector<std::string> & dirVec)354 ErrCode InstalldClient::ObtainQuickFixFileDir(const std::string &dir, std::vector<std::string> &dirVec)
355 {
356     return CallService(&IInstalld::ObtainQuickFixFileDir, dir, dirVec);
357 }
358 
CopyFiles(const std::string & sourceDir,const std::string & destinationDir)359 ErrCode InstalldClient::CopyFiles(const std::string &sourceDir, const std::string &destinationDir)
360 {
361     return CallService(&IInstalld::CopyFiles, sourceDir, destinationDir);
362 }
363 
GetNativeLibraryFileNames(const std::string & filePath,const std::string & cpuAbi,std::vector<std::string> & fileNames)364 ErrCode InstalldClient::GetNativeLibraryFileNames(const std::string &filePath, const std::string &cpuAbi,
365     std::vector<std::string> &fileNames)
366 {
367     return CallService(&IInstalld::GetNativeLibraryFileNames, filePath, cpuAbi, fileNames);
368 }
369 
VerifyCodeSignature(const CodeSignatureParam & codeSignatureParam)370 ErrCode InstalldClient::VerifyCodeSignature(const CodeSignatureParam &codeSignatureParam)
371 {
372     if (codeSignatureParam.modulePath.empty()) {
373         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
374     }
375     return CallService(&IInstalld::VerifyCodeSignature, codeSignatureParam);
376 }
377 
CheckEncryption(const CheckEncryptionParam & checkEncryptionParam,bool & isEncryption)378 ErrCode InstalldClient::CheckEncryption(const CheckEncryptionParam &checkEncryptionParam, bool &isEncryption)
379 {
380     if (checkEncryptionParam.modulePath.empty()) {
381         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
382     }
383     return CallService(&IInstalld::CheckEncryption, checkEncryptionParam, isEncryption);
384 }
385 
MoveFiles(const std::string & srcDir,const std::string & desDir)386 ErrCode InstalldClient::MoveFiles(const std::string &srcDir, const std::string &desDir)
387 {
388     if (srcDir.empty() || desDir.empty()) {
389         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
390     }
391     return CallService(&IInstalld::MoveFiles, srcDir, desDir);
392 }
393 
394 
ExtractDriverSoFiles(const std::string & srcPath,const std::unordered_multimap<std::string,std::string> & dirMap)395 ErrCode InstalldClient::ExtractDriverSoFiles(const std::string &srcPath,
396     const std::unordered_multimap<std::string, std::string> &dirMap)
397 {
398     if (srcPath.empty() || dirMap.empty()) {
399         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
400     }
401     return CallService(&IInstalld::ExtractDriverSoFiles, srcPath, dirMap);
402 }
403 
VerifyCodeSignatureForHap(const CodeSignatureParam & codeSignatureParam)404 ErrCode InstalldClient::VerifyCodeSignatureForHap(const CodeSignatureParam &codeSignatureParam)
405 {
406     if (codeSignatureParam.modulePath.empty()) {
407         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
408     }
409     return CallService(&IInstalld::VerifyCodeSignatureForHap, codeSignatureParam);
410 }
411 
DeliverySignProfile(const std::string & bundleName,int32_t profileBlockLength,const unsigned char * profileBlock)412 ErrCode InstalldClient::DeliverySignProfile(const std::string &bundleName, int32_t profileBlockLength,
413     const unsigned char *profileBlock)
414 {
415     if (bundleName.empty() || profileBlock == nullptr) {
416         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
417     }
418     return CallService(&IInstalld::DeliverySignProfile, bundleName, profileBlockLength, profileBlock);
419 }
420 
RemoveSignProfile(const std::string & bundleName)421 ErrCode InstalldClient::RemoveSignProfile(const std::string &bundleName)
422 {
423     if (bundleName.empty()) {
424         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
425     }
426     return CallService(&IInstalld::RemoveSignProfile, bundleName);
427 }
428 
OnLoadSystemAbilitySuccess(const sptr<IRemoteObject> & remoteObject)429 void InstalldClient::OnLoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject)
430 {
431     {
432         std::lock_guard<std::mutex> lock(mutex_);
433         installdProxy_ = iface_cast<IInstalld>(remoteObject);
434     }
435 
436     {
437         std::lock_guard<std::mutex> lock(loadSaMutex_);
438         loadSaFinished_ = true;
439         loadSaCondition_.notify_one();
440     }
441 }
442 
OnLoadSystemAbilityFail()443 void InstalldClient::OnLoadSystemAbilityFail()
444 {
445     {
446         std::lock_guard<std::mutex> lock(mutex_);
447         installdProxy_ = nullptr;
448     }
449 
450     {
451         std::lock_guard<std::mutex> lock(loadSaMutex_);
452         loadSaFinished_ = true;
453         loadSaCondition_.notify_one();
454     }
455 }
456 
StartInstalldService()457 bool InstalldClient::StartInstalldService()
458 {
459     return GetInstalldProxy();
460 }
461 
ExtractEncryptedSoFiles(const std::string & hapPath,const std::string & realSoFilesPath,const std::string & cpuAbi,const std::string & tmpSoPath,int32_t uid)462 ErrCode InstalldClient::ExtractEncryptedSoFiles(const std::string &hapPath, const std::string &realSoFilesPath,
463     const std::string &cpuAbi, const std::string &tmpSoPath, int32_t uid)
464 {
465     if (hapPath.empty() || tmpSoPath.empty() || cpuAbi.empty()) {
466         APP_LOGE("params are invalid");
467         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
468     }
469     return CallService(&IInstalld::ExtractEncryptedSoFiles, hapPath, realSoFilesPath, cpuAbi, tmpSoPath, uid);
470 }
471 }  // namespace AppExecFwk
472 }  // namespace OHOS
473