diff --git a/services/samgr/native/include/system_ability_manager.h b/services/samgr/native/include/system_ability_manager.h index be32c56f7bbf302e27c8450d6796b7702d4efbc7..c3c23e2e1f06067f936d20b558185c9da61f42bc 100644 --- a/services/samgr/native/include/system_ability_manager.h +++ b/services/samgr/native/include/system_ability_manager.h @@ -233,6 +233,7 @@ public: collectManager_->RemoveWhiteCommonEvent(); } } + void InitDbinderService(); void RemoveOnDemandSaInDiedProc(std::shared_ptr& processContext); private: enum class AbilityState { @@ -335,6 +336,7 @@ private: void IpcDumpSamgrProcess(int32_t fd, int32_t cmd); void IpcDumpSingleProcess(int32_t fd, int32_t cmd, const std::string processName); int32_t IpcDumpProc(int32_t fd, const std::vector& args); + void RegisterDistribute(int32_t said, bool isDistributed); std::u16string deviceName_; static sptr instance; @@ -348,6 +350,10 @@ private: sptr collectManager_; std::shared_ptr rpcCallbackImp_; + std::shared_mutex dBinderServiceLock_; + std::list distributedSaList_; + bool isDbinderServiceInit_ = false; + // must hold abilityMapLock_ never access other locks std::shared_mutex abilityMapLock_; std::map abilityMap_; diff --git a/services/samgr/native/source/collect/device_networking_collect.cpp b/services/samgr/native/source/collect/device_networking_collect.cpp index 44120afca395c6851cf5e8a482a04d547fc26597..97ad65b0f3c1f962189b8569faef50171ee29235 100644 --- a/services/samgr/native/source/collect/device_networking_collect.cpp +++ b/services/samgr/native/source/collect/device_networking_collect.cpp @@ -193,6 +193,7 @@ void DeviceInitCallBack::OnRemoteDied() void DeviceStateCallback::OnDeviceOnline(const DmDeviceInfo& deviceInfo) { HILOGI("DeviceNetworkingCollect OnDeviceOnline size %{public}zu", deviceOnlineSet_.size()); + SystemAbilityManager::GetInstance()->InitDbinderService(); { lock_guard autoLock(deviceOnlineLock_); deviceOnlineSet_.emplace(deviceInfo.networkId); diff --git a/services/samgr/native/source/system_ability_manager.cpp b/services/samgr/native/source/system_ability_manager.cpp index 1814010c02c22d88ee6e5e57ad9558d420e2d0b9..6768db9f529407223adc7a53b8d834c0283229a2 100644 --- a/services/samgr/native/source/system_ability_manager.cpp +++ b/services/samgr/native/source/system_ability_manager.cpp @@ -84,7 +84,6 @@ sptr SystemAbilityManager::instance; SystemAbilityManager::SystemAbilityManager() { - dBinderService_ = DBinderService::GetInstance(); } SystemAbilityManager::~SystemAbilityManager() @@ -94,6 +93,48 @@ SystemAbilityManager::~SystemAbilityManager() } } +void SystemAbilityManager::RegisterDistribute(int32_t systemAbilityId, bool isDistributed) +{ + if (isDistributed) { + std::shared_lock readLock(dBinderServiceLock_); + if (dBinderService_ != nullptr) { + u16string strName = Str8ToStr16(to_string(systemAbilityId)); + dBinderService_->RegisterRemoteProxy(strName, systemAbilityId); + HILOGI("AddSystemAbility RegisterRemoteProxy, SA:%{public}d", systemAbilityId); + } + } + if (systemAbilityId == SOFTBUS_SERVER_SA_ID) { + std::shared_lock readLock(dBinderServiceLock_); + if (dBinderService_ != nullptr && rpcCallbackImp_ != nullptr) { + bool ret = dBinderService_->StartDBinderService(rpcCallbackImp_); + HILOGI("start result is %{public}s", ret ? "succeed" : "fail"); + } + } +} + +void SystemAbilityManager::InitDbinderService() +{ + std::unique_lock writeLock(dBinderServiceLock_); + if (!isDbinderServiceInit_) { + dBinderService_ = DBinderService::GetInstance(); + rpcCallbackImp_ = make_shared(); + if (dBinderService_ != nullptr) { + for (auto said : distributedSaList_) { + u16string strName = Str8ToStr16(to_string(said)); + dBinderService_->RegisterRemoteProxy(strName, said); + HILOGI("AddSystemAbility RegisterRemoteProxy, SA:%{public}d", said); + } + } + isDbinderServiceInit_ = true; + } + if (CheckSystemAbility(SOFTBUS_SERVER_SA_ID) != nullptr) { + if (dBinderService_ != nullptr && rpcCallbackImp_ != nullptr) { + bool ret = dBinderService_->StartDBinderService(rpcCallbackImp_); + HILOGI("start result is %{public}s", ret ? "succeed" : "fail"); + } + } +} + void SystemAbilityManager::Init() { abilityDeath_ = sptr(new AbilityDeathRecipient()); @@ -102,7 +143,6 @@ void SystemAbilityManager::Init() abilityCallbackDeath_ = sptr(new AbilityCallbackDeathRecipient()); remoteCallbackDeath_ = sptr(new RemoteCallbackDeathRecipient()); - rpcCallbackImp_ = make_shared(); if (workHandler_ == nullptr) { workHandler_ = make_shared("workHandler"); } @@ -979,6 +1019,7 @@ void SystemAbilityManager::NotifyRemoteSaDied(const std::u16string& name) std::u16string saName; std::string deviceId; SamgrUtil::ParseRemoteSaName(name, deviceId, saName); + std::shared_lock readLock(dBinderServiceLock_); if (dBinderService_ != nullptr) { std::string nodeId = SamgrUtil::TransformDeviceId(deviceId, NODE_ID, false); dBinderService_->NoticeServiceDie(saName, nodeId); @@ -989,6 +1030,7 @@ void SystemAbilityManager::NotifyRemoteSaDied(const std::u16string& name) void SystemAbilityManager::NotifyRemoteDeviceOffline(const std::string& deviceId) { + std::shared_lock readLock(dBinderServiceLock_); if (dBinderService_ != nullptr) { dBinderService_->NoticeDeviceDie(deviceId); HILOGI("NotifyRemoteDeviceOffline, deviceId:%{public}s", AnonymizeDeviceId(deviceId).c_str()); @@ -1033,20 +1075,10 @@ int32_t SystemAbilityManager::AddSystemAbility(int32_t systemAbilityId, const sp KHILOGI("insert %{public}d. size:%{public}zu", systemAbilityId, abilityMap_.size()); } RemoveCheckLoadedMsg(systemAbilityId); + RegisterDistribute(systemAbilityId, extraProp.isDistributed); if (abilityDeath_ != nullptr) { ability->AddDeathRecipient(abilityDeath_); } - u16string strName = Str8ToStr16(to_string(systemAbilityId)); - if (extraProp.isDistributed && dBinderService_ != nullptr) { - dBinderService_->RegisterRemoteProxy(strName, systemAbilityId); - HILOGI("AddSystemAbility RegisterRemoteProxy, SA:%{public}d", systemAbilityId); - } - if (systemAbilityId == SOFTBUS_SERVER_SA_ID) { - if (dBinderService_ != nullptr && rpcCallbackImp_ != nullptr) { - bool ret = dBinderService_->StartDBinderService(rpcCallbackImp_); - HILOGI("start result is %{public}s", ret ? "succeed" : "fail"); - } - } if (abilityStateScheduler_ == nullptr) { HILOGE("abilityStateScheduler is nullptr"); return ERR_INVALID_VALUE; @@ -1840,6 +1872,7 @@ sptr SystemAbilityManager::DoMakeRemoteBinder(int32_t system DeviceIdToNetworkId(networkId); #endif sptr remoteBinder = nullptr; + std::shared_lock readLock(dBinderServiceLock_); if (dBinderService_ != nullptr) { string strName = to_string(systemAbilityId); { @@ -1861,6 +1894,7 @@ void SystemAbilityManager::NotifyRpcLoadCompleted(const std::string& srcDeviceId return; } auto notifyTask = [srcDeviceId, systemAbilityId, remoteObject, this]() { + std::shared_lock readLock(dBinderServiceLock_); if (dBinderService_ != nullptr) { SamgrXCollie samgrXCollie("samgr--LoadSystemAbilityComplete_" + ToString(systemAbilityId)); dBinderService_->LoadSystemAbilityComplete(srcDeviceId, systemAbilityId, remoteObject);