From 7e0ba0e8be7dbbee7ac326f872efb8442fb4559f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E6=B4=8B?= Date: Thu, 11 Sep 2025 19:39:06 +0800 Subject: [PATCH 1/2] fix:RPC multi-threaded client and server permission management. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 袁洋 Change-Id: I3bd046f53ef12604aa25409d2969b34424bbf7f5 --- .../dbinder/include/dbinder_softbus_client.h | 3 ++ .../dbinder/source/dbinder_softbus_client.cpp | 31 +++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/ipc/native/src/core/dbinder/include/dbinder_softbus_client.h b/ipc/native/src/core/dbinder/include/dbinder_softbus_client.h index f6335112..990145f3 100644 --- a/ipc/native/src/core/dbinder/include/dbinder_softbus_client.h +++ b/ipc/native/src/core/dbinder/include/dbinder_softbus_client.h @@ -19,6 +19,7 @@ #include #include #include +#include #include "dsoftbus_interface.h" #include "nocopyable.h" @@ -75,7 +76,9 @@ private: ShutdownFunc shutdownFunc_ = nullptr; std::mutex loadSoMutex_; + std::mutex permissionMutex_; std::atomic exitFlag_ = false; + std::map mapSessionRefCount_; bool isLoaded_ = false; void *soHandle_ = nullptr; }; diff --git a/ipc/native/src/core/dbinder/source/dbinder_softbus_client.cpp b/ipc/native/src/core/dbinder/source/dbinder_softbus_client.cpp index 1911756e..c6607c9a 100644 --- a/ipc/native/src/core/dbinder/source/dbinder_softbus_client.cpp +++ b/ipc/native/src/core/dbinder/source/dbinder_softbus_client.cpp @@ -19,6 +19,7 @@ #include "check_instance_exit.h" #include "ipc_debug.h" +#include "ipc_types.h" #include "log_tags.h" namespace OHOS { @@ -69,8 +70,9 @@ bool DBinderSoftbusClient::OpenSoftbusClientSo() int32_t DBinderSoftbusClient::DBinderGrantPermission(int32_t uid, int32_t pid, const std::string &socketName) { CHECK_INSTANCE_EXIT_WITH_RETVAL(exitFlag_, SOFTBUS_CLIENT_INSTANCE_EXIT); + std::lock_guard lockGuard(permissionMutex_); if (grantPermissionFunc_ != nullptr) { - return grantPermissionFunc_(uid, pid, socketName.c_str()); + goto DO_GRANT; } if (!OpenSoftbusClientSo()) { @@ -82,7 +84,17 @@ int32_t DBinderSoftbusClient::DBinderGrantPermission(int32_t uid, int32_t pid, c ZLOGE(LOG_LABEL, "dlsym DBinderGrantPermission fail, err msg:%{public}s", dlerror()); return SOFTBUS_CLIENT_DLSYM_FAILED; } - + goto DO_GRANT; + +DO_GRANT: + auto it = mapSessionRefCount_.find(socketName); + if (it != mapSessionRefCount_.end()) { + it->second++; + ZLOGI(LOG_LABEL, "alreadyhas permission socName:%{public}s refCount:%{public}d", socketName.c_str(), it->second); + return ERR_NONE; + } + mapSessionRefCount_.insert(std::pair, std::string, int32_t>(socketName, 1)); + ZLOGI(LOG_LABEL, "refCount +1 socketName:%{public}s", socketName.c_str()); return grantPermissionFunc_(uid, pid, socketName.c_str()); } @@ -90,7 +102,7 @@ int32_t DBinderSoftbusClient::DBinderRemovePermission(const std::string &socketN { CHECK_INSTANCE_EXIT_WITH_RETVAL(exitFlag_, SOFTBUS_CLIENT_INSTANCE_EXIT); if (removePermissionFunc_ != nullptr) { - return removePermissionFunc_(socketName.c_str()); + goto DO_REMOVE; } if (!OpenSoftbusClientSo()) { @@ -103,6 +115,19 @@ int32_t DBinderSoftbusClient::DBinderRemovePermission(const std::string &socketN return SOFTBUS_CLIENT_DLSYM_FAILED; } + goto DO_REMOVE; + +DO_REMOVE: + auto it = mapSessionRefCount_.find(socketName); + if (it != mapSessionRefCount_.end()) { + it->second--; + if (it->second <= 0) { + mapSessionRefCount_.erase(socketName); + return removePermissionFunc_(socketName.c_str()); + } + ZLOGI(LOG_LABEL, "still need permission socketName:%{public}s refCount:%{public}d", socketName.c_str(), it->second); + return ERR_NONE; + } return removePermissionFunc_(socketName.c_str()); } -- Gitee From 1c50c28a62d1ca1fe00eb770a1b8e409ec4e12b5 Mon Sep 17 00:00:00 2001 From: wangwang Date: Thu, 11 Sep 2025 11:50:01 +0000 Subject: [PATCH 2/2] update ipc/native/src/core/dbinder/source/dbinder_softbus_client.cpp. Signed-off-by: wangwang --- ipc/native/src/core/dbinder/source/dbinder_softbus_client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ipc/native/src/core/dbinder/source/dbinder_softbus_client.cpp b/ipc/native/src/core/dbinder/source/dbinder_softbus_client.cpp index c6607c9a..17f78171 100644 --- a/ipc/native/src/core/dbinder/source/dbinder_softbus_client.cpp +++ b/ipc/native/src/core/dbinder/source/dbinder_softbus_client.cpp @@ -93,7 +93,7 @@ DO_GRANT: ZLOGI(LOG_LABEL, "alreadyhas permission socName:%{public}s refCount:%{public}d", socketName.c_str(), it->second); return ERR_NONE; } - mapSessionRefCount_.insert(std::pair, std::string, int32_t>(socketName, 1)); + mapSessionRefCount_.insert(std::pair(socketName, 1)); ZLOGI(LOG_LABEL, "refCount +1 socketName:%{public}s", socketName.c_str()); return grantPermissionFunc_(uid, pid, socketName.c_str()); } -- Gitee