diff --git a/interfaces/innerkits/c/dbinder/include/dbinder_service.h b/interfaces/innerkits/c/dbinder/include/dbinder_service.h index 9b424467..42331b8c 100644 --- a/interfaces/innerkits/c/dbinder/include/dbinder_service.h +++ b/interfaces/innerkits/c/dbinder/include/dbinder_service.h @@ -16,44 +16,18 @@ #ifndef DBINDER_SERVICE_H #define DBINDER_SERVICE_H -#include -#include -#include #include -#include "dbinder_types.h" -#include "utils_list.h" - #ifdef __cplusplus #if __cplusplus extern "C" { #endif #endif -typedef struct { - struct DHandleEntryHead head; - uint32_t transType; - uint32_t dBinderCode; - uint16_t fromPort; - uint16_t toPort; - uint64_t stubIndex; - uint32_t seqNumber; - uintptr_t binderObject; - struct DeviceIdInfo deviceIdInfo; - uintptr_t stub; - uint16_t serviceNameLength; - char serviceName[SERVICENAME_LENGTH + 1]; - uint32_t pid; - uint32_t uid; -} DHandleEntryTxRx; - int32_t StartDBinderService(void); int32_t RegisterRemoteProxy(const void *name, uint32_t len, int32_t systemAbility); int32_t MakeRemoteBinder(const void *serviceName, uint32_t nameLen, const char *deviceID, uint32_t idLen, uintptr_t binderObject, uint64_t pid, void *remoteObject); -int32_t OnRemoteMessageTask(const DHandleEntryTxRx *message); -SessionInfo *QuerySessionObject(uintptr_t stub); -void DetachProxyObject(ProxyObject *proxy); #ifdef __cplusplus #if __cplusplus diff --git a/ipc/native/c/manager/include/ipc_process_skeleton.h b/ipc/native/c/manager/include/ipc_process_skeleton.h index 87a31aeb..d6d4cb92 100644 --- a/ipc/native/c/manager/include/ipc_process_skeleton.h +++ b/ipc/native/c/manager/include/ipc_process_skeleton.h @@ -72,7 +72,7 @@ int32_t OnRemoteRequestInner(uint32_t code, IpcIo *data, IpcIo *reply, bool OnThreadTerminated(pthread_t threadId); void SendObituary(DeathCallback *deathCallback); void DeleteDeathCallback(DeathCallback *deathCallback); -void WaitForProxyInit(int32_t handle); +void WaitForProxyInit(SvcIdentity *svc); #ifdef __cplusplus #if __cplusplus } diff --git a/ipc/native/c/manager/include/ipc_thread_pool.h b/ipc/native/c/manager/include/ipc_thread_pool.h index 90cffecf..066cfd6c 100644 --- a/ipc/native/c/manager/include/ipc_thread_pool.h +++ b/ipc/native/c/manager/include/ipc_thread_pool.h @@ -47,7 +47,7 @@ typedef struct { char callerDeviceID[DEVICEID_LENGTH + 1]; bool stopWorkThread; uint64_t seqNumber; - uint32_t clientFd; + uint32_t sessionId; } ThreadContext; typedef struct { diff --git a/ipc/native/c/manager/src/ipc_process_skeleton.c b/ipc/native/c/manager/src/ipc_process_skeleton.c index c71b8874..4345edcd 100644 --- a/ipc/native/c/manager/src/ipc_process_skeleton.c +++ b/ipc/native/c/manager/src/ipc_process_skeleton.c @@ -270,7 +270,7 @@ static uint32_t SetDeathHandlerPair(DeathCallback *node, uint32_t index, OnRemot int32_t ProcessAddDeathRecipient(int32_t handle, OnRemoteDead deathFunc, void *args, uint32_t *cbId) { - int32_t ret = ERR_NONE; + int32_t ret = ERR_INVALID_PARAM; if (g_ipcSkeleton == NULL) { return ERR_IPC_SKELETON_NOT_INIT; } @@ -419,9 +419,9 @@ void DeleteDeathCallback(DeathCallback *deathCallback) free(deathCallback); } -void WaitForProxyInit(int32_t handle) +void WaitForProxyInit(SvcIdentity *svc) { RPC_LOG_INFO("ipc skeleton wait for proxy init"); - OnFirstStrongRef(handle); - UpdateProtoIfNeed(handle); + OnFirstStrongRef(svc->handle); + UpdateProtoIfNeed(svc); } \ No newline at end of file diff --git a/ipc/native/c/manager/src/serializer.c b/ipc/native/c/manager/src/serializer.c index 6f3ad1d8..815563f6 100644 --- a/ipc/native/c/manager/src/serializer.c +++ b/ipc/native/c/manager/src/serializer.c @@ -202,9 +202,9 @@ bool ReadRemoteObject(IpcIo *io, SvcIdentity *svc) svc->handle = MIN_BINDER_HANDLE; svc->cookie = obj->cookie; } else { - WaitForProxyInit(obj->handle); svc->handle = obj->handle; svc->cookie = obj->cookie; + WaitForProxyInit(svc); } return true; } @@ -273,7 +273,7 @@ bool ReadRemoteObject(IpcIo *io, SvcIdentity *svc) svc->handle = svcId->handle; svc->token = svcId->token; svc->cookie = svcId->cookie; - WaitForProxyInit(svcId->handle); + WaitForProxyInit(svcId); return true; } diff --git a/ipc/native/c/rpc/include/rpc_process_skeleton.h b/ipc/native/c/rpc/include/rpc_process_skeleton.h index 0f48c3d4..079c9b65 100644 --- a/ipc/native/c/rpc/include/rpc_process_skeleton.h +++ b/ipc/native/c/rpc/include/rpc_process_skeleton.h @@ -50,7 +50,7 @@ typedef struct { typedef struct { UTILS_DL_LIST list; pthread_t threadId; - uint32_t listenFd; + uint32_t sessionId; uint32_t packageSize; char *buffer; } ThreadProcessInfo; @@ -104,7 +104,7 @@ typedef struct { size_t bufferSize; size_t offsetsSize; uintptr_t offsets; - uint32_t socketId; + uint32_t sessionId; void *buffer; } ThreadMessageInfo; @@ -134,7 +134,7 @@ ThreadMessageInfo *QueryThreadBySeqNumber(uint64_t seqNumber); void WakeUpThreadBySeqNumber(uint64_t seqNumber, uint32_t handle); int32_t RpcOnRemoteRequestInner(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option, IpcObjectStub *objectStub); -void UpdateProtoIfNeed(int32_t handle); +void UpdateProtoIfNeed(SvcIdentity *svc); void WakeUpDataThread(pthread_t threadId); uint64_t GetNewStubIndex(void); diff --git a/ipc/native/c/rpc/ipc_adapter/include/ipc_proxy_inner.h b/ipc/native/c/rpc/ipc_adapter/include/ipc_proxy_inner.h index 90f269ef..b500b006 100644 --- a/ipc/native/c/rpc/ipc_adapter/include/ipc_proxy_inner.h +++ b/ipc/native/c/rpc/ipc_adapter/include/ipc_proxy_inner.h @@ -29,7 +29,7 @@ int32_t InvokerListenThread(ProxyObject *proxyObject, const char *localDeviceID, const char *remoteDeviceID, uint32_t pid, uint32_t uid, IpcIo *reply, uintptr_t *ptr); int32_t GetPidAndUidInfo(ProxyObject *proxy); char *GetDataBusName(void); -void UpdateProto(int32_t handle); +void UpdateProto(SvcIdentity *svc); #ifdef __cplusplus } diff --git a/ipc/native/c/rpc/ipc_adapter/mini/ipc_proxy_inner.c b/ipc/native/c/rpc/ipc_adapter/mini/ipc_proxy_inner.c index 1897a6d8..458629fe 100644 --- a/ipc/native/c/rpc/ipc_adapter/mini/ipc_proxy_inner.c +++ b/ipc/native/c/rpc/ipc_adapter/mini/ipc_proxy_inner.c @@ -23,6 +23,7 @@ #include "rpc_process_skeleton.h" #include "dbinder_invoker.h" #include "dbinder_types.h" +#include "dbinder_service_inner.h" #include "rpc_errno.h" #include "rpc_log.h" @@ -169,12 +170,12 @@ static char *CreateDatabusName(void) return sessionName; } -static int GetSessionFromDBinderService(uint32_t handle) +static int GetSessionFromDBinderService(SvcIdentity *svc) { RPC_LOG_INFO("GetSessionFromDBinderService start"); int32_t proto = IF_PROT_DATABUS; - SessionInfo *session = QuerySessionObject((uintptr_t)handle); + SessionInfo *session = QuerySessionObject(svc->cookie); if (session == NULL) { RPC_LOG_ERROR("client find session is null"); return proto; @@ -197,7 +198,7 @@ static int GetSessionFromDBinderService(uint32_t handle) free(sessionObject); return proto; } - handleToIndex->handle = handle; + handleToIndex->handle = svc->handle; handleToIndex->index = session->stubIndex; if (AttachHandleToIndex(handleToIndex) != ERR_NONE) { @@ -216,14 +217,15 @@ static int GetSessionFromDBinderService(uint32_t handle) return proto; } - UpdateClientSession(handle, sessionObject, localBusName, session->serviceName, session->deviceIdInfo.toDeviceId); + UpdateClientSession(svc->handle, sessionObject, localBusName, + session->serviceName, session->deviceIdInfo.toDeviceId); return proto; } -void UpdateProto(int32_t handle) +void UpdateProto(SvcIdentity *svc) { - if (handle < 0) { + if (svc->handle < 0) { RPC_LOG_ERROR("UpdateProto handle invalid"); return; } @@ -233,11 +235,11 @@ void UpdateProto(int32_t handle) RPC_LOG_ERROR("UpdateProto threadContext is null"); return; } - HandleSessionList *sessionObject = QueryProxySession(handle); + HandleSessionList *sessionObject = QueryProxySession(svc->handle); if (sessionObject != NULL) { threadContext->proto = IF_PROT_DATABUS; return; } - threadContext->proto = GetSessionFromDBinderService(handle); + threadContext->proto = GetSessionFromDBinderService(svc); RPC_LOG_INFO("UpdateProto get proto: %d", threadContext->proto); } \ No newline at end of file diff --git a/ipc/native/c/rpc/ipc_adapter/small/ipc_proxy_inner.c b/ipc/native/c/rpc/ipc_adapter/small/ipc_proxy_inner.c index 35d57493..f7aa9e30 100644 --- a/ipc/native/c/rpc/ipc_adapter/small/ipc_proxy_inner.c +++ b/ipc/native/c/rpc/ipc_adapter/small/ipc_proxy_inner.c @@ -225,9 +225,9 @@ char *GetDataBusName(void) return sessionName; } -void UpdateProto(int32_t handle) +void UpdateProto(SvcIdentity *svc) { - if (handle < 0) { + if (svc->handle < 0) { RPC_LOG_ERROR("UpdateProto handle invalid"); return; } @@ -237,11 +237,11 @@ void UpdateProto(int32_t handle) RPC_LOG_ERROR("UpdateProto threadContext is null"); return; } - HandleSessionList *sessionObject = QueryProxySession(handle); + HandleSessionList *sessionObject = QueryProxySession(svc->handle); if (sessionObject != NULL) { threadContext->proto = IF_PROT_DATABUS; return; } - threadContext->proto = GetSessionFromDBinderService(handle); + threadContext->proto = GetSessionFromDBinderService(svc->handle); RPC_LOG_INFO("UpdateProto get proto: %d", threadContext->proto); } \ No newline at end of file diff --git a/ipc/native/c/rpc/src/dbinder_invoker.c b/ipc/native/c/rpc/src/dbinder_invoker.c index e9ca4591..fa3b77b2 100644 --- a/ipc/native/c/rpc/src/dbinder_invoker.c +++ b/ipc/native/c/rpc/src/dbinder_invoker.c @@ -26,6 +26,7 @@ #include "rpc_errno.h" #include "rpc_trans.h" #include "rpc_trans_callback.h" +#include "rpc_process_skeleton.h" #include "ipc_skeleton.h" #include "ipc_process_skeleton.h" #include "ipc_thread_pool.h" @@ -46,14 +47,14 @@ void DeleteRpcInvoker(RemoteInvoker *remoteInvoker) free(remoteInvoker); } -static HandleSessionList *GetSessionObject(uint32_t handle, uint32_t socketId) +static HandleSessionList *GetSessionObject(uint32_t handle, uint32_t sessionId) { if (handle != 0) { /* transact case */ return QueryProxySession(handle); } else { /* reply case */ - return QueryStubSession(socketId); + return QueryStubSession(sessionId); } } @@ -176,11 +177,11 @@ static int32_t MoveTransData2Buffer(HandleSessionList *sessionObject, dbinder_tr } static HandleSessionList *WriteTransaction(int cmd, MessageOption option, int32_t handle, - int32_t socketId, uint32_t code, IpcIo *data, uint64_t *seqNumber, int status) + int32_t sessionId, uint32_t code, IpcIo *data, uint64_t *seqNumber, int status) { - HandleSessionList *sessionObject = GetSessionObject(handle, socketId); + HandleSessionList *sessionObject = GetSessionObject(handle, sessionId); if (sessionObject == NULL) { - RPC_LOG_ERROR("session is not exist for listenFd = %d, handle = %d", socketId, handle); + RPC_LOG_ERROR("session is not exist for sessionId = %d, handle = %d", sessionId, handle); return NULL; } @@ -253,7 +254,7 @@ static ThreadMessageInfo *MakeThreadMessageInfo(uint64_t seqNumber, uint32_t han messageInfo->seqNumber = seqNumber; messageInfo->buffer = NULL; messageInfo->offsets = 0; - messageInfo->socketId = handle; + messageInfo->sessionId = handle; return messageInfo; } @@ -329,16 +330,16 @@ static int32_t SendOrWaitForCompletion(int userWaitTime, uint64_t seqNumber, return WaitForReply(seqNumber, reply, sessionOfPeer->handle, userWaitTime, buffer); } -static int32_t GetClientFd(void) +static int32_t GetCallerSessionId(void) { ThreadContext *threadContext = GetCurrentThreadContext(); - return threadContext->clientFd; + return threadContext->sessionId; } static int32_t SendReply(IpcIo *reply, uint32_t flags, int32_t result) { uint64_t seqNumber = 0; - HandleSessionList *sessionObject = WriteTransaction(BC_REPLY, flags, 0, GetClientFd(), + HandleSessionList *sessionObject = WriteTransaction(BC_REPLY, flags, 0, GetCallerSessionId(), 0, reply, &seqNumber, result); if (seqNumber == 0) { @@ -349,7 +350,7 @@ static int32_t SendReply(IpcIo *reply, uint32_t flags, int32_t result) return ERR_NONE; } -static void ProcessTransaction(const dbinder_transaction_data *tr, uint32_t listenFd) +static void ProcessTransaction(const dbinder_transaction_data *tr, uint32_t sessionId) { if (tr == NULL || tr->cookie == 0) { return; @@ -384,10 +385,10 @@ static void ProcessTransaction(const dbinder_transaction_data *tr, uint32_t list RPC_LOG_ERROR("stub is invalid, has not OnReceive or Request"); } if (!(option & TF_OP_ASYNC)) { - threadContext->clientFd = listenFd; + threadContext->sessionId = sessionId; threadContext->seqNumber = senderSeqNumber; SendReply(&reply, 0, result); - threadContext->clientFd = 0; + threadContext->sessionId = 0; threadContext->seqNumber = 0; } @@ -398,7 +399,7 @@ static void ProcessTransaction(const dbinder_transaction_data *tr, uint32_t list } } -static void ProcessReply(const dbinder_transaction_data *tr, uint32_t listenFd) +static void ProcessReply(const dbinder_transaction_data *tr, uint32_t sessionId) { ThreadMessageInfo *messageInfo = QueryThreadBySeqNumber(tr->seqNumber); if (messageInfo == NULL) { @@ -412,14 +413,14 @@ static void ProcessReply(const dbinder_transaction_data *tr, uint32_t listenFd) if (messageInfo->buffer == NULL) { RPC_LOG_ERROR("some thread is waiting for reply message, but no memory"); /* wake up sender thread */ - WakeUpThreadBySeqNumber(tr->seqNumber, listenFd); + WakeUpThreadBySeqNumber(tr->seqNumber, sessionId); return; } if (memcpy_s(messageInfo->buffer, bufferSize, tr->buffer, bufferSize) != EOK) { RPC_LOG_ERROR("messageInfo buffer memset failed"); free(messageInfo->buffer); - WakeUpThreadBySeqNumber(tr->seqNumber, listenFd); + WakeUpThreadBySeqNumber(tr->seqNumber, sessionId); return; } @@ -427,10 +428,10 @@ static void ProcessReply(const dbinder_transaction_data *tr, uint32_t listenFd) messageInfo->bufferSize = tr->buffer_size; messageInfo->offsetsSize = tr->offsets_size; messageInfo->offsets = tr->offsets; - messageInfo->socketId = listenFd; + messageInfo->sessionId = sessionId; /* wake up sender thread */ - WakeUpThreadBySeqNumber(tr->seqNumber, listenFd); + WakeUpThreadBySeqNumber(tr->seqNumber, sessionId); } static void OnTransaction(ThreadProcessInfo *processInfo) @@ -442,9 +443,9 @@ static void OnTransaction(ThreadProcessInfo *processInfo) tr->buffer = (char *)(processInfo->buffer + sizeof(dbinder_transaction_data)); if (tr->cmd == BC_TRANSACTION) { - ProcessTransaction(tr, processInfo->listenFd); + ProcessTransaction(tr, processInfo->sessionId); } else if (tr->cmd == BC_REPLY) { - ProcessReply(tr, processInfo->listenFd); + ProcessReply(tr, processInfo->sessionId); } } @@ -469,7 +470,7 @@ static ThreadProcessInfo *MakeThreadProcessInfo(uint32_t handle, const char *inB free(processInfo); return NULL; } - processInfo->listenFd = handle; + processInfo->sessionId = handle; processInfo->packageSize = size; return processInfo; diff --git a/ipc/native/c/rpc/src/rpc_process_skeleton.c b/ipc/native/c/rpc/src/rpc_process_skeleton.c index b5053f69..f7faf908 100644 --- a/ipc/native/c/rpc/src/rpc_process_skeleton.c +++ b/ipc/native/c/rpc/src/rpc_process_skeleton.c @@ -27,8 +27,6 @@ #include "rpc_errno.h" #include "rpc_log.h" -#define USECTONSEC 1000 - static RpcSkeleton g_rpcSkeleton = { .lock = PTHREAD_MUTEX_INITIALIZER, .isServerCreated = -1 @@ -440,9 +438,9 @@ void WakeUpThreadBySeqNumber(uint64_t seqNumber, uint32_t handle) return; } - if (handle != messageInfo->socketId) { + if (handle != messageInfo->sessionId) { RPC_LOG_ERROR("error! handle is not equal messageInfo, handle = %d, messageFd = %u", handle, - messageInfo->socketId); + messageInfo->sessionId); return; } if (pthread_equal(messageInfo->threadId, pthread_self()) == 0) { @@ -482,10 +480,10 @@ int32_t RpcOnRemoteRequestInner(uint32_t code, IpcIo *data, IpcIo *reply, Messag return result; } -void UpdateProtoIfNeed(int32_t handle) +void UpdateProtoIfNeed(SvcIdentity *svc) { - RPC_LOG_INFO("rpc manager update proto, handle %d", handle); - UpdateProto(handle); + RPC_LOG_INFO("rpc manager update proto, handle %d", svc->handle); + UpdateProto(svc); } uint64_t GetNewStubIndex(void) diff --git a/ipc/native/c/rpc/src/rpc_process_skeleton_virtual.c b/ipc/native/c/rpc/src/rpc_process_skeleton_virtual.c index bba055c1..31fb604e 100644 --- a/ipc/native/c/rpc/src/rpc_process_skeleton_virtual.c +++ b/ipc/native/c/rpc/src/rpc_process_skeleton_virtual.c @@ -34,7 +34,7 @@ int32_t RpcOnRemoteRequestInner(uint32_t code, IpcIo *data, IpcIo *reply, Messag return ERR_NOT_RPC; } -void UpdateProtoIfNeed(int32_t handle) +void UpdateProtoIfNeed(SvcIdentity *svc) { return; } diff --git a/ipc/native/c/rpc/trans_adapter/include/rpc_trans.h b/ipc/native/c/rpc/trans_adapter/include/rpc_trans.h index 1d8983dd..fe1ac837 100644 --- a/ipc/native/c/rpc/trans_adapter/include/rpc_trans.h +++ b/ipc/native/c/rpc/trans_adapter/include/rpc_trans.h @@ -34,10 +34,10 @@ typedef struct { int32_t (*Connect)(const char *SaSessionName, const char *peerDeviceId, void *args); int32_t (*Disconnect)(int32_t sessionId); int32_t (*Send)(int32_t sessionId, const void *data, uint32_t len); + char *(*GetLocalDeviceID)(void); } TransInterface; TransInterface *GetRpcTrans(void); -char *GetLocalDeviceID(void); #ifdef __cplusplus } diff --git a/ipc/native/c/rpc/trans_adapter/src/rpc_trans.c b/ipc/native/c/rpc/trans_adapter/src/rpc_trans.c index 08371b14..7bb465de 100644 --- a/ipc/native/c/rpc/trans_adapter/src/rpc_trans.c +++ b/ipc/native/c/rpc/trans_adapter/src/rpc_trans.c @@ -27,14 +27,5 @@ TransInterface *GetRpcTrans(void) return GetSocketTrans(); #endif - return NULL; -} - -char *GetLocalDeviceID(void) -{ -#if defined(RPC_SOCKET_TRANS) - return GetSocketLocalDeviceID(); -#endif - return NULL; } \ No newline at end of file diff --git a/ipc/test/rpc/client/rpc_client.c b/ipc/test/rpc/client/rpc_client.c index a29e5ed0..e906fb55 100644 --- a/ipc/test/rpc/client/rpc_client.c +++ b/ipc/test/rpc/client/rpc_client.c @@ -106,13 +106,8 @@ int main(int argc, char *argv[]) return -1; } - RPC_LOG_INFO("argv 1 is %s", argv[1]); - if (strcmp(argv[1], "deviceId") == 0) { - RPC_LOG_INFO("local deviceId is %s", GetLocalDeviceID()); - return 0; - } - const char *deviceId = argv[1]; + RPC_LOG_INFO("input deviceid is %s", deviceId); IpcIo data1; uint8_t tmpData1[IPC_LENGTH]; diff --git a/ipc/test/rpc/include/rpc_mini_samgr.h b/ipc/test/rpc/include/rpc_mini_samgr.h index c02dc43d..ac42fb37 100644 --- a/ipc/test/rpc/include/rpc_mini_samgr.h +++ b/ipc/test/rpc/include/rpc_mini_samgr.h @@ -26,9 +26,9 @@ extern "C" { void RpcStartSamgr(void); int32_t AddSystemAbility(int32_t saId, SvcIdentity *sid); -SvcIdentity *GetSystemAbilityById(int32_t systemAbility); -int32_t AddRemoteSystemAbility(int32_t saId, SvcIdentity *sid); -int32_t GetRemoteSystemAbility(int32_t saId, const char* deviceId, SvcIdentity *sid); +int32_t GetSystemAbilityById(int32_t systemAbility, IpcIo *reply); +int32_t AddRemoteSystemAbility(IpcIo *data); +int32_t GetRemoteSystemAbility(int32_t saId, const char* deviceId, IpcIo *reply); #ifdef __cplusplus } diff --git a/ipc/test/rpc/samgr/rpc_mini_samgr.c b/ipc/test/rpc/samgr/rpc_mini_samgr.c index 17641eaa..67be460f 100644 --- a/ipc/test/rpc/samgr/rpc_mini_samgr.c +++ b/ipc/test/rpc/samgr/rpc_mini_samgr.c @@ -37,6 +37,14 @@ static UTILS_DL_LIST *g_saList = NULL; static pthread_mutex_t g_handleMutex = PTHREAD_MUTEX_INITIALIZER; static int32_t g_handle = 0; +static int32_t GetNextHandle(void) +{ + pthread_mutex_lock(&g_handleMutex); + int32_t handle = ++g_handle; + pthread_mutex_unlock(&g_handleMutex); + return handle; +} + int32_t AddSystemAbility(int32_t saId, SvcIdentity *sid) { RPC_LOG_INFO("AddSystemAbility called.... handle = %d", sid->handle); @@ -59,15 +67,12 @@ int32_t AddSystemAbility(int32_t saId, SvcIdentity *sid) return ERR_FAILED; } - pthread_mutex_lock(&g_handleMutex); - node->sid->handle = ++g_handle; - pthread_mutex_unlock(&g_handleMutex); - + node->sid->handle = GetNextHandle(); UtilsListAdd(g_saList, &node->list); return ERR_NONE; } -SvcIdentity *GetSystemAbilityById(int32_t systemAbility) +int32_t GetSystemAbilityById(int32_t systemAbility, IpcIo *reply) { SvcInfo* node = NULL; SvcInfo* next = NULL; @@ -75,15 +80,22 @@ SvcIdentity *GetSystemAbilityById(int32_t systemAbility) { RPC_LOG_INFO("GetSystemAbilityById %d", node->saId); if (node->saId == systemAbility) { - return node->sid; + WriteRemoteObject(reply, node->sid); + reply->bufferCur = reply->bufferBase; + reply->offsetsCur = reply->offsetsCur; + return ERR_NONE; } } - return NULL; + return ERR_FAILED; } -int32_t AddRemoteSystemAbility(int32_t saId, SvcIdentity *sid) +int32_t AddRemoteSystemAbility(IpcIo *data) { - if (AddSystemAbility(saId, sid) == ERR_FAILED) { + int32_t saId; + ReadInt32(data, &saId); + SvcIdentity sid; + ReadRemoteObject(data, &sid); + if (AddSystemAbility(saId, &sid) == ERR_FAILED) { RPC_LOG_ERROR("AddSystemAbility failed"); return ERR_FAILED; } @@ -97,16 +109,22 @@ int32_t AddRemoteSystemAbility(int32_t saId, SvcIdentity *sid) return ERR_NONE; } -int32_t GetRemoteSystemAbility(int32_t saId, const char* deviceId, SvcIdentity *sid) +int32_t GetRemoteSystemAbility(int32_t saId, const char* deviceId, IpcIo *reply) { RPC_LOG_INFO("GetRemoteSystemAbility start"); const char *name = "16"; uint32_t idLen = (uint32_t)strlen(deviceId); - int32_t ret = MakeRemoteBinder(name, 2, deviceId, idLen, (uintptr_t)saId, 0, (void *)sid); + SvcIdentity sid; + int32_t ret = MakeRemoteBinder(name, 2, deviceId, idLen, (uintptr_t)saId, 0, &sid); if (ret != ERR_NONE) { RPC_LOG_ERROR("MakeRemoteBinder failed"); } + sid.handle = GetNextHandle(); + RPC_LOG_INFO("GetRemoteSystemAbility sid handle=%d", sid.handle); + WriteRemoteObject(reply, &sid); + reply->bufferCur = reply->bufferBase; + reply->offsetsCur = reply->offsetsBase; return ret; } @@ -117,6 +135,11 @@ void RpcStartSamgr(void) g_saList = (UTILS_DL_LIST *)calloc(1, sizeof(UTILS_DL_LIST)); UtilsListInit(g_saList); + SvcIdentity target = { + .cookie = 0 + }; + (void)SetContextObject(target); + StartDBinderService(); RPC_LOG_INFO("StartDBinderService finished"); diff --git a/ipc/test/rpc/samgr/rpc_samgr.c b/ipc/test/rpc/samgr/rpc_samgr.c index 0f69adb1..783a281f 100644 --- a/ipc/test/rpc/samgr/rpc_samgr.c +++ b/ipc/test/rpc/samgr/rpc_samgr.c @@ -32,6 +32,7 @@ typedef struct { static UTILS_DL_LIST *g_saList = NULL; enum { + GET_SYSTEM_ABILITY_TRANSACTION = 1, ADD_SYSTEM_ABILITY_TRANSACTION = 2, GET_REMOTE_SYSTEM_ABILITY_TRANSACTION = 3, ADD_REMOTE_SYSTEM_ABILITY_TRANSACTION = 4, diff --git a/ipc/test/rpc/socket_trans/include/rpc_socket_trans.h b/ipc/test/rpc/socket_trans/include/rpc_socket_trans.h index 0e2f69d8..c97037f0 100644 --- a/ipc/test/rpc/socket_trans/include/rpc_socket_trans.h +++ b/ipc/test/rpc/socket_trans/include/rpc_socket_trans.h @@ -34,7 +34,6 @@ static const uint16_t DEFAULT_HASH_SEED = 5381; static const uint16_t DEFAULT_PORT_MIN = 10000; TransInterface *GetSocketTrans(void); -char *GetSocketLocalDeviceID(void); #ifdef __cplusplus } diff --git a/ipc/test/rpc/socket_trans/src/rpc_mini_socket_trans.c b/ipc/test/rpc/socket_trans/src/rpc_mini_socket_trans.c index 926c8a21..300ac53f 100644 --- a/ipc/test/rpc/socket_trans/src/rpc_mini_socket_trans.c +++ b/ipc/test/rpc/socket_trans/src/rpc_mini_socket_trans.c @@ -353,27 +353,7 @@ static int32_t Send(int32_t sessionId, const void *data, uint32_t len) return ERR_NONE; } -static TransInterface g_socketTrans = { - .StartListen = StartListen, - .StopListen = StopListen, - .Connect = Connect, - .Disconnect = Disconnect, - .Send = Send -}; - -TransInterface *GetSocketTrans(void) -{ - if (g_init == -1) { - pthread_mutex_lock(&g_socketNodeList.mutex); - UtilsListInit(&g_socketNodeList.list); - g_init = 0; - printf("g_socketTrans %x\n", g_socketTrans.StartListen); - pthread_mutex_unlock(&g_socketNodeList.mutex); - } - return &g_socketTrans; -} - -char *GetSocketLocalDeviceID(void) +static char *GetSocketLocalDeviceID(void) { extern struct netif if_wifi; @@ -386,4 +366,25 @@ char *GetSocketLocalDeviceID(void) RPC_LOG_INFO("GetSocketLocalDeviceID %s\n", localDeviceId); return localDeviceId; +} + +static TransInterface g_socketTrans = { + .StartListen = StartListen, + .StopListen = StopListen, + .Connect = Connect, + .Disconnect = Disconnect, + .Send = Send, + .GetLocalDeviceID = GetSocketLocalDeviceID +}; + +TransInterface *GetSocketTrans(void) +{ + if (g_init == -1) { + pthread_mutex_lock(&g_socketNodeList.mutex); + UtilsListInit(&g_socketNodeList.list); + g_init = 0; + printf("g_socketTrans %x\n", g_socketTrans.StartListen); + pthread_mutex_unlock(&g_socketNodeList.mutex); + } + return &g_socketTrans; } \ No newline at end of file diff --git a/ipc/test/rpc/socket_trans/src/rpc_socket_trans.c b/ipc/test/rpc/socket_trans/src/rpc_socket_trans.c index 956a202b..94c10265 100644 --- a/ipc/test/rpc/socket_trans/src/rpc_socket_trans.c +++ b/ipc/test/rpc/socket_trans/src/rpc_socket_trans.c @@ -296,26 +296,7 @@ static int32_t Send(int32_t sessionId, const void *data, uint32_t len) return ERR_NONE; } -static TransInterface g_socketTrans = { - .StartListen = StartListen, - .StopListen = StopListen, - .Connect = Connect, - .Disconnect = Disconnect, - .Send = Send -}; - -TransInterface *GetSocketTrans(void) -{ - if (g_init == -1) { - pthread_mutex_lock(&g_socketNodeList.mutex); - UtilsListInit(&g_socketNodeList.list); - g_init = 0; - pthread_mutex_unlock(&g_socketNodeList.mutex); - } - return &g_socketTrans; -} - -char *GetSocketLocalDeviceID(void) +static char *GetSocketLocalDeviceID(void) { if (g_localDeviceId != NULL) { return g_localDeviceId; @@ -365,4 +346,24 @@ char *GetSocketLocalDeviceID(void) freeifaddrs(ifaddr); return g_localDeviceId; +} + +static TransInterface g_socketTrans = { + .StartListen = StartListen, + .StopListen = StopListen, + .Connect = Connect, + .Disconnect = Disconnect, + .Send = Send, + .GetLocalDeviceID = GetSocketLocalDeviceID, +}; + +TransInterface *GetSocketTrans(void) +{ + if (g_init == -1) { + pthread_mutex_lock(&g_socketNodeList.mutex); + UtilsListInit(&g_socketNodeList.list); + g_init = 0; + pthread_mutex_unlock(&g_socketNodeList.mutex); + } + return &g_socketTrans; } \ No newline at end of file diff --git a/services/dbinder/c/include/dbinder_service_inner.h b/services/dbinder/c/include/dbinder_service_inner.h new file mode 100644 index 00000000..541e8d6c --- /dev/null +++ b/services/dbinder/c/include/dbinder_service_inner.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DBINDER_SERVICE_INNER_H +#define DBINDER_SERVICE_INNER_H + +#include "dbinder_types.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif /* __cplusplus */ + +int32_t OnRemoteMessageTask(const DHandleEntryTxRx *message); +SessionInfo *QuerySessionObject(uintptr_t stub); + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif /* __cplusplus */ +#endif /* DBINDER_SERVICE_INNER_H */ \ No newline at end of file diff --git a/services/dbinder/c/include/dbinder_trans_callback.h b/services/dbinder/c/include/dbinder_trans_callback.h index a25bb2bf..3a2f437b 100644 --- a/services/dbinder/c/include/dbinder_trans_callback.h +++ b/services/dbinder/c/include/dbinder_trans_callback.h @@ -20,7 +20,6 @@ #include #include "rpc_trans.h" -#include "dbinder_service.h" #ifdef __cplusplus extern "C" { diff --git a/services/dbinder/c/include/dbinder_types.h b/services/dbinder/c/include/dbinder_types.h index 5ff382d5..d2c92867 100644 --- a/services/dbinder/c/include/dbinder_types.h +++ b/services/dbinder/c/include/dbinder_types.h @@ -35,6 +35,7 @@ extern "C" { #define GET_SYSTEM_ABILITY_TRANSACTION 1 #define ID_DIGITS 10 #define DEFAULT_SEND_WAIT_TIME 4 +#define USECTONSEC 1000 enum DBinderCode { MESSAGE_AS_INVOKER = 1, @@ -99,6 +100,23 @@ typedef struct { char *buffer; } dbinder_transaction_data; +typedef struct { + struct DHandleEntryHead head; + uint32_t transType; + uint32_t dBinderCode; + uint16_t fromPort; + uint16_t toPort; + uint64_t stubIndex; + uint32_t seqNumber; + uintptr_t binderObject; + struct DeviceIdInfo deviceIdInfo; + uintptr_t stub; + uint16_t serviceNameLength; + char serviceName[SERVICENAME_LENGTH + 1]; + uint32_t pid; + uint32_t uid; +} DHandleEntryTxRx; + #ifdef __cplusplus } #endif diff --git a/services/dbinder/c/ipc_adapter/include/dbinder_ipc_adapter.h b/services/dbinder/c/ipc_adapter/include/dbinder_ipc_adapter.h index 4a57e215..ea39dd48 100644 --- a/services/dbinder/c/ipc_adapter/include/dbinder_ipc_adapter.h +++ b/services/dbinder/c/ipc_adapter/include/dbinder_ipc_adapter.h @@ -28,8 +28,6 @@ extern "C" { bool IsSameStub(DBinderServiceStub *stub, const char *serviceName, const char *deviceID, uintptr_t binderObject); -int32_t GetDBinderHandle(uintptr_t stubAddr); -int32_t UpdateSessionIfNeed(uintptr_t stubAddr); ProxyObject *RpcGetSystemAbility(int32_t systemAbility); #ifdef __cplusplus diff --git a/services/dbinder/c/ipc_adapter/mini/dbinder_ipc_adapter.c b/services/dbinder/c/ipc_adapter/mini/dbinder_ipc_adapter.c index ce7b9644..9524bc6b 100644 --- a/services/dbinder/c/ipc_adapter/mini/dbinder_ipc_adapter.c +++ b/services/dbinder/c/ipc_adapter/mini/dbinder_ipc_adapter.c @@ -28,24 +28,17 @@ bool IsSameStub(DBinderServiceStub *stub, const char *serviceName, return false; } -int32_t GetDBinderHandle(uintptr_t stubAddr) -{ - return (int32_t)stubAddr; -} - -int32_t UpdateSessionIfNeed(uintptr_t stubAddr) -{ - UpdateProto((int32_t)stubAddr); - return ERR_NONE; -} - ProxyObject *RpcGetSystemAbility(int32_t systemAbility) { - SvcIdentity *target = GetSystemAbilityById(systemAbility); - if (target == NULL) { - RPC_LOG_ERROR("GetSystemAbilityById return null"); + IpcIo reply; + uint8_t replyAlloc[RPC_IPC_LENGTH]; + IpcIoInit(&reply, replyAlloc, RPC_IPC_LENGTH, 0); + if (GetSystemAbilityById(systemAbility, &reply) != ERR_NONE) { + RPC_LOG_ERROR("GetSystemAbilityById failed"); return NULL; } + SvcIdentity target; + ReadRemoteObject(&reply, &target); ProxyObject *proxyObject = (ProxyObject *)calloc(1, sizeof(ProxyObject)); if (proxyObject == NULL) { @@ -57,7 +50,7 @@ ProxyObject *RpcGetSystemAbility(int32_t systemAbility) return NULL; } - if (memcpy_s(proxyObject->proxy, sizeof(SvcIdentity), target, sizeof(SvcIdentity)) != EOK) { + if (memcpy_s(proxyObject->proxy, sizeof(SvcIdentity), &target, sizeof(SvcIdentity)) != EOK) { free(proxyObject->proxy); free(proxyObject); return NULL; diff --git a/services/dbinder/c/ipc_adapter/small/dbinder_ipc_adapter.c b/services/dbinder/c/ipc_adapter/small/dbinder_ipc_adapter.c index 5faf5982..6d58a921 100644 --- a/services/dbinder/c/ipc_adapter/small/dbinder_ipc_adapter.c +++ b/services/dbinder/c/ipc_adapter/small/dbinder_ipc_adapter.c @@ -23,8 +23,6 @@ #include "rpc_log.h" #include "rpc_errno.h" -#define IPC_STUB_HANDLE (-1) - bool IsSameStub(DBinderServiceStub *stub, const char *serviceName, const char *deviceID, uintptr_t binderObject) { @@ -35,16 +33,6 @@ bool IsSameStub(DBinderServiceStub *stub, const char *serviceName, && stub->binderObject == binderObject); } -int32_t GetDBinderHandle(uintptr_t stubAddr) -{ - return IPC_STUB_HANDLE; -} - -int32_t UpdateSessionIfNeed(uintptr_t stubAddr) -{ - return ERR_NONE; -} - ProxyObject *RpcGetSystemAbility(int32_t systemAbility) { IpcIo data; diff --git a/services/dbinder/c/src/dbinder_service.c b/services/dbinder/c/src/dbinder_service.c index 782d135a..da49e6e9 100644 --- a/services/dbinder/c/src/dbinder_service.c +++ b/services/dbinder/c/src/dbinder_service.c @@ -20,6 +20,7 @@ #include #include #include +#include #include "utils_list.h" #include "securec.h" @@ -27,6 +28,7 @@ #include "rpc_trans.h" #include "dbinder_trans_callback.h" #include "dbinder_ipc_adapter.h" +#include "dbinder_service_inner.h" #include "serializer.h" #include "dbinder_stub.h" #include "ipc_skeleton.h" @@ -214,7 +216,7 @@ static int32_t SendEntryToRemote(DBinderServiceStub *stub, const uint32_t seqNum } uint32_t toDeviceIDLength = (uint32_t)strlen(toDeviceID); - char *localDeviceID = GetLocalDeviceID(); + char *localDeviceID = g_trans->GetLocalDeviceID(); if (localDeviceID == NULL) { RPC_LOG_ERROR("GetLocalDeviceID failed"); return ERR_FAILED; @@ -270,6 +272,40 @@ static void DetachThreadLockInfo(ThreadLockInfo *threadLockInfo) pthread_mutex_unlock(&g_threadLockInfoList.mutex); } +static ThreadLockInfo *NewThreadLock(void) +{ + ThreadLockInfo *threadLockInfo = (ThreadLockInfo *)malloc(sizeof(ThreadLockInfo)); + if (threadLockInfo == NULL) { + RPC_LOG_ERROR("threadLockInfo malloc failed"); + return NULL; + } + if (pthread_mutex_init(&threadLockInfo->mutex, NULL) != 0) { + RPC_LOG_ERROR("threadLockInfo mutex init failed"); + free(threadLockInfo); + return NULL; + } + if (pthread_cond_init(&threadLockInfo->condition, NULL) != 0) { + RPC_LOG_ERROR("threadLockInfo condition init failed"); + free(threadLockInfo); + return NULL; + } + + return threadLockInfo; +} + +static int32_t GetWaitTime(struct timespec *waitTime) +{ + struct timeval now; + if (gettimeofday(&now, NULL) != 0) { + RPC_LOG_ERROR("gettimeofday failed"); + return ERR_FAILED; + } + waitTime->tv_sec = now.tv_sec + DEFAULT_SEND_WAIT_TIME; + waitTime->tv_nsec = now.tv_usec * USECTONSEC; + + return ERR_NONE; +} + static int32_t InvokerRemoteDBinder(DBinderServiceStub *dBinderServiceStub, uint32_t seqNumber) { if (dBinderServiceStub == NULL) { @@ -278,29 +314,14 @@ static int32_t InvokerRemoteDBinder(DBinderServiceStub *dBinderServiceStub, uint } int32_t ret = ERR_FAILED; - ThreadLockInfo *threadLockInfo = (ThreadLockInfo *)malloc(sizeof(ThreadLockInfo)); + ThreadLockInfo *threadLockInfo = NewThreadLock(); if (threadLockInfo == NULL) { - RPC_LOG_ERROR("threadLockInfo malloc failed"); - return ERR_FAILED; + return ret; } - if (pthread_mutex_init(&threadLockInfo->mutex, NULL) != 0) { - RPC_LOG_ERROR("threadLockInfo mutex init failed"); - free(threadLockInfo); - return ERR_FAILED; - } - if (pthread_cond_init(&threadLockInfo->condition, NULL) != 0) { - RPC_LOG_ERROR("threadLockInfo condition init failed"); - pthread_mutex_destroy(&threadLockInfo->mutex); - free(threadLockInfo); - return ERR_FAILED; - } - threadLockInfo->seqNumber = seqNumber; ret = AttachThreadLockInfo(threadLockInfo); if (ret != ERR_NONE) { RPC_LOG_ERROR("AttachThreadLockInfo failed"); - pthread_mutex_destroy(&threadLockInfo->mutex); - pthread_cond_destroy(&threadLockInfo->condition); free(threadLockInfo); return ret; } @@ -310,7 +331,23 @@ static int32_t InvokerRemoteDBinder(DBinderServiceStub *dBinderServiceStub, uint if (ret != ERR_NONE) { RPC_LOG_ERROR("send entry to remote dbinderService failed"); } else { - pthread_cond_wait(&threadLockInfo->condition, &threadLockInfo->mutex); + struct timespec waitTime; + ret = GetWaitTime(&waitTime); + if (ret != ERR_NONE) { + DetachThreadLockInfo(threadLockInfo); + pthread_mutex_unlock(&threadLockInfo->mutex); + free(threadLockInfo); + return ERR_FAILED; + } + + ret = pthread_cond_timedwait(&threadLockInfo->condition, &threadLockInfo->mutex, &waitTime); + if (ret == ETIMEDOUT) { + RPC_LOG_ERROR("InvokerRemoteDBinder wait for reply timeout"); + DetachThreadLockInfo(threadLockInfo); + pthread_mutex_unlock(&threadLockInfo->mutex); + free(threadLockInfo); + return ERR_FAILED; + } RPC_LOG_INFO("InvokerRemoteDBinder wakeup!"); } @@ -319,13 +356,8 @@ static int32_t InvokerRemoteDBinder(DBinderServiceStub *dBinderServiceStub, uint ret = ERR_FAILED; } - if (UpdateSessionIfNeed(dBinderServiceStub->svc.cookie) != ERR_NONE) { - RPC_LOG_ERROR("UpdateSessionIfNeed failed"); - ret = ERR_FAILED; - } - - pthread_mutex_unlock(&threadLockInfo->mutex); DetachThreadLockInfo(threadLockInfo); + pthread_mutex_unlock(&threadLockInfo->mutex); free(threadLockInfo); return ret; @@ -405,7 +437,7 @@ static int32_t AttachProxyObject(ProxyObject *proxy) return ERR_NONE; } -void DetachProxyObject(ProxyObject *proxy) +static void DetachProxyObject(ProxyObject *proxy) { pthread_mutex_lock(&g_proxyObjectList.mutex); UtilsListDelete(&proxy->list); @@ -501,7 +533,7 @@ static int32_t OnRemoteInvokerDataBusMessage(ProxyObject *proxy, DHandleEntryTxR IpcIo reply; uintptr_t ptr; - int32_t ret = InvokerListenThread(proxy, GetLocalDeviceID(), remoteDeviceID, pid, uid, &reply, &ptr); + int32_t ret = InvokerListenThread(proxy, g_trans->GetLocalDeviceID(), remoteDeviceID, pid, uid, &reply, &ptr); if (ret != ERR_NONE) { RPC_LOG_ERROR("INVOKE_LISTEN_THREAD failed"); FreeBuffer((void *)ptr); diff --git a/services/dbinder/c/src/dbinder_stub.c b/services/dbinder/c/src/dbinder_stub.c index 627e8f97..8674e3dc 100644 --- a/services/dbinder/c/src/dbinder_stub.c +++ b/services/dbinder/c/src/dbinder_stub.c @@ -24,8 +24,10 @@ #include "ipc_thread_pool.h" #include "ipc_skeleton.h" #include "ipc_process_skeleton.h" -#include "dbinder_service.h" #include "dbinder_ipc_adapter.h" +#include "dbinder_service_inner.h" + +#define IPC_INVALID_HANDLE (-1) static int32_t GetDigits(int32_t number) { @@ -135,7 +137,7 @@ int32_t GetDBinderStub(const char *serviceName, const char *deviceID, objectStub->isRemote = true; dBinderServiceStub->binderObject = binderObject; - dBinderServiceStub->svc.handle = GetDBinderHandle((uintptr_t)objectStub); + dBinderServiceStub->svc.handle = IPC_INVALID_HANDLE; dBinderServiceStub->svc.token = (uintptr_t)objectStub; dBinderServiceStub->svc.cookie = (uintptr_t)objectStub; return ERR_NONE; diff --git a/services/dbinder/c/src/dbinder_trans_callback.c b/services/dbinder/c/src/dbinder_trans_callback.c index 55393d67..39293f4f 100644 --- a/services/dbinder/c/src/dbinder_trans_callback.c +++ b/services/dbinder/c/src/dbinder_trans_callback.c @@ -17,7 +17,7 @@ #include -#include "dbinder_service.h" +#include "dbinder_service_inner.h" #include "rpc_log.h" #include "rpc_errno.h"