Squashed import of adb root patches

Author: Luca Stefani <luca.stefani.ge1@gmail.com>
Date:   Sat Nov 2 22:46:12 2019 +0100
    Add adb_root daemon
    Change-Id: I516f1b85e28f5d061c8c54282a7657d53162bccb

Author: Luca Stefani <luca.stefani.ge1@gmail.com>
Date:   Sat Nov 2 23:54:43 2019 +0100
    Add adb root hooks
    Change-Id: I6b42ae84ab22ed4e86b92c3c1276a44a14d61781

Author: Luca Stefani <luca.stefani.ge1@gmail.com>
Date:   Fri Nov 8 11:36:04 2019 +0100
    Make adb use a custom prop for adb root
    Change-Id: Ie2b8c5e9d75371da43305f003607869370bbd1a4

Author: dianlujitao <dianlujitao@lineageos.org>
Date:   Sun Nov 17 23:21:36 2019 +0800
    Create /data/adbroot on post-fs-data
     * The real /data partition is not available before this stage.
    Change-Id: I2cbcd788f35a2d51c823a71e2c944502812c1cfd

Author: dianlujitao <dianlujitao@lineageos.org>
Date:   Sat Nov 23 20:55:42 2019 +0800
    adbd: Initialize adbroot status to false
    Change-Id: I6d5dc84974dff1a219702fb628e08517848977eb

Author: dianlujitao <dianlujitao@lineageos.org>
Date:   Sat Nov 23 20:58:31 2019 +0800
    adbd: Make adbroot interface a static lib
    Change-Id: I40e46b8312ec77992fc1c11d2190b611bfd049cb

Author: dianlujitao <dianlujitao@lineageos.org>
Date:   Sun Nov 24 16:24:27 2019 +0800
    adb_root: Enforce caller UID
     * AID_SYSTEM - Settings app
     * AID_SHELL - adbd
     * Apply minor clean up while at it
    Change-Id: I15a5d18204a96cc2f8352a6cc7f6f901193aa5b9

Author: dianlujitao <dianlujitao@lineageos.org>
Date:   Sun Nov 24 16:39:11 2019 +0800
    adb_root: Add mutex to binder interface
     * Binder IPC is not implemented in a thread-safe manner, add mutex to
       avoid possible race.
    Change-Id: I37cdca33d519b0fc960960272790d1f55874e2e2

Author: dianlujitao <dianlujitao@lineageos.org>
Date:   Sun Nov 24 17:01:49 2019 +0800
    adb_root: Restart adbd when turning off root
     * The running adbd session remains rooted after turning off from UI.
    Change-Id: Iedd5282b4e3c11ef6c0959ae6867448521cb2c78

Author: Alessandro Astone <ales.astone@gmail.com>
Date:   Fri Mar 20 19:47:03 2020 +0100
    adb: go back to standard adb root prop
     * This reverts change Ie2b8c5e9d75371da43305f003607869370bbd1a4
       plus more changes
    Change-Id: Ia17239e3671d2c1a92664810ed53175110699473

Author: Alessandro Astone <ales.astone@gmail.com>
Date:   Thu Jan 6 22:07:54 2022 +0100
    adb_root: Move to system_ext
    Change-Id: If9840afe40d19b785080681dc92527330fca595a

Author: LuK1337 <priv.luk@gmail.com>
Date:   Thu Mar 24 09:05:17 2022 +0100
    adb_root: Add isSupported() method to determine if we are debuggable
    This allows us to show the preference in Settings even if
    Build.IS_DEBUGGABLE is false.
    Change-Id: If94294f980ccdc10f2473b6f1a858e36547fc258

[LuK1337]: Rework for Android 11.

