diff --git a/samgr_client/source/remote_register_rpc.c b/samgr_client/source/remote_register_rpc.c index 5a630dd94d706f11546902240da9e4dae2e8eedd..2471a042a38c5b5fc3d2409bc9435a843bf80f10 100644 --- a/samgr_client/source/remote_register_rpc.c +++ b/samgr_client/source/remote_register_rpc.c @@ -78,7 +78,10 @@ IUnknown *SAMGR_FindServiceApi(const char *service, const char *feature) SaName key = {service, feature}; int index = VECTOR_FindByKey(&g_remoteRegister.clients, &key); if (index != INVALID_INDEX) { - return VECTOR_At(&g_remoteRegister.clients, index); + IUnknown *proxy = VECTOR_At(&g_remoteRegister.clients, index); + if (SAMGR_IsProxyValid(proxy)) { + return proxy; + } } IUnknown *proxy = SAMGR_CreateIProxy(service, feature); if (proxy == NULL) { @@ -87,9 +90,12 @@ IUnknown *SAMGR_FindServiceApi(const char *service, const char *feature) MUTEX_Lock(g_remoteRegister.mtx); index = VECTOR_FindByKey(&g_remoteRegister.clients, &key); if (index != INVALID_INDEX) { + IUnknown *oldProxy = VECTOR_Swap(&g_remoteRegister.clients, index, proxy); MUTEX_Unlock(g_remoteRegister.mtx); - proxy->Release(proxy); - return VECTOR_At(&g_remoteRegister.clients, index); + if (oldProxy != NULL) { + oldProxy->Release(oldProxy); + } + return proxy; } VECTOR_Add(&g_remoteRegister.clients, proxy); MUTEX_Unlock(g_remoteRegister.mtx); diff --git a/samgr_endpoint/source/default_client.h b/samgr_endpoint/source/default_client.h index d1cfdb99beadb383ee35d769fa890dde137cc482..25b40667802f16cdfbb9534d86d6ac34888fe63d 100644 --- a/samgr_endpoint/source/default_client.h +++ b/samgr_endpoint/source/default_client.h @@ -28,6 +28,7 @@ struct SaName { const char *feature; }; +BOOL SAMGR_IsProxyValid(IUnknown *proxy); IUnknown *SAMGR_CreateIProxy(const char *service, const char *feature); IUnknown *SAMGR_CreateIRemoteProxy(const char *deviceId, const char *service, const char *feature); SaName *SAMGR_GetSAName(const IUnknown *proxy); diff --git a/samgr_endpoint/source/default_client_rpc.c b/samgr_endpoint/source/default_client_rpc.c index d18730b7250750eabc65ed352bc3e0accf90f621..ecfa3cb6448cfbdc2d9ff342eff03a009d20e7c8 100644 --- a/samgr_endpoint/source/default_client_rpc.c +++ b/samgr_endpoint/source/default_client_rpc.c @@ -24,6 +24,29 @@ static SvcIdentity QueryRemoteIdentity(const char *deviceId, const char *service static const IClientEntry DEFAULT_ENTRY = {CLIENT_IPROXY_BEGIN, .Invoke = ProxyInvoke, IPROXY_END}; static MutexId g_mutex = NULL; +BOOL SAMGR_IsProxyValid(IUnknown *proxy) +{ + if (proxy != NULL) { + IDefaultClient *client = GET_OBJECT(proxy, IDefaultClient, entry.iUnknown); + if (client == NULL) { + return false; + } + IClientHeader *header = &client->header; + if (header == NULL) { + return false; + } + if (header->deadId == INVALID_INDEX && + header->target.handle == INVALID_INDEX && + header->target.token == INVALID_INDEX && + header->target.cookie == INVALID_INDEX) { + return false; + } else { + return true; + } + } + return false; +} + IUnknown *SAMGR_CreateIProxy(const char *service, const char *feature) { SvcIdentity identity = QueryIdentity(service, feature);