In AOSP, where is the ServiceManager implemented

android, android-binder, android-source

Solution

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.

Problem

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.

Original source