Change-Id: Ie8db34705d36f3e9f2c53617ec59a26087cbcc87
Signed-off-by: Francescodario Cuzzocrea <bosconovic@gmail.com>
7 files changed
tree: 28df4a52951ccd067a6a1b13b9d0eab1f782b739
  1. aidl/
  2. apex/
  3. client/
  4. coverage/
  5. crypto/
  6. daemon/
  7. fastdeploy/
  8. fdevent/
  9. libs/
  10. pairing_auth/
  11. pairing_connection/
  12. proto/
  13. root/
  14. sysdeps/
  15. tls/
  16. tools/
  17. .clang-format
  18. adb.bash
  19. adb.cpp
  20. adb.h
  21. adb_auth.h
  22. adb_integration_test_adb.xml
  23. adb_integration_test_device.xml
  24. adb_io.cpp
  25. adb_io.h
  26. adb_io_test.cpp
  27. adb_listeners.cpp
  28. adb_listeners.h
  29. adb_listeners_test.cpp
  30. adb_mdns.cpp
  31. adb_mdns.h
  32. adb_test.xml
  33. adb_trace.cpp
  34. adb_trace.h
  35. adb_unique_fd.cpp
  36. adb_unique_fd.h
  37. adb_utils.cpp
  38. adb_utils.h
  39. adb_utils_test.cpp
  40. adb_wifi.h
  41. Android.bp
  42. benchmark_device.py
  43. bugreport_test.cpp
  44. compression_utils.h
  45. file_sync_protocol.h
  46. mdns_test.cpp
  47. MODULE_LICENSE_APACHE2
  48. NOTICE
  49. OVERVIEW.TXT
  50. OWNERS
  51. PREUPLOAD.cfg
  52. protocol.txt
  53. README.md
  54. security_log_tags.h
  55. services.cpp
  56. services.h
  57. SERVICES.TXT
  58. shell_protocol.h
  59. shell_service_protocol.cpp
  60. shell_service_protocol_test.cpp
  61. SOCKET-ACTIVATION.txt
  62. socket.h
  63. socket_spec.cpp
  64. socket_spec.h
  65. socket_spec_test.cpp
  66. socket_test.cpp
  67. sockets.cpp
  68. sockets.dia
  69. SYNC.TXT
  70. sysdeps.h
  71. sysdeps_test.cpp
  72. sysdeps_unix.cpp
  73. sysdeps_win32.cpp
  74. sysdeps_win32_test.cpp
  75. test_adb.py
  76. test_device.py
  77. TEST_MAPPING
  78. trace.sh
  79. transfer_id.h
  80. transport.cpp
  81. transport.h
  82. transport_benchmark.cpp
  83. transport_fd.cpp
  84. transport_test.cpp
  85. types.cpp
  86. types.h
  87. types_test.cpp
README.md

ADB Internals

If you are new to adb source code, you should start by reading OVERVIEW.TXT which describes the three components of adb pipeline.

This document is here to boost what can be achieved within a "window of naive interest". You will not find function or class documentation here but rather the "big picture" which should allow you to build a mental map to help navigate the code.

Three components of adb pipeline

As outlined in the overview, this codebase generates three components (Client, Server (a.k.a Host), and Daemon (a.k.a adbd)). The central part is the Server which runs on the Host computer. On one side the Server exposes a "Smart Socket" to Clients such as adb or DDMLIB. On the other side, the Server continuously monitors for connecting Daemons (as USB devices or TCP emulator). Communication with a device is done with a Transport.

+----------+              +------------------------+
|   ADB    +----------+   |      ADB SERVER        |                   +----------+
|  CLIENT  |          |   |                        |              (USB)|   ADBD   |
+----------+          |   |                     Transport+-------------+ (DEVICE) |
                      |   |                        |                   +----------+
+-----------          |   |                        |
|   ADB    |          v   +                        |                   +----------+
|  CLIENT  +--------->SmartSocket                  |              (USB)|   ADBD   |
+----------+          ^   | (TCP/IP)            Transport+-------------+ (DEVICE) |
                      |   |                        |                   +----------+
+----------+          |   |                        |
|  DDMLIB  |          |   |                     Transport+--+          +----------+
|  CLIENT  +----------+   |                        |        |  (TCP/IP)|   ADBD   |
+----------+              +------------------------+        +----------|(EMULATOR)|
                                                                       +----------+

