The Android Open Source Project | 9ca14dc | 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 | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG SERVICES |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
| 19 | #include "sysdeps.h" |
| 20 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 21 | #include <errno.h> |
Dan Albert | b302d12 | 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 | |
Josh Gao | 0f3312a | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 27 | #include <thread> |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 28 | #include <android-base/stringprintf.h> |
| 29 | #include <android-base/strings.h> |
Elliott Hughes | 43df109 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 30 | #include <cutils/sockets.h> |
Elliott Hughes | e4b6479 | 2015-04-17 20:11:08 -0700 | [diff] [blame] | 31 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 32 | #include "adb.h" |
Dan Albert | 66a91b0 | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 33 | #include "adb_io.h" |
Josh Gao | 4c28dde | 2018-07-25 16:51:59 -0700 | [diff] [blame] | 34 | #include "adb_unique_fd.h" |
Elliott Hughes | 09ccf1f | 2015-07-18 12:21:30 -0700 | [diff] [blame] | 35 | #include "adb_utils.h" |
David Pursell | 22fc5e9 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 36 | #include "services.h" |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 37 | #include "socket_spec.h" |
Josh Gao | c1fab36 | 2016-02-19 10:42:40 -0800 | [diff] [blame] | 38 | #include "sysdeps.h" |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 39 | #include "transport.h" |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 40 | |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 41 | namespace { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 42 | |
Josh Gao | 4c28dde | 2018-07-25 16:51:59 -0700 | [diff] [blame] | 43 | void service_bootstrap_func(std::string service_name, std::function<void(unique_fd)> func, |
| 44 | unique_fd fd) { |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 45 | adb_thread_setname(android::base::StringPrintf("%s svc %d", service_name.c_str(), fd.get())); |
| 46 | func(std::move(fd)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 49 | } // namespace |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 50 | |
Josh Gao | 4c28dde | 2018-07-25 16:51:59 -0700 | [diff] [blame] | 51 | unique_fd create_service_thread(const char* service_name, std::function<void(unique_fd)> func) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 52 | int s[2]; |
Dan Albert | f30d73c | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 53 | if (adb_socketpair(s)) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 54 | printf("cannot create service socket pair\n"); |
Josh Gao | 4c28dde | 2018-07-25 16:51:59 -0700 | [diff] [blame] | 55 | return unique_fd(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 56 | } |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 57 | D("socketpair: (%d,%d)", s[0], s[1]); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 58 | |
Jerry Zhang | f0e239c | 2017-02-10 17:45:27 -0800 | [diff] [blame] | 59 | #if !ADB_HOST |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 60 | if (strcmp(service_name, "sync") == 0) { |
Jerry Zhang | f0e239c | 2017-02-10 17:45:27 -0800 | [diff] [blame] | 61 | // Set file sync service socket to maximum size |
| 62 | int max_buf = LINUX_MAX_SOCKET_SIZE; |
| 63 | adb_setsockopt(s[0], SOL_SOCKET, SO_SNDBUF, &max_buf, sizeof(max_buf)); |
| 64 | adb_setsockopt(s[1], SOL_SOCKET, SO_SNDBUF, &max_buf, sizeof(max_buf)); |
| 65 | } |
| 66 | #endif // !ADB_HOST |
| 67 | |
Josh Gao | 4c28dde | 2018-07-25 16:51:59 -0700 | [diff] [blame] | 68 | std::thread(service_bootstrap_func, service_name, func, unique_fd(s[1])).detach(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 69 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 70 | D("service thread started, %d:%d",s[0], s[1]); |
Josh Gao | 4c28dde | 2018-07-25 16:51:59 -0700 | [diff] [blame] | 71 | return unique_fd(s[0]); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Josh Gao | ebc1c31 | 2018-04-13 12:17:03 -0700 | [diff] [blame] | 74 | int service_to_fd(const char* name, atransport* transport) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 75 | int ret = -1; |
| 76 | |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 77 | if (is_socket_spec(name)) { |
| 78 | std::string error; |
| 79 | ret = socket_spec_connect(name, &error); |
| 80 | if (ret < 0) { |
| 81 | LOG(ERROR) << "failed to connect to socket '" << name << "': " << error; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 82 | } |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 83 | } else { |
Benoit Goby | 12dc369 | 2013-02-20 15:04:53 -0800 | [diff] [blame] | 84 | #if !ADB_HOST |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 85 | ret = daemon_service_to_fd(name, transport).release(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 86 | #endif |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 87 | } |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 88 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 89 | if (ret >= 0) { |
| 90 | close_on_exec(ret); |
| 91 | } |
| 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | #if ADB_HOST |
| 96 | struct state_info { |
Elliott Hughes | 3aec2ba | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 97 | TransportType transport_type; |
Leo Sartre | 6cd9bc3 | 2015-11-27 18:56:48 +0100 | [diff] [blame] | 98 | std::string serial; |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 99 | TransportId transport_id; |
Dan Albert | 9a50f4c | 2015-05-18 16:43:57 -0700 | [diff] [blame] | 100 | ConnectionState state; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 101 | }; |
| 102 | |
Leo Sartre | 6cd9bc3 | 2015-11-27 18:56:48 +0100 | [diff] [blame] | 103 | static void wait_for_state(int fd, void* data) { |
| 104 | std::unique_ptr<state_info> sinfo(reinterpret_cast<state_info*>(data)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 105 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 106 | D("wait_for_state %d", sinfo->state); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 107 | |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 108 | while (true) { |
| 109 | bool is_ambiguous = false; |
| 110 | std::string error = "unknown error"; |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 111 | const char* serial = sinfo->serial.length() ? sinfo->serial.c_str() : nullptr; |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 112 | atransport* t = acquire_one_transport(sinfo->transport_type, serial, sinfo->transport_id, |
| 113 | &is_ambiguous, &error); |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 114 | if (t != nullptr && (sinfo->state == kCsAny || sinfo->state == t->GetConnectionState())) { |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 115 | SendOkay(fd); |
| 116 | break; |
| 117 | } else if (!is_ambiguous) { |
Josh Gao | c1fab36 | 2016-02-19 10:42:40 -0800 | [diff] [blame] | 118 | adb_pollfd pfd = {.fd = fd, .events = POLLIN }; |
| 119 | int rc = adb_poll(&pfd, 1, 1000); |
| 120 | if (rc < 0) { |
| 121 | SendFail(fd, error); |
| 122 | break; |
| 123 | } else if (rc > 0 && (pfd.revents & POLLHUP) != 0) { |
| 124 | // The other end of the socket is closed, probably because the other side was |
| 125 | // terminated, bail out. |
| 126 | break; |
| 127 | } |
| 128 | |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 129 | // Try again... |
| 130 | } else { |
| 131 | SendFail(fd, error); |
| 132 | break; |
| 133 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 134 | } |
| 135 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 136 | adb_close(fd); |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 137 | D("wait_for_state is done"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 138 | } |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 139 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 140 | void connect_emulator(const std::string& port_spec, std::string* response) { |
| 141 | std::vector<std::string> pieces = android::base::Split(port_spec, ","); |
| 142 | if (pieces.size() != 2) { |
| 143 | *response = android::base::StringPrintf("unable to parse '%s' as <console port>,<adb port>", |
| 144 | port_spec.c_str()); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 145 | return; |
| 146 | } |
| 147 | |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 148 | int console_port = strtol(pieces[0].c_str(), nullptr, 0); |
| 149 | int adb_port = strtol(pieces[1].c_str(), nullptr, 0); |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 150 | if (console_port <= 0 || adb_port <= 0) { |
| 151 | *response = android::base::StringPrintf("Invalid port numbers: %s", port_spec.c_str()); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 152 | return; |
| 153 | } |
| 154 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 155 | // Check if the emulator is already known. |
| 156 | // Note: There's a small but harmless race condition here: An emulator not |
| 157 | // present just yet could be registered by another invocation right |
| 158 | // after doing this check here. However, local_connect protects |
| 159 | // against double-registration too. From here, a better error message |
| 160 | // can be produced. In the case of the race condition, the very specific |
| 161 | // error message won't be shown, but the data doesn't get corrupted. |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 162 | atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port); |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 163 | if (known_emulator != nullptr) { |
| 164 | *response = android::base::StringPrintf("Emulator already registered on port %d", adb_port); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 165 | return; |
| 166 | } |
| 167 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 168 | // Preconditions met, try to connect to the emulator. |
Elliott Hughes | 43df109 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 169 | std::string error; |
| 170 | if (!local_connect_arbitrary_ports(console_port, adb_port, &error)) { |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 171 | *response = android::base::StringPrintf("Connected to emulator on ports %d,%d", |
| 172 | console_port, adb_port); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 173 | } else { |
Elliott Hughes | 43df109 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 174 | *response = android::base::StringPrintf("Could not connect to emulator on ports %d,%d: %s", |
| 175 | console_port, adb_port, error.c_str()); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Josh Gao | 4c28dde | 2018-07-25 16:51:59 -0700 | [diff] [blame] | 179 | static void connect_service(unique_fd fd, std::string host) { |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 180 | std::string response; |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 181 | if (!strncmp(host.c_str(), "emu:", 4)) { |
| 182 | connect_emulator(host.c_str() + 4, &response); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 183 | } else { |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 184 | connect_device(host.c_str(), &response); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | // Send response for emulator and device |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 188 | SendProtocolString(fd.get(), response); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 189 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 190 | #endif |
| 191 | |
| 192 | #if ADB_HOST |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 193 | asocket* host_service_to_socket(const char* name, const char* serial, TransportId transport_id) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 194 | if (!strcmp(name,"track-devices")) { |
Josh Gao | 3212463 | 2017-08-14 18:57:54 -0700 | [diff] [blame] | 195 | return create_device_tracker(false); |
| 196 | } else if (!strcmp(name, "track-devices-l")) { |
| 197 | return create_device_tracker(true); |
Leo Sartre | 6cd9bc3 | 2015-11-27 18:56:48 +0100 | [diff] [blame] | 198 | } else if (android::base::StartsWith(name, "wait-for-")) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 199 | name += strlen("wait-for-"); |
| 200 | |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 201 | std::unique_ptr<state_info> sinfo = std::make_unique<state_info>(); |
Leo Sartre | 6cd9bc3 | 2015-11-27 18:56:48 +0100 | [diff] [blame] | 202 | if (sinfo == nullptr) { |
| 203 | fprintf(stderr, "couldn't allocate state_info: %s", strerror(errno)); |
| 204 | return nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 205 | } |
| 206 | |
Leo Sartre | 6cd9bc3 | 2015-11-27 18:56:48 +0100 | [diff] [blame] | 207 | if (serial) sinfo->serial = serial; |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 208 | sinfo->transport_id = transport_id; |
Leo Sartre | 6cd9bc3 | 2015-11-27 18:56:48 +0100 | [diff] [blame] | 209 | |
| 210 | if (android::base::StartsWith(name, "local")) { |
| 211 | name += strlen("local"); |
| 212 | sinfo->transport_type = kTransportLocal; |
| 213 | } else if (android::base::StartsWith(name, "usb")) { |
| 214 | name += strlen("usb"); |
| 215 | sinfo->transport_type = kTransportUsb; |
| 216 | } else if (android::base::StartsWith(name, "any")) { |
| 217 | name += strlen("any"); |
| 218 | sinfo->transport_type = kTransportAny; |
| 219 | } else { |
| 220 | return nullptr; |
| 221 | } |
| 222 | |
| 223 | if (!strcmp(name, "-device")) { |
| 224 | sinfo->state = kCsDevice; |
| 225 | } else if (!strcmp(name, "-recovery")) { |
| 226 | sinfo->state = kCsRecovery; |
| 227 | } else if (!strcmp(name, "-sideload")) { |
| 228 | sinfo->state = kCsSideload; |
| 229 | } else if (!strcmp(name, "-bootloader")) { |
| 230 | sinfo->state = kCsBootloader; |
Josh Gao | 071328d | 2016-04-13 12:18:58 -0700 | [diff] [blame] | 231 | } else if (!strcmp(name, "-any")) { |
| 232 | sinfo->state = kCsAny; |
Leo Sartre | 6cd9bc3 | 2015-11-27 18:56:48 +0100 | [diff] [blame] | 233 | } else { |
| 234 | return nullptr; |
| 235 | } |
| 236 | |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 237 | int fd = create_service_thread( |
| 238 | "wait", std::bind(wait_for_state, std::placeholders::_1, sinfo.get())) |
| 239 | .release(); |
Ting-Yuan Huang | aa923c1 | 2017-08-15 15:07:21 -0700 | [diff] [blame] | 240 | if (fd != -1) { |
| 241 | sinfo.release(); |
| 242 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 243 | return create_local_socket(fd); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 244 | } else if (!strncmp(name, "connect:", 8)) { |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 245 | std::string host(name + strlen("connect:")); |
| 246 | int fd = create_service_thread("connect", |
| 247 | std::bind(connect_service, std::placeholders::_1, host)) |
| 248 | .release(); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 249 | return create_local_socket(fd); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 250 | } |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 251 | return nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 252 | } |
| 253 | #endif /* ADB_HOST */ |