Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 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 | |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 17 | #define TRACE_TAG TRACE_ADB |
| 18 | |
| 19 | #include "sysdeps.h" |
| 20 | #include "adb_client.h" |
| 21 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 22 | #include <errno.h> |
| 23 | #include <limits.h> |
| 24 | #include <stdarg.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | #include <sys/stat.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 29 | #include <sys/types.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | |
Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 31 | #include <string> |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 32 | #include <vector> |
| 33 | |
| 34 | #include <base/stringprintf.h> |
Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 35 | |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 36 | #include "adb_io.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 37 | |
| 38 | static transport_type __adb_transport = kTransportAny; |
| 39 | static const char* __adb_serial = NULL; |
| 40 | |
Stefan Hilzinger | d0eacb8 | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 41 | static int __adb_server_port = DEFAULT_ADB_PORT; |
Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 42 | static const char* __adb_server_name = NULL; |
Stefan Hilzinger | d0eacb8 | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 43 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 44 | void adb_set_transport(transport_type type, const char* serial) |
| 45 | { |
| 46 | __adb_transport = type; |
| 47 | __adb_serial = serial; |
| 48 | } |
| 49 | |
Stefan Hilzinger | d0eacb8 | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 50 | void adb_set_tcp_specifics(int server_port) |
| 51 | { |
| 52 | __adb_server_port = server_port; |
| 53 | } |
| 54 | |
Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 55 | void adb_set_tcp_name(const char* hostname) |
| 56 | { |
| 57 | __adb_server_name = hostname; |
| 58 | } |
| 59 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 60 | int adb_get_emulator_console_port(void) |
| 61 | { |
| 62 | const char* serial = __adb_serial; |
| 63 | int port; |
| 64 | |
| 65 | if (serial == NULL) { |
| 66 | /* if no specific device was specified, we need to look at */ |
| 67 | /* the list of connected devices, and extract an emulator */ |
| 68 | /* name from it. two emulators is an error */ |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 69 | std::string error; |
| 70 | char* tmp = adb_query("host:devices", &error); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 71 | char* p = tmp; |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 72 | if (!tmp) { |
| 73 | printf("no emulator connected: %s\n", error.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 74 | return -1; |
| 75 | } |
| 76 | while (*p) { |
| 77 | char* q = strchr(p, '\n'); |
| 78 | if (q != NULL) |
| 79 | *q++ = 0; |
| 80 | else |
| 81 | q = p + strlen(p); |
| 82 | |
| 83 | if (!memcmp(p, LOCAL_CLIENT_PREFIX, sizeof(LOCAL_CLIENT_PREFIX)-1)) { |
| 84 | if (serial != NULL) { /* more than one emulator listed */ |
| 85 | free(tmp); |
| 86 | return -2; |
| 87 | } |
| 88 | serial = p; |
| 89 | } |
| 90 | |
| 91 | p = q; |
| 92 | } |
| 93 | free(tmp); |
| 94 | |
| 95 | if (serial == NULL) |
| 96 | return -1; /* no emulator found */ |
| 97 | } |
| 98 | else { |
| 99 | if (memcmp(serial, LOCAL_CLIENT_PREFIX, sizeof(LOCAL_CLIENT_PREFIX)-1) != 0) |
| 100 | return -1; /* not an emulator */ |
| 101 | } |
| 102 | |
| 103 | serial += sizeof(LOCAL_CLIENT_PREFIX)-1; |
| 104 | port = strtol(serial, NULL, 10); |
| 105 | return port; |
| 106 | } |
| 107 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 108 | std::string perror_str(const char* msg) { |
| 109 | return android::base::StringPrintf("%s: %s", msg, strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 112 | static int switch_socket_transport(int fd, std::string* error) { |
Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 113 | std::string service; |
| 114 | if (__adb_serial) { |
| 115 | service += "host:transport:"; |
| 116 | service += __adb_serial; |
| 117 | } else { |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 118 | const char* transport_type = "???"; |
Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 119 | switch (__adb_transport) { |
| 120 | case kTransportUsb: |
| 121 | transport_type = "transport-usb"; |
| 122 | break; |
| 123 | case kTransportLocal: |
| 124 | transport_type = "transport-local"; |
| 125 | break; |
| 126 | case kTransportAny: |
| 127 | transport_type = "transport-any"; |
| 128 | break; |
| 129 | case kTransportHost: |
| 130 | // no switch necessary |
| 131 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 132 | } |
Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 133 | service += "host:"; |
| 134 | service += transport_type; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 135 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 136 | |
Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 137 | char tmp[5]; |
| 138 | snprintf(tmp, sizeof(tmp), "%04zx", service.size()); |
| 139 | if (!WriteFdExactly(fd, tmp, 4) || !WriteFdExactly(fd, service.c_str(), service.size())) { |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 140 | *error = perror_str("write failure during connection"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 141 | adb_close(fd); |
| 142 | return -1; |
| 143 | } |
| 144 | D("Switch transport in progress\n"); |
| 145 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 146 | if (!adb_status(fd, error)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 147 | adb_close(fd); |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 148 | D("Switch transport failed: %s\n", error->c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 149 | return -1; |
| 150 | } |
| 151 | D("Switch transport success\n"); |
| 152 | return 0; |
| 153 | } |
| 154 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 155 | bool adb_status(int fd, std::string* error) { |
| 156 | char buf[5]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 157 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 158 | if (!ReadFdExactly(fd, buf, 4)) { |
| 159 | *error = perror_str("protocol fault (couldn't read status)"); |
| 160 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 163 | if (!memcmp(buf, "OKAY", 4)) { |
| 164 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 167 | if (memcmp(buf, "FAIL", 4)) { |
| 168 | *error = android::base::StringPrintf("protocol fault (status %02x %02x %02x %02x?!)", |
| 169 | buf[0], buf[1], buf[2], buf[3]); |
| 170 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 173 | if (!ReadFdExactly(fd, buf, 4)) { |
| 174 | *error = perror_str("protocol fault (couldn't read status length)"); |
| 175 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 176 | } |
| 177 | buf[4] = 0; |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 178 | |
| 179 | unsigned long len = strtoul(buf, 0, 16); |
| 180 | error->resize(len + 1, '\0'); // Ensure NUL-termination. |
| 181 | if (!ReadFdExactly(fd, &(*error)[0], len)) { |
| 182 | *error = perror_str("protocol fault (couldn't read status message)"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 183 | } |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 184 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 187 | int _adb_connect(const char *service, std::string* error) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 188 | char tmp[5]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 189 | int fd; |
| 190 | |
| 191 | D("_adb_connect: %s\n", service); |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 192 | size_t len = strlen(service); |
| 193 | if ((len < 1) || (len > 1024)) { |
| 194 | *error = android::base::StringPrintf("service name too long (%zd)", len); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 195 | return -1; |
| 196 | } |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 197 | snprintf(tmp, sizeof tmp, "%04zx", len); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 198 | |
Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 199 | if (__adb_server_name) |
| 200 | fd = socket_network_client(__adb_server_name, __adb_server_port, SOCK_STREAM); |
| 201 | else |
| 202 | fd = socket_loopback_client(__adb_server_port, SOCK_STREAM); |
| 203 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 204 | if(fd < 0) { |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 205 | *error = perror_str("cannot connect to daemon"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 206 | return -2; |
| 207 | } |
| 208 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 209 | if (memcmp(service,"host",4) != 0 && switch_socket_transport(fd, error)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 210 | return -1; |
| 211 | } |
| 212 | |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 213 | if(!WriteFdExactly(fd, tmp, 4) || !WriteFdExactly(fd, service, len)) { |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 214 | *error = perror_str("write failure during connection"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 215 | adb_close(fd); |
| 216 | return -1; |
| 217 | } |
| 218 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 219 | if (!adb_status(fd, error)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 220 | adb_close(fd); |
| 221 | return -1; |
| 222 | } |
| 223 | |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 224 | D("_adb_connect: return fd %d\n", fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 225 | return fd; |
| 226 | } |
| 227 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 228 | int adb_connect(const char* service, std::string* error) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 229 | // first query the adb server's version |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 230 | int fd = _adb_connect("host:version", error); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 231 | |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 232 | D("adb_connect: service %s\n", service); |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 233 | if (fd == -2 && __adb_server_name) { |
Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 234 | fprintf(stderr,"** Cannot start server on remote host\n"); |
| 235 | return fd; |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 236 | } else if (fd == -2) { |
Stefan Hilzinger | d0eacb8 | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 237 | fprintf(stdout,"* daemon not running. starting it now on port %d *\n", |
| 238 | __adb_server_port); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 239 | start_server: |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 240 | if (launch_server(__adb_server_port)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 241 | fprintf(stderr,"* failed to start daemon *\n"); |
| 242 | return -1; |
| 243 | } else { |
| 244 | fprintf(stdout,"* daemon started successfully *\n"); |
| 245 | } |
| 246 | /* give the server some time to start properly and detect devices */ |
Xavier Ducrohet | a481d09 | 2009-05-21 17:47:43 -0700 | [diff] [blame] | 247 | adb_sleep_ms(3000); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 248 | // fall through to _adb_connect |
| 249 | } else { |
| 250 | // if server was running, check its version to make sure it is not out of date |
| 251 | char buf[100]; |
Nick Kralevich | 75e0645 | 2013-12-10 10:18:10 -0800 | [diff] [blame] | 252 | size_t n; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 253 | int version = ADB_SERVER_VERSION - 1; |
| 254 | |
| 255 | // if we have a file descriptor, then parse version result |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 256 | if (fd >= 0) { |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 257 | if(!ReadFdExactly(fd, buf, 4)) goto error; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 258 | |
| 259 | buf[4] = 0; |
| 260 | n = strtoul(buf, 0, 16); |
Nick Kralevich | 75e0645 | 2013-12-10 10:18:10 -0800 | [diff] [blame] | 261 | if(n > sizeof(buf)) goto error; |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 262 | if(!ReadFdExactly(fd, buf, n)) goto error; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 263 | adb_close(fd); |
| 264 | |
| 265 | if (sscanf(buf, "%04x", &version) != 1) goto error; |
| 266 | } else { |
| 267 | // if fd is -1, then check for "unknown host service", |
| 268 | // which would indicate a version of adb that does not support the version command |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 269 | if (*error == "unknown host service") { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 270 | return fd; |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 271 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | if(version != ADB_SERVER_VERSION) { |
| 275 | printf("adb server is out of date. killing...\n"); |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 276 | fd = _adb_connect("host:kill", error); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 277 | adb_close(fd); |
| 278 | |
| 279 | /* XXX can we better detect its death? */ |
| 280 | adb_sleep_ms(2000); |
| 281 | goto start_server; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // if the command is start-server, we are done. |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 286 | if (!strcmp(service, "host:start-server")) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 287 | return 0; |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 288 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 289 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 290 | fd = _adb_connect(service, error); |
| 291 | if (fd == -1) { |
| 292 | D("_adb_connect error: %s", error->c_str()); |
Brian Carlstrom | 93c91fa | 2013-10-18 13:58:48 -0700 | [diff] [blame] | 293 | } else if(fd == -2) { |
Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 294 | fprintf(stderr,"** daemon still not running\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 295 | } |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 296 | D("adb_connect: return fd %d\n", fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 297 | |
| 298 | return fd; |
| 299 | error: |
| 300 | adb_close(fd); |
| 301 | return -1; |
| 302 | } |
| 303 | |
| 304 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 305 | int adb_command(const char* service, std::string* error) { |
| 306 | int fd = adb_connect(service, error); |
| 307 | if (fd < 0) { |
| 308 | fprintf(stderr, "error: %s\n", error->c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 309 | return -1; |
| 310 | } |
| 311 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 312 | if (!adb_status(fd, error)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 313 | adb_close(fd); |
| 314 | return -1; |
| 315 | } |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 320 | char* adb_query(const char* service, std::string* error) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 321 | char buf[5]; |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 322 | unsigned long n; |
| 323 | char* tmp; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 324 | |
| 325 | D("adb_query: %s\n", service); |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 326 | int fd = adb_connect(service, error); |
| 327 | if (fd < 0) { |
| 328 | fprintf(stderr,"error: %s\n", error->c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 329 | return 0; |
| 330 | } |
| 331 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 332 | if (!ReadFdExactly(fd, buf, 4)) goto oops; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 333 | |
| 334 | buf[4] = 0; |
| 335 | n = strtoul(buf, 0, 16); |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame^] | 336 | // TODO: given that we just read a 4-byte hex length 0x????, why the test? |
| 337 | if (n >= 0xffff) { |
| 338 | *error = "reply is too long (>= 64KiB)"; |
Snild Dolkow | 2264e7c | 2014-01-30 10:08:38 +0100 | [diff] [blame] | 339 | goto oops; |
| 340 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 341 | |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 342 | tmp = reinterpret_cast<char*>(malloc(n + 1)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 343 | if(tmp == 0) goto oops; |
| 344 | |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 345 | if(!ReadFdExactly(fd, tmp, n) == 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 346 | tmp[n] = 0; |
| 347 | adb_close(fd); |
| 348 | return tmp; |
| 349 | } |
| 350 | free(tmp); |
| 351 | |
| 352 | oops: |
| 353 | adb_close(fd); |
| 354 | return 0; |
| 355 | } |