Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Nothing but the truth about Binder

Nothing but the truth about Binder

NOTICE: There are animations in this presentation, sadly conversion from Slides to PDF made them a bit unreadable :-/

We all know that Android was given us by the Google but don't know that its roots are somewhere else. With Android being nowdays mosty Android Support Library, Design Library, and whatever new just got released, we will have a look at good old Binder, who is our frind since forever and makes Android a pleasant place to be for us, an Android developers.

Google Slides link, one with animations actually working: https://docs.google.com/presentation/d/1JqtdZUETc7isee8hj7hWIVaS6PNy82atKsnsq8n-tBA/edit?usp=sharing

Aleksander Piotrowski

December 04, 2015
Tweet

More Decks by Aleksander Piotrowski

Other Decks in Programming

Transcript

  1. Nothing but the truth about Binder So basically about Binder.java,

    IBinder.java, and Linux kernel... Aleksander Piotrowski @pelotasplus
  2. Dianne Hackborn • using Google+ • 2 years Be Inc.

    • 4 years at PalmSource • 10 years at Google • Android Framework team
  3. Dianne Hackborn "This post is by Dianne Hackborn, a Software

    Engineer who sits very near the exact center of everything Android.” — Tim Bray The Android 3.0 Fragments API http://android-developers.blogspot.com/2011/02/android-30-fragments-api.html
  4. Androidisms • low memory killer • wakelocks • ashmem •

    alarms • paranoid network • Binder ◦ implements IPC ◦ security mechanism • ... http://www.elinux.org/Android_Kernel_Features
  5. IPC basics • all processes have separate address spaces •

    cannot directly access another process memory ◦ stability reasons ◦ security reasons • structured way, a mechanism to offer some services across multiple processes • all Unix-like systems have some kind of IPC • discovery • interaction
  6. IPC Unix roots • files • signals • sockets ◦

    UNIX domain ◦ TCP/IP • pipes • semaphores • shared memory
  7. Binder IPC designed for BeOS IPC designed for BeOS PalmOS

    IPC designed for BeOS PalmOS Android!
  8. Binder basics • based on OpenBinder, ~2005 • brand new

    implementation • distributed component architecture • message-passing • method invocation system • runs on a single device • doesn’t support RPC across the network • kernel-part of Linux version in kernel version 3.19, Feb 2015 http://www.angryredplanet.com/~hackbod/openbinder/docs/html/
  9. Binder terminology • Binder ◦ the Binder ◦ a Binder

    • IBinder interface • Binder or Binder Object • Binder Service and Client • Binder Protocol • Binder token • Binder Context Manager • Binder driver
  10. Linux kernel Dalvik Libraries Android Framework Applications SQLite Wi-Fi driver

    Window Manager Facebook Package Manager Chrome Camera Driver BINDER BINDER BINDER BINDER
  11. Service Manager aka Binder Context Manager • /system/bin/servicemanager • information

    directory • started by init at system start • register itself as Binder Context Manager • applications get handlers to services from SM • via Binder mechanism • implements IServiceManager interface • glue in ServiceManager.java
  12. System Services • 79 as of KitKat • implement fundamental

    Android features ◦ display & touch screen ◦ telephony ◦ android.app.DownloadManager ◦ android.app.job.JobScheduler • define a remote interface • can be called from other services and applications • of course using Binder • mostly in Java but few are native
  13. System Services list via System Manager root@vbox86p:/ # service list

    Found 94 services: 0 sip: [android.net.sip.ISipService] 1 phone: [com.android.internal.telephony.ITelephony] 18 dreams: [android.service.dreams.IDreamManager] 25 jobscheduler: [android.app.job.IJobScheduler] 30 audio: [android.media.IAudioService] 35 search: [android.app.ISearchManager] 37 location: [android.location.ILocationManager] 39 notification: [android.app.INotificationManager] 42 connectivity: [android.net.IConnectivityManager] 45 wifi: [android.net.wifi.IWifiManager] 60 window: [android.view.IWindowManager] 65 account: [android.accounts.IAccountManager] 79 activity: [android.app.IActivityManager] 80 user: [android.os.IUserManager] 81 package: [android.content.pm.IPackageManager]
  14. Process A Process B Kernel ioctl() ioctl() struct binder_write_read struct

    binder_write_read /dev/binder Binder driver Binder managed memory region ?
  15. Binder features • two-way or one-way communication • manage ◦

    threads ◦ memory ◦ object mapping ◦ reference counting • notification • identification ◦ UID, PID ◦ token
  16. Binder features • marshalling of transaction data aka parcels •

    security ◦ based on UIDs and PIDs ◦ and tokens • share file descriptor • local execution mode
  17. public void iLoveCracow(Context context) { Uri uri = Uri.parse("http://partykrakow.co.uk/"); Intent

    intent = new Intent(Intent.ACTION_VIEW, uri); context.startActivity(intent); }
  18. Intents • higher-level abstraction on top of Binder • easier

    to use • but a bit limited • API is loosely-defined • so prone to run-time errors • class Intent implements Parcelable, Cloneable • ActivityMangerService Activity A Application X Activity B Application Y startActivity(intent) setResult(RESULT_O K, intent)
  19. Where is the binder in Intents? • frameworks/base/core/java/android/app/ActivityManagerNative.java 5 9

    9 1 • frameworks/base/core/java/android/app/IActivityManager.java 8 6 7 • frameworks/base/core/java/android/app/ContextImpl.java 2 0 4 3 • frameworks/base/.../com/android/server/am/ActivityManagerService.java 2 0 7 4 4
  20. public void grazynaIsAwesome(Context context) { String action = "pl.droidcon.service"; Intent

    intent = new Intent(action); intent.putExtras(bundle); context.startService(intent); } Messenger messenger = new Messenger(handler); intent.putExtra("messenger", messenger); }
  21. Messenger • a reference to Handler • can be send

    to another process • used to send Messages via Handler • like Intents, have an action and data ◦ int what, int arg1, int arg2, and Object • asynchronous • lower overhead than Intents
  22. public final class Messenger implements Parcelable { private final IMessenger

    mTarget; // [...] public IBinder getBinder() { return mTarget.asBinder(); } // [...] } public final class Message implements Parcelable { // [...] }
  23. public interface IBusinessLogic extends android.os.IInterface { public static abstract class

    Stub extends android.os.Binder implments IBusinessLogic { // onTransact } private static class Proxy implements IBusinessLogic { // IBinder mRemote } void add(Product product) throws android.os.RemoteException; java.util.List<Product> getProducts() throws android.os.RemoteException; }
  24. AIDL • Android Interface Definition Language • for defining Binder-based

    services • Java-like language • supports plenty of types ◦ null, float, char[] ◦ Map<,>, Bundle, List, Parcelable ◦ File descriptor ◦ IBinder • limited exceptions supported
  25. Bring It On by smooved System service, if you have

    any question in your mind as to whether I can satisfy you, bring your requirements to me now. smooved will satisfy you. Bring it on to me, and I will do you like nobody can. I will publish your services for you like nobody can. I will provide an auxilary process in which to run these services like nobody can. I am ready for you now. Lay your requirements down, and I will show you what makes smooved the only process you need. I will call into your plug-in and obtain a top level GHandler. I will provide your GHandler with only the finest BMessages from the farthest reaches of the system. Then, I will allow your handler to register itself as providing a particular type of service. We will be launched at boot time. You and I. http://www.angryredplanet.com/~hackbod/openbinder/docs/html/BringItOn.html