blob: 853d658976e149c10aa65b839186084bfb92ec00 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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 Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG SERVICES
Dan Albert33134262015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080021#include <errno.h>
Dan Albert76649012015-02-24 15:51:19 -080022#include <stddef.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
Joshua Duongd85f5c02019-11-20 14:18:43 -080027#include <cstring>
Josh Gaoe1dacfc2017-04-12 17:00:49 -070028#include <thread>
Josh Gao0ecc4022019-02-22 13:41:55 -080029
Elliott Hughes4f713192015-12-04 22:00:26 -080030#include <android-base/stringprintf.h>
31#include <android-base/strings.h>
Elliott Hughes381cfa92015-07-23 17:12:58 -070032#include <cutils/sockets.h>
Elliott Hughes6c34bba2015-04-17 20:11:08 -070033
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034#include "adb.h"
Dan Albertcc731cc2015-02-24 21:26:58 -080035#include "adb_io.h"
Josh Gao73a5ee42018-07-25 16:51:59 -070036#include "adb_unique_fd.h"
Elliott Hughes3d5f60d2015-07-18 12:21:30 -070037#include "adb_utils.h"
Joshua Duongd85f5c02019-11-20 14:18:43 -080038#include "adb_wifi.h"
David Pursell70ef7b42015-09-30 13:35:42 -070039#include "services.h"
Josh Gaocfb21412016-08-24 18:38:44 -070040#include "socket_spec.h"
Josh Gao09855472016-02-19 10:42:40 -080041#include "sysdeps.h"
Dan Albert76649012015-02-24 15:51:19 -080042#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043
Luis Hector Chavez095792c2018-07-18 19:40:12 -070044namespace {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045
Josh Gao73a5ee42018-07-25 16:51:59 -070046void service_bootstrap_func(std::string service_name, std::function<void(unique_fd)> func,
47 unique_fd fd) {
Luis Hector Chavez095792c2018-07-18 19:40:12 -070048 adb_thread_setname(android::base::StringPrintf("%s svc %d", service_name.c_str(), fd.get()));
49 func(std::move(fd));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050}
51
Josh Gao997cfac2018-07-25 18:15:52 -070052} // namespace
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053
Josh Gao73a5ee42018-07-25 16:51:59 -070054unique_fd create_service_thread(const char* service_name, std::function<void(unique_fd)> func) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055 int s[2];
Dan Albertbac34742015-02-25 17:51:28 -080056 if (adb_socketpair(s)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057 printf("cannot create service socket pair\n");
Josh Gao73a5ee42018-07-25 16:51:59 -070058 return unique_fd();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -070060 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061
Jerry Zhang2f8c60b2017-02-10 17:45:27 -080062#if !ADB_HOST
Luis Hector Chavez095792c2018-07-18 19:40:12 -070063 if (strcmp(service_name, "sync") == 0) {
Jerry Zhang2f8c60b2017-02-10 17:45:27 -080064 // 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 Gao0ecc4022019-02-22 13:41:55 -080069#endif // !ADB_HOST
Jerry Zhang2f8c60b2017-02-10 17:45:27 -080070
Josh Gao73a5ee42018-07-25 16:51:59 -070071 std::thread(service_bootstrap_func, service_name, func, unique_fd(s[1])).detach();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072
Josh Gao0ecc4022019-02-22 13:41:55 -080073 D("service thread started, %d:%d", s[0], s[1]);
Josh Gao73a5ee42018-07-25 16:51:59 -070074 return unique_fd(s[0]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075}
76
Josh Gao74ccdf92019-01-23 15:36:42 -080077unique_fd service_to_fd(std::string_view name, atransport* transport) {
Cody Schuffelenaf0e2202019-01-02 14:17:29 -080078 unique_fd ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079
Josh Gaocfb21412016-08-24 18:38:44 -070080 if (is_socket_spec(name)) {
81 std::string error;
Cody Schuffelenaf0e2202019-01-02 14:17:29 -080082 if (!socket_spec_connect(&ret, name, nullptr, nullptr, &error)) {
Josh Gaocfb21412016-08-24 18:38:44 -070083 LOG(ERROR) << "failed to connect to socket '" << name << "': " << error;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080084 }
Josh Gao997cfac2018-07-25 18:15:52 -070085 } else {
Benoit Goby9470c2f2013-02-20 15:04:53 -080086#if !ADB_HOST
Cody Schuffelenaf0e2202019-01-02 14:17:29 -080087 ret = daemon_service_to_fd(name, transport);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089 }
Josh Gao997cfac2018-07-25 18:15:52 -070090
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091 if (ret >= 0) {
Josh Gao74ccdf92019-01-23 15:36:42 -080092 close_on_exec(ret.get());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093 }
Josh Gao74ccdf92019-01-23 15:36:42 -080094 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080095}
96
97#if ADB_HOST
98struct state_info {
Elliott Hughes3bd73c12015-05-05 13:10:43 -070099 TransportType transport_type;
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100100 std::string serial;
Josh Gaob122b172017-08-16 16:57:01 -0700101 TransportId transport_id;
Dan Albertdcd78a12015-05-18 16:43:57 -0700102 ConnectionState state;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103};
104
Josh Gao499601b2019-04-18 16:46:42 -0700105static void wait_for_state(unique_fd fd, state_info* sinfo) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700106 D("wait_for_state %d", sinfo->state);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800107
Elliott Hughes8d28e192015-10-07 14:55:10 -0700108 while (true) {
109 bool is_ambiguous = false;
110 std::string error = "unknown error";
Yi Kongaed415c2018-07-13 18:15:16 -0700111 const char* serial = sinfo->serial.length() ? sinfo->serial.c_str() : nullptr;
Josh Gaob122b172017-08-16 16:57:01 -0700112 atransport* t = acquire_one_transport(sinfo->transport_type, serial, sinfo->transport_id,
113 &is_ambiguous, &error);
Josh Gao1e9e4712019-02-22 14:01:36 -0800114 if (sinfo->state == kCsOffline) {
115 // wait-for-disconnect uses kCsOffline, we don't actually want to wait for 'offline'.
116 if (t == nullptr) {
117 SendOkay(fd);
118 break;
119 }
120 } else if (t != nullptr &&
121 (sinfo->state == kCsAny || sinfo->state == t->GetConnectionState())) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700122 SendOkay(fd);
123 break;
Josh Gao1e9e4712019-02-22 14:01:36 -0800124 }
125
126 if (!is_ambiguous) {
Josh Gao499601b2019-04-18 16:46:42 -0700127 adb_pollfd pfd = {.fd = fd.get(), .events = POLLIN};
Josh Gao1e9e4712019-02-22 14:01:36 -0800128 int rc = adb_poll(&pfd, 1, 100);
Josh Gao09855472016-02-19 10:42:40 -0800129 if (rc < 0) {
130 SendFail(fd, error);
131 break;
132 } else if (rc > 0 && (pfd.revents & POLLHUP) != 0) {
133 // The other end of the socket is closed, probably because the other side was
134 // terminated, bail out.
135 break;
136 }
137
Elliott Hughes8d28e192015-10-07 14:55:10 -0700138 // Try again...
139 } else {
140 SendFail(fd, error);
141 break;
142 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143 }
144
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700145 D("wait_for_state is done");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146}
Benoit Goby1c45ee92013-03-29 18:22:36 -0700147
Elliott Hughese67f1f82015-04-30 17:32:03 -0700148void connect_emulator(const std::string& port_spec, std::string* response) {
149 std::vector<std::string> pieces = android::base::Split(port_spec, ",");
150 if (pieces.size() != 2) {
151 *response = android::base::StringPrintf("unable to parse '%s' as <console port>,<adb port>",
152 port_spec.c_str());
Benoit Goby1c45ee92013-03-29 18:22:36 -0700153 return;
154 }
155
Yi Kongaed415c2018-07-13 18:15:16 -0700156 int console_port = strtol(pieces[0].c_str(), nullptr, 0);
157 int adb_port = strtol(pieces[1].c_str(), nullptr, 0);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700158 if (console_port <= 0 || adb_port <= 0) {
159 *response = android::base::StringPrintf("Invalid port numbers: %s", port_spec.c_str());
Benoit Goby1c45ee92013-03-29 18:22:36 -0700160 return;
161 }
162
Elliott Hughese67f1f82015-04-30 17:32:03 -0700163 // Check if the emulator is already known.
164 // Note: There's a small but harmless race condition here: An emulator not
165 // present just yet could be registered by another invocation right
166 // after doing this check here. However, local_connect protects
167 // against double-registration too. From here, a better error message
168 // can be produced. In the case of the race condition, the very specific
169 // error message won't be shown, but the data doesn't get corrupted.
Benoit Goby1c45ee92013-03-29 18:22:36 -0700170 atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700171 if (known_emulator != nullptr) {
172 *response = android::base::StringPrintf("Emulator already registered on port %d", adb_port);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700173 return;
174 }
175
Elliott Hughese67f1f82015-04-30 17:32:03 -0700176 // Preconditions met, try to connect to the emulator.
Elliott Hughes381cfa92015-07-23 17:12:58 -0700177 std::string error;
178 if (!local_connect_arbitrary_ports(console_port, adb_port, &error)) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700179 *response = android::base::StringPrintf("Connected to emulator on ports %d,%d",
180 console_port, adb_port);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700181 } else {
Elliott Hughes381cfa92015-07-23 17:12:58 -0700182 *response = android::base::StringPrintf("Could not connect to emulator on ports %d,%d: %s",
183 console_port, adb_port, error.c_str());
Benoit Goby1c45ee92013-03-29 18:22:36 -0700184 }
185}
186
Josh Gao73a5ee42018-07-25 16:51:59 -0700187static void connect_service(unique_fd fd, std::string host) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700188 std::string response;
Luis Hector Chavez095792c2018-07-18 19:40:12 -0700189 if (!strncmp(host.c_str(), "emu:", 4)) {
190 connect_emulator(host.c_str() + 4, &response);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700191 } else {
Greg Kaisere2125fd2019-03-26 11:58:53 -0700192 connect_device(host, &response);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700193 }
194
195 // Send response for emulator and device
Luis Hector Chavez095792c2018-07-18 19:40:12 -0700196 SendProtocolString(fd.get(), response);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700197}
Joshua Duongd85f5c02019-11-20 14:18:43 -0800198
199static void pair_service(unique_fd fd, std::string host, std::string password) {
200 std::string response;
201 adb_wifi_pair_device(host, password, response);
202 SendProtocolString(fd.get(), response);
203}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204#endif
205
206#if ADB_HOST
Josh Gao0ecc4022019-02-22 13:41:55 -0800207asocket* host_service_to_socket(std::string_view name, std::string_view serial,
208 TransportId transport_id) {
209 if (name == "track-devices") {
Josh Gaob0c18022017-08-14 18:57:54 -0700210 return create_device_tracker(false);
Josh Gao0ecc4022019-02-22 13:41:55 -0800211 } else if (name == "track-devices-l") {
Josh Gaob0c18022017-08-14 18:57:54 -0700212 return create_device_tracker(true);
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700213 } else if (android::base::ConsumePrefix(&name, "wait-for-")) {
Josh Gao74ccdf92019-01-23 15:36:42 -0800214 std::shared_ptr<state_info> sinfo = std::make_shared<state_info>();
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100215 if (sinfo == nullptr) {
216 fprintf(stderr, "couldn't allocate state_info: %s", strerror(errno));
217 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800218 }
219
Josh Gao0ecc4022019-02-22 13:41:55 -0800220 sinfo->serial = serial;
Josh Gaob122b172017-08-16 16:57:01 -0700221 sinfo->transport_id = transport_id;
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100222
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700223 if (android::base::ConsumePrefix(&name, "local")) {
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100224 sinfo->transport_type = kTransportLocal;
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700225 } else if (android::base::ConsumePrefix(&name, "usb")) {
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100226 sinfo->transport_type = kTransportUsb;
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700227 } else if (android::base::ConsumePrefix(&name, "any")) {
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100228 sinfo->transport_type = kTransportAny;
229 } else {
230 return nullptr;
231 }
232
Josh Gao0ecc4022019-02-22 13:41:55 -0800233 if (name == "-device") {
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100234 sinfo->state = kCsDevice;
Josh Gao0ecc4022019-02-22 13:41:55 -0800235 } else if (name == "-recovery") {
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100236 sinfo->state = kCsRecovery;
Tao Bao55d407e2019-04-07 23:24:03 -0700237 } else if (name == "-rescue") {
238 sinfo->state = kCsRescue;
Josh Gao0ecc4022019-02-22 13:41:55 -0800239 } else if (name == "-sideload") {
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100240 sinfo->state = kCsSideload;
Josh Gao0ecc4022019-02-22 13:41:55 -0800241 } else if (name == "-bootloader") {
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100242 sinfo->state = kCsBootloader;
Josh Gao0ecc4022019-02-22 13:41:55 -0800243 } else if (name == "-any") {
Josh Gao86441c32016-04-13 12:18:58 -0700244 sinfo->state = kCsAny;
Josh Gao1e9e4712019-02-22 14:01:36 -0800245 } else if (name == "-disconnect") {
246 sinfo->state = kCsOffline;
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100247 } else {
248 return nullptr;
249 }
250
Josh Gao499601b2019-04-18 16:46:42 -0700251 unique_fd fd = create_service_thread(
252 "wait", [sinfo](unique_fd fd) { wait_for_state(std::move(fd), sinfo.get()); });
Josh Gao74ccdf92019-01-23 15:36:42 -0800253 return create_local_socket(std::move(fd));
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700254 } else if (android::base::ConsumePrefix(&name, "connect:")) {
Josh Gao0ecc4022019-02-22 13:41:55 -0800255 std::string host(name);
Josh Gao74ccdf92019-01-23 15:36:42 -0800256 unique_fd fd = create_service_thread(
257 "connect", std::bind(connect_service, std::placeholders::_1, host));
258 return create_local_socket(std::move(fd));
Joshua Duongd85f5c02019-11-20 14:18:43 -0800259 } else if (android::base::ConsumePrefix(&name, "pair:")) {
260 const char* divider = strchr(name.data(), ':');
261 if (!divider) {
262 return nullptr;
263 }
264 std::string password(name.data(), divider);
265 std::string host(divider + 1);
266 unique_fd fd = create_service_thread(
267 "pair", std::bind(pair_service, std::placeholders::_1, host, password));
268 return create_local_socket(std::move(fd));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800269 }
Yi Kongaed415c2018-07-13 18:15:16 -0700270 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271}
272#endif /* ADB_HOST */