2009-03-04 04:32:55 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-09-23 00:52:57 +02:00
|
|
|
#define TRACE_TAG SERVICES
|
2015-03-19 23:21:08 +01:00
|
|
|
|
|
|
|
#include "sysdeps.h"
|
|
|
|
|
2015-02-25 00:51:19 +01:00
|
|
|
#include <errno.h>
|
2013-03-30 02:22:36 +01:00
|
|
|
#include <stddef.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <stdio.h>
|
2015-02-25 00:51:19 +01:00
|
|
|
#include <stdlib.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <string.h>
|
2015-02-25 00:51:19 +01:00
|
|
|
|
2019-11-20 23:18:43 +01:00
|
|
|
#include <cstring>
|
2017-04-13 02:00:49 +02:00
|
|
|
#include <thread>
|
2019-02-22 22:41:55 +01:00
|
|
|
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
|
|
|
#include <android-base/strings.h>
|
2015-07-24 02:12:58 +02:00
|
|
|
#include <cutils/sockets.h>
|
2015-04-18 05:11:08 +02:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#include "adb.h"
|
2015-02-25 06:26:58 +01:00
|
|
|
#include "adb_io.h"
|
2018-07-26 01:51:59 +02:00
|
|
|
#include "adb_unique_fd.h"
|
2015-07-18 21:21:30 +02:00
|
|
|
#include "adb_utils.h"
|
2019-11-20 23:18:43 +01:00
|
|
|
#include "adb_wifi.h"
|
2015-09-30 22:35:42 +02:00
|
|
|
#include "services.h"
|
2016-08-25 03:38:44 +02:00
|
|
|
#include "socket_spec.h"
|
2016-02-19 19:42:40 +01:00
|
|
|
#include "sysdeps.h"
|
2015-02-25 00:51:19 +01:00
|
|
|
#include "transport.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-07-19 04:40:12 +02:00
|
|
|
namespace {
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-07-26 01:51:59 +02:00
|
|
|
void service_bootstrap_func(std::string service_name, std::function<void(unique_fd)> func,
|
|
|
|
unique_fd fd) {
|
2018-07-19 04:40:12 +02:00
|
|
|
adb_thread_setname(android::base::StringPrintf("%s svc %d", service_name.c_str(), fd.get()));
|
|
|
|
func(std::move(fd));
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2018-07-26 03:15:52 +02:00
|
|
|
} // namespace
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-07-26 01:51:59 +02:00
|
|
|
unique_fd create_service_thread(const char* service_name, std::function<void(unique_fd)> func) {
|
2009-03-04 04:32:55 +01:00
|
|
|
int s[2];
|
2015-02-26 02:51:28 +01:00
|
|
|
if (adb_socketpair(s)) {
|
2009-03-04 04:32:55 +01:00
|
|
|
printf("cannot create service socket pair\n");
|
2018-07-26 01:51:59 +02:00
|
|
|
return unique_fd();
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-09-03 02:44:28 +02:00
|
|
|
D("socketpair: (%d,%d)", s[0], s[1]);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-11 02:45:27 +01:00
|
|
|
#if !ADB_HOST
|
2018-07-19 04:40:12 +02:00
|
|
|
if (strcmp(service_name, "sync") == 0) {
|
2017-02-11 02:45:27 +01:00
|
|
|
// Set file sync service socket to maximum size
|
|
|
|
int max_buf = LINUX_MAX_SOCKET_SIZE;
|
|
|
|
adb_setsockopt(s[0], SOL_SOCKET, SO_SNDBUF, &max_buf, sizeof(max_buf));
|
|
|
|
adb_setsockopt(s[1], SOL_SOCKET, SO_SNDBUF, &max_buf, sizeof(max_buf));
|
|
|
|
}
|
2019-02-22 22:41:55 +01:00
|
|
|
#endif // !ADB_HOST
|
2017-02-11 02:45:27 +01:00
|
|
|
|
2018-07-26 01:51:59 +02:00
|
|
|
std::thread(service_bootstrap_func, service_name, func, unique_fd(s[1])).detach();
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-02-22 22:41:55 +01:00
|
|
|
D("service thread started, %d:%d", s[0], s[1]);
|
2018-07-26 01:51:59 +02:00
|
|
|
return unique_fd(s[0]);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2019-01-24 00:36:42 +01:00
|
|
|
unique_fd service_to_fd(std::string_view name, atransport* transport) {
|
2019-01-02 23:17:29 +01:00
|
|
|
unique_fd ret;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-08-25 03:38:44 +02:00
|
|
|
if (is_socket_spec(name)) {
|
|
|
|
std::string error;
|
2019-01-02 23:17:29 +01:00
|
|
|
if (!socket_spec_connect(&ret, name, nullptr, nullptr, &error)) {
|
2016-08-25 03:38:44 +02:00
|
|
|
LOG(ERROR) << "failed to connect to socket '" << name << "': " << error;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2018-07-26 03:15:52 +02:00
|
|
|
} else {
|
2013-02-21 00:04:53 +01:00
|
|
|
#if !ADB_HOST
|
2019-01-02 23:17:29 +01:00
|
|
|
ret = daemon_service_to_fd(name, transport);
|
2009-03-04 04:32:55 +01:00
|
|
|
#endif
|
|
|
|
}
|
2018-07-26 03:15:52 +02:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
if (ret >= 0) {
|
2019-01-24 00:36:42 +01:00
|
|
|
close_on_exec(ret.get());
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2019-01-24 00:36:42 +01:00
|
|
|
return ret;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if ADB_HOST
|
|
|
|
struct state_info {
|
2015-05-05 22:10:43 +02:00
|
|
|
TransportType transport_type;
|
2015-11-27 18:56:48 +01:00
|
|
|
std::string serial;
|
2017-08-17 01:57:01 +02:00
|
|
|
TransportId transport_id;
|
2015-05-19 01:43:57 +02:00
|
|
|
ConnectionState state;
|
2009-03-04 04:32:55 +01:00
|
|
|
};
|
|
|
|
|
2019-04-19 01:46:42 +02:00
|
|
|
static void wait_for_state(unique_fd fd, state_info* sinfo) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("wait_for_state %d", sinfo->state);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-10-07 23:55:10 +02:00
|
|
|
while (true) {
|
|
|
|
bool is_ambiguous = false;
|
|
|
|
std::string error = "unknown error";
|
2018-07-14 03:15:16 +02:00
|
|
|
const char* serial = sinfo->serial.length() ? sinfo->serial.c_str() : nullptr;
|
2017-08-17 01:57:01 +02:00
|
|
|
atransport* t = acquire_one_transport(sinfo->transport_type, serial, sinfo->transport_id,
|
|
|
|
&is_ambiguous, &error);
|
2019-02-22 23:01:36 +01:00
|
|
|
if (sinfo->state == kCsOffline) {
|
|
|
|
// wait-for-disconnect uses kCsOffline, we don't actually want to wait for 'offline'.
|
|
|
|
if (t == nullptr) {
|
|
|
|
SendOkay(fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (t != nullptr &&
|
|
|
|
(sinfo->state == kCsAny || sinfo->state == t->GetConnectionState())) {
|
2015-10-07 23:55:10 +02:00
|
|
|
SendOkay(fd);
|
|
|
|
break;
|
2019-02-22 23:01:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_ambiguous) {
|
2019-04-19 01:46:42 +02:00
|
|
|
adb_pollfd pfd = {.fd = fd.get(), .events = POLLIN};
|
2019-02-22 23:01:36 +01:00
|
|
|
int rc = adb_poll(&pfd, 1, 100);
|
2016-02-19 19:42:40 +01:00
|
|
|
if (rc < 0) {
|
|
|
|
SendFail(fd, error);
|
|
|
|
break;
|
|
|
|
} else if (rc > 0 && (pfd.revents & POLLHUP) != 0) {
|
|
|
|
// The other end of the socket is closed, probably because the other side was
|
|
|
|
// terminated, bail out.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-10-07 23:55:10 +02:00
|
|
|
// Try again...
|
|
|
|
} else {
|
|
|
|
SendFail(fd, error);
|
|
|
|
break;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("wait_for_state is done");
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2013-03-30 02:22:36 +01:00
|
|
|
|
2015-05-01 02:32:03 +02:00
|
|
|
void connect_emulator(const std::string& port_spec, std::string* response) {
|
|
|
|
std::vector<std::string> pieces = android::base::Split(port_spec, ",");
|
|
|
|
if (pieces.size() != 2) {
|
|
|
|
*response = android::base::StringPrintf("unable to parse '%s' as <console port>,<adb port>",
|
|
|
|
port_spec.c_str());
|
2013-03-30 02:22:36 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-14 03:15:16 +02:00
|
|
|
int console_port = strtol(pieces[0].c_str(), nullptr, 0);
|
|
|
|
int adb_port = strtol(pieces[1].c_str(), nullptr, 0);
|
2015-05-01 02:32:03 +02:00
|
|
|
if (console_port <= 0 || adb_port <= 0) {
|
|
|
|
*response = android::base::StringPrintf("Invalid port numbers: %s", port_spec.c_str());
|
2013-03-30 02:22:36 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-01 02:32:03 +02:00
|
|
|
// Check if the emulator is already known.
|
|
|
|
// Note: There's a small but harmless race condition here: An emulator not
|
|
|
|
// present just yet could be registered by another invocation right
|
|
|
|
// after doing this check here. However, local_connect protects
|
|
|
|
// against double-registration too. From here, a better error message
|
|
|
|
// can be produced. In the case of the race condition, the very specific
|
|
|
|
// error message won't be shown, but the data doesn't get corrupted.
|
2013-03-30 02:22:36 +01:00
|
|
|
atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port);
|
2015-05-01 02:32:03 +02:00
|
|
|
if (known_emulator != nullptr) {
|
|
|
|
*response = android::base::StringPrintf("Emulator already registered on port %d", adb_port);
|
2013-03-30 02:22:36 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-01 02:32:03 +02:00
|
|
|
// Preconditions met, try to connect to the emulator.
|
2015-07-24 02:12:58 +02:00
|
|
|
std::string error;
|
|
|
|
if (!local_connect_arbitrary_ports(console_port, adb_port, &error)) {
|
2015-05-01 02:32:03 +02:00
|
|
|
*response = android::base::StringPrintf("Connected to emulator on ports %d,%d",
|
|
|
|
console_port, adb_port);
|
2013-03-30 02:22:36 +01:00
|
|
|
} else {
|
2015-07-24 02:12:58 +02:00
|
|
|
*response = android::base::StringPrintf("Could not connect to emulator on ports %d,%d: %s",
|
|
|
|
console_port, adb_port, error.c_str());
|
2013-03-30 02:22:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-26 01:51:59 +02:00
|
|
|
static void connect_service(unique_fd fd, std::string host) {
|
2015-05-01 02:32:03 +02:00
|
|
|
std::string response;
|
2018-07-19 04:40:12 +02:00
|
|
|
if (!strncmp(host.c_str(), "emu:", 4)) {
|
|
|
|
connect_emulator(host.c_str() + 4, &response);
|
2013-03-30 02:22:36 +01:00
|
|
|
} else {
|
2019-03-26 19:58:53 +01:00
|
|
|
connect_device(host, &response);
|
2013-03-30 02:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send response for emulator and device
|
2018-07-19 04:40:12 +02:00
|
|
|
SendProtocolString(fd.get(), response);
|
2013-03-30 02:22:36 +01:00
|
|
|
}
|
2019-11-20 23:18:43 +01:00
|
|
|
|
|
|
|
static void pair_service(unique_fd fd, std::string host, std::string password) {
|
|
|
|
std::string response;
|
|
|
|
adb_wifi_pair_device(host, password, response);
|
|
|
|
SendProtocolString(fd.get(), response);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ADB_HOST
|
2019-02-22 22:41:55 +01:00
|
|
|
asocket* host_service_to_socket(std::string_view name, std::string_view serial,
|
|
|
|
TransportId transport_id) {
|
|
|
|
if (name == "track-devices") {
|
2017-08-15 03:57:54 +02:00
|
|
|
return create_device_tracker(false);
|
2019-02-22 22:41:55 +01:00
|
|
|
} else if (name == "track-devices-l") {
|
2017-08-15 03:57:54 +02:00
|
|
|
return create_device_tracker(true);
|
2019-05-03 18:02:45 +02:00
|
|
|
} else if (android::base::ConsumePrefix(&name, "wait-for-")) {
|
2019-01-24 00:36:42 +01:00
|
|
|
std::shared_ptr<state_info> sinfo = std::make_shared<state_info>();
|
2015-04-22 04:39:52 +02:00
|
|
|
if (sinfo == nullptr) {
|
|
|
|
fprintf(stderr, "couldn't allocate state_info: %s", strerror(errno));
|
2015-11-27 18:56:48 +01:00
|
|
|
return nullptr;
|
2015-04-22 04:39:52 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-02-22 22:41:55 +01:00
|
|
|
sinfo->serial = serial;
|
2017-08-17 01:57:01 +02:00
|
|
|
sinfo->transport_id = transport_id;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-05-03 18:02:45 +02:00
|
|
|
if (android::base::ConsumePrefix(&name, "local")) {
|
2015-05-05 22:10:43 +02:00
|
|
|
sinfo->transport_type = kTransportLocal;
|
2019-05-03 18:02:45 +02:00
|
|
|
} else if (android::base::ConsumePrefix(&name, "usb")) {
|
2015-05-05 22:10:43 +02:00
|
|
|
sinfo->transport_type = kTransportUsb;
|
2019-05-03 18:02:45 +02:00
|
|
|
} else if (android::base::ConsumePrefix(&name, "any")) {
|
2015-05-05 22:10:43 +02:00
|
|
|
sinfo->transport_type = kTransportAny;
|
2015-11-27 18:56:48 +01:00
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-02-22 22:41:55 +01:00
|
|
|
if (name == "-device") {
|
2015-05-19 01:43:57 +02:00
|
|
|
sinfo->state = kCsDevice;
|
2019-02-22 22:41:55 +01:00
|
|
|
} else if (name == "-recovery") {
|
2015-11-27 18:56:48 +01:00
|
|
|
sinfo->state = kCsRecovery;
|
2019-04-08 08:24:03 +02:00
|
|
|
} else if (name == "-rescue") {
|
|
|
|
sinfo->state = kCsRescue;
|
2019-02-22 22:41:55 +01:00
|
|
|
} else if (name == "-sideload") {
|
2015-11-27 18:56:48 +01:00
|
|
|
sinfo->state = kCsSideload;
|
2019-02-22 22:41:55 +01:00
|
|
|
} else if (name == "-bootloader") {
|
2015-11-27 18:56:48 +01:00
|
|
|
sinfo->state = kCsBootloader;
|
2019-02-22 22:41:55 +01:00
|
|
|
} else if (name == "-any") {
|
2016-04-13 21:18:58 +02:00
|
|
|
sinfo->state = kCsAny;
|
2019-02-22 23:01:36 +01:00
|
|
|
} else if (name == "-disconnect") {
|
|
|
|
sinfo->state = kCsOffline;
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
2015-11-27 18:56:48 +01:00
|
|
|
return nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2019-04-19 01:46:42 +02:00
|
|
|
unique_fd fd = create_service_thread(
|
|
|
|
"wait", [sinfo](unique_fd fd) { wait_for_state(std::move(fd), sinfo.get()); });
|
2019-01-24 00:36:42 +01:00
|
|
|
return create_local_socket(std::move(fd));
|
2019-05-03 18:02:45 +02:00
|
|
|
} else if (android::base::ConsumePrefix(&name, "connect:")) {
|
2019-02-22 22:41:55 +01:00
|
|
|
std::string host(name);
|
2019-01-24 00:36:42 +01:00
|
|
|
unique_fd fd = create_service_thread(
|
|
|
|
"connect", std::bind(connect_service, std::placeholders::_1, host));
|
|
|
|
return create_local_socket(std::move(fd));
|
2019-11-20 23:18:43 +01:00
|
|
|
} else if (android::base::ConsumePrefix(&name, "pair:")) {
|
|
|
|
const char* divider = strchr(name.data(), ':');
|
|
|
|
if (!divider) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
std::string password(name.data(), divider);
|
|
|
|
std::string host(divider + 1);
|
|
|
|
unique_fd fd = create_service_thread(
|
|
|
|
"pair", std::bind(pair_service, std::placeholders::_1, host, password));
|
|
|
|
return create_local_socket(std::move(fd));
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2018-07-14 03:15:16 +02:00
|
|
|
return nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
#endif /* ADB_HOST */
|