blob: 73fed0932446a909aca66120533dd70fb5296ef9 [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>
Elliott Hughes4f713192015-12-04 22:00:26 -080028#include <android-base/stringprintf.h>
29#include <android-base/strings.h>
Elliott Hughes381cfa92015-07-23 17:12:58 -070030#include <cutils/sockets.h>
Elliott Hughes6c34bba2015-04-17 20:11:08 -070031
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032#include "adb.h"
Dan Albertcc731cc2015-02-24 21:26:58 -080033#include "adb_io.h"
Josh Gao73a5ee42018-07-25 16:51:59 -070034#include "adb_unique_fd.h"
Elliott Hughes3d5f60d2015-07-18 12:21:30 -070035#include "adb_utils.h"
David Pursell70ef7b42015-09-30 13:35:42 -070036#include "services.h"
Josh Gaocfb21412016-08-24 18:38:44 -070037#include "socket_spec.h"
Josh Gao09855472016-02-19 10:42:40 -080038#include "sysdeps.h"
Dan Albert76649012015-02-24 15:51:19 -080039#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040
Luis Hector Chavez095792c2018-07-18 19:40:12 -070041namespace {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
Josh Gao73a5ee42018-07-25 16:51:59 -070043void service_bootstrap_func(std::string service_name, std::function<void(unique_fd)> func,
44 unique_fd fd) {
Luis Hector Chavez095792c2018-07-18 19:40:12 -070045 adb_thread_setname(android::base::StringPrintf("%s svc %d", service_name.c_str(), fd.get()));
46 func(std::move(fd));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047}
48
Josh Gao997cfac2018-07-25 18:15:52 -070049} // namespace
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050
Josh Gao73a5ee42018-07-25 16:51:59 -070051unique_fd create_service_thread(const char* service_name, std::function<void(unique_fd)> func) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052 int s[2];
Dan Albertbac34742015-02-25 17:51:28 -080053 if (adb_socketpair(s)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054 printf("cannot create service socket pair\n");
Josh Gao73a5ee42018-07-25 16:51:59 -070055 return unique_fd();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -070057 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058
Jerry Zhang2f8c60b2017-02-10 17:45:27 -080059#if !ADB_HOST
Luis Hector Chavez095792c2018-07-18 19:40:12 -070060 if (strcmp(service_name, "sync") == 0) {
Jerry Zhang2f8c60b2017-02-10 17:45:27 -080061 // 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 Gao73a5ee42018-07-25 16:51:59 -070068 std::thread(service_bootstrap_func, service_name, func, unique_fd(s[1])).detach();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069
Yabin Cui7a3f8d62015-09-02 17:44:28 -070070 D("service thread started, %d:%d",s[0], s[1]);
Josh Gao73a5ee42018-07-25 16:51:59 -070071 return unique_fd(s[0]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072}
73
Josh Gaod19b77a2018-12-13 14:21:00 -080074int service_to_fd(std::string_view name, atransport* transport) {
Cody Schuffelenaf0e2202019-01-02 14:17:29 -080075 unique_fd ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076
Josh Gaocfb21412016-08-24 18:38:44 -070077 if (is_socket_spec(name)) {
78 std::string error;
Cody Schuffelenaf0e2202019-01-02 14:17:29 -080079 if (!socket_spec_connect(&ret, name, nullptr, nullptr, &error)) {
Josh Gaocfb21412016-08-24 18:38:44 -070080 LOG(ERROR) << "failed to connect to socket '" << name << "': " << error;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081 }
Josh Gao997cfac2018-07-25 18:15:52 -070082 } else {
Benoit Goby9470c2f2013-02-20 15:04:53 -080083#if !ADB_HOST
Cody Schuffelenaf0e2202019-01-02 14:17:29 -080084 ret = daemon_service_to_fd(name, transport);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080086 }
Josh Gao997cfac2018-07-25 18:15:52 -070087
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088 if (ret >= 0) {
89 close_on_exec(ret);
90 }
Cody Schuffelenaf0e2202019-01-02 14:17:29 -080091 return ret.release();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080092}
93
94#if ADB_HOST
95struct state_info {
Elliott Hughes3bd73c12015-05-05 13:10:43 -070096 TransportType transport_type;
Leo Sartre1fbc9db2015-11-27 18:56:48 +010097 std::string serial;
Josh Gaob122b172017-08-16 16:57:01 -070098 TransportId transport_id;
Dan Albertdcd78a12015-05-18 16:43:57 -070099 ConnectionState state;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100};
101
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100102static void wait_for_state(int fd, void* data) {
103 std::unique_ptr<state_info> sinfo(reinterpret_cast<state_info*>(data));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700105 D("wait_for_state %d", sinfo->state);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106
Elliott Hughes8d28e192015-10-07 14:55:10 -0700107 while (true) {
108 bool is_ambiguous = false;
109 std::string error = "unknown error";
Yi Kongaed415c2018-07-13 18:15:16 -0700110 const char* serial = sinfo->serial.length() ? sinfo->serial.c_str() : nullptr;
Josh Gaob122b172017-08-16 16:57:01 -0700111 atransport* t = acquire_one_transport(sinfo->transport_type, serial, sinfo->transport_id,
112 &is_ambiguous, &error);
Yabin Cuib5e11412017-03-10 16:01:01 -0800113 if (t != nullptr && (sinfo->state == kCsAny || sinfo->state == t->GetConnectionState())) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700114 SendOkay(fd);
115 break;
116 } else if (!is_ambiguous) {
Josh Gao09855472016-02-19 10:42:40 -0800117 adb_pollfd pfd = {.fd = fd, .events = POLLIN };
118 int rc = adb_poll(&pfd, 1, 1000);
119 if (rc < 0) {
120 SendFail(fd, error);
121 break;
122 } else if (rc > 0 && (pfd.revents & POLLHUP) != 0) {
123 // The other end of the socket is closed, probably because the other side was
124 // terminated, bail out.
125 break;
126 }
127
Elliott Hughes8d28e192015-10-07 14:55:10 -0700128 // Try again...
129 } else {
130 SendFail(fd, error);
131 break;
132 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800133 }
134
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135 adb_close(fd);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700136 D("wait_for_state is done");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137}
Benoit Goby1c45ee92013-03-29 18:22:36 -0700138
Elliott Hughese67f1f82015-04-30 17:32:03 -0700139void connect_emulator(const std::string& port_spec, std::string* response) {
140 std::vector<std::string> pieces = android::base::Split(port_spec, ",");
141 if (pieces.size() != 2) {
142 *response = android::base::StringPrintf("unable to parse '%s' as <console port>,<adb port>",
143 port_spec.c_str());
Benoit Goby1c45ee92013-03-29 18:22:36 -0700144 return;
145 }
146
Yi Kongaed415c2018-07-13 18:15:16 -0700147 int console_port = strtol(pieces[0].c_str(), nullptr, 0);
148 int adb_port = strtol(pieces[1].c_str(), nullptr, 0);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700149 if (console_port <= 0 || adb_port <= 0) {
150 *response = android::base::StringPrintf("Invalid port numbers: %s", port_spec.c_str());
Benoit Goby1c45ee92013-03-29 18:22:36 -0700151 return;
152 }
153
Elliott Hughese67f1f82015-04-30 17:32:03 -0700154 // Check if the emulator is already known.
155 // Note: There's a small but harmless race condition here: An emulator not
156 // present just yet could be registered by another invocation right
157 // after doing this check here. However, local_connect protects
158 // against double-registration too. From here, a better error message
159 // can be produced. In the case of the race condition, the very specific
160 // error message won't be shown, but the data doesn't get corrupted.
Benoit Goby1c45ee92013-03-29 18:22:36 -0700161 atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700162 if (known_emulator != nullptr) {
163 *response = android::base::StringPrintf("Emulator already registered on port %d", adb_port);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700164 return;
165 }
166
Elliott Hughese67f1f82015-04-30 17:32:03 -0700167 // Preconditions met, try to connect to the emulator.
Elliott Hughes381cfa92015-07-23 17:12:58 -0700168 std::string error;
169 if (!local_connect_arbitrary_ports(console_port, adb_port, &error)) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700170 *response = android::base::StringPrintf("Connected to emulator on ports %d,%d",
171 console_port, adb_port);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700172 } else {
Elliott Hughes381cfa92015-07-23 17:12:58 -0700173 *response = android::base::StringPrintf("Could not connect to emulator on ports %d,%d: %s",
174 console_port, adb_port, error.c_str());
Benoit Goby1c45ee92013-03-29 18:22:36 -0700175 }
176}
177
Josh Gao73a5ee42018-07-25 16:51:59 -0700178static void connect_service(unique_fd fd, std::string host) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700179 std::string response;
Luis Hector Chavez095792c2018-07-18 19:40:12 -0700180 if (!strncmp(host.c_str(), "emu:", 4)) {
181 connect_emulator(host.c_str() + 4, &response);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700182 } else {
Luis Hector Chavez095792c2018-07-18 19:40:12 -0700183 connect_device(host.c_str(), &response);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700184 }
185
186 // Send response for emulator and device
Luis Hector Chavez095792c2018-07-18 19:40:12 -0700187 SendProtocolString(fd.get(), response);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700188}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189#endif
190
191#if ADB_HOST
Josh Gaob122b172017-08-16 16:57:01 -0700192asocket* host_service_to_socket(const char* name, const char* serial, TransportId transport_id) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193 if (!strcmp(name,"track-devices")) {
Josh Gaob0c18022017-08-14 18:57:54 -0700194 return create_device_tracker(false);
195 } else if (!strcmp(name, "track-devices-l")) {
196 return create_device_tracker(true);
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100197 } else if (android::base::StartsWith(name, "wait-for-")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198 name += strlen("wait-for-");
199
Luis Hector Chavez095792c2018-07-18 19:40:12 -0700200 std::unique_ptr<state_info> sinfo = std::make_unique<state_info>();
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100201 if (sinfo == nullptr) {
202 fprintf(stderr, "couldn't allocate state_info: %s", strerror(errno));
203 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204 }
205
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100206 if (serial) sinfo->serial = serial;
Josh Gaob122b172017-08-16 16:57:01 -0700207 sinfo->transport_id = transport_id;
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100208
209 if (android::base::StartsWith(name, "local")) {
210 name += strlen("local");
211 sinfo->transport_type = kTransportLocal;
212 } else if (android::base::StartsWith(name, "usb")) {
213 name += strlen("usb");
214 sinfo->transport_type = kTransportUsb;
215 } else if (android::base::StartsWith(name, "any")) {
216 name += strlen("any");
217 sinfo->transport_type = kTransportAny;
218 } else {
219 return nullptr;
220 }
221
222 if (!strcmp(name, "-device")) {
223 sinfo->state = kCsDevice;
224 } else if (!strcmp(name, "-recovery")) {
225 sinfo->state = kCsRecovery;
226 } else if (!strcmp(name, "-sideload")) {
227 sinfo->state = kCsSideload;
228 } else if (!strcmp(name, "-bootloader")) {
229 sinfo->state = kCsBootloader;
Josh Gao86441c32016-04-13 12:18:58 -0700230 } else if (!strcmp(name, "-any")) {
231 sinfo->state = kCsAny;
Leo Sartre1fbc9db2015-11-27 18:56:48 +0100232 } else {
233 return nullptr;
234 }
235
Luis Hector Chavez095792c2018-07-18 19:40:12 -0700236 int fd = create_service_thread(
237 "wait", std::bind(wait_for_state, std::placeholders::_1, sinfo.get()))
238 .release();
Ting-Yuan Huangf26cf6d2017-08-15 15:07:21 -0700239 if (fd != -1) {
240 sinfo.release();
241 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 return create_local_socket(fd);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700243 } else if (!strncmp(name, "connect:", 8)) {
Luis Hector Chavez095792c2018-07-18 19:40:12 -0700244 std::string host(name + strlen("connect:"));
245 int fd = create_service_thread("connect",
246 std::bind(connect_service, std::placeholders::_1, host))
247 .release();
Benoit Goby1c45ee92013-03-29 18:22:36 -0700248 return create_local_socket(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249 }
Yi Kongaed415c2018-07-13 18:15:16 -0700250 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251}
252#endif /* ADB_HOST */