blob: 1ed71823e8dc4eeff93d3ec86599172959465f09 [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
Josh Gaoe1dacfc2017-04-12 17:00:49 -070027#include <thread>
Josh Gao0ecc4022019-02-22 13:41:55 -080028
Elliott Hughes4f713192015-12-04 22:00:26 -080029#include <android-base/stringprintf.h>
30#include <android-base/strings.h>
Elliott Hughes381cfa92015-07-23 17:12:58 -070031#include <cutils/sockets.h>
Elliott Hughes6c34bba2015-04-17 20:11:08 -070032
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033#include "adb.h"
Dan Albertcc731cc2015-02-24 21:26:58 -080034#include "adb_io.h"
Josh Gao73a5ee42018-07-25 16:51:59 -070035#include "adb_unique_fd.h"
Elliott Hughes3d5f60d2015-07-18 12:21:30 -070036#include "adb_utils.h"
David Pursell70ef7b42015-09-30 13:35:42 -070037#include "services.h"
Josh Gaocfb21412016-08-24 18:38:44 -070038#include "socket_spec.h"
Josh Gao09855472016-02-19 10:42:40 -080039#include "sysdeps.h"
Dan Albert76649012015-02-24 15:51:19 -080040#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041
Luis Hector Chavez095792c2018-07-18 19:40:12 -070042namespace {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043
Josh Gao73a5ee42018-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 Chavez095792c2018-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 Projectdd7bc332009-03-03 19:32:55 -080048}
49
Josh Gao997cfac2018-07-25 18:15:52 -070050} // namespace
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051
Josh Gao73a5ee42018-07-25 16:51:59 -070052unique_fd create_service_thread(const char* service_name, std::function<void(unique_fd)> func) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053 int s[2];
Dan Albertbac34742015-02-25 17:51:28 -080054 if (adb_socketpair(s)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055 printf("cannot create service socket pair\n");
Josh Gao73a5ee42018-07-25 16:51:59 -070056 return unique_fd();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -070058 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059
Jerry Zhang2f8c60b2017-02-10 17:45:27 -080060#if !ADB_HOST
Luis Hector Chavez095792c2018-07-18 19:40:12 -070061 if (strcmp(service_name, "sync") == 0) {
Jerry Zhang2f8c60b2017-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 Gao0ecc4022019-02-22 13:41:55 -080067#endif // !ADB_HOST
Jerry Zhang2f8c60b2017-02-10 17:45:27 -080068
Josh Gao73a5ee42018-07-25 16:51:59 -070069 std::thread(service_bootstrap_func, service_name, func, unique_fd(s[1])).detach();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070
Josh Gao0ecc4022019-02-22 13:41:55 -080071 D("service thread started, %d:%d", s[0], s[1]);
Josh Gao73a5ee42018-07-25 16:51:59 -070072 return unique_fd(s[0]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073}
74
Josh Gao74ccdf92019-01-23 15:36:42 -080075unique_fd service_to_fd(std::string_view name, atransport* transport) {
Cody Schuffelenaf0e2202019-01-02 14:17:29 -080076 unique_fd ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077
Josh Gaocfb21412016-08-24 18:38:44 -070078 if (is_socket_spec(name)) {
79 std::string error;
Cody Schuffelenaf0e2202019-01-02 14:17:29 -080080 if (!socket_spec_connect(&ret, name, nullptr, nullptr, &error)) {
Josh Gaocfb21412016-08-24 18:38:44 -070081 LOG(ERROR) << "failed to connect to socket '" << name << "': " << error;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082 }
Josh Gao997cfac2018-07-25 18:15:52 -070083 } else {
Benoit Goby9470c2f2013-02-20 15:04:53 -080084#if !ADB_HOST
Cody Schuffelenaf0e2202019-01-02 14:17:29 -080085 ret = daemon_service_to_fd(name, transport);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080086#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087 }
Josh Gao997cfac2018-07-25 18:15:52 -070088
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089 if (ret >= 0) {
Josh Gao74ccdf92019-01-23 15:36:42 -080090 close_on_exec(ret.get());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091 }
Josh Gao74ccdf92019-01-23 15:36:42 -080092 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093}
94
95#if ADB_HOST
96struct state_info {
Elliott Hughes3bd73c12015-05-05 13:10:43 -070097 TransportType transport_type;
Leo Sartre1fbc9db2015-11-27 18:56:48 +010098 std::string serial;
Josh Gaob122b172017-08-16 16:57:01 -070099 TransportId transport_id;
Dan Albertdcd78a12015-05-18 16:43:57 -0700100 ConnectionState state;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101};
102
Josh Gao74ccdf92019-01-23 15:36:42 -0800103static void wait_for_state(int fd, state_info* sinfo) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700104 D("wait_for_state %d", sinfo->state);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800105
Elliott Hughes8d28e192015-10-07 14:55:10 -0700106 while (true) {
107 bool is_ambiguous = false;
108 std::string error = "unknown error";
Yi Kongaed415c2018-07-13 18:15:16 -0700109 const char* serial = sinfo->serial.length() ? sinfo->serial.c_str() : nullptr;
Josh Gaob122b172017-08-16 16:57:01 -0700110 atransport* t = acquire_one_transport(sinfo->transport_type, serial, sinfo->transport_id,
111 &is_ambiguous, &error);
Yabin Cuib5e11412017-03-10 16:01:01 -0800112 if (t != nullptr && (sinfo->state == kCsAny || sinfo->state == t->GetConnectionState())) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700113 SendOkay(fd);
114 break;
115 } else if (!is_ambiguous) {
Josh Gao0ecc4022019-02-22 13:41:55 -0800116 adb_pollfd pfd = {.fd = fd, .events = POLLIN};
Josh Gao09855472016-02-19 10:42:40 -0800117 int rc = adb_poll(&pfd, 1, 1000);
118 if (rc < 0) {
119 SendFail(fd, error);
120 break;
121 } else if (rc > 0 && (pfd.revents & POLLHUP) != 0) {
122 // The other end of the socket is closed, probably because the other side was
123 // terminated, bail out.
124 break;
125 }
126
Elliott Hughes8d28e192015-10-07 14:55:10 -0700127 // Try again...
128 } else {
129 SendFail(fd, error);
130 break;
131 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132 }
133
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134 adb_close(fd);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700135 D("wait_for_state is done");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136}
Benoit Goby1c45ee92013-03-29 18:22:36 -0700137
Elliott Hughese67f1f82015-04-30 17:32:03 -0700138void connect_emulator(const std::string& port_spec, std::string* response) {
139 std::vector<std::string> pieces = android::base::Split(port_spec, ",");
140 if (pieces.size() != 2) {
141 *response = android::base::StringPrintf("unable to parse '%s' as <console port>,<adb port>",
142 port_spec.c_str());
Benoit Goby1c45ee92013-03-29 18:22:36 -0700143 return;
144 }
145
Yi Kongaed415c2018-07-13 18:15:16 -0700146 int console_port = strtol(pieces[0].c_str(), nullptr, 0);
147 int adb_port = strtol(pieces[1].c_str(), nullptr, 0);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700148 if (console_port <= 0 || adb_port <= 0) {
149 *response = android::base::StringPrintf("Invalid port numbers: %s", port_spec.c_str());
Benoit Goby1c45ee92013-03-29 18:22:36 -0700150 return;
151 }
152
Elliott Hughese67f1f82015-04-30 17:32:03 -0700153 // Check if the emulator is already known.
154 // Note: There's a small but harmless race condition here: An emulator not
155 // present just yet could be registered by another invocation right
156 // after doing this check here. However, local_connect protects
157 // against double-registration too. From here, a better error message
158 // can be produced. In the case of the race condition, the very specific
159 // error message won't be shown, but the data doesn't get corrupted.
Benoit Goby1c45ee92013-03-29 18:22:36 -0700160 atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700161 if (known_emulator != nullptr) {
162 *response = android::base::StringPrintf("Emulator already registered on port %d", adb_port);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700163 return;
164 }
165
Elliott Hughese67f1f82015-04-30 17:32:03 -0700166 // Preconditions met, try to connect to the emulator.
Elliott Hughes381cfa92015-07-23 17:12:58 -0700167 std::string error;
168 if (!local_connect_arbitrary_ports(console_port, adb_port, &error)) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700169 *response = android::base::StringPrintf("Connected to emulator on ports %d,%d",
170 console_port, adb_port);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700171 } else {
Elliott Hughes381cfa92015-07-23 17:12:58 -0700172 *response = android::base::StringPrintf("Could not connect to emulator on ports %d,%d: %s",
173 console_port, adb_port, error.c_str());
Benoit Goby1c45ee92013-03-29 18:22:36 -0700174 }
175}
176
Josh Gao73a5ee42018-07-25 16:51:59 -0700177static void connect_service(unique_fd fd, std::string host) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700178 std::string response;
Luis Hector Chavez095792c2018-07-18 19:40:12 -0700179 if (!strncmp(host.c_str(), "emu:", 4)) {
180 connect_emulator(host.c_str() + 4, &response);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700181 } else {
Luis Hector Chavez095792c2018-07-18 19:40:12 -0700182 connect_device(host.c_str(), &response);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700183 }
184
185 // Send response for emulator and device
Luis Hector Chavez095792c2018-07-18 19:40:12 -0700186 SendProtocolString(fd.get(), response);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700187}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188#endif
189
190#if ADB_HOST
Josh Gao0ecc4022019-02-22 13:41:55 -0800191asocket* host_service_to_socket(std::string_view name, std::string_view serial,
192 TransportId transport_id) {
193 if (name == "track-devices") {
Josh Gaob0c18022017-08-14 18:57:54 -0700194 return create_device_tracker(false);
Josh Gao0ecc4022019-02-22 13:41:55 -0800195 } else if (name == "track-devices-l") {
Josh Gaob0c18022017-08-14 18:57:54 -0700196 return create_device_tracker(true);
Josh Gao0ecc4022019-02-22 13:41:55 -0800197 } else if (ConsumePrefix(&name, "wait-for-")) {
Josh Gao74ccdf92019-01-23 15:36:42 -0800198 std::shared_ptr<state_info> sinfo = std::make_shared<state_info>();
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100199 if (sinfo == nullptr) {
200 fprintf(stderr, "couldn't allocate state_info: %s", strerror(errno));
201 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202 }
203
Josh Gao0ecc4022019-02-22 13:41:55 -0800204 sinfo->serial = serial;
Josh Gaob122b172017-08-16 16:57:01 -0700205 sinfo->transport_id = transport_id;
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100206
Josh Gao0ecc4022019-02-22 13:41:55 -0800207 if (ConsumePrefix(&name, "local")) {
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100208 sinfo->transport_type = kTransportLocal;
Josh Gao0ecc4022019-02-22 13:41:55 -0800209 } else if (ConsumePrefix(&name, "usb")) {
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100210 sinfo->transport_type = kTransportUsb;
Josh Gao0ecc4022019-02-22 13:41:55 -0800211 } else if (ConsumePrefix(&name, "any")) {
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100212 sinfo->transport_type = kTransportAny;
213 } else {
214 return nullptr;
215 }
216
Josh Gao0ecc4022019-02-22 13:41:55 -0800217 if (name == "-device") {
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100218 sinfo->state = kCsDevice;
Josh Gao0ecc4022019-02-22 13:41:55 -0800219 } else if (name == "-recovery") {
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100220 sinfo->state = kCsRecovery;
Josh Gao0ecc4022019-02-22 13:41:55 -0800221 } else if (name == "-sideload") {
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100222 sinfo->state = kCsSideload;
Josh Gao0ecc4022019-02-22 13:41:55 -0800223 } else if (name == "-bootloader") {
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100224 sinfo->state = kCsBootloader;
Josh Gao0ecc4022019-02-22 13:41:55 -0800225 } else if (name == "-any") {
Josh Gao86441c32016-04-13 12:18:58 -0700226 sinfo->state = kCsAny;
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100227 } else {
228 return nullptr;
229 }
230
Josh Gao74ccdf92019-01-23 15:36:42 -0800231 unique_fd fd = create_service_thread("wait", [sinfo](int fd) {
232 wait_for_state(fd, sinfo.get());
233 });
234 return create_local_socket(std::move(fd));
Josh Gao0ecc4022019-02-22 13:41:55 -0800235 } else if (ConsumePrefix(&name, "connect:")) {
236 std::string host(name);
Josh Gao74ccdf92019-01-23 15:36:42 -0800237 unique_fd fd = create_service_thread(
238 "connect", std::bind(connect_service, std::placeholders::_1, host));
239 return create_local_socket(std::move(fd));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240 }
Yi Kongaed415c2018-07-13 18:15:16 -0700241 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242}
243#endif /* ADB_HOST */