Introduction (Summary)
Yi, Yi, Yi.
I had planned to finish binder at the end of August and record what I had learned with an article, but:
- Read read, feel the lack of a can connect the whole
binder
The mainline, so decided to addtakeaway
- Writing, suddenly found that a fit, so split into three
takeaway
andIntroduction of binder
For a- The use of binder is a
Principle of binder
For a large piece (85KB….)- Digging can not be divided into: principle
- The excavation can not be divided into: driving
Simple start
Let’s start with a brief overview of the concepts involved in binder architecture:
The client process
: The process that initiates the remote method callService process
: The process that the real service runs onBinder drive
:Binder communication
Is the core through which remote calls are processed and passedservicemanager
: a managementBinder service
The process of
A simple version of the process called across processes:
The client process
Send the call informationdrive
And wait fordrive
Return messagesdrive
Process the call information and notifyService process
Service process
receiveddrive
To start execution based on the call informationService process
Completion of execution, noticedrive
drive
To receiveService process
For simple data processing, notificationThe client process
Read the dataThe client process
Received notification, readdrive
Returned data
Two nouns that need to be easily distinguished:
- To implement a
Binder service
:Binder
To implement one of my ownIPC
Service (how to useBinder
) Binder
The implementation of the:Binder
The realization of the principle, involving learningBinder drive
Part of the
Binder’s questions
The first question is, how do you determine which service to call if the client process wants to call the function of the server process? How do you know which function to call?
Let’s start with a few points:
- in
Binder service
Android defines some template classes for the use of- in
Service process
If you write a service that you want to supportBinder Remote call
, you have to write it the way I do- for
C++
Layer, to be implemented manuallyBn...
,Bp...
Such as class (do not know how to write? There are so many IPC support services in Android, learn from them. The good news is there’s aHIDL
Simplifies development) - for
Java
Layer, becauseAIDL
There will be automatic script generationBn...
,Bp...
Class, just implement the business logic in the callback
- for
- And for
The client process
If you want to complete a remote call, you must do it at programming time- for
C++
Layer, to get the service that you want to callBp.....
Type information is ok, it’s written in during the programming phaseThe client process
- for
Java
Layer, copy what you want to callService process
In theAIDL
inThe client process
, recompile to get the corresponding service interface class
- for
- for
The client process
There is another way to do remote calls, which is the various methods we use. Manager
- and
c++
Layer and theAIDL
Similarly, we also pass for the client process. Manager
Class interface to complete the call
- and
- in
- Imagine a
Free communication state
It should be: Give me aBinder drive
I can live with that. Why integrate yoursService interface class
Ah, so many rules. I want to call whichIPC
Service, directly throughdrive
Just send it to him.- I am not thorough, just an example:
- How do I get the service object to invoke? Background service thousands of millions, even if do not get, also have to inform this service
- How do you pass which method to call?
- How many ways are there to get remote services?
- In addition to completing business logic, a server process is written to work independently and to drive communication.
- These questions,
Android
With self-designed Binder relatedTemplate class
Have solved the- Don’t know where the service object is?
- Then in
The client process
Plus I designed itBp...
Class with predefined interfacesasInterface
, directly conversion is good
- Then in
- Don’t know what methods you can call remotely?
- All give you
Bp...
Class, what elseThe bicycle
?
- All give you
- Use and
Binder drive
Communication?- Through what letter ah, wrapped up, call method directly on the line
- Don’t know where the service object is?
- I am not thorough, just an example:
- Answer here
First question
:- In programming, we call what needs to be called according to requirements
IPC
The service ofBp...
Class party to our client processBp...
Class aasInterface
Method, can be converted into what we need through this methodProxy objects
- Binder will specify the method to call
Bp...
andBn...
The template class defines the function number to call- The client process is ready
Bp...
Class, also have the call function number
- The client process is ready
- In programming, we call what needs to be called according to requirements
Second question, when did the service we wrote start?
Generally speaking, our service should start listening for messages from the outside world when the process starts, otherwise it will miss messages.
Binder did the same thing:
- for
Service process
for- Will be in
Process started
When performingjoinThreadPool
- This function is going to enter
The while loop
readBinder drive
The data of
- This function is going to enter
- Will be in
- for
The client process
for- Is executed when a remote call is sent
transact
When the function- This function sends the call data to
Binder drive
Then, it continues to read and view the returned data
- This function sends the call data to
- Is executed when a remote call is sent
Why doesn’t the Servicemanager get a lot of space?
First of all this is not a problem, hahaha!
servicemanager
isAndroid
Used to manage registrationBinder service
OursgetService
Most likely I’m looking for it. (Have not read the source code, do not believe)servicemanager
Instead of following the template defined by Android, we implemented a simple versionservicemanager
It starts when Android starts, and this part is ininit.rc
In the configuration- There is no
servicemanager
Does not affect our understanding of learningbinder
Overall process and structure - There are the
servicemanager
Can deepen our understanding ofbinder
The understanding of the
End of the introduction
The Binder introduction section describes some knowledge points simply. See the text for details
- The source code in the book is
The Android 5.0
, butbinder
This part from9.0
Not much has changed on the top, just a few morelog
print - The code’s strong vitality is exactly what we’re after, hahaha
- I was going to say that in the introduction
A copy of
Forget it and leave it in the text
Binder’s course was two weeks behind schedule…
Fortunately, really drop can learn a lot of things, the following is the text, not proficient, please carefully read 😌
Introduction of Binder
Binder is an Android – specific way of communicating between processes. Android Binder, formerly Known as OpenBinder, was first developed by Dianne Hackborn for PalmOS, who later joined Google, Android Binder was developed based on OpenBinder
Binder incorporates the concept of remote procedure call (RPC) compared to traditional IPC mechanisms, and this remote call is not a traditional procedure oriented remote call, but an object-oriented remote call.
Developed from Unix, IPC mechanism can only provide a relatively primitive inter-process communication means, communication between the two sides must deal with thread synchronization, memory management and other complex issues, not only a large workload, but also prone to error.
In addition to sockets and anonymous pipes, traditional IPC, such as named pipes, semaphores, message queues, etc. have been removed from Android.
Compared with other IPC, Socket is a more mature means of communication, synchronization control is also easy to achieve. Socket is good for network communication, but not very efficient for interprocess communication.
The Android architecture has always wanted to blur the concept of processes and replace it with the concept of components. Applications do not need to be concerned with where components are stored, which process they are running in, the life cycle, etc. Component functionality is available anytime, anywhere with a Binder object. Binder acts as a web that holds together the components of an entire system, across processes and threads.
Binder is powerful and complex. But when we use it, it is a very easy thing, no need to consider thread synchronization, memory allocation, etc. All of this complexity is done with the Binder framework.
Because of this, Android services are built with Binder. Android has dozens of services that no other embedded platform has.
Android uses shared memory to transfer data between processes, so that data can be copied from one process to another only once (common IPC requires two steps, from user process to kernel, and then from kernel to server process), greatly improving data transmission efficiency.
Binder calls pass the process’S EUID to the server. Therefore, the server can check the calling process’s permission to decide whether to allow it to use the called service.
Binder is the IPC mechanism for Android. To deepen the understanding, the article has a surprise
Binder
Object definitions
Binder objects are defined as follows for ease of understanding:
Binder entity object
:Binder entity object
isBinder
Service provider. An offerBinder
The class of the service must be inheritedBBinder
Class. Therefore, sometimes to emphasize the type of the object, also usedBBinder object
To take the place ofBinder entity object
.Binder reference object
:Binder reference object
isBinder entity object
On behalf of the client process, the type of each reference object isBpBinder
Class. You can also use namesBpBinder object
To take the place ofBinder reference object
.Binder Proxy object
:Binder Proxy object
Also known asBinder Interface object
, it mainly provides interface services for the client’s upper-layer applications fromIInterface
Class derived, which implementsBinder
The functional interface of the service, of course, is just an empty shell for tuning. throughBinder Proxy object
Applications can use remote objects as if they were local objectsBinder entity object
Services provided.IBinder object
:BBinder
andBpBinder
Class fromIBinder
Object inherited from. In many cases, there is no need to distinguishBinder entity object
andBinder reference object
This is a deliberate useIBinder object
To call them all together.
Binder interface objects work with applications. The advantage of separating Binder interface objects from Binder reference objects is that Binder interface objects can have many instances, but they all contain the same Binder reference object, which facilitates the use of the user layer.
The diagram is as follows:
Binder interface objects can be used directly with Binder interface objects, but they are not compatible. The separation of Binder reference objects and Binder interface objects on the client side enables Android to provide Binder services to both the Java layer and Native layer in a single architecture. After isolation, the Binder layer does not need to care about the implementation details of the upper layer and only needs to interact with Binder entity objects and Binder reference objects.
Binder
The architecture of the
The participants of Binder Communications consist of four parts:
Binder drive
:Binder
The core of various implementationsBinder
The underlying operation of.ServiceManager
: provideBinder entity object
The name of theBinder reference object
Conversion service ofThe service side
:Binder
Service providerThe client
:Binder
Consumer of the Service
The diagram is as follows:
Binder driver simple explanation:
Binder drive
Belong toBinder
The core of the architecture is through standard interfaces within the file system, such asopen
,ioctl
,mmap
Etc., providing services to the user layer.- The application and
Binder drive
Between the data throughioctl
Interface to complete and is not usedread
andwrite
System call. useioctl
The advantage is that a system call can complete the two-way data exchange between the user system and the driver, not only simplifies the control logic, but also improves the transmission efficiency. Binder drive
The main function of theBinder
Communication channel, maintenanceBinder
Object reference count, transform in transitBinder entity object
andBinder reference object
And manage the data cache.
ServiceManager:
ServiceManager
Is a daemon that providesBinder
The query function of a service that returns a reference to the service being queried.- For a
Binder
A service will usually have a unique string identifier as long as it identifies toServiceManager
By registering the identity, the application can be acquired by the identityBinder
The reference object of the service.ServiceManager
Is a separate process for others to obtainBinder
For the process of service, the first task is to obtainServiceManager
.ServiceManager
Also through the Binder framework to provide services, it feels like the situation of chicken born chicken eggs- To ensure that other processes can obtain
ServiceManager
Android registers bindings by defaultServiceManager
:Binder reference object
The core data is oneBinder entity object
Is a value assigned within the driver.Binder framework
It’s hard and fast0
On behalf ofServiceManager
. This allows the user process to use parameters0
You can construct it directlyServiceManager
theBinder reference object
.
- Why search
Binder
Service functions are not placed directlyBinder drive
How about in?- Android is a security management requirement that does not allow any process to register
Binder
Service,Although any process can be createdBinder
service, but onlyThe root process
orThe system process
Before they can have unlimited access toServiceManager
Registration services. ServiceManager
There is a table specifying the user ids of the processes that can register the service, and the names of the services that can register.
- Android is a security management requirement that does not allow any process to register
Binder services can be divided into two types: real name services and anonymous services. Except for the fact that the real name service can be queried through the ServiceManager, there is no other difference between development and use.
Binder services for common application development are anonymous services. For anonymous services that are not available through ServiceManager, how do clients obtain Binder reference objects?
- First of all,
Anonymous Binder Services
The transmission must be based on established Binder communication connections, i.e. based on real name registrationBinder
Server
willAnonymous Binder objects
writeParcel
And then throughioctl
andBinder drive
The communication,Bidner drive
Incoming objects are checked.- If you find incoming
type
isbinder
In the current process (Server
The correspondingproc
) to see if the node exists- If not, use the incoming one
Anonymous Binder objects
In user spacePointer to the
andcookie
(usuallyBBinder
Construct a new node, and then getAnonymous Binder objects
References, stored intransaction_data
Waiting in theClient
To obtain.
- If not, use the incoming one
Client
To get thisAnonymous Binder
Through this reference to theServer
The entity in the.
These are the information collected by Anonymous Binder for the time being, which can be supplemented after learning
componentService
And anonymous Binder services
Android component Services contain a way to start Java anonymous Binder services, which we’ll take a quick look at
- First, you need to apply the call
bindService
Method emits aIntent
Framework
According to theIntent
Find the corresponding componentService
And start itService
In theAnonymous Binder Services
It will be created at the same time.- Then,
Framework
It will be servedIBinder
Object throughConnectivityManager
The callback method ofonServiceConnected()
Pass back to the application - So we have our application
Anonymous Binder Services
Reference object to
Here’s AndroidFramework
withIntent
Instead of aBinder service
The name to look up the corresponding service, and also bear itServiceManager
The work of parsingIntent
And returns a reference object to the service. As shown in figure:
The Android Framework does not use new techniques to pass Binder objects because the ActivityManagerService that plays this role in the Framework is itself a Binder service, Ultimately, IBinder objects are passed between the server and client through the Binder framework.
The level of the Binder
In terms of code implementation, Binde’s designed classes can be divided into four levels, as shown in the figure:
- The topmost layer is located at
Framework
Binder service classes and their interface classes. This layer has many classes, such as:ActivityManagerService
,WindowManagerService
,PackageManagerService
They provide a variety of services for applications. - The second layer is for the top layer
Service classes and interface classes
Provides the basis for the development of IBinder, BBinder, BpBinder, etc. - The third layer is sum
Binder drive
The interaction ofIPCThreadState
andProcessState
class - The bottom is
Binder drive
.
From the diagram, libbinder is split into two layers for the following reasons:
- Layer 1 and Layer 2 are closely linked, with various Binder classes in layer 2 supporting the development of service and proxy classes
- But the third tier
IPCThread
It’s very coupled to the fourth level drive
This is where the Binder architecture has been criticized for being too coupling between the driver and application layers, which violates the principles of Linux driver design. As a result, mainstream Linux has been reluctant to embrace Binder.