From 55b0aad4237a21ae51790cdab7b6ad597d46720c Mon Sep 17 00:00:00 2001 From: guoyi Date: Wed, 13 Aug 2025 18:37:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96bind?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: guoyi --- .../include/device_manager_service_impl.h | 24 +- .../src/device_manager_service_impl.cpp | 308 +++++++++++------- .../UTTest_device_manager_service_impl.cpp | 78 ++++- 3 files changed, 276 insertions(+), 134 deletions(-) diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index 75b46dbad..1625b3f56 100644 --- a/services/implementation/include/device_manager_service_impl.h +++ b/services/implementation/include/device_manager_service_impl.h @@ -227,12 +227,14 @@ private: void HandleRemoteUserRemoved(int32_t preUserId, const std::string &remoteUdid); DmAuthForm ConvertBindTypeToAuthForm(int32_t bindType); std::shared_ptr GetAuthMgr(); + void AddAuthMgr(uint64_t tokenId, int sessionId, std::shared_ptr authMgr); + void EraseAuthMgr(uint64_t tokenId); std::shared_ptr GetAuthMgrByTokenId(uint64_t tokenId); std::shared_ptr GetCurrentAuthMgr(); void CreateGlobalClassicalAuthMgr(); std::shared_ptr GetCurSession(int sessionId); std::shared_ptr GetOrCreateSession(const std::string& deviceId, - const std::map &bindParam); + const std::map &bindParam, uint64_t tokenId); int32_t ParseConnectAddr(const PeerTargetId &targetId, std::string &deviceId, const std::map &bindParam); std::shared_ptr GetConfigByTokenId(); @@ -256,7 +258,8 @@ private: void ImportAuthCodeToConfig(std::shared_ptr authMgr, uint64_t tokenId); void CleanAuthMgrByLogicalSessionId(uint64_t logicalSessionId); - void CleanSessionMap(int sessionId, std::shared_ptr session); + void CleanSessionMap(std::shared_ptr session); + void CleanSessionMap(int sessionId); void CleanSessionMapByLogicalSessionId(uint64_t logicalSessionId); int32_t DeleteAclForProcV2(const std::string &localUdid, uint32_t localTokenId, const std::string &remoteUdid, int32_t bindLevel, const std::string &extra, int32_t userId); @@ -291,13 +294,13 @@ private: 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); - bool ParseConnectAddrAndSetProcessInfo(const PeerTargetId &targetId, - PeerTargetId &targetIdTmp, const std::map &bindParam, ProcessInfo &processInfo, - const std::string &pkgName); + const std::string &pkgName, int sessionId); + int32_t InitOldProtocolAuthMgr(uint64_t tokenId, const std::string &pkgName, int sessionId); + bool ParseConnectAddrAndSetProcessInfo(PeerTargetId &targetIdTmp, + const std::map &bindParam, ProcessInfo &processInfo, + const std::string &pkgName, uint64_t tokenId); void OnAuthResultAndOnBindResult(const ProcessInfo &processInfo, const PeerTargetId &targetId, - const std::string &deviceId, int32_t reason); + const std::string &deviceId, int32_t reason, uint64_t tokenId); void GetBundleName(const DMAclQuadInfo &info, std::set &pkgNameSet); void DeleteSessionKey(int32_t userId, const DistributedDeviceProfile::AccessControlProfile &profile); private: @@ -320,11 +323,12 @@ private: // The session ID corresponding to the device ID, used only on the src side std::map deviceId2SessionIdMap_; std::map> sessionsMap_; // sessionId corresponds to the session object - std::map deviceIdMutexMap_; // Lock corresponding to the device ID std::mutex mapMutex_; // sessionsMap_ lock 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::map sessionStopMap_; // stop flag + std::map sessionEnableMap_; // enable flag std::mutex logicalSessionId2TokenIdMapMtx_; std::map logicalSessionId2TokenIdMap_; // The relationship between logicalSessionId and tokenId std::mutex logicalSessionId2SessionIdMapMtx_; @@ -333,6 +337,8 @@ private: std::map> configsMap_; // Import when authMgr is not initialized std::mutex authMgrMapMtx_; std::map> authMgrMap_; // New protocol sharing + std::mutex tokenIdSessionIdMapMtx_; + std::map tokenIdSessionIdMap_; // New protocol sharing }; using CreateDMServiceFuncPtr = IDeviceManagerServiceImpl *(*)(void); diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index ec8fc1518..27d4803a6 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -74,6 +74,8 @@ constexpr const char* NO_NEED_JOIN_LNN = "1"; // currently, we just support one bind session in one device at same time constexpr size_t MAX_NEW_PROC_SESSION_COUNT_TEMP = 1; const int32_t USLEEP_TIME_US_500000 = 500000; // 500ms +const int32_t USLEEP_TIME_US_550000 = 550000; // 550ms +const int32_t USLEEP_TIME_US_20000 = 20000; // 20ms const int32_t OPEN_AUTH_SESSION_TIMEOUT = 15000; // 15000ms const std::map BUNDLENAME_MAPPING = { @@ -160,7 +162,7 @@ static uint64_t GetTokenId(bool isSrcSide, int32_t displayId, std::string &bundl } else { // sink end int64_t tmpTokenId; - // get userId + // get userId int32_t targetUserId = AuthManagerBase::DmGetUserId(displayId); if (targetUserId == -1) { return tokenId; @@ -234,7 +236,7 @@ void DeviceManagerServiceImpl::ImportAuthCodeToConfig(std::shared_ptrRegisterHiChainAuthCallbackById(logicalSessionId, authMgr); LOGI("Initialize authMgr token: %{public}" PRId64 ".", tokenId); ImportConfig(authMgr, tokenId, pkgName); - { - std::lock_guard lock(authMgrMapMtx_); - authMgrMap_[tokenId] = authMgr; - } + AddAuthMgr(tokenId, sessionId, authMgr); return DM_OK; } -int32_t DeviceManagerServiceImpl::InitOldProtocolAuthMgr(uint64_t tokenId, const std::string &pkgName) +int32_t DeviceManagerServiceImpl::InitOldProtocolAuthMgr(uint64_t tokenId, const std::string &pkgName, int sessionId) { LOGI("tokenId: %{public}" PRIu64 ", pkgname:%{public}s", tokenId, pkgName.c_str()); if (authMgr_ == nullptr) { CreateGlobalClassicalAuthMgr(); } authMgr_->PrepareSoftbusSessionCallback(); - { - std::lock_guard lock(authMgrMapMtx_); - authMgrMap_[tokenId] = authMgr_; - } ImportConfig(authMgr_, tokenId, pkgName); + AddAuthMgr(tokenId, sessionId, authMgr_); { // The value of logicalSessionId in the old protocol is always 0. std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); @@ -304,30 +300,20 @@ int32_t DeviceManagerServiceImpl::InitAndRegisterAuthMgr(bool isSrcSide, uint64_ return ERR_DM_AUTH_BUSINESS_BUSY; } } - { - 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; - } - } if (session->version_ == "" || CompareVersion(session->version_, DM_VERSION_5_0_OLD_MAX)) { - return InitNewProtocolAuthMgr(isSrcSide, tokenId, logicalSessionId, pkgName); + return InitNewProtocolAuthMgr(isSrcSide, tokenId, logicalSessionId, pkgName, session->sessionId_); } LOGI("InitAndRegisterAuthMgr old authMgr."); - return InitOldProtocolAuthMgr(tokenId, pkgName); + return InitOldProtocolAuthMgr(tokenId, pkgName, session->sessionId_); } -void DeviceManagerServiceImpl::CleanSessionMap(int sessionId, std::shared_ptr session) +void DeviceManagerServiceImpl::CleanSessionMap(std::shared_ptr session) { + if (session == nullptr) { + return; + } session->logicalSessionCnt_.fetch_sub(1); - if (session->logicalSessionCnt_.load(std::memory_order_relaxed) == 0) { + if (session->logicalSessionCnt_.load(std::memory_order_relaxed) <= 0) { { std::lock_guard lock(isNeedJoinLnnMtx_); if (isNeedJoinLnn_) { @@ -335,18 +321,32 @@ void DeviceManagerServiceImpl::CleanSessionMap(int sessionId, std::shared_ptrsessionId_); + } +} + +void DeviceManagerServiceImpl::CleanSessionMap(int sessionId) +{ + if (softbusConnector_ != nullptr) { softbusConnector_->GetSoftbusSession()->CloseAuthSession(sessionId); + } + { std::lock_guard lock(mapMutex_); + std::shared_ptr session = nullptr; if (sessionsMap_.find(sessionId) != sessionsMap_.end()) { - sessionsMap_[sessionId] = nullptr; + session = sessionsMap_[sessionId]; sessionsMap_.erase(sessionId); } - if (deviceId2SessionIdMap_.find(session->deviceId_) != deviceId2SessionIdMap_.end()) { + if (session != nullptr && deviceId2SessionIdMap_.find(session->deviceId_) != deviceId2SessionIdMap_.end()) { deviceId2SessionIdMap_.erase(session->deviceId_); } + if (sessionEnableMutexMap_.find(sessionId) != sessionEnableMutexMap_.end()) { + sessionStopMap_.erase(sessionId); + sessionEnableMap_.erase(sessionId); + sessionEnableCvReadyMap_.erase(sessionId); + sessionEnableMutexMap_.erase(sessionId); + } } - return; } void DeviceManagerServiceImpl::CleanSessionMapByLogicalSessionId(uint64_t logicalSessionId) @@ -366,7 +366,7 @@ void DeviceManagerServiceImpl::CleanSessionMapByLogicalSessionId(uint64_t logica } auto session = GetCurSession(sessionId); if (session != nullptr) { - CleanSessionMap(sessionId, session); + CleanSessionMap(session); } return; } @@ -390,28 +390,45 @@ void DeviceManagerServiceImpl::CleanAuthMgrByLogicalSessionId(uint64_t logicalSe authMgr_->ClearSoftbusSessionCallback(); } hiChainAuthConnector_->UnRegisterHiChainAuthCallbackById(logicalSessionId); + EraseAuthMgr(tokenId); +} + +std::shared_ptr DeviceManagerServiceImpl::GetAuthMgr() +{ + uint64_t tokenId = IPCSkeleton::GetCallingTokenID(); + return GetAuthMgrByTokenId(tokenId); +} + +void DeviceManagerServiceImpl::AddAuthMgr(uint64_t tokenId, std::shared_ptr authMgr) +{ + if (authMgr == nullptr) { + return; + } { - std::lock_guard lock(authMgrMapMtx_); - if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { - authMgrMap_[tokenId] = nullptr; - authMgrMap_.erase(tokenId); + std::lock_guard mapLock(mapMutex_); + if (sessionEnableMap_.find(sessionId) != sessionEnableMap_.end() && !sessionEnableMap_[sessionId]) { + return; } } - return; + { + std::lock_guard lock(authMgrMapMtx_); + authMgrMap_[tokenId] = authMgr; + } } -std::shared_ptr DeviceManagerServiceImpl::GetAuthMgr() +void DeviceManagerServiceImpl::EraseAuthMgr(uint64_t tokenId) { - uint64_t tokenId = IPCSkeleton::GetCallingTokenID(); { std::lock_guard lock(authMgrMapMtx_); if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { - LOGI("authMgrMap_ token: %{public}" PRId64 ".", tokenId); - return authMgrMap_[tokenId]; + LOGI("authMgrMap_ erase token: %{public}" PRIu64 ".", tokenId); + authMgrMap_.erase(tokenId); } } - LOGE("authMgrMap_ not found, token: %{public}" PRId64 ".", tokenId); - return nullptr; + { + std::lock_guard lock(tokenIdSessionIdMapMtx_); + tokenIdSessionIdMap_.erase(tokenId); + } } // Needed in the callback function @@ -420,11 +437,11 @@ std::shared_ptr DeviceManagerServiceImpl::GetAuthMgrByTokenId(u { std::lock_guard lock(authMgrMapMtx_); if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { - LOGI("authMgrMap_ token: %{public}" PRId64 ".", tokenId); + LOGI("authMgrMap_ token: %{public}" PRIu64 ".", tokenId); return authMgrMap_[tokenId]; } } - LOGE("authMgrMap_ not found, token: %{public}" PRId64 ".", tokenId); + LOGE("authMgrMap_ not found, token: %{public}" PRIu64 ".", tokenId); return nullptr; } @@ -525,9 +542,11 @@ void DeviceManagerServiceImpl::ReleaseMaps() configsMap_.clear(); } deviceId2SessionIdMap_.clear(); - deviceIdMutexMap_.clear(); sessionEnableMutexMap_.clear(); sessionEnableCvMap_.clear(); + sessionStopMap_.clear(); + sessionEnableMap_.clear(); + sessionEnableCvReadyMap_.clear(); { std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); logicalSessionId2TokenIdMap_.clear(); @@ -593,12 +612,48 @@ int32_t DeviceManagerServiceImpl::StopAuthenticateDevice(const std::string &pkgN LOGE("DeviceManagerServiceImpl::StopAuthenticateDevice failed"); return ERR_DM_INPUT_PARA_INVALID; } - auto authMgr = GetAuthMgr(); - if (authMgr == nullptr) { - LOGE("authMgr is nullptr"); - return ERR_DM_POINT_NULL; + uint64_t tokenId = IPCSkeleton::GetCallingTokenID(); + int32_t sessionId = 0; + int32_t tryCnt = 0; + const int32_t maxTryCnt = 5; + while (tryCnt < maxTryCnt) { + { + std::lock_guard lock(tokenIdSessionIdMapMtx_); + auto iter = tokenIdSessionIdMap_.find(tokenId); + if (iter == tokenIdSessionIdMap_.end()) { + return DM_OK; + } + sessionId = iter->second; + } + if (sessionId == 0) { + usleep(USLEEP_TIME_US_20000); + } else { + break; + } + } + if (sessionId == 0) { + return ERR_DM_AUTH_BUSINESS_BUSY; + } + { + std::unique_lock cvLock(sessionEnableMutexMap_[sessionId]); + if (!sessionEnableMap_[sessionId]) { + sessionStopMap_[sessionId] = true; + } + sessionEnableCvReadyMap_[sessionId] = true; + sessionEnableCvMap_[sessionId].notify_all(); + } + auto authMgr = GetAuthMgrByTokenId(tokenId); + int32_t ret = DM_OK; + if (authMgr != nullptr) { + ret = authMgr->StopAuthenticateDevice(pkgName); + if (ret == DM_OK) { + usleep(USLEEP_TIME_US_550000); + } + } else { + CleanSessionMap(sessionId); } - return authMgr->StopAuthenticateDevice(pkgName); + EraseAuthMgr(tokenId); + return ret; } int32_t DeviceManagerServiceImpl::UnBindDevice(const std::string &pkgName, const std::string &udid, @@ -839,10 +894,16 @@ std::string DeviceManagerServiceImpl::GetUdidHashByNetworkId(const std::string & int DeviceManagerServiceImpl::OnSessionOpened(int sessionId, int result) { - { + bool isNeedCloseSession = false; + if (sessionEnableCvMap_.find(sessionId) != sessionEnableCvMap_.end()) { std::lock_guard lock(sessionEnableMutexMap_[sessionId]); if (result == 0) { - LOGE("OnSessionOpened successful, sessionId: %{public}d", sessionId); + LOGI("OnSessionOpened successful, sessionId: %{public}d", sessionId); + if (sessionStopMap_.find(sessionId) != sessionStopMap_.end() && sessionStopMap_[sessionId]) { + isNeedCloseSession = true; + } else { + sessionEnableMap_[sessionId] = true; + } } else { LOGE("OnSessionOpened failed, sessionId: %{public}d, res: %{public}d", sessionId, result); } @@ -861,7 +922,10 @@ int DeviceManagerServiceImpl::OnSessionOpened(int sessionId, int result) if (!DmRadarHelper::GetInstance().ReportAuthSessionOpenCb(info)) { LOGE("ReportAuthSessionOpenCb failed"); } - + if (isNeedCloseSession) { + CleanSessionMap(sessionId); + return DM_OK; + } // Get the remote deviceId, sink end gives sessionsMap[deviceId] = session; { std::lock_guard lock(mapMutex_); @@ -991,10 +1055,7 @@ int32_t DeviceManagerServiceImpl::TransferSrcOldAuthMgr(std::shared_ptr int32_t authType = -1; authMgr->ParseAuthType(bindParam, authType); ImportAuthCodeToConfig(authMgr, tokenId); - { - std::lock_guard lock(authMgrMapMtx_); - authMgrMap_.erase(tokenId); - } + EraseAuthMgr(tokenId); if (InitAndRegisterAuthMgr(true, tokenId, curSession, logicalSessionId, "") != DM_OK) { return ERR_DM_AUTH_FAILED; } @@ -1544,56 +1605,54 @@ int DeviceManagerServiceImpl::OpenAuthSession(const std::string& deviceId, } std::shared_ptr DeviceManagerServiceImpl::GetOrCreateSession(const std::string& deviceId, - const std::map &bindParam) + const std::map &bindParam, uint64_t tokenId) { std::shared_ptr instance; int sessionId = -1; - // Acquire global lock to ensure thread safety for maps - { - std::lock_guard lock(mapMutex_); - if (deviceId2SessionIdMap_.find(deviceId) != deviceId2SessionIdMap_.end()) { - sessionId = deviceId2SessionIdMap_[deviceId]; - } - if (sessionsMap_.find(sessionId) != sessionsMap_.end()) { - return sessionsMap_[sessionId]; - } - } - - // Get the lock corresponding to deviceId - std::mutex& device_mutex = deviceIdMutexMap_[deviceId]; - std::lock_guard lock(device_mutex); // Check again whether the corresponding object already exists (because other threads may have created it during // the lock acquisition in the previous step) - { - std::lock_guard lock(mapMutex_); - if (deviceId2SessionIdMap_.find(deviceId) != deviceId2SessionIdMap_.end()) { - sessionId = deviceId2SessionIdMap_[deviceId]; - } - if (sessionsMap_.find(sessionId) != sessionsMap_.end()) { - return sessionsMap_[sessionId]; - } - sessionId = OpenAuthSession(deviceId, bindParam); - if (sessionId < 0) { - LOGE("OpenAuthSession failed, stop the authentication"); - return nullptr; - } + std::lock_guard lock(mapMutex_); + if (deviceId2SessionIdMap_.find(deviceId) != deviceId2SessionIdMap_.end()) { + sessionId = deviceId2SessionIdMap_[deviceId]; + } + if (sessionsMap_.find(sessionId) != sessionsMap_.end()) { + return sessionsMap_[sessionId]; + } - std::unique_lock cvLock(sessionEnableMutexMap_[sessionId]); - sessionEnableCvReadyMap_[sessionId] = false; - if (sessionEnableCvMap_[sessionId].wait_for(cvLock, std::chrono::milliseconds(OPEN_AUTH_SESSION_TIMEOUT), - [&] { return sessionEnableCvReadyMap_[sessionId]; })) { - LOGI("session enable, sessionId: %{public}d.", sessionId); - } else { - LOGE("wait session enable timeout or enable fail, sessionId: %{public}d.", sessionId); - return nullptr; - } - sessionEnableCvReadyMap_.erase(sessionId); - instance = std::make_shared(sessionId, deviceId); - deviceId2SessionIdMap_[deviceId] = sessionId; - sessionsMap_[sessionId] = instance; + sessionId = OpenAuthSession(deviceId, bindParam); + if (sessionId < 0) { + LOGE("OpenAuthSession failed, stop the authentication"); + return nullptr; } + + { + std::lock_guard lock(tokenIdSessionIdMapMtx_); + tokenIdSessionIdMap_[tokenId] = sessionId; + } + + std::unique_lock cvLock(sessionEnableMutexMap_[sessionId]); + sessionEnableCvReadyMap_[sessionId] = false; + sessionStopMap_[sessionId] = false; + sessionEnableMap_[sessionId] = false; + if (!sessionEnableCvMap_[sessionId].wait_for(cvLock, std::chrono::milliseconds(OPEN_AUTH_SESSION_TIMEOUT), + [&] { return sessionEnableCvReadyMap_[sessionId]; })) { + LOGE("wait session enable timeout or enable fail, sessionId: %{public}d.", sessionId); + return nullptr; + } + if (sessionStopMap_.find(sessionId) != sessionStopMap_.end() && sessionStopMap_[sessionId]) { + LOGW("stop by caller, sessionId: %{public}d.", sessionId); + return nullptr; + } + if (sessionEnableMap_.find(sessionId) != sessionEnableMap_.end() && !sessionEnableMap_[sessionId]) { + LOGW("Open session failed, sessionId: %{public}d.", sessionId); + return nullptr; + } + instance = std::make_shared(sessionId, deviceId); + deviceId2SessionIdMap_[deviceId] = sessionId; + sessionsMap_[sessionId] = instance; + return instance; } @@ -1714,9 +1773,9 @@ void DeviceManagerServiceImpl::SaveTokenIdAndSessionId(uint64_t &tokenId, } } -bool DeviceManagerServiceImpl::ParseConnectAddrAndSetProcessInfo(const PeerTargetId &targetId, - PeerTargetId &targetIdTmp, const std::map &bindParam, ProcessInfo &processInfo, - const std::string &pkgName) +bool DeviceManagerServiceImpl::ParseConnectAddrAndSetProcessInfo(PeerTargetId &targetId, + const std::map &bindParam, ProcessInfo &processInfo, + const std::string &pkgName, uint64_t tokenId) { processInfo.pkgName = pkgName; if (bindParam.count(BIND_CALLER_USERID) != 0) { @@ -1724,11 +1783,11 @@ bool DeviceManagerServiceImpl::ParseConnectAddrAndSetProcessInfo(const PeerTarge } std::string deviceId = ""; if (ParseConnectAddr(targetId, deviceId, bindParam) == DM_OK) { - targetIdTmp.deviceId = deviceId; + targetId.deviceId = deviceId; } else { if (targetId.deviceId.empty()) { LOGE("DeviceManagerServiceImpl::BindTarget failed, ParseConnectAddr failed."); - OnAuthResultAndOnBindResult(processInfo, targetId, targetId.deviceId, ERR_DM_INPUT_PARA_INVALID); + OnAuthResultAndOnBindResult(processInfo, targetId, "", ERR_DM_INPUT_PARA_INVALID, tokenId); return false; } } @@ -1736,8 +1795,15 @@ bool DeviceManagerServiceImpl::ParseConnectAddrAndSetProcessInfo(const PeerTarge } void DeviceManagerServiceImpl::OnAuthResultAndOnBindResult(const ProcessInfo &processInfo, const PeerTargetId &targetId, - const std::string &deviceId, int32_t reason) + const std::string &deviceId, int32_t reason, uint64_t tokenId) { + { + std::lock_guard lock(tokenIdSessionIdMapMtx_); + auto iter = tokenIdSessionIdMap_.find(tokenId); + if (iter != tokenIdSessionIdMap_.end()) { + tokenIdSessionIdMap_.erase(iter); + } + } CHECK_NULL_VOID(listener_); listener_->OnAuthResult(processInfo, deviceId, "", DmAuthStatus::STATUS_DM_AUTH_DEFAULT, reason); listener_->OnBindResult(processInfo, targetId, reason, DmAuthStatus::STATUS_DM_AUTH_DEFAULT, ""); @@ -1748,15 +1814,14 @@ void DeviceManagerServiceImpl::BindTargetImpl(uint64_t tokenId, const std::strin { PeerTargetId targetIdTmp = const_cast(targetId); ProcessInfo processInfo; - if (!ParseConnectAddrAndSetProcessInfo(targetId, targetIdTmp, bindParam, processInfo, pkgName)) { + if (!ParseConnectAddrAndSetProcessInfo(targetIdTmp, bindParam, processInfo, pkgName, tokenId)) { return; } // Created only at the source end. The same target device will not be created repeatedly with the new protocol. - std::shared_ptr curSession = GetOrCreateSession(targetIdTmp.deviceId, bindParam); + std::shared_ptr curSession = GetOrCreateSession(targetIdTmp.deviceId, bindParam, tokenId); if (curSession == nullptr) { - LOGE("Failed to create the session. Target deviceId: %{public}s.", - GetAnonyString(targetIdTmp.deviceId).c_str()); - OnAuthResultAndOnBindResult(processInfo, targetId, targetIdTmp.deviceId, ERR_DM_TIME_OUT); + LOGE("create session failed. Target deviceId: %{public}s.", GetAnonyString(targetIdTmp.deviceId).c_str()); + OnAuthResultAndOnBindResult(processInfo, targetId, targetIdTmp.deviceId, ERR_DM_TIME_OUT, tokenId); return; } @@ -1767,8 +1832,9 @@ void DeviceManagerServiceImpl::BindTargetImpl(uint64_t tokenId, const std::strin logicalSessionId = GenerateRandNum(sessionId); if (curSession->logicalSessionSet_.find(logicalSessionId) != curSession->logicalSessionSet_.end()) { LOGE("Failed to create the logical session."); - softbusConnector_->GetSoftbusSession()->CloseAuthSession(sessionId); - OnAuthResultAndOnBindResult(processInfo, targetId, targetIdTmp.deviceId, ERR_DM_INPUT_PARA_INVALID); + CleanSessionMap(sessionId); + OnAuthResultAndOnBindResult(processInfo, targetId, targetIdTmp.deviceId, + ERR_DM_INPUT_PARA_INVALID, tokenId); return; } } @@ -1777,8 +1843,8 @@ void DeviceManagerServiceImpl::BindTargetImpl(uint64_t tokenId, const std::strin int32_t ret = InitAndRegisterAuthMgr(true, tokenId, curSession, logicalSessionId, ""); if (ret != DM_OK) { LOGE("InitAndRegisterAuthMgr failed, ret %{public}d.", ret); - softbusConnector_->GetSoftbusSession()->CloseAuthSession(sessionId); - OnAuthResultAndOnBindResult(processInfo, targetId, targetIdTmp.deviceId, ret); + CleanSessionMap(sessionId); + OnAuthResultAndOnBindResult(processInfo, targetId, targetIdTmp.deviceId, ret, tokenId); return; } curSession->logicalSessionSet_.insert(logicalSessionId); @@ -1787,16 +1853,16 @@ void DeviceManagerServiceImpl::BindTargetImpl(uint64_t tokenId, const std::strin auto authMgr = GetAuthMgrByTokenId(tokenId); if (authMgr == nullptr) { CleanAuthMgrByLogicalSessionId(logicalSessionId); - OnAuthResultAndOnBindResult(processInfo, targetId, targetIdTmp.deviceId, ERR_DM_POINT_NULL); + OnAuthResultAndOnBindResult(processInfo, targetId, targetIdTmp.deviceId, ERR_DM_POINT_NULL, tokenId); return; } authMgr->SetBindTargetParams(targetId); if ((ret = authMgr->BindTarget(pkgName, targetIdTmp, bindParam, sessionId, logicalSessionId)) != DM_OK) { LOGE("authMgr BindTarget failed, ret %{public}d.", ret); CleanAuthMgrByLogicalSessionId(logicalSessionId); - OnAuthResultAndOnBindResult(processInfo, targetId, targetIdTmp.deviceId, ret); + OnAuthResultAndOnBindResult(processInfo, targetId, targetIdTmp.deviceId, ret, tokenId); } - LOGI("end, tokenId %{public}" PRId64".", tokenId); + LOGI("end, tokenId %{public}" PRIu64".", tokenId); return; } @@ -1813,11 +1879,12 @@ int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const P SetBindCallerInfoToBindParam(bindParam, bindParamTmp, bindCallerInfo); uint64_t tokenId = IPCSkeleton::GetCallingTokenID(); { - std::lock_guard lock(authMgrMapMtx_); - if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { + std::lock_guard lock(tokenIdSessionIdMapMtx_); + if (tokenIdSessionIdMap_.find(tokenId) != tokenIdSessionIdMap_.end()) { LOGE("BindTarget failed, this device is being bound. please try again later."); return ERR_DM_AUTH_BUSINESS_BUSY; } + tokenIdSessionIdMap_[tokenId] = 0; } std::thread newThread(&DeviceManagerServiceImpl::BindTargetImpl, this, tokenId, pkgName, targetId, bindParamTmp); newThread.detach(); @@ -2986,6 +3053,7 @@ void DeviceManagerServiceImpl::GetBindCallerInfo(DmBindCallerInfo &bindCallerInf bindLevel = static_cast(DmRole::DM_ROLE_FA); } if (AppManager::GetInstance().IsSystemSA()) { + isSystemSA = true; bindLevel = static_cast(DmRole::DM_ROLE_SA); } if (AuthManagerBase::CheckProcessNameInWhiteList(bundleName)) { diff --git a/test/unittest/UTTest_device_manager_service_impl.cpp b/test/unittest/UTTest_device_manager_service_impl.cpp index 992779122..3c1e0aed2 100644 --- a/test/unittest/UTTest_device_manager_service_impl.cpp +++ b/test/unittest/UTTest_device_manager_service_impl.cpp @@ -1636,8 +1636,9 @@ HWTEST_F(DeviceManagerServiceImplTest, StopAuthenticateDevice_102, testing::ext: if (deviceManagerServiceImpl_->authMgr_ == nullptr) { deviceManagerServiceImpl_->Initialize(listener_); } + deviceManagerServiceImpl_->tokenIdSessionIdMap_.clear(); int ret = deviceManagerServiceImpl_->StopAuthenticateDevice(pkgName); - EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + EXPECT_EQ(ret, DM_OK); } HWTEST_F(DeviceManagerServiceImplTest, CheckIsSameAccount_001, testing::ext::TestSize.Level1) @@ -2106,26 +2107,93 @@ HWTEST_F(DeviceManagerServiceImplTest, InitNewProtocolAuthMgr_001, testing::ext: bool isSrcSide = true; uint64_t tokenId = 1000023; uint64_t logicalSessionId = 456789; + int sessionId = 1; const std::string pkgName = "InitNewProtocolAuthMgr"; - int32_t ret = deviceManagerServiceImpl_->InitNewProtocolAuthMgr(isSrcSide, tokenId, logicalSessionId, pkgName); + int32_t ret = deviceManagerServiceImpl_->InitNewProtocolAuthMgr(isSrcSide, tokenId, logicalSessionId, pkgName, + sessionId); EXPECT_EQ(ret, DM_OK); isSrcSide = false; - ret = deviceManagerServiceImpl_->InitNewProtocolAuthMgr(isSrcSide, tokenId, logicalSessionId, pkgName); + ret = deviceManagerServiceImpl_->InitNewProtocolAuthMgr(isSrcSide, tokenId, logicalSessionId, pkgName, sessionId); EXPECT_EQ(ret, DM_OK); } HWTEST_F(DeviceManagerServiceImplTest, InitOldProtocolAuthMgr_001, testing::ext::TestSize.Level1) { uint64_t tokenId = 100002311; + int sessionId = 1; const std::string pkgName = "InitOldProtocolAuthMgr"; - int32_t ret = deviceManagerServiceImpl_->InitOldProtocolAuthMgr(tokenId, pkgName); + int32_t ret = deviceManagerServiceImpl_->InitOldProtocolAuthMgr(tokenId, pkgName, sessionId); EXPECT_EQ(ret, DM_OK); deviceManagerServiceImpl_->authMgr_ = nullptr; - ret = deviceManagerServiceImpl_->InitOldProtocolAuthMgr(tokenId, pkgName); + ret = deviceManagerServiceImpl_->InitOldProtocolAuthMgr(tokenId, pkgName, sessionId); EXPECT_EQ(ret, DM_OK); } + +HWTEST_F(DeviceManagerServiceImplTest, CleanSessionMap_001, testing::ext::TestSize.Level1) +{ + int sessionId = 0; + std::string deviceId = "deviceId"; + deviceManagerServiceImpl_->CleanSessionMap(nullptr); + std::shared_ptr session = std::make_shared(sessionId, deviceId); + session->logicalSessionCnt_.fetch_add(1); + deviceManagerServiceImpl_->CleanSessionMap(session); + EXPECT_EQ(session->logicalSessionCnt_.load(), 0); +} + +HWTEST_F(DeviceManagerServiceImplTest, CleanSessionMap_002, testing::ext::TestSize.Level1) +{ + int sessionId = 0; + deviceManagerServiceImpl_->softbusConnector_ = nullptr; + deviceManagerServiceImpl_->CleanSessionMap(sessionId); + if (deviceManagerServiceImpl_->softbusConnector_ == nullptr) { + deviceManagerServiceImpl_->Initialize(listener_); + } + deviceManagerServiceImpl_->sessionsMap_.clear(); + deviceManagerServiceImpl_->CleanSessionMap(sessionId); + std::string deviceId = "deviceId"; + std::shared_ptr session = std::make_shared(sessionId, deviceId); + deviceManagerServiceImpl_->sessionsMap_[sessionId] = session; + deviceManagerServiceImpl_->CleanSessionMap(sessionId); + EXPECT_TRUE(deviceManagerServiceImpl_->sessionsMap_.empty()); +} + +HWTEST_F(DeviceManagerServiceImplTest, AddAuthMgr_001, testing::ext::TestSize.Level1) +{ + uint64_t tokenId = 0; + int sessionId = 1; + deviceManagerServiceImpl_->authMgrMap_.clear(); + deviceManagerServiceImpl_->AddAuthMgr(tokenId, sessionId, nullptr); + EXPECT_TRUE(deviceManagerServiceImpl_->authMgrMap_.empty()); + + if (deviceManagerServiceImpl_->softbusConnector_ == nullptr) { + deviceManagerServiceImpl_->Initialize(listener_); + } + std::shared_ptr authMgr = std::make_shared( + deviceManagerServiceImpl_->softbusConnector_, deviceManagerServiceImpl_->hiChainConnector_, + deviceManagerServiceImpl_->listener_, deviceManagerServiceImpl_->hiChainAuthConnector_); + deviceManagerServiceImpl_->sessionEnableMap_[sessionId] = false; + deviceManagerServiceImpl_->AddAuthMgr(tokenId, sessionId, authMgr); + EXPECT_TRUE(deviceManagerServiceImpl_->authMgrMap_.empty()); + + deviceManagerServiceImpl_->sessionEnableMap_.clear(); + deviceManagerServiceImpl_->AddAuthMgr(tokenId, sessionId, authMgr); + EXPECT_FALSE(deviceManagerServiceImpl_->authMgrMap_.empty()); + deviceManagerServiceImpl_->sessionEnableMap_.clear(); + deviceManagerServiceImpl_->authMgrMap_.clear(); +} + +HWTEST_F(DeviceManagerServiceImplTest, EraseAuthMgr_001, testing::ext::TestSize.Level1) +{ + uint64_t tokenId = 0; + std::shared_ptr authMgr = std::make_shared( + deviceManagerServiceImpl_->softbusConnector_, deviceManagerServiceImpl_->hiChainConnector_, + deviceManagerServiceImpl_->listener_, deviceManagerServiceImpl_->hiChainAuthConnector_); + deviceManagerServiceImpl_->authMgrMap_[tokenId] = authMgr; + deviceManagerServiceImpl_->EraseAuthMgr(tokenId); + EXPECT_TRUE(deviceManagerServiceImpl_->authMgrMap_.find(tokenId) == deviceManagerServiceImpl_->authMgrMap_.end()); +} } // namespace } // namespace DistributedHardware } // namespace OHOS -- Gitee