From 19b30c06a9223084a3b443f54e8338a4f4837d5f Mon Sep 17 00:00:00 2001 From: crazy_hu Date: Sun, 27 Nov 2022 17:14:01 +0800 Subject: [PATCH] Add libipc_common Signed-off-by: crazy_hu --- BUILD.gn | 1 + interfaces/innerkits/ipc_core/BUILD.gn | 2 + interfaces/innerkits/ipc_single/BUILD.gn | 2 + ipc/native/src/core/BUILD.gn | 43 +++++++++++++++ .../src/core/include/ipc_process_skeleton.h | 1 - .../src/core/include/process_skeleton.h | 43 +++++++++++++++ .../src/core/source/ipc_process_skeleton.cpp | 26 ++++++--- .../src/core/source/process_skeleton.cpp | 53 +++++++++++++++++++ ipc/native/src/mock/source/binder_invoker.cpp | 1 - 9 files changed, 163 insertions(+), 9 deletions(-) create mode 100644 ipc/native/src/core/BUILD.gn create mode 100644 ipc/native/src/core/include/process_skeleton.h create mode 100644 ipc/native/src/core/source/process_skeleton.cpp diff --git a/BUILD.gn b/BUILD.gn index 86beb586..9fa6e552 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -45,6 +45,7 @@ group("ipc_components") { "//foundation/communication/ipc/interfaces/innerkits/ipc_core:ipc_core", "//foundation/communication/ipc/interfaces/innerkits/ipc_single:ipc_single", "//foundation/communication/ipc/interfaces/innerkits/libdbinder:libdbinder", + "//foundation/communication/ipc/ipc/native/src/core:ipc_common", ] } else { deps = [ "//foundation/communication/ipc/interfaces/innerkits/c:rpc" ] diff --git a/interfaces/innerkits/ipc_core/BUILD.gn b/interfaces/innerkits/ipc_core/BUILD.gn index cb350838..910befb6 100644 --- a/interfaces/innerkits/ipc_core/BUILD.gn +++ b/interfaces/innerkits/ipc_core/BUILD.gn @@ -70,6 +70,8 @@ ohos_shared_library("ipc_core") { all_dependent_configs = [ ":ipc_all_deps_config" ] + deps = [ "$IPC_CORE_ROOT/src/core:ipc_common" ] + external_deps = [ "access_token:libaccesstoken_sdk", "c_utils:utils", diff --git a/interfaces/innerkits/ipc_single/BUILD.gn b/interfaces/innerkits/ipc_single/BUILD.gn index de33cdbf..5c224b64 100644 --- a/interfaces/innerkits/ipc_single/BUILD.gn +++ b/interfaces/innerkits/ipc_single/BUILD.gn @@ -57,6 +57,8 @@ ohos_shared_library("ipc_single") { ] public_configs = [ "$SUBSYSTEM_DIR:ipc_util_config" ] + deps = [ "$IPC_CORE_ROOT/src/core:ipc_common" ] + external_deps = [ "c_utils:utils", "hitrace_native:libhitracechain", diff --git a/ipc/native/src/core/BUILD.gn b/ipc/native/src/core/BUILD.gn new file mode 100644 index 00000000..fa539eb6 --- /dev/null +++ b/ipc/native/src/core/BUILD.gn @@ -0,0 +1,43 @@ +# 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. + +import("//build/ohos.gni") + +SUBSYSTEM_DIR = "//foundation/communication/ipc" + +config("libipc_common_private_config") { + cflags_cc = [ "-O2" ] +} + +ohos_shared_library("ipc_common") { + include_dirs = [ + "$SUBSYSTEM_DIR/utils/include", + "include", + ] + sources = [ "source/process_skeleton.cpp" ] + + configs = [ + "$SUBSYSTEM_DIR:ipc_util_config", + ":libipc_common_private_config", + ] + + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + ] + + install_images = [ system_base_dir ] + relative_install_dir = "chipset-pub-sdk" + subsystem_name = "communication" + part_name = "ipc" +} diff --git a/ipc/native/src/core/include/ipc_process_skeleton.h b/ipc/native/src/core/include/ipc_process_skeleton.h index 3b0f1d78..bb26e7ee 100644 --- a/ipc/native/src/core/include/ipc_process_skeleton.h +++ b/ipc/native/src/core/include/ipc_process_skeleton.h @@ -203,7 +203,6 @@ private: std::map isContainStub_; std::map> rawData_; IPCWorkThreadPool *threadPool_ = nullptr; - sptr registryObject_ = nullptr; #ifndef CONFIG_IPC_SINGLE std::mutex databusProcMutex_; diff --git a/ipc/native/src/core/include/process_skeleton.h b/ipc/native/src/core/include/process_skeleton.h new file mode 100644 index 00000000..a0a8bed0 --- /dev/null +++ b/ipc/native/src/core/include/process_skeleton.h @@ -0,0 +1,43 @@ +/* + * 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 OHOS_IPC_PROCESS_SKELETON_H +#define OHOS_IPC_PROCESS_SKELETON_H + +#include +#include + +#include "iremote_object.h" + +namespace OHOS { + +class ProcessSkeleton : public virtual RefBase { +public: + + static sptr GetInstance(); + sptr GetRegistryObject(); + void SetRegistryObject(sptr &object); + +private: + DISALLOW_COPY_AND_MOVE(ProcessSkeleton); + ProcessSkeleton() = default; + ~ProcessSkeleton() = default; + + static sptr instance_; + static std::mutex mutex_; + sptr registryObject_ = nullptr; +}; +} // namespace OHOS +#endif // OHOS_IPC_PROCESS_SKELETON_H diff --git a/ipc/native/src/core/source/ipc_process_skeleton.cpp b/ipc/native/src/core/source/ipc_process_skeleton.cpp index 894b725e..e451cd88 100644 --- a/ipc/native/src/core/source/ipc_process_skeleton.cpp +++ b/ipc/native/src/core/source/ipc_process_skeleton.cpp @@ -24,6 +24,7 @@ #include "ipc_types.h" #include "ipc_thread_skeleton.h" +#include "process_skeleton.h" #include "sys_binder.h" #include "log_tags.h" @@ -113,11 +114,19 @@ IPCProcessSkeleton::~IPCProcessSkeleton() sptr IPCProcessSkeleton::GetRegistryObject() { - if (registryObject_ == nullptr) { - registryObject_ = FindOrNewObject(REGISTRY_HANDLE); + auto current = ProcessSkeleton::GetInstance(); + if (current == nullptr) { + ZLOGE(LOG_LABEL, "get process skeleton failed"); + return nullptr; } - - return registryObject_; + sptr object = current->GetRegistryObject(); + if (object == nullptr) { + object = FindOrNewObject(REGISTRY_HANDLE); + if (object != nullptr) { + current->SetRegistryObject(object); + } + } + return object; } std::u16string IPCProcessSkeleton::MakeHandleDescriptor(int handle) @@ -201,16 +210,19 @@ bool IPCProcessSkeleton::SetRegistryObject(sptr &object) ZLOGE(LOG_LABEL, "object is null"); return false; } - + auto current = ProcessSkeleton::GetInstance(); + if (current == nullptr) { + ZLOGE(LOG_LABEL, "get process skeleton failed"); + return false; + } IRemoteInvoker *invoker = IPCThreadSkeleton::GetRemoteInvoker(IRemoteObject::IF_PROT_DEFAULT); if (invoker == nullptr) { ZLOGE(LOG_LABEL, "fail to get invoker"); return false; } - bool ret = invoker->SetRegistryObject(object); if (ret) { - registryObject_ = object; + current->SetRegistryObject(object); } ZLOGI(LOG_LABEL, "%{public}s set registry result is %{public}d", __func__, ret); return ret; diff --git a/ipc/native/src/core/source/process_skeleton.cpp b/ipc/native/src/core/source/process_skeleton.cpp new file mode 100644 index 00000000..70503d02 --- /dev/null +++ b/ipc/native/src/core/source/process_skeleton.cpp @@ -0,0 +1,53 @@ +/* + * 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 "process_skeleton.h" + +#include "log_tags.h" +#include "ipc_debug.h" + +namespace OHOS { +static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC, "ProcessSkeleton" }; + +sptr ProcessSkeleton::instance_ = nullptr; +std::mutex ProcessSkeleton::mutex_; + +sptr ProcessSkeleton::GetInstance() +{ + if (instance_ == nullptr) { + std::lock_guard lockGuard(mutex_); + if (instance_ == nullptr) { + instance_ = new (std::nothrow) ProcessSkeleton(); + if (instance_ == nullptr) { + ZLOGE(LOG_LABEL, "create ProcessSkeleton object failed"); + return nullptr; + } + } + } + return instance_; +} + +sptr ProcessSkeleton::GetRegistryObject() +{ + std::lock_guard lockGuard(mutex_); + return registryObject_; +} + +void ProcessSkeleton::SetRegistryObject(sptr &object) +{ + std::lock_guard lockGuard(mutex_); + registryObject_ = object; +} +} // namespace OHOS \ No newline at end of file diff --git a/ipc/native/src/mock/source/binder_invoker.cpp b/ipc/native/src/mock/source/binder_invoker.cpp index a362fcac..766a8202 100644 --- a/ipc/native/src/mock/source/binder_invoker.cpp +++ b/ipc/native/src/mock/source/binder_invoker.cpp @@ -487,7 +487,6 @@ void BinderInvoker::OnTransaction(const uint8_t *buffer) auto targetObject = IPCProcessSkeleton::GetCurrent()->GetRegistryObject(); if (targetObject == nullptr) { ZLOGE(LABEL, "Invalid samgr stub object"); - abort(); } else { error = targetObject->SendRequest(tr->code, *data, reply, option); }