Android system-ServiceManager2

Table of contents

introduction:

GetServiceManager

flow chart

Register System Service

Get system services


introduction:

Before registering or using a service, you need to find the corresponding service through ServiceManager, the DNS. So how do you find the ServiceManager?

How to register system service?

How to obtain system services?

GetServiceManager

framework/native/libs/binder/

- ProcessState.cpp

- BpBinder.cpp

- Binder.cpp

- IServiceManager.cpp

framework/native/include/binder/

- IServiceManager.h

- IInterface.h

Obtain the gDefaultServiceManager object through the defaultServiceManager method. If the object exists, it returns directly, and if the object does not exist, it creates the object.

The process of creation: open the binder driver, mmap maps the kernel address space, and enters the loop message loop

flow chart

The creation process of gDefaultServiceManager can be decomposed into the following three steps:

1) ProcessState::self(): used to obtain ProcessState object (also singleton mode), each process has one and only one ProcessState object, if it exists, it will be returned directly, if it does not exist, it will be created; 2) getContextObject(): used to
obtain BpBinder object, for the BpBinder object with handle=0, if it exists, it will be returned directly, if it does not exist, it will be created;
3) interface_cast<IServiceManager>(): used to obtain the BpServiceManager object;

defaultServiceManager is equivalent to new BpServiceManager(new BpBinder(0));


The main work of ProcessState::self():
call open() to open the /dev/binder drive device;
then use mmap() to create a memory address space with a size of 1M-8K;
set the maximum number of concurrent Binder threads in the current process The number is 16.


BpServiceManager cleverly integrates the communication layer and business layer logic:

The business logic function in the interface is realized by inheriting the interface IServiceManager; the
Binder communication work is carried out through the member variable mRemote=new BpBinder(0).
BpBinder points to the corresponding BBinder through the handler. In the entire Binder system, handle=0 represents the BBinder corresponding to the ServiceManager.

Register System Service

framework/native/libs/binder/
  - Binder.cpp
  - BpBinder.cpp
  - IPCThreadState.cpp
  - ProcessState.cpp
  - IServiceManager.cpp
  - IInterface.cpp
  - Parcel.cpp

frameworks/native/include/binder/
  - IInterface.h (包括BnInterface, BpInterface)

Get system services

Guess you like

Origin blog.csdn.net/haigand/article/details/132251585