From 2089b53a2c30ba27c6792c0fbd7d09a5172029c1 Mon Sep 17 00:00:00 2001 From: guoyi Date: Mon, 11 Aug 2025 16:18:28 +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 | 11 +- .../src/device_manager_service_impl.cpp | 110 ++++++++++++------ .../service/src/device_manager_service.cpp | 6 +- 3 files changed, 81 insertions(+), 46 deletions(-) diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index 75b46dbad..762ec5a14 100644 --- a/services/implementation/include/device_manager_service_impl.h +++ b/services/implementation/include/device_manager_service_impl.h @@ -227,6 +227,7 @@ private: void HandleRemoteUserRemoved(int32_t preUserId, const std::string &remoteUdid); DmAuthForm ConvertBindTypeToAuthForm(int32_t bindType); std::shared_ptr GetAuthMgr(); + void EraseAuthMgr(uint64_t tokenId); std::shared_ptr GetAuthMgrByTokenId(uint64_t tokenId); std::shared_ptr GetCurrentAuthMgr(); void CreateGlobalClassicalAuthMgr(); @@ -293,11 +294,11 @@ private: 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); + 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: @@ -333,6 +334,8 @@ private: std::map> configsMap_; // Import when authMgr is not initialized std::mutex authMgrMapMtx_; std::map> authMgrMap_; // New protocol sharing + std::mutex bindTargetTokenIdsMtx_; + std::unordered_set bindTargetTokenIds_; // 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..4ad77bc19 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -74,6 +74,7 @@ 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 OPEN_AUTH_SESSION_TIMEOUT = 15000; // 15000ms const std::map BUNDLENAME_MAPPING = { @@ -160,7 +161,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; @@ -390,28 +391,31 @@ void DeviceManagerServiceImpl::CleanAuthMgrByLogicalSessionId(uint64_t logicalSe authMgr_->ClearSoftbusSessionCallback(); } hiChainAuthConnector_->UnRegisterHiChainAuthCallbackById(logicalSessionId); - { - std::lock_guard lock(authMgrMapMtx_); - if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { - authMgrMap_[tokenId] = nullptr; - authMgrMap_.erase(tokenId); - } - } + EraseAuthMgr(tokenId); return; } std::shared_ptr DeviceManagerServiceImpl::GetAuthMgr() { uint64_t tokenId = IPCSkeleton::GetCallingTokenID(); + return GetAuthMgrByTokenId(tokenId); +} + +void DeviceManagerServiceImpl::EraseAuthMgr(uint64_t tokenId) +{ + { + std::lock_guard lock(bindTargetTokenIdsMtx_); + LOGI("bindTargetTokenIds erase token: %{public}" PRIu64 ".", tokenId); + bindTargetTokenIds_.erase(tokenId); + } { 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_[tokenId] = nullptr; + authMgrMap_.erase(tokenId); } } - LOGE("authMgrMap_ not found, token: %{public}" PRId64 ".", tokenId); - return nullptr; } // Needed in the callback function @@ -420,11 +424,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; } @@ -506,6 +510,10 @@ int32_t DeviceManagerServiceImpl::Initialize(const std::shared_ptr lock(bindTargetTokenIdsMtx_); + bindTargetTokenIds_.clear(); + } { std::lock_guard lock(authMgrMapMtx_); for (auto& pair : authMgrMap_) { @@ -575,7 +583,8 @@ int32_t DeviceManagerServiceImpl::UnAuthenticateDevice(const std::string &pkgNam pkgName.c_str(), GetAnonyString(udid).c_str()); return ERR_DM_INPUT_PARA_INVALID; } - auto authMgr = GetAuthMgr(); + uint64_t tokenId = IPCSkeleton::GetCallingTokenID(); + auto authMgr = GetAuthMgrByTokenId(tokenId); if (authMgr == nullptr) { LOGE("authMgr is nullptr, invoke the old protocal."); if (authMgr_ == nullptr) { @@ -593,12 +602,23 @@ int32_t DeviceManagerServiceImpl::StopAuthenticateDevice(const std::string &pkgN LOGE("DeviceManagerServiceImpl::StopAuthenticateDevice failed"); return ERR_DM_INPUT_PARA_INVALID; } - auto authMgr = GetAuthMgr(); + uint64_t tokenId = IPCSkeleton::GetCallingTokenID(); + auto authMgr = GetAuthMgrByTokenId(tokenId); if (authMgr == nullptr) { - LOGE("authMgr is nullptr"); + std::lock_guard lock(bindTargetTokenIdsMtx_); + if (bindTargetTokenIds_.find(tokenId) != bindTargetTokenIds_.end()) { + LOGE("Last bind is runnings, please try again later. ret:%{public}d", ERR_DM_AUTH_BUSINESS_BUSY); + return ERR_DM_AUTH_BUSINESS_BUSY; + } + LOGE("authMgr is nullptr. ret:%{public}d", ERR_DM_POINT_NULL); return ERR_DM_POINT_NULL; } - return authMgr->StopAuthenticateDevice(pkgName); + int32_t ret = authMgr->StopAuthenticateDevice(pkgName); + if (ret == DM_OK) { + usleep(USLEEP_TIME_US_550000); + } + EraseAuthMgr(tokenId); + return ret; } int32_t DeviceManagerServiceImpl::UnBindDevice(const std::string &pkgName, const std::string &udid, @@ -991,10 +1011,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; } @@ -1714,21 +1731,22 @@ 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 &targetIdTmp, + const std::map &bindParam, ProcessInfo &processInfo, + const std::string &pkgName, uint64_t tokenId) { processInfo.pkgName = pkgName; if (bindParam.count(BIND_CALLER_USERID) != 0) { processInfo.userId = std::atoi(bindParam.at(BIND_CALLER_USERID).c_str()); } std::string deviceId = ""; - if (ParseConnectAddr(targetId, deviceId, bindParam) == DM_OK) { + if (ParseConnectAddr(targetIdTmp, deviceId, bindParam) == DM_OK) { targetIdTmp.deviceId = deviceId; } else { - if (targetId.deviceId.empty()) { + if (targetIdTmp.deviceId.empty()) { LOGE("DeviceManagerServiceImpl::BindTarget failed, ParseConnectAddr failed."); - OnAuthResultAndOnBindResult(processInfo, targetId, targetId.deviceId, ERR_DM_INPUT_PARA_INVALID); + OnAuthResultAndOnBindResult(processInfo, targetIdTmp, targetIdTmp.deviceId, + ERR_DM_INPUT_PARA_INVALID, tokenId); return false; } } @@ -1736,11 +1754,16 @@ 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) { CHECK_NULL_VOID(listener_); listener_->OnAuthResult(processInfo, deviceId, "", DmAuthStatus::STATUS_DM_AUTH_DEFAULT, reason); listener_->OnBindResult(processInfo, targetId, reason, DmAuthStatus::STATUS_DM_AUTH_DEFAULT, ""); + { + std::lock_guard lock(bindTargetTokenIdsMtx_); + LOGE("bindTargetTokenIds_.erase"); + bindTargetTokenIds_.erase(tokenId); + } } void DeviceManagerServiceImpl::BindTargetImpl(uint64_t tokenId, const std::string &pkgName, @@ -1748,15 +1771,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); 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("Failed to create the session. Target deviceId:%{public}s.", GetAnonyString(targetIdTmp.deviceId).c_str()); + OnAuthResultAndOnBindResult(processInfo, targetId, targetIdTmp.deviceId, ERR_DM_TIME_OUT, tokenId); return; } @@ -1768,7 +1790,8 @@ void DeviceManagerServiceImpl::BindTargetImpl(uint64_t tokenId, const std::strin 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); + OnAuthResultAndOnBindResult(processInfo, targetId, targetIdTmp.deviceId, + ERR_DM_INPUT_PARA_INVALID, tokenId); return; } } @@ -1778,7 +1801,7 @@ void DeviceManagerServiceImpl::BindTargetImpl(uint64_t tokenId, const std::strin if (ret != DM_OK) { LOGE("InitAndRegisterAuthMgr failed, ret %{public}d.", ret); softbusConnector_->GetSoftbusSession()->CloseAuthSession(sessionId); - OnAuthResultAndOnBindResult(processInfo, targetId, targetIdTmp.deviceId, ret); + OnAuthResultAndOnBindResult(processInfo, targetId, targetIdTmp.deviceId, ret, tokenId); return; } curSession->logicalSessionSet_.insert(logicalSessionId); @@ -1787,14 +1810,14 @@ 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); return; @@ -1819,6 +1842,18 @@ int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const P return ERR_DM_AUTH_BUSINESS_BUSY; } } + { + std::lock_guard lock(bindTargetTokenIdsMtx_); + if (bindTargetTokenIds_.find(tokenId) != bindTargetTokenIds_.end()) { + LOGE("BindTarget failed, this device is being bound. please try again later."); + return ERR_DM_AUTH_BUSINESS_BUSY; + } + if (bindTargetTokenIds_.size() > MAX_NEW_PROC_SESSION_COUNT_TEMP) { + LOGE("Other bind session exist, can not start new one."); + return ERR_DM_AUTH_BUSINESS_BUSY; + } + bindTargetTokenIds_.insert(tokenId); + } std::thread newThread(&DeviceManagerServiceImpl::BindTargetImpl, this, tokenId, pkgName, targetId, bindParamTmp); newThread.detach(); return DM_OK; @@ -2983,6 +3018,7 @@ void DeviceManagerServiceImpl::GetBindCallerInfo(DmBindCallerInfo &bindCallerInf AppManager::GetInstance().GetCallerProcessName(processName); int32_t bindLevel = static_cast(DmRole::DM_ROLE_FA); if (AppManager::GetInstance().IsSystemApp()) { + isSystemSA = true; bindLevel = static_cast(DmRole::DM_ROLE_FA); } if (AppManager::GetInstance().IsSystemSA()) { diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index bf85b5fca..27e1c3d78 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -813,11 +813,7 @@ int32_t DeviceManagerService::StopAuthenticateDevice(const std::string &pkgName) LOGE("StopAuthenticateDevice failed, instance not init or init failed."); return ERR_DM_NOT_INIT; } - if (dmServiceImpl_->StopAuthenticateDevice(pkgName) != DM_OK) { - LOGE("dmServiceImpl_ StopAuthenticateDevice failed."); - return ERR_DM_FAILED; - } - return DM_OK; + return dmServiceImpl_->StopAuthenticateDevice(pkgName); } int32_t DeviceManagerService::BindDevice(const std::string &pkgName, int32_t authType, const std::string &deviceId, -- Gitee