| 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 |  | 
| Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG ADB | 
| Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 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> | 
| Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 23 | #include <inttypes.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 24 | #include <limits.h> | 
 | 25 | #include <stdarg.h> | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 26 | #include <stdio.h> | 
 | 27 | #include <stdlib.h> | 
 | 28 | #include <string.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 29 | #include <sys/stat.h> | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 30 | #include <sys/types.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 31 |  | 
| Josh Gao | fd713e5 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 32 | #include <condition_variable> | 
 | 33 | #include <mutex> | 
| Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 34 | #include <string> | 
| Elliott Hughes | dbe91ee | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 35 | #include <thread> | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 36 | #include <vector> | 
 | 37 |  | 
| Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 38 | #include <android-base/stringprintf.h> | 
 | 39 | #include <android-base/strings.h> | 
| Josh Gao | fd713e5 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 40 | #include <android-base/thread_annotations.h> | 
| Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 41 | #include <cutils/sockets.h> | 
| Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 42 |  | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 43 | #include "adb_io.h" | 
| Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 44 | #include "adb_utils.h" | 
| Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 45 | #include "socket_spec.h" | 
| Josh Gao | 4602adb | 2016-11-15 18:55:47 -0800 | [diff] [blame] | 46 | #include "sysdeps/chrono.h" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 47 |  | 
| Elliott Hughes | 3bd73c1 | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 48 | static TransportType __adb_transport = kTransportAny; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 49 | static const char* __adb_serial = NULL; | 
| Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 50 | static TransportId __adb_transport_id = 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 |  | 
| Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 52 | static const char* __adb_server_socket_spec; | 
| Stefan Hilzinger | d0eacb8 | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 53 |  | 
| Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 54 | void adb_set_transport(TransportType type, const char* serial, TransportId transport_id) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 55 |     __adb_transport = type; | 
 | 56 |     __adb_serial = serial; | 
| Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 57 |     __adb_transport_id = transport_id; | 
 | 58 | } | 
 | 59 |  | 
 | 60 | void adb_get_transport(TransportType* type, const char** serial, TransportId* transport_id) { | 
 | 61 |     if (type) *type = __adb_transport; | 
 | 62 |     if (serial) *serial = __adb_serial; | 
 | 63 |     if (transport_id) *transport_id = __adb_transport_id; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 64 | } | 
 | 65 |  | 
| Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 66 | void adb_set_socket_spec(const char* socket_spec) { | 
| Elliott Hughes | 7f4ab76 | 2016-09-01 20:48:45 -0700 | [diff] [blame] | 67 |     if (__adb_server_socket_spec) { | 
 | 68 |         LOG(FATAL) << "attempted to reinitialize adb_server_socket_spec " << socket_spec << " (was " << __adb_server_socket_spec << ")"; | 
| Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 69 |     } | 
 | 70 |     __adb_server_socket_spec = socket_spec; | 
| Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 71 | } | 
 | 72 |  | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 73 | static int switch_socket_transport(int fd, std::string* error) { | 
| Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 74 |     std::string service; | 
| Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 75 |     if (__adb_transport_id) { | 
 | 76 |         service += "host:transport-id:"; | 
 | 77 |         service += std::to_string(__adb_transport_id); | 
 | 78 |     } else if (__adb_serial) { | 
| Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 79 |         service += "host:transport:"; | 
 | 80 |         service += __adb_serial; | 
 | 81 |     } else { | 
| Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 82 |         const char* transport_type = "???"; | 
| Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 83 |         switch (__adb_transport) { | 
 | 84 |           case kTransportUsb: | 
 | 85 |             transport_type = "transport-usb"; | 
 | 86 |             break; | 
 | 87 |           case kTransportLocal: | 
 | 88 |             transport_type = "transport-local"; | 
 | 89 |             break; | 
 | 90 |           case kTransportAny: | 
 | 91 |             transport_type = "transport-any"; | 
 | 92 |             break; | 
 | 93 |           case kTransportHost: | 
 | 94 |             // no switch necessary | 
 | 95 |             return 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 96 |         } | 
| Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 97 |         service += "host:"; | 
 | 98 |         service += transport_type; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 99 |     } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 100 |  | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 101 |     if (!SendProtocolString(fd, service)) { | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 102 |         *error = perror_str("write failure during connection"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 103 |         adb_close(fd); | 
 | 104 |         return -1; | 
 | 105 |     } | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 106 |     D("Switch transport in progress"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 107 |  | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 108 |     if (!adb_status(fd, error)) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 109 |         adb_close(fd); | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 110 |         D("Switch transport failed: %s", error->c_str()); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 111 |         return -1; | 
 | 112 |     } | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 113 |     D("Switch transport success"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 114 |     return 0; | 
 | 115 | } | 
 | 116 |  | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 117 | bool adb_status(int fd, std::string* error) { | 
 | 118 |     char buf[5]; | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 119 |     if (!ReadFdExactly(fd, buf, 4)) { | 
 | 120 |         *error = perror_str("protocol fault (couldn't read status)"); | 
 | 121 |         return false; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 122 |     } | 
 | 123 |  | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 124 |     if (!memcmp(buf, "OKAY", 4)) { | 
 | 125 |         return true; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 126 |     } | 
 | 127 |  | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 128 |     if (memcmp(buf, "FAIL", 4)) { | 
 | 129 |         *error = android::base::StringPrintf("protocol fault (status %02x %02x %02x %02x?!)", | 
 | 130 |                                              buf[0], buf[1], buf[2], buf[3]); | 
 | 131 |         return false; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 132 |     } | 
 | 133 |  | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 134 |     ReadProtocolString(fd, error, error); | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 135 |     return false; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 136 | } | 
 | 137 |  | 
| Josh Gao | b3c14ec | 2017-05-08 18:37:17 -0700 | [diff] [blame] | 138 | static int _adb_connect(const std::string& service, std::string* error) { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 139 |     D("_adb_connect: %s", service.c_str()); | 
| Josh Gao | a019f78 | 2017-06-16 15:34:34 -0700 | [diff] [blame] | 140 |     if (service.empty() || service.size() > MAX_PAYLOAD) { | 
| Spencer Low | 6001c87 | 2015-05-13 00:02:55 -0700 | [diff] [blame] | 141 |         *error = android::base::StringPrintf("bad service name length (%zd)", | 
 | 142 |                                              service.size()); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 143 |         return -1; | 
 | 144 |     } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 145 |  | 
| Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 146 |     std::string reason; | 
| Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 147 |     int fd = socket_spec_connect(__adb_server_socket_spec, &reason); | 
 | 148 |     if (fd < 0) { | 
 | 149 |         *error = android::base::StringPrintf("cannot connect to daemon at %s: %s", | 
 | 150 |                                              __adb_server_socket_spec, reason.c_str()); | 
 | 151 |         return -2; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 152 |     } | 
 | 153 |  | 
| Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 154 |     if (memcmp(&service[0], "host", 4) != 0 && switch_socket_transport(fd, error)) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 155 |         return -1; | 
 | 156 |     } | 
 | 157 |  | 
| Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 158 |     if (!SendProtocolString(fd, service)) { | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 159 |         *error = perror_str("write failure during connection"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 160 |         adb_close(fd); | 
 | 161 |         return -1; | 
 | 162 |     } | 
 | 163 |  | 
| Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 164 |     if (!adb_status(fd, error)) { | 
 | 165 |         adb_close(fd); | 
 | 166 |         return -1; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 167 |     } | 
 | 168 |  | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 169 |     D("_adb_connect: return fd %d", fd); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 170 |     return fd; | 
 | 171 | } | 
 | 172 |  | 
| Josh Gao | b3c14ec | 2017-05-08 18:37:17 -0700 | [diff] [blame] | 173 | bool adb_kill_server() { | 
 | 174 |     D("adb_kill_server"); | 
 | 175 |     std::string reason; | 
 | 176 |     int fd = socket_spec_connect(__adb_server_socket_spec, &reason); | 
 | 177 |     if (fd < 0) { | 
 | 178 |         fprintf(stderr, "cannot connect to daemon at %s: %s\n", __adb_server_socket_spec, | 
 | 179 |                 reason.c_str()); | 
 | 180 |         return true; | 
 | 181 |     } | 
 | 182 |  | 
 | 183 |     if (!SendProtocolString(fd, "host:kill")) { | 
 | 184 |         fprintf(stderr, "error: write failure during connection: %s\n", strerror(errno)); | 
 | 185 |         return false; | 
 | 186 |     } | 
 | 187 |  | 
 | 188 |     ReadOrderlyShutdown(fd); | 
 | 189 |     return true; | 
 | 190 | } | 
 | 191 |  | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 192 | int adb_connect(const std::string& service, std::string* error) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 193 |     // first query the adb server's version | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 194 |     int fd = _adb_connect("host:version", error); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 195 |  | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 196 |     D("adb_connect: service %s", service.c_str()); | 
| Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 197 |     if (fd == -2 && !is_local_socket_spec(__adb_server_socket_spec)) { | 
| Elliott Hughes | 1fc8f6e | 2017-04-18 14:34:16 -0700 | [diff] [blame] | 198 |         fprintf(stderr, "* cannot start server on remote host\n"); | 
| Spencer Low | f18fc08 | 2015-08-11 17:05:02 -0700 | [diff] [blame] | 199 |         // error is the original network connection error | 
| Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 200 |         return fd; | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 201 |     } else if (fd == -2) { | 
| Elliott Hughes | 1fc8f6e | 2017-04-18 14:34:16 -0700 | [diff] [blame] | 202 |         fprintf(stderr, "* daemon not running; starting now at %s\n", __adb_server_socket_spec); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 203 |     start_server: | 
| Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 204 |         if (launch_server(__adb_server_socket_spec)) { | 
| Elliott Hughes | 1fc8f6e | 2017-04-18 14:34:16 -0700 | [diff] [blame] | 205 |             fprintf(stderr, "* failed to start daemon\n"); | 
| Spencer Low | f18fc08 | 2015-08-11 17:05:02 -0700 | [diff] [blame] | 206 |             // launch_server() has already printed detailed error info, so just | 
 | 207 |             // return a generic error string about the overall adb_connect() | 
 | 208 |             // that the caller requested. | 
 | 209 |             *error = "cannot connect to daemon"; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 210 |             return -1; | 
 | 211 |         } else { | 
| Elliott Hughes | 1fc8f6e | 2017-04-18 14:34:16 -0700 | [diff] [blame] | 212 |             fprintf(stderr, "* daemon started successfully\n"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 213 |         } | 
| Josh Gao | fd713e5 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 214 |         // The server will wait until it detects all of its connected devices before acking. | 
 | 215 |         // Fall through to _adb_connect. | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 216 |     } else { | 
| Elliott Hughes | 5b73a10 | 2015-10-02 19:49:10 -0700 | [diff] [blame] | 217 |         // If a server is already running, check its version matches. | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 218 |         int version = ADB_SERVER_VERSION - 1; | 
 | 219 |  | 
| Elliott Hughes | 5b73a10 | 2015-10-02 19:49:10 -0700 | [diff] [blame] | 220 |         // If we have a file descriptor, then parse version result. | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 221 |         if (fd >= 0) { | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 222 |             std::string version_string; | 
 | 223 |             if (!ReadProtocolString(fd, &version_string, error)) { | 
| Elliott Hughes | 5b73a10 | 2015-10-02 19:49:10 -0700 | [diff] [blame] | 224 |                 adb_close(fd); | 
 | 225 |                 return -1; | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 226 |             } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 227 |  | 
| Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 228 |             ReadOrderlyShutdown(fd); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 229 |             adb_close(fd); | 
 | 230 |  | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 231 |             if (sscanf(&version_string[0], "%04x", &version) != 1) { | 
| Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 232 |                 *error = android::base::StringPrintf("cannot parse version string: %s", | 
 | 233 |                                                      version_string.c_str()); | 
| Spencer Low | f18fc08 | 2015-08-11 17:05:02 -0700 | [diff] [blame] | 234 |                 return -1; | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 235 |             } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 236 |         } else { | 
| Elliott Hughes | 5b73a10 | 2015-10-02 19:49:10 -0700 | [diff] [blame] | 237 |             // If fd is -1 check for "unknown host service" which would | 
 | 238 |             // indicate a version of adb that does not support the | 
| Spencer Low | 71635bb | 2015-08-05 19:26:50 -0700 | [diff] [blame] | 239 |             // version command, in which case we should fall-through to kill it. | 
 | 240 |             if (*error != "unknown host service") { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 241 |                 return fd; | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 242 |             } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 243 |         } | 
 | 244 |  | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 245 |         if (version != ADB_SERVER_VERSION) { | 
| Elliott Hughes | 1fc8f6e | 2017-04-18 14:34:16 -0700 | [diff] [blame] | 246 |             fprintf(stderr, "adb server version (%d) doesn't match this client (%d); killing...\n", | 
 | 247 |                     version, ADB_SERVER_VERSION); | 
| Josh Gao | b3c14ec | 2017-05-08 18:37:17 -0700 | [diff] [blame] | 248 |             adb_kill_server(); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 249 |             goto start_server; | 
 | 250 |         } | 
 | 251 |     } | 
 | 252 |  | 
 | 253 |     // if the command is start-server, we are done. | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 254 |     if (service == "host:start-server") { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 255 |         return 0; | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 256 |     } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 257 |  | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 258 |     fd = _adb_connect(service, error); | 
 | 259 |     if (fd == -1) { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 260 |         D("_adb_connect error: %s", error->c_str()); | 
| Brian Carlstrom | 93c91fa | 2013-10-18 13:58:48 -0700 | [diff] [blame] | 261 |     } else if(fd == -2) { | 
| Elliott Hughes | 1fc8f6e | 2017-04-18 14:34:16 -0700 | [diff] [blame] | 262 |         fprintf(stderr, "* daemon still not running\n"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 263 |     } | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 264 |     D("adb_connect: return fd %d", fd); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 265 |  | 
 | 266 |     return fd; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 267 | } | 
 | 268 |  | 
 | 269 |  | 
| Elliott Hughes | 424af02 | 2015-05-29 17:55:19 -0700 | [diff] [blame] | 270 | bool adb_command(const std::string& service) { | 
 | 271 |     std::string error; | 
 | 272 |     int fd = adb_connect(service, &error); | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 273 |     if (fd < 0) { | 
| Elliott Hughes | 424af02 | 2015-05-29 17:55:19 -0700 | [diff] [blame] | 274 |         fprintf(stderr, "error: %s\n", error.c_str()); | 
 | 275 |         return false; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 276 |     } | 
 | 277 |  | 
| Elliott Hughes | 424af02 | 2015-05-29 17:55:19 -0700 | [diff] [blame] | 278 |     if (!adb_status(fd, &error)) { | 
 | 279 |         fprintf(stderr, "error: %s\n", error.c_str()); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 280 |         adb_close(fd); | 
| Elliott Hughes | 424af02 | 2015-05-29 17:55:19 -0700 | [diff] [blame] | 281 |         return false; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 282 |     } | 
 | 283 |  | 
| Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 284 |     ReadOrderlyShutdown(fd); | 
 | 285 |     adb_close(fd); | 
| Elliott Hughes | 424af02 | 2015-05-29 17:55:19 -0700 | [diff] [blame] | 286 |     return true; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 287 | } | 
 | 288 |  | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 289 | bool adb_query(const std::string& service, std::string* result, std::string* error) { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 290 |     D("adb_query: %s", service.c_str()); | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 291 |     int fd = adb_connect(service, error); | 
 | 292 |     if (fd < 0) { | 
| Elliott Hughes | 3d5f60d | 2015-07-18 12:21:30 -0700 | [diff] [blame] | 293 |         return false; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 294 |     } | 
 | 295 |  | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 296 |     result->clear(); | 
 | 297 |     if (!ReadProtocolString(fd, result, error)) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 298 |         adb_close(fd); | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 299 |         return false; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 300 |     } | 
| Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 301 |  | 
 | 302 |     ReadOrderlyShutdown(fd); | 
 | 303 |     adb_close(fd); | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 304 |     return true; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 305 | } | 
| Josh Gao | f3f6a1d | 2016-01-31 19:12:26 -0800 | [diff] [blame] | 306 |  | 
| Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 307 | std::string format_host_command(const char* command) { | 
 | 308 |     if (__adb_transport_id) { | 
 | 309 |         return android::base::StringPrintf("host-transport-id:%" PRIu64 ":%s", __adb_transport_id, | 
 | 310 |                                            command); | 
 | 311 |     } else if (__adb_serial) { | 
 | 312 |         return android::base::StringPrintf("host-serial:%s:%s", __adb_serial, command); | 
| Josh Gao | f3f6a1d | 2016-01-31 19:12:26 -0800 | [diff] [blame] | 313 |     } | 
 | 314 |  | 
 | 315 |     const char* prefix = "host"; | 
| Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 316 |     if (__adb_transport == kTransportUsb) { | 
| Josh Gao | f3f6a1d | 2016-01-31 19:12:26 -0800 | [diff] [blame] | 317 |         prefix = "host-usb"; | 
| Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 318 |     } else if (__adb_transport == kTransportLocal) { | 
| Josh Gao | f3f6a1d | 2016-01-31 19:12:26 -0800 | [diff] [blame] | 319 |         prefix = "host-local"; | 
 | 320 |     } | 
 | 321 |     return android::base::StringPrintf("%s:%s", prefix, command); | 
 | 322 | } | 
 | 323 |  | 
 | 324 | bool adb_get_feature_set(FeatureSet* feature_set, std::string* error) { | 
 | 325 |     std::string result; | 
| Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 326 |     if (adb_query(format_host_command("features"), &result, error)) { | 
| Josh Gao | f3f6a1d | 2016-01-31 19:12:26 -0800 | [diff] [blame] | 327 |         *feature_set = StringToFeatureSet(result); | 
 | 328 |         return true; | 
 | 329 |     } | 
 | 330 |     feature_set->clear(); | 
 | 331 |     return false; | 
 | 332 | } |