blob: ff9b32a1323831fbb4acab81054d522db34a5093 [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
Elliott Hughes949f2622015-04-27 14:20:17 -070031#include <string>
Elliott Hughes73925982016-11-15 12:37:32 -080032#include <thread>
Elliott Hughes04a98c22015-04-29 08:35:59 -070033#include <vector>
34
Elliott Hughesf55ead92015-12-04 22:00:26 -080035#include <android-base/stringprintf.h>
36#include <android-base/strings.h>
Elliott Hughes43df1092015-07-23 17:12:58 -070037#include <cutils/sockets.h>
Elliott Hughes949f2622015-04-27 14:20:17 -070038
Dan Albert66a91b02015-02-24 21:26:58 -080039#include "adb_io.h"
Elliott Hughes43df1092015-07-23 17:12:58 -070040#include "adb_utils.h"
Josh Gaobb4f8602016-08-25 16:00:22 -070041#include "socket_spec.h"
Josh Gao70267e42016-11-15 18:55:47 -080042#include "sysdeps/chrono.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080043
Elliott Hughes3aec2ba2015-05-05 13:10:43 -070044static TransportType __adb_transport = kTransportAny;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080045static const char* __adb_serial = NULL;
46
Josh Gaobb4f8602016-08-25 16:00:22 -070047static const char* __adb_server_socket_spec;
Stefan Hilzingerfb798d92010-04-19 12:21:12 +010048
Elliott Hughes7991ba22016-12-02 12:53:09 -080049void adb_set_transport(TransportType type, const char* serial) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080050 __adb_transport = type;
51 __adb_serial = serial;
52}
53
Josh Gaobb4f8602016-08-25 16:00:22 -070054void adb_set_socket_spec(const char* socket_spec) {
Elliott Hughes6d959d12016-09-01 20:48:45 -070055 if (__adb_server_socket_spec) {
56 LOG(FATAL) << "attempted to reinitialize adb_server_socket_spec " << socket_spec << " (was " << __adb_server_socket_spec << ")";
Josh Gaobb4f8602016-08-25 16:00:22 -070057 }
58 __adb_server_socket_spec = socket_spec;
Matt Gumbel411775c2012-11-14 10:16:17 -080059}
60
Elliott Hughes04a98c22015-04-29 08:35:59 -070061static int switch_socket_transport(int fd, std::string* error) {
Elliott Hughes949f2622015-04-27 14:20:17 -070062 std::string service;
63 if (__adb_serial) {
64 service += "host:transport:";
65 service += __adb_serial;
66 } else {
Dan Albertf30d73c2015-02-25 17:51:28 -080067 const char* transport_type = "???";
Elliott Hughes949f2622015-04-27 14:20:17 -070068 switch (__adb_transport) {
69 case kTransportUsb:
70 transport_type = "transport-usb";
71 break;
72 case kTransportLocal:
73 transport_type = "transport-local";
74 break;
75 case kTransportAny:
76 transport_type = "transport-any";
77 break;
78 case kTransportHost:
79 // no switch necessary
80 return 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080081 }
Elliott Hughes949f2622015-04-27 14:20:17 -070082 service += "host:";
83 service += transport_type;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080084 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080085
Elliott Hughes88b4c852015-04-30 17:32:03 -070086 if (!SendProtocolString(fd, service)) {
Elliott Hughes04a98c22015-04-29 08:35:59 -070087 *error = perror_str("write failure during connection");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080088 adb_close(fd);
89 return -1;
90 }
Yabin Cui815ad882015-09-02 17:44:28 -070091 D("Switch transport in progress");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080092
Elliott Hughes04a98c22015-04-29 08:35:59 -070093 if (!adb_status(fd, error)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080094 adb_close(fd);
Yabin Cui815ad882015-09-02 17:44:28 -070095 D("Switch transport failed: %s", error->c_str());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080096 return -1;
97 }
Yabin Cui815ad882015-09-02 17:44:28 -070098 D("Switch transport success");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080099 return 0;
100}
101
Elliott Hughes04a98c22015-04-29 08:35:59 -0700102bool adb_status(int fd, std::string* error) {
103 char buf[5];
Elliott Hughes04a98c22015-04-29 08:35:59 -0700104 if (!ReadFdExactly(fd, buf, 4)) {
105 *error = perror_str("protocol fault (couldn't read status)");
106 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800107 }
108
Elliott Hughes04a98c22015-04-29 08:35:59 -0700109 if (!memcmp(buf, "OKAY", 4)) {
110 return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800111 }
112
Elliott Hughes04a98c22015-04-29 08:35:59 -0700113 if (memcmp(buf, "FAIL", 4)) {
114 *error = android::base::StringPrintf("protocol fault (status %02x %02x %02x %02x?!)",
115 buf[0], buf[1], buf[2], buf[3]);
116 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800117 }
118
Elliott Hughesda945812015-04-29 12:28:13 -0700119 ReadProtocolString(fd, error, error);
Elliott Hughes04a98c22015-04-29 08:35:59 -0700120 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800121}
122
Elliott Hughesda945812015-04-29 12:28:13 -0700123int _adb_connect(const std::string& service, std::string* error) {
Yabin Cui815ad882015-09-02 17:44:28 -0700124 D("_adb_connect: %s", service.c_str());
Josh Gao9055a582016-01-15 14:35:54 -0800125 if (service.empty() || service.size() > MAX_PAYLOAD_V1) {
Spencer Low803451e2015-05-13 00:02:55 -0700126 *error = android::base::StringPrintf("bad service name length (%zd)",
127 service.size());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800128 return -1;
129 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800130
Spencer Low753d4852015-07-30 23:07:55 -0700131 std::string reason;
Josh Gaobb4f8602016-08-25 16:00:22 -0700132 int fd = socket_spec_connect(__adb_server_socket_spec, &reason);
133 if (fd < 0) {
134 *error = android::base::StringPrintf("cannot connect to daemon at %s: %s",
135 __adb_server_socket_spec, reason.c_str());
136 return -2;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800137 }
138
Yabin Cui3cf1b362017-03-10 16:01:01 -0800139 if (memcmp(&service[0], "host", 4) != 0 && switch_socket_transport(fd, error)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800140 return -1;
141 }
142
Elliott Hughesb628cb12015-08-03 10:38:08 -0700143 if (!SendProtocolString(fd, service)) {
Elliott Hughes04a98c22015-04-29 08:35:59 -0700144 *error = perror_str("write failure during connection");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800145 adb_close(fd);
146 return -1;
147 }
148
Yabin Cui3cf1b362017-03-10 16:01:01 -0800149 if (!adb_status(fd, error)) {
150 adb_close(fd);
151 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800152 }
153
Yabin Cui815ad882015-09-02 17:44:28 -0700154 D("_adb_connect: return fd %d", fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800155 return fd;
156}
157
Elliott Hughesda945812015-04-29 12:28:13 -0700158int adb_connect(const std::string& service, std::string* error) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800159 // first query the adb server's version
Elliott Hughes04a98c22015-04-29 08:35:59 -0700160 int fd = _adb_connect("host:version", error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800161
Yabin Cui815ad882015-09-02 17:44:28 -0700162 D("adb_connect: service %s", service.c_str());
Josh Gaobb4f8602016-08-25 16:00:22 -0700163 if (fd == -2 && !is_local_socket_spec(__adb_server_socket_spec)) {
Matt Gumbel411775c2012-11-14 10:16:17 -0800164 fprintf(stderr,"** Cannot start server on remote host\n");
Spencer Low85ee64b2015-08-11 17:05:02 -0700165 // error is the original network connection error
Matt Gumbel411775c2012-11-14 10:16:17 -0800166 return fd;
Elliott Hughes04a98c22015-04-29 08:35:59 -0700167 } else if (fd == -2) {
Josh Gaobb4f8602016-08-25 16:00:22 -0700168 fprintf(stdout, "* daemon not running. starting it now at %s *\n", __adb_server_socket_spec);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800169 start_server:
Josh Gaobb4f8602016-08-25 16:00:22 -0700170 if (launch_server(__adb_server_socket_spec)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800171 fprintf(stderr,"* failed to start daemon *\n");
Spencer Low85ee64b2015-08-11 17:05:02 -0700172 // launch_server() has already printed detailed error info, so just
173 // return a generic error string about the overall adb_connect()
174 // that the caller requested.
175 *error = "cannot connect to daemon";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800176 return -1;
177 } else {
178 fprintf(stdout,"* daemon started successfully *\n");
179 }
Elliott Hughes73925982016-11-15 12:37:32 -0800180 // Give the server some time to start properly and detect devices.
Josh Gao70267e42016-11-15 18:55:47 -0800181 std::this_thread::sleep_for(3s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800182 // fall through to _adb_connect
183 } else {
Elliott Hughesa1452ab2015-10-02 19:49:10 -0700184 // If a server is already running, check its version matches.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800185 int version = ADB_SERVER_VERSION - 1;
186
Elliott Hughesa1452ab2015-10-02 19:49:10 -0700187 // If we have a file descriptor, then parse version result.
Elliott Hughes04a98c22015-04-29 08:35:59 -0700188 if (fd >= 0) {
Elliott Hughesda945812015-04-29 12:28:13 -0700189 std::string version_string;
190 if (!ReadProtocolString(fd, &version_string, error)) {
Elliott Hughesa1452ab2015-10-02 19:49:10 -0700191 adb_close(fd);
192 return -1;
Elliott Hughesda945812015-04-29 12:28:13 -0700193 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800194
Spencer Lowcc4a4b12015-10-14 17:32:44 -0700195 ReadOrderlyShutdown(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800196 adb_close(fd);
197
Elliott Hughesda945812015-04-29 12:28:13 -0700198 if (sscanf(&version_string[0], "%04x", &version) != 1) {
Elliott Hughes67943d12015-10-07 14:55:10 -0700199 *error = android::base::StringPrintf("cannot parse version string: %s",
200 version_string.c_str());
Spencer Low85ee64b2015-08-11 17:05:02 -0700201 return -1;
Elliott Hughesda945812015-04-29 12:28:13 -0700202 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800203 } else {
Elliott Hughesa1452ab2015-10-02 19:49:10 -0700204 // If fd is -1 check for "unknown host service" which would
205 // indicate a version of adb that does not support the
Spencer Low2218c752015-08-05 19:26:50 -0700206 // version command, in which case we should fall-through to kill it.
207 if (*error != "unknown host service") {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800208 return fd;
Elliott Hughes04a98c22015-04-29 08:35:59 -0700209 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800210 }
211
Elliott Hughesda945812015-04-29 12:28:13 -0700212 if (version != ADB_SERVER_VERSION) {
Elliott Hughesa1452ab2015-10-02 19:49:10 -0700213 printf("adb server version (%d) doesn't match this client (%d); killing...\n",
214 version, ADB_SERVER_VERSION);
Elliott Hughes04a98c22015-04-29 08:35:59 -0700215 fd = _adb_connect("host:kill", error);
Spencer Low85ee64b2015-08-11 17:05:02 -0700216 if (fd >= 0) {
Spencer Lowcc4a4b12015-10-14 17:32:44 -0700217 ReadOrderlyShutdown(fd);
Spencer Low85ee64b2015-08-11 17:05:02 -0700218 adb_close(fd);
219 } else {
220 // If we couldn't connect to the server or had some other error,
221 // report it, but still try to start the server.
222 fprintf(stderr, "error: %s\n", error->c_str());
223 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800224
225 /* XXX can we better detect its death? */
Josh Gao70267e42016-11-15 18:55:47 -0800226 std::this_thread::sleep_for(2s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800227 goto start_server;
228 }
229 }
230
231 // if the command is start-server, we are done.
Elliott Hughesda945812015-04-29 12:28:13 -0700232 if (service == "host:start-server") {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800233 return 0;
Elliott Hughes04a98c22015-04-29 08:35:59 -0700234 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800235
Elliott Hughes04a98c22015-04-29 08:35:59 -0700236 fd = _adb_connect(service, error);
237 if (fd == -1) {
Yabin Cui815ad882015-09-02 17:44:28 -0700238 D("_adb_connect error: %s", error->c_str());
Brian Carlstromcad81322013-10-18 13:58:48 -0700239 } else if(fd == -2) {
Matt Gumbel411775c2012-11-14 10:16:17 -0800240 fprintf(stderr,"** daemon still not running\n");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800241 }
Yabin Cui815ad882015-09-02 17:44:28 -0700242 D("adb_connect: return fd %d", fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800243
244 return fd;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800245}
246
247
Elliott Hughesd98ca8a2015-05-29 17:55:19 -0700248bool adb_command(const std::string& service) {
249 std::string error;
250 int fd = adb_connect(service, &error);
Elliott Hughes04a98c22015-04-29 08:35:59 -0700251 if (fd < 0) {
Elliott Hughesd98ca8a2015-05-29 17:55:19 -0700252 fprintf(stderr, "error: %s\n", error.c_str());
253 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800254 }
255
Elliott Hughesd98ca8a2015-05-29 17:55:19 -0700256 if (!adb_status(fd, &error)) {
257 fprintf(stderr, "error: %s\n", error.c_str());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800258 adb_close(fd);
Elliott Hughesd98ca8a2015-05-29 17:55:19 -0700259 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800260 }
261
Spencer Lowcc4a4b12015-10-14 17:32:44 -0700262 ReadOrderlyShutdown(fd);
263 adb_close(fd);
Elliott Hughesd98ca8a2015-05-29 17:55:19 -0700264 return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800265}
266
Elliott Hughesda945812015-04-29 12:28:13 -0700267bool adb_query(const std::string& service, std::string* result, std::string* error) {
Yabin Cui815ad882015-09-02 17:44:28 -0700268 D("adb_query: %s", service.c_str());
Elliott Hughes04a98c22015-04-29 08:35:59 -0700269 int fd = adb_connect(service, error);
270 if (fd < 0) {
Elliott Hughes09ccf1f2015-07-18 12:21:30 -0700271 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800272 }
273
Elliott Hughesda945812015-04-29 12:28:13 -0700274 result->clear();
275 if (!ReadProtocolString(fd, result, error)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800276 adb_close(fd);
Elliott Hughesda945812015-04-29 12:28:13 -0700277 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800278 }
Spencer Lowcc4a4b12015-10-14 17:32:44 -0700279
280 ReadOrderlyShutdown(fd);
281 adb_close(fd);
Elliott Hughesda945812015-04-29 12:28:13 -0700282 return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800283}
Josh Gao91d53b52016-01-31 19:12:26 -0800284
285std::string format_host_command(const char* command, TransportType type, const char* serial) {
286 if (serial) {
287 return android::base::StringPrintf("host-serial:%s:%s", serial, command);
288 }
289
290 const char* prefix = "host";
291 if (type == kTransportUsb) {
292 prefix = "host-usb";
293 } else if (type == kTransportLocal) {
294 prefix = "host-local";
295 }
296 return android::base::StringPrintf("%s:%s", prefix, command);
297}
298
299bool adb_get_feature_set(FeatureSet* feature_set, std::string* error) {
300 std::string result;
301 if (adb_query(format_host_command("features", __adb_transport, __adb_serial), &result, error)) {
302 *feature_set = StringToFeatureSet(result);
303 return true;
304 }
305 feature_set->clear();
306 return false;
307}