• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# How adbd starts
2
3The `adbd` service life cycle is managed by the [init](../../../../../system/core/init/README.md) process.
4The daemon will be started when the two following conditions are true.
51. The device is in developer mode.
62. Adb over USB or adb over Wifi are enabled.
7
8`init` itself doesn't have any special knowledge about adbd. Everything it needs to know comes from the various .rc files
9(telling it what to do when various properties are set) and the processes that set those properties.
10
11There are two main scenarios where init will start `adbd`. When the device boots and when a user runs a device into
12"developer mode".
13
14## When the device boots
15
16The behavior of `init` is controlled by `.rc` files, commands, and system properties.
17
18- The `adbd` service is described [here](https://cs.android.com/android/platform/superproject/main/+/main:packages/modules/adb/apex/adbd.rc;drc=a9b3987d2a42a40de0d67fcecb50c9716639ef03).
19- The [rc language](../../../../../system/core/init/README.md) tie together properties, commands, and services.
20
21When a device boots, the script init.usb.rc [checks](https://cs.android.com/android/platform/superproject/main/+/main:system/core/rootdir/init.usb.rc;l=109;drc=e34549af332e4be13a2ffb385455280d4736c1a9)
22if persistent property `persist.sys.usb.config` is set, in which case the values is copied into `sys.usb.config`.
23When this value is written, it [triggers](https://cs.android.com/android/platform/superproject/main/+/main:system/core/rootdir/init.usb.rc;l=47;drc=e34549af332e4be13a2ffb385455280d4736c1a9) `init` to run `start adbd`.
24
25## When the device is already booted
26
27When the device is up and running, it could be in "Developer Mode" but `adbd` service may not be running. It is only
28after the user toggles "Developer options" -> "USB debugging" or "Developer options" -> "Wireless debugging" via the GUI that `adbd` starts.
29
30Note that the previous description is valid for `user` builds. In the case of `userdebug` and `eng`, properties set
31at build-time, such as `ro.adb.secure` or `persist.sys.usb.config`, will automate adbd starting up and disable authentication.
32
33Four layers are involved.
34
351. GUI USB / GUI Wireless
362. AdbSettingsObserver
372. AdbService
383. init process
39
40
41### GUI (USB)
42
431. The confirmation dialog is displayed from [AdbPreferenceController.showConfirmationDialog](https://cs.android.com/android/platform/superproject/main/+/main:packages/apps/Settings/src/com/android/settings/development/AdbPreferenceController.java;l=48;drc=1b8c0fdfdb9a36f691402513258b26036c41667f).
442. Validation is performed in [AdbPreferenceController.writeAdbSettings](https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/packages/SettingsLib/src/com/android/settingslib/development/AbstractEnableAdbPreferenceController.java;l=133;drc=aaea2d2266d29b3881f452899b79fb9e71525c3b) once the dialog is validated by user
453. `Settings.Global.ADB_ENABLED` is set.
46
47```
48Settings.Global.putInt(mContext.getContentResolver(),
49Settings.Global.ADB_ENABLED, enabled ? ADB_SETTING_ON : ADB_SETTING_OFF);
50```
51
52### GUI (Wireless)
53In the case of "Wireless debugging" toggle, the same kind of interaction leads to `ADB_WIFI_ENABLED` being set.
54
55```
56Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.ADB_WIFI_ENABLED , 1);
57```
58### AdbSettingsObserver
591. Both `ADB_ENABLED` and `ADB_WIFI_ENABLED` are monitored by [AdbSettingsObserver](https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/services/core/java/com/android/server/adb/AdbService.java;l=208;drc=6474abd265cae9ccbe4e5d9ad37959215dcf564b).
60
612. When a change is detected, the Observers calls [AdbService::setAdbEnabled](https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/services/core/java/com/android/server/adb/AdbService.java;l=213;drc=6474abd265cae9ccbe4e5d9ad37959215dcf564b).
62
63### AdbService
64
651. [AdbService.startAdbd](https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/services/core/java/com/android/server/adb/AdbService.java;l=480;drc=6474abd265cae9ccbe4e5d9ad37959215dcf564b) is called. This talks to the `init` process by setting `ctl.start` or `ctl.stop` to "adbd".
66This step is equivalent to `.rc` files `start adbd` and `stop adbd`.
67
68### USBDeviceManager (USB only)
69
70If USB is involved (as opposed to ADB Wifi), ([USBDeviceManager.onAdbEnabled](https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/services/usb/java/com/android/server/usb/UsbDeviceManager.java;l=1090;drc=e36f88c420fe00112e11e85634851d047c0b623e)
71) is called to recompose the gadget functions. As a side effect, persistent property `persist.sys.usb.config`
72is set so `init` will automatically start `adbd` service on the next device start.
73
741. `MSG_ENABLE_ADB` message is sent from [onAdbEnabled](https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/services/usb/java/com/android/server/usb/UsbDeviceManager.java;l=1090;drc=6474abd265cae9ccbe4e5d9ad37959215dcf564b).
75
762. In [UsbDeviceManager.setAdbEnabled](https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/services/usb/java/com/android/server/usb/UsbDeviceManager.java;l=780;drc=6474abd265cae9ccbe4e5d9ad37959215dcf564b) property `persist.sys.usb.config` is set.
77
783. The manager needs to recompose the functions into a gadget.
79    1. [UsbDeviceManager.setEnabledFunctions](https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/services/usb/java/com/android/server/usb/UsbDeviceManager.java;l=2422;drc=6474abd265cae9ccbe4e5d9ad37959215dcf564b).
80    2. [UsbDeviceManager.setUsbConfig()](https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/services/usb/java/com/android/server/usb/UsbDeviceManager.java;l=2376;drc=6474abd265cae9ccbe4e5d9ad37959215dcf564b).
81
82### init
83
84`init` [monitors](https://cs.android.com/android/platform/superproject/main/+/main:system/core/init/property_service.cpp;l=551;drc=8067bd819f42be5512cdab8aaa3b0e9b4dba2369)
85properties `ctl.start` and `ctl.stop` and interprets changes
86as requests to start/stop a service. See `init` built-in commands (such as `start` and `stop`) [here](https://cs.android.com/android/platform/superproject/main/+/main:system/core/init/builtins.cpp;l=1334;drc=6474abd265cae9ccbe4e5d9ad37959215dcf564b).
87
88To let other systems observe services' lifecycle, `init` [sets properties](https://cs.android.com/android/platform/superproject/main/+/main:system/core/init/service.cpp;l=179;drc=6474abd265cae9ccbe4e5d9ad37959215dcf564b) with known prefixes.
89- Lifecycle: `init.svc.SERVICE_NAME` (`init.svc.adbd`), which can be set to "running", "stopped", "stopping" (see [Service::NotifyStateChange](https://cs.android.com/android/platform/superproject/main/+/main:system/core/init/service.cpp;l=172;drc=6474abd265cae9ccbe4e5d9ad37959215dcf564b) for all possible values).
90- Bootime: `ro.boottime.SERVICE_NAME` (`ro.boottime.adbd`).
91