The Client and the Server are contained in the same executable and both run on the Host machine. Code sections specific to the Host is enclosed within ADB_HOST guard. adbd runs on the Android Device. Daemon specific code is enclosed in !ADB_HOST but also sometimes with-in __ANDROID__ guard.

"SMART SOCKET" and TRANSPORT

A smart socket is a simple TCP socket with a smart protocol built on top of it. This is what Clients connect onto from the Host side. The Client must always initiate communication via a human readable request but the response format varies. The smart protocol is documented in SERVICES.TXT.

On the other side, the Server communicates with a device via a Transport. adb initially targeted devices connecting over USB, which is restricted to a fixed number of data streams. Therefore, adb multiplexes multiple byte streams over a single pipe via Transport. When devices connecting over other mechanisms (e.g. emulators over TCP) were introduced, the existing transport protocol was maintained.

THREADING MODEL and FDEVENT system

At the heart of both the Server and Daemon is a main thread running an fdevent loop, which is a platform-independent abstraction over poll/epoll/WSAPoll monitoring file descriptors events. Requests and services are usually served from the main thread but some service requests result in new threads being spawned.

To allow for operations to run on the Main thread, fdevent features a RunQueue combined with an interrupt fd to force polling to return.

+------------+    +-------------------------^
|  RUNQUEUE  |    |                         |
+------------+    |  POLLING (Main thread)  |
| Function<> |    |                         |
+------------+    |                         |
| Function<> |    ^-^-------^-------^------^^
+------------+      |       |       |       |
|    ...     |      |       |       |       |
+------------+      |       |       |       |
|            |      |       |       |       |
|============|      |       |       |       |
|Interrupt fd+------+  +----+  +----+  +----+
+------------+         fd      Socket  Pipe

ASOCKET, APACKET, and AMESSAGE

The asocket, apacket, and amessage constructs exist only to wrap data while it transits on a Transport. An asocket handles a stream of apackets. An apacket consists in a amessage header featuring a command (A_SYNC, A_OPEN, A_CLSE, A_WRTE, A_OKAY, ...) followed by a payload (find more documentation in protocol.txt. There is no A_READ command because an asocket is unidirectional. To model a bi-directional stream, asocket have a peer which go in the opposite direction.

An asocket features a buffer where the elemental unit is an apacket. If traffic is inbound, the buffer stores the apacket until it is consumed. If the traffic is oubound, the buffer stores apackets until they are sent down the wire (with A_WRTE commands).

+---------------------ASocket------------------------+
 |                                                   |
 | +----------------APacket Queue------------------+ |
 | |                                               | |
 | |            APacket     APacket     APacket    | |
 | |          +--------+  +--------+  +--------+   | |
 | |          |AMessage|  |AMessage|  |AMessage|   | |
 | |          +--------+  +--------+  +--------+   | |
 | |          |        |  |        |  |        |   | |
 | |  .....   |        |  |        |  |        |   | |
 | |          |  Data  |  |  Data  |  |  Data  |   | |
 | |          |        |  |        |  |        |   | |
 | |          |        |  |        |  |        |   | |
 | |          +--------+  +--------+  +--------+   | |
 | |                                               | |
 | +-----------------------------------------------+ |
 +---------------------------------------------------+

This system allows to multiplex data streams on an unique byte stream. Without entering too much into details, the amessage fields arg1 and arg2 are used alike in the TCP protocol where local and remote ports identify an unique stream. Note that unlike TCP which feature an "unacknowledged-send window", an apacket is sent only after the previous one has been confirmed to be received.

The two types of asocket (Remote and Local) differentiate between outbound and inbound traffic.

adbd <-> APPPLICATION communication

This pipeline is detailed in daemon/jdwp_service.cpp with ASCII drawings! The JDWP extension implemented by Dalvik/ART are documented in:

  • platform/dalvik/+/master/docs/debugmon.html
  • platform/dalvik/+/master/docs/debugger.html