This article will take the "ADB Connect" and "adb shell" two commands as an example to resolve the ADB command implementation process.
First, the ADB connect command execution process
currently ADB supports two forms of connection between host side and device side: USB and network. The purpose of the ADB Connect command is to establish a TCP/IP connection between the host and the device.
1. Client Side
adb_commnadline (), adb_query (), Adb_connect ("host:connect:xx.xx.xx.xx") _adb_connect ("host:connect:xx.xx.xx.xx"), Writex (FD, ...), Where FD is the service descriptor that listens on port 5037 .
2. Service Side
client End data sent- > Local_socket_event_func (), Smart_socket_enqueue (), Create_host_service_socket () -> host_service_to_socket () -> conncet_service () will establish a socket connection with xx.xx.xx.xx.:5555 and then create the socket by Register_socket_transport () listen.
ADB connect command processing is relatively straightforward and involves only the client and service side. Other complex commands are eventually handled by the server side, such as the ADB shell.
Second, the ADB shell command execution process (in network mode connection as an example)
1. Client Side
Adb_commandline (), Interactive_shell (), Adb_connect ("Shell:"), _adb_connect ("Shell:")
- > socket_loopback_client (),
This establishes a socket connection (via the local 5037 port) with the local service and begins sending data to the service side.
2. Service Side
in the previous <<android ADB implementation parsing >>, the service-side code Execution Install_listener (local_name, "*smartsocket*", NULL, 0) after that, the local 5037 port is started listening, and the processing function is set to Ss_listener_event_func (). Therefore, when the service side receives the data from the client, it will trigger Ss_listener_event_func (), which constructs a create_local_socket () and Connect_to_smartsocket in the function Asokcet (associated with Port 5037), this asocket is responsible for communicating with client side, through Fevent_install () setting the listener, callback handler function Local_socket_event_func (). Local_socket_event_func () corresponds to enter the Fde_read Processing section, first create a apacket, and then call S->peer->enqueue (), in the previous connect_to_smartsocket (), S->peer has pointed to the SS, so call the Ss->enqueue function, which is Smart_socket_enqueue (), if the received data is "shell", call Connect_to_remote (), then the service side will send to the server side The a_open command starts a series of communications.
3. Server Side
also in the previous <<android ADB implementation resolution >>, the server-side code executes Local_init () and starts listening on the local 5555 port. Therefore, When the data sent in the second step is received, Output_thread reads the data first, triggering transport_socket_events (), Handle_packet ( ), Create_local_service_socket (), SERVICE_TO_FD (), Create_subproc_thread (), Create_subproc_thread () will fork a process to execute exec/system/bin/sh. when this is done, the shell prompt appears on the client side, which is then sent through the socket to the server, and the server-side new fork threading, and the result is returned to the client side through the socket display.
Third, summary
through the analysis of the above two commands, and then combined with the previous <<android ADB implementation of the Analytic >>, the ADB command in the implementation process has a general understanding of the processing of other commands similar. But the entire ADB system is more complex, the two articles only explain a rough structure, many details no one by one analysis, follow-up if there is a chance, will be in Some detail part of further research.
ADB command Execution Process parsing