hwservicemanager是hidl服务管理中心,负责管理系统中的所有hidl服务,由init进程启动。
- 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
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
- 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
- #define BINDER_SET_INHERIT_FIFO_PRIO _IO(‘b’, 10)
- int rc = ioctl(binder_fd, BINDER_SET_INHERIT_FIFO_PRIO);
- if (rc) {
- ALOGE(”BINDER_SET_INHERIT_FIFO_PRIO failed with error %d\n”, rc);
- }
- //通过属性方式告知其他进程,hwservicemanager已经就绪
- rc = property_set(”hwservicemanager.ready”, “true”);
- if (rc) {
- ALOGE(”Failed to set \”hwservicemanager.ready\” (error %d). ”\
- ”HAL services will not start!\n”, rc);
- }
- //进入消息循环
- while (true) {
- looper->pollAll(-1 /* timeoutMillis */);
- }
- return 0;
- }
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