| Daniel Colascione | 3ec7be7 | 2019-11-13 17:49:37 -0800 | [diff] [blame] | 1 | adb can be configured to work with systemd-style socket activation, | 
 | 2 | allowing the daemon to start automatically when the adb control port | 
 | 3 | is forwarded across a network. You need two files, placed in the usual | 
 | 4 | systemd service directories (e.g., ~/.config/systemd/user for a user | 
 | 5 | service). | 
 | 6 |  | 
 | 7 | adb.service: | 
 | 8 |  | 
 | 9 | --- START adb.service CUT HERE --- | 
 | 10 | [Unit] | 
 | 11 | Description=adb | 
 | 12 | After=adb.socket | 
 | 13 | Requires=adb.socket | 
 | 14 | [Service] | 
 | 15 | Type=simple | 
 | 16 | # FD 3 is part of the systemd interface | 
 | 17 | ExecStart=/path/to/adb server nodaemon -L acceptfd:3 | 
 | 18 | --- END adb.service CUT HERE --- | 
 | 19 |  | 
 | 20 | --- START adb.socket CUT HERE --- | 
 | 21 | [Unit] | 
 | 22 | Description=adb | 
 | 23 | PartOf=adb.service | 
 | 24 | [Socket] | 
 | 25 | ListenStream=127.0.0.1:5037 | 
 | 26 | Accept=no | 
 | 27 | [Install] | 
 | 28 | WantedBy=sockets.target | 
 | 29 | --- END adb.socket CUT HERE --- | 
 | 30 |  | 
 | 31 | After installing the adb service, the adb server will be started | 
 | 32 | automatically on any connection to 127.0.0.1:5037 (the default adb | 
 | 33 | control port), even after adb kill-server kills the server. | 
 | 34 |  | 
 | 35 | Other "superserver" launcher systems (like macOS launchd) can be | 
 | 36 | configured analogously. The important part is that adb be started with | 
 | 37 | "server" and "nodaemon" command line arguments and that the listen | 
 | 38 | address (passed to -L) name a file descriptor that's ready to | 
 | 39 | accept(2) connections and that's already bound to the desired address | 
 | 40 | and listening. inetd-style pre-accepted sockets do _not_ work in this | 
 | 41 | configuration: the file descriptor passed to acceptfd must be the | 
 | 42 | serve socket, not the accepted connection socket. |