feat:Decoupling softbus clients

Signed-off-by: chenchong_666 <chenchong57@huawei.com>
This commit is contained in:
chenchong_666 2024-04-25 17:17:20 +08:00
parent 26f6b09918
commit 5a71ac7ab5
12 changed files with 100 additions and 65 deletions

View File

@ -49,7 +49,5 @@ config("dsoftbus_header_deps_config") {
"$DSOFTBUS_INTERFACES_PATH/interfaces/kits/bus_center",
"$DSOFTBUS_INTERFACES_PATH/interfaces/kits/common",
"$DSOFTBUS_INTERFACES_PATH/interfaces/kits/transport",
"$DSOFTBUS_INTERFACES_PATH/sdk/transmission/session/cpp/include",
"$DSOFTBUS_INTERFACES_PATH/sdk/transmission/session/cpp/src",
]
}

View File

@ -17,6 +17,7 @@
#define OHOS_IPC_DBINDER_SOFTBUS_CLIENT_H
#include <mutex>
#include <string>
#include "inner_socket.h"
#include "nocopyable.h"
@ -25,14 +26,23 @@
namespace OHOS {
enum {
DLOPNE_FAILED = -99,
DLSYM_FAILED,
INSTANCE_EXIT,
GET_DEVICE_INFO_FAILED,
SUCCESS = 0,
};
class DBinderSoftbusClient {
public:
static DBinderSoftbusClient& GetInstance();
DBinderSoftbusClient();
~DBinderSoftbusClient();
int32_t DBinderGrantPermission(int32_t uid, int32_t pid, const char *socketName);
int32_t GetLocalNodeDeviceInfo(const char *pkgName, NodeBasicInfo *info);
int32_t DBinderGrantPermission(int32_t uid, int32_t pid, const std::string &socketName);
int32_t DBinderRemovePermission(const std::string &socketName);
int32_t GetLocalNodeDeviceId(const std::string &pkgName, std::string &devId);
int32_t Socket(SocketInfo info);
int32_t Listen(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISocketListener *listener);
int32_t Bind(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISocketListener *listener);
@ -44,7 +54,8 @@ private:
bool OpenSoftbusClientSo();
using DBinderGrantPermissionFunc = int32_t (*)(int32_t, int32_t, const char*);
using GetLocalNodeDeviceInfoFunc = int32_t (*)(const char *, NodeBasicInfo*);
using DBinderRemovePermissionFunc = int32_t (*)(const char*);
using GetLocalNodeDeviceInfoFunc = int32_t (*)(const char*, NodeBasicInfo*);
using SocketFunc = int32_t (*)(SocketInfo);
using ListenFunc = int32_t (*)(int32_t, const QosTV[], uint32_t, const ISocketListener*);
using BindFunc = int32_t (*)(int32_t, const QosTV[], uint32_t, const ISocketListener*);
@ -52,6 +63,7 @@ private:
using ShutdownFunc = void (*)(int32_t);
DBinderGrantPermissionFunc grantPermissionFunc_ = nullptr;
DBinderRemovePermissionFunc removePermissionFunc_ = nullptr;
GetLocalNodeDeviceInfoFunc getLocalNodeDeviceInfoFunc_ = nullptr;
SocketFunc socketFunc_ = nullptr;
ListenFunc listenFunc_ = nullptr;

View File

@ -31,12 +31,6 @@ static constexpr const char *SOFTBUS_PATH_NAME = "/system/lib64/platformsdk/libs
static constexpr const char *SOFTBUS_PATH_NAME = "/system/lib/platformsdk/libsoftbus_client.z.so";
#endif
namespace {
constexpr int32_t DLOPNE_FAILED = -1;
constexpr int32_t DLSYM_FAILED = -2;
constexpr int32_t INSTANCE_EXIT = -5;
}
DBinderSoftbusClient& DBinderSoftbusClient::GetInstance()
{
static DBinderSoftbusClient instance;
@ -49,23 +43,22 @@ DBinderSoftbusClient::DBinderSoftbusClient()
DBinderSoftbusClient::~DBinderSoftbusClient()
{
dlclose(soHandle_);
exitFlag_ = true;
ZLOGI(LOG_LABEL, "destroy");
}
bool DBinderSoftbusClient::OpenSoftbusClientSo()
{
CHECK_INSTANCE_EXIT_WITH_RETVAL(exitFlag_, false); // 单例对象退出时的保护
CHECK_INSTANCE_EXIT_WITH_RETVAL(exitFlag_, false);
std::lock_guard<std::mutex> lockGuard(loadSoMutex_);
if (isLoaded_ && soHandle_ != nullptr) {
if (isLoaded_ && (soHandle_ != nullptr)) {
return true;
}
soHandle_ = dlopen(SOFTBUS_PATH_NAME, RTLD_NOW | RTLD_NODELETE);
if (soHandle_ == nullptr) {
ZLOGE(LOG_LABEL, "dlopen %{public}s failed, err = %{public}s", SOFTBUS_PATH_NAME, dlerror());
ZLOGE(LOG_LABEL, "dlopen %{public}s failed, err msg:%{public}s", SOFTBUS_PATH_NAME, dlerror());
return false;
}
@ -75,46 +68,75 @@ bool DBinderSoftbusClient::OpenSoftbusClientSo()
return true;
}
int32_t DBinderSoftbusClient::DBinderGrantPermission(int32_t uid, int32_t pid, const char *socketName)
int32_t DBinderSoftbusClient::DBinderGrantPermission(int32_t uid, int32_t pid, const std::string &socketName)
{
CHECK_INSTANCE_EXIT_WITH_RETVAL(exitFlag_, INSTANCE_EXIT);
if (grantPermissionFunc_ != nullptr) {
return grantPermissionFunc_(uid, pid, socketName);
return grantPermissionFunc_(uid, pid, socketName.c_str());
}
if (!OpenSoftbusClientSo()) {
ZLOGE(LOG_LABEL, "dlopen %{public}s failed.", SOFTBUS_PATH_NAME);
return DLOPNE_FAILED;
}
grantPermissionFunc_ = (DBinderGrantPermissionFunc)dlsym(soHandle_, "DBinderGrantPermission");
if (grantPermissionFunc_ == nullptr) {
ZLOGE(LOG_LABEL, "dlsym DBinderGrantPermission funcation fail, err = %{public}s", dlerror());
ZLOGE(LOG_LABEL, "dlsym DBinderGrantPermission fail, err msg:%{public}s", dlerror());
return DLSYM_FAILED;
}
return grantPermissionFunc_(uid, pid, socketName);
return grantPermissionFunc_(uid, pid, socketName.c_str());
}
int32_t DBinderSoftbusClient::GetLocalNodeDeviceInfo(const char *pkgName, NodeBasicInfo *info)
int32_t DBinderSoftbusClient::DBinderRemovePermission(const std::string &socketName)
{
CHECK_INSTANCE_EXIT_WITH_RETVAL(exitFlag_, INSTANCE_EXIT);
if (getLocalNodeDeviceInfoFunc_ != nullptr) {
return getLocalNodeDeviceInfoFunc_(pkgName, info);
if (removePermissionFunc_ != nullptr) {
return removePermissionFunc_(socketName.c_str());
}
if (!OpenSoftbusClientSo()) {
return DLOPNE_FAILED;
}
removePermissionFunc_ = (DBinderRemovePermissionFunc)dlsym(soHandle_, "DBinderRemovePermission");
if (removePermissionFunc_ == nullptr) {
ZLOGE(LOG_LABEL, "dlsym DBinderRemovePermission fail, err msg:%{public}s", dlerror());
return DLSYM_FAILED;
}
return removePermissionFunc_(socketName.c_str());
}
int32_t DBinderSoftbusClient::GetLocalNodeDeviceId(const std::string &pkgName, std::string &devId)
{
CHECK_INSTANCE_EXIT_WITH_RETVAL(exitFlag_, INSTANCE_EXIT);
NodeBasicInfo nodeBasicInfo;
if (getLocalNodeDeviceInfoFunc_ != nullptr) {
if (getLocalNodeDeviceInfoFunc_(pkgName.c_str(), &nodeBasicInfo) != 0) {
ZLOGE(LOG_LABEL, "Get local node device info failed");
return GET_DEVICE_INFO_FAILED;
}
devId = nodeBasicInfo.networkId;
return SUCCESS;
}
if (!OpenSoftbusClientSo()) {
ZLOGE(LOG_LABEL, "dlopen %{public}s failed.", SOFTBUS_PATH_NAME);
return DLOPNE_FAILED;
}
getLocalNodeDeviceInfoFunc_ = (GetLocalNodeDeviceInfoFunc)dlsym(soHandle_, "GetLocalNodeDeviceInfo");
if (getLocalNodeDeviceInfoFunc_ == nullptr) {
ZLOGE(LOG_LABEL, "dlsym DBinderGrantPermission funcation fail, err = %{public}s", dlerror());
ZLOGE(LOG_LABEL, "dlsym GetLocalNodeDeviceInfo fail, err msg:%{public}s", dlerror());
return DLSYM_FAILED;
}
return getLocalNodeDeviceInfoFunc_(pkgName, info);
if (getLocalNodeDeviceInfoFunc_(pkgName.c_str(), &nodeBasicInfo) != 0) {
ZLOGE(LOG_LABEL, "Get local node device info failed");
return GET_DEVICE_INFO_FAILED;
}
devId = nodeBasicInfo.networkId;
return SUCCESS;
}
int32_t DBinderSoftbusClient::Socket(SocketInfo info)
@ -125,13 +147,12 @@ int32_t DBinderSoftbusClient::Socket(SocketInfo info)
}
if (!OpenSoftbusClientSo()) {
ZLOGE(LOG_LABEL, "dlopen %{public}s failed.", SOFTBUS_PATH_NAME);
return DLOPNE_FAILED;
}
socketFunc_ = (SocketFunc)dlsym(soHandle_, "Socket");
if (socketFunc_ == nullptr) {
ZLOGE(LOG_LABEL, "dlsym Socket funcation fail, err = %{public}s", dlerror());
ZLOGE(LOG_LABEL, "dlsym Socket fail, err msg:%{public}s", dlerror());
return DLSYM_FAILED;
}
@ -147,13 +168,12 @@ int32_t DBinderSoftbusClient::Listen(
}
if (!OpenSoftbusClientSo()) {
ZLOGE(LOG_LABEL, "dlopen %{public}s failed.", SOFTBUS_PATH_NAME);
return DLOPNE_FAILED;
}
listenFunc_ = (ListenFunc)dlsym(soHandle_, "Listen");
if (listenFunc_ == nullptr) {
ZLOGE(LOG_LABEL, "dlsym Listen funcation fail, err = %{public}s", dlerror());
ZLOGE(LOG_LABEL, "dlsym Listen fail, err msg:%{public}s", dlerror());
return DLSYM_FAILED;
}
@ -169,13 +189,12 @@ int32_t DBinderSoftbusClient::Bind(
}
if (!OpenSoftbusClientSo()) {
ZLOGE(LOG_LABEL, "dlopen %{public}s failed.", SOFTBUS_PATH_NAME);
return DLOPNE_FAILED;
}
bindFunc_ = (BindFunc)dlsym(soHandle_, "Bind");
if (bindFunc_ == nullptr) {
ZLOGE(LOG_LABEL, "dlsym Bind funcation fail, err = %{public}s", dlerror());
ZLOGE(LOG_LABEL, "dlsym Bind fail, err msg:%{public}s", dlerror());
return DLSYM_FAILED;
}
@ -190,13 +209,12 @@ int32_t DBinderSoftbusClient::SendBytes(int32_t socket, const void *data, uint32
}
if (!OpenSoftbusClientSo()) {
ZLOGE(LOG_LABEL, "dlopen %{public}s failed.", SOFTBUS_PATH_NAME);
return DLOPNE_FAILED;
}
sendBytesFunc_ = (SendBytesFunc)dlsym(soHandle_, "SendBytes");
if (sendBytesFunc_ == nullptr) {
ZLOGE(LOG_LABEL, "dlsym Bind funcation fail, err = %{public}s", dlerror());
ZLOGE(LOG_LABEL, "dlsym SendBytes fail, err msg:%{public}s", dlerror());
return DLSYM_FAILED;
}
@ -212,17 +230,15 @@ void DBinderSoftbusClient::Shutdown(int32_t socket)
}
if (!OpenSoftbusClientSo()) {
ZLOGE(LOG_LABEL, "dlopen %{public}s failed.", SOFTBUS_PATH_NAME);
return;
}
shutdownFunc_ = (ShutdownFunc)dlsym(soHandle_, "Shutdown");
if (shutdownFunc_ == nullptr) {
ZLOGE(LOG_LABEL, "dlsym Shutdown funcation fail, err = %{public}s", dlerror());
ZLOGE(LOG_LABEL, "dlsym Shutdown fail, err msg:%{public}s", dlerror());
return;
}
shutdownFunc_(socket);
return;
}
} // namespace OHOS
} // namespace OHOS

