blob: 46cab6e5ca658442a3d70e6e72197f15b8a89313 [file] [log] [blame]
The Android Open Source Project9ca14dc2009-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 Cui19bec5b2015-09-22 15:52:57 -070017#define TRACE_TAG SERVICES
Dan Albertdb6fe642015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080021#include <errno.h>
Dan Albertb302d122015-02-24 15:51:19 -080022#include <stddef.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
Josh Gao0f3312a2017-04-12 17:00:49 -070027#include <thread>
Josh Gao0565ae02019-02-22 13:41:55 -080028
Elliott Hughesf55ead92015-12-04 22:00:26 -080029#include <android-base/stringprintf.h>
30#include <android-base/strings.h>
Elliott Hughes43df1092015-07-23 17:12:58 -070031#include <cutils/sockets.h>
Elliott Hughese4b64792015-04-17 20:11:08 -070032
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080033#include "adb.h"
Dan Albert66a91b02015-02-24 21:26:58 -080034#include "adb_io.h"
Josh Gao4c28dde2018-07-25 16:51:59 -070035#include "adb_unique_fd.h"
Elliott Hughes09ccf1f2015-07-18 12:21:30 -070036#include "adb_utils.h"
David Pursell22fc5e92015-09-30 13:35:42 -070037#include "services.h"
Josh Gao4a5a95d2016-08-24 18:38:44 -070038#include "socket_spec.h"
Josh Gaoc1fab362016-02-19 10:42:40 -080039#include "sysdeps.h"
Dan Albertb302d122015-02-24 15:51:19 -080040#include "transport.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080041
Luis Hector Chavezce7a2842018-07-18 19:40:12 -070042namespace {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080043
Josh Gao4c28dde2018-07-25 16:51:59 -070044void service_bootstrap_func(std::string service_name, std::function<void(unique_fd)> func,
45 unique_fd fd) {
Luis Hector Chavezce7a2842018-07-18 19:40:12 -070046 adb_thread_setname(android::base::StringPrintf("%s svc %d", service_name.c_str(), fd.get()));
47 func(std::move(fd));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080048}
49
Josh Gao5607e922018-07-25 18:15:52 -070050} // namespace
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080051
Josh Gao4c28dde2018-07-25 16:51:59 -070052unique_fd create_service_thread(const char* service_name, std::function<void(unique_fd)> func) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080053 int s[2];
Dan Albertf30d73c2015-02-25 17:51:28 -080054 if (adb_socketpair(s)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080055 printf("cannot create service socket pair\n");
Josh Gao4c28dde2018-07-25 16:51:59 -070056 return unique_fd();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080057 }
Yabin Cui815ad882015-09-02 17:44:28 -070058 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080059
Jerry Zhangf0e239c2017-02-10 17:45:27 -080060#if !ADB_HOST
Luis Hector Chavezce7a2842018-07-18 19:40:12 -070061 if (strcmp(service_name, "sync") == 0) {
Jerry Zhangf0e239c2017-02-10 17:45:27 -080062 // Set file sync service socket to maximum size
63 int max_buf = LINUX_MAX_SOCKET_SIZE;
64 adb_setsockopt(s[0], SOL_SOCKET, SO_SNDBUF, &max_buf, sizeof(max_buf));
65 adb_setsockopt(s[1], SOL_SOCKET, SO_SNDBUF, &max_buf, sizeof(max_buf));
66 }
Josh Gao0565ae02019-02-22 13:41:55 -080067#endif // !ADB_HOST
Jerry Zhangf0e239c2017-02-10 17:45:27 -080068
Josh Gao4c28dde2018-07-25 16:51:59 -070069 std::thread(service_bootstrap_func, service_name, func, unique_fd(s[1])).detach();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080070
Josh Gao0565ae02019-02-22 13:41:55 -080071 D("service thread started, %d:%d", s[0], s[1]);
Josh Gao4c28dde2018-07-25 16:51:59 -070072 return unique_fd(s[0]);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080073}
74
Josh Gaoc2705962019-01-23 15:36:42 -080075unique_fd service_to_fd(std::string_view name, atransport* transport) {
Cody Schuffelen331a9082019-01-02 14:17:29 -080076 unique_fd ret;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080077
Josh Gao4a5a95d2016-08-24 18:38:44 -070078 if (is_socket_spec(name)) {
79 std::string error;
Cody Schuffelen331a9082019-01-02 14:17:29 -080080 if (!socket_spec_connect(&ret, name, nullptr, nullptr, &error)) {
Josh Gao4a5a95d2016-08-24 18:38:44 -070081 LOG(ERROR) << "failed to connect to socket '" << name << "': " << error;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080082 }
Josh Gao5607e922018-07-25 18:15:52 -070083 } else {
Benoit Goby12dc3692013-02-20 15:04:53 -080084#if !ADB_HOST
Cody Schuffelen331a9082019-01-02 14:17:29 -080085 ret = daemon_service_to_fd(name, transport);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080086#endif
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080087 }
Josh Gao5607e922018-07-25 18:15:52 -070088
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080089 if (ret >= 0) {
Josh Gaoc2705962019-01-23 15:36:42 -080090 close_on_exec(ret.get());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080091 }
Josh Gaoc2705962019-01-23 15:36:42 -080092 return ret;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080093}
94
95#if ADB_HOST
96struct state_info {
Elliott Hughes3aec2ba2015-05-05 13:10:43 -070097 TransportType transport_type;
Leo Sartre6cd9bc32015-11-27 18:56:48 +010098 std::string serial;
Josh Gaob39e4152017-08-16 16:57:01 -070099 TransportId transport_id;
Dan Albert9a50f4c2015-05-18 16:43:57 -0700100 ConnectionState state;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800101};
102
Josh Gao4c0c9c92019-04-18 16:46:42 -0700103static void wait_for_state(unique_fd fd, state_info* sinfo) {
Yabin Cui815ad882015-09-02 17:44:28 -0700104 D("wait_for_state %d", sinfo->state);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800105
Elliott Hughes67943d12015-10-07 14:55:10 -0700106 while (true) {
107 bool is_ambiguous = false;
108 std::string error = "unknown error";
Yi Kong86e67182018-07-13 18:15:16 -0700109 const char* serial = sinfo->serial.length() ? sinfo->serial.c_str() : nullptr;
Josh Gaob39e4152017-08-16 16:57:01 -0700110 atransport* t = acquire_one_transport(sinfo->transport_type, serial, sinfo->transport_id,
111 &is_ambiguous, &error);
Josh Gaob32f5ac2019-02-22 14:01:36 -0800112 if (sinfo->state == kCsOffline) {
113 // wait-for-disconnect uses kCsOffline, we don't actually want to wait for 'offline'.
114 if (t == nullptr) {
115 SendOkay(fd);
116 break;
117 }
118 } else if (t != nullptr &&
119 (sinfo->state == kCsAny || sinfo->state == t->GetConnectionState())) {
Elliott Hughes67943d12015-10-07 14:55:10 -0700120 SendOkay(fd);
121 break;
Josh Gaob32f5ac2019-02-22 14:01:36 -0800122 }
123
124 if (!is_ambiguous) {
Josh Gao4c0c9c92019-04-18 16:46:42 -0700125 adb_pollfd pfd = {.fd = fd.get(), .events = POLLIN};
Josh Gaob32f5ac2019-02-22 14:01:36 -0800126 int rc = adb_poll(&pfd, 1, 100);
Josh Gaoc1fab362016-02-19 10:42:40 -0800127 if (rc < 0) {
128 SendFail(fd, error);
129 break;
130 } else if (rc > 0 && (pfd.revents & POLLHUP) != 0) {
131 // The other end of the socket is closed, probably because the other side was
132 // terminated, bail out.
133 break;
134 }
135
Elliott Hughes67943d12015-10-07 14:55:10 -0700136 // Try again...
137 } else {
138 SendFail(fd, error);
139 break;
140 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800141 }
142
Yabin Cui815ad882015-09-02 17:44:28 -0700143 D("wait_for_state is done");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800144}
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700145
Elliott Hughes88b4c852015-04-30 17:32:03 -0700146void connect_emulator(const std::string& port_spec, std::string* response) {
147 std::vector<std::string> pieces = android::base::Split(port_spec, ",");
148 if (pieces.size() != 2) {
149 *response = android::base::StringPrintf("unable to parse '%s' as <console port>,<adb port>",
150 port_spec.c_str());
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700151 return;
152 }
153
Yi Kong86e67182018-07-13 18:15:16 -0700154 int console_port = strtol(pieces[0].c_str(), nullptr, 0);
155 int adb_port = strtol(pieces[1].c_str(), nullptr, 0);
Elliott Hughes88b4c852015-04-30 17:32:03 -0700156 if (console_port <= 0 || adb_port <= 0) {
157 *response = android::base::StringPrintf("Invalid port numbers: %s", port_spec.c_str());
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700158 return;
159 }
160
Elliott Hughes88b4c852015-04-30 17:32:03 -0700161 // Check if the emulator is already known.
162 // Note: There's a small but harmless race condition here: An emulator not
163 // present just yet could be registered by another invocation right
164 // after doing this check here. However, local_connect protects
165 // against double-registration too. From here, a better error message
166 // can be produced. In the case of the race condition, the very specific
167 // error message won't be shown, but the data doesn't get corrupted.
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700168 atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port);
Elliott Hughes88b4c852015-04-30 17:32:03 -0700169 if (known_emulator != nullptr) {
170 *response = android::base::StringPrintf("Emulator already registered on port %d", adb_port);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700171 return;
172 }
173
Elliott Hughes88b4c852015-04-30 17:32:03 -0700174 // Preconditions met, try to connect to the emulator.
Elliott Hughes43df1092015-07-23 17:12:58 -0700175 std::string error;
176 if (!local_connect_arbitrary_ports(console_port, adb_port, &error)) {
Elliott Hughes88b4c852015-04-30 17:32:03 -0700177 *response = android::base::StringPrintf("Connected to emulator on ports %d,%d",
178 console_port, adb_port);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700179 } else {
Elliott Hughes43df1092015-07-23 17:12:58 -0700180 *response = android::base::StringPrintf("Could not connect to emulator on ports %d,%d: %s",
181 console_port, adb_port, error.c_str());
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700182 }
183}
184
Josh Gao4c28dde2018-07-25 16:51:59 -0700185static void connect_service(unique_fd fd, std::string host) {
Elliott Hughes88b4c852015-04-30 17:32:03 -0700186 std::string response;
Luis Hector Chavezce7a2842018-07-18 19:40:12 -0700187 if (!strncmp(host.c_str(), "emu:", 4)) {
188 connect_emulator(host.c_str() + 4, &response);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700189 } else {
Greg Kaisere2f32342019-03-26 11:58:53 -0700190 connect_device(host, &response);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700191 }
192
193 // Send response for emulator and device
Luis Hector Chavezce7a2842018-07-18 19:40:12 -0700194 SendProtocolString(fd.get(), response);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700195}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800196#endif
197
198#if ADB_HOST
Josh Gao0565ae02019-02-22 13:41:55 -0800199asocket* host_service_to_socket(std::string_view name, std::string_view serial,
200 TransportId transport_id) {
201 if (name == "track-devices") {
Josh Gao32124632017-08-14 18:57:54 -0700202 return create_device_tracker(false);
Josh Gao0565ae02019-02-22 13:41:55 -0800203 } else if (name == "track-devices-l") {
Josh Gao32124632017-08-14 18:57:54 -0700204 return create_device_tracker(true);
Josh Gao0565ae02019-02-22 13:41:55 -0800205 } else if (ConsumePrefix(&name, "wait-for-")) {
Josh Gaoc2705962019-01-23 15:36:42 -0800206 std::shared_ptr<state_info> sinfo = std::make_shared<state_info>();
Leo Sartre6cd9bc32015-11-27 18:56:48 +0100207 if (sinfo == nullptr) {
208 fprintf(stderr, "couldn't allocate state_info: %s", strerror(errno));
209 return nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800210 }
211
Josh Gao0565ae02019-02-22 13:41:55 -0800212 sinfo->serial = serial;
Josh Gaob39e4152017-08-16 16:57:01 -0700213 sinfo->transport_id = transport_id;
Leo Sartre6cd9bc32015-11-27 18:56:48 +0100214
Josh Gao0565ae02019-02-22 13:41:55 -0800215 if (ConsumePrefix(&name, "local")) {
Leo Sartre6cd9bc32015-11-27 18:56:48 +0100216 sinfo->transport_type = kTransportLocal;
Josh Gao0565ae02019-02-22 13:41:55 -0800217 } else if (ConsumePrefix(&name, "usb")) {
Leo Sartre6cd9bc32015-11-27 18:56:48 +0100218 sinfo->transport_type = kTransportUsb;
Josh Gao0565ae02019-02-22 13:41:55 -0800219 } else if (ConsumePrefix(&name, "any")) {
Leo Sartre6cd9bc32015-11-27 18:56:48 +0100220 sinfo->transport_type = kTransportAny;
221 } else {
222 return nullptr;
223 }
224
Josh Gao0565ae02019-02-22 13:41:55 -0800225 if (name == "-device") {
Leo Sartre6cd9bc32015-11-27 18:56:48 +0100226 sinfo->state = kCsDevice;
Josh Gao0565ae02019-02-22 13:41:55 -0800227 } else if (name == "-recovery") {
Leo Sartre6cd9bc32015-11-27 18:56:48 +0100228 sinfo->state = kCsRecovery;
Tao Bao9d6eca52019-04-07 23:24:03 -0700229 } else if (name == "-rescue") {
230 sinfo->state = kCsRescue;
Josh Gao0565ae02019-02-22 13:41:55 -0800231 } else if (name == "-sideload") {
Leo Sartre6cd9bc32015-11-27 18:56:48 +0100232 sinfo->state = kCsSideload;
Josh Gao0565ae02019-02-22 13:41:55 -0800233 } else if (name == "-bootloader") {
Leo Sartre6cd9bc32015-11-27 18:56:48 +0100234 sinfo->state = kCsBootloader;
Josh Gao0565ae02019-02-22 13:41:55 -0800235 } else if (name == "-any") {
Josh Gao071328d2016-04-13 12:18:58 -0700236 sinfo->state = kCsAny;
Josh Gaob32f5ac2019-02-22 14:01:36 -0800237 } else if (name == "-disconnect") {
238 sinfo->state = kCsOffline;
Leo Sartre6cd9bc32015-11-27 18:56:48 +0100239 } else {
240 return nullptr;
241 }
242
Josh Gao4c0c9c92019-04-18 16:46:42 -0700243 unique_fd fd = create_service_thread(
244 "wait", [sinfo](unique_fd fd) { wait_for_state(std::move(fd), sinfo.get()); });
Josh Gaoc2705962019-01-23 15:36:42 -0800245 return create_local_socket(std::move(fd));
Josh Gao0565ae02019-02-22 13:41:55 -0800246 } else if (ConsumePrefix(&name, "connect:")) {
247 std::string host(name);
Josh Gaoc2705962019-01-23 15:36:42 -0800248 unique_fd fd = create_service_thread(
249 "connect", std::bind(connect_service, std::placeholders::_1, host));
250 return create_local_socket(std::move(fd));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800251 }
Yi Kong86e67182018-07-13 18:15:16 -0700252 return nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800253}
254#endif /* ADB_HOST */