installs start on demand

Signed-off-by: xsz233 <xushizhe@huawei.com>
This commit is contained in:
xsz233 2023-06-28 07:53:31 +00:00
parent dc5da459e1
commit 9e68a347ce
10 changed files with 204 additions and 21 deletions

View File

@ -4,7 +4,7 @@
{
"name": 511,
"libpath": "libinstalls.z.so",
"run-on-create": true,
"run-on-create": false,
"depend_time_out": 60000,
"distributed": false,
"bootphase": "CoreStartPhase",

View File

@ -17,6 +17,7 @@ install_daemon_sources = [
"${services_path}/bundlemgr/src/aot/aot_args.cpp",
"${services_path}/bundlemgr/src/aot/aot_executor.cpp",
"${services_path}/bundlemgr/src/installd/installd_host_impl.cpp",
"${services_path}/bundlemgr/src/installd/installd_load_callback.cpp",
"${services_path}/bundlemgr/src/installd/installd_operator.cpp",
"${services_path}/bundlemgr/src/installd/installd_permission_mgr.cpp",
"${services_path}/bundlemgr/src/installd/installd_service.cpp",
@ -46,6 +47,7 @@ bundle_install_sources = [
"${services_path}/bundlemgr/src/event_report.cpp",
"${services_path}/bundlemgr/src/inner_bundle_info.cpp",
"${services_path}/bundlemgr/src/inner_bundle_user_info.cpp",
"${services_path}/bundlemgr/src/installd/installd_load_callback.cpp",
"${services_path}/bundlemgr/src/installd_client.cpp",
"${services_path}/bundlemgr/src/installd_death_recipient.cpp",
"${services_path}/bundlemgr/src/ipc/create_dir_param.cpp",

View File

@ -152,6 +152,7 @@ private:
bool InitAppControl();
bool InitQuickFixManager();
bool InitOverlayManager();
void CreateBmsServiceDir();
private:
bool ready_ = false;

View File

@ -0,0 +1,33 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_LOAD_CALLBACK_H
#define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_LOAD_CALLBACK_H
#include "iremote_object.h"
#include "system_ability_load_callback_stub.h"
namespace OHOS {
namespace AppExecFwk {
class InstalldLoadCallback : public SystemAbilityLoadCallbackStub {
public:
InstalldLoadCallback() = default;
void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject) override;
void OnLoadSystemAbilityFail(int32_t systemAbilityId) override;
};
} // namespace AppExecFwk
} // namespace OHOS
#endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_LOAD_CALLBACK_H

View File

@ -16,6 +16,7 @@
#ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_CLIENT_H
#define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_CLIENT_H
#include <condition_variable>
#include <memory>
#include <mutex>
#include <string>
@ -161,12 +162,16 @@ public:
ErrCode MoveFiles(const std::string &srcDir, const std::string &desDir);
void OnLoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject);
void OnLoadSystemAbilityFail();
private:
/**
* @brief Get the installd proxy object.
* @return Returns true if the installd proxy object got successfully; returns false otherwise.
*/
bool GetInstalldProxy();
bool LoadInstalldService();
template<typename F, typename... Args>
ErrCode CallService(F func, Args&&... args)
@ -178,7 +183,10 @@ private:
}
private:
bool loadSaFinished_;
std::mutex mutex_;
std::mutex loadSaMutex_;
std::condition_variable loadSaCondition_;
sptr<IInstalld> installdProxy_;
sptr<IRemoteObject::DeathRecipient> recipient_;
};

View File

