From 48a6845a149d695df37e2bd35b1b900e3bf99b6a Mon Sep 17 00:00:00 2001 From: ZHANGHE24 Date: Wed, 30 Apr 2025 00:54:42 +0800 Subject: [PATCH 1/4] fix: bindtarget bug Signed-off-by: ZHANGHE24 --- .../authentication_v2/dm_auth_manager_base.h | 1 - .../authentication_v2/dm_auth_state_machine.h | 1 - .../include/device_manager_service_impl.h | 4 +- .../dm_auth_manager_base.cpp | 1 - .../dm_auth_state_machine.cpp | 16 +----- .../src/device_manager_service_impl.cpp | 55 +++++++++++-------- 6 files changed, 38 insertions(+), 40 deletions(-) diff --git a/services/implementation/include/authentication_v2/dm_auth_manager_base.h b/services/implementation/include/authentication_v2/dm_auth_manager_base.h index c196aada9..2ceff22d2 100644 --- a/services/implementation/include/authentication_v2/dm_auth_manager_base.h +++ b/services/implementation/include/authentication_v2/dm_auth_manager_base.h @@ -96,7 +96,6 @@ extern const int32_t HML_SESSION_TIMEOUT; extern const int32_t SESSION_HEARTBEAT_TIMEOUT; extern const int32_t PIN_AUTH_TIMEOUT; extern const int32_t EVENT_TIMEOUT; -extern const int32_t WAIT_TIMEOUT; extern const int32_t DM_AUTH_TYPE_MAX; extern const int32_t DM_AUTH_TYPE_MIN; diff --git a/services/implementation/include/authentication_v2/dm_auth_state_machine.h b/services/implementation/include/authentication_v2/dm_auth_state_machine.h index ed7114836..03a0d83dd 100644 --- a/services/implementation/include/authentication_v2/dm_auth_state_machine.h +++ b/services/implementation/include/authentication_v2/dm_auth_state_machine.h @@ -114,7 +114,6 @@ private: std::condition_variable stateCv_; std::mutex eventMutex_; std::condition_variable eventCv_; - bool eventCvReady_{false}; // Direction of authentication DmAuthDirection direction_; diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index 32347ab91..328a61a1a 100644 --- a/services/implementation/include/device_manager_service_impl.h +++ b/services/implementation/include/device_manager_service_impl.h @@ -243,6 +243,8 @@ private: void CheckIsLastLnnAcl(DistributedDeviceProfile::AccessControlProfile profile, DistributedDeviceProfile::AccessControlProfile delProfile, DmOfflineParam &lnnAclParam, bool &isLastLnnAcl, const std::string &localUdid); + void BindTargetImpl(uint64_t tokenId, const std::string &pkgName, const PeerTargetId &targetId, + const std::map &bindParam); private: std::shared_ptr authMgr_; // Old protocol only std::mutex authMgrMtx_; @@ -263,7 +265,7 @@ private: 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_的锁 + 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 diff --git a/services/implementation/src/authentication_v2/dm_auth_manager_base.cpp b/services/implementation/src/authentication_v2/dm_auth_manager_base.cpp index b57e00daa..49eb28e17 100644 --- a/services/implementation/src/authentication_v2/dm_auth_manager_base.cpp +++ b/services/implementation/src/authentication_v2/dm_auth_manager_base.cpp @@ -97,7 +97,6 @@ const int32_t HML_SESSION_TIMEOUT = 10; const int32_t SESSION_HEARTBEAT_TIMEOUT = 50; const int32_t PIN_AUTH_TIMEOUT = 60; const int32_t EVENT_TIMEOUT = 5000; // 5000 ms -const int32_t WAIT_TIMEOUT = 2000; // 2000 ms int32_t AuthManagerBase::AuthenticateDevice(const std::string &pkgName, int32_t authType, diff --git a/services/implementation/src/authentication_v2/dm_auth_state_machine.cpp b/services/implementation/src/authentication_v2/dm_auth_state_machine.cpp index 8265cbe40..fea412184 100644 --- a/services/implementation/src/authentication_v2/dm_auth_state_machine.cpp +++ b/services/implementation/src/authentication_v2/dm_auth_state_machine.cpp @@ -262,18 +262,9 @@ DmEventType DmAuthStateMachine::WaitExpectEvent(DmEventType eventType) std::unique_lock lock(eventMutex_); auto startTime = std::chrono::high_resolution_clock::now(); while (running_.load()) { - if (eventCv_.wait_for(lock, std::chrono::milliseconds(WAIT_TIMEOUT), [&] { - return !running_.load() || !eventQueue_.empty() || eventCvReady_; - })) { - eventCvReady_ = false; - LOGI("DmAuthStateMachine: WaitExpectEvent wait successful."); - } else { - if (!eventCvReady_) { - LOGE("DmAuthStateMachine: WaitExpectEvent wait timeout."); - return DmEventType::ON_TIMEOUT; - } - eventCvReady_ = false; - } + eventCv_.wait(lock, [&] { + return !running_.load() || !eventQueue_.empty(); + }); if (!running_.load()) { return DmEventType::ON_FAIL; } @@ -305,7 +296,6 @@ void DmAuthStateMachine::NotifyEventFinish(DmEventType eventType) { std::unique_lock lock(eventMutex_); eventQueue_.push(eventType); - eventCvReady_ = true; } eventCv_.notify_one(); if (eventType == DmEventType::ON_FAIL) { diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index ff7e0ef17..88731386d 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -717,11 +717,11 @@ int DeviceManagerServiceImpl::OnSessionOpened(int sessionId, int result) { std::lock_guard lock(sessionEnableMutexMap_[sessionId]); if (result == 0) { - sessionEnableCvReadyMap_[sessionId] = true; LOGE("OnSessionOpened successful, sessionId: %{public}d", sessionId); } else { LOGE("OnSessionOpened failed, sessionId: %{public}d, res: %{public}d", sessionId, result); } + sessionEnableCvReadyMap_[sessionId] = true; sessionEnableCvMap_[sessionId].notify_all(); } std::string peerUdid = ""; @@ -1424,14 +1424,10 @@ std::shared_ptr DeviceManagerServiceImpl::GetOrCreateSession(const std: } std::unique_lock cvLock(sessionEnableMutexMap_[sessionId]); - sessionEnableCvReadyMap_[sessionId] = false; - if (sessionEnableCvMap_[sessionId].wait_for(cvLock, std::chrono::milliseconds(WAIT_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; - } + sessionEnableCvMap_[sessionId].wait(cvLock, [&] { + return sessionEnableCvReadyMap_[sessionId]; + }); + instance = std::make_shared(sessionId, deviceId); deviceId2SessionIdMap_[deviceId] = sessionId; sessionsMap_[sessionId] = instance; @@ -1533,14 +1529,9 @@ int32_t DeviceManagerServiceImpl::ParseConnectAddr(const PeerTargetId &targetId, return DM_OK; } -int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const PeerTargetId &targetId, - const std::map &bindParam) +void DeviceManagerServiceImpl::BindTargetImpl(uint64_t tokenId, const std::string &pkgName, + const PeerTargetId &targetId, const std::map &bindParam) { - if (pkgName.empty()) { - LOGE("BindTarget failed, pkgName is empty."); - return ERR_DM_INPUT_PARA_INVALID; - } - std::string deviceId = ""; PeerTargetId targetIdTmp = const_cast(targetId); if (ParseConnectAddr(targetId, deviceId, bindParam) == DM_OK) { @@ -1548,14 +1539,14 @@ int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const P } else { if (targetId.deviceId.empty()) { LOGE("DeviceManagerServiceImpl::BindTarget failed, ParseConnectAddr failed."); - return ERR_DM_INPUT_PARA_INVALID; + 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.", targetIdTmp.deviceId.c_str()); - return ERR_DM_AUTH_OPEN_SESSION_FAILED; + return; } // Logical session random number @@ -1565,16 +1556,17 @@ int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const P logicalSessionId = GenerateRandNum(sessionId); if (curSession->logicalSessionSet_.find(logicalSessionId) != curSession->logicalSessionSet_.end()) { LOGE("Failed to create the logical session."); - return ERR_DM_LOGIC_SESSION_CREATE_FAILED; + softbusConnector_->GetSoftbusSession()->CloseAuthSession(sessionId); + return; } } // Create on the src end. - uint64_t tokenId = IPCSkeleton::GetCallingTokenID(); int32_t ret = InitAndRegisterAuthMgr(true, tokenId, curSession, logicalSessionId); if (ret != DM_OK) { LOGE("InitAndRegisterAuthMgr failed, ret %{public}d.", ret); - return ret; + softbusConnector_->GetSoftbusSession()->CloseAuthSession(sessionId); + return; } curSession->logicalSessionSet_.insert(logicalSessionId); curSession->logicalSessionCnt_.fetch_add(1); @@ -1583,14 +1575,31 @@ int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const P auto authMgr = GetAuthMgrByTokenId(tokenId); if (authMgr == nullptr) { - return ERR_DM_POINT_NULL; + CleanAuthMgrByLogicalSessionId(logicalSessionId); + 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); } - return ret; + LOGI("DeviceManagerServiceImpl BindTargetImpl thread end, tokenId: %{public}" PRId64 ".", tokenId); + return; +} + +int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const PeerTargetId &targetId, + const std::map &bindParam) +{ + if (pkgName.empty()) { + LOGE("BindTarget failed, pkgName is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + + uint64_t tokenId = IPCSkeleton::GetCallingTokenID(); + std::thread newThread(&DeviceManagerServiceImpl::BindTargetImpl, this, tokenId, pkgName, + targetId, bindParam); + newThread.detach(); + return DM_OK; } int32_t DeviceManagerServiceImpl::DpAclAdd(const std::string &udid) -- Gitee From 4524aca44ff97f1f8c60fc22a00ef0731d6f817b Mon Sep 17 00:00:00 2001 From: ZHANGHE24 Date: Wed, 30 Apr 2025 02:15:53 +0800 Subject: [PATCH 2/4] fix: judge send data err code transit to finish Signed-off-by: ZHANGHE24 --- .../authentication_v2/dm_auth_message_processor.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp b/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp index ebee651ec..0b78e0284 100644 --- a/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp +++ b/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp @@ -1000,7 +1000,7 @@ int32_t DmAuthMessageProcessor::ParseNegotiateMessage( if (jsonObject[TAG_BUNDLE_NAME_V2].IsString()) { context->accesser.bundleName = jsonObject[TAG_BUNDLE_NAME_V2].Get(); } - + if (jsonObject[TAG_EXTRA_INFO].IsString()) { context->accesser.extraInfo = jsonObject[TAG_EXTRA_INFO].Get(); } @@ -1269,7 +1269,14 @@ int32_t DmAuthMessageProcessor::CreateMessageForwardUltrasonicNegotiate(std::sha void DmAuthMessageProcessor::CreateAndSendMsg(DmMessageType msgType, std::shared_ptr context) { auto message = CreateMessage(msgType, context); - context->softbusConnector->GetSoftbusSession()->SendData(context->sessionId, message); + int32_t ret = context->softbusConnector->GetSoftbusSession()->SendData(context->sessionId, message); + if (ret != DM_OK) { + if (context->direction == DM_AUTH_SOURCE) { + context->authStateMachine->TransitionTo(std::make_shared()); + } else { + context->authStateMachine->TransitionTo(std::make_shared()); + } + } } std::string DmAuthMessageProcessor::CompressSyncMsg(std::string &inputStr) -- Gitee From c7aa4a6e45432b8681187f2f906050c7deae9f67 Mon Sep 17 00:00:00 2001 From: ZHANGHE24 Date: Wed, 30 Apr 2025 09:48:46 +0800 Subject: [PATCH 3/4] fix: add thread creation restriction Signed-off-by: ZHANGHE24 --- .../src/device_manager_service_impl.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index 88731386d..8c23b6f9d 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -1009,6 +1009,9 @@ void DeviceManagerServiceImpl::OnBytesReceived(int sessionId, const void *data, if (authMgr == nullptr) { return; } + if (msgType == MSG_TYPE_REQ_ACL_NEGOTIATE || msgType == MSG_TYPE_RESP_ACL_NEGOTIATE) { + curSession->version_ = DM_CURRENT_VERSION; + } } else { /**         Monitor old messages on ports 80/90 @@ -1596,9 +1599,14 @@ int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const P } uint64_t tokenId = IPCSkeleton::GetCallingTokenID(); - std::thread newThread(&DeviceManagerServiceImpl::BindTargetImpl, this, tokenId, pkgName, - targetId, bindParam); - newThread.detach(); + if (authMgrMap_.find(tokenId) == authMgrMap_.end()) { + std::thread newThread(&DeviceManagerServiceImpl::BindTargetImpl, this, tokenId, pkgName, + targetId, bindParam); + newThread.detach(); + } else { + LOGE("BindTarget failed, this device is being bound. Please try again later."); + return ERR_DM_AUTH_BUSINESS_BUSY; + } return DM_OK; } -- Gitee From b3c6a5b79d580283c30031c466f02e05508e61a6 Mon Sep 17 00:00:00 2001 From: ZHANGHE24 Date: Wed, 30 Apr 2025 14:32:04 +0800 Subject: [PATCH 4/4] fix: src bindtarget bundleName err Signed-off-by: ZHANGHE24 --- .../include/authentication_v2/auth_manager.h | 1 - .../include/device_manager_service_impl.h | 2 ++ .../src/authentication_v2/auth_manager.cpp | 21 ++++-------- .../src/device_manager_service_impl.cpp | 32 ++++++++++++++++++- 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/services/implementation/include/authentication_v2/auth_manager.h b/services/implementation/include/authentication_v2/auth_manager.h index f0cb9b39d..5ceca2747 100644 --- a/services/implementation/include/authentication_v2/auth_manager.h +++ b/services/implementation/include/authentication_v2/auth_manager.h @@ -135,7 +135,6 @@ private: void ParseJsonObject(const JsonObject &jsonObject); void GetAuthParam(const std::string &sessionName, int32_t authType, const std::string &deviceId, const std::string &extra); - std::string GetBundleName(const JsonObject &jsonObject); void SetAuthType(int32_t authType); bool IsAuthTypeSupported(const int32_t &authType); bool IsAuthCodeReady(const std::string &sessionName); diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index 328a61a1a..99fccbb45 100644 --- a/services/implementation/include/device_manager_service_impl.h +++ b/services/implementation/include/device_manager_service_impl.h @@ -245,6 +245,8 @@ private: bool &isLastLnnAcl, const std::string &localUdid); void BindTargetImpl(uint64_t tokenId, const std::string &pkgName, const PeerTargetId &targetId, const std::map &bindParam); + int32_t SetBundleNameAndTokenIdToExtra(const std::map &bindParam, + std::map &bindParamTmp, uint64_t tokenId); private: std::shared_ptr authMgr_; // Old protocol only std::mutex authMgrMtx_; diff --git a/services/implementation/src/authentication_v2/auth_manager.cpp b/services/implementation/src/authentication_v2/auth_manager.cpp index 662df9c97..6abe745e2 100644 --- a/services/implementation/src/authentication_v2/auth_manager.cpp +++ b/services/implementation/src/authentication_v2/auth_manager.cpp @@ -405,23 +405,16 @@ void AuthManager::ParseHmlInfoInJsonObject(const JsonObject &jsonObject) return; } -std::string AuthManager::GetBundleName(const JsonObject &jsonObject) -{ - if (!jsonObject.IsDiscarded() && jsonObject[BUNDLE_NAME_KEY].IsString()) { - return jsonObject[BUNDLE_NAME_KEY].Get(); - } - bool isSystemSA = false; - std::string bundleName; - AppManager::GetInstance().GetCallerName(isSystemSA, bundleName); - return bundleName; -} - void AuthManager::ParseJsonObject(const JsonObject &jsonObject) { if (jsonObject.IsDiscarded()) { return; } + if (jsonObject[TAG_TOKENID].IsNumberInteger()) { + context_->accesser.tokenId = jsonObject[TAG_TOKENID].Get(); + } + if (jsonObject[APP_OPERATION_KEY].IsString()) { context_->appOperation = jsonObject[APP_OPERATION_KEY].Get(); } @@ -437,7 +430,8 @@ void AuthManager::ParseJsonObject(const JsonObject &jsonObject) context_->connDelayCloseTime = GetCloseSessionDelaySeconds(delaySecondsStr); } - context_->accesser.bundleName = GetBundleName(jsonObject); + // 上层逻辑已保证jsonObject[BUNDLE_NAME_KEY]不为空 + context_->accesser.bundleName = jsonObject[BUNDLE_NAME_KEY].Get(); bindParam_[BUNDLE_NAME_KEY] = context_->accesser.bundleName; context_->accessee.bundleName = context_->accesser.bundleName; @@ -553,9 +547,6 @@ void AuthManager::GetAuthParam(const std::string &pkgName, int32_t authType, context_->accesser.deviceName = context_->listener->GetLocalDisplayDeviceNameForPrivacy(); context_->accesser.deviceType = context_->softbusConnector->GetLocalDeviceTypeId(); context_->accesser.isOnline = false; - uint32_t callingTokenId = 0; - MultipleUserConnector::GetCallingTokenId(callingTokenId); - context_->accesser.tokenId = static_cast(callingTokenId); context_->accessee.deviceId = deviceId; context_->accessee.addr = deviceId; diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index 8c23b6f9d..049e623d1 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -1590,6 +1590,31 @@ void DeviceManagerServiceImpl::BindTargetImpl(uint64_t tokenId, const std::strin return; } +int32_t DeviceManagerServiceImpl::SetBundleNameAndTokenIdToExtra(const std::map &bindParam, + std::map &bindParamTmp, uint64_t tokenId) +{ + bindParamTmp = const_cast&>(bindParam); + JsonObject jsonObject = GetExtraJsonObject(bindParamTmp); + if (jsonObject.IsDiscarded()) { + return ERR_DM_INPUT_PARA_INVALID; + } + jsonObject[TAG_TOKENID] = tokenId; + if (!jsonObject[BUNDLE_NAME_KEY].IsString()) { + bool isSystemSA = false; + std::string bundleName; + AppManager::GetInstance().GetCallerName(isSystemSA, bundleName); + jsonObject[BUNDLE_NAME_KEY] = bundleName; + } + + auto iter = bindParamTmp.find(PARAM_KEY_BIND_EXTRA_DATA); + if (iter != bindParamTmp.end()) { + iter->second = jsonObject.Dump(); + } else { + ParseMapFromJsonString(jsonObject.Dump(), bindParamTmp); + } + return DM_OK; +} + int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const PeerTargetId &targetId, const std::map &bindParam) { @@ -1600,8 +1625,13 @@ int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const P uint64_t tokenId = IPCSkeleton::GetCallingTokenID(); if (authMgrMap_.find(tokenId) == authMgrMap_.end()) { + std::map bindParamTmp; + if (SetBundleNameAndTokenIdToExtra(bindParam, bindParamTmp, tokenId) != DM_OK) { + LOGE("BindTarget failed, SetBundleNameAndTokenIdToExtra failed."); + return ERR_DM_INPUT_PARA_INVALID; + } std::thread newThread(&DeviceManagerServiceImpl::BindTargetImpl, this, tokenId, pkgName, - targetId, bindParam); + targetId, bindParamTmp); newThread.detach(); } else { LOGE("BindTarget failed, this device is being bound. Please try again later."); -- Gitee