blob: 849a6e77244c80b85bdfc1d37fe3e60ffad2ad58 [file] [log] [blame]
Dan Albert76649012015-02-24 15:51:19 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yabin Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG ADB
Dan Albert33134262015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20#include "adb_client.h"
21
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022#include <errno.h>
Josh Gaob122b172017-08-16 16:57:01 -070023#include <inttypes.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024#include <limits.h>
25#include <stdarg.h>
Dan Albert76649012015-02-24 15:51:19 -080026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029#include <sys/stat.h>
Dan Albert76649012015-02-24 15:51:19 -080030#include <sys/types.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031
Josh Gaofd713e52017-05-03 22:37:10 -070032#include <condition_variable>
33#include <mutex>
Elliott Hughes9309ecb2015-04-27 14:20:17 -070034#include <string>
Elliott Hughesdbe91ee2016-11-15 12:37:32 -080035#include <thread>
Elliott Hughes078f0fc2015-04-29 08:35:59 -070036#include <vector>
37
Elliott Hughes4f713192015-12-04 22:00:26 -080038#include <android-base/stringprintf.h>
39#include <android-base/strings.h>
Josh Gaofd713e52017-05-03 22:37:10 -070040#include <android-base/thread_annotations.h>
Elliott Hughes381cfa92015-07-23 17:12:58 -070041#include <cutils/sockets.h>
Elliott Hughes9309ecb2015-04-27 14:20:17 -070042
Dan Albertcc731cc2015-02-24 21:26:58 -080043#include "adb_io.h"
Elliott Hughes381cfa92015-07-23 17:12:58 -070044#include "adb_utils.h"
Josh Gao9c869b52016-08-25 16:00:22 -070045#include "socket_spec.h"
Josh Gao4602adb2016-11-15 18:55:47 -080046#include "sysdeps/chrono.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047
Elliott Hughes3bd73c12015-05-05 13:10:43 -070048static TransportType __adb_transport = kTransportAny;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049static const char* __adb_serial = NULL;
Josh Gaob122b172017-08-16 16:57:01 -070050static TransportId __adb_transport_id = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051
Josh Gao9c869b52016-08-25 16:00:22 -070052static const char* __adb_server_socket_spec;
Stefan Hilzingerd0eacb82010-04-19 12:21:12 +010053
Josh Gaob122b172017-08-16 16:57:01 -070054void adb_set_transport(TransportType type, const char* serial, TransportId transport_id) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055 __adb_transport = type;
56 __adb_serial = serial;
Josh Gaob122b172017-08-16 16:57:01 -070057 __adb_transport_id = transport_id;
58}
59
60void adb_get_transport(TransportType* type, const char** serial, TransportId* transport_id) {
61 if (type) *type = __adb_transport;
62 if (serial) *serial = __adb_serial;
63 if (transport_id) *transport_id = __adb_transport_id;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064}
65
Josh Gao9c869b52016-08-25 16:00:22 -070066void adb_set_socket_spec(const char* socket_spec) {
Elliott Hughes7f4ab762016-09-01 20:48:45 -070067 if (__adb_server_socket_spec) {
68 LOG(FATAL) << "attempted to reinitialize adb_server_socket_spec " << socket_spec << " (was " << __adb_server_socket_spec << ")";
Josh Gao9c869b52016-08-25 16:00:22 -070069 }
70 __adb_server_socket_spec = socket_spec;
Matt Gumbeld7b33082012-11-14 10:16:17 -080071}
72
Elliott Hughes078f0fc2015-04-29 08:35:59 -070073static int switch_socket_transport(int fd, std::string* error) {
Elliott Hughes9309ecb2015-04-27 14:20:17 -070074 std::string service;
Josh Gaob122b172017-08-16 16:57:01 -070075 if (__adb_transport_id) {
76 service += "host:transport-id:";
77 service += std::to_string(__adb_transport_id);
78 } else if (__adb_serial) {
Elliott Hughes9309ecb2015-04-27 14:20:17 -070079 service += "host:transport:";
80 service += __adb_serial;
81 } else {
Dan Albertbac34742015-02-25 17:51:28 -080082 const char* transport_type = "???";
Elliott Hughes9309ecb2015-04-27 14:20:17 -070083 switch (__adb_transport) {
84 case kTransportUsb:
85 transport_type = "transport-usb";
86 break;
87 case kTransportLocal:
88 transport_type = "transport-local";
89 break;
90 case kTransportAny:
91 transport_type = "transport-any";
92 break;
93 case kTransportHost:
94 // no switch necessary
95 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080096 }
Elliott Hughes9309ecb2015-04-27 14:20:17 -070097 service += "host:";
98 service += transport_type;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100
Elliott Hughese67f1f82015-04-30 17:32:03 -0700101 if (!SendProtocolString(fd, service)) {
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700102 *error = perror_str("write failure during connection");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103 adb_close(fd);
104 return -1;
105 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700106 D("Switch transport in progress");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800107
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700108 if (!adb_status(fd, error)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109 adb_close(fd);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700110 D("Switch transport failed: %s", error->c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800111 return -1;
112 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700113 D("Switch transport success");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800114 return 0;
115}
116
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700117bool adb_status(int fd, std::string* error) {
118 char buf[5];
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700119 if (!ReadFdExactly(fd, buf, 4)) {
120 *error = perror_str("protocol fault (couldn't read status)");
121 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122 }
123
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700124 if (!memcmp(buf, "OKAY", 4)) {
125 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126 }
127
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700128 if (memcmp(buf, "FAIL", 4)) {
129 *error = android::base::StringPrintf("protocol fault (status %02x %02x %02x %02x?!)",
130 buf[0], buf[1], buf[2], buf[3]);
131 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132 }
133
Elliott Hughes6452a892015-04-29 12:28:13 -0700134 ReadProtocolString(fd, error, error);
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700135 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136}
137
Josh Gaob3c14ec2017-05-08 18:37:17 -0700138static int _adb_connect(const std::string& service, std::string* error) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700139 D("_adb_connect: %s", service.c_str());
Josh Gaoa019f782017-06-16 15:34:34 -0700140 if (service.empty() || service.size() > MAX_PAYLOAD) {
Spencer Low6001c872015-05-13 00:02:55 -0700141 *error = android::base::StringPrintf("bad service name length (%zd)",
142 service.size());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143 return -1;
144 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145
Spencer Low5200c662015-07-30 23:07:55 -0700146 std::string reason;
Josh Gao9c869b52016-08-25 16:00:22 -0700147 int fd = socket_spec_connect(__adb_server_socket_spec, &reason);
148 if (fd < 0) {
149 *error = android::base::StringPrintf("cannot connect to daemon at %s: %s",
150 __adb_server_socket_spec, reason.c_str());
151 return -2;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152 }
153
Yabin Cuib5e11412017-03-10 16:01:01 -0800154 if (memcmp(&service[0], "host", 4) != 0 && switch_socket_transport(fd, error)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155 return -1;
156 }
157
Elliott Hughesaa245492015-08-03 10:38:08 -0700158 if (!SendProtocolString(fd, service)) {
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700159 *error = perror_str("write failure during connection");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160 adb_close(fd);
161 return -1;
162 }
163
Yabin Cuib5e11412017-03-10 16:01:01 -0800164 if (!adb_status(fd, error)) {
165 adb_close(fd);
166 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167 }
168
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700169 D("_adb_connect: return fd %d", fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170 return fd;
171}
172
Josh Gaob3c14ec2017-05-08 18:37:17 -0700173bool adb_kill_server() {
174 D("adb_kill_server");
175 std::string reason;
176 int fd = socket_spec_connect(__adb_server_socket_spec, &reason);
177 if (fd < 0) {
178 fprintf(stderr, "cannot connect to daemon at %s: %s\n", __adb_server_socket_spec,
179 reason.c_str());
180 return true;
181 }
182
183 if (!SendProtocolString(fd, "host:kill")) {
184 fprintf(stderr, "error: write failure during connection: %s\n", strerror(errno));
185 return false;
186 }
187
188 ReadOrderlyShutdown(fd);
189 return true;
190}
191
Elliott Hughes6452a892015-04-29 12:28:13 -0700192int adb_connect(const std::string& service, std::string* error) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193 // first query the adb server's version
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700194 int fd = _adb_connect("host:version", error);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700196 D("adb_connect: service %s", service.c_str());
Josh Gao9c869b52016-08-25 16:00:22 -0700197 if (fd == -2 && !is_local_socket_spec(__adb_server_socket_spec)) {
Elliott Hughes1fc8f6e2017-04-18 14:34:16 -0700198 fprintf(stderr, "* cannot start server on remote host\n");
Spencer Lowf18fc082015-08-11 17:05:02 -0700199 // error is the original network connection error
Matt Gumbeld7b33082012-11-14 10:16:17 -0800200 return fd;
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700201 } else if (fd == -2) {
Elliott Hughes1fc8f6e2017-04-18 14:34:16 -0700202 fprintf(stderr, "* daemon not running; starting now at %s\n", __adb_server_socket_spec);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203 start_server:
Josh Gao9c869b52016-08-25 16:00:22 -0700204 if (launch_server(__adb_server_socket_spec)) {
Elliott Hughes1fc8f6e2017-04-18 14:34:16 -0700205 fprintf(stderr, "* failed to start daemon\n");
Spencer Lowf18fc082015-08-11 17:05:02 -0700206 // launch_server() has already printed detailed error info, so just
207 // return a generic error string about the overall adb_connect()
208 // that the caller requested.
209 *error = "cannot connect to daemon";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210 return -1;
211 } else {
Elliott Hughes1fc8f6e2017-04-18 14:34:16 -0700212 fprintf(stderr, "* daemon started successfully\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213 }
Josh Gaofd713e52017-05-03 22:37:10 -0700214 // The server will wait until it detects all of its connected devices before acking.
215 // Fall through to _adb_connect.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216 } else {
Elliott Hughes5b73a102015-10-02 19:49:10 -0700217 // If a server is already running, check its version matches.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800218 int version = ADB_SERVER_VERSION - 1;
219
Elliott Hughes5b73a102015-10-02 19:49:10 -0700220 // If we have a file descriptor, then parse version result.
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700221 if (fd >= 0) {
Elliott Hughes6452a892015-04-29 12:28:13 -0700222 std::string version_string;
223 if (!ReadProtocolString(fd, &version_string, error)) {
Elliott Hughes5b73a102015-10-02 19:49:10 -0700224 adb_close(fd);
225 return -1;
Elliott Hughes6452a892015-04-29 12:28:13 -0700226 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227
Spencer Low351ecd12015-10-14 17:32:44 -0700228 ReadOrderlyShutdown(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229 adb_close(fd);
230
Elliott Hughes6452a892015-04-29 12:28:13 -0700231 if (sscanf(&version_string[0], "%04x", &version) != 1) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700232 *error = android::base::StringPrintf("cannot parse version string: %s",
233 version_string.c_str());
Spencer Lowf18fc082015-08-11 17:05:02 -0700234 return -1;
Elliott Hughes6452a892015-04-29 12:28:13 -0700235 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236 } else {
Elliott Hughes5b73a102015-10-02 19:49:10 -0700237 // If fd is -1 check for "unknown host service" which would
238 // indicate a version of adb that does not support the
Spencer Low71635bb2015-08-05 19:26:50 -0700239 // version command, in which case we should fall-through to kill it.
240 if (*error != "unknown host service") {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800241 return fd;
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700242 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243 }
244
Elliott Hughes6452a892015-04-29 12:28:13 -0700245 if (version != ADB_SERVER_VERSION) {
Elliott Hughes1fc8f6e2017-04-18 14:34:16 -0700246 fprintf(stderr, "adb server version (%d) doesn't match this client (%d); killing...\n",
247 version, ADB_SERVER_VERSION);
Josh Gaob3c14ec2017-05-08 18:37:17 -0700248 adb_kill_server();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249 goto start_server;
250 }
251 }
252
253 // if the command is start-server, we are done.
Elliott Hughes6452a892015-04-29 12:28:13 -0700254 if (service == "host:start-server") {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800255 return 0;
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700256 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800257
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700258 fd = _adb_connect(service, error);
259 if (fd == -1) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700260 D("_adb_connect error: %s", error->c_str());
Brian Carlstrom93c91fa2013-10-18 13:58:48 -0700261 } else if(fd == -2) {
Elliott Hughes1fc8f6e2017-04-18 14:34:16 -0700262 fprintf(stderr, "* daemon still not running\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800263 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700264 D("adb_connect: return fd %d", fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265
266 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800267}
268
269
Elliott Hughes424af022015-05-29 17:55:19 -0700270bool adb_command(const std::string& service) {
271 std::string error;
272 int fd = adb_connect(service, &error);
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700273 if (fd < 0) {
Elliott Hughes424af022015-05-29 17:55:19 -0700274 fprintf(stderr, "error: %s\n", error.c_str());
275 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800276 }
277
Elliott Hughes424af022015-05-29 17:55:19 -0700278 if (!adb_status(fd, &error)) {
279 fprintf(stderr, "error: %s\n", error.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800280 adb_close(fd);
Elliott Hughes424af022015-05-29 17:55:19 -0700281 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800282 }
283
Spencer Low351ecd12015-10-14 17:32:44 -0700284 ReadOrderlyShutdown(fd);
285 adb_close(fd);
Elliott Hughes424af022015-05-29 17:55:19 -0700286 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800287}
288
Elliott Hughes6452a892015-04-29 12:28:13 -0700289bool adb_query(const std::string& service, std::string* result, std::string* error) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700290 D("adb_query: %s", service.c_str());
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700291 int fd = adb_connect(service, error);
292 if (fd < 0) {
Elliott Hughes3d5f60d2015-07-18 12:21:30 -0700293 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800294 }
295
Elliott Hughes6452a892015-04-29 12:28:13 -0700296 result->clear();
297 if (!ReadProtocolString(fd, result, error)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298 adb_close(fd);
Elliott Hughes6452a892015-04-29 12:28:13 -0700299 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300 }
Spencer Low351ecd12015-10-14 17:32:44 -0700301
302 ReadOrderlyShutdown(fd);
303 adb_close(fd);
Elliott Hughes6452a892015-04-29 12:28:13 -0700304 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305}
Josh Gaof3f6a1d2016-01-31 19:12:26 -0800306
Josh Gaob122b172017-08-16 16:57:01 -0700307std::string format_host_command(const char* command) {
308 if (__adb_transport_id) {
309 return android::base::StringPrintf("host-transport-id:%" PRIu64 ":%s", __adb_transport_id,
310 command);
311 } else if (__adb_serial) {
312 return android::base::StringPrintf("host-serial:%s:%s", __adb_serial, command);
Josh Gaof3f6a1d2016-01-31 19:12:26 -0800313 }
314
315 const char* prefix = "host";
Josh Gaob122b172017-08-16 16:57:01 -0700316 if (__adb_transport == kTransportUsb) {
Josh Gaof3f6a1d2016-01-31 19:12:26 -0800317 prefix = "host-usb";
Josh Gaob122b172017-08-16 16:57:01 -0700318 } else if (__adb_transport == kTransportLocal) {
Josh Gaof3f6a1d2016-01-31 19:12:26 -0800319 prefix = "host-local";
320 }
321 return android::base::StringPrintf("%s:%s", prefix, command);
322}
323
324bool adb_get_feature_set(FeatureSet* feature_set, std::string* error) {
325 std::string result;
Josh Gaob122b172017-08-16 16:57:01 -0700326 if (adb_query(format_host_command("features"), &result, error)) {
Josh Gaof3f6a1d2016-01-31 19:12:26 -0800327 *feature_set = StringToFeatureSet(result);
328 return true;
329 }
330 feature_set->clear();
331 return false;
332}