blob: 418662c4d9f35fe42bb8e07198f26905f76df75c [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
Dan Albertdb6fe642015-03-19 15:21:08 -070017#define TRACE_TAG TRACE_ADB
18
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 Hughes04a98c22015-04-29 08:35:59 -070032#include <vector>
33
34#include <base/stringprintf.h>
Elliott Hughesda945812015-04-29 12:28:13 -070035#include <base/strings.h>
Elliott Hughes43df1092015-07-23 17:12:58 -070036#include <cutils/sockets.h>
Elliott Hughes949f2622015-04-27 14:20:17 -070037
Dan Albert66a91b02015-02-24 21:26:58 -080038#include "adb_io.h"
Elliott Hughes43df1092015-07-23 17:12:58 -070039#include "adb_utils.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080040
Elliott Hughes3aec2ba2015-05-05 13:10:43 -070041static TransportType __adb_transport = kTransportAny;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080042static const char* __adb_serial = NULL;
43
Stefan Hilzingerfb798d92010-04-19 12:21:12 +010044static int __adb_server_port = DEFAULT_ADB_PORT;
Matt Gumbel411775c2012-11-14 10:16:17 -080045static const char* __adb_server_name = NULL;
Stefan Hilzingerfb798d92010-04-19 12:21:12 +010046
Elliott Hughesda945812015-04-29 12:28:13 -070047static std::string perror_str(const char* msg) {
48 return android::base::StringPrintf("%s: %s", msg, strerror(errno));
49}
50
51static bool ReadProtocolString(int fd, std::string* s, std::string* error) {
52 char buf[5];
Elliott Hughesda945812015-04-29 12:28:13 -070053 if (!ReadFdExactly(fd, buf, 4)) {
54 *error = perror_str("protocol fault (couldn't read status length)");
55 return false;
56 }
57 buf[4] = 0;
58
59 unsigned long len = strtoul(buf, 0, 16);
Elliott Hughes88b4c852015-04-30 17:32:03 -070060 s->resize(len, '\0');
Elliott Hughesda945812015-04-29 12:28:13 -070061 if (!ReadFdExactly(fd, &(*s)[0], len)) {
62 *error = perror_str("protocol fault (couldn't read status message)");
63 return false;
64 }
65
66 return true;
67}
68
Elliott Hughes3aec2ba2015-05-05 13:10:43 -070069void adb_set_transport(TransportType type, const char* serial)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080070{
71 __adb_transport = type;
72 __adb_serial = serial;
73}
74
Stefan Hilzingerfb798d92010-04-19 12:21:12 +010075void adb_set_tcp_specifics(int server_port)
76{
77 __adb_server_port = server_port;
78}
79
Matt Gumbel411775c2012-11-14 10:16:17 -080080void adb_set_tcp_name(const char* hostname)
81{
82 __adb_server_name = hostname;
83}
84
Elliott Hughes04a98c22015-04-29 08:35:59 -070085static int switch_socket_transport(int fd, std::string* error) {
Elliott Hughes949f2622015-04-27 14:20:17 -070086 std::string service;
87 if (__adb_serial) {
88 service += "host:transport:";
89 service += __adb_serial;
90 } else {
Dan Albertf30d73c2015-02-25 17:51:28 -080091 const char* transport_type = "???";
Elliott Hughes949f2622015-04-27 14:20:17 -070092 switch (__adb_transport) {
93 case kTransportUsb:
94 transport_type = "transport-usb";
95 break;
96 case kTransportLocal:
97 transport_type = "transport-local";
98 break;
99 case kTransportAny:
100 transport_type = "transport-any";
101 break;
102 case kTransportHost:
103 // no switch necessary
104 return 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800105 }
Elliott Hughes949f2622015-04-27 14:20:17 -0700106 service += "host:";
107 service += transport_type;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800108 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800109
Elliott Hughes88b4c852015-04-30 17:32:03 -0700110 if (!SendProtocolString(fd, service)) {
Elliott Hughes04a98c22015-04-29 08:35:59 -0700111 *error = perror_str("write failure during connection");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800112 adb_close(fd);
113 return -1;
114 }
115 D("Switch transport in progress\n");
116
Elliott Hughes04a98c22015-04-29 08:35:59 -0700117 if (!adb_status(fd, error)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800118 adb_close(fd);
Elliott Hughes04a98c22015-04-29 08:35:59 -0700119 D("Switch transport failed: %s\n", error->c_str());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800120 return -1;
121 }
122 D("Switch transport success\n");
123 return 0;
124}
125
Elliott Hughes04a98c22015-04-29 08:35:59 -0700126bool adb_status(int fd, std::string* error) {
127 char buf[5];
Elliott Hughes04a98c22015-04-29 08:35:59 -0700128 if (!ReadFdExactly(fd, buf, 4)) {
129 *error = perror_str("protocol fault (couldn't read status)");
130 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800131 }
132
Elliott Hughes04a98c22015-04-29 08:35:59 -0700133 if (!memcmp(buf, "OKAY", 4)) {
134 return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800135 }
136
Elliott Hughes04a98c22015-04-29 08:35:59 -0700137 if (memcmp(buf, "FAIL", 4)) {
138 *error = android::base::StringPrintf("protocol fault (status %02x %02x %02x %02x?!)",
139 buf[0], buf[1], buf[2], buf[3]);
140 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800141 }
142
Elliott Hughesda945812015-04-29 12:28:13 -0700143 ReadProtocolString(fd, error, error);
Elliott Hughes04a98c22015-04-29 08:35:59 -0700144 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800145}
146
Elliott Hughesda945812015-04-29 12:28:13 -0700147int _adb_connect(const std::string& service, std::string* error) {
148 D("_adb_connect: %s\n", service.c_str());
149 if (service.empty() || service.size() > 1024) {
Spencer Low803451e2015-05-13 00:02:55 -0700150 *error = android::base::StringPrintf("bad service name length (%zd)",
151 service.size());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800152 return -1;
153 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800154
Elliott Hughesda945812015-04-29 12:28:13 -0700155 int fd;
156 if (__adb_server_name) {
Elliott Hughes43df1092015-07-23 17:12:58 -0700157 std::string reason;
158 fd = network_connect(__adb_server_name, __adb_server_port, SOCK_STREAM, 0, &reason);
159 if (fd == -1) {
160 *error = android::base::StringPrintf("can't connect to %s:%d: %s",
161 __adb_server_name, __adb_server_port,
162 reason.c_str());
163 return -2;
164 }
Elliott Hughesda945812015-04-29 12:28:13 -0700165 } else {
Matt Gumbel411775c2012-11-14 10:16:17 -0800166 fd = socket_loopback_client(__adb_server_port, SOCK_STREAM);
Elliott Hughes43df1092015-07-23 17:12:58 -0700167 if (fd == -1) {
168 *error = perror_str("cannot connect to daemon");
169 return -2;
170 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800171 }
172
Elliott Hughesda945812015-04-29 12:28:13 -0700173 if (memcmp(&service[0],"host",4) != 0 && switch_socket_transport(fd, error)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800174 return -1;
175 }
176
Elliott Hughes88b4c852015-04-30 17:32:03 -0700177 if(!SendProtocolString(fd, service)) {
Elliott Hughes04a98c22015-04-29 08:35:59 -0700178 *error = perror_str("write failure during connection");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800179 adb_close(fd);
180 return -1;
181 }
182
Elliott Hughes04a98c22015-04-29 08:35:59 -0700183 if (!adb_status(fd, error)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800184 adb_close(fd);
185 return -1;
186 }
187
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700188 D("_adb_connect: return fd %d\n", fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800189 return fd;
190}
191
Elliott Hughesda945812015-04-29 12:28:13 -0700192int adb_connect(const std::string& service, std::string* error) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800193 // first query the adb server's version
Elliott Hughes04a98c22015-04-29 08:35:59 -0700194 int fd = _adb_connect("host:version", error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800195
Elliott Hughesda945812015-04-29 12:28:13 -0700196 D("adb_connect: service %s\n", service.c_str());
Elliott Hughes04a98c22015-04-29 08:35:59 -0700197 if (fd == -2 && __adb_server_name) {
Matt Gumbel411775c2012-11-14 10:16:17 -0800198 fprintf(stderr,"** Cannot start server on remote host\n");
199 return fd;
Elliott Hughes04a98c22015-04-29 08:35:59 -0700200 } else if (fd == -2) {
Stefan Hilzingerfb798d92010-04-19 12:21:12 +0100201 fprintf(stdout,"* daemon not running. starting it now on port %d *\n",
202 __adb_server_port);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800203 start_server:
Elliott Hughes04a98c22015-04-29 08:35:59 -0700204 if (launch_server(__adb_server_port)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800205 fprintf(stderr,"* failed to start daemon *\n");
206 return -1;
207 } else {
208 fprintf(stdout,"* daemon started successfully *\n");
209 }
210 /* give the server some time to start properly and detect devices */
Xavier Ducrohet30a40332009-05-21 17:47:43 -0700211 adb_sleep_ms(3000);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800212 // fall through to _adb_connect
213 } else {
214 // if server was running, check its version to make sure it is not out of date
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800215 int version = ADB_SERVER_VERSION - 1;
216
217 // if we have a file descriptor, then parse version result
Elliott Hughes04a98c22015-04-29 08:35:59 -0700218 if (fd >= 0) {
Elliott Hughesda945812015-04-29 12:28:13 -0700219 std::string version_string;
220 if (!ReadProtocolString(fd, &version_string, error)) {
221 goto error;
222 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800223
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800224 adb_close(fd);
225
Elliott Hughesda945812015-04-29 12:28:13 -0700226 if (sscanf(&version_string[0], "%04x", &version) != 1) {
227 goto error;
228 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800229 } else {
230 // if fd is -1, then check for "unknown host service",
231 // which would indicate a version of adb that does not support the version command
Elliott Hughes04a98c22015-04-29 08:35:59 -0700232 if (*error == "unknown host service") {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800233 return fd;
Elliott Hughes04a98c22015-04-29 08:35:59 -0700234 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800235 }
236
Elliott Hughesda945812015-04-29 12:28:13 -0700237 if (version != ADB_SERVER_VERSION) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800238 printf("adb server is out of date. killing...\n");
Elliott Hughes04a98c22015-04-29 08:35:59 -0700239 fd = _adb_connect("host:kill", error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800240 adb_close(fd);
241
242 /* XXX can we better detect its death? */
243 adb_sleep_ms(2000);
244 goto start_server;
245 }
246 }
247
248 // if the command is start-server, we are done.
Elliott Hughesda945812015-04-29 12:28:13 -0700249 if (service == "host:start-server") {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800250 return 0;
Elliott Hughes04a98c22015-04-29 08:35:59 -0700251 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800252
Elliott Hughes04a98c22015-04-29 08:35:59 -0700253 fd = _adb_connect(service, error);
254 if (fd == -1) {
Spencer Low5c761bd2015-07-21 02:06:26 -0700255 D("_adb_connect error: %s\n", error->c_str());
Brian Carlstromcad81322013-10-18 13:58:48 -0700256 } else if(fd == -2) {
Matt Gumbel411775c2012-11-14 10:16:17 -0800257 fprintf(stderr,"** daemon still not running\n");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800258 }
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700259 D("adb_connect: return fd %d\n", fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800260
261 return fd;
262error:
263 adb_close(fd);
264 return -1;
265}
266
267
Elliott Hughesd98ca8a2015-05-29 17:55:19 -0700268bool adb_command(const std::string& service) {
269 std::string error;
270 int fd = adb_connect(service, &error);
Elliott Hughes04a98c22015-04-29 08:35:59 -0700271 if (fd < 0) {
Elliott Hughesd98ca8a2015-05-29 17:55:19 -0700272 fprintf(stderr, "error: %s\n", error.c_str());
273 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800274 }
275
Elliott Hughesd98ca8a2015-05-29 17:55:19 -0700276 if (!adb_status(fd, &error)) {
277 fprintf(stderr, "error: %s\n", error.c_str());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800278 adb_close(fd);
Elliott Hughesd98ca8a2015-05-29 17:55:19 -0700279 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800280 }
281
Elliott Hughesd98ca8a2015-05-29 17:55:19 -0700282 return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800283}
284
Elliott Hughesda945812015-04-29 12:28:13 -0700285bool adb_query(const std::string& service, std::string* result, std::string* error) {
286 D("adb_query: %s\n", service.c_str());
Elliott Hughes04a98c22015-04-29 08:35:59 -0700287 int fd = adb_connect(service, error);
288 if (fd < 0) {
Elliott Hughes09ccf1f2015-07-18 12:21:30 -0700289 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800290 }
291
Elliott Hughesda945812015-04-29 12:28:13 -0700292 result->clear();
293 if (!ReadProtocolString(fd, result, error)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800294 adb_close(fd);
Elliott Hughesda945812015-04-29 12:28:13 -0700295 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800296 }
Elliott Hughesda945812015-04-29 12:28:13 -0700297 return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800298}