From 9cb8c97bbdad5a7372e55753364b4b8b221d840b Mon Sep 17 00:00:00 2001 From: liangshenglin1 Date: Mon, 13 Sep 2021 15:22:10 +0800 Subject: [PATCH] MakeRemoteBinder with sa id Signed-off-by: liangshenglin1 --- .../libdbinder/include/dbinder_service.h | 7 ++++-- .../dbinder_service/src/dbinder_service.cpp | 23 +++++++++++-------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/interfaces/innerkits/libdbinder/include/dbinder_service.h b/interfaces/innerkits/libdbinder/include/dbinder_service.h index cd36954e..b23e5f47 100755 --- a/interfaces/innerkits/libdbinder/include/dbinder_service.h +++ b/interfaces/innerkits/libdbinder/include/dbinder_service.h @@ -105,7 +105,6 @@ public: const std::string &deviceID, binder_uintptr_t binderObject, uint64_t pid = 0); bool RegisterRemoteProxy(std::u16string serviceName, sptr binderObject); bool RegisterRemoteProxy(std::u16string serviceName, int32_t systemAbilityId); - bool RegisterRemoteProxyInner(std::u16string serviceName, binder_uintptr_t binder); bool OnRemoteMessageTask(const struct DHandleEntryTxRx *message); std::shared_ptr QuerySessionObject(binder_uintptr_t stub); bool DetachDeathRecipient(sptr object); @@ -137,7 +136,7 @@ private: sptr QueryProxyObject(binder_uintptr_t binderObject); bool DetachSessionObject(binder_uintptr_t stub); bool AttachSessionObject(std::shared_ptr object, binder_uintptr_t stub); - sptr FindOrNewProxy(binder_uintptr_t binderObject); + sptr FindOrNewProxy(binder_uintptr_t binderObject, int32_t systemAbilityId); bool SendEntryToRemote(const sptr stub, uint32_t seqNumber); uint16_t AllocFreeSocketPort(); std::string GetLocalDeviceID(); @@ -161,6 +160,8 @@ private: std::string QueryBusNameObject(IPCObjectProxy *proxy); std::string GetDatabusNameByProxy(IPCObjectProxy *proxy); uint32_t GetSeqNumber(); + bool RegisterRemoteProxyInner(std::u16string serviceName, binder_uintptr_t binder); + bool CheckSystemAbilityId(int32_t systemAbilityId); private: DISALLOW_COPY_AND_MOVE(DBinderService); @@ -191,6 +192,8 @@ private: std::map, DBinderServiceStub *> noticeProxy_; std::map, sptr> deathRecipients_; std::map busNameObject_; + static constexpr int32_t FIRST_SYS_ABILITY_ID = 0x00000001; + static constexpr int32_t LAST_SYS_ABILITY_ID = 0x00ffffff; }; } // namespace OHOS #endif // OHOS_IPC_SERVICES_DBINDER_DBINDER_SERVICE_H diff --git a/services/dbinder/dbinder_service/src/dbinder_service.cpp b/services/dbinder/dbinder_service/src/dbinder_service.cpp index 8e63a2c8..9e4ce2c0 100755 --- a/services/dbinder/dbinder_service/src/dbinder_service.cpp +++ b/services/dbinder/dbinder_service/src/dbinder_service.cpp @@ -244,7 +244,7 @@ sptr DBinderService::FindOrNewDBinderStub(const std::u16stri sptr DBinderService::MakeRemoteBinder(const std::u16string &serviceName, const std::string &deviceID, binder_uintptr_t binderObject, uint64_t pid) { - if (IsDeviceIdIllegal(deviceID) || serviceName.length() == 0 || binderObject == 0) { + if (IsDeviceIdIllegal(deviceID) || serviceName.length() == 0) { DBINDER_LOGE("para is wrong device id length = %zu, service name length = %zu", deviceID.length(), serviceName.length()); return nullptr; @@ -295,7 +295,7 @@ bool DBinderService::SendEntryToRemote(const sptr stub, uint message->transType = GetRemoteTransType(); message->fromPort = 0; message->toPort = 0; - message->stubIndex = 0; + message->stubIndex = static_cast(std::atoi(stub->GetServiceName().c_str())); message->seqNumber = seqNumber; message->binderObject = stub->GetBinderObject(); message->stub = reinterpret_cast(stub.GetRefPtr()); @@ -360,7 +360,12 @@ bool DBinderService::InvokerRemoteDBinder(const sptr stub, u return true; } -sptr DBinderService::FindOrNewProxy(binder_uintptr_t binderObject) +bool DBinderService::CheckSystemAbilityId(int32_t systemAbilityId) +{ + return systemAbilityId >= FIRST_SYS_ABILITY_ID && systemAbilityId <= LAST_SYS_ABILITY_ID; +} + +sptr DBinderService::FindOrNewProxy(binder_uintptr_t binderObject, int32_t systemAbilityId) { sptr proxy = QueryProxyObject(binderObject); if (proxy != nullptr) { @@ -369,20 +374,18 @@ sptr DBinderService::FindOrNewProxy(binder_uintptr_t binderObject } /* proxy is null, attempt to get a new proxy */ std::u16string serviceName = GetRegisterService(binderObject); - if (serviceName.empty()) { - DBINDER_LOGE("service is not registered in this device"); + if (serviceName.empty() && !CheckSystemAbilityId(systemAbilityId)) { + DBINDER_LOGE("service is not registered in this device, saId:%{public}d", systemAbilityId); return nullptr; } - DBINDER_LOGI("new proxy serviceName = %s", Str16ToStr8(serviceName).c_str()); - auto manager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (manager == nullptr) { DBINDER_LOGE("when new proxy, find samgr fail!"); return nullptr; } - int digitalName = std::atoi(Str16ToStr8(serviceName).c_str()); + int digitalName = !serviceName.empty() ? std::atoi(Str16ToStr8(serviceName).c_str()) : systemAbilityId; proxy = manager->GetSystemAbility(digitalName); if (proxy != nullptr) { /* When the stub object dies, you need to delete the corresponding busName information */ @@ -397,6 +400,8 @@ sptr DBinderService::FindOrNewProxy(binder_uintptr_t binderObject DBINDER_LOGE("attach proxy object fail"); return nullptr; } + } else { + DBINDER_LOGW("GetSystemAbility from samgr error, saId:%{public}d", digitalName); } return proxy; } @@ -408,7 +413,7 @@ uint16_t DBinderService::AllocFreeSocketPort() bool DBinderService::OnRemoteInvokerMessage(const struct DHandleEntryTxRx *message) { - sptr proxy = FindOrNewProxy(message->binderObject); + sptr proxy = FindOrNewProxy(message->binderObject, static_cast(message->stubIndex)); if (proxy == nullptr) { DBINDER_LOGE("find and new proxy fail"); return false; -- Gitee