• 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 
RemoveBundleDataDir(const std::string & bundleName,const int userid)88 ErrCode InstalldClient::RemoveBundleDataDir(
89     const std::string &bundleName, const int userid)
90 {
91     if (bundleName.empty() || userid < 0) {
92         APP_LOGE("params are invalid");
93         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
94     }
95 
96     return CallService(&IInstalld::RemoveBundleDataDir, bundleName, userid);
97 }
98 
RemoveModuleDataDir(const std::string & ModuleName,const int userid)99 ErrCode InstalldClient::RemoveModuleDataDir(const std::string &ModuleName, const int userid)
100 {
101     if (ModuleName.empty() || userid < 0) {
102         APP_LOGE("params are invalid");
103         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
104     }
105 
106     return CallService(&IInstalld::RemoveModuleDataDir, ModuleName, userid);
107 }
108 
RemoveDir(const std::string & dir)109 ErrCode InstalldClient::RemoveDir(const std::string &dir)
110 {
111     if (dir.empty()) {
112         APP_LOGE("dir removed is empty");
113         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
114     }
115 
116     return CallService(&IInstalld::RemoveDir, dir);
117 }
118 
CleanBundleDataDir(const std::string & bundleDir)119 ErrCode InstalldClient::CleanBundleDataDir(const std::string &bundleDir)
120 {
121     if (bundleDir.empty()) {
122         APP_LOGE("bundle dir is empty");
123         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
124     }
125 
126     return CallService(&IInstalld::CleanBundleDataDir, bundleDir);
127 }
128 
GetBundleStats(const std::string & bundleName,const int32_t userId,std::vector<int64_t> & bundleStats)129 ErrCode InstalldClient::GetBundleStats(
130     const std::string &bundleName, const int32_t userId, std::vector<int64_t> &bundleStats)
131 {
132     if (bundleName.empty()) {
133         APP_LOGE("bundleName is empty");
134         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
135     }
136 
137     return CallService(&IInstalld::GetBundleStats, bundleName, userId, bundleStats);
138 }
139 
SetDirApl(const std::string & dir,const std::string & bundleName,const std::string & apl,bool isPreInstallApp,bool debug)140 ErrCode InstalldClient::SetDirApl(const std::string &dir, const std::string &bundleName, const std::string &apl,
141     bool isPreInstallApp, bool debug)
142 {
143     if (dir.empty() || bundleName.empty() || apl.empty()) {
144         APP_LOGE("params are invalid");
145         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
146     }
147 
148     return CallService(&IInstalld::SetDirApl, dir, bundleName, apl, isPreInstallApp, debug);
149 }
150 
GetBundleCachePath(const std::string & dir,std::vector<std::string> & cachePath)151 ErrCode InstalldClient::GetBundleCachePath(const std::string &dir, std::vector<std::string> &cachePath)
152 {
153     if (dir.empty()) {
154         APP_LOGE("params are invalid");
155         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
156     }
157 
158     return CallService(&IInstalld::GetBundleCachePath, dir, cachePath);
159 }
160 
ResetInstalldProxy()161 void InstalldClient::ResetInstalldProxy()
162 {
163     if ((installdProxy_ != nullptr) && (installdProxy_->AsObject() != nullptr)) {
164         installdProxy_->AsObject()->RemoveDeathRecipient(recipient_);
165     }
166     SystemAbilityHelper::UnloadSystemAbility(INSTALLD_SERVICE_ID);
167     installdProxy_ = nullptr;
168 }
169 
LoadInstalldService()170 bool InstalldClient::LoadInstalldService()
171 {
172     {
173         std::unique_lock<std::mutex> lock(loadSaMutex_);
174         loadSaFinished_ = false;
175     }
176     auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
177     if (systemAbilityMgr == nullptr) {
178         APP_LOGE("Failed to get SystemAbilityManager.");
179         return false;
180     }
181     sptr<InstalldLoadCallback> loadCallback = new (std::nothrow) InstalldLoadCallback();
182     if (loadCallback == nullptr) {
183         APP_LOGE("Create load callback failed.");
184         return false;
185     }
186     auto ret = systemAbilityMgr->LoadSystemAbility(INSTALLD_SERVICE_ID, loadCallback);
187     if (ret != 0) {
188         APP_LOGE("Load system ability %{public}d failed with %{public}d.", INSTALLD_SERVICE_ID, ret);
189         return false;
190     }
191 
192     {
193         std::unique_lock<std::mutex> lock(loadSaMutex_);
194         auto waitStatus = loadSaCondition_.wait_for(lock, std::chrono::milliseconds(LOAD_SA_TIMEOUT_MS),
195             [this]() {
196                 return loadSaFinished_;
197             });
198         if (!waitStatus) {
199             APP_LOGE("Wait for load sa timeout.");
200             return false;
201         }
202     }
203     return true;
204 }
205 
GetInstalldProxy()206 bool InstalldClient::GetInstalldProxy()
207 {
208     std::lock_guard<std::mutex> lock(getProxyMutex_);
209     if (installdProxy_ != nullptr) {
210         APP_LOGD("installd ready");
211         return true;
212     }
213 
214     APP_LOGD("try to get installd proxy");
215     if (!LoadInstalldService()) {
216         APP_LOGE("load installd service failed");
217         return false;
218     }
219     if ((installdProxy_ == nullptr) || (installdProxy_->AsObject() == nullptr)) {
220         APP_LOGE("the installd proxy or remote object is null");
221         return false;
222     }
223 
224     recipient_ = new (std::nothrow) InstalldDeathRecipient();
225     if (recipient_ == nullptr) {
226         APP_LOGE("the death recipient is nullptr");
227         return false;
228     }
229     installdProxy_->AsObject()->AddDeathRecipient(recipient_);
230     return true;
231 }
232 
ScanDir(const std::string & dir,ScanMode scanMode,ResultMode resultMode,std::vector<std::string> & paths)233 ErrCode InstalldClient::ScanDir(
234     const std::string &dir, ScanMode scanMode, ResultMode resultMode, std::vector<std::string> &paths)
235 {
236     if (dir.empty()) {
237         APP_LOGE("params are invalid");
238         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
239     }
240 
241     return CallService(&IInstalld::ScanDir, dir, scanMode, resultMode, paths);
242 }
243 
MoveFile(const std::string & oldPath,const std::string & newPath)244 ErrCode InstalldClient::MoveFile(const std::string &oldPath, const std::string &newPath)
245 {
246     if (oldPath.empty() || newPath.empty()) {
247         APP_LOGE("params are invalid");
248         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
249     }
250 
251     return CallService(&IInstalld::MoveFile, oldPath, newPath);
252 }
253 
CopyFile(const std::string & oldPath,const std::string & newPath,const std::string & signatureFilePath)254 ErrCode InstalldClient::CopyFile(const std::string &oldPath, const std::string &newPath,
255     const std::string &signatureFilePath)
256 {
257     if (oldPath.empty() || newPath.empty()) {
258         APP_LOGE("params are invalid");
259         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
260     }
261 
262     return CallService(&IInstalld::CopyFile, oldPath, newPath, signatureFilePath);
263 }
264 
Mkdir(const std::string & dir,const int32_t mode,const int32_t uid,const int32_t gid)265 ErrCode InstalldClient::Mkdir(
266     const std::string &dir, const int32_t mode, const int32_t uid, const int32_t gid)
267 {
268     if (dir.empty()) {
269         APP_LOGE("params are invalid");
270         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
271     }
272 
273     return CallService(&IInstalld::Mkdir, dir, mode, uid, gid);
274 }
275 
GetFileStat(const std::string & file,FileStat & fileStat)276 ErrCode InstalldClient::GetFileStat(const std::string &file, FileStat &fileStat)
277 {
278     if (file.empty()) {
279         APP_LOGE("params are invalid");
280         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
281     }
282 
283     return CallService(&IInstalld::GetFileStat, file, fileStat);
284 }
285 
ExtractDiffFiles(const std::string & filePath,const std::string & targetPath,const std::string & cpuAbi)286 ErrCode InstalldClient::ExtractDiffFiles(const std::string &filePath, const std::string &targetPath,
287     const std::string &cpuAbi)
288 {
289     if (filePath.empty() || targetPath.empty() || cpuAbi.empty()) {
290         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
291     }
292     return CallService(&IInstalld::ExtractDiffFiles, filePath, targetPath, cpuAbi);
293 }
294 
ApplyDiffPatch(const std::string & oldSoPath,const std::string & diffFilePath,const std::string & newSoPath)295 ErrCode InstalldClient::ApplyDiffPatch(const std::string &oldSoPath, const std::string &diffFilePath,
296     const std::string &newSoPath)
297 {
298     if (oldSoPath.empty() || diffFilePath.empty() || newSoPath.empty()) {
299         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
300     }
301     return CallService(&IInstalld::ApplyDiffPatch, oldSoPath, diffFilePath, newSoPath);
302 }
303 
IsExistDir(const std::string & dir,bool & isExist)304 ErrCode InstalldClient::IsExistDir(const std::string &dir, bool &isExist)
305 {
306     return CallService(&IInstalld::IsExistDir, dir, isExist);
307 }
308 
IsExistFile(const std::string & path,bool & isExist)309 ErrCode InstalldClient::IsExistFile(const std::string &path, bool &isExist)
310 {
311     return CallService(&IInstalld::IsExistFile, path, isExist);
312 }
313 
IsDirEmpty(const std::string & dir,bool & isDirEmpty)314 ErrCode InstalldClient::IsDirEmpty(const std::string &dir, bool &isDirEmpty)
315 {
316     return CallService(&IInstalld::IsDirEmpty, dir, isDirEmpty);
317 }
318 
ObtainQuickFixFileDir(const std::string & dir,std::vector<std::string> & dirVec)319 ErrCode InstalldClient::ObtainQuickFixFileDir(const std::string &dir, std::vector<std::string> &dirVec)
320 {
321     return CallService(&IInstalld::ObtainQuickFixFileDir, dir, dirVec);
322 }
323 
CopyFiles(const std::string & sourceDir,const std::string & destinationDir)324 ErrCode InstalldClient::CopyFiles(const std::string &sourceDir, const std::string &destinationDir)
325 {
326     return CallService(&IInstalld::CopyFiles, sourceDir, destinationDir);
327 }
328 
GetNativeLibraryFileNames(const std::string & filePath,const std::string & cpuAbi,std::vector<std::string> & fileNames)329 ErrCode InstalldClient::GetNativeLibraryFileNames(const std::string &filePath, const std::string &cpuAbi,
330     std::vector<std::string> &fileNames)
331 {
332     return CallService(&IInstalld::GetNativeLibraryFileNames, filePath, cpuAbi, fileNames);
333 }
334 
VerifyCodeSignature(const std::string & modulePath,const std::string & cpuAbi,const std::string & targetSoPath,const std::string & signatureFileDir)335 ErrCode InstalldClient::VerifyCodeSignature(const std::string &modulePath, const std::string &cpuAbi,
336     const std::string &targetSoPath, const std::string &signatureFileDir)
337 {
338     if (modulePath.empty()) {
339         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
340     }
341     return CallService(&IInstalld::VerifyCodeSignature, modulePath, cpuAbi, targetSoPath,
342         signatureFileDir);
343 }
344 
MoveFiles(const std::string & srcDir,const std::string & desDir)345 ErrCode InstalldClient::MoveFiles(const std::string &srcDir, const std::string &desDir)
346 {
347     if (srcDir.empty() || desDir.empty()) {
348         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
349     }
350     return CallService(&IInstalld::MoveFiles, srcDir, desDir);
351 }
352 
OnLoadSystemAbilitySuccess(const sptr<IRemoteObject> & remoteObject)353 void InstalldClient::OnLoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject)
354 {
355     {
356         std::lock_guard<std::mutex> lock(mutex_);
357         installdProxy_ = iface_cast<IInstalld>(remoteObject);
358     }
359 
360     {
361         std::lock_guard<std::mutex> lock(loadSaMutex_);
362         loadSaFinished_ = true;
363         loadSaCondition_.notify_one();
364     }
365 }
366 
OnLoadSystemAbilityFail()367 void InstalldClient::OnLoadSystemAbilityFail()
368 {
369     {
370         std::lock_guard<std::mutex> lock(mutex_);
371         installdProxy_ = nullptr;
372     }
373 
374     {
375         std::lock_guard<std::mutex> lock(loadSaMutex_);
376         loadSaFinished_ = true;
377         loadSaCondition_.notify_one();
378     }
379 }
380 
StartInstalldService()381 bool InstalldClient::StartInstalldService()
382 {
383     return GetInstalldProxy();
384 }
385 }  // namespace AppExecFwk
386 }  // namespace OHOS
387