OHOS標(biāo)準(zhǔn)系統(tǒng)的IPC和RPC代碼解讀(一)
??想了解更多關(guān)于開源的內(nèi)容,請訪問:??
本想接著前文《OHOS標(biāo)準(zhǔn)系統(tǒng)的SAMGR代碼解讀》繼續(xù)分析dmsfwk組件的實現(xiàn)細節(jié),但發(fā)現(xiàn)涉及太多的IPC/RPC的內(nèi)容了,如果對OHOS的IPC/RPC沒有足夠的理解,很難把dmsfwk組件理解透徹,因此我花了點時間,先整理了一下IPC/RPC相關(guān)的代碼和部分流程,作為理解SAMGR相關(guān)組件的預(yù)備知識。
IPC/RPC的代碼倉庫,在OHOS 3.1分支上,分為ipc和ipc_lite兩個倉庫,在master分支上,ipc_lite倉庫已經(jīng)合并到ipc倉庫中,因此本文在整理ipc倉庫代碼目錄樹結(jié)構(gòu)時,是以當(dāng)前的master分支代碼為準(zhǔn)整理的。
但由于我當(dāng)前工作主要還是在OHOS 3.1分支上,所以在后面整理相關(guān)的流程圖或數(shù)據(jù)結(jié)構(gòu)關(guān)系圖時,還是基于3.1分支代碼進行的(待930版本發(fā)布后再確認(rèn)是否更新到930版本)。
1、整理代碼目錄樹結(jié)構(gòu)(基于master分支)
在Linux命令行下進入 //foundation/communication/ipc/ 目錄,通過tree命令將目錄樹結(jié)構(gòu)打印出來并重定向到文本文件中。為減少干擾,直接去除test相關(guān)的目錄和文件,再酌情微調(diào)一下部分目錄和文件的位置。重點閱讀剩下的幾個BUILD.gn文件,結(jié)合幾類系統(tǒng)分別整理BUILD.gn中的編譯目標(biāo),盡可能地挖掘有用信息(后繼在深入理解代碼時也可以對該目錄樹結(jié)構(gòu)補充新的信息)。
目前我整理出來的信息大概如下:
# 系統(tǒng)類型:Lite類型系統(tǒng)(a-LiteM、b-LiteA、c-LiteL)、STD系統(tǒng)
# Lite類型系統(tǒng)的編譯目標(biāo):
#【0】shared_library("rpc_log"),LiteM系統(tǒng)該文件直接編譯進2.1中。
# 【Lite類型系統(tǒng):LOG_DOMAIN 0xD001518,LOG_TAG "IPCRPC"】
# 【 STD類型系統(tǒng):ipc_debug.h、log_tags.h】
#【1】靜態(tài)或動態(tài)library("rpc_adapter")【簡單:RpcGetPid()/RpcGetUid()的適配】
# 【1.1】static_library("rpc_adapter"),LiteM系統(tǒng)依賴該庫
# 【1.2】shared_library("rpc_adapter"),LiteA+LiteL小型系統(tǒng)依賴該庫
#【2】靜態(tài)或動態(tài)library("rpc_manager")
# 【2.1】static_library("rpc_manager"),LiteM系統(tǒng)依賴該庫
# 【2.2】shared_library("rpc_manager"),LiteA編譯該庫為空實際不依賴,LiteL小型系統(tǒng)依賴該庫
#【3】靜態(tài)或動態(tài)library("dbinder")
# 【3.1】static_library("dbinder"),LiteM系統(tǒng)依賴該庫
# 【3.2】shared_library("dbinder"),LiteA小型系統(tǒng)不依賴該庫,LiteL小型系統(tǒng)依賴該庫
#【4】shared_library("ipc_single"),LiteM系統(tǒng)不依賴該庫,LiteA和LiteL小型系統(tǒng)依賴該庫
# STD系統(tǒng)的編譯目標(biāo):
#【10】ohos_shared_library("ipc_core") 【包含本地設(shè)備內(nèi)的IPC,以及與dbinder相關(guān)的基礎(chǔ)功能】
#【11】ohos_shared_library("ipc_single")【有定義CONFIG_IPC_SINGLE,完全是本地設(shè)備內(nèi)的IPC】
#【12】ohos_shared_library("libdbinder")【dbinder,與RPC相關(guān)的功能實現(xiàn)】
#【13】ohos_shared_library("rpc") 【rpc_js接口】
# 依賴關(guān)系:
#LiteM:依賴【 1.1+2.1+3.1 】(0-直接編譯到2.1中)
#LiteA:依賴【0+1.2+ 4】(2-編譯為空實際不依賴,3-不依賴)
#LiteL:依賴【0+1.2+2.2+3.2+4】
#STD :依賴【10+11+12】+【13】
# 目錄樹結(jié)構(gòu)
//foundation/communication/ipc/
├── bundle.json
├── BUILD.gn #定義ipc_components:STD系統(tǒng)依賴【10+11+12】,Lite系統(tǒng)依賴【interfaces/innerkits/c:rpc】
├── interfaces #接口部分,整理編譯目標(biāo)和依賴關(guān)系
│ ├── innerkits
│ │ │ # Lite類型系統(tǒng)組件
│ │ ├── c
│ │ │ ├── BUILD.gn #Lite系統(tǒng)專用的組件【lite_component("rpc")】
│ │ │ │ #LiteM:依賴【 1.1+2.1+3.1 】(0-直接編譯到2.1中)
│ │ │ │ #LiteA:依賴【0+1.2+ 4】(2-編譯為空實際不依賴,3-不依賴)
│ │ │ │ #LiteL:依賴【0+1.2+2.2+3.2+4】
│ │ │ ├── dbinder #distributed binder for Lite
│ │ │ │ ├── BUILD.gn #【3.1】和【3.2】
│ │ │ │ └── include/dbinder_service.h
│ │ │ └── ipc
│ │ │ ├── BUILD.gn #【2.1】和【2.2】
│ │ │ └── include
│ │ │ ├── ipc_skeleton.h
│ │ │ ├── rpc_errno.h
│ │ │ └── serializer.h
│ │ │
│ │ │ # STD系統(tǒng)專用的組件【10】【11】【12】【13】
│ │ ├── ipc_core
│ │ │ ├── BUILD.gn #【10】ohos_shared_library("ipc_core")
│ │ │ └── include
│ │ │ ├── ipc_file_descriptor.h
│ │ │ ├── ipc_object_proxy.h
│ │ │ ├── ipc_object_stub.h
│ │ │ ├── ipc_skeleton.h
│ │ │ ├── ipc_types.h
│ │ │ ├── iremote_broker.h
│ │ │ ├── iremote_object.h
│ │ │ ├── iremote_proxy.h
│ │ │ ├── iremote_stub.h
│ │ │ ├── jni_help.h
│ │ │ ├── message_option.h
│ │ │ ├── message_parcel.h
│ │ │ └── peer_holder.h
│ │ ├── ipc_single/BUILD.gn #【11】ohos_shared_library("ipc_single") + CONFIG_IPC_SINGLE
│ │ └── libdbinder # distributed binder for STD
│ │ ├── BUILD.gn #【12】ohos_shared_library("libdbinder")依賴【10】
│ │ └── include
│ │ ├── dbinder_service.h
│ │ ├── dbinder_service_stub.h
│ │ └── rpc_system_ability_callback.h
│ └── kits
│ ├── bundle.json # RPC napi for js
│ └── js/napi/BUILD.gn #【13】ohos_shared_library("rpc") 依賴【10】
│
│ #工具部分【Lite類型系統(tǒng)在用,STD系統(tǒng)不用】
├── utils
│ ├── include
│ │ ├── log_tags.h # LOG_ID_COMMUNICATION 以及子模塊的LOG_ID定義
│ │ └── rpc_session_handle.h
│ └── src/rpc_session_handle.c #【a+c】
│
│ # IPC/RPC的框架實現(xiàn)代碼
├── ipc
│ └── native
│ │ #Lite類型系統(tǒng)的IPC框架代碼
│ ├── c
│ │ ├── adapter
│ │ │ ├── access_token
│ │ │ │ ├── include/access_token_adapter.h
│ │ │ │ └── src/access_token_adapter.c #【STD】【只有STD系統(tǒng)的[10][11]編譯該文件,Lite類型系統(tǒng)不編譯】
│ │ │ ├── BUILD.gn #【1】單獨編譯 rpc_os_adapter.c 源文件
│ │ │ ├── include
│ │ │ │ ├── rpc_bytrace.h
│ │ │ │ └── rpc_os_adapter.h
│ │ │ ├── Linux/rpc_os_adapter.c #【1.2】【b+c】
│ │ │ └── Liteos_m/rpc_os_adapter.c #【1.1】【a】
│ │ ├── ipc
│ │ │ ├── include
│ │ │ │ ├── ipc_invoker.h
│ │ │ │ └── ipc_types.h
│ │ │ └── src
│ │ │ ├── linux
│ │ │ │ ├── include/sys_binder.h
│ │ │ │ ├── ipc_invoker.c #【c】
│ │ │ │ └── serializer_inner.c #【c】
│ │ │ ├── liteos_a
│ │ │ │ ├── include/lite_ipc.h
│ │ │ │ ├── ipc_invoker.c #【b】
│ │ │ │ └── serializer_inner.c #【b】
│ │ │ └── liteos_m
│ │ │ ├── ipc_invoker.c #【a】
│ │ │ └── serializer_inner.c #【a】
│ │ ├── manager
│ │ │ ├── include
│ │ │ │ ├── ipc_process_skeleton.h
│ │ │ │ ├── ipc_skeleton_pri.h
│ │ │ │ ├── ipc_thread_pool.h
│ │ │ │ ├── iremote_invoker.h
│ │ │ │ ├── rpc_log.h
│ │ │ │ ├── rpc_types.h
│ │ │ │ └── serializer_inner.h
│ │ │ └── src
│ │ │ ├── ipc_process_skeleton.c #【a+b+c】公共部分5個文件
│ │ │ ├── ipc_skeleton.c #【a+b+c】公共部分5個文件
│ │ │ ├── ipc_thread_pool.c #【a+b+c】公共部分5個文件
│ │ │ ├── iremote_invoker.c #【a+b+c】公共部分5個文件
│ │ │ ├── serializer.c #【a+b+c】公共部分5個文件
│ │ │ └── rpc_log.c #【a】編譯在【2.1】,【b+c】LiteA+LiteL單獨編譯出【0】
│ │ └── rpc
│ │ ├── include
│ │ │ ├── dbinder_invoker.h
│ │ │ ├── rpc_feature_set.h
│ │ │ ├── rpc_process_skeleton.h
│ │ │ └── rpc_trans_callback.h
│ │ ├── ipc_adapter
│ │ │ ├── include
│ │ │ │ ├── ipc_proxy_inner.h
│ │ │ │ └── ipc_stub_inner.h
│ │ │ ├── mini
│ │ │ │ ├── ipc_proxy_inner.c #【a】
│ │ │ │ └── ipc_stub_inner.c #【a】
│ │ │ └── small
│ │ │ ├── ipc_proxy_inner.c #【c】
│ │ │ └── ipc_stub_inner.c #【c】
│ │ ├── src
│ │ │ ├── dbinder_invoker.c #【a+c】
│ │ │ ├── rpc_feature_set.c #【STD】【只有STD系統(tǒng)的[10]編譯該文件,Lite類型系統(tǒng)不編譯】
│ │ │ ├── rpc_process_skeleton.c #【a+c】
│ │ │ ├── rpc_process_skeleton_virtual.c #【b+c】
│ │ │ └── rpc_trans_callback.c #【a+c】
│ │ └── trans_adapter
│ │ ├── include
│ │ │ ├── rpc_softbus_trans.h
│ │ │ └── rpc_trans.h
│ │ └── src
./ipc/test/rpc/socket_trans/src/rpc_socket_trans.c #【c】因為enable_socket_trans[true]用socket而不用softbus
│ │ ├── rpc_softbus_trans.c #【a】如果enable_socket_trans[false],則這里【+c】用softbus而不用socket
│ │ └── rpc_trans.c #【a+c】
│ │
│ │ # STD系統(tǒng)的IPC框架代碼
│ └── src
│ ├── core
│ │ ├── include
│ │ │ ├── buffer_object.h
│ │ │ ├── comm_auth_info.h
│ │ │ ├── databus_session_callback.h
│ │ │ ├── dbinder_callback_stub.h
│ │ │ ├── dbinder_error_code.h
│ │ │ ├── dbinder_session_object.h
│ │ │ ├── ipc_debug.h
│ │ │ ├── ipc_process_skeleton.h
│ │ │ ├── ipc_thread_pool.h
│ │ │ ├── ipc_thread_skeleton.h
│ │ │ ├── ipc_workthread.h
│ │ │ └── stub_refcount_object.h
│ │ └── source
│ │ ├── buffer_object.cpp
│ │ ├── comm_auth_info.cpp
│ │ ├── databus_session_callback.cpp
│ │ ├── dbinder_callback_stub.cpp
│ │ ├── dbinder_session_object.cpp
│ │ ├── ipc_file_descriptor.cpp
│ │ ├── ipc_object_proxy.cpp
│ │ ├── ipc_object_stub.cpp
│ │ ├── ipc_process_skeleton.cpp
│ │ ├── ipc_skeleton.cpp
│ │ ├── ipc_thread_pool.cpp
│ │ ├── ipc_thread_skeleton.cpp
│ │ ├── ipc_workthread.cpp
│ │ ├── iremote_broker.cpp
│ │ ├── iremote_object.cpp
│ │ ├── message_option.cpp
│ │ ├── message_parcel.cpp
│ │ ├── peer_holder.cpp
│ │ └── stub_refcount_object.cpp
│ ├── mock
│ │ ├── include
│ │ │ ├── binder_connector.h
│ │ │ ├── binder_debug.h
│ │ │ ├── binder_invoker.h
│ │ │ ├── dbinder_base_invoker.h
│ │ │ ├── dbinder_databus_invoker.h
│ │ │ ├── hitrace_invoker.h
│ │ │ ├── invoker_factory.h
│ │ │ ├── invoker_rawdata.h
│ │ │ ├── iremote_invoker.h
│ │ │ └── sys_binder.h
│ │ └── source
│ │ ├── binder_connector.cpp
│ │ ├── binder_debug.cpp
│ │ ├── binder_invoker.cpp
│ │ ├── hitrace_invoker.cpp
│ │ ├── invoker_factory.cpp
│ │ ├── nvoker_rawdata.cpp
│ │ └── idbinder_databus_invoker.cpp
│ ├── jni
│ │ ├── include
│ │ │ ├── jni_helper.h
│ │ │ ├── jni_remote_object.h
│ │ │ ├── ohos_rpc_message_option.h
│ │ │ ├── ohos_rpc_message_parcel.h
│ │ │ └── ohos_rpc_remote_object.h
│ │ └── source
│ │ ├── jni_helper.cpp
│ │ ├── ohos_rpc_message_option.cpp
│ │ ├── ohos_rpc_message_parcel.cpp
│ │ └── ohos_rpc_remote_object.cpp
│ └── napi
│ ├── include
│ │ ├── napi_ashmem.h
│ │ ├── napi_message_option.h
│ │ ├── napi_message_parcel.h
│ │ └── napi_remote_object.h
│ └── src
│ ├── napi_ashmem.cpp
│ ├── napi_message_option.cpp
│ ├── napi_message_parcel.cpp
│ ├── napi_remote_object.cpp
│ └── napi_rpc_native_module.cpp
│
│ # dbinder服務(wù)的實現(xiàn)代碼【又分Lite類型系統(tǒng)和STD系統(tǒng)】
└── services
└── dbinder
│ # Lite類型系統(tǒng)的dbinder服務(wù)的實現(xiàn)代碼【LiteA系統(tǒng)不走dbinder】
├── c
│ ├── include
│ │ ├── dbinder_service_inner.h
│ │ ├── dbinder_stub.h
│ │ ├── dbinder_trans_callback.h
│ │ └── dbinder_types.h
│ ├── ipc_adapter
│ │ ├── include/dbinder_ipc_adapter.h
│ │ ├── mini/dbinder_ipc_adapter.c #【a】
│ │ └── small/dbinder_ipc_adapter.c #【c】
│ └── src
│ ├── dbinder_service.c #【a+c】
│ ├── dbinder_stub.c #【a+c】
│ └── dbinder_trans_callback.c #【a+c】
│
│ # STD系統(tǒng)的dbinder服務(wù)的實現(xiàn)代碼
└── dbinder_service #【12】ohos_shared_library("libdbinder")
├── include
│ ├── dbinder_death_recipient.h
│ ├── dbinder_log.h
│ ├── dbinder_remote_listener.h
│ └── dbinder_sa_death_recipient.h
└── src
├── dbinder_death_recipient.cpp
├── dbinder_sa_death_recipient.cpp
├── dbinder_service.cpp
├── dbinder_service_stub.cpp
└── socket/dbinder_remote_listener.cpp
從上面的整理可以看出,Lite類型系統(tǒng)和STD系統(tǒng),各有一套自己的IPC/RPC的實現(xiàn)代碼。因為當(dāng)前目標(biāo)是要研究標(biāo)準(zhǔn)系統(tǒng)的dmsfwk組件(和samgr等其他組件),所以我們當(dāng)前的重點是上文中的編譯目標(biāo)【10】【11】【12】。
通過對比【10-ipc_core】和【11-ipc_single】兩個編譯目標(biāo)的BUILD.gn文件,可以認(rèn)為【11】是【10】的子集,因為:
- 目標(biāo)【10】沒有定義CONFIG_IPC_SINGLE,目標(biāo)【11】是有定義的。在部分共用的源代碼文件中,有使用 #ifndef CONFIG_IPC_SINGLE 控制的代碼,是目標(biāo)【10】專用的。
- 目標(biāo)【10】比目標(biāo)【11】多編譯幾個文件,是與DBinder、DataBus相關(guān)的,用于RPC。
目前還沒有非常確定【10】【11】各自的應(yīng)用場景有多大的差別,但我的理解偏向于Stub/Server端多依賴【10】,而Proxy/Client端多依賴【11】,或者分布式SA多依賴【10】,普通SA多依賴【11】(有待后面有更多的理解后確認(rèn))。
不管怎樣,【10】和【11】兩個目標(biāo)的代碼是共用的,都屬于IPC范疇,可以一并閱讀理解。【12】則屬于RPC范圍,再單獨閱讀理解。至于Lite類型系統(tǒng)的編譯目標(biāo),待以后有空研究的時候再補充相關(guān)總結(jié)。
2、基于Binder驅(qū)動的IPC(基于3.1 Release分支)
ipc組件根目錄下的README.md文件中有提到:
IPC(Inter-Process Communication)與RPC(Remote Procedure Call)機制用于實現(xiàn)跨進程通信,不同的是前者使用Binder驅(qū)動,用于設(shè)備內(nèi)的跨進程通信,而后者使用軟總線驅(qū)動,用于跨設(shè)備跨進程通信。IPC和RPC通常采用客戶端-服務(wù)器(Client-Server)模型,服務(wù)請求方(Client)可獲取提供服務(wù)提供方(Server)的代理 (Proxy),并通過此代理讀寫數(shù)據(jù)來實現(xiàn)進程間的數(shù)據(jù)通信。通常,系統(tǒng)能力(System Ability)Server側(cè)會先注冊到系統(tǒng)能力管理者(System Ability Manager,縮寫SAMgr)中,SAMgr負(fù)責(zé)管理這些SA并向Client提供相關(guān)的接口。Client要和某個具體的SA通信,必須先從SAMgr中獲取該SA的代理,然后使用代理和SA通信。
熟悉Android系統(tǒng)或者做過OHOS系統(tǒng)移植的小伙伴,即使沒有非常深入理解過Binder,但估計也會經(jīng)常聽到基于Binder驅(qū)動的IPC機制。
內(nèi)核態(tài)的Binder驅(qū)動實現(xiàn)代碼,在 //kernel/linux/linux-5.10/drivers/android/ 目錄下。在編譯Linux內(nèi)核時,通過 //kernel/linux/config/linux-5.10/arch/arm(arm64)/configs/ 目錄下的 xxxx_defconfig 文件中的如下配置,將Binder驅(qū)動模塊編譯進內(nèi)核:
#
# Android
#
CONFIG_ANDROID=y
CONFIG_ANDROID_BINDER_IPC=y
CONFIG_ANDROID_BINDER_DEVICES="binder,hwbinder,vndbinder"
# CONFIG_ANDROID_BINDER_IPC_SELFTEST is not set
# CONFIG_DAX is not set
CONFIG_NVMEM=y
Binder驅(qū)動在內(nèi)核向用戶態(tài)進程提供服務(wù);用戶態(tài)進程通過 open(“/dev/binder”) 和 ioctl() 來使用Binder服務(wù)實現(xiàn)IPC。
這中間其實要經(jīng)歷一些非常復(fù)雜的過程,建議小伙伴們自行搜索和理解Binder在內(nèi)核中實現(xiàn)IPC的原理和細節(jié),我現(xiàn)在還無法保證未來會做這方面的總結(jié)。
但本系列文章的下一篇,將會以全景圖的形式展示和講解OHOS的用戶態(tài)IPC框架的實現(xiàn)流程和細節(jié)。
3、基于DBinder驅(qū)動的RPC(待定)
DBinder應(yīng)該是Distributed Binder的縮寫。
DBinder將會涉及軟總線的一些實現(xiàn)細節(jié),我先看看能理解得多深入,待有所理解后再寫總結(jié)。