| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2007 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 SERVICES | 
| Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 |  | 
|  | 19 | #include "sysdeps.h" | 
|  | 20 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 21 | #include <errno.h> | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 22 | #include <stddef.h> | 
|  | 23 | #include <stdio.h> | 
|  | 24 | #include <stdlib.h> | 
|  | 25 | #include <string.h> | 
|  | 26 |  | 
| Joshua Duong | d85f5c0 | 2019-11-20 14:18:43 -0800 | [diff] [blame] | 27 | #include <cstring> | 
| Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 28 | #include <thread> | 
| Josh Gao | 0ecc402 | 2019-02-22 13:41:55 -0800 | [diff] [blame] | 29 |  | 
| Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 30 | #include <android-base/stringprintf.h> | 
|  | 31 | #include <android-base/strings.h> | 
| Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 32 | #include <cutils/sockets.h> | 
| Elliott Hughes | 6c34bba | 2015-04-17 20:11:08 -0700 | [diff] [blame] | 33 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 34 | #include "adb.h" | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 35 | #include "adb_io.h" | 
| Josh Gao | 73a5ee4 | 2018-07-25 16:51:59 -0700 | [diff] [blame] | 36 | #include "adb_unique_fd.h" | 
| Elliott Hughes | 3d5f60d | 2015-07-18 12:21:30 -0700 | [diff] [blame] | 37 | #include "adb_utils.h" | 
| Joshua Duong | d85f5c0 | 2019-11-20 14:18:43 -0800 | [diff] [blame] | 38 | #include "adb_wifi.h" | 
| David Pursell | 70ef7b4 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 39 | #include "services.h" | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 40 | #include "socket_spec.h" | 
| Josh Gao | 0985547 | 2016-02-19 10:42:40 -0800 | [diff] [blame] | 41 | #include "sysdeps.h" | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 42 | #include "transport.h" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 |  | 
| Luis Hector Chavez | 095792c | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 44 | namespace { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 45 |  | 
| Josh Gao | 73a5ee4 | 2018-07-25 16:51:59 -0700 | [diff] [blame] | 46 | void service_bootstrap_func(std::string service_name, std::function<void(unique_fd)> func, | 
|  | 47 | unique_fd fd) { | 
| Luis Hector Chavez | 095792c | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 48 | adb_thread_setname(android::base::StringPrintf("%s svc %d", service_name.c_str(), fd.get())); | 
|  | 49 | func(std::move(fd)); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 50 | } | 
|  | 51 |  | 
| Josh Gao | 997cfac | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 52 | }  // namespace | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 53 |  | 
| Josh Gao | 73a5ee4 | 2018-07-25 16:51:59 -0700 | [diff] [blame] | 54 | unique_fd create_service_thread(const char* service_name, std::function<void(unique_fd)> func) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 55 | int s[2]; | 
| Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 56 | if (adb_socketpair(s)) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 57 | printf("cannot create service socket pair\n"); | 
| Josh Gao | 73a5ee4 | 2018-07-25 16:51:59 -0700 | [diff] [blame] | 58 | return unique_fd(); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 59 | } | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 60 | D("socketpair: (%d,%d)", s[0], s[1]); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 61 |  | 
| Jerry Zhang | 2f8c60b | 2017-02-10 17:45:27 -0800 | [diff] [blame] | 62 | #if !ADB_HOST | 
| Luis Hector Chavez | 095792c | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 63 | if (strcmp(service_name, "sync") == 0) { | 
| Jerry Zhang | 2f8c60b | 2017-02-10 17:45:27 -0800 | [diff] [blame] | 64 | // Set file sync service socket to maximum size | 
|  | 65 | int max_buf = LINUX_MAX_SOCKET_SIZE; | 
|  | 66 | adb_setsockopt(s[0], SOL_SOCKET, SO_SNDBUF, &max_buf, sizeof(max_buf)); | 
|  | 67 | adb_setsockopt(s[1], SOL_SOCKET, SO_SNDBUF, &max_buf, sizeof(max_buf)); | 
|  | 68 | } | 
| Josh Gao | 0ecc402 | 2019-02-22 13:41:55 -0800 | [diff] [blame] | 69 | #endif  // !ADB_HOST | 
| Jerry Zhang | 2f8c60b | 2017-02-10 17:45:27 -0800 | [diff] [blame] | 70 |  | 
| Josh Gao | 73a5ee4 | 2018-07-25 16:51:59 -0700 | [diff] [blame] | 71 | std::thread(service_bootstrap_func, service_name, func, unique_fd(s[1])).detach(); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 72 |  | 
| Josh Gao | 0ecc402 | 2019-02-22 13:41:55 -0800 | [diff] [blame] | 73 | D("service thread started, %d:%d", s[0], s[1]); | 
| Josh Gao | 73a5ee4 | 2018-07-25 16:51:59 -0700 | [diff] [blame] | 74 | return unique_fd(s[0]); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 75 | } | 
|  | 76 |  | 
| Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 77 | unique_fd service_to_fd(std::string_view name, atransport* transport) { | 
| Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 78 | unique_fd ret; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 79 |  | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 80 | if (is_socket_spec(name)) { | 
|  | 81 | std::string error; | 
| Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 82 | if (!socket_spec_connect(&ret, name, nullptr, nullptr, &error)) { | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 83 | LOG(ERROR) << "failed to connect to socket '" << name << "': " << error; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 84 | } | 
| Josh Gao | 997cfac | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 85 | } else { | 
| Benoit Goby | 9470c2f | 2013-02-20 15:04:53 -0800 | [diff] [blame] | 86 | #if !ADB_HOST | 
| Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 87 | ret = daemon_service_to_fd(name, transport); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 88 | #endif | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 89 | } | 
| Josh Gao | 997cfac | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 90 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 91 | if (ret >= 0) { | 
| Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 92 | close_on_exec(ret.get()); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 93 | } | 
| Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 94 | return ret; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 95 | } | 
|  | 96 |  | 
|  | 97 | #if ADB_HOST | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 98 | void connect_emulator(const std::string& port_spec, std::string* response) { | 
|  | 99 | std::vector<std::string> pieces = android::base::Split(port_spec, ","); | 
|  | 100 | if (pieces.size() != 2) { | 
|  | 101 | *response = android::base::StringPrintf("unable to parse '%s' as <console port>,<adb port>", | 
|  | 102 | port_spec.c_str()); | 
| Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 103 | return; | 
|  | 104 | } | 
|  | 105 |  | 
| Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 106 | int console_port = strtol(pieces[0].c_str(), nullptr, 0); | 
|  | 107 | int adb_port = strtol(pieces[1].c_str(), nullptr, 0); | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 108 | if (console_port <= 0 || adb_port <= 0) { | 
|  | 109 | *response = android::base::StringPrintf("Invalid port numbers: %s", port_spec.c_str()); | 
| Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 110 | return; | 
|  | 111 | } | 
|  | 112 |  | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 113 | // Check if the emulator is already known. | 
|  | 114 | // Note: There's a small but harmless race condition here: An emulator not | 
|  | 115 | // present just yet could be registered by another invocation right | 
|  | 116 | // after doing this check here. However, local_connect protects | 
|  | 117 | // against double-registration too. From here, a better error message | 
|  | 118 | // can be produced. In the case of the race condition, the very specific | 
|  | 119 | // error message won't be shown, but the data doesn't get corrupted. | 
| Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 120 | atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port); | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 121 | if (known_emulator != nullptr) { | 
|  | 122 | *response = android::base::StringPrintf("Emulator already registered on port %d", adb_port); | 
| Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 123 | return; | 
|  | 124 | } | 
|  | 125 |  | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 126 | // Preconditions met, try to connect to the emulator. | 
| Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 127 | std::string error; | 
|  | 128 | if (!local_connect_arbitrary_ports(console_port, adb_port, &error)) { | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 129 | *response = android::base::StringPrintf("Connected to emulator on ports %d,%d", | 
|  | 130 | console_port, adb_port); | 
| Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 131 | } else { | 
| Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 132 | *response = android::base::StringPrintf("Could not connect to emulator on ports %d,%d: %s", | 
|  | 133 | console_port, adb_port, error.c_str()); | 
| Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 134 | } | 
|  | 135 | } | 
|  | 136 |  | 
| Josh Gao | 73a5ee4 | 2018-07-25 16:51:59 -0700 | [diff] [blame] | 137 | static void connect_service(unique_fd fd, std::string host) { | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 138 | std::string response; | 
| Luis Hector Chavez | 095792c | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 139 | if (!strncmp(host.c_str(), "emu:", 4)) { | 
|  | 140 | connect_emulator(host.c_str() + 4, &response); | 
| Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 141 | } else { | 
| Greg Kaiser | e2125fd | 2019-03-26 11:58:53 -0700 | [diff] [blame] | 142 | connect_device(host, &response); | 
| Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 143 | } | 
|  | 144 |  | 
|  | 145 | // Send response for emulator and device | 
| Luis Hector Chavez | 095792c | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 146 | SendProtocolString(fd.get(), response); | 
| Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 147 | } | 
| Joshua Duong | d85f5c0 | 2019-11-20 14:18:43 -0800 | [diff] [blame] | 148 |  | 
|  | 149 | static void pair_service(unique_fd fd, std::string host, std::string password) { | 
|  | 150 | std::string response; | 
|  | 151 | adb_wifi_pair_device(host, password, response); | 
|  | 152 | SendProtocolString(fd.get(), response); | 
|  | 153 | } | 
| Elliott Hughes | a52b458 | 2020-03-10 16:57:47 -0700 | [diff] [blame] | 154 |  | 
|  | 155 | static void wait_service(unique_fd fd, std::string serial, TransportId transport_id, | 
|  | 156 | std::string spec) { | 
|  | 157 | std::vector<std::string> components = android::base::Split(spec, "-"); | 
|  | 158 | if (components.size() < 2) { | 
|  | 159 | SendFail(fd, "short wait-for-: " + spec); | 
|  | 160 | return; | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | TransportType transport_type; | 
|  | 164 | if (components[0] == "local") { | 
|  | 165 | transport_type = kTransportLocal; | 
|  | 166 | } else if (components[0] == "usb") { | 
|  | 167 | transport_type = kTransportUsb; | 
|  | 168 | } else if (components[0] == "any") { | 
|  | 169 | transport_type = kTransportAny; | 
|  | 170 | } else { | 
|  | 171 | SendFail(fd, "bad wait-for- transport: " + spec); | 
|  | 172 | return; | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | std::vector<ConnectionState> states; | 
|  | 176 | for (size_t i = 1; i < components.size(); ++i) { | 
|  | 177 | if (components[i] == "device") { | 
|  | 178 | states.push_back(kCsDevice); | 
|  | 179 | } else if (components[i] == "recovery") { | 
|  | 180 | states.push_back(kCsRecovery); | 
|  | 181 | } else if (components[i] == "rescue") { | 
|  | 182 | states.push_back(kCsRescue); | 
|  | 183 | } else if (components[i] == "sideload") { | 
|  | 184 | states.push_back(kCsSideload); | 
|  | 185 | } else if (components[i] == "bootloader") { | 
|  | 186 | states.push_back(kCsBootloader); | 
|  | 187 | } else if (components[i] == "any") { | 
|  | 188 | states.push_back(kCsAny); | 
|  | 189 | } else if (components[i] == "disconnect") { | 
|  | 190 | states.push_back(kCsOffline); | 
|  | 191 | } else { | 
|  | 192 | SendFail(fd, "bad wait-for- state: " + spec); | 
|  | 193 | return; | 
|  | 194 | } | 
|  | 195 | } | 
|  | 196 |  | 
|  | 197 | while (true) { | 
|  | 198 | bool is_ambiguous = false; | 
|  | 199 | std::string error = "unknown error"; | 
|  | 200 | atransport* t = | 
|  | 201 | acquire_one_transport(transport_type, !serial.empty() ? serial.c_str() : nullptr, | 
|  | 202 | transport_id, &is_ambiguous, &error); | 
|  | 203 |  | 
|  | 204 | for (const auto& state : states) { | 
|  | 205 | // wait-for-disconnect uses kCsOffline, we don't actually want to wait for 'offline'. | 
|  | 206 | if ((t == nullptr && state == kCsOffline) || (t != nullptr && state == kCsAny) || | 
|  | 207 | (t != nullptr && state == t->GetConnectionState())) { | 
|  | 208 | SendOkay(fd); | 
|  | 209 | return; | 
|  | 210 | } | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | if (is_ambiguous) { | 
|  | 214 | SendFail(fd, error); | 
|  | 215 | return; | 
|  | 216 | } | 
|  | 217 |  | 
|  | 218 | // Sleep before retrying. | 
|  | 219 | adb_pollfd pfd = {.fd = fd.get(), .events = POLLIN}; | 
|  | 220 | if (adb_poll(&pfd, 1, 100) != 0) { | 
|  | 221 | // The other end of the socket is closed, probably because the | 
|  | 222 | // client terminated. Bail out. | 
|  | 223 | SendFail(fd, error); | 
|  | 224 | return; | 
|  | 225 | } | 
|  | 226 | } | 
|  | 227 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 228 | #endif | 
|  | 229 |  | 
|  | 230 | #if ADB_HOST | 
| Josh Gao | 0ecc402 | 2019-02-22 13:41:55 -0800 | [diff] [blame] | 231 | asocket* host_service_to_socket(std::string_view name, std::string_view serial, | 
|  | 232 | TransportId transport_id) { | 
|  | 233 | if (name == "track-devices") { | 
| Josh Gao | b0c1802 | 2017-08-14 18:57:54 -0700 | [diff] [blame] | 234 | return create_device_tracker(false); | 
| Josh Gao | 0ecc402 | 2019-02-22 13:41:55 -0800 | [diff] [blame] | 235 | } else if (name == "track-devices-l") { | 
| Josh Gao | b0c1802 | 2017-08-14 18:57:54 -0700 | [diff] [blame] | 236 | return create_device_tracker(true); | 
| Elliott Hughes | b4dc7be | 2019-05-03 09:02:45 -0700 | [diff] [blame] | 237 | } else if (android::base::ConsumePrefix(&name, "wait-for-")) { | 
| Elliott Hughes | a52b458 | 2020-03-10 16:57:47 -0700 | [diff] [blame] | 238 | std::string spec(name); | 
|  | 239 | unique_fd fd = | 
|  | 240 | create_service_thread("wait", std::bind(wait_service, std::placeholders::_1, | 
|  | 241 | std::string(serial), transport_id, spec)); | 
| Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 242 | return create_local_socket(std::move(fd)); | 
| Elliott Hughes | b4dc7be | 2019-05-03 09:02:45 -0700 | [diff] [blame] | 243 | } else if (android::base::ConsumePrefix(&name, "connect:")) { | 
| Josh Gao | 0ecc402 | 2019-02-22 13:41:55 -0800 | [diff] [blame] | 244 | std::string host(name); | 
| Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 245 | unique_fd fd = create_service_thread( | 
|  | 246 | "connect", std::bind(connect_service, std::placeholders::_1, host)); | 
|  | 247 | return create_local_socket(std::move(fd)); | 
| Joshua Duong | d85f5c0 | 2019-11-20 14:18:43 -0800 | [diff] [blame] | 248 | } else if (android::base::ConsumePrefix(&name, "pair:")) { | 
|  | 249 | const char* divider = strchr(name.data(), ':'); | 
|  | 250 | if (!divider) { | 
|  | 251 | return nullptr; | 
|  | 252 | } | 
|  | 253 | std::string password(name.data(), divider); | 
|  | 254 | std::string host(divider + 1); | 
|  | 255 | unique_fd fd = create_service_thread( | 
|  | 256 | "pair", std::bind(pair_service, std::placeholders::_1, host, password)); | 
|  | 257 | return create_local_socket(std::move(fd)); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 258 | } | 
| Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 259 | return nullptr; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 260 | } | 
|  | 261 | #endif /* ADB_HOST */ |