blob: 9a1b7612a2ae668410a943c358d3674b66be87c0 [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 Hughes949f2622015-04-27 14:20:17 -070036
Dan Albert66a91b02015-02-24 21:26:58 -080037#include "adb_io.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080038
Elliott Hughes3aec2ba2015-05-05 13:10:43 -070039static TransportType __adb_transport = kTransportAny;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080040static const char* __adb_serial = NULL;
41
Stefan Hilzingerfb798d92010-04-19 12:21:12 +010042static int __adb_server_port = DEFAULT_ADB_PORT;
Matt Gumbel411775c2012-11-14 10:16:17 -080043static const char* __adb_server_name = NULL;
Stefan Hilzingerfb798d92010-04-19 12:21:12 +010044
Elliott Hughesda945812015-04-29 12:28:13 -070045static std::string perror_str(const char* msg) {
46 return android::base::StringPrintf("%s: %s", msg, strerror(errno));
47}
48
49static bool ReadProtocolString(int fd, std::string* s, std::string* error) {
50 char buf[5];
Elliott Hughesda945812015-04-29 12:28:13 -070051 if (!ReadFdExactly(fd, buf, 4)) {
52 *error = perror_str("protocol fault (couldn't read status length)");
53 return false;
54 }
55 buf[4] = 0;
56
57 unsigned long len = strtoul(buf, 0, 16);
Elliott Hughes88b4c852015-04-30 17:32:03 -070058 s->resize(len, '\0');
Elliott Hughesda945812015-04-29 12:28:13 -070059 if (!ReadFdExactly(fd, &(*s)[0], len)) {
60 *error = perror_str("protocol fault (couldn't read status message)");
61 return false;
62 }
63
64 return true;
65}
66
Elliott Hughes3aec2ba2015-05-05 13:10:43 -070067void adb_set_transport(TransportType type, const char* serial)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080068{
69 __adb_transport = type;
70 __adb_serial = serial;
71}
72
Stefan Hilzingerfb798d92010-04-19 12:21:12 +010073void adb_set_tcp_specifics(int server_port)
74{
75 __adb_server_port = server_port;
76}
77
Matt Gumbel411775c2012-11-14 10:16:17 -080078void adb_set_tcp_name(const char* hostname)
79{
80 __adb_server_name = hostname;
81}
82
Elliott Hughes04a98c22015-04-29 08:35:59 -070083static int switch_socket_transport(int fd, std::string* error) {
Elliott Hughes949f2622015-04-27 14:20:17 -070084 std::string service;
85 if (__adb_serial) {
86 service += "host:transport:";
87 service += __adb_serial;
88 } else {
Dan Albertf30d73c2015-02-25 17:51:28 -080089 const char* transport_type = "???";
Elliott Hughes949f2622015-04-27 14:20:17 -070090 switch (__adb_transport) {
91 case kTransportUsb:
92 transport_type = "transport-usb";
93 break;
94 case kTransportLocal:
95 transport_type = "transport-local";
96 break;
97 case kTransportAny:
98 transport_type = "transport-any";
99 break;
100 case kTransportHost:
101 // no switch necessary
102 return 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800103 }
Elliott Hughes949f2622015-04-27 14:20:17 -0700104 service += "host:";
105 service += transport_type;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800106 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800107
Elliott Hughes88b4c852015-04-30 17:32:03 -0700108 if (!SendProtocolString(fd, service)) {
Elliott Hughes04a98c22015-04-29 08:35:59 -0700109 *error = perror_str("write failure during connection");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800110 adb_close(fd);
111 return -1;
112 }
113 D("Switch transport in progress\n");
114
Elliott Hughes04a98c22015-04-29 08:35:59 -0700115 if (!adb_status(fd, error)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800116 adb_close(fd);
Elliott Hughes04a98c22015-04-29 08:35:59 -0700117 D("Switch transport failed: %s\n", error->c_str());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800118 return -1;
119 }
120 D("Switch transport success\n");
121 return 0;
122}
123
Elliott Hughes04a98c22015-04-29 08:35:59 -0700124bool adb_status(int fd, std::string* error) {
125 char buf[5];
Elliott Hughes04a98c22015-04-29 08:35:59 -0700126 if (!ReadFdExactly(fd, buf, 4)) {
127 *error = perror_str("protocol fault (couldn't read status)");
128 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800129 }
130
Elliott Hughes04a98c22015-04-29 08:35:59 -0700131 if (!memcmp(buf, "OKAY", 4)) {
132 return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800133 }
134
Elliott Hughes04a98c22015-04-29 08:35:59 -0700135 if (memcmp(buf, "FAIL", 4)) {
136 *error = android::base::StringPrintf("protocol fault (status %02x %02x %02x %02x?!)",
137 buf[0], buf[1], buf[2], buf[3]);
138 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800139 }
140
Elliott Hughesda945812015-04-29 12:28:13 -0700141 ReadProtocolString(fd, error, error);
Elliott Hughes04a98c22015-04-29 08:35:59 -0700142 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800143}
144
Elliott Hughesda945812015-04-29 12:28:13 -0700145int _adb_connect(const std::string& service, std::string* error) {
146 D("_adb_connect: %s\n", service.c_str());
147 if (service.empty() || service.size() > 1024) {
Spencer Low803451e2015-05-13 00:02:55 -0700148 *error = android::base::StringPrintf("bad service name length (%zd)",
149 service.size());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800150 return -1;
151 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800152
Elliott Hughesda945812015-04-29 12:28:13 -0700153 int fd;
154 if (__adb_server_name) {
Matt Gumbel411775c2012-11-14 10:16:17 -0800155 fd = socket_network_client(__adb_server_name, __adb_server_port, SOCK_STREAM);
Elliott Hughesda945812015-04-29 12:28:13 -0700156 } else {
Matt Gumbel411775c2012-11-14 10:16:17 -0800157 fd = socket_loopback_client(__adb_server_port, SOCK_STREAM);
Elliott Hughesda945812015-04-29 12:28:13 -0700158 }
159 if (fd < 0) {
Elliott Hughes04a98c22015-04-29 08:35:59 -0700160 *error = perror_str("cannot connect to daemon");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800161 return -2;
162 }
163
Elliott Hughesda945812015-04-29 12:28:13 -0700164 if (memcmp(&service[0],"host",4) != 0 && switch_socket_transport(fd, error)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800165 return -1;
166 }
167
Elliott Hughes88b4c852015-04-30 17:32:03 -0700168 if(!SendProtocolString(fd, service)) {
Elliott Hughes04a98c22015-04-29 08:35:59 -0700169 *error = perror_str("write failure during connection");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800170 adb_close(fd);
171 return -1;
172 }
173
Elliott Hughes04a98c22015-04-29 08:35:59 -0700174 if (!adb_status(fd, error)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800175 adb_close(fd);
176 return -1;
177 }
178
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700179 D("_adb_connect: return fd %d\n", fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800180 return fd;
181}
182
Elliott Hughesda945812015-04-29 12:28:13 -0700183int adb_connect(const std::string& service, std::string* error) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800184 // first query the adb server's version
Elliott Hughes04a98c22015-04-29 08:35:59 -0700185 int fd = _adb_connect("host:version", error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800186
Elliott Hughesda945812015-04-29 12:28:13 -0700187 D("adb_connect: service %s\n", service.c_str());
Elliott Hughes04a98c22015-04-29 08:35:59 -0700188 if (fd == -2 && __adb_server_name) {
Matt Gumbel411775c2012-11-14 10:16:17 -0800189 fprintf(stderr,"** Cannot start server on remote host\n");
190 return fd;
Elliott Hughes04a98c22015-04-29 08:35:59 -0700191 } else if (fd == -2) {
Stefan Hilzingerfb798d92010-04-19 12:21:12 +0100192 fprintf(stdout,"* daemon not running. starting it now on port %d *\n",
193 __adb_server_port);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800194 start_server:
Elliott Hughes04a98c22015-04-29 08:35:59 -0700195 if (launch_server(__adb_server_port)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800196 fprintf(stderr,"* failed to start daemon *\n");
197 return -1;
198 } else {
199 fprintf(stdout,"* daemon started successfully *\n");
200 }
201 /* give the server some time to start properly and detect devices */
Xavier Ducrohet30a40332009-05-21 17:47:43 -0700202 adb_sleep_ms(3000);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800203 // fall through to _adb_connect
204 } else {
205 // 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 -0800206 int version = ADB_SERVER_VERSION - 1;
207
208 // 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)) {
212 goto error;
213 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800214
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800215 adb_close(fd);
216
Elliott Hughesda945812015-04-29 12:28:13 -0700217 if (sscanf(&version_string[0], "%04x", &version) != 1) {
218 goto error;
219 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800220 } else {
221 // if fd is -1, then check for "unknown host service",
222 // which would indicate a version of adb that does not support the version command
Elliott Hughes04a98c22015-04-29 08:35:59 -0700223 if (*error == "unknown host service") {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800224 return fd;
Elliott Hughes04a98c22015-04-29 08:35:59 -0700225 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800226 }
227
Elliott Hughesda945812015-04-29 12:28:13 -0700228 if (version != ADB_SERVER_VERSION) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800229 printf("adb server is out of date. killing...\n");
Elliott Hughes04a98c22015-04-29 08:35:59 -0700230 fd = _adb_connect("host:kill", error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800231 adb_close(fd);
232
233 /* XXX can we better detect its death? */
234 adb_sleep_ms(2000);
235 goto start_server;
236 }
237 }
238
239 // if the command is start-server, we are done.
Elliott Hughesda945812015-04-29 12:28:13 -0700240 if (service == "host:start-server") {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800241 return 0;
Elliott Hughes04a98c22015-04-29 08:35:59 -0700242 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800243
Elliott Hughes04a98c22015-04-29 08:35:59 -0700244 fd = _adb_connect(service, error);
245 if (fd == -1) {
246 D("_adb_connect error: %s", error->c_str());
Brian Carlstromcad81322013-10-18 13:58:48 -0700247 } else if(fd == -2) {
Matt Gumbel411775c2012-11-14 10:16:17 -0800248 fprintf(stderr,"** daemon still not running\n");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800249 }
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700250 D("adb_connect: return fd %d\n", fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800251
252 return fd;
253error:
254 adb_close(fd);
255 return -1;
256}
257
258
Elliott Hughesd98ca8a2015-05-29 17:55:19 -0700259bool adb_command(const std::string& service) {
260 std::string error;
261 int fd = adb_connect(service, &error);
Elliott Hughes04a98c22015-04-29 08:35:59 -0700262 if (fd < 0) {
Elliott Hughesd98ca8a2015-05-29 17:55:19 -0700263 fprintf(stderr, "error: %s\n", error.c_str());
264 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800265 }
266
Elliott Hughesd98ca8a2015-05-29 17:55:19 -0700267 if (!adb_status(fd, &error)) {
268 fprintf(stderr, "error: %s\n", error.c_str());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800269 adb_close(fd);
Elliott Hughesd98ca8a2015-05-29 17:55:19 -0700270 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800271 }
272
Elliott Hughesd98ca8a2015-05-29 17:55:19 -0700273 return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800274}
275
Elliott Hughesda945812015-04-29 12:28:13 -0700276bool adb_query(const std::string& service, std::string* result, std::string* error) {
277 D("adb_query: %s\n", service.c_str());
Elliott Hughes04a98c22015-04-29 08:35:59 -0700278 int fd = adb_connect(service, error);
279 if (fd < 0) {
Elliott Hughes09ccf1f2015-07-18 12:21:30 -0700280 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800281 }
282
Elliott Hughesda945812015-04-29 12:28:13 -0700283 result->clear();
284 if (!ReadProtocolString(fd, result, error)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800285 adb_close(fd);
Elliott Hughesda945812015-04-29 12:28:13 -0700286 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800287 }
Elliott Hughesda945812015-04-29 12:28:13 -0700288 return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800289}