7

In the IServiceManager.cpp file, I see the BnServiceManager::onTransact function definition.

Inside that function, there is a call to "addService", "listServices" etc.

I couldn't find the implementation for those functions (which are declared under IServiceManager).

Can somebody please tell me, where to find the implementation of BnServiceManager.

Onik
  • 19,396
  • 14
  • 68
  • 91
Chakkra
  • 337
  • 3
  • 10

1 Answers1

10

It's in service_manager.c, under frameworks/base/cmds/servicemanager.

You may wondering about how they find the servicemanager, it is a feature of binder, after the systemserver start servicemanager(call main in service_manager.c), the servicemanager will register itself as a context_manager of binder by ioctl(bs->fd, BINDER_SET_CONTEXT_MGR, 0);. Then you can always get that service from binder.

So when other service want to use the service manager to list, lookup or add a service, it will call defaultServiceManager method in IServiceManager.cpp. That method will look up the handle 0 to get the BpServiceManager. When you use BpServiceManager->addService, it won't call BnServiceManager, this is slightly different with other service like CameraService. The binder will directly parse the transaction code and call do_add_service method in service_manager.c. You may notice that the transaction code used by BpServiceManager is exactly same with the one in svcmgr_handler.

//transaction code used by svcmgr_handler
enum {
    SVC_MGR_GET_SERVICE = 1,
    SVC_MGR_CHECK_SERVICE,
    SVC_MGR_ADD_SERVICE,
    SVC_MGR_LIST_SERVICES,
};

//transaction code used by BpServiceManager.
enum {
        GET_SERVICE_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION, //0x00000001
        CHECK_SERVICE_TRANSACTION,
        ADD_SERVICE_TRANSACTION,
        LIST_SERVICES_TRANSACTION,
    };

Only servicemanager works like this, other BpService will call their BnService, For example, the CameraService extends the BnCameraService, so it is the actual server side of CameraService. The BpCameraService will start a binder transaction, and the binder transaction will finally get handled by BnCameraService, which is the CameraService.

You can search the entire AOSP, there is no implementation of BnServiceManager, so it is not possible for it to get called.

StarPinkER
  • 14,081
  • 7
  • 55
  • 81
  • Hi, thanks for the reply. I went through the service_manager.c file. I see that the service_manager reigisters itself to the binder with handle '0', to specify to that it is the service manager. So, when a servie request (getService) goes to BpServiceManager, do you mean to say that the binder won't call the BnServiceManager::onTransact, instead call the svcmgr_handler registered in service_manager.c ? In that case, may I know why the BnServiceManager was defined ? Please, correct me, if my understanding is wrong. – Chakkra Mar 20 '13 at 05:49
  • The existence of BnServiceManager disturb me a lot as well. Thanks to your elegant and clear explanation. ~~ – Victor Choy Mar 18 '16 at 18:55
  • Appreciate this Q was answered a long time ago, but if anyone interested, `BpServiceManager` is defined https://android.googlesource.com/platform/frameworks/native/+/4aaa39358f538d8e06e026385bb8be8088d78c35/libs/binder/IServiceManager.cpp#126 – Dori Sep 15 '16 at 13:04