Service Manager is one of the core components of binder interprocess communication:
It plays the role of binder between the interprocess communication context Manager
Responsible for managing service components in the system
Providing services to the client component to obtain proxy objects
First, Service manager start-up
1, ServiceManager is initiated by Init, startup script in System/core/rootdir/init.rc.
Service Servicemanager/system/bin/servicemanager
Class Core
User System
Group system
Critical
Onrestart Restart HEALTHD
Onrestart Restart Zygote
Onrestart Restart Media
Onrestart Restart Surfaceflinger
Onrestart Restart DRM
Keyword service, which indicates that ServiceManager is started as a service
Keyword user, indicating running as Systemm
Keyword critical, indicating that the system is a key service.
Keyword Onrestart, which indicates that the process needs to be restarted after ServiceManager restarts.
2. ServiceManager code path: Framworks/native/cmds/servicemanager
The entry function is the main function of SERVICE_MANAGER.C.
The process is generally as follows:
Binder_open: Opening the Dev/binder device and mapping to the address space of the process triggers the Binder_open and binder_mmap that call the binder driver and constructs the BINDER_PROC structure for it.
Binder_become_context_manager: With the IOCTL system call, using the BINDER_SET_CONTEXT_MGR protocol to communicate with the binder driver, the IOCTL call triggers a call to the BINDER_IOCTL call, Where the binder_thread body of the current thread is created for the ServiceManager process. The process's binder_node is then saved in the global variable Binder_context_mgr_node, and a valid user ID for the current process is saved in Binder_context_mgr_uid. This indicates the identity of the ServiceManager manager.
Binder_loop: In the second step, when generating the binder_thread body for the ServiceManager process, set its state to return immediately (Binder_looper_state_need_return) in order to complete the current operation. Such a flag causes the ServiceManager binder thread to be set to not be prepared for interprocess communication, thus not receiving requests for interprocess communication. The Binder_loop will first send the Bc_enter_loop protocol via Binder_write_read, informing the binder driver that the main thread of ServiceManager has been able to receive interprocess communication requests. The binder_loop will then use Binder_write_read to detect whether the request is pending, and if so, to Binder_parse parse the command and process it. Within the binder driver, the corresponding functions for handling binder_write_read are: Binder_thread_write and Binder_thread_read.
Second, the acquisition of ServiceManager agent Object
The ServiceManager proxy object type is Bpservicemanager, which is used to describe a client component that implements the Iservicemanage interface.
The implementation structure can refer to the implementation schematic diagram of the client component, and the XXXX can be replaced with ServiceManager.
The difference is that the acquisition of a generic Service proxy object is a client-initiated request, and the binder driver returns a handle value that creates a binder proxy object based on the handle value. The ServiceManager handle has a constant value of 0, eliminating the process of interacting with the binder driver.
The call to get the proxy is Defaultservicemanager (), which is implemented in IServiceManager.cpp.
The process is:
1, get the Processstate object of ServiceManager process, can realize by Processstate.self ()
2. During the construction of the processstate, the Dev/binder device is opened and mapped to the client process, which can live with the kernel buffers used to pass the communication data.
3, call processstate function Getcontextobject, get binder proxy object Bpbinder, here handle is 0.
4, the use of Interface_cast, in which the Bpbinder packaging into Bpservicemanager. and returns a strong reference sp<iservicemanager> for the object's interface Iservicemanager.
Android Binder interprocess communication mechanism 4-service Manager