Android binder learning notes 3 - Registration Service (addService)

Keywords: Android

1. Preface

This paper is mainly about binder series The summary notes of the article are mainly to clarify the overall process and overall architecture of the binder. During this period, the code will be re read according to Android R, and the content will be adjusted according to their own understanding to deepen their overall understanding of the binder. This paper mainly takes vold service as an example to record the main process of addservice.

2. General framework of registration services

Register the MediaPlayerService server. We register the service through the addService() method of ServiceManager.

First, ServiceManager sends BC to Binder driver_ The transaction command (ioctl command, BC can be understood as binder command) carries add_ SERVICE_ The transaction command, and the thread registering the service enters the waiting state waitForResponse(). The Binder driver receives the request command and adds a transaction to the todo queue of the service manager to register the service. The task of a transaction is to create a server-side process binder_node information and insert it into the binder_procs linked list.

Send br after the transaction is completed_ Transaction command: after receiving the command, the service manager adds the registered services to the svcinfo list. Last send br_ The reply command wakes up the waiting thread and notifies the successful registration.

3. vold main

main(int argc, char** argv)
	|--VolumeManager* vm;
	|  NetlinkManager* nm;
	|--parse_args(argc, argv);
	|--mkdir("/dev/block/vold", 0755);
	|  //Create a VolumeManager instance
	|--vm = VolumeManager::Instance()
	|  //Creating a NetlinkManager instance
	|--nm = NetlinkManager::Instance()
	|--android::base::GetBoolProperty("vold.debug", false)
	|--process_config(vm, &has_adoptable, &has_quota, &has_reserved)
	|--android::hardware::configureRpcThreadpool(1, false /* callerWillJoin */);
	|--android::base::SetProperty("vold.has_adoptable", has_adoptable ? "1" : "0");
    |  android::base::SetProperty("vold.has_quota", has_quota ? "1" : "0");
    |  android::base::SetProperty("vold.has_reserved", has_reserved ? "1" : "0");

The init process will start the vold daemon, which will create a volumenanager. After entering the main function entry, it will execute Android:: vold:: voldnatureservice:: start(). In this function, the registration of the vold service with the service manager is completed.

|- -android::vold::VoldNativeService::start

    |--sp<ProcessState> ps(ProcessState::self());
    |--sp<IServiceManager> sm(defaultServiceManager());
    |-- sm->addService(String16(VoldNativeService::getServiceName()), 
            new VoldNativeService(), false, DUMP_FLAG_PRIORITY_DEFAULT)

stay Android binder learning notes 2 - get ServiceManager In this section, we analyze the defaultServiceManager, which obtains the ProcessState object (also a singleton mode) of the vold process through ProcessState::self(). Each process has and has only one ProcessState object. If it exists, it will be returned directly. If it does not exist, it will be created. In this process, a binder is created in the binder driver_ Proc is connected to the linked list, and the buffer space is allocated through mmap; Get the BpBinder object through getContextObject(). For the BpBinder object with handle=0, if it exists, it will be returned directly; if it does not exist, it will be created; Finally, the gDefaultServiceManager object is returned, through which addService is executed

status_t ServiceManagerShim::addService(const String16& name, const sp<IBinder>& service,
                                        bool allowIsolated, int dumpsysPriority)
    Status status = mTheRealServiceManager->addService(
        String8(name).c_str(), service, allowIsolated, dumpsysPriority);
    return status.exceptionCode();

mTheRealServiceManager here is BpServiceManager

Reference documents

Posted by razvypp on Fri, 10 Sep 2021 16:47:27 -0700