blob: ddeb5f133bfb66331c65ed583f1103809889546f [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>
23#include <limits.h>
24#include <stdarg.h>
Dan Albert76649012015-02-24 15:51:19 -080025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028#include <sys/stat.h>
Dan Albert76649012015-02-24 15:51:19 -080029#include <sys/types.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030
Elliott Hughes9309ecb2015-04-27 14:20:17 -070031#include <string>
Elliott Hughes078f0fc2015-04-29 08:35:59 -070032#include <vector>
33
34#include <base/stringprintf.h>
Elliott Hughes6452a892015-04-29 12:28:13 -070035#include <base/strings.h>
Elliott Hughes381cfa92015-07-23 17:12:58 -070036#include <cutils/sockets.h>
Elliott Hughes9309ecb2015-04-27 14:20:17 -070037
Dan Albertcc731cc2015-02-24 21:26:58 -080038#include "adb_io.h"
Elliott Hughes381cfa92015-07-23 17:12:58 -070039#include "adb_utils.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040
Elliott Hughes3bd73c12015-05-05 13:10:43 -070041static TransportType __adb_transport = kTransportAny;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042static const char* __adb_serial = NULL;
43
Stefan Hilzingerd0eacb82010-04-19 12:21:12 +010044static int __adb_server_port = DEFAULT_ADB_PORT;
Matt Gumbeld7b33082012-11-14 10:16:17 -080045static const char* __adb_server_name = NULL;
Stefan Hilzingerd0eacb82010-04-19 12:21:12 +010046
Elliott Hughes3bd73c12015-05-05 13:10:43 -070047void adb_set_transport(TransportType type, const char* serial)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048{
49 __adb_transport = type;
50 __adb_serial = serial;
51}
52
Stefan Hilzingerd0eacb82010-04-19 12:21:12 +010053void adb_set_tcp_specifics(int server_port)
54{
55 __adb_server_port = server_port;
56}
57
Matt Gumbeld7b33082012-11-14 10:16:17 -080058void adb_set_tcp_name(const char* hostname)
59{
60 __adb_server_name = hostname;
61}
62
Elliott Hughes078f0fc2015-04-29 08:35:59 -070063static int switch_socket_transport(int fd, std::string* error) {
Elliott Hughes9309ecb2015-04-27 14:20:17 -070064 std::string service;
65 if (__adb_serial) {
66 service += "host:transport:";
67 service += __adb_serial;
68 } else {
Dan Albertbac34742015-02-25 17:51:28 -080069 const char* transport_type = "???";
Elliott Hughes9309ecb2015-04-27 14:20:17 -070070 switch (__adb_transport) {
71 case kTransportUsb:
72 transport_type = "transport-usb";
73 break;
74 case kTransportLocal:
75 transport_type = "transport-local";
76 break;
77 case kTransportAny:
78 transport_type = "transport-any";
79 break;
80 case kTransportHost:
81 // no switch necessary
82 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080083 }
Elliott Hughes9309ecb2015-04-27 14:20:17 -070084 service += "host:";
85 service += transport_type;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080086 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087
Elliott Hughese67f1f82015-04-30 17:32:03 -070088 if (!SendProtocolString(fd, service)) {
Elliott Hughes078f0fc2015-04-29 08:35:59 -070089 *error = perror_str("write failure during connection");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090 adb_close(fd);
91 return -1;
92 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -070093 D("Switch transport in progress");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094
Elliott Hughes078f0fc2015-04-29 08:35:59 -070095 if (!adb_status(fd, error)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080096 adb_close(fd);
Yabin Cui7a3f8d62015-09-02 17:44:28 -070097 D("Switch transport failed: %s", error->c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098 return -1;
99 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700100 D("Switch transport success");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101 return 0;
102}
103
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700104bool adb_status(int fd, std::string* error) {
105 char buf[5];
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700106 if (!ReadFdExactly(fd, buf, 4)) {
107 *error = perror_str("protocol fault (couldn't read status)");
108 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109 }
110
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700111 if (!memcmp(buf, "OKAY", 4)) {
112 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113 }
114
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700115 if (memcmp(buf, "FAIL", 4)) {
116 *error = android::base::StringPrintf("protocol fault (status %02x %02x %02x %02x?!)",
117 buf[0], buf[1], buf[2], buf[3]);
118 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119 }
120
Elliott Hughes6452a892015-04-29 12:28:13 -0700121 ReadProtocolString(fd, error, error);
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700122 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800123}
124
Elliott Hughes6452a892015-04-29 12:28:13 -0700125int _adb_connect(const std::string& service, std::string* error) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700126 D("_adb_connect: %s", service.c_str());
Elliott Hughes6452a892015-04-29 12:28:13 -0700127 if (service.empty() || service.size() > 1024) {
Spencer Low6001c872015-05-13 00:02:55 -0700128 *error = android::base::StringPrintf("bad service name length (%zd)",
129 service.size());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 return -1;
131 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132
Elliott Hughes6452a892015-04-29 12:28:13 -0700133 int fd;
Spencer Low5200c662015-07-30 23:07:55 -0700134 std::string reason;
Elliott Hughes6452a892015-04-29 12:28:13 -0700135 if (__adb_server_name) {
Elliott Hughes381cfa92015-07-23 17:12:58 -0700136 fd = network_connect(__adb_server_name, __adb_server_port, SOCK_STREAM, 0, &reason);
137 if (fd == -1) {
138 *error = android::base::StringPrintf("can't connect to %s:%d: %s",
139 __adb_server_name, __adb_server_port,
140 reason.c_str());
141 return -2;
142 }
Elliott Hughes6452a892015-04-29 12:28:13 -0700143 } else {
Spencer Low5200c662015-07-30 23:07:55 -0700144 fd = network_loopback_client(__adb_server_port, SOCK_STREAM, &reason);
Elliott Hughes381cfa92015-07-23 17:12:58 -0700145 if (fd == -1) {
Spencer Low5200c662015-07-30 23:07:55 -0700146 *error = android::base::StringPrintf("cannot connect to daemon: %s",
147 reason.c_str());
Elliott Hughes381cfa92015-07-23 17:12:58 -0700148 return -2;
149 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150 }
151
Elliott Hughes6452a892015-04-29 12:28:13 -0700152 if (memcmp(&service[0],"host",4) != 0 && switch_socket_transport(fd, error)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153 return -1;
154 }
155
Elliott Hughesaa245492015-08-03 10:38:08 -0700156 if (!SendProtocolString(fd, service)) {
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700157 *error = perror_str("write failure during connection");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158 adb_close(fd);
159 return -1;
160 }
161
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700162 if (!adb_status(fd, error)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163 adb_close(fd);
164 return -1;
165 }
166
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700167 D("_adb_connect: return fd %d", fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168 return fd;
169}
170
Elliott Hughes6452a892015-04-29 12:28:13 -0700171int adb_connect(const std::string& service, std::string* error) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172 // first query the adb server's version
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700173 int fd = _adb_connect("host:version", error);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700175 D("adb_connect: service %s", service.c_str());
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700176 if (fd == -2 && __adb_server_name) {
Matt Gumbeld7b33082012-11-14 10:16:17 -0800177 fprintf(stderr,"** Cannot start server on remote host\n");
Spencer Lowf18fc082015-08-11 17:05:02 -0700178 // error is the original network connection error
Matt Gumbeld7b33082012-11-14 10:16:17 -0800179 return fd;
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700180 } else if (fd == -2) {
Stefan Hilzingerd0eacb82010-04-19 12:21:12 +0100181 fprintf(stdout,"* daemon not running. starting it now on port %d *\n",
182 __adb_server_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183 start_server:
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700184 if (launch_server(__adb_server_port)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185 fprintf(stderr,"* failed to start daemon *\n");
Spencer Lowf18fc082015-08-11 17:05:02 -0700186 // launch_server() has already printed detailed error info, so just
187 // return a generic error string about the overall adb_connect()
188 // that the caller requested.
189 *error = "cannot connect to daemon";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190 return -1;
191 } else {
192 fprintf(stdout,"* daemon started successfully *\n");
193 }
194 /* give the server some time to start properly and detect devices */
Xavier Ducroheta481d092009-05-21 17:47:43 -0700195 adb_sleep_ms(3000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196 // fall through to _adb_connect
197 } else {
Elliott Hughes5b73a102015-10-02 19:49:10 -0700198 // If a server is already running, check its version matches.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199 int version = ADB_SERVER_VERSION - 1;
200
Elliott Hughes5b73a102015-10-02 19:49:10 -0700201 // If we have a file descriptor, then parse version result.
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700202 if (fd >= 0) {
Elliott Hughes6452a892015-04-29 12:28:13 -0700203 std::string version_string;
204 if (!ReadProtocolString(fd, &version_string, error)) {
Elliott Hughes5b73a102015-10-02 19:49:10 -0700205 adb_close(fd);
206 return -1;
Elliott Hughes6452a892015-04-29 12:28:13 -0700207 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208
Spencer Low351ecd12015-10-14 17:32:44 -0700209 ReadOrderlyShutdown(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210 adb_close(fd);
211
Elliott Hughes6452a892015-04-29 12:28:13 -0700212 if (sscanf(&version_string[0], "%04x", &version) != 1) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700213 *error = android::base::StringPrintf("cannot parse version string: %s",
214 version_string.c_str());
Spencer Lowf18fc082015-08-11 17:05:02 -0700215 return -1;
Elliott Hughes6452a892015-04-29 12:28:13 -0700216 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217 } else {
Elliott Hughes5b73a102015-10-02 19:49:10 -0700218 // If fd is -1 check for "unknown host service" which would
219 // indicate a version of adb that does not support the
Spencer Low71635bb2015-08-05 19:26:50 -0700220 // version command, in which case we should fall-through to kill it.
221 if (*error != "unknown host service") {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 return fd;
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700223 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224 }
225
Elliott Hughes6452a892015-04-29 12:28:13 -0700226 if (version != ADB_SERVER_VERSION) {
Elliott Hughes5b73a102015-10-02 19:49:10 -0700227 printf("adb server version (%d) doesn't match this client (%d); killing...\n",
228 version, ADB_SERVER_VERSION);
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700229 fd = _adb_connect("host:kill", error);
Spencer Lowf18fc082015-08-11 17:05:02 -0700230 if (fd >= 0) {
Spencer Low351ecd12015-10-14 17:32:44 -0700231 ReadOrderlyShutdown(fd);
Spencer Lowf18fc082015-08-11 17:05:02 -0700232 adb_close(fd);
233 } else {
234 // If we couldn't connect to the server or had some other error,
235 // report it, but still try to start the server.
236 fprintf(stderr, "error: %s\n", error->c_str());
237 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238
239 /* XXX can we better detect its death? */
240 adb_sleep_ms(2000);
241 goto start_server;
242 }
243 }
244
245 // if the command is start-server, we are done.
Elliott Hughes6452a892015-04-29 12:28:13 -0700246 if (service == "host:start-server") {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247 return 0;
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700248 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700250 fd = _adb_connect(service, error);
251 if (fd == -1) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700252 D("_adb_connect error: %s", error->c_str());
Brian Carlstrom93c91fa2013-10-18 13:58:48 -0700253 } else if(fd == -2) {
Matt Gumbeld7b33082012-11-14 10:16:17 -0800254 fprintf(stderr,"** daemon still not running\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800255 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700256 D("adb_connect: return fd %d", fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800257
258 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800259}
260
261
Elliott Hughes424af022015-05-29 17:55:19 -0700262bool adb_command(const std::string& service) {
263 std::string error;
264 int fd = adb_connect(service, &error);
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700265 if (fd < 0) {
Elliott Hughes424af022015-05-29 17:55:19 -0700266 fprintf(stderr, "error: %s\n", error.c_str());
267 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800268 }
269
Elliott Hughes424af022015-05-29 17:55:19 -0700270 if (!adb_status(fd, &error)) {
271 fprintf(stderr, "error: %s\n", error.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800272 adb_close(fd);
Elliott Hughes424af022015-05-29 17:55:19 -0700273 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800274 }
275
Spencer Low351ecd12015-10-14 17:32:44 -0700276 ReadOrderlyShutdown(fd);
277 adb_close(fd);
Elliott Hughes424af022015-05-29 17:55:19 -0700278 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800279}
280
Elliott Hughes6452a892015-04-29 12:28:13 -0700281bool adb_query(const std::string& service, std::string* result, std::string* error) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700282 D("adb_query: %s", service.c_str());
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700283 int fd = adb_connect(service, error);
284 if (fd < 0) {
Elliott Hughes3d5f60d2015-07-18 12:21:30 -0700285 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800286 }
287
Elliott Hughes6452a892015-04-29 12:28:13 -0700288 result->clear();
289 if (!ReadProtocolString(fd, result, error)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800290 adb_close(fd);
Elliott Hughes6452a892015-04-29 12:28:13 -0700291 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800292 }
Spencer Low351ecd12015-10-14 17:32:44 -0700293
294 ReadOrderlyShutdown(fd);
295 adb_close(fd);
Elliott Hughes6452a892015-04-29 12:28:13 -0700296 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800297}