Wed. Dec 1st, 2021
Android System IPC Mechanisms. Introduction | by PK
PK

On an Android system, as we all know, each app runs by itself course of. When the app hundreds, the Application Manager intimates the Zygote.Zygote forks itself and makes a replica of the Virtual Machine (VM). So the appliance will get a ‘warm’ VM with loaded libraries. Each Android apps are sand-boxed, so every course of is impartial of one another. Sandbox idea is used to handle or comprise the completely different ranges of permissions restricted to every particular person apps. So, it wants IPC to speak between the apps / companies / system companies. Under one course of, there could possibly be a number of threads, which share the method reminiscence, however has its personal stack.

IPC is the spine of any software program system / OS. Android offers 2 methods to attain IPC.

  1. ashmem

2. Binder

3. Socket

The above 3 methods help IPC inside a tool. Let me contact upon ashmem first.

ashmem (Anonymous shared reminiscence subsystem) is an idea which has similarities to POSIX SHM (Shared Memory). The distinction is, ashmem claims that it overcomes the issue of reminiscence leaks. ashmem isn’t accessible for Android purposes, however they’re used by the low stage system software program / processes. The parts of System Server like SurfaceFlinger, AudioFlinger, and many others use ashmem for IPC. ashmem is quick. Another instance the place ashmem is used is, the Virtual Machine. When .oat code is offered to the VM utilizing ashmem. When a course of needs to speak with one other course of, the method creates a shared reminiscence area, after which shares the file descriptor of that reminiscence area with the opposite course of with which it needs to speak. This file descriptor is shipped to different course of through Binder. The system processes rely ashmem by way of IMemory interface, which isn’t accessible or not uncovered to the app builders.

ashmem additionally does reminiscence administration, by shrinking or increasing the reminiscence space as wanted. When the system wants extra reminiscence, it shrinks its are and releases reminiscence for the system. If a shared reminiscence space is marked as pinned, then that space cant be launched at any circumstance. ashmem makes use of reference counting to destroy reminiscence areas, when the processes referring to them have exited.

Basically, ashmem is a means used by the system stage processes, not by utility processes for IPC

Binder framework has a historical past. This was mainly developed for Be computer systems. Then it was taken over by Palm, and embedded in Palm OS. In 2005, the Binder Framework was made open supply by identify OpenBinder. (Ref: https://en.wikipedia.org/wiki/OpenBinder). The individuals behind the Binder framework are a crew, lead by Dianne Hackborn. Another outstanding identify within the Binder world is George Hoffman. Later, Google employed this crew to re-engineer Binder Framework, freshly developed the Binder Framework, since then it is part of Android.

While talking about Binder, watch out who’s your viewers. By the phrase ‘Binder’, an app developer might think about the Binder interface accessible within the framework, to bind to an utility service. On the opposite hand, Binder has extra depth than simply the Binder interface (IBinder) on the framework stage which is uncovered to the app builders. For the app builders, there are few IPC strategies accessible like Messenger, Intent, AIDL and the IBinder interface itself. In the background, all of those are based mostly on Binder. AOSP reveals that every of those use IBinder interface. By saying Binder, right here I’m referring tot he entire “Binder Framework” in Android. So, the place is that this Binder which I’m referring to?

Fig. 1 Where is Binder?

Yes, Binder is in every single place. Above diagram reveals a quick illustration of an Android system. There are Linux Kernel, Libraries, ART, Framework and Applications. Some instance contents of every blocks are proven (With arrow). Blue containers proven in every block, every considered one of which is a Binder framework part. Part of Binder Framework sits within the Kernel, which known as Binder Driver (/dev/binder). There is Libbinder.so, which is the Binder library as a part of the Android Libraries. Then there may be the IBinder interface within the Android Framework. An utility developer instantiates this to attain the IPC on the utility stage. So, technically, Binder runs by way of the system stack.

In a Linux system, there are a number of IPC mechanisms accessible, akin to Files, Signals, Sockets, Pipes, on a better stage, D-Bus. Linux Kernel used with Android doesn’t carry these highly effective(?) mechanisms. Instead, Binder replaces all of them.

Not precisely the constructing blocks, however the essential ideas or summary concepts of Binder are:

  • Binder Context Manager
  • Binder Service and Client
  • Binder Token
  • Binder Protocol
  • Binder driver

When the system begins, Binder driver lets one course of to register itself because the context supervisor. That course of is the Service Manager of the Android system. As we all know, Service Manager is the central registry for all of the system companies.

To perform the communication between 2 processes, there needs to be 2 processes. One would be the Service and the opposite one subscribing to it is going to be referred to as because the consumer.

Binder token is for identification. When a Binder message is handed to the opposite course of, it’s handed with an identification token, to establish the sender

Binder protocol is nothing however the completely different steps these happen whereas passing a message from one course of to a different course of. The message passing cant be completed like under

Fig. 2 Not this fashion!

Instead, contemplate the under diagram

Fig. 3 Step 1

In the above diagram, there are 2 processes A and B, and A needs to ship a message (Yellow field) to B. These 2 course of needs to be “bound” to one another with the assistance of IBinder interface (Intent, AIDL, IBinder, Messenger, doesn’t matter). The inexperienced ellipses proven on every course of are the learn solely reminiscence areas, from which these respective processes can learn the information however cant write into them. Only the Kernel can write knowledge into these inexperienced ellipses. As every other gadget driver, Binder Driver additionally implements ioctl() capabilities. Process A calls this perform to move the message to the Binder Driver. The Binder Driver offers a particular struct named binder_write_read. The message is handed with this format.

Fig. 3 Step 2

As proven within the above image, now the information has been transferred to the Kernel house. From right here, the Kernel places the information to the opposite aspect of the communication channel, lastly it leads to the reminiscence space of the Process B as proven under.

Fig. 4 Step 3

From right here, the Process B reads it for additional processing.

As mentioned above, binder driver is a software program parts, which hundreds within the Kernel house, and is accountable for transferring the information from the reminiscence are of 1 course of to the opposite. It will be accessed by ioctl() calls

Note: The learn solely reminiscence space is proven within the course of space. But they’re truly managed by the Kernel

Binder helps

  • 2 Way communication
  • Manages: Threads, Memory, Object Mapping, Reference Counting
  • Notifications: When a service dies, Binder takes care of notifying the consumer in regards to the service demise
  • Identification and Security: Done utilizing Binder Token; additionally based mostly on UIDs and PIDs
  • Marshalling of transaction knowledge aka parcels
  • Shares the file descriptors of ashmem reminiscence areas between the processes
  • Local execution mode: If the communication is inside a course of, it doesn’t undergo the kernel, it makes use of native shared reminiscence

It would come as a shock, however Android makes use of socket to speak with one part of the system. That is Zygote. When the Zygote is chilly booted (launched by way of app_process from the init.rc), it launches a VM and retains a socket open to hear directions from the Application Manager. When the person launches an app, the Application Manager intimates the Zygote by way of this open socket.

By admin