Binder 域

Posted xhBruce

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Binder 域相关的知识,希望对你有一定的参考价值。

多个 Binder 域

android12-release

Binder系列1-Binder Driver
Binder系列2-ServiceManager
Binder系列3-framework层


1. Binder域:binder,hwbinder,vndbinder

都是Binder IPC,查看 Binder系列1-Binder Driver binder_init() -> init_binder_device(device_name) 注册misc设备节点
对应设备节点:/dev/binder/dev/hwbinder/dev/vndbinder

drivers/android/binder.c
kernel/configs/android-base.config
char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
CONFIG_ANDROID_BINDER_DEVICES=binder,hwbinder,vndbinder

2. init.rc文件启动ServiceManager

上边是kernel底层misc设备节点添加

2.1 /dev/binder 框架/应用进程之间的 IPC,使用 AIDL 接口

Binder系列2-ServiceManager

frameworks/native/cmds/servicemanager/servicemanager.rc

  • 进程名:servicemanager
  • bin文件:/system/bin/servicemanager
  • 对应启动入口:frameworks/native/cmds/servicemanager/main.cpp

2.2 /dev/hwbinder 框架/供应商进程之间的 IPC,使用 HIDL 接口;供应商进程之间的 IPC,使用 HIDL 接口

system/hwservicemanager/hwservicemanager.rc

  • 进程名:hwservicemanager
  • bin文件:/system/bin/hwservicemanager
  • 对应启动入口:system/hwservicemanager/service.cpp

2.3 /dev/vndbinder 供应商/供应商进程之间的 IPC,使用 AIDL 接口

与/dev/binder是同一入口,通过rc文件参数区分const char* driver = argc == 2 ? argv[1] : "/dev/binder";
/dev/vndbinder是两个参数service vndservicemanager /vendor/bin/vndservicemanager /dev/vndbinder

frameworks/native/cmds/servicemanager/vndservicemanager.rc

  • 进程名:vndservicemanager
  • bin文件:/vendor/bin/vndservicemanager
  • 对应启动入口:frameworks/native/cmds/servicemanager/main.cpp

3. defaultServiceManager获取

  • /dev/binder获取方法defaultServiceManager(),如mediaserver.rc启动通过binder/IServiceManager.hdefaultServiceManager()(frameworks/native/libs/binder/IServiceManager.cpp)

frameworks/av/media/mediaserver/mediaserver.rc
frameworks/av/media/mediaserver/main_mediaserver.cpp
frameworks/av/media/libmediaplayerservice/MediaPlayerService.cpp

#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <binder/MemoryHeapBase.h>
#include <binder/MemoryBase.h>

void MediaPlayerService::instantiate() 
    defaultServiceManager()->addService(
            String16("media.player"), new MediaPlayerService());

  • /dev/hwbinder获取方法defaultServiceManager()(system/libhidl/transport/ServiceManagement.cpp)
sp<IServiceManager1_0> defaultServiceManager() 
    return defaultServiceManager1_2();

sp<IServiceManager1_1> defaultServiceManager1_1() 
    return defaultServiceManager1_2();

sp<IServiceManager1_2> defaultServiceManager1_2() 
    using android::hidl::manager::V1_2::BnHwServiceManager;
    using android::hidl::manager::V1_2::BpHwServiceManager;

    static std::mutex& gDefaultServiceManagerLock = *new std::mutex;
    static sp<IServiceManager1_2>& gDefaultServiceManager = *new sp<IServiceManager1_2>;

    
        std::lock_guard<std::mutex> _l(gDefaultServiceManagerLock);
        if (gDefaultServiceManager != nullptr) 
            return gDefaultServiceManager;
        

        if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) 
            // HwBinder not available on this device or not accessible to
            // this process.
            return nullptr;
        

        waitForHwServiceManager();

        while (gDefaultServiceManager == nullptr) 
            gDefaultServiceManager =
                fromBinder<IServiceManager1_2, BpHwServiceManager, BnHwServiceManager>(
                    ProcessState::self()->getContextObject(nullptr));
            if (gDefaultServiceManager == nullptr) 
                LOG(ERROR) << "Waited for hwservicemanager, but got nullptr.";
                sleep(1);
            
        
    

    return gDefaultServiceManager;

  • /dev/vndbinder获取方法defaultServiceManager(),从上面2.3查看与/dev/binder共用一套libbinder,这里通过ProcessState::self()ProcessState::initWithDriver("/dev/vndbinder")切换,默认kDefaultDriver = "/dev/binder";因此两者使用时,每次只能指定一个设备节点,不能同时使用。左图mediaserver.rc启动,右图CameraProvider进程启动(CameraProvider进程-Android12

以上是关于Binder 域的主要内容,如果未能解决你的问题,请参考以下文章

Android之Binder通信篇

将 android binder 的性能与 unix-socket 进行比较

Binder 机制Native 层 Binder 机制分析 ( binder_loop | svcmgr_handler | binder.c | binder_parse )

Binder 机制Native 层 Binder 机制分析 ( service_manager.c | 开启 Binder | 注册 Binder 进程上下文 | 开启 Binder 循环 )(代码片

Android Binder实现浅析-Binder驱动

Binder 机制分析 Android 内核源码中的 Binder 驱动源码 binder.c ( googlesource 中的 Android 内核源码 | 内核源码下载 )