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 f63351127a6df62692c38428503d4791f8b13608..990145f39b38a260fcabe7c6b195ad65304d80ce 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 1911756e669ba21760e915f48bbdcc126f345a65..17f78171ee903755925d0782a47ae64196094c18 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(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()); }