@ -9,6 +9,7 @@
"services" : [{
"name" : "installs",
"path" : ["/system/bin/sa_main", "/system/profile/installs.json"],
"ondemand" : true,
"importance" : -20,
"uid" : "installs",
"gid" : ["installs"],

View File

@ -15,6 +15,8 @@
#include "bundle_mgr_service.h"
#include <sys/stat.h>
#include "account_helper.h"
#include "app_log_wrapper.h"
#include "bundle_common_event.h"
@ -26,6 +28,7 @@
#include "common_event_support.h"
#include "datetime_ex.h"
#include "ffrt.h"
#include "installd_client.h"
#ifdef BUNDLE_FRAMEWORK_APP_CONTROL
#include "app_control_manager_host_impl.h"
#endif
@ -99,6 +102,7 @@ bool BundleMgrService::Init()
}
APP_LOGI("Init begin");
CreateBmsServiceDir();
InitBmsParam();
CHECK_INIT_RESULT(InitBundleMgrHost(), "Init bundleMgr fail");
CHECK_INIT_RESULT(InitBundleInstaller(), "Init bundleInstaller fail");
@ -256,6 +260,16 @@ bool BundleMgrService::InitOverlayManager()
return true;
}
void BundleMgrService::CreateBmsServiceDir()
{
auto ret = InstalldClient::GetInstance()->Mkdir(
Constants::HAP_COPY_PATH, S_IRWXU | S_IXGRP | S_IRGRP | S_IROTH | S_IXOTH,
Constants::FOUNDATION_UID, Constants::BMS_GID);
if (!ret) {
APP_LOGW("create dir failed");
}
}
sptr<IBundleInstaller> BundleMgrService::GetBundleInstaller() const
{
return installer_;

View File

@ -0,0 +1,51 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "installd/installd_load_callback.h"
#include "app_log_wrapper.h"
#include "installd_client.h"
#include "system_ability_definition.h"
namespace OHOS {
namespace AppExecFwk {
void InstalldLoadCallback::OnLoadSystemAbilitySuccess(
int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject)
{
if (systemAbilityId != INSTALLD_SERVICE_ID) {
APP_LOGE("System ability id %{public}d mismatch.", systemAbilityId);
return;
}
if (remoteObject == nullptr) {
APP_LOGE("Object is nullptr.");
return;
}
APP_LOGD("Load system ability %{public}d succeed.", systemAbilityId);
InstalldClient::GetInstance()->OnLoadSystemAbilitySuccess(remoteObject);
}
void InstalldLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
{
if (systemAbilityId != INSTALLD_SERVICE_ID) {
APP_LOGE("System ability id %{public}d mismatch.", systemAbilityId);
return;
}
APP_LOGD("Load system ability %{public}d failed.", systemAbilityId);
InstalldClient::GetInstance()->OnLoadSystemAbilityFail();
}
}
}

View File

@ -35,14 +35,14 @@ namespace {
const int32_t INSTALLD_ID = 511;
}
REGISTER_SYSTEM_ABILITY_BY_ID(InstalldService, INSTALLD_ID, true);
REGISTER_SYSTEM_ABILITY_BY_ID(InstalldService, INSTALLD_ID, false);
InstalldService::InstalldService(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate)
{
APP_LOGI("installd service instance is created");
}
InstalldService::InstalldService() : SystemAbility(INSTALLD_ID, true)
InstalldService::InstalldService() : SystemAbility(INSTALLD_ID, false)
{
APP_LOGI("installd service instance is created");
}

View File

@ -17,12 +17,19 @@
#include "app_log_wrapper.h"
#include "bundle_constants.h"
#include "if_system_ability_manager.h"
#include "installd/installd_load_callback.h"
#include "installd_death_recipient.h"
#include "iservice_registry.h"
#include "system_ability_definition.h"
#include "system_ability_helper.h"
namespace OHOS {
namespace AppExecFwk {
namespace {
const int LOAD_SA_TIMEOUT_MS = 4 * 1000;
} // namespace
ErrCode InstalldClient::CreateBundleDir(const std::string &bundleDir)
{
if (bundleDir.empty()) {
@ -159,30 +166,68 @@ void InstalldClient::ResetInstalldProxy()
installdProxy_ = nullptr;
}
bool InstalldClient::GetInstalldProxy()
bool InstalldClient::LoadInstalldService()
{
if (installdProxy_ == nullptr) {
APP_LOGD("try to get installd proxy");
std::lock_guard<std::mutex> lock(mutex_);
if (installdProxy_ == nullptr) {
sptr<IInstalld> tempProxy =
iface_cast<IInstalld>(SystemAbilityHelper::GetSystemAbility(INSTALLD_SERVICE_ID));
if ((tempProxy == nullptr) || (tempProxy->AsObject() == nullptr)) {
APP_LOGE("the installd proxy or remote object is null");
return false;
}
recipient_ = new (std::nothrow) InstalldDeathRecipient();
if (recipient_ == nullptr) {
APP_LOGE("the death recipient is nullptr");
return false;
}
tempProxy->AsObject()->AddDeathRecipient(recipient_);
installdProxy_ = tempProxy;
{
std::unique_lock<std::mutex> lock(loadSaMutex_);
loadSaFinished_ = false;
}
auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (systemAbilityMgr == nullptr) {
APP_LOGE("Failed to get SystemAbilityManager.");
return false;
}
sptr<InstalldLoadCallback> loadCallback = new (std::nothrow) InstalldLoadCallback();
if (loadCallback == nullptr) {
APP_LOGE("Create load callback failed.");
return false;
}
auto ret = systemAbilityMgr->LoadSystemAbility(INSTALLD_SERVICE_ID, loadCallback);
if (ret != 0) {
APP_LOGE("Load system ability %{public}d failed with %{public}d.", INSTALLD_SERVICE_ID, ret);
return false;
}
{
std::unique_lock<std::mutex> lock(loadSaMutex_);
auto waitStatus = loadSaCondition_.wait_for(lock, std::chrono::milliseconds(LOAD_SA_TIMEOUT_MS),
[this]() {
return loadSaFinished_;
});
if (!waitStatus) {
APP_LOGE("Wait for load sa timeout.");
return false;
}
}
return true;
}
bool InstalldClient::GetInstalldProxy()
{
if (installdProxy_ != nullptr) {
APP_LOGD("installd ready");
return true;
}
APP_LOGD("try to get installd proxy");
if (!LoadInstalldService()) {
APP_LOGE("load installd service failed");
return false;
}
if ((installdProxy_ == nullptr) || (installdProxy_->AsObject() == nullptr)) {
APP_LOGE("the installd proxy or remote object is null");
return false;
}
recipient_ = new (std::nothrow) InstalldDeathRecipient();
if (recipient_ == nullptr) {
APP_LOGE("the death recipient is nullptr");
return false;
}
installdProxy_->AsObject()->AddDeathRecipient(recipient_);
return true;
}
ErrCode InstalldClient::ScanDir(
const std::string &dir, ScanMode scanMode, ResultMode resultMode, std::vector<std::string> &paths)
{
@ -302,5 +347,33 @@ ErrCode InstalldClient::MoveFiles(const std::string &srcDir, const std::string &
}
return CallService(&IInstalld::MoveFiles, srcDir, desDir);
}
void InstalldClient::OnLoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject)
{
{
std::lock_guard<std::mutex> lock(mutex_);
installdProxy_ = iface_cast<IInstalld>(remoteObject);
}
{
std::lock_guard<std::mutex> lock(loadSaMutex_);
loadSaFinished_ = true;
loadSaCondition_.notify_one();
}
}
void InstalldClient::OnLoadSystemAbilityFail()
{
{
std::lock_guard<std::mutex> lock(mutex_);
installdProxy_ = nullptr;
}
{
std::lock_guard<std::mutex> lock(loadSaMutex_);
loadSaFinished_ = true;
loadSaCondition_.notify_one();
}
}
} // namespace AppExecFwk
} // namespace OHOS