View File

@ -648,7 +648,7 @@ std::string IPCObjectStub::CreateSessionName(int uid, int pid)
{
std::string sessionName = DBINDER_SOCKET_NAME_PREFIX +
std::to_string(uid) + std::string("_") + std::to_string(pid);
if (DBinderSoftbusClient::GetInstance().DBinderGrantPermission(uid, pid, sessionName.c_str()) != ERR_NONE) {
if (DBinderSoftbusClient::GetInstance().DBinderGrantPermission(uid, pid, sessionName) != ERR_NONE) {
ZLOGE(LABEL, "fail to Grant Permission softbus name");
return "";
}

View File

@ -469,12 +469,12 @@ std::string IPCProcessSkeleton::GetLocalDeviceID()
std::lock_guard<std::mutex> lockGuard(databusProcMutex_);
std::string pkgName = std::string(DBINDER_PKG_NAME) + "_" + std::to_string(getpid());
NodeBasicInfo nodeBasicInfo;
if (DBinderSoftbusClient::GetInstance().GetLocalNodeDeviceInfo(pkgName.c_str(), &nodeBasicInfo) != 0) {
ZLOGE(LOG_LABEL, "Get local node device info failed");
return "";
std::string networkId = "";
if (DBinderSoftbusClient::GetInstance().GetLocalNodeDeviceId(pkgName.c_str(), networkId) != SUCCESS) {
ZLOGE(LOG_LABEL, "Get local node device id failed");
}
std::string networkId(nodeBasicInfo.networkId);
return networkId;
}

View File

@ -66,12 +66,12 @@ DBinderService::~DBinderService()
std::string DBinderService::GetLocalDeviceID()
{
std::string pkgName = "DBinderService";
NodeBasicInfo nodeBasicInfo;
if (DBinderSoftbusClient::GetInstance().GetLocalNodeDeviceInfo(pkgName.c_str(), &nodeBasicInfo) != 0) {
DBINDER_LOGE(LOG_LABEL, "Get local node device info failed");
return "";
std::string networkId = "";
if (DBinderSoftbusClient::GetInstance().GetLocalNodeDeviceId(pkgName.c_str(), networkId) != SUCCESS) {
DBINDER_LOGE(LOG_LABEL, "Get local node device id failed");
}
std::string networkId(nodeBasicInfo.networkId);
return networkId;
}
@ -622,7 +622,7 @@ std::string DBinderService::GetDatabusNameByProxy(IPCObjectProxy *proxy)
std::string DBinderService::CreateDatabusName(int uid, int pid)
{
std::string sessionName = "DBinder" + std::to_string(uid) + std::string("_") + std::to_string(pid);
if (DBinderSoftbusClient::GetInstance().DBinderGrantPermission(uid, pid, sessionName.c_str()) != ERR_NONE) {
if (DBinderSoftbusClient::GetInstance().DBinderGrantPermission(uid, pid, sessionName) != ERR_NONE) {
DBINDER_LOGE(LOG_LABEL, "fail to Grant Permission softbus name");
DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_GRANT_PERMISSION_FAIL, __FUNCTION__);
return "";

View File

@ -167,7 +167,7 @@ int32_t DBinderRemoteListener::CreateClientSocket(const std::string &peerNetwork
socketId, DBinderService::ConvertToSecureDeviceID(peerNetworkId).c_str());
{
std::lock_guard<std::mutex> lockGuard(clientSocketMutex_);
clientSocketInfos_[peerNetworkId] = socketId;
}
@ -190,7 +190,7 @@ bool DBinderRemoteListener::StartListener()
int pid = static_cast<int>(getpid());
int uid = static_cast<int>(getuid());
int32_t ret = DBinderSoftbusClient::GetInstance().DBinderGrantPermission(uid, pid, OWN_SESSION_NAME.c_str());
int32_t ret = DBinderSoftbusClient::GetInstance().DBinderGrantPermission(uid, pid, OWN_SESSION_NAME);
if (ret != ERR_NONE) {
DBINDER_LOGE(LOG_LABEL, "GrantPermission failed softbus name:%{public}s", OWN_SESSION_NAME.c_str());
DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_GRANT_PERMISSION_FAIL, __FUNCTION__);

View File

@ -57,6 +57,7 @@ ohos_distributedtest("DbinderTest") {
external_deps = [
"c_utils:utils",
"hilog:libhilog",
"hitrace:libhitracechain",
"ipc:ipc_core",
"samgr:samgr_proxy",
]
@ -75,7 +76,10 @@ ohos_distributedtest("DbinderTestAgent") {
"$DBINDER_TEST_ROOT/distributedtest/src/dbinder_test_service_skeleton.cpp",
]
configs = [ ":libtestdbinder_config" ]
configs = [
":libtestdbinder_config",
"$SUBSYSTEM_DIR/config:dsoftbus_header_deps_config",
]
deps = [
":dbinder_send",
@ -86,8 +90,8 @@ ohos_distributedtest("DbinderTestAgent") {
external_deps = [
"c_utils:utils",
"dsoftbus:softbus_client",
"hilog:libhilog",
"hitrace:libhitracechain",
"samgr:samgr_proxy",
]
@ -110,6 +114,7 @@ ohos_executable("dbinder_test") {
external_deps = [
"c_utils:utils",
"hilog:libhilog",
"hitrace:libhitracechain",
"samgr:samgr_proxy",
]
@ -133,6 +138,7 @@ ohos_executable("dbinder_send") {
external_deps = [
"c_utils:utils",
"hilog:libhilog",
"hitrace:libhitracechain",
"samgr:samgr_proxy",
]

View File

@ -24,15 +24,15 @@ int main(int argc __attribute__((unused)), char **argv __attribute__((unused)))
std::string str;
std::cout << "Please enter a string to start linking dynamic libraries." << std::endl;
(void)getline(std::cin, str);
std::cin >> str;
std::cout << str << std::endl;
std::string pkgName = "dbinderService";
NodeBasicInfo nodeBasicInfo;
(void)OHOS::DBinderSoftbusClient::GetInstance().GetLocalNodeDeviceInfo(pkgName.c_str(), &nodeBasicInfo);
std::string networkId;
(void)OHOS::DBinderSoftbusClient::GetInstance().GetLocalNodeDeviceId(pkgName.c_str(), networkId);
std::cout << "Please enter a string to exit the program." << std::endl;
(void)getline(std::cin, str);
std::cin >> str;
std::cout << str << std::endl;
return 0;

View File

@ -58,7 +58,6 @@ ohos_moduletest("RPCSERVERTEST") {
external_deps = [
"c_utils:utils",
"dsoftbus:softbus_client",
"ipc:ipc_core",
"samgr:samgr_proxy",
]

12
services/dbinder/test/unittest/BUILD.gn Executable file → Normal file
View File

@ -13,6 +13,7 @@
import("//build/test.gni")
DSOFTBUS_PATH = "//foundation/communication/dsoftbus"
SUBSYSTEM_DIR = "//foundation/communication/ipc"
IPC_CORE_ROOT = "$SUBSYSTEM_DIR/ipc/native"
MODULE_OUTPUT_PATH = "ipc"
@ -21,9 +22,10 @@ ohos_unittest("RPCDbinderTest") {
module_out_path = MODULE_OUTPUT_PATH
include_dirs = [
"$DSOFTBUS_PATH/sdk/transmission/session/cpp/src",
"$DSOFTBUS_PATH/sdk/transmission/session/cpp/include",
"$IPC_CORE_ROOT/c/manager/include",
"$SUBSYSTEM_DIR/utils/include",
"//foundation/communication/dsoftbus/sdk/transmission/session/cpp/src",
"$SUBSYSTEM_DIR/interfaces/innerkits/ipc_core/include",
"$IPC_CORE_ROOT/c/rpc/include",
]
@ -35,7 +37,7 @@ ohos_unittest("RPCDbinderTest") {
"dbinder_service_unittest.cpp",
]
configs = []
configs = [ "$SUBSYSTEM_DIR/config:dsoftbus_header_deps_config" ]
deps = [
"$SUBSYSTEM_DIR/interfaces/innerkits/ipc_core:ipc_core",
@ -44,7 +46,6 @@ ohos_unittest("RPCDbinderTest") {
external_deps = [
"c_utils:utils",
"dsoftbus:softbus_client",
"hilog:libhilog",
"ipc:libdbinder",
]
@ -55,5 +56,8 @@ ohos_unittest("RPCDbinderTest") {
###############################################################################
group("unittest") {
testonly = true
deps = [ ":RPCDbinderTest" ]
deps = [
":RPCDbinderTest",
"../auxiliary:Dynamic_loading_softbus_client_test",
]
}

View File

@ -32,7 +32,6 @@ const unsigned int LOG_ID_IPC_OTHER = LOG_ID_IPC_BASE | 0x09;
const unsigned int LOG_ID_IPC_RUST = LOG_ID_IPC_BASE | 0x0A;
const unsigned int LOG_ID_IPC_PARCEL = LOG_ID_IPC_BASE | 0x0B;
const unsigned int LOG_ID_IPC_PAYLOAD_STATISTICS_IMPL = LOG_ID_IPC_BASE | 0x0C;
const unsigned int LOG_ID_IPC_DBINDER_SOFTBUS_CLIENT = LOG_ID_IPC_BASE | 0x0D;
const unsigned int LOG_ID_IPC_TEST = 0xD000F00;
const unsigned int LOG_ID_RPC_COMMON = LOG_ID_IPC_BASE | 0x10;
@ -42,5 +41,6 @@ const unsigned int LOG_ID_RPC_DBINDER_SER_STUB = LOG_ID_IPC_BASE | 0x13;
const unsigned int LOG_ID_RPC_DBINDER_INVOKER = LOG_ID_IPC_BASE | 0x14;
const unsigned int LOG_ID_RPC_REMOTE_LISTENER = LOG_ID_IPC_BASE | 0x15;
const unsigned int LOG_ID_RPC_DBINDER_CB_STUB = LOG_ID_IPC_BASE | 0x16;
const unsigned int LOG_ID_IPC_DBINDER_SOFTBUS_CLIENT = LOG_ID_IPC_BASE | 0x17;
} // namespace OHOS
#endif // OHOS_COMMUNICATION_LOG_TAGS_H