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

F3f71a3003c496bb78fac5736288373b?s=128

Aleksander Piotrowski

December 04, 2015
Tweet

Transcript

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

    IBinder.java, and Linux kernel... Aleksander Piotrowski @pelotasplus
  2. Context is everything ...on Android, I mean.

  3. Authors

  4. Dianne Hackborn • using Google+ • 2 years Be Inc.

    • 4 years at PalmSource • 10 years at Google • Android Framework team
  5. 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
  6. http://www.openbsd.org/

  7. http://cr.yp.to/

  8. http://www.angryredplanet.com/~hackbod/

  9. http://www.angryredplanet.com/~hackbod/pics/cats2/

  10. http://www.angryredplanet.com/~hackbod/openbinder/docs/html/Authors.html

  11. Software roots

  12. ~ 95/97 1995- 2001

  13. ~ 2004 PalmOS Cobalt 6

  14. Android as Linux Androidisms

  15. Androidisms • low memory killer • wakelocks • ashmem •

    alarms • paranoid network • Binder ◦ implements IPC ◦ security mechanism • ... http://www.elinux.org/Android_Kernel_Features
  16. IPC Inter-process communication

  17. 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
  18. IPC Unix roots • files • signals • sockets ◦

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

    IPC designed for BeOS PalmOS Android!
  20. 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/
  21. 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
  22. Linux kernel Dalvik Libraries Android Framework Applications SQLite Wi-Fi driver

    Window Manager Facebook Package Manager Chrome Camera Driver BINDER BINDER BINDER BINDER
  23. None
  24. Discovery

  25. 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
  26. 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
  27. 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]
  28. Interaction boring low-level stuff

  29. Process A Process B Kernel ioctl() ioctl() struct binder_write_read struct

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

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

    security ◦ based on UIDs and PIDs ◦ and tokens • share file descriptor • local execution mode
  32. “Exciting” Java world

  33. public void iLoveCracow(Context context) { Uri uri = Uri.parse("http://partykrakow.co.uk/"); Intent

    intent = new Intent(Intent.ACTION_VIEW, uri); context.startActivity(intent); }
  34. 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)
  35. 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
  36. 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); }
  37. Service Application A Activity Handler Messanger reference callback Application B

    startService(intent) Messenger intent intent
  38. 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
  39. public final class Messenger implements Parcelable { private final IMessenger

    mTarget; // [...] public IBinder getBinder() { return mTarget.asBinder(); } // [...] } public final class Message implements Parcelable { // [...] }
  40. interface IBusinessLogic { void add(inout Product product); List<Product> getProducts(); }

  41. 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; }
  42. 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
  43. Client Server Kernel ioctl() ioctl() struct binder_write_read struct binder_write_read /dev/binder

    Binder driver Proxy Stub AIDL
  44. 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