1a34a8711Sopenharmony_ci/* 2a34a8711Sopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd. 3a34a8711Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4a34a8711Sopenharmony_ci * you may not use this file except in compliance with the License. 5a34a8711Sopenharmony_ci * You may obtain a copy of the License at 6a34a8711Sopenharmony_ci * 7a34a8711Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8a34a8711Sopenharmony_ci * 9a34a8711Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10a34a8711Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11a34a8711Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12a34a8711Sopenharmony_ci * See the License for the specific language governing permissions and 13a34a8711Sopenharmony_ci * limitations under the License. 14a34a8711Sopenharmony_ci */ 15a34a8711Sopenharmony_ci 16a34a8711Sopenharmony_ci#include "dbinder_service.h" 17a34a8711Sopenharmony_ci 18a34a8711Sopenharmony_ci#include <cinttypes> 19a34a8711Sopenharmony_ci#include "securec.h" 20a34a8711Sopenharmony_ci#include "string_ex.h" 21a34a8711Sopenharmony_ci#include "ipc_skeleton.h" 22a34a8711Sopenharmony_ci#include "ipc_thread_skeleton.h" 23a34a8711Sopenharmony_ci 24a34a8711Sopenharmony_ci#include "dbinder_error_code.h" 25a34a8711Sopenharmony_ci#include "dbinder_log.h" 26a34a8711Sopenharmony_ci#include "dbinder_remote_listener.h" 27a34a8711Sopenharmony_ci#include "dbinder_sa_death_recipient.h" 28a34a8711Sopenharmony_ci#include "dbinder_service_stub.h" 29a34a8711Sopenharmony_ci#include "ffrt.h" 30a34a8711Sopenharmony_ci#include "dsoftbus_interface.h" 31a34a8711Sopenharmony_ci 32a34a8711Sopenharmony_cinamespace OHOS { 33a34a8711Sopenharmony_ci 34a34a8711Sopenharmony_cistatic constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_RPC_DBINDER_SER, "DbinderService" }; 35a34a8711Sopenharmony_ci 36a34a8711Sopenharmony_cisptr<DBinderService> DBinderService::instance_ = nullptr; 37a34a8711Sopenharmony_cibool DBinderService::mainThreadCreated_ = false; 38a34a8711Sopenharmony_cistd::mutex DBinderService::instanceMutex_; 39a34a8711Sopenharmony_cistd::shared_ptr<DBinderRemoteListener> DBinderService::remoteListener_ = nullptr; 40a34a8711Sopenharmony_ciconstexpr unsigned int BINDER_MASK = 0xffff; 41a34a8711Sopenharmony_ci// DBinderServiceStub's reference count in a MakeRemoteBinder call. 42a34a8711Sopenharmony_ciconstexpr int DBINDER_STUB_REF_COUNT = 2; 43a34a8711Sopenharmony_ci 44a34a8711Sopenharmony_ciDBinderService::DBinderService() 45a34a8711Sopenharmony_ci{ 46a34a8711Sopenharmony_ci DBINDER_LOGI(LOG_LABEL, "create dbinder service"); 47a34a8711Sopenharmony_ci} 48a34a8711Sopenharmony_ci 49a34a8711Sopenharmony_ciDBinderService::~DBinderService() 50a34a8711Sopenharmony_ci{ 51a34a8711Sopenharmony_ci StopRemoteListener(); 52a34a8711Sopenharmony_ci 53a34a8711Sopenharmony_ci DBinderStubRegisted_.clear(); 54a34a8711Sopenharmony_ci mapDBinderStubRegisters_.clear(); 55a34a8711Sopenharmony_ci mapRemoteBinderObjects_.clear(); 56a34a8711Sopenharmony_ci threadLockInfo_.clear(); 57a34a8711Sopenharmony_ci proxyObject_.clear(); 58a34a8711Sopenharmony_ci sessionObject_.clear(); 59a34a8711Sopenharmony_ci noticeProxy_.clear(); 60a34a8711Sopenharmony_ci deathRecipients_.clear(); 61a34a8711Sopenharmony_ci loadSaReply_.clear(); 62a34a8711Sopenharmony_ci dbinderCallback_ = nullptr; 63a34a8711Sopenharmony_ci DBINDER_LOGI(LOG_LABEL, "dbinder service died"); 64a34a8711Sopenharmony_ci} 65a34a8711Sopenharmony_ci 66a34a8711Sopenharmony_cistd::string DBinderService::GetLocalDeviceID() 67a34a8711Sopenharmony_ci{ 68a34a8711Sopenharmony_ci std::string pkgName = "DBinderService"; 69a34a8711Sopenharmony_ci std::string networkId; 70a34a8711Sopenharmony_ci 71a34a8711Sopenharmony_ci if (DBinderSoftbusClient::GetInstance().GetLocalNodeDeviceId( 72a34a8711Sopenharmony_ci pkgName.c_str(), networkId) != SOFTBUS_CLIENT_SUCCESS) { 73a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "Get local node device id failed"); 74a34a8711Sopenharmony_ci } 75a34a8711Sopenharmony_ci 76a34a8711Sopenharmony_ci return networkId; 77a34a8711Sopenharmony_ci} 78a34a8711Sopenharmony_ci 79a34a8711Sopenharmony_cibool DBinderService::StartDBinderService(std::shared_ptr<RpcSystemAbilityCallback> &callbackImpl) 80a34a8711Sopenharmony_ci{ 81a34a8711Sopenharmony_ci if (mainThreadCreated_) { 82a34a8711Sopenharmony_ci return ReStartRemoteListener(); 83a34a8711Sopenharmony_ci } 84a34a8711Sopenharmony_ci 85a34a8711Sopenharmony_ci bool result = StartRemoteListener(); 86a34a8711Sopenharmony_ci if (!result) { 87a34a8711Sopenharmony_ci return false; 88a34a8711Sopenharmony_ci } 89a34a8711Sopenharmony_ci mainThreadCreated_ = true; 90a34a8711Sopenharmony_ci dbinderCallback_ = callbackImpl; 91a34a8711Sopenharmony_ci 92a34a8711Sopenharmony_ci return true; 93a34a8711Sopenharmony_ci} 94a34a8711Sopenharmony_ci 95a34a8711Sopenharmony_cibool DBinderService::StartRemoteListener() 96a34a8711Sopenharmony_ci{ 97a34a8711Sopenharmony_ci if (remoteListener_ != nullptr) { 98a34a8711Sopenharmony_ci DBINDER_LOGI(LOG_LABEL, "remote listener started"); 99a34a8711Sopenharmony_ci return true; 100a34a8711Sopenharmony_ci } 101a34a8711Sopenharmony_ci 102a34a8711Sopenharmony_ci remoteListener_ = std::make_shared<DBinderRemoteListener>(); 103a34a8711Sopenharmony_ci if (remoteListener_ == nullptr) { 104a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "failed to create remote listener"); 105a34a8711Sopenharmony_ci return false; 106a34a8711Sopenharmony_ci } 107a34a8711Sopenharmony_ci 108a34a8711Sopenharmony_ci if (remoteListener_->StartListener() != true) { 109a34a8711Sopenharmony_ci StopRemoteListener(); 110a34a8711Sopenharmony_ci return false; 111a34a8711Sopenharmony_ci } 112a34a8711Sopenharmony_ci 113a34a8711Sopenharmony_ci DBINDER_LOGI(LOG_LABEL, "start remote listener ok"); 114a34a8711Sopenharmony_ci return true; 115a34a8711Sopenharmony_ci} 116a34a8711Sopenharmony_ci 117a34a8711Sopenharmony_cibool DBinderService::ReStartRemoteListener() 118a34a8711Sopenharmony_ci{ 119a34a8711Sopenharmony_ci if (remoteListener_ == nullptr) { 120a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "restart remote listener got null"); 121a34a8711Sopenharmony_ci return false; 122a34a8711Sopenharmony_ci } 123a34a8711Sopenharmony_ci if (remoteListener_->StartListener() != true) { 124a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "restart dbinder server failed"); 125a34a8711Sopenharmony_ci StopRemoteListener(); 126a34a8711Sopenharmony_ci return false; 127a34a8711Sopenharmony_ci } 128a34a8711Sopenharmony_ci return true; 129a34a8711Sopenharmony_ci} 130a34a8711Sopenharmony_ci 131a34a8711Sopenharmony_civoid DBinderService::StopRemoteListener() 132a34a8711Sopenharmony_ci{ 133a34a8711Sopenharmony_ci if (remoteListener_ != nullptr) { 134a34a8711Sopenharmony_ci remoteListener_->StopListener(); 135a34a8711Sopenharmony_ci remoteListener_ = nullptr; 136a34a8711Sopenharmony_ci } 137a34a8711Sopenharmony_ci} 138a34a8711Sopenharmony_ci 139a34a8711Sopenharmony_cistd::shared_ptr<DBinderRemoteListener> DBinderService::GetRemoteListener() 140a34a8711Sopenharmony_ci{ 141a34a8711Sopenharmony_ci if (remoteListener_ == nullptr && !StartRemoteListener()) { 142a34a8711Sopenharmony_ci return nullptr; 143a34a8711Sopenharmony_ci } 144a34a8711Sopenharmony_ci return remoteListener_; 145a34a8711Sopenharmony_ci} 146a34a8711Sopenharmony_ci 147a34a8711Sopenharmony_cisptr<DBinderService> DBinderService::GetInstance() 148a34a8711Sopenharmony_ci{ 149a34a8711Sopenharmony_ci if (instance_ == nullptr) { 150a34a8711Sopenharmony_ci std::lock_guard<std::mutex> lockGuard(instanceMutex_); 151a34a8711Sopenharmony_ci if (instance_ == nullptr) { 152a34a8711Sopenharmony_ci sptr<DBinderService> temp = new DBinderService(); 153a34a8711Sopenharmony_ci instance_ = temp; 154a34a8711Sopenharmony_ci } 155a34a8711Sopenharmony_ci } 156a34a8711Sopenharmony_ci return instance_; 157a34a8711Sopenharmony_ci} 158a34a8711Sopenharmony_ci 159a34a8711Sopenharmony_ciuint32_t DBinderService::GetSeqNumber() 160a34a8711Sopenharmony_ci{ 161a34a8711Sopenharmony_ci std::lock_guard<std::mutex> lockGuard(instanceMutex_); 162a34a8711Sopenharmony_ci if (seqNumber_ == std::numeric_limits<uint32_t>::max()) { 163a34a8711Sopenharmony_ci seqNumber_ = 0; 164a34a8711Sopenharmony_ci } 165a34a8711Sopenharmony_ci seqNumber_++; 166a34a8711Sopenharmony_ci return seqNumber_; 167a34a8711Sopenharmony_ci} 168a34a8711Sopenharmony_ci 169a34a8711Sopenharmony_cibool DBinderService::IsDeviceIdIllegal(const std::string &deviceID) 170a34a8711Sopenharmony_ci{ 171a34a8711Sopenharmony_ci if (deviceID.empty() || deviceID.length() > DEVICEID_LENGTH) { 172a34a8711Sopenharmony_ci return true; 173a34a8711Sopenharmony_ci } 174a34a8711Sopenharmony_ci return false; 175a34a8711Sopenharmony_ci} 176a34a8711Sopenharmony_ci 177a34a8711Sopenharmony_cibinder_uintptr_t DBinderService::AddStubByTag(binder_uintptr_t stub) 178a34a8711Sopenharmony_ci{ 179a34a8711Sopenharmony_ci std::lock_guard<std::mutex> lockGuard(handleEntryMutex_); 180a34a8711Sopenharmony_ci 181a34a8711Sopenharmony_ci // the same stub needs add stubNum to mapDBinderStubRegisters_, the previous corresponding stubNum will be returned. 182a34a8711Sopenharmony_ci for (auto iter = mapDBinderStubRegisters_.begin(); iter != mapDBinderStubRegisters_.end(); iter++) { 183a34a8711Sopenharmony_ci if (iter->second == stub) { 184a34a8711Sopenharmony_ci return iter->first; 185a34a8711Sopenharmony_ci } 186a34a8711Sopenharmony_ci } 187a34a8711Sopenharmony_ci binder_uintptr_t stubTag = stubTagNum_++; 188a34a8711Sopenharmony_ci auto result = mapDBinderStubRegisters_.insert( 189a34a8711Sopenharmony_ci std::pair<binder_uintptr_t, binder_uintptr_t>(stubTag, stub)); 190a34a8711Sopenharmony_ci if (result.second) { 191a34a8711Sopenharmony_ci return stubTag; 192a34a8711Sopenharmony_ci } else { 193a34a8711Sopenharmony_ci return 0; 194a34a8711Sopenharmony_ci } 195a34a8711Sopenharmony_ci} 196a34a8711Sopenharmony_ci 197a34a8711Sopenharmony_cibinder_uintptr_t DBinderService::QueryStubPtr(binder_uintptr_t stubTag) 198a34a8711Sopenharmony_ci{ 199a34a8711Sopenharmony_ci std::lock_guard<std::mutex> lockGuard(handleEntryMutex_); 200a34a8711Sopenharmony_ci 201a34a8711Sopenharmony_ci auto iter = mapDBinderStubRegisters_.find(stubTag); 202a34a8711Sopenharmony_ci if (iter != mapDBinderStubRegisters_.end()) { 203a34a8711Sopenharmony_ci return iter->second; 204a34a8711Sopenharmony_ci } 205a34a8711Sopenharmony_ci 206a34a8711Sopenharmony_ci return 0; 207a34a8711Sopenharmony_ci} 208a34a8711Sopenharmony_ci 209a34a8711Sopenharmony_cibool DBinderService::CheckBinderObject(const sptr<DBinderServiceStub> &stub, binder_uintptr_t stubPtr) 210a34a8711Sopenharmony_ci{ 211a34a8711Sopenharmony_ci if (stub == nullptr) { 212a34a8711Sopenharmony_ci return false; 213a34a8711Sopenharmony_ci } 214a34a8711Sopenharmony_ci 215a34a8711Sopenharmony_ci if (reinterpret_cast<binder_uintptr_t>(stub.GetRefPtr()) == stubPtr) { 216a34a8711Sopenharmony_ci DBINDER_LOGI(LOG_LABEL, "found registered stub"); 217a34a8711Sopenharmony_ci return true; 218a34a8711Sopenharmony_ci } 219a34a8711Sopenharmony_ci return false; 220a34a8711Sopenharmony_ci} 221a34a8711Sopenharmony_ci 222a34a8711Sopenharmony_cibool DBinderService::HasDBinderStub(binder_uintptr_t stubPtr) 223a34a8711Sopenharmony_ci{ 224a34a8711Sopenharmony_ci auto checkStub = [&stubPtr, this](const sptr<DBinderServiceStub> &stub) { 225a34a8711Sopenharmony_ci return CheckBinderObject(stub, stubPtr); 226a34a8711Sopenharmony_ci }; 227a34a8711Sopenharmony_ci 228a34a8711Sopenharmony_ci std::lock_guard<std::mutex> lockGuard(handleEntryMutex_); 229a34a8711Sopenharmony_ci auto it = std::find_if(DBinderStubRegisted_.begin(), DBinderStubRegisted_.end(), checkStub); 230a34a8711Sopenharmony_ci if (it != DBinderStubRegisted_.end()) { 231a34a8711Sopenharmony_ci return true; 232a34a8711Sopenharmony_ci } 233a34a8711Sopenharmony_ci return false; 234a34a8711Sopenharmony_ci} 235a34a8711Sopenharmony_ci 236a34a8711Sopenharmony_cibool DBinderService::IsSameStubObject(const sptr<DBinderServiceStub> &stub, const std::u16string &service, 237a34a8711Sopenharmony_ci const std::string &device) 238a34a8711Sopenharmony_ci{ 239a34a8711Sopenharmony_ci if (stub == nullptr) { 240a34a8711Sopenharmony_ci return false; 241a34a8711Sopenharmony_ci } 242a34a8711Sopenharmony_ci const std::string serviceStr8 = Str16ToStr8(service); 243a34a8711Sopenharmony_ci if (IsSameTextStr(stub->GetServiceName(), serviceStr8) && IsSameTextStr(stub->GetDeviceID(), device)) { 244a34a8711Sopenharmony_ci DBINDER_LOGI(LOG_LABEL, "found registered service, name:%{public}s device:%{public}s", 245a34a8711Sopenharmony_ci serviceStr8.c_str(), DBinderService::ConvertToSecureDeviceID(device).c_str()); 246a34a8711Sopenharmony_ci return true; 247a34a8711Sopenharmony_ci } 248a34a8711Sopenharmony_ci return false; 249a34a8711Sopenharmony_ci} 250a34a8711Sopenharmony_ci 251a34a8711Sopenharmony_cisptr<DBinderServiceStub> DBinderService::FindDBinderStub(const std::u16string &service, const std::string &device) 252a34a8711Sopenharmony_ci{ 253a34a8711Sopenharmony_ci auto checkStub = [&service, &device, this](const sptr<DBinderServiceStub> &stub) { 254a34a8711Sopenharmony_ci return IsSameStubObject(stub, service, device); 255a34a8711Sopenharmony_ci }; 256a34a8711Sopenharmony_ci 257a34a8711Sopenharmony_ci std::lock_guard<std::mutex> lockGuard(handleEntryMutex_); 258a34a8711Sopenharmony_ci auto it = std::find_if(DBinderStubRegisted_.begin(), DBinderStubRegisted_.end(), checkStub); 259a34a8711Sopenharmony_ci if (it == DBinderStubRegisted_.end()) { 260a34a8711Sopenharmony_ci DBINDER_LOGW(LOG_LABEL, "not found, service:%{public}s device:%{public}s", Str16ToStr8(service).c_str(), 261a34a8711Sopenharmony_ci DBinderService::ConvertToSecureDeviceID(device).c_str()); 262a34a8711Sopenharmony_ci return nullptr; 263a34a8711Sopenharmony_ci } 264a34a8711Sopenharmony_ci DBINDER_LOGD(LOG_LABEL, "found, service:%{public}s device:%{public}s", Str16ToStr8(service).c_str(), 265a34a8711Sopenharmony_ci DBinderService::ConvertToSecureDeviceID(device).c_str()); 266a34a8711Sopenharmony_ci return (*it); 267a34a8711Sopenharmony_ci} 268a34a8711Sopenharmony_ci 269a34a8711Sopenharmony_cibool DBinderService::DeleteDBinderStub(const std::u16string &service, const std::string &device) 270a34a8711Sopenharmony_ci{ 271a34a8711Sopenharmony_ci auto checkStub = [&service, &device, this](const sptr<DBinderServiceStub> &stub) { 272a34a8711Sopenharmony_ci return IsSameStubObject(stub, service, device); 273a34a8711Sopenharmony_ci }; 274a34a8711Sopenharmony_ci 275a34a8711Sopenharmony_ci std::lock_guard<std::mutex> lockGuard(handleEntryMutex_); 276a34a8711Sopenharmony_ci auto it = std::find_if(DBinderStubRegisted_.begin(), DBinderStubRegisted_.end(), checkStub); 277a34a8711Sopenharmony_ci if (it == DBinderStubRegisted_.end()) { 278a34a8711Sopenharmony_ci DBINDER_LOGW(LOG_LABEL, "not found, service:%{public}s device:%{public}s", Str16ToStr8(service).c_str(), 279a34a8711Sopenharmony_ci DBinderService::ConvertToSecureDeviceID(device).c_str()); 280a34a8711Sopenharmony_ci return false; 281a34a8711Sopenharmony_ci } 282a34a8711Sopenharmony_ci 283a34a8711Sopenharmony_ci for (auto mapIt = mapDBinderStubRegisters_.begin(); mapIt != mapDBinderStubRegisters_.end();) { 284a34a8711Sopenharmony_ci if (mapIt->second == reinterpret_cast<binder_uintptr_t>((*it).GetRefPtr())) { 285a34a8711Sopenharmony_ci mapIt = mapDBinderStubRegisters_.erase(mapIt); 286a34a8711Sopenharmony_ci } else { 287a34a8711Sopenharmony_ci ++mapIt; 288a34a8711Sopenharmony_ci } 289a34a8711Sopenharmony_ci } 290a34a8711Sopenharmony_ci DBinderStubRegisted_.erase(it); 291a34a8711Sopenharmony_ci DBINDER_LOGI(LOG_LABEL, "succ, service:%{public}s device:%{public}s", Str16ToStr8(service).c_str(), 292a34a8711Sopenharmony_ci DBinderService::ConvertToSecureDeviceID(device).c_str()); 293a34a8711Sopenharmony_ci return true; 294a34a8711Sopenharmony_ci} 295a34a8711Sopenharmony_ci 296a34a8711Sopenharmony_cisptr<DBinderServiceStub> DBinderService::FindOrNewDBinderStub(const std::u16string &service, const std::string &device, 297a34a8711Sopenharmony_ci binder_uintptr_t binderObject) 298a34a8711Sopenharmony_ci{ 299a34a8711Sopenharmony_ci auto checkStub = [&service, &device, this](const sptr<DBinderServiceStub> &stub) { 300a34a8711Sopenharmony_ci return IsSameStubObject(stub, service, device); 301a34a8711Sopenharmony_ci }; 302a34a8711Sopenharmony_ci 303a34a8711Sopenharmony_ci std::lock_guard<std::mutex> lockGuard(handleEntryMutex_); 304a34a8711Sopenharmony_ci const std::string serviceStr8 = Str16ToStr8(service); 305a34a8711Sopenharmony_ci auto it = std::find_if(DBinderStubRegisted_.begin(), DBinderStubRegisted_.end(), checkStub); 306a34a8711Sopenharmony_ci if (it != DBinderStubRegisted_.end()) { 307a34a8711Sopenharmony_ci DBINDER_LOGD(LOG_LABEL, "found, service:%{public}s device:%{public}s", serviceStr8.c_str(), 308a34a8711Sopenharmony_ci DBinderService::ConvertToSecureDeviceID(device).c_str()); 309a34a8711Sopenharmony_ci return (*it); 310a34a8711Sopenharmony_ci } 311a34a8711Sopenharmony_ci 312a34a8711Sopenharmony_ci sptr<DBinderServiceStub> dBinderServiceStub = new DBinderServiceStub(serviceStr8, device, binderObject); 313a34a8711Sopenharmony_ci DBinderStubRegisted_.push_back(dBinderServiceStub); 314a34a8711Sopenharmony_ci DBINDER_LOGD(LOG_LABEL, "create, service:%{public}s device:%{public}s", serviceStr8.c_str(), 315a34a8711Sopenharmony_ci DBinderService::ConvertToSecureDeviceID(device).c_str()); 316a34a8711Sopenharmony_ci return dBinderServiceStub; 317a34a8711Sopenharmony_ci} 318a34a8711Sopenharmony_ci 319a34a8711Sopenharmony_cisptr<DBinderServiceStub> DBinderService::MakeRemoteBinder(const std::u16string &serviceName, 320a34a8711Sopenharmony_ci const std::string &deviceID, int32_t binderObject, uint32_t pid, uint32_t uid) 321a34a8711Sopenharmony_ci{ 322a34a8711Sopenharmony_ci if (IsDeviceIdIllegal(deviceID) || serviceName.length() == 0) { 323a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "para is wrong, device length:%{public}zu, service length:%{public}zu", 324a34a8711Sopenharmony_ci deviceID.length(), serviceName.length()); 325a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_ERR_INVALID_DATA, __FUNCTION__); 326a34a8711Sopenharmony_ci return nullptr; 327a34a8711Sopenharmony_ci } 328a34a8711Sopenharmony_ci const std::string serviceNameStr8 = Str16ToStr8(serviceName); 329a34a8711Sopenharmony_ci DBINDER_LOGI(LOG_LABEL, "service:%{public}s device:%{public}s", serviceNameStr8.c_str(), 330a34a8711Sopenharmony_ci DBinderService::ConvertToSecureDeviceID(deviceID).c_str()); 331a34a8711Sopenharmony_ci DfxReportDeviceEvent(DbinderErrorCode::RPC_DRIVER, DbinderErrorCode::IPC_RESULT_IDLE, 332a34a8711Sopenharmony_ci DBinderService::ConvertToSecureDeviceID(deviceID).c_str(), __FUNCTION__); 333a34a8711Sopenharmony_ci 334a34a8711Sopenharmony_ci sptr<DBinderServiceStub> dBinderServiceStub = FindOrNewDBinderStub(serviceName, deviceID, 335a34a8711Sopenharmony_ci static_cast<binder_uintptr_t>(binderObject)); 336a34a8711Sopenharmony_ci if (dBinderServiceStub == nullptr) { 337a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "FindOrNewDBinderStub fail, service:%{public}s", serviceNameStr8.c_str()); 338a34a8711Sopenharmony_ci return nullptr; 339a34a8711Sopenharmony_ci } 340a34a8711Sopenharmony_ci 341a34a8711Sopenharmony_ci /* if not found dBinderServiceStub, should send msg to toDeviceID 342a34a8711Sopenharmony_ci * to invoker socket thread and add authentication info for create softbus session 343a34a8711Sopenharmony_ci */ 344a34a8711Sopenharmony_ci int retryTimes = 0; 345a34a8711Sopenharmony_ci int32_t ret = -1; 346a34a8711Sopenharmony_ci do { 347a34a8711Sopenharmony_ci ret = InvokerRemoteDBinder(dBinderServiceStub, GetSeqNumber(), pid, uid); 348a34a8711Sopenharmony_ci retryTimes++; 349a34a8711Sopenharmony_ci } while (ret == WAIT_REPLY_TIMEOUT && (retryTimes < RETRY_TIMES)); 350a34a8711Sopenharmony_ci 351a34a8711Sopenharmony_ci if (ret != DBINDER_OK) { 352a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "fail to invoke service, service:%{public}s device:%{public}s " 353a34a8711Sopenharmony_ci "DBinderServiceStub refcount:%{public}d", 354a34a8711Sopenharmony_ci serviceNameStr8.c_str(), DBinderService::ConvertToSecureDeviceID(deviceID).c_str(), 355a34a8711Sopenharmony_ci dBinderServiceStub->GetSptrRefCount()); 356a34a8711Sopenharmony_ci if (dBinderServiceStub->GetSptrRefCount() <= DBINDER_STUB_REF_COUNT) { 357a34a8711Sopenharmony_ci /* invoke fail, delete dbinder stub info */ 358a34a8711Sopenharmony_ci (void)DeleteDBinderStub(serviceName, deviceID); 359a34a8711Sopenharmony_ci (void)DetachSessionObject(reinterpret_cast<binder_uintptr_t>(dBinderServiceStub.GetRefPtr())); 360a34a8711Sopenharmony_ci } 361a34a8711Sopenharmony_ci return nullptr; 362a34a8711Sopenharmony_ci } 363a34a8711Sopenharmony_ci 364a34a8711Sopenharmony_ci return dBinderServiceStub; 365a34a8711Sopenharmony_ci} 366a34a8711Sopenharmony_ci 367a34a8711Sopenharmony_cibool DBinderService::CheckDeviceIDsInvalid(const std::string &deviceID, const std::string &localDevID) 368a34a8711Sopenharmony_ci{ 369a34a8711Sopenharmony_ci if (IsDeviceIdIllegal(deviceID) || IsDeviceIdIllegal(localDevID)) { 370a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "wrong device id length, remote:%{public}zu local:%{public}zu", 371a34a8711Sopenharmony_ci deviceID.length(), localDevID.length()); 372a34a8711Sopenharmony_ci return true; 373a34a8711Sopenharmony_ci } 374a34a8711Sopenharmony_ci return false; 375a34a8711Sopenharmony_ci} 376a34a8711Sopenharmony_ci 377a34a8711Sopenharmony_cibool DBinderService::CopyDeviceIDsToMessage(std::shared_ptr<struct DHandleEntryTxRx> message, 378a34a8711Sopenharmony_ci const std::string &localDevID, const std::string &deviceID) 379a34a8711Sopenharmony_ci{ 380a34a8711Sopenharmony_ci if (memcpy_s(message->deviceIdInfo.fromDeviceId, DEVICEID_LENGTH, localDevID.data(), localDevID.length()) != 0 || 381a34a8711Sopenharmony_ci memcpy_s(message->deviceIdInfo.toDeviceId, DEVICEID_LENGTH, deviceID.data(), deviceID.length()) != 0) { 382a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "fail to copy memory, service:%{public}" PRIu64" seq:%{public}u", 383a34a8711Sopenharmony_ci message->stubIndex, message->seqNumber); 384a34a8711Sopenharmony_ci return false; 385a34a8711Sopenharmony_ci } 386a34a8711Sopenharmony_ci message->deviceIdInfo.fromDeviceId[localDevID.length()] = '\0'; 387a34a8711Sopenharmony_ci message->deviceIdInfo.toDeviceId[deviceID.length()] = '\0'; 388a34a8711Sopenharmony_ci return true; 389a34a8711Sopenharmony_ci} 390a34a8711Sopenharmony_ci 391a34a8711Sopenharmony_cistd::shared_ptr<struct DHandleEntryTxRx> DBinderService::CreateMessage(const sptr<DBinderServiceStub> &stub, 392a34a8711Sopenharmony_ci uint32_t seqNumber, uint32_t pid, uint32_t uid) 393a34a8711Sopenharmony_ci{ 394a34a8711Sopenharmony_ci auto message = std::make_shared<struct DHandleEntryTxRx>(); 395a34a8711Sopenharmony_ci if (message == nullptr) { 396a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "new DHandleEntryTxRx fail"); 397a34a8711Sopenharmony_ci return nullptr; 398a34a8711Sopenharmony_ci } 399a34a8711Sopenharmony_ci 400a34a8711Sopenharmony_ci message->head.len = sizeof(DHandleEntryTxRx); 401a34a8711Sopenharmony_ci message->head.version = RPC_TOKENID_SUPPORT_VERSION; 402a34a8711Sopenharmony_ci message->dBinderCode = MESSAGE_AS_INVOKER; 403a34a8711Sopenharmony_ci message->transType = GetRemoteTransType(); 404a34a8711Sopenharmony_ci message->fromPort = 0; 405a34a8711Sopenharmony_ci message->toPort = 0; 406a34a8711Sopenharmony_ci message->stubIndex = static_cast<uint64_t>(std::atoi(stub->GetServiceName().c_str())); 407a34a8711Sopenharmony_ci message->seqNumber = seqNumber; 408a34a8711Sopenharmony_ci message->binderObject = stub->GetBinderObject(); 409a34a8711Sopenharmony_ci message->stub = AddStubByTag(reinterpret_cast<binder_uintptr_t>(stub.GetRefPtr())); 410a34a8711Sopenharmony_ci message->deviceIdInfo.tokenId = IPCSkeleton::GetCallingTokenID(); 411a34a8711Sopenharmony_ci message->pid = pid; 412a34a8711Sopenharmony_ci message->uid = uid; 413a34a8711Sopenharmony_ci 414a34a8711Sopenharmony_ci return message; 415a34a8711Sopenharmony_ci} 416a34a8711Sopenharmony_ci 417a34a8711Sopenharmony_cibool DBinderService::SendEntryToRemote(const sptr<DBinderServiceStub> stub, uint32_t seqNumber, uint32_t pid, 418a34a8711Sopenharmony_ci uint32_t uid) 419a34a8711Sopenharmony_ci{ 420a34a8711Sopenharmony_ci const std::string deviceID = stub->GetDeviceID(); 421a34a8711Sopenharmony_ci const std::string localDevID = GetLocalDeviceID(); 422a34a8711Sopenharmony_ci if (CheckDeviceIDsInvalid(deviceID, localDevID)) { 423a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_ERR_INVALID_DATA, __FUNCTION__); 424a34a8711Sopenharmony_ci return false; 425a34a8711Sopenharmony_ci } 426a34a8711Sopenharmony_ci 427a34a8711Sopenharmony_ci auto message = CreateMessage(stub, seqNumber, pid, uid); 428a34a8711Sopenharmony_ci if (message == nullptr) { 429a34a8711Sopenharmony_ci return false; 430a34a8711Sopenharmony_ci } 431a34a8711Sopenharmony_ci 432a34a8711Sopenharmony_ci if (!CopyDeviceIDsToMessage(message, localDevID, deviceID)) { 433a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_ERR_MEMCPY_DATA, __FUNCTION__); 434a34a8711Sopenharmony_ci return false; 435a34a8711Sopenharmony_ci } 436a34a8711Sopenharmony_ci 437a34a8711Sopenharmony_ci DBINDER_LOGI(LOG_LABEL, "pid:%{public}u uid:%{public}u seq:%{public}u stub:%{public}llu" 438a34a8711Sopenharmony_ci " tokenId:%{public}u", message->pid, message->uid, message->seqNumber, 439a34a8711Sopenharmony_ci (message->stub & BINDER_MASK), message->deviceIdInfo.tokenId); 440a34a8711Sopenharmony_ci std::shared_ptr<DBinderRemoteListener> remoteListener = GetRemoteListener(); 441a34a8711Sopenharmony_ci if (remoteListener == nullptr) { 442a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "remoteListener is null, service:%{public}" PRIu64 " seq:%{public}u", 443a34a8711Sopenharmony_ci message->stubIndex, message->seqNumber); 444a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_GET_REMOTE_LISTENER_FAIL, __FUNCTION__); 445a34a8711Sopenharmony_ci return false; 446a34a8711Sopenharmony_ci } 447a34a8711Sopenharmony_ci bool result = remoteListener->SendDataToRemote(deviceID, message.get()); 448a34a8711Sopenharmony_ci if (result != true) { 449a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "SendDataToRemote failed, service:%{public}" PRIu64" seq:%{public}u", 450a34a8711Sopenharmony_ci message->stubIndex, message->seqNumber); 451a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_SEND_DATA_TO_REMOTE_FAIL, __FUNCTION__); 452a34a8711Sopenharmony_ci return false; 453a34a8711Sopenharmony_ci } 454a34a8711Sopenharmony_ci return true; 455a34a8711Sopenharmony_ci} 456a34a8711Sopenharmony_ci 457a34a8711Sopenharmony_ciint32_t DBinderService::InvokerRemoteDBinder(const sptr<DBinderServiceStub> stub, uint32_t seqNumber, 458a34a8711Sopenharmony_ci uint32_t pid, uint32_t uid) 459a34a8711Sopenharmony_ci{ 460a34a8711Sopenharmony_ci if (stub == nullptr) { 461a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "stub is nullptr"); 462a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_ERR_INVALID_DATA, __FUNCTION__); 463a34a8711Sopenharmony_ci return STUB_INVALID; 464a34a8711Sopenharmony_ci } 465a34a8711Sopenharmony_ci 466a34a8711Sopenharmony_ci bool result = SendEntryToRemote(stub, seqNumber, pid, uid); 467a34a8711Sopenharmony_ci if (!result) { 468a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "SendEntryToRemote fail, seq:%{public}u", seqNumber); 469a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_SEND_ENTRY_TO_REMOTE_FAIL, __FUNCTION__); 470a34a8711Sopenharmony_ci return SEND_MESSAGE_FAILED; 471a34a8711Sopenharmony_ci } 472a34a8711Sopenharmony_ci 473a34a8711Sopenharmony_ci /* pend to wait reply */ 474a34a8711Sopenharmony_ci std::shared_ptr<struct ThreadLockInfo> threadLockInfo = std::make_shared<struct ThreadLockInfo>(); 475a34a8711Sopenharmony_ci result = AttachThreadLockInfo(seqNumber, stub->GetDeviceID(), threadLockInfo); 476a34a8711Sopenharmony_ci if (result != true) { 477a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "AttachThreadLockInfo fail, seq:%{public}u", seqNumber); 478a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_ATTACH_THREADLOCK_FAIL, __FUNCTION__); 479a34a8711Sopenharmony_ci return MAKE_THREADLOCK_FAILED; 480a34a8711Sopenharmony_ci } 481a34a8711Sopenharmony_ci 482a34a8711Sopenharmony_ci std::unique_lock<std::mutex> lock(threadLockInfo->mutex); 483a34a8711Sopenharmony_ci if (threadLockInfo->condition.wait_for(lock, std::chrono::seconds(WAIT_FOR_REPLY_MAX_SEC), 484a34a8711Sopenharmony_ci [&threadLockInfo] { return threadLockInfo->ready; }) == false) { 485a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "get remote data timeout or ssession is closed, seq:%{public}u", seqNumber); 486a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_WAIT_REPLY_TIMEOUT, __FUNCTION__); 487a34a8711Sopenharmony_ci DetachThreadLockInfo(seqNumber); 488a34a8711Sopenharmony_ci threadLockInfo->ready = false; 489a34a8711Sopenharmony_ci return WAIT_REPLY_TIMEOUT; 490a34a8711Sopenharmony_ci } 491a34a8711Sopenharmony_ci /* if can not find session, means invoke failed or nothing in OnRemoteReplyMessage() */ 492a34a8711Sopenharmony_ci auto session = QuerySessionObject(reinterpret_cast<binder_uintptr_t>(stub.GetRefPtr())); 493a34a8711Sopenharmony_ci if (session == nullptr) { 494a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "client find session is null, seq:%{public}u", seqNumber); 495a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_QUERY_REPLY_SESSION_FAIL, __FUNCTION__); 496a34a8711Sopenharmony_ci return QUERY_REPLY_SESSION_FAILED; 497a34a8711Sopenharmony_ci } 498a34a8711Sopenharmony_ci return DBINDER_OK; 499a34a8711Sopenharmony_ci} 500a34a8711Sopenharmony_ci 501a34a8711Sopenharmony_cibool DBinderService::CheckSystemAbilityId(int32_t systemAbilityId) 502a34a8711Sopenharmony_ci{ 503a34a8711Sopenharmony_ci return systemAbilityId >= FIRST_SYS_ABILITY_ID && systemAbilityId <= LAST_SYS_ABILITY_ID; 504a34a8711Sopenharmony_ci} 505a34a8711Sopenharmony_ci 506a34a8711Sopenharmony_ciuint16_t DBinderService::AllocFreeSocketPort() 507a34a8711Sopenharmony_ci{ 508a34a8711Sopenharmony_ci /* alloc port by system */ 509a34a8711Sopenharmony_ci return 0; 510a34a8711Sopenharmony_ci} 511a34a8711Sopenharmony_ci 512a34a8711Sopenharmony_cibool DBinderService::IsSameLoadSaItem(const std::string& srcNetworkId, int32_t systemAbilityId, 513a34a8711Sopenharmony_ci std::shared_ptr<DHandleEntryTxRx> loadSaItem) 514a34a8711Sopenharmony_ci{ 515a34a8711Sopenharmony_ci if (static_cast<int32_t>(loadSaItem->stubIndex) == systemAbilityId && 516a34a8711Sopenharmony_ci loadSaItem->deviceIdInfo.fromDeviceId == srcNetworkId) { 517a34a8711Sopenharmony_ci DBINDER_LOGI(LOG_LABEL, "match succeed"); 518a34a8711Sopenharmony_ci return true; 519a34a8711Sopenharmony_ci } 520a34a8711Sopenharmony_ci return false; 521a34a8711Sopenharmony_ci} 522a34a8711Sopenharmony_ci 523a34a8711Sopenharmony_cistd::shared_ptr<DHandleEntryTxRx> DBinderService::PopLoadSaItem(const std::string& srcNetworkId, 524a34a8711Sopenharmony_ci int32_t systemAbilityId) 525a34a8711Sopenharmony_ci{ 526a34a8711Sopenharmony_ci auto checkSaItem = [srcNetworkId, systemAbilityId, this](std::shared_ptr<DHandleEntryTxRx> loadSaItem) { 527a34a8711Sopenharmony_ci return IsSameLoadSaItem(srcNetworkId, systemAbilityId, loadSaItem); 528a34a8711Sopenharmony_ci }; 529a34a8711Sopenharmony_ci 530a34a8711Sopenharmony_ci std::lock_guard<std::shared_mutex> lockGuard(loadSaMutex_); 531a34a8711Sopenharmony_ci auto it = std::find_if(loadSaReply_.begin(), loadSaReply_.end(), checkSaItem); 532a34a8711Sopenharmony_ci if (it == loadSaReply_.end()) { 533a34a8711Sopenharmony_ci DBINDER_LOGI(LOG_LABEL, "no msg for saId:%{public}d, deviceId:%{public}s", 534a34a8711Sopenharmony_ci systemAbilityId, DBinderService::ConvertToSecureDeviceID(srcNetworkId).c_str()); 535a34a8711Sopenharmony_ci return nullptr; 536a34a8711Sopenharmony_ci } 537a34a8711Sopenharmony_ci std::shared_ptr<DHandleEntryTxRx> replymsg = (*it); 538a34a8711Sopenharmony_ci it = loadSaReply_.erase(it); 539a34a8711Sopenharmony_ci return replymsg; 540a34a8711Sopenharmony_ci} 541a34a8711Sopenharmony_ci 542a34a8711Sopenharmony_civoid DBinderService::LoadSystemAbilityComplete(const std::string& srcNetworkId, int32_t systemAbilityId, 543a34a8711Sopenharmony_ci const sptr<IRemoteObject>& remoteObject) 544a34a8711Sopenharmony_ci{ 545a34a8711Sopenharmony_ci while (true) { 546a34a8711Sopenharmony_ci std::shared_ptr<struct DHandleEntryTxRx> replyMessage = PopLoadSaItem(srcNetworkId, systemAbilityId); 547a34a8711Sopenharmony_ci if (replyMessage == nullptr) { 548a34a8711Sopenharmony_ci break; 549a34a8711Sopenharmony_ci } 550a34a8711Sopenharmony_ci if (remoteObject == nullptr) { 551a34a8711Sopenharmony_ci SendReplyMessageToRemote(MESSAGE_AS_REMOTE_ERROR, SA_NOT_FOUND, replyMessage); 552a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "GetSystemAbility from samgr error, saId:%{public}d", systemAbilityId); 553a34a8711Sopenharmony_ci continue; 554a34a8711Sopenharmony_ci } 555a34a8711Sopenharmony_ci binder_uintptr_t binderObject = replyMessage->binderObject; 556a34a8711Sopenharmony_ci IPCObjectProxy *saProxy = reinterpret_cast<IPCObjectProxy *>(remoteObject.GetRefPtr()); 557a34a8711Sopenharmony_ci if (QueryProxyObject(binderObject) == nullptr) { 558a34a8711Sopenharmony_ci /* When the stub object dies, you need to delete the corresponding busName information */ 559a34a8711Sopenharmony_ci sptr<IRemoteObject::DeathRecipient> death(new DbinderSaDeathRecipient(binderObject)); 560a34a8711Sopenharmony_ci if (!saProxy->AddDeathRecipient(death)) { 561a34a8711Sopenharmony_ci SendReplyMessageToRemote(MESSAGE_AS_REMOTE_ERROR, SA_NOT_FOUND, replyMessage); 562a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "fail to add death recipient"); 563a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_ADD_DEATH_RECIPIENT_FAIL, __FUNCTION__); 564a34a8711Sopenharmony_ci continue; 565a34a8711Sopenharmony_ci } 566a34a8711Sopenharmony_ci if (!AttachProxyObject(remoteObject, binderObject)) { 567a34a8711Sopenharmony_ci DBINDER_LOGW(LOG_LABEL, "attach proxy object is already existed"); 568a34a8711Sopenharmony_ci } 569a34a8711Sopenharmony_ci } 570a34a8711Sopenharmony_ci std::string deviceId = replyMessage->deviceIdInfo.fromDeviceId; 571a34a8711Sopenharmony_ci if (replyMessage->transType != IRemoteObject::DATABUS_TYPE) { 572a34a8711Sopenharmony_ci SendReplyMessageToRemote(MESSAGE_AS_REMOTE_ERROR, SA_INVOKE_FAILED, replyMessage); 573a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "Invalid Message Type"); 574a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_ERR_INVALID_DATA, __FUNCTION__); 575a34a8711Sopenharmony_ci } else { 576a34a8711Sopenharmony_ci // peer device rpc version == 1, not support thokenId and message->deviceIdInfo.tokenId is random value 577a34a8711Sopenharmony_ci uint32_t tokenId = (replyMessage->head.version < RPC_TOKENID_SUPPORT_VERSION) ? 578a34a8711Sopenharmony_ci 0 : replyMessage->deviceIdInfo.tokenId; 579a34a8711Sopenharmony_ci uint32_t result = OnRemoteInvokerDataBusMessage(saProxy, replyMessage, deviceId, 580a34a8711Sopenharmony_ci replyMessage->pid, replyMessage->uid, tokenId); 581a34a8711Sopenharmony_ci if (result != 0) { 582a34a8711Sopenharmony_ci SendReplyMessageToRemote(MESSAGE_AS_REMOTE_ERROR, result, replyMessage); 583a34a8711Sopenharmony_ci continue; 584a34a8711Sopenharmony_ci } 585a34a8711Sopenharmony_ci SendReplyMessageToRemote(MESSAGE_AS_REPLY, 0, replyMessage); 586a34a8711Sopenharmony_ci } 587a34a8711Sopenharmony_ci } 588a34a8711Sopenharmony_ci DBINDER_LOGI(LOG_LABEL, "LoadSystemAbility complete"); 589a34a8711Sopenharmony_ci} 590a34a8711Sopenharmony_ci 591a34a8711Sopenharmony_civoid DBinderService::SendReplyMessageToRemote(uint32_t dBinderCode, uint32_t reason, 592a34a8711Sopenharmony_ci std::shared_ptr<struct DHandleEntryTxRx> replyMessage) 593a34a8711Sopenharmony_ci{ 594a34a8711Sopenharmony_ci std::shared_ptr<DBinderRemoteListener> remoteListener = GetRemoteListener(); 595a34a8711Sopenharmony_ci if (remoteListener == nullptr) { 596a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "remoteListener is null"); 597a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_GET_REMOTE_LISTENER_FAIL, __FUNCTION__); 598a34a8711Sopenharmony_ci return; 599a34a8711Sopenharmony_ci } 600a34a8711Sopenharmony_ci replyMessage->dBinderCode = dBinderCode; 601a34a8711Sopenharmony_ci if (dBinderCode == MESSAGE_AS_REMOTE_ERROR) { 602a34a8711Sopenharmony_ci replyMessage->transType = reason; // reuse transType send back error code 603a34a8711Sopenharmony_ci } 604a34a8711Sopenharmony_ci if (!remoteListener->SendDataReply(replyMessage->deviceIdInfo.fromDeviceId, replyMessage.get())) { 605a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "fail to send data from server DBS to client DBS"); 606a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_SEND_DATA_REPLAY_FAIL, __FUNCTION__); 607a34a8711Sopenharmony_ci } 608a34a8711Sopenharmony_ci} 609a34a8711Sopenharmony_ci 610a34a8711Sopenharmony_cibool DBinderService::CheckAndAmendSaId(std::shared_ptr<struct DHandleEntryTxRx> message) 611a34a8711Sopenharmony_ci{ 612a34a8711Sopenharmony_ci bool ret = true; 613a34a8711Sopenharmony_ci int32_t stubIndex = static_cast<int32_t>(message->stubIndex); 614a34a8711Sopenharmony_ci int32_t binderObject = static_cast<int32_t>(message->binderObject); 615a34a8711Sopenharmony_ci bool stubIndexVaild = CheckSystemAbilityId(stubIndex); 616a34a8711Sopenharmony_ci bool binderObjectVaild = CheckSystemAbilityId(binderObject); 617a34a8711Sopenharmony_ci if (stubIndexVaild && binderObjectVaild) { 618a34a8711Sopenharmony_ci if (stubIndex != binderObject) { 619a34a8711Sopenharmony_ci DBINDER_LOGW(LOG_LABEL, "stubIndex(%{public}d) != binderObject(%{public}d), update said:%{public}d", 620a34a8711Sopenharmony_ci stubIndex, binderObject, stubIndex); 621a34a8711Sopenharmony_ci message->binderObject = message->stubIndex; 622a34a8711Sopenharmony_ci } 623a34a8711Sopenharmony_ci } else if (stubIndexVaild && !binderObjectVaild) { 624a34a8711Sopenharmony_ci DBINDER_LOGI(LOG_LABEL, "update said, replace binderObject:%{public}d with stubIndex:%{public}d", 625a34a8711Sopenharmony_ci binderObject, stubIndex); 626a34a8711Sopenharmony_ci message->binderObject = message->stubIndex; 627a34a8711Sopenharmony_ci } else if (!stubIndexVaild && binderObjectVaild) { 628a34a8711Sopenharmony_ci DBINDER_LOGI(LOG_LABEL, "update said, replace stubIndex:%{public}d with binderObject:%{public}d", 629a34a8711Sopenharmony_ci stubIndex, binderObject); 630a34a8711Sopenharmony_ci message->stubIndex = message->binderObject; 631a34a8711Sopenharmony_ci } else { 632a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "invalid said, stubIndex:%{public}d binderObject:%{public}d", 633a34a8711Sopenharmony_ci stubIndex, binderObject); 634a34a8711Sopenharmony_ci ret = false; 635a34a8711Sopenharmony_ci } 636a34a8711Sopenharmony_ci return ret; 637a34a8711Sopenharmony_ci} 638a34a8711Sopenharmony_ci 639a34a8711Sopenharmony_cibool DBinderService::OnRemoteInvokerMessage(std::shared_ptr<struct DHandleEntryTxRx> message) 640a34a8711Sopenharmony_ci{ 641a34a8711Sopenharmony_ci if (!CheckAndAmendSaId(message)) { 642a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_INVALID_SAID, __FUNCTION__); 643a34a8711Sopenharmony_ci SendReplyMessageToRemote(MESSAGE_AS_REMOTE_ERROR, SAID_INVALID_ERR, message); 644a34a8711Sopenharmony_ci return false; 645a34a8711Sopenharmony_ci } 646a34a8711Sopenharmony_ci 647a34a8711Sopenharmony_ci DBINDER_LOGI(LOG_LABEL, 648a34a8711Sopenharmony_ci "invoke business service:%{public}d seq:%{public}u stub:%{public}llu tokenId:%{public}u", 649a34a8711Sopenharmony_ci static_cast<int32_t>(message->stubIndex), message->seqNumber, 650a34a8711Sopenharmony_ci (message->stub & BINDER_MASK), message->deviceIdInfo.tokenId); 651a34a8711Sopenharmony_ci if (!dbinderCallback_->IsDistributedSystemAbility(message->binderObject)) { 652a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "SA:%{public}llu not have distributed capability.", message->binderObject); 653a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_NOT_DISTEIBUTED_SA, __FUNCTION__); 654a34a8711Sopenharmony_ci SendReplyMessageToRemote(MESSAGE_AS_REMOTE_ERROR, SA_NOT_DISTRUBUTED_ERR, message); 655a34a8711Sopenharmony_ci return false; 656a34a8711Sopenharmony_ci } 657a34a8711Sopenharmony_ci 658a34a8711Sopenharmony_ci std::shared_ptr<DHandleEntryTxRx> replyMessage = message; 659a34a8711Sopenharmony_ci { 660a34a8711Sopenharmony_ci std::lock_guard<std::shared_mutex> lockGuard(loadSaMutex_); 661a34a8711Sopenharmony_ci loadSaReply_.push_back(replyMessage); 662a34a8711Sopenharmony_ci } 663a34a8711Sopenharmony_ci bool isSaAvailable = dbinderCallback_->LoadSystemAbilityFromRemote(replyMessage->deviceIdInfo.fromDeviceId, 664a34a8711Sopenharmony_ci static_cast<int32_t>(replyMessage->stubIndex)); 665a34a8711Sopenharmony_ci if (!isSaAvailable) { 666a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "fail to call the system ability:%{public}d", 667a34a8711Sopenharmony_ci static_cast<int32_t>(replyMessage->stubIndex)); 668a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_CALL_SYSTEM_ABILITY_FAIL, __FUNCTION__); 669a34a8711Sopenharmony_ci PopLoadSaItem(replyMessage->deviceIdInfo.fromDeviceId, static_cast<int32_t>(replyMessage->stubIndex)); 670a34a8711Sopenharmony_ci SendReplyMessageToRemote(MESSAGE_AS_REMOTE_ERROR, SA_NOT_AVAILABLE, replyMessage); 671a34a8711Sopenharmony_ci return false; 672a34a8711Sopenharmony_ci } 673a34a8711Sopenharmony_ci 674a34a8711Sopenharmony_ci return true; 675a34a8711Sopenharmony_ci} 676a34a8711Sopenharmony_ci 677a34a8711Sopenharmony_cistd::string DBinderService::GetDatabusNameByProxy(IPCObjectProxy *proxy) 678a34a8711Sopenharmony_ci{ 679a34a8711Sopenharmony_ci if (proxy == nullptr) { 680a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "proxy can not be null"); 681a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_ERR_INVALID_DATA, __FUNCTION__); 682a34a8711Sopenharmony_ci return ""; 683a34a8711Sopenharmony_ci } 684a34a8711Sopenharmony_ci std::string sessionName = proxy->GetSessionName(); 685a34a8711Sopenharmony_ci if (sessionName.empty()) { 686a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "grand session name failed"); 687a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_GRT_SESSION_NAME_FAIL, __FUNCTION__); 688a34a8711Sopenharmony_ci return ""; 689a34a8711Sopenharmony_ci } 690a34a8711Sopenharmony_ci DBINDER_LOGD(LOG_LABEL, "succ, handle:%{public}d sessionName:%{public}s", 691a34a8711Sopenharmony_ci proxy->GetHandle(), sessionName.c_str()); 692a34a8711Sopenharmony_ci return sessionName; 693a34a8711Sopenharmony_ci} 694a34a8711Sopenharmony_ci 695a34a8711Sopenharmony_cistd::string DBinderService::CreateDatabusName(int uid, int pid) 696a34a8711Sopenharmony_ci{ 697a34a8711Sopenharmony_ci std::string sessionName = "DBinder" + std::to_string(uid) + std::string("_") + std::to_string(pid); 698a34a8711Sopenharmony_ci if (DBinderSoftbusClient::GetInstance().DBinderGrantPermission(uid, pid, sessionName) != ERR_NONE) { 699a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "fail to Grant Permission softbus name"); 700a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_GRANT_PERMISSION_FAIL, __FUNCTION__); 701a34a8711Sopenharmony_ci return ""; 702a34a8711Sopenharmony_ci } 703a34a8711Sopenharmony_ci 704a34a8711Sopenharmony_ci return sessionName; 705a34a8711Sopenharmony_ci} 706a34a8711Sopenharmony_ci 707a34a8711Sopenharmony_cibool DBinderService::CheckDeviceIdIllegal(const std::string &remoteDeviceId) 708a34a8711Sopenharmony_ci{ 709a34a8711Sopenharmony_ci if (IsDeviceIdIllegal(remoteDeviceId)) { 710a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "remote device id is error"); 711a34a8711Sopenharmony_ci return true; 712a34a8711Sopenharmony_ci } 713a34a8711Sopenharmony_ci return false; 714a34a8711Sopenharmony_ci} 715a34a8711Sopenharmony_ci 716a34a8711Sopenharmony_cibool DBinderService::CheckSessionNameIsEmpty(const std::string &sessionName) 717a34a8711Sopenharmony_ci{ 718a34a8711Sopenharmony_ci if (sessionName.empty()) { 719a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "get bus name fail"); 720a34a8711Sopenharmony_ci return true; 721a34a8711Sopenharmony_ci } 722a34a8711Sopenharmony_ci return false; 723a34a8711Sopenharmony_ci} 724a34a8711Sopenharmony_ci 725a34a8711Sopenharmony_cibool DBinderService::CheckInvokeListenThreadIllegal(IPCObjectProxy *proxy, MessageParcel &data, MessageParcel &reply) 726a34a8711Sopenharmony_ci{ 727a34a8711Sopenharmony_ci int err = proxy->InvokeListenThread(data, reply); 728a34a8711Sopenharmony_ci if (err != ERR_NONE) { 729a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "start service listen error:%{public}d handle:%{public}d", err, proxy->GetHandle()); 730a34a8711Sopenharmony_ci return true; 731a34a8711Sopenharmony_ci } 732a34a8711Sopenharmony_ci return false; 733a34a8711Sopenharmony_ci} 734a34a8711Sopenharmony_ci 735a34a8711Sopenharmony_cibool DBinderService::CheckStubIndexAndSessionNameIllegal(uint64_t stubIndex, const std::string &serverSessionName, 736a34a8711Sopenharmony_ci const std::string &deviceId, IPCObjectProxy *proxy) 737a34a8711Sopenharmony_ci{ 738a34a8711Sopenharmony_ci if (stubIndex == 0 || serverSessionName.empty() || serverSessionName.length() > SERVICENAME_LENGTH) { 739a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "stubindex:%{public}" PRIu64 " or sessionName:%{public}s is invalid" 740a34a8711Sopenharmony_ci " handle:%{public}d deviceId:%{public}s", stubIndex, serverSessionName.c_str(), proxy->GetHandle(), 741a34a8711Sopenharmony_ci DBinderService::ConvertToSecureDeviceID(deviceId).c_str()); 742a34a8711Sopenharmony_ci return true; 743a34a8711Sopenharmony_ci } 744a34a8711Sopenharmony_ci return false; 745a34a8711Sopenharmony_ci} 746a34a8711Sopenharmony_ci 747a34a8711Sopenharmony_cibool DBinderService::SetReplyMessage(std::shared_ptr<struct DHandleEntryTxRx> replyMessage, uint64_t stubIndex, 748a34a8711Sopenharmony_ci const std::string &serverSessionName, uint32_t selfTokenId, IPCObjectProxy *proxy) 749a34a8711Sopenharmony_ci{ 750a34a8711Sopenharmony_ci replyMessage->dBinderCode = MESSAGE_AS_REPLY; 751a34a8711Sopenharmony_ci if (replyMessage->head.version >= RPC_TOKENID_SUPPORT_VERSION) { 752a34a8711Sopenharmony_ci replyMessage->dBinderCode = MESSAGE_AS_REPLY_TOKENID; 753a34a8711Sopenharmony_ci } 754a34a8711Sopenharmony_ci replyMessage->head.version = RPC_TOKENID_SUPPORT_VERSION; 755a34a8711Sopenharmony_ci replyMessage->stubIndex = stubIndex; 756a34a8711Sopenharmony_ci replyMessage->serviceNameLength = serverSessionName.length(); 757a34a8711Sopenharmony_ci replyMessage->deviceIdInfo.tokenId = selfTokenId; 758a34a8711Sopenharmony_ci if (memcpy_s(replyMessage->serviceName, SERVICENAME_LENGTH, serverSessionName.data(), 759a34a8711Sopenharmony_ci replyMessage->serviceNameLength) != 0) { 760a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "memcpy serviceName fail, handle:%{public}d", proxy->GetHandle()); 761a34a8711Sopenharmony_ci return false; 762a34a8711Sopenharmony_ci } 763a34a8711Sopenharmony_ci replyMessage->serviceName[replyMessage->serviceNameLength] = '\0'; 764a34a8711Sopenharmony_ci return true; 765a34a8711Sopenharmony_ci} 766a34a8711Sopenharmony_ci 767a34a8711Sopenharmony_ciuint32_t DBinderService::OnRemoteInvokerDataBusMessage(IPCObjectProxy *proxy, 768a34a8711Sopenharmony_ci std::shared_ptr<struct DHandleEntryTxRx> replyMessage, 769a34a8711Sopenharmony_ci std::string &remoteDeviceId, int pid, int uid, uint32_t tokenId) 770a34a8711Sopenharmony_ci{ 771a34a8711Sopenharmony_ci if (CheckDeviceIdIllegal(remoteDeviceId)) { 772a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_ERR_INVALID_DATA, __FUNCTION__); 773a34a8711Sopenharmony_ci return DEVICEID_INVALID; 774a34a8711Sopenharmony_ci } 775a34a8711Sopenharmony_ci std::string sessionName = GetDatabusNameByProxy(proxy); 776a34a8711Sopenharmony_ci if (CheckSessionNameIsEmpty(sessionName)) { 777a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_GET_BUS_NAME_FAIL, __FUNCTION__); 778a34a8711Sopenharmony_ci return SESSION_NAME_NOT_FOUND; 779a34a8711Sopenharmony_ci } 780a34a8711Sopenharmony_ci 781a34a8711Sopenharmony_ci MessageParcel data; 782a34a8711Sopenharmony_ci MessageParcel reply; 783a34a8711Sopenharmony_ci if (!data.WriteUint16(IRemoteObject::DATABUS_TYPE) || !data.WriteString(GetLocalDeviceID()) || 784a34a8711Sopenharmony_ci !data.WriteUint32(pid) || !data.WriteUint32(uid) || !data.WriteString(remoteDeviceId) || 785a34a8711Sopenharmony_ci !data.WriteString(sessionName) || !data.WriteUint32(tokenId)) { 786a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "write to parcel fail, handle:%{public}d", proxy->GetHandle()); 787a34a8711Sopenharmony_ci DfxReportFailHandleEvent(DbinderErrorCode::RPC_DRIVER, proxy->GetHandle(), 788a34a8711Sopenharmony_ci RADAR_WRITE_PARCEL_FAIL, __FUNCTION__); 789a34a8711Sopenharmony_ci return WRITE_PARCEL_FAILED; 790a34a8711Sopenharmony_ci } 791a34a8711Sopenharmony_ci if (CheckInvokeListenThreadIllegal(proxy, data, reply)) { 792a34a8711Sopenharmony_ci DfxReportFailHandleEvent(DbinderErrorCode::RPC_DRIVER, proxy->GetHandle(), 793a34a8711Sopenharmony_ci RADAR_INVOKE_STUB_THREAD_FAIL, __FUNCTION__); 794a34a8711Sopenharmony_ci return INVOKE_STUB_THREAD_FAILED; 795a34a8711Sopenharmony_ci } 796a34a8711Sopenharmony_ci 797a34a8711Sopenharmony_ci uint64_t stubIndex = reply.ReadUint64(); 798a34a8711Sopenharmony_ci std::string serverSessionName = reply.ReadString(); 799a34a8711Sopenharmony_ci std::string deviceId = reply.ReadString(); 800a34a8711Sopenharmony_ci uint32_t selfTokenId = reply.ReadUint32(); 801a34a8711Sopenharmony_ci if (CheckStubIndexAndSessionNameIllegal(stubIndex, serverSessionName, deviceId, proxy)) { 802a34a8711Sopenharmony_ci DfxReportFailHandleEvent(DbinderErrorCode::RPC_DRIVER, proxy->GetHandle(), 803a34a8711Sopenharmony_ci RADAR_SESSION_NAME_INVALID, __FUNCTION__); 804a34a8711Sopenharmony_ci return SESSION_NAME_INVALID; 805a34a8711Sopenharmony_ci } 806a34a8711Sopenharmony_ci if (!SetReplyMessage(replyMessage, stubIndex, serverSessionName, selfTokenId, proxy)) { 807a34a8711Sopenharmony_ci DfxReportFailHandleEvent(DbinderErrorCode::RPC_DRIVER, proxy->GetHandle(), RADAR_ERR_MEMCPY_DATA, __FUNCTION__); 808a34a8711Sopenharmony_ci return SESSION_NAME_INVALID; 809a34a8711Sopenharmony_ci } 810a34a8711Sopenharmony_ci return 0; 811a34a8711Sopenharmony_ci} 812a34a8711Sopenharmony_ci 813a34a8711Sopenharmony_cistd::u16string DBinderService::GetRegisterService(binder_uintptr_t binderObject) 814a34a8711Sopenharmony_ci{ 815a34a8711Sopenharmony_ci std::shared_lock<std::shared_mutex> lockGuard(remoteBinderMutex_); 816a34a8711Sopenharmony_ci for (auto it = mapRemoteBinderObjects_.begin(); it != mapRemoteBinderObjects_.end(); it++) { 817a34a8711Sopenharmony_ci if (it->second == binderObject) { 818a34a8711Sopenharmony_ci DBINDER_LOGI(LOG_LABEL, "get service:%{public}s", Str16ToStr8(it->first).c_str()); 819a34a8711Sopenharmony_ci return it->first; 820a34a8711Sopenharmony_ci } 821a34a8711Sopenharmony_ci } 822a34a8711Sopenharmony_ci return std::u16string(); 823a34a8711Sopenharmony_ci} 824a34a8711Sopenharmony_ci 825a34a8711Sopenharmony_cibool DBinderService::RegisterRemoteProxy(std::u16string serviceName, sptr<IRemoteObject> binderObject) 826a34a8711Sopenharmony_ci{ 827a34a8711Sopenharmony_ci if (serviceName.length() == 0 || binderObject == nullptr) { 828a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "serviceName length:%{public}zu", serviceName.length()); 829a34a8711Sopenharmony_ci return false; 830a34a8711Sopenharmony_ci } 831a34a8711Sopenharmony_ci 832a34a8711Sopenharmony_ci DBINDER_LOGI(LOG_LABEL, "service name:%{public}s", Str16ToStr8(serviceName).c_str()); 833a34a8711Sopenharmony_ci binder_uintptr_t binder = (binder_uintptr_t)binderObject.GetRefPtr(); 834a34a8711Sopenharmony_ci return RegisterRemoteProxyInner(serviceName, binder); 835a34a8711Sopenharmony_ci} 836a34a8711Sopenharmony_ci 837a34a8711Sopenharmony_cibool DBinderService::RegisterRemoteProxy(std::u16string serviceName, int32_t systemAbilityId) 838a34a8711Sopenharmony_ci{ 839a34a8711Sopenharmony_ci if (serviceName.length() == 0 || systemAbilityId <= 0) { 840a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "serviceName length:%{public}zu", serviceName.length()); 841a34a8711Sopenharmony_ci return false; 842a34a8711Sopenharmony_ci } 843a34a8711Sopenharmony_ci DBINDER_LOGI(LOG_LABEL, "service name:%{public}s saId:%{public}d", 844a34a8711Sopenharmony_ci Str16ToStr8(serviceName).c_str(), systemAbilityId); 845a34a8711Sopenharmony_ci binder_uintptr_t binder = (binder_uintptr_t)systemAbilityId; 846a34a8711Sopenharmony_ci return RegisterRemoteProxyInner(serviceName, binder); 847a34a8711Sopenharmony_ci} 848a34a8711Sopenharmony_ci 849a34a8711Sopenharmony_cibool DBinderService::RegisterRemoteProxyInner(std::u16string serviceName, binder_uintptr_t binder) 850a34a8711Sopenharmony_ci{ 851a34a8711Sopenharmony_ci std::unique_lock<std::shared_mutex> lockGuard(remoteBinderMutex_); 852a34a8711Sopenharmony_ci // clear historical remnants, Don't care if it succeeds 853a34a8711Sopenharmony_ci (void)mapRemoteBinderObjects_.erase(serviceName); 854a34a8711Sopenharmony_ci auto result = mapRemoteBinderObjects_.insert(std::pair<std::u16string, binder_uintptr_t>(serviceName, binder)); 855a34a8711Sopenharmony_ci return result.second; 856a34a8711Sopenharmony_ci} 857a34a8711Sopenharmony_ci 858a34a8711Sopenharmony_civoid DBinderService::AddAsynMessageTask(std::shared_ptr<struct DHandleEntryTxRx> message) 859a34a8711Sopenharmony_ci{ 860a34a8711Sopenharmony_ci auto task = [this, message] { this->OnRemoteMessageTask(message); }; 861a34a8711Sopenharmony_ci ffrt::submit(task); 862a34a8711Sopenharmony_ci} 863a34a8711Sopenharmony_ci 864a34a8711Sopenharmony_cibool DBinderService::OnRemoteMessageTask(std::shared_ptr<struct DHandleEntryTxRx> message) 865a34a8711Sopenharmony_ci{ 866a34a8711Sopenharmony_ci if (message == nullptr) { 867a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "message is null"); 868a34a8711Sopenharmony_ci return false; 869a34a8711Sopenharmony_ci } 870a34a8711Sopenharmony_ci 871a34a8711Sopenharmony_ci bool result = false; 872a34a8711Sopenharmony_ci switch (message->dBinderCode) { 873a34a8711Sopenharmony_ci case MESSAGE_AS_INVOKER: { 874a34a8711Sopenharmony_ci result = OnRemoteInvokerMessage(message); 875a34a8711Sopenharmony_ci break; 876a34a8711Sopenharmony_ci } 877a34a8711Sopenharmony_ci case MESSAGE_AS_REPLY: 878a34a8711Sopenharmony_ci case MESSAGE_AS_REPLY_TOKENID: { 879a34a8711Sopenharmony_ci result = OnRemoteReplyMessage(message); 880a34a8711Sopenharmony_ci break; 881a34a8711Sopenharmony_ci } 882a34a8711Sopenharmony_ci case MESSAGE_AS_REMOTE_ERROR: { 883a34a8711Sopenharmony_ci result = OnRemoteErrorMessage(message); 884a34a8711Sopenharmony_ci break; 885a34a8711Sopenharmony_ci } 886a34a8711Sopenharmony_ci default: { 887a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "DbinderCode:%{public}u is not support", message->dBinderCode); 888a34a8711Sopenharmony_ci result = false; 889a34a8711Sopenharmony_ci break; 890a34a8711Sopenharmony_ci } 891a34a8711Sopenharmony_ci } 892a34a8711Sopenharmony_ci return result; 893a34a8711Sopenharmony_ci} 894a34a8711Sopenharmony_ci 895a34a8711Sopenharmony_cibool DBinderService::ProcessOnSessionClosed(const std::string &networkId) 896a34a8711Sopenharmony_ci{ 897a34a8711Sopenharmony_ci std::lock_guard<std::mutex> lock(threadLockMutex_); 898a34a8711Sopenharmony_ci for (auto it = threadLockInfo_.begin(); it != threadLockInfo_.end();) { 899a34a8711Sopenharmony_ci if (it->second->networkId != networkId) { 900a34a8711Sopenharmony_ci it++; 901a34a8711Sopenharmony_ci continue; 902a34a8711Sopenharmony_ci } 903a34a8711Sopenharmony_ci std::unique_lock<std::mutex> lock(it->second->mutex); 904a34a8711Sopenharmony_ci it->second->ready = false; 905a34a8711Sopenharmony_ci it->second->condition.notify_all(); 906a34a8711Sopenharmony_ci it = threadLockInfo_.erase(it); 907a34a8711Sopenharmony_ci } 908a34a8711Sopenharmony_ci return true; 909a34a8711Sopenharmony_ci} 910a34a8711Sopenharmony_ci 911a34a8711Sopenharmony_cibool DBinderService::OnRemoteErrorMessage(std::shared_ptr<struct DHandleEntryTxRx> replyMessage) 912a34a8711Sopenharmony_ci{ 913a34a8711Sopenharmony_ci DfxReportEvent(DbinderErrorCode::RPC_DRIVER, DbinderErrorCode::IPC_RESULT_IDLE, __FUNCTION__); 914a34a8711Sopenharmony_ci DBINDER_LOGI(LOG_LABEL, "invoke remote stubIndex:%{public}d fail, error:%{public}u seq:%{public}u", 915a34a8711Sopenharmony_ci static_cast<int32_t>(replyMessage->stubIndex), replyMessage->transType, replyMessage->seqNumber); 916a34a8711Sopenharmony_ci WakeupThreadByStub(replyMessage->seqNumber); 917a34a8711Sopenharmony_ci DetachThreadLockInfo(replyMessage->seqNumber); 918a34a8711Sopenharmony_ci return true; 919a34a8711Sopenharmony_ci} 920a34a8711Sopenharmony_ci 921a34a8711Sopenharmony_cibool DBinderService::OnRemoteReplyMessage(std::shared_ptr<struct DHandleEntryTxRx> replyMessage) 922a34a8711Sopenharmony_ci{ 923a34a8711Sopenharmony_ci DBINDER_LOGI(LOG_LABEL, "invoker remote stubIndex:%{public}d succ, seq:%{public}u stub:%{public}llu " 924a34a8711Sopenharmony_ci "tokenId:%{public}u dBinderCode:%{public}u", static_cast<int32_t>(replyMessage->stubIndex), 925a34a8711Sopenharmony_ci replyMessage->seqNumber, (replyMessage->stub & BINDER_MASK), replyMessage->deviceIdInfo.tokenId, 926a34a8711Sopenharmony_ci replyMessage->dBinderCode); 927a34a8711Sopenharmony_ci MakeSessionByReplyMessage(replyMessage); 928a34a8711Sopenharmony_ci WakeupThreadByStub(replyMessage->seqNumber); 929a34a8711Sopenharmony_ci DetachThreadLockInfo(replyMessage->seqNumber); 930a34a8711Sopenharmony_ci return true; 931a34a8711Sopenharmony_ci} 932a34a8711Sopenharmony_ci 933a34a8711Sopenharmony_cibool DBinderService::IsSameSession(std::shared_ptr<struct SessionInfo> oldSession, 934a34a8711Sopenharmony_ci std::shared_ptr<struct SessionInfo> newSession) 935a34a8711Sopenharmony_ci{ 936a34a8711Sopenharmony_ci if ((oldSession->stubIndex != newSession->stubIndex) || (oldSession->toPort != newSession->toPort) 937a34a8711Sopenharmony_ci || (oldSession->fromPort != newSession->fromPort) || (oldSession->type != newSession->type) 938a34a8711Sopenharmony_ci || (oldSession->serviceName != newSession->serviceName)) { 939a34a8711Sopenharmony_ci return false; 940a34a8711Sopenharmony_ci } 941a34a8711Sopenharmony_ci if (strncmp(oldSession->deviceIdInfo.fromDeviceId, newSession->deviceIdInfo.fromDeviceId, DEVICEID_LENGTH) != 0 942a34a8711Sopenharmony_ci || strncmp(oldSession->deviceIdInfo.toDeviceId, newSession->deviceIdInfo.toDeviceId, DEVICEID_LENGTH) != 0) { 943a34a8711Sopenharmony_ci return false; 944a34a8711Sopenharmony_ci } 945a34a8711Sopenharmony_ci 946a34a8711Sopenharmony_ci return true; 947a34a8711Sopenharmony_ci} 948a34a8711Sopenharmony_ci 949a34a8711Sopenharmony_cibool DBinderService::IsInvalidStub(std::shared_ptr<struct DHandleEntryTxRx> replyMessage) 950a34a8711Sopenharmony_ci{ 951a34a8711Sopenharmony_ci if (HasDBinderStub(QueryStubPtr(replyMessage->stub)) == false) { 952a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "invalid stub object"); 953a34a8711Sopenharmony_ci return true; 954a34a8711Sopenharmony_ci } 955a34a8711Sopenharmony_ci return false; 956a34a8711Sopenharmony_ci} 957a34a8711Sopenharmony_ci 958a34a8711Sopenharmony_cibool DBinderService::CopyDeviceIdInfo(std::shared_ptr<struct SessionInfo> &session, 959a34a8711Sopenharmony_ci std::shared_ptr<struct DHandleEntryTxRx> replyMessage) 960a34a8711Sopenharmony_ci{ 961a34a8711Sopenharmony_ci if (memcpy_s(&session->deviceIdInfo, sizeof(struct DeviceIdInfo), &replyMessage->deviceIdInfo, 962a34a8711Sopenharmony_ci sizeof(struct DeviceIdInfo)) != 0) { 963a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "fail to copy memory"); 964a34a8711Sopenharmony_ci return false; 965a34a8711Sopenharmony_ci } 966a34a8711Sopenharmony_ci return true; 967a34a8711Sopenharmony_ci} 968a34a8711Sopenharmony_ci 969a34a8711Sopenharmony_civoid DBinderService::InitializeSession(std::shared_ptr<struct SessionInfo> &session, 970a34a8711Sopenharmony_ci std::shared_ptr<struct DHandleEntryTxRx> replyMessage) 971a34a8711Sopenharmony_ci{ 972a34a8711Sopenharmony_ci session->seqNumber = replyMessage->seqNumber; 973a34a8711Sopenharmony_ci session->socketFd = 0; 974a34a8711Sopenharmony_ci session->stubIndex = replyMessage->stubIndex; 975a34a8711Sopenharmony_ci session->toPort = replyMessage->toPort; 976a34a8711Sopenharmony_ci session->fromPort = replyMessage->fromPort; 977a34a8711Sopenharmony_ci session->type = replyMessage->transType; 978a34a8711Sopenharmony_ci session->serviceName = replyMessage->serviceName; 979a34a8711Sopenharmony_ci} 980a34a8711Sopenharmony_ci 981a34a8711Sopenharmony_civoid DBinderService::MakeSessionByReplyMessage(std::shared_ptr<struct DHandleEntryTxRx> replyMessage) 982a34a8711Sopenharmony_ci{ 983a34a8711Sopenharmony_ci if (IsInvalidStub(replyMessage)) { 984a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_STUB_INVALID, __FUNCTION__); 985a34a8711Sopenharmony_ci return; 986a34a8711Sopenharmony_ci } 987a34a8711Sopenharmony_ci 988a34a8711Sopenharmony_ci std::shared_ptr<struct SessionInfo> session = std::make_shared<struct SessionInfo>(); 989a34a8711Sopenharmony_ci if (session == nullptr) { 990a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "new SessionInfo fail"); 991a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_IPC_NEW_SESSION_FAIL, __FUNCTION__); 992a34a8711Sopenharmony_ci return; 993a34a8711Sopenharmony_ci } 994a34a8711Sopenharmony_ci 995a34a8711Sopenharmony_ci if (!CopyDeviceIdInfo(session, replyMessage)) { 996a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_ERR_MEMCPY_DATA, __FUNCTION__); 997a34a8711Sopenharmony_ci return; 998a34a8711Sopenharmony_ci } 999a34a8711Sopenharmony_ci // remote device NOT support tokenId, clear random value 1000a34a8711Sopenharmony_ci if (replyMessage->dBinderCode == MESSAGE_AS_REPLY) { 1001a34a8711Sopenharmony_ci session->deviceIdInfo.tokenId = 0; 1002a34a8711Sopenharmony_ci } 1003a34a8711Sopenharmony_ci DBINDER_LOGI(LOG_LABEL, "stubIndex:%{public}d tokenId:%{public}u", 1004a34a8711Sopenharmony_ci static_cast<int32_t>(replyMessage->stubIndex), session->deviceIdInfo.tokenId); 1005a34a8711Sopenharmony_ci InitializeSession(session, replyMessage); 1006a34a8711Sopenharmony_ci 1007a34a8711Sopenharmony_ci if (session->stubIndex == 0) { 1008a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "get stubIndex == 0, it is invalid"); 1009a34a8711Sopenharmony_ci return; 1010a34a8711Sopenharmony_ci } 1011a34a8711Sopenharmony_ci // check whether need to update session 1012a34a8711Sopenharmony_ci std::shared_ptr<struct SessionInfo> oldSession = QuerySessionObject(QueryStubPtr(replyMessage->stub)); 1013a34a8711Sopenharmony_ci if (oldSession != nullptr) { 1014a34a8711Sopenharmony_ci if (IsSameSession(oldSession, session) == true) { 1015a34a8711Sopenharmony_ci DBINDER_LOGI(LOG_LABEL, "invoker remote session already, do nothing"); 1016a34a8711Sopenharmony_ci return; 1017a34a8711Sopenharmony_ci } 1018a34a8711Sopenharmony_ci // ignore seqNumber overflow here, greater seqNumber means later request 1019a34a8711Sopenharmony_ci if (oldSession->seqNumber < session->seqNumber) { 1020a34a8711Sopenharmony_ci // remote old session 1021a34a8711Sopenharmony_ci if (!DetachSessionObject(QueryStubPtr(replyMessage->stub))) { 1022a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "failed to detach session object"); 1023a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_DETACH_SESSION_FAIL, __FUNCTION__); 1024a34a8711Sopenharmony_ci } 1025a34a8711Sopenharmony_ci } else { 1026a34a8711Sopenharmony_ci // do nothing, use old session, discard session got this time 1027a34a8711Sopenharmony_ci // in this case, old session is requested later, but it comes back earlier 1028a34a8711Sopenharmony_ci } 1029a34a8711Sopenharmony_ci } 1030a34a8711Sopenharmony_ci 1031a34a8711Sopenharmony_ci if (!AttachSessionObject(session, QueryStubPtr(replyMessage->stub))) { 1032a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "attach SessionInfo fail"); 1033a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_ATTACH_SESSION_FAIL, __FUNCTION__); 1034a34a8711Sopenharmony_ci return; 1035a34a8711Sopenharmony_ci } 1036a34a8711Sopenharmony_ci} 1037a34a8711Sopenharmony_ci 1038a34a8711Sopenharmony_civoid DBinderService::WakeupThreadByStub(uint32_t seqNumber) 1039a34a8711Sopenharmony_ci{ 1040a34a8711Sopenharmony_ci std::shared_ptr<struct ThreadLockInfo> threadLockInfo = QueryThreadLockInfo(seqNumber); 1041a34a8711Sopenharmony_ci if (threadLockInfo == nullptr) { 1042a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "threadLockInfo is not exist"); 1043a34a8711Sopenharmony_ci return; 1044a34a8711Sopenharmony_ci } 1045a34a8711Sopenharmony_ci /* Wake up the client processing thread */ 1046a34a8711Sopenharmony_ci std::unique_lock<std::mutex> lock(threadLockInfo->mutex); 1047a34a8711Sopenharmony_ci threadLockInfo->ready = true; 1048a34a8711Sopenharmony_ci threadLockInfo->condition.notify_all(); 1049a34a8711Sopenharmony_ci} 1050a34a8711Sopenharmony_ci 1051a34a8711Sopenharmony_civoid DBinderService::DetachThreadLockInfo(uint32_t seqNumber) 1052a34a8711Sopenharmony_ci{ 1053a34a8711Sopenharmony_ci std::lock_guard<std::mutex> lock(threadLockMutex_); 1054a34a8711Sopenharmony_ci threadLockInfo_.erase(seqNumber); 1055a34a8711Sopenharmony_ci} 1056a34a8711Sopenharmony_ci 1057a34a8711Sopenharmony_cibool DBinderService::AttachThreadLockInfo(uint32_t seqNumber, const std::string &networkId, 1058a34a8711Sopenharmony_ci std::shared_ptr<struct ThreadLockInfo> object) 1059a34a8711Sopenharmony_ci{ 1060a34a8711Sopenharmony_ci std::lock_guard<std::mutex> lock(threadLockMutex_); 1061a34a8711Sopenharmony_ci object->networkId = networkId; 1062a34a8711Sopenharmony_ci auto result = 1063a34a8711Sopenharmony_ci threadLockInfo_.insert(std::pair<uint32_t, std::shared_ptr<struct ThreadLockInfo>>(seqNumber, object)); 1064a34a8711Sopenharmony_ci return result.second; 1065a34a8711Sopenharmony_ci} 1066a34a8711Sopenharmony_ci 1067a34a8711Sopenharmony_cistd::shared_ptr<struct ThreadLockInfo> DBinderService::QueryThreadLockInfo(uint32_t seqNumber) 1068a34a8711Sopenharmony_ci{ 1069a34a8711Sopenharmony_ci std::lock_guard<std::mutex> lock(threadLockMutex_); 1070a34a8711Sopenharmony_ci 1071a34a8711Sopenharmony_ci auto it = threadLockInfo_.find(seqNumber); 1072a34a8711Sopenharmony_ci if (it != threadLockInfo_.end()) { 1073a34a8711Sopenharmony_ci return it->second; 1074a34a8711Sopenharmony_ci } 1075a34a8711Sopenharmony_ci return nullptr; 1076a34a8711Sopenharmony_ci} 1077a34a8711Sopenharmony_ci 1078a34a8711Sopenharmony_cibool DBinderService::DetachProxyObject(binder_uintptr_t binderObject) 1079a34a8711Sopenharmony_ci{ 1080a34a8711Sopenharmony_ci std::unique_lock<std::shared_mutex> lock(proxyMutex_); 1081a34a8711Sopenharmony_ci 1082a34a8711Sopenharmony_ci return (proxyObject_.erase(binderObject) > 0); 1083a34a8711Sopenharmony_ci} 1084a34a8711Sopenharmony_ci 1085a34a8711Sopenharmony_cibool DBinderService::AttachProxyObject(sptr<IRemoteObject> object, binder_uintptr_t binderObject) 1086a34a8711Sopenharmony_ci{ 1087a34a8711Sopenharmony_ci std::unique_lock<std::shared_mutex> lock(proxyMutex_); 1088a34a8711Sopenharmony_ci 1089a34a8711Sopenharmony_ci auto result = proxyObject_.insert(std::pair<int, sptr<IRemoteObject>>(binderObject, object)); 1090a34a8711Sopenharmony_ci return result.second; 1091a34a8711Sopenharmony_ci} 1092a34a8711Sopenharmony_ci 1093a34a8711Sopenharmony_cisptr<IRemoteObject> DBinderService::QueryProxyObject(binder_uintptr_t binderObject) 1094a34a8711Sopenharmony_ci{ 1095a34a8711Sopenharmony_ci std::shared_lock<std::shared_mutex> lock(proxyMutex_); 1096a34a8711Sopenharmony_ci 1097a34a8711Sopenharmony_ci auto it = proxyObject_.find(binderObject); 1098a34a8711Sopenharmony_ci if (it != proxyObject_.end()) { 1099a34a8711Sopenharmony_ci return it->second; 1100a34a8711Sopenharmony_ci } 1101a34a8711Sopenharmony_ci return nullptr; 1102a34a8711Sopenharmony_ci} 1103a34a8711Sopenharmony_ci 1104a34a8711Sopenharmony_cibool DBinderService::DetachSessionObject(binder_uintptr_t stub) 1105a34a8711Sopenharmony_ci{ 1106a34a8711Sopenharmony_ci std::unique_lock<std::shared_mutex> lock(sessionMutex_); 1107a34a8711Sopenharmony_ci return (sessionObject_.erase(stub) > 0); 1108a34a8711Sopenharmony_ci} 1109a34a8711Sopenharmony_ci 1110a34a8711Sopenharmony_cibool DBinderService::AttachSessionObject(std::shared_ptr<struct SessionInfo> object, binder_uintptr_t stub) 1111a34a8711Sopenharmony_ci{ 1112a34a8711Sopenharmony_ci std::unique_lock<std::shared_mutex> lock(sessionMutex_); 1113a34a8711Sopenharmony_ci 1114a34a8711Sopenharmony_ci auto ret = sessionObject_.insert(std::pair<binder_uintptr_t, std::shared_ptr<struct SessionInfo>>(stub, object)); 1115a34a8711Sopenharmony_ci return ret.second; 1116a34a8711Sopenharmony_ci} 1117a34a8711Sopenharmony_ci 1118a34a8711Sopenharmony_cistd::shared_ptr<struct SessionInfo> DBinderService::QuerySessionObject(binder_uintptr_t stub) 1119a34a8711Sopenharmony_ci{ 1120a34a8711Sopenharmony_ci std::shared_lock<std::shared_mutex> lock(sessionMutex_); 1121a34a8711Sopenharmony_ci 1122a34a8711Sopenharmony_ci auto it = sessionObject_.find(stub); 1123a34a8711Sopenharmony_ci if (it != sessionObject_.end()) { 1124a34a8711Sopenharmony_ci return it->second; 1125a34a8711Sopenharmony_ci } 1126a34a8711Sopenharmony_ci return nullptr; 1127a34a8711Sopenharmony_ci} 1128a34a8711Sopenharmony_ci 1129a34a8711Sopenharmony_cibool DBinderService::DetachDeathRecipient(sptr<IRemoteObject> object) 1130a34a8711Sopenharmony_ci{ 1131a34a8711Sopenharmony_ci std::unique_lock<std::shared_mutex> lockGuard(deathRecipientMutex_); 1132a34a8711Sopenharmony_ci 1133a34a8711Sopenharmony_ci return (deathRecipients_.erase(object) > 0); 1134a34a8711Sopenharmony_ci} 1135a34a8711Sopenharmony_ci 1136a34a8711Sopenharmony_cibool DBinderService::AttachDeathRecipient(sptr<IRemoteObject> object, 1137a34a8711Sopenharmony_ci sptr<IRemoteObject::DeathRecipient> deathRecipient) 1138a34a8711Sopenharmony_ci{ 1139a34a8711Sopenharmony_ci std::unique_lock<std::shared_mutex> lockGuard(deathRecipientMutex_); 1140a34a8711Sopenharmony_ci 1141a34a8711Sopenharmony_ci auto ret = deathRecipients_.insert( 1142a34a8711Sopenharmony_ci std::pair<sptr<IRemoteObject>, sptr<IRemoteObject::DeathRecipient>>(object, deathRecipient)); 1143a34a8711Sopenharmony_ci 1144a34a8711Sopenharmony_ci return ret.second; 1145a34a8711Sopenharmony_ci} 1146a34a8711Sopenharmony_ci 1147a34a8711Sopenharmony_cisptr<IRemoteObject::DeathRecipient> DBinderService::QueryDeathRecipient(sptr<IRemoteObject> object) 1148a34a8711Sopenharmony_ci{ 1149a34a8711Sopenharmony_ci std::shared_lock<std::shared_mutex> lockGuard(deathRecipientMutex_); 1150a34a8711Sopenharmony_ci 1151a34a8711Sopenharmony_ci auto it = deathRecipients_.find(object); 1152a34a8711Sopenharmony_ci if (it != deathRecipients_.end()) { 1153a34a8711Sopenharmony_ci return it->second; 1154a34a8711Sopenharmony_ci } 1155a34a8711Sopenharmony_ci 1156a34a8711Sopenharmony_ci return nullptr; 1157a34a8711Sopenharmony_ci} 1158a34a8711Sopenharmony_ci 1159a34a8711Sopenharmony_ci 1160a34a8711Sopenharmony_cibool DBinderService::DetachCallbackProxy(sptr<IRemoteObject> object) 1161a34a8711Sopenharmony_ci{ 1162a34a8711Sopenharmony_ci std::lock_guard<std::mutex> lockGuard(callbackProxyMutex_); 1163a34a8711Sopenharmony_ci 1164a34a8711Sopenharmony_ci return (noticeProxy_.erase(object) > 0); 1165a34a8711Sopenharmony_ci} 1166a34a8711Sopenharmony_ci 1167a34a8711Sopenharmony_cibool DBinderService::AttachCallbackProxy(sptr<IRemoteObject> object, DBinderServiceStub *dbStub) 1168a34a8711Sopenharmony_ci{ 1169a34a8711Sopenharmony_ci std::lock_guard<std::mutex> lockGuard(callbackProxyMutex_); 1170a34a8711Sopenharmony_ci 1171a34a8711Sopenharmony_ci auto result = noticeProxy_.insert(std::pair<sptr<IRemoteObject>, DBinderServiceStub *>(object, dbStub)); 1172a34a8711Sopenharmony_ci 1173a34a8711Sopenharmony_ci return result.second; 1174a34a8711Sopenharmony_ci} 1175a34a8711Sopenharmony_ci 1176a34a8711Sopenharmony_cibool DBinderService::NoticeCallbackProxy(sptr<DBinderServiceStub> dbStub) 1177a34a8711Sopenharmony_ci{ 1178a34a8711Sopenharmony_ci DBINDER_LOGI(LOG_LABEL, "service:%{public}s devicId:%{public}s", 1179a34a8711Sopenharmony_ci dbStub->GetServiceName().c_str(), DBinderService::ConvertToSecureDeviceID(dbStub->GetDeviceID()).c_str()); 1180a34a8711Sopenharmony_ci bool status = true; 1181a34a8711Sopenharmony_ci const binder_uintptr_t binderObject = reinterpret_cast<binder_uintptr_t>(dbStub.GetRefPtr()); 1182a34a8711Sopenharmony_ci if (!DetachSessionObject(binderObject)) { 1183a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "fail to detach session object"); 1184a34a8711Sopenharmony_ci status = false; 1185a34a8711Sopenharmony_ci } 1186a34a8711Sopenharmony_ci 1187a34a8711Sopenharmony_ci if (!DeleteDBinderStub(Str8ToStr16(dbStub->GetServiceName()), dbStub->GetDeviceID())) { 1188a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "fail to delete DBinder stub"); 1189a34a8711Sopenharmony_ci status = false; 1190a34a8711Sopenharmony_ci } 1191a34a8711Sopenharmony_ci 1192a34a8711Sopenharmony_ci ProcessCallbackProxy(dbStub); 1193a34a8711Sopenharmony_ci 1194a34a8711Sopenharmony_ci return status; 1195a34a8711Sopenharmony_ci} 1196a34a8711Sopenharmony_ci 1197a34a8711Sopenharmony_civoid DBinderService::ProcessCallbackProxy(sptr<DBinderServiceStub> dbStub) 1198a34a8711Sopenharmony_ci{ 1199a34a8711Sopenharmony_ci std::lock_guard<std::mutex> lockGuard(callbackProxyMutex_); 1200a34a8711Sopenharmony_ci for (auto it = noticeProxy_.begin(); it != noticeProxy_.end();) { 1201a34a8711Sopenharmony_ci if (it->second == dbStub.GetRefPtr()) { 1202a34a8711Sopenharmony_ci IPCObjectProxy *callbackProxy = reinterpret_cast<IPCObjectProxy *>((it->first).GetRefPtr()); 1203a34a8711Sopenharmony_ci int status = callbackProxy->NoticeServiceDie(); 1204a34a8711Sopenharmony_ci if (status != ERR_NONE) { 1205a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "fail to notice service:%{public}s die, handle:%{public}d", 1206a34a8711Sopenharmony_ci dbStub->GetServiceName().c_str(), callbackProxy->GetHandle()); 1207a34a8711Sopenharmony_ci // do nothing, Continue to clear subsequent data 1208a34a8711Sopenharmony_ci } 1209a34a8711Sopenharmony_ci 1210a34a8711Sopenharmony_ci sptr<IRemoteObject::DeathRecipient> death = QueryDeathRecipient((it->first)); 1211a34a8711Sopenharmony_ci if (death != nullptr) { 1212a34a8711Sopenharmony_ci // Continue to clear subsequent data 1213a34a8711Sopenharmony_ci callbackProxy->RemoveDeathRecipient(death); 1214a34a8711Sopenharmony_ci } 1215a34a8711Sopenharmony_ci 1216a34a8711Sopenharmony_ci if (!DetachDeathRecipient((it->first))) { 1217a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "detaching death recipient is failed, service:%{public}s handle:%{public}d", 1218a34a8711Sopenharmony_ci dbStub->GetServiceName().c_str(), callbackProxy->GetHandle()); 1219a34a8711Sopenharmony_ci } 1220a34a8711Sopenharmony_ci 1221a34a8711Sopenharmony_ci it = noticeProxy_.erase(it); 1222a34a8711Sopenharmony_ci } else { 1223a34a8711Sopenharmony_ci it++; 1224a34a8711Sopenharmony_ci } 1225a34a8711Sopenharmony_ci } 1226a34a8711Sopenharmony_ci} 1227a34a8711Sopenharmony_ci 1228a34a8711Sopenharmony_ciint32_t DBinderService::NoticeServiceDieInner(const std::u16string &serviceName, const std::string &deviceID) 1229a34a8711Sopenharmony_ci{ 1230a34a8711Sopenharmony_ci if (serviceName.empty() || IsDeviceIdIllegal(deviceID)) { 1231a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "service name length:%{public}zu, deviceID length:%{public}zu", 1232a34a8711Sopenharmony_ci serviceName.length(), deviceID.length()); 1233a34a8711Sopenharmony_ci return DBINDER_SERVICE_INVALID_DATA_ERR; 1234a34a8711Sopenharmony_ci } 1235a34a8711Sopenharmony_ci 1236a34a8711Sopenharmony_ci DBINDER_LOGI(LOG_LABEL, "service:%{public}s deviceId:%{public}s", 1237a34a8711Sopenharmony_ci Str16ToStr8(serviceName).c_str(), DBinderService::ConvertToSecureDeviceID(deviceID).c_str()); 1238a34a8711Sopenharmony_ci sptr<DBinderServiceStub> dbStub = FindDBinderStub(serviceName, deviceID); 1239a34a8711Sopenharmony_ci if (dbStub == nullptr) { 1240a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "find null stub, do not need notice death"); 1241a34a8711Sopenharmony_ci return ERR_NONE; 1242a34a8711Sopenharmony_ci } 1243a34a8711Sopenharmony_ci 1244a34a8711Sopenharmony_ci if (!NoticeCallbackProxy(dbStub)) { 1245a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "find null proxy"); 1246a34a8711Sopenharmony_ci return DBINDER_SERVICE_NOTICE_DIE_ERR; 1247a34a8711Sopenharmony_ci } 1248a34a8711Sopenharmony_ci return ERR_NONE; 1249a34a8711Sopenharmony_ci} 1250a34a8711Sopenharmony_ci 1251a34a8711Sopenharmony_ciint32_t DBinderService::NoticeServiceDie(const std::u16string &serviceName, const std::string &deviceID) 1252a34a8711Sopenharmony_ci{ 1253a34a8711Sopenharmony_ci if (IsDeviceIdIllegal(deviceID)) { 1254a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_ERR_INVALID_DATA, __FUNCTION__); 1255a34a8711Sopenharmony_ci } else { 1256a34a8711Sopenharmony_ci DfxReportDeviceEvent(DbinderErrorCode::RPC_DRIVER, DbinderErrorCode::IPC_RESULT_IDLE, 1257a34a8711Sopenharmony_ci DBinderService::ConvertToSecureDeviceID(deviceID).c_str(), __FUNCTION__); 1258a34a8711Sopenharmony_ci } 1259a34a8711Sopenharmony_ci std::lock_guard<std::mutex> lockGuard(deathNotificationMutex_); 1260a34a8711Sopenharmony_ci return NoticeServiceDieInner(serviceName, deviceID); 1261a34a8711Sopenharmony_ci} 1262a34a8711Sopenharmony_ci 1263a34a8711Sopenharmony_ciint32_t DBinderService::NoticeDeviceDie(const std::string &deviceID) 1264a34a8711Sopenharmony_ci{ 1265a34a8711Sopenharmony_ci if (IsDeviceIdIllegal(deviceID)) { 1266a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "deviceID length:%{public}zu", deviceID.length()); 1267a34a8711Sopenharmony_ci DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_ERR_INVALID_DATA, __FUNCTION__); 1268a34a8711Sopenharmony_ci return DBINDER_SERVICE_INVALID_DATA_ERR; 1269a34a8711Sopenharmony_ci } 1270a34a8711Sopenharmony_ci DBINDER_LOGI(LOG_LABEL, "remote device:%{public}s is dead", 1271a34a8711Sopenharmony_ci DBinderService::ConvertToSecureDeviceID(deviceID).c_str()); 1272a34a8711Sopenharmony_ci DfxReportDeviceEvent(DbinderErrorCode::RPC_DRIVER, DbinderErrorCode::IPC_RESULT_IDLE, 1273a34a8711Sopenharmony_ci DBinderService::ConvertToSecureDeviceID(deviceID).c_str(), __FUNCTION__); 1274a34a8711Sopenharmony_ci 1275a34a8711Sopenharmony_ci if (remoteListener_ == nullptr) { 1276a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "remote listener is null"); 1277a34a8711Sopenharmony_ci return DBINDER_SERVICE_NOTICE_DIE_ERR; 1278a34a8711Sopenharmony_ci } 1279a34a8711Sopenharmony_ci 1280a34a8711Sopenharmony_ci if (!remoteListener_->ShutdownSocket(deviceID)) { 1281a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "Shutdown fail"); 1282a34a8711Sopenharmony_ci // do nothing 1283a34a8711Sopenharmony_ci } 1284a34a8711Sopenharmony_ci 1285a34a8711Sopenharmony_ci std::list<std::u16string> serviceNames = FindServicesByDeviceID(deviceID); 1286a34a8711Sopenharmony_ci if (serviceNames.empty()) { 1287a34a8711Sopenharmony_ci DBINDER_LOGE(LOG_LABEL, "the device does not have any registered service"); 1288a34a8711Sopenharmony_ci return ERR_NONE; 1289a34a8711Sopenharmony_ci } 1290a34a8711Sopenharmony_ci 1291a34a8711Sopenharmony_ci int status = ERR_NONE; 1292a34a8711Sopenharmony_ci std::lock_guard<std::mutex> lockGuard(deathNotificationMutex_); 1293a34a8711Sopenharmony_ci 1294a34a8711Sopenharmony_ci for (auto it = serviceNames.begin(); it != serviceNames.end(); it++) { 1295a34a8711Sopenharmony_ci status += NoticeServiceDieInner((*it), deviceID); 1296a34a8711Sopenharmony_ci } 1297a34a8711Sopenharmony_ci 1298a34a8711Sopenharmony_ci return status; 1299a34a8711Sopenharmony_ci} 1300a34a8711Sopenharmony_ci 1301a34a8711Sopenharmony_cistd::list<std::u16string> DBinderService::FindServicesByDeviceID(const std::string &deviceID) 1302a34a8711Sopenharmony_ci{ 1303a34a8711Sopenharmony_ci std::lock_guard<std::mutex> lockGuard(handleEntryMutex_); 1304a34a8711Sopenharmony_ci std::list<std::u16string> serviceNames; 1305a34a8711Sopenharmony_ci for (auto it = DBinderStubRegisted_.begin(); it != DBinderStubRegisted_.end(); it++) { 1306a34a8711Sopenharmony_ci if ((*it)->GetDeviceID() == deviceID) { 1307a34a8711Sopenharmony_ci serviceNames.push_back(Str8ToStr16((*it)->GetServiceName())); 1308a34a8711Sopenharmony_ci } 1309a34a8711Sopenharmony_ci } 1310a34a8711Sopenharmony_ci 1311a34a8711Sopenharmony_ci DBINDER_LOGI(LOG_LABEL, "deviceId:%{public}s, service size:%{public}zu", 1312a34a8711Sopenharmony_ci DBinderService::ConvertToSecureDeviceID(deviceID).c_str(), serviceNames.size()); 1313a34a8711Sopenharmony_ci return serviceNames; 1314a34a8711Sopenharmony_ci} 1315a34a8711Sopenharmony_ci 1316a34a8711Sopenharmony_ciuint32_t DBinderService::GetRemoteTransType() 1317a34a8711Sopenharmony_ci{ 1318a34a8711Sopenharmony_ci return IRemoteObject::DATABUS_TYPE; 1319a34a8711Sopenharmony_ci} 1320a34a8711Sopenharmony_ci 1321a34a8711Sopenharmony_cistd::string DBinderService::ConvertToSecureDeviceID(const std::string &str) 1322a34a8711Sopenharmony_ci{ 1323a34a8711Sopenharmony_ci size_t len = str.size(); 1324a34a8711Sopenharmony_ci if (len <= ENCRYPT_LENGTH) { 1325a34a8711Sopenharmony_ci return "****"; 1326a34a8711Sopenharmony_ci } 1327a34a8711Sopenharmony_ci return str.substr(0, ENCRYPT_LENGTH) + "****" + str.substr(len - ENCRYPT_LENGTH); 1328a34a8711Sopenharmony_ci} 1329a34a8711Sopenharmony_ci} // namespace OHOS 1330