From d20ca5294458e9d721c383a761c4043a37347069 Mon Sep 17 00:00:00 2001 From: gaoqiang_strong Date: Mon, 2 Jun 2025 19:32:17 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=86=85=E5=AD=98=E9=87=8A=E6=94=BE?= =?UTF-8?q?=E5=92=8C=E5=8A=A0=E9=94=81=E4=BF=9D=E6=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gaoqiang_strong --- diff.txt | 546 ++++++++++++++++++ .../kits/js/src/native_devicemanager_js.cpp | 2 + .../js4.0/src/native_devicemanager_js.cpp | 2 + .../include/device_manager_service_impl.h | 14 +- .../src/device_manager_service_impl.cpp | 306 ++++++---- 5 files changed, 766 insertions(+), 104 deletions(-) create mode 100644 diff.txt diff --git a/diff.txt b/diff.txt new file mode 100644 index 000000000..ce4475dd2 --- /dev/null +++ b/diff.txt @@ -0,0 +1,546 @@ +diff --git a/interfaces/kits/js/src/native_devicemanager_js.cpp b/interfaces/kits/js/src/native_devicemanager_js.cpp +index 2c619595fb5a930379105d7722054022e24c3b54..633b02b6b4baa9ea0b3dbf807bff94168efc9b96 100644 +--- a/interfaces/kits/js/src/native_devicemanager_js.cpp ++++ b/interfaces/kits/js/src/native_devicemanager_js.cpp +@@ -2084,6 +2084,8 @@ napi_value DeviceManagerNapi::GetTrustedDeviceList(napi_env env, napi_callback_i + return CallDeviceList(env, info, deviceInfoListAsyncCallbackInfo); + } else if (argc == DM_NAPI_ARGS_TWO) { + return GetTrustedDeviceListByFilter(env, info, deviceInfoListAsyncCallbackInfo); ++ } else { ++ DeleteAsyncCallbackInfo(deviceInfoListAsyncCallbackInfo); + } + napi_get_undefined(env, &result); + return result; +diff --git a/interfaces/kits/js4.0/src/native_devicemanager_js.cpp b/interfaces/kits/js4.0/src/native_devicemanager_js.cpp +index d9092f571bae242dfef82e0d4d9678b50c98277e..4117aacc36acd2a4f5aaa7faf08801cc4ad2e65e 100644 +--- a/interfaces/kits/js4.0/src/native_devicemanager_js.cpp ++++ b/interfaces/kits/js4.0/src/native_devicemanager_js.cpp +@@ -1333,6 +1333,8 @@ napi_value DeviceManagerNapi::GetAvailableDeviceList(napi_env env, napi_callback + return nullptr; + } + return CallDeviceList(env, info, deviceBasicInfoListAsyncCallbackInfo); ++ } else { ++ DeleteAsyncCallbackInfo(deviceBasicInfoListAsyncCallbackInfo); + } + napi_get_undefined(env, &result); + return result; +diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h +index c69db3f78e55b2e57e5b1dad1e32d3a59072d3ae..165247131ccd7a616f5ac2a6bfb41c2e68e8245a 100644 +--- a/services/implementation/include/device_manager_service_impl.h ++++ b/services/implementation/include/device_manager_service_impl.h +@@ -286,10 +286,15 @@ private: + void SetBindCallerInfoToBindParam(const std::map &bindParam, + std::map &bindParamTmp, const DmBindCallerInfo &bindCallerInfo); + std::string GetBundleLable(const std::string &bundleName); ++ int32_t GetLogicalIdAndTokenIdBySessionId(uint64_t &logicalSessionId, uint64_t &tokenId, int32_t sessionId); ++ void SaveTokenIdAndSessionId(uint64_t &tokenId, int32_t &sessionId, uint64_t &logicalSessionId); ++ void ReleaseMaps(); ++ int32_t InitNewProtocolAuthMgr(bool isSrcSide, uint64_t tokenId, uint64_t logicalSessionId, ++ const std::string &pkgName); ++ int32_t InitOldProtocolAuthMgr(uint64_t tokenId, const std::string &pkgName); + private: + std::shared_ptr authMgr_; // Old protocol only +- std::mutex authMgrMtx_; +- std::map> authMgrMap_; // New protocol sharing ++ + std::shared_ptr hiChainConnector_; + std::shared_ptr hiChainAuthConnector_; + std::shared_ptr deviceStateMgr_; +@@ -310,9 +315,14 @@ private: + std::map sessionEnableCvMap_; // Condition variable corresponding to the session + std::map sessionEnableMutexMap_; // Lock corresponding to the session + std::map sessionEnableCvReadyMap_; // Condition variable ready flag ++ std::mutex logicalSessionId2TokenIdMapMtx_; + std::map logicalSessionId2TokenIdMap_; // The relationship between logicalSessionId and tokenId ++ std::mutex logicalSessionId2SessionIdMapMtx_; + std::map logicalSessionId2SessionIdMap_; // The relationship logicalSessionId and physical sessionId ++ std::mutex configsMapMutex_; + std::map> configsMap_; // Import when authMgr is not initialized ++ std::mutex authMgrMapMtx_; ++ std::map> authMgrMap_; // New protocol sharing + + std::thread thread_; + std::atomic running_; +diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp +index 314cdf47b98790973bb9de2b2829fcf29c0e3f5e..8001c0a6ba64ebcd53d215c960730dd0fce7db67 100644 +--- a/services/implementation/src/device_manager_service_impl.cpp ++++ b/services/implementation/src/device_manager_service_impl.cpp +@@ -234,6 +234,7 @@ void DeviceManagerServiceImpl::ImportConfig(std::shared_ptr aut + const std::string &pkgName) + { + // Import configuration ++ std::lock_guard configsLock(configsMapMutex_); + if (configsMap_.find(tokenId) != configsMap_.end()) { + authMgr->ImportAuthCode(configsMap_[tokenId]->pkgName, configsMap_[tokenId]->authCode); + authMgr->RegisterAuthenticationType(configsMap_[tokenId]->authenticationType); +@@ -266,6 +267,7 @@ void DeviceManagerServiceImpl::ImportAuthCodeToConfig(std::shared_ptrGetAuthCodeAndPkgName(pkgName, authCode); ++ std::lock_guard configsLock(configsMapMutex_); + if (configsMap_.find(tokenId) == configsMap_.end()) { + configsMap_[tokenId] = std::make_shared(); + } +@@ -273,6 +275,51 @@ void DeviceManagerServiceImpl::ImportAuthCodeToConfig(std::shared_ptrauthCode = authCode; + } + ++int32_t DeviceManagerServiceImpl::InitNewProtocolAuthMgr(bool isSrcSide, uint64_t tokenId, uint64_t logicalSessionId, ++ const std::string &pkgName) ++{ ++ std::shared_ptr authMgr = nullptr; ++ // Create a new auth_mgr, create authMgr ++ if (isSrcSide) { ++ // src end ++ authMgr = std::make_shared(softbusConnector_, hiChainConnector_, ++ listener_, hiChainAuthConnector_); ++ } else { ++ // sink end ++ authMgr = std::make_shared(softbusConnector_, hiChainConnector_, ++ listener_, hiChainAuthConnector_); ++ } ++ // Register resource destruction notification function ++ authMgr->RegisterCleanNotifyCallback(&DeviceManagerServiceImpl::NotifyCleanEvent); ++ hiChainAuthConnector_->RegisterHiChainAuthCallbackById(logicalSessionId, authMgr); ++ LOGI("Initialize authMgr token: %{public}" PRId64 ".", tokenId); ++ ImportConfig(authMgr, tokenId, pkgName); ++ { ++ std::lock_guard lock(authMgrMapMtx_); ++ authMgrMap_[tokenId] = authMgr; ++ } ++ return DM_OK; ++} ++ ++int32_t DeviceManagerServiceImpl::InitOldProtocolAuthMgr(uint64_t tokenId, const std::string &pkgName) ++{ ++ if (authMgr_ == nullptr) { ++ CreateGlobalClassicalAuthMgr(); ++ } ++ authMgr_->PrepareSoftbusSessionCallback(); ++ { ++ std::lock_guard lock(authMgrMapMtx_); ++ authMgrMap_[tokenId] = authMgr_; ++ } ++ ImportConfig(authMgr_, tokenId, pkgName); ++ { ++ // The value of logicalSessionId in the old protocol is always 0. ++ std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); ++ logicalSessionId2TokenIdMap_[0] = tokenId; ++ } ++ return DM_OK; ++} ++ + int32_t DeviceManagerServiceImpl::InitAndRegisterAuthMgr(bool isSrcSide, uint64_t tokenId, + std::shared_ptr session, uint64_t logicalSessionId, const std::string &pkgName) + { +@@ -292,47 +339,24 @@ int32_t DeviceManagerServiceImpl::InitAndRegisterAuthMgr(bool isSrcSide, uint64_ + return ERR_DM_AUTH_BUSINESS_BUSY; + } + } +- +- std::lock_guard lock(authMgrMtx_); +- if (authMgrMap_.find(tokenId) == authMgrMap_.end()) { +- if (session->version_ == "" || CompareVersion(session->version_, DM_VERSION_5_0_OLD_MAX)) { +- if (authMgrMap_.size() > MAX_NEW_PROC_SESSION_COUNT_TEMP) { +- LOGE("Other bind session exist, can not start new one."); +- return ERR_DM_AUTH_BUSINESS_BUSY; +- } +- // Create a new auth_mgr, create authMgrMap_[tokenId] +- if (isSrcSide) { +- // src end +- authMgrMap_[tokenId] = std::make_shared(softbusConnector_, hiChainConnector_, +- listener_, hiChainAuthConnector_); +- } else { +- // sink end +- authMgrMap_[tokenId] = std::make_shared(softbusConnector_, hiChainConnector_, +- listener_, hiChainAuthConnector_); +- } +- // Register resource destruction notification function +- authMgrMap_[tokenId]->RegisterCleanNotifyCallback(&DeviceManagerServiceImpl::NotifyCleanEvent); +- hiChainAuthConnector_->RegisterHiChainAuthCallbackById(logicalSessionId, authMgrMap_[tokenId]); +- LOGI("Initialize authMgrMap_ token: %{public}" PRId64 ".", tokenId); +- ImportConfig(authMgrMap_[tokenId], tokenId, pkgName); +- return DM_OK; +- } else { +- LOGI("InitAndRegisterAuthMgr old authMgr."); +- if (authMgr_ == nullptr) { +- CreateGlobalClassicalAuthMgr(); +- } +- authMgr_->PrepareSoftbusSessionCallback(); +- authMgrMap_[tokenId] = authMgr_; +- ImportConfig(authMgr_, tokenId, pkgName); +- // The value of logicalSessionId in the old protocol is always 0. +- logicalSessionId2TokenIdMap_[0] = tokenId; +- return DM_OK; ++ { ++ std::lock_guard lock(authMgrMapMtx_); ++ if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { ++ // authMgr_ has been created, indicating that a binding event already exists. ++ // Other requests are rejected, and an error code is returned. ++ LOGE("BindTarget failed, this device is being bound. Please try again later."); ++ return ERR_DM_AUTH_BUSINESS_BUSY; ++ } ++ if (authMgrMap_.size() > MAX_NEW_PROC_SESSION_COUNT_TEMP) { ++ LOGE("Other bind session exist, can not start new one."); ++ return ERR_DM_AUTH_BUSINESS_BUSY; + } + } +- // authMgr_ has been created, indicating that a binding event already exists. +- // Other requests are rejected, and an error code is returned. +- LOGE("BindTarget failed, this device is being bound. Please try again later."); +- return ERR_DM_AUTH_BUSINESS_BUSY; ++ if (session->version_ == "" || CompareVersion(session->version_, DM_VERSION_5_0_OLD_MAX)) { ++ return InitNewProtocolAuthMgr(isSrcSide, tokenId, logicalSessionId, pkgName); ++ } ++ LOGI("InitAndRegisterAuthMgr old authMgr."); ++ return InitOldProtocolAuthMgr(tokenId, pkgName); + } + + void DeviceManagerServiceImpl::CleanSessionMap(int sessionId, std::shared_ptr session) +@@ -356,26 +380,37 @@ void DeviceManagerServiceImpl::CleanSessionMap(int sessionId, std::shared_ptr tokenIdLock(logicalSessionId2TokenIdMapMtx_); ++ logicalSessionId2TokenIdMap_.erase(logicalSessionId); ++ } ++ int32_t sessionId = 0; ++ { ++ std::lock_guard sessionIdLock(logicalSessionId2SessionIdMapMtx_); ++ if (logicalSessionId2SessionIdMap_.find(logicalSessionId) == logicalSessionId2SessionIdMap_.end()) { ++ return; + } ++ sessionId = logicalSessionId2SessionIdMap_[logicalSessionId]; + logicalSessionId2SessionIdMap_.erase(logicalSessionId); + } +- logicalSessionId2TokenIdMap_.erase(logicalSessionId); ++ auto session = GetCurSession(sessionId); ++ if (session != nullptr) { ++ CleanSessionMap(sessionId, session); ++ } + return; + } + + void DeviceManagerServiceImpl::CleanAuthMgrByLogicalSessionId(uint64_t logicalSessionId) + { + uint64_t tokenId = 0; +- if (logicalSessionId2TokenIdMap_.find(logicalSessionId) != logicalSessionId2TokenIdMap_.end()) { +- tokenId = logicalSessionId2TokenIdMap_[logicalSessionId]; +- } else { +- LOGE("logicalSessionId(%{public}" PRIu64 ") can not find the tokenId.", logicalSessionId); +- return; ++ { ++ std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); ++ if (logicalSessionId2TokenIdMap_.find(logicalSessionId) != logicalSessionId2TokenIdMap_.end()) { ++ tokenId = logicalSessionId2TokenIdMap_[logicalSessionId]; ++ } else { ++ LOGE("logicalSessionId(%{public}" PRIu64 ") can not find the tokenId.", logicalSessionId); ++ return; ++ } + } + + CleanSessionMapByLogicalSessionId(logicalSessionId); +@@ -384,9 +419,12 @@ void DeviceManagerServiceImpl::CleanAuthMgrByLogicalSessionId(uint64_t logicalSe + authMgr_->ClearSoftbusSessionCallback(); + } + hiChainAuthConnector_->UnRegisterHiChainAuthCallbackById(logicalSessionId); +- if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { +- authMgrMap_[tokenId] = nullptr; +- authMgrMap_.erase(tokenId); ++ { ++ std::lock_guard lock(authMgrMapMtx_); ++ if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { ++ authMgrMap_[tokenId] = nullptr; ++ authMgrMap_.erase(tokenId); ++ } + } + return; + } +@@ -394,9 +432,12 @@ void DeviceManagerServiceImpl::CleanAuthMgrByLogicalSessionId(uint64_t logicalSe + std::shared_ptr DeviceManagerServiceImpl::GetAuthMgr() + { + uint64_t tokenId = IPCSkeleton::GetCallingTokenID(); +- if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { +- LOGI("authMgrMap_ token: %{public}" PRId64 ".", tokenId); +- return authMgrMap_[tokenId]; ++ { ++ std::lock_guard lock(authMgrMapMtx_); ++ if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { ++ LOGI("authMgrMap_ token: %{public}" PRId64 ".", tokenId); ++ return authMgrMap_[tokenId]; ++ } + } + LOGE("authMgrMap_ not found, token: %{public}" PRId64 ".", tokenId); + return nullptr; +@@ -405,9 +446,12 @@ std::shared_ptr DeviceManagerServiceImpl::GetAuthMgr() + // Needed in the callback function + std::shared_ptr DeviceManagerServiceImpl::GetAuthMgrByTokenId(uint64_t tokenId) + { +- if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { +- LOGI("authMgrMap_ token: %{public}" PRId64 ".", tokenId); +- return authMgrMap_[tokenId]; ++ { ++ std::lock_guard lock(authMgrMapMtx_); ++ if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { ++ LOGI("authMgrMap_ token: %{public}" PRId64 ".", tokenId); ++ return authMgrMap_[tokenId]; ++ } + } + LOGE("authMgrMap_ not found, token: %{public}" PRId64 ".", tokenId); + return nullptr; +@@ -416,12 +460,18 @@ std::shared_ptr DeviceManagerServiceImpl::GetAuthMgrByTokenId(u + std::shared_ptr DeviceManagerServiceImpl::GetCurrentAuthMgr() + { + uint64_t tokenId = 0; +- if (logicalSessionId2TokenIdMap_.find(0) != logicalSessionId2TokenIdMap_.end()) { +- tokenId = logicalSessionId2TokenIdMap_[0]; ++ { ++ std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); ++ if (logicalSessionId2TokenIdMap_.find(0) != logicalSessionId2TokenIdMap_.end()) { ++ tokenId = logicalSessionId2TokenIdMap_[0]; ++ } + } +- for (auto &pair : authMgrMap_) { +- if (pair.first != tokenId) { +- return pair.second; ++ { ++ std::lock_guard lock(authMgrMapMtx_); ++ for (auto &pair : authMgrMap_) { ++ if (pair.first != tokenId) { ++ return pair.second; ++ } + } + } + return authMgr_; +@@ -483,6 +533,40 @@ int32_t DeviceManagerServiceImpl::Initialize(const std::shared_ptr lock(authMgrMapMtx_); ++ for (auto& pair : authMgrMap_) { ++ pair.second = nullptr; ++ } ++ authMgrMap_.clear(); ++ } ++ for (auto& pair : sessionsMap_) { ++ pair.second = nullptr; ++ } ++ sessionsMap_.clear(); ++ { ++ std::lock_guard configsLock(configsMapMutex_); ++ for (auto& pair : configsMap_) { ++ pair.second = nullptr; ++ } ++ configsMap_.clear(); ++ } ++ deviceId2SessionIdMap_.clear(); ++ deviceIdMutexMap_.clear(); ++ sessionEnableMutexMap_.clear(); ++ sessionEnableCvMap_.clear(); ++ { ++ std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); ++ logicalSessionId2TokenIdMap_.clear(); ++ } ++ { ++ std::lock_guard sessionIdLock(logicalSessionId2SessionIdMapMtx_); ++ logicalSessionId2SessionIdMap_.clear(); ++ } ++} ++ + void DeviceManagerServiceImpl::Release() + { + LOGI("Release"); +@@ -503,24 +587,7 @@ void DeviceManagerServiceImpl::Release() + hiChainAuthConnector_->UnRegisterHiChainAuthCallback(); + } + authMgr_ = nullptr; +- for (auto& pair : authMgrMap_) { +- pair.second = nullptr; +- } +- authMgrMap_.clear(); +- for (auto& pair : sessionsMap_) { +- pair.second = nullptr; +- } +- sessionsMap_.clear(); +- for (auto& pair : configsMap_) { +- pair.second = nullptr; +- } +- configsMap_.clear(); +- deviceId2SessionIdMap_.clear(); +- deviceIdMutexMap_.clear(); +- sessionEnableMutexMap_.clear(); +- sessionEnableCvMap_.clear(); +- logicalSessionId2TokenIdMap_.clear(); +- logicalSessionId2SessionIdMap_.clear(); ++ ReleaseMaps(); + deviceStateMgr_ = nullptr; + softbusConnector_ = nullptr; + abilityMgr_ = nullptr; +@@ -886,6 +953,8 @@ std::shared_ptr DeviceManagerServiceImpl::GetAuthMgrByMessage(i + return nullptr; + } + curSession->logicalSessionSet_.insert(logicalSessionId); ++ ++ std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); + if (logicalSessionId2TokenIdMap_.find(logicalSessionId) != logicalSessionId2TokenIdMap_.end()) { + LOGE("GetAuthMgrByMessage, logicalSessionId exists in logicalSessionId2TokenIdMap_."); + return nullptr; +@@ -896,29 +965,42 @@ std::shared_ptr DeviceManagerServiceImpl::GetAuthMgrByMessage(i + LOGE("GetAuthMgrByMessage, The logical session ID does not exist in the physical session."); + return nullptr; + } ++ std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); + tokenId = logicalSessionId2TokenIdMap_[logicalSessionId]; + } + + return GetAuthMgrByTokenId(tokenId); + } + +-int32_t DeviceManagerServiceImpl::TransferSrcOldAuthMgr(std::shared_ptr curSession) ++int32_t DeviceManagerServiceImpl::GetLogicalIdAndTokenIdBySessionId(uint64_t &logicalSessionId, ++ uint64_t &tokenId, int32_t sessionId) + { +- // New Old Receive 90, destroy new authMgr, create old authMgr, source side +- // The old protocol has only one session, reverse lookup logicalSessionId and tokenId +- int sessionId = curSession->sessionId_; +- uint64_t logicalSessionId = 0; +- uint64_t tokenId = 0; ++ std::lock_guard sessionIdLock(logicalSessionId2SessionIdMapMtx_); + for (auto& pair : logicalSessionId2SessionIdMap_) { + if (pair.second == sessionId) { + logicalSessionId = pair.first; ++ std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); + tokenId = logicalSessionId2TokenIdMap_[logicalSessionId]; + } + } + if (logicalSessionId == 0 || tokenId == 0) { +- LOGE("DeviceManagerServiceImpl::TransferSrcOldAuthMgr can not find logicalSessionId and tokenId."); ++ LOGE("can not find logicalSessionId and tokenId."); + return ERR_DM_AUTH_FAILED; + } ++ return DM_OK; ++} ++ ++int32_t DeviceManagerServiceImpl::TransferSrcOldAuthMgr(std::shared_ptr curSession) ++{ ++ // New Old Receive 90, destroy new authMgr, create old authMgr, source side ++ // The old protocol has only one session, reverse lookup logicalSessionId and tokenId ++ int sessionId = curSession->sessionId_; ++ uint64_t logicalSessionId = 0; ++ uint64_t tokenId = 0; ++ int32_t ret = GetLogicalIdAndTokenIdBySessionId(logicalSessionId, tokenId, sessionId); ++ if (ret != DM_OK) { ++ return ret; ++ } + std::string pkgName; + PeerTargetId peerTargetId; + std::map bindParam; +@@ -931,12 +1013,15 @@ int32_t DeviceManagerServiceImpl::TransferSrcOldAuthMgr(std::shared_ptr + int32_t authType = -1; + authMgr->ParseAuthType(bindParam, authType); + ImportAuthCodeToConfig(authMgr, tokenId); +- authMgrMap_.erase(tokenId); ++ { ++ std::lock_guard lock(authMgrMapMtx_); ++ authMgrMap_.erase(tokenId); ++ } + if (InitAndRegisterAuthMgr(true, tokenId, curSession, logicalSessionId, "") != DM_OK) { + return ERR_DM_AUTH_FAILED; + } + +- int ret = TransferByAuthType(authType, curSession, authMgr, bindParam, logicalSessionId); ++ ret = TransferByAuthType(authType, curSession, authMgr, bindParam, logicalSessionId); + if (ret != DM_OK) { + LOGE("DeviceManagerServiceImpl::TransferByAuthType TransferByAuthType failed."); + return ret; +@@ -966,7 +1051,10 @@ int32_t DeviceManagerServiceImpl::TransferByAuthType(int32_t authType, + if (authType == DmAuthType::AUTH_TYPE_IMPORT_AUTH_CODE) { + authMgr_->EnableInsensibleSwitching(); + curSession->logicalSessionSet_.insert(0); +- logicalSessionId2SessionIdMap_[0] = sessionId; ++ { ++ std::lock_guard sessionIdLock(logicalSessionId2SessionIdMapMtx_); ++ logicalSessionId2SessionIdMap_[0] = sessionId; ++ } + authMgr->OnSessionDisable(); + } else { + authMgr_->DisableInsensibleSwitching(); +@@ -1368,6 +1456,7 @@ int32_t DeviceManagerServiceImpl::GetUdidHashByNetWorkId(const char *networkId, + std::shared_ptr DeviceManagerServiceImpl::GetConfigByTokenId() + { + uint64_t tokenId = IPCSkeleton::GetCallingTokenID(); ++ std::lock_guard configsLock(configsMapMutex_); + if (configsMap_.find(tokenId) == configsMap_.end()) { + configsMap_[tokenId] = std::make_shared(); + } +@@ -1624,6 +1713,19 @@ int32_t DeviceManagerServiceImpl::ParseConnectAddr(const PeerTargetId &targetId, + return DM_OK; + } + ++void DeviceManagerServiceImpl::SaveTokenIdAndSessionId(uint64_t &tokenId, ++ int32_t &sessionId, uint64_t &logicalSessionId) ++{ ++ { ++ std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); ++ logicalSessionId2TokenIdMap_[logicalSessionId] = tokenId; ++ } ++ { ++ std::lock_guard sessionIdLock(logicalSessionId2SessionIdMapMtx_); ++ logicalSessionId2SessionIdMap_[logicalSessionId] = sessionId; ++ } ++} ++ + void DeviceManagerServiceImpl::BindTargetImpl(uint64_t tokenId, const std::string &pkgName, + const PeerTargetId &targetId, const std::map &bindParam) + { +@@ -1665,9 +1767,7 @@ void DeviceManagerServiceImpl::BindTargetImpl(uint64_t tokenId, const std::strin + } + curSession->logicalSessionSet_.insert(logicalSessionId); + curSession->logicalSessionCnt_.fetch_add(1); +- logicalSessionId2TokenIdMap_[logicalSessionId] = tokenId; +- logicalSessionId2SessionIdMap_[logicalSessionId] = sessionId; +- ++ SaveTokenIdAndSessionId(tokenId, sessionId, logicalSessionId); + auto authMgr = GetAuthMgrByTokenId(tokenId); + if (authMgr == nullptr) { + CleanAuthMgrByLogicalSessionId(logicalSessionId); +@@ -1694,14 +1794,15 @@ int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const P + std::map bindParamTmp; + SetBindCallerInfoToBindParam(bindParam, bindParamTmp, bindCallerInfo); + uint64_t tokenId = IPCSkeleton::GetCallingTokenID(); +- if (authMgrMap_.find(tokenId) == authMgrMap_.end()) { +- std::thread newThread(&DeviceManagerServiceImpl::BindTargetImpl, this, tokenId, pkgName, +- targetId, bindParamTmp); +- newThread.detach(); +- } else { +- LOGE("BindTarget failed, this device is being bound. please try again later."); +- return ERR_DM_AUTH_BUSINESS_BUSY; ++ { ++ std::lock_guard lock(authMgrMapMtx_); ++ if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { ++ LOGE("BindTarget failed, this device is being bound. please try again later."); ++ return ERR_DM_AUTH_BUSINESS_BUSY; ++ } + } ++ std::thread newThread(&DeviceManagerServiceImpl::BindTargetImpl, this, tokenId, pkgName, targetId, bindParamTmp); ++ newThread.detach(); + return DM_OK; + } + +@@ -1843,6 +1944,7 @@ void DeviceManagerServiceImpl::ScreenCommonEventCallback(std::string commonEvent + { + if (commonEventType == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED) { + LOGI("on screen locked."); ++ std::lock_guard lock(authMgrMapMtx_); + for (auto& pair : authMgrMap_) { + if (pair.second != nullptr) { + LOGI("tokenId: %{public}" PRId64 ".", pair.first); \ No newline at end of file diff --git a/interfaces/kits/js/src/native_devicemanager_js.cpp b/interfaces/kits/js/src/native_devicemanager_js.cpp index 2c619595f..633b02b6b 100644 --- a/interfaces/kits/js/src/native_devicemanager_js.cpp +++ b/interfaces/kits/js/src/native_devicemanager_js.cpp @@ -2084,6 +2084,8 @@ napi_value DeviceManagerNapi::GetTrustedDeviceList(napi_env env, napi_callback_i return CallDeviceList(env, info, deviceInfoListAsyncCallbackInfo); } else if (argc == DM_NAPI_ARGS_TWO) { return GetTrustedDeviceListByFilter(env, info, deviceInfoListAsyncCallbackInfo); + } else { + DeleteAsyncCallbackInfo(deviceInfoListAsyncCallbackInfo); } napi_get_undefined(env, &result); return result; diff --git a/interfaces/kits/js4.0/src/native_devicemanager_js.cpp b/interfaces/kits/js4.0/src/native_devicemanager_js.cpp index 909fc4fc9..005d39c8a 100644 --- a/interfaces/kits/js4.0/src/native_devicemanager_js.cpp +++ b/interfaces/kits/js4.0/src/native_devicemanager_js.cpp @@ -1346,6 +1346,8 @@ napi_value DeviceManagerNapi::GetAvailableDeviceList(napi_env env, napi_callback return nullptr; } return CallDeviceList(env, info, deviceBasicInfoListAsyncCallbackInfo); + } else { + DeleteAsyncCallbackInfo(deviceBasicInfoListAsyncCallbackInfo); } napi_get_undefined(env, &result); return result; diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index c69db3f78..165247131 100644 --- a/services/implementation/include/device_manager_service_impl.h +++ b/services/implementation/include/device_manager_service_impl.h @@ -286,10 +286,15 @@ private: void SetBindCallerInfoToBindParam(const std::map &bindParam, std::map &bindParamTmp, const DmBindCallerInfo &bindCallerInfo); std::string GetBundleLable(const std::string &bundleName); + int32_t GetLogicalIdAndTokenIdBySessionId(uint64_t &logicalSessionId, uint64_t &tokenId, int32_t sessionId); + void SaveTokenIdAndSessionId(uint64_t &tokenId, int32_t &sessionId, uint64_t &logicalSessionId); + void ReleaseMaps(); + int32_t InitNewProtocolAuthMgr(bool isSrcSide, uint64_t tokenId, uint64_t logicalSessionId, + const std::string &pkgName); + int32_t InitOldProtocolAuthMgr(uint64_t tokenId, const std::string &pkgName); private: std::shared_ptr authMgr_; // Old protocol only - std::mutex authMgrMtx_; - std::map> authMgrMap_; // New protocol sharing + std::shared_ptr hiChainConnector_; std::shared_ptr hiChainAuthConnector_; std::shared_ptr deviceStateMgr_; @@ -310,9 +315,14 @@ private: std::map sessionEnableCvMap_; // Condition variable corresponding to the session std::map sessionEnableMutexMap_; // Lock corresponding to the session std::map sessionEnableCvReadyMap_; // Condition variable ready flag + std::mutex logicalSessionId2TokenIdMapMtx_; std::map logicalSessionId2TokenIdMap_; // The relationship between logicalSessionId and tokenId + std::mutex logicalSessionId2SessionIdMapMtx_; std::map logicalSessionId2SessionIdMap_; // The relationship logicalSessionId and physical sessionId + std::mutex configsMapMutex_; std::map> configsMap_; // Import when authMgr is not initialized + std::mutex authMgrMapMtx_; + std::map> authMgrMap_; // New protocol sharing std::thread thread_; std::atomic running_; diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index bc58bf392..d22424a2e 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -234,6 +234,7 @@ void DeviceManagerServiceImpl::ImportConfig(std::shared_ptr aut const std::string &pkgName) { // Import configuration + std::lock_guard configsLock(configsMapMutex_); if (configsMap_.find(tokenId) != configsMap_.end()) { authMgr->ImportAuthCode(configsMap_[tokenId]->pkgName, configsMap_[tokenId]->authCode); authMgr->RegisterAuthenticationType(configsMap_[tokenId]->authenticationType); @@ -266,6 +267,7 @@ void DeviceManagerServiceImpl::ImportAuthCodeToConfig(std::shared_ptrGetAuthCodeAndPkgName(pkgName, authCode); + std::lock_guard configsLock(configsMapMutex_); if (configsMap_.find(tokenId) == configsMap_.end()) { configsMap_[tokenId] = std::make_shared(); } @@ -273,6 +275,51 @@ void DeviceManagerServiceImpl::ImportAuthCodeToConfig(std::shared_ptrauthCode = authCode; } +int32_t DeviceManagerServiceImpl::InitNewProtocolAuthMgr(bool isSrcSide, uint64_t tokenId, uint64_t logicalSessionId, + const std::string &pkgName) +{ + std::shared_ptr authMgr = nullptr; + // Create a new auth_mgr, create authMgr + if (isSrcSide) { + // src end + authMgr = std::make_shared(softbusConnector_, hiChainConnector_, + listener_, hiChainAuthConnector_); + } else { + // sink end + authMgr = std::make_shared(softbusConnector_, hiChainConnector_, + listener_, hiChainAuthConnector_); + } + // Register resource destruction notification function + authMgr->RegisterCleanNotifyCallback(&DeviceManagerServiceImpl::NotifyCleanEvent); + hiChainAuthConnector_->RegisterHiChainAuthCallbackById(logicalSessionId, authMgr); + LOGI("Initialize authMgr token: %{public}" PRId64 ".", tokenId); + ImportConfig(authMgr, tokenId, pkgName); + { + std::lock_guard lock(authMgrMapMtx_); + authMgrMap_[tokenId] = authMgr; + } + return DM_OK; +} + +int32_t DeviceManagerServiceImpl::InitOldProtocolAuthMgr(uint64_t tokenId, const std::string &pkgName) +{ + if (authMgr_ == nullptr) { + CreateGlobalClassicalAuthMgr(); + } + authMgr_->PrepareSoftbusSessionCallback(); + { + std::lock_guard lock(authMgrMapMtx_); + authMgrMap_[tokenId] = authMgr_; + } + ImportConfig(authMgr_, tokenId, pkgName); + { + // The value of logicalSessionId in the old protocol is always 0. + std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); + logicalSessionId2TokenIdMap_[0] = tokenId; + } + return DM_OK; +} + int32_t DeviceManagerServiceImpl::InitAndRegisterAuthMgr(bool isSrcSide, uint64_t tokenId, std::shared_ptr session, uint64_t logicalSessionId, const std::string &pkgName) { @@ -292,47 +339,24 @@ int32_t DeviceManagerServiceImpl::InitAndRegisterAuthMgr(bool isSrcSide, uint64_ return ERR_DM_AUTH_BUSINESS_BUSY; } } - - std::lock_guard lock(authMgrMtx_); - if (authMgrMap_.find(tokenId) == authMgrMap_.end()) { - if (session->version_ == "" || CompareVersion(session->version_, DM_VERSION_5_0_OLD_MAX)) { - if (authMgrMap_.size() > MAX_NEW_PROC_SESSION_COUNT_TEMP) { - LOGE("Other bind session exist, can not start new one."); - return ERR_DM_AUTH_BUSINESS_BUSY; - } - // Create a new auth_mgr, create authMgrMap_[tokenId] - if (isSrcSide) { - // src end - authMgrMap_[tokenId] = std::make_shared(softbusConnector_, hiChainConnector_, - listener_, hiChainAuthConnector_); - } else { - // sink end - authMgrMap_[tokenId] = std::make_shared(softbusConnector_, hiChainConnector_, - listener_, hiChainAuthConnector_); - } - // Register resource destruction notification function - authMgrMap_[tokenId]->RegisterCleanNotifyCallback(&DeviceManagerServiceImpl::NotifyCleanEvent); - hiChainAuthConnector_->RegisterHiChainAuthCallbackById(logicalSessionId, authMgrMap_[tokenId]); - LOGI("Initialize authMgrMap_ token: %{public}" PRId64 ".", tokenId); - ImportConfig(authMgrMap_[tokenId], tokenId, pkgName); - return DM_OK; - } else { - LOGI("InitAndRegisterAuthMgr old authMgr."); - if (authMgr_ == nullptr) { - CreateGlobalClassicalAuthMgr(); - } - authMgr_->PrepareSoftbusSessionCallback(); - authMgrMap_[tokenId] = authMgr_; - ImportConfig(authMgr_, tokenId, pkgName); - // The value of logicalSessionId in the old protocol is always 0. - logicalSessionId2TokenIdMap_[0] = tokenId; - return DM_OK; + { + std::lock_guard lock(authMgrMapMtx_); + if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { + // authMgr_ has been created, indicating that a binding event already exists. + // Other requests are rejected, and an error code is returned. + LOGE("BindTarget failed, this device is being bound. Please try again later."); + return ERR_DM_AUTH_BUSINESS_BUSY; + } + if (authMgrMap_.size() > MAX_NEW_PROC_SESSION_COUNT_TEMP) { + LOGE("Other bind session exist, can not start new one."); + return ERR_DM_AUTH_BUSINESS_BUSY; } } - // authMgr_ has been created, indicating that a binding event already exists. - // Other requests are rejected, and an error code is returned. - LOGE("BindTarget failed, this device is being bound. Please try again later."); - return ERR_DM_AUTH_BUSINESS_BUSY; + if (session->version_ == "" || CompareVersion(session->version_, DM_VERSION_5_0_OLD_MAX)) { + return InitNewProtocolAuthMgr(isSrcSide, tokenId, logicalSessionId, pkgName); + } + LOGI("InitAndRegisterAuthMgr old authMgr."); + return InitOldProtocolAuthMgr(tokenId, pkgName); } void DeviceManagerServiceImpl::CleanSessionMap(int sessionId, std::shared_ptr session) @@ -356,26 +380,37 @@ void DeviceManagerServiceImpl::CleanSessionMap(int sessionId, std::shared_ptr tokenIdLock(logicalSessionId2TokenIdMapMtx_); + logicalSessionId2TokenIdMap_.erase(logicalSessionId); + } + int32_t sessionId = 0; + { + std::lock_guard sessionIdLock(logicalSessionId2SessionIdMapMtx_); + if (logicalSessionId2SessionIdMap_.find(logicalSessionId) == logicalSessionId2SessionIdMap_.end()) { + return; } + sessionId = logicalSessionId2SessionIdMap_[logicalSessionId]; logicalSessionId2SessionIdMap_.erase(logicalSessionId); } - logicalSessionId2TokenIdMap_.erase(logicalSessionId); + auto session = GetCurSession(sessionId); + if (session != nullptr) { + CleanSessionMap(sessionId, session); + } return; } void DeviceManagerServiceImpl::CleanAuthMgrByLogicalSessionId(uint64_t logicalSessionId) { uint64_t tokenId = 0; - if (logicalSessionId2TokenIdMap_.find(logicalSessionId) != logicalSessionId2TokenIdMap_.end()) { - tokenId = logicalSessionId2TokenIdMap_[logicalSessionId]; - } else { - LOGE("logicalSessionId(%{public}" PRIu64 ") can not find the tokenId.", logicalSessionId); - return; + { + std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); + if (logicalSessionId2TokenIdMap_.find(logicalSessionId) != logicalSessionId2TokenIdMap_.end()) { + tokenId = logicalSessionId2TokenIdMap_[logicalSessionId]; + } else { + LOGE("logicalSessionId(%{public}" PRIu64 ") can not find the tokenId.", logicalSessionId); + return; + } } CleanSessionMapByLogicalSessionId(logicalSessionId); @@ -384,9 +419,12 @@ void DeviceManagerServiceImpl::CleanAuthMgrByLogicalSessionId(uint64_t logicalSe authMgr_->ClearSoftbusSessionCallback(); } hiChainAuthConnector_->UnRegisterHiChainAuthCallbackById(logicalSessionId); - if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { - authMgrMap_[tokenId] = nullptr; - authMgrMap_.erase(tokenId); + { + std::lock_guard lock(authMgrMapMtx_); + if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { + authMgrMap_[tokenId] = nullptr; + authMgrMap_.erase(tokenId); + } } return; } @@ -394,9 +432,12 @@ void DeviceManagerServiceImpl::CleanAuthMgrByLogicalSessionId(uint64_t logicalSe std::shared_ptr DeviceManagerServiceImpl::GetAuthMgr() { uint64_t tokenId = IPCSkeleton::GetCallingTokenID(); - if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { - LOGI("authMgrMap_ token: %{public}" PRId64 ".", tokenId); - return authMgrMap_[tokenId]; + { + std::lock_guard lock(authMgrMapMtx_); + if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { + LOGI("authMgrMap_ token: %{public}" PRId64 ".", tokenId); + return authMgrMap_[tokenId]; + } } LOGE("authMgrMap_ not found, token: %{public}" PRId64 ".", tokenId); return nullptr; @@ -405,9 +446,12 @@ std::shared_ptr DeviceManagerServiceImpl::GetAuthMgr() // Needed in the callback function std::shared_ptr DeviceManagerServiceImpl::GetAuthMgrByTokenId(uint64_t tokenId) { - if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { - LOGI("authMgrMap_ token: %{public}" PRId64 ".", tokenId); - return authMgrMap_[tokenId]; + { + std::lock_guard lock(authMgrMapMtx_); + if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { + LOGI("authMgrMap_ token: %{public}" PRId64 ".", tokenId); + return authMgrMap_[tokenId]; + } } LOGE("authMgrMap_ not found, token: %{public}" PRId64 ".", tokenId); return nullptr; @@ -416,12 +460,18 @@ std::shared_ptr DeviceManagerServiceImpl::GetAuthMgrByTokenId(u std::shared_ptr DeviceManagerServiceImpl::GetCurrentAuthMgr() { uint64_t tokenId = 0; - if (logicalSessionId2TokenIdMap_.find(0) != logicalSessionId2TokenIdMap_.end()) { - tokenId = logicalSessionId2TokenIdMap_[0]; + { + std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); + if (logicalSessionId2TokenIdMap_.find(0) != logicalSessionId2TokenIdMap_.end()) { + tokenId = logicalSessionId2TokenIdMap_[0]; + } } - for (auto &pair : authMgrMap_) { - if (pair.first != tokenId) { - return pair.second; + { + std::lock_guard lock(authMgrMapMtx_); + for (auto &pair : authMgrMap_) { + if (pair.first != tokenId) { + return pair.second; + } } } return authMgr_; @@ -483,6 +533,40 @@ int32_t DeviceManagerServiceImpl::Initialize(const std::shared_ptr lock(authMgrMapMtx_); + for (auto& pair : authMgrMap_) { + pair.second = nullptr; + } + authMgrMap_.clear(); + } + for (auto& pair : sessionsMap_) { + pair.second = nullptr; + } + sessionsMap_.clear(); + { + std::lock_guard configsLock(configsMapMutex_); + for (auto& pair : configsMap_) { + pair.second = nullptr; + } + configsMap_.clear(); + } + deviceId2SessionIdMap_.clear(); + deviceIdMutexMap_.clear(); + sessionEnableMutexMap_.clear(); + sessionEnableCvMap_.clear(); + { + std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); + logicalSessionId2TokenIdMap_.clear(); + } + { + std::lock_guard sessionIdLock(logicalSessionId2SessionIdMapMtx_); + logicalSessionId2SessionIdMap_.clear(); + } +} + void DeviceManagerServiceImpl::Release() { LOGI("Release"); @@ -503,24 +587,7 @@ void DeviceManagerServiceImpl::Release() hiChainAuthConnector_->UnRegisterHiChainAuthCallback(); } authMgr_ = nullptr; - for (auto& pair : authMgrMap_) { - pair.second = nullptr; - } - authMgrMap_.clear(); - for (auto& pair : sessionsMap_) { - pair.second = nullptr; - } - sessionsMap_.clear(); - for (auto& pair : configsMap_) { - pair.second = nullptr; - } - configsMap_.clear(); - deviceId2SessionIdMap_.clear(); - deviceIdMutexMap_.clear(); - sessionEnableMutexMap_.clear(); - sessionEnableCvMap_.clear(); - logicalSessionId2TokenIdMap_.clear(); - logicalSessionId2SessionIdMap_.clear(); + ReleaseMaps(); deviceStateMgr_ = nullptr; softbusConnector_ = nullptr; abilityMgr_ = nullptr; @@ -886,6 +953,8 @@ std::shared_ptr DeviceManagerServiceImpl::GetAuthMgrByMessage(i return nullptr; } curSession->logicalSessionSet_.insert(logicalSessionId); + + std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); if (logicalSessionId2TokenIdMap_.find(logicalSessionId) != logicalSessionId2TokenIdMap_.end()) { LOGE("GetAuthMgrByMessage, logicalSessionId exists in logicalSessionId2TokenIdMap_."); return nullptr; @@ -896,29 +965,42 @@ std::shared_ptr DeviceManagerServiceImpl::GetAuthMgrByMessage(i LOGE("GetAuthMgrByMessage, The logical session ID does not exist in the physical session."); return nullptr; } + std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); tokenId = logicalSessionId2TokenIdMap_[logicalSessionId]; } return GetAuthMgrByTokenId(tokenId); } -int32_t DeviceManagerServiceImpl::TransferSrcOldAuthMgr(std::shared_ptr curSession) +int32_t DeviceManagerServiceImpl::GetLogicalIdAndTokenIdBySessionId(uint64_t &logicalSessionId, + uint64_t &tokenId, int32_t sessionId) { - // New Old Receive 90, destroy new authMgr, create old authMgr, source side - // The old protocol has only one session, reverse lookup logicalSessionId and tokenId - int sessionId = curSession->sessionId_; - uint64_t logicalSessionId = 0; - uint64_t tokenId = 0; + std::lock_guard sessionIdLock(logicalSessionId2SessionIdMapMtx_); for (auto& pair : logicalSessionId2SessionIdMap_) { if (pair.second == sessionId) { logicalSessionId = pair.first; + std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); tokenId = logicalSessionId2TokenIdMap_[logicalSessionId]; } } if (logicalSessionId == 0 || tokenId == 0) { - LOGE("DeviceManagerServiceImpl::TransferSrcOldAuthMgr can not find logicalSessionId and tokenId."); + LOGE("can not find logicalSessionId and tokenId."); return ERR_DM_AUTH_FAILED; } + return DM_OK; +} + +int32_t DeviceManagerServiceImpl::TransferSrcOldAuthMgr(std::shared_ptr curSession) +{ + // New Old Receive 90, destroy new authMgr, create old authMgr, source side + // The old protocol has only one session, reverse lookup logicalSessionId and tokenId + int sessionId = curSession->sessionId_; + uint64_t logicalSessionId = 0; + uint64_t tokenId = 0; + int32_t ret = GetLogicalIdAndTokenIdBySessionId(logicalSessionId, tokenId, sessionId); + if (ret != DM_OK) { + return ret; + } std::string pkgName; PeerTargetId peerTargetId; std::map bindParam; @@ -931,12 +1013,15 @@ int32_t DeviceManagerServiceImpl::TransferSrcOldAuthMgr(std::shared_ptr int32_t authType = -1; authMgr->ParseAuthType(bindParam, authType); ImportAuthCodeToConfig(authMgr, tokenId); - authMgrMap_.erase(tokenId); + { + std::lock_guard lock(authMgrMapMtx_); + authMgrMap_.erase(tokenId); + } if (InitAndRegisterAuthMgr(true, tokenId, curSession, logicalSessionId, "") != DM_OK) { return ERR_DM_AUTH_FAILED; } - int ret = TransferByAuthType(authType, curSession, authMgr, bindParam, logicalSessionId); + ret = TransferByAuthType(authType, curSession, authMgr, bindParam, logicalSessionId); if (ret != DM_OK) { LOGE("DeviceManagerServiceImpl::TransferByAuthType TransferByAuthType failed."); return ret; @@ -966,7 +1051,10 @@ int32_t DeviceManagerServiceImpl::TransferByAuthType(int32_t authType, if (authType == DmAuthType::AUTH_TYPE_IMPORT_AUTH_CODE) { authMgr_->EnableInsensibleSwitching(); curSession->logicalSessionSet_.insert(0); - logicalSessionId2SessionIdMap_[0] = sessionId; + { + std::lock_guard sessionIdLock(logicalSessionId2SessionIdMapMtx_); + logicalSessionId2SessionIdMap_[0] = sessionId; + } authMgr->OnSessionDisable(); } else { authMgr_->DisableInsensibleSwitching(); @@ -1368,6 +1456,7 @@ int32_t DeviceManagerServiceImpl::GetUdidHashByNetWorkId(const char *networkId, std::shared_ptr DeviceManagerServiceImpl::GetConfigByTokenId() { uint64_t tokenId = IPCSkeleton::GetCallingTokenID(); + std::lock_guard configsLock(configsMapMutex_); if (configsMap_.find(tokenId) == configsMap_.end()) { configsMap_[tokenId] = std::make_shared(); } @@ -1624,6 +1713,19 @@ int32_t DeviceManagerServiceImpl::ParseConnectAddr(const PeerTargetId &targetId, return DM_OK; } +void DeviceManagerServiceImpl::SaveTokenIdAndSessionId(uint64_t &tokenId, + int32_t &sessionId, uint64_t &logicalSessionId) +{ + { + std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); + logicalSessionId2TokenIdMap_[logicalSessionId] = tokenId; + } + { + std::lock_guard sessionIdLock(logicalSessionId2SessionIdMapMtx_); + logicalSessionId2SessionIdMap_[logicalSessionId] = sessionId; + } +} + void DeviceManagerServiceImpl::BindTargetImpl(uint64_t tokenId, const std::string &pkgName, const PeerTargetId &targetId, const std::map &bindParam) { @@ -1665,9 +1767,7 @@ void DeviceManagerServiceImpl::BindTargetImpl(uint64_t tokenId, const std::strin } curSession->logicalSessionSet_.insert(logicalSessionId); curSession->logicalSessionCnt_.fetch_add(1); - logicalSessionId2TokenIdMap_[logicalSessionId] = tokenId; - logicalSessionId2SessionIdMap_[logicalSessionId] = sessionId; - + SaveTokenIdAndSessionId(tokenId, sessionId, logicalSessionId); auto authMgr = GetAuthMgrByTokenId(tokenId); if (authMgr == nullptr) { CleanAuthMgrByLogicalSessionId(logicalSessionId); @@ -1694,14 +1794,15 @@ int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const P std::map bindParamTmp; SetBindCallerInfoToBindParam(bindParam, bindParamTmp, bindCallerInfo); uint64_t tokenId = IPCSkeleton::GetCallingTokenID(); - if (authMgrMap_.find(tokenId) == authMgrMap_.end()) { - std::thread newThread(&DeviceManagerServiceImpl::BindTargetImpl, this, tokenId, pkgName, - targetId, bindParamTmp); - newThread.detach(); - } else { - LOGE("BindTarget failed, this device is being bound. please try again later."); - return ERR_DM_AUTH_BUSINESS_BUSY; + { + std::lock_guard lock(authMgrMapMtx_); + if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { + LOGE("BindTarget failed, this device is being bound. please try again later."); + return ERR_DM_AUTH_BUSINESS_BUSY; + } } + std::thread newThread(&DeviceManagerServiceImpl::BindTargetImpl, this, tokenId, pkgName, targetId, bindParamTmp); + newThread.detach(); return DM_OK; } @@ -1843,6 +1944,7 @@ void DeviceManagerServiceImpl::ScreenCommonEventCallback(std::string commonEvent { if (commonEventType == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED) { LOGI("on screen locked."); + std::lock_guard lock(authMgrMapMtx_); for (auto& pair : authMgrMap_) { if (pair.second != nullptr) { LOGI("tokenId: %{public}" PRId64 ".", pair.first); -- Gitee From 50cfb7c9c94565d266c2b4a48c4388ddd00bdffb Mon Sep 17 00:00:00 2001 From: gaoqiang_strong Date: Mon, 2 Jun 2025 19:33:39 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=A4=9A=E4=BD=99?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gaoqiang_strong --- diff.txt | 546 ------------------------------------------------------- 1 file changed, 546 deletions(-) delete mode 100644 diff.txt diff --git a/diff.txt b/diff.txt deleted file mode 100644 index ce4475dd2..000000000 --- a/diff.txt +++ /dev/null @@ -1,546 +0,0 @@ -diff --git a/interfaces/kits/js/src/native_devicemanager_js.cpp b/interfaces/kits/js/src/native_devicemanager_js.cpp -index 2c619595fb5a930379105d7722054022e24c3b54..633b02b6b4baa9ea0b3dbf807bff94168efc9b96 100644 ---- a/interfaces/kits/js/src/native_devicemanager_js.cpp -+++ b/interfaces/kits/js/src/native_devicemanager_js.cpp -@@ -2084,6 +2084,8 @@ napi_value DeviceManagerNapi::GetTrustedDeviceList(napi_env env, napi_callback_i - return CallDeviceList(env, info, deviceInfoListAsyncCallbackInfo); - } else if (argc == DM_NAPI_ARGS_TWO) { - return GetTrustedDeviceListByFilter(env, info, deviceInfoListAsyncCallbackInfo); -+ } else { -+ DeleteAsyncCallbackInfo(deviceInfoListAsyncCallbackInfo); - } - napi_get_undefined(env, &result); - return result; -diff --git a/interfaces/kits/js4.0/src/native_devicemanager_js.cpp b/interfaces/kits/js4.0/src/native_devicemanager_js.cpp -index d9092f571bae242dfef82e0d4d9678b50c98277e..4117aacc36acd2a4f5aaa7faf08801cc4ad2e65e 100644 ---- a/interfaces/kits/js4.0/src/native_devicemanager_js.cpp -+++ b/interfaces/kits/js4.0/src/native_devicemanager_js.cpp -@@ -1333,6 +1333,8 @@ napi_value DeviceManagerNapi::GetAvailableDeviceList(napi_env env, napi_callback - return nullptr; - } - return CallDeviceList(env, info, deviceBasicInfoListAsyncCallbackInfo); -+ } else { -+ DeleteAsyncCallbackInfo(deviceBasicInfoListAsyncCallbackInfo); - } - napi_get_undefined(env, &result); - return result; -diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h -index c69db3f78e55b2e57e5b1dad1e32d3a59072d3ae..165247131ccd7a616f5ac2a6bfb41c2e68e8245a 100644 ---- a/services/implementation/include/device_manager_service_impl.h -+++ b/services/implementation/include/device_manager_service_impl.h -@@ -286,10 +286,15 @@ private: - void SetBindCallerInfoToBindParam(const std::map &bindParam, - std::map &bindParamTmp, const DmBindCallerInfo &bindCallerInfo); - std::string GetBundleLable(const std::string &bundleName); -+ int32_t GetLogicalIdAndTokenIdBySessionId(uint64_t &logicalSessionId, uint64_t &tokenId, int32_t sessionId); -+ void SaveTokenIdAndSessionId(uint64_t &tokenId, int32_t &sessionId, uint64_t &logicalSessionId); -+ void ReleaseMaps(); -+ int32_t InitNewProtocolAuthMgr(bool isSrcSide, uint64_t tokenId, uint64_t logicalSessionId, -+ const std::string &pkgName); -+ int32_t InitOldProtocolAuthMgr(uint64_t tokenId, const std::string &pkgName); - private: - std::shared_ptr authMgr_; // Old protocol only -- std::mutex authMgrMtx_; -- std::map> authMgrMap_; // New protocol sharing -+ - std::shared_ptr hiChainConnector_; - std::shared_ptr hiChainAuthConnector_; - std::shared_ptr deviceStateMgr_; -@@ -310,9 +315,14 @@ private: - std::map sessionEnableCvMap_; // Condition variable corresponding to the session - std::map sessionEnableMutexMap_; // Lock corresponding to the session - std::map sessionEnableCvReadyMap_; // Condition variable ready flag -+ std::mutex logicalSessionId2TokenIdMapMtx_; - std::map logicalSessionId2TokenIdMap_; // The relationship between logicalSessionId and tokenId -+ std::mutex logicalSessionId2SessionIdMapMtx_; - std::map logicalSessionId2SessionIdMap_; // The relationship logicalSessionId and physical sessionId -+ std::mutex configsMapMutex_; - std::map> configsMap_; // Import when authMgr is not initialized -+ std::mutex authMgrMapMtx_; -+ std::map> authMgrMap_; // New protocol sharing - - std::thread thread_; - std::atomic running_; -diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp -index 314cdf47b98790973bb9de2b2829fcf29c0e3f5e..8001c0a6ba64ebcd53d215c960730dd0fce7db67 100644 ---- a/services/implementation/src/device_manager_service_impl.cpp -+++ b/services/implementation/src/device_manager_service_impl.cpp -@@ -234,6 +234,7 @@ void DeviceManagerServiceImpl::ImportConfig(std::shared_ptr aut - const std::string &pkgName) - { - // Import configuration -+ std::lock_guard configsLock(configsMapMutex_); - if (configsMap_.find(tokenId) != configsMap_.end()) { - authMgr->ImportAuthCode(configsMap_[tokenId]->pkgName, configsMap_[tokenId]->authCode); - authMgr->RegisterAuthenticationType(configsMap_[tokenId]->authenticationType); -@@ -266,6 +267,7 @@ void DeviceManagerServiceImpl::ImportAuthCodeToConfig(std::shared_ptrGetAuthCodeAndPkgName(pkgName, authCode); -+ std::lock_guard configsLock(configsMapMutex_); - if (configsMap_.find(tokenId) == configsMap_.end()) { - configsMap_[tokenId] = std::make_shared(); - } -@@ -273,6 +275,51 @@ void DeviceManagerServiceImpl::ImportAuthCodeToConfig(std::shared_ptrauthCode = authCode; - } - -+int32_t DeviceManagerServiceImpl::InitNewProtocolAuthMgr(bool isSrcSide, uint64_t tokenId, uint64_t logicalSessionId, -+ const std::string &pkgName) -+{ -+ std::shared_ptr authMgr = nullptr; -+ // Create a new auth_mgr, create authMgr -+ if (isSrcSide) { -+ // src end -+ authMgr = std::make_shared(softbusConnector_, hiChainConnector_, -+ listener_, hiChainAuthConnector_); -+ } else { -+ // sink end -+ authMgr = std::make_shared(softbusConnector_, hiChainConnector_, -+ listener_, hiChainAuthConnector_); -+ } -+ // Register resource destruction notification function -+ authMgr->RegisterCleanNotifyCallback(&DeviceManagerServiceImpl::NotifyCleanEvent); -+ hiChainAuthConnector_->RegisterHiChainAuthCallbackById(logicalSessionId, authMgr); -+ LOGI("Initialize authMgr token: %{public}" PRId64 ".", tokenId); -+ ImportConfig(authMgr, tokenId, pkgName); -+ { -+ std::lock_guard lock(authMgrMapMtx_); -+ authMgrMap_[tokenId] = authMgr; -+ } -+ return DM_OK; -+} -+ -+int32_t DeviceManagerServiceImpl::InitOldProtocolAuthMgr(uint64_t tokenId, const std::string &pkgName) -+{ -+ if (authMgr_ == nullptr) { -+ CreateGlobalClassicalAuthMgr(); -+ } -+ authMgr_->PrepareSoftbusSessionCallback(); -+ { -+ std::lock_guard lock(authMgrMapMtx_); -+ authMgrMap_[tokenId] = authMgr_; -+ } -+ ImportConfig(authMgr_, tokenId, pkgName); -+ { -+ // The value of logicalSessionId in the old protocol is always 0. -+ std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); -+ logicalSessionId2TokenIdMap_[0] = tokenId; -+ } -+ return DM_OK; -+} -+ - int32_t DeviceManagerServiceImpl::InitAndRegisterAuthMgr(bool isSrcSide, uint64_t tokenId, - std::shared_ptr session, uint64_t logicalSessionId, const std::string &pkgName) - { -@@ -292,47 +339,24 @@ int32_t DeviceManagerServiceImpl::InitAndRegisterAuthMgr(bool isSrcSide, uint64_ - return ERR_DM_AUTH_BUSINESS_BUSY; - } - } -- -- std::lock_guard lock(authMgrMtx_); -- if (authMgrMap_.find(tokenId) == authMgrMap_.end()) { -- if (session->version_ == "" || CompareVersion(session->version_, DM_VERSION_5_0_OLD_MAX)) { -- if (authMgrMap_.size() > MAX_NEW_PROC_SESSION_COUNT_TEMP) { -- LOGE("Other bind session exist, can not start new one."); -- return ERR_DM_AUTH_BUSINESS_BUSY; -- } -- // Create a new auth_mgr, create authMgrMap_[tokenId] -- if (isSrcSide) { -- // src end -- authMgrMap_[tokenId] = std::make_shared(softbusConnector_, hiChainConnector_, -- listener_, hiChainAuthConnector_); -- } else { -- // sink end -- authMgrMap_[tokenId] = std::make_shared(softbusConnector_, hiChainConnector_, -- listener_, hiChainAuthConnector_); -- } -- // Register resource destruction notification function -- authMgrMap_[tokenId]->RegisterCleanNotifyCallback(&DeviceManagerServiceImpl::NotifyCleanEvent); -- hiChainAuthConnector_->RegisterHiChainAuthCallbackById(logicalSessionId, authMgrMap_[tokenId]); -- LOGI("Initialize authMgrMap_ token: %{public}" PRId64 ".", tokenId); -- ImportConfig(authMgrMap_[tokenId], tokenId, pkgName); -- return DM_OK; -- } else { -- LOGI("InitAndRegisterAuthMgr old authMgr."); -- if (authMgr_ == nullptr) { -- CreateGlobalClassicalAuthMgr(); -- } -- authMgr_->PrepareSoftbusSessionCallback(); -- authMgrMap_[tokenId] = authMgr_; -- ImportConfig(authMgr_, tokenId, pkgName); -- // The value of logicalSessionId in the old protocol is always 0. -- logicalSessionId2TokenIdMap_[0] = tokenId; -- return DM_OK; -+ { -+ std::lock_guard lock(authMgrMapMtx_); -+ if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { -+ // authMgr_ has been created, indicating that a binding event already exists. -+ // Other requests are rejected, and an error code is returned. -+ LOGE("BindTarget failed, this device is being bound. Please try again later."); -+ return ERR_DM_AUTH_BUSINESS_BUSY; -+ } -+ if (authMgrMap_.size() > MAX_NEW_PROC_SESSION_COUNT_TEMP) { -+ LOGE("Other bind session exist, can not start new one."); -+ return ERR_DM_AUTH_BUSINESS_BUSY; - } - } -- // authMgr_ has been created, indicating that a binding event already exists. -- // Other requests are rejected, and an error code is returned. -- LOGE("BindTarget failed, this device is being bound. Please try again later."); -- return ERR_DM_AUTH_BUSINESS_BUSY; -+ if (session->version_ == "" || CompareVersion(session->version_, DM_VERSION_5_0_OLD_MAX)) { -+ return InitNewProtocolAuthMgr(isSrcSide, tokenId, logicalSessionId, pkgName); -+ } -+ LOGI("InitAndRegisterAuthMgr old authMgr."); -+ return InitOldProtocolAuthMgr(tokenId, pkgName); - } - - void DeviceManagerServiceImpl::CleanSessionMap(int sessionId, std::shared_ptr session) -@@ -356,26 +380,37 @@ void DeviceManagerServiceImpl::CleanSessionMap(int sessionId, std::shared_ptr tokenIdLock(logicalSessionId2TokenIdMapMtx_); -+ logicalSessionId2TokenIdMap_.erase(logicalSessionId); -+ } -+ int32_t sessionId = 0; -+ { -+ std::lock_guard sessionIdLock(logicalSessionId2SessionIdMapMtx_); -+ if (logicalSessionId2SessionIdMap_.find(logicalSessionId) == logicalSessionId2SessionIdMap_.end()) { -+ return; - } -+ sessionId = logicalSessionId2SessionIdMap_[logicalSessionId]; - logicalSessionId2SessionIdMap_.erase(logicalSessionId); - } -- logicalSessionId2TokenIdMap_.erase(logicalSessionId); -+ auto session = GetCurSession(sessionId); -+ if (session != nullptr) { -+ CleanSessionMap(sessionId, session); -+ } - return; - } - - void DeviceManagerServiceImpl::CleanAuthMgrByLogicalSessionId(uint64_t logicalSessionId) - { - uint64_t tokenId = 0; -- if (logicalSessionId2TokenIdMap_.find(logicalSessionId) != logicalSessionId2TokenIdMap_.end()) { -- tokenId = logicalSessionId2TokenIdMap_[logicalSessionId]; -- } else { -- LOGE("logicalSessionId(%{public}" PRIu64 ") can not find the tokenId.", logicalSessionId); -- return; -+ { -+ std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); -+ if (logicalSessionId2TokenIdMap_.find(logicalSessionId) != logicalSessionId2TokenIdMap_.end()) { -+ tokenId = logicalSessionId2TokenIdMap_[logicalSessionId]; -+ } else { -+ LOGE("logicalSessionId(%{public}" PRIu64 ") can not find the tokenId.", logicalSessionId); -+ return; -+ } - } - - CleanSessionMapByLogicalSessionId(logicalSessionId); -@@ -384,9 +419,12 @@ void DeviceManagerServiceImpl::CleanAuthMgrByLogicalSessionId(uint64_t logicalSe - authMgr_->ClearSoftbusSessionCallback(); - } - hiChainAuthConnector_->UnRegisterHiChainAuthCallbackById(logicalSessionId); -- if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { -- authMgrMap_[tokenId] = nullptr; -- authMgrMap_.erase(tokenId); -+ { -+ std::lock_guard lock(authMgrMapMtx_); -+ if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { -+ authMgrMap_[tokenId] = nullptr; -+ authMgrMap_.erase(tokenId); -+ } - } - return; - } -@@ -394,9 +432,12 @@ void DeviceManagerServiceImpl::CleanAuthMgrByLogicalSessionId(uint64_t logicalSe - std::shared_ptr DeviceManagerServiceImpl::GetAuthMgr() - { - uint64_t tokenId = IPCSkeleton::GetCallingTokenID(); -- if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { -- LOGI("authMgrMap_ token: %{public}" PRId64 ".", tokenId); -- return authMgrMap_[tokenId]; -+ { -+ std::lock_guard lock(authMgrMapMtx_); -+ if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { -+ LOGI("authMgrMap_ token: %{public}" PRId64 ".", tokenId); -+ return authMgrMap_[tokenId]; -+ } - } - LOGE("authMgrMap_ not found, token: %{public}" PRId64 ".", tokenId); - return nullptr; -@@ -405,9 +446,12 @@ std::shared_ptr DeviceManagerServiceImpl::GetAuthMgr() - // Needed in the callback function - std::shared_ptr DeviceManagerServiceImpl::GetAuthMgrByTokenId(uint64_t tokenId) - { -- if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { -- LOGI("authMgrMap_ token: %{public}" PRId64 ".", tokenId); -- return authMgrMap_[tokenId]; -+ { -+ std::lock_guard lock(authMgrMapMtx_); -+ if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { -+ LOGI("authMgrMap_ token: %{public}" PRId64 ".", tokenId); -+ return authMgrMap_[tokenId]; -+ } - } - LOGE("authMgrMap_ not found, token: %{public}" PRId64 ".", tokenId); - return nullptr; -@@ -416,12 +460,18 @@ std::shared_ptr DeviceManagerServiceImpl::GetAuthMgrByTokenId(u - std::shared_ptr DeviceManagerServiceImpl::GetCurrentAuthMgr() - { - uint64_t tokenId = 0; -- if (logicalSessionId2TokenIdMap_.find(0) != logicalSessionId2TokenIdMap_.end()) { -- tokenId = logicalSessionId2TokenIdMap_[0]; -+ { -+ std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); -+ if (logicalSessionId2TokenIdMap_.find(0) != logicalSessionId2TokenIdMap_.end()) { -+ tokenId = logicalSessionId2TokenIdMap_[0]; -+ } - } -- for (auto &pair : authMgrMap_) { -- if (pair.first != tokenId) { -- return pair.second; -+ { -+ std::lock_guard lock(authMgrMapMtx_); -+ for (auto &pair : authMgrMap_) { -+ if (pair.first != tokenId) { -+ return pair.second; -+ } - } - } - return authMgr_; -@@ -483,6 +533,40 @@ int32_t DeviceManagerServiceImpl::Initialize(const std::shared_ptr lock(authMgrMapMtx_); -+ for (auto& pair : authMgrMap_) { -+ pair.second = nullptr; -+ } -+ authMgrMap_.clear(); -+ } -+ for (auto& pair : sessionsMap_) { -+ pair.second = nullptr; -+ } -+ sessionsMap_.clear(); -+ { -+ std::lock_guard configsLock(configsMapMutex_); -+ for (auto& pair : configsMap_) { -+ pair.second = nullptr; -+ } -+ configsMap_.clear(); -+ } -+ deviceId2SessionIdMap_.clear(); -+ deviceIdMutexMap_.clear(); -+ sessionEnableMutexMap_.clear(); -+ sessionEnableCvMap_.clear(); -+ { -+ std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); -+ logicalSessionId2TokenIdMap_.clear(); -+ } -+ { -+ std::lock_guard sessionIdLock(logicalSessionId2SessionIdMapMtx_); -+ logicalSessionId2SessionIdMap_.clear(); -+ } -+} -+ - void DeviceManagerServiceImpl::Release() - { - LOGI("Release"); -@@ -503,24 +587,7 @@ void DeviceManagerServiceImpl::Release() - hiChainAuthConnector_->UnRegisterHiChainAuthCallback(); - } - authMgr_ = nullptr; -- for (auto& pair : authMgrMap_) { -- pair.second = nullptr; -- } -- authMgrMap_.clear(); -- for (auto& pair : sessionsMap_) { -- pair.second = nullptr; -- } -- sessionsMap_.clear(); -- for (auto& pair : configsMap_) { -- pair.second = nullptr; -- } -- configsMap_.clear(); -- deviceId2SessionIdMap_.clear(); -- deviceIdMutexMap_.clear(); -- sessionEnableMutexMap_.clear(); -- sessionEnableCvMap_.clear(); -- logicalSessionId2TokenIdMap_.clear(); -- logicalSessionId2SessionIdMap_.clear(); -+ ReleaseMaps(); - deviceStateMgr_ = nullptr; - softbusConnector_ = nullptr; - abilityMgr_ = nullptr; -@@ -886,6 +953,8 @@ std::shared_ptr DeviceManagerServiceImpl::GetAuthMgrByMessage(i - return nullptr; - } - curSession->logicalSessionSet_.insert(logicalSessionId); -+ -+ std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); - if (logicalSessionId2TokenIdMap_.find(logicalSessionId) != logicalSessionId2TokenIdMap_.end()) { - LOGE("GetAuthMgrByMessage, logicalSessionId exists in logicalSessionId2TokenIdMap_."); - return nullptr; -@@ -896,29 +965,42 @@ std::shared_ptr DeviceManagerServiceImpl::GetAuthMgrByMessage(i - LOGE("GetAuthMgrByMessage, The logical session ID does not exist in the physical session."); - return nullptr; - } -+ std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); - tokenId = logicalSessionId2TokenIdMap_[logicalSessionId]; - } - - return GetAuthMgrByTokenId(tokenId); - } - --int32_t DeviceManagerServiceImpl::TransferSrcOldAuthMgr(std::shared_ptr curSession) -+int32_t DeviceManagerServiceImpl::GetLogicalIdAndTokenIdBySessionId(uint64_t &logicalSessionId, -+ uint64_t &tokenId, int32_t sessionId) - { -- // New Old Receive 90, destroy new authMgr, create old authMgr, source side -- // The old protocol has only one session, reverse lookup logicalSessionId and tokenId -- int sessionId = curSession->sessionId_; -- uint64_t logicalSessionId = 0; -- uint64_t tokenId = 0; -+ std::lock_guard sessionIdLock(logicalSessionId2SessionIdMapMtx_); - for (auto& pair : logicalSessionId2SessionIdMap_) { - if (pair.second == sessionId) { - logicalSessionId = pair.first; -+ std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); - tokenId = logicalSessionId2TokenIdMap_[logicalSessionId]; - } - } - if (logicalSessionId == 0 || tokenId == 0) { -- LOGE("DeviceManagerServiceImpl::TransferSrcOldAuthMgr can not find logicalSessionId and tokenId."); -+ LOGE("can not find logicalSessionId and tokenId."); - return ERR_DM_AUTH_FAILED; - } -+ return DM_OK; -+} -+ -+int32_t DeviceManagerServiceImpl::TransferSrcOldAuthMgr(std::shared_ptr curSession) -+{ -+ // New Old Receive 90, destroy new authMgr, create old authMgr, source side -+ // The old protocol has only one session, reverse lookup logicalSessionId and tokenId -+ int sessionId = curSession->sessionId_; -+ uint64_t logicalSessionId = 0; -+ uint64_t tokenId = 0; -+ int32_t ret = GetLogicalIdAndTokenIdBySessionId(logicalSessionId, tokenId, sessionId); -+ if (ret != DM_OK) { -+ return ret; -+ } - std::string pkgName; - PeerTargetId peerTargetId; - std::map bindParam; -@@ -931,12 +1013,15 @@ int32_t DeviceManagerServiceImpl::TransferSrcOldAuthMgr(std::shared_ptr - int32_t authType = -1; - authMgr->ParseAuthType(bindParam, authType); - ImportAuthCodeToConfig(authMgr, tokenId); -- authMgrMap_.erase(tokenId); -+ { -+ std::lock_guard lock(authMgrMapMtx_); -+ authMgrMap_.erase(tokenId); -+ } - if (InitAndRegisterAuthMgr(true, tokenId, curSession, logicalSessionId, "") != DM_OK) { - return ERR_DM_AUTH_FAILED; - } - -- int ret = TransferByAuthType(authType, curSession, authMgr, bindParam, logicalSessionId); -+ ret = TransferByAuthType(authType, curSession, authMgr, bindParam, logicalSessionId); - if (ret != DM_OK) { - LOGE("DeviceManagerServiceImpl::TransferByAuthType TransferByAuthType failed."); - return ret; -@@ -966,7 +1051,10 @@ int32_t DeviceManagerServiceImpl::TransferByAuthType(int32_t authType, - if (authType == DmAuthType::AUTH_TYPE_IMPORT_AUTH_CODE) { - authMgr_->EnableInsensibleSwitching(); - curSession->logicalSessionSet_.insert(0); -- logicalSessionId2SessionIdMap_[0] = sessionId; -+ { -+ std::lock_guard sessionIdLock(logicalSessionId2SessionIdMapMtx_); -+ logicalSessionId2SessionIdMap_[0] = sessionId; -+ } - authMgr->OnSessionDisable(); - } else { - authMgr_->DisableInsensibleSwitching(); -@@ -1368,6 +1456,7 @@ int32_t DeviceManagerServiceImpl::GetUdidHashByNetWorkId(const char *networkId, - std::shared_ptr DeviceManagerServiceImpl::GetConfigByTokenId() - { - uint64_t tokenId = IPCSkeleton::GetCallingTokenID(); -+ std::lock_guard configsLock(configsMapMutex_); - if (configsMap_.find(tokenId) == configsMap_.end()) { - configsMap_[tokenId] = std::make_shared(); - } -@@ -1624,6 +1713,19 @@ int32_t DeviceManagerServiceImpl::ParseConnectAddr(const PeerTargetId &targetId, - return DM_OK; - } - -+void DeviceManagerServiceImpl::SaveTokenIdAndSessionId(uint64_t &tokenId, -+ int32_t &sessionId, uint64_t &logicalSessionId) -+{ -+ { -+ std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); -+ logicalSessionId2TokenIdMap_[logicalSessionId] = tokenId; -+ } -+ { -+ std::lock_guard sessionIdLock(logicalSessionId2SessionIdMapMtx_); -+ logicalSessionId2SessionIdMap_[logicalSessionId] = sessionId; -+ } -+} -+ - void DeviceManagerServiceImpl::BindTargetImpl(uint64_t tokenId, const std::string &pkgName, - const PeerTargetId &targetId, const std::map &bindParam) - { -@@ -1665,9 +1767,7 @@ void DeviceManagerServiceImpl::BindTargetImpl(uint64_t tokenId, const std::strin - } - curSession->logicalSessionSet_.insert(logicalSessionId); - curSession->logicalSessionCnt_.fetch_add(1); -- logicalSessionId2TokenIdMap_[logicalSessionId] = tokenId; -- logicalSessionId2SessionIdMap_[logicalSessionId] = sessionId; -- -+ SaveTokenIdAndSessionId(tokenId, sessionId, logicalSessionId); - auto authMgr = GetAuthMgrByTokenId(tokenId); - if (authMgr == nullptr) { - CleanAuthMgrByLogicalSessionId(logicalSessionId); -@@ -1694,14 +1794,15 @@ int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const P - std::map bindParamTmp; - SetBindCallerInfoToBindParam(bindParam, bindParamTmp, bindCallerInfo); - uint64_t tokenId = IPCSkeleton::GetCallingTokenID(); -- if (authMgrMap_.find(tokenId) == authMgrMap_.end()) { -- std::thread newThread(&DeviceManagerServiceImpl::BindTargetImpl, this, tokenId, pkgName, -- targetId, bindParamTmp); -- newThread.detach(); -- } else { -- LOGE("BindTarget failed, this device is being bound. please try again later."); -- return ERR_DM_AUTH_BUSINESS_BUSY; -+ { -+ std::lock_guard lock(authMgrMapMtx_); -+ if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { -+ LOGE("BindTarget failed, this device is being bound. please try again later."); -+ return ERR_DM_AUTH_BUSINESS_BUSY; -+ } - } -+ std::thread newThread(&DeviceManagerServiceImpl::BindTargetImpl, this, tokenId, pkgName, targetId, bindParamTmp); -+ newThread.detach(); - return DM_OK; - } - -@@ -1843,6 +1944,7 @@ void DeviceManagerServiceImpl::ScreenCommonEventCallback(std::string commonEvent - { - if (commonEventType == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED) { - LOGI("on screen locked."); -+ std::lock_guard lock(authMgrMapMtx_); - for (auto& pair : authMgrMap_) { - if (pair.second != nullptr) { - LOGI("tokenId: %{public}" PRId64 ".", pair.first); \ No newline at end of file -- Gitee From 7ae92d370ddfbadaa12b930513afc146d8fa0b28 Mon Sep 17 00:00:00 2001 From: gaoqiang_strong Date: Tue, 3 Jun 2025 11:58:07 +0800 Subject: [PATCH 3/4] add ut Signed-off-by: gaoqiang_strong --- .../UTTest_device_manager_service_impl.cpp | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/test/unittest/UTTest_device_manager_service_impl.cpp b/test/unittest/UTTest_device_manager_service_impl.cpp index fae4ecbfe..c2c5a453e 100644 --- a/test/unittest/UTTest_device_manager_service_impl.cpp +++ b/test/unittest/UTTest_device_manager_service_impl.cpp @@ -2073,6 +2073,64 @@ HWTEST_F(DeviceManagerServiceImplTest, ChangeUltrasonicTypeToPin_002, testing::e int32_t ret = deviceManagerServiceImpl_->ChangeUltrasonicTypeToPin(bindParam); EXPECT_EQ(ret, DM_OK); } + +HWTEST_F(DeviceManagerServiceImplTest, ReleaseMaps_001, testing::ext::TestSize.Level1) +{ + int32_t sessionId = 100012; + uint64_t logicalSessionId = 456789102; + deviceManagerServiceImpl_->logicalSessionId2SessionIdMap_[logicalSessionId] = sessionId; + deviceManagerServiceImpl_->ReleaseMaps(); + EXPECT_EQ(deviceManagerServiceImpl_->logicalSessionId2SessionIdMap_.size(), 0); +} + +HWTEST_F(DeviceManagerServiceImplTest, GetLogicalIdAndTokenIdBySessionId_001, testing::ext::TestSize.Level1) +{ + int32_t sessionId = 10001; + uint64_t tokenId = 1000023; + uint64_t logicalSessionId = 45678910; + int32_t ret = deviceManagerServiceImpl_->GetLogicalIdAndTokenIdBySessionId(logicalSessionId, tokenId, sessionId); + EXPECT_EQ(ret, DM_OK); + + logicalSessionId = 0; + ret = deviceManagerServiceImpl_->GetLogicalIdAndTokenIdBySessionId(logicalSessionId, tokenId, sessionId); + EXPECT_EQ(ret, ERR_DM_AUTH_FAILED); + + logicalSessionId = 45678910; + tokenId = 0; + ret = deviceManagerServiceImpl_->GetLogicalIdAndTokenIdBySessionId(logicalSessionId, tokenId, sessionId); + EXPECT_EQ(ret, ERR_DM_AUTH_FAILED); + + deviceManagerServiceImpl_->logicalSessionId2SessionIdMap_[logicalSessionId] = sessionId; + deviceManagerServiceImpl_->logicalSessionId2TokenIdMap_[logicalSessionId] = tokenId; + ret = deviceManagerServiceImpl_->GetLogicalIdAndTokenIdBySessionId(logicalSessionId, tokenId, sessionId); + EXPECT_EQ(ret, ERR_DM_AUTH_FAILED); +} + +HWTEST_F(DeviceManagerServiceImplTest, InitNewProtocolAuthMgr_001, testing::ext::TestSize.Level1) +{ + bool isSrcSide = true; + uint64_t tokenId = 1000023; + uint64_t logicalSessionId = 456789; + const std::string pkgName = "InitNewProtocolAuthMgr"; + int32_t ret = deviceManagerServiceImpl_->InitNewProtocolAuthMgr(isSrcSide, tokenId, logicalSessionId, pkgName); + EXPECT_EQ(ret, DM_OK); + + isSrcSide = false; + ret = deviceManagerServiceImpl_->InitNewProtocolAuthMgr(isSrcSide, tokenId, logicalSessionId, pkgName); + EXPECT_EQ(ret, DM_OK); +} + +HWTEST_F(DeviceManagerServiceImplTest, InitOldProtocolAuthMgr_001, testing::ext::TestSize.Level1) +{ + uint64_t tokenId = 100002311; + const std::string pkgName = "InitOldProtocolAuthMgr"; + int32_t ret = deviceManagerServiceImpl_->InitOldProtocolAuthMgr(tokenId, pkgName); + EXPECT_EQ(ret, DM_OK); + + deviceManagerServiceImpl_->authMgr_ = nullptr; + ret = deviceManagerServiceImpl_->InitOldProtocolAuthMgr(tokenId, pkgName); + EXPECT_EQ(ret, DM_OK); +} } // namespace } // namespace DistributedHardware } // namespace OHOS -- Gitee From 4e0217ce067e1d7d6776b2ee9e2fffbd2caf5795 Mon Sep 17 00:00:00 2001 From: gaoqiang_strong Date: Wed, 4 Jun 2025 15:34:16 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A3=80=E8=A7=86?= =?UTF-8?q?=E6=84=8F=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gaoqiang_strong --- .../src/device_manager_service_impl.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index d22424a2e..490ba5368 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -278,6 +278,11 @@ void DeviceManagerServiceImpl::ImportAuthCodeToConfig(std::shared_ptr authMgr = nullptr; // Create a new auth_mgr, create authMgr if (isSrcSide) { @@ -291,6 +296,7 @@ int32_t DeviceManagerServiceImpl::InitNewProtocolAuthMgr(bool isSrcSide, uint64_ } // Register resource destruction notification function authMgr->RegisterCleanNotifyCallback(&DeviceManagerServiceImpl::NotifyCleanEvent); + CHECK_NULL_RETURN(hiChainAuthConnector_, ERR_DM_POINT_NULL); hiChainAuthConnector_->RegisterHiChainAuthCallbackById(logicalSessionId, authMgr); LOGI("Initialize authMgr token: %{public}" PRId64 ".", tokenId); ImportConfig(authMgr, tokenId, pkgName); @@ -303,6 +309,11 @@ int32_t DeviceManagerServiceImpl::InitNewProtocolAuthMgr(bool isSrcSide, uint64_ int32_t DeviceManagerServiceImpl::InitOldProtocolAuthMgr(uint64_t tokenId, const std::string &pkgName) { + LOGI("in"); + if (pkgName.empty()) { + LOGE("pkgName is empty"); + return ERR_DM_INPUT_PARA_INVALID; + } if (authMgr_ == nullptr) { CreateGlobalClassicalAuthMgr(); } @@ -994,11 +1005,16 @@ int32_t DeviceManagerServiceImpl::TransferSrcOldAuthMgr(std::shared_ptr { // New Old Receive 90, destroy new authMgr, create old authMgr, source side // The old protocol has only one session, reverse lookup logicalSessionId and tokenId + if (curSession == nullptr) { + LOGE("curSession is nullptr"); + return ERR_DM_POINT_NULL; + } int sessionId = curSession->sessionId_; uint64_t logicalSessionId = 0; uint64_t tokenId = 0; int32_t ret = GetLogicalIdAndTokenIdBySessionId(logicalSessionId, tokenId, sessionId); if (ret != DM_OK) { + LOGE("failed, logicalSessionId: %{public}" PRIu64 ", tokenId: %{public}" PRIu64 "", logicalSessionId, tokenId); return ret; } std::string pkgName; -- Gitee