• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 #define TRACE_TAG TRACE_ADB
18 
19 #include "sysdeps.h"
20 #include "adb_client.h"
21 
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 
31 #include <string>
32 #include <vector>
33 
34 #include <base/stringprintf.h>
35 #include <base/strings.h>
36 
37 #include "adb_io.h"
38 
39 static transport_type __adb_transport = kTransportAny;
40 static const char* __adb_serial = NULL;
41 
42 static int __adb_server_port = DEFAULT_ADB_PORT;
43 static const char* __adb_server_name = NULL;
44 
perror_str(const char * msg)45 static std::string perror_str(const char* msg) {
46     return android::base::StringPrintf("%s: %s", msg, strerror(errno));
47 }
48 
ReadProtocolString(int fd,std::string * s,std::string * error)49 static bool ReadProtocolString(int fd, std::string* s, std::string* error) {
50     char buf[5];
51     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);
58     s->resize(len, '\0');
59     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 
adb_set_transport(transport_type type,const char * serial)67 void adb_set_transport(transport_type type, const char* serial)
68 {
69     __adb_transport = type;
70     __adb_serial = serial;
71 }
72 
adb_set_tcp_specifics(int server_port)73 void adb_set_tcp_specifics(int server_port)
74 {
75     __adb_server_port = server_port;
76 }
77 
adb_set_tcp_name(const char * hostname)78 void adb_set_tcp_name(const char* hostname)
79 {
80     __adb_server_name = hostname;
81 }
82 
adb_get_emulator_console_port()83 int adb_get_emulator_console_port() {
84     if (__adb_serial) {
85         // The user specified a serial number; is it an emulator?
86         int port;
87         return (sscanf(__adb_serial, "emulator-%d", &port) == 1) ? port : -1;
88     }
89 
90     // No specific device was given, so get the list of connected
91     // devices and search for emulators. If there's one, we'll
92     // take it. If there are more than one, that's an error.
93     std::string devices;
94     std::string error;
95     if (!adb_query("host:devices", &devices, &error)) {
96         printf("no emulator connected: %s\n", error.c_str());
97         return -1;
98     }
99 
100     int port;
101     size_t emulator_count = 0;
102     for (auto& device : android::base::Split(devices, "\n")) {
103         if (sscanf(device.c_str(), "emulator-%d", &port) == 1) {
104             if (++emulator_count > 1) {
105                 return -2;
106             }
107         }
108     }
109     if (emulator_count == 0) return -1;
110     return port;
111 }
112 
switch_socket_transport(int fd,std::string * error)113 static int switch_socket_transport(int fd, std::string* error) {
114     std::string service;
115     if (__adb_serial) {
116         service += "host:transport:";
117         service += __adb_serial;
118     } else {
119         const char* transport_type = "???";
120         switch (__adb_transport) {
121           case kTransportUsb:
122             transport_type = "transport-usb";
123             break;
124           case kTransportLocal:
125             transport_type = "transport-local";
126             break;
127           case kTransportAny:
128             transport_type = "transport-any";
129             break;
130           case kTransportHost:
131             // no switch necessary
132             return 0;
133         }
134         service += "host:";
135         service += transport_type;
136     }
137 
138     if (!SendProtocolString(fd, service)) {
139         *error = perror_str("write failure during connection");
140         adb_close(fd);
141         return -1;
142     }
143     D("Switch transport in progress\n");
144 
145     if (!adb_status(fd, error)) {
146         adb_close(fd);
147         D("Switch transport failed: %s\n", error->c_str());
148         return -1;
149     }
150     D("Switch transport success\n");
151     return 0;
152 }
153 
adb_status(int fd,std::string * error)154 bool adb_status(int fd, std::string* error) {
155     char buf[5];
156     if (!ReadFdExactly(fd, buf, 4)) {
157         *error = perror_str("protocol fault (couldn't read status)");
158         return false;
159     }
160 
161     if (!memcmp(buf, "OKAY", 4)) {
162         return true;
163     }
164 
165     if (memcmp(buf, "FAIL", 4)) {
166         *error = android::base::StringPrintf("protocol fault (status %02x %02x %02x %02x?!)",
167                                              buf[0], buf[1], buf[2], buf[3]);
168         return false;
169     }
170 
171     ReadProtocolString(fd, error, error);
172     return false;
173 }
174 
_adb_connect(const std::string & service,std::string * error)175 int _adb_connect(const std::string& service, std::string* error) {
176     D("_adb_connect: %s\n", service.c_str());
177     if (service.empty() || service.size() > 1024) {
178         *error = android::base::StringPrintf("bad service name length (%d)",
179                                              static_cast<int>(service.size()));
180         return -1;
181     }
182 
183     int fd;
184     if (__adb_server_name) {
185         fd = socket_network_client(__adb_server_name, __adb_server_port, SOCK_STREAM);
186     } else {
187         fd = socket_loopback_client(__adb_server_port, SOCK_STREAM);
188     }
189     if (fd < 0) {
190         *error = perror_str("cannot connect to daemon");
191         return -2;
192     }
193 
194     if (memcmp(&service[0],"host",4) != 0 && switch_socket_transport(fd, error)) {
195         return -1;
196     }
197 
198     if(!SendProtocolString(fd, service)) {
199         *error = perror_str("write failure during connection");
200         adb_close(fd);
201         return -1;
202     }
203 
204     if (!adb_status(fd, error)) {
205         adb_close(fd);
206         return -1;
207     }
208 
209     D("_adb_connect: return fd %d\n", fd);
210     return fd;
211 }
212 
adb_connect(const std::string & service,std::string * error)213 int adb_connect(const std::string& service, std::string* error) {
214     // first query the adb server's version
215     int fd = _adb_connect("host:version", error);
216 
217     D("adb_connect: service %s\n", service.c_str());
218     if (fd == -2 && __adb_server_name) {
219         fprintf(stderr,"** Cannot start server on remote host\n");
220         return fd;
221     } else if (fd == -2) {
222         fprintf(stdout,"* daemon not running. starting it now on port %d *\n",
223                 __adb_server_port);
224     start_server:
225         if (launch_server(__adb_server_port)) {
226             fprintf(stderr,"* failed to start daemon *\n");
227             return -1;
228         } else {
229             fprintf(stdout,"* daemon started successfully *\n");
230         }
231         /* give the server some time to start properly and detect devices */
232         adb_sleep_ms(3000);
233         // fall through to _adb_connect
234     } else {
235         // if server was running, check its version to make sure it is not out of date
236         int version = ADB_SERVER_VERSION - 1;
237 
238         // if we have a file descriptor, then parse version result
239         if (fd >= 0) {
240             std::string version_string;
241             if (!ReadProtocolString(fd, &version_string, error)) {
242                 goto error;
243             }
244 
245             adb_close(fd);
246 
247             if (sscanf(&version_string[0], "%04x", &version) != 1) {
248                 goto error;
249             }
250         } else {
251             // if fd is -1, then check for "unknown host service",
252             // which would indicate a version of adb that does not support the version command
253             if (*error == "unknown host service") {
254                 return fd;
255             }
256         }
257 
258         if (version != ADB_SERVER_VERSION) {
259             printf("adb server is out of date.  killing...\n");
260             fd = _adb_connect("host:kill", error);
261             adb_close(fd);
262 
263             /* XXX can we better detect its death? */
264             adb_sleep_ms(2000);
265             goto start_server;
266         }
267     }
268 
269     // if the command is start-server, we are done.
270     if (service == "host:start-server") {
271         return 0;
272     }
273 
274     fd = _adb_connect(service, error);
275     if (fd == -1) {
276         D("_adb_connect error: %s", error->c_str());
277     } else if(fd == -2) {
278         fprintf(stderr,"** daemon still not running\n");
279     }
280     D("adb_connect: return fd %d\n", fd);
281 
282     return fd;
283 error:
284     adb_close(fd);
285     return -1;
286 }
287 
288 
adb_command(const std::string & service,std::string * error)289 int adb_command(const std::string& service, std::string* error) {
290     int fd = adb_connect(service, error);
291     if (fd < 0) {
292         fprintf(stderr, "error: %s\n", error->c_str());
293         return -1;
294     }
295 
296     if (!adb_status(fd, error)) {
297         adb_close(fd);
298         return -1;
299     }
300 
301     return 0;
302 }
303 
adb_query(const std::string & service,std::string * result,std::string * error)304 bool adb_query(const std::string& service, std::string* result, std::string* error) {
305     D("adb_query: %s\n", service.c_str());
306     int fd = adb_connect(service, error);
307     if (fd < 0) {
308         fprintf(stderr,"error: %s\n", error->c_str());
309         return 0;
310     }
311 
312     result->clear();
313     if (!ReadProtocolString(fd, result, error)) {
314         adb_close(fd);
315         return false;
316     }
317     return true;
318 }
319