blob: 6d75966fd8f65356dd16f877bdf9af2d9f0b6494 [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;
Spencer Low753d4852015-07-30 23:07:55 -0700156 std::string reason;
Elliott Hughesda945812015-04-29 12:28:13 -0700157 if (__adb_server_name) {
Elliott Hughes43df1092015-07-23 17:12:58 -0700158 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 {
Spencer Low753d4852015-07-30 23:07:55 -0700166 fd = network_loopback_client(__adb_server_port, SOCK_STREAM, &reason);
Elliott Hughes43df1092015-07-23 17:12:58 -0700167 if (fd == -1) {
Spencer Low753d4852015-07-30 23:07:55 -0700168 *error = android::base::StringPrintf("cannot connect to daemon: %s",
169 reason.c_str());
Elliott Hughes43df1092015-07-23 17:12:58 -0700170 return -2;
171 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800172 }
173
Elliott Hughesda945812015-04-29 12:28:13 -0700174 if (memcmp(&service[0],"host",4) != 0 && switch_socket_transport(fd, error)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800175 return -1;
176 }
177
Elliott Hughes88b4c852015-04-30 17:32:03 -0700178 if(!SendProtocolString(fd, service)) {
Elliott Hughes04a98c22015-04-29 08:35:59 -0700179 *error = perror_str("write failure during connection");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800180 adb_close(fd);
181 return -1;
182 }
183
Elliott Hughes04a98c22015-04-29 08:35:59 -0700184 if (!adb_status(fd, error)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800185 adb_close(fd);
186 return -1;
187 }
188
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700189 D("_adb_connect: return fd %d\n", fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800190 return fd;
191}
192
Elliott Hughesda945812015-04-29 12:28:13 -0700193int adb_connect(const std::string& service, std::string* error) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800194 // first query the adb server's version
Elliott Hughes04a98c22015-04-29 08:35:59 -0700195 int fd = _adb_connect("host:version", error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800196
Elliott Hughesda945812015-04-29 12:28:13 -0700197 D("adb_connect: service %s\n", service.c_str());
Elliott Hughes04a98c22015-04-29 08:35:59 -0700198 if (fd == -2 && __adb_server_name) {
Matt Gumbel411775c2012-11-14 10:16:17 -0800199 fprintf(stderr,"** Cannot start server on remote host\n");
200 return fd;
Elliott Hughes04a98c22015-04-29 08:35:59 -0700201 } else if (fd == -2) {
Stefan Hilzingerfb798d92010-04-19 12:21:12 +0100202 fprintf(stdout,"* daemon not running. starting it now on port %d *\n",
203 __adb_server_port);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800204 start_server:
Elliott Hughes04a98c22015-04-29 08:35:59 -0700205 if (launch_server(__adb_server_port)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800206 fprintf(stderr,"* failed to start daemon *\n");
207 return -1;
208 } else {
209 fprintf(stdout,"* daemon started successfully *\n");
210 }
211 /* give the server some time to start properly and detect devices */
Xavier Ducrohet30a40332009-05-21 17:47:43 -0700212 adb_sleep_ms(3000);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800213 // fall through to _adb_connect
214 } else {
215 // 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 -0800216 int version = ADB_SERVER_VERSION - 1;
217
218 // if we have a file descriptor, then parse version result
Elliott Hughes04a98c22015-04-29 08:35:59 -0700219 if (fd >= 0) {
Elliott Hughesda945812015-04-29 12:28:13 -0700220 std::string version_string;
221 if (!ReadProtocolString(fd, &version_string, error)) {
222 goto error;
223 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800224
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800225 adb_close(fd);
226
Elliott Hughesda945812015-04-29 12:28:13 -0700227 if (sscanf(&version_string[0], "%04x", &version) != 1) {
228 goto error;
229 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800230 } else {
231 // if fd is -1, then check for "unknown host service",
232 // which would indicate a version of adb that does not support the version command
Elliott Hughes04a98c22015-04-29 08:35:59 -0700233 if (*error == "unknown host service") {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800234 return fd;
Elliott Hughes04a98c22015-04-29 08:35:59 -0700235 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800236 }
237
Elliott Hughesda945812015-04-29 12:28:13 -0700238 if (version != ADB_SERVER_VERSION) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800239 printf("adb server is out of date. killing...\n");
Elliott Hughes04a98c22015-04-29 08:35:59 -0700240 fd = _adb_connect("host:kill", error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800241 adb_close(fd);
242
243 /* XXX can we better detect its death? */
244 adb_sleep_ms(2000);
245 goto start_server;
246 }
247 }
248
249 // if the command is start-server, we are done.
Elliott Hughesda945812015-04-29 12:28:13 -0700250 if (service == "host:start-server") {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800251 return 0;
Elliott Hughes04a98c22015-04-29 08:35:59 -0700252 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800253
Elliott Hughes04a98c22015-04-29 08:35:59 -0700254 fd = _adb_connect(service, error);
255 if (fd == -1) {
Spencer Low5c761bd2015-07-21 02:06:26 -0700256 D("_adb_connect error: %s\n", error->c_str());
Brian Carlstromcad81322013-10-18 13:58:48 -0700257 } else if(fd == -2) {
Matt Gumbel411775c2012-11-14 10:16:17 -0800258 fprintf(stderr,"** daemon still not running\n");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800259 }
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700260 D("adb_connect: return fd %d\n", fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800261
262 return fd;
263error:
264 adb_close(fd);
265 return -1;
266}
267
268
Elliott Hughesd98ca8a2015-05-29 17:55:19 -0700269bool adb_command(const std::string& service) {
270 std::string error;
271 int fd = adb_connect(service, &error);
Elliott Hughes04a98c22015-04-29 08:35:59 -0700272 if (fd < 0) {
Elliott Hughesd98ca8a2015-05-29 17:55:19 -0700273 fprintf(stderr, "error: %s\n", error.c_str());
274 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800275 }
276
Elliott Hughesd98ca8a2015-05-29 17:55:19 -0700277 if (!adb_status(fd, &error)) {
278 fprintf(stderr, "error: %s\n", error.c_str());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800279 adb_close(fd);
Elliott Hughesd98ca8a2015-05-29 17:55:19 -0700280 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800281 }
282
Elliott Hughesd98ca8a2015-05-29 17:55:19 -0700283 return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800284}
285
Elliott Hughesda945812015-04-29 12:28:13 -0700286bool adb_query(const std::string& service, std::string* result, std::string* error) {
287 D("adb_query: %s\n", service.c_str());
Elliott Hughes04a98c22015-04-29 08:35:59 -0700288 int fd = adb_connect(service, error);
289 if (fd < 0) {
Elliott Hughes09ccf1f2015-07-18 12:21:30 -0700290 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800291 }
292
Elliott Hughesda945812015-04-29 12:28:13 -0700293 result->clear();
294 if (!ReadProtocolString(fd, result, error)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800295 adb_close(fd);
Elliott Hughesda945812015-04-29 12:28:13 -0700296 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800297 }
Elliott Hughesda945812015-04-29 12:28:13 -0700298 return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800299}