blob: e533a00eb284fa739c0a0085b9bc48394260c67e [file] [log] [blame]
Dan Albertb302d122015-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 Cui19bec5b2015-09-22 15:52:57 -070017#define TRACE_TAG ADB
Dan Albertdb6fe642015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20#include "adb_client.h"
21
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080022#include <errno.h>
23#include <limits.h>
24#include <stdarg.h>
Dan Albertb302d122015-02-24 15:51:19 -080025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080028#include <sys/stat.h>
Dan Albertb302d122015-02-24 15:51:19 -080029#include <sys/types.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080030
Josh Gao1e3bf732017-05-03 22:37:10 -070031#include <condition_variable>
32#include <mutex>
Elliott Hughes949f2622015-04-27 14:20:17 -070033#include <string>
Elliott Hughes73925982016-11-15 12:37:32 -080034#include <thread>
Elliott Hughes04a98c22015-04-29 08:35:59 -070035#include <vector>
36
Elliott Hughesf55ead92015-12-04 22:00:26 -080037#include <android-base/stringprintf.h>
38#include <android-base/strings.h>
Josh Gao1e3bf732017-05-03 22:37:10 -070039#include <android-base/thread_annotations.h>
Elliott Hughes43df1092015-07-23 17:12:58 -070040#include <cutils/sockets.h>
Elliott Hughes949f2622015-04-27 14:20:17 -070041
Dan Albert66a91b02015-02-24 21:26:58 -080042#include "adb_io.h"
Elliott Hughes43df1092015-07-23 17:12:58 -070043#include "adb_utils.h"
Josh Gaobb4f8602016-08-25 16:00:22 -070044#include "socket_spec.h"
Josh Gao70267e42016-11-15 18:55:47 -080045#include "sysdeps/chrono.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080046
Elliott Hughes3aec2ba2015-05-05 13:10:43 -070047static TransportType __adb_transport = kTransportAny;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080048static const char* __adb_serial = NULL;
49
Josh Gaobb4f8602016-08-25 16:00:22 -070050static const char* __adb_server_socket_spec;
Stefan Hilzingerfb798d92010-04-19 12:21:12 +010051
Elliott Hughes7991ba22016-12-02 12:53:09 -080052void adb_set_transport(TransportType type, const char* serial) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080053 __adb_transport = type;
54 __adb_serial = serial;
55}
56
Josh Gaobb4f8602016-08-25 16:00:22 -070057void adb_set_socket_spec(const char* socket_spec) {
Elliott Hughes6d959d12016-09-01 20:48:45 -070058 if (__adb_server_socket_spec) {
59 LOG(FATAL) << "attempted to reinitialize adb_server_socket_spec " << socket_spec << " (was " << __adb_server_socket_spec << ")";
Josh Gaobb4f8602016-08-25 16:00:22 -070060 }
61 __adb_server_socket_spec = socket_spec;
Matt Gumbel411775c2012-11-14 10:16:17 -080062}
63
Elliott Hughes04a98c22015-04-29 08:35:59 -070064static int switch_socket_transport(int fd, std::string* error) {
Elliott Hughes949f2622015-04-27 14:20:17 -070065 std::string service;
66 if (__adb_serial) {
67 service += "host:transport:";
68 service += __adb_serial;
69 } else {
Dan Albertf30d73c2015-02-25 17:51:28 -080070 const char* transport_type = "???";
Elliott Hughes949f2622015-04-27 14:20:17 -070071 switch (__adb_transport) {
72 case kTransportUsb:
73 transport_type = "transport-usb";
74 break;
75 case kTransportLocal:
76 transport_type = "transport-local";
77 break;
78 case kTransportAny:
79 transport_type = "transport-any";
80 break;
81 case kTransportHost:
82 // no switch necessary
83 return 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080084 }
Elliott Hughes949f2622015-04-27 14:20:17 -070085 service += "host:";
86 service += transport_type;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080087 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080088
Elliott Hughes88b4c852015-04-30 17:32:03 -070089 if (!SendProtocolString(fd, service)) {
Elliott Hughes04a98c22015-04-29 08:35:59 -070090 *error = perror_str("write failure during connection");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080091 adb_close(fd);
92 return -1;
93 }
Yabin Cui815ad882015-09-02 17:44:28 -070094 D("Switch transport in progress");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080095
Elliott Hughes04a98c22015-04-29 08:35:59 -070096 if (!adb_status(fd, error)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080097 adb_close(fd);
Yabin Cui815ad882015-09-02 17:44:28 -070098 D("Switch transport failed: %s", error->c_str());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080099 return -1;
100 }
Yabin Cui815ad882015-09-02 17:44:28 -0700101 D("Switch transport success");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800102 return 0;
103}
104
Elliott Hughes04a98c22015-04-29 08:35:59 -0700105bool adb_status(int fd, std::string* error) {
106 char buf[5];
Elliott Hughes04a98c22015-04-29 08:35:59 -0700107 if (!ReadFdExactly(fd, buf, 4)) {
108 *error = perror_str("protocol fault (couldn't read status)");
109 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800110 }
111
Elliott Hughes04a98c22015-04-29 08:35:59 -0700112 if (!memcmp(buf, "OKAY", 4)) {
113 return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800114 }
115
Elliott Hughes04a98c22015-04-29 08:35:59 -0700116 if (memcmp(buf, "FAIL", 4)) {
117 *error = android::base::StringPrintf("protocol fault (status %02x %02x %02x %02x?!)",
118 buf[0], buf[1], buf[2], buf[3]);
119 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800120 }
121
Elliott Hughesda945812015-04-29 12:28:13 -0700122 ReadProtocolString(fd, error, error);
Elliott Hughes04a98c22015-04-29 08:35:59 -0700123 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800124}
125
Josh Gaoee356a72017-05-08 18:37:17 -0700126static int _adb_connect(const std::string& service, std::string* error) {
Yabin Cui815ad882015-09-02 17:44:28 -0700127 D("_adb_connect: %s", service.c_str());
Josh Gao05012022017-06-16 15:34:34 -0700128 if (service.empty() || service.size() > MAX_PAYLOAD) {
Spencer Low803451e2015-05-13 00:02:55 -0700129 *error = android::base::StringPrintf("bad service name length (%zd)",
130 service.size());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800131 return -1;
132 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800133
Spencer Low753d4852015-07-30 23:07:55 -0700134 std::string reason;
Josh Gaobb4f8602016-08-25 16:00:22 -0700135 int fd = socket_spec_connect(__adb_server_socket_spec, &reason);
136 if (fd < 0) {
137 *error = android::base::StringPrintf("cannot connect to daemon at %s: %s",
138 __adb_server_socket_spec, reason.c_str());
139 return -2;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800140 }
141
Yabin Cui3cf1b362017-03-10 16:01:01 -0800142 if (memcmp(&service[0], "host", 4) != 0 && switch_socket_transport(fd, error)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800143 return -1;
144 }
145
Elliott Hughesb628cb12015-08-03 10:38:08 -0700146 if (!SendProtocolString(fd, service)) {
Elliott Hughes04a98c22015-04-29 08:35:59 -0700147 *error = perror_str("write failure during connection");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800148 adb_close(fd);
149 return -1;
150 }
151
Yabin Cui3cf1b362017-03-10 16:01:01 -0800152 if (!adb_status(fd, error)) {
153 adb_close(fd);
154 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800155 }
156
Yabin Cui815ad882015-09-02 17:44:28 -0700157 D("_adb_connect: return fd %d", fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800158 return fd;
159}
160
Josh Gaoee356a72017-05-08 18:37:17 -0700161bool adb_kill_server() {
162 D("adb_kill_server");
163 std::string reason;
164 int fd = socket_spec_connect(__adb_server_socket_spec, &reason);
165 if (fd < 0) {
166 fprintf(stderr, "cannot connect to daemon at %s: %s\n", __adb_server_socket_spec,
167 reason.c_str());
168 return true;
169 }
170
171 if (!SendProtocolString(fd, "host:kill")) {
172 fprintf(stderr, "error: write failure during connection: %s\n", strerror(errno));
173 return false;
174 }
175
176 ReadOrderlyShutdown(fd);
177 return true;
178}
179
Elliott Hughesda945812015-04-29 12:28:13 -0700180int adb_connect(const std::string& service, std::string* error) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800181 // first query the adb server's version
Elliott Hughes04a98c22015-04-29 08:35:59 -0700182 int fd = _adb_connect("host:version", error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800183
Yabin Cui815ad882015-09-02 17:44:28 -0700184 D("adb_connect: service %s", service.c_str());
Josh Gaobb4f8602016-08-25 16:00:22 -0700185 if (fd == -2 && !is_local_socket_spec(__adb_server_socket_spec)) {
Elliott Hughes65bc2272017-04-18 14:34:16 -0700186 fprintf(stderr, "* cannot start server on remote host\n");
Spencer Low85ee64b2015-08-11 17:05:02 -0700187 // error is the original network connection error
Matt Gumbel411775c2012-11-14 10:16:17 -0800188 return fd;
Elliott Hughes04a98c22015-04-29 08:35:59 -0700189 } else if (fd == -2) {
Elliott Hughes65bc2272017-04-18 14:34:16 -0700190 fprintf(stderr, "* daemon not running; starting now at %s\n", __adb_server_socket_spec);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800191 start_server:
Josh Gaobb4f8602016-08-25 16:00:22 -0700192 if (launch_server(__adb_server_socket_spec)) {
Elliott Hughes65bc2272017-04-18 14:34:16 -0700193 fprintf(stderr, "* failed to start daemon\n");
Spencer Low85ee64b2015-08-11 17:05:02 -0700194 // launch_server() has already printed detailed error info, so just
195 // return a generic error string about the overall adb_connect()
196 // that the caller requested.
197 *error = "cannot connect to daemon";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800198 return -1;
199 } else {
Elliott Hughes65bc2272017-04-18 14:34:16 -0700200 fprintf(stderr, "* daemon started successfully\n");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800201 }
Josh Gao1e3bf732017-05-03 22:37:10 -0700202 // The server will wait until it detects all of its connected devices before acking.
203 // Fall through to _adb_connect.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800204 } else {
Elliott Hughesa1452ab2015-10-02 19:49:10 -0700205 // If a server is already running, check its version matches.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800206 int version = ADB_SERVER_VERSION - 1;
207
Elliott Hughesa1452ab2015-10-02 19:49:10 -0700208 // If we have a file descriptor, then parse version result.
Elliott Hughes04a98c22015-04-29 08:35:59 -0700209 if (fd >= 0) {
Elliott Hughesda945812015-04-29 12:28:13 -0700210 std::string version_string;
211 if (!ReadProtocolString(fd, &version_string, error)) {
Elliott Hughesa1452ab2015-10-02 19:49:10 -0700212 adb_close(fd);
213 return -1;
Elliott Hughesda945812015-04-29 12:28:13 -0700214 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800215
Spencer Lowcc4a4b12015-10-14 17:32:44 -0700216 ReadOrderlyShutdown(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800217 adb_close(fd);
218
Elliott Hughesda945812015-04-29 12:28:13 -0700219 if (sscanf(&version_string[0], "%04x", &version) != 1) {
Elliott Hughes67943d12015-10-07 14:55:10 -0700220 *error = android::base::StringPrintf("cannot parse version string: %s",
221 version_string.c_str());
Spencer Low85ee64b2015-08-11 17:05:02 -0700222 return -1;
Elliott Hughesda945812015-04-29 12:28:13 -0700223 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800224 } else {
Elliott Hughesa1452ab2015-10-02 19:49:10 -0700225 // If fd is -1 check for "unknown host service" which would
226 // indicate a version of adb that does not support the
Spencer Low2218c752015-08-05 19:26:50 -0700227 // version command, in which case we should fall-through to kill it.
228 if (*error != "unknown host service") {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800229 return fd;
Elliott Hughes04a98c22015-04-29 08:35:59 -0700230 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800231 }
232
Elliott Hughesda945812015-04-29 12:28:13 -0700233 if (version != ADB_SERVER_VERSION) {
Elliott Hughes65bc2272017-04-18 14:34:16 -0700234 fprintf(stderr, "adb server version (%d) doesn't match this client (%d); killing...\n",
235 version, ADB_SERVER_VERSION);
Josh Gaoee356a72017-05-08 18:37:17 -0700236 adb_kill_server();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800237 goto start_server;
238 }
239 }
240
241 // if the command is start-server, we are done.
Elliott Hughesda945812015-04-29 12:28:13 -0700242 if (service == "host:start-server") {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800243 return 0;
Elliott Hughes04a98c22015-04-29 08:35:59 -0700244 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800245
Elliott Hughes04a98c22015-04-29 08:35:59 -0700246 fd = _adb_connect(service, error);
247 if (fd == -1) {
Yabin Cui815ad882015-09-02 17:44:28 -0700248 D("_adb_connect error: %s", error->c_str());
Brian Carlstromcad81322013-10-18 13:58:48 -0700249 } else if(fd == -2) {
Elliott Hughes65bc2272017-04-18 14:34:16 -0700250 fprintf(stderr, "* daemon still not running\n");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800251 }
Yabin Cui815ad882015-09-02 17:44:28 -0700252 D("adb_connect: return fd %d", fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800253
254 return fd;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800255}
256
257
Elliott Hughesd98ca8a2015-05-29 17:55:19 -0700258bool adb_command(const std::string& service) {
259 std::string error;
260 int fd = adb_connect(service, &error);
Elliott Hughes04a98c22015-04-29 08:35:59 -0700261 if (fd < 0) {
Elliott Hughesd98ca8a2015-05-29 17:55:19 -0700262 fprintf(stderr, "error: %s\n", error.c_str());
263 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800264 }
265
Elliott Hughesd98ca8a2015-05-29 17:55:19 -0700266 if (!adb_status(fd, &error)) {
267 fprintf(stderr, "error: %s\n", error.c_str());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800268 adb_close(fd);
Elliott Hughesd98ca8a2015-05-29 17:55:19 -0700269 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800270 }
271
Spencer Lowcc4a4b12015-10-14 17:32:44 -0700272 ReadOrderlyShutdown(fd);
273 adb_close(fd);
Elliott Hughesd98ca8a2015-05-29 17:55:19 -0700274 return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800275}
276
Elliott Hughesda945812015-04-29 12:28:13 -0700277bool adb_query(const std::string& service, std::string* result, std::string* error) {
Yabin Cui815ad882015-09-02 17:44:28 -0700278 D("adb_query: %s", service.c_str());
Elliott Hughes04a98c22015-04-29 08:35:59 -0700279 int fd = adb_connect(service, error);
280 if (fd < 0) {
Elliott Hughes09ccf1f2015-07-18 12:21:30 -0700281 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800282 }
283
Elliott Hughesda945812015-04-29 12:28:13 -0700284 result->clear();
285 if (!ReadProtocolString(fd, result, error)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800286 adb_close(fd);
Elliott Hughesda945812015-04-29 12:28:13 -0700287 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800288 }
Spencer Lowcc4a4b12015-10-14 17:32:44 -0700289
290 ReadOrderlyShutdown(fd);
291 adb_close(fd);
Elliott Hughesda945812015-04-29 12:28:13 -0700292 return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800293}
Josh Gao91d53b52016-01-31 19:12:26 -0800294
295std::string format_host_command(const char* command, TransportType type, const char* serial) {
296 if (serial) {
297 return android::base::StringPrintf("host-serial:%s:%s", serial, command);
298 }
299
300 const char* prefix = "host";
301 if (type == kTransportUsb) {
302 prefix = "host-usb";
303 } else if (type == kTransportLocal) {
304 prefix = "host-local";
305 }
306 return android::base::StringPrintf("%s:%s", prefix, command);
307}
308
309bool adb_get_feature_set(FeatureSet* feature_set, std::string* error) {
310 std::string result;
311 if (adb_query(format_host_command("features", __adb_transport, __adb_serial), &result, error)) {
312 *feature_set = StringToFeatureSet(result);
313 return true;
314 }
315 feature_set->clear();
316 return false;
317}