diff --git a/interfaces/kits/samgr/feature.h b/interfaces/kits/samgr/feature.h old mode 100755 new mode 100644 index 1fa3f280e644ac4fb86254dfee042b3ec2bb8755..e49cea1c6eb61dd8890d1948c04e9019c9b8c8ad --- a/interfaces/kits/samgr/feature.h +++ b/interfaces/kits/samgr/feature.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Huawei Device Co., Ltd. + * Copyright (c) 2020-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 @@ -129,15 +129,6 @@ struct Feature { * This macro provides the capability of inheriting the feature lifecycle. \n * */ -#ifndef MINI_SAMGR_LITE_RPC - -#define INHERIT_FEATURE \ - const char *(*GetName)(Feature *feature); \ - void (*OnInitialize)(Feature *feature, Service *parent, Identity identity); \ - void (*OnStop)(Feature *feature, Identity identity); \ - BOOL (*OnMessage)(Feature *feature, Request *request) - -#else #define INHERIT_FEATURE \ const char *(*GetName)(Feature *feature); \ @@ -145,9 +136,6 @@ struct Feature { void (*OnStop)(Feature *feature, Identity identity); \ BOOL (*OnMessage)(Feature *feature, Request *request); \ BOOL (*IsDistributed)(void) - -#endif - #ifdef __cplusplus #if __cplusplus } diff --git a/interfaces/kits/samgr/message.h b/interfaces/kits/samgr/message.h old mode 100755 new mode 100644 index 713fb28bcc5013083b852a7906b002c01af8ef34..b48bdb4a33a0d0f3ac29fbeef49ca39b63b4920a --- a/interfaces/kits/samgr/message.h +++ b/interfaces/kits/samgr/message.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Huawei Device Co., Ltd. + * Copyright (c) 2020-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 @@ -116,9 +116,7 @@ struct Response { void *data; /** Data length */ int16 len; -#ifdef MINI_SAMGR_LITE_RPC void *reply; -#endif }; #pragma pack() diff --git a/samgr/BUILD.gn b/samgr/BUILD.gn old mode 100755 new mode 100644 index 8cfc3acc42935e5f92a513f8dd052dd06581127b..3b92be0b3b8878f1df48678a2e5f54094fbaee64 --- a/samgr/BUILD.gn +++ b/samgr/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2020 Huawei Device Co., Ltd. +# Copyright (c) 2020-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 @@ -78,15 +78,15 @@ if (ohos_kernel_type == "liteos_a" || ohos_kernel_type == "linux") { include_dirs = [ "//third_party/bounds_checking_function/include", "//third_party/cJSON", + "//foundation/distributedschedule/samgr_lite/samgr_endpoint/source", ] public_deps = [ "//build/lite/config/component/cJSON:cjson_shared", - "//foundation/communication/ipc_lite:liteipc_adapter", + "//foundation/communication/ipc/interfaces/innerkits/c/ipc:ipc_single", "//foundation/distributedschedule/samgr_lite/samgr/source:samgr_source", "//foundation/distributedschedule/samgr_lite/samgr_client:client", ] - public_configs += [ ":external_settings_shared" ] } } diff --git a/samgr_client/BUILD.gn b/samgr_client/BUILD.gn index 37d3f523df176558e7a718c3fd0f43b763677feb..1052f1f1e3a3195d0d2e49eafff7ad6f9ca1c917 100644 --- a/samgr_client/BUILD.gn +++ b/samgr_client/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2020 Huawei Device Co., Ltd. +# Copyright (c) 2020-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 @@ -15,7 +15,7 @@ import("../config.gni") if (!enable_ohos_distributedschedule_samgr_lite_rpc_mini) { source_set("client") { - sources = [ "source/remote_register.c" ] + sources = [ "source/remote_register_rpc.c" ] cflags = [ "-fPIC", @@ -31,13 +31,14 @@ if (!enable_ohos_distributedschedule_samgr_lite_rpc_mini) { ] public_deps = [ - "//foundation/communication/ipc_lite:liteipc_adapter", + "//foundation/communication/ipc/interfaces/innerkits/c/ipc:ipc_single", "//foundation/distributedschedule/samgr_lite/samgr_endpoint:endpoint_source", "//third_party/bounds_checking_function:libsec_shared", ] } } else { source_set("client") { + defines = [ "MINI_SAMGR_LITE_RPC" ] sources = [ "source/remote_register_rpc.c" ] cflags = [ "-Wall" ] diff --git a/samgr_client/source/remote_register_rpc.c b/samgr_client/source/remote_register_rpc.c old mode 100755 new mode 100644 index 42327ffadc9718bf96916664e0b57050e2391ec7..5a630dd94d706f11546902240da9e4dae2e8eedd --- a/samgr_client/source/remote_register_rpc.c +++ b/samgr_client/source/remote_register_rpc.c @@ -16,15 +16,16 @@ #include #include #include - +#ifdef MINI_SAMGR_LITE_RPC #include "dbinder_service.h" +#include "samgr_server.h" +#endif #include "default_client.h" #include "iproxy_client.h" #include "memory_adapter.h" #include "policy_define.h" #include "pthread.h" #include "samgr_lite.h" -#include "samgr_server.h" #include "thread_adapter.h" #include "unistd.h" @@ -49,6 +50,7 @@ int SAMGR_RegisterServiceApi(const char *service, const char *feature, const Ide MUTEX_Lock(g_remoteRegister.mtx); SaName saName = {service, feature}; int32 token = SAMGR_AddRouter(g_remoteRegister.endpoint, &saName, identity, iUnknown); +#ifdef MINI_SAMGR_LITE_RPC char saNameStr[2 * MAX_NAME_LEN + 2]; (void)sprintf_s(saNameStr, 2 * MAX_NAME_LEN + 2, "%s#%s", service, feature?feature:""); HILOG_INFO(HILOG_MODULE_SAMGR, "register saname: %s index: %d\n", saNameStr, token); @@ -56,10 +58,14 @@ int SAMGR_RegisterServiceApi(const char *service, const char *feature, const Ide if (saNode != NULL) { RegisterRemoteProxy(saNameStr, strlen(saNameStr), saNode->saId); } +#endif MUTEX_Unlock(g_remoteRegister.mtx); if (token < 0 || !g_remoteRegister.endpoint->running) { return token; } +#ifndef MINI_SAMGR_LITE_RPC + SAMGR_ProcPolicy(g_remoteRegister.endpoint, &saName, token); +#endif return EC_SUCCESS; } @@ -129,6 +135,13 @@ static void ClearRegistry(void) static void ClientInitializeRegistry(void) { +#ifndef MINI_SAMGR_LITE_RPC + if (getuid() >= ABILITY_UID_START && !g_isAbilityInited) { + ClearRegistry(); + g_isAbilityInited = TRUE; + } +#endif + if (g_remoteRegister.endpoint != NULL) { return; } diff --git a/samgr_endpoint/BUILD.gn b/samgr_endpoint/BUILD.gn index 40fd787eb73635dd124cd9c89f3b90359e8e733f..8f3af27a9bd87b94d19bccd4037a246663177c9c 100644 --- a/samgr_endpoint/BUILD.gn +++ b/samgr_endpoint/BUILD.gn @@ -20,21 +20,25 @@ if (!enable_ohos_distributedschedule_samgr_lite_rpc_mini) { "//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr", "//foundation/distributedschedule/samgr_lite/interfaces/kits/registry", "//utils/native/lite/include", - "//foundation/communication/ipc_lite/interfaces/kits", "//third_party/bounds_checking_function/include", "//base/security/permission/services/permission_lite/ipc_auth/include", ] } config("endpoint_internal") { - include_dirs = [ "../samgr/registry" ] + include_dirs = [ + "./source", + "../samgr/registry", + ] } source_set("endpoint_source") { sources = [ "source/client_factory.c", - "source/default_client.c", - "source/endpoint.c", + "source/default_client_rpc.c", + "source/default_client_small_adapter.c", + "source/endpoint_rpc.c", + "source/samgr_small_ipc_adapter.c", "source/token_bucket.c", ] @@ -56,7 +60,7 @@ if (!enable_ohos_distributedschedule_samgr_lite_rpc_mini) { public_deps = [ "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared", - "//foundation/communication/ipc_lite:liteipc_adapter", + "//foundation/communication/ipc/interfaces/innerkits/c/ipc:ipc_single", "//third_party/bounds_checking_function:libsec_shared", ] } @@ -73,7 +77,10 @@ if (!enable_ohos_distributedschedule_samgr_lite_rpc_mini) { public_configs = [ ":endpoint_public" ] - public_deps = [ "//third_party/bounds_checking_function:libsec_shared" ] + public_deps = [ + "//foundation/communication/ipc/interfaces/innerkits/c/ipc:ipc_single", + "//third_party/bounds_checking_function:libsec_shared", + ] } } else { defines = [ "MINI_SAMGR_LITE_RPC" ] @@ -101,8 +108,10 @@ if (!enable_ohos_distributedschedule_samgr_lite_rpc_mini) { source_set("endpoint_source") { sources = [ "source/client_factory.c", + "source/default_client_mini_adapter.c", "source/default_client_rpc.c", "source/endpoint_rpc.c", + "source/samgr_mini_ipc_adapter.c", "source/token_bucket.c", ] diff --git a/samgr_endpoint/source/default_client.h b/samgr_endpoint/source/default_client.h old mode 100755 new mode 100644 index ef83e3198819a74358570aa549aba0aa940819b7..d1cfdb99beadb383ee35d769fa890dde137cc482 --- a/samgr_endpoint/source/default_client.h +++ b/samgr_endpoint/source/default_client.h @@ -15,10 +15,6 @@ #ifndef LITE_DEFAULT_CLIENT_H #define LITE_DEFAULT_CLIENT_H -#ifndef MINI_SAMGR_LITE_RPC -#include -#endif - #include "iunknown.h" #ifdef __cplusplus @@ -32,13 +28,8 @@ struct SaName { const char *feature; }; -#ifndef MINI_SAMGR_LITE_RPC -IUnknown *SAMGR_CreateIProxy(const IpcContext *context, const char *service, const char *feature); -#else IUnknown *SAMGR_CreateIProxy(const char *service, const char *feature); IUnknown *SAMGR_CreateIRemoteProxy(const char *deviceId, const char *service, const char *feature); -#endif - SaName *SAMGR_GetSAName(const IUnknown *proxy); int SAMGR_CompareSAName(const SaName *key1, const SaName *key2); #ifdef __cplusplus diff --git a/samgr_endpoint/source/default_client_adapter.h b/samgr_endpoint/source/default_client_adapter.h new file mode 100644 index 0000000000000000000000000000000000000000..8b66b391ef0e235af49e0425c64a80f8d3c8c158 --- /dev/null +++ b/samgr_endpoint/source/default_client_adapter.h @@ -0,0 +1,70 @@ +/* + * 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 LITE_DEFAULT_CLIENT_ADAPTER_H +#define LITE_DEFAULT_CLIENT_ADAPTER_H +#include "default_client.h" + +#include +#include +#include +#include +#include +#include "client_factory.h" +#include "endpoint.h" +#include "ipc_skeleton.h" +#include "iproxy_client.h" +#include "memory_adapter.h" +#include "thread_adapter.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif +#undef LOG_TAG +#undef LOG_DOMAIN +#define LOG_TAG "Samgr" +#define LOG_DOMAIN 0xD001800 + +typedef struct IClientHeader IClientHeader; +typedef struct IDefaultClient IDefaultClient; +typedef struct IClientEntry IClientEntry; +struct IClientHeader { + SaName key; + SvcIdentity target; + uint32 deadId; + uintptr_t saId; +}; + +struct IClientEntry { + INHERIT_IUNKNOWNENTRY(IClientProxy); +}; + +#pragma pack(1) +struct IDefaultClient { + IClientHeader header; + IClientEntry entry; +}; +#pragma pack() + +uintptr_t GetRemoteSaIdInner(const char *service, const char *feature); +void ProxyInvokeArgInner(IpcIo *reply, IClientHeader *header); +SvcIdentity QueryRemoteIdentityInner(const char *deviceId, const char *service, const char *feature); +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif +#endif // LITE_DEFAULT_CLIENT_H diff --git a/samgr_endpoint/source/default_client_mini_adapter.c b/samgr_endpoint/source/default_client_mini_adapter.c new file mode 100644 index 0000000000000000000000000000000000000000..c4b3a3c413221beab1cd1e356d4e9e1b78139c5d --- /dev/null +++ b/samgr_endpoint/source/default_client_mini_adapter.c @@ -0,0 +1,66 @@ +/* + * 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. + */ +#include "default_client_adapter.h" +#include "samgr_server.h" +#include "dbinder_service.h" +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; +} + +uintptr_t GetRemoteSaIdInner(const char *service, const char *feature) +{ + SaNode *saNode = GetSaNodeBySaName(service, feature); + if (saNode != NULL) { + return saNode->saId; + } + return 0; +} + +void ProxyInvokeArgInner(IpcIo *reply, IClientHeader *header) +{ + WriteInt32(reply, (int32_t)header->saId); +} + +SvcIdentity QueryRemoteIdentityInner(const char *deviceId, const char *service, const char *feature) +{ + char saName[2 * MAX_NAME_LEN + 2]; + (void)sprintf_s(saName, 2 * MAX_NAME_LEN + 2, "%s#%s", service?service:"", feature?feature:""); + HILOG_INFO(HILOG_MODULE_SAMGR, "saName %s, make remote binder start", saName); + + SvcIdentity target = {INVALID_INDEX, INVALID_INDEX, INVALID_INDEX}; + SaNode *saNode = GetSaNodeBySaName(service, feature); + if (saNode == NULL) { + HILOG_ERROR(HILOG_MODULE_SAMGR, "service: %s feature: %s have no saId in sa map", service, feature); + return target; + } + int32_t ret = MakeRemoteBinder(saName, strlen(saName), deviceId, strlen(deviceId), saNode->saId, 0, + &target); + if (ret != EC_SUCCESS) { + HILOG_ERROR(HILOG_MODULE_SAMGR, "MakeRemoteBinder failed"); + return target; + } + target.handle = GetNextHandle(); + extern void WaitForProxyInit(SvcIdentity *svc); + WaitForProxyInit(&target); + HILOG_ERROR(HILOG_MODULE_SAMGR, "MakeRemoteBinder sid handle=%d", target.handle); + return target; +} \ No newline at end of file diff --git a/samgr_endpoint/source/default_client_rpc.c b/samgr_endpoint/source/default_client_rpc.c old mode 100755 new mode 100644 index ce5a2cb824fd080eff4955a004ac33c34c53dfb1..dc6f9559d19b6e72ee0d110d66133d8784a075da --- a/samgr_endpoint/source/default_client_rpc.c +++ b/samgr_endpoint/source/default_client_rpc.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Huawei Device Co., Ltd. + * Copyright (c) 2020-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 @@ -12,65 +12,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "default_client.h" - -#include -#include -#include -#include -#include -#include "client_factory.h" -#include "dbinder_service.h" -#include "endpoint.h" -#include "ipc_skeleton.h" -#include "iproxy_client.h" -#include "memory_adapter.h" -#include "samgr_server.h" -#include "thread_adapter.h" - -#undef LOG_TAG -#undef LOG_DOMAIN -#define LOG_TAG "Samgr" -#define LOG_DOMAIN 0xD001800 -typedef struct IClientHeader IClientHeader; -typedef struct IDefaultClient IDefaultClient; -typedef struct IClientEntry IClientEntry; -struct IClientHeader { - SaName key; - SvcIdentity target; - uint32 deadId; - uintptr_t saId; -}; - -struct IClientEntry { - INHERIT_IUNKNOWNENTRY(IClientProxy); -}; - -#pragma pack(1) -struct IDefaultClient { - IClientHeader header; - IClientEntry entry; -}; -#pragma pack() +#include "default_client_adapter.h" static int AddRef(IUnknown *iUnknown); static int Release(IUnknown *proxy); static int ProxyInvoke(IClientProxy *proxy, int funcId, IpcIo *request, IOwner owner, INotify notify); -static int OnServiceExit(void *ipcMsg, IpcIo *data, void *argv); +static void OnServiceExit(void *argv); static SvcIdentity QueryIdentity(const char *service, const char *feature); static SvcIdentity QueryRemoteIdentity(const char *deviceId, const char *service, const char *feature); static const IClientEntry DEFAULT_ENTRY = {CLIENT_IPROXY_BEGIN, .Invoke = ProxyInvoke, IPROXY_END}; static MutexId g_mutex = 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; -} IUnknown *SAMGR_CreateIProxy(const char *service, const char *feature) { @@ -92,6 +43,7 @@ IUnknown *SAMGR_CreateIProxy(const char *service, const char *feature) header->target = identity; header->key.service = service; header->key.feature = feature; + header->saId = 0; (void)AddDeathRecipient(identity, OnServiceExit, client, &header->deadId); IClientEntry *entry = &client->entry; @@ -118,10 +70,7 @@ IUnknown *SAMGR_CreateIRemoteProxy(const char* deviceId, const char *service, co header->target = identity; header->key.service = service; header->key.feature = feature; - SaNode *saNode = GetSaNodeBySaName(service, feature); - if (saNode != NULL) { - header->saId = saNode->saId; - } + header->saId = GetRemoteSaIdInner(service, feature); IClientEntry *entry = &client->entry; entry->iUnknown.Invoke = ProxyInvoke; @@ -220,24 +169,24 @@ static int ProxyInvoke(IClientProxy *proxy, int funcId, IpcIo *request, IOwner o MessageOption flag; MessageOptionInit(&flag); flag.flags = (notify == NULL) ? TF_OP_ASYNC : TF_OP_SYNC; - HILOG_DEBUG(HILOG_MODULE_SAMGR, "%d %lu, %lu, saId: %d\n", header->target.handle, header->target.token, - header->target.cookie, header->saId); IpcIo requestWrapper; - uint8_t *tmpData2 = (uint8_t *) malloc(MAX_IO_SIZE); - if (tmpData2 == NULL) { + uint8_t *data = (uint8_t *) malloc(MAX_IO_SIZE); + if (data == NULL) { HILOG_ERROR(HILOG_MODULE_SAMGR, "malloc data for ipc io failed\n"); return EC_INVALID; } else { - IpcIoInit(&requestWrapper, tmpData2, MAX_IO_SIZE, MAX_OBJ_NUM); - } - WriteInt32(&requestWrapper, (int32_t)header->saId); - if (!IpcIoAppend(&requestWrapper, request)) { - HILOG_ERROR(HILOG_MODULE_SAMGR, "ipc io append fail\n"); - FreeBuffer(tmpData2); - return EC_INVALID; + IpcIoInit(&requestWrapper, data, MAX_IO_SIZE, MAX_OBJ_NUM); + } + ProxyInvokeArgInner(&requestWrapper, header); + if(request != NULL) { + if (!IpcIoAppend(&requestWrapper, request)) { + HILOG_ERROR(HILOG_MODULE_SAMGR, "ipc io append fail\n"); + free(data); + return EC_INVALID; + } } int ret = SendRequest(header->target, funcId, &requestWrapper, &reply, flag, (uintptr_t *)&replyBuf); - FreeBuffer(tmpData2); + free(data); if (notify != NULL) { notify(owner, ret, &reply); @@ -249,20 +198,15 @@ static int ProxyInvoke(IClientProxy *proxy, int funcId, IpcIo *request, IOwner o return ret; } -static int OnServiceExit(void *ipcMsg, IpcIo *data, void *argv) +static void OnServiceExit(void *argv) { - (void)data; IClientHeader *header = (IClientHeader *)argv; - (void)RemoveDeathRecipient(header->target, header->deadId); + ReleaseSvc(header->target); header->deadId = INVALID_INDEX; header->target.handle = INVALID_INDEX; header->target.token = INVALID_INDEX; header->target.cookie = INVALID_INDEX; - if (ipcMsg != NULL) { - FreeBuffer(ipcMsg); - } HILOG_ERROR(HILOG_MODULE_SAMGR, "Miss the remote service<%u, %u>!", header->target.handle, header->target.token); - return EC_SUCCESS; } static SvcIdentity QueryIdentity(const char *service, const char *feature) @@ -270,24 +214,28 @@ static SvcIdentity QueryIdentity(const char *service, const char *feature) IpcIo req; uint8 data[MAX_DATA_LEN]; IpcIoInit(&req, data, MAX_DATA_LEN, 0); + WriteInt32(&req, 0); WriteUint32(&req, RES_FEATURE); WriteUint32(&req, OP_GET); WriteString(&req, service); WriteBool(&req, feature == NULL); if (feature != NULL) { - WriteBool(&req, feature); + WriteString(&req, feature); } IpcIo reply; void *replyBuf = NULL; - SvcIdentity samgr = {SAMGR_HANDLE, SAMGR_TOKEN, SAMGR_COOKIE}; + SvcIdentity *samgr = GetContextObject(); MessageOption flag; MessageOptionInit(&flag); - int ret = SendRequest(samgr, INVALID_INDEX, &req, &reply, flag, (uintptr_t *)&replyBuf); + int ret = SendRequest(*samgr, INVALID_INDEX, &req, &reply, flag, (uintptr_t *)&replyBuf); int32_t sa_ret = EC_FAILURE; ret = (ret != EC_SUCCESS) ? EC_FAILURE : ReadInt32(&reply, &sa_ret); SvcIdentity target = {INVALID_INDEX, INVALID_INDEX, INVALID_INDEX}; if (sa_ret == EC_SUCCESS) { ReadRemoteObject(&reply, &target); + uint32_t token; + ReadUint32(&reply, &token); + target.token = (uintptr_t)token; } if (ret == EC_PERMISSION) { HILOG_INFO(HILOG_MODULE_SAMGR, "Cannot Access<%s, %s> No Permission!", service, feature); @@ -300,25 +248,5 @@ static SvcIdentity QueryIdentity(const char *service, const char *feature) static SvcIdentity QueryRemoteIdentity(const char *deviceId, const char *service, const char *feature) { - char saName[2 * MAX_NAME_LEN + 2]; - (void)sprintf_s(saName, 2 * MAX_NAME_LEN + 2, "%s#%s", service?service:"", feature?feature:""); - HILOG_INFO(HILOG_MODULE_SAMGR, "saName %s, make remote binder start", saName); - - SvcIdentity target = {INVALID_INDEX, INVALID_INDEX, INVALID_INDEX};; - SaNode *saNode = GetSaNodeBySaName(service, feature); - if (saNode == NULL) { - HILOG_ERROR(HILOG_MODULE_SAMGR, "service: %s feature: %s have no saId in sa map", service, feature); - return target; - } - int32_t ret = MakeRemoteBinder(saName, strlen(saName), deviceId, strlen(deviceId), saNode->saId, 0, - &target); - if (ret != EC_SUCCESS) { - HILOG_ERROR(HILOG_MODULE_SAMGR, "MakeRemoteBinder failed"); - return target; - } - target.handle = GetNextHandle(); - extern void WaitForProxyInit(SvcIdentity *svc); - WaitForProxyInit(&target); - HILOG_ERROR(HILOG_MODULE_SAMGR, "MakeRemoteBinder sid handle=%d", target.handle); - return target; + return QueryRemoteIdentityInner(deviceId, service, feature); } diff --git a/samgr_endpoint/source/default_client_small_adapter.c b/samgr_endpoint/source/default_client_small_adapter.c new file mode 100644 index 0000000000000000000000000000000000000000..087c5cf13604cf0fa4de46a5300250837f8ce45f --- /dev/null +++ b/samgr_endpoint/source/default_client_small_adapter.c @@ -0,0 +1,30 @@ +/* + * 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. + */ +#include "default_client_adapter.h" + +uintptr_t GetRemoteSaIdInner(const char *service, const char *feature) +{ + return 0; +} +void ProxyInvokeArgInner(IpcIo *reply, IClientHeader *header) +{ + WriteInt32(reply, (int32_t)header->target.token); +} + +SvcIdentity QueryRemoteIdentityInner(const char *deviceId, const char *service, const char *feature) +{ + SvcIdentity target = {INVALID_INDEX, INVALID_INDEX, INVALID_INDEX}; + return target; +} \ No newline at end of file diff --git a/samgr_endpoint/source/endpoint.h b/samgr_endpoint/source/endpoint.h old mode 100755 new mode 100644 index d36c256027b79a4ab86acb382c7791ae651cd552..233c45f88e5d0a0266b59c3b296e98e6c9d6a66c --- a/samgr_endpoint/source/endpoint.h +++ b/samgr_endpoint/source/endpoint.h @@ -55,18 +55,10 @@ typedef enum OptionID { OP_ALL, } OptionID; typedef struct Endpoint Endpoint; - -#ifndef MINI_SAMGR_LITE_RPC -typedef int (*RegisterEndpoint)(const IpcContext *context, SvcIdentity *identity); -#else typedef int (*RegisterEndpoint)(SvcIdentity *identity, int token, const char *service, const char *feature); -#endif struct Endpoint { const char *name; -#ifndef MINI_SAMGR_LITE_RPC - IpcContext *context; -#endif Vector routers; ThreadId boss; uint32 deadId; diff --git a/samgr_endpoint/source/endpoint_rpc.c b/samgr_endpoint/source/endpoint_rpc.c index fde0b9b80690ad9da931922d167cacb5a7f8a3a6..9a40af6d3e186e75a19559d298cd740f8f4be4a8 100644 --- a/samgr_endpoint/source/endpoint_rpc.c +++ b/samgr_endpoint/source/endpoint_rpc.c @@ -27,67 +27,15 @@ #include "memory_adapter.h" #include "policy_define.h" #include "pthread.h" -#include "samgr_server.h" #include "serializer.h" #include "thread_adapter.h" - -typedef struct RemoteRegister RemoteRegister; -struct RemoteRegister { - MutexId mtx; - Endpoint *endpoint; - Vector clients; -}; - -#undef LOG_TAG -#undef LOG_DOMAIN -#define LOG_TAG "Samgr" -#define LOG_DOMAIN 0xD001800 - -#ifdef LITE_LINUX_BINDER_IPC -#define MAX_STACK_SIZE 0x100000 -#else -#define MAX_STACK_SIZE 0x1000 -#endif -#define MAX_OBJECT_NUM 5 -#define MAX_RETRY_TIMES 300 -#define RETRY_INTERVAL (50 * 1000) -#define MAX_REGISTER_RETRY_TIMES 10 -#define REGISTER_RETRY_INTERVAL 2 -#define MAX_POLICY_NUM 8 - -#ifndef MAX_BUCKET_RATE -#define MAX_BUCKET_RATE 1000 -#endif - -#ifndef MAX_BURST_RATE -#define MAX_BURST_RATE (MAX_BUCKET_RATE + (MAX_BUCKET_RATE >> 1)) -#endif - -#define SAMGR_SERVICE "samgr" - -typedef struct Router { - SaName saName; - Identity identity; - IServerProxy *proxy; - PolicyTrans *policy; - uint32 policyNum; -} Router; +#include "samgr_ipc_adapter.h" static int CompareIServerProxy(const IServerProxy *proxy1, const IServerProxy *proxy2); static IServerProxy *GetIServerProxy(const Router *router); -static int Dispatch(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option); -static void HandleIpc(const Request *request, const Response *response); -static int OnSamgrServerExit(void *ipcMsg, IpcIo *data, void *argv); -static int RegisterRemoteFeatures(Endpoint *endpoint); -static void Listen(Endpoint *endpoint, int index, const char *service, const char *feature); -static boolean JudgePolicy(uid_t callingUid, const PolicyTrans *policy, uint32 policyNum); -static boolean SearchFixedPolicy(uid_t callingUid, PolicyTrans policy); static int AddPolicyToRouter(const Endpoint *endpoint, const SvcIdentity *saInfo, const PolicyTrans *policy, uint32 policyNum); static int RegisterRemoteEndpoint(SvcIdentity *identity, int token, const char *service, const char *feature); -static int RegisterIdentity(const SaName *saName, SvcIdentity *saInfo, - PolicyTrans **policy, uint32 *policyNum); -static void GetRemotePolicy(IpcIo *reply, PolicyTrans **policy, uint32 *policyNum); Endpoint *SAMGR_CreateEndpoint(const char *name, RegisterEndpoint registry) { @@ -115,10 +63,21 @@ int SAMGR_AddRouter(Endpoint *endpoint, const SaName *saName, const Identity *id } IServerProxy *serverProxy = NULL; proxy->QueryInterface(proxy, SERVER_PROXY_VER, (void *)&serverProxy); +#ifndef MINI_SAMGR_LITE_RPC + if (serverProxy == NULL) { + return EC_INVALID; + } int index = VECTOR_FindByKey(&endpoint->routers, proxy); if (index != INVALID_INDEX) { serverProxy->Release((IUnknown *)serverProxy); + return index; } +#else + int index = VECTOR_FindByKey(&endpoint->routers, proxy); + if (index != INVALID_INDEX) { + serverProxy->Release((IUnknown *)serverProxy); + } +#endif Router *router = SAMGR_Malloc(sizeof(Router)); if (router == NULL) { HILOG_ERROR(HILOG_MODULE_SAMGR, "Memory is not enough! Identity<%d, %d>", @@ -148,6 +107,7 @@ int32 SAMGR_AddSysCap(const Endpoint *endpoint, const char *sysCap, BOOL isReg) IpcIo req; uint8 data[MAX_DATA_LEN]; IpcIoInit(&req, data, MAX_DATA_LEN, 0); + WriteInt32(&req, 0); WriteUint32(&req, RES_SYSCAP); WriteUint32(&req, OP_PUT); WriteBool(&req, sysCap); @@ -155,10 +115,10 @@ int32 SAMGR_AddSysCap(const Endpoint *endpoint, const char *sysCap, BOOL isReg) IpcIo reply; void *replyBuf = NULL; - SvcIdentity samgr = {SAMGR_HANDLE, SAMGR_TOKEN, SAMGR_COOKIE}; + SvcIdentity *samgr = GetContextObject(); MessageOption option; MessageOptionInit(&option); - int ret = SendRequest(samgr, INVALID_INDEX, &req, &reply, + int ret = SendRequest(*samgr, INVALID_INDEX, &req, &reply, option, (uintptr_t *)&replyBuf); ret = -ret; int32_t ipcRet = ret; @@ -183,16 +143,17 @@ int32 SAMGR_GetSysCap(const Endpoint *endpoint, const char *sysCap, BOOL *isReg) IpcIo req; uint8 data[MAX_DATA_LEN]; IpcIoInit(&req, data, MAX_DATA_LEN, 0); + WriteInt32(&req, 0); WriteUint32(&req, RES_SYSCAP); WriteUint32(&req, OP_GET); WriteBool(&req, sysCap); IpcIo reply; void *replyBuf = NULL; - SvcIdentity samgr = {SAMGR_HANDLE, SAMGR_TOKEN, SAMGR_COOKIE}; + SvcIdentity *samgr = GetContextObject(); MessageOption option; MessageOptionInit(&option); - int ret = SendRequest(samgr, INVALID_INDEX, &req, &reply, + int ret = SendRequest(*samgr, INVALID_INDEX, &req, &reply, option, (uintptr_t *)&replyBuf); ret = -ret; *isReg = FALSE; @@ -215,13 +176,14 @@ static int SendGetAllSysCapsRequest(const Endpoint *endpoint, uint32 startIdx, I IpcIo req; uint8 data[MAX_DATA_LEN]; IpcIoInit(&req, data, MAX_DATA_LEN, 0); + WriteInt32(&req, 0); WriteUint32(&req, RES_SYSCAP); WriteUint32(&req, OP_ALL); WriteUint32(&req, startIdx); - SvcIdentity samgr = {SAMGR_HANDLE, SAMGR_TOKEN, SAMGR_COOKIE}; + SvcIdentity *samgr = GetContextObject(); MessageOption option; MessageOptionInit(&option); - int ret = SendRequest(samgr, INVALID_INDEX, &req, reply, + int ret = SendRequest(*samgr, INVALID_INDEX, &req, reply, option, (uintptr_t *)replyBuf); HILOG_DEBUG(HILOG_MODULE_SAMGR, "SendGetAllSysCapsRequest startIdx:%u, ret:%d!", startIdx, ret); return -ret; @@ -322,11 +284,6 @@ int SAMGR_ProcPolicy(const Endpoint *endpoint, const SaName *saName, int token) return ret; } -static void Listen(Endpoint *endpoint, int index, const char *service, const char *feature) -{ - endpoint->registerEP(&endpoint->identity, index, service, feature); -} - static int AddPolicyToRouter(const Endpoint *endpoint, const SvcIdentity *saInfo, const PolicyTrans *policy, uint32 policyNum) { @@ -364,55 +321,6 @@ static int AddPolicyToRouter(const Endpoint *endpoint, const SvcIdentity *saInfo return EC_SUCCESS; } -static int Dispatch(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option) -{ - extern RemoteRegister g_remoteRegister; - Endpoint *endpoint = g_remoteRegister.endpoint; - uintptr_t saId = 0; - ReadInt32(data, &saId); - SaNode *saNode = GetSaNodeBySaId(saId); - if (saNode == NULL) { - HILOG_WARN(HILOG_MODULE_SAMGR, "get sa node by sa id %d is NULL", saId); - goto ERROR; - } - if (TB_CheckMessage(&endpoint->bucket) == BUCKET_BUSY) { - HILOG_WARN(HILOG_MODULE_SAMGR, "Flow Control <%u> is NULL", saNode->token); - goto ERROR; - } - - Router *router = VECTOR_At(&endpoint->routers, saNode->token); - if (router == NULL) { - HILOG_ERROR(HILOG_MODULE_SAMGR, "Router <%s, %u> is NULL", endpoint->name, saNode->token); - goto ERROR; - } - Response resp = {0}; - resp.data = endpoint; - Request request = {0}; - request.msgId = saNode->token; - request.data = data; - resp.reply = reply; - request.msgValue = code; - uint32 *ref = NULL; - int ret = SAMGR_SendSharedDirectRequest(&router->identity, &request, &resp, &ref, HandleIpc); - if (ret != EC_SUCCESS) { - HILOG_ERROR(HILOG_MODULE_SAMGR, "Router[%u] Service<%d, %d> is busy", - saNode->token, router->identity.serviceId, router->identity.featureId); - goto ERROR; - } - - return EC_SUCCESS; -ERROR: - return EC_INVALID; -} - -static void HandleIpc(const Request *request, const Response *response) -{ - Endpoint *endpoint = (Endpoint *)response->data; - Router *router = VECTOR_At(&endpoint->routers, request->msgId); - - router->proxy->Invoke(router->proxy, request->msgValue, NULL, request->data, response->reply); -} - static int CompareIServerProxy(const IServerProxy *proxy1, const IServerProxy *proxy2) { if (proxy1 == proxy2) { @@ -429,196 +337,7 @@ static IServerProxy *GetIServerProxy(const Router *router) return router->proxy; } -static int RegisterIdentity(const SaName *saName, SvcIdentity *saInfo, - PolicyTrans **policy, uint32 *policyNum) -{ - IpcIo req; - uint8 data[MAX_DATA_LEN]; - IpcIoInit(&req, data, MAX_DATA_LEN, 0); - WriteUint32(&req, RES_FEATURE); - WriteUint32(&req, OP_PUT); - WriteBool(&req, saName->service); - WriteBool(&req, saName->feature == NULL); - if (saName->feature != NULL) { - WriteBool(&req, saName->feature); - } - WriteUint32(&req, saInfo->token); - IpcIo reply; - void *replyBuf = NULL; - SvcIdentity samgr = {SAMGR_HANDLE, SAMGR_TOKEN, SAMGR_COOKIE}; - MessageOption option; - MessageOptionInit(&option); - int ret = SendRequest(samgr, INVALID_INDEX, &req, &reply, option, - (uintptr_t *)&replyBuf); - ret = -ret; - int32_t ipcRet = EC_FAILURE; - if (ret == EC_SUCCESS) { - ret = ReadInt32(&reply, &ipcRet); - } - if (ipcRet == EC_SUCCESS) { - SvcIdentity target; - (void)ReadRemoteObject(&reply, &target); - GetRemotePolicy(&reply, policy, policyNum); - } - if (replyBuf != NULL) { - FreeBuffer(replyBuf); - } - return ret; -} - -static int RegisterRemoteFeatures(Endpoint *endpoint) -{ - int nums = 0; - int size = VECTOR_Size(&endpoint->routers); - int i; - SvcIdentity identity; - for (i = 0; i < size; ++i) { - Router *router = VECTOR_At(&endpoint->routers, i); - if (router == NULL) { - continue; - } - - identity.handle = endpoint->identity.handle; - identity.token = i; - int ret = RegisterIdentity(&(router->saName), &identity, &(router->policy), - &(router->policyNum)); - if (ret == EC_SUCCESS) { - ++nums; - } - HILOG_DEBUG(HILOG_MODULE_SAMGR, "RegisterRemoteFeatures<%s, %s> ret:%d", - router->saName.service, router->saName.feature, ret); - } - return VECTOR_Num(&endpoint->routers) - nums; -} - static int RegisterRemoteEndpoint(SvcIdentity *identity, int token, const char *service, const char *feature) { - IpcObjectStub *objectStubOne = (IpcObjectStub *)calloc(1, sizeof(IpcObjectStub)); - if (objectStubOne == NULL) { - return -1; - } - objectStubOne->func = Dispatch; - objectStubOne->isRemote = true; - // handle is used by rpc, should be bigger than 0 - identity->handle = token + 1; - identity->cookie = objectStubOne; - // token is used by router index, should be itself, and save in SaNode - identity->token = token; - extern int AddEndpoint(SvcIdentity identity, const char *service, const char *feature); - AddEndpoint(*identity, service, feature); - return EC_SUCCESS; -} - -static int OnSamgrServerExit(void *ipcMsg, IpcIo *data, void *argv) -{ - (void)data; - HILOG_ERROR(HILOG_MODULE_SAMGR, "Disconnect to samgr server!"); - Endpoint *endpoint = (Endpoint *)argv; - if (endpoint == NULL || endpoint->registerEP == NULL) { - return EC_FAILURE; - } - if (ipcMsg != NULL) { - FreeBuffer(ipcMsg); - } - int size = VECTOR_Size(&endpoint->routers); - int i; - for (i = 0; i < size; i++) { - Router *router = VECTOR_At(&endpoint->routers, i); - if (router == NULL) { - continue; - } - SAMGR_Free(router->policy); - router->policy = NULL; - router->policyNum = 0; - } - - SvcIdentity old = endpoint->identity; - while (endpoint->registerEP(&endpoint->identity, 0, "", "") != EC_SUCCESS) { - HILOG_ERROR(HILOG_MODULE_SAMGR, "Reconnect to samgr server failed!"); - usleep(RETRY_INTERVAL); - } - SvcIdentity new = endpoint->identity; - if (old.handle != new.handle || old.cookie != new.cookie || old.token != new.token) { - HILOG_ERROR(HILOG_MODULE_SAMGR, "Samgr server identity error!"); - exit(-1); - } - - SvcIdentity identity = {SAMGR_HANDLE, SAMGR_TOKEN, SAMGR_COOKIE}; - (void)RemoveDeathRecipient(identity, endpoint->deadId); - (void)AddDeathRecipient(identity, OnSamgrServerExit, endpoint, &endpoint->deadId); - int remain = RegisterRemoteFeatures(endpoint); - HILOG_INFO(HILOG_MODULE_SAMGR, "Reconnect and register finished! remain<%d> iunknown!", remain); - return EC_SUCCESS; -} - -static void GetRemotePolicy(IpcIo *reply, PolicyTrans **policy, uint32 *policyNum) -{ - if (reply == NULL) { - return; - } - uint32 i; - uint32 j; - ReadUint32(reply, policyNum); - if (*policyNum > MAX_POLICY_NUM) { - *policyNum = MAX_POLICY_NUM; - } - SAMGR_Free(*policy); - if (*policyNum == 0) { - *policy = NULL; - return; - } - *policy = (PolicyTrans *)SAMGR_Malloc(sizeof(PolicyTrans) * (*policyNum)); - if (*policy == NULL) { - return; - } - for (i = 0; i < *policyNum; i++) { - if (ReadInt32(reply, &(*policy)[i].type)) { - switch ((*policy)[i].type) { - case RANGE: - ReadInt32(reply, &((*policy)[i].uidMin)); - ReadInt32(reply, &((*policy)[i].uidMax)); - break; - case FIXED: - for (j = 0; j < UID_SIZE; j++) { - ReadInt32(reply, &((*policy)[i].fixedUid[j])); - } - break; - case BUNDLENAME: - ReadInt32(reply, &((*policy)[i].fixedUid[0])); - break; - default: - break; - } - } - } -} - -static boolean JudgePolicy(uid_t callingUid, const PolicyTrans *policy, uint32 policyNum) -{ - if (policy == NULL) { - HILOG_ERROR(HILOG_MODULE_SAMGR, "Policy is NULL! Num is %u", policyNum); - return FALSE; - } - - uint32 i; - for (i = 0; i < policyNum; i++) { - if (policy[i].type == RANGE && callingUid >= policy[i].uidMin && callingUid <= policy[i].uidMax) { - return TRUE; - } - if (policy[i].type == FIXED && SearchFixedPolicy(callingUid, policy[i])) { - return TRUE; - } - } - return FALSE; -} - -static boolean SearchFixedPolicy(uid_t callingUid, PolicyTrans policy) -{ - int i; - for (i = 0; i < UID_SIZE; i++) { - if (callingUid == policy.fixedUid[i]) { - return TRUE; - } - } - return FALSE; -} + return ClientRegisterRemoteEndpoint(identity, token, service, feature); +} \ No newline at end of file diff --git a/samgr_endpoint/source/sa_store.c b/samgr_endpoint/source/sa_store.c old mode 100755 new mode 100644 index d9851d46d8faeae9ea581eaa5b7ae9c45cd04b7d..016c77780f0696ef193d263b5e0b0d1c4d288128 --- a/samgr_endpoint/source/sa_store.c +++ b/samgr_endpoint/source/sa_store.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Huawei Device Co., Ltd. + * Copyright (c) 2020-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 @@ -17,7 +17,6 @@ #include #include "common.h" #include "memory_adapter.h" - #ifdef MINI_SAMGR_LITE_RPC #include "samgr_server.h" #endif @@ -97,11 +96,8 @@ int SASTORA_Save(SAStore *saStore, const char *service, const char *feature, con SAMGR_Free(curNode); return EC_INVALID; } - curNode->info.handle = identity->handle; -#ifdef MINI_SAMGR_LITE_RPC curNode->info.cookie = identity->cookie; -#endif curNode->info.head = NULL; curNode->next = saStore->root; saStore->root = curNode; @@ -221,9 +217,7 @@ SvcIdentity SASTORA_Find(SAStore *saStore, const char *service, const char *feat } identity.handle = curNode->info.handle; -#ifdef MINI_SAMGR_LITE_RPC identity.cookie = curNode->info.cookie; -#endif FeatureNode *featureNode = FindFeatureByName(curNode->info.head, feature); if (featureNode != NULL) { identity.token = featureNode->token; diff --git a/samgr_endpoint/source/sa_store.h b/samgr_endpoint/source/sa_store.h old mode 100755 new mode 100644 index 19754fb37ae4acd2df2bca81b80cacfbc9c69d12..a35860722c6f53a13d45b96ffcf57db4016eecce --- a/samgr_endpoint/source/sa_store.h +++ b/samgr_endpoint/source/sa_store.h @@ -16,13 +16,8 @@ #define LITE_SA_STORE_H #include - -#ifndef MINI_SAMGR_LITE_RPC -#include -#else #include #include -#endif #ifdef __cplusplus #if __cplusplus @@ -39,9 +34,7 @@ typedef struct PidHandle PidHandle; struct ServiceInfo { char name[MAX_NAME_LEN]; uint32 handle; -#ifdef MINI_SAMGR_LITE_RPC uintptr_t cookie; -#endif FeatureNode *head; }; @@ -69,6 +62,7 @@ struct PidHandle { pid_t pid; uid_t uid; uint32 handle; + uintptr_t cookie; uint32 deadId; }; diff --git a/samgr_endpoint/source/samgr_ipc_adapter.h b/samgr_endpoint/source/samgr_ipc_adapter.h new file mode 100644 index 0000000000000000000000000000000000000000..3f41598c402f154f4e0c415ce78f07d837dd4ca5 --- /dev/null +++ b/samgr_endpoint/source/samgr_ipc_adapter.h @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2020 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 SAMGR_IPC_ADAPTER_H +#define SAMGR_IPC_ADAPTER_H + +#include "endpoint.h" + +#include +#include +#include +#include +#include +#include + +#include "default_client.h" +#include "ipc_skeleton.h" +#include "iproxy_server.h" +#include "memory_adapter.h" +#include "policy_define.h" +#include "pthread.h" +#include "serializer.h" +#include "thread_adapter.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif +typedef struct RemoteRegister RemoteRegister; +struct RemoteRegister { + MutexId mtx; + Endpoint *endpoint; + Vector clients; +}; + +#undef LOG_TAG +#undef LOG_DOMAIN +#define LOG_TAG "Samgr" +#define LOG_DOMAIN 0xD001800 + +#ifdef LITE_LINUX_BINDER_IPC +#define MAX_STACK_SIZE 0x100000 +#else +#define MAX_STACK_SIZE 0x1000 +#endif +#define MAX_OBJECT_NUM 5 +#define MAX_RETRY_TIMES 300 +#define RETRY_INTERVAL (50 * 1000) +#define MAX_REGISTER_RETRY_TIMES 10 +#define REGISTER_RETRY_INTERVAL 2 +#define MAX_POLICY_NUM 8 + +#ifndef MAX_BUCKET_RATE +#define MAX_BUCKET_RATE 1000 +#endif + +#ifndef MAX_BURST_RATE +#define MAX_BURST_RATE (MAX_BUCKET_RATE + (MAX_BUCKET_RATE >> 1)) +#endif + +#define SAMGR_SERVICE "samgr" + +typedef struct Router { + SaName saName; + Identity identity; + IServerProxy *proxy; + PolicyTrans *policy; + uint32 policyNum; +} Router; + +int ClientRegisterRemoteEndpoint(SvcIdentity *identity, int token, const char *service, const char *feature); +void Listen(Endpoint *endpoint, int token, const char *service, const char *feature); +int RegisterIdentity(const SaName *saName, SvcIdentity *saInfo, PolicyTrans **policy, uint32 *policyNum); +int GetRemoteToken(IpcIo *data); +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif +#endif // LITE_DEFAULT_CLIENT_H diff --git a/samgr_endpoint/source/samgr_mini_ipc_adapter.c b/samgr_endpoint/source/samgr_mini_ipc_adapter.c new file mode 100644 index 0000000000000000000000000000000000000000..ae5e68c765fe9499a208d248b06efca15640aae3 --- /dev/null +++ b/samgr_endpoint/source/samgr_mini_ipc_adapter.c @@ -0,0 +1,175 @@ +/* + * 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. + */ +#include "samgr_ipc_adapter.h" +#include "samgr_server.h" +#include "dbinder_service.h" +static int Dispatch(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option); +static void HandleIpc(const Request *request, const Response *response); +static void GetRemotePolicy(IpcIo *reply, PolicyTrans **policy, uint32 *policyNum); +int ClientRegisterRemoteEndpoint(SvcIdentity *identity, int token, const char *service, const char *feature) +{ + IpcObjectStub *objectStubOne = (IpcObjectStub *)calloc(1, sizeof(IpcObjectStub)); + if (objectStubOne == NULL) { + return -1; + } + objectStubOne->func = Dispatch; + objectStubOne->isRemote = true; + // handle is used by rpc, should be bigger than 0 + identity->handle = token + 1; + identity->cookie = objectStubOne; + // token is used by router index, should be itself, and save in SaNode + identity->token = token; + extern int AddEndpoint(SvcIdentity identity, const char *service, const char *feature); + return AddEndpoint(*identity, service, feature); +} + +void Listen(Endpoint *endpoint, int token, const char *service, const char *feature) +{ + endpoint->registerEP(&endpoint->identity, token, service, feature); +} + +static int Dispatch(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option) +{ + extern RemoteRegister g_remoteRegister; + Endpoint *endpoint = g_remoteRegister.endpoint; + int token = GetRemoteToken(data); + if (token == EC_INVALID) { + goto ERROR; + } + if (TB_CheckMessage(&endpoint->bucket) == BUCKET_BUSY) { + HILOG_WARN(HILOG_MODULE_SAMGR, "Flow Control <%u> is NULL", token); + goto ERROR; + } + + Router *router = VECTOR_At(&endpoint->routers, token); + if (router == NULL) { + HILOG_ERROR(HILOG_MODULE_SAMGR, "Router <%s, %u> is NULL", endpoint->name, token); + goto ERROR; + } + Response resp = {0}; + resp.data = endpoint; + Request request = {0}; + request.msgId = token; + request.data = data; + resp.reply = reply; + request.msgValue = code; + uint32 *ref = NULL; + int ret = SAMGR_SendSharedDirectRequest(&router->identity, &request, &resp, &ref, HandleIpc); + if (ret != EC_SUCCESS) { + HILOG_ERROR(HILOG_MODULE_SAMGR, "Router[%u] Service<%d, %d> is busy", + token, router->identity.serviceId, router->identity.featureId); + goto ERROR; + } + + return EC_SUCCESS; +ERROR: + return EC_INVALID; +} + +static void HandleIpc(const Request *request, const Response *response) +{ + Endpoint *endpoint = (Endpoint *)response->data; + Router *router = VECTOR_At(&endpoint->routers, request->msgId); + + router->proxy->Invoke(router->proxy, request->msgValue, NULL, request->data, response->reply); +} + +int RegisterIdentity(const SaName *saName, SvcIdentity *saInfo, PolicyTrans **policy, uint32 *policyNum) +{ + IpcIo req; + uint8 data[MAX_DATA_LEN]; + IpcIoInit(&req, data, MAX_DATA_LEN, 0); + WriteUint32(&req, RES_FEATURE); + WriteUint32(&req, OP_PUT); + WriteString(&req, saName->service); + WriteBool(&req, saName->feature == NULL); + if (saName->feature != NULL) { + WriteBool(&req, saName->feature); + } + WriteUint32(&req, saInfo->token); + IpcIo reply; + void *replyBuf = NULL; + SvcIdentity *samgr = GetContextObject(); + MessageOption option; + MessageOptionInit(&option); + int ret = SendRequest(*samgr, INVALID_INDEX, &req, &reply, option, + (uintptr_t *)&replyBuf); + ret = -ret; + int32_t ipcRet = EC_FAILURE; + if (ret == EC_SUCCESS) { + ret = ReadInt32(&reply, &ipcRet); + } + if (ipcRet == EC_SUCCESS) { + SvcIdentity target; + (void)ReadRemoteObject(&reply, &target); + GetRemotePolicy(&reply, policy, policyNum); + } + if (replyBuf != NULL) { + FreeBuffer(replyBuf); + } + return ret; +} + +int GetRemoteToken(IpcIo *data) +{ + uintptr_t saId = 0; + ReadInt32(data, &saId); + SaNode *saNode = GetSaNodeBySaId(saId); + if (saNode == NULL) { + HILOG_WARN(HILOG_MODULE_SAMGR, "get sa node by sa id %d is NULL", saId); + return EC_INVALID; + } + return saNode->token; +} + +static void GetRemotePolicy(IpcIo *reply, PolicyTrans **policy, uint32 *policyNum) +{ + if (reply == NULL) { + return; + } + ReadUint32(reply, policyNum); + if (*policyNum > MAX_POLICY_NUM) { + *policyNum = MAX_POLICY_NUM; + } + SAMGR_Free(*policy); + if (*policyNum == 0) { + *policy = NULL; + return; + } + *policy = (PolicyTrans *)SAMGR_Malloc(sizeof(PolicyTrans) * (*policyNum)); + if (*policy == NULL) { + return; + } + for (uint32 i = 0; i < *policyNum; i++) { + if (ReadInt32(reply, &(*policy)[i].type)) { + switch ((*policy)[i].type) { + case RANGE: + ReadInt32(reply, &((*policy)[i].uidMin)); + ReadInt32(reply, &((*policy)[i].uidMax)); + break; + case FIXED: + for (uint32 j = 0; j < UID_SIZE; j++) { + ReadInt32(reply, &((*policy)[i].fixedUid[j])); + } + break; + case BUNDLENAME: + ReadInt32(reply, &((*policy)[i].fixedUid[0])); + break; + default: + break; + } + } + } +} \ No newline at end of file diff --git a/samgr_endpoint/source/samgr_small_ipc_adapter.c b/samgr_endpoint/source/samgr_small_ipc_adapter.c new file mode 100644 index 0000000000000000000000000000000000000000..a58199cb50d39526596aea4d32f030d49fb7c54c --- /dev/null +++ b/samgr_endpoint/source/samgr_small_ipc_adapter.c @@ -0,0 +1,362 @@ +/* + * 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. + */ +#include "samgr_ipc_adapter.h" + +typedef struct IRegisterEpArg IRegisterEpArg; +struct IRegisterEpArg { + Endpoint *endpoint; + int token; + char *service; + char *feature; +}; +static int Dispatch(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option); +static void HandleIpc(const Request *request, const Response *response); +static void *Receive(void *argv); +static int RegisterRemoteFeatures(Endpoint *endpoint); +static void OnSamgrServerExit(void *argv); +static void GetRemotePolicy(IpcIo *reply, PolicyTrans **policy, uint32 *policyNum); +static boolean JudgePolicy(uid_t callingUid, const PolicyTrans *policy, uint32 policyNum); +static boolean SearchFixedPolicy(uid_t callingUid, PolicyTrans policy); +int ClientRegisterRemoteEndpoint(SvcIdentity *identity, int token, const char *service, const char *feature) +{ + IpcIo req; + uint8 data[MAX_DATA_LEN]; + IpcIoInit(&req, data, MAX_DATA_LEN, 1); + // add samgr server token + WriteInt32(&req, 0); + WriteUint32(&req, RES_ENDPOINT); + WriteUint32(&req, OP_POST); + bool ret = WriteRemoteObject(&req, identity); + if (!ret) { + return EC_FAILURE; + } + uint32 retry = 0; + while (retry < MAX_RETRY_TIMES) { + ++retry; + IpcIo reply; + void *replyBuf = NULL; + MessageOption option; + MessageOptionInit(&option); + SvcIdentity *samgr = GetContextObject(); + + int err = SendRequest(*samgr, INVALID_INDEX, &req, &reply, option, (uintptr_t *)&replyBuf); + if (err == EC_SUCCESS) { + ret = ReadInt32(&reply, &identity->handle); + if (!ret || identity->handle == INVALID_INDEX) { + continue; + } + if (replyBuf != NULL) { + FreeBuffer(replyBuf); + } + return EC_SUCCESS; + } + usleep(RETRY_INTERVAL); + } + return EC_FAILURE; +} + +void Listen(Endpoint *endpoint, int token, const char *service, const char *feature) +{ + if (endpoint->boss != NULL) { + return; + } + ThreadAttr attr = {endpoint->name, MAX_STACK_SIZE, PRI_ABOVE_NORMAL, 0, 0}; + IRegisterEpArg *registerEpArg = SAMGR_Malloc(sizeof(IRegisterEpArg)); + if (registerEpArg == NULL) { + HILOG_ERROR(HILOG_MODULE_SAMGR, "IRegisterEpArg Memory is not enough!"); + return; + } + IpcObjectStub *objectStubOne = (IpcObjectStub *)calloc(1, sizeof(IpcObjectStub)); + if (objectStubOne == NULL) { + HILOG_ERROR(HILOG_MODULE_SAMGR, "IpcObjectStub Memory is not enough!"); + return; + } + + objectStubOne->func = Dispatch; + objectStubOne->args = endpoint; + objectStubOne->isRemote = false; + endpoint->identity.cookie = objectStubOne; + // handle must -1 + endpoint->identity.handle = INVALID_INDEX; + endpoint->identity.token = SERVICE_TYPE_NORMAL; + + registerEpArg->endpoint = endpoint; + registerEpArg->token = token; + registerEpArg->service = service; + registerEpArg->feature = feature; + endpoint->boss = (ThreadId)THREAD_Create(Receive, registerEpArg, &attr); +} + +static void *Receive(void *argv) +{ + IRegisterEpArg *registerEpArg = (IRegisterEpArg *)argv; + if (registerEpArg == NULL || registerEpArg->endpoint->registerEP == NULL) { + return NULL; + } + int ret = EC_INVALID; + uint32 retry = 0; + while (retry < MAX_RETRY_TIMES) { + ret = registerEpArg->endpoint->registerEP(®isterEpArg->endpoint->identity, + registerEpArg->token, registerEpArg->service, registerEpArg->feature); + if (ret == EC_SUCCESS) { + SvcIdentity *samgr = GetContextObject(); + (void)RemoveDeathRecipient(*samgr, registerEpArg->endpoint->deadId); + (void)AddDeathRecipient(*samgr, OnSamgrServerExit, registerEpArg->endpoint, + ®isterEpArg->endpoint->deadId); + break; + } + ++retry; + usleep(RETRY_INTERVAL); + } + if (ret != EC_SUCCESS) { + HILOG_FATAL(HILOG_MODULE_SAMGR, "Register endpoint<%s>, handle<%u> failed! will exit to recover!", + registerEpArg->endpoint->name, registerEpArg->endpoint->identity.handle); + SAMGR_Free(registerEpArg); + return NULL; + } + registerEpArg->endpoint->running = TRUE; + if (strcmp(registerEpArg->endpoint->name, SAMGR_SERVICE) != 0) { + int remain = RegisterRemoteFeatures(registerEpArg->endpoint); + HILOG_INFO(HILOG_MODULE_SAMGR, "Register endpoint<%s> and iunknown finished! remain<%d> iunknown!", + registerEpArg->endpoint->name, remain); + } + SAMGR_Free(registerEpArg); + JoinWorkThread(); + return NULL; +} + +static int RegisterRemoteFeatures(Endpoint *endpoint) +{ + int nums = 0; + int size = VECTOR_Size(&endpoint->routers); + int i; + SvcIdentity identity; + for (i = 0; i < size; ++i) { + Router *router = VECTOR_At(&endpoint->routers, i); + if (router == NULL) { + continue; + } + identity.handle = endpoint->identity.handle; + identity.token = i; + int ret = RegisterIdentity(&(router->saName), &identity, &(router->policy), + &(router->policyNum)); + if (ret == EC_SUCCESS) { + ++nums; + } + HILOG_DEBUG(HILOG_MODULE_SAMGR, "RegisterRemoteFeatures<%s, %s> ret:%d", + router->saName.service, router->saName.feature, ret); + } + return VECTOR_Num(&endpoint->routers) - nums; +} + +int RegisterIdentity(const SaName *saName, SvcIdentity *saInfo, PolicyTrans **policy, uint32 *policyNum) +{ + IpcIo req; + uint8 data[MAX_DATA_LEN]; + IpcIoInit(&req, data, MAX_DATA_LEN, 0); + WriteInt32(&req, 0); + WriteUint32(&req, RES_FEATURE); + WriteUint32(&req, OP_PUT); + WriteString(&req, saName->service); + WriteBool(&req, saName->feature == NULL); + if (saName->feature != NULL) { + WriteString(&req, saName->feature); + } + WriteUint32(&req, saInfo->token); + IpcIo reply; + void *replyBuf = NULL; + SvcIdentity *samgr = GetContextObject(); + MessageOption option; + MessageOptionInit(&option); + int ret = SendRequest(*samgr, INVALID_INDEX, &req, &reply, option, + (uintptr_t *)&replyBuf); + ret = -ret; + int32_t ipcRet = EC_FAILURE; + if (ret == EC_SUCCESS) { + ReadInt32(&reply, &ipcRet); + } + if (ipcRet == EC_SUCCESS) { + SvcIdentity target; + (void)ReadRemoteObject(&reply, &target); + GetRemotePolicy(&reply, policy, policyNum); + } + if (replyBuf != NULL) { + FreeBuffer(replyBuf); + } + return ipcRet; +} +static int Dispatch(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option) +{ + Endpoint *endpoint = (Endpoint *)option.args; + int token = GetRemoteToken(data); + if (token == EC_INVALID) { + goto ERROR; + } + if (TB_CheckMessage(&endpoint->bucket) == BUCKET_BUSY) { + HILOG_WARN(HILOG_MODULE_SAMGR, "Flow Control <%u> is NULL", token); + goto ERROR; + } + Router *router = VECTOR_At(&endpoint->routers, token); + if (router == NULL) { + HILOG_ERROR(HILOG_MODULE_SAMGR, "Router <%s, %u> is NULL", endpoint->name, token); + goto ERROR; + } + + Response resp = {0}; + resp.data = endpoint; + resp.reply = reply; + Request request = {0}; + request.msgId = token; + request.data = data; + request.msgValue = code; + + HandleIpc(&request, &resp); + return EC_SUCCESS; +ERROR: + return EC_INVALID; +} + +static void HandleIpc(const Request *request, const Response *response) +{ + Endpoint *endpoint = (Endpoint *)response->data; + Router *router = VECTOR_At(&endpoint->routers, request->msgId); + if (router == NULL || router->proxy == NULL || router->proxy->Invoke == NULL) { + HILOG_ERROR(HILOG_MODULE_SAMGR, "Invalid IPC router!"); + return; + } + uid_t uid = GetCallingUid(); + if ((strcmp(router->saName.service, SAMGR_SERVICE) != 0) && + !JudgePolicy(uid, (const PolicyTrans *)(router->policy), router->policyNum)) { + HILOG_ERROR(HILOG_MODULE_SAMGR, "Consumer uid<%d> has no permission to access<%s, %d, %d>!", + uid, router->saName.service, router->identity.serviceId, router->identity.featureId); + return; + } + router->proxy->Invoke(router->proxy, request->msgValue, NULL, request->data, response->reply); +} + +static boolean JudgePolicy(uid_t callingUid, const PolicyTrans *policy, uint32 policyNum) +{ + if (policy == NULL) { + HILOG_ERROR(HILOG_MODULE_SAMGR, "Policy is NULL! Num is %u", policyNum); + return FALSE; + } + uint32 i; + for (i = 0; i < policyNum; i++) { + if (policy[i].type == RANGE && callingUid >= policy[i].uidMin && callingUid <= policy[i].uidMax) { + return TRUE; + } + if (policy[i].type == FIXED && SearchFixedPolicy(callingUid, policy[i])) { + return TRUE; + } + } + return FALSE; +} +static boolean SearchFixedPolicy(uid_t callingUid, PolicyTrans policy) +{ + int i; + for (i = 0; i < UID_SIZE; i++) { + if (callingUid == policy.fixedUid[i]) { + return TRUE; + } + } + return FALSE; +} +static void OnSamgrServerExit(void *argv) +{ + HILOG_ERROR(HILOG_MODULE_SAMGR, "Disconnect to samgr server!"); + Endpoint *endpoint = (Endpoint *)argv; + if (endpoint == NULL || endpoint->registerEP == NULL) { + return; + } + int size = VECTOR_Size(&endpoint->routers); + int i; + for (i = 0; i < size; i++) { + Router *router = VECTOR_At(&endpoint->routers, i); + if (router == NULL) { + continue; + } + SAMGR_Free(router->policy); + router->policy = NULL; + router->policyNum = 0; + } + + SvcIdentity old = endpoint->identity; + while (endpoint->registerEP(&endpoint->identity, 0, "", "") != EC_SUCCESS) { + HILOG_ERROR(HILOG_MODULE_SAMGR, "Reconnect to samgr server failed!"); + usleep(RETRY_INTERVAL); + } + SvcIdentity new = endpoint->identity; + if (old.handle != new.handle || old.cookie != new.cookie || old.token != new.token) { + HILOG_ERROR(HILOG_MODULE_SAMGR, "Samgr server identity error!"); + exit(-1); + } + + SvcIdentity identity = {SAMGR_HANDLE, SAMGR_TOKEN, SAMGR_COOKIE}; + ReleaseSvc(identity); + (void)AddDeathRecipient(identity, OnSamgrServerExit, endpoint, &endpoint->deadId); + int remain = RegisterRemoteFeatures(endpoint); + HILOG_INFO(HILOG_MODULE_SAMGR, "Reconnect and register finished! remain<%d> iunknown!", remain); +} + +int GetRemoteToken(IpcIo *data) +{ + int32_t token; + if (ReadInt32(data, &token)) { + return token; + } + return EC_INVALID; +} + +static void GetRemotePolicy(IpcIo *reply, PolicyTrans **policy, uint32 *policyNum) +{ + if (reply == NULL) { + return; + } + uint32 i; + uint32 j; + ReadUint32(reply, policyNum); + if (*policyNum > MAX_POLICY_NUM) { + *policyNum = MAX_POLICY_NUM; + } + SAMGR_Free(*policy); + if (*policyNum == 0) { + *policy = NULL; + return; + } + *policy = (PolicyTrans *)SAMGR_Malloc(sizeof(PolicyTrans) * (*policyNum)); + if (*policy == NULL) { + return; + } + for (i = 0; i < *policyNum; i++) { + if (ReadInt32(reply, &(*policy)[i].type)) { + switch ((*policy)[i].type) { + case RANGE: + ReadInt32(reply, &((*policy)[i].uidMin)); + ReadInt32(reply, &((*policy)[i].uidMax)); + break; + case FIXED: + for (j = 0; j < UID_SIZE; j++) { + ReadInt32(reply, &((*policy)[i].fixedUid[j])); + } + break; + case BUNDLENAME: + ReadInt32(reply, &((*policy)[i].fixedUid[0])); + break; + default: + break; + } + } + } +} \ No newline at end of file diff --git a/samgr_server/BUILD.gn b/samgr_server/BUILD.gn index a39b1caa342d03f250378dd2fbd99e1b4832a231..799fbf0a22478d1d1c6da2339ba0ddafb541a085 100644 --- a/samgr_server/BUILD.gn +++ b/samgr_server/BUILD.gn @@ -15,7 +15,7 @@ import("../config.gni") if (!enable_ohos_distributedschedule_samgr_lite_rpc_mini) { shared_library("server") { - sources = [ "source/samgr_server.c" ] + sources = [ "source/samgr_server_rpc.c" ] cflags = [ "-fPIC", @@ -24,19 +24,19 @@ if (!enable_ohos_distributedschedule_samgr_lite_rpc_mini) { include_dirs = [ "../samgr_endpoint/source", + "//base/security/permission/interfaces/innerkits/permission_lite", "//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr", "//foundation/distributedschedule/samgr_lite/interfaces/kits/registry", "//utils/native/lite/include", + "//third_party/cJSON", "//third_party/bounds_checking_function/include", - "//base/security/permission/interfaces/innerkits/permission_lite", "//base/security/permission/services/permission_lite/pms_base/include", - "//foundation/communication/ipc_lite/frameworks/liteipc/include", ] deps = [ "//foundation/distributedschedule/samgr_lite:ConfigFiles" ] public_deps = [ - "//foundation/communication/ipc_lite:liteipc_adapter", + "//foundation/communication/ipc/interfaces/innerkits/c/ipc:ipc_single", "//foundation/distributedschedule/samgr_lite/samgr:samgr", "//foundation/distributedschedule/samgr_lite/samgr_endpoint:store_source", "//third_party/bounds_checking_function:libsec_shared", diff --git a/samgr_server/source/samgr_server.h b/samgr_server/source/samgr_server.h old mode 100755 new mode 100644 index aa7150b11414d8b99ba668c1c2b377d09f897ba1..81fe38c1480481ab619b39506fb286aaf6b81301 --- a/samgr_server/source/samgr_server.h +++ b/samgr_server/source/samgr_server.h @@ -20,9 +20,7 @@ #include "iproxy_server.h" #include "endpoint.h" #include "sa_store.h" -#ifdef MINI_SAMGR_LITE_RPC #include "utils_list.h" -#endif #ifdef __cplusplus #if __cplusplus @@ -58,7 +56,6 @@ struct SysCapImpl { BOOL isRegister; }; -#ifdef MINI_SAMGR_LITE_RPC typedef struct { UTILS_DL_LIST list; SaName saName; @@ -67,7 +64,6 @@ typedef struct { } SaNode; SaNode *GetSaNodeBySaName(const char *service, const char *feature); SaNode *GetSaNodeBySaId(uintptr_t saId); -#endif #ifdef __cplusplus #if __cplusplus diff --git a/samgr_server/source/samgr_server_rpc.c b/samgr_server/source/samgr_server_rpc.c old mode 100755 new mode 100644 index 68d2a802a6b00bb364b8d92722820d14a2f10954..fabded24b3a36b6c34e11140402b320d84f9b93e --- a/samgr_server/source/samgr_server_rpc.c +++ b/samgr_server/source/samgr_server_rpc.c @@ -25,7 +25,9 @@ #include #include "cJSON.h" +#ifdef MINI_SAMGR_LITE_RPC #include "dbinder_service.h" +#endif #include "default_client.h" #include "ipc_skeleton.h" #include "memory_adapter.h" @@ -58,7 +60,7 @@ static int ProcEndpoint(SamgrServer *server, int32 option, void *origin, IpcIo * static int32 ProcPutFeature(SamgrServer *server, const void *origin, IpcIo *req, IpcIo *reply, SvcIdentity *identity); static int32 ProcGetFeature(SamgrServer *server, const void *origin, IpcIo *req, IpcIo *reply, SvcIdentity *identity); static int ProcFeature(SamgrServer *server, int32 option, void *origin, IpcIo *req, IpcIo *reply); -static int RegisterSamgrEndpoint(SvcIdentity* identity); +static int RegisterSamgrEndpoint(SvcIdentity* identity, int token, const char *service, const char *feature); static void TransmitPolicy(int ret, const SvcIdentity* identity, IpcIo *reply, const PolicyTrans *policy, uint32 policyNum); static void TransmitFixedPolicy(IpcIo *reply, PolicyTrans policy); @@ -125,7 +127,7 @@ SaNode *GetSaNodeBySaId(uintptr_t saId) pthread_mutex_unlock(&g_saMutex); return retNode; } - +#ifdef MINI_SAMGR_LITE_RPC void RpcStartSamgr(void) { pthread_setname_np(pthread_self(), "rpc_server"); @@ -133,10 +135,9 @@ void RpcStartSamgr(void) .cookie = 0 }; (void)SetContextObject(target); - StartDBinderService(); } - +#endif int32_t GetSystemAbilityById(int32_t saId, IpcIo *reply) { SaNode *saNode = GetSaNodeBySaId(saId); @@ -173,19 +174,29 @@ static void InitializeGSaList() static void InitializeRegistry(void) { +#ifdef MINI_SAMGR_LITE_RPC InitializeGSaList(); +#endif g_server.mtx = MUTEX_InitValue(); SASTORA_Init(&g_server.store); g_server.samgr = SAMGR_CreateEndpoint("samgr", RegisterSamgrEndpoint); SAMGR_GetInstance()->RegisterService((Service *)&g_server); g_server.sysCapMtx = MUTEX_InitValue(); g_server.sysCapabilitys = VECTOR_Make((VECTOR_Key)GetSysCapName, (VECTOR_Compare)strcmp); +#ifndef MINI_SAMGR_LITE_RPC + ParseSysCap(); +#endif } SYS_SERVICE_INIT(InitializeRegistry); static BOOL CanRequest(const void *origin) { +#ifndef MINI_SAMGR_LITE_RPC + pid_t uid = GetCallingUid(); + return uid < UID_HAP; +#else return TRUE; +#endif } static const char *GetName(Service *service) @@ -233,7 +244,7 @@ static int32 Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, int32_t option; ReadInt32(req, &option); if (server == NULL || resource >= RES_BUTT || g_functions[resource] == NULL) { - HILOG_ERROR(HILOG_MODULE_SAMGR, "Invalid Msg<%d, %d, %d>", resource, option, funcId); + HILOG_ERROR(HILOG_MODULE_SAMGR, "Invalid Msg<%d, %u, %d>", resource, option, funcId); return EC_INVALID; } return g_functions[resource](server, option, origin, req, reply); @@ -250,32 +261,35 @@ static int ProcEndpoint(SamgrServer *server, int32 option, void *origin, IpcIo * WriteInt32(reply, INVALID_INDEX); return EC_FAILURE; } - - pid_t pid = 0; + pid_t pid = GetCallingPid(); PidHandle handle; MUTEX_Lock(server->mtx); int index = SASTORA_FindHandleByPid(&g_server.store, pid, &handle); if (index == INVALID_INDEX) { SvcIdentity identity = {(int32)INVALID_INDEX, (uint32)INVALID_INDEX, (uint32)INVALID_INDEX}; -#ifdef __LINUX__ - IpcMsg* data = (IpcMsg*)origin; - if (data == NULL) { - HILOG_ERROR(HILOG_MODULE_SAMGR, "Register Endpoint origin null pointer!"); +#ifndef MINI_SAMGR_LITE_RPC + bool ret = ReadRemoteObject(req, &identity); + if (ret) { + // identity.handle <= 0 In-process communication,identity.handle > 0 Cross-process communication + if ((identity.handle <= 0) && (identity.cookie != 0)) { + identity.handle = 0; + } + } else { + WriteInt32(reply, INVALID_INDEX); return EC_FAILURE; } - identity.handle = data->target.handle; - BinderAcquire(g_server.samgr->context, identity.handle); #endif + handle.uid = GetCallingUid(); handle.pid = pid; - handle.uid = 0; handle.handle = identity.handle; + handle.cookie = identity.cookie; handle.deadId = INVALID_INDEX; (void)SASTORA_SaveHandleByPid(&server->store, handle); (void)RemoveDeathRecipient(identity, handle.deadId); (void)AddDeathRecipient(identity, OnEndpointExit, (void*)((uintptr_t)pid), &handle.deadId); } MUTEX_Unlock(server->mtx); - WriteUint32(reply, handle.handle); + WriteInt32(reply, handle.handle); HILOG_INFO(HILOG_MODULE_SAMGR, "Register Endpoint<%d, %d, %d>", handle.pid, handle.handle, handle.deadId); return EC_SUCCESS; } @@ -288,13 +302,12 @@ static int32 ProcPutFeature(SamgrServer *server, const void *origin, IpcIo *req, WriteInt32(reply, EC_INVALID); return EC_INVALID; } - pid_t pid = 0; - uid_t uid = 0; + pid_t pid = GetCallingPid(); + uid_t uid = GetCallingUid(); bool isFeature; ReadBool(req, &isFeature); - char *feature = NULL; - if (isFeature) { + if (!isFeature) { feature = (char *)ReadString(req, &len); } MUTEX_Lock(server->mtx); @@ -312,9 +325,13 @@ static int32 ProcPutFeature(SamgrServer *server, const void *origin, IpcIo *req, WriteInt32(reply, EC_INVALID); return EC_INVALID; } +#ifndef MINI_SAMGR_LITE_RPC + ReadUint32(req, &identity->token); +#else identity->token = ReadPointer(req); +#endif identity->handle = (int32_t)handle.handle; - + identity->cookie = handle.cookie; PolicyTrans *policy = NULL; RegParams regParams = {service, feature, handle.uid, handle.pid}; uint32 policyNum = 0; @@ -327,7 +344,6 @@ static int32 ProcPutFeature(SamgrServer *server, const void *origin, IpcIo *req, WriteInt32(reply, EC_PERMISSION); return EC_PERMISSION; } - ret = SASTORA_Save(&server->store, service, feature, identity); MUTEX_Unlock(server->mtx); HILOG_DEBUG(HILOG_MODULE_SAMGR, "Register Feature<%s, %s> pid<%d>, id<%d, %d> ret:%d", @@ -393,7 +409,7 @@ static int32 ProcGetFeature(SamgrServer *server, const void *origin, IpcIo *req, bool isFeature; ReadBool(req, &isFeature); char *feature = NULL; - if (isFeature) { + if (!isFeature) { feature = (char *)ReadString(req, &len); } MUTEX_Lock(server->mtx); @@ -404,7 +420,6 @@ static int32 ProcGetFeature(SamgrServer *server, const void *origin, IpcIo *req, service, feature, identity->handle, identity->token, EC_NOSERVICE); return EC_NOSERVICE; } - PidHandle providerPid = SASTORA_FindPidHandleByIpcHandle(&server->store, identity->handle); MUTEX_Unlock(server->mtx); if (providerPid.pid == INVALID_INDEX || providerPid.uid == INVALID_INDEX) { @@ -415,8 +430,8 @@ static int32 ProcGetFeature(SamgrServer *server, const void *origin, IpcIo *req, AuthParams authParams = { .providerService = service, .providerfeature = feature, - .consumerPid = 0, - .consumerUid = 0, + .consumerPid = GetCallingPid(), + .consumerUid = GetCallingUid(), .providerPid = providerPid.pid, .providerUid = providerPid.uid }; @@ -453,7 +468,9 @@ static int ProcFeature(SamgrServer *server, int32 option, void *origin, IpcIo *r ret = ProcGetFeature(server, origin, req, reply, &identity); WriteInt32(reply, ret); if (ret == EC_SUCCESS) { + // The WriteRemoteObject identity.token parameter is invalid WriteRemoteObject(reply, &identity); + WriteUint32(reply, (uint32_t)identity.token); } } return ret; @@ -558,7 +575,7 @@ void ProcGetAllSysCap(SamgrServer *server, IpcIo *req, IpcIo *reply) if (serviceImpl->isRegister == FALSE) { continue; } - WriteBool(reply, serviceImpl->name); + WriteString(reply, serviceImpl->name); cnt++; } MUTEX_Unlock(server->sysCapMtx); @@ -594,8 +611,15 @@ static int ProcSysCap(SamgrServer *server, int32 option, void *origin, IpcIo *re return EC_SUCCESS; } -static int RegisterSamgrEndpoint(SvcIdentity* identity) +static int RegisterSamgrEndpoint(SvcIdentity* identity, int token, const char *service, const char *feature) { +#ifndef MINI_SAMGR_LITE_RPC + int ret = SetContextObject(*identity); + if (ret != EC_SUCCESS) { + HILOG_FATAL(HILOG_MODULE_SAMGR, "Set sa manager<%d> failed!", ret); + return EC_INVALID; + } +#endif identity->handle = SAMGR_HANDLE; identity->token = SAMGR_TOKEN; identity->cookie = SAMGR_COOKIE; @@ -618,15 +642,15 @@ static void OnEndpointExit(void* argv) sleep(RETRY_INTERVAL); --retry; } -#ifdef __LINUX__ + PidHandle handle; int err = SASTORA_FindHandleByPid(&g_server.store, pid, &handle); if (err != INVALID_INDEX) { - BinderRelease(context, handle.handle); + SvcIdentity target = {INVALID_INDEX, INVALID_INDEX, INVALID_INDEX}; + target.handle = handle.handle; + ReleaseSvc(target); } -#endif HILOG_ERROR(HILOG_MODULE_SAMGR, "IPC pid<%d> exit! send clean request retry(%d), ret(%d)!", pid, retry, ret); - return EC_SUCCESS; } static IpcAuthInterface *GetIpcAuthInterface(void)