AndroidO Treble架构下hwservicemanager启动过程

hwservicemanager是hidl服务管理中心,负责管理系统中的所有hidl服务,由init进程启动。

  1. service hwservicemanager /system/bin/hwservicemanager  
  2.     user system  
  3.     disabled  
  4.     group system readproc  
  5.     critical  
  6.     onrestart setprop hwservicemanager.ready false  
  7.     onrestart class_restart hal  
  8.     onrestart class_restart early_hal  
  9.     writepid /dev/cpuset/system-background/tasks  
  10.     class animation  
  11.     shutdown critical  
service hwservicemanager /system/bin/hwservicemanager
    user system
    disabled
    group system readproc
    critical
    onrestart setprop hwservicemanager.ready false
    onrestart class_restart hal
    onrestart class_restart early_hal
    writepid /dev/cpuset/system-background/tasks
    class animation
    shutdown critical

hwservicemanager的源码位于:system/hwservicemanager

C:\Source\androido\system\hwservicemanager\service.cpp

  1. int main() {  
  2.     configureRpcThreadpool(1, true /* callerWillJoin */);  
  3.     //创建ServiceManager对象  
  4.     ServiceManager *manager = new ServiceManager();  
  5.     //将ServiceManager对象自身注册到mServiceMap表中  
  6.     if (!manager->add(serviceName, manager)) {  
  7.         ALOGE(”Failed to register hwservicemanager with itself.”);  
  8.     }  
  9.     //创建TokenManager对象  
  10.     TokenManager *tokenManager = new TokenManager();  
  11.     //将TokenManager对象自身注册到mServiceMap表中  
  12.     if (!manager->add(serviceName, tokenManager)) {  
  13.         ALOGE(”Failed to register ITokenManager with hwservicemanager.”);  
  14.     }  
  15.     //建立消息循环  
  16.     sp<Looper> looper(Looper::prepare(0 /* opts */));  
  17.   
  18.     int binder_fd = -1;  
  19.     //将主线程加入binder线程池,并得到/dev/hwbinder句柄  
  20.     IPCThreadState::self()->setupPolling(&binder_fd);  
  21.     if (binder_fd < 0) {  
  22.         ALOGE(”Failed to aquire binder FD. Aborting…”);  
  23.         return -1;  
  24.     }  
  25.     // Flush after setupPolling(), to make sure the binder driver  
  26.     // knows about this thread handling commands.  
  27.     IPCThreadState::self()->flushCommands();  
  28.     //主线程监听EVENT_INPUT,通过回调BinderCallback处理  
  29.     sp<BinderCallback> cb(new BinderCallback);  
  30.     if (looper->addFd(binder_fd, Looper::POLL_CALLBACK, Looper::EVENT_INPUT, cb,  
  31.             nullptr) != 1) {  
  32.         ALOGE(”Failed to add hwbinder FD to Looper. Aborting…”);  
  33.         return -1;  
  34.     }  
  35.     //创建BnHwServiceManager对象  
  36.     // Tell IPCThreadState we’re the service manager  
  37.     sp<BnHwServiceManager> service = new BnHwServiceManager(manager);  
  38.     IPCThreadState::self()->setTheContextObject(service);  
  39.     // Then tell binder kernel  
  40.     ioctl(binder_fd, BINDER_SET_CONTEXT_MGR, 0);  
  41.     // Only enable FIFO inheritance for hwbinder  
  42.     // FIXME: remove define when in the kernel  
  43. #define BINDER_SET_INHERIT_FIFO_PRIO    _IO(‘b’, 10)  
  44.   
  45.     int rc = ioctl(binder_fd, BINDER_SET_INHERIT_FIFO_PRIO);  
  46.     if (rc) {  
  47.         ALOGE(”BINDER_SET_INHERIT_FIFO_PRIO failed with error %d\n”, rc);  
  48.     }  
  49.     //通过属性方式告知其他进程,hwservicemanager已经就绪  
  50.     rc = property_set(”hwservicemanager.ready”“true”);  
  51.     if (rc) {  
  52.         ALOGE(”Failed to set \”hwservicemanager.ready\” (error %d). ”\  
  53.               ”HAL services will not start!\n”, rc);  
  54.     }  
  55.     //进入消息循环  
  56.     while (true) {  
  57.         looper->pollAll(-1 /* timeoutMillis */);  
  58.     }  
  59.   
  60.     return 0;  
  61. }  
int main() {
    configureRpcThreadpool(1, true /* callerWillJoin */);
    //创建ServiceManager对象
    ServiceManager *manager = new ServiceManager();
    //将ServiceManager对象自身注册到mServiceMap表中
    if (!manager->add(serviceName, manager)) {
        ALOGE("Failed to register hwservicemanager with itself.");
    }
    //创建TokenManager对象
    TokenManager *tokenManager = new TokenManager();
    //将TokenManager对象自身注册到mServiceMap表中
    if (!manager->add(serviceName, tokenManager)) {
        ALOGE("Failed to register ITokenManager with hwservicemanager.");
    }
    //建立消息循环
    sp<Looper> looper(Looper::prepare(0 /* opts */));

    int binder_fd = -1;
    //将主线程加入binder线程池,并得到/dev/hwbinder句柄
    IPCThreadState::self()->setupPolling(&binder_fd);
    if (binder_fd < 0) {
        ALOGE("Failed to aquire binder FD. Aborting...");
        return -1;
    }
    // Flush after setupPolling(), to make sure the binder driver
    // knows about this thread handling commands.
    IPCThreadState::self()->flushCommands();
    //主线程监听EVENT_INPUT,通过回调BinderCallback处理
    sp<BinderCallback> cb(new BinderCallback);
    if (looper->addFd(binder_fd, Looper::POLL_CALLBACK, Looper::EVENT_INPUT, cb,
            nullptr) != 1) {
        ALOGE("Failed to add hwbinder FD to Looper. Aborting...");
        return -1;
    }
    //创建BnHwServiceManager对象
    // Tell IPCThreadState we're the service manager
    sp<BnHwServiceManager> service = new BnHwServiceManager(manager);
    IPCThreadState::self()->setTheContextObject(service);
    // Then tell binder kernel
    ioctl(binder_fd, BINDER_SET_CONTEXT_MGR, 0);
    // Only enable FIFO inheritance for hwbinder
    // FIXME: remove define when in the kernel




猜你喜欢

转载自blog.csdn.net/marshal_zsx/article/details